]> git.sesse.net Git - vlc/blobdiff - modules/services_discovery/udev.c
hds: Ignore sentinel value at the end of the fragment table
[vlc] / modules / services_discovery / udev.c
index e39f43a7c5ba749f6bf441a8fdc7692eb7bd97e4..a91dcf8a8810dcaf0400efd8da99dffc051a6232 100644 (file)
@@ -5,35 +5,45 @@
 /*****************************************************************************
  * Copyright © 2009 Rémi Denis-Courmont
  *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public License
- * as published by the Free Software Foundation; either version 2.1
- * of the License, or (at your option) any later version.
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or
+ * (at your option) any later version.
  *
- * This library is distributed in the hope that it will be useful,
+ * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
+ * GNU Lesser General Public License for more details.
  *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
- ****************************************************************************/
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ *****************************************************************************/
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
 #endif
 
+#include <errno.h>
+#include <search.h>
+#include <poll.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+
 #include <libudev.h>
+
 #include <vlc_common.h>
 #include <vlc_services_discovery.h>
 #include <vlc_plugin.h>
-#include <search.h>
-#include <poll.h>
-#include <errno.h>
+#ifdef HAVE_ALSA
+# include <vlc_modules.h>
+#endif
+#include <vlc_url.h>
 
 static int OpenV4L (vlc_object_t *);
+#ifdef HAVE_ALSA
 static int OpenALSA (vlc_object_t *);
+#endif
 static int OpenDisc (vlc_object_t *);
 static void Close (vlc_object_t *);
 static int vlc_sd_probe_Open (vlc_object_t *);
@@ -48,8 +58,8 @@ vlc_module_begin ()
     set_subcategory (SUBCAT_PLAYLIST_SD)
     set_capability ("services_discovery", 0)
     set_callbacks (OpenV4L, Close)
-    add_shortcut ("v4l")
-
+    add_shortcut ("v4l", "video")
+#ifdef HAVE_ALSA
     add_submodule ()
     set_shortname (N_("Audio capture"))
     set_description (N_("Audio capture (ALSA)"))
@@ -57,8 +67,8 @@ vlc_module_begin ()
     set_subcategory (SUBCAT_PLAYLIST_SD)
     set_capability ("services_discovery", 0)
     set_callbacks (OpenALSA, Close)
-    add_shortcut ("alsa")
-
+    add_shortcut ("alsa", "audio")
+#endif
     add_submodule ()
     set_shortname (N_("Discs"))
     set_description (N_("Discs"))
@@ -85,8 +95,11 @@ static int vlc_sd_probe_Open (vlc_object_t *obj)
     {
         vlc_sd_probe_Add (probe, "v4l{longname=\"Video capture\"}",
                           N_("Video capture"), SD_CAT_DEVICES);
-        vlc_sd_probe_Add (probe, "alsa{longname=\"Audio capture\"}",
-                          N_("Audio capture"), SD_CAT_DEVICES);
+#ifdef HAVE_ALSA
+        if (!module_exists ("pulselist"))
+            vlc_sd_probe_Add (probe, "alsa{longname=\"Audio capture\"}",
+                              N_("Audio capture"), SD_CAT_DEVICES);
+#endif
         vlc_sd_probe_Add (probe, "disc{longname=\"Discs\"}", N_("Discs"),
                           SD_CAT_DEVICES);
         udev_monitor_unref (mon);
@@ -107,7 +120,6 @@ struct subsys
     const char *name;
     char * (*get_mrl) (struct udev_device *dev);
     char * (*get_name) (struct udev_device *dev);
-    char * (*get_cat) (struct udev_device *dev);
     int item_type;
 };
 
@@ -125,11 +137,12 @@ struct services_discovery_sys_t
 static int cmpdev (const void *a, const void *b)
 {
     const dev_t *da = a, *db = b;
-    dev_t delta = *da - *db;
 
-    if (sizeof (delta) > sizeof (int))
-        return delta ? ((delta > 0) ? 1 : -1) : 0;
-    return (signed)delta;
+    if (*da > *db)
+        return +1;
+    if (*da < *db)
+        return -1;
+    return 0;
 }
 
 static void DestroyDevice (void *data)
