]> git.sesse.net Git - vlc/blobdiff - modules/misc/lua/vlc.c
lua: Add freebox and frenchtv back in the builtin SD.
[vlc] / modules / misc / lua / vlc.c
index ca48ad681e4f5f889e2e3fbe539eab37ac8c08be..9084739a37d31ba2f8830c38d1bd44bda4aa95ee 100644 (file)
 #include <vlc_plugin.h>
 #include <vlc_meta.h>
 #include <vlc_charset.h>
+#include <vlc_fs.h>
 #include <vlc_aout.h>
+#include <vlc_services_discovery.h>
+#include <sys/stat.h>
 
 #include <lua.h>        /* Low level lua C API */
 #include <lauxlib.h>    /* Higher level C API */
 /*****************************************************************************
  * 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") )
         set_capability( "art finder", 10 )
         set_callbacks( FindArt, NULL )
 
+    add_submodule ()
+        set_shortname( N_( "Lua Meta Fetcher" ) )
+        set_description( N_("Fetch meta data using lua scripts") )
+        set_capability( "meta fetcher", 10 )
+        set_callbacks( FetchMeta, NULL )
+
+    add_submodule ()
+        set_shortname( N_( "Lua Meta Reader" ) )
+        set_description( N_("Read meta data using lua scripts") )
+        set_capability( "meta reader", 10 )
+        set_callbacks( ReadMeta, NULL )
+
     add_submodule ()
         add_shortcut( "luaplaylist" )
         set_category( CAT_INPUT )
@@ -76,7 +92,7 @@ vlc_module_begin ()
         set_description( N_("Lua Interface Module (shortcuts)") )
         add_shortcut( "luarc" )
         add_shortcut( "rc" )
-        set_capability( "interface", 15 )
+        set_capability( "interface", 25 )
         set_callbacks( Open_LuaIntf, Close_LuaIntf )
 
     add_submodule ()
@@ -94,15 +110,51 @@ vlc_module_begin ()
         add_string( "lua-config", "", NULL,
                     CONFIG_TEXT, CONFIG_LONGTEXT, false )
         set_callbacks( Open_LuaIntf, Close_LuaIntf )
+
+    add_submodule ()
+        set_shortname( N_("Lua Extension") )
+        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 )
+
+    add_submodule ()
+        set_description( N_("Freebox TV") )
+        add_shortcut( "freebox" )
+        set_capability( "services_discovery", 0 )
+        set_callbacks( Open_LuaSD, Close_LuaSD )
+
+    add_submodule ()
+        set_description( N_("French TV") )
+        add_shortcut( "frenchtv" )
+        set_capability( "services_discovery", 0 )
+        set_callbacks( Open_LuaSD, Close_LuaSD )
+
+    VLC_SD_PROBE_SUBMODULE
+
 vlc_module_end ()
 
 /*****************************************************************************
  *
  *****************************************************************************/
+static const char *ppsz_lua_exts[] = { ".luac", ".lua", NULL };
 static int file_select( const char *file )
 {
     int i = strlen( file );
-    return i > 4 && !strcmp( file+i-4, ".lua" );
+    int j;
+    for( j = 0; ppsz_lua_exts[j]; j++ )
+    {
+        int l = strlen( ppsz_lua_exts[j] );
+        if( i >= l && !strcmp( file+i-l, ppsz_lua_exts[j] ) )
+            return 1;
+    }
+    return 0;
 }
 
 static int file_compare( const char **a, const char **b )
@@ -110,42 +162,41 @@ static int file_compare( const char **a, const char **b )
     return strcmp( *a, *b );
 }
 
