From e304d33dd5e2d21dc4085ffab5ce14c70e8bbdad Mon Sep 17 00:00:00 2001
From: Fabio Ritrovato <exsephiroth87@gmail.com>
Date: Wed, 3 Feb 2010 23:45:25 +0100
Subject: [PATCH] [GSoC] luasd: the script loading module

---
 modules/misc/lua/Modules.am           |    1 +
 modules/misc/lua/services_discovery.c |  184 +++++++++++++++++++++++++++++++++
 modules/misc/lua/vlc.c                |   93 ++++++++++++++++-
 modules/misc/lua/vlc.h                |    2 +
 4 files changed, 279 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 851fbfa..f7c5b1c 100644
--- a/modules/misc/lua/Modules.am
+++ b/modules/misc/lua/Modules.am
@@ -5,6 +5,7 @@ SOURCES_lua = \
 	intf.c \
 	meta.c \
 	demux.c \
+	services_discovery.c \
 	vlc.c \
 	vlc.h \
 	libs.h \
diff --git a/modules/misc/lua/services_discovery.c b/modules/misc/lua/services_discovery.c
new file mode 100644
index 0000000..d37133d
--- /dev/null
+++ b/modules/misc/lua/services_discovery.c
@@ -0,0 +1,184 @@
+/*****************************************************************************
+ * services_discovery.c : Services discovery using lua scripts
+ *****************************************************************************
+ * Copyright (C) 2009 the VideoLAN team
+ *
+ * Authors: Fabio Ritrovato <exsephiroth87 at videolan dot 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
+ * (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 const char * const ppsz_sd_options[] = { "sd", NULL };
+
+/*****************************************************************************
+ * 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 } };
+
+/*****************************************************************************
+ *
+ *****************************************************************************/
+static char *FindFile( vlc_object_t *p_this, const char *psz_name )
+{
+    char  *ppsz_dir_list[] = { NULL, NULL, NULL, NULL };
+    char **ppsz_dir;
+    vlclua_dir_list( p_this, "sd", ppsz_dir_list );
+    for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
+    {
+        char *psz_filename;
+        FILE *fp;
+        if( asprintf( &psz_filename, "%s"DIR_SEP"%s.lua", *ppsz_dir,
+                      psz_name ) < 0 )
+        {
+            vlclua_dir_list_free( ppsz_dir_list );
+            return NULL;
+        }
+        fp = utf8_fopen( psz_filename, "r" );
+        if( fp )
+        {
+            fclose( fp );
+            vlclua_dir_list_free( ppsz_dir_list );
+            return psz_filename;
+        }
+        free( psz_filename );
+    }
+    vlclua_dir_list_free( ppsz_dir_list );
+    return 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;
+    char *psz_name = NULL;
+
+    config_ChainParse( p_sd, "lua-", ppsz_sd_options, p_sd->p_cfg );
+    psz_name = var_CreateGetString( p_sd, "lua-sd" );
+    if( !( p_sys = malloc( sizeof( services_discovery_sys_t ) ) ) )
+        return VLC_ENOMEM;
+    p_sd->p_sys = p_sys;
+    p_sys->psz_filename = FindFile( p_this, psz_name );
+    if( !p_sys->psz_filename )
+    {
+        msg_Err( p_sd, "Couldn't find lua services discovery script \"%s\".",
+                 psz_name );
+        free( psz_name );
+        free( p_sys );
+        return VLC_EGENERIC;
+    }
+    free( psz_name );
+    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;
+    }
+    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_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;
+    }
+    //msg_Info( p_sd, "LuaSD script loaded: %s", p_sd->psz_name );
+    return NULL;
+}
diff --git a/modules/misc/lua/vlc.c b/modules/misc/lua/vlc.c
index dc49903..46997fa 100644
--- a/modules/misc/lua/vlc.c
+++ b/modules/misc/lua/vlc.c
@@ -40,6 +40,7 @@
 #include <vlc_meta.h>
 #include <vlc_charset.h>
 #include <vlc_aout.h>
+#include <vlc_services_discovery.h>
 
 #include <lua.h>        /* Low level lua C API */
 #include <lauxlib.h>    /* Higher level C API */
@@ -50,13 +51,14 @@
 /*****************************************************************************
  * Module descriptor
  *****************************************************************************/
