[vlc-commits] commit: lua_sd: Implement the descriptor request. ( Rémi Duraffort )

git at videolan.org git at videolan.org
Tue Nov 2 08:30:48 CET 2010


vlc | branch: master | Rémi Duraffort <ivoire at videolan.org> | Tue Nov  2 08:26:27 2010 +0100| [3c12dc172ec1d87ab8560f7a9ede5f6658d2836e] | committer: Rémi Duraffort 

lua_sd: Implement the descriptor request.

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=3c12dc172ec1d87ab8560f7a9ede5f6658d2836e
---

 modules/misc/lua/services_discovery.c |   89 +++++++++++++++++++++++++++++++--
 1 files changed, 85 insertions(+), 4 deletions(-)

diff --git a/modules/misc/lua/services_discovery.c b/modules/misc/lua/services_discovery.c
index 097e6b4..7a87b9d 100644
--- a/modules/misc/lua/services_discovery.c
+++ b/modules/misc/lua/services_discovery.c
@@ -34,8 +34,9 @@
  * Local prototypes
  *****************************************************************************/
 static void *Run( void * );
-static int   DoSearch( services_discovery_t *p_sd, const char *psz_query );
-static int   Control( services_discovery_t *p_sd, int i_command, va_list args );
+static int DoSearch( services_discovery_t *p_sd, const char *psz_query );
+static int FillDescriptor( services_discovery_t *, services_discovery_descriptor_t * );
+static int Control( services_discovery_t *p_sd, int i_command, va_list args );
 
 static const char * const ppsz_sd_options[] = { "sd", "longname", NULL };
 
@@ -263,8 +264,12 @@ static int Control( services_discovery_t *p_sd, int i_command, va_list args )
         break;
     }
 
-    case SD_CMD_CAPABILITIES:
-        return VLC_EGENERIC;
+    case SD_CMD_DESCRIPTOR:
+    {
+        services_discovery_descriptor_t *p_desc = va_arg( args,
+                                services_discovery_descriptor_t * );
+        return FillDescriptor( p_sd, p_desc );
+    }
     }
 
     return VLC_SUCCESS;
@@ -299,3 +304,79 @@ static int DoSearch( services_discovery_t *p_sd, const char *psz_query )
 
     return VLC_SUCCESS;
 }
+
+/** List of capabilities */
+static const char *const ppsz_capabilities[] = {
+    "search",
+    NULL
+};
+
+static int FillDescriptor( services_discovery_t *p_sd,
+                           services_discovery_descriptor_t *p_desc )
+{
+    services_discovery_sys_t *p_sys = p_sd->p_sys;
+    int i_ret = VLC_EGENERIC;
+
+    lua_State *L = luaL_newstate();
+    if( luaL_dofile( L, p_sys->psz_filename ) )
+    {
+        msg_Err( p_sd, "Error loading script %s: %s", p_sys->psz_filename,
+                 lua_tostring( L, -1 ) );
+        goto end;
+    }
+
+    /* Call the "descriptor" function */
+    lua_getglobal( L, "descriptor" );
+    if( !lua_isfunction( L, -1 ) || lua_pcall( L, 0, 1, 0 ) )
+    {
+        msg_Warn( p_sd, "Error getting the descriptor in '%s': %s",
+                  p_sys->psz_filename, lua_tostring( L, -1 ) );
+        goto end;
+    }
+
+    /* Get the different fields of the returned table */
+    lua_getfield( L, -1, "short_description" );
+    p_desc->psz_short_desc = luaL_strdupornull( L, -1 );
+    lua_pop( L, 1 );
+
+    lua_getfield( L, -1, "icon" );
+    p_desc->psz_icon_url = luaL_strdupornull( L, -1 );
+    lua_pop( L, 1 );
+
+    lua_getfield( L, -1, "url" );
+    p_desc->psz_url = luaL_strdupornull( L, -1 );
+    lua_pop( L, 1 );
+
+    lua_getfield( L, -1, "capabilities" );
+    p_desc->i_capabilities = 0;
+    if( lua_istable( L, -1 ) )
+    {
+        lua_pushnil( L );
+        while( lua_next( L, -2 ) != 0 )
+        {
+            /* Key is at index -2 and value at index -1 */
+            const char *psz_cap = luaL_checkstring( L, -1 );
+            int i_cap = 0;
+            const char *psz_iter;
+            for( psz_iter = *ppsz_capabilities; psz_iter;
+                 psz_iter = ppsz_capabilities[ ++i_cap ] )
+            {
+                if( !strcmp( psz_iter, psz_cap ) )
+                {
+                    p_desc->i_capabilities |= 1 << i_cap;
+                    break;
+                }
+            }
+            if( !psz_iter )
+                msg_Warn( p_sd, "Services discovery capability '%s' unknown in "
+                                "script '%s'", psz_cap, p_sys->psz_filename );
+        }
+    }
+    lua_pop( L, 1 );
+    i_ret = VLC_SUCCESS;
+
+end:
+    lua_close( L );
+    return i_ret;
+
+}



More information about the vlc-commits mailing list