1 /*****************************************************************************
2 * extension.c: Lua Extensions (meta data, web information, ...)
3 *****************************************************************************
4 * Copyright (C) 2009-2010 VideoLAN and authors
7 * Authors: Jean-Philippe André < jpeg # videolan.org >
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
26 #include "extension.h"
29 /* Functions to register */
30 static const luaL_Reg p_reg[] =
36 * Extensions capabilities
37 * Note: #define and ppsz_capabilities must be in sync
39 #define EXT_HAS_MENU (1 << 0)
40 #define EXT_TRIGGER_ONLY (1 << 1)
42 const char* const ppsz_capabilities[] = {
48 static int ScanExtensions( extensions_manager_t *p_this );
49 static int ScanLuaCallback( vlc_object_t *p_this, const char *psz_script,
50 lua_State *L, void *pb_continue );
51 static int Control( extensions_manager_t *, int, va_list );
52 static int GetMenuEntries( extensions_manager_t *p_mgr, extension_t *p_ext,
53 char ***pppsz_titles, uint16_t **ppi_ids );
54 static lua_State* GetLuaState( extensions_manager_t *p_mgr,
56 static int TriggerMenu( extension_t *p_ext, int id );
57 static int TriggerExtension( extensions_manager_t *p_mgr,
60 int vlclua_extension_deactivate( lua_State *L );
63 static int vlclua_extension_dialog_callback( vlc_object_t *p_this,
73 int Open_Extension( vlc_object_t *p_this )
75 msg_Dbg( p_this, "Opening EXPERIMENTAL Lua Extension module" );
77 extensions_manager_t *p_mgr = ( extensions_manager_t* ) p_this;
79 p_mgr->pf_control = Control;
81 extensions_manager_sys_t *p_sys = ( extensions_manager_sys_t* )
82 calloc( 1, sizeof( extensions_manager_sys_t ) );
83 if( !p_sys ) return VLC_ENOMEM;
86 ARRAY_INIT( p_sys->activated_extensions );
87 ARRAY_INIT( p_mgr->extensions );
88 vlc_mutex_init( &p_mgr->lock );
89 vlc_mutex_init( &p_mgr->p_sys->lock );
91 /* Initialise Lua state structure */
92 lua_State *L = GetLuaState( p_mgr, NULL );
100 /* Scan available Lua Extensions */
101 if( ScanExtensions( p_mgr ) != VLC_SUCCESS )
103 msg_Err( p_mgr, "Can't load extensions modules" );
110 // Create the dialog-event variable
111 var_Create( p_this, "dialog-event", VLC_VAR_ADDRESS );
112 var_AddCallback( p_this, "dialog-event",
113 vlclua_extension_dialog_callback, NULL );
119 * Module unload function
121 void Close_Extension( vlc_object_t *p_this )
123 extensions_manager_t *p_mgr = ( extensions_manager_t* ) p_this;
124 msg_Dbg( p_mgr, "Deactivating all loaded extensions" );
126 vlc_mutex_lock( &p_mgr->lock );
127 p_mgr->p_sys->b_killed = true;
128 vlc_mutex_unlock( &p_mgr->lock );
130 var_Destroy( p_mgr, "dialog-event" );
132 extension_t *p_ext = NULL;
133 FOREACH_ARRAY( p_ext, p_mgr->p_sys->activated_extensions )
136 Deactivate( p_mgr, p_ext );
137 WaitForDeactivation( p_ext );
141 msg_Dbg( p_mgr, "All extensions are now deactivated" );
142 ARRAY_RESET( p_mgr->p_sys->activated_extensions );
144 if( p_mgr->p_sys && p_mgr->p_sys->L )
145 lua_close( p_mgr->p_sys->L );
147 vlc_mutex_destroy( &p_mgr->lock );
148 vlc_mutex_destroy( &p_mgr->p_sys->lock );
149 free( p_mgr->p_sys );
152 /* Free extensions' memory */
153 FOREACH_ARRAY( p_ext, p_mgr->extensions )
157 if( p_ext->p_sys->L )
158 lua_close( p_ext->p_sys->L );
159 free( p_ext->psz_name );
160 free( p_ext->psz_title );
162 vlc_mutex_destroy( &p_ext->p_sys->running_lock );
163 vlc_mutex_destroy( &p_ext->p_sys->command_lock );
164 vlc_cond_destroy( &p_ext->p_sys->wait );
166 free( p_ext->p_sys );
171 ARRAY_RESET( p_mgr->extensions );
175 * Batch scan all Lua files in folder "extensions"
176 * @param p_mgr This extensions_manager_t object
178 static int ScanExtensions( extensions_manager_t *p_mgr )
182 vlclua_scripts_batch_execute( VLC_OBJECT( p_mgr ),
185 p_mgr->p_sys->L, &b_true );
194 * Batch scan all Lua files in folder "extensions": callback
195 * @param p_this This extensions_manager_t object
196 * @param psz_script Name of the script to run
197 * @param L Lua State, common to all scripts here
198 * @param pb_continue bool* that indicates whether to continue batch or not
200 int ScanLuaCallback( vlc_object_t *p_this, const char *psz_script,
201 lua_State *L, void *pb_continue )
203 extensions_manager_t *p_mgr = ( extensions_manager_t* ) p_this;
206 msg_Dbg( p_mgr, "Scanning Lua script %s", psz_script );
208 vlc_mutex_lock( &p_mgr->lock );
210 /* Create new script descriptor */
211 extension_t *p_ext = ( extension_t* ) calloc( 1, sizeof( extension_t ) );
214 vlc_mutex_unlock( &p_mgr->lock );
218 p_ext->psz_name = strdup( psz_script );
219 p_ext->p_sys = (extension_sys_t*) calloc( 1, sizeof( extension_sys_t ) );
220 if( !p_ext->p_sys || !p_ext->psz_name )
222 free( p_ext->psz_name );
223 free( p_ext->p_sys );
225 vlc_mutex_unlock( &p_mgr->lock );
228 p_ext->p_sys->p_mgr = p_mgr;
230 /* Mutexes and conditions */
231 vlc_mutex_init( &p_ext->p_sys->command_lock );
232 vlc_mutex_init( &p_ext->p_sys->running_lock );
233 vlc_cond_init( &p_ext->p_sys->wait );
235 /* Load and run the script(s) */
236 if( luaL_dofile( L, psz_script ) )
238 msg_Warn( p_mgr, "Error loading script %s: %s", psz_script,
239 lua_tostring( L, lua_gettop( L ) ) );
244 /* Scan script for capabilities */
245 lua_getglobal( L, "descriptor" );
247 if( !lua_isfunction( L, -1 ) )
249 msg_Warn( p_mgr, "Error while runing script %s, "
250 "function descriptor() not found", psz_script );
254 if( lua_pcall( L, 0, 1, 0 ) )
256 msg_Warn( p_mgr, "Error while runing script %s, "
257 "function descriptor(): %s", psz_script,
258 lua_tostring( L, lua_gettop( L ) ) );
262 if( lua_gettop( L ) )
264 if( lua_istable( L, -1 ) )
266 lua_getfield( L, -1, "capabilities" );
267 if( lua_istable( L, -1 ) )
270 while( lua_next( L, -2 ) != 0 )
272 /* Key is at index -2 and value at index -1. Discard key */
273 const char *psz_cap = luaL_checkstring( L, -1 );
276 /* Find this capability's flag */
277 for( const char *iter = *ppsz_capabilities;
279 iter = ppsz_capabilities[ ++i_cap ])
281 if( !strcmp( iter, psz_cap ) )
284 p_ext->p_sys->i_capabilities |= 1 << i_cap;
291 msg_Warn( p_mgr, "Extension capability '%s' unknown in"
292 " script %s", psz_cap, psz_script );
294 /* Removes 'value'; keeps 'key' for next iteration */
300 msg_Warn( p_mgr, "In script %s, function descriptor() "
301 "did not return a table of capabilities.",
306 lua_getfield( L, -1, "title" );
307 if( lua_isstring( L, -1 ) )
309 p_ext->psz_title = strdup( luaL_checkstring( L, -1 ) );
313 msg_Dbg( p_mgr, "In script %s, function descriptor() "
314 "did not return a string as title.",
316 p_ext->psz_title = strdup( psz_script );
321 msg_Warn( p_mgr, "In script %s, function descriptor() "
322 "did not return a table!", psz_script );
328 msg_Err( p_mgr, "Script %s went completely foobar", psz_script );
332 msg_Dbg( p_mgr, "Script %s has the following capability flags: 0x%x",
333 psz_script, p_ext->p_sys->i_capabilities );
339 free( p_ext->psz_name );
340 free( p_ext->psz_title );
341 vlc_mutex_destroy( &p_ext->p_sys->command_lock );
342 vlc_mutex_destroy( &p_ext->p_sys->running_lock );
343 vlc_cond_destroy( &p_ext->p_sys->wait );
344 free( p_ext->p_sys );
349 /* Add the extension to the list of known extensions */
350 ARRAY_APPEND( p_mgr->extensions, p_ext );
353 vlc_mutex_unlock( &p_mgr->lock );
354 /* Continue batch execution */
355 return pb_continue ? ( (* (bool*)pb_continue) ? -1 : 0 ) : -1;
358 static int Control( extensions_manager_t *p_mgr, int i_control, va_list args )
360 extension_t *p_ext = NULL;
362 uint16_t **ppus = NULL;
363 char ***pppsz = NULL;
368 case EXTENSION_ACTIVATE:
369 p_ext = ( extension_t* ) va_arg( args, extension_t* );
370 return Activate( p_mgr, p_ext );
372 case EXTENSION_DEACTIVATE:
373 p_ext = ( extension_t* ) va_arg( args, extension_t* );
374 return Deactivate( p_mgr, p_ext );
376 case EXTENSION_IS_ACTIVATED:
377 p_ext = ( extension_t* ) va_arg( args, extension_t* );
378 pb = ( bool* ) va_arg( args, bool* );
379 *pb = IsActivated( p_mgr, p_ext );
382 case EXTENSION_HAS_MENU:
383 p_ext = ( extension_t* ) va_arg( args, extension_t* );
384 pb = ( bool* ) va_arg( args, bool* );
385 *pb = ( p_ext->p_sys->i_capabilities & EXT_HAS_MENU ) ? 1 : 0;
388 case EXTENSION_GET_MENU:
389 p_ext = ( extension_t* ) va_arg( args, extension_t* );
390 pppsz = ( char*** ) va_arg( args, char*** );
391 ppus = ( uint16_t** ) va_arg( args, uint16_t** );
392 return GetMenuEntries( p_mgr, p_ext, pppsz, ppus );
394 case EXTENSION_TRIGGER_ONLY:
395 p_ext = ( extension_t* ) va_arg( args, extension_t* );
396 pb = ( bool* ) va_arg( args, bool* );
397 *pb = ( p_ext->p_sys->i_capabilities & EXT_TRIGGER_ONLY ) ? 1 : 0;
400 case EXTENSION_TRIGGER:
401 p_ext = ( extension_t* ) va_arg( args, extension_t* );
402 return TriggerExtension( p_mgr, p_ext );
404 case EXTENSION_TRIGGER_MENU:
405 p_ext = ( extension_t* ) va_arg( args, extension_t* );
406 // GCC: 'uint16_t' is promoted to 'int' when passed through '...'
407 i = ( int ) va_arg( args, int );
408 return TriggerMenu( p_ext, i );
411 msg_Err( p_mgr, "Control '%d' not yet implemented in Extension",
419 int lua_ExtensionActivate( extensions_manager_t *p_mgr, extension_t *p_ext )
421 assert( p_mgr != NULL && p_ext != NULL );
422 return lua_ExecuteFunction( p_mgr, p_ext, "activate" );
425 int lua_ExtensionDeactivate( extensions_manager_t *p_mgr, extension_t *p_ext )
427 assert( p_mgr != NULL && p_ext != NULL );
429 if( !p_ext->p_sys->L )
432 int i_ret = lua_ExecuteFunction( p_mgr, p_ext, "deactivate" );
434 /* Clear Lua State */
435 lua_close( p_ext->p_sys->L );
436 p_ext->p_sys->L = NULL;
441 int lua_ExtensionWidgetClick( extensions_manager_t *p_mgr,
443 extension_widget_t *p_widget )
445 if( !p_ext->p_sys->L )
448 return lua_ExecuteFunction( p_mgr, p_ext, (const char*) p_widget->p_sys );
453 * Get the list of menu entries from an extension script
456 * @param pppsz_titles Pointer to NULL. All strings must be freed by the caller
457 * @param ppi_ids Pointer to NULL. Must be feed by the caller.
458 * @note This function is allowed to run in the UI thread. This means
459 * that it MUST respond very fast.
460 * @todo Remove the menu() hook and provide a new function vlc.set_menu()
462 static int GetMenuEntries( extensions_manager_t *p_mgr, extension_t *p_ext,
463 char ***pppsz_titles, uint16_t **ppi_ids )
465 assert( *pppsz_titles == NULL );
466 assert( *ppi_ids == NULL );
468 if( !IsActivated( p_mgr, p_ext ) )
470 msg_Dbg( p_mgr, "Can't get menu before activating the extension!" );
474 if( !LockExtension( p_ext ) )
476 /* Dying extension, fail. */
480 int i_ret = VLC_EGENERIC;
481 lua_State *L = GetLuaState( p_mgr, p_ext );
483 if( ( p_ext->p_sys->i_capabilities & EXT_HAS_MENU ) == 0 )
485 msg_Dbg( p_mgr, "can't get a menu from an extension without menu!" );
489 lua_getglobal( L, "menu" );
491 if( !lua_isfunction( L, -1 ) )
493 msg_Warn( p_mgr, "Error while runing script %s, "
494 "function menu() not found", p_ext->psz_name );
498 if( lua_pcall( L, 0, 1, 0 ) )
500 msg_Warn( p_mgr, "Error while runing script %s, "
501 "function menu(): %s", p_ext->psz_name,
502 lua_tostring( L, lua_gettop( L ) ) );
506 if( lua_gettop( L ) )
508 if( lua_istable( L, -1 ) )
511 size_t i_size = lua_objlen( L, -1 );
512 *pppsz_titles = ( char** ) calloc( i_size+1, sizeof( char* ) );
513 *ppi_ids = ( uint16_t* ) calloc( i_size+1, sizeof( uint16_t ) );
518 while( lua_next( L, -2 ) != 0 )
520 assert( i_idx < i_size );
521 if( (!lua_isstring( L, -1 )) || (!lua_isnumber( L, -2 )) )
523 msg_Warn( p_mgr, "In script %s, an entry in "
524 "the menu table is invalid!", p_ext->psz_name );
527 (*pppsz_titles)[ i_idx ] = strdup( luaL_checkstring( L, -1 ) );
528 (*ppi_ids)[ i_idx ] = (uint16_t) ( luaL_checkinteger( L, -2 ) & 0xFFFF );
535 msg_Warn( p_mgr, "Function menu() in script %s "
536 "did not return a table", p_ext->psz_name );
542 msg_Warn( p_mgr, "Script %s went completely foobar", p_ext->psz_name );
549 UnlockExtension( p_ext );
550 if( i_ret != VLC_SUCCESS )
552 msg_Dbg( p_mgr, "Something went wrong in %s (%s:%d)",
553 __func__, __FILE__, __LINE__ );
558 /* Must be entered with the Lock on Extension */
559 static lua_State* GetLuaState( extensions_manager_t *p_mgr,
571 msg_Err( p_mgr, "Could not create new Lua State" );
575 luaL_register( L, "vlc", p_reg );
578 lua_pushlightuserdata( L, p_mgr );
579 lua_setfield( L, -2, "private" );
581 lua_pushlightuserdata( L, p_ext );
582 lua_setfield( L, -2, "extension" );
586 /* Load more libraries */
589 luaopen_dialog( L, p_ext );
596 luaopen_playlist( L );
599 luaopen_strings( L );
600 luaopen_variables( L );
605 /* Register extension specific functions */
606 lua_getglobal( L, "vlc" );
607 lua_pushcfunction( L, vlclua_extension_deactivate );
608 lua_setfield( L, -2, "deactivate" );
610 /* Load and run the script(s) */
611 if( luaL_dofile( L, p_ext->psz_name ) != 0 )
613 msg_Warn( p_mgr, "Error loading script %s: %s", p_ext->psz_name,
614 lua_tostring( L, lua_gettop( L ) ) );
625 msg_Dbg( p_mgr, "Reusing old Lua state for extension '%s'",
634 * Execute a function in a Lua script
635 * @return < 0 in case of failure, >= 0 in case of success
636 * @note It's better to call this function from a dedicated thread
637 * (see extension_thread.c)
639 int lua_ExecuteFunction( extensions_manager_t *p_mgr,
641 const char *psz_function )
643 int i_ret = VLC_EGENERIC;
644 assert( p_mgr != NULL );
645 assert( p_ext != NULL );
647 lua_State *L = GetLuaState( p_mgr, p_ext );
648 lua_getglobal( L, psz_function );
650 if( !lua_isfunction( L, -1 ) )
652 msg_Warn( p_mgr, "Error while runing script %s, "
653 "function %s() not found", p_ext->psz_name, psz_function );
657 if( lua_pcall( L, 0, 1, 0 ) )
659 msg_Warn( p_mgr, "Error while runing script %s, "
660 "function %s(): %s", p_ext->psz_name, psz_function,
661 lua_tostring( L, lua_gettop( L ) ) );
670 static inline int TriggerMenu( extension_t *p_ext, int i_id )
672 return PushCommand( p_ext, CMD_TRIGGERMENU, i_id );
675 int lua_ExtensionTriggerMenu( extensions_manager_t *p_mgr,
676 extension_t *p_ext, int id )
678 int i_ret = VLC_EGENERIC;
679 lua_State *L = GetLuaState( p_mgr, p_ext );
684 luaopen_dialog( L, p_ext );
686 lua_getglobal( L, "trigger_menu" );
687 if( !lua_isfunction( L, -1 ) )
689 msg_Warn( p_mgr, "Error while runing script %s, "
690 "function trigger_menu() not found", p_ext->psz_name );
694 /* Pass id as unique argument to the function */
695 lua_pushinteger( L, id );
697 if( lua_pcall( L, 1, 1, 0 ) != 0 )
699 msg_Warn( p_mgr, "Error while runing script %s, "
700 "function trigger_menu(): %s", p_ext->psz_name,
701 lua_tostring( L, lua_gettop( L ) ) );
705 if( i_ret < VLC_SUCCESS )
707 msg_Dbg( p_mgr, "Something went wrong in %s (%s:%d)",
708 __func__, __FILE__, __LINE__ );
713 /** Directly trigger an extension, without activating it
714 * This is NOT multithreaded, and this code runs in the UI thread
716 * @param p_ext Extension to trigger
717 * @return Value returned by the lua function "trigger"
719 static int TriggerExtension( extensions_manager_t *p_mgr,
722 int i_ret = lua_ExecuteFunction( p_mgr, p_ext, "trigger" );
724 /* Close lua state for trigger-only extensions */
725 if( p_ext->p_sys->L )
726 lua_close( p_ext->p_sys->L );
727 p_ext->p_sys->L = NULL;
732 /** Retrieve extension associated to the current script
733 * @param L current lua_State
734 * @return Lua userdata "vlc.extension"
736 extension_t *vlclua_extension_get( lua_State *L )
738 extension_t *p_ext = NULL;
739 lua_getglobal( L, "vlc" );
740 lua_getfield( L, -1, "extension" );
741 p_ext = (extension_t*) lua_topointer( L, lua_gettop( L ) );
746 /** Deactivate an extension by order from the extension itself
748 * @note This is an asynchronous call. A script calling vlc.deactivate() will
749 * be executed to the end before the last call to deactivate() is done.
751 int vlclua_extension_deactivate( lua_State *L )
753 extension_t *p_ext = vlclua_extension_get( L );
754 int i_ret = Deactivate( p_ext->p_sys->p_mgr, p_ext );
755 return ( i_ret == VLC_SUCCESS ) ? 1 : 0;
758 /** Callback for the variable "dialog-event"
759 * @param p_this Current object owner of the extension and the dialog
760 * @param psz_var "dialog-event"
761 * @param oldval Unused
762 * @param newval Address of the dialog
763 * @param p_data Unused
765 static int vlclua_extension_dialog_callback( vlc_object_t *p_this,
771 /* psz_var == "dialog-event" */
776 extension_dialog_command_t *command = newval.p_address;
777 assert( command != NULL );
778 assert( command->p_dlg != NULL);
780 extension_t *p_ext = command->p_dlg->p_sys;
781 assert( p_ext != NULL );
783 extension_widget_t *p_widget = command->p_data;
785 switch( command->event )
787 case EXTENSION_EVENT_CLICK:
788 assert( p_widget != NULL );
789 PushCommand( p_ext, CMD_CLICK, p_widget );
791 case EXTENSION_EVENT_CLOSE:
792 PushCommand( p_ext, CMD_CLOSE );
795 msg_Dbg( p_this, "Received unknown UI event %d, discarded",
803 /* Lock this extension. Can fail. */
804 bool LockExtension( extension_t *p_ext )
806 if( p_ext->p_sys->b_exiting )
809 vlc_mutex_lock( &p_ext->p_sys->running_lock );
810 if( p_ext->p_sys->b_exiting )
812 vlc_mutex_unlock( &p_ext->p_sys->running_lock );
819 void UnlockExtension( extension_t *p_ext )
821 vlc_mutex_unlock( &p_ext->p_sys->running_lock );