@@ -155,8 +168,7 @@ static int AddDevice (services_discovery_t *sd, struct udev_device *dev)
     if (mrl == NULL)
         return 0; /* don't know if it was an error... */
     char *name = p_sys->subsys->get_name (dev);
-    input_item_t *item = input_item_NewWithType (VLC_OBJECT (sd), mrl,
-                                                 name ? name : mrl,
+    input_item_t *item = input_item_NewWithType (mrl, name ? name : mrl,
                                                  0, NULL, 0, -1,
                                                  p_sys->subsys->item_type);
     msg_Dbg (sd, "adding %s (%s)", mrl, name);
@@ -187,10 +199,8 @@ static int AddDevice (services_discovery_t *sd, struct udev_device *dev)
         *dp = d;
     }
 
-    name = p_sys->subsys->get_cat (dev);
-    services_discovery_AddItem (sd, item, name ? name : "Generic");
+    services_discovery_AddItem (sd, item, NULL);
     d->sd = sd;
-    free (name);
     return 0;
 }
 
@@ -392,24 +402,32 @@ static char *decode_property (struct udev_device *dev, const char *name)
 
 
 /*** Video4Linux support ***/
-static bool is_v4l_legacy (struct udev_device *dev)
+static bool v4l_is_legacy (struct udev_device *dev)
 {
     const char *version;
 
     version = udev_device_get_property_value (dev, "ID_V4L_VERSION");
-    return version && !strcmp (version, "1");
+    return (version != NULL) && !strcmp (version, "1");
+}
+
+static bool v4l_can_capture (struct udev_device *dev)
+{
+    const char *caps;
+
+    caps = udev_device_get_property_value (dev, "ID_V4L_CAPABILITIES");
+    return (caps != NULL) && (strstr (caps, ":capture:") != NULL);
 }
 
 static char *v4l_get_mrl (struct udev_device *dev)
 {
     /* Determine media location */
-    const char *scheme = "v4l2";
-    if (is_v4l_legacy (dev))
-        scheme = "v4l";
+    if (v4l_is_legacy (dev) || !v4l_can_capture (dev))
+        return NULL;
+
     const char *node = udev_device_get_devnode (dev);
     char *mrl;
 
-    if (asprintf (&mrl, "%s://%s", scheme, node) == -1)
+    if (asprintf (&mrl, "v4l2://%s", node) == -1)
         mrl = NULL;
     return mrl;
 }
@@ -420,15 +438,10 @@ static char *v4l_get_name (struct udev_device *dev)
     return prd ? strdup (prd) : NULL;
 }
 
-static char *v4l_get_cat (struct udev_device *dev)
-{
-    return decode_property (dev, "ID_VENDOR_ENC");
-}
-
 int OpenV4L (vlc_object_t *obj)
 {
     static const struct subsys subsys = {
-        "video4linux", v4l_get_mrl, v4l_get_name, v4l_get_cat, ITEM_TYPE_CARD,
+        "video4linux", v4l_get_mrl, v4l_get_name, ITEM_TYPE_CARD,
     };
 
     return Open (obj, &subsys);
@@ -499,25 +512,10 @@ out:
     return name;
 }
 
