]> git.sesse.net Git - vlc/commitdiff
Lua: Allow arguments to be passed to lua functions in lua_ExecuteFunction
authorSrikanth Raju <srikiraju@gmail.com>
Mon, 8 Feb 2010 17:54:18 +0000 (23:24 +0530)
committerJean-Philippe André <jpeg@videolan.org>
Sun, 14 Feb 2010 17:30:59 +0000 (18:30 +0100)
Signed-off-by: Jean-Philippe André <jpeg@videolan.org>
modules/misc/lua/extension.c
modules/misc/lua/extension.h
modules/misc/lua/extension_thread.c

index 18a0b30bca0789eb925f33188e6f955eef5c74c3..90e588edaf8f095f1a925111297d9d54057ed396 100644 (file)
@@ -542,7 +542,7 @@ static int Control( extensions_manager_t *p_mgr, int i_control, va_list args )
 int lua_ExtensionActivate( extensions_manager_t *p_mgr, extension_t *p_ext )
 {
     assert( p_mgr != NULL && p_ext != NULL );
-    return lua_ExecuteFunction( p_mgr, p_ext, "activate" );
+    return lua_ExecuteFunction( p_mgr, p_ext, "activate", LUA_END );
 }
 
 int lua_ExtensionDeactivate( extensions_manager_t *p_mgr, extension_t *p_ext )
@@ -564,7 +564,7 @@ int lua_ExtensionDeactivate( extensions_manager_t *p_mgr, extension_t *p_ext )
         vlc_object_release( p_ext->p_sys->p_input );
     }
 
-    int i_ret = lua_ExecuteFunction( p_mgr, p_ext, "deactivate" );
+    int i_ret = lua_ExecuteFunction( p_mgr, p_ext, "deactivate", LUA_END );
 
     /* Clear Lua State */
     lua_close( p_ext->p_sys->L );
@@ -580,7 +580,7 @@ int lua_ExtensionWidgetClick( extensions_manager_t *p_mgr,
     if( !p_ext->p_sys->L )
         return VLC_SUCCESS;
 
-    return lua_ExecuteFunction( p_mgr, p_ext, (const char*) p_widget->p_sys );
+    return lua_ExecuteFunction( p_mgr, p_ext, (const char*) p_widget->p_sys, LUA_END );
 }
 
 
@@ -773,17 +773,27 @@ static lua_State* GetLuaState( extensions_manager_t *p_mgr,
     return L;
 }
 
+int lua_ExecuteFunction( extensions_manager_t *p_mgr, extension_t *p_ext,
+                            const char *psz_function, ... )
+{
+    va_list args;
+    va_start( args, psz_function );
+    int i_ret = lua_ExecuteFunctionVa( p_mgr, p_ext, psz_function, args );
+    va_end( args );
+    return i_ret;
+}
+
 /**
  * Execute a function in a Lua script
  * @return < 0 in case of failure, >= 0 in case of success
  * @note It's better to call this function from a dedicated thread
  * (see extension_thread.c)
  **/
