]> git.sesse.net Git - vlc/blobdiff - modules/misc/lua/vlc.c
Don't re-invent uint8_t - remove byte_t
[vlc] / modules / misc / lua / vlc.c
index eb07f4fb9e8e67e48908d04e1e14666636195431..d985340b4a4f42118cff57f643d5fc93a0315782 100644 (file)
 #   define  _GNU_SOURCE
 #endif
 
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
 #include <vlc/vlc.h>
 #include <vlc_meta.h>
 #include <vlc_charset.h>
 #define CONFIG_LONGTEXT N_("Lua interface configuration string. Format is: '[\"<interface module name>\"] = { <option> = <value>, ...}, ...'.")
 
 vlc_module_begin();
-    add_shortcut( "luameta" );
-    set_shortname( N_( "Lua Meta" ) );
-    set_description( _("Fetch metadata using lua scripts") );
-    set_capability( "meta fetcher", 10 );
-    set_callbacks( E_(FindMeta), NULL );
     add_submodule();
         set_shortname( N_( "Lua Art" ) );
         set_description( _("Fetch artwork using lua scripts") );
@@ -67,7 +66,7 @@ vlc_module_begin();
         set_subcategory( SUBCAT_INPUT_DEMUX );
         set_shortname( _("Lua Playlist") );
         set_description( _("Lua Playlist Parser Interface") );
-        set_capability( "demux2", 9 );
+        set_capability( "demux", 2 );
         set_callbacks( E_(Import_LuaPlaylist), E_(Close_LuaPlaylist) );
     add_submodule();
         add_shortcut( "luaintf" );
@@ -77,12 +76,14 @@ vlc_module_begin();
         /* add_shortcut( "hotkeys" ); */
         add_shortcut( "luatelnet" );
         /* add_shortcut( "telnet" ); */
+        add_shortcut( "luahttp" );
+        /* add_shortcut( "http" ); */
         set_description( _("Lua Interface Module") );
         set_capability( "interface", 0 );
         add_string( "lua-intf", "dummy", NULL,
-                    INTF_TEXT, INTF_LONGTEXT, VLC_FALSE );
+                    INTF_TEXT, INTF_LONGTEXT, false );
         add_string( "lua-config", "", NULL,
-                    CONFIG_TEXT, CONFIG_LONGTEXT, VLC_FALSE );
+                    CONFIG_TEXT, CONFIG_LONGTEXT, false );
         set_callbacks( E_(Open_LuaIntf), E_(Close_LuaIntf) );
 vlc_module_end();
 
@@ -118,6 +119,24 @@ int vlclua_version( lua_State *L )
     return 1;
 }
 
+/*****************************************************************************
+ * Get the VLC copyright
+ *****************************************************************************/
+int vlclua_copyright( lua_State *L )
+{
+    lua_pushstring( L, COPYRIGHT_MESSAGE );
+    return 1;
+}
+
+/*****************************************************************************
+ * Get the VLC license msg/disclaimer
+ *****************************************************************************/
+int vlclua_license( lua_State *L )
+{
+    lua_pushstring( L, LICENSE_MSG );
+    return 1;
+}
+
 /*****************************************************************************
  * Quit VLC
  *****************************************************************************/
@@ -130,13 +149,56 @@ int vlclua_quit( lua_State *L )
     return 0;
 }
 
+/*****************************************************************************
+ * Global properties getters
+ *****************************************************************************/
+int vlclua_datadir( lua_State *L )
+{
+    lua_pushstring( L, config_GetDataDir() );
+    return 1;
+}
+int vlclua_homedir( lua_State *L )
+{
+    lua_pushstring( L, vlclua_get_this( L )->p_libvlc->psz_homedir );
+    return 1;
+}
+int vlclua_configdir( lua_State *L )
+{
+    lua_pushstring( L, vlclua_get_this( L )->p_libvlc->psz_configdir );
+    return 1;
+}
+int vlclua_cachedir( lua_State *L )
+{
+    lua_pushstring( L, vlclua_get_this( L )->p_libvlc->psz_cachedir );
+    return 1;
+}
+int vlclua_datadir_list( lua_State *L )
+{
+    const char *psz_dirname = luaL_checkstring( L, 1 );
+    vlc_object_t *p_this = vlclua_get_this( L );
+    char  *ppsz_dir_list[] = { NULL, NULL, NULL, NULL };
+    char **ppsz_dir = ppsz_dir_list;
+    int i = 1;
+
+    if( vlclua_dir_list( p_this, psz_dirname, ppsz_dir_list ) != VLC_SUCCESS )
+        return 0;
+    lua_newtable( L );
+    for( ; *ppsz_dir; ppsz_dir++ )
+    {
+        lua_pushstring( L, *ppsz_dir );
+        lua_rawseti( L, -2, i );
+        i ++;
+    }
+    return 1;
+}
+
 /*****************************************************************************
  * Volume related
  *****************************************************************************/
 int vlclua_volume_set( lua_State *L )
 {
     vlc_object_t *p_this = vlclua_get_this( L );
-    int i_volume = luaL_checkint( L, -1 );
+    int i_volume = luaL_checkint( L, 1 );
     /* Do we need to check that i_volume is in the AOUT_VOLUME_MIN->MAX range?*/
     return vlclua_push_ret( L, aout_VolumeSet( p_this, i_volume ) );
 }
