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_interface.h>
40 #include <vlc_events.h>
41 #include <vlc_dialog.h>
43 /* Functions to register */
44 static const luaL_Reg p_reg[] =
50 * Extensions capabilities
51 * Note: #define and ppsz_capabilities must be in sync
53 static const char caps[][20] = {
54 #define EXT_HAS_MENU (1 << 0) ///< Hook: menu
56 #define EXT_TRIGGER_ONLY (1 << 1) ///< Hook: trigger. Not activable
58 #define EXT_INPUT_LISTENER (1 << 2) ///< Hook: input_changed
60 #define EXT_META_LISTENER (1 << 3) ///< Hook: meta_changed
62 #define EXT_PLAYING_LISTENER (1 << 4) ///< Hook: status_changed
66 #define WATCH_TIMER_PERIOD (10 * CLOCK_FREQ) ///< 10s period for the timer
68 static int ScanExtensions( extensions_manager_t *p_this );
69 static int ScanLuaCallback( vlc_object_t *p_this, const char *psz_script,
70 const struct luabatch_context_t * );
71 static int Control( extensions_manager_t *, int, va_list );
72 static int GetMenuEntries( extensions_manager_t *p_mgr, extension_t *p_ext,
73 char ***pppsz_titles, uint16_t **ppi_ids );
74 static lua_State* GetLuaState( extensions_manager_t *p_mgr,
76 static int TriggerMenu( extension_t *p_ext, int id );
77 static int TriggerExtension( extensions_manager_t *p_mgr,
79 static void WatchTimerCallback( void* );
81 static int vlclua_extension_deactivate( lua_State *L );
82 static int vlclua_extension_keep_alive( lua_State *L );
85 static int vlclua_extension_dialog_callback( vlc_object_t *p_this,
91 /* Input item callback: vlc_InputItemMetaChanged */
92 static void inputItemMetaChanged( const vlc_event_t *p_event,
99 int Open_Extension( vlc_object_t *p_this )
101 msg_Dbg( p_this, "Opening Lua Extension module" );
103 extensions_manager_t *p_mgr = ( extensions_manager_t* ) p_this;
105 p_mgr->pf_control = Control;
107 extensions_manager_sys_t *p_sys = ( extensions_manager_sys_t* )
108 calloc( 1, sizeof( extensions_manager_sys_t ) );
109 if( !p_sys ) return VLC_ENOMEM;
111 p_mgr->p_sys = p_sys;
112 ARRAY_INIT( p_sys->activated_extensions );
113 ARRAY_INIT( p_mgr->extensions );
114 vlc_mutex_init( &p_mgr->lock );
115 vlc_mutex_init( &p_mgr->p_sys->lock );
117 /* Scan available Lua Extensions */
118 if( ScanExtensions( p_mgr ) != VLC_SUCCESS )
120 msg_Err( p_mgr, "Can't load extensions modules" );
124 // Create the dialog-event variable
125 var_Create( p_this, "dialog-event", VLC_VAR_ADDRESS );
126 var_AddCallback( p_this, "dialog-event",
127 vlclua_extension_dialog_callback, NULL );
133 * Module unload function
135 void Close_Extension( vlc_object_t *p_this )
137 extensions_manager_t *p_mgr = ( extensions_manager_t* ) p_this;
138 msg_Dbg( p_mgr, "Deactivating all loaded extensions" );
140 vlc_mutex_lock( &p_mgr->lock );
141 p_mgr->p_sys->b_killed = true;
142 vlc_mutex_unlock( &p_mgr->lock );
144 var_Destroy( p_mgr, "dialog-event" );
146 extension_t *p_ext = NULL;
147 FOREACH_ARRAY( p_ext, p_mgr->p_sys->activated_extensions )
150 msg_Dbg( p_mgr, "Deactivating '%s'", p_ext->psz_title );
151 Deactivate( p_mgr, p_ext );
152 vlc_join( p_ext->p_sys->thread, NULL );
156 msg_Dbg( p_mgr, "All extensions are now deactivated" );
157 ARRAY_RESET( p_mgr->p_sys->activated_extensions );
159 vlc_mutex_destroy( &p_mgr->lock );
160 vlc_mutex_destroy( &p_mgr->p_sys->lock );
161 free( p_mgr->p_sys );
164 /* Free extensions' memory */
165 FOREACH_ARRAY( p_ext, p_mgr->extensions )
169 if( p_ext->p_sys->L )
170 lua_close( p_ext->p_sys->L );
171 free( p_ext->psz_name );
172 free( p_ext->psz_title );
173 free( p_ext->psz_author );
174 free( p_ext->psz_description );
175 free( p_ext->psz_shortdescription );
176 free( p_ext->psz_url );
177 free( p_ext->psz_version );
178 free( p_ext->p_icondata );
180 vlc_mutex_destroy( &p_ext->p_sys->running_lock );
181 vlc_mutex_destroy( &p_ext->p_sys->command_lock );
182 vlc_cond_destroy( &p_ext->p_sys->wait );
183 vlc_timer_destroy( p_ext->p_sys->timer );
185 free( p_ext->p_sys );
190 ARRAY_RESET( p_mgr->extensions );
192 var_DelCallback( p_this, "dialog-event",
193 vlclua_extension_dialog_callback, NULL );
197 * Batch scan all Lua files in folder "extensions"
198 * @param p_mgr This extensions_manager_t object
200 static int ScanExtensions( extensions_manager_t *p_mgr )
203 vlclua_scripts_batch_execute( VLC_OBJECT( p_mgr ),
215 * Dummy Lua function: does nothing
216 * @note This function can be used to replace "require" while scanning for
218 * Even the built-in libraries are not loaded when calling descriptor()
220 static int vlclua_dummy_require( lua_State *L )
227 * Replacement for "require", adding support for packaged extensions
228 * @note Loads modules in the modules/ folder of a package
229 * @note Try first with .luac and then with .lua
231 static int vlclua_extension_require( lua_State *L )
233 const char *psz_module = luaL_checkstring( L, 1 );
234 vlc_object_t *p_this = vlclua_get_this( L );
235 extension_t *p_ext = vlclua_extension_get( L );
236 msg_Dbg( p_this, "loading module '%s' from extension package",
238 char *psz_fullpath, *psz_package, *sep;
239 psz_package = strdup( p_ext->psz_name );
240 sep = strrchr( psz_package, '/' );
244 return luaL_error( L, "could not find package name" );
247 if( -1 == asprintf( &psz_fullpath,
248 "%s/modules/%s.luac", psz_package, psz_module ) )
253 int i_ret = vlclua_dofile( p_this, L, psz_fullpath );
256 // Remove trailing 'c' --> try with .lua script
257 psz_fullpath[ strlen( psz_fullpath ) - 1 ] = '\0';
258 i_ret = vlclua_dofile( p_this, L, psz_fullpath );
260 free( psz_fullpath );
264 return luaL_error( L, "unable to load module '%s' from package",
271 * Batch scan all Lua files in folder "extensions": callback
272 * @param p_this This extensions_manager_t object
273 * @param psz_filename Name of the script to run
274 * @param L Lua State, common to all scripts here
275 * @param dummy: unused
277 int ScanLuaCallback( vlc_object_t *p_this, const char *psz_filename,
278 const struct luabatch_context_t *dummy )
281 extensions_manager_t *p_mgr = ( extensions_manager_t* ) p_this;
284 msg_Dbg( p_mgr, "Scanning Lua script %s", psz_filename );
286 /* Experimental: read .vle packages (Zip archives) */
287 char *psz_script = NULL;
288 int i_flen = strlen( psz_filename );
289 if( !strncasecmp( psz_filename + i_flen - 4, ".vle", 4 ) )
291 msg_Dbg( p_this, "reading Lua script in a zip archive" );
292 psz_script = calloc( 1, i_flen + 6 + 12 + 1 );
295 strcpy( psz_script, "zip://" );
296 strncat( psz_script, psz_filename, i_flen + 19 );
297 strncat( psz_script, "!/script.lua", i_flen + 19 );
301 psz_script = vlc_path2uri( psz_filename, "file" );
306 /* Create new script descriptor */
307 extension_t *p_ext = ( extension_t* ) calloc( 1, sizeof( extension_t ) );
314 p_ext->psz_name = psz_script;
315 p_ext->p_sys = (extension_sys_t*) calloc( 1, sizeof( extension_sys_t ) );
316 if( !p_ext->p_sys || !p_ext->psz_name )
318 free( p_ext->psz_name );
319 free( p_ext->p_sys );
323 p_ext->p_sys->p_mgr = p_mgr;
326 if( vlc_timer_create( &p_ext->p_sys->timer, WatchTimerCallback, p_ext ) )
328 free( p_ext->psz_name );
329 free( p_ext->p_sys );
334 /* Mutexes and conditions */
335 vlc_mutex_init( &p_ext->p_sys->command_lock );
336 vlc_mutex_init( &p_ext->p_sys->running_lock );
337 vlc_cond_init( &p_ext->p_sys->wait );
339 /* Prepare Lua state */
340 lua_State *L = luaL_newstate();
341 lua_register( L, "require", &vlclua_dummy_require );
344 if( vlclua_dofile( p_this, L, psz_script ) ) // luaL_dofile
346 msg_Warn( p_mgr, "Error loading script %s: %s", psz_script,
347 lua_tostring( L, lua_gettop( L ) ) );
352 /* Scan script for capabilities */
353 lua_getglobal( L, "descriptor" );
355 if( !lua_isfunction( L, -1 ) )
357 msg_Warn( p_mgr, "Error while running script %s, "
358 "function descriptor() not found", psz_script );
362 if( lua_pcall( L, 0, 1, 0 ) )
364 msg_Warn( p_mgr, "Error while running script %s, "
365 "function descriptor(): %s", psz_script,
366 lua_tostring( L, lua_gettop( L ) ) );
370 if( lua_gettop( L ) )
372 if( lua_istable( L, -1 ) )
375 lua_getfield( L, -1, "capabilities" );
376 if( lua_istable( L, -1 ) )
379 while( lua_next( L, -2 ) != 0 )
381 /* Key is at index -2 and value at index -1. Discard key */
382 const char *psz_cap = luaL_checkstring( L, -1 );
384 /* Find this capability's flag */
385 for( size_t i = 0; i < sizeof(caps)/sizeof(caps[0]); i++ )
387 if( !strcmp( caps[i], psz_cap ) )
390 p_ext->p_sys->i_capabilities |= 1 << i;
397 msg_Warn( p_mgr, "Extension capability '%s' unknown in"
398 " script %s", psz_cap, psz_script );
400 /* Removes 'value'; keeps 'key' for next iteration */
406 msg_Warn( p_mgr, "In script %s, function descriptor() "
407 "did not return a table of capabilities.",
413 lua_getfield( L, -1, "title" );
414 if( lua_isstring( L, -1 ) )
416 p_ext->psz_title = strdup( luaL_checkstring( L, -1 ) );
420 msg_Dbg( p_mgr, "In script %s, function descriptor() "
421 "did not return a string as title.",
423 p_ext->psz_title = strdup( psz_script );
428 lua_getfield( L, -1, "author" );
429 p_ext->psz_author = luaL_strdupornull( L, -1 );
432 /* Get description */
433 lua_getfield( L, -1, "description" );
434 p_ext->psz_description = luaL_strdupornull( L, -1 );
437 /* Get short description */
438 lua_getfield( L, -1, "shortdesc" );
439 p_ext->psz_shortdescription = luaL_strdupornull( L, -1 );
443 lua_getfield( L, -1, "url" );
444 p_ext->psz_url = luaL_strdupornull( L, -1 );
448 lua_getfield( L, -1, "version" );
449 p_ext->psz_version = luaL_strdupornull( L, -1 );
453 lua_getfield( L, -1, "icon" );
454 if( !lua_isnil( L, -1 ) && lua_isstring( L, -1 ) )
456 int len = lua_strlen( L, -1 );
457 p_ext->p_icondata = malloc( len );
458 if( p_ext->p_icondata )
460 p_ext->i_icondata_size = len;
461 memcpy( p_ext->p_icondata, lua_tostring( L, -1 ), len );
468 msg_Warn( p_mgr, "In script %s, function descriptor() "
469 "did not return a table!", psz_script );
475 msg_Err( p_mgr, "Script %s went completely foobar", psz_script );
479 msg_Dbg( p_mgr, "Script %s has the following capability flags: 0x%x",
480 psz_script, p_ext->p_sys->i_capabilities );
487 free( p_ext->psz_name );
488 free( p_ext->psz_title );
489 free( p_ext->psz_url );
490 free( p_ext->psz_author );
491 free( p_ext->psz_description );
492 free( p_ext->psz_shortdescription );
493 free( p_ext->psz_version );
494 vlc_mutex_destroy( &p_ext->p_sys->command_lock );
495 vlc_mutex_destroy( &p_ext->p_sys->running_lock );
496 vlc_cond_destroy( &p_ext->p_sys->wait );
497 free( p_ext->p_sys );
502 /* Add the extension to the list of known extensions */
503 ARRAY_APPEND( p_mgr->extensions, p_ext );
506 /* Continue batch execution */
510 static int Control( extensions_manager_t *p_mgr, int i_control, va_list args )
512 extension_t *p_ext = NULL;
514 uint16_t **ppus = NULL;
515 char ***pppsz = NULL;
520 case EXTENSION_ACTIVATE:
521 p_ext = ( extension_t* ) va_arg( args, extension_t* );
522 return Activate( p_mgr, p_ext );
524 case EXTENSION_DEACTIVATE:
525 p_ext = ( extension_t* ) va_arg( args, extension_t* );
526 return Deactivate( p_mgr, p_ext );
528 case EXTENSION_IS_ACTIVATED:
529 p_ext = ( extension_t* ) va_arg( args, extension_t* );
530 pb = ( bool* ) va_arg( args, bool* );
531 *pb = IsActivated( p_mgr, p_ext );
534 case EXTENSION_HAS_MENU:
535 p_ext = ( extension_t* ) va_arg( args, extension_t* );
536 pb = ( bool* ) va_arg( args, bool* );
537 *pb = ( p_ext->p_sys->i_capabilities & EXT_HAS_MENU ) ? 1 : 0;
540 case EXTENSION_GET_MENU:
541 p_ext = ( extension_t* ) va_arg( args, extension_t* );
542 pppsz = ( char*** ) va_arg( args, char*** );
543 ppus = ( uint16_t** ) va_arg( args, uint16_t** );
544 return GetMenuEntries( p_mgr, p_ext, pppsz, ppus );
546 case EXTENSION_TRIGGER_ONLY:
547 p_ext = ( extension_t* ) va_arg( args, extension_t* );
548 pb = ( bool* ) va_arg( args, bool* );
549 *pb = ( p_ext->p_sys->i_capabilities & EXT_TRIGGER_ONLY ) ? 1 : 0;
552 case EXTENSION_TRIGGER:
553 p_ext = ( extension_t* ) va_arg( args, extension_t* );
554 return TriggerExtension( p_mgr, p_ext );
556 case EXTENSION_TRIGGER_MENU:
557 p_ext = ( extension_t* ) va_arg( args, extension_t* );
558 // GCC: 'uint16_t' is promoted to 'int' when passed through '...'
559 i = ( int ) va_arg( args, int );
560 return TriggerMenu( p_ext, i );
562 case EXTENSION_SET_INPUT:
564 p_ext = ( extension_t* ) va_arg( args, extension_t* );
565 input_thread_t *p_input = va_arg( args, struct input_thread_t * );
567 if( !LockExtension( p_ext ) )
571 input_thread_t *old = p_ext->p_sys->p_input;
572 input_item_t *p_item;
575 // Untrack meta fetched events
576 if( p_ext->p_sys->i_capabilities & EXT_META_LISTENER )
578 p_item = input_GetItem( old );
579 vlc_event_detach( &p_item->event_manager,
580 vlc_InputItemMetaChanged,
581 inputItemMetaChanged,
583 vlc_gc_decref( p_item );
585 vlc_object_release( old );
588 p_ext->p_sys->p_input = p_input ? vlc_object_hold( p_input )
591 // Tell the script the input changed
592 if( p_ext->p_sys->i_capabilities & EXT_INPUT_LISTENER )
594 PushCommandUnique( p_ext, CMD_SET_INPUT );
597 // Track meta fetched events
598 if( p_ext->p_sys->p_input &&
599 p_ext->p_sys->i_capabilities & EXT_META_LISTENER )
601 p_item = input_GetItem( p_ext->p_sys->p_input );
602 vlc_gc_incref( p_item );
603 vlc_event_attach( &p_item->event_manager,
604 vlc_InputItemMetaChanged,
605 inputItemMetaChanged,
609 UnlockExtension( p_ext );
612 case EXTENSION_PLAYING_CHANGED:
615 p_ext = ( extension_t* ) va_arg( args, extension_t* );
616 assert( p_ext->psz_name != NULL );
617 i = ( int ) va_arg( args, int );
618 if( p_ext->p_sys->i_capabilities & EXT_PLAYING_LISTENER )
620 PushCommand( p_ext, CMD_PLAYING_CHANGED, i );
624 case EXTENSION_META_CHANGED:
627 p_ext = ( extension_t* ) va_arg( args, extension_t* );
628 PushCommand( p_ext, CMD_UPDATE_META );
632 msg_Warn( p_mgr, "Control '%d' not yet implemented in Extension",
640 int lua_ExtensionActivate( extensions_manager_t *p_mgr, extension_t *p_ext )
642 assert( p_mgr != NULL && p_ext != NULL );
643 return lua_ExecuteFunction( p_mgr, p_ext, "activate", LUA_END );
646 int lua_ExtensionDeactivate( extensions_manager_t *p_mgr, extension_t *p_ext )
648 assert( p_mgr != NULL && p_ext != NULL );
650 if( !p_ext->p_sys->L )
653 vlclua_fd_interrupt( &p_ext->p_sys->dtable );
655 // Unset and release input objects
656 if( p_ext->p_sys->p_input )
658 if( p_ext->p_sys->i_capabilities & EXT_META_LISTENER )
661 input_item_t *p_item = input_GetItem( p_ext->p_sys->p_input );
662 vlc_gc_decref( p_item );
664 vlc_object_release( p_ext->p_sys->p_input );
665 p_ext->p_sys->p_input = NULL;
668 int i_ret = lua_ExecuteFunction( p_mgr, p_ext, "deactivate", LUA_END );
670 /* Clear Lua State */
671 vlclua_fd_cleanup( &p_ext->p_sys->dtable );
672 lua_close( p_ext->p_sys->L );
673 p_ext->p_sys->L = NULL;
678 int lua_ExtensionWidgetClick( extensions_manager_t *p_mgr,
680 extension_widget_t *p_widget )
682 if( !p_ext->p_sys->L )
685 lua_State *L = GetLuaState( p_mgr, p_ext );
686 lua_pushlightuserdata( L, p_widget );
687 lua_gettable( L, LUA_REGISTRYINDEX );
688 return lua_ExecuteFunction( p_mgr, p_ext, NULL, LUA_END );
693 * Get the list of menu entries from an extension script
696 * @param pppsz_titles Pointer to NULL. All strings must be freed by the caller
697 * @param ppi_ids Pointer to NULL. Must be freed by the caller.
698 * @note This function is allowed to run in the UI thread. This means
699 * that it MUST respond very fast.
700 * @todo Remove the menu() hook and provide a new function vlc.set_menu()
702 static int GetMenuEntries( extensions_manager_t *p_mgr, extension_t *p_ext,
703 char ***pppsz_titles, uint16_t **ppi_ids )
705 assert( *pppsz_titles == NULL );
706 assert( *ppi_ids == NULL );
708 if( !IsActivated( p_mgr, p_ext ) )
710 msg_Dbg( p_mgr, "Can't get menu before activating the extension!" );
714 if( !LockExtension( p_ext ) )
716 /* Dying extension, fail. */
720 int i_ret = VLC_EGENERIC;
721 lua_State *L = GetLuaState( p_mgr, p_ext );
723 if( ( p_ext->p_sys->i_capabilities & EXT_HAS_MENU ) == 0 )
725 msg_Dbg( p_mgr, "can't get a menu from an extension without menu!" );
729 lua_getglobal( L, "menu" );
731 if( !lua_isfunction( L, -1 ) )
733 msg_Warn( p_mgr, "Error while running script %s, "
734 "function menu() not found", p_ext->psz_name );
738 if( lua_pcall( L, 0, 1, 0 ) )
740 msg_Warn( p_mgr, "Error while running script %s, "
741 "function menu(): %s", p_ext->psz_name,
742 lua_tostring( L, lua_gettop( L ) ) );
746 if( lua_gettop( L ) )
748 if( lua_istable( L, -1 ) )
751 size_t i_size = lua_objlen( L, -1 );
752 *pppsz_titles = ( char** ) calloc( i_size+1, sizeof( char* ) );
753 *ppi_ids = ( uint16_t* ) calloc( i_size+1, sizeof( uint16_t ) );
758 while( lua_next( L, -2 ) != 0 )
760 assert( i_idx < i_size );
761 if( (!lua_isstring( L, -1 )) || (!lua_isnumber( L, -2 )) )
763 msg_Warn( p_mgr, "In script %s, an entry in "
764 "the menu table is invalid!", p_ext->psz_name );
767 (*pppsz_titles)[ i_idx ] = strdup( luaL_checkstring( L, -1 ) );
768 (*ppi_ids)[ i_idx ] = (uint16_t) ( luaL_checkinteger( L, -2 ) & 0xFFFF );
775 msg_Warn( p_mgr, "Function menu() in script %s "
776 "did not return a table", p_ext->psz_name );
782 msg_Warn( p_mgr, "Script %s went completely foobar", p_ext->psz_name );
789 UnlockExtension( p_ext );
790 if( i_ret != VLC_SUCCESS )
792 msg_Dbg( p_mgr, "Something went wrong in %s (%s:%d)",
793 __func__, __FILE__, __LINE__ );
798 /* Must be entered with the Lock on Extension */
799 static lua_State* GetLuaState( extensions_manager_t *p_mgr,
811 msg_Err( p_mgr, "Could not create new Lua State" );
814 vlclua_set_this( L, p_mgr );
815 vlclua_set_playlist_internal( L,
816 pl_Get((intf_thread_t *)(p_mgr->p_parent)) );
817 vlclua_extension_set( L, p_ext );
820 luaL_register( L, "vlc", p_reg );
825 /* Load more libraries */
827 luaopen_dialog( L, p_ext );
830 if( vlclua_fd_init( L, &p_ext->p_sys->dtable ) )
837 luaopen_playlist( L );
838 luaopen_sd_intf( L );
840 luaopen_strings( L );
841 luaopen_variables( L );
846 #if defined(_WIN32) && !VLC_WINSTORE_APP
850 /* Register extension specific functions */
851 lua_getglobal( L, "vlc" );
852 lua_pushcfunction( L, vlclua_extension_deactivate );
853 lua_setfield( L, -2, "deactivate" );
854 lua_pushcfunction( L, vlclua_extension_keep_alive );
855 lua_setfield( L, -2, "keep_alive" );
857 /* Setup the module search path */
858 if( !strncmp( p_ext->psz_name, "zip://", 6 ) )
860 /* Load all required modules manually */
861 lua_register( L, "require", &vlclua_extension_require );
865 if( vlclua_add_modules_path( L, p_ext->psz_name ) )
867 msg_Warn( p_mgr, "Error while setting the module "
868 "search path for %s", p_ext->psz_name );
869 vlclua_fd_cleanup( &p_ext->p_sys->dtable );
874 /* Load and run the script(s) */
875 if( vlclua_dofile( VLC_OBJECT( p_mgr ), L, p_ext->psz_name ) )
877 msg_Warn( p_mgr, "Error loading script %s: %s", p_ext->psz_name,
878 lua_tostring( L, lua_gettop( L ) ) );
879 vlclua_fd_cleanup( &p_ext->p_sys->dtable );
891 int lua_ExecuteFunction( extensions_manager_t *p_mgr, extension_t *p_ext,
892 const char *psz_function, ... )
895 va_start( args, psz_function );
896 int i_ret = lua_ExecuteFunctionVa( p_mgr, p_ext, psz_function, args );
902 * Execute a function in a Lua script
903 * @param psz_function Name of global function to execute. If NULL, assume
904 * that the function object is already on top of the
906 * @return < 0 in case of failure, >= 0 in case of success
907 * @note It's better to call this function from a dedicated thread
908 * (see extension_thread.c)
910 int lua_ExecuteFunctionVa( extensions_manager_t *p_mgr, extension_t *p_ext,
911 const char *psz_function, va_list args )
913 int i_ret = VLC_SUCCESS;
915 assert( p_mgr != NULL );
916 assert( p_ext != NULL );
918 lua_State *L = GetLuaState( p_mgr, p_ext );
923 lua_getglobal( L, psz_function );
925 if( !lua_isfunction( L, -1 ) )
927 msg_Warn( p_mgr, "Error while running script %s, "
928 "function %s() not found", p_ext->psz_name, psz_function );
932 lua_datatype_e type = LUA_END;
933 while( ( type = va_arg( args, int ) ) != LUA_END )
935 if( type == LUA_NUM )
937 lua_pushnumber( L , ( int ) va_arg( args, int ) );
939 else if( type == LUA_TEXT )
941 lua_pushstring( L , ( char * ) va_arg( args, char* ) );
945 msg_Warn( p_mgr, "Undefined argument type %d to lua function %s"
946 "from script %s", type, psz_function, p_ext->psz_name );
952 // Create watch timer
953 vlc_mutex_lock( &p_ext->p_sys->command_lock );
954 vlc_timer_schedule( p_ext->p_sys->timer, false, WATCH_TIMER_PERIOD, 0 );
955 vlc_mutex_unlock( &p_ext->p_sys->command_lock );
957 // Start actual call to Lua
958 if( lua_pcall( L, i_args, 1, 0 ) )
960 msg_Warn( p_mgr, "Error while running script %s, "
961 "function %s(): %s", p_ext->psz_name, psz_function,
962 lua_tostring( L, lua_gettop( L ) ) );
963 i_ret = VLC_EGENERIC;
966 // Reset watch timer and timestamp
967 vlc_mutex_lock( &p_ext->p_sys->command_lock );
968 if( p_ext->p_sys->progress )
970 dialog_ProgressDestroy( p_ext->p_sys->progress );
971 p_ext->p_sys->progress = NULL;
973 vlc_timer_schedule( p_ext->p_sys->timer, false, 0, 0 );
974 vlc_mutex_unlock( &p_ext->p_sys->command_lock );
976 i_ret |= lua_DialogFlush( L );
983 static inline int TriggerMenu( extension_t *p_ext, int i_id )
985 return PushCommand( p_ext, CMD_TRIGGERMENU, i_id );
988 int lua_ExtensionTriggerMenu( extensions_manager_t *p_mgr,
989 extension_t *p_ext, int id )
991 int i_ret = VLC_SUCCESS;
992 lua_State *L = GetLuaState( p_mgr, p_ext );
997 luaopen_dialog( L, p_ext );
999 lua_getglobal( L, "trigger_menu" );
1000 if( !lua_isfunction( L, -1 ) )
1002 msg_Warn( p_mgr, "Error while running script %s, "
1003 "function trigger_menu() not found", p_ext->psz_name );
1004 return VLC_EGENERIC;
1007 /* Pass id as unique argument to the function */
1008 lua_pushinteger( L, id );
1010 // Create watch timer
1011 vlc_mutex_lock( &p_ext->p_sys->command_lock );
1012 vlc_timer_schedule( p_ext->p_sys->timer, false, WATCH_TIMER_PERIOD, 0 );
1013 vlc_mutex_unlock( &p_ext->p_sys->command_lock );
1015 if( lua_pcall( L, 1, 1, 0 ) != 0 )
1017 msg_Warn( p_mgr, "Error while running script %s, "
1018 "function trigger_menu(): %s", p_ext->psz_name,
1019 lua_tostring( L, lua_gettop( L ) ) );
1020 i_ret = VLC_EGENERIC;
1023 // Reset watch timer and timestamp
1024 vlc_mutex_lock( &p_ext->p_sys->command_lock );
1025 if( p_ext->p_sys->progress )
1027 dialog_ProgressDestroy( p_ext->p_sys->progress );
1028 p_ext->p_sys->progress = NULL;
1030 vlc_timer_schedule( p_ext->p_sys->timer, false, 0, 0 );
1031 vlc_mutex_unlock( &p_ext->p_sys->command_lock );
1033 i_ret |= lua_DialogFlush( L );
1034 if( i_ret < VLC_SUCCESS )
1036 msg_Dbg( p_mgr, "Something went wrong in %s (%s:%d)",
1037 __func__, __FILE__, __LINE__ );
1043 /** Directly trigger an extension, without activating it
1044 * This is NOT multithreaded, and this code runs in the UI thread
1046 * @param p_ext Extension to trigger
1047 * @return Value returned by the lua function "trigger"
1049 static int TriggerExtension( extensions_manager_t *p_mgr,
1050 extension_t *p_ext )
1052 int i_ret = lua_ExecuteFunction( p_mgr, p_ext, "trigger", LUA_END );
1054 /* Close lua state for trigger-only extensions */
1055 if( p_ext->p_sys->L )
1057 vlclua_fd_cleanup( &p_ext->p_sys->dtable );
1058 lua_close( p_ext->p_sys->L );
1060 p_ext->p_sys->L = NULL;
1065 /** Set extension associated to the current script
1066 * @param L current lua_State
1067 * @param p_ext the extension
1069 void vlclua_extension_set( lua_State *L, extension_t *p_ext )
1071 lua_pushlightuserdata( L, vlclua_extension_set );
1072 lua_pushlightuserdata( L, p_ext );
1073 lua_rawset( L, LUA_REGISTRYINDEX );
1076 /** Retrieve extension associated to the current script
1077 * @param L current lua_State
1078 * @return Extension pointer
1080 extension_t *vlclua_extension_get( lua_State *L )
1082 lua_pushlightuserdata( L, vlclua_extension_set );
1083 lua_rawget( L, LUA_REGISTRYINDEX );
1084 extension_t *p_ext = (extension_t*) lua_topointer( L, -1 );
1089 /** Deactivate an extension by order from the extension itself
1090 * @param L lua_State
1091 * @note This is an asynchronous call. A script calling vlc.deactivate() will
1092 * be executed to the end before the last call to deactivate() is done.
1094 int vlclua_extension_deactivate( lua_State *L )
1096 extension_t *p_ext = vlclua_extension_get( L );
1097 int i_ret = Deactivate( p_ext->p_sys->p_mgr, p_ext );
1098 return ( i_ret == VLC_SUCCESS ) ? 1 : 0;
1101 /** Keep an extension alive. This resets the watch timer to 0
1102 * @param L lua_State
1103 * @note This is the "vlc.keep_alive()" function
1105 int vlclua_extension_keep_alive( lua_State *L )
1107 extension_t *p_ext = vlclua_extension_get( L );
1109 vlc_mutex_lock( &p_ext->p_sys->command_lock );
1110 if( p_ext->p_sys->progress )
1112 dialog_ProgressDestroy( p_ext->p_sys->progress );
1113 p_ext->p_sys->progress = NULL;
1115 vlc_timer_schedule( p_ext->p_sys->timer, false, WATCH_TIMER_PERIOD, 0 );
1116 vlc_mutex_unlock( &p_ext->p_sys->command_lock );
1121 /** Callback for the variable "dialog-event"
1122 * @param p_this Current object owner of the extension and the dialog
1123 * @param psz_var "dialog-event"
1124 * @param oldval Unused
1125 * @param newval Address of the dialog
1126 * @param p_data Unused
1128 static int vlclua_extension_dialog_callback( vlc_object_t *p_this,
1129 char const *psz_var,
1134 /* psz_var == "dialog-event" */
1139 extension_dialog_command_t *command = newval.p_address;
1140 assert( command != NULL );
1141 assert( command->p_dlg != NULL);
1143 extension_t *p_ext = command->p_dlg->p_sys;
1144 assert( p_ext != NULL );
1146 extension_widget_t *p_widget = command->p_data;
1148 switch( command->event )
1150 case EXTENSION_EVENT_CLICK:
1151 assert( p_widget != NULL );
1152 PushCommandUnique( p_ext, CMD_CLICK, p_widget );
1154 case EXTENSION_EVENT_CLOSE:
1155 PushCommandUnique( p_ext, CMD_CLOSE );
1158 msg_Dbg( p_this, "Received unknown UI event %d, discarded",
1166 /** Callback on vlc_InputItemMetaChanged event
1168 static void inputItemMetaChanged( const vlc_event_t *p_event,
1171 assert( p_event && p_event->type == vlc_InputItemMetaChanged );
1173 extension_t *p_ext = ( extension_t* ) data;
1174 assert( p_ext != NULL );
1176 PushCommandUnique( p_ext, CMD_UPDATE_META );
1179 /** Lock this extension. Can fail. */
1180 bool LockExtension( extension_t *p_ext )
1182 vlc_mutex_lock( &p_ext->p_sys->command_lock );
1183 if( p_ext->p_sys->b_exiting )
1185 vlc_mutex_unlock( &p_ext->p_sys->command_lock );
1189 vlc_mutex_lock( &p_ext->p_sys->running_lock );
1190 if( p_ext->p_sys->b_exiting )
1192 vlc_mutex_unlock( &p_ext->p_sys->running_lock );
1193 vlc_mutex_unlock( &p_ext->p_sys->command_lock );
1197 vlc_mutex_unlock( &p_ext->p_sys->command_lock );
1201 /** Unlock this extension. */
1202 void UnlockExtension( extension_t *p_ext )
1204 vlc_mutex_unlock( &p_ext->p_sys->running_lock );
1207 /** Watch timer callback
1208 * The timer expired, Lua may be stuck, ask the user what to do now
1210 static void WatchTimerCallback( void *data )
1212 extension_t *p_ext = data;
1213 extensions_manager_t *p_mgr = p_ext->p_sys->p_mgr;
1216 if( asprintf( &message, _( "Extension '%s' does not respond.\n"
1217 "Do you want to kill it now? " ),
1218 p_ext->psz_title ) == -1 )
1223 vlc_mutex_lock( &p_ext->p_sys->command_lock );
1225 for( struct command_t *cmd = p_ext->p_sys->command;
1228 if( cmd->i_command == CMD_DEACTIVATE )
1229 { /* We have a pending Deactivate command... */
1230 if( p_ext->p_sys->progress )
1232 dialog_ProgressDestroy( p_ext->p_sys->progress );
1233 p_ext->p_sys->progress = NULL;
1235 vlc_mutex_unlock( &p_ext->p_sys->command_lock );
1236 KillExtension( p_mgr, p_ext );
1240 if( !p_ext->p_sys->progress )
1242 p_ext->p_sys->progress =
1243 dialog_ProgressCreate( p_mgr, _( "Extension not responding!" ),
1246 vlc_timer_schedule( p_ext->p_sys->timer, false, 100000, 0 );
1250 if( dialog_ProgressCancelled( p_ext->p_sys->progress ) )
1252 dialog_ProgressDestroy( p_ext->p_sys->progress );
1253 p_ext->p_sys->progress = NULL;
1254 vlc_mutex_unlock( &p_ext->p_sys->command_lock );
1255 KillExtension( p_mgr, p_ext );
1258 vlc_timer_schedule( p_ext->p_sys->timer, false, 100000, 0 );
1260 vlc_mutex_unlock( &p_ext->p_sys->command_lock );