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 vlc_mutex_destroy( &p_ext->p_sys->command_lock );
393 vlc_mutex_destroy( &p_ext->p_sys->running_lock );
394 vlc_cond_destroy( &p_ext->p_sys->wait );
395 free( p_ext->p_sys );
400 /* Add the extension to the list of known extensions */
401 ARRAY_APPEND( p_mgr->extensions, p_ext );
404 vlc_mutex_unlock( &p_mgr->lock );
405 /* Continue batch execution */
406 return pb_continue ? ( (* (bool*)pb_continue) ? -1 : 0 ) : -1;
409 static int Control( extensions_manager_t *p_mgr, int i_control, va_list args )
411 extension_t *p_ext = NULL;
413 uint16_t **ppus = NULL;
414 char ***pppsz = NULL;
419 case EXTENSION_ACTIVATE:
420 p_ext = ( extension_t* ) va_arg( args, extension_t* );
421 return Activate( p_mgr, p_ext );
423 case EXTENSION_DEACTIVATE:
424 p_ext = ( extension_t* ) va_arg( args, extension_t* );
425 return Deactivate( p_mgr, p_ext );
427 case EXTENSION_IS_ACTIVATED:
428 p_ext = ( extension_t* ) va_arg( args, extension_t* );
429 pb = ( bool* ) va_arg( args, bool* );
430 *pb = IsActivated( p_mgr, p_ext );
433 case EXTENSION_HAS_MENU:
434 p_ext = ( extension_t* ) va_arg( args, extension_t* );
435 pb = ( bool* ) va_arg( args, bool* );
436 *pb = ( p_ext->p_sys->i_capabilities & EXT_HAS_MENU ) ? 1 : 0;
439 case EXTENSION_GET_MENU:
440 p_ext = ( extension_t* ) va_arg( args, extension_t* );
441 pppsz = ( char*** ) va_arg( args, char*** );
442 ppus = ( uint16_t** ) va_arg( args, uint16_t** );
443 return GetMenuEntries( p_mgr, p_ext, pppsz, ppus );
445 case EXTENSION_TRIGGER_ONLY:
446 p_ext = ( extension_t* ) va_arg( args, extension_t* );
447 pb = ( bool* ) va_arg( args, bool* );
448 *pb = ( p_ext->p_sys->i_capabilities & EXT_TRIGGER_ONLY ) ? 1 : 0;
451 case EXTENSION_TRIGGER:
452 p_ext = ( extension_t* ) va_arg( args, extension_t* );
453 return TriggerExtension( p_mgr, p_ext );
455 case EXTENSION_TRIGGER_MENU:
456 p_ext = ( extension_t* ) va_arg( args, extension_t* );
457 // GCC: 'uint16_t' is promoted to 'int' when passed through '...'
458 i = ( int ) va_arg( args, int );
459 return TriggerMenu( p_ext, i );
462 msg_Err( p_mgr, "Control '%d' not yet implemented in Extension",
470 int lua_ExtensionActivate( extensions_manager_t *p_mgr, extension_t *p_ext )
472 assert( p_mgr != NULL && p_ext != NULL );
473 return lua_ExecuteFunction( p_mgr, p_ext, "activate" );
476 int lua_ExtensionDeactivate( extensions_manager_t *p_mgr, extension_t *p_ext )
478 assert( p_mgr != NULL && p_ext != NULL );
480 if( !p_ext->p_sys->L )
483 int i_ret = lua_ExecuteFunction( p_mgr, p_ext, "deactivate" );
485 /* Clear Lua State */
486 lua_close( p_ext->p_sys->L );
487 p_ext->p_sys->L = NULL;
492 int lua_ExtensionWidgetClick( extensions_manager_t *p_mgr,
494 extension_widget_t *p_widget )
496 if( !p_ext->p_sys->L )
499 return lua_ExecuteFunction( p_mgr, p_ext, (const char*) p_widget->p_sys );
504 * Get the list of menu entries from an extension script
507 * @param pppsz_titles Pointer to NULL. All strings must be freed by the caller
508 * @param ppi_ids Pointer to NULL. Must be feed by the caller.
509 * @note This function is allowed to run in the UI thread. This means
510 * that it MUST respond very fast.
511 * @todo Remove the menu() hook and provide a new function vlc.set_menu()
513 static int GetMenuEntries( extensions_manager_t *p_mgr, extension_t *p_ext,
514 char ***pppsz_titles, uint16_t **ppi_ids )
516 assert( *pppsz_titles == NULL );
517 assert( *ppi_ids == NULL );
519 if( !IsActivated( p_mgr, p_ext ) )
521 msg_Dbg( p_mgr, "Can't get menu before activating the extension!" );
525 if( !LockExtension( p_ext ) )
527 /* Dying extension, fail. */
531 int i_ret = VLC_EGENERIC;
532 lua_State *L = GetLuaState( p_mgr, p_ext );
534 if( ( p_ext->p_sys->i_capabilities & EXT_HAS_MENU ) == 0 )
536 msg_Dbg( p_mgr, "can't get a menu from an extension without menu!" );
540 lua_getglobal( L, "menu" );
542 if( !lua_isfunction( L, -1 ) )
544 msg_Warn( p_mgr, "Error while runing script %s, "
545 "function menu() not found", p_ext->psz_name );
549 if( lua_pcall( L, 0, 1, 0 ) )
551 msg_Warn( p_mgr, "Error while runing script %s, "
552 "function menu(): %s", p_ext->psz_name,
553 lua_tostring( L, lua_gettop( L ) ) );
557 if( lua_gettop( L ) )
559 if( lua_istable( L, -1 ) )
562 size_t i_size = lua_objlen( L, -1 );
563 *pppsz_titles = ( char** ) calloc( i_size+1, sizeof( char* ) );
564 *ppi_ids = ( uint16_t* ) calloc( i_size+1, sizeof( uint16_t ) );
569 while( lua_next( L, -2 ) != 0 )
571 assert( i_idx < i_size );
572 if( (!lua_isstring( L, -1 )) || (!lua_isnumber( L, -2 )) )
574 msg_Warn( p_mgr, "In script %s, an entry in "
575 "the menu table is invalid!", p_ext->psz_name );
578 (*pppsz_titles)[ i_idx ] = strdup( luaL_checkstring( L, -1 ) );
579 (*ppi_ids)[ i_idx ] = (uint16_t) ( luaL_checkinteger( L, -2 ) & 0xFFFF );
586 msg_Warn( p_mgr, "Function menu() in script %s "
587 "did not return a table", p_ext->psz_name );
593 msg_Warn( p_mgr, "Script %s went completely foobar", p_ext->psz_name );
600 UnlockExtension( p_ext );
601 if( i_ret != VLC_SUCCESS )
603 msg_Dbg( p_mgr, "Something went wrong in %s (%s:%d)",
604 __func__, __FILE__, __LINE__ );
609 /* Must be entered with the Lock on Extension */
610 static lua_State* GetLuaState( extensions_manager_t *p_mgr,
622 msg_Err( p_mgr, "Could not create new Lua State" );
626 luaL_register( L, "vlc", p_reg );
629 lua_pushlightuserdata( L, p_mgr );
630 lua_setfield( L, -2, "private" );
632 lua_pushlightuserdata( L, p_ext );
633 lua_setfield( L, -2, "extension" );
637 /* Load more libraries */
640 luaopen_dialog( L, p_ext );
647 luaopen_playlist( L );
650 luaopen_strings( L );
651 luaopen_variables( L );
656 /* Register extension specific functions */
657 lua_getglobal( L, "vlc" );
658 lua_pushcfunction( L, vlclua_extension_deactivate );
659 lua_setfield( L, -2, "deactivate" );
661 /* Load and run the script(s) */
662 if( luaL_dofile( L, p_ext->psz_name ) != 0 )
664 msg_Warn( p_mgr, "Error loading script %s: %s", p_ext->psz_name,
665 lua_tostring( L, lua_gettop( L ) ) );
676 msg_Dbg( p_mgr, "Reusing old Lua state for extension '%s'",
685 * Execute a function in a Lua script
686 * @return < 0 in case of failure, >= 0 in case of success
687 * @note It's better to call this function from a dedicated thread
688 * (see extension_thread.c)
690 int lua_ExecuteFunction( extensions_manager_t *p_mgr,
692 const char *psz_function )
694 int i_ret = VLC_EGENERIC;
695 assert( p_mgr != NULL );
696 assert( p_ext != NULL );
698 lua_State *L = GetLuaState( p_mgr, p_ext );
699 lua_getglobal( L, psz_function );
701 if( !lua_isfunction( L, -1 ) )
703 msg_Warn( p_mgr, "Error while runing script %s, "
704 "function %s() not found", p_ext->psz_name, psz_function );
708 if( lua_pcall( L, 0, 1, 0 ) )
710 msg_Warn( p_mgr, "Error while runing script %s, "
711 "function %s(): %s", p_ext->psz_name, psz_function,
712 lua_tostring( L, lua_gettop( L ) ) );
721 static inline int TriggerMenu( extension_t *p_ext, int i_id )
723 return PushCommand( p_ext, CMD_TRIGGERMENU, i_id );
726 int lua_ExtensionTriggerMenu( extensions_manager_t *p_mgr,
727 extension_t *p_ext, int id )
729 int i_ret = VLC_EGENERIC;
730 lua_State *L = GetLuaState( p_mgr, p_ext );
735 luaopen_dialog( L, p_ext );
737 lua_getglobal( L, "trigger_menu" );
738 if( !lua_isfunction( L, -1 ) )
740 msg_Warn( p_mgr, "Error while runing script %s, "
741 "function trigger_menu() not found", p_ext->psz_name );
745 /* Pass id as unique argument to the function */
746 lua_pushinteger( L, id );
748 if( lua_pcall( L, 1, 1, 0 ) != 0 )
750 msg_Warn( p_mgr, "Error while runing script %s, "
751 "function trigger_menu(): %s", p_ext->psz_name,
752 lua_tostring( L, lua_gettop( L ) ) );
756 if( i_ret < VLC_SUCCESS )
758 msg_Dbg( p_mgr, "Something went wrong in %s (%s:%d)",
759 __func__, __FILE__, __LINE__ );
764 /** Directly trigger an extension, without activating it
765 * This is NOT multithreaded, and this code runs in the UI thread
767 * @param p_ext Extension to trigger
768 * @return Value returned by the lua function "trigger"
770 static int TriggerExtension( extensions_manager_t *p_mgr,
773 int i_ret = lua_ExecuteFunction( p_mgr, p_ext, "trigger" );
775 /* Close lua state for trigger-only extensions */
776 if( p_ext->p_sys->L )
777 lua_close( p_ext->p_sys->L );
778 p_ext->p_sys->L = NULL;
783 /** Retrieve extension associated to the current script
784 * @param L current lua_State
785 * @return Lua userdata "vlc.extension"
787 extension_t *vlclua_extension_get( lua_State *L )
789 extension_t *p_ext = NULL;
790 lua_getglobal( L, "vlc" );
791 lua_getfield( L, -1, "extension" );
792 p_ext = (extension_t*) lua_topointer( L, lua_gettop( L ) );
797 /** Deactivate an extension by order from the extension itself
799 * @note This is an asynchronous call. A script calling vlc.deactivate() will
800 * be executed to the end before the last call to deactivate() is done.
802 int vlclua_extension_deactivate( lua_State *L )
804 extension_t *p_ext = vlclua_extension_get( L );
805 int i_ret = Deactivate( p_ext->p_sys->p_mgr, p_ext );
806 return ( i_ret == VLC_SUCCESS ) ? 1 : 0;
809 /** Callback for the variable "dialog-event"
810 * @param p_this Current object owner of the extension and the dialog
811 * @param psz_var "dialog-event"
812 * @param oldval Unused
813 * @param newval Address of the dialog
814 * @param p_data Unused
816 static int vlclua_extension_dialog_callback( vlc_object_t *p_this,
822 /* psz_var == "dialog-event" */
827 extension_dialog_command_t *command = newval.p_address;
828 assert( command != NULL );
829 assert( command->p_dlg != NULL);
831 extension_t *p_ext = command->p_dlg->p_sys;
832 assert( p_ext != NULL );
834 extension_widget_t *p_widget = command->p_data;
836 switch( command->event )
838 case EXTENSION_EVENT_CLICK:
839 assert( p_widget != NULL );
840 PushCommand( p_ext, CMD_CLICK, p_widget );
842 case EXTENSION_EVENT_CLOSE:
843 PushCommand( p_ext, CMD_CLOSE );
846 msg_Dbg( p_this, "Received unknown UI event %d, discarded",
854 /* Lock this extension. Can fail. */
855 bool LockExtension( extension_t *p_ext )
857 if( p_ext->p_sys->b_exiting )
860 vlc_mutex_lock( &p_ext->p_sys->running_lock );
861 if( p_ext->p_sys->b_exiting )
863 vlc_mutex_unlock( &p_ext->p_sys->running_lock );
870 void UnlockExtension( extension_t *p_ext )
872 vlc_mutex_unlock( &p_ext->p_sys->running_lock );