]> git.sesse.net Git - vlc/blob - modules/services_discovery/udev.c
b443a832221a12ee174133d7423fa00280ebe7ef
[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     int item_type;
75 };
76
77 struct services_discovery_sys_t
78 {
79     const struct subsys *subsys;
80     struct udev_monitor *monitor;
81     vlc_thread_t         thread;
82     void                *root;
83 };
84
85 /**
86  * Compares two devices (to support binary search).
87  */
88 static int cmpdev (const void *a, const void *b)
89 {
90     const dev_t *da = a, *db = b;
91     dev_t delta = *da - *db;
92
93     if (sizeof (delta) > sizeof (int))
94         return delta ? ((delta > 0) ? 1 : -1) : 0;
95     return (signed)delta;
96 }
97
98 static void DestroyDevice (void *data)
99 {
100     struct device *d = data;
101
102     vlc_gc_decref (d->item);
103     free (d);
104 }
105
106 static char *decode_property (struct udev_device *, const char *);
107
108 /**
109  * Adds a udev device.
110  */
111 static int AddDevice (services_discovery_t *sd, struct udev_device *dev)
112 {
113     services_discovery_sys_t *p_sys = sd->p_sys;
114
115     char *mrl = p_sys->subsys->get_mrl (dev);
116     if (mrl == NULL)
117         return 0; /* don't know if it was an error... */
118     char *name = p_sys->subsys->get_name (dev);
119     input_item_t *item = input_item_NewWithType (VLC_OBJECT (sd), mrl,
120                                                  name ? name : mrl,
121                                                  0, NULL, 0, -1,
122                                                  p_sys->subsys->item_type);
123     msg_Dbg (sd, "adding %s (%s)", mrl, name);
124     free (name);
125     free (mrl);
126     if (item == NULL)
127         return -1;
128
129     struct device *d = malloc (sizeof (*d));
130     if (d == NULL)
131     {
132         vlc_gc_decref (item);
133         return -1;
134     }
135     d->item = item;
136     d->devnum = udev_device_get_devnum (dev);
137
138     struct device **dp = tsearch (d, &p_sys->root, cmpdev);
139     if (dp == NULL) /* Out-of-memory */
140     {
141         DestroyDevice (d);
142         return -1;
143     }
144     if (*dp != d) /* Overwrite existing device */
145     {
146         services_discovery_RemoveItem (sd, (*dp)->item);
147         DestroyDevice (*dp);
148         *dp = d;
149     }
150
151     name = p_sys->subsys->get_cat (dev);
152     services_discovery_AddItem (sd, item, name ? name : "Generic");
153     free (name);
154     return 0;
155 }
156
157 /**
158  * Removes a udev device (if present).
159  */
160 static void RemoveDevice (services_discovery_t *sd, struct udev_device *dev)
161 {
162     services_discovery_sys_t *p_sys = sd->p_sys;
163
164     dev_t num = udev_device_get_devnum (dev);
165     struct device **dp = tfind (&(dev_t){ num }, &p_sys->root, cmpdev);
166     if (dp == NULL)
167         return;
168
169     struct device *d = *dp;
170     services_discovery_RemoveItem (sd, d->item);
171     tdelete (d, &p_sys->root, cmpdev);
172     DestroyDevice (d);
173 }
174
175 static void HandleDevice (services_discovery_t *sd, struct udev_device *dev,
176                           bool add)
177 {
178     if (!add)
179         RemoveDevice (sd, dev);
180     else
181         AddDevice (sd, dev);
182 }
183
184 static void *Run (void *);
185
186 /**
187  * Probes and initializes.
188  */
189 static int Open (vlc_object_t *obj, const struct subsys *subsys)
190 {
191     services_discovery_t *sd = (services_discovery_t *)obj;
192     services_discovery_sys_t *p_sys = malloc (sizeof (*p_sys));
193
194     if (p_sys == NULL)
195         return VLC_ENOMEM;
196     sd->p_sys = p_sys;
197     p_sys->subsys = subsys;
198     p_sys->root = NULL;
199
200     struct udev_monitor *mon = NULL;
201     struct udev *udev = udev_new ();
202     if (udev == NULL)
203         goto error;
204
205     mon = udev_monitor_new_from_netlink (udev, "udev");
206     if (mon == NULL
207      || udev_monitor_filter_add_match_subsystem_devtype (mon, subsys->name,
208                                                          NULL))
209         goto error;
210     p_sys->monitor = mon;
211
212     /* Enumerate existing devices */
213     struct udev_enumerate *devenum = udev_enumerate_new (udev);
214     if (devenum == NULL)
215         goto error;
216     if (udev_enumerate_add_match_subsystem (devenum, subsys->name))
217     {
218         udev_enumerate_unref (devenum);
219         goto error;
220     }
221
222     udev_monitor_enable_receiving (mon);
223     /* Note that we enumerate _after_ monitoring is enabled so that we do not
224      * loose device events occuring while we are enumerating. We could still
225      * loose events if the Netlink socket receive buffer overflows. */
226     udev_enumerate_scan_devices (devenum);
227     struct udev_list_entry *devlist = udev_enumerate_get_list_entry (devenum);
228     struct udev_list_entry *deventry;
229     udev_list_entry_foreach (deventry, devlist)
230     {
231         const char *path = udev_list_entry_get_name (deventry);
232         struct udev_device *dev = udev_device_new_from_syspath (udev, path);
233         HandleDevice (sd, dev, true);
234         udev_device_unref (dev);
235     }
236     udev_enumerate_unref (devenum);
237
238     if (vlc_clone (&p_sys->thread, Run, sd, VLC_THREAD_PRIORITY_LOW))
239     {   /* Fallback without thread */
240         udev_monitor_unref (mon);
241         udev_unref (udev);
242         p_sys->monitor = NULL;
243     }
244     return VLC_SUCCESS;
245
246 error:
247     if (mon != NULL)
248         udev_monitor_unref (mon);
249     if (udev != NULL)
250         udev_unref (udev);
251     free (p_sys);
252     return VLC_EGENERIC;
253 }
254
255 /**
256  * Releases resources
257  */
258 static void Close (vlc_object_t *obj)
259 {
260     services_discovery_t *sd = (services_discovery_t *)obj;
261     services_discovery_sys_t *p_sys = sd->p_sys;
262
263     if (p_sys->monitor != NULL)
264     {
265         struct udev *udev = udev_monitor_get_udev (p_sys->monitor);
266
267         vlc_cancel (p_sys->thread);
268         vlc_join (p_sys->thread, NULL);
269         udev_monitor_unref (p_sys->monitor);
270         udev_unref (udev);
271     }
272
273     tdestroy (p_sys->root, DestroyDevice);
274     free (p_sys);
275 }
276
277 static void *Run (void *data)
278 {
279     services_discovery_t *sd = data;
280     services_discovery_sys_t *p_sys = sd->p_sys;
281     struct udev_monitor *mon = p_sys->monitor;
282
283     int fd = udev_monitor_get_fd (mon);
284     struct pollfd ufd = { .fd = fd, .events = POLLIN, };
285
286     for (;;)
287     {
288         while (poll (&ufd, 1, -1) == -1)
289             if (errno != EINTR)
290                 break;
291
292         struct udev_device *dev = udev_monitor_receive_device (mon);
293         if (dev == NULL)
294             continue;
295
296         /* FIXME: handle change, offline, online */
297         const char *action = udev_device_get_action (dev);
298         if (!strcmp (action, "add"))
299             HandleDevice (sd, dev, true);
300         else if (!strcmp (action, "remove"))
301             HandleDevice (sd, dev, false);
302
303         udev_device_unref (dev);
304     }
305     return NULL;
306 }
307
308 /**
309  * Converts an hexadecimal digit to an integer.
310  */
311 static int hex (char c)
312 {
313     if (c >= '0' && c <= '9')
314         return c - '0';
315     if (c >= 'A' && c <= 'F')
316         return c + 10 - 'A';
317     if (c >= 'a' && c <= 'f')
318         return c + 10 - 'a';
319     return -1;
320 }
321
322 /**
323  * Decodes a udev hexadecimal-encoded property.
324  */
325 static char *decode (const char *enc)
326 {
327     char *ret = enc ? strdup (enc) : NULL;
328     if (ret == NULL)
329         return NULL;
330
331     char *out = ret;
332     for (const char *in = ret; *in; out++)
333     {
334         int h1, h2;
335
336         if ((in[0] == '\\') && (in[1] == 'x')
337          && ((h1 = hex (in[2])) != -1)
338          && ((h2 = hex (in[3])) != -1))
339         {
340             *out = (h1 << 4) | h2;
341             in += 4;
342         }
343         else
344         {
345             *out = *in;
346             in++;
347         }
348     }
349     *out = 0;
350     return ret;
351 }
352
353 static char *decode_property (struct udev_device *dev, const char *name)
354 {
355     return decode (udev_device_get_property_value (dev, name));
356 }
357
358 /*** Video4Linux support ***/
359 static bool is_v4l_legacy (struct udev_device *dev)
360 {
361     const char *version;
362
363     version = udev_device_get_property_value (dev, "ID_V4L_VERSION");
364     return version && !strcmp (version, "1");
365 }
366
367 static char *v4l_get_mrl (struct udev_device *dev)
368 {
369     /* Determine media location */
370     const char *scheme = "v4l2";
371     if (is_v4l_legacy (dev))
372         scheme = "v4l";
373     const char *node = udev_device_get_devnode (dev);
374     char *mrl;
375
376     if (asprintf (&mrl, "%s://%s", scheme, node) == -1)
377         mrl = NULL;
378     return mrl;
379 }
380
381 static char *v4l_get_name (struct udev_device *dev)
382 {
383     const char *prd = udev_device_get_property_value (dev, "ID_V4L_PRODUCT");
384     return prd ? strdup (prd) : NULL;
385 }
386
387 static char *v4l_get_cat (struct udev_device *dev)
388 {
389     return decode_property (dev, "ID_VENDOR_ENC");
390 }
391
392 int OpenV4L (vlc_object_t *obj)
393 {
394     static const struct subsys subsys = {
395         "video4linux", v4l_get_mrl, v4l_get_name, v4l_get_cat, ITEM_TYPE_CARD,
396     };
397
398     return Open (obj, &subsys);
399 }
400
401 /*** Discs support ***/
402 static char *disc_get_mrl (struct udev_device *dev)
403 {
404     const char *val;
405
406     val = udev_device_get_property_value (dev, "ID_CDROM");
407     if (val == NULL)
408         return NULL; /* Ignore non-optical block devices */
409
410     val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_STATE");
411     if ((val == NULL) || !strcmp (val, "blank"))
412         return NULL; /* ignore empty drives and virgin recordable discs */
413
414     const char *scheme = "file";
415     val = udev_device_get_property_value (dev,
416                                           "ID_CDROM_MEDIA_TRACK_COUNT_AUDIO");
417     if (val && atoi (val))
418         scheme = "cdda"; /* Audio CD rather than file system */
419 #if 0 /* we can use file:// for DVDs */
420     val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_DVD");
421     if (val && atoi (val))
422         scheme = "dvd";
423 #endif
424     val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_BD");
425     if (val && atoi (val))
426         scheme = "bd";
427 #ifdef LOL
428     val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_HDDVD");
429     if (val && atoi (val))
430         scheme = "hddvd";
431 #endif
432
433     val = udev_device_get_devnode (dev);
434     char *mrl;
435
436     if (asprintf (&mrl, "%s://%s", scheme, val) == -1)
437         mrl = NULL;
438     return mrl;
439 }
440
441 static char *disc_get_name (struct udev_device *dev)
442 {
443     return decode_property (dev, "ID_FS_LABEL_ENC");
444 }
445
446 static char *disc_get_cat (struct udev_device *dev)
447 {
448     const char *val;
449     const char *cat = "Unknown";
450
451     val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_CD");
452     if (val && atoi (val))
453         cat = "CD";
454     val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_DVD");
455     if (val && atoi (val))
456         cat = "DVD";
457     val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_BD");
458     if (val && atoi (val))
459         cat = "Blue-ray disc";
460     val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_HDDVD");
461     if (val && atoi (val))
462         cat = "HD DVD";
463
464     return strdup (cat);
465 }
466
467 int OpenDisc (vlc_object_t *obj)
468 {
469     static const struct subsys subsys = {
470         "block", disc_get_mrl, disc_get_name, disc_get_cat, ITEM_TYPE_DISC,
471     };
472
473     return Open (obj, &subsys);
474 }