]> git.sesse.net Git - vlc/blobdiff - src/playlist/services_discovery.c
vlc_getaddrinfo: port is unsigned, 0 means unspecified
[vlc] / src / playlist / services_discovery.c
index 9d53971d607109714a7395cd43b973acfee08737..e413f9ba1b9a5c57134a7b857cb4c6bfbbdf88bd 100644 (file)
@@ -1,24 +1,24 @@
 /*****************************************************************************
  * services_discovery.c : Manage playlist services_discovery modules
  *****************************************************************************
- * Copyright (C) 1999-2004 the VideoLAN team
+ * Copyright (C) 1999-2004 VLC authors and VideoLAN
  * $Id$
  *
  * Authors: ClĂ©ment Stenac <zorglub@videolan.org>
  *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
+ * 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 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.
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
  *
- * You should have received a copy of the GNU 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.
+ * 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"
@@ -30,6 +30,7 @@
 #include "vlc_events.h"
 #include <vlc_services_discovery.h>
 #include <vlc_probe.h>
+#include <vlc_modules.h>
 #include "playlist_internal.h"
 #include "../libvlc.h"
 
@@ -37,12 +38,13 @@ typedef struct
 {
     char *name;
     char *longname;
+    int category;
 } vlc_sd_probe_t;
 
 int vlc_sd_probe_Add (vlc_probe_t *probe, const char *name,
-                      const char *longname)
+                      const char *longname, int category)
 {
-    vlc_sd_probe_t names = { strdup(name), strdup(longname) };
+    vlc_sd_probe_t names = { strdup(name), strdup(longname), category };
 
     if (unlikely (names.name == NULL || names.longname == NULL
                || vlc_probe_add (probe, &names, sizeof (names))))
@@ -59,7 +61,7 @@ int vlc_sd_probe_Add (vlc_probe_t *probe, const char *name,
 /**
  * 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)
 {
     size_t count;
     vlc_sd_probe_t *tab = vlc_probe (obj, "services probe", &count);
@@ -72,29 +74,26 @@ char **vlc_sd_GetNames (vlc_object_t *obj, char ***pppsz_longnames)
 
     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))
+    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;
 }
 
 
-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;
-};
-
 static void services_discovery_Destructor ( vlc_object_t *p_obj );
 
 /*
@@ -104,73 +103,80 @@ static void services_discovery_Destructor ( vlc_object_t *p_obj );
  * That's how the playlist get's Service Discovery information
  */
 
-/***********************************************************************
- * Create
+/*******************************************************************//**
+ * Create a Service discovery
  ***********************************************************************/
-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;
 
-    p_sd = vlc_custom_create( p_super, sizeof( *p_sd ), VLC_OBJECT_GENERIC,
-                              "services discovery" );
+    p_sd = vlc_custom_create( p_super, sizeof( *p_sd ), "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_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_ServicesDiscoveryItemRemoveAll);
+    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 );
-
     return p_sd;
 }
 
-/***********************************************************************
- * Stop
+/*******************************************************************//**
+ * Start a Service Discovery
  ***********************************************************************/
-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
+
+/*******************************************************************//**
+ * Stop a Service Discovery
  ***********************************************************************/
 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;
 }
 
-/***********************************************************************
- * Destructor
+/*******************************************************************//**
+ * Destroy a Service Discovery
+ ***********************************************************************/
+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 of the Service Discovery
  ***********************************************************************/
 static void services_discovery_Destructor ( vlc_object_t *p_obj )
 {
@@ -179,8 +185,10 @@ static void services_discovery_Destructor ( vlc_object_t *p_obj )
     vlc_event_manager_fini( &p_sd->event_manager );
 }
 
-/***********************************************************************
- * GetLocalizedName
+/*******************************************************************//**
+ * Get the Localized Name
+ *
+ * This is useful for interfaces and libVLC
  ***********************************************************************/
 char *
 services_discovery_GetLocalizedName ( services_discovery_t * p_sd )
@@ -188,8 +196,11 @@ services_discovery_GetLocalizedName ( services_discovery_t * p_sd )
     return strdup( module_get_name( p_sd->p_module, true ) );
 }
 
-/***********************************************************************
- * EventManager
+/*******************************************************************//**
+ * Getter for the EventManager
+ *
+ * You can receive event notification
+ * This is the preferred way to get new items
  ***********************************************************************/
 vlc_event_manager_t *
 services_discovery_EventManager ( services_discovery_t * p_sd )
