]> git.sesse.net Git - vlc/blobdiff - src/playlist/services_discovery.c
Use var_Inherit* instead of var_CreateGet*.
[vlc] / src / playlist / services_discovery.c
index 53a314be44e19788e4df8cf103835233933a6798..2f384e679add18ac2eeae47aa8a795025e636b62 100644 (file)
 #include "vlc_playlist.h"
 #include "vlc_events.h"
 #include <vlc_services_discovery.h>
+#include <vlc_probe.h>
+#include <vlc_modules.h>
 #include "playlist_internal.h"
 #include "../libvlc.h"
 
-struct vlc_sd_internal_t
+typedef struct
 {
-    /* the playlist items for category and onelevel */
-    playlist_item_t      *p_cat;
-    playlist_item_t      *p_one;
-    services_discovery_t *p_sd; /**< Loaded service discovery modules */
-    char                 *psz_name;
-};
+    char *name;
+    char *longname;
+    int category;
+} vlc_sd_probe_t;
 
-static void services_discovery_Destructor ( vlc_object_t *p_obj );
+int vlc_sd_probe_Add (vlc_probe_t *probe, const char *name,
+                      const char *longname, int category)
+{
+    vlc_sd_probe_t names = { strdup(name), strdup(longname), category };
 
-/*
- * Services discovery
- * Basically you just listen to Service discovery event through the
- * sd's event manager.
- * That's how the playlist get's Service Discovery information
- */
+    if (unlikely (names.name == NULL || names.longname == NULL
+               || vlc_probe_add (probe, &names, sizeof (names))))
+    {
+        free (names.name);
+        free (names.longname);
+        return VLC_ENOMEM;
+    }
+    return VLC_PROBE_CONTINUE;
+}
 
 #undef vlc_sd_GetNames
 
 /**
  * Gets the list of available services discovery plugins.
  */
-char **vlc_sd_GetNames( vlc_object_t *obj, char ***pppsz_longnames )
+char **vlc_sd_GetNames (vlc_object_t *obj, char ***pppsz_longnames, int **pp_categories)
 {
-    return module_GetModulesNamesForCapability( "services_discovery",
-                                                pppsz_longnames );
+    size_t count;
+    vlc_sd_probe_t *tab = vlc_probe (obj, "services probe", &count);
+
+    if (count == 0)
+    {
+        free (tab);
+        return NULL;
+    }
+
+    char **names = malloc (sizeof(char *) * (count + 1));
+    char **longnames = malloc (sizeof(char *) * (count + 1));
+    int *categories = malloc(sizeof(int) * (count + 1));
+
+    if (unlikely (names == NULL || longnames == NULL || categories == NULL))
+        abort();
+    for( size_t i = 0; i < count; i++ )
+    {
+        names[i] = tab[i].name;
+        longnames[i] = tab[i].longname;
+        categories[i] = tab[i].category;
+    }
+    free (tab);
+    names[count] = longnames[count] = NULL;
+    categories[count] = 0;
+    *pppsz_longnames = longnames;
+    if( pp_categories ) *pp_categories = categories;
+    else free( categories );
+    return names;
 }
 
+
+static void services_discovery_Destructor ( vlc_object_t *p_obj );
+
+/*
+ * Services discovery
+ * Basically you just listen to Service discovery event through the
+ * sd's event manager.
+ * That's how the playlist get's Service Discovery information
+ */
+
 /***********************************************************************
  * Create
  ***********************************************************************/
-services_discovery_t *vlc_sd_Create( vlc_object_t *p_super )
+services_discovery_t *vlc_sd_Create( vlc_object_t *p_super,
+                                     const char *cfg )
 {
     services_discovery_t *p_sd;
 
@@ -72,16 +115,14 @@ services_discovery_t *vlc_sd_Create( vlc_object_t *p_super )
                               "services discovery" );
     if( !p_sd )
         return NULL;
+    free(config_ChainCreate( &p_sd->psz_name, &p_sd->p_cfg, cfg ));
 
