From a575246966b73eab6d9646f03a371300806c0267 Mon Sep 17 00:00:00 2001
From: Fabio Ritrovato <exsephiroth87@gmail.com>
Date: Sun, 8 Nov 2009 20:56:54 +0100
Subject: [PATCH] [GSoC] luasd: lua backend

---
 modules/misc/lua/Modules.am |    1 +
 modules/misc/lua/libs/sd.c  |  143 +++++++++++++++++++++++++++++++++++++++++++
 modules/misc/lua/vlc.c      |   61 ++++++++++++++++++-
 modules/misc/lua/vlc.h      |    3 +
 4 files changed, 207 insertions(+), 1 deletions(-)

diff --git a/modules/misc/lua/Modules.am b/modules/misc/lua/Modules.am
index 0b6b38b..ee91c9e 100644
--- a/modules/misc/lua/Modules.am
+++ b/modules/misc/lua/Modules.am
@@ -2,6 +2,7 @@ SOURCES_lua = \
 	intf.c \
 	meta.c \
 	demux.c \
+	services_discovery.c \
 	vlc.c \
 	vlc.h \
 	libs.h \
diff --git a/modules/misc/lua/libs/sd.c b/modules/misc/lua/libs/sd.c
index 491b682..92ca5bc 100644
--- a/modules/misc/lua/libs/sd.c
+++ b/modules/misc/lua/libs/sd.c
@@ -5,6 +5,7 @@
  * $Id$
  *
  * Authors: Antoine Cellerier <dionoea at videolan tod org>
+ *          Fabio Ritrovato <exsephiroth87@gmail.com>
  *
  * 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
@@ -97,6 +98,144 @@ static int vlclua_sd_is_loaded( lua_State *L )
     return 1;
 }
 
