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 #include <vlc_input.h>
31 /* Functions to register */
32 static const luaL_Reg p_reg[] =
38 * Extensions capabilities
39 * Note: #define and ppsz_capabilities must be in sync
41 #define EXT_HAS_MENU (1 << 0)
42 #define EXT_TRIGGER_ONLY (1 << 1)
44 const char* const ppsz_capabilities[] = {
50 static int ScanExtensions( extensions_manager_t *p_this );
51 static int ScanLuaCallback( vlc_object_t *p_this, const char *psz_script,
52 lua_State *L, void *pb_continue );
53 static int Control( extensions_manager_t *, int, va_list );
54 static int GetMenuEntries( extensions_manager_t *p_mgr, extension_t *p_ext,
55 char ***pppsz_titles, uint16_t **ppi_ids );
56 static lua_State* GetLuaState( extensions_manager_t *p_mgr,
58 static int TriggerMenu( extension_t *p_ext, int id );
59 static int TriggerExtension( extensions_manager_t *p_mgr,
62 int vlclua_extension_deactivate( lua_State *L );
65 static int vlclua_extension_dialog_callback( vlc_object_t *p_this,
75 int Open_Extension( vlc_object_t *p_this )
77 msg_Dbg( p_this, "Opening EXPERIMENTAL Lua Extension module" );
79 extensions_manager_t *p_mgr = ( extensions_manager_t* ) p_this;
81 p_mgr->pf_control = Control;
83 extensions_manager_sys_t *p_sys = ( extensions_manager_sys_t* )
84 calloc( 1, sizeof( extensions_manager_sys_t ) );
85 if( !p_sys ) return VLC_ENOMEM;
88 ARRAY_INIT( p_sys->activated_extensions );
89 ARRAY_INIT( p_mgr->extensions );
90 vlc_mutex_init( &p_mgr->lock );
91 vlc_mutex_init( &p_mgr->p_sys->lock );
93 /* Initialise Lua state structure */
94 lua_State *L = GetLuaState( p_mgr, NULL );
102 /* Scan available Lua Extensions */
103 if( ScanExtensions( p_mgr ) != VLC_SUCCESS )
105 msg_Err( p_mgr, "Can't load extensions modules" );
112 // Create the dialog-event variable
113 var_Create( p_this, "dialog-event", VLC_VAR_ADDRESS );
114 var_AddCallback( p_this, "dialog-event",
115 vlclua_extension_dialog_callback, NULL );
121 * Module unload function
123 void Close_Extension( vlc_object_t *p_this )
125 extensions_manager_t *p_mgr = ( extensions_manager_t* ) p_this;
126 msg_Dbg( p_mgr, "Deactivating all loaded extensions" );
128 vlc_mutex_lock( &p_mgr->lock );
129 p_mgr->p_sys->b_killed = true;
130 vlc_mutex_unlock( &p_mgr->lock );
132 var_Destroy( p_mgr, "dialog-event" );
134 extension_t *p_ext = NULL;
135 FOREACH_ARRAY( p_ext, p_mgr->p_sys->activated_extensions )
138 Deactivate( p_mgr, p_ext );
139 WaitForDeactivation( p_ext );
143 msg_Dbg( p_mgr, "All extensions are now deactivated" );
144 ARRAY_RESET( p_mgr->p_sys->activated_extensions );
146 if( p_mgr->p_sys && p_mgr->p_sys->L )
147 lua_close( p_mgr->p_sys->L );
149 vlc_mutex_destroy( &p_mgr->lock );
150 vlc_mutex_destroy( &p_mgr->p_sys->lock );
151 free( p_mgr->p_sys );
154 /* Free extensions' memory */
155 FOREACH_ARRAY( p_ext, p_mgr->extensions )
159 if( p_ext->p_sys->L )
160 lua_close( p_ext->p_sys->L );
161 free( p_ext->psz_name );
162 free( p_ext->psz_title );
164 vlc_mutex_destroy( &p_ext->p_sys->running_lock );
165 vlc_mutex_destroy( &p_ext->p_sys->command_lock );
166 vlc_cond_destroy( &p_ext->p_sys->wait );
168 free( p_ext->p_sys );
173 ARRAY_RESET( p_mgr->extensions );
177 * Batch scan all Lua files in folder "extensions"
178 * @param p_mgr This extensions_manager_t object
180 static int ScanExtensions( extensions_manager_t *p_mgr )
184 vlclua_scripts_batch_execute( VLC_OBJECT( p_mgr ),
187 p_mgr->p_sys->L, &b_true );
196 * Batch scan all Lua files in folder "extensions": callback
197 * @param p_this This extensions_manager_t object
198 * @param psz_script Name of the script to run
199 * @param L Lua State, common to all scripts here
200 * @param pb_continue bool* that indicates whether to continue batch or not
202 int ScanLuaCallback( vlc_object_t *p_this, const char *psz_script,
203 lua_State *L, void *pb_continue )
205 extensions_manager_t *p_mgr = ( extensions_manager_t* ) p_this;
208 msg_Dbg( p_mgr, "Scanning Lua script %s", psz_script );
210 vlc_mutex_lock( &p_mgr->lock );
212 /* Create new script descriptor */
213 extension_t *p_ext = ( extension_t* ) calloc( 1, sizeof( extension_t ) );
216 vlc_mutex_unlock( &p_mgr->lock );
220 p_ext->psz_name = strdup( psz_script );
221 p_ext->p_sys = (extension_sys_t*) calloc( 1, sizeof( extension_sys_t ) );
222 if( !p_ext->p_sys || !p_ext->psz_name )
224 free( p_ext->psz_name );
225 free( p_ext->p_sys );
227 vlc_mutex_unlock( &p_mgr->lock );
230 p_ext->p_sys->p_mgr = p_mgr;
232 /* Mutexes and conditions */
233 vlc_mutex_init( &p_ext->p_sys->command_lock );
234 vlc_mutex_init( &p_ext->p_sys->running_lock );
235 vlc_cond_init( &p_ext->p_sys->wait );
237 /* Load and run the script(s) */
238 if( luaL_dofile( L, psz_script ) )
240 msg_Warn( p_mgr, "Error loading script %s: %s", psz_script,
241 lua_tostring( L, lua_gettop( L ) ) );
246 /* Scan script for capabilities */
247 lua_getglobal( L, "descriptor" );
249 if( !lua_isfunction( L, -1 ) )
251 msg_Warn( p_mgr, "Error while runing script %s, "
252 "function descriptor() not found", psz_script );
256 if( lua_pcall( L, 0, 1, 0 ) )
258 msg_Warn( p_mgr, "Error while runing script %s, "
259 "function descriptor(): %s", psz_script,
260 lua_tostring( L, lua_gettop( L ) ) );
264 if( lua_gettop( L ) )
266 if( lua_istable( L, -1 ) )
269 lua_getfield( L, -1, "capabilities" );
270 if( lua_istable( L, -1 ) )
273 while( lua_next( L, -2 ) != 0 )
275 /* Key is at index -2 and value at index -1. Discard key */
276 const char *psz_cap = luaL_checkstring( L, -1 );
279 /* Find this capability's flag */
280 for( const char *iter = *ppsz_capabilities;
282 iter = ppsz_capabilities[ ++i_cap ])
284 if( !strcmp( iter, psz_cap ) )
287 p_ext->p_sys->i_capabilities |= 1 << i_cap;
294 msg_Warn( p_mgr, "Extension capability '%s' unknown in"
295 " script %s", psz_cap, psz_script );
297 /* Removes 'value'; keeps 'key' for next iteration */
303 msg_Warn( p_mgr, "In script %s, function descriptor() "
304 "did not return a table of capabilities.",
310 lua_getfield( L, -1, "title" );
311 if( lua_isstring( L, -1 ) )
313 p_ext->psz_title = strdup( luaL_checkstring( L, -1 ) );
317 msg_Dbg( p_mgr, "In script %s, function descriptor() "
318 "did not return a string as title.",
320 p_ext->psz_title = strdup( psz_script );
325 lua_getfield( L, -1, "author" );
326 if( lua_isstring( L, -1 ) )
328 p_ext->psz_author = strdup( luaL_checkstring( L, -1 ) );
332 p_ext->psz_author = NULL;
336 /* Get description */
337 lua_getfield( L, -1, "description" );
338 if( lua_isstring( L, -1 ) )
340 p_ext->psz_description = strdup( luaL_checkstring( L, -1 ) );
344 p_ext->psz_description = NULL;
349 lua_getfield( L, -1, "url" );
350 if( lua_isstring( L, -1 ) )
352 p_ext->psz_url = strdup( luaL_checkstring( L, -1 ) );
356 p_ext->psz_url = NULL;
361 lua_getfield( L, -1, "version" );
362 if( lua_isstring( L, -1 ) )
364 p_ext->psz_version = strdup( luaL_checkstring( L, -1 ) );
368 p_ext->psz_version = NULL;
374 msg_Warn( p_mgr, "In script %s, function descriptor() "
375 "did not return a table!", psz_script );
381 msg_Err( p_mgr, "Script %s went completely foobar", psz_script );
385 msg_Dbg( p_mgr, "Script %s has the following capability flags: 0x%x",
386 psz_script, p_ext->p_sys->i_capabilities );
392 free( p_ext->psz_name );
393 free( p_ext->psz_title );
394 free( p_ext->psz_url );
395 free( p_ext->psz_author );
396 free( p_ext->psz_description );
397 free( p_ext->psz_version );
398 vlc_mutex_destroy( &p_ext->p_sys->command_lock );
399 vlc_mutex_destroy( &p_ext->p_sys->running_lock );
400 vlc_cond_destroy( &p_ext->p_sys->wait );
401 free( p_ext->p_sys );
406 /* Add the extension to the list of known extensions */
407 ARRAY_APPEND( p_mgr->extensions, p_ext );
410 vlc_mutex_unlock( &p_mgr->lock );
411 /* Continue batch execution */
412 return pb_continue ? ( (* (bool*)pb_continue) ? -1 : 0 ) : -1;
415 static int Control( extensions_manager_t *p_mgr, int i_control, va_list args )
417 extension_t *p_ext = NULL;
419 uint16_t **ppus = NULL;
420 char ***pppsz = NULL;
425 case EXTENSION_ACTIVATE:
426 p_ext = ( extension_t* ) va_arg( args, extension_t* );
427 return Activate( p_mgr, p_ext );
429 case EXTENSION_DEACTIVATE:
430 p_ext = ( extension_t* ) va_arg( args, extension_t* );
431 return Deactivate( p_mgr, p_ext );
433 case EXTENSION_IS_ACTIVATED:
434 p_ext = ( extension_t* ) va_arg( args, extension_t* );
435 pb = ( bool* ) va_arg( args, bool* );
436 *pb = IsActivated( p_mgr, p_ext );
439 case EXTENSION_HAS_MENU:
440 p_ext = ( extension_t* ) va_arg( args, extension_t* );
441 pb = ( bool* ) va_arg( args, bool* );
442 *pb = ( p_ext->p_sys->i_capabilities & EXT_HAS_MENU ) ? 1 : 0;
445 case EXTENSION_GET_MENU:
446 p_ext = ( extension_t* ) va_arg( args, extension_t* );
447 pppsz = ( char*** ) va_arg( args, char*** );
448 ppus = ( uint16_t** ) va_arg( args, uint16_t** );
449 return GetMenuEntries( p_mgr, p_ext, pppsz, ppus );
451 case EXTENSION_TRIGGER_ONLY:
452 p_ext = ( extension_t* ) va_arg( args, extension_t* );
453 pb = ( bool* ) va_arg( args, bool* );
454 *pb = ( p_ext->p_sys->i_capabilities & EXT_TRIGGER_ONLY ) ? 1 : 0;
457 case EXTENSION_TRIGGER:
458 p_ext = ( extension_t* ) va_arg( args, extension_t* );
459 return TriggerExtension( p_mgr, p_ext );
461 case EXTENSION_TRIGGER_MENU:
462 p_ext = ( extension_t* ) va_arg( args, extension_t* );
463 // GCC: 'uint16_t' is promoted to 'int' when passed through '...'
464 i = ( int ) va_arg( args, int );
465 return TriggerMenu( p_ext, i );
467 case EXTENSION_SET_INPUT:
469 p_ext = ( extension_t* ) va_arg( args, extension_t* );
470 input_thread_t *p_input = va_arg( args, struct input_thread_t * );
472 bool ok = LockExtension(p_ext);
475 input_thread_t *old = p_ext->p_sys->p_input;
477 vlc_object_release(old);
478 p_ext->p_sys->p_input = p_input ? vlc_object_hold(p_input) : p_input;
479 UnlockExtension(p_ext);
484 msg_Err( p_mgr, "Control '%d' not yet implemented in Extension",
492 int lua_ExtensionActivate( extensions_manager_t *p_mgr, extension_t *p_ext )
494 assert( p_mgr != NULL && p_ext != NULL );
495 return lua_ExecuteFunction( p_mgr, p_ext, "activate" );
498 int lua_ExtensionDeactivate( extensions_manager_t *p_mgr, extension_t *p_ext )
500 assert( p_mgr != NULL && p_ext != NULL );
502 if( !p_ext->p_sys->L )
505 int i_ret = lua_ExecuteFunction( p_mgr, p_ext, "deactivate" );
507 /* Clear Lua State */
508 lua_close( p_ext->p_sys->L );
509 p_ext->p_sys->L = NULL;
514 int lua_ExtensionWidgetClick( extensions_manager_t *p_mgr,
516 extension_widget_t *p_widget )
518 if( !p_ext->p_sys->L )
521 return lua_ExecuteFunction( p_mgr, p_ext, (const char*) p_widget->p_sys );
526 * Get the list of menu entries from an extension script
529 * @param pppsz_titles Pointer to NULL. All strings must be freed by the caller
530 * @param ppi_ids Pointer to NULL. Must be feed by the caller.
531 * @note This function is allowed to run in the UI thread. This means
532 * that it MUST respond very fast.
533 * @todo Remove the menu() hook and provide a new function vlc.set_menu()
535 static int GetMenuEntries( extensions_manager_t *p_mgr, extension_t *p_ext,
536 char ***pppsz_titles, uint16_t **ppi_ids )
538 assert( *pppsz_titles == NULL );
539 assert( *ppi_ids == NULL );
541 if( !IsActivated( p_mgr, p_ext ) )
543 msg_Dbg( p_mgr, "Can't get menu before activating the extension!" );
547 if( !LockExtension( p_ext ) )
549 /* Dying extension, fail. */
553 int i_ret = VLC_EGENERIC;
554 lua_State *L = GetLuaState( p_mgr, p_ext );
556 if( ( p_ext->p_sys->i_capabilities & EXT_HAS_MENU ) == 0 )
558 msg_Dbg( p_mgr, "can't get a menu from an extension without menu!" );
562 lua_getglobal( L, "menu" );
564 if( !lua_isfunction( L, -1 ) )
566 msg_Warn( p_mgr, "Error while runing script %s, "
567 "function menu() not found", p_ext->psz_name );
571 if( lua_pcall( L, 0, 1, 0 ) )
573 msg_Warn( p_mgr, "Error while runing script %s, "
574 "function menu(): %s", p_ext->psz_name,
575 lua_tostring( L, lua_gettop( L ) ) );
579 if( lua_gettop( L ) )
581 if( lua_istable( L, -1 ) )
584 size_t i_size = lua_objlen( L, -1 );
585 *pppsz_titles = ( char** ) calloc( i_size+1, sizeof( char* ) );
586 *ppi_ids = ( uint16_t* ) calloc( i_size+1, sizeof( uint16_t ) );
591 while( lua_next( L, -2 ) != 0 )
593 assert( i_idx < i_size );
594 if( (!lua_isstring( L, -1 )) || (!lua_isnumber( L, -2 )) )
596 msg_Warn( p_mgr, "In script %s, an entry in "
597 "the menu table is invalid!", p_ext->psz_name );
600 (*pppsz_titles)[ i_idx ] = strdup( luaL_checkstring( L, -1 ) );
601 (*ppi_ids)[ i_idx ] = (uint16_t) ( luaL_checkinteger( L, -2 ) & 0xFFFF );
608 msg_Warn( p_mgr, "Function menu() in script %s "
609 "did not return a table", p_ext->psz_name );
615 msg_Warn( p_mgr, "Script %s went completely foobar", p_ext->psz_name );
622 UnlockExtension( p_ext );
623 if( i_ret != VLC_SUCCESS )
625 msg_Dbg( p_mgr, "Something went wrong in %s (%s:%d)",
626 __func__, __FILE__, __LINE__ );
631 /* Must be entered with the Lock on Extension */
632 static lua_State* GetLuaState( extensions_manager_t *p_mgr,
644 msg_Err( p_mgr, "Could not create new Lua State" );
648 luaL_register( L, "vlc", p_reg );
651 lua_pushlightuserdata( L, p_mgr );
652 lua_setfield( L, -2, "private" );
654 lua_pushlightuserdata( L, p_ext );
655 lua_setfield( L, -2, "extension" );
659 /* Load more libraries */
662 luaopen_dialog( L, p_ext );
669 luaopen_playlist( L );
672 luaopen_strings( L );
673 luaopen_variables( L );
678 /* Register extension specific functions */
679 lua_getglobal( L, "vlc" );
680 lua_pushcfunction( L, vlclua_extension_deactivate );
681 lua_setfield( L, -2, "deactivate" );
683 /* Load and run the script(s) */
684 if( luaL_dofile( L, p_ext->psz_name ) != 0 )
686 msg_Warn( p_mgr, "Error loading script %s: %s", p_ext->psz_name,
687 lua_tostring( L, lua_gettop( L ) ) );
698 msg_Dbg( p_mgr, "Reusing old Lua state for extension '%s'",
707 * Execute a function in a Lua script
708 * @return < 0 in case of failure, >= 0 in case of success
709 * @note It's better to call this function from a dedicated thread
710 * (see extension_thread.c)
712 int lua_ExecuteFunction( extensions_manager_t *p_mgr,
714 const char *psz_function )
716 int i_ret = VLC_EGENERIC;
717 assert( p_mgr != NULL );
718 assert( p_ext != NULL );
720 lua_State *L = GetLuaState( p_mgr, p_ext );
721 lua_getglobal( L, psz_function );
723 if( !lua_isfunction( L, -1 ) )
725 msg_Warn( p_mgr, "Error while runing script %s, "
726 "function %s() not found", p_ext->psz_name, psz_function );
730 if( lua_pcall( L, 0, 1, 0 ) )
732 msg_Warn( p_mgr, "Error while runing script %s, "
733 "function %s(): %s", p_ext->psz_name, psz_function,
734 lua_tostring( L, lua_gettop( L ) ) );
743 static inline int TriggerMenu( extension_t *p_ext, int i_id )
745 return PushCommand( p_ext, CMD_TRIGGERMENU, i_id );
748 int lua_ExtensionTriggerMenu( extensions_manager_t *p_mgr,
749 extension_t *p_ext, int id )
751 int i_ret = VLC_EGENERIC;
752 lua_State *L = GetLuaState( p_mgr, p_ext );
757 luaopen_dialog( L, p_ext );
759 lua_getglobal( L, "trigger_menu" );
760 if( !lua_isfunction( L, -1 ) )
762 msg_Warn( p_mgr, "Error while runing script %s, "
763 "function trigger_menu() not found", p_ext->psz_name );
767 /* Pass id as unique argument to the function */
768 lua_pushinteger( L, id );
770 if( lua_pcall( L, 1, 1, 0 ) != 0 )
772 msg_Warn( p_mgr, "Error while runing script %s, "
773 "function trigger_menu(): %s", p_ext->psz_name,
774 lua_tostring( L, lua_gettop( L ) ) );
778 if( i_ret < VLC_SUCCESS )
780 msg_Dbg( p_mgr, "Something went wrong in %s (%s:%d)",
781 __func__, __FILE__, __LINE__ );
786 /** Directly trigger an extension, without activating it
787 * This is NOT multithreaded, and this code runs in the UI thread
789 * @param p_ext Extension to trigger
790 * @return Value returned by the lua function "trigger"
792 static int TriggerExtension( extensions_manager_t *p_mgr,
795 int i_ret = lua_ExecuteFunction( p_mgr, p_ext, "trigger" );
797 /* Close lua state for trigger-only extensions */
798 if( p_ext->p_sys->L )
799 lua_close( p_ext->p_sys->L );
800 p_ext->p_sys->L = NULL;
805 /** Retrieve extension associated to the current script
806 * @param L current lua_State
807 * @return Lua userdata "vlc.extension"
809 extension_t *vlclua_extension_get( lua_State *L )
811 extension_t *p_ext = NULL;
812 lua_getglobal( L, "vlc" );
813 lua_getfield( L, -1, "extension" );
814 p_ext = (extension_t*) lua_topointer( L, lua_gettop( L ) );
819 /** Deactivate an extension by order from the extension itself
821 * @note This is an asynchronous call. A script calling vlc.deactivate() will
822 * be executed to the end before the last call to deactivate() is done.
824 int vlclua_extension_deactivate( lua_State *L )
826 extension_t *p_ext = vlclua_extension_get( L );
827 int i_ret = Deactivate( p_ext->p_sys->p_mgr, p_ext );
828 return ( i_ret == VLC_SUCCESS ) ? 1 : 0;
831 /** Callback for the variable "dialog-event"
832 * @param p_this Current object owner of the extension and the dialog
833 * @param psz_var "dialog-event"
834 * @param oldval Unused
835 * @param newval Address of the dialog
836 * @param p_data Unused
838 static int vlclua_extension_dialog_callback( vlc_object_t *p_this,
844 /* psz_var == "dialog-event" */
849 extension_dialog_command_t *command = newval.p_address;
850 assert( command != NULL );
851 assert( command->p_dlg != NULL);
853 extension_t *p_ext = command->p_dlg->p_sys;
854 assert( p_ext != NULL );
856 extension_widget_t *p_widget = command->p_data;
858 switch( command->event )
860 case EXTENSION_EVENT_CLICK:
861 assert( p_widget != NULL );
862 PushCommand( p_ext, CMD_CLICK, p_widget );
864 case EXTENSION_EVENT_CLOSE:
865 PushCommand( p_ext, CMD_CLOSE );
868 msg_Dbg( p_this, "Received unknown UI event %d, discarded",
876 /* Lock this extension. Can fail. */
877 bool LockExtension( extension_t *p_ext )
879 if( p_ext->p_sys->b_exiting )
882 vlc_mutex_lock( &p_ext->p_sys->running_lock );
883 if( p_ext->p_sys->b_exiting )
885 vlc_mutex_unlock( &p_ext->p_sys->running_lock );
892 void UnlockExtension( extension_t *p_ext )
894 vlc_mutex_unlock( &p_ext->p_sys->running_lock );