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 *****************************************************************************/
34 #include "extension.h"
37 #include <vlc_common.h>
38 #include <vlc_input.h>
39 #include <vlc_events.h>
40 #include <vlc_dialog.h>
42 /* Functions to register */
43 static const luaL_Reg p_reg[] =
49 * Extensions capabilities
50 * Note: #define and ppsz_capabilities must be in sync
52 #define EXT_HAS_MENU (1 << 0) ///< Hook: menu
53 #define EXT_TRIGGER_ONLY (1 << 1) ///< Hook: trigger. Not activable
54 #define EXT_INPUT_LISTENER (1 << 2) ///< Hook: input_changed
55 #define EXT_META_LISTENER (1 << 3) ///< Hook: meta_changed
56 #define EXT_PLAYING_LISTENER (1 << 4) ///< Hook: status_changed
58 static const char* const ppsz_capabilities[] = {
67 #define WATCH_TIMER_PERIOD (10 * CLOCK_FREQ) ///< 10s period for the timer
69 static int ScanExtensions( extensions_manager_t *p_this );
70 static int ScanLuaCallback( vlc_object_t *p_this, const char *psz_script,
72 static int Control( extensions_manager_t *, int, va_list );
73 static int GetMenuEntries( extensions_manager_t *p_mgr, extension_t *p_ext,
74 char ***pppsz_titles, uint16_t **ppi_ids );
75 static lua_State* GetLuaState( extensions_manager_t *p_mgr,
77 static int TriggerMenu( extension_t *p_ext, int id );
78 static int TriggerExtension( extensions_manager_t *p_mgr,
80 static void WatchTimerCallback( void* );
82 static int vlclua_extension_deactivate( lua_State *L );
83 static int vlclua_extension_keep_alive( lua_State *L );
86 static int vlclua_extension_dialog_callback( vlc_object_t *p_this,
92 /* Input item callback: vlc_InputItemMetaChanged */
93 static void inputItemMetaChanged( const vlc_event_t *p_event,
100 int Open_Extension( vlc_object_t *p_this )
102 msg_Dbg( p_this, "Opening EXPERIMENTAL Lua Extension module" );
104 extensions_manager_t *p_mgr = ( extensions_manager_t* ) p_this;
106 p_mgr->pf_control = Control;
108 extensions_manager_sys_t *p_sys = ( extensions_manager_sys_t* )
109 calloc( 1, sizeof( extensions_manager_sys_t ) );
110 if( !p_sys ) return VLC_ENOMEM;
112 p_mgr->p_sys = p_sys;
113 ARRAY_INIT( p_sys->activated_extensions );
114 ARRAY_INIT( p_mgr->extensions );
115 vlc_mutex_init( &p_mgr->lock );
116 vlc_mutex_init( &p_mgr->p_sys->lock );
118 /* Scan available Lua Extensions */
119 if( ScanExtensions( p_mgr ) != VLC_SUCCESS )
121 msg_Err( p_mgr, "Can't load extensions modules" );
125 // Create the dialog-event variable
126 var_Create( p_this, "dialog-event", VLC_VAR_ADDRESS );
127 var_AddCallback( p_this, "dialog-event",
128 vlclua_extension_dialog_callback, NULL );
134 * Module unload function
136 void Close_Extension( vlc_object_t *p_this )
138 extensions_manager_t *p_mgr = ( extensions_manager_t* ) p_this;
139 msg_Dbg( p_mgr, "Deactivating all loaded extensions" );
141 vlc_mutex_lock( &p_mgr->lock );
142 p_mgr->p_sys->b_killed = true;
143 vlc_mutex_unlock( &p_mgr->lock );
145 var_Destroy( p_mgr, "dialog-event" );
147 extension_t *p_ext = NULL;
148 FOREACH_ARRAY( p_ext, p_mgr->p_sys->activated_extensions )
151 msg_Dbg( p_mgr, "Deactivating '%s'", p_ext->psz_title );
152 Deactivate( p_mgr, p_ext );
153 vlc_join( p_ext->p_sys->thread, NULL );
157 msg_Dbg( p_mgr, "All extensions are now deactivated" );
158 ARRAY_RESET( p_mgr->p_sys->activated_extensions );
160 vlc_mutex_destroy( &p_mgr->lock );
161 vlc_mutex_destroy( &p_mgr->p_sys->lock );
162 free( p_mgr->p_sys );
165 /* Free extensions' memory */
166 FOREACH_ARRAY( p_ext, p_mgr->extensions )
170 if( p_ext->p_sys->L )
171 lua_close( p_ext->p_sys->L );
172 free( p_ext->psz_name );
173 free( p_ext->psz_title );
174 free( p_ext->psz_author );
175 free( p_ext->psz_description );
176 free( p_ext->psz_shortdescription );
177 free( p_ext->psz_url );
178 free( p_ext->psz_version );
179 free( p_ext->p_icondata );
181 vlc_mutex_destroy( &p_ext->p_sys->running_lock );
182 vlc_mutex_destroy( &p_ext->p_sys->command_lock );
183 vlc_cond_destroy( &p_ext->p_sys->wait );
184 vlc_timer_destroy( p_ext->p_sys->timer );
186 free( p_ext->p_sys );
191 ARRAY_RESET( p_mgr->extensions );
195 * Batch scan all Lua files in folder "extensions"
196 * @param p_mgr This extensions_manager_t object
198 static int ScanExtensions( extensions_manager_t *p_mgr )
201 vlclua_scripts_batch_execute( VLC_OBJECT( p_mgr ),
213 * Dummy Lua function: does nothing
214 * @note This function can be used to replace "require" while scanning for
216 * Even the built-in libraries are not loaded when calling descriptor()
218 static int vlclua_dummy_require( lua_State *L )
225 * Replacement for "require", adding support for packaged extensions
226 * @note Loads modules in the modules/ folder of a package
227 * @note Try first with .luac and then with .lua
229 static int vlclua_extension_require( lua_State *L )
231 const char *psz_module = luaL_checkstring( L, 1 );
232 vlc_object_t *p_this = vlclua_get_this( L );
233 extension_t *p_ext = vlclua_extension_get( L );
234 msg_Dbg( p_this, "loading module '%s' from extension package",
236 char *psz_fullpath, *psz_package, *sep;
237 psz_package = strdup( p_ext->psz_name );
238 sep = strrchr( psz_package, '/' );
242 return luaL_error( L, "could not find package name" );
245 if( -1 == asprintf( &psz_fullpath,
246 "%s/modules/%s.luac", psz_package, psz_module ) )
251 int i_ret = vlclua_dofile( p_this, L, psz_fullpath );
254 // Remove trailing 'c' --> try with .lua script
255 psz_fullpath[ strlen( psz_fullpath ) - 1 ] = '\0';
256 i_ret = vlclua_dofile( p_this, L, psz_fullpath );
258 free( psz_fullpath );
262 return luaL_error( L, "unable to load module '%s' from package",
269 * Batch scan all Lua files in folder "extensions": callback
270 * @param p_this This extensions_manager_t object
271 * @param psz_filename Name of the script to run
272 * @param L Lua State, common to all scripts here
273 * @param dummy: unused
275 int ScanLuaCallback( vlc_object_t *p_this, const char *psz_filename,
279 extensions_manager_t *p_mgr = ( extensions_manager_t* ) p_this;
282 msg_Dbg( p_mgr, "Scanning Lua script %s", psz_filename );
284 /* Experimental: read .vle packages (Zip archives) */
285 char *psz_script = NULL;
286 int i_flen = strlen( psz_filename );
287 if( !strncasecmp( psz_filename + i_flen - 4, ".vle", 4 ) )
289 msg_Dbg( p_this, "reading Lua script in a zip archive" );
290 psz_script = calloc( 1, i_flen + 6 + 12 + 1 );
293 strcpy( psz_script, "zip://" );
294 strncat( psz_script, psz_filename, i_flen + 19 );
295 strncat( psz_script, "!/script.lua", i_flen + 19 );
299 psz_script = strdup( psz_filename );
304 /* Create new script descriptor */
305 extension_t *p_ext = ( extension_t* ) calloc( 1, sizeof( extension_t ) );
312 p_ext->psz_name = psz_script;
313 p_ext->p_sys = (extension_sys_t*) calloc( 1, sizeof( extension_sys_t ) );
314 if( !p_ext->p_sys || !p_ext->psz_name )
316 free( p_ext->psz_name );
317 free( p_ext->p_sys );
321 p_ext->p_sys->p_mgr = p_mgr;
324 if( vlc_timer_create( &p_ext->p_sys->timer, WatchTimerCallback, p_ext ) )
326 free( p_ext->psz_name );
327 free( p_ext->p_sys );
332 /* Mutexes and conditions */
333 vlc_mutex_init( &p_ext->p_sys->command_lock );
334 vlc_mutex_init( &p_ext->p_sys->running_lock );
335 vlc_cond_init( &p_ext->p_sys->wait );
337 /* Prepare Lua state */
338 lua_State *L = luaL_newstate();
339 lua_register( L, "require", &vlclua_dummy_require );
342 if( vlclua_dofile( p_this, L, psz_script ) ) // luaL_dofile
344 msg_Warn( p_mgr, "Error loading script %s: %s", psz_script,
345 lua_tostring( L, lua_gettop( L ) ) );
350 /* Scan script for capabilities */
351 lua_getglobal( L, "descriptor" );
353 if( !lua_isfunction( L, -1 ) )
355 msg_Warn( p_mgr, "Error while running script %s, "
356 "function descriptor() not found", psz_script );
360 if( lua_pcall( L, 0, 1, 0 ) )
362 msg_Warn( p_mgr, "Error while running script %s, "
363 "function descriptor(): %s", psz_script,
364 lua_tostring( L, lua_gettop( L ) ) );
368 if( lua_gettop( L ) )
370 if( lua_istable( L, -1 ) )
373 lua_getfield( L, -1, "capabilities" );
374 if( lua_istable( L, -1 ) )
377 while( lua_next( L, -2 ) != 0 )
379 /* Key is at index -2 and value at index -1. Discard key */
380 const char *psz_cap = luaL_checkstring( L, -1 );
383 /* Find this capability's flag */
384 for( const char *iter = *ppsz_capabilities;
386 iter = ppsz_capabilities[ ++i_cap ])
388 if( !strcmp( iter, psz_cap ) )
391 p_ext->p_sys->i_capabilities |= 1 << i_cap;
398 msg_Warn( p_mgr, "Extension capability '%s' unknown in"
399 " script %s", psz_cap, psz_script );
401 /* Removes 'value'; keeps 'key' for next iteration */
407 msg_Warn( p_mgr, "In script %s, function descriptor() "
408 "did not return a table of capabilities.",
414 lua_getfield( L, -1, "title" );
415 if( lua_isstring( L, -1 ) )
417 p_ext->psz_title = strdup( luaL_checkstring( L, -1 ) );
421 msg_Dbg( p_mgr, "In script %s, function descriptor() "
422 "did not return a string as title.",
424 p_ext->psz_title = strdup( psz_script );
429 lua_getfield( L, -1, "author" );
430 p_ext->psz_author = luaL_strdupornull( L, -1 );
433 /* Get description */
434 lua_getfield( L, -1, "description" );
435 p_ext->psz_description = luaL_strdupornull( L, -1 );
438 /* Get short description */
439 lua_getfield( L, -1, "shortdesc" );
440 p_ext->psz_shortdescription = luaL_strdupornull( L, -1 );
444 lua_getfield( L, -1, "url" );
445 p_ext->psz_url = luaL_strdupornull( L, -1 );
449 lua_getfield( L, -1, "version" );
450 p_ext->psz_version = luaL_strdupornull( L, -1 );
454 lua_getfield( L, -1, "icon" );
455 if( !lua_isnil( L, -1 ) && lua_isstring( L, -1 ) )
457 int len = lua_strlen( L, -1 );
458 p_ext->p_icondata = malloc( len );
459 if( p_ext->p_icondata )
461 p_ext->i_icondata_size = len;
462 memcpy( p_ext->p_icondata, lua_tostring( L, -1 ), len );
469 msg_Warn( p_mgr, "In script %s, function descriptor() "
470 "did not return a table!", psz_script );
476 msg_Err( p_mgr, "Script %s went completely foobar", psz_script );
480 msg_Dbg( p_mgr, "Script %s has the following capability flags: 0x%x",
481 psz_script, p_ext->p_sys->i_capabilities );
488 free( p_ext->psz_name );
489 free( p_ext->psz_title );
490 free( p_ext->psz_url );
491 free( p_ext->psz_author );
492 free( p_ext->psz_description );
493 free( p_ext->psz_shortdescription );
494 free( p_ext->psz_version );
495 vlc_mutex_destroy( &p_ext->p_sys->command_lock );
496 vlc_mutex_destroy( &p_ext->p_sys->running_lock );
497 vlc_cond_destroy( &p_ext->p_sys->wait );
498 free( p_ext->p_sys );
503 /* Add the extension to the list of known extensions */
504 ARRAY_APPEND( p_mgr->extensions, p_ext );
507 /* Continue batch execution */
511 static int Control( extensions_manager_t *p_mgr, int i_control, va_list args )
513 extension_t *p_ext = NULL;
515 uint16_t **ppus = NULL;
516 char ***pppsz = NULL;
521 case EXTENSION_ACTIVATE:
522 p_ext = ( extension_t* ) va_arg( args, extension_t* );
523 return Activate( p_mgr, p_ext );
525 case EXTENSION_DEACTIVATE:
526 p_ext = ( extension_t* ) va_arg( args, extension_t* );
527 return Deactivate( p_mgr, p_ext );
529 case EXTENSION_IS_ACTIVATED:
530 p_ext = ( extension_t* ) va_arg( args, extension_t* );
531 pb = ( bool* ) va_arg( args, bool* );
532 *pb = IsActivated( p_mgr, p_ext );
535 case EXTENSION_HAS_MENU:
536 p_ext = ( extension_t* ) va_arg( args, extension_t* );
537 pb = ( bool* ) va_arg( args, bool* );
538 *pb = ( p_ext->p_sys->i_capabilities & EXT_HAS_MENU ) ? 1 : 0;
541 case EXTENSION_GET_MENU:
542 p_ext = ( extension_t* ) va_arg( args, extension_t* );
543 pppsz = ( char*** ) va_arg( args, char*** );
544 ppus = ( uint16_t** ) va_arg( args, uint16_t** );
545 return GetMenuEntries( p_mgr, p_ext, pppsz, ppus );
547 case EXTENSION_TRIGGER_ONLY:
548 p_ext = ( extension_t* ) va_arg( args, extension_t* );
549 pb = ( bool* ) va_arg( args, bool* );
550 *pb = ( p_ext->p_sys->i_capabilities & EXT_TRIGGER_ONLY ) ? 1 : 0;
553 case EXTENSION_TRIGGER:
554 p_ext = ( extension_t* ) va_arg( args, extension_t* );
555 return TriggerExtension( p_mgr, p_ext );
557 case EXTENSION_TRIGGER_MENU:
558 p_ext = ( extension_t* ) va_arg( args, extension_t* );
559 // GCC: 'uint16_t' is promoted to 'int' when passed through '...'
560 i = ( int ) va_arg( args, int );
561 return TriggerMenu( p_ext, i );
563 case EXTENSION_SET_INPUT:
565 p_ext = ( extension_t* ) va_arg( args, extension_t* );
566 input_thread_t *p_input = va_arg( args, struct input_thread_t * );
568 if( !LockExtension( p_ext ) )
572 input_thread_t *old = p_ext->p_sys->p_input;
573 input_item_t *p_item;
576 // Untrack meta fetched events
577 if( p_ext->p_sys->i_capabilities & EXT_META_LISTENER )
579 p_item = input_GetItem( old );
580 vlc_event_detach( &p_item->event_manager,
581 vlc_InputItemMetaChanged,
582 inputItemMetaChanged,
584 vlc_gc_decref( p_item );
586 vlc_object_release( old );
589 p_ext->p_sys->p_input = p_input ? vlc_object_hold( p_input )
592 // Tell the script the input changed
593 if( p_ext->p_sys->i_capabilities & EXT_INPUT_LISTENER )
595 PushCommandUnique( p_ext, CMD_SET_INPUT );
598 // Track meta fetched events
599 if( p_ext->p_sys->p_input &&
600 p_ext->p_sys->i_capabilities & EXT_META_LISTENER )
602 p_item = input_GetItem( p_ext->p_sys->p_input );
603 vlc_gc_incref( p_item );
604 vlc_event_attach( &p_item->event_manager,
605 vlc_InputItemMetaChanged,
606 inputItemMetaChanged,
610 UnlockExtension( p_ext );
613 case EXTENSION_PLAYING_CHANGED:
616 p_ext = ( extension_t* ) va_arg( args, extension_t* );
617 assert( p_ext->psz_name != NULL );
618 i = ( int ) va_arg( args, int );
619 if( p_ext->p_sys->i_capabilities & EXT_PLAYING_LISTENER )
621 PushCommand( p_ext, CMD_PLAYING_CHANGED, i );
625 case EXTENSION_META_CHANGED:
628 p_ext = ( extension_t* ) va_arg( args, extension_t* );
629 PushCommand( p_ext, CMD_UPDATE_META );
633 msg_Warn( p_mgr, "Control '%d' not yet implemented in Extension",
641 int lua_ExtensionActivate( extensions_manager_t *p_mgr, extension_t *p_ext )
643 assert( p_mgr != NULL && p_ext != NULL );
644 return lua_ExecuteFunction( p_mgr, p_ext, "activate", LUA_END );
647 int lua_ExtensionDeactivate( extensions_manager_t *p_mgr, extension_t *p_ext )
649 assert( p_mgr != NULL && p_ext != NULL );
651 if( !p_ext->p_sys->L )
654 // Unset and release input objects
655 if( p_ext->p_sys->p_input )
657 if( p_ext->p_sys->i_capabilities & EXT_META_LISTENER )
660 input_item_t *p_item = input_GetItem( p_ext->p_sys->p_input );
661 vlc_gc_decref( p_item );
663 vlc_object_release( p_ext->p_sys->p_input );
664 p_ext->p_sys->p_input = NULL;
667 int i_ret = lua_ExecuteFunction( p_mgr, p_ext, "deactivate", LUA_END );
669 /* Clear Lua State */
670 lua_close( p_ext->p_sys->L );
671 p_ext->p_sys->L = NULL;
676 int lua_ExtensionWidgetClick( extensions_manager_t *p_mgr,
678 extension_widget_t *p_widget )
680 if( !p_ext->p_sys->L )
683 lua_State *L = GetLuaState( p_mgr, p_ext );
684 lua_pushlightuserdata( L, p_widget );
685 lua_gettable( L, LUA_REGISTRYINDEX );
686 return lua_ExecuteFunction( p_mgr, p_ext, NULL, LUA_END );
691 * Get the list of menu entries from an extension script
694 * @param pppsz_titles Pointer to NULL. All strings must be freed by the caller
695 * @param ppi_ids Pointer to NULL. Must be freed by the caller.
696 * @note This function is allowed to run in the UI thread. This means
697 * that it MUST respond very fast.
698 * @todo Remove the menu() hook and provide a new function vlc.set_menu()
700 static int GetMenuEntries( extensions_manager_t *p_mgr, extension_t *p_ext,
701 char ***pppsz_titles, uint16_t **ppi_ids )
703 assert( *pppsz_titles == NULL );
704 assert( *ppi_ids == NULL );
706 if( !IsActivated( p_mgr, p_ext ) )
708 msg_Dbg( p_mgr, "Can't get menu before activating the extension!" );
712 if( !LockExtension( p_ext ) )
714 /* Dying extension, fail. */
718 int i_ret = VLC_EGENERIC;
719 lua_State *L = GetLuaState( p_mgr, p_ext );
721 if( ( p_ext->p_sys->i_capabilities & EXT_HAS_MENU ) == 0 )
723 msg_Dbg( p_mgr, "can't get a menu from an extension without menu!" );
727 lua_getglobal( L, "menu" );
729 if( !lua_isfunction( L, -1 ) )
731 msg_Warn( p_mgr, "Error while running script %s, "
732 "function menu() not found", p_ext->psz_name );
736 if( lua_pcall( L, 0, 1, 0 ) )
738 msg_Warn( p_mgr, "Error while running script %s, "
739 "function menu(): %s", p_ext->psz_name,
740 lua_tostring( L, lua_gettop( L ) ) );
744 if( lua_gettop( L ) )
746 if( lua_istable( L, -1 ) )
749 size_t i_size = lua_objlen( L, -1 );
750 *pppsz_titles = ( char** ) calloc( i_size+1, sizeof( char* ) );
751 *ppi_ids = ( uint16_t* ) calloc( i_size+1, sizeof( uint16_t ) );
756 while( lua_next( L, -2 ) != 0 )
758 assert( i_idx < i_size );
759 if( (!lua_isstring( L, -1 )) || (!lua_isnumber( L, -2 )) )
761 msg_Warn( p_mgr, "In script %s, an entry in "
762 "the menu table is invalid!", p_ext->psz_name );
765 (*pppsz_titles)[ i_idx ] = strdup( luaL_checkstring( L, -1 ) );
766 (*ppi_ids)[ i_idx ] = (uint16_t) ( luaL_checkinteger( L, -2 ) & 0xFFFF );
773 msg_Warn( p_mgr, "Function menu() in script %s "
774 "did not return a table", p_ext->psz_name );
780 msg_Warn( p_mgr, "Script %s went completely foobar", p_ext->psz_name );
787 UnlockExtension( p_ext );
788 if( i_ret != VLC_SUCCESS )
790 msg_Dbg( p_mgr, "Something went wrong in %s (%s:%d)",
791 __func__, __FILE__, __LINE__ );
796 /* Must be entered with the Lock on Extension */
797 static lua_State* GetLuaState( extensions_manager_t *p_mgr,
809 msg_Err( p_mgr, "Could not create new Lua State" );
812 vlclua_set_this( L, p_mgr );
813 vlclua_extension_set( L, p_ext );
816 luaL_register( L, "vlc", p_reg );
821 /* Load more libraries */
824 luaopen_dialog( L, p_ext );
832 luaopen_playlist( L );
835 luaopen_strings( L );
836 luaopen_variables( L );
842 /* Register extension specific functions */
843 lua_getglobal( L, "vlc" );
844 lua_pushcfunction( L, vlclua_extension_deactivate );
845 lua_setfield( L, -2, "deactivate" );
846 lua_pushcfunction( L, vlclua_extension_keep_alive );
847 lua_setfield( L, -2, "keep_alive" );
849 /* Setup the module search path */
850 if( !strncmp( p_ext->psz_name, "zip://", 6 ) )
852 /* Load all required modules manually */
853 lua_register( L, "require", &vlclua_extension_require );
857 if( vlclua_add_modules_path( p_mgr, L, p_ext->psz_name ) )
859 msg_Warn( p_mgr, "Error while setting the module "
860 "search path for %s", p_ext->psz_name );
865 /* Load and run the script(s) */
866 if( vlclua_dofile( VLC_OBJECT( p_mgr ), L, p_ext->psz_name ) )
868 msg_Warn( p_mgr, "Error loading script %s: %s", p_ext->psz_name,
869 lua_tostring( L, lua_gettop( L ) ) );
881 int lua_ExecuteFunction( extensions_manager_t *p_mgr, extension_t *p_ext,
882 const char *psz_function, ... )
885 va_start( args, psz_function );
886 int i_ret = lua_ExecuteFunctionVa( p_mgr, p_ext, psz_function, args );
892 * Execute a function in a Lua script
893 * @param psz_function Name of global function to execute. If NULL, assume
894 * that the function object is already on top of the
896 * @return < 0 in case of failure, >= 0 in case of success
897 * @note It's better to call this function from a dedicated thread
898 * (see extension_thread.c)
900 int lua_ExecuteFunctionVa( extensions_manager_t *p_mgr, extension_t *p_ext,
901 const char *psz_function, va_list args )
903 int i_ret = VLC_SUCCESS;
905 assert( p_mgr != NULL );
906 assert( p_ext != NULL );
908 lua_State *L = GetLuaState( p_mgr, p_ext );
913 lua_getglobal( L, psz_function );
915 if( !lua_isfunction( L, -1 ) )
917 msg_Warn( p_mgr, "Error while running script %s, "
918 "function %s() not found", p_ext->psz_name, psz_function );
922 lua_datatype_e type = LUA_END;
923 while( ( type = va_arg( args, int ) ) != LUA_END )
925 if( type == LUA_NUM )
927 lua_pushnumber( L , ( int ) va_arg( args, int ) );
929 else if( type == LUA_TEXT )
931 lua_pushstring( L , ( char * ) va_arg( args, char* ) );
935 msg_Warn( p_mgr, "Undefined argument type %d to lua function %s"
936 "from script %s", type, psz_function, p_ext->psz_name );
942 // Create watch timer
943 vlc_mutex_lock( &p_ext->p_sys->command_lock );
944 vlc_timer_schedule( p_ext->p_sys->timer, false, WATCH_TIMER_PERIOD, 0 );
945 vlc_mutex_unlock( &p_ext->p_sys->command_lock );
947 // Start actual call to Lua
948 if( lua_pcall( L, i_args, 1, 0 ) )
950 msg_Warn( p_mgr, "Error while running script %s, "
951 "function %s(): %s", p_ext->psz_name, psz_function,
952 lua_tostring( L, lua_gettop( L ) ) );
953 i_ret = VLC_EGENERIC;
956 // Reset watch timer and timestamp
957 vlc_mutex_lock( &p_ext->p_sys->command_lock );
958 if( p_ext->p_sys->progress )
960 dialog_ProgressDestroy( p_ext->p_sys->progress );
961 p_ext->p_sys->progress = NULL;
963 vlc_timer_schedule( p_ext->p_sys->timer, false, 0, 0 );
964 vlc_mutex_unlock( &p_ext->p_sys->command_lock );
966 i_ret |= lua_DialogFlush( L );
973 static inline int TriggerMenu( extension_t *p_ext, int i_id )
975 return PushCommand( p_ext, CMD_TRIGGERMENU, i_id );
978 int lua_ExtensionTriggerMenu( extensions_manager_t *p_mgr,
979 extension_t *p_ext, int id )
981 int i_ret = VLC_SUCCESS;
982 lua_State *L = GetLuaState( p_mgr, p_ext );
987 luaopen_dialog( L, p_ext );
989 lua_getglobal( L, "trigger_menu" );
990 if( !lua_isfunction( L, -1 ) )
992 msg_Warn( p_mgr, "Error while running script %s, "
993 "function trigger_menu() not found", p_ext->psz_name );
997 /* Pass id as unique argument to the function */
998 lua_pushinteger( L, id );
1000 // Create watch timer
1001 vlc_mutex_lock( &p_ext->p_sys->command_lock );
1002 vlc_timer_schedule( p_ext->p_sys->timer, false, WATCH_TIMER_PERIOD, 0 );
1003 vlc_mutex_unlock( &p_ext->p_sys->command_lock );
1005 if( lua_pcall( L, 1, 1, 0 ) != 0 )
1007 msg_Warn( p_mgr, "Error while running script %s, "
1008 "function trigger_menu(): %s", p_ext->psz_name,
1009 lua_tostring( L, lua_gettop( L ) ) );
1010 i_ret = VLC_EGENERIC;
1013 // Reset watch timer and timestamp
1014 vlc_mutex_lock( &p_ext->p_sys->command_lock );
1015 if( p_ext->p_sys->progress )
1017 dialog_ProgressDestroy( p_ext->p_sys->progress );
1018 p_ext->p_sys->progress = NULL;
1020 vlc_timer_schedule( p_ext->p_sys->timer, false, 0, 0 );
1021 vlc_mutex_unlock( &p_ext->p_sys->command_lock );
1023 i_ret |= lua_DialogFlush( L );
1024 if( i_ret < VLC_SUCCESS )
1026 msg_Dbg( p_mgr, "Something went wrong in %s (%s:%d)",
1027 __func__, __FILE__, __LINE__ );
1033 /** Directly trigger an extension, without activating it
1034 * This is NOT multithreaded, and this code runs in the UI thread
1036 * @param p_ext Extension to trigger
1037 * @return Value returned by the lua function "trigger"
1039 static int TriggerExtension( extensions_manager_t *p_mgr,
1040 extension_t *p_ext )
1042 int i_ret = lua_ExecuteFunction( p_mgr, p_ext, "trigger", LUA_END );
1044 /* Close lua state for trigger-only extensions */
1045 if( p_ext->p_sys->L )
1046 lua_close( p_ext->p_sys->L );
1047 p_ext->p_sys->L = NULL;
1052 /** Set extension associated to the current script
1053 * @param L current lua_State
1054 * @param p_ext the extension
1056 void vlclua_extension_set( lua_State *L, extension_t *p_ext )
1058 lua_pushlightuserdata( L, vlclua_extension_set );
1059 lua_pushlightuserdata( L, p_ext );
1060 lua_rawset( L, LUA_REGISTRYINDEX );
1063 /** Retrieve extension associated to the current script
1064 * @param L current lua_State
1065 * @return Extension pointer
1067 extension_t *vlclua_extension_get( lua_State *L )
1069 lua_pushlightuserdata( L, vlclua_extension_set );
1070 lua_rawget( L, LUA_REGISTRYINDEX );
1071 extension_t *p_ext = (extension_t*) lua_topointer( L, -1 );
1076 /** Deactivate an extension by order from the extension itself
1077 * @param L lua_State
1078 * @note This is an asynchronous call. A script calling vlc.deactivate() will
1079 * be executed to the end before the last call to deactivate() is done.
1081 int vlclua_extension_deactivate( lua_State *L )
1083 extension_t *p_ext = vlclua_extension_get( L );
1084 int i_ret = Deactivate( p_ext->p_sys->p_mgr, p_ext );
1085 return ( i_ret == VLC_SUCCESS ) ? 1 : 0;
1088 /** Keep an extension alive. This resets the watch timer to 0
1089 * @param L lua_State
1090 * @note This is the "vlc.keep_alive()" function
1092 int vlclua_extension_keep_alive( lua_State *L )
1094 extension_t *p_ext = vlclua_extension_get( L );
1096 vlc_mutex_lock( &p_ext->p_sys->command_lock );
1097 if( p_ext->p_sys->progress )
1099 dialog_ProgressDestroy( p_ext->p_sys->progress );
1100 p_ext->p_sys->progress = NULL;
1102 vlc_timer_schedule( p_ext->p_sys->timer, false, WATCH_TIMER_PERIOD, 0 );
1103 vlc_mutex_unlock( &p_ext->p_sys->command_lock );
1108 /** Callback for the variable "dialog-event"
1109 * @param p_this Current object owner of the extension and the dialog
1110 * @param psz_var "dialog-event"
1111 * @param oldval Unused
1112 * @param newval Address of the dialog
1113 * @param p_data Unused
1115 static int vlclua_extension_dialog_callback( vlc_object_t *p_this,
1116 char const *psz_var,
1121 /* psz_var == "dialog-event" */
1126 extension_dialog_command_t *command = newval.p_address;
1127 assert( command != NULL );
1128 assert( command->p_dlg != NULL);
1130 extension_t *p_ext = command->p_dlg->p_sys;
1131 assert( p_ext != NULL );
1133 extension_widget_t *p_widget = command->p_data;
1135 switch( command->event )
1137 case EXTENSION_EVENT_CLICK:
1138 assert( p_widget != NULL );
1139 PushCommandUnique( p_ext, CMD_CLICK, p_widget );
1141 case EXTENSION_EVENT_CLOSE:
1142 PushCommandUnique( p_ext, CMD_CLOSE );
1145 msg_Dbg( p_this, "Received unknown UI event %d, discarded",
1153 /** Callback on vlc_InputItemMetaChanged event
1155 static void inputItemMetaChanged( const vlc_event_t *p_event,
1158 assert( p_event && p_event->type == vlc_InputItemMetaChanged );
1160 extension_t *p_ext = ( extension_t* ) data;
1161 assert( p_ext != NULL );
1163 PushCommandUnique( p_ext, CMD_UPDATE_META );
1166 /** Lock this extension. Can fail. */
1167 bool LockExtension( extension_t *p_ext )
1169 vlc_mutex_lock( &p_ext->p_sys->command_lock );
1170 if( p_ext->p_sys->b_exiting )
1172 vlc_mutex_unlock( &p_ext->p_sys->command_lock );
1176 vlc_mutex_lock( &p_ext->p_sys->running_lock );
1177 if( p_ext->p_sys->b_exiting )
1179 vlc_mutex_unlock( &p_ext->p_sys->running_lock );
1180 vlc_mutex_unlock( &p_ext->p_sys->command_lock );
1184 vlc_mutex_unlock( &p_ext->p_sys->command_lock );
1188 /** Unlock this extension. */
1189 void UnlockExtension( extension_t *p_ext )
1191 vlc_mutex_unlock( &p_ext->p_sys->running_lock );
1194 /** Watch timer callback
1195 * The timer expired, Lua may be stuck, ask the user what to do now
1197 static void WatchTimerCallback( void *data )
1199 extension_t *p_ext = data;
1200 extensions_manager_t *p_mgr = p_ext->p_sys->p_mgr;
1203 if( asprintf( &message, _( "Extension '%s' does not respond.\n"
1204 "Do you want to kill it now? " ),
1205 p_ext->psz_title ) == -1 )
1210 vlc_mutex_lock( &p_ext->p_sys->command_lock );
1212 // Do we have a pending Deactivate command?
1213 if( ( p_ext->p_sys->command &&
1214 p_ext->p_sys->command->i_command == CMD_DEACTIVATE )
1215 || ( p_ext->p_sys->command->next
1216 && p_ext->p_sys->command->next->i_command == CMD_DEACTIVATE) )
1218 if( p_ext->p_sys->progress )
1220 dialog_ProgressDestroy( p_ext->p_sys->progress );
1221 p_ext->p_sys->progress = NULL;
1223 vlc_mutex_unlock( &p_ext->p_sys->command_lock );
1224 KillExtension( p_mgr, p_ext );
1228 if( !p_ext->p_sys->progress )
1230 p_ext->p_sys->progress =
1231 dialog_ProgressCreate( p_mgr, _( "Extension not responding!" ),
1234 vlc_timer_schedule( p_ext->p_sys->timer, false, 100000, 0 );
1238 if( dialog_ProgressCancelled( p_ext->p_sys->progress ) )
1240 dialog_ProgressDestroy( p_ext->p_sys->progress );
1241 p_ext->p_sys->progress = NULL;
1242 vlc_mutex_unlock( &p_ext->p_sys->command_lock );
1243 KillExtension( p_mgr, p_ext );
1246 vlc_timer_schedule( p_ext->p_sys->timer, false, 100000, 0 );
1248 vlc_mutex_unlock( &p_ext->p_sys->command_lock );