-static char *alsa_get_cat (struct udev_device *dev)
-{
-    const char *vnd;
-
-    dev = udev_device_get_parent (dev);
-    if (dev == NULL)
-        return NULL;
-
-    vnd = udev_device_get_property_value (dev, "ID_VENDOR_FROM_DATABASE");
-    if (vnd == NULL)
-        /* FIXME: USB may take time to settle... the parent device */
-        vnd = udev_device_get_property_value (dev, "ID_BUS");
-    return vnd ? strdup (vnd) : NULL;
-}
-
 int OpenALSA (vlc_object_t *obj)
 {
     static const struct subsys subsys = {
-        "sound", alsa_get_mrl, alsa_get_name, alsa_get_cat, ITEM_TYPE_CARD,
+        "sound", alsa_get_mrl, alsa_get_name, ITEM_TYPE_CARD,
     };
 
     return Open (obj, &subsys);
@@ -528,6 +526,7 @@ int OpenALSA (vlc_object_t *obj)
 /*** Discs support ***/
 static char *disc_get_mrl (struct udev_device *dev)
 {
+    const char *node = udev_device_get_devnode (dev);
     const char *val;
 
     val = udev_device_get_property_value (dev, "ID_CDROM");
@@ -535,21 +534,28 @@ static char *disc_get_mrl (struct udev_device *dev)
         return NULL; /* Ignore non-optical block devices */
 
     val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_STATE");
-    if (val && !strcmp (val, "blank"))
+    if (val == NULL)
+    {   /* Force probing of the disc in the drive if any. */
+        int fd = open (node, O_RDONLY|O_CLOEXEC);
+        if (fd != -1)
+            close (fd);
+        return NULL;
+    }
+    if (!strcmp (val, "blank"))
         return NULL; /* ignore empty drives and virgin recordable discs */
 
-    const char *scheme = "invalid";
+    const char *scheme = NULL;
     val = udev_device_get_property_value (dev,
                                           "ID_CDROM_MEDIA_TRACK_COUNT_AUDIO");
     if (val && atoi (val))
         scheme = "cdda"; /* Audio CD rather than file system */
     val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_DVD");
     if (val && atoi (val))
-        scheme = "file";
+        scheme = "dvd";
 
     val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_BD");
     if (val && atoi (val))
-        scheme = "bd";
+        scheme = "bluray";
 #ifdef LOL
     val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_HDDVD");
     if (val && atoi (val))
@@ -558,24 +564,15 @@ static char *disc_get_mrl (struct udev_device *dev)
 
     /* We didn't get any property that could tell we have optical disc
        that we can play */
-    if( !strcmp( scheme, "invalid" ) )
+    if (scheme == NULL)
         return NULL;
 
-    val = udev_device_get_devnode (dev);
-    char *mrl;
-
-    if (asprintf (&mrl, "%s://%s", scheme, val) == -1)
-        mrl = NULL;
-    return mrl;
+    return vlc_path2uri (node, scheme);
 }
 
 static char *disc_get_name (struct udev_device *dev)
 {
-    return decode_property (dev, "ID_FS_LABEL_ENC");
-}
-
-static char *disc_get_cat (struct udev_device *dev)
-{
+    char *name = NULL;
     struct udev_list_entry *list, *entry;
 
     list = udev_device_get_properties_list_entry (dev);
@@ -598,7 +595,7 @@ static char *disc_get_cat (struct udev_device *dev)
         else if (!strncmp (name, "DVD", 3))
             cat = N_("DVD");
         else if (!strncmp (name, "BD", 2))
-            cat = N_("Blu-Ray");
+            cat = N_("Blu-ray");
         else if (!strncmp (name, "HDDVD", 5))
             cat = N_("HD DVD");
 
@@ -608,13 +605,21 @@ static char *disc_get_cat (struct udev_device *dev)
 
     if (cat == NULL)
         cat = N_("Unknown type");
-    return strdup (vlc_gettext (cat));
+
+    char *label = decode_property (dev, "ID_FS_LABEL_ENC");
+
+    if (label)
+        if (asprintf(&name, "%s (%s)", label, vlc_gettext(cat)) < 0)
+            name = NULL;
+    free(label);
+
+    return name;
 }
 
 int OpenDisc (vlc_object_t *obj)
 {
     static const struct subsys subsys = {
-        "block", disc_get_mrl, disc_get_name, disc_get_cat, ITEM_TYPE_DISC,
+        "block", disc_get_mrl, disc_get_name, ITEM_TYPE_DISC,
     };
 
     return Open (obj, &subsys);