1 #ifdef HAVE_CONFIG_H
2 #include "config.h"
3 #endif
4 
5 #include <Eeze.h>
6 #include "eeze_udev_private.h"
7 
8 EAPI Eina_Stringshare *
eeze_udev_syspath_get_parent_filtered(const char * syspath,const char * subsystem,const char * devtype)9 eeze_udev_syspath_get_parent_filtered(const char *syspath, const char *subsystem, const char *devtype)
10 {
11    _udev_device *device, *parent;
12    Eina_Stringshare *ret = NULL;
13 
14    EINA_SAFETY_ON_NULL_RETURN_VAL(syspath, NULL);
15 
16    if (!(device = _new_device(syspath)))
17      return NULL;
18    parent = udev_device_get_parent_with_subsystem_devtype(device, subsystem, devtype);
19    if (parent)
20      ret = eina_stringshare_add(udev_device_get_syspath(parent));
21    udev_device_unref(device);
22    return ret;
23 }
24 
25 EAPI const char *
eeze_udev_syspath_get_parent(const char * syspath)26 eeze_udev_syspath_get_parent(const char *syspath)
27 {
28    _udev_device *device, *parent;
29    const char *ret;
30 
31    if (!syspath)
32      return NULL;
33 
34    if (!(device = _new_device(syspath)))
35      return NULL;
36    parent = udev_device_get_parent(device);
37    ret = eina_stringshare_add(udev_device_get_syspath(parent));
38    udev_device_unref(device);
39    return ret;
40 }
41 
42 EAPI Eina_List *
eeze_udev_syspath_get_parents(const char * syspath)43 eeze_udev_syspath_get_parents(const char *syspath)
44 {
45    _udev_device *child, *parent, *device;
46    const char *path;
47    Eina_List *devlist = NULL;
48 
49    if (!syspath)
50      return NULL;
51 
52    if (!(device = _new_device(syspath)))
53      return NULL;
54 
55    if (!(parent = udev_device_get_parent(device)))
56      return NULL;
57 
58    for (; parent; child = parent, parent = udev_device_get_parent(child))
59      {
60         path = udev_device_get_syspath(parent);
61         devlist = eina_list_append(devlist, eina_stringshare_add(path));
62      }
63 
64    udev_device_unref(device);
65    return devlist;
66 }
67 
68 EAPI const char *
eeze_udev_syspath_get_devpath(const char * syspath)69 eeze_udev_syspath_get_devpath(const char *syspath)
70 {
71    _udev_device *device;
72    const char *name = NULL;
73 
74    if (!syspath)
75      return NULL;
76 
77    if (!(device = _new_device(syspath)))
78      return NULL;
79 
80    if (!(name = udev_device_get_devnode(device)))
81      return NULL;
82 
83    name = eina_stringshare_add(name);
84    udev_device_unref(device);
85    return name;
86 }
87 
88 EAPI const char *
eeze_udev_syspath_get_devname(const char * syspath)89 eeze_udev_syspath_get_devname(const char *syspath)
90 {
91    _udev_device *device;
92    const char *name = NULL;
93 
94    if (!syspath)
95      return NULL;
96 
97    if (!(device = _new_device(syspath)))
98      return NULL;
99 
100    if (!(name = udev_device_get_sysname(device)))
101      return NULL;
102 
103    name = eina_stringshare_add(name);
104    udev_device_unref(device);
105    return name;
106 }
107 
108 EAPI const char *
eeze_udev_syspath_get_subsystem(const char * syspath)109 eeze_udev_syspath_get_subsystem(const char *syspath)
110 {
111    _udev_device *device;
112    const char *subsystem;
113 
114    if (!syspath)
115      return NULL;
116 
117    if (!(device = _new_device(syspath)))
118      return NULL;
119    subsystem = eina_stringshare_add(udev_device_get_property_value(device, "SUBSYSTEM"));
120    udev_device_unref(device);
121    return subsystem;
122 }
123 
124 EAPI Eina_Bool
eeze_udev_syspath_check_property(const char * syspath,const char * property,const char * value)125 eeze_udev_syspath_check_property(const char *syspath, const char *property, const char *value)
126 {
127    _udev_device *device;
128    const char *test;
129    Eina_Bool ret = EINA_FALSE;
130 
131    if (!syspath || !property || !value)
132      return EINA_FALSE;
133 
134    if (!(device = _new_device(syspath)))
135      return EINA_FALSE;
136    if ((test = udev_device_get_property_value(device, property)))
137      ret = !strcmp(test, value);
138 
139    udev_device_unref(device);
140    return ret;
141 }
142 
143 EAPI const char *
eeze_udev_syspath_get_property(const char * syspath,const char * property)144 eeze_udev_syspath_get_property(const char *syspath,
145                                const char *property)
146 {
147    _udev_device *device;
148    const char *test;
149    Eina_Stringshare *value = NULL;
150 
151    if (!syspath || !property)
152      return NULL;
153 
154    if (!(device = _new_device(syspath)))
155      return NULL;
156    if ((test = udev_device_get_property_value(device, property)))
157      value = eina_stringshare_add(test);
158 
159    udev_device_unref(device);
160    return value;
161 }
162 
163 EAPI Eina_Bool
eeze_udev_syspath_check_sysattr(const char * syspath,const char * sysattr,const char * value)164 eeze_udev_syspath_check_sysattr(const char *syspath, const char *sysattr, const char *value)
165 {
166    _udev_device *device;
167    const char *test;
168    Eina_Bool ret = EINA_FALSE;
169 
170    if (!syspath || !sysattr || !value)
171      return EINA_FALSE;
172 
173    if (!(device = _new_device(syspath)))
174      return EINA_FALSE;
175 
176    if ((test = udev_device_get_sysattr_value(device, sysattr)))
177      ret = !strcmp(test, value);
178 
179    udev_device_unref(device);
180    return ret;
181 }
182 
183 EAPI const char *
eeze_udev_syspath_get_sysattr(const char * syspath,const char * sysattr)184 eeze_udev_syspath_get_sysattr(const char *syspath,
185                               const char *sysattr)
186 {
187    _udev_device *device;
188    const char *value = NULL, *test;
189 
190    if (!syspath || !sysattr)
191      return NULL;
192 
193    if (!(device = _new_device(syspath)))
194      return NULL;
195 
196    if ((test = udev_device_get_sysattr_value(device, sysattr)))
197      value = eina_stringshare_add(test);
198 
199    udev_device_unref(device);
200    return value;
201 }
202 
203 EAPI Eina_Bool
eeze_udev_syspath_set_sysattr(const char * syspath,const char * sysattr,double value)204 eeze_udev_syspath_set_sysattr(const char *syspath,
205                               const char *sysattr,
206                               double value)
207 {
208    _udev_device *device;
209    Eina_Bool ret = EINA_FALSE;
210 
211    if (!syspath || !sysattr)
212      return EINA_FALSE;
213 
214    if (!(device = _new_device(syspath)))
215      return EINA_FALSE;
216 
217 #ifndef OLD_LIBUDEV
218    char val[16];
219    int test;
220 
221    sprintf(val, "%f", value);
222    test = udev_device_set_sysattr_value(device, sysattr, val);
223    if (test == 0)
224      ret = EINA_TRUE;
225 #else
226    (void)value;
227 #endif
228 
229   udev_device_unref(device);
230   return ret;
231 }
232 
233 EAPI Eina_List *
eeze_udev_syspath_get_sysattr_list(const char * syspath)234 eeze_udev_syspath_get_sysattr_list(const char *syspath)
235 {
236    _udev_device *device;
237    _udev_list_entry *devs, *cur;
238    Eina_List *syslist = NULL;
239 
240    if (!syspath)
241      return NULL;
242 
243    if (!(device = _new_device(syspath)))
244      return NULL;
245 
246    devs = udev_device_get_sysattr_list_entry(device);
247    udev_list_entry_foreach(cur, devs)
248      {
249         syslist = eina_list_append(syslist,
250                    eina_stringshare_add(udev_list_entry_get_name(cur)));
251      }
252 
253    udev_device_unref(device);
254    return syslist;
255 }
256 
257 EAPI Eina_Bool
eeze_udev_syspath_is_mouse(const char * syspath)258 eeze_udev_syspath_is_mouse(const char *syspath)
259 {
260    _udev_device *device = NULL;
261    Eina_Bool mouse = EINA_FALSE;
262    const char *test = NULL;
263 
264    if (!syspath)
265      return EINA_FALSE;
266 
267    if (!(device = _new_device(syspath)))
268      return EINA_FALSE;
269 
270    test = udev_device_get_property_value(device, "ID_INPUT_MOUSE");
271 
272    if (test && (test[0] == '1'))
273      mouse = EINA_TRUE;
274 
275    udev_device_unref(device);
276    return mouse;
277 }
278 
279 EAPI Eina_Bool
eeze_udev_syspath_is_kbd(const char * syspath)280 eeze_udev_syspath_is_kbd(const char *syspath)
281 {
282    _udev_device *device = NULL;
283    Eina_Bool kbd = EINA_FALSE;
284    const char *test = NULL;
285 
286    if (!syspath)
287      return EINA_FALSE;
288 
289    if (!(device = _new_device(syspath)))
290      return EINA_FALSE;
291 
292    test = udev_device_get_property_value(device, "ID_INPUT_KEYBOARD");
293 
294    if (test && (test[0] == '1'))
295      kbd = EINA_TRUE;
296 
297    udev_device_unref(device);
298    return kbd;
299 }
300 
301 EAPI Eina_Bool
eeze_udev_syspath_is_touchpad(const char * syspath)302 eeze_udev_syspath_is_touchpad(const char *syspath)
303 {
304    _udev_device *device = NULL;
305    Eina_Bool touchpad = EINA_FALSE;
306    const char *test;
307 
308    if (!syspath)
309      return EINA_FALSE;
310 
311    if (!(device = _new_device(syspath)))
312      return EINA_FALSE;
313 
314    test = udev_device_get_property_value(device, "ID_INPUT_TOUCHPAD");
315 
316    if (test && (test[0] == '1'))
317      touchpad = EINA_TRUE;
318 
319    udev_device_unref(device);
320    return touchpad;
321 }
322 
323 EAPI Eina_Bool
eeze_udev_syspath_is_joystick(const char * syspath)324 eeze_udev_syspath_is_joystick(const char *syspath)
325 {
326    _udev_device *device = NULL;
327    Eina_Bool joystick = EINA_FALSE;
328    const char *test;
329 
330    if (!syspath)
331      return EINA_FALSE;
332 
333    if (!(device = _new_device(syspath)))
334      return EINA_FALSE;
335 
336    test = udev_device_get_property_value(device, "ID_INPUT_JOYSTICK");
337 
338    if (test && (test[0] == '1'))
339      joystick = EINA_TRUE;
340 
341    udev_device_unref(device);
342    return joystick;
343 }
344 
345 EAPI const char *
eeze_udev_devpath_get_syspath(const char * devpath)346 eeze_udev_devpath_get_syspath(const char *devpath)
347 {
348    _udev_enumerate *en;
349    _udev_list_entry *devs, *cur;
350    const char *ret = NULL;
351 
352    if (!devpath)
353      return NULL;
354 
355    en = udev_enumerate_new(udev);
356 
357    if (!en)
358      return NULL;
359 
360    udev_enumerate_add_match_property(en, "DEVNAME", devpath);
361    udev_enumerate_scan_devices(en);
362    devs = udev_enumerate_get_list_entry(en);
363    udev_list_entry_foreach(cur, devs)
364      {
365         ret = eina_stringshare_add(udev_list_entry_get_name(cur));
366         break; /*just in case there's more than one somehow */
367      }
368    udev_enumerate_unref(en);
369    return ret;
370 }
371 
372 EAPI int
eeze_udev_syspath_get_sysnum(const char * syspath)373 eeze_udev_syspath_get_sysnum(const char *syspath)
374 {
375    _udev_device *device;
376    const char *test;
377    int ret = -1;
378 
379    if (!syspath)
380      return -1;
381 
382    if (!(device = _new_device(syspath)))
383      return -1;
384 
385    if ((test = udev_device_get_sysnum(device)))
386      ret = atoi(test);
387 
388    udev_device_unref(device);
389    return ret;
390 }
391 
392