From e5eb61579255ea3d412cd449833019cad768af45 Mon Sep 17 00:00:00 2001
From: Fabio Ritrovato <exsephiroth87@gmail.com>
Date: Sat, 25 Jul 2009 13:30:33 +0200
Subject: [PATCH] [GSoC] luasd: lua backend

---
 modules/misc/lua/Modules.am           |    1 +
 modules/misc/lua/libs/sd.c            |  143 +++++++++++++++++++
 modules/misc/lua/services_discovery.c |  246 +++++++++++++++++++++++++++++++++
 modules/misc/lua/vlc.c                |   60 ++++++++-
 modules/misc/lua/vlc.h                |    3 +
 5 files changed, 452 insertions(+), 1 deletions(-)
 create mode 100644 modules/misc/lua/services_discovery.c

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/services_discovery.c b/modules/misc/lua/services_discovery.c
new file mode 100644
index 0000000..00a6650
--- /dev/null
+++ b/modules/misc/lua/services_discovery.c
@@ -0,0 +1,246 @@
+/*****************************************************************************
+ * services_discovery.c : Services discovery using lua scripts
+ *****************************************************************************
+ * Copyright (C) 2009 the VideoLAN team
+ *
+ * Authors: 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
+ * the Free Software Foundation; either version 2 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.
+ *
+ * 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.
+ *****************************************************************************/
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <vlc_common.h>
+#include <vlc_services_discovery.h>
+#include <vlc_playlist.h>
+
+#include "vlc.h"
+#include "libs.h"
+
+/*****************************************************************************
+ * Local prototypes
+ *****************************************************************************/
+
+static void *Run( void * );
+static int ItemPut( services_discovery_t *, const char * );
+static int ItemGet( services_discovery_t *, const char *, input_item_t * );
+static int ItemDelete( services_discovery_t *, input_item_t * );
+
+/*****************************************************************************
+ * Local structures
+ *****************************************************************************/
+
+struct services_discovery_sys_t
+{
+    lua_State *L;
+    char *psz_filename;
+    vlc_thread_t thread;
+};
+static const luaL_Reg p_reg[] = { { NULL, NULL } };
+
+/*****************************************************************************
+ * Open: initialize and create stuff
+ *****************************************************************************/
+int Open_LuaSD( vlc_object_t *p_this )
+{
+    services_discovery_t *p_sd = ( services_discovery_t * )p_this;
+    services_discovery_sys_t *p_sys;
+    lua_State *L;
+
+    if( !( p_sys = malloc( sizeof( services_discovery_sys_t ) ) ) )
+        return VLC_ENOMEM;
+    p_sd->p_sys = p_sys;
+    msg_Info( p_sd, "Running LuaSD: %s", p_sd->psz_name );
+    if( asprintf( &p_sys->psz_filename,
+                  "%s" DIR_SEP "lua" DIR_SEP "sd" DIR_SEP "%s.lua",
+                   config_GetUserDataDir(), p_sd->psz_name ) < 0 )
+    {
+        free( p_sys );
+        return VLC_ENOMEM;
+    }
+    L = luaL_newstate();
+    if( !L )
+    {
+        msg_Err( p_sd, "Could not create new Lua State" );
+        free( p_sys->psz_filename );
+        free( p_sys );
+        return VLC_EGENERIC;
+    }
+    luaL_openlibs( L );
+    luaL_register( L, "vlc", p_reg );
+    lua_pushlightuserdata( L, p_sd );
+    lua_setfield( L, -2, "private" );
+    luaopen_input( L );
+    luaopen_msg( L );
+    luaopen_misc( L );
+    luaopen_net( L );
+    luaopen_object( L );
+    luaopen_playlist( L );
+    luaopen_sd( L );
+    luaopen_strings( L );
+    luaopen_variables( L );
+    luaopen_stream( L );
+    lua_pop( L, 1 );
+    if( luaL_dofile( L, p_sys->psz_filename ) )
+    {
+
+        msg_Err( p_sd, "Error loading script %s: %s", p_sys->psz_filename,
+                  lua_tostring( L, lua_gettop( L ) ) );
+        lua_pop( L, 1 );
+        free( p_sys->psz_filename );
+        free( p_sys );
+        return VLC_EGENERIC;
+    }
+    lua_getglobal( L, "item_put" );
+    if( lua_isfunction( L, lua_gettop( L ) ) )
+        p_sd->pf_item_put = ItemPut;
+    lua_getglobal( L, "item_get" );
+    if( lua_isfunction( L, lua_gettop( L ) ) )
+        p_sd->pf_item_get = ItemGet;
+    lua_getglobal( L, "item_delete" );
+    if( lua_isfunction( L, lua_gettop( L ) ) )
+        p_sd->pf_item_delete = ItemDelete;
+    p_sys->L = L;
+    if( vlc_clone (&p_sd->p_sys->thread, Run, p_sd, VLC_THREAD_PRIORITY_LOW) )
+    {
+        free( p_sys->psz_filename );
+        free( p_sys );
+        return VLC_EGENERIC;
+    }
+    return VLC_SUCCESS;
+}
+
+/*****************************************************************************
+ * Close: cleanup
+ *****************************************************************************/
+void Close_LuaSD( vlc_object_t *p_this )
+{
+    services_discovery_t *p_sd = ( services_discovery_t * )p_this;
+
+    vlc_cancel (p_sd->p_sys->thread);
+    vlc_join (p_sd->p_sys->thread, NULL);
+    free( p_sd->p_sys->psz_filename );
+    lua_close( p_sd->p_sys->L );
+    free( p_sd->p_sys );
+}
+
+/*****************************************************************************
+ * Run: Thread entry-point
+ ****************************************************************************/
+static void* Run( void *data )
+{
+    services_discovery_t *p_sd = ( services_discovery_t * )data;
+    services_discovery_sys_t *p_sys = p_sd->p_sys;
+    lua_State *L = p_sys->L;
+
+    lua_getglobal( L, "main" );
+    if( !lua_isfunction( L, lua_gettop( L ) ) || lua_pcall( L, 0, 1, 0 ) )
+    {
+        msg_Err( p_sd, "Error while runing script %s, "
+                  "function main(): %s", p_sys->psz_filename,
+                  lua_tostring( L, lua_gettop( L ) ) );
+        lua_pop( L, 1 );
+        return NULL;
+    }
+    return NULL;
+}
+
+/*****************************************************************************
+ * Item oprations callbacks
+ ****************************************************************************/
+static int ItemPut( services_discovery_t *p_sd, const char *psz_string )
+{
+    lua_State *L = p_sd->p_sys->L;
+
+    lua_getglobal( L, "item_put" );
+    if( !lua_isfunction( L, lua_gettop( L ) ) )
+    {
+        msg_Err( p_sd, "Error while runing script %s, "
+                  "function item_put() not found", p_sd->p_sys->psz_filename );
+        lua_pop( L, 1 );
+        return VLC_EGENERIC;
+    }
+    lua_pushstring( L, psz_string );
+    if( lua_pcall( L, 1, 1, 0 ) )
+    {
+        msg_Err( p_sd, "Error while runing script %s, "
+                  "function item_put(): %s", p_sd->p_sys->psz_filename,
+                  lua_tostring( L, lua_gettop( L ) ) );
+        lua_pop( L, 1 );
+        return VLC_EGENERIC;
+    }
+    return VLC_SUCCESS;
+}
+
+static int ItemGet( services_discovery_t *p_sd, const char *psz_string,
+                    input_item_t *p_item )
+{
+    lua_State *L = p_sd->p_sys->L;
+
+    lua_getglobal( L, "item_get" );
+    if( !lua_isfunction( L, lua_gettop( L ) ) )
+    {
+        msg_Err( p_sd, "Error while runing script %s, "
+                  "function item_get() not found",p_sd->p_sys->psz_filename );
+        lua_pop( L, 1 );
+        return VLC_EGENERIC;
+    }
+    /* TODO: is this the best way to do it? */
+    lua_pushstring( L, input_item_GetURL( p_item ) );
+    lua_pushstring( L, psz_string );
+    if( lua_pcall( L, 2, 1, 0 ) )
+    {
+        msg_Err( p_sd, "Error while runing script %s, "
+                  "function item_get(): %s", p_sd->p_sys->psz_filename,
+                  lua_tostring( L, lua_gettop( L ) ) );
+        lua_pop( L, 1 );
+        return VLC_EGENERIC;
+    }
+    return VLC_SUCCESS;
+}
+
+static int ItemDelete( services_discovery_t *p_sd, input_item_t *p_item )
+{
+    lua_State *L = p_sd->p_sys->L;
+
+    lua_getglobal( L, "item_delete" );
+    if( !lua_isfunction( L, lua_gettop( L ) ) )
+    {
+        msg_Err( p_sd, "Error while runing script %s, "
+                  "function item_delete() not found", p_sd->p_sys->psz_filename );
+        lua_pop( L, 1 );
+        return VLC_EGENERIC;
+    }
+    input_item_t **udata = (input_item_t **)
+                           lua_newuserdata( L, sizeof( input_item_t * ) );
+    *udata = p_item;
+    if( luaL_newmetatable( L, "input_item_t" ) )
+    {
+        lua_pushstring( L, "none of your business" );
+        lua_setfield( L, -2, "__metatable" );
+    }
+    lua_setmetatable( L, -2 );
+    if( lua_pcall( L, 1, 1, 0 ) )
+    {
+        msg_Err( p_sd, "Error while runing script %s, "
+                  "function item_delete(): %s", p_sd->p_sys->psz_filename,
+                  lua_tostring( L, lua_gettop( L ) ) );
+        lua_pop( L, 1 );
+        return VLC_EGENERIC;
+    }
+    return VLC_SUCCESS;
+}
diff --git a/modules/misc/lua/vlc.c b/modules/misc/lua/vlc.c
index f59c391..04c21b4 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,64 @@ 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_GetUserDataDir() ) >= 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

