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 static const char const caps[][20] = {
53 #define EXT_HAS_MENU (1 << 0) ///< Hook: menu
55 #define EXT_TRIGGER_ONLY (1 << 1) ///< Hook: trigger. Not activable
57 #define EXT_INPUT_LISTENER (1 << 2) ///< Hook: input_changed
59 #define EXT_META_LISTENER (1 << 3) ///< Hook: meta_changed
61 #define EXT_PLAYING_LISTENER (1 << 4) ///< Hook: status_changed
65 #define WATCH_TIMER_PERIOD (10 * CLOCK_FREQ) ///< 10s period for the timer
67 static int ScanExtensions( extensions_manager_t *p_this );
68 static int ScanLuaCallback( vlc_object_t *p_this, const char *psz_script,
70 static int Control( extensions_manager_t *, int, va_list );
71 static int GetMenuEntries( extensions_manager_t *p_mgr, extension_t *p_ext,
72 char ***pppsz_titles, uint16_t **ppi_ids );
73 static lua_State* GetLuaState( extensions_manager_t *p_mgr,
75 static int TriggerMenu( extension_t *p_ext, int id );
76 static int TriggerExtension( extensions_manager_t *p_mgr,
78 static void WatchTimerCallback( void* );
80 static int vlclua_extension_deactivate( lua_State *L );
81 static int vlclua_extension_keep_alive( lua_State *L );
84 static int vlclua_extension_dialog_callback( vlc_object_t *p_this,
90 /* Input item callback: vlc_InputItemMetaChanged */
91 static void inputItemMetaChanged( const vlc_event_t *p_event,
98 int Open_Extension( vlc_object_t *p_this )
100 msg_Dbg( p_this, "Opening Lua Extension module" );
102 extensions_manager_t *p_mgr = ( extensions_manager_t* ) p_this;
104 p_mgr->pf_control = Control;
106 extensions_manager_sys_t *p_sys = ( extensions_manager_sys_t* )
107 calloc( 1, sizeof( extensions_manager_sys_t ) );
108 if( !p_sys ) return VLC_ENOMEM;
110 p_mgr->p_sys = p_sys;
111 ARRAY_INIT( p_sys->activated_extensions );
112 ARRAY_INIT( p_mgr->extensions );
113 vlc_mutex_init( &p_mgr->lock );
114 vlc_mutex_init( &p_mgr->p_sys->lock );
116 /* Scan available Lua Extensions */
117 if( ScanExtensions( p_mgr ) != VLC_SUCCESS )
119 msg_Err( p_mgr, "Can't load extensions modules" );
123 // Create the dialog-event variable
124 var_Create( p_this, "dialog-event", VLC_VAR_ADDRESS );
125 var_AddCallback( p_this, "dialog-event",
126 vlclua_extension_dialog_callback, NULL );
132 * Module unload function
134 void Close_Extension( vlc_object_t *p_this )
136 extensions_manager_t *p_mgr = ( extensions_manager_t* ) p_this;
137 msg_Dbg( p_mgr, "Deactivating all loaded extensions" );
139 vlc_mutex_lock( &p_mgr->lock );
140 p_mgr->p_sys->b_killed = true;
141 vlc_mutex_unlock( &p_mgr->lock );
143 var_Destroy( p_mgr, "dialog-event" );
145 extension_t *p_ext = NULL;
146 FOREACH_ARRAY( p_ext, p_mgr->p_sys->activated_extensions )
149 msg_Dbg( p_mgr, "Deactivating '%s'", p_ext->psz_title );
150 Deactivate( p_mgr, p_ext );
151 vlc_join( p_ext->p_sys->thread, NULL );
155 msg_Dbg( p_mgr, "All extensions are now deactivated" );
156 ARRAY_RESET( p_mgr->p_sys->activated_extensions );
158 vlc_mutex_destroy( &p_mgr->lock );
159 vlc_mutex_destroy( &p_mgr->p_sys->lock );
160 free( p_mgr->p_sys );
163 /* Free extensions' memory */
164 FOREACH_ARRAY( p_ext, p_mgr->extensions )
168 if( p_ext->p_sys->L )
169 lua_close( p_ext->p_sys->L );
170 free( p_ext->psz_name );
171 free( p_ext->psz_title );
172 free( p_ext->psz_author );
173 free( p_ext->psz_description );
174 free( p_ext->psz_shortdescription );
175 free( p_ext->psz_url );
176 free( p_ext->psz_version );
177 free( p_ext->p_icondata );
179 vlc_mutex_destroy( &p_ext->p_sys->running_lock );
180 vlc_mutex_destroy( &p_ext->p_sys->command_lock );
181 vlc_cond_destroy( &p_ext->p_sys->wait );
182 vlc_timer_destroy( p_ext->p_sys->timer );
184 free( p_ext->p_sys );
189 ARRAY_RESET( p_mgr->extensions );
191 var_DelCallback( p_this, "dialog-event",
192 vlclua_extension_dialog_callback, NULL );
196 * Batch scan all Lua files in folder "extensions"
197 * @param p_mgr This extensions_manager_t object
199 static int ScanExtensions( extensions_manager_t *p_mgr )
202 vlclua_scripts_batch_execute( VLC_OBJECT( p_mgr ),
214 * Dummy Lua function: does nothing
215 * @note This function can be used to replace "require" while scanning for
217 * Even the built-in libraries are not loaded when calling descriptor()
219 static int vlclua_dummy_require( lua_State *L )
226 * Replacement for "require", adding support for packaged extensions
227 * @note Loads modules in the modules/ folder of a package
228 * @note Try first with .luac and then with .lua
230 static int vlclua_extension_require( lua_State *L )
232 const char *psz_module = luaL_checkstring( L, 1 );
233 vlc_object_t *p_this = vlclua_get_this( L );
234 extension_t *p_ext = vlclua_extension_get( L );
235 msg_Dbg( p_this, "loading module '%s' from extension package",
237 char *psz_fullpath, *psz_package, *sep;
238 psz_package = strdup( p_ext->psz_name );
239 sep = strrchr( psz_package, '/' );
243 return luaL_error( L, "could not find package name" );
246 if( -1 == asprintf( &psz_fullpath,
247 "%s/modules/%s.luac", psz_package, psz_module ) )
252 int i_ret = vlclua_dofile( p_this, L, psz_fullpath );
255 // Remove trailing 'c' --> try with .lua script
256 psz_fullpath[ strlen( psz_fullpath ) - 1 ] = '\0';
257 i_ret = vlclua_dofile( p_this, L, psz_fullpath );
259 free( psz_fullpath );
263 return luaL_error( L, "unable to load module '%s' from package",
270 * Batch scan all Lua files in folder "extensions": callback
271 * @param p_this This extensions_manager_t object
272 * @param psz_filename Name of the script to run
273 * @param L Lua State, common to all scripts here
274 * @param dummy: unused
276 int ScanLuaCallback( vlc_object_t *p_this, const char *psz_filename,
280 extensions_manager_t *p_mgr = ( extensions_manager_t* ) p_this;
283 msg_Dbg( p_mgr, "Scanning Lua script %s", psz_filename );
285 /* Experimental: read .vle packages (Zip archives) */
286 char *psz_script = NULL;
287 int i_flen = strlen( psz_filename );
288 if( !strncasecmp( psz_filename + i_flen - 4, ".vle", 4 ) )
290 msg_Dbg( p_this, "reading Lua script in a zip archive" );
291 psz_script = calloc( 1, i_flen + 6 + 12 + 1 );
294 strcpy( psz_script, "zip://" );
295 strncat( psz_script, psz_filename, i_flen + 19 );
296 strncat( psz_script, "!/script.lua", i_flen + 19 );
300 psz_script = strdup( psz_filename );
305 /* Create new script descriptor */
306 extension_t *p_ext = ( extension_t* ) calloc( 1, sizeof( extension_t ) );
313 p_ext->psz_name = psz_script;
314 p_ext->p_sys = (extension_sys_t*) calloc( 1, sizeof( extension_sys_t ) );
315 if( !p_ext->p_sys || !p_ext->psz_name )
317 free( p_ext->psz_name );
318 free( p_ext->p_sys );
322 p_ext->p_sys->p_mgr = p_mgr;
325 if( vlc_timer_create( &p_ext->p_sys->timer, WatchTimerCallback, p_ext ) )
327 free( p_ext->psz_name );
328 free( p_ext->p_sys );
333 /* Mutexes and conditions */
334 vlc_mutex_init( &p_ext->p_sys->command_lock );
335 vlc_mutex_init( &p_ext->p_sys->running_lock );
336 vlc_cond_init( &p_ext->p_sys->wait );
338 /* Prepare Lua state */
339 lua_State *L = luaL_newstate();
340 lua_register( L, "require", &vlclua_dummy_require );
343 if( vlclua_dofile( p_this, L, psz_script ) ) // luaL_dofile
345 msg_Warn( p_mgr, "Error loading script %s: %s", psz_script,
346 lua_tostring( L, lua_gettop( L ) ) );
351 /* Scan script for capabilities */
352 lua_getglobal( L, "descriptor" );
354 if( !lua_isfunction( L, -1 ) )
356 msg_Warn( p_mgr, "Error while running script %s, "
357 "function descriptor() not found", psz_script );
361 if( lua_pcall( L, 0, 1, 0 ) )
363 msg_Warn( p_mgr, "Error while running script %s, "
364 "function descriptor(): %s", psz_script,
365 lua_tostring( L, lua_gettop( L ) ) );
369 if( lua_gettop( L ) )
371 if( lua_istable( L, -1 ) )
374 lua_getfield( L, -1, "capabilities" );
375 if( lua_istable( L, -1 ) )
378 while( lua_next( L, -2 ) != 0 )
380 /* Key is at index -2 and value at index -1. Discard key */
381 const char *psz_cap = luaL_checkstring( L, -1 );
383 /* Find this capability's flag */
384 for( size_t i = 0; i < sizeof(caps)/sizeof(caps[0]); i++ )
386 if( !strcmp( caps[i], psz_cap ) )
389 p_ext->p_sys->i_capabilities |= 1 << i;
396 msg_Warn( p_mgr, "Extension capability '%s' unknown in"
397 " script %s", psz_cap, psz_script );
399 /* Removes 'value'; keeps 'key' for next iteration */
405 msg_Warn( p_mgr, "In script %s, function descriptor() "
406 "did not return a table of capabilities.",
412 lua_getfield( L, -1, "title" );
413 if( lua_isstring( L, -1 ) )
415 p_ext->psz_title = strdup( luaL_checkstring( L, -1 ) );
419 msg_Dbg( p_mgr, "In script %s, function descriptor() "
420 "did not return a string as title.",
422 p_ext->psz_title = strdup( psz_script );
427 lua_getfield( L, -1, "author" );
428 p_ext->psz_author = luaL_strdupornull( L, -1 );
431 /* Get description */
432 lua_getfield( L, -1, "description" );
433 p_ext->psz_description = luaL_strdupornull( L, -1 );
436 /* Get short description */
437 lua_getfield( L, -1, "shortdesc" );
438 p_ext->psz_shortdescription = luaL_strdupornull( L, -1 );
442 lua_getfield( L, -1, "url" );
443 p_ext->psz_url = luaL_strdupornull( L, -1 );
447 lua_getfield( L, -1, "version" );
448 p_ext->psz_version = luaL_strdupornull( L, -1 );
452 lua_getfield( L, -1, "icon" );
453 if( !lua_isnil( L, -1 ) && lua_isstring( L, -1 ) )
455 int len = lua_strlen( L, -1 );
456 p_ext->p_icondata = malloc( len );
457 if( p_ext->p_icondata )
459 p_ext->i_icondata_size = len;
460 memcpy( p_ext->p_icondata, lua_tostring( L, -1 ), len );
467 msg_Warn( p_mgr, "In script %s, function descriptor() "
468 "did not return a table!", psz_script );
474 msg_Err( p_mgr, "Script %s went completely foobar", psz_script );
478 msg_Dbg( p_mgr, "Script %s has the following capability flags: 0x%x",
479 psz_script, p_ext->p_sys->i_capabilities );
486 free( p_ext->psz_name );
487 free( p_ext->psz_title );
488 free( p_ext->psz_url );
489 free( p_ext->psz_author );
490 free( p_ext->psz_description );
491 free( p_ext->psz_shortdescription );
492 free( p_ext->psz_version );
493 vlc_mutex_destroy( &p_ext->p_sys->command_lock );
494 vlc_mutex_destroy( &p_ext->p_sys->running_lock );
495 vlc_cond_destroy( &p_ext->p_sys->wait );
496 free( p_ext->p_sys );
501 /* Add the extension to the list of known extensions */
502 ARRAY_APPEND( p_mgr->extensions, p_ext );
505 /* Continue batch execution */
509 static int Control( extensions_manager_t *p_mgr, int i_control, va_list args )
511 extension_t *p_ext = NULL;
513 uint16_t **ppus = NULL;
514 char ***pppsz = NULL;
519 case EXTENSION_ACTIVATE:
520 p_ext = ( extension_t* ) va_arg( args, extension_t* );
521 return Activate( p_mgr, p_ext );
523 case EXTENSION_DEACTIVATE:
524 p_ext = ( extension_t* ) va_arg( args, extension_t* );
525 return Deactivate( p_mgr, p_ext );
527 case EXTENSION_IS_ACTIVATED:
528 p_ext = ( extension_t* ) va_arg( args, extension_t* );
529 pb = ( bool* ) va_arg( args, bool* );
530 *pb = IsActivated( p_mgr, p_ext );
533 case EXTENSION_HAS_MENU:
534 p_ext = ( extension_t* ) va_arg( args, extension_t* );
535 pb = ( bool* ) va_arg( args, bool* );
536 *pb = ( p_ext->p_sys->i_capabilities & EXT_HAS_MENU ) ? 1 : 0;
539 case EXTENSION_GET_MENU:
540 p_ext = ( extension_t* ) va_arg( args, extension_t* );
541 pppsz = ( char*** ) va_arg( args, char*** );
542 ppus = ( uint16_t** ) va_arg( args, uint16_t** );
543 return GetMenuEntries( p_mgr, p_ext, pppsz, ppus );
545 case EXTENSION_TRIGGER_ONLY:
546 p_ext = ( extension_t* ) va_arg( args, extension_t* );
547 pb = ( bool* ) va_arg( args, bool* );
548 *pb = ( p_ext->p_sys->i_capabilities & EXT_TRIGGER_ONLY ) ? 1 : 0;
551 case EXTENSION_TRIGGER:
552 p_ext = ( extension_t* ) va_arg( args, extension_t* );
553 return TriggerExtension( p_mgr, p_ext );
555 case EXTENSION_TRIGGER_MENU:
556 p_ext = ( extension_t* ) va_arg( args, extension_t* );
557 // GCC: 'uint16_t' is promoted to 'int' when passed through '...'
558 i = ( int ) va_arg( args, int );
559 return TriggerMenu( p_ext, i );
561 case EXTENSION_SET_INPUT:
563 p_ext = ( extension_t* ) va_arg( args, extension_t* );
564 input_thread_t *p_input = va_arg( args, struct input_thread_t * );
566 if( !LockExtension( p_ext ) )
570 input_thread_t *old = p_ext->p_sys->p_input;
571 input_item_t *p_item;
574 // Untrack meta fetched events
575 if( p_ext->p_sys->i_capabilities & EXT_META_LISTENER )
577 p_item = input_GetItem( old );
578 vlc_event_detach( &p_item->event_manager,
579 vlc_InputItemMetaChanged,
580 inputItemMetaChanged,
582 vlc_gc_decref( p_item );
584 vlc_object_release( old );
587 p_ext->p_sys->p_input = p_input ? vlc_object_hold( p_input )
590 // Tell the script the input changed
591 if( p_ext->p_sys->i_capabilities & EXT_INPUT_LISTENER )
593 PushCommandUnique( p_ext, CMD_SET_INPUT );
596 // Track meta fetched events
597 if( p_ext->p_sys->p_input &&
598 p_ext->p_sys->i_capabilities & EXT_META_LISTENER )
600 p_item = input_GetItem( p_ext->p_sys->p_input );
601 vlc_gc_incref( p_item );
602 vlc_event_attach( &p_item->event_manager,
603 vlc_InputItemMetaChanged,
604 inputItemMetaChanged,
608 UnlockExtension( p_ext );
611 case EXTENSION_PLAYING_CHANGED:
614 p_ext = ( extension_t* ) va_arg( args, extension_t* );
615 assert( p_ext->psz_name != NULL );
616 i = ( int ) va_arg( args, int );
617 if( p_ext->p_sys->i_capabilities & EXT_PLAYING_LISTENER )
619 PushCommand( p_ext, CMD_PLAYING_CHANGED, i );
623 case EXTENSION_META_CHANGED:
626 p_ext = ( extension_t* ) va_arg( args, extension_t* );
627 PushCommand( p_ext, CMD_UPDATE_META );
631 msg_Warn( p_mgr, "Control '%d' not yet implemented in Extension",
639 int lua_ExtensionActivate( extensions_manager_t *p_mgr, extension_t *p_ext )
641 assert( p_mgr != NULL && p_ext != NULL );
642 return lua_ExecuteFunction( p_mgr, p_ext, "activate", LUA_END );
645 int lua_ExtensionDeactivate( extensions_manager_t *p_mgr, extension_t *p_ext )
647 assert( p_mgr != NULL && p_ext != NULL );
649 if( !p_ext->p_sys->L )
652 // Unset and release input objects
653 if( p_ext->p_sys->p_input )
655 if( p_ext->p_sys->i_capabilities & EXT_META_LISTENER )
658 input_item_t *p_item = input_GetItem( p_ext->p_sys->p_input );
659 vlc_gc_decref( p_item );
661 vlc_object_release( p_ext->p_sys->p_input );
662 p_ext->p_sys->p_input = NULL;
665 int i_ret = lua_ExecuteFunction( p_mgr, p_ext, "deactivate", LUA_END );
667 /* Clear Lua State */
668 lua_close( p_ext->p_sys->L );
669 p_ext->p_sys->L = NULL;
674 int lua_ExtensionWidgetClick( extensions_manager_t *p_mgr,
676 extension_widget_t *p_widget )
678 if( !p_ext->p_sys->L )
681 lua_State *L = GetLuaState( p_mgr, p_ext );
682 lua_pushlightuserdata( L, p_widget );
683 lua_gettable( L, LUA_REGISTRYINDEX );
684 return lua_ExecuteFunction( p_mgr, p_ext, NULL, LUA_END );
689 * Get the list of menu entries from an extension script
692 * @param pppsz_titles Pointer to NULL. All strings must be freed by the caller
693 * @param ppi_ids Pointer to NULL. Must be freed by the caller.
694 * @note This function is allowed to run in the UI thread. This means
695 * that it MUST respond very fast.
696 * @todo Remove the menu() hook and provide a new function vlc.set_menu()
698 static int GetMenuEntries( extensions_manager_t *p_mgr, extension_t *p_ext,
699 char ***pppsz_titles, uint16_t **ppi_ids )
701 assert( *pppsz_titles == NULL );
702 assert( *ppi_ids == NULL );
704 if( !IsActivated( p_mgr, p_ext ) )
706 msg_Dbg( p_mgr, "Can't get menu before activating the extension!" );
710 if( !LockExtension( p_ext ) )
712 /* Dying extension, fail. */
716 int i_ret = VLC_EGENERIC;
717 lua_State *L = GetLuaState( p_mgr, p_ext );
719 if( ( p_ext->p_sys->i_capabilities & EXT_HAS_MENU ) == 0 )
721 msg_Dbg( p_mgr, "can't get a menu from an extension without menu!" );
725 lua_getglobal( L, "menu" );
727 if( !lua_isfunction( L, -1 ) )
729 msg_Warn( p_mgr, "Error while running script %s, "
730 "function menu() not found", p_ext->psz_name );
734 if( lua_pcall( L, 0, 1, 0 ) )
736 msg_Warn( p_mgr, "Error while running script %s, "
737 "function menu(): %s", p_ext->psz_name,
738 lua_tostring( L, lua_gettop( L ) ) );
742 if( lua_gettop( L ) )
744 if( lua_istable( L, -1 ) )
747 size_t i_size = lua_objlen( L, -1 );
748 *pppsz_titles = ( char** ) calloc( i_size+1, sizeof( char* ) );
749 *ppi_ids = ( uint16_t* ) calloc( i_size+1, sizeof( uint16_t ) );
754 while( lua_next( L, -2 ) != 0 )
756 assert( i_idx < i_size );
757 if( (!lua_isstring( L, -1 )) || (!lua_isnumber( L, -2 )) )
759 msg_Warn( p_mgr, "In script %s, an entry in "
760 "the menu table is invalid!", p_ext->psz_name );
763 (*pppsz_titles)[ i_idx ] = strdup( luaL_checkstring( L, -1 ) );
764 (*ppi_ids)[ i_idx ] = (uint16_t) ( luaL_checkinteger( L, -2 ) & 0xFFFF );
771 msg_Warn( p_mgr, "Function menu() in script %s "
772 "did not return a table", p_ext->psz_name );
778 msg_Warn( p_mgr, "Script %s went completely foobar", p_ext->psz_name );
785 UnlockExtension( p_ext );
786 if( i_ret != VLC_SUCCESS )
788 msg_Dbg( p_mgr, "Something went wrong in %s (%s:%d)",
789 __func__, __FILE__, __LINE__ );
794 /* Must be entered with the Lock on Extension */
795 static lua_State* GetLuaState( extensions_manager_t *p_mgr,
807 msg_Err( p_mgr, "Could not create new Lua State" );
810 vlclua_set_this( L, p_mgr );
811 vlclua_extension_set( L, p_ext );
814 luaL_register( L, "vlc", p_reg );
819 /* Load more libraries */
821 luaopen_dialog( L, p_ext );
826 luaopen_playlist( L );
829 luaopen_strings( L );
830 luaopen_variables( L );
835 #if defined(_WIN32) && !VLC_WINSTORE_APP
839 /* Register extension specific functions */
840 lua_getglobal( L, "vlc" );
841 lua_pushcfunction( L, vlclua_extension_deactivate );
842 lua_setfield( L, -2, "deactivate" );
843 lua_pushcfunction( L, vlclua_extension_keep_alive );
844 lua_setfield( L, -2, "keep_alive" );
846 /* Setup the module search path */
847 if( !strncmp( p_ext->psz_name, "zip://", 6 ) )
849 /* Load all required modules manually */
850 lua_register( L, "require", &vlclua_extension_require );
854 if( vlclua_add_modules_path( L, p_ext->psz_name ) )
856 msg_Warn( p_mgr, "Error while setting the module "
857 "search path for %s", p_ext->psz_name );
862 /* Load and run the script(s) */
863 if( vlclua_dofile( VLC_OBJECT( p_mgr ), L, p_ext->psz_name ) )
865 msg_Warn( p_mgr, "Error loading script %s: %s", p_ext->psz_name,
866 lua_tostring( L, lua_gettop( L ) ) );
878 int lua_ExecuteFunction( extensions_manager_t *p_mgr, extension_t *p_ext,
879 const char *psz_function, ... )
882 va_start( args, psz_function );
883 int i_ret = lua_ExecuteFunctionVa( p_mgr, p_ext, psz_function, args );
889 * Execute a function in a Lua script
890 * @param psz_function Name of global function to execute. If NULL, assume
891 * that the function object is already on top of the
893 * @return < 0 in case of failure, >= 0 in case of success
894 * @note It's better to call this function from a dedicated thread
895 * (see extension_thread.c)
897 int lua_ExecuteFunctionVa( extensions_manager_t *p_mgr, extension_t *p_ext,
898 const char *psz_function, va_list args )
900 int i_ret = VLC_SUCCESS;
902 assert( p_mgr != NULL );
903 assert( p_ext != NULL );
905 lua_State *L = GetLuaState( p_mgr, p_ext );
910 lua_getglobal( L, psz_function );
912 if( !lua_isfunction( L, -1 ) )
914 msg_Warn( p_mgr, "Error while running script %s, "
915 "function %s() not found", p_ext->psz_name, psz_function );
919 lua_datatype_e type = LUA_END;
920 while( ( type = va_arg( args, int ) ) != LUA_END )
922 if( type == LUA_NUM )
924 lua_pushnumber( L , ( int ) va_arg( args, int ) );
926 else if( type == LUA_TEXT )
928 lua_pushstring( L , ( char * ) va_arg( args, char* ) );
932 msg_Warn( p_mgr, "Undefined argument type %d to lua function %s"
933 "from script %s", type, psz_function, p_ext->psz_name );
939 // Create watch timer
940 vlc_mutex_lock( &p_ext->p_sys->command_lock );
941 vlc_timer_schedule( p_ext->p_sys->timer, false, WATCH_TIMER_PERIOD, 0 );
942 vlc_mutex_unlock( &p_ext->p_sys->command_lock );
944 // Start actual call to Lua
945 if( lua_pcall( L, i_args, 1, 0 ) )
947 msg_Warn( p_mgr, "Error while running script %s, "
948 "function %s(): %s", p_ext->psz_name, psz_function,
949 lua_tostring( L, lua_gettop( L ) ) );
950 i_ret = VLC_EGENERIC;
953 // Reset watch timer and timestamp
954 vlc_mutex_lock( &p_ext->p_sys->command_lock );
955 if( p_ext->p_sys->progress )
957 dialog_ProgressDestroy( p_ext->p_sys->progress );
958 p_ext->p_sys->progress = NULL;
960 vlc_timer_schedule( p_ext->p_sys->timer, false, 0, 0 );
961 vlc_mutex_unlock( &p_ext->p_sys->command_lock );
963 i_ret |= lua_DialogFlush( L );
970 static inline int TriggerMenu( extension_t *p_ext, int i_id )
972 return PushCommand( p_ext, CMD_TRIGGERMENU, i_id );
975 int lua_ExtensionTriggerMenu( extensions_manager_t *p_mgr,
976 extension_t *p_ext, int id )
978 int i_ret = VLC_SUCCESS;
979 lua_State *L = GetLuaState( p_mgr, p_ext );
984 luaopen_dialog( L, p_ext );
986 lua_getglobal( L, "trigger_menu" );
987 if( !lua_isfunction( L, -1 ) )
989 msg_Warn( p_mgr, "Error while running script %s, "
990 "function trigger_menu() not found", p_ext->psz_name );
994 /* Pass id as unique argument to the function */
995 lua_pushinteger( L, id );
997 // Create watch timer
998 vlc_mutex_lock( &p_ext->p_sys->command_lock );
999 vlc_timer_schedule( p_ext->p_sys->timer, false, WATCH_TIMER_PERIOD, 0 );
1000 vlc_mutex_unlock( &p_ext->p_sys->command_lock );
1002 if( lua_pcall( L, 1, 1, 0 ) != 0 )
1004 msg_Warn( p_mgr, "Error while running script %s, "
1005 "function trigger_menu(): %s", p_ext->psz_name,
1006 lua_tostring( L, lua_gettop( L ) ) );
1007 i_ret = VLC_EGENERIC;
1010 // Reset watch timer and timestamp
1011 vlc_mutex_lock( &p_ext->p_sys->command_lock );
1012 if( p_ext->p_sys->progress )
1014 dialog_ProgressDestroy( p_ext->p_sys->progress );
1015 p_ext->p_sys->progress = NULL;
1017 vlc_timer_schedule( p_ext->p_sys->timer, false, 0, 0 );
1018 vlc_mutex_unlock( &p_ext->p_sys->command_lock );
1020 i_ret |= lua_DialogFlush( L );
1021 if( i_ret < VLC_SUCCESS )
1023 msg_Dbg( p_mgr, "Something went wrong in %s (%s:%d)",
1024 __func__, __FILE__, __LINE__ );
1030 /** Directly trigger an extension, without activating it
1031 * This is NOT multithreaded, and this code runs in the UI thread
1033 * @param p_ext Extension to trigger
1034 * @return Value returned by the lua function "trigger"
1036 static int TriggerExtension( extensions_manager_t *p_mgr,
1037 extension_t *p_ext )
1039 int i_ret = lua_ExecuteFunction( p_mgr, p_ext, "trigger", LUA_END );
1041 /* Close lua state for trigger-only extensions */
1042 if( p_ext->p_sys->L )
1043 lua_close( p_ext->p_sys->L );
1044 p_ext->p_sys->L = NULL;
1049 /** Set extension associated to the current script
1050 * @param L current lua_State
1051 * @param p_ext the extension
1053 void vlclua_extension_set( lua_State *L, extension_t *p_ext )
1055 lua_pushlightuserdata( L, vlclua_extension_set );
1056 lua_pushlightuserdata( L, p_ext );
1057 lua_rawset( L, LUA_REGISTRYINDEX );
1060 /** Retrieve extension associated to the current script
1061 * @param L current lua_State
1062 * @return Extension pointer
1064 extension_t *vlclua_extension_get( lua_State *L )
1066 lua_pushlightuserdata( L, vlclua_extension_set );
1067 lua_rawget( L, LUA_REGISTRYINDEX );
1068 extension_t *p_ext = (extension_t*) lua_topointer( L, -1 );
1073 /** Deactivate an extension by order from the extension itself
1074 * @param L lua_State
1075 * @note This is an asynchronous call. A script calling vlc.deactivate() will
1076 * be executed to the end before the last call to deactivate() is done.
1078 int vlclua_extension_deactivate( lua_State *L )
1080 extension_t *p_ext = vlclua_extension_get( L );
1081 int i_ret = Deactivate( p_ext->p_sys->p_mgr, p_ext );
1082 return ( i_ret == VLC_SUCCESS ) ? 1 : 0;
1085 /** Keep an extension alive. This resets the watch timer to 0
1086 * @param L lua_State
1087 * @note This is the "vlc.keep_alive()" function
1089 int vlclua_extension_keep_alive( lua_State *L )
1091 extension_t *p_ext = vlclua_extension_get( L );
1093 vlc_mutex_lock( &p_ext->p_sys->command_lock );
1094 if( p_ext->p_sys->progress )
1096 dialog_ProgressDestroy( p_ext->p_sys->progress );
1097 p_ext->p_sys->progress = NULL;
1099 vlc_timer_schedule( p_ext->p_sys->timer, false, WATCH_TIMER_PERIOD, 0 );
1100 vlc_mutex_unlock( &p_ext->p_sys->command_lock );
1105 /** Callback for the variable "dialog-event"
1106 * @param p_this Current object owner of the extension and the dialog
1107 * @param psz_var "dialog-event"
1108 * @param oldval Unused
1109 * @param newval Address of the dialog
1110 * @param p_data Unused
1112 static int vlclua_extension_dialog_callback( vlc_object_t *p_this,
1113 char const *psz_var,
1118 /* psz_var == "dialog-event" */
1123 extension_dialog_command_t *command = newval.p_address;
1124 assert( command != NULL );
1125 assert( command->p_dlg != NULL);
1127 extension_t *p_ext = command->p_dlg->p_sys;
1128 assert( p_ext != NULL );
1130 extension_widget_t *p_widget = command->p_data;
1132 switch( command->event )
1134 case EXTENSION_EVENT_CLICK:
1135 assert( p_widget != NULL );
1136 PushCommandUnique( p_ext, CMD_CLICK, p_widget );
1138 case EXTENSION_EVENT_CLOSE:
1139 PushCommandUnique( p_ext, CMD_CLOSE );
1142 msg_Dbg( p_this, "Received unknown UI event %d, discarded",
1150 /** Callback on vlc_InputItemMetaChanged event
1152 static void inputItemMetaChanged( const vlc_event_t *p_event,
1155 assert( p_event && p_event->type == vlc_InputItemMetaChanged );
1157 extension_t *p_ext = ( extension_t* ) data;
1158 assert( p_ext != NULL );
1160 PushCommandUnique( p_ext, CMD_UPDATE_META );
1163 /** Lock this extension. Can fail. */
1164 bool LockExtension( extension_t *p_ext )
1166 vlc_mutex_lock( &p_ext->p_sys->command_lock );
1167 if( p_ext->p_sys->b_exiting )
1169 vlc_mutex_unlock( &p_ext->p_sys->command_lock );
1173 vlc_mutex_lock( &p_ext->p_sys->running_lock );
1174 if( p_ext->p_sys->b_exiting )
1176 vlc_mutex_unlock( &p_ext->p_sys->running_lock );
1177 vlc_mutex_unlock( &p_ext->p_sys->command_lock );
1181 vlc_mutex_unlock( &p_ext->p_sys->command_lock );
1185 /** Unlock this extension. */
1186 void UnlockExtension( extension_t *p_ext )
1188 vlc_mutex_unlock( &p_ext->p_sys->running_lock );
1191 /** Watch timer callback
1192 * The timer expired, Lua may be stuck, ask the user what to do now
1194 static void WatchTimerCallback( void *data )
1196 extension_t *p_ext = data;
1197 extensions_manager_t *p_mgr = p_ext->p_sys->p_mgr;
1200 if( asprintf( &message, _( "Extension '%s' does not respond.\n"
1201 "Do you want to kill it now? " ),
1202 p_ext->psz_title ) == -1 )
1207 vlc_mutex_lock( &p_ext->p_sys->command_lock );
1209 // Do we have a pending Deactivate command?
1210 if( ( p_ext->p_sys->command &&
1211 p_ext->p_sys->command->i_command == CMD_DEACTIVATE )
1212 || ( p_ext->p_sys->command->next
1213 && p_ext->p_sys->command->next->i_command == CMD_DEACTIVATE) )
1215 if( p_ext->p_sys->progress )
1217 dialog_ProgressDestroy( p_ext->p_sys->progress );
1218 p_ext->p_sys->progress = NULL;
1220 vlc_mutex_unlock( &p_ext->p_sys->command_lock );
1221 KillExtension( p_mgr, p_ext );
1225 if( !p_ext->p_sys->progress )
1227 p_ext->p_sys->progress =
1228 dialog_ProgressCreate( p_mgr, _( "Extension not responding!" ),
1231 vlc_timer_schedule( p_ext->p_sys->timer, false, 100000, 0 );
1235 if( dialog_ProgressCancelled( p_ext->p_sys->progress ) )
1237 dialog_ProgressDestroy( p_ext->p_sys->progress );
1238 p_ext->p_sys->progress = NULL;
1239 vlc_mutex_unlock( &p_ext->p_sys->command_lock );
1240 KillExtension( p_mgr, p_ext );
1243 vlc_timer_schedule( p_ext->p_sys->timer, false, 100000, 0 );
1245 vlc_mutex_unlock( &p_ext->p_sys->command_lock );