-int vlclua_dir_list( vlc_object_t *p_this, const char *luadirname, char **ppsz_dir_list )
+int vlclua_dir_list( vlc_object_t *p_this, const char *luadirname,
+                     char **ppsz_dir_list )
 {
     int i = 0;
     char *datadir = config_GetUserDir( VLC_DATA_DIR );
-    if( datadir == NULL )
-        return VLC_ENOMEM;
 
-    if( asprintf( &ppsz_dir_list[i], "%s" DIR_SEP "lua" DIR_SEP "%s",
-                   datadir, luadirname ) < 0 )
-    {
-        free( datadir );
-        return VLC_ENOMEM;
-    }
+    if( likely(datadir != NULL)
+     && likely(asprintf( &ppsz_dir_list[i], "%s"DIR_SEP"lua"DIR_SEP"%s",
+                         datadir, luadirname ) != -1) )
+        i++;
     free( datadir );
-    i++;
+
+#if !(defined(__APPLE__) || defined(SYS_BEOS) || defined(WIN32))
+    if( likely(asprintf( &ppsz_dir_list[i], "%s"DIR_SEP"lua"DIR_SEP"%s",
+                         config_GetLibDir(), luadirname ) != -1) )
+            i++;
+#endif
 
     char *psz_datapath = config_GetDataDir( p_this );
-#   if defined(__APPLE__) || defined(SYS_BEOS) || defined(WIN32)
+    if( likely(psz_datapath != NULL) )
     {
-        if( asprintf( &ppsz_dir_list[i], "%s" DIR_SEP "lua" DIR_SEP "%s",
-                      psz_datapath, luadirname )  < 0 )
-            return VLC_ENOMEM;
-        i++;
-        if( asprintf( &ppsz_dir_list[i], "%s" DIR_SEP "share" DIR_SEP "lua" DIR_SEP "%s",
-                      psz_datapath, luadirname )  < 0 )
-            return VLC_ENOMEM;
-        i++;
-
+        if( likely(asprintf( &ppsz_dir_list[i], "%s"DIR_SEP"lua"DIR_SEP"%s",
+                              psz_datapath, luadirname ) != -1) )
+            i++;
+
+#if defined(__APPLE__) || defined(SYS_BEOS) || defined(WIN32)
+        if( likely(asprintf( &ppsz_dir_list[i],
+                             "%s"DIR_SEP"share"DIR_SEP"lua"DIR_SEP"%s",
+                             psz_datapath, luadirname ) != -1) )
+            i++;
+#endif
+        free( psz_datapath );
     }
-#   else
-    if( asprintf( &ppsz_dir_list[i], "%s" DIR_SEP "lua" DIR_SEP "%s",
-                  psz_datapath, luadirname ) < 0 )
-        return VLC_ENOMEM;
-    i++;
-#   endif
-    free( psz_datapath );
+
+    ppsz_dir_list[i] = NULL;
     return VLC_SUCCESS;
 }
 
@@ -162,76 +213,92 @@ void vlclua_dir_list_free( char **ppsz_dir_list )
  *****************************************************************************/
 int vlclua_scripts_batch_execute( vlc_object_t *p_this,
                                   const char * luadirname,
-                                  int (*func)(vlc_object_t *, const char *, lua_State *, void *),
-                                  lua_State * L,
+                                  int (*func)(vlc_object_t *, const char *, void *),
                                   void * user_data)
 {
-    int i_ret = VLC_EGENERIC;
-
-    char **ppsz_filelist = NULL;
-    char **ppsz_fileend  = NULL;
-    char **ppsz_file;
-
     char  *ppsz_dir_list[] = { NULL, NULL, NULL, NULL };
-    char **ppsz_dir;
 
-    i_ret = vlclua_dir_list( p_this, luadirname, ppsz_dir_list );
+    int i_ret = vlclua_dir_list( p_this, luadirname, ppsz_dir_list );
     if( i_ret != VLC_SUCCESS )
         return i_ret;
     i_ret = VLC_EGENERIC;
 
-
-    for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
+    for( char **ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
     {
+        char **ppsz_filelist;
         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;
-        }
-
         msg_Dbg( p_this, "Trying Lua scripts in %s", *ppsz_dir );
-        i_files = utf8_scandir( *ppsz_dir, &ppsz_filelist, file_select,
+        i_files = vlc_scandir( *ppsz_dir, &ppsz_filelist, file_select,
                                 file_compare );
-        if( i_files < 1 ) continue;
-        ppsz_fileend = ppsz_filelist + i_files;
+        if( i_files < 0 )
+            continue;
 
-        for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend; ppsz_file++ )
+        char **ppsz_file = ppsz_filelist;
+        char **ppsz_fileend = ppsz_filelist + i_files;
+
+        while( ppsz_file < ppsz_fileend )
         {
-            char  *psz_filename;
+            char *psz_filename;
+
             if( asprintf( &psz_filename,
-                          "%s" DIR_SEP "%s", *ppsz_dir, *ppsz_file ) < 0)
+                          "%s" DIR_SEP "%s", *ppsz_dir, *ppsz_file ) == -1 )
+                psz_filename = NULL;
+            free( *(ppsz_file++) );
+
+            if( likely(psz_filename != NULL) )
             {
-                vlclua_dir_list_free( ppsz_dir_list );
-                return VLC_ENOMEM;
+                msg_Dbg( p_this, "Trying Lua playlist script %s",
+                         psz_filename );
+                i_ret = func( p_this, psz_filename, user_data );
+                free( psz_filename );
+                if( i_ret == VLC_SUCCESS )
+                    break;
             }
-            msg_Dbg( p_this, "Trying Lua playlist script %s", psz_filename );
-
-            i_ret = func( p_this, psz_filename, L, user_data );
-
-            free( psz_filename );
-
-            if( i_ret == VLC_SUCCESS ) break;
         }