-
 #define INTF_TEXT N_("Lua interface")
 #define INTF_LONGTEXT N_("Lua interface module to load")
 
 #define CONFIG_TEXT N_("Lua interface configuration")
 #define CONFIG_LONGTEXT N_("Lua interface configuration string. Format is: '[\"<interface module name>\"] = { <option> = <value>, ...}, ...'.")
 
+static int vlc_sd_probe_Open( vlc_object_t * );
+
 vlc_module_begin ()
         set_shortname( N_( "Lua Art" ) )
         set_description( N_("Fetch artwork using lua scripts") )
@@ -112,6 +114,16 @@ vlc_module_begin ()
         add_shortcut( "luaextension" )
         set_capability( "extension", 1 )
         set_callbacks( Open_Extension, Close_Extension )
+
+    add_submodule ()
+        set_description( N_("Lua SD Module") )
+        add_shortcut( "luasd" )
+        set_capability( "services_discovery", 0 )
+        add_string( "lua-sd", "", NULL, "", "", false )
+        set_callbacks( Open_LuaSD, Close_LuaSD )
+
+    VLC_SD_PROBE_SUBMODULE
+
 vlc_module_end ()
 
 /*****************************************************************************
@@ -513,3 +525,82 @@ int __vlclua_playlist_add_internal( vlc_object_t *p_this, lua_State *L,
     }
     return i_count;
 }
+
+static int vlc_sd_probe_Open( vlc_object_t *obj )
+{
+    vlc_probe_t *probe = (vlc_probe_t *)obj;
+    char **ppsz_filelist = NULL;
+    char **ppsz_fileend  = NULL;
+    char **ppsz_file;
+    char *psz_name;
+    char  *ppsz_dir_list[] = { NULL, NULL, NULL, NULL };
+    char **ppsz_dir;
+    vlclua_dir_list( obj, "sd", ppsz_dir_list );
+    for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
+    {
+        int i_files;
+        if( ppsz_filelist )
+        {
+            for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
+                 ppsz_file++ )
+                free( *ppsz_file );
+            free( ppsz_filelist );
+            ppsz_filelist = NULL;
+        }
+        i_files = utf8_scandir( *ppsz_dir, &ppsz_filelist, file_select,
+                                file_compare );
+        if( i_files < 1 ) continue;
+        ppsz_fileend = ppsz_filelist + i_files;
+        for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend; ppsz_file++ )
+        {
+            char  *psz_filename;
+            if( asprintf( &psz_filename,
+                          "%s" DIR_SEP "%s", *ppsz_dir, *ppsz_file ) < 0 )
+            {
+                goto error;
+            }
+            FILE *fd = utf8_fopen( psz_filename, "r" );
+            if( fd )
+            {
+                char description[256];
+                if( fgets( description, 256, fd ) != NULL )
+                {
+                    char *temp = strchr( description, '\n' );
+                    if( temp )
+                        *temp = '\0';
+                    *(*ppsz_file + strlen(*ppsz_file) - 4 )= '\0';
+                    if( asprintf( &psz_name, "lua{sd=%s}", *ppsz_file ) < 0 )
+                    {
+                        fclose( fd );
+                        free( psz_filename );
+                        goto error;
+                    }
+                    vlc_sd_probe_Add( probe, psz_name,
+                                      description + 17 );
+                    free( psz_name );
+                }
+                fclose( fd );
+            }
+            free( psz_filename );
+        }
+    }
+    if( ppsz_filelist )
+    {
+        for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
+             ppsz_file++ )
+            free( *ppsz_file );
+        free( ppsz_filelist );
+    }
+    vlclua_dir_list_free( ppsz_dir_list );
+    return VLC_PROBE_CONTINUE;
+error:
+    if( ppsz_filelist )
+    {
+        for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
+             ppsz_file++ )
+            free( *ppsz_file );
+        free( ppsz_filelist );
+    }
+    vlclua_dir_list_free( ppsz_dir_list );
+    return VLC_ENOMEM;
+}
diff --git a/modules/misc/lua/vlc.h b/modules/misc/lua/vlc.h
index 6aa6b66..8215dbc 100644
--- a/modules/misc/lua/vlc.h
+++ b/modules/misc/lua/vlc.h
@@ -57,6 +57,8 @@ void Close_LuaIntf( vlc_object_t * );
 int Open_Extension( vlc_object_t * );
 void Close_Extension( vlc_object_t * );
 
+int Open_LuaSD( vlc_object_t * );
+void Close_LuaSD( vlc_object_t * );
 
 /*****************************************************************************
  * Lua debug
-- 
1.6.0.4