@@ -197,8 +208,20 @@ services_discovery_EventManager ( services_discovery_t * p_sd )
     return &p_sd->event_manager;
 }
 
-/***********************************************************************
- * AddItem
+/*******************************************************************//**
+ * Remove all items from the Service Discovery listing
+ ***********************************************************************/
+void
+services_discovery_RemoveAll ( services_discovery_t * p_sd )
+{
+    vlc_event_t event;
+    event.type = vlc_ServicesDiscoveryItemRemoveAll;
+
+    vlc_event_send( &p_sd->event_manager, &event );
+}
+
+/*******************************************************************//**
+ * Add an item to the Service Discovery listing
  ***********************************************************************/
 void
 services_discovery_AddItem ( services_discovery_t * p_sd, input_item_t * p_item,
@@ -212,8 +235,8 @@ services_discovery_AddItem ( services_discovery_t * p_sd, input_item_t * p_item,
     vlc_event_send( &p_sd->event_manager, &event );
 }
 
-/***********************************************************************
- * RemoveItem
+/*******************************************************************//**
+ * Remove an item from the Service Discovery listing
  ***********************************************************************/
 void
 services_discovery_RemoveItem ( services_discovery_t * p_sd, input_item_t * p_item )
@@ -229,6 +252,14 @@ 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 )
 {
@@ -251,7 +282,7 @@ 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;
@@ -267,67 +298,101 @@ 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_DeleteFromInput( p_parent->p_playlist, p_input, false );
+    playlist_item_t * p_sd_node = user_data;
+    playlist_t *p_playlist = p_sd_node->p_playlist;
+
+    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
+        playlist_NodeDelete( p_playlist, p_parent, true, true );
+    PL_UNLOCK;
+}
+
+/* A request to remove all ideas from SD */
+static void playlist_sd_item_removeall( const vlc_event_t * p_event, void * user_data )
+{
+    VLC_UNUSED(p_event);
+    playlist_item_t* p_sd_node = user_data;
+    if( p_sd_node == NULL ) return;
+    playlist_t* p_playlist = p_sd_node->p_playlist;
+    PL_LOCK;
+    playlist_NodeEmpty( p_playlist, p_sd_node, 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;
 
-    char *psz_name = NULL;
-    config_ChainCreate( &psz_name, &p_sd->p_cfg, psz_module );
-
-    module_t *m = module_find_by_shortcut( psz_name );
-    if( !m )
-    {
-        msg_Err( p_playlist, "No such module: %s", psz_name );
-        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_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;
-    p_node = playlist_NodeCreate( p_playlist, module_get_name( m, true ),
-                                  p_playlist->p_root, 0, NULL );
+    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,
+    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,
+    vlc_event_attach( em, vlc_ServicesDiscoveryItemRemoved,
                       playlist_sd_item_removed, p_node );
 
-    if( !vlc_sd_Start( p_sd, psz_name ) )
+    vlc_event_attach( em, vlc_ServicesDiscoveryItemRemoveAll,
+                      playlist_sd_item_removeall, p_node );
+
+    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_node->p_input->b_prefers_tree = true;
     p_sds->p_sd = p_sd;
     p_sds->p_node = p_node;
-    p_sds->psz_name = strdup( psz_module );
+    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 );
@@ -408,6 +473,32 @@ bool playlist_IsServicesDiscoveryLoaded( playlist_t * p_playlist,
     return found;
 }
 
+int playlist_ServicesDiscoveryControl( playlist_t *p_playlist, const char *psz_name, int i_control, ... )
+{
+    playlist_private_t *priv = pl_priv( p_playlist );
+    int i_ret = VLC_EGENERIC;
+    int i;
+
+    PL_LOCK;
+    for( i = 0; i < priv->i_sds; i++ )
+    {
+        vlc_sd_internal_t *sd = priv->pp_sds[i];
+        if( sd->psz_name && !strcmp( psz_name, sd->psz_name ) )
+        {
+            va_list args;
+            va_start( args, i_control );
+            i_ret = vlc_sd_control( sd->p_sd, i_control, args );
+            va_end( args );
+            break;
+        }
+    }
+
+    assert( i != priv->i_sds );
+    PL_UNLOCK;
+
+    return i_ret;
+}
+
 void playlist_ServicesDiscoveryKillAll( playlist_t *p_playlist )
 {
     playlist_private_t *priv = pl_priv( p_playlist );