]> git.sesse.net Git - vlc/blob - modules/services_discovery/udev.c
udev: refactor (binary search tree)
[vlc] / modules / services_discovery / udev.c
1 /**
2  * @file udev.c
3  * @brief List of multimedia devices for VLC media player
4  */
5 /*****************************************************************************
6  * Copyright © 2009 Rémi Denis-Courmont
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation; either version 2.1
11  * of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21  ****************************************************************************/
22
23 #ifdef HAVE_CONFIG_H
24 # include <config.h>
25 #endif
26
27 #include <libudev.h>
28 #include <vlc_common.h>
29 #include <vlc_services_discovery.h>
30 #include <vlc_plugin.h>
31 #include <search.h>
32 #include <poll.h>
33 #include <errno.h>
34
35 static int OpenV4L (vlc_object_t *);
36 static int OpenDisc (vlc_object_t *);
37 static void Close (vlc_object_t *);
38
39 /*
40  * Module descriptor
41  */
42 vlc_module_begin ()
43     set_shortname (N_("Devices"))
44     set_description (N_("Capture devices"))
45     set_category (CAT_PLAYLIST)
46     set_subcategory (SUBCAT_PLAYLIST_SD)
47     set_capability ("services_discovery", 0)
48     set_callbacks (OpenV4L, Close)
49     add_shortcut ("v4l")
50
51     add_submodule ()
52     set_shortname (N_("Discs"))
53     set_description (N_("Discs"))
54     set_category (CAT_PLAYLIST)
55     set_subcategory (SUBCAT_PLAYLIST_SD)
56     set_capability ("services_discovery", 0)
57     set_callbacks (OpenDisc, Close)
58     add_shortcut ("disc")
59
60 vlc_module_end ()
61
62 struct device
63 {
64     dev_t devnum; /* must be first */
65     input_item_t *item;
66 };
67
68 struct subsys
69 {
70     const char *name;
71     char * (*get_mrl) (struct udev_device *dev);
72     char * (*get_name) (struct udev_device *dev);
73     char * (*get_cat) (struct udev_device *dev);
74 };
75
76 struct services_discovery_sys_t
77 {
78     const struct subsys *subsys;
79     struct udev_monitor *monitor;
80     vlc_thread_t         thread;
81     void                *root;
82 };
83
84 /**
85  * Compares two devices (to support binary search).
86  */
87 static int cmpdev (const void *a, const void *b)
88 {
89     const dev_t *da = a, *db = b;
90     dev_t delta = *da - *db;
91
92     if (sizeof (delta) > sizeof (int))
93         return delta ? ((delta > 0) ? 1 : -1) : 0;
94     return (signed)delta;
95 }
96
97 static void DestroyDevice (void *data)
98 {
99     struct device *d = data;
100
101     vlc_gc_decref (d->item);
102     free (d);
103 }
104
105 static char *decode_property (struct udev_device *, const char *);
106
107 /**
108  * Adds a udev device.
109  */
110 static int AddDevice (services_discovery_t *sd, struct udev_device *dev)
111 {
112     services_discovery_sys_t *p_sys = sd->p_sys;
113
114     char *mrl = p_sys->subsys->get_mrl (dev);
115     if (mrl == NULL)
116         return 0; /* don't know if it was an error... */
117     char *name = p_sys->subsys->get_name (dev);
118     input_item_t *item = input_item_NewWithType (VLC_OBJECT (sd), mrl,
119                                                  name ? name : mrl,
120                                                  0, NULL, 0, -1, ITEM_TYPE_CARD);
121     msg_Dbg (sd, "adding %s (%s)", mrl, name);
122     free (name);
123     free (mrl);
124     if (item == NULL)
125         return -1;
126
127     struct device *d = malloc (sizeof (*d));
128     if (d == NULL)
129     {
130         vlc_gc_decref (item);
131         return -1;
132     }
133     d->item = item;
134     d->devnum = udev_device_get_devnum (dev);
135
136     struct device **dp = tsearch (d, &p_sys->root, cmpdev);
137     if (dp == NULL) /* Out-of-memory */
138     {
139         DestroyDevice (d);
140         return -1;
141     }
142     if (*dp != d) /* Overwrite existing device */
143     {
144         services_discovery_RemoveItem (sd, (*dp)->item);
145         DestroyDevice (*dp);
146         *dp = d;
147     }
148
149     name = p_sys->subsys->get_cat (dev);
150     services_discovery_AddItem (sd, item, name ? name : "Generic");
151     free (name);
152     return 0;
153 }
154
155 /**
156  * Removes a udev device (if present).
157  */
158 static void RemoveDevice (services_discovery_t *sd, struct udev_device *dev)
159 {
160     services_discovery_sys_t *p_sys = sd->p_sys;
161
162     dev_t num = udev_device_get_devnum (dev);
163     struct device **dp = tfind (&(dev_t){ num }, &p_sys->root, cmpdev);
164     if (dp == NULL)
165         return;
166
167     struct device *d = *dp;
168     services_discovery_RemoveItem (sd, d->item);
169     tdelete (d, &p_sys->root, cmpdev);
170     DestroyDevice (d);
171 }
172
173 static void HandleDevice (services_discovery_t *sd, struct udev_device *dev,
174                           bool add)
175 {
176     if (!add)
177         RemoveDevice (sd, dev);
178     else
179         AddDevice (sd, dev);
180 }
181
182 static void *Run (void *);
183
184 /**
185  * Probes and initializes.
186  */
187 static int Open (vlc_object_t *obj, const struct subsys *subsys)
188 {
189     services_discovery_t *sd = (services_discovery_t *)obj;
190     services_discovery_sys_t *p_sys = malloc (sizeof (*p_sys));
191
192     if (p_sys == NULL)
193         return VLC_ENOMEM;
194     sd->p_sys = p_sys;
195     p_sys->subsys = subsys;
196     p_sys->root = NULL;
197
198     struct udev_monitor *mon = NULL;
199     struct udev *udev = udev_new ();
200     if (udev == NULL)
201         goto error;
202
203     mon = udev_monitor_new_from_netlink (udev, "udev");
204     if (mon == NULL
205      || udev_monitor_filter_add_match_subsystem_devtype (mon, subsys->name,
206                                                          NULL))
207         goto error;
208     p_sys->monitor = mon;
209
210     /* Enumerate existing devices */
211     struct udev_enumerate *devenum = udev_enumerate_new (udev);
212     if (devenum == NULL)
213         goto error;
214     if (udev_enumerate_add_match_subsystem (devenum, subsys->name))
215     {
216         udev_enumerate_unref (devenum);
217         goto error;
218     }
219
220     udev_monitor_enable_receiving (mon);
221     /* Note that we enumerate _after_ monitoring is enabled so that we do not
222      * loose device events occuring while we are enumerating. We could still
223      * loose events if the Netlink socket receive buffer overflows. */
224     udev_enumerate_scan_devices (devenum);
225     struct udev_list_entry *devlist = udev_enumerate_get_list_entry (devenum);
226     struct udev_list_entry *deventry;
227     udev_list_entry_foreach (deventry, devlist)
228     {
229         const char *path = udev_list_entry_get_name (deventry);
230         struct udev_device *dev = udev_device_new_from_syspath (udev, path);
231         HandleDevice (sd, dev, true);
232         udev_device_unref (dev);
233     }
234     udev_enumerate_unref (devenum);
235
236     if (vlc_clone (&p_sys->thread, Run, sd, VLC_THREAD_PRIORITY_LOW))
237     {   /* Fallback without thread */
238         udev_monitor_unref (mon);
239         udev_unref (udev);
240         p_sys->monitor = NULL;
241     }
242     return VLC_SUCCESS;
243
244 error:
245     if (mon != NULL)
246         udev_monitor_unref (mon);
247     if (udev != NULL)
248         udev_unref (udev);
249     free (p_sys);
250     return VLC_EGENERIC;
251 }
252
253 /**
254  * Releases resources
255  */
256 static void Close (vlc_object_t *obj)
257 {
258     services_discovery_t *sd = (services_discovery_t *)obj;
259     services_discovery_sys_t *p_sys = sd->p_sys;
260
261     if (p_sys->monitor != NULL)
262     {
263         struct udev *udev = udev_monitor_get_udev (p_sys->monitor);
264
265         vlc_cancel (p_sys->thread);
266         vlc_join (p_sys->thread, NULL);
267         udev_monitor_unref (p_sys->monitor);
268         udev_unref (udev);
269     }
270
271     tdestroy (p_sys->root, DestroyDevice);
272     free (p_sys);
273 }
274
275 static void *Run (void *data)
276 {
277     services_discovery_t *sd = data;
278     services_discovery_sys_t *p_sys = sd->p_sys;
279     struct udev_monitor *mon = p_sys->monitor;
280
281     int fd = udev_monitor_get_fd (mon);
282     struct pollfd ufd = { .fd = fd, .events = POLLIN, };
283
284     for (;;)
285     {
286         while (poll (&ufd, 1, -1) == -1)
287             if (errno != EINTR)
288                 break;
289
290         struct udev_device *dev = udev_monitor_receive_device (mon);
291         if (dev == NULL)
292             continue;
293
294         /* FIXME: handle change, offline, online */
295         const char *action = udev_device_get_action (dev);
296         if (!strcmp (action, "add"))
297             HandleDevice (sd, dev, true);
298         else if (!strcmp (action, "remove"))
299             HandleDevice (sd, dev, false);
300
301         udev_device_unref (dev);
302     }
303     return NULL;
304 }
305
306 /**
307  * Converts an hexadecimal digit to an integer.
308  */
309 static int hex (char c)
310 {
311     if (c >= '0' && c <= '9')
312         return c - '0';
313     if (c >= 'A' && c <= 'F')
314         return c + 10 - 'A';
315     if (c >= 'a' && c <= 'f')
316         return c + 10 - 'a';
317     return -1;
318 }
319
320 /**
321  * Decodes a udev hexadecimal-encoded property.
322  */
323 static char *decode (const char *enc)
324 {
325     char *ret = enc ? strdup (enc) : NULL;
326     if (ret == NULL)
327         return NULL;
328
329     char *out = ret;
330     for (const char *in = ret; *in; out++)
331     {
332         int h1, h2;
333
334         if ((in[0] == '\\') && (in[1] == 'x')
335          && ((h1 = hex (in[2])) != -1)
336          && ((h2 = hex (in[3])) != -1))
337         {
338             *out = (h1 << 4) | h2;
339             in += 4;
340         }
341         else
342         {
343             *out = *in;
344             in++;
345         }
346     }
347     *out = 0;
348     return ret;
349 }
350
351 static char *decode_property (struct udev_device *dev, const char *name)
352 {
353     return decode (udev_device_get_property_value (dev, name));
354 }
355
356 /*** Video4Linux support ***/
357 static bool is_v4l_legacy (struct udev_device *dev)
358 {
359     const char *version;
360
361     version = udev_device_get_property_value (dev, "ID_V4L_VERSION");
362     return version && !strcmp (version, "1");
363 }
364
365 static char *v4l_get_mrl (struct udev_device *dev)
366 {
367     /* Determine media location */
368     const char *scheme = "v4l2";
369     if (is_v4l_legacy (dev))
370         scheme = "v4l";
371     const char *node = udev_device_get_devnode (dev);
372     char *mrl;
373
374     if (asprintf (&mrl, "%s://%s", scheme, node) == -1)
375         mrl = NULL;
376     return mrl;
377 }
378
379 static char *v4l_get_name (struct udev_device *dev)
380 {
381     const char *prd = udev_device_get_property_value (dev, "ID_V4L_PRODUCT");
382     return prd ? strdup (prd) : NULL;
383 }
384
385 static char *v4l_get_cat (struct udev_device *dev)
386 {
387     return decode_property (dev, "ID_VENDOR_ENC");
388 }
389
390 int OpenV4L (vlc_object_t *obj)
391 {
392     static const struct subsys subsys = {
393         "video4linux", v4l_get_mrl, v4l_get_name, v4l_get_cat,
394     };
395
396     return Open (obj, &subsys);
397 }
398
399 /*** Discs support ***/
400 static char *disc_get_mrl (struct udev_device *dev)
401 {
402     const char *val;
403
404     val = udev_device_get_property_value (dev, "ID_CDROM");
405     if (val == NULL)
406         return NULL; /* Ignore non-optical block devices */
407
408     val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_STATE");
409     if ((val == NULL) || !strcmp (val, "blank"))
410         return NULL; /* ignore empty drives and virgin recordable discs */
411
412     const char *scheme = "file";
413     val = udev_device_get_property_value (dev,
414                                           "ID_CDROM_MEDIA_TRACK_COUNT_AUDIO");
415     if (val && atoi (val))
416         scheme = "cdda"; /* Audio CD rather than file system */
417 #if 0 /* we can use file:// for DVDs */
418     val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_DVD");
419     if (val && atoi (val))
420         scheme = "dvd";
421 #endif
422     val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_BD");
423     if (val && atoi (val))
424         scheme = "bd";
425 #ifdef LOL
426     val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_HDDVD");
427     if (val && atoi (val))
428         scheme = "hddvd";
429 #endif
430
431     val = udev_device_get_devnode (dev);
432     char *mrl;
433
434     if (asprintf (&mrl, "%s://%s", scheme, val) == -1)
435         mrl = NULL;
436     return mrl;
437 }
438
439 static char *disc_get_name (struct udev_device *dev)
440 {
441     return decode_property (dev, "ID_FS_LABEL_ENC");
442 }
443
444 static char *disc_get_cat (struct udev_device *dev)
445 {
446     const char *val;
447     const char *cat = "Unknown";
448
449     val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_CD");
450     if (val && atoi (val))
451         cat = "CD";
452     val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_DVD");
453     if (val && atoi (val))
454         cat = "DVD";
455     val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_BD");
456     if (val && atoi (val))
457         cat = "Blue-ray disc";
458     val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_HDDVD");
459     if (val && atoi (val))
460         cat = "HD DVD";
461
462     return strdup (cat);
463 }
464
465 int OpenDisc (vlc_object_t *obj)
466 {
467     static const struct subsys subsys = {
468         "block", disc_get_mrl, disc_get_name, disc_get_cat,
469     };
470
471     return Open (obj, &subsys);
472 }