]> git.sesse.net Git - vlc/blob - modules/services_discovery/udev.c
udev: handle "change" action properly, fix disc ejection/insertion
[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         struct udev_device *dev = udev_monitor_receive_device (mon);
284         if (dev == NULL)
285             continue;
286
287         const char *action = udev_device_get_action (dev);
288         if (!strcmp (action, "add"))
289             AddDevice (sd, dev);
290         else if (!strcmp (action, "remove"))
291             RemoveDevice (sd, dev);
292         else if (!strcmp (action, "change"))
293         {
294             RemoveDevice (sd, dev);
295             AddDevice (sd, dev);
296         }
297         udev_device_unref (dev);
298     }
299     return NULL;
300 }
301
302 /**
303  * Converts an hexadecimal digit to an integer.
304  */
305 static int hex (char c)
306 {
307     if (c >= '0' && c <= '9')
308         return c - '0';
309     if (c >= 'A' && c <= 'F')
310         return c + 10 - 'A';
311     if (c >= 'a' && c <= 'f')
312         return c + 10 - 'a';
313     return -1;
314 }
315
316 /**
317  * Decodes a udev hexadecimal-encoded property.
318  */
319 static char *decode (const char *enc)
320 {
321     char *ret = enc ? strdup (enc) : NULL;
322     if (ret == NULL)
323         return NULL;
324
325     char *out = ret;
326     for (const char *in = ret; *in; out++)
327     {
328         int h1, h2;
329
330         if ((in[0] == '\\') && (in[1] == 'x')
331          && ((h1 = hex (in[2])) != -1)
332          && ((h2 = hex (in[3])) != -1))
333         {
334             *out = (h1 << 4) | h2;
335             in += 4;
336         }
337         else
338         {
339             *out = *in;
340             in++;
341         }
342     }
343     *out = 0;
344     return ret;
345 }
346
347 static char *decode_property (struct udev_device *dev, const char *name)
348 {
349     return decode (udev_device_get_property_value (dev, name));
350 }
351
352 /*** Video4Linux support ***/
353 static bool is_v4l_legacy (struct udev_device *dev)
354 {
355     const char *version;
356
357     version = udev_device_get_property_value (dev, "ID_V4L_VERSION");
358     return version && !strcmp (version, "1");
359 }
360
361 static char *v4l_get_mrl (struct udev_device *dev)
362 {
363     /* Determine media location */
364     const char *scheme = "v4l2";
365     if (is_v4l_legacy (dev))
366         scheme = "v4l";
367     const char *node = udev_device_get_devnode (dev);
368     char *mrl;
369
370     if (asprintf (&mrl, "%s://%s", scheme, node) == -1)
371         mrl = NULL;
372     return mrl;
373 }
374
375 static char *v4l_get_name (struct udev_device *dev)
376 {
377     const char *prd = udev_device_get_property_value (dev, "ID_V4L_PRODUCT");
378     return prd ? strdup (prd) : NULL;
379 }
380
381 static char *v4l_get_cat (struct udev_device *dev)
382 {
383     return decode_property (dev, "ID_VENDOR_ENC");
384 }
385
386 int OpenV4L (vlc_object_t *obj)
387 {
388     static const struct subsys subsys = {
389         "video4linux", v4l_get_mrl, v4l_get_name, v4l_get_cat, ITEM_TYPE_CARD,
390     };
391
392     return Open (obj, &subsys);
393 }
394
395 /*** Discs support ***/
396 static char *disc_get_mrl (struct udev_device *dev)
397 {
398     const char *val;
399
400     val = udev_device_get_property_value (dev, "ID_CDROM");
401     if (val == NULL)
402         return NULL; /* Ignore non-optical block devices */
403
404     val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_STATE");
405     if ((val == NULL) || !strcmp (val, "blank"))
406         return NULL; /* ignore empty drives and virgin recordable discs */
407
408     const char *scheme = "file";
409     val = udev_device_get_property_value (dev,
410                                           "ID_CDROM_MEDIA_TRACK_COUNT_AUDIO");
411     if (val && atoi (val))
412         scheme = "cdda"; /* Audio CD rather than file system */
413 #if 0 /* we can use file:// for DVDs */
414     val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_DVD");
415     if (val && atoi (val))
416         scheme = "dvd";
417 #endif
418     val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_BD");
419     if (val && atoi (val))
420         scheme = "bd";
421 #ifdef LOL
422     val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_HDDVD");
423     if (val && atoi (val))
424         scheme = "hddvd";
425 #endif
426
427     val = udev_device_get_devnode (dev);
428     char *mrl;
429
430     if (asprintf (&mrl, "%s://%s", scheme, val) == -1)
431         mrl = NULL;
432     return mrl;
433 }
434
435 static char *disc_get_name (struct udev_device *dev)
436 {
437     return decode_property (dev, "ID_FS_LABEL_ENC");
438 }
439
440 static char *disc_get_cat (struct udev_device *dev)
441 {
442     const char *val;
443     const char *cat = "Unknown";
444
445     val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_CD");
446     if (val && atoi (val))
447         cat = "CD";
448     val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_DVD");
449     if (val && atoi (val))
450         cat = "DVD";
451     val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_BD");
452     if (val && atoi (val))
453         cat = "Blue-ray disc";
454     val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_HDDVD");
455     if (val && atoi (val))
456         cat = "HD DVD";
457
458     return strdup (cat);
459 }
460
461 int OpenDisc (vlc_object_t *obj)
462 {
463     static const struct subsys subsys = {
464         "block", disc_get_mrl, disc_get_name, disc_get_cat, ITEM_TYPE_DISC,
465     };
466
467     return Open (obj, &subsys);
468 }