+static int vlclua_sd_add_node( lua_State *L )
+{
+    services_discovery_t *p_sd = (services_discovery_t *)vlclua_get_this( L );
+    if( lua_istable( L, -1 ) )
+    {
+        lua_getfield( L, -1, "title" );
+        if( lua_isstring( L, -1 ) )
+        {
+            input_item_t *p_input = input_item_New( p_sd,
+                                                    "vlc://nop",
+                                                    strdup( lua_tostring( L, -1 ) ) );
+            lua_pop( L, 1 );
+
+            lua_getfield( L, -1, "parent" );
+            if( !lua_isnil( L, -1 ) )
+            {
+                input_item_t **pp_parent = luaL_checkudata( L, -1, "input_item_t" );
+                if( *pp_parent )
+                    input_item_AddSubItem( *pp_parent, p_input );
+                else
+                    services_discovery_AddItem( p_sd, p_input, NULL );
+            }
+            else
+                services_discovery_AddItem( p_sd, p_input, NULL );
+            input_item_t **udata = (input_item_t **)
+                                   lua_newuserdata( L, sizeof( input_item_t * ) );
+            *udata = p_input;
+            if( luaL_newmetatable( L, "input_item_t" ) )
+            {
+                lua_pushstring( L, "none of your business" );
+                lua_setfield( L, -2, "__metatable" );
+            }
+            lua_setmetatable( L, -2 );
+        }
+        else
+            msg_Err( p_sd, "vlc.sd.add_node: the \"title\" parameter can't be empty" );
+    }
+    else
+        msg_Err( p_sd, "Error parsing add_node arguments" );
+    return 1;
+}
+
+static int vlclua_sd_add_item( lua_State *L )
+{
+    services_discovery_t *p_sd = (services_discovery_t *)vlclua_get_this( L );
+    if( lua_istable( L, -1 ) )
+    {
+        lua_getfield( L, -1, "url" );
+        if( lua_isstring( L, -1 ) )
+        {
+            input_item_t *p_input = input_item_New( p_sd,
+                                                    strdup( lua_tostring( L, -1 ) ),
+                                                    strdup( lua_tostring( L, -1 ) ) );
+            lua_pop( L, 1 );
+            vlclua_read_meta_data( p_sd, L, p_input );
+            /* This one is to be tested... */
+            vlclua_read_custom_meta_data( p_sd, L, p_input );
+            lua_getfield( L, -1, "duration" );
+            if( lua_isnumber( L, -1 ) )
+               input_item_SetDuration( p_input, (lua_tonumber( L, -1 )*1e6) );
+            else if( !lua_isnil( L, -1 ) )
+                msg_Warn( p_sd, "Item duration should be a number (in seconds)." );
+            lua_pop( L, 1 );
+            lua_getfield( L, -1, "parent" );
+            if( !lua_isnil( L, -1 ) )
+            {
+                input_item_t **pp_parent = luaL_checkudata( L, -1, "input_item_t" );
+                if( *pp_parent )
+                    input_item_AddSubItem( *pp_parent, p_input );
+                else
+                    services_discovery_AddItem( p_sd, p_input, NULL );
+            }
+            else
+                services_discovery_AddItem( p_sd, p_input, NULL );
+            input_item_t **udata = (input_item_t **)
+                                   lua_newuserdata( L, sizeof( input_item_t * ) );
+            *udata = p_input;
+            if( luaL_newmetatable( L, "input_item_t" ) )
+            {
+                lua_pushstring( L, "none of your business" );
+                lua_setfield( L, -2, "__metatable" );
+            }
+            lua_setmetatable( L, -2 );
+        }
+        else
+            msg_Err( p_sd, "vlc.sd.add_item: the \"url\" parameter can't be empty" );
+    }
+    else
+        msg_Err( p_sd, "Error parsing add_item arguments" );
+    return 1;
+}
+
+static int vlclua_sd_remove_item( lua_State *L )
+{
+    services_discovery_t *p_sd = (services_discovery_t *)vlclua_get_this( L );
+    if( !lua_isnil( L, -1 ) )
+    {
+        input_item_t **pp_input = luaL_checkudata( L, -1, "input_item_t" );
+        if( *pp_input )
+            services_discovery_RemoveItem( p_sd, *pp_input );
+    }
+    return 1;
+}
+
+static int vlclua_sd_add_parameter_value( lua_State *L )
+{
+    services_discovery_t *p_sd = (services_discovery_t *)vlclua_get_this( L );
+    if( lua_istable( L, -1 ) )
+    {
+        lua_getfield( L, -1, "parameter" );
+        if( lua_isstring( L, -1 ) )
+        {
+            char *parameter = lua_tostring( L, -1 );
+            lua_pop( L, 1 );
+            lua_getfield( L, -1, "name" );
+            if( lua_isstring( L, -1 ) )
+            {
+                char *name = lua_tostring( L, -1 );
+                lua_pop( L, 1 );
+                lua_getfield( L, -1, "value" );
+                if( lua_isstring( L, -1 ) )
+                {
+                    char *value = lua_tostring( L, -1 );
+                    lua_pop( L, 1 );
+                    services_discovery_AddParameterValue( p_sd, parameter, name, "%s", value );
+                    return 1;
+                }
+            }
+            services_discovery_AddParameterValue( p_sd, parameter, NULL, NULL );
+        }
+        else
+            msg_Err( p_sd, "vlc.sd.add_parameter_value: the \"parameter\" parameter can't be empty" );
+    }
+    else
+        msg_Err( p_sd, "Error parsing add_item arguments" );
+    return 1;
+}
+
 /*****************************************************************************
  *
  *****************************************************************************/
@@ -105,6 +244,10 @@ static const luaL_Reg vlclua_sd_reg[] = {
     { "add", vlclua_sd_add },
     { "remove", vlclua_sd_remove },
     { "is_loaded", vlclua_sd_is_loaded },
+    { "add_node", vlclua_sd_add_node },
+    { "add_item", vlclua_sd_add_item },
+    { "remove_item", vlclua_sd_remove_item },
+    { "add_parameter_value", vlclua_sd_add_parameter_value },
     { NULL, NULL }
 };
 