@@ -180,11 +242,10 @@ int vlclua_stream_new( lua_State *L )
     vlc_object_t * p_this = vlclua_get_this( L );
     stream_t * p_stream;
     const char * psz_url;
-    if( lua_gettop( L ) != 1 ) return vlclua_error( L );
     psz_url = luaL_checkstring( L, -1 );
-    lua_pop( L, 1 );
     p_stream = stream_UrlNew( p_this, psz_url );
-    if( !p_stream ) return vlclua_error( L );
+    if( !p_stream )
+        return luaL_error( L, "Error when opening url: `%s'", psz_url );
     lua_pushlightuserdata( L, p_stream );
     return 1;
 }
@@ -193,12 +254,10 @@ int vlclua_stream_read( lua_State *L )
 {
     stream_t * p_stream;
     int n;
-    byte_t *p_read;
+    uint8_t *p_read;
     int i_read;
-    if( lua_gettop( L ) != 2 ) return vlclua_error( L );
-    p_stream = (stream_t *)luaL_checklightuserdata( L, -2 );
-    n = luaL_checkint( L, -1 );
-    lua_pop( L, 2 );
+    p_stream = (stream_t *)luaL_checklightuserdata( L, 1 );
+    n = luaL_checkint( L, 2 );
     p_read = malloc( n );
     if( !p_read ) return vlclua_error( L );
     i_read = stream_Read( p_stream, p_read, n );
@@ -210,9 +269,7 @@ int vlclua_stream_read( lua_State *L )
 int vlclua_stream_readline( lua_State *L )
 {
     stream_t * p_stream;
-    if( lua_gettop( L ) != 1 ) return vlclua_error( L );
-    p_stream = (stream_t *)luaL_checklightuserdata( L, -1 );
-    lua_pop( L, 1 );
+    p_stream = (stream_t *)luaL_checklightuserdata( L, 1 );
     char *psz_line = stream_ReadLine( p_stream );
     if( psz_line )
     {
@@ -220,94 +277,108 @@ int vlclua_stream_readline( lua_State *L )
         free( psz_line );
     }
     else
-    {
         lua_pushnil( L );
-    }
     return 1;
 }
 
 int vlclua_stream_delete( lua_State *L )
 {
     stream_t * p_stream;
-    if( lua_gettop( L ) != 1 ) return vlclua_error( L );
-    p_stream = (stream_t *)luaL_checklightuserdata( L, -1 );
-    lua_pop( L, 1 );
+    p_stream = (stream_t *)luaL_checklightuserdata( L, 1 );
     stream_Delete( p_stream );
-    return 1;
+    return 0;
 }
 
 /*****************************************************************************
  * String transformations
  *****************************************************************************/
-/* TODO: make it work for any number of arguments */
 int vlclua_decode_uri( lua_State *L )
 {
-    if( lua_gettop( L ) != 1 ) return vlclua_error( L );
-    const char *psz_cstring = luaL_checkstring( L, 1 );
-    if( !psz_cstring ) return vlclua_error( L );
-    char *psz_string = strdup( psz_cstring );
-    lua_pop( L, 1 );
-    decode_URI( psz_string );
-    lua_pushstring( L, psz_string );
-    free( psz_string );
-    return 1;
+    int i_top = lua_gettop( L );
+    int i;
+    for( i = 1; i <= i_top; i++ )
+    {
+        const char *psz_cstring = luaL_checkstring( L, 1 );
+        char *psz_string = strdup( psz_cstring );
+        lua_remove( L, 1 ); /* remove elements to prevent being limited by
+                             * the stack's size (this function will work with
+                             * up to (stack size - 1) arguments */
+        decode_URI( psz_string );
+        lua_pushstring( L, psz_string );
+        free( psz_string );
+    }
+    return i_top;
 }
 
-/* TODO: make it work for any number of arguments */
 int vlclua_resolve_xml_special_chars( lua_State *L )
 {
-    if( lua_gettop( L ) != 1 ) return vlclua_error( L );
-    const char *psz_cstring = luaL_checkstring( L, 1 );
-    if( !psz_cstring ) return vlclua_error( L );
-    char *psz_string = strdup( psz_cstring );
-    lua_pop( L, 1 );
-    resolve_xml_special_chars( psz_string );
-    lua_pushstring( L, psz_string );
-    free( psz_string );
-    return 1;
+    int i_top = lua_gettop( L );
+    int i;
+    for( i = 1; i <= i_top; i++ )
+    {
+        const char *psz_cstring = luaL_checkstring( L, 1 );
+        char *psz_string = strdup( psz_cstring );
+        lua_remove( L, 1 ); /* remove elements to prevent being limited by
+                             * the stack's size (this function will work with
+                             * up to (stack size - 1) arguments */
+        resolve_xml_special_chars( psz_string );
+        lua_pushstring( L, psz_string );
+        free( psz_string );
+    }
+    return i_top;
+}
+
+int vlclua_convert_xml_special_chars( lua_State *L )
+{
+    int i_top = lua_gettop( L );
+    int i;
+    for( i = 1; i <= i_top; i++ )
+    {
+        char *psz_string = convert_xml_special_chars( luaL_checkstring(L,1) );
+        lua_remove( L, 1 );
+        lua_pushstring( L, psz_string );
+        free( psz_string );
+    }
+    return i_top;
 }
 
 /*****************************************************************************
  * Messaging facilities
  *****************************************************************************/
-/* TODO: make it work for any number of arguments */
 int vlclua_msg_dbg( lua_State *L )
 {
+    int i_top = lua_gettop( L );
     vlc_object_t *p_this = vlclua_get_this( L );
-    if( lua_gettop( L ) != 1 ) return vlclua_error( L );
-    const char *psz_cstring = luaL_checkstring( L, 1 );
-    if( !psz_cstring ) return vlclua_error( L );
-    msg_Dbg( p_this, "%s", psz_cstring );
+    int i;
+    for( i = 1; i <= i_top; i++ )
+        msg_Dbg( p_this, "%s", luaL_checkstring( L, 1 ) );
     return 0;
 }
-/* TODO: make it work for any number of arguments */
 int vlclua_msg_warn( lua_State *L )
 {
+    int i_top = lua_gettop( L );
     vlc_object_t *p_this = vlclua_get_this( L );
-    if( lua_gettop( L ) != 1 ) return vlclua_error( L );
-    const char *psz_cstring = luaL_checkstring( L, 1 );
-    if( !psz_cstring ) return vlclua_error( L );
-    msg_Warn( p_this, "%s", psz_cstring );
+    int i;
+    for( i = 1; i <= i_top; i++ )
+        msg_Warn( p_this, "%s", luaL_checkstring( L, i ) );
     return 0;
 }
-/* TODO: make it work for any number of arguments */
 int vlclua_msg_err( lua_State *L )
 {
+    int i_top = lua_gettop( L );
     vlc_object_t *p_this = vlclua_get_this( L );
-    if( lua_gettop( L ) != 1 ) return vlclua_error( L );
-    const char *psz_cstring = luaL_checkstring( L, 1 );
-    if( !psz_cstring ) return vlclua_error( L );
-    msg_Err( p_this, "%s", psz_cstring );
+    int i;
+    for( i = 1; i <= i_top; i++ )
+        msg_Err( p_this, "%s", luaL_checkstring( L, i ) );
     return 0;
 }
-/* TODO: make it work for any number of arguments */
 int vlclua_msg_info( lua_State *L )
 {
+    int i_top = lua_gettop( L );
     vlc_object_t *p_this = vlclua_get_this( L );
-    if( lua_gettop( L ) != 1 ) return vlclua_error( L );
-    const char *psz_cstring = luaL_checkstring( L, 1 );
-    if( !psz_cstring ) return vlclua_error( L );
-    msg_Info( p_this, "%s", psz_cstring );
+    int i;
+    for( i = 1; i <= i_top; i++ )
+        msg_Info( p_this, "%s", luaL_checkstring( L, i ) );
     return 0;
 }
 
@@ -328,39 +399,51 @@ static int file_compare( const char **a, const char **b )
 int vlclua_dir_list( vlc_object_t *p_this, const char *luadirname,
                      char **ppsz_dir_list )
 {
-    if( asprintf( &ppsz_dir_list[0], "%s" DIR_SEP "%s",
+    int i = 0;
+    if( asprintf( &ppsz_dir_list[i], "%s" DIR_SEP "lua" DIR_SEP "%s",
                    p_this->p_libvlc->psz_datadir, luadirname ) < 0 )
         return VLC_ENOMEM;
+    i++;
 
 #   if defined(__APPLE__) || defined(SYS_BEOS) || defined(WIN32)
     {
         const char *psz_vlcpath = config_GetDataDir();
-        if( asprintf( &ppsz_dir_list[1], "%s" DIR_SEP "%s",
+        if( asprintf( &ppsz_dir_list[i], "%s" DIR_SEP "lua" DIR_SEP "%s",
                       psz_vlcpath, luadirname )  < 0 )
             return VLC_ENOMEM;
-
-        if( asprintf( &ppsz_dir_list[2], "%s" DIR_SEP "share" DIR_SEP "%s",
+        i++;
+#       ifdef WIN32
+        if( asprintf( &ppsz_dir_list[i], "%s" DIR_SEP "scripts" DIR_SEP "%s",
+                      psz_vlcpath, luadirname )  < 0 )
+            return VLC_ENOMEM;
+        i++;
+#       else
+         if( asprintf( &ppsz_dir_list[i], "%s" DIR_SEP "share" DIR_SEP "lua" DIR_SEP "%s",
                       psz_vlcpath, luadirname )  < 0 )
             return VLC_ENOMEM;
+        i++;
+#       endif
+
     }
 #   else
-    if( asprintf( &ppsz_dir_list[1],
-                  "share" DIR_SEP "%s", luadirname ) < 0 )
+    if( asprintf( &ppsz_dir_list[i],
+                  "share" DIR_SEP "lua" DIR_SEP "%s", luadirname ) < 0 )
         return VLC_ENOMEM;
 
 #   ifdef HAVE_SYS_STAT_H
     {
         struct stat stat_info;
-        if( ( utf8_stat( ppsz_dir_list[1], &stat_info ) == -1 )
+        if( ( utf8_stat( ppsz_dir_list[i], &stat_info ) == -1 )
             || !S_ISDIR( stat_info.st_mode ) )
         {
-            free(ppsz_dir_list[1]);
-            if( asprintf( &ppsz_dir_list[1],
-                          DATA_PATH DIR_SEP "%s", luadirname ) < 0 )
+            free(ppsz_dir_list[i]);
+            if( asprintf( &ppsz_dir_list[i], "%s" DIR_SEP "lua" DIR_SEP "%s",
+                          config_GetDataDir (), luadirname ) < 0 )
                 return VLC_ENOMEM;
         }
     }
 #   endif
+    i++;
 #   endif
     return VLC_SUCCESS;
 }
@@ -591,7 +674,7 @@ void __vlclua_read_options( vlc_object_t *p_this, lua_State *L,
 
 int __vlclua_playlist_add_internal( vlc_object_t *p_this, lua_State *L,
                                     playlist_t *p_playlist,
-                                    input_item_t *p_parent, vlc_bool_t b_play )
+                                    input_item_t *p_parent, bool b_play )
 {
     int i_count = 0;
 
@@ -671,16 +754,14 @@ int __vlclua_playlist_add_internal( vlc_object_t *p_this, lua_State *L,
                     /* Append item to playlist */
                     if( p_parent ) /* Add to node */
                         input_ItemAddSubItem( p_parent, p_input );
-                    else if( b_play ) /* Play */
-                        playlist_AddInput( p_playlist, p_input,
-                                           PLAYLIST_APPEND | PLAYLIST_GO,
-                                           PLAYLIST_END, VLC_TRUE, VLC_FALSE );
-                    else /* Enqueue */
+                    else /* Play or Enqueue (preparse) */
+                        /* FIXME: playlist_AddInput() can fail */
                         playlist_AddInput( p_playlist, p_input,
-                                           PLAYLIST_APPEND | PLAYLIST_PREPARSE,
-                                           PLAYLIST_END, VLC_TRUE, VLC_FALSE );
+                               PLAYLIST_APPEND | 
+                               ( b_play ? PLAYLIST_GO : PLAYLIST_PREPARSE ),
+                               PLAYLIST_END, true, false );
                     i_count ++; /* increment counter */
-
+                    vlc_gc_decref( p_input );
                     while( i_options > 0 )
                         free( ppsz_options[--i_options] );
                     free( ppsz_options );