Commit 1bbf6f1c authored by JPeg's avatar JPeg
Browse files

Extensions: core library

This contains:
- Extensions manager
- Extensions activation / deactivation functions
- Separate threads for each extension
- Lua specific functions for calling the script
parent c6912c81
/*****************************************************************************
* extension.c: Lua Extensions (meta data, web information, ...)
*****************************************************************************
* Copyright (C) 2009-2010 VideoLAN and authors
* $Id$
*
* Authors: Jean-Philippe André < jpeg # videolan.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.
*****************************************************************************/
#include "vlc.h"
#include "libs.h"
#include "extension.h"
#include "assert.h"
/* Functions to register */
static const luaL_Reg p_reg[] =
{
{ NULL, NULL }
};
/*
* Extensions capabilities
* Note: #define and ppsz_capabilities must be in sync
*/
#define EXT_HAS_MENU (1 << 0)
#define EXT_TRIGGER_ONLY (1 << 1)
const char* const ppsz_capabilities[] = {
"menu",
"trigger",
NULL
};
static int ScanExtensions( extensions_manager_t *p_this );
static int ScanLuaCallback( vlc_object_t *p_this, const char *psz_script,
lua_State *L, void *pb_continue );
static int Control( extensions_manager_t *, int, va_list );
static int GetMenu( extensions_manager_t *p_mgr, extension_t *p_ext,
char ***pppsz_titles, uint16_t **ppi_ids );
static lua_State* GetLuaState( extensions_manager_t *p_mgr,
extension_t *p_ext );
static int TriggerMenu( extension_t *p_ext, int id );
static int TriggerExtension( extensions_manager_t *p_mgr,
extension_t *p_ext );
int vlclua_extension_deactivate( lua_State *L );
/* Interactions */
static int vlclua_extension_dialog_callback( vlc_object_t *p_this,
char const *psz_var,
vlc_value_t oldval,
vlc_value_t newval,
void *p_data );
/**
* Module entry-point
**/
int Open_Extension( vlc_object_t *p_this )
{
msg_Dbg( p_this, "Opening EXPERIMENTAL Lua Extension module" );
extensions_manager_t *p_mgr = ( extensions_manager_t* ) p_this;
p_mgr->pf_control = Control;
extensions_manager_sys_t *p_sys = ( extensions_manager_sys_t* )
calloc( 1, sizeof( extensions_manager_sys_t ) );
if( !p_sys ) return VLC_ENOMEM;
p_mgr->p_sys = p_sys;
ARRAY_INIT( p_sys->activated_extensions );
ARRAY_INIT( p_mgr->extensions );
/* Initialise Lua state structure */
lua_State *L = GetLuaState( p_mgr, NULL );
if( !L )
{
free( p_sys );
return VLC_EGENERIC;
}
p_sys->L = L;
/* Scan available Lua Extensions */
if( ScanExtensions( p_mgr ) != VLC_SUCCESS )
{
msg_Err( p_mgr, "Can't load extensions modules" );
return VLC_EGENERIC;
}
lua_close( L );
p_sys->L = NULL;
vlc_mutex_init( &p_sys->lock );
// Create the dialog-event variable
var_Create( p_this, "dialog-event", VLC_VAR_ADDRESS );
var_AddCallback( p_this, "dialog-event",
vlclua_extension_dialog_callback, NULL );
return VLC_SUCCESS;
}
/**
* Module unload function
**/
void Close_Extension( vlc_object_t *p_this )
{
extensions_manager_t *p_mgr = ( extensions_manager_t* ) p_this;
msg_Dbg( p_mgr, "Deactivating all loaded extensions" );
vlc_mutex_lock( &p_mgr->p_sys->lock );
p_mgr->p_sys->b_killed = true;
vlc_mutex_unlock( &p_mgr->p_sys->lock );
extension_t *p_ext = NULL;
FOREACH_ARRAY( p_ext, p_mgr->p_sys->activated_extensions )
{
if( !p_ext ) break;
Deactivate( p_mgr, p_ext );
WaitForDeactivation( p_ext );
}
FOREACH_END()
msg_Dbg( p_mgr, "All extensions are now deactivated" );
ARRAY_RESET( p_mgr->p_sys->activated_extensions );
if( p_mgr->p_sys && p_mgr->p_sys->L )
lua_close( p_mgr->p_sys->L );
vlc_mutex_destroy( &p_mgr->p_sys->lock );
free( p_mgr->p_sys );
p_mgr->p_sys = NULL;
/* Free extensions' memory */
FOREACH_ARRAY( p_ext, p_mgr->extensions )
{
if( !p_ext )
break;
if( p_ext->p_sys->L )
lua_close( p_ext->p_sys->L );
free( p_ext->psz_name );
free( p_ext->psz_title );
vlc_mutex_destroy( &p_ext->p_sys->running_lock );
vlc_mutex_destroy( &p_ext->p_sys->command_lock );
vlc_cond_destroy( &p_ext->p_sys->wait );
free( p_ext->p_sys );
free( p_ext );
}
FOREACH_END()
ARRAY_RESET( p_mgr->extensions );
var_Destroy( p_mgr, "dialog-event" );
}
/**
* Batch scan all Lua files in folder "extensions"
* @param p_mgr This extensions_manager_t object
**/
static int ScanExtensions( extensions_manager_t *p_mgr )
{
bool b_true = true;
int i_ret =
vlclua_scripts_batch_execute( VLC_OBJECT( p_mgr ),
"extensions",
&ScanLuaCallback,
p_mgr->p_sys->L, &b_true );
if( !i_ret )
return VLC_EGENERIC;
return VLC_SUCCESS;
}
/**
* Batch scan all Lua files in folder "extensions": callback
* @param p_this This extensions_manager_t object
* @param psz_script Name of the script to run
* @param L Lua State, common to all scripts here
* @param pb_continue bool* that indicates whether to continue batch or not
**/
int ScanLuaCallback( vlc_object_t *p_this, const char *psz_script,
lua_State *L, void *pb_continue )
{
extensions_manager_t *p_mgr = ( extensions_manager_t* ) p_this;
bool b_ok = false;
msg_Dbg( p_mgr, "Scanning Lua script %s", psz_script );
vlc_mutex_lock( &p_mgr->p_sys->lock );
/* Create new script descriptor */
extension_t *p_ext = ( extension_t* ) calloc( 1, sizeof( extension_t ) );
if( !p_ext )
{
vlc_mutex_unlock( &p_mgr->p_sys->lock );
return 0;
}
p_ext->psz_name = strdup( psz_script );
p_ext->p_sys = (extension_sys_t*) calloc( 1, sizeof( extension_sys_t ) );
if( !p_ext->p_sys || !p_ext->psz_name )
{
free( p_ext->psz_name );
free( p_ext->p_sys );
free( p_ext );
vlc_mutex_unlock( &p_mgr->p_sys->lock );
return 0;
}
p_ext->p_sys->p_mgr = p_mgr;
/* Mutexes and conditions */
vlc_mutex_init( &p_ext->p_sys->command_lock );
vlc_mutex_init( &p_ext->p_sys->running_lock );
vlc_cond_init( &p_ext->p_sys->wait );
/* Load and run the script(s) */
if( luaL_dofile( L, psz_script ) )
{
msg_Warn( p_mgr, "Error loading script %s: %s", psz_script,
lua_tostring( L, lua_gettop( L ) ) );
lua_pop( L, 1 );
goto exit;
}
/* Scan script for capabilities */
lua_getglobal( L, "descriptor" );
if( !lua_isfunction( L, -1 ) )
{
msg_Warn( p_mgr, "Error while runing script %s, "
"function descriptor() not found", psz_script );
goto exit;
}
if( lua_pcall( L, 0, 1, 0 ) )
{
msg_Warn( p_mgr, "Error while runing script %s, "
"function descriptor(): %s", psz_script,
lua_tostring( L, lua_gettop( L ) ) );
goto exit;
}
if( lua_gettop( L ) )
{
if( lua_istable( L, -1 ) )
{
lua_getfield( L, -1, "capabilities" );
if( lua_istable( L, -1 ) )
{
lua_pushnil( L );
while( lua_next( L, -2 ) != 0 )
{
/* Key is at index -2 and value at index -1. Discard key */
const char *psz_cap = luaL_checkstring( L, -1 );
int i_cap = 0;
bool b_ok = false;
/* Find this capability's flag */
for( const char *iter = *ppsz_capabilities;
iter != NULL;
iter = ppsz_capabilities[ ++i_cap ])
{
if( !strcmp( iter, psz_cap ) )
{
/* Flag it! */
p_ext->p_sys->i_capabilities |= 1 << i_cap;
b_ok = true;
break;
}
}
if( !b_ok )
{
msg_Warn( p_mgr, "Extension capability '%s' unknown in"
" script %s", psz_cap, psz_script );
}
/* Removes 'value'; keeps 'key' for next iteration */
lua_pop( L, 1 );
}
}
else
{
msg_Warn( p_mgr, "In script %s, function descriptor() "
"did not return a table of capabilities.",
psz_script );
}
lua_pop( L, 1 );
lua_getfield( L, -1, "title" );
if( lua_isstring( L, -1 ) )
{
p_ext->psz_title = strdup( luaL_checkstring( L, -1 ) );
}
else
{
msg_Dbg( p_mgr, "In script %s, function descriptor() "
"did not return a string as title.",
psz_script );
p_ext->psz_title = strdup( psz_script );
}
}
else
{
msg_Warn( p_mgr, "In script %s, function descriptor() "
"did not return a table!", psz_script );
goto exit;
}
}
else
{
msg_Err( p_mgr, "Script %s went completely foobar", psz_script );
goto exit;
}
msg_Dbg( p_mgr, "Script %s has the following capability flags: 0x%x",
psz_script, p_ext->p_sys->i_capabilities );
b_ok = true;
exit:
if( !b_ok )
{
free( p_ext->psz_name );
free( p_ext->psz_title );
vlc_mutex_destroy( &p_ext->p_sys->command_lock );
vlc_mutex_destroy( &p_ext->p_sys->running_lock );
vlc_cond_destroy( &p_ext->p_sys->wait );
free( p_ext->p_sys );
free( p_ext );
}
else
{
/* Add the extension to the list of known extensions */
ARRAY_APPEND( p_mgr->extensions, p_ext );
}
vlc_mutex_unlock( &p_mgr->p_sys->lock );
/* Continue batch execution */
return pb_continue ? ( (* (bool*)pb_continue) ? -1 : 0 ) : -1;
}
static int Control( extensions_manager_t *p_mgr, int i_control, va_list args )
{
extension_t *p_ext = NULL;
bool *pb = NULL;
uint16_t **ppus = NULL;
char ***pppsz = NULL;
int i = 0;
switch( i_control )
{
case EXTENSION_ACTIVATE:
p_ext = ( extension_t* ) va_arg( args, extension_t* );
return Activate( p_mgr, p_ext );
case EXTENSION_DEACTIVATE:
p_ext = ( extension_t* ) va_arg( args, extension_t* );
return Deactivate( p_mgr, p_ext );
case EXTENSION_IS_ACTIVATED:
p_ext = ( extension_t* ) va_arg( args, extension_t* );
pb = ( bool* ) va_arg( args, bool* );
*pb = IsActivated( p_mgr, p_ext );
break;
case EXTENSION_HAS_MENU:
p_ext = ( extension_t* ) va_arg( args, extension_t* );
pb = ( bool* ) va_arg( args, bool* );
*pb = ( p_ext->p_sys->i_capabilities & EXT_HAS_MENU ) ? 1 : 0;
break;
case EXTENSION_GET_MENU:
p_ext = ( extension_t* ) va_arg( args, extension_t* );
pppsz = ( char*** ) va_arg( args, char*** );
ppus = ( uint16_t** ) va_arg( args, uint16_t** );
return GetMenu( p_mgr, p_ext, pppsz, ppus );
case EXTENSION_TRIGGER_ONLY:
p_ext = ( extension_t* ) va_arg( args, extension_t* );
pb = ( bool* ) va_arg( args, bool* );
*pb = ( p_ext->p_sys->i_capabilities & EXT_TRIGGER_ONLY ) ? 1 : 0;
break;
case EXTENSION_TRIGGER:
p_ext = ( extension_t* ) va_arg( args, extension_t* );
return TriggerExtension( p_mgr, p_ext );
case EXTENSION_TRIGGER_MENU:
p_ext = ( extension_t* ) va_arg( args, extension_t* );
// GCC: 'uint16_t' is promoted to 'int' when passed through '...'
i = ( int ) va_arg( args, int );
return TriggerMenu( p_ext, i );
default:
msg_Err( p_mgr, "Control '%d' not yet implemented in Extension",
i_control );
return VLC_EGENERIC;
}
return VLC_SUCCESS;
}
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" );
}
int lua_ExtensionDeactivate( extensions_manager_t *p_mgr, extension_t *p_ext )
{
assert( p_mgr != NULL && p_ext != NULL );
if( !p_ext->p_sys->L )
return VLC_SUCCESS;
int i_ret = lua_ExecuteFunction( p_mgr, p_ext, "deactivate" );
/* Clear Lua State */
lua_close( p_ext->p_sys->L );
p_ext->p_sys->L = NULL;
return i_ret;
}
int lua_ExtensionWidgetClick( extensions_manager_t *p_mgr,
extension_t *p_ext,
extension_widget_t *p_widget )
{
if( !p_ext->p_sys->L )
return VLC_SUCCESS;
return lua_ExecuteFunction( p_mgr, p_ext, (const char*) p_widget->p_sys );
}
/**
* Get the list of menu entries from an extension script
* @param p_mgr
* @param p_ext
* @param pppsz_titles Pointer to NULL. All strings must be freed by the caller
* @param ppi_ids Pointer to NULL. Must be feed by the caller.
* @note This function is allowed to run in the UI thread. This means
* that it MUST respond very fast.
* @todo Remove the menu() hook and provide a new function vlc.set_menu()
**/
static int GetMenu( extensions_manager_t *p_mgr, extension_t *p_ext,
char ***pppsz_titles, uint16_t **ppi_ids )
{
assert( *pppsz_titles == NULL );
assert( *ppi_ids == NULL );
if( !IsActivated( p_mgr, p_ext ) )
{
msg_Dbg( p_mgr, "Can't get menu before activating the extension!" );
return VLC_EGENERIC;
}
if( !LockExtension( p_ext ) )
{
/* Dying extension, fail. */
return VLC_EGENERIC;
}
int i_ret = VLC_EGENERIC;
lua_State *L = GetLuaState( p_mgr, p_ext );
if( ( p_ext->p_sys->i_capabilities & EXT_HAS_MENU ) == 0 )
{
msg_Dbg( p_mgr, "can't get a menu from an extension without menu!" );
goto exit;
}
lua_getglobal( L, "menu" );
if( !lua_isfunction( L, -1 ) )
{
msg_Warn( p_mgr, "Error while runing script %s, "
"function menu() not found", p_ext->psz_name );
goto exit;
}
if( lua_pcall( L, 0, 1, 0 ) )
{
msg_Warn( p_mgr, "Error while runing script %s, "
"function menu(): %s", p_ext->psz_name,
lua_tostring( L, lua_gettop( L ) ) );
goto exit;
}
if( lua_gettop( L ) )
{
if( lua_istable( L, -1 ) )
{
/* Get table size */
size_t i_size = lua_objlen( L, -1 );
*pppsz_titles = ( char** ) calloc( i_size+1, sizeof( char* ) );
*ppi_ids = ( uint16_t* ) calloc( i_size+1, sizeof( uint16_t ) );
/* Walk table */
size_t i_idx = 0;
lua_pushnil( L );
while( lua_next( L, -2 ) != 0 )
{
assert( i_idx < i_size );
if( (!lua_isstring( L, -1 )) || (!lua_isnumber( L, -2 )) )
{
msg_Warn( p_mgr, "In script %s, an entry in "
"the menu table is invalid!", p_ext->psz_name );
goto exit;
}
(*pppsz_titles)[ i_idx ] = strdup( luaL_checkstring( L, -1 ) );
(*ppi_ids)[ i_idx ] = (uint16_t) ( luaL_checkinteger( L, -2 ) & 0xFFFF );
i_idx++;
lua_pop( L, 1 );
}
}
else
{
msg_Warn( p_mgr, "Function menu() in script %s "
"did not return a table", p_ext->psz_name );
goto exit;
}
}
else
{
msg_Warn( p_mgr, "Script %s went completely foobar", p_ext->psz_name );
goto exit;
}
i_ret = VLC_SUCCESS;
exit:
UnlockExtension( p_ext );
if( i_ret != VLC_SUCCESS )
{
msg_Dbg( p_mgr, "Something went wrong in %s (%s:%d)",
__func__, __FILE__, __LINE__ );
}
return i_ret;
}
/* Must be entered with the Lock on Extension */
static lua_State* GetLuaState( extensions_manager_t *p_mgr,
extension_t *p_ext )
{
lua_State *L = NULL;
if( p_ext )
L = p_ext->p_sys->L;
if( !L )
{
L = luaL_newstate();
if( !L )
{
msg_Err( p_mgr, "Could not create new Lua State" );
return NULL;
}
luaL_openlibs( L );
luaL_register( L, "vlc", p_reg );
luaopen_msg( L );
lua_pushlightuserdata( L, p_mgr );
lua_setfield( L, -2, "private" );
lua_pushlightuserdata( L, p_ext );
lua_setfield( L, -2, "extension" );
if( p_ext )
{
/* Load more libraries */
luaopen_acl( L );
luaopen_config( L );
luaopen_dialog( L, p_ext );
luaopen_input( L );
luaopen_msg( L );
luaopen_misc( L );
luaopen_net( L );
luaopen_object( L );
luaopen_osd( L );
luaopen_playlist( L );
luaopen_sd( L );
luaopen_stream( L );
luaopen_strings( L );
luaopen_variables( L );
luaopen_video( L );
luaopen_vlm( L );
luaopen_volume( L );
/* Register extension specific functions */
lua_getglobal( L, "vlc" );
lua_pushcfunction( L, vlclua_extension_deactivate );
lua_setfield( L, -2, "deactivate" );
/* Load and run the script(s) */
if( luaL_dofile( L, p_ext->psz_name ) != 0 )
{
msg_Warn( p_mgr, "Error loading script %s: %s", p_ext->psz_name,
lua_tostring( L, lua_gettop( L ) ) );
lua_pop( L, 1 );
return NULL;
}