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 GetMenu( 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 );
89 /* Initialise Lua state structure */
90 lua_State *L = GetLuaState( p_mgr, NULL );
98 /* Scan available Lua Extensions */
99 if( ScanExtensions( p_mgr ) != VLC_SUCCESS )
101 msg_Err( p_mgr, "Can't load extensions modules" );
108 vlc_mutex_init( &p_sys->lock );
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->p_sys->lock );
127 p_mgr->p_sys->b_killed = true;
128 vlc_mutex_unlock( &p_mgr->p_sys->lock );
130 extension_t *p_ext = NULL;
131 FOREACH_ARRAY( p_ext, p_mgr->p_sys->activated_extensions )
134 Deactivate( p_mgr, p_ext );
135 WaitForDeactivation( p_ext );
139 msg_Dbg( p_mgr, "All extensions are now deactivated" );
140 ARRAY_RESET( p_mgr->p_sys->activated_extensions );
142 if( p_mgr->p_sys && p_mgr->p_sys->L )
143 lua_close( p_mgr->p_sys->L );
145 vlc_mutex_destroy( &p_mgr->p_sys->lock );
146 free( p_mgr->p_sys );
149 /* Free extensions' memory */
150 FOREACH_ARRAY( p_ext, p_mgr->extensions )
154 if( p_ext->p_sys->L )
155 lua_close( p_ext->p_sys->L );
156 free( p_ext->psz_name );
157 free( p_ext->psz_title );
159 vlc_mutex_destroy( &p_ext->p_sys->running_lock );
160 vlc_mutex_destroy( &p_ext->p_sys->command_lock );
161 vlc_cond_destroy( &p_ext->p_sys->wait );
163 free( p_ext->p_sys );
168 ARRAY_RESET( p_mgr->extensions );
170 var_Destroy( p_mgr, "dialog-event" );
174 * Batch scan all Lua files in folder "extensions"
175 * @param p_mgr This extensions_manager_t object
177 static int ScanExtensions( extensions_manager_t *p_mgr )
181 vlclua_scripts_batch_execute( VLC_OBJECT( p_mgr ),
184 p_mgr->p_sys->L, &b_true );
193 * Batch scan all Lua files in folder "extensions": callback
194 * @param p_this This extensions_manager_t object
195 * @param psz_script Name of the script to run
196 * @param L Lua State, common to all scripts here
197 * @param pb_continue bool* that indicates whether to continue batch or not
199 int ScanLuaCallback( vlc_object_t *p_this, const char *psz_script,
200 lua_State *L, void *pb_continue )
202 extensions_manager_t *p_mgr = ( extensions_manager_t* ) p_this;
205 msg_Dbg( p_mgr, "Scanning Lua script %s", psz_script );
207 vlc_mutex_lock( &p_mgr->p_sys->lock );
209 /* Create new script descriptor */
210 extension_t *p_ext = ( extension_t* ) calloc( 1, sizeof( extension_t ) );
213 vlc_mutex_unlock( &p_mgr->p_sys->lock );
217 p_ext->psz_name = strdup( psz_script );
218 p_ext->p_sys = (extension_sys_t*) calloc( 1, sizeof( extension_sys_t ) );
219 if( !p_ext->p_sys || !p_ext->psz_name )
221 free( p_ext->psz_name );
222 free( p_ext->p_sys );
224 vlc_mutex_unlock( &p_mgr->p_sys->lock );
227 p_ext->p_sys->p_mgr = p_mgr;
229 /* Mutexes and conditions */
230 vlc_mutex_init( &p_ext->p_sys->command_lock );
231 vlc_mutex_init( &p_ext->p_sys->running_lock );
232 vlc_cond_init( &p_ext->p_sys->wait );
234 /* Load and run the script(s) */
235 if( luaL_dofile( L, psz_script ) )
237 msg_Warn( p_mgr, "Error loading script %s: %s", psz_script,
238 lua_tostring( L, lua_gettop( L ) ) );
243 /* Scan script for capabilities */
244 lua_getglobal( L, "descriptor" );
246 if( !lua_isfunction( L, -1 ) )
248 msg_Warn( p_mgr, "Error while runing script %s, "
249 "function descriptor() not found", psz_script );
253 if( lua_pcall( L, 0, 1, 0 ) )
255 msg_Warn( p_mgr, "Error while runing script %s, "
256 "function descriptor(): %s", psz_script,
257 lua_tostring( L, lua_gettop( L ) ) );
261 if( lua_gettop( L ) )
263 if( lua_istable( L, -1 ) )
265 lua_getfield( L, -1, "capabilities" );
266 if( lua_istable( L, -1 ) )
269 while( lua_next( L, -2 ) != 0 )
271 /* Key is at index -2 and value at index -1. Discard key */
272 const char *psz_cap = luaL_checkstring( L, -1 );
275 /* Find this capability's flag */
276 for( const char *iter = *ppsz_capabilities;
278 iter = ppsz_capabilities[ ++i_cap ])
280 if( !strcmp( iter, psz_cap ) )
283 p_ext->p_sys->i_capabilities |= 1 << i_cap;
290 msg_Warn( p_mgr, "Extension capability '%s' unknown in"
291 " script %s", psz_cap, psz_script );
293 /* Removes 'value'; keeps 'key' for next iteration */
299 msg_Warn( p_mgr, "In script %s, function descriptor() "
300 "did not return a table of capabilities.",
305 lua_getfield( L, -1, "title" );
306 if( lua_isstring( L, -1 ) )
308 p_ext->psz_title = strdup( luaL_checkstring( L, -1 ) );
312 msg_Dbg( p_mgr, "In script %s, function descriptor() "
313 "did not return a string as title.",
315 p_ext->psz_title = strdup( psz_script );
320 msg_Warn( p_mgr, "In script %s, function descriptor() "
321 "did not return a table!", psz_script );
327 msg_Err( p_mgr, "Script %s went completely foobar", psz_script );
331 msg_Dbg( p_mgr, "Script %s has the following capability flags: 0x%x",
332 psz_script, p_ext->p_sys->i_capabilities );
338 free( p_ext->psz_name );
339 free( p_ext->psz_title );
340 vlc_mutex_destroy( &p_ext->p_sys->command_lock );
341 vlc_mutex_destroy( &p_ext->p_sys->running_lock );
342 vlc_cond_destroy( &p_ext->p_sys->wait );
343 free( p_ext->p_sys );
348 /* Add the extension to the list of known extensions */
349 ARRAY_APPEND( p_mgr->extensions, p_ext );
352 vlc_mutex_unlock( &p_mgr->p_sys->lock );
353 /* Continue batch execution */
354 return pb_continue ? ( (* (bool*)pb_continue) ? -1 : 0 ) : -1;
357 static int Control( extensions_manager_t *p_mgr, int i_control, va_list args )
359 extension_t *p_ext = NULL;
361 uint16_t **ppus = NULL;
362 char ***pppsz = NULL;
367 case EXTENSION_ACTIVATE:
368 p_ext = ( extension_t* ) va_arg( args, extension_t* );
369 return Activate( p_mgr, p_ext );
371 case EXTENSION_DEACTIVATE:
372 p_ext = ( extension_t* ) va_arg( args, extension_t* );
373 return Deactivate( p_mgr, p_ext );
375 case EXTENSION_IS_ACTIVATED:
376 p_ext = ( extension_t* ) va_arg( args, extension_t* );
377 pb = ( bool* ) va_arg( args, bool* );
378 *pb = IsActivated( p_mgr, p_ext );
381 case EXTENSION_HAS_MENU:
382 p_ext = ( extension_t* ) va_arg( args, extension_t* );
383 pb = ( bool* ) va_arg( args, bool* );
384 *pb = ( p_ext->p_sys->i_capabilities & EXT_HAS_MENU ) ? 1 : 0;
387 case EXTENSION_GET_MENU:
388 p_ext = ( extension_t* ) va_arg( args, extension_t* );
389 pppsz = ( char*** ) va_arg( args, char*** );
390 ppus = ( uint16_t** ) va_arg( args, uint16_t** );
391 return GetMenu( p_mgr, p_ext, pppsz, ppus );
393 case EXTENSION_TRIGGER_ONLY:
394 p_ext = ( extension_t* ) va_arg( args, extension_t* );
395 pb = ( bool* ) va_arg( args, bool* );
396 *pb = ( p_ext->p_sys->i_capabilities & EXT_TRIGGER_ONLY ) ? 1 : 0;
399 case EXTENSION_TRIGGER:
400 p_ext = ( extension_t* ) va_arg( args, extension_t* );
401 return TriggerExtension( p_mgr, p_ext );
403 case EXTENSION_TRIGGER_MENU:
404 p_ext = ( extension_t* ) va_arg( args, extension_t* );
405 // GCC: 'uint16_t' is promoted to 'int' when passed through '...'
406 i = ( int ) va_arg( args, int );
407 return TriggerMenu( p_ext, i );
410 msg_Err( p_mgr, "Control '%d' not yet implemented in Extension",
418 int lua_ExtensionActivate( extensions_manager_t *p_mgr, extension_t *p_ext )
420 assert( p_mgr != NULL && p_ext != NULL );
421 return lua_ExecuteFunction( p_mgr, p_ext, "activate" );
424 int lua_ExtensionDeactivate( extensions_manager_t *p_mgr, extension_t *p_ext )
426 assert( p_mgr != NULL && p_ext != NULL );
428 if( !p_ext->p_sys->L )
431 int i_ret = lua_ExecuteFunction( p_mgr, p_ext, "deactivate" );
433 /* Clear Lua State */
434 lua_close( p_ext->p_sys->L );
435 p_ext->p_sys->L = NULL;
440 int lua_ExtensionWidgetClick( extensions_manager_t *p_mgr,
442 extension_widget_t *p_widget )
444 if( !p_ext->p_sys->L )
447 return lua_ExecuteFunction( p_mgr, p_ext, (const char*) p_widget->p_sys );
452 * Get the list of menu entries from an extension script
455 * @param pppsz_titles Pointer to NULL. All strings must be freed by the caller
456 * @param ppi_ids Pointer to NULL. Must be feed by the caller.
457 * @note This function is allowed to run in the UI thread. This means
458 * that it MUST respond very fast.
459 * @todo Remove the menu() hook and provide a new function vlc.set_menu()
461 static int GetMenu( extensions_manager_t *p_mgr, extension_t *p_ext,
462 char ***pppsz_titles, uint16_t **ppi_ids )
464 assert( *pppsz_titles == NULL );
465 assert( *ppi_ids == NULL );
467 if( !IsActivated( p_mgr, p_ext ) )
469 msg_Dbg( p_mgr, "Can't get menu before activating the extension!" );
473 if( !LockExtension( p_ext ) )
475 /* Dying extension, fail. */
479 int i_ret = VLC_EGENERIC;
480 lua_State *L = GetLuaState( p_mgr, p_ext );
482 if( ( p_ext->p_sys->i_capabilities & EXT_HAS_MENU ) == 0 )
484 msg_Dbg( p_mgr, "can't get a menu from an extension without menu!" );
488 lua_getglobal( L, "menu" );
490 if( !lua_isfunction( L, -1 ) )
492 msg_Warn( p_mgr, "Error while runing script %s, "
493 "function menu() not found", p_ext->psz_name );
497 if( lua_pcall( L, 0, 1, 0 ) )
499 msg_Warn( p_mgr, "Error while runing script %s, "
500 "function menu(): %s", p_ext->psz_name,
501 lua_tostring( L, lua_gettop( L ) ) );
505 if( lua_gettop( L ) )
507 if( lua_istable( L, -1 ) )
510 size_t i_size = lua_objlen( L, -1 );
511 *pppsz_titles = ( char** ) calloc( i_size+1, sizeof( char* ) );
512 *ppi_ids = ( uint16_t* ) calloc( i_size+1, sizeof( uint16_t ) );
517 while( lua_next( L, -2 ) != 0 )
519 assert( i_idx < i_size );
520 if( (!lua_isstring( L, -1 )) || (!lua_isnumber( L, -2 )) )
522 msg_Warn( p_mgr, "In script %s, an entry in "
523 "the menu table is invalid!", p_ext->psz_name );
526 (*pppsz_titles)[ i_idx ] = strdup( luaL_checkstring( L, -1 ) );
527 (*ppi_ids)[ i_idx ] = (uint16_t) ( luaL_checkinteger( L, -2 ) & 0xFFFF );
534 msg_Warn( p_mgr, "Function menu() in script %s "
535 "did not return a table", p_ext->psz_name );
541 msg_Warn( p_mgr, "Script %s went completely foobar", p_ext->psz_name );
548 UnlockExtension( p_ext );
549 if( i_ret != VLC_SUCCESS )
551 msg_Dbg( p_mgr, "Something went wrong in %s (%s:%d)",
552 __func__, __FILE__, __LINE__ );
557 /* Must be entered with the Lock on Extension */
558 static lua_State* GetLuaState( extensions_manager_t *p_mgr,
570 msg_Err( p_mgr, "Could not create new Lua State" );
574 luaL_register( L, "vlc", p_reg );
577 lua_pushlightuserdata( L, p_mgr );
578 lua_setfield( L, -2, "private" );
580 lua_pushlightuserdata( L, p_ext );
581 lua_setfield( L, -2, "extension" );
585 /* Load more libraries */
588 luaopen_dialog( L, p_ext );
595 luaopen_playlist( L );
598 luaopen_strings( L );
599 luaopen_variables( L );
604 /* Register extension specific functions */
605 lua_getglobal( L, "vlc" );
606 lua_pushcfunction( L, vlclua_extension_deactivate );
607 lua_setfield( L, -2, "deactivate" );
609 /* Load and run the script(s) */
610 if( luaL_dofile( L, p_ext->psz_name ) != 0 )
612 msg_Warn( p_mgr, "Error loading script %s: %s", p_ext->psz_name,
613 lua_tostring( L, lua_gettop( L ) ) );
624 msg_Dbg( p_mgr, "Reusing old Lua state for extension '%s'",
633 * Execute a function in a Lua script
634 * @return < 0 in case of failure, >= 0 in case of success
635 * @note It's better to call this function from a dedicated thread
636 * (see extension_thread.c)
638 int lua_ExecuteFunction( extensions_manager_t *p_mgr,
640 const char *psz_function )
642 int i_ret = VLC_EGENERIC;
643 assert( p_mgr != NULL );
644 assert( p_ext != NULL );
646 lua_State *L = GetLuaState( p_mgr, p_ext );
647 lua_getglobal( L, psz_function );
649 if( !lua_isfunction( L, -1 ) )
651 msg_Warn( p_mgr, "Error while runing script %s, "
652 "function %s() not found", p_ext->psz_name, psz_function );
656 if( lua_pcall( L, 0, 1, 0 ) )
658 msg_Warn( p_mgr, "Error while runing script %s, "
659 "function %s(): %s", p_ext->psz_name, psz_function,
660 lua_tostring( L, lua_gettop( L ) ) );
669 static inline int TriggerMenu( extension_t *p_ext, int i_id )
671 return PushCommand( p_ext, CMD_TRIGGERMENU, i_id );
674 int lua_ExtensionTriggerMenu( extensions_manager_t *p_mgr,
675 extension_t *p_ext, int id )
677 int i_ret = VLC_EGENERIC;
678 lua_State *L = GetLuaState( p_mgr, p_ext );
683 luaopen_dialog( L, p_ext );
685 lua_getglobal( L, "trigger_menu" );
686 if( !lua_isfunction( L, -1 ) )
688 msg_Warn( p_mgr, "Error while runing script %s, "
689 "function trigger_menu() not found", p_ext->psz_name );
693 /* Pass id as unique argument to the function */
694 lua_pushinteger( L, id );
696 if( lua_pcall( L, 1, 1, 0 ) != 0 )
698 msg_Warn( p_mgr, "Error while runing script %s, "
699 "function trigger_menu(): %s", p_ext->psz_name,
700 lua_tostring( L, lua_gettop( L ) ) );
704 if( i_ret < VLC_SUCCESS )
706 msg_Dbg( p_mgr, "Something went wrong in %s (%s:%d)",
707 __func__, __FILE__, __LINE__ );
712 /** Directly trigger an extension, without activating it
713 * This is NOT multithreaded, and this code runs in the UI thread
715 * @param p_ext Extension to trigger
716 * @return Value returned by the lua function "trigger"
718 static int TriggerExtension( extensions_manager_t *p_mgr,
721 int i_ret = lua_ExecuteFunction( p_mgr, p_ext, "trigger" );
723 /* Close lua state for trigger-only extensions */
724 if( p_ext->p_sys->L )
725 lua_close( p_ext->p_sys->L );
726 p_ext->p_sys->L = NULL;
731 /** Retrieve extension associated to the current script
732 * @param L current lua_State
733 * @return Lua userdata "vlc.extension"
735 extension_t *vlclua_extension_get( lua_State *L )
737 extension_t *p_ext = NULL;
738 lua_getglobal( L, "vlc" );
739 lua_getfield( L, -1, "extension" );
740 p_ext = (extension_t*) lua_topointer( L, lua_gettop( L ) );
745 /** Deactivate an extension by order from the extension itself
747 * @note This is an asynchronous call. A script calling vlc.deactivate() will
748 * be executed to the end before the last call to deactivate() is done.
750 int vlclua_extension_deactivate( lua_State *L )
752 extension_t *p_ext = vlclua_extension_get( L );
753 int i_ret = Deactivate( p_ext->p_sys->p_mgr, p_ext );
754 return ( i_ret == VLC_SUCCESS ) ? 1 : 0;
757 /** Callback for the variable "dialog-event"
758 * @param p_this Current object owner of the extension and the dialog
759 * @param psz_var "dialog-event"
760 * @param oldval Unused
761 * @param newval Address of the dialog
762 * @param p_data Unused
764 static int vlclua_extension_dialog_callback( vlc_object_t *p_this,
770 /* psz_var == "dialog-event" */
775 extension_dialog_command_t *command = newval.p_address;
776 assert( command != NULL );
777 assert( command->p_dlg != NULL);
779 extension_t *p_ext = command->p_dlg->p_sys;
780 assert( p_ext != NULL );
782 extension_widget_t *p_widget = command->p_data;
784 switch( command->event )
786 case EXTENSION_EVENT_CLICK:
787 assert( p_widget != NULL );
788 PushCommand( p_ext, CMD_CLICK, p_widget );
790 case EXTENSION_EVENT_CLOSE:
791 PushCommand( p_ext, CMD_CLOSE );
794 msg_Dbg( p_this, "Received unknown UI event %d, discarded",
802 /* Lock this extension. Can fail. */
803 bool LockExtension( extension_t *p_ext )
805 if( p_ext->p_sys->b_exiting )
808 vlc_mutex_lock( &p_ext->p_sys->running_lock );
809 if( p_ext->p_sys->b_exiting )
811 vlc_mutex_unlock( &p_ext->p_sys->running_lock );
818 void UnlockExtension( extension_t *p_ext )
820 vlc_mutex_unlock( &p_ext->p_sys->running_lock );