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