-int lua_ExecuteFunction( extensions_manager_t *p_mgr,
-                         extension_t *p_ext,
-                         const char *psz_function )
+int lua_ExecuteFunctionVa( extensions_manager_t *p_mgr, extension_t *p_ext,
+                            const char *psz_function, va_list args )
 {
     int i_ret = VLC_EGENERIC;
+    int i_args = 0;
     assert( p_mgr != NULL );
     assert( p_ext != NULL );
 
@@ -797,7 +807,26 @@ int lua_ExecuteFunction( extensions_manager_t *p_mgr,
         goto exit;
     }
 
-    if( lua_pcall( L, 0, 1, 0 ) )
+    lua_datatype_e type = LUA_END;
+    while( ( type = va_arg( args, int ) ) != LUA_END )
+    {
+        if( type == LUA_NUM )
+        {
+            lua_pushnumber( L , ( int ) va_arg( args, int ) );
+        }
+        else if( type == LUA_TEXT )
+        {
+            lua_pushstring( L , ( char * ) va_arg( args, char* ) );
+        }
+        else
+        {
+            msg_Warn( p_mgr, "Undefined argument type %d to lua function %s"
+                   "from script %s", type, psz_function, p_ext->psz_name );
+            goto exit;
+        }
+        i_args ++;
+    }
+    if( lua_pcall( L, i_args, 1, 0 ) )
     {
         msg_Warn( p_mgr, "Error while runing script %s, "
                   "function %s(): %s", p_ext->psz_name, psz_function,
@@ -808,6 +837,7 @@ int lua_ExecuteFunction( extensions_manager_t *p_mgr,
     i_ret = lua_DialogFlush( L );
 exit:
     return i_ret;
+
 }
 
 static inline int TriggerMenu( extension_t *p_ext, int i_id )
@@ -863,7 +893,7 @@ int lua_ExtensionTriggerMenu( extensions_manager_t *p_mgr,
 static int TriggerExtension( extensions_manager_t *p_mgr,
                              extension_t *p_ext )
 {
-    int i_ret = lua_ExecuteFunction( p_mgr, p_ext, "trigger" );
+    int i_ret = lua_ExecuteFunction( p_mgr, p_ext, "trigger", LUA_END );
 
     /* Close lua state for trigger-only extensions */
     if( p_ext->p_sys->L )
index 921ad4327e186f25ff5df4ab2c23ee8dfff94576..22cf46d60bb535c367a73a59ef826fdd24740721 100644 (file)
@@ -40,6 +40,14 @@ TYPEDEF_ARRAY( extension_t, array_extension_t );
 #define CMD_UPDATE_META 7    /* No arg. Just signal current input item meta
                               * changed */
 
+//Data types
+typedef enum
+{
+    LUA_END = 0,
+    LUA_NUM,
+    LUA_TEXT
+} lua_datatype_e;
+
 struct extensions_manager_sys_t
 {
     /* List of activated extensions */
@@ -111,8 +119,10 @@ void UnlockExtension( extension_t *p_ext );
 extension_t *vlclua_extension_get( lua_State *L );
 int lua_ExtensionActivate( extensions_manager_t *, extension_t * );
 int lua_ExtensionDeactivate( extensions_manager_t *, extension_t * );
+int lua_ExecuteFunctionVa( extensions_manager_t *p_mgr, extension_t *p_ext,
+                            const char *psz_function, va_list args );
 int lua_ExecuteFunction( extensions_manager_t *p_mgr, extension_t *p_ext,
-                         const char *psz_function );
+                         const char *psz_function, ... );
 int lua_ExtensionWidgetClick( extensions_manager_t *p_mgr,
                               extension_t *p_ext,
                               extension_widget_t *p_widget );
index cadfc3acc1fc17d633d4fac1b2f16fa2420564e1..41faaf5dc615b8046f06161ed03b5bc24406f115 100644 (file)
@@ -299,7 +299,7 @@ static void* Run( void *data )
                 {
                     case CMD_ACTIVATE:
                     {
-                        if( lua_ExecuteFunction( p_mgr, p_ext, "activate" ) < 0 )
+                        if( lua_ExecuteFunction( p_mgr, p_ext, "activate", LUA_END ) < 0 )
                         {
                             msg_Dbg( p_mgr, "Could not activate extension!" );
                             Deactivate( p_mgr, p_ext );
@@ -322,7 +322,7 @@ static void* Run( void *data )
 
                     case CMD_CLOSE:
                     {
-                        lua_ExecuteFunction( p_mgr, p_ext, "close" );
+                        lua_ExecuteFunction( p_mgr, p_ext, "close", LUA_END );
                         break;
                     }
 
@@ -352,13 +352,13 @@ static void* Run( void *data )
 
                     case CMD_SET_INPUT:
                     {
-                        lua_ExecuteFunction( p_mgr, p_ext, "input_changed" );
+                        lua_ExecuteFunction( p_mgr, p_ext, "input_changed", LUA_END );
                         break;
                     }
 
                     case CMD_UPDATE_META:
                     {
-                        lua_ExecuteFunction( p_mgr, p_ext, "meta_changed" );
+                        lua_ExecuteFunction( p_mgr, p_ext, "meta_changed", LUA_END );
                         break;
                     }