-        if( i_ret == VLC_SUCCESS ) break;
-    }
 
-    if( ppsz_filelist )
-    {
-        for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
-             ppsz_file++ )
-            free( *ppsz_file );
+        while( ppsz_file < ppsz_fileend )
+            free( *(ppsz_file++) );
         free( ppsz_filelist );
+
+        if( i_ret == VLC_SUCCESS )
+            break;
     }
     vlclua_dir_list_free( ppsz_dir_list );
-
     return i_ret;
 }
 
+char *vlclua_find_file( vlc_object_t *p_this, const char *psz_luadirname, const char *psz_name )
+{
+    char  *ppsz_dir_list[] = { NULL, NULL, NULL, NULL };
+    char **ppsz_dir;
+    vlclua_dir_list( p_this, psz_luadirname, ppsz_dir_list );
+    for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
+    {
+        for( const char **ppsz_ext = ppsz_lua_exts; *ppsz_ext; ppsz_ext++ )
+        {
+            char *psz_filename;
+            struct stat st;
+
+            if( asprintf( &psz_filename, "%s"DIR_SEP"%s%s", *ppsz_dir,
+                          psz_name, *ppsz_ext ) < 0 )
+            {
+                vlclua_dir_list_free( ppsz_dir_list );
+                return NULL;
+            }
+
+            if( vlc_stat( psz_filename, &st ) == 0
+                && S_ISREG( st.st_mode ) )
+            {
+                vlclua_dir_list_free( ppsz_dir_list );
+                return psz_filename;
+            }
+            free( psz_filename );
+        }
+    }
+    vlclua_dir_list_free( ppsz_dir_list );
+    return NULL;
+}
 
 /*****************************************************************************
  * Meta data setters utility.
@@ -376,12 +443,14 @@ int __vlclua_playlist_add_internal( vlc_object_t *p_this, lua_State *L,
                                     input_item_t *p_parent, bool b_play )
 {
     int i_count = 0;
+    input_item_node_t *p_parent_node = NULL;
 
     assert( p_parent || p_playlist );
 
     /* playlist */
     if( lua_istable( L, -1 ) )
     {
+        if( p_parent ) p_parent_node = input_item_node_Create( p_parent );
         lua_pushnil( L );
         /* playlist nil */
         while( lua_next( L, -2 ) )
@@ -455,11 +524,13 @@ int __vlclua_playlist_add_internal( vlc_object_t *p_this, lua_State *L,
 
                     /* Append item to playlist */
                     if( p_parent ) /* Add to node */
-                        input_item_AddSubItem( p_parent, p_input );
+                    {
+                        input_item_node_AppendItem( p_parent_node, p_input );
+                    }
                     else /* Play or Enqueue (preparse) */
                         /* FIXME: playlist_AddInput() can fail */
                         playlist_AddInput( p_playlist, p_input,
-                               PLAYLIST_APPEND | 
+                               PLAYLIST_APPEND |
                                ( b_play ? PLAYLIST_GO : PLAYLIST_PREPARSE ),
                                PLAYLIST_END, true, false );
                     i_count ++; /* increment counter */
@@ -486,6 +557,11 @@ int __vlclua_playlist_add_internal( vlc_object_t *p_this, lua_State *L,
             /* playlist key */
         }
         /* playlist */
+        if( p_parent )
+        {
+            if( i_count ) input_item_node_PostAndDelete( p_parent_node );
+            else input_item_node_Delete( p_parent_node );
+        }
     }
     else
     {
@@ -493,3 +569,202 @@ 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 = vlc_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 = vlc_fopen( psz_filename, "r" );
+            if( fd )
+            {
+                char description[256];
+                if( fgets( description, 256, fd ) != NULL )
+                {
+                    char *temp = strchr( description, '\n' );
+                    if( temp )
+                        *temp = '\0';
+                    temp = strchr( *ppsz_file, '.' );
+                    if( temp )
+                        *temp = '\0';
+                    char *psz_longname;
+                    if( !strncmp( description, "--SD_Description=", 17 ) )
+                    {
+                        if( !( psz_longname = strdup( description + 17 ) ) )
+                        {
+                            fclose( fd );
+                            free( psz_filename );
+                            goto error;
+                        }
+                    }
+                    else
+                    {
+                        if( !( psz_longname = strdup( *ppsz_file ) ) )
+                        {
+                            fclose( fd );
+                            free( psz_filename );
+                            goto error;
+                        }
+                    }
+                    if( asprintf( &psz_name, "lua{sd=%s,longname=%s}",
+                                  *ppsz_file, psz_longname ) < 0 )
+                    {
+                        fclose( fd );
+                        free( psz_filename );
+                        free( psz_longname );
+                        goto error;
+                    }
+                    vlc_sd_probe_Add( probe, psz_name, psz_longname, SD_CAT_INTERNET );
+                    free( psz_name );
+                    free( psz_longname );
+                }
+                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;
+}
+
+static int vlclua_add_modules_path_inner( lua_State *L, const char *psz_path )
+{
+    /* FIXME: don't use luaL_dostring */
+    char *psz_command = NULL;
+    if( asprintf( &psz_command,
+                  "package.path =[[%s"DIR_SEP"modules"DIR_SEP"?.lua;]]..package.path",
+                  psz_path ) < 0 )
+    {
+        return 1;
+    }
+
+    if( luaL_dostring( L, psz_command ) )
+    {
+        free( psz_command );
+        return 1;
+    }
+    free( psz_command );
+
+    return 0;
+}
+
+int __vlclua_add_modules_path( vlc_object_t *obj, lua_State *L, const char *psz_filename )
+{
+    /* Setup the module search path:
+     *   * "The script's directory"/modules
+     *   * "The script's parent directory"/modules
+     *   * and so on for all the next directories in the directory list
+     */
+
+    char *psz_path = strdup( psz_filename );
+    if( !psz_path )
+        return 1;
+
+    char *psz_char = strrchr( psz_path, DIR_SEP_CHAR );
+    if( !psz_char )
+    {
+        free( psz_path );
+        return 1;
+    }
+    *psz_char = '\0';
+
+    /* psz_path now holds the file's directory */
+    psz_char = strrchr( psz_path, DIR_SEP_CHAR );
+    if( !psz_char )
+    {
+        free( psz_path );
+        return 1;
+    }
+    *psz_char = '\0';
+
+    /* psz_path now holds the file's parent directory */
+    if( vlclua_add_modules_path_inner( L, psz_path ) )
+    {
+        free( psz_path );
+        return 1;
+    }
+    *psz_char = DIR_SEP_CHAR;
+
+    /* psz_path now holds the file's directory */
+    if( vlclua_add_modules_path_inner( L, psz_path ) )
+    {
+        free( psz_path );
+        return 1;
+    }
+
+    char  *ppsz_dir_list[] = { NULL, NULL, NULL, NULL };
+    vlclua_dir_list( obj, psz_char+1/* gruik? */, ppsz_dir_list );
+    char **ppsz_dir = ppsz_dir_list;
+
+    for( ; *ppsz_dir && strcmp( *ppsz_dir, psz_path ); ppsz_dir++ );
+    free( psz_path );
+
+    for( ; *ppsz_dir; ppsz_dir++ )
+    {
+        psz_path = *ppsz_dir;
+        psz_char = strrchr( psz_path, DIR_SEP_CHAR );
+        if( !psz_char )
+            goto exit;
+
+        *psz_char = '\0';
+        if( vlclua_add_modules_path_inner( L, psz_path ) )
+            goto exit;
+        *psz_char = DIR_SEP_CHAR;
+
+        if( vlclua_add_modules_path_inner( L, psz_path ) )
+            goto exit;
+    }
+
+    vlclua_dir_list_free( ppsz_dir_list );
+    return 0;
+
+    exit:
+    vlclua_dir_list_free( ppsz_dir_list );
+    return 1;
+}
+