EFL 1.7 svn doobies
[profile/ivi/eeze.git] / src / lib / eeze_udev_syspath.c
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 const char *
9 eeze_udev_syspath_get_parent(const char *syspath)
10 {
11    _udev_device *device, *parent;
12    const char *ret;
13
14    if (!syspath)
15      return NULL;
16
17    if (!(device = _new_device(syspath)))
18      return NULL;
19    parent = udev_device_get_parent(device);
20    ret = eina_stringshare_add(udev_device_get_syspath(parent));
21    udev_device_unref(device);
22    return ret;
23 }
24
25 EAPI Eina_List *
26 eeze_udev_syspath_get_parents(const char *syspath)
27 {
28    _udev_device *child, *parent, *device;
29    const char *path;
30    Eina_List *devlist = NULL;
31
32    if (!syspath)
33      return NULL;
34
35    if (!(device = _new_device(syspath)))
36      return NULL;
37
38    if (!(parent = udev_device_get_parent(device)))
39      return NULL;
40
41    for (; parent; child = parent, parent = udev_device_get_parent(child))
42      {
43         path = udev_device_get_syspath(parent);
44         devlist = eina_list_append(devlist, eina_stringshare_add(path));
45      }
46
47    udev_device_unref(device);
48    return devlist;
49 }
50
51 EAPI const char *
52 eeze_udev_syspath_get_devpath(const char *syspath)
53 {
54    _udev_device *device;
55    const char *name = NULL;
56
57    if (!syspath)
58      return NULL;
59
60    if (!(device = _new_device(syspath)))
61      return NULL;
62
63    if (!(name = udev_device_get_devnode(device)))
64      return NULL;
65
66    name = eina_stringshare_add(name);
67    udev_device_unref(device);
68    return name;
69 }
70
71 EAPI const char *
72 eeze_udev_syspath_get_devname(const char *syspath)
73 {
74    _udev_device *device;
75    const char *name = NULL;
76
77    if (!syspath)
78      return NULL;
79
80    if (!(device = _new_device(syspath)))
81      return NULL;
82
83    if (!(name = udev_device_get_sysname(device)))
84      return NULL;
85
86    name = eina_stringshare_add(name);
87    udev_device_unref(device);
88    return name;
89 }
90
91 EAPI const char *
92 eeze_udev_syspath_get_subsystem(const char *syspath)
93 {
94    _udev_device *device;
95    const char *subsystem;
96
97    if (!syspath)
98      return NULL;
99
100    if (!(device = _new_device(syspath)))
101      return NULL;
102    subsystem = eina_stringshare_add(udev_device_get_property_value(device, "SUBSYSTEM"));
103    udev_device_unref(device);
104    return subsystem;
105 }
106
107 EAPI const char *
108 eeze_udev_syspath_get_property(const char *syspath,
109                                const char *property)
110 {
111    _udev_device *device;
112    const char *value = NULL, *test;
113
114    if (!syspath || !property)
115      return NULL;
116
117    if (!(device = _new_device(syspath)))
118      return NULL;
119    if ((test = udev_device_get_property_value(device, property)))
120      value = eina_stringshare_add(test);
121
122    udev_device_unref(device);
123    return value;
124 }
125
126 EAPI const char *
127 eeze_udev_syspath_get_sysattr(const char *syspath,
128                               const char *sysattr)
129 {
130    _udev_device *device;
131    const char *value = NULL, *test;
132
133    if (!syspath || !sysattr)
134      return NULL;
135
136    if (!(device = _new_device(syspath)))
137      return NULL;
138
139    if ((test = udev_device_get_sysattr_value(device, sysattr)))
140      value = eina_stringshare_add(test);
141
142    udev_device_unref(device);
143    return value;
144 }
145
146 EAPI Eina_Bool
147 eeze_udev_syspath_is_mouse(const char *syspath)
148 {
149    _udev_device *device = NULL;
150    Eina_Bool mouse = EINA_FALSE;
151    const char *test = NULL;
152
153    if (!syspath)
154      return EINA_FALSE;
155
156    if (!(device = _new_device(syspath)))
157      return EINA_FALSE;
158 #ifdef OLD_UDEV_RRRRRRRRRRRRRR
159    mouse = _walk_parents_test_attr(device, "bInterfaceProtocol", "02");
160
161    if (!mouse)
162      {
163         test = udev_device_get_property_value(device, "ID_CLASS");
164
165         if ((test) && (!strcmp(test, "mouse")))
166           mouse = EINA_TRUE;
167      }
168
169 #else
170    test = udev_device_get_property_value(device, "ID_INPUT_MOUSE");
171
172    if (test && (test[0] == '1'))
173      mouse = EINA_TRUE;
174
175 #endif
176    udev_device_unref(device);
177    return mouse;
178 }
179
180 EAPI Eina_Bool
181 eeze_udev_syspath_is_kbd(const char *syspath)
182 {
183    _udev_device *device = NULL;
184    Eina_Bool kbd = EINA_FALSE;
185    const char *test = NULL;
186
187    if (!syspath)
188      return EINA_FALSE;
189
190    if (!(device = _new_device(syspath)))
191      return EINA_FALSE;
192 #ifdef OLD_UDEV_RRRRRRRRRRRRRR
193    kbd = _walk_parents_test_attr(device, "bInterfaceProtocol", "01");
194
195    if (!kbd)
196      {
197         test = udev_device_get_property_value(device, "ID_CLASS");
198
199         if ((test) && (!strcmp(test, "kbd")))
200           kbd = EINA_TRUE;
201      }
202
203 #else
204    test = udev_device_get_property_value(device, "ID_INPUT_KEYBOARD");
205
206    if (test && (test[0] == '1'))
207      kbd = EINA_TRUE;
208
209 #endif
210    udev_device_unref(device);
211    return kbd;
212 }
213
214 EAPI Eina_Bool
215 eeze_udev_syspath_is_touchpad(const char *syspath)
216 {
217    _udev_device *device = NULL;
218    Eina_Bool touchpad = EINA_FALSE;
219
220    if (!syspath)
221      return EINA_FALSE;
222
223    if (!(device = _new_device(syspath)))
224      return EINA_FALSE;
225 #ifdef OLD_UDEV_RRRRRRRRRRRRRR
226    touchpad = _walk_parents_test_attr(device, "resolution", NULL);
227 #else
228    const char *test;
229    test = udev_device_get_property_value(device, "ID_INPUT_TOUCHPAD");
230
231    if (test && (test[0] == '1'))
232      touchpad = EINA_TRUE;
233
234 #endif
235    udev_device_unref(device);
236    return touchpad;
237 }
238
239 EAPI Eina_Bool
240 eeze_udev_syspath_is_joystick(const char *syspath)
241 {
242    _udev_device *device = NULL;
243    Eina_Bool joystick = EINA_FALSE;
244    const char *test;
245
246    if (!syspath)
247      return EINA_FALSE;
248
249    if (!(device = _new_device(syspath)))
250      return EINA_FALSE;
251 #ifdef OLD_UDEV_RRRRRRRRRRRRRR
252    test = udev_device_get_property_value(device, "ID_CLASS");
253
254    if ((test) && (!strcmp(test, "joystick")))
255      joystick = EINA_TRUE;
256 #else
257    test = udev_device_get_property_value(device, "ID_INPUT_JOYSTICK");
258
259    if (test && (test[0] == '1'))
260      joystick = EINA_TRUE;
261
262 #endif
263    udev_device_unref(device);
264    return joystick;
265 }
266
267 EAPI const char *
268 eeze_udev_devpath_get_syspath(const char *devpath)
269 {
270    _udev_enumerate *en;
271    _udev_list_entry *devs, *cur;
272    const char *ret = NULL;
273
274    if (!devpath)
275      return NULL;
276
277    en = udev_enumerate_new(udev);
278
279    if (!en)
280      return NULL;
281
282    udev_enumerate_add_match_property(en, "DEVNAME", devpath);
283    udev_enumerate_scan_devices(en);
284    devs = udev_enumerate_get_list_entry(en);
285    udev_list_entry_foreach(cur, devs)
286      {
287         ret = eina_stringshare_add(udev_list_entry_get_name(cur));
288         break; /*just in case there's more than one somehow */
289      }
290    udev_enumerate_unref(en);
291    return ret;
292 }