summaryrefslogtreecommitdiff
path: root/src/bin/eeze/eeze_udev_test
diff options
context:
space:
mode:
authorMarcel Hollerbach <marcel-hollerbach@t-online.de>2017-02-03 00:04:19 +0100
committerMarcel Hollerbach <marcel-hollerbach@t-online.de>2017-02-03 10:59:04 +0100
commit94ddd709dbae52a729179d8ad7ac4f8b06845ea3 (patch)
tree4c52f7f8a329a59beb3a9e552af4f5d7a92c0043 /src/bin/eeze/eeze_udev_test
parenteab798c753e72eece526ceccf3c9beb441b5f5ff (diff)
eeze: move binaries into subdirectories
in order to move to cmake we are moving every binary into its own subdirectory.
Diffstat (limited to 'src/bin/eeze/eeze_udev_test')
-rw-r--r--src/bin/eeze/eeze_udev_test/eeze_udev_test.c242
1 files changed, 242 insertions, 0 deletions
diff --git a/src/bin/eeze/eeze_udev_test/eeze_udev_test.c b/src/bin/eeze/eeze_udev_test/eeze_udev_test.c
new file mode 100644
index 0000000000..d3ebfd6023
--- /dev/null
+++ b/src/bin/eeze/eeze_udev_test/eeze_udev_test.c
@@ -0,0 +1,242 @@
1#ifdef HAVE_CONFIG_H
2# include <config.h>
3#endif
4
5#include <Eeze.h>
6#include <Ecore.h>
7#include <stdio.h>
8
9/**
10 * This demo program shows how to use some eeze_udev functions. It roughly
11 * 1kb as of now, TODO is to fix this but I'm too lazy now and it's only
12 * a demo.
13 */
14
15typedef struct kbdmouse
16{
17 Eina_List *kbds;
18 Eina_List *mice;
19 Eina_Hash *hash;
20} kbdmouse;
21
22static void
23/* event will always be a syspath starting with /sys */
24catch_events(const char *device,
25 Eeze_Udev_Event event,
26 void *data,
27 Eeze_Udev_Watch *watch)
28{
29 kbdmouse *akbdmouse = data;
30 Eina_List *l;
31 const char *name, *dev, *type;
32
33 /* the device that comes through will be prefixed by "/sys"
34 * but the saved name will not, so we check for the saved name
35 * inside the device name
36 */
37 EINA_LIST_FOREACH(akbdmouse->kbds, l, name)
38 if (!strncmp(device + 5, name, strlen(device + 5) - 8)) goto end;
39 EINA_LIST_FOREACH(akbdmouse->mice, l, name)
40 if (!strncmp(device + 5, name, strlen(device + 5) - 8)) goto end;
41
42 /* check to see if the device was just plugged in */
43 if (eeze_udev_syspath_is_kbd(device) || eeze_udev_syspath_is_mouse(device))
44 goto end;
45 /* if we reach here, the device is neither a keyboard nor a mouse that we saw
46 * previously, so we print a moderately amusing message and bail
47 */
48 printf("Sneaky sneaky! But %s is not a keyboard or a mouse!!\n", device);
49 return;
50
51end:
52 /* we stored the devpaths for all the syspaths previously so that
53 * we can retrieve them now even though the device has been removed and
54 * is inaccessible to udev
55 */
56 if ((event & EEZE_UDEV_EVENT_ADD) == EEZE_UDEV_EVENT_ADD)
57 {
58 dev = eeze_udev_syspath_get_devpath(device);
59 type = "plugged in";
60 }
61 else
62 {
63 dev = eina_hash_find(akbdmouse->hash, name);
64 type = "unplugged";
65 }
66 printf("You %s %s!\n", type, dev);
67 printf("All tests completed, exiting successfully!\n");
68 /* and the hash */
69 eina_hash_free(akbdmouse->hash);
70 /* now we free the lists */
71 eina_list_free(akbdmouse->kbds);
72 eina_list_free(akbdmouse->mice);
73 /* and the random storage struct */
74 free(akbdmouse);
75 /* and delete the watch */
76 eeze_udev_watch_del(watch);
77 /* and shut down eudev */
78 eeze_shutdown();
79 /* and quit the main loop */
80 ecore_main_loop_quit();
81}
82
83static void
84hash_free(void *data)
85{
86 eina_stringshare_del(data);
87}
88
89int
90main()
91{
92 Eina_List *type, *l;
93 const char *name, *check, *check2;
94 kbdmouse *akbdmouse;
95 Eina_Hash *hash;
96
97 ecore_init();
98 eeze_init();
99
100 hash = eina_hash_stringshared_new(hash_free);
101 akbdmouse = malloc(sizeof(kbdmouse));
102 akbdmouse->hash = hash;
103
104 printf("For my first trick, I will find all of your keyboards and return their syspaths.\n");
105 /* find all keyboards using type EEZE_UDEV_TYPE_KEYBOARD */
106 type = eeze_udev_find_by_type(EEZE_UDEV_TYPE_KEYBOARD, NULL);
107 /* add all "link" devices that aren't explicitly found, but are still
108 * part of the device chain
109 */
110 type = eeze_udev_find_unlisted_similar(type);
111 EINA_LIST_FOREACH(type, l, name)
112 {
113 /* add the devpath to the hash for use in the cb later */
114 if ((check = eeze_udev_syspath_get_devpath(name)))
115 eina_hash_direct_add(hash, name, check);
116 printf("Found keyboard: %s\n", name);
117 }
118 /* we save this list for later, because once a device is unplugged it can
119 * no longer be detected by udev, and any related properties are unusable unless
120 * they have been previously stored
121 */
122 akbdmouse->kbds = type;
123
124 printf("\nNext, I will find all of your mice and print the corresponding manufacturer.\n");
125 /* find all mice using type EEZE_UDEV_TYPE_MOUSE */
126 type = eeze_udev_find_by_type(EEZE_UDEV_TYPE_MOUSE, NULL);
127 type = eeze_udev_find_unlisted_similar(type);
128 EINA_LIST_FOREACH(type, l, name)
129 { /* add the devpath to the hash for use in the cb later */
130 if ((check = eeze_udev_syspath_get_devpath(name)))
131 eina_hash_direct_add(hash, name, check); /* get a property using the device's syspath */
132 printf("Found mouse %s with vendor: %s\n", name, eeze_udev_walk_get_sysattr(name, "manufacturer"));
133 }
134 /* we save this list for later, because once a device is unplugged it can
135 * no longer be detected by udev, and any related properties are unusable unless
136 * they have been previously stored
137 */
138 akbdmouse->mice = type;
139
140 printf("\nNow let's try something a little more difficult. Mountable filesystems!\n");
141 /* find all mountable drives using type EEZE_UDEV_TYPE_DRIVE_MOUNTABLE */
142 type = eeze_udev_find_by_type(EEZE_UDEV_TYPE_DRIVE_MOUNTABLE, NULL);
143 type = eeze_udev_find_unlisted_similar(type);
144 EINA_LIST_FREE(type, name)
145 {
146 printf("Found device: %s\n", name); /* get a property using the device's syspath */
147 if ((check = eeze_udev_syspath_get_property(name, "DEVNAME")))
148 {
149 printf("\tYou probably know it better as %s\n", check);
150 eina_stringshare_del(check);
151 }
152 if ((check = eeze_udev_syspath_get_property(name, "ID_FS_TYPE")))
153 {
154 printf("\tIt's formatted as %s", check);
155 eina_stringshare_del(check);
156 check = eeze_udev_syspath_get_property(name, "FSTAB_DIR");
157 if (check)
158 {
159 printf(", and gets mounted at %s", check);
160 eina_stringshare_del(check);
161 }
162 printf("!\n");
163 }
164 eina_stringshare_del(name);
165 }
166
167 printf("\nNetwork devices!\n");
168 type = eeze_udev_find_by_type(EEZE_UDEV_TYPE_NET, NULL);
169 type = eeze_udev_find_unlisted_similar(type);
170 EINA_LIST_FREE(type, name)
171 {
172 printf("Found device: %s\n", name); /* get a property using the device's syspath */
173 if ((check = eeze_udev_syspath_get_property(name, "INTERFACE")))
174 {
175 printf("\tYou probably know it better as %s\n", check);
176 eina_stringshare_del(check);
177 }
178 eina_stringshare_del(name);
179 }
180
181 printf("\nInternal drives, anyone? With serial numbers?\n");
182 /* find all internal drives using type EEZE_UDEV_TYPE_DRIVE_INTERNAL */
183 type = eeze_udev_find_by_type(EEZE_UDEV_TYPE_DRIVE_INTERNAL, NULL);
184 type = eeze_udev_find_unlisted_similar(type);
185 EINA_LIST_FREE(type, name) /* get a property using the device's syspath */
186 {
187 if ((check = eeze_udev_syspath_get_property(name, "ID_SERIAL")))
188 {
189 printf("%s: %s\n", name, check);
190 eina_stringshare_del(check);
191 }
192 eina_stringshare_del(name);
193 }
194
195 printf("\nGot any removables? I'm gonna find em!\n");
196 /* find all removable media using type EEZE_UDEV_TYPE_DRIVE_REMOVABLE */
197 type = eeze_udev_find_by_type(EEZE_UDEV_TYPE_DRIVE_REMOVABLE, NULL);
198 type = eeze_udev_find_unlisted_similar(type);
199 EINA_LIST_FREE(type, name) /* get a property using the device's syspath */
200 {
201 if ((check = eeze_udev_syspath_get_sysattr(name, "model")))
202 {
203 check2 = eeze_udev_syspath_get_subsystem(name);
204 printf("\tOoh, a %s attached to the %s subsytem!\n", check, check2);
205 eina_stringshare_del(check);
206 eina_stringshare_del(check2);
207 }
208 eina_stringshare_del(name);
209 }
210
211 printf("\nGot any v4l device ?\n");
212 /* find all V4L device, may be a webcam or anything that can get a video
213 * stream from the real worl in a numerical form */
214 type = eeze_udev_find_by_type(EEZE_UDEV_TYPE_V4L, NULL);
215 type = eeze_udev_find_unlisted_similar(type);
216 EINA_LIST_FREE(type, name) /* get a device name using the device's syspath */
217 {
218 if ((check = eeze_udev_syspath_get_property(name, "DEVNAME")))
219 {
220 if ((check2 = eeze_udev_syspath_get_sysattr(name, "name")))
221 {
222 printf("%s: '%s' [%s]\n", name, check2, check);
223 eina_stringshare_del(check2);
224 }
225 eina_stringshare_del(check);
226 }
227 eina_stringshare_del(name);
228 }
229
230 /* set a udev watch, grab all events because no EEZE_UDEV_TYPE filter is specified,
231 * set the events to be sent to callback function catch_events(), and attach
232 * kbdmouse to the watch as associated data
233 */
234 eeze_udev_watch_add(EEZE_UDEV_TYPE_NONE, (EEZE_UDEV_EVENT_ADD | EEZE_UDEV_EVENT_REMOVE), catch_events, akbdmouse);
235 printf("\nAnd now for something more complicated. Plug or unplug your keyboard or mouse for me.\n");
236
237 /* main loop must be started to use ecore fd polling */
238 ecore_main_loop_begin();
239
240 return 0;
241}
242