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