-    vlc_event_manager_init( &p_sd->event_manager, p_sd, (vlc_object_t *)p_sd );
-    vlc_event_manager_register_event_type( &p_sd->event_manager,
-            vlc_ServicesDiscoveryItemAdded );
-    vlc_event_manager_register_event_type( &p_sd->event_manager,
-            vlc_ServicesDiscoveryItemRemoved );
-    vlc_event_manager_register_event_type( &p_sd->event_manager,
-            vlc_ServicesDiscoveryStarted );
-    vlc_event_manager_register_event_type( &p_sd->event_manager,
-            vlc_ServicesDiscoveryEnded );
+    vlc_event_manager_t *em = &p_sd->event_manager;
+    vlc_event_manager_init( em, p_sd, (vlc_object_t *)p_sd );
+    vlc_event_manager_register_event_type(em, vlc_ServicesDiscoveryItemAdded);
+    vlc_event_manager_register_event_type(em, vlc_ServicesDiscoveryItemRemoved);
+    vlc_event_manager_register_event_type(em, vlc_ServicesDiscoveryStarted);
+    vlc_event_manager_register_event_type(em, vlc_ServicesDiscoveryEnded);
 
     vlc_object_set_destructor( p_sd, services_discovery_Destructor );
     vlc_object_attach( p_sd, p_super );
@@ -92,25 +133,25 @@ services_discovery_t *vlc_sd_Create( vlc_object_t *p_super )
 /***********************************************************************
  * Stop
  ***********************************************************************/
-bool vlc_sd_Start ( services_discovery_t * p_sd, const char *module )
+bool vlc_sd_Start ( services_discovery_t * p_sd )
 {
     assert(!p_sd->p_module);
 
-    p_sd->p_module = module_need( p_sd, "services_discovery", module, true );
-
+    p_sd->p_module = module_need( p_sd, "services_discovery",
+                                  p_sd->psz_name, true );
     if( p_sd->p_module == NULL )
     {
         msg_Err( p_sd, "no suitable services discovery module" );
         return false;
     }
-        
+
     vlc_event_t event = {
         .type = vlc_ServicesDiscoveryStarted
     };
     vlc_event_send( &p_sd->event_manager, &event );
     return true;
 }
-                          
+
 /***********************************************************************
  * Stop
  ***********************************************************************/
@@ -119,13 +160,20 @@ void vlc_sd_Stop ( services_discovery_t * p_sd )
     vlc_event_t event = {
         .type = vlc_ServicesDiscoveryEnded
     };
-    
+
     vlc_event_send( &p_sd->event_manager, &event );
 
     module_unneed( p_sd, p_sd->p_module );
     p_sd->p_module = NULL;
 }
 
+void vlc_sd_Destroy( services_discovery_t *p_sd )
+{
+    config_ChainDestroy( p_sd->p_cfg );
+    free( p_sd->psz_name );
+    vlc_object_release( p_sd );
+}
+
 /***********************************************************************
  * Destructor
  ***********************************************************************/
