From 284e30d1cd0cfb2beb97ea7eac92e8d82d2da12c Mon Sep 17 00:00:00 2001
From: Fabio Ritrovato <exsephiroth87@gmail.com>
Date: Sat, 16 Jan 2010 21:43:41 +0100
Subject: [PATCH] [GSoC] luasd: edit the SD probe to allow script loading

---
 include/vlc_services_discovery.h       |    8 ++++--
 modules/services_discovery/mediadirs.c |    6 ++--
 modules/services_discovery/shout.c     |    6 ++--
 modules/services_discovery/xcb_apps.c  |    2 +-
 src/playlist/services_discovery.c      |   37 +++++++++++++++++++++++++------
 5 files changed, 42 insertions(+), 17 deletions(-)

diff --git a/include/vlc_services_discovery.h b/include/vlc_services_discovery.h
index 7094def..237eedc 100644
--- a/include/vlc_services_discovery.h
+++ b/include/vlc_services_discovery.h
@@ -48,6 +48,9 @@ struct services_discovery_t
 
     vlc_event_manager_t event_manager;      /* Accessed through Setters for non class function */
 
+    char *psz_name;
+    char *psz_parent;
+
     services_discovery_sys_t *p_sys;
 };
 
@@ -89,10 +92,9 @@ VLC_EXPORT( vlc_event_manager_t *,  services_discovery_EventManager, ( services_
 VLC_EXPORT( void,                   services_discovery_AddItem, ( services_discovery_t * p_this, input_item_t * p_item, const char * psz_category ) );
 VLC_EXPORT( void,                   services_discovery_RemoveItem, ( services_discovery_t * p_this, input_item_t * p_item ) );
 
-
 /* SD probing */
 
-VLC_EXPORT(int, vlc_sd_probe_Add, (vlc_probe_t *, const char *, const char *));
+VLC_EXPORT(int, vlc_sd_probe_Add, (vlc_probe_t *, const char *, const char *, const char *));
 
 #define VLC_SD_PROBE_SUBMODULE \
     add_submodule() \
@@ -102,7 +104,7 @@ VLC_EXPORT(int, vlc_sd_probe_Add, (vlc_probe_t *, const char *, const char *));
 #define VLC_SD_PROBE_HELPER(name, longname) \
 static int vlc_sd_probe_Open (vlc_object_t *obj) \
 { \
-    return vlc_sd_probe_Add ((struct vlc_probe_t *)obj, name, longname); \
+    return vlc_sd_probe_Add ((struct vlc_probe_t *)obj, name, longname, NULL); \
 }
 
 /** @} */
diff --git a/modules/services_discovery/mediadirs.c b/modules/services_discovery/mediadirs.c
index 1dd4ce8..4629988 100644
--- a/modules/services_discovery/mediadirs.c
+++ b/modules/services_discovery/mediadirs.c
@@ -355,7 +355,7 @@ static int vlc_sd_probe_Open( vlc_object_t *obj )
 {
     vlc_probe_t *probe = (vlc_probe_t *)obj;
 
-    vlc_sd_probe_Add( probe, "video_dir", N_("My Videos") );
-    vlc_sd_probe_Add( probe, "audio_dir", N_("My Music") );
-    return vlc_sd_probe_Add( probe, "picture_dir", N_("My Pictures") );
+    vlc_sd_probe_Add( probe, "video_dir", N_("My Videos"), NULL );
+    vlc_sd_probe_Add( probe, "audio_dir", N_("My Music"), NULL );
+    return vlc_sd_probe_Add( probe, "picture_dir", N_("My Pictures"), NULL );
 }
diff --git a/modules/services_discovery/shout.c b/modules/services_discovery/shout.c
index b8f40f1..fba6278 100644
--- a/modules/services_discovery/shout.c
+++ b/modules/services_discovery/shout.c
@@ -282,8 +282,8 @@ static int vlc_sd_probe_Open( vlc_object_t *obj )
 {
     vlc_probe_t *probe = (vlc_probe_t *)obj;
 
-    vlc_sd_probe_Add( probe, "shoutcast", N_("Shoutcast Radio") );
-    vlc_sd_probe_Add( probe, "shoutcasttv", N_("Shoutcast TV") );
-    vlc_sd_probe_Add( probe, "frenchtv", N_("French TV") );
+    vlc_sd_probe_Add( probe, "shoutcast", N_("Shoutcast Radio"), NULL );
+    vlc_sd_probe_Add( probe, "shoutcasttv", N_("Shoutcast TV"), NULL );
+    vlc_sd_probe_Add( probe, "frenchtv", N_("French TV"), NULL );
     return VLC_PROBE_CONTINUE;
 }
diff --git a/modules/services_discovery/xcb_apps.c b/modules/services_discovery/xcb_apps.c
index adab54c..44284e6 100644
--- a/modules/services_discovery/xcb_apps.c
+++ b/modules/services_discovery/xcb_apps.c
@@ -79,7 +79,7 @@ static int vlc_sd_probe_Open (vlc_object_t *obj)
     if (xcb_connection_has_error (conn))
         return VLC_EGENERIC;
     xcb_disconnect (conn);
-    return vlc_sd_probe_Add (probe, "xcb_apps", N_("Screen capture"));
+    return vlc_sd_probe_Add (probe, "xcb_apps", N_("Screen capture"), NULL);
 }
 
 /**
diff --git a/src/playlist/services_discovery.c b/src/playlist/services_discovery.c
index 355dffb..423840d 100644
--- a/src/playlist/services_discovery.c
+++ b/src/playlist/services_discovery.c
@@ -37,18 +37,20 @@ typedef struct
 {
     char *name;
     char *longname;
+    char *parent;
 } vlc_sd_probe_t;
 
 int vlc_sd_probe_Add (vlc_probe_t *probe, const char *name,
-                      const char *longname)
+                      const char *longname, const char *parent)
 {
-    vlc_sd_probe_t names = { strdup(name), strdup(longname) };
+    vlc_sd_probe_t names = { strdup(name), strdup(longname), parent ? strdup(parent) : NULL };
 
     if (unlikely (names.name == NULL || names.longname == NULL
                || vlc_probe_add (probe, &names, sizeof (names))))
     {
         free (names.name);
         free (names.longname);
+        free (names.parent);
         return VLC_ENOMEM;
     }
     return VLC_PROBE_CONTINUE;
@@ -294,6 +296,7 @@ static void playlist_sd_item_removed( const vlc_event_t * p_event, void * user_d
 
 int playlist_ServicesDiscoveryAdd( playlist_t *p_playlist, const char *psz_module )
 {
+    char *psz_longname;
     /* Perform the addition */
     msg_Dbg( p_playlist, "adding services_discovery %s...", psz_module );
 
@@ -301,13 +304,32 @@ int playlist_ServicesDiscoveryAdd( playlist_t *p_playlist, const char *psz_modul
     if( !p_sd )
         return VLC_ENOMEM;
 
+    p_sd->psz_parent = NULL;
     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;
+        size_t count;
+        vlc_sd_probe_t *tab = vlc_probe (p_playlist, "services probe", &count);
+        for( size_t i = 0; i < count; i++ )
+        {
+            if( !strcmp( tab[i].name, psz_module ) )
+            {
+                m = module_find_by_shortcut( tab[i].parent );
+                if( !m )
+                {
+                    msg_Err( p_playlist, "No such module: %s", psz_module );
+                    vlc_sd_Destroy( p_sd );
+                    return VLC_EGENERIC;
+                }
+                p_sd->psz_parent = strdup( tab[i].parent );
+                psz_longname = strdup( tab[i].longname );
+            }
+        }
     }
+    else
+        psz_longname = strdup( module_get_name( m, true ) );
+
+    p_sd->psz_name = strdup( psz_module );
 
     /* Free in playlist_ServicesDiscoveryRemove */
     vlc_sd_internal_t * p_sds = malloc( sizeof(*p_sds) );
@@ -322,7 +344,7 @@ int playlist_ServicesDiscoveryAdd( playlist_t *p_playlist, const char *psz_modul
     playlist_item_t * p_one;
 
     PL_LOCK;
-    playlist_NodesPairCreate( p_playlist, module_get_name( m, true ),
+    playlist_NodesPairCreate( p_playlist, psz_longname,
                               &p_cat, &p_one, false );
     PL_UNLOCK;
     module_release( m );
@@ -343,7 +365,7 @@ int playlist_ServicesDiscoveryAdd( playlist_t *p_playlist, const char *psz_modul
                       vlc_ServicesDiscoveryItemRemoved,
                       playlist_sd_item_removed, p_cat );
 
-    if( !vlc_sd_Start( p_sd, psz_module ) )
+    if( !vlc_sd_Start( p_sd, p_sd->psz_parent ? p_sd->psz_parent : psz_module ) )
     {
         vlc_sd_Destroy( p_sd );
         free( p_sds );
@@ -423,6 +445,7 @@ int playlist_ServicesDiscoveryRemove( playlist_t * p_playlist,
     }
     PL_UNLOCK;
 
+    free( p_sd->psz_name );
     vlc_sd_Destroy( p_sd );
     free( p_sds->psz_name );
     free( p_sds );
-- 
1.6.0.4