diff --git a/modules/misc/lua/vlc.c b/modules/misc/lua/vlc.c
index 2fa37dc..15778fa 100644
--- a/modules/misc/lua/vlc.c
+++ b/modules/misc/lua/vlc.c
@@ -50,7 +50,6 @@
 /*****************************************************************************
  * Module descriptor
  *****************************************************************************/
-
 #define INTF_TEXT N_("Lua interface")
 #define INTF_LONGTEXT N_("Lua interface module to load")
 
@@ -62,6 +61,7 @@ vlc_module_begin ()
         set_description( N_("Fetch artwork using lua scripts") )
         set_capability( "art finder", 10 )
         set_callbacks( FindArt, NULL )
+        set_not_cacheable( )
 
     add_submodule ()
         add_shortcut( "luaplaylist" )
@@ -89,6 +89,65 @@ vlc_module_begin ()
         add_string( "lua-config", "", NULL,
                     CONFIG_TEXT, CONFIG_LONGTEXT, false )
         set_callbacks( Open_LuaIntf, Close_LuaIntf )
+
+    char *psz_file;
+    char *psz_dir;
+    int    i_dirlen;
+    DIR *  dir;
+    struct dirent * file;
+    if( asprintf( &psz_dir, "%s" DIR_SEP "lua" DIR_SEP "sd",
+                  config_GetUserDir( VLC_DATA_DIR ) ) >= 0 )
+    {
+        dir = opendir( psz_dir );
+        if( dir )
+        {
+            i_dirlen = strlen( psz_dir );
+            while( ( file = readdir( dir ) ) )
+            {
+                unsigned int i_len;
+                if( *file->d_name )
+                {
+                    i_len = strlen( file->d_name );
+                    if( !strcmp( file->d_name + ( i_len - 4 ), ".lua" ) )
+                    {
+                        psz_file = malloc( i_dirlen + 1 + i_len + 1 );
+                        sprintf( psz_file, "%s"DIR_SEP"%s", psz_dir, file->d_name );
+                        FILE *fd = fopen( psz_file, "r" );
+                        if( fd )
+                        {
+                            char description[256];
+                            if( fgets( description, 256, fd ) != NULL )
+                            {
+                                char *temp = strchr( description, '\n' );
+                                if( temp )
+                                    *temp = '\0';
+                                file->d_name[ i_len - 4 ] = '\0';
+                                add_submodule ()
+                                    set_shortname( strdup( file->d_name ) )
+                                    add_shortcut( strdup( file->d_name ) )
+                                    if( !strncmp( description, "--SD_Description=", 17 ) )
+                                    {
+                                        set_description( strdup( description + 17 ) )
+                                    }
+                                    else
+                                    {
+                                        set_description( strdup(file->d_name ) )
+                                    }
+                                    set_category( CAT_PLAYLIST )
+                                    set_subcategory( SUBCAT_PLAYLIST_SD )
+                                    set_capability( "services_discovery", 0 )
+                                    set_callbacks( Open_LuaSD, Close_LuaSD )
+                            }
+                            fclose( fd );
+                        }
+                        free( psz_file );
+                    }
+                }
+            }
+            closedir( dir );
+        }
+        free( psz_dir );
+    }
 vlc_module_end ()
 
 /*****************************************************************************
diff --git a/modules/misc/lua/vlc.h b/modules/misc/lua/vlc.h
index 94d69f6..60839f8 100644
--- a/modules/misc/lua/vlc.h
+++ b/modules/misc/lua/vlc.h
@@ -56,6 +56,9 @@ void Close_LuaPlaylist( vlc_object_t * );
 int Open_LuaIntf( vlc_object_t * );
 void Close_LuaIntf( vlc_object_t * );
 
+int Open_LuaSD( vlc_object_t * );
+void Close_LuaSD( vlc_object_t * );
+
 
 /*****************************************************************************
  * Lua debug
-- 
1.6.0.4

