Commit 31d2afee authored by Srikanth Raju's avatar Srikanth Raju Committed by Jean-Philippe André

Lua: Allow arguments to be passed to lua functions in lua_ExecuteFunction

Signed-off-by: default avatarJean-Philippe André <jpeg@videolan.org>
parent d4b9c029
...@@ -542,7 +542,7 @@ static int Control( extensions_manager_t *p_mgr, int i_control, va_list args ) ...@@ -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 ) int lua_ExtensionActivate( extensions_manager_t *p_mgr, extension_t *p_ext )
{ {
assert( p_mgr != NULL && p_ext != NULL ); 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 ) 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 ) ...@@ -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 ); 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 */ /* Clear Lua State */
lua_close( p_ext->p_sys->L ); lua_close( p_ext->p_sys->L );
...@@ -580,7 +580,7 @@ int lua_ExtensionWidgetClick( extensions_manager_t *p_mgr, ...@@ -580,7 +580,7 @@ int lua_ExtensionWidgetClick( extensions_manager_t *p_mgr,
if( !p_ext->p_sys->L ) if( !p_ext->p_sys->L )
return VLC_SUCCESS; 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, ...@@ -773,17 +773,27 @@ static lua_State* GetLuaState( extensions_manager_t *p_mgr,
return L; 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 * Execute a function in a Lua script
* @return < 0 in case of failure, >= 0 in case of success * @return < 0 in case of failure, >= 0 in case of success
* @note It's better to call this function from a dedicated thread * @note It's better to call this function from a dedicated thread
* (see extension_thread.c) * (see extension_thread.c)
**/ **/
int lua_ExecuteFunction( extensions_manager_t *p_mgr, int lua_ExecuteFunctionVa( extensions_manager_t *p_mgr, extension_t *p_ext,
extension_t *p_ext, const char *psz_function, va_list args )
const char *psz_function )
{ {
int i_ret = VLC_EGENERIC; int i_ret = VLC_EGENERIC;
int i_args = 0;
assert( p_mgr != NULL ); assert( p_mgr != NULL );
assert( p_ext != NULL ); assert( p_ext != NULL );
...@@ -797,7 +807,26 @@ int lua_ExecuteFunction( extensions_manager_t *p_mgr, ...@@ -797,7 +807,26 @@ int lua_ExecuteFunction( extensions_manager_t *p_mgr,
goto exit; 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, " msg_Warn( p_mgr, "Error while runing script %s, "
"function %s(): %s", p_ext->psz_name, psz_function, "function %s(): %s", p_ext->psz_name, psz_function,
...@@ -808,6 +837,7 @@ int lua_ExecuteFunction( extensions_manager_t *p_mgr, ...@@ -808,6 +837,7 @@ int lua_ExecuteFunction( extensions_manager_t *p_mgr,
i_ret = lua_DialogFlush( L ); i_ret = lua_DialogFlush( L );
exit: exit:
return i_ret; return i_ret;
} }
static inline int TriggerMenu( extension_t *p_ext, int i_id ) static inline int TriggerMenu( extension_t *p_ext, int i_id )
...@@ -863,7 +893,7 @@ int lua_ExtensionTriggerMenu( extensions_manager_t *p_mgr, ...@@ -863,7 +893,7 @@ int lua_ExtensionTriggerMenu( extensions_manager_t *p_mgr,
static int TriggerExtension( extensions_manager_t *p_mgr, static int TriggerExtension( extensions_manager_t *p_mgr,
extension_t *p_ext ) 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 */ /* Close lua state for trigger-only extensions */
if( p_ext->p_sys->L ) if( p_ext->p_sys->L )
......
...@@ -40,6 +40,14 @@ TYPEDEF_ARRAY( extension_t, array_extension_t ); ...@@ -40,6 +40,14 @@ TYPEDEF_ARRAY( extension_t, array_extension_t );
#define CMD_UPDATE_META 7 /* No arg. Just signal current input item meta #define CMD_UPDATE_META 7 /* No arg. Just signal current input item meta
* changed */ * changed */
//Data types
typedef enum
{
LUA_END = 0,
LUA_NUM,
LUA_TEXT
} lua_datatype_e;
struct extensions_manager_sys_t struct extensions_manager_sys_t
{ {
/* List of activated extensions */ /* List of activated extensions */
...@@ -111,8 +119,10 @@ void UnlockExtension( extension_t *p_ext ); ...@@ -111,8 +119,10 @@ void UnlockExtension( extension_t *p_ext );
extension_t *vlclua_extension_get( lua_State *L ); extension_t *vlclua_extension_get( lua_State *L );
int lua_ExtensionActivate( extensions_manager_t *, extension_t * ); int lua_ExtensionActivate( extensions_manager_t *, extension_t * );
int lua_ExtensionDeactivate( 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, 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, int lua_ExtensionWidgetClick( extensions_manager_t *p_mgr,
extension_t *p_ext, extension_t *p_ext,
extension_widget_t *p_widget ); extension_widget_t *p_widget );
......
...@@ -299,7 +299,7 @@ static void* Run( void *data ) ...@@ -299,7 +299,7 @@ static void* Run( void *data )
{ {
case CMD_ACTIVATE: 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!" ); msg_Dbg( p_mgr, "Could not activate extension!" );
Deactivate( p_mgr, p_ext ); Deactivate( p_mgr, p_ext );
...@@ -322,7 +322,7 @@ static void* Run( void *data ) ...@@ -322,7 +322,7 @@ static void* Run( void *data )
case CMD_CLOSE: case CMD_CLOSE:
{ {
lua_ExecuteFunction( p_mgr, p_ext, "close" ); lua_ExecuteFunction( p_mgr, p_ext, "close", LUA_END );
break; break;
} }
...@@ -352,13 +352,13 @@ static void* Run( void *data ) ...@@ -352,13 +352,13 @@ static void* Run( void *data )
case CMD_SET_INPUT: case CMD_SET_INPUT:
{ {
lua_ExecuteFunction( p_mgr, p_ext, "input_changed" ); lua_ExecuteFunction( p_mgr, p_ext, "input_changed", LUA_END );
break; break;
} }
case CMD_UPDATE_META: case CMD_UPDATE_META:
{ {
lua_ExecuteFunction( p_mgr, p_ext, "meta_changed" ); lua_ExecuteFunction( p_mgr, p_ext, "meta_changed", LUA_END );
break; break;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment