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 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 );
193 var_DelCallback( p_this, "dialog-event",
194 vlclua_extension_dialog_callback, NULL );
198 * Batch scan all Lua files in folder "extensions"
199 * @param p_mgr This extensions_manager_t object
201 static int ScanExtensions( extensions_manager_t *p_mgr )
204 vlclua_scripts_batch_execute( VLC_OBJECT( p_mgr ),
216 * Dummy Lua function: does nothing
217 * @note This function can be used to replace "require" while scanning for
219 * Even the built-in libraries are not loaded when calling descriptor()
221 static int vlclua_dummy_require( lua_State *L )
228 * Replacement for "require", adding support for packaged extensions
229 * @note Loads modules in the modules/ folder of a package
230 * @note Try first with .luac and then with .lua
232 static int vlclua_extension_require( lua_State *L )
234 const char *psz_module = luaL_checkstring( L, 1 );
235 vlc_object_t *p_this = vlclua_get_this( L );
236 extension_t *p_ext = vlclua_extension_get( L );
237 msg_Dbg( p_this, "loading module '%s' from extension package",
239 char *psz_fullpath, *psz_package, *sep;
240 psz_package = strdup( p_ext->psz_name );
241 sep = strrchr( psz_package, '/' );
245 return luaL_error( L, "could not find package name" );
248 if( -1 == asprintf( &psz_fullpath,
249 "%s/modules/%s.luac", psz_package, psz_module ) )
254 int i_ret = vlclua_dofile( p_this, L, psz_fullpath );
257 // Remove trailing 'c' --> try with .lua script
258 psz_fullpath[ strlen( psz_fullpath ) - 1 ] = '\0';
259 i_ret = vlclua_dofile( p_this, L, psz_fullpath );
261 free( psz_fullpath );
265 return luaL_error( L, "unable to load module '%s' from package",
272 * Batch scan all Lua files in folder "extensions": callback
273 * @param p_this This extensions_manager_t object
274 * @param psz_filename Name of the script to run
275 * @param L Lua State, common to all scripts here
276 * @param dummy: unused
278 int ScanLuaCallback( vlc_object_t *p_this, const char *psz_filename,
282 extensions_manager_t *p_mgr = ( extensions_manager_t* ) p_this;
285 msg_Dbg( p_mgr, "Scanning Lua script %s", psz_filename );
287 /* Experimental: read .vle packages (Zip archives) */
288 char *psz_script = NULL;
289 int i_flen = strlen( psz_filename );
290 if( !strncasecmp( psz_filename + i_flen - 4, ".vle", 4 ) )
292 msg_Dbg( p_this, "reading Lua script in a zip archive" );
293 psz_script = calloc( 1, i_flen + 6 + 12 + 1 );
296 strcpy( psz_script, "zip://" );
297 strncat( psz_script, psz_filename, i_flen + 19 );
298 strncat( psz_script, "!/script.lua", i_flen + 19 );
302 psz_script = strdup( psz_filename );
307 /* Create new script descriptor */
308 extension_t *p_ext = ( extension_t* ) calloc( 1, sizeof( extension_t ) );
315 p_ext->psz_name = psz_script;
316 p_ext->p_sys = (extension_sys_t*) calloc( 1, sizeof( extension_sys_t ) );
317 if( !p_ext->p_sys || !p_ext->psz_name )
319 free( p_ext->psz_name );
320 free( p_ext->p_sys );
324 p_ext->p_sys->p_mgr = p_mgr;
327 if( vlc_timer_create( &p_ext->p_sys->timer, WatchTimerCallback, p_ext ) )
329 free( p_ext->psz_name );
330 free( p_ext->p_sys );
335 /* Mutexes and conditions */
336 vlc_mutex_init( &p_ext->p_sys->command_lock );
337 vlc_mutex_init( &p_ext->p_sys->running_lock );
338 vlc_cond_init( &p_ext->p_sys->wait );
340 /* Prepare Lua state */
341 lua_State *L = luaL_newstate();
342 lua_register( L, "require", &vlclua_dummy_require );
345 if( vlclua_dofile( p_this, L, psz_script ) ) // luaL_dofile
347 msg_Warn( p_mgr, "Error loading script %s: %s", psz_script,
348 lua_tostring( L, lua_gettop( L ) ) );
353 /* Scan script for capabilities */
354 lua_getglobal( L, "descriptor" );
356 if( !lua_isfunction( L, -1 ) )
358 msg_Warn( p_mgr, "Error while running script %s, "
359 "function descriptor() not found", psz_script );
363 if( lua_pcall( L, 0, 1, 0 ) )
365 msg_Warn( p_mgr, "Error while running script %s, "
366 "function descriptor(): %s", psz_script,
367 lua_tostring( L, lua_gettop( L ) ) );
371 if( lua_gettop( L ) )
373 if( lua_istable( L, -1 ) )
376 lua_getfield( L, -1, "capabilities" );
377 if( lua_istable( L, -1 ) )
380 while( lua_next( L, -2 ) != 0 )
382 /* Key is at index -2 and value at index -1. Discard key */
383 const char *psz_cap = luaL_checkstring( L, -1 );
386 /* Find this capability's flag */
387 for( const char *iter = *ppsz_capabilities;
389 iter = ppsz_capabilities[ ++i_cap ])
391 if( !strcmp( iter, psz_cap ) )
394 p_ext->p_sys->i_capabilities |= 1 << i_cap;
401 msg_Warn( p_mgr, "Extension capability '%s' unknown in"
402 " script %s", psz_cap, psz_script );
404 /* Removes 'value'; keeps 'key' for next iteration */
410 msg_Warn( p_mgr, "In script %s, function descriptor() "
411 "did not return a table of capabilities.",
417 lua_getfield( L, -1, "title" );
418 if( lua_isstring( L, -1 ) )
420 p_ext->psz_title = strdup( luaL_checkstring( L, -1 ) );
424 msg_Dbg( p_mgr, "In script %s, function descriptor() "
425 "did not return a string as title.",
427 p_ext->psz_title = strdup( psz_script );
432 lua_getfield( L, -1, "author" );
433 p_ext->psz_author = luaL_strdupornull( L, -1 );
436 /* Get description */
437 lua_getfield( L, -1, "description" );
438 p_ext->psz_description = luaL_strdupornull( L, -1 );
441 /* Get short description */
442 lua_getfield( L, -1, "shortdesc" );
443 p_ext->psz_shortdescription = luaL_strdupornull( L, -1 );
447 lua_getfield( L, -1, "url" );
448 p_ext->psz_url = luaL_strdupornull( L, -1 );
452 lua_getfield( L, -1, "version" );
453 p_ext->psz_version = luaL_strdupornull( L, -1 );
457 lua_getfield( L, -1, "icon" );
458 if( !lua_isnil( L, -1 ) && lua_isstring( L, -1 ) )
460 int len = lua_strlen( L, -1 );
461 p_ext->p_icondata = malloc( len );
462 if( p_ext->p_icondata )
464 p_ext->i_icondata_size = len;
465 memcpy( p_ext->p_icondata, lua_tostring( L, -1 ), len );
472 msg_Warn( p_mgr, "In script %s, function descriptor() "
473 "did not return a table!", psz_script );
479 msg_Err( p_mgr, "Script %s went completely foobar", psz_script );
483 msg_Dbg( p_mgr, "Script %s has the following capability flags: 0x%x",
484 psz_script, p_ext->p_sys->i_capabilities );
491 free( p_ext->psz_name );
492 free( p_ext->psz_title );
493 free( p_ext->psz_url );
494 free( p_ext->psz_author );
495 free( p_ext->psz_description );
496 free( p_ext->psz_shortdescription );
497 free( p_ext->psz_version );
498 vlc_mutex_destroy( &p_ext->p_sys->command_lock );
499 vlc_mutex_destroy( &p_ext->p_sys->running_lock );
500 vlc_cond_destroy( &p_ext->p_sys->wait );
501 free( p_ext->p_sys );
506 /* Add the extension to the list of known extensions */
507 ARRAY_APPEND( p_mgr->extensions, p_ext );
510 /* Continue batch execution */
514 static int Control( extensions_manager_t *p_mgr, int i_control, va_list args )
516 extension_t *p_ext = NULL;
518 uint16_t **ppus = NULL;
519 char ***pppsz = NULL;
524 case EXTENSION_ACTIVATE:
525 p_ext = ( extension_t* ) va_arg( args, extension_t* );
526 return Activate( p_mgr, p_ext );
528 case EXTENSION_DEACTIVATE:
529 p_ext = ( extension_t* ) va_arg( args, extension_t* );
530 return Deactivate( p_mgr, p_ext );
532 case EXTENSION_IS_ACTIVATED:
533 p_ext = ( extension_t* ) va_arg( args, extension_t* );
534 pb = ( bool* ) va_arg( args, bool* );
535 *pb = IsActivated( p_mgr, p_ext );
538 case EXTENSION_HAS_MENU:
539 p_ext = ( extension_t* ) va_arg( args, extension_t* );
540 pb = ( bool* ) va_arg( args, bool* );
541 *pb = ( p_ext->p_sys->i_capabilities & EXT_HAS_MENU ) ? 1 : 0;
544 case EXTENSION_GET_MENU:
545 p_ext = ( extension_t* ) va_arg( args, extension_t* );
546 pppsz = ( char*** ) va_arg( args, char*** );
547 ppus = ( uint16_t** ) va_arg( args, uint16_t** );
548 return GetMenuEntries( p_mgr, p_ext, pppsz, ppus );
550 case EXTENSION_TRIGGER_ONLY:
551 p_ext = ( extension_t* ) va_arg( args, extension_t* );
552 pb = ( bool* ) va_arg( args, bool* );
553 *pb = ( p_ext->p_sys->i_capabilities & EXT_TRIGGER_ONLY ) ? 1 : 0;
556 case EXTENSION_TRIGGER:
557 p_ext = ( extension_t* ) va_arg( args, extension_t* );
558 return TriggerExtension( p_mgr, p_ext );
560 case EXTENSION_TRIGGER_MENU:
561 p_ext = ( extension_t* ) va_arg( args, extension_t* );
562 // GCC: 'uint16_t' is promoted to 'int' when passed through '...'
563 i = ( int ) va_arg( args, int );
564 return TriggerMenu( p_ext, i );
566 case EXTENSION_SET_INPUT:
568 p_ext = ( extension_t* ) va_arg( args, extension_t* );
569 input_thread_t *p_input = va_arg( args, struct input_thread_t * );
571 if( !LockExtension( p_ext ) )
575 input_thread_t *old = p_ext->p_sys->p_input;
576 input_item_t *p_item;
579 // Untrack meta fetched events
580 if( p_ext->p_sys->i_capabilities & EXT_META_LISTENER )
582 p_item = input_GetItem( old );
583 vlc_event_detach( &p_item->event_manager,
584 vlc_InputItemMetaChanged,
585 inputItemMetaChanged,
587 vlc_gc_decref( p_item );
589 vlc_object_release( old );
592 p_ext->p_sys->p_input = p_input ? vlc_object_hold( p_input )
595 // Tell the script the input changed
596 if( p_ext->p_sys->i_capabilities & EXT_INPUT_LISTENER )
598 PushCommandUnique( p_ext, CMD_SET_INPUT );
601 // Track meta fetched events
602 if( p_ext->p_sys->p_input &&
603 p_ext->p_sys->i_capabilities & EXT_META_LISTENER )
605 p_item = input_GetItem( p_ext->p_sys->p_input );
606 vlc_gc_incref( p_item );
607 vlc_event_attach( &p_item->event_manager,
608 vlc_InputItemMetaChanged,
609 inputItemMetaChanged,
613 UnlockExtension( p_ext );
616 case EXTENSION_PLAYING_CHANGED:
619 p_ext = ( extension_t* ) va_arg( args, extension_t* );
620 assert( p_ext->psz_name != NULL );
621 i = ( int ) va_arg( args, int );
622 if( p_ext->p_sys->i_capabilities & EXT_PLAYING_LISTENER )
624 PushCommand( p_ext, CMD_PLAYING_CHANGED, i );
628 case EXTENSION_META_CHANGED:
631 p_ext = ( extension_t* ) va_arg( args, extension_t* );
632 PushCommand( p_ext, CMD_UPDATE_META );
636 msg_Warn( p_mgr, "Control '%d' not yet implemented in Extension",
644 int lua_ExtensionActivate( extensions_manager_t *p_mgr, extension_t *p_ext )
646 assert( p_mgr != NULL && p_ext != NULL );
647 return lua_ExecuteFunction( p_mgr, p_ext, "activate", LUA_END );
650 int lua_ExtensionDeactivate( extensions_manager_t *p_mgr, extension_t *p_ext )
652 assert( p_mgr != NULL && p_ext != NULL );
654 if( !p_ext->p_sys->L )
657 // Unset and release input objects
658 if( p_ext->p_sys->p_input )
660 if( p_ext->p_sys->i_capabilities & EXT_META_LISTENER )
663 input_item_t *p_item = input_GetItem( p_ext->p_sys->p_input );
664 vlc_gc_decref( p_item );
666 vlc_object_release( p_ext->p_sys->p_input );
667 p_ext->p_sys->p_input = NULL;
670 int i_ret = lua_ExecuteFunction( p_mgr, p_ext, "deactivate", LUA_END );
672 /* Clear Lua State */
673 lua_close( p_ext->p_sys->L );
674 p_ext->p_sys->L = NULL;
679 int lua_ExtensionWidgetClick( extensions_manager_t *p_mgr,
681 extension_widget_t *p_widget )
683 if( !p_ext->p_sys->L )
686 lua_State *L = GetLuaState( p_mgr, p_ext );
687 lua_pushlightuserdata( L, p_widget );
688 lua_gettable( L, LUA_REGISTRYINDEX );
689 return lua_ExecuteFunction( p_mgr, p_ext, NULL, LUA_END );
694 * Get the list of menu entries from an extension script
697 * @param pppsz_titles Pointer to NULL. All strings must be freed by the caller
698 * @param ppi_ids Pointer to NULL. Must be freed by the caller.
699 * @note This function is allowed to run in the UI thread. This means
700 * that it MUST respond very fast.
701 * @todo Remove the menu() hook and provide a new function vlc.set_menu()
703 static int GetMenuEntries( extensions_manager_t *p_mgr, extension_t *p_ext,
704 char ***pppsz_titles, uint16_t **ppi_ids )
706 assert( *pppsz_titles == NULL );
707 assert( *ppi_ids == NULL );
709 if( !IsActivated( p_mgr, p_ext ) )
711 msg_Dbg( p_mgr, "Can't get menu before activating the extension!" );
715 if( !LockExtension( p_ext ) )
717 /* Dying extension, fail. */
721 int i_ret = VLC_EGENERIC;
722 lua_State *L = GetLuaState( p_mgr, p_ext );
724 if( ( p_ext->p_sys->i_capabilities & EXT_HAS_MENU ) == 0 )
726 msg_Dbg( p_mgr, "can't get a menu from an extension without menu!" );
730 lua_getglobal( L, "menu" );
732 if( !lua_isfunction( L, -1 ) )
734 msg_Warn( p_mgr, "Error while running script %s, "
735 "function menu() not found", p_ext->psz_name );
739 if( lua_pcall( L, 0, 1, 0 ) )
741 msg_Warn( p_mgr, "Error while running script %s, "
742 "function menu(): %s", p_ext->psz_name,
743 lua_tostring( L, lua_gettop( L ) ) );
747 if( lua_gettop( L ) )
749 if( lua_istable( L, -1 ) )
752 size_t i_size = lua_objlen( L, -1 );
753 *pppsz_titles = ( char** ) calloc( i_size+1, sizeof( char* ) );
754 *ppi_ids = ( uint16_t* ) calloc( i_size+1, sizeof( uint16_t ) );
759 while( lua_next( L, -2 ) != 0 )
761 assert( i_idx < i_size );
762 if( (!lua_isstring( L, -1 )) || (!lua_isnumber( L, -2 )) )
764 msg_Warn( p_mgr, "In script %s, an entry in "
765 "the menu table is invalid!", p_ext->psz_name );
768 (*pppsz_titles)[ i_idx ] = strdup( luaL_checkstring( L, -1 ) );
769 (*ppi_ids)[ i_idx ] = (uint16_t) ( luaL_checkinteger( L, -2 ) & 0xFFFF );
776 msg_Warn( p_mgr, "Function menu() in script %s "
777 "did not return a table", p_ext->psz_name );
783 msg_Warn( p_mgr, "Script %s went completely foobar", p_ext->psz_name );
790 UnlockExtension( p_ext );
791 if( i_ret != VLC_SUCCESS )
793 msg_Dbg( p_mgr, "Something went wrong in %s (%s:%d)",
794 __func__, __FILE__, __LINE__ );
799 /* Must be entered with the Lock on Extension */
800 static lua_State* GetLuaState( extensions_manager_t *p_mgr,
812 msg_Err( p_mgr, "Could not create new Lua State" );
815 vlclua_set_this( L, p_mgr );
816 vlclua_extension_set( L, p_ext );
819 luaL_register( L, "vlc", p_reg );
824 /* Load more libraries */
827 luaopen_dialog( L, p_ext );
834 luaopen_playlist( L );
837 luaopen_strings( L );
838 luaopen_variables( L );
844 /* Register extension specific functions */
845 lua_getglobal( L, "vlc" );
846 lua_pushcfunction( L, vlclua_extension_deactivate );
847 lua_setfield( L, -2, "deactivate" );
848 lua_pushcfunction( L, vlclua_extension_keep_alive );
849 lua_setfield( L, -2, "keep_alive" );
851 /* Setup the module search path */
852 if( !strncmp( p_ext->psz_name, "zip://", 6 ) )
854 /* Load all required modules manually */
855 lua_register( L, "require", &vlclua_extension_require );
859 if( vlclua_add_modules_path( p_mgr, L, p_ext->psz_name ) )
861 msg_Warn( p_mgr, "Error while setting the module "
862 "search path for %s", p_ext->psz_name );
867 /* Load and run the script(s) */
868 if( vlclua_dofile( VLC_OBJECT( p_mgr ), L, p_ext->psz_name ) )
870 msg_Warn( p_mgr, "Error loading script %s: %s", p_ext->psz_name,
871 lua_tostring( L, lua_gettop( L ) ) );
883 int lua_ExecuteFunction( extensions_manager_t *p_mgr, extension_t *p_ext,
884 const char *psz_function, ... )
887 va_start( args, psz_function );
888 int i_ret = lua_ExecuteFunctionVa( p_mgr, p_ext, psz_function, args );
894 * Execute a function in a Lua script
895 * @param psz_function Name of global function to execute. If NULL, assume
896 * that the function object is already on top of the
898 * @return < 0 in case of failure, >= 0 in case of success
899 * @note It's better to call this function from a dedicated thread
900 * (see extension_thread.c)
902 int lua_ExecuteFunctionVa( extensions_manager_t *p_mgr, extension_t *p_ext,
903 const char *psz_function, va_list args )
905 int i_ret = VLC_SUCCESS;
907 assert( p_mgr != NULL );
908 assert( p_ext != NULL );
910 lua_State *L = GetLuaState( p_mgr, p_ext );
915 lua_getglobal( L, psz_function );
917 if( !lua_isfunction( L, -1 ) )
919 msg_Warn( p_mgr, "Error while running script %s, "
920 "function %s() not found", p_ext->psz_name, psz_function );
924 lua_datatype_e type = LUA_END;
925 while( ( type = va_arg( args, int ) ) != LUA_END )
927 if( type == LUA_NUM )
929 lua_pushnumber( L , ( int ) va_arg( args, int ) );
931 else if( type == LUA_TEXT )
933 lua_pushstring( L , ( char * ) va_arg( args, char* ) );
937 msg_Warn( p_mgr, "Undefined argument type %d to lua function %s"
938 "from script %s", type, psz_function, p_ext->psz_name );
944 // Create watch timer
945 vlc_mutex_lock( &p_ext->p_sys->command_lock );
946 vlc_timer_schedule( p_ext->p_sys->timer, false, WATCH_TIMER_PERIOD, 0 );
947 vlc_mutex_unlock( &p_ext->p_sys->command_lock );
949 // Start actual call to Lua
950 if( lua_pcall( L, i_args, 1, 0 ) )
952 msg_Warn( p_mgr, "Error while running script %s, "
953 "function %s(): %s", p_ext->psz_name, psz_function,
954 lua_tostring( L, lua_gettop( L ) ) );
955 i_ret = VLC_EGENERIC;
958 // Reset watch timer and timestamp
959 vlc_mutex_lock( &p_ext->p_sys->command_lock );
960 if( p_ext->p_sys->progress )
962 dialog_ProgressDestroy( p_ext->p_sys->progress );
963 p_ext->p_sys->progress = NULL;
965 vlc_timer_schedule( p_ext->p_sys->timer, false, 0, 0 );
966 vlc_mutex_unlock( &p_ext->p_sys->command_lock );
968 i_ret |= lua_DialogFlush( L );
975 static inline int TriggerMenu( extension_t *p_ext, int i_id )
977 return PushCommand( p_ext, CMD_TRIGGERMENU, i_id );
980 int lua_ExtensionTriggerMenu( extensions_manager_t *p_mgr,
981 extension_t *p_ext, int id )
983 int i_ret = VLC_SUCCESS;
984 lua_State *L = GetLuaState( p_mgr, p_ext );
989 luaopen_dialog( L, p_ext );
991 lua_getglobal( L, "trigger_menu" );
992 if( !lua_isfunction( L, -1 ) )
994 msg_Warn( p_mgr, "Error while running script %s, "
995 "function trigger_menu() not found", p_ext->psz_name );
999 /* Pass id as unique argument to the function */
1000 lua_pushinteger( L, id );
1002 // Create watch timer
1003 vlc_mutex_lock( &p_ext->p_sys->command_lock );
1004 vlc_timer_schedule( p_ext->p_sys->timer, false, WATCH_TIMER_PERIOD, 0 );
1005 vlc_mutex_unlock( &p_ext->p_sys->command_lock );
1007 if( lua_pcall( L, 1, 1, 0 ) != 0 )
1009 msg_Warn( p_mgr, "Error while running script %s, "
1010 "function trigger_menu(): %s", p_ext->psz_name,
1011 lua_tostring( L, lua_gettop( L ) ) );
1012 i_ret = VLC_EGENERIC;
1015 // Reset watch timer and timestamp
1016 vlc_mutex_lock( &p_ext->p_sys->command_lock );
1017 if( p_ext->p_sys->progress )
1019 dialog_ProgressDestroy( p_ext->p_sys->progress );
1020 p_ext->p_sys->progress = NULL;
1022 vlc_timer_schedule( p_ext->p_sys->timer, false, 0, 0 );
1023 vlc_mutex_unlock( &p_ext->p_sys->command_lock );
1025 i_ret |= lua_DialogFlush( L );
1026 if( i_ret < VLC_SUCCESS )
1028 msg_Dbg( p_mgr, "Something went wrong in %s (%s:%d)",
1029 __func__, __FILE__, __LINE__ );
1035 /** Directly trigger an extension, without activating it
1036 * This is NOT multithreaded, and this code runs in the UI thread
1038 * @param p_ext Extension to trigger
1039 * @return Value returned by the lua function "trigger"
1041 static int TriggerExtension( extensions_manager_t *p_mgr,
1042 extension_t *p_ext )
1044 int i_ret = lua_ExecuteFunction( p_mgr, p_ext, "trigger", LUA_END );
1046 /* Close lua state for trigger-only extensions */
1047 if( p_ext->p_sys->L )
1048 lua_close( p_ext->p_sys->L );
1049 p_ext->p_sys->L = NULL;
1054 /** Set extension associated to the current script
1055 * @param L current lua_State
1056 * @param p_ext the extension
1058 void vlclua_extension_set( lua_State *L, extension_t *p_ext )
1060 lua_pushlightuserdata( L, vlclua_extension_set );
1061 lua_pushlightuserdata( L, p_ext );
1062 lua_rawset( L, LUA_REGISTRYINDEX );
1065 /** Retrieve extension associated to the current script
1066 * @param L current lua_State
1067 * @return Extension pointer
1069 extension_t *vlclua_extension_get( lua_State *L )
1071 lua_pushlightuserdata( L, vlclua_extension_set );
1072 lua_rawget( L, LUA_REGISTRYINDEX );
1073 extension_t *p_ext = (extension_t*) lua_topointer( L, -1 );
1078 /** Deactivate an extension by order from the extension itself
1079 * @param L lua_State
1080 * @note This is an asynchronous call. A script calling vlc.deactivate() will
1081 * be executed to the end before the last call to deactivate() is done.
1083 int vlclua_extension_deactivate( lua_State *L )
1085 extension_t *p_ext = vlclua_extension_get( L );
1086 int i_ret = Deactivate( p_ext->p_sys->p_mgr, p_ext );
1087 return ( i_ret == VLC_SUCCESS ) ? 1 : 0;
1090 /** Keep an extension alive. This resets the watch timer to 0
1091 * @param L lua_State
1092 * @note This is the "vlc.keep_alive()" function
1094 int vlclua_extension_keep_alive( lua_State *L )
1096 extension_t *p_ext = vlclua_extension_get( L );
1098 vlc_mutex_lock( &p_ext->p_sys->command_lock );
1099 if( p_ext->p_sys->progress )
1101 dialog_ProgressDestroy( p_ext->p_sys->progress );
1102 p_ext->p_sys->progress = NULL;
1104 vlc_timer_schedule( p_ext->p_sys->timer, false, WATCH_TIMER_PERIOD, 0 );
1105 vlc_mutex_unlock( &p_ext->p_sys->command_lock );
1110 /** Callback for the variable "dialog-event"
1111 * @param p_this Current object owner of the extension and the dialog
1112 * @param psz_var "dialog-event"
1113 * @param oldval Unused
1114 * @param newval Address of the dialog
1115 * @param p_data Unused
1117 static int vlclua_extension_dialog_callback( vlc_object_t *p_this,
1118 char const *psz_var,
1123 /* psz_var == "dialog-event" */
1128 extension_dialog_command_t *command = newval.p_address;
1129 assert( command != NULL );
1130 assert( command->p_dlg != NULL);
1132 extension_t *p_ext = command->p_dlg->p_sys;
1133 assert( p_ext != NULL );
1135 extension_widget_t *p_widget = command->p_data;
1137 switch( command->event )
1139 case EXTENSION_EVENT_CLICK:
1140 assert( p_widget != NULL );
1141 PushCommandUnique( p_ext, CMD_CLICK, p_widget );
1143 case EXTENSION_EVENT_CLOSE:
1144 PushCommandUnique( p_ext, CMD_CLOSE );
1147 msg_Dbg( p_this, "Received unknown UI event %d, discarded",
1155 /** Callback on vlc_InputItemMetaChanged event
1157 static void inputItemMetaChanged( const vlc_event_t *p_event,
1160 assert( p_event && p_event->type == vlc_InputItemMetaChanged );
1162 extension_t *p_ext = ( extension_t* ) data;
1163 assert( p_ext != NULL );
1165 PushCommandUnique( p_ext, CMD_UPDATE_META );
1168 /** Lock this extension. Can fail. */
1169 bool LockExtension( extension_t *p_ext )
1171 vlc_mutex_lock( &p_ext->p_sys->command_lock );
1172 if( p_ext->p_sys->b_exiting )
1174 vlc_mutex_unlock( &p_ext->p_sys->command_lock );
1178 vlc_mutex_lock( &p_ext->p_sys->running_lock );
1179 if( p_ext->p_sys->b_exiting )
1181 vlc_mutex_unlock( &p_ext->p_sys->running_lock );
1182 vlc_mutex_unlock( &p_ext->p_sys->command_lock );
1186 vlc_mutex_unlock( &p_ext->p_sys->command_lock );
1190 /** Unlock this extension. */
1191 void UnlockExtension( extension_t *p_ext )
1193 vlc_mutex_unlock( &p_ext->p_sys->running_lock );
1196 /** Watch timer callback
1197 * The timer expired, Lua may be stuck, ask the user what to do now
1199 static void WatchTimerCallback( void *data )
1201 extension_t *p_ext = data;
1202 extensions_manager_t *p_mgr = p_ext->p_sys->p_mgr;
1205 if( asprintf( &message, _( "Extension '%s' does not respond.\n"
1206 "Do you want to kill it now? " ),
1207 p_ext->psz_title ) == -1 )
1212 vlc_mutex_lock( &p_ext->p_sys->command_lock );
1214 // Do we have a pending Deactivate command?
1215 if( ( p_ext->p_sys->command &&
1216 p_ext->p_sys->command->i_command == CMD_DEACTIVATE )
1217 || ( p_ext->p_sys->command->next
1218 && p_ext->p_sys->command->next->i_command == CMD_DEACTIVATE) )
1220 if( p_ext->p_sys->progress )
1222 dialog_ProgressDestroy( p_ext->p_sys->progress );
1223 p_ext->p_sys->progress = NULL;
1225 vlc_mutex_unlock( &p_ext->p_sys->command_lock );
1226 KillExtension( p_mgr, p_ext );
1230 if( !p_ext->p_sys->progress )
1232 p_ext->p_sys->progress =
1233 dialog_ProgressCreate( p_mgr, _( "Extension not responding!" ),
1236 vlc_timer_schedule( p_ext->p_sys->timer, false, 100000, 0 );
1240 if( dialog_ProgressCancelled( p_ext->p_sys->progress ) )
1242 dialog_ProgressDestroy( p_ext->p_sys->progress );
1243 p_ext->p_sys->progress = NULL;
1244 vlc_mutex_unlock( &p_ext->p_sys->command_lock );
1245 KillExtension( p_mgr, p_ext );
1248 vlc_timer_schedule( p_ext->p_sys->timer, false, 100000, 0 );
1250 vlc_mutex_unlock( &p_ext->p_sys->command_lock );