udev: use dvd:// rather than file:// for DVD device nodes
[vlc/asuraparaju-public.git] / modules / services_discovery / udev.c
blob650059b9432786207b31ea72573f3f72aaa98023
1 /**
2 * @file udev.c
3 * @brief List of multimedia devices for VLC media player
4 */
5 /*****************************************************************************
6 * Copyright © 2009 Rémi Denis-Courmont
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.
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.
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 ****************************************************************************/
23 #ifdef HAVE_CONFIG_H
24 # include <config.h>
25 #endif
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>
35 static int OpenV4L (vlc_object_t *);
36 static int OpenALSA (vlc_object_t *);
37 static int OpenDisc (vlc_object_t *);
38 static void Close (vlc_object_t *);
39 static int vlc_sd_probe_Open (vlc_object_t *);
42 * Module descriptor
44 vlc_module_begin ()
45 set_shortname (N_("Video capture"))
46 set_description (N_("Video capture (Video4Linux)"))
47 set_category (CAT_PLAYLIST)
48 set_subcategory (SUBCAT_PLAYLIST_SD)
49 set_capability ("services_discovery", 0)
50 set_callbacks (OpenV4L, Close)
51 add_shortcut ("v4l")
53 add_submodule ()
54 set_shortname (N_("Audio capture"))
55 set_description (N_("Audio capture (ALSA)"))
56 set_category (CAT_PLAYLIST)
57 set_subcategory (SUBCAT_PLAYLIST_SD)
58 set_capability ("services_discovery", 0)
59 set_callbacks (OpenALSA, Close)
60 add_shortcut ("alsa")
62 add_submodule ()
63 set_shortname (N_("Discs"))
64 set_description (N_("Discs"))
65 set_category (CAT_PLAYLIST)
66 set_subcategory (SUBCAT_PLAYLIST_SD)
67 set_capability ("services_discovery", 0)
68 set_callbacks (OpenDisc, Close)
69 add_shortcut ("disc")
71 VLC_SD_PROBE_SUBMODULE
73 vlc_module_end ()
75 static int vlc_sd_probe_Open (vlc_object_t *obj)
77 vlc_probe_t *probe = (vlc_probe_t *)obj;
79 struct udev *udev = udev_new ();
80 if (udev == NULL)
81 return VLC_PROBE_CONTINUE;
83 struct udev_monitor *mon = udev_monitor_new_from_netlink (udev, "udev");
84 if (mon != NULL)
86 vlc_sd_probe_Add (probe, "v4l{longname=\"Video capture\"}",
87 N_("Video capture"), SD_CAT_DEVICES);
88 vlc_sd_probe_Add (probe, "alsa{longname=\"Audio capture\"}",
89 N_("Audio capture"), SD_CAT_DEVICES);
90 vlc_sd_probe_Add (probe, "disc{longname=\"Discs\"}", N_("Discs"),
91 SD_CAT_DEVICES);
92 udev_monitor_unref (mon);
94 udev_unref (udev);
95 return VLC_PROBE_CONTINUE;
98 struct device
100 dev_t devnum; /* must be first */
101 input_item_t *item;
102 services_discovery_t *sd;
105 struct subsys
107 const char *name;
108 char * (*get_mrl) (struct udev_device *dev);
109 char * (*get_name) (struct udev_device *dev);
110 char * (*get_cat) (struct udev_device *dev);
111 int item_type;
114 struct services_discovery_sys_t
116 const struct subsys *subsys;
117 struct udev_monitor *monitor;
118 vlc_thread_t thread;
119 void *root;
123 * Compares two devices (to support binary search).
125 static int cmpdev (const void *a, const void *b)
127 const dev_t *da = a, *db = b;
128 dev_t delta = *da - *db;
130 if (sizeof (delta) > sizeof (int))
131 return delta ? ((delta > 0) ? 1 : -1) : 0;
132 return (signed)delta;
135 static void DestroyDevice (void *data)
137 struct device *d = data;
139 if (d->sd)
140 services_discovery_RemoveItem (d->sd, d->item);
141 vlc_gc_decref (d->item);
142 free (d);
145 static char *decode_property (struct udev_device *, const char *);
148 * Adds a udev device.
150 static int AddDevice (services_discovery_t *sd, struct udev_device *dev)
152 services_discovery_sys_t *p_sys = sd->p_sys;
154 char *mrl = p_sys->subsys->get_mrl (dev);
155 if (mrl == NULL)
156 return 0; /* don't know if it was an error... */
157 char *name = p_sys->subsys->get_name (dev);
158 input_item_t *item = input_item_NewWithType (VLC_OBJECT (sd), mrl,
159 name ? name : mrl,
160 0, NULL, 0, -1,
161 p_sys->subsys->item_type);
162 msg_Dbg (sd, "adding %s (%s)", mrl, name);
163 free (name);
164 free (mrl);
165 if (item == NULL)
166 return -1;
168 struct device *d = malloc (sizeof (*d));
169 if (d == NULL)
171 vlc_gc_decref (item);
172 return -1;
174 d->devnum = udev_device_get_devnum (dev);
175 d->item = item;
176 d->sd = NULL;
178 struct device **dp = tsearch (d, &p_sys->root, cmpdev);
179 if (dp == NULL) /* Out-of-memory */
181 DestroyDevice (d);
182 return -1;
184 if (*dp != d) /* Overwrite existing device */
186 DestroyDevice (*dp);
187 *dp = d;
190 name = p_sys->subsys->get_cat (dev);
191 services_discovery_AddItem (sd, item, name ? name : "Generic");
192 d->sd = sd;
193 free (name);
194 return 0;
198 * Removes a udev device (if present).
200 static void RemoveDevice (services_discovery_t *sd, struct udev_device *dev)
202 services_discovery_sys_t *p_sys = sd->p_sys;
204 dev_t num = udev_device_get_devnum (dev);
205 struct device **dp = tfind (&(dev_t){ num }, &p_sys->root, cmpdev);
206 if (dp == NULL)
207 return;
209 struct device *d = *dp;
210 tdelete (d, &p_sys->root, cmpdev);
211 DestroyDevice (d);
214 static void *Run (void *);
217 * Probes and initializes.
219 static int Open (vlc_object_t *obj, const struct subsys *subsys)
221 services_discovery_t *sd = (services_discovery_t *)obj;
222 services_discovery_sys_t *p_sys = malloc (sizeof (*p_sys));
224 if (p_sys == NULL)
225 return VLC_ENOMEM;
226 sd->p_sys = p_sys;
227 p_sys->subsys = subsys;
228 p_sys->root = NULL;
230 struct udev_monitor *mon = NULL;
231 struct udev *udev = udev_new ();
232 if (udev == NULL)
233 goto error;
235 mon = udev_monitor_new_from_netlink (udev, "udev");
236 if (mon == NULL
237 || udev_monitor_filter_add_match_subsystem_devtype (mon, subsys->name,
238 NULL))
239 goto error;
240 p_sys->monitor = mon;
242 /* Enumerate existing devices */
243 struct udev_enumerate *devenum = udev_enumerate_new (udev);
244 if (devenum == NULL)
245 goto error;
246 if (udev_enumerate_add_match_subsystem (devenum, subsys->name))
248 udev_enumerate_unref (devenum);
249 goto error;
252 udev_monitor_enable_receiving (mon);
253 /* Note that we enumerate _after_ monitoring is enabled so that we do not
254 * loose device events occuring while we are enumerating. We could still
255 * loose events if the Netlink socket receive buffer overflows. */
256 udev_enumerate_scan_devices (devenum);
257 struct udev_list_entry *devlist = udev_enumerate_get_list_entry (devenum);
258 struct udev_list_entry *deventry;
259 udev_list_entry_foreach (deventry, devlist)
261 const char *path = udev_list_entry_get_name (deventry);
262 struct udev_device *dev = udev_device_new_from_syspath (udev, path);
263 AddDevice (sd, dev);
264 udev_device_unref (dev);
266 udev_enumerate_unref (devenum);
268 if (vlc_clone (&p_sys->thread, Run, sd, VLC_THREAD_PRIORITY_LOW))
269 { /* Fallback without thread */
270 udev_monitor_unref (mon);
271 udev_unref (udev);
272 p_sys->monitor = NULL;
274 return VLC_SUCCESS;
276 error:
277 if (mon != NULL)
278 udev_monitor_unref (mon);
279 if (udev != NULL)
280 udev_unref (udev);
281 free (p_sys);
282 return VLC_EGENERIC;
286 * Releases resources
288 static void Close (vlc_object_t *obj)
290 services_discovery_t *sd = (services_discovery_t *)obj;
291 services_discovery_sys_t *p_sys = sd->p_sys;
293 if (p_sys->monitor != NULL)
295 struct udev *udev = udev_monitor_get_udev (p_sys->monitor);
297 vlc_cancel (p_sys->thread);
298 vlc_join (p_sys->thread, NULL);
299 udev_monitor_unref (p_sys->monitor);
300 udev_unref (udev);
303 tdestroy (p_sys->root, DestroyDevice);
304 free (p_sys);
307 static void *Run (void *data)
309 services_discovery_t *sd = data;
310 services_discovery_sys_t *p_sys = sd->p_sys;
311 struct udev_monitor *mon = p_sys->monitor;
313 int fd = udev_monitor_get_fd (mon);
314 struct pollfd ufd = { .fd = fd, .events = POLLIN, };
316 for (;;)
318 while (poll (&ufd, 1, -1) == -1)
319 if (errno != EINTR)
320 break;
322 int canc = vlc_savecancel ();
323 struct udev_device *dev = udev_monitor_receive_device (mon);
324 if (dev == NULL)
325 continue;
327 const char *action = udev_device_get_action (dev);
328 if (!strcmp (action, "add"))
329 AddDevice (sd, dev);
330 else if (!strcmp (action, "remove"))
331 RemoveDevice (sd, dev);
332 else if (!strcmp (action, "change"))
334 RemoveDevice (sd, dev);
335 AddDevice (sd, dev);
337 udev_device_unref (dev);
338 vlc_restorecancel (canc);
340 return NULL;
344 * Converts an hexadecimal digit to an integer.
346 static int hex (char c)
348 if (c >= '0' && c <= '9')
349 return c - '0';
350 if (c >= 'A' && c <= 'F')
351 return c + 10 - 'A';
352 if (c >= 'a' && c <= 'f')
353 return c + 10 - 'a';
354 return -1;
358 * Decodes a udev hexadecimal-encoded property.
360 static char *decode (const char *enc)
362 char *ret = enc ? strdup (enc) : NULL;
363 if (ret == NULL)
364 return NULL;
366 char *out = ret;
367 for (const char *in = ret; *in; out++)
369 int h1, h2;
371 if ((in[0] == '\\') && (in[1] == 'x')
372 && ((h1 = hex (in[2])) != -1)
373 && ((h2 = hex (in[3])) != -1))
375 *out = (h1 << 4) | h2;
376 in += 4;
378 else
380 *out = *in;
381 in++;
384 *out = 0;
385 return ret;
388 static char *decode_property (struct udev_device *dev, const char *name)
390 return decode (udev_device_get_property_value (dev, name));
394 /*** Video4Linux support ***/
395 static bool is_v4l_legacy (struct udev_device *dev)
397 const char *version;
399 version = udev_device_get_property_value (dev, "ID_V4L_VERSION");
400 return version && !strcmp (version, "1");
403 static char *v4l_get_mrl (struct udev_device *dev)
405 /* Determine media location */
406 const char *scheme = "v4l2";
407 if (is_v4l_legacy (dev))
408 scheme = "v4l";
409 const char *node = udev_device_get_devnode (dev);
410 char *mrl;
412 if (asprintf (&mrl, "%s://%s", scheme, node) == -1)
413 mrl = NULL;
414 return mrl;
417 static char *v4l_get_name (struct udev_device *dev)
419 const char *prd = udev_device_get_property_value (dev, "ID_V4L_PRODUCT");
420 return prd ? strdup (prd) : NULL;
423 static char *v4l_get_cat (struct udev_device *dev)
425 return decode_property (dev, "ID_VENDOR_ENC");
428 int OpenV4L (vlc_object_t *obj)
430 static const struct subsys subsys = {
431 "video4linux", v4l_get_mrl, v4l_get_name, v4l_get_cat, ITEM_TYPE_CARD,
434 return Open (obj, &subsys);
438 #ifdef HAVE_ALSA
439 /*** Advanced Linux Sound Architecture support ***/
440 #include <alsa/asoundlib.h>
442 static int alsa_get_device (struct udev_device *dev, unsigned *restrict pcard,
443 unsigned *restrict pdevice)
445 const char *node = udev_device_get_devpath (dev);
446 char type;
448 node = strrchr (node, '/');
449 if (node == NULL)
450 return -1;
451 if (sscanf (node, "/pcmC%uD%u%c", pcard, pdevice, &type) < 3)
452 return -1;
453 if (type != 'c')
454 return -1;
455 return 0;
459 static char *alsa_get_mrl (struct udev_device *dev)
461 /* Determine media location */
462 char *mrl;
463 unsigned card, device;
465 if (alsa_get_device (dev, &card, &device))
466 return NULL;
468 if (asprintf (&mrl, "alsa://plughw:%u,%u", card, device) == -1)
469 mrl = NULL;
470 return mrl;
473 static char *alsa_get_name (struct udev_device *dev)
475 char *name = NULL;
476 unsigned card, device;
478 if (alsa_get_device (dev, &card, &device))
479 return NULL;
481 char card_name[4 + 3 * sizeof (int)];
482 snprintf (card_name, sizeof (card_name), "hw:%u", card);
484 snd_ctl_t *ctl;
485 if (snd_ctl_open (&ctl, card_name, 0))
486 return NULL;
488 snd_pcm_info_t *pcm_info;
489 snd_pcm_info_alloca (&pcm_info);
490 snd_pcm_info_set_device (pcm_info, device);
491 snd_pcm_info_set_subdevice (pcm_info, 0);
492 snd_pcm_info_set_stream (pcm_info, SND_PCM_STREAM_CAPTURE);
493 if (snd_ctl_pcm_info (ctl, pcm_info))
494 goto out;
496 name = strdup (snd_pcm_info_get_name (pcm_info));
497 out:
498 snd_ctl_close (ctl);
499 return name;
502 static char *alsa_get_cat (struct udev_device *dev)
504 const char *vnd;
506 dev = udev_device_get_parent (dev);
507 if (dev == NULL)
508 return NULL;
510 vnd = udev_device_get_property_value (dev, "ID_VENDOR_FROM_DATABASE");
511 if (vnd == NULL)
512 /* FIXME: USB may take time to settle... the parent device */
513 vnd = udev_device_get_property_value (dev, "ID_BUS");
514 return vnd ? strdup (vnd) : NULL;
517 int OpenALSA (vlc_object_t *obj)
519 static const struct subsys subsys = {
520 "sound", alsa_get_mrl, alsa_get_name, alsa_get_cat, ITEM_TYPE_CARD,
523 return Open (obj, &subsys);
525 #endif /* HAVE_ALSA */
528 /*** Discs support ***/
529 static char *disc_get_mrl (struct udev_device *dev)
531 const char *val;
533 val = udev_device_get_property_value (dev, "ID_CDROM");
534 if (val == NULL)
535 return NULL; /* Ignore non-optical block devices */
537 val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_STATE");
538 if (val && !strcmp (val, "blank"))
539 return NULL; /* ignore empty drives and virgin recordable discs */
541 const char *scheme = "invalid";
542 val = udev_device_get_property_value (dev,
543 "ID_CDROM_MEDIA_TRACK_COUNT_AUDIO");
544 if (val && atoi (val))
545 scheme = "cdda"; /* Audio CD rather than file system */
546 val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_DVD");
547 if (val && atoi (val))
548 scheme = "dvd";
550 val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_BD");
551 if (val && atoi (val))
552 scheme = "bd";
553 #ifdef LOL
554 val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_HDDVD");
555 if (val && atoi (val))
556 scheme = "hddvd";
557 #endif
559 /* We didn't get any property that could tell we have optical disc
560 that we can play */
561 if( !strcmp( scheme, "invalid" ) )
562 return NULL;
564 val = udev_device_get_devnode (dev);
565 char *mrl;
567 if (asprintf (&mrl, "%s://%s", scheme, val) == -1)
568 mrl = NULL;
569 return mrl;
572 static char *disc_get_name (struct udev_device *dev)
574 return decode_property (dev, "ID_FS_LABEL_ENC");
577 static char *disc_get_cat (struct udev_device *dev)
579 struct udev_list_entry *list, *entry;
581 list = udev_device_get_properties_list_entry (dev);
582 if (unlikely(list == NULL))
583 return NULL;
585 const char *cat = NULL;
586 udev_list_entry_foreach (entry, list)
588 const char *name = udev_list_entry_get_name (entry);
590 if (strncmp (name, "ID_CDROM_MEDIA_", 15))
591 continue;
592 if (!atoi (udev_list_entry_get_value (entry)))
593 continue;
594 name += 15;
596 if (!strncmp (name, "CD", 2))
597 cat = N_("CD");
598 else if (!strncmp (name, "DVD", 3))
599 cat = N_("DVD");
600 else if (!strncmp (name, "BD", 2))
601 cat = N_("Blu-Ray");
602 else if (!strncmp (name, "HDDVD", 5))
603 cat = N_("HD DVD");
605 if (cat != NULL)
606 break;
609 if (cat == NULL)
610 cat = N_("Unknown type");
611 return strdup (vlc_gettext (cat));
614 int OpenDisc (vlc_object_t *obj)
616 static const struct subsys subsys = {
617 "block", disc_get_mrl, disc_get_name, disc_get_cat, ITEM_TYPE_DISC,
620 return Open (obj, &subsys);