]> git.sesse.net Git - vlc/blob - modules/services_discovery/udev.c
udev: preempt cancellation
[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 *Run (void *);
176
177 /**
178  * Probes and initializes.
179  */
180 static int Open (vlc_object_t *obj, const struct subsys *subsys)
181 {
182     services_discovery_t *sd = (services_discovery_t *)obj;
183     services_discovery_sys_t *p_sys = malloc (sizeof (*p_sys));
184
185     if (p_sys == NULL)
186         return VLC_ENOMEM;
187     sd->p_sys = p_sys;
188     p_sys->subsys = subsys;
189     p_sys->root = NULL;
190
191     struct udev_monitor *mon = NULL;
192     struct udev *udev = udev_new ();
193     if (udev == NULL)
194         goto error;
195
196     mon = udev_monitor_new_from_netlink (udev, "udev");
197     if (mon == NULL
198      || udev_monitor_filter_add_match_subsystem_devtype (mon, subsys->name,
199                                                          NULL))
200         goto error;
201     p_sys->monitor = mon;
202
203     /* Enumerate existing devices */
204     struct udev_enumerate *devenum = udev_enumerate_new (udev);
205     if (devenum == NULL)
206         goto error;
207     if (udev_enumerate_add_match_subsystem (devenum, subsys->name))
208     {
209         udev_enumerate_unref (devenum);
210         goto error;
211     }
212
213     udev_monitor_enable_receiving (mon);
214     /* Note that we enumerate _after_ monitoring is enabled so that we do not
215      * loose device events occuring while we are enumerating. We could still
216      * loose events if the Netlink socket receive buffer overflows. */
217     udev_enumerate_scan_devices (devenum);
218     struct udev_list_entry *devlist = udev_enumerate_get_list_entry (devenum);
219     struct udev_list_entry *deventry;
220     udev_list_entry_foreach (deventry, devlist)
221     {
222         const char *path = udev_list_entry_get_name (deventry);
223         struct udev_device *dev = udev_device_new_from_syspath (udev, path);
224         AddDevice (sd, dev);
225         udev_device_unref (dev);
226     }
227     udev_enumerate_unref (devenum);
228
229     if (vlc_clone (&p_sys->thread, Run, sd, VLC_THREAD_PRIORITY_LOW))
230     {   /* Fallback without thread */
231         udev_monitor_unref (mon);
232         udev_unref (udev);
233         p_sys->monitor = NULL;
234     }
235     return VLC_SUCCESS;
236
237 error:
238     if (mon != NULL)
239         udev_monitor_unref (mon);
240     if (udev != NULL)
241         udev_unref (udev);
242     free (p_sys);
243     return VLC_EGENERIC;
244 }
245
246 /**
247  * Releases resources
248  */
249 static void Close (vlc_object_t *obj)
250 {
251     services_discovery_t *sd = (services_discovery_t *)obj;
252     services_discovery_sys_t *p_sys = sd->p_sys;
253
254     if (p_sys->monitor != NULL)
255     {
256         struct udev *udev = udev_monitor_get_udev (p_sys->monitor);
257
258         vlc_cancel (p_sys->thread);
259         vlc_join (p_sys->thread, NULL);
260         udev_monitor_unref (p_sys->monitor);
261         udev_unref (udev);
262     }
263
264     tdestroy (p_sys->root, DestroyDevice);
265     free (p_sys);
266 }
267
268 static void *Run (void *data)
269 {
270     services_discovery_t *sd = data;
271     services_discovery_sys_t *p_sys = sd->p_sys;
272     struct udev_monitor *mon = p_sys->monitor;
273
274     int fd = udev_monitor_get_fd (mon);
275     struct pollfd ufd = { .fd = fd, .events = POLLIN, };
276
277     for (;;)
278     {
279         while (poll (&ufd, 1, -1) == -1)
280             if (errno != EINTR)
281                 break;
282
283         int canc = vlc_savecancel ();
284         struct udev_device *dev = udev_monitor_receive_device (mon);
285         if (dev == NULL)
286             continue;
287
288         const char *action = udev_device_get_action (dev);
289         if (!strcmp (action, "add"))
290             AddDevice (sd, dev);
291         else if (!strcmp (action, "remove"))
292             RemoveDevice (sd, dev);
293         else if (!strcmp (action, "change"))
294         {
295             RemoveDevice (sd, dev);
296             AddDevice (sd, dev);
297         }
298         udev_device_unref (dev);
299         vlc_restorecancel (canc);
300     }
301     return NULL;
302 }
303
304 /**
305  * Converts an hexadecimal digit to an integer.
306  */
307 static int hex (char c)
308 {
309     if (c >= '0' && c <= '9')
310         return c - '0';
311     if (c >= 'A' && c <= 'F')
312         return c + 10 - 'A';
313     if (c >= 'a' && c <= 'f')
314         return c + 10 - 'a';
315     return -1;
316 }
317
318 /**
319  * Decodes a udev hexadecimal-encoded property.
320  */
321 static char *decode (const char *enc)
322 {
323     char *ret = enc ? strdup (enc) : NULL;
324     if (ret == NULL)
325         return NULL;
326
327     char *out = ret;
328     for (const char *in = ret; *in; out++)
329     {
330         int h1, h2;
331
332         if ((in[0] == '\\') && (in[1] == 'x')
333          && ((h1 = hex (in[2])) != -1)
334          && ((h2 = hex (in[3])) != -1))
335         {
336             *out = (h1 << 4) | h2;
337             in += 4;
338         }
339         else
340         {
341             *out = *in;
342             in++;
343         }
344     }
345     *out = 0;
346     return ret;
347 }
348
349 static char *decode_property (struct udev_device *dev, const char *name)
350 {
351     return decode (udev_device_get_property_value (dev, name));
352 }
353
354 /*** Video4Linux support ***/
355 static bool is_v4l_legacy (struct udev_device *dev)
356 {
357     const char *version;
358
359     version = udev_device_get_property_value (dev, "ID_V4L_VERSION");
360     return version && !strcmp (version, "1");
361 }
362
363 static char *v4l_get_mrl (struct udev_device *dev)
364 {
365     /* Determine media location */
366     const char *scheme = "v4l2";
367     if (is_v4l_legacy (dev))
368         scheme = "v4l";
369     const char *node = udev_device_get_devnode (dev);
370     char *mrl;
371
372     if (asprintf (&mrl, "%s://%s", scheme, node) == -1)
373         mrl = NULL;
374     return mrl;
375 }
376
377 static char *v4l_get_name (struct udev_device *dev)
378 {
379     const char *prd = udev_device_get_property_value (dev, "ID_V4L_PRODUCT");
380     return prd ? strdup (prd) : NULL;
381 }
382
383 static char *v4l_get_cat (struct udev_device *dev)
384 {
385     return decode_property (dev, "ID_VENDOR_ENC");
386 }
387
388 int OpenV4L (vlc_object_t *obj)
389 {
390     static const struct subsys subsys = {
391         "video4linux", v4l_get_mrl, v4l_get_name, v4l_get_cat, ITEM_TYPE_CARD,
392     };
393
394     return Open (obj, &subsys);
395 }
396
397 /*** Discs support ***/
398 static char *disc_get_mrl (struct udev_device *dev)
399 {
400     const char *val;
401
402     val = udev_device_get_property_value (dev, "ID_CDROM");
403     if (val == NULL)
404         return NULL; /* Ignore non-optical block devices */
405
406     val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_STATE");
407     if ((val == NULL) || !strcmp (val, "blank"))
408         return NULL; /* ignore empty drives and virgin recordable discs */
409
410     const char *scheme = "file";
411     val = udev_device_get_property_value (dev,
412                                           "ID_CDROM_MEDIA_TRACK_COUNT_AUDIO");
413     if (val && atoi (val))
414         scheme = "cdda"; /* Audio CD rather than file system */
415 #if 0 /* we can use file:// for DVDs */
416     val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_DVD");
417     if (val && atoi (val))
418         scheme = "dvd";
419 #endif
420     val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_BD");
421     if (val && atoi (val))
422         scheme = "bd";
423 #ifdef LOL
424     val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_HDDVD");
425     if (val && atoi (val))
426         scheme = "hddvd";
427 #endif
428
429     val = udev_device_get_devnode (dev);
430     char *mrl;
431
432     if (asprintf (&mrl, "%s://%s", scheme, val) == -1)
433         mrl = NULL;
434     return mrl;
435 }
436
437 static char *disc_get_name (struct udev_device *dev)
438 {
439     return decode_property (dev, "ID_FS_LABEL_ENC");
440 }
441
442 static char *disc_get_cat (struct udev_device *dev)
443 {
444     const char *val;
445     const char *cat = "Unknown";
446
447     val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_CD");
448     if (val && atoi (val))
449         cat = "CD";
450     val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_DVD");
451     if (val && atoi (val))
452         cat = "DVD";
453     val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_BD");
454     if (val && atoi (val))
455         cat = "Blue-ray disc";
456     val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_HDDVD");
457     if (val && atoi (val))
458         cat = "HD DVD";
459
460     return strdup (cat);
461 }
462
463 int OpenDisc (vlc_object_t *obj)
464 {
465     static const struct subsys subsys = {
466         "block", disc_get_mrl, disc_get_name, disc_get_cat, ITEM_TYPE_DISC,
467     };
468
469     return Open (obj, &subsys);
470 }