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 ) )
267 lua_getfield( L, -1, "capabilities" );
268 if( lua_istable( L, -1 ) )
271 while( lua_next( L, -2 ) != 0 )
273 /* Key is at index -2 and value at index -1. Discard key */
274 const char *psz_cap = luaL_checkstring( L, -1 );
277 /* Find this capability's flag */
278 for( const char *iter = *ppsz_capabilities;
280 iter = ppsz_capabilities[ ++i_cap ])
282 if( !strcmp( iter, psz_cap ) )
285 p_ext->p_sys->i_capabilities |= 1 << i_cap;
292 msg_Warn( p_mgr, "Extension capability '%s' unknown in"
293 " script %s", psz_cap, psz_script );
295 /* Removes 'value'; keeps 'key' for next iteration */
301 msg_Warn( p_mgr, "In script %s, function descriptor() "
302 "did not return a table of capabilities.",
308 lua_getfield( L, -1, "title" );
309 if( lua_isstring( L, -1 ) )
311 p_ext->psz_title = strdup( luaL_checkstring( L, -1 ) );
315 msg_Dbg( p_mgr, "In script %s, function descriptor() "
316 "did not return a string as title.",
318 p_ext->psz_title = strdup( psz_script );
323 lua_getfield( L, -1, "author" );
324 if( lua_isstring( L, -1 ) )
326 p_ext->psz_author = strdup( luaL_checkstring( L, -1 ) );
330 p_ext->psz_author = NULL;
334 /* Get description */
335 lua_getfield( L, -1, "description" );
336 if( lua_isstring( L, -1 ) )
338 p_ext->psz_description = strdup( luaL_checkstring( L, -1 ) );
342 p_ext->psz_description = NULL;
347 lua_getfield( L, -1, "url" );
348 if( lua_isstring( L, -1 ) )
350 p_ext->psz_url = strdup( luaL_checkstring( L, -1 ) );
354 p_ext->psz_url = NULL;
359 lua_getfield( L, -1, "version" );
360 if( lua_isstring( L, -1 ) )
362 p_ext->psz_version = strdup( luaL_checkstring( L, -1 ) );
366 p_ext->psz_version = NULL;
372 msg_Warn( p_mgr, "In script %s, function descriptor() "
373 "did not return a table!", psz_script );
379 msg_Err( p_mgr, "Script %s went completely foobar", psz_script );
383 msg_Dbg( p_mgr, "Script %s has the following capability flags: 0x%x",
384 psz_script, p_ext->p_sys->i_capabilities );
390 free( p_ext->psz_name );
391 free( p_ext->psz_title );
392 free( p_ext->psz_url );
393 free( p_ext->psz_author );
394 free( p_ext->psz_description );
395 free( p_ext->psz_version );
396 vlc_mutex_destroy( &p_ext->p_sys->command_lock );
397 vlc_mutex_destroy( &p_ext->p_sys->running_lock );
398 vlc_cond_destroy( &p_ext->p_sys->wait );
399 free( p_ext->p_sys );
404 /* Add the extension to the list of known extensions */
405 ARRAY_APPEND( p_mgr->extensions, p_ext );
408 vlc_mutex_unlock( &p_mgr->lock );
409 /* Continue batch execution */
410 return pb_continue ? ( (* (bool*)pb_continue) ? -1 : 0 ) : -1;
413 static int Control( extensions_manager_t *p_mgr, int i_control, va_list args )
415 extension_t *p_ext = NULL;
417 uint16_t **ppus = NULL;
418 char ***pppsz = NULL;
423 case EXTENSION_ACTIVATE:
424 p_ext = ( extension_t* ) va_arg( args, extension_t* );
425 return Activate( p_mgr, p_ext );
427 case EXTENSION_DEACTIVATE:
428 p_ext = ( extension_t* ) va_arg( args, extension_t* );
429 return Deactivate( p_mgr, p_ext );
431 case EXTENSION_IS_ACTIVATED:
432 p_ext = ( extension_t* ) va_arg( args, extension_t* );
433 pb = ( bool* ) va_arg( args, bool* );
434 *pb = IsActivated( p_mgr, p_ext );
437 case EXTENSION_HAS_MENU:
438 p_ext = ( extension_t* ) va_arg( args, extension_t* );
439 pb = ( bool* ) va_arg( args, bool* );
440 *pb = ( p_ext->p_sys->i_capabilities & EXT_HAS_MENU ) ? 1 : 0;
443 case EXTENSION_GET_MENU:
444 p_ext = ( extension_t* ) va_arg( args, extension_t* );
445 pppsz = ( char*** ) va_arg( args, char*** );
446 ppus = ( uint16_t** ) va_arg( args, uint16_t** );
447 return GetMenuEntries( p_mgr, p_ext, pppsz, ppus );
449 case EXTENSION_TRIGGER_ONLY:
450 p_ext = ( extension_t* ) va_arg( args, extension_t* );
451 pb = ( bool* ) va_arg( args, bool* );
452 *pb = ( p_ext->p_sys->i_capabilities & EXT_TRIGGER_ONLY ) ? 1 : 0;
455 case EXTENSION_TRIGGER:
456 p_ext = ( extension_t* ) va_arg( args, extension_t* );
457 return TriggerExtension( p_mgr, p_ext );
459 case EXTENSION_TRIGGER_MENU:
460 p_ext = ( extension_t* ) va_arg( args, extension_t* );
461 // GCC: 'uint16_t' is promoted to 'int' when passed through '...'
462 i = ( int ) va_arg( args, int );
463 return TriggerMenu( p_ext, i );
466 msg_Err( p_mgr, "Control '%d' not yet implemented in Extension",
474 int lua_ExtensionActivate( extensions_manager_t *p_mgr, extension_t *p_ext )
476 assert( p_mgr != NULL && p_ext != NULL );
477 return lua_ExecuteFunction( p_mgr, p_ext, "activate" );
480 int lua_ExtensionDeactivate( extensions_manager_t *p_mgr, extension_t *p_ext )
482 assert( p_mgr != NULL && p_ext != NULL );
484 if( !p_ext->p_sys->L )
487 int i_ret = lua_ExecuteFunction( p_mgr, p_ext, "deactivate" );
489 /* Clear Lua State */
490 lua_close( p_ext->p_sys->L );
491 p_ext->p_sys->L = NULL;
496 int lua_ExtensionWidgetClick( extensions_manager_t *p_mgr,
498 extension_widget_t *p_widget )
500 if( !p_ext->p_sys->L )
503 return lua_ExecuteFunction( p_mgr, p_ext, (const char*) p_widget->p_sys );
508 * Get the list of menu entries from an extension script
511 * @param pppsz_titles Pointer to NULL. All strings must be freed by the caller
512 * @param ppi_ids Pointer to NULL. Must be feed by the caller.
513 * @note This function is allowed to run in the UI thread. This means
514 * that it MUST respond very fast.
515 * @todo Remove the menu() hook and provide a new function vlc.set_menu()
517 static int GetMenuEntries( extensions_manager_t *p_mgr, extension_t *p_ext,
518 char ***pppsz_titles, uint16_t **ppi_ids )
520 assert( *pppsz_titles == NULL );
521 assert( *ppi_ids == NULL );
523 if( !IsActivated( p_mgr, p_ext ) )
525 msg_Dbg( p_mgr, "Can't get menu before activating the extension!" );
529 if( !LockExtension( p_ext ) )
531 /* Dying extension, fail. */
535 int i_ret = VLC_EGENERIC;
536 lua_State *L = GetLuaState( p_mgr, p_ext );
538 if( ( p_ext->p_sys->i_capabilities & EXT_HAS_MENU ) == 0 )
540 msg_Dbg( p_mgr, "can't get a menu from an extension without menu!" );
544 lua_getglobal( L, "menu" );
546 if( !lua_isfunction( L, -1 ) )
548 msg_Warn( p_mgr, "Error while runing script %s, "
549 "function menu() not found", p_ext->psz_name );
553 if( lua_pcall( L, 0, 1, 0 ) )
555 msg_Warn( p_mgr, "Error while runing script %s, "
556 "function menu(): %s", p_ext->psz_name,
557 lua_tostring( L, lua_gettop( L ) ) );
561 if( lua_gettop( L ) )
563 if( lua_istable( L, -1 ) )
566 size_t i_size = lua_objlen( L, -1 );
567 *pppsz_titles = ( char** ) calloc( i_size+1, sizeof( char* ) );
568 *ppi_ids = ( uint16_t* ) calloc( i_size+1, sizeof( uint16_t ) );
573 while( lua_next( L, -2 ) != 0 )
575 assert( i_idx < i_size );
576 if( (!lua_isstring( L, -1 )) || (!lua_isnumber( L, -2 )) )
578 msg_Warn( p_mgr, "In script %s, an entry in "
579 "the menu table is invalid!", p_ext->psz_name );
582 (*pppsz_titles)[ i_idx ] = strdup( luaL_checkstring( L, -1 ) );
583 (*ppi_ids)[ i_idx ] = (uint16_t) ( luaL_checkinteger( L, -2 ) & 0xFFFF );
590 msg_Warn( p_mgr, "Function menu() in script %s "
591 "did not return a table", p_ext->psz_name );
597 msg_Warn( p_mgr, "Script %s went completely foobar", p_ext->psz_name );
604 UnlockExtension( p_ext );
605 if( i_ret != VLC_SUCCESS )
607 msg_Dbg( p_mgr, "Something went wrong in %s (%s:%d)",
608 __func__, __FILE__, __LINE__ );
613 /* Must be entered with the Lock on Extension */
614 static lua_State* GetLuaState( extensions_manager_t *p_mgr,
626 msg_Err( p_mgr, "Could not create new Lua State" );
630 luaL_register( L, "vlc", p_reg );
633 lua_pushlightuserdata( L, p_mgr );
634 lua_setfield( L, -2, "private" );
636 lua_pushlightuserdata( L, p_ext );
637 lua_setfield( L, -2, "extension" );
641 /* Load more libraries */
644 luaopen_dialog( L, p_ext );
651 luaopen_playlist( L );
654 luaopen_strings( L );
655 luaopen_variables( L );
660 /* Register extension specific functions */
661 lua_getglobal( L, "vlc" );
662 lua_pushcfunction( L, vlclua_extension_deactivate );
663 lua_setfield( L, -2, "deactivate" );
665 /* Load and run the script(s) */
666 if( luaL_dofile( L, p_ext->psz_name ) != 0 )
668 msg_Warn( p_mgr, "Error loading script %s: %s", p_ext->psz_name,
669 lua_tostring( L, lua_gettop( L ) ) );
680 msg_Dbg( p_mgr, "Reusing old Lua state for extension '%s'",
689 * Execute a function in a Lua script
690 * @return < 0 in case of failure, >= 0 in case of success
691 * @note It's better to call this function from a dedicated thread
692 * (see extension_thread.c)
694 int lua_ExecuteFunction( extensions_manager_t *p_mgr,
696 const char *psz_function )
698 int i_ret = VLC_EGENERIC;
699 assert( p_mgr != NULL );
700 assert( p_ext != NULL );
702 lua_State *L = GetLuaState( p_mgr, p_ext );
703 lua_getglobal( L, psz_function );
705 if( !lua_isfunction( L, -1 ) )
707 msg_Warn( p_mgr, "Error while runing script %s, "
708 "function %s() not found", p_ext->psz_name, psz_function );
712 if( lua_pcall( L, 0, 1, 0 ) )
714 msg_Warn( p_mgr, "Error while runing script %s, "
715 "function %s(): %s", p_ext->psz_name, psz_function,
716 lua_tostring( L, lua_gettop( L ) ) );
725 static inline int TriggerMenu( extension_t *p_ext, int i_id )
727 return PushCommand( p_ext, CMD_TRIGGERMENU, i_id );
730 int lua_ExtensionTriggerMenu( extensions_manager_t *p_mgr,
731 extension_t *p_ext, int id )
733 int i_ret = VLC_EGENERIC;
734 lua_State *L = GetLuaState( p_mgr, p_ext );
739 luaopen_dialog( L, p_ext );
741 lua_getglobal( L, "trigger_menu" );
742 if( !lua_isfunction( L, -1 ) )
744 msg_Warn( p_mgr, "Error while runing script %s, "
745 "function trigger_menu() not found", p_ext->psz_name );
749 /* Pass id as unique argument to the function */
750 lua_pushinteger( L, id );
752 if( lua_pcall( L, 1, 1, 0 ) != 0 )
754 msg_Warn( p_mgr, "Error while runing script %s, "
755 "function trigger_menu(): %s", p_ext->psz_name,
756 lua_tostring( L, lua_gettop( L ) ) );
760 if( i_ret < VLC_SUCCESS )
762 msg_Dbg( p_mgr, "Something went wrong in %s (%s:%d)",
763 __func__, __FILE__, __LINE__ );
768 /** Directly trigger an extension, without activating it
769 * This is NOT multithreaded, and this code runs in the UI thread
771 * @param p_ext Extension to trigger
772 * @return Value returned by the lua function "trigger"
774 static int TriggerExtension( extensions_manager_t *p_mgr,
777 int i_ret = lua_ExecuteFunction( p_mgr, p_ext, "trigger" );
779 /* Close lua state for trigger-only extensions */
780 if( p_ext->p_sys->L )
781 lua_close( p_ext->p_sys->L );
782 p_ext->p_sys->L = NULL;
787 /** Retrieve extension associated to the current script
788 * @param L current lua_State
789 * @return Lua userdata "vlc.extension"
791 extension_t *vlclua_extension_get( lua_State *L )
793 extension_t *p_ext = NULL;
794 lua_getglobal( L, "vlc" );
795 lua_getfield( L, -1, "extension" );
796 p_ext = (extension_t*) lua_topointer( L, lua_gettop( L ) );
801 /** Deactivate an extension by order from the extension itself
803 * @note This is an asynchronous call. A script calling vlc.deactivate() will
804 * be executed to the end before the last call to deactivate() is done.
806 int vlclua_extension_deactivate( lua_State *L )
808 extension_t *p_ext = vlclua_extension_get( L );
809 int i_ret = Deactivate( p_ext->p_sys->p_mgr, p_ext );
810 return ( i_ret == VLC_SUCCESS ) ? 1 : 0;
813 /** Callback for the variable "dialog-event"
814 * @param p_this Current object owner of the extension and the dialog
815 * @param psz_var "dialog-event"
816 * @param oldval Unused
817 * @param newval Address of the dialog
818 * @param p_data Unused
820 static int vlclua_extension_dialog_callback( vlc_object_t *p_this,
826 /* psz_var == "dialog-event" */
831 extension_dialog_command_t *command = newval.p_address;
832 assert( command != NULL );
833 assert( command->p_dlg != NULL);
835 extension_t *p_ext = command->p_dlg->p_sys;
836 assert( p_ext != NULL );
838 extension_widget_t *p_widget = command->p_data;
840 switch( command->event )
842 case EXTENSION_EVENT_CLICK:
843 assert( p_widget != NULL );
844 PushCommand( p_ext, CMD_CLICK, p_widget );
846 case EXTENSION_EVENT_CLOSE:
847 PushCommand( p_ext, CMD_CLOSE );
850 msg_Dbg( p_this, "Received unknown UI event %d, discarded",
858 /* Lock this extension. Can fail. */
859 bool LockExtension( extension_t *p_ext )
861 if( p_ext->p_sys->b_exiting )
864 vlc_mutex_lock( &p_ext->p_sys->running_lock );
865 if( p_ext->p_sys->b_exiting )
867 vlc_mutex_unlock( &p_ext->p_sys->running_lock );
874 void UnlockExtension( extension_t *p_ext )
876 vlc_mutex_unlock( &p_ext->p_sys->running_lock );