@@ -186,12 +234,20 @@ services_discovery_RemoveItem ( services_discovery_t * p_sd, input_item_t * p_it
  * Playlist - Services discovery bridge
  */
 
+struct vlc_sd_internal_t
+{
+    /* the playlist items for category and onelevel */
+    playlist_item_t      *p_node;
+    services_discovery_t *p_sd; /**< Loaded service discovery modules */
+    char                 *psz_name;
+};
+
  /* A new item has been added to a certain sd */
 static void playlist_sd_item_added( const vlc_event_t * p_event, void * user_data )
 {
     input_item_t * p_input = p_event->u.services_discovery_item_added.p_new_item;
     const char * psz_cat = p_event->u.services_discovery_item_added.psz_category;
-    playlist_item_t *p_new_item, * p_parent = user_data;
+    playlist_item_t * p_parent = user_data;
     playlist_t * p_playlist = p_parent->p_playlist;
 
     msg_Dbg( p_playlist, "Adding %s in %s",
@@ -200,8 +256,7 @@ static void playlist_sd_item_added( const vlc_event_t * p_event, void * user_dat
 
     PL_LOCK;
     /* If p_parent is in root category (this is clearly a hack) and we have a cat */
-    if( !EMPTY_STR(psz_cat) &&
-        p_parent->p_parent == p_playlist->p_root_category )
+    if( !EMPTY_STR(psz_cat) )
     {
         /* */
         playlist_item_t * p_cat;
@@ -209,19 +264,15 @@ static void playlist_sd_item_added( const vlc_event_t * p_event, void * user_dat
         if( !p_cat )
         {
             p_cat = playlist_NodeCreate( p_playlist, psz_cat,
-                                         p_parent, 0, NULL );
+                                         p_parent, PLAYLIST_END, 0, NULL );
             p_cat->i_flags &= ~PLAYLIST_SKIP_FLAG;
         }
         p_parent = p_cat;
     }
 
-    p_new_item = playlist_NodeAddInput( p_playlist, p_input, p_parent,
-                                        PLAYLIST_APPEND, PLAYLIST_END, pl_Locked );
-    if( p_new_item )
-    {
-        p_new_item->i_flags &= ~PLAYLIST_SKIP_FLAG;
-        p_new_item->i_flags &= ~PLAYLIST_SAVE_FLAG;
-    }
+    playlist_NodeAddInput( p_playlist, p_input, p_parent,
+                           PLAYLIST_APPEND, PLAYLIST_END,
+                           pl_Locked );
     PL_UNLOCK;
 }
 
@@ -229,89 +280,86 @@ static void playlist_sd_item_added( const vlc_event_t * p_event, void * user_dat
 static void playlist_sd_item_removed( const vlc_event_t * p_event, void * user_data )
 {
     input_item_t * p_input = p_event->u.services_discovery_item_removed.p_item;
-    playlist_item_t * p_parent = user_data;
-    playlist_item_t * p_pl_item;
-
-    /* First make sure that if item is a node it will be deleted.
-     * XXX: Why don't we have a function to ensure that in the playlist code ? */
-    playlist_Lock( p_parent->p_playlist );
-    p_pl_item = playlist_ItemFindFromInputAndRoot( p_parent->p_playlist,
-            p_input, p_parent, false );
+    playlist_item_t * p_sd_node = user_data;
+    playlist_t *p_playlist = p_sd_node->p_playlist;
 
-    if( p_pl_item && p_pl_item->i_children > -1 )
-        playlist_NodeDelete( p_parent->p_playlist, p_pl_item, true, false );
+    PL_LOCK;
+    playlist_item_t *p_item =
+        playlist_ItemFindFromInputAndRoot( p_playlist, p_input,
+                                           p_sd_node, false );
+    if( !p_item )
+    {
+        PL_UNLOCK; return;
+    }
+    playlist_item_t *p_parent = p_item->p_parent;
+    /* if the item was added under a category and the category node
+       becomes empty, delete that node as well */
+    if( p_parent->i_children > 1 || p_parent == p_sd_node )
+        playlist_DeleteItem( p_playlist, p_item, true );
     else
-        /* Delete the non-node item normally */
-        playlist_DeleteFromInputInParent( p_parent->p_playlist, p_input,
-                                          p_parent, pl_Locked );
-
-    playlist_Unlock( p_parent->p_playlist );
+        playlist_NodeDelete( p_playlist, p_parent, true, true );
+    PL_UNLOCK;
 }
 
-int playlist_ServicesDiscoveryAdd( playlist_t *p_playlist, const char *psz_module )
+int playlist_ServicesDiscoveryAdd( playlist_t *p_playlist,
+                                   const char *psz_name )
 {
     /* Perform the addition */
-    msg_Dbg( p_playlist, "adding services_discovery %s...", psz_module );
+    services_discovery_t *p_sd;
 
-    services_discovery_t *p_sd = vlc_sd_Create( VLC_OBJECT(p_playlist) );
+    msg_Dbg( p_playlist, "adding services_discovery %s...", psz_name );
+    p_sd = vlc_sd_Create( VLC_OBJECT(p_playlist), psz_name );
     if( !p_sd )
         return VLC_ENOMEM;
 
-    module_t *m = module_find_by_shortcut( psz_module );
-    if( !m )
-    {
-        msg_Err( p_playlist, "No such module: %s", psz_module );
-        vlc_sd_Destroy( p_sd );
-        return VLC_EGENERIC;
-    }
-
     /* Free in playlist_ServicesDiscoveryRemove */
     vlc_sd_internal_t * p_sds = malloc( sizeof(*p_sds) );
     if( !p_sds )
     {
         vlc_sd_Destroy( p_sd );
-        module_release( m );
         return VLC_ENOMEM;
     }
 
-    playlist_item_t * p_cat;
-    playlist_item_t * p_one;
+    playlist_item_t *p_node;
+
+    /* Look for configuration chain "longname" */
+    const char *psz_longname = "?";
+    if( p_sd->p_cfg )
+    {
+        config_chain_t *cfg = p_sd->p_cfg;
+        while( cfg )
+        {
+            if( cfg->psz_name && !strcmp( cfg->psz_name, "longname" ) )
+            {
+                psz_longname = cfg->psz_value;
+                break;
+            }
+            cfg = cfg->p_next;
+        }
+    }
 
     PL_LOCK;
-    playlist_NodesPairCreate( p_playlist, module_get_name( m, true ),
-                              &p_cat, &p_one, false );
+    p_node = playlist_NodeCreate( p_playlist, psz_longname,
+                                  p_playlist->p_root, PLAYLIST_END, 0, NULL );
     PL_UNLOCK;
-    module_release( m );
-
-    vlc_event_attach( services_discovery_EventManager( p_sd ),
-                      vlc_ServicesDiscoveryItemAdded,
-                      playlist_sd_item_added, p_one );
-        
-    vlc_event_attach( services_discovery_EventManager( p_sd ),
-                      vlc_ServicesDiscoveryItemAdded,
-                      playlist_sd_item_added, p_cat );
 
-    vlc_event_attach( services_discovery_EventManager( p_sd ),
-                      vlc_ServicesDiscoveryItemRemoved,
-                      playlist_sd_item_removed, p_one );
+    vlc_event_manager_t *em = services_discovery_EventManager( p_sd );
+    vlc_event_attach( em, vlc_ServicesDiscoveryItemAdded,
+                      playlist_sd_item_added, p_node );
 
-    vlc_event_attach( services_discovery_EventManager( p_sd ),
-                      vlc_ServicesDiscoveryItemRemoved,
-                      playlist_sd_item_removed, p_cat );
+    vlc_event_attach( em, vlc_ServicesDiscoveryItemRemoved,
+                      playlist_sd_item_removed, p_node );
 
-    if( !vlc_sd_Start( p_sd, psz_module ) )
+    if( !vlc_sd_Start( p_sd ) )
     {
         vlc_sd_Destroy( p_sd );
         free( p_sds );
         return VLC_EGENERIC;
     }
 
-    /* We want tree-view for service directory */
-    p_one->p_input->b_prefers_tree = true;
     p_sds->p_sd = p_sd;
-    p_sds->p_one = p_one;
-    p_sds->p_cat = p_cat;
-    p_sds->psz_name = strdup( psz_module );
+    p_sds->p_node = p_node;
+    p_sds->psz_name = strdup( psz_name );
 
     PL_LOCK;
     TAB_APPEND( pl_priv(p_playlist)->i_sds, pl_priv(p_playlist)->pp_sds, p_sds );
@@ -352,31 +400,16 @@ int playlist_ServicesDiscoveryRemove( playlist_t * p_playlist,
     vlc_event_detach( services_discovery_EventManager( p_sd ),
                         vlc_ServicesDiscoveryItemAdded,
                         playlist_sd_item_added,
-                        p_sds->p_one );
-
-    vlc_event_detach( services_discovery_EventManager( p_sd ),
-                        vlc_ServicesDiscoveryItemAdded,
-                        playlist_sd_item_added,
-                        p_sds->p_cat );
-
-    vlc_event_detach( services_discovery_EventManager( p_sd ),
-                        vlc_ServicesDiscoveryItemRemoved,
-                        playlist_sd_item_removed,
-                        p_sds->p_one );
+                        p_sds->p_node );
 
     vlc_event_detach( services_discovery_EventManager( p_sd ),
                         vlc_ServicesDiscoveryItemRemoved,
                         playlist_sd_item_removed,
-                        p_sds->p_cat );
+                        p_sds->p_node );
 
     /* Remove the sd playlist node if it exists */
     PL_LOCK;
-    if( p_sds->p_cat != p_playlist->p_root_category &&
-        p_sds->p_one != p_playlist->p_root_onelevel )
-    {
-        playlist_NodeDelete( p_playlist, p_sds->p_cat, true, false );
-        playlist_NodeDelete( p_playlist, p_sds->p_one, true, false );
-    }
+    playlist_NodeDelete( p_playlist, p_sds->p_node, true, false );
     PL_UNLOCK;
 
     vlc_sd_Destroy( p_sd );