1 /*****************************************************************************
2 * extension.c: Lua Extensions (meta data, web information, ...)
3 *****************************************************************************
4 * Copyright (C) 2009-2010 VideoLAN and authors
7 * Authors: Jean-Philippe André < jpeg # videolan.org >
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
26 #include "extension.h"
29 #include <vlc_input.h>
30 #include <vlc_events.h>
32 /* Functions to register */
33 static const luaL_Reg p_reg[] =
39 * Extensions capabilities
40 * Note: #define and ppsz_capabilities must be in sync
42 #define EXT_HAS_MENU (1 << 0) ///< Hook: menu
43 #define EXT_TRIGGER_ONLY (1 << 1) ///< Hook: trigger. Not activable
44 #define EXT_INPUT_LISTENER (1 << 2) ///< Hook: input_changed
45 #define EXT_META_LISTENER (1 << 3) ///< Hook: meta_changed
46 #define EXT_PLAYING_LISTENER (1 << 4) ///< Hook: status_changed
48 const char* const ppsz_capabilities[] = {
57 static int ScanExtensions( extensions_manager_t *p_this );
58 static int ScanLuaCallback( vlc_object_t *p_this, const char *psz_script,
60 static int Control( extensions_manager_t *, int, va_list );
61 static int GetMenuEntries( extensions_manager_t *p_mgr, extension_t *p_ext,
62 char ***pppsz_titles, uint16_t **ppi_ids );
63 static lua_State* GetLuaState( extensions_manager_t *p_mgr,
65 static int TriggerMenu( extension_t *p_ext, int id );
66 static int TriggerExtension( extensions_manager_t *p_mgr,
69 int vlclua_extension_deactivate( lua_State *L );
72 static int vlclua_extension_dialog_callback( vlc_object_t *p_this,
78 /* Input item callback: vlc_InputItemMetaChanged */
79 static void inputItemMetaChanged( const vlc_event_t *p_event,
86 int Open_Extension( vlc_object_t *p_this )
88 msg_Dbg( p_this, "Opening EXPERIMENTAL Lua Extension module" );
90 extensions_manager_t *p_mgr = ( extensions_manager_t* ) p_this;
92 p_mgr->pf_control = Control;
94 extensions_manager_sys_t *p_sys = ( extensions_manager_sys_t* )
95 calloc( 1, sizeof( extensions_manager_sys_t ) );
96 if( !p_sys ) return VLC_ENOMEM;
99 ARRAY_INIT( p_sys->activated_extensions );
100 ARRAY_INIT( p_mgr->extensions );
101 vlc_mutex_init( &p_mgr->lock );
102 vlc_mutex_init( &p_mgr->p_sys->lock );
104 /* Scan available Lua Extensions */
105 if( ScanExtensions( p_mgr ) != VLC_SUCCESS )
107 msg_Err( p_mgr, "Can't load extensions modules" );
111 // Create the dialog-event variable
112 var_Create( p_this, "dialog-event", VLC_VAR_ADDRESS );
113 var_AddCallback( p_this, "dialog-event",
114 vlclua_extension_dialog_callback, NULL );
120 * Module unload function
122 void Close_Extension( vlc_object_t *p_this )
124 extensions_manager_t *p_mgr = ( extensions_manager_t* ) p_this;
125 msg_Dbg( p_mgr, "Deactivating all loaded extensions" );
127 vlc_mutex_lock( &p_mgr->lock );
128 p_mgr->p_sys->b_killed = true;
129 vlc_mutex_unlock( &p_mgr->lock );
131 var_Destroy( p_mgr, "dialog-event" );
133 extension_t *p_ext = NULL;
134 FOREACH_ARRAY( p_ext, p_mgr->p_sys->activated_extensions )
137 msg_Dbg( p_mgr, "Deactivating '%s'", p_ext->psz_title );
138 Deactivate( p_mgr, p_ext );
139 WaitForDeactivation( p_ext );
143 msg_Dbg( p_mgr, "All extensions are now deactivated" );
144 ARRAY_RESET( p_mgr->p_sys->activated_extensions );
146 vlc_mutex_destroy( &p_mgr->lock );
147 vlc_mutex_destroy( &p_mgr->p_sys->lock );
148 free( p_mgr->p_sys );
151 /* Free extensions' memory */
152 FOREACH_ARRAY( p_ext, p_mgr->extensions )
156 if( p_ext->p_sys->L )
157 lua_close( p_ext->p_sys->L );
158 free( p_ext->psz_name );
159 free( p_ext->psz_title );
160 free( p_ext->psz_author );
161 free( p_ext->psz_description );
162 free( p_ext->psz_shortdescription );
163 free( p_ext->psz_url );
164 free( p_ext->psz_version );
166 vlc_mutex_destroy( &p_ext->p_sys->running_lock );
167 vlc_mutex_destroy( &p_ext->p_sys->command_lock );
168 vlc_cond_destroy( &p_ext->p_sys->wait );
170 free( p_ext->p_sys );
175 ARRAY_RESET( p_mgr->extensions );
179 * Batch scan all Lua files in folder "extensions"
180 * @param p_mgr This extensions_manager_t object
182 static int ScanExtensions( extensions_manager_t *p_mgr )
185 vlclua_scripts_batch_execute( VLC_OBJECT( p_mgr ),
197 * Dummy Lua function: does nothing
198 * @note This function can be used to replace "require" while scanning for
200 * Even the built-in libraries are not loaded when calling descriptor()
202 static int vlclua_dummy_require( lua_State *L )
209 * Batch scan all Lua files in folder "extensions": callback
210 * @param p_this This extensions_manager_t object
211 * @param psz_script Name of the script to run
212 * @param L Lua State, common to all scripts here
213 * @param dummy: unused
215 int ScanLuaCallback( vlc_object_t *p_this, const char *psz_script,
219 extensions_manager_t *p_mgr = ( extensions_manager_t* ) p_this;
222 msg_Dbg( p_mgr, "Scanning Lua script %s", psz_script );
224 vlc_mutex_lock( &p_mgr->lock );
226 /* Create new script descriptor */
227 extension_t *p_ext = ( extension_t* ) calloc( 1, sizeof( extension_t ) );
230 vlc_mutex_unlock( &p_mgr->lock );
234 p_ext->psz_name = strdup( psz_script );
235 p_ext->p_sys = (extension_sys_t*) calloc( 1, sizeof( extension_sys_t ) );
236 if( !p_ext->p_sys || !p_ext->psz_name )
238 free( p_ext->psz_name );
239 free( p_ext->p_sys );
241 vlc_mutex_unlock( &p_mgr->lock );
244 p_ext->p_sys->p_mgr = p_mgr;
246 /* Mutexes and conditions */
247 vlc_mutex_init( &p_ext->p_sys->command_lock );
248 vlc_mutex_init( &p_ext->p_sys->running_lock );
249 vlc_cond_init( &p_ext->p_sys->wait );
251 /* Prepare Lua state */
252 lua_State *L = luaL_newstate();
253 lua_register( L, "require", &vlclua_dummy_require );
256 if( luaL_dofile( L, psz_script ) )
258 msg_Warn( p_mgr, "Error loading script %s: %s", psz_script,
259 lua_tostring( L, lua_gettop( L ) ) );
264 /* Scan script for capabilities */
265 lua_getglobal( L, "descriptor" );
267 if( !lua_isfunction( L, -1 ) )
269 msg_Warn( p_mgr, "Error while running script %s, "
270 "function descriptor() not found", psz_script );
274 if( lua_pcall( L, 0, 1, 0 ) )
276 msg_Warn( p_mgr, "Error while running script %s, "
277 "function descriptor(): %s", psz_script,
278 lua_tostring( L, lua_gettop( L ) ) );
282 if( lua_gettop( L ) )
284 if( lua_istable( L, -1 ) )
287 lua_getfield( L, -1, "capabilities" );
288 if( lua_istable( L, -1 ) )
291 while( lua_next( L, -2 ) != 0 )
293 /* Key is at index -2 and value at index -1. Discard key */
294 const char *psz_cap = luaL_checkstring( L, -1 );
297 /* Find this capability's flag */
298 for( const char *iter = *ppsz_capabilities;
300 iter = ppsz_capabilities[ ++i_cap ])
302 if( !strcmp( iter, psz_cap ) )
305 p_ext->p_sys->i_capabilities |= 1 << i_cap;
312 msg_Warn( p_mgr, "Extension capability '%s' unknown in"
313 " script %s", psz_cap, psz_script );
315 /* Removes 'value'; keeps 'key' for next iteration */
321 msg_Warn( p_mgr, "In script %s, function descriptor() "
322 "did not return a table of capabilities.",
328 lua_getfield( L, -1, "title" );
329 if( lua_isstring( L, -1 ) )
331 p_ext->psz_title = strdup( luaL_checkstring( L, -1 ) );
335 msg_Dbg( p_mgr, "In script %s, function descriptor() "
336 "did not return a string as title.",
338 p_ext->psz_title = strdup( psz_script );
343 lua_getfield( L, -1, "author" );
344 p_ext->psz_author = luaL_strdupornull( L, -1 );
347 /* Get description */
348 lua_getfield( L, -1, "description" );
349 p_ext->psz_description = luaL_strdupornull( L, -1 );
352 /* Get short description */
353 lua_getfield( L, -1, "shortdesc" );
354 p_ext->psz_shortdescription = luaL_strdupornull( L, -1 );
358 lua_getfield( L, -1, "url" );
359 p_ext->psz_url = luaL_strdupornull( L, -1 );
363 lua_getfield( L, -1, "version" );
364 p_ext->psz_version = luaL_strdupornull( L, -1 );
369 msg_Warn( p_mgr, "In script %s, function descriptor() "
370 "did not return a table!", psz_script );
376 msg_Err( p_mgr, "Script %s went completely foobar", psz_script );
380 msg_Dbg( p_mgr, "Script %s has the following capability flags: 0x%x",
381 psz_script, p_ext->p_sys->i_capabilities );
388 free( p_ext->psz_name );
389 free( p_ext->psz_title );
390 free( p_ext->psz_url );
391 free( p_ext->psz_author );
392 free( p_ext->psz_description );
393 free( p_ext->psz_shortdescription );
394 free( p_ext->psz_version );
395 vlc_mutex_destroy( &p_ext->p_sys->command_lock );
396 vlc_mutex_destroy( &p_ext->p_sys->running_lock );
397 vlc_cond_destroy( &p_ext->p_sys->wait );
398 free( p_ext->p_sys );
403 /* Add the extension to the list of known extensions */
404 ARRAY_APPEND( p_mgr->extensions, p_ext );
407 vlc_mutex_unlock( &p_mgr->lock );
408 /* Continue batch execution */
412 static int Control( extensions_manager_t *p_mgr, int i_control, va_list args )
414 extension_t *p_ext = NULL;
416 uint16_t **ppus = NULL;
417 char ***pppsz = NULL;
422 case EXTENSION_ACTIVATE:
423 p_ext = ( extension_t* ) va_arg( args, extension_t* );
424 return Activate( p_mgr, p_ext );
426 case EXTENSION_DEACTIVATE:
427 p_ext = ( extension_t* ) va_arg( args, extension_t* );
428 return Deactivate( p_mgr, p_ext );
430 case EXTENSION_IS_ACTIVATED:
431 p_ext = ( extension_t* ) va_arg( args, extension_t* );
432 pb = ( bool* ) va_arg( args, bool* );
433 *pb = IsActivated( p_mgr, p_ext );
436 case EXTENSION_HAS_MENU:
437 p_ext = ( extension_t* ) va_arg( args, extension_t* );
438 pb = ( bool* ) va_arg( args, bool* );
439 *pb = ( p_ext->p_sys->i_capabilities & EXT_HAS_MENU ) ? 1 : 0;
442 case EXTENSION_GET_MENU:
443 p_ext = ( extension_t* ) va_arg( args, extension_t* );
444 pppsz = ( char*** ) va_arg( args, char*** );
445 ppus = ( uint16_t** ) va_arg( args, uint16_t** );
446 return GetMenuEntries( p_mgr, p_ext, pppsz, ppus );
448 case EXTENSION_TRIGGER_ONLY:
449 p_ext = ( extension_t* ) va_arg( args, extension_t* );
450 pb = ( bool* ) va_arg( args, bool* );
451 *pb = ( p_ext->p_sys->i_capabilities & EXT_TRIGGER_ONLY ) ? 1 : 0;
454 case EXTENSION_TRIGGER:
455 p_ext = ( extension_t* ) va_arg( args, extension_t* );
456 return TriggerExtension( p_mgr, p_ext );
458 case EXTENSION_TRIGGER_MENU:
459 p_ext = ( extension_t* ) va_arg( args, extension_t* );
460 // GCC: 'uint16_t' is promoted to 'int' when passed through '...'
461 i = ( int ) va_arg( args, int );
462 return TriggerMenu( p_ext, i );
464 case EXTENSION_SET_INPUT:
466 p_ext = ( extension_t* ) va_arg( args, extension_t* );
467 input_thread_t *p_input = va_arg( args, struct input_thread_t * );
469 if( !LockExtension( p_ext ) )
473 input_thread_t *old = p_ext->p_sys->p_input;
474 input_item_t *p_item;
477 // Untrack meta fetched events
478 if( p_ext->p_sys->i_capabilities & EXT_META_LISTENER )
480 p_item = input_GetItem( old );
481 vlc_event_detach( &p_item->event_manager,
482 vlc_InputItemMetaChanged,
483 inputItemMetaChanged,
485 vlc_gc_decref( p_item );
487 vlc_object_release( old );
490 p_ext->p_sys->p_input = p_input ? vlc_object_hold( p_input )
493 // Tell the script the input changed
494 if( p_ext->p_sys->i_capabilities & EXT_INPUT_LISTENER )
496 PushCommandUnique( p_ext, CMD_SET_INPUT );
499 // Track meta fetched events
500 if( p_ext->p_sys->p_input &&
501 p_ext->p_sys->i_capabilities & EXT_META_LISTENER )
503 p_item = input_GetItem( p_ext->p_sys->p_input );
504 vlc_gc_incref( p_item );
505 vlc_event_attach( &p_item->event_manager,
506 vlc_InputItemMetaChanged,
507 inputItemMetaChanged,
511 UnlockExtension( p_ext );
514 case EXTENSION_PLAYING_CHANGED:
517 p_ext = ( extension_t* ) va_arg( args, extension_t* );
518 assert( p_ext->psz_name != NULL );
519 i = ( int ) va_arg( args, int );
520 if( p_ext->p_sys->i_capabilities & EXT_PLAYING_LISTENER )
522 PushCommand( p_ext, CMD_PLAYING_CHANGED, i );
527 msg_Warn( p_mgr, "Control '%d' not yet implemented in Extension",
535 int lua_ExtensionActivate( extensions_manager_t *p_mgr, extension_t *p_ext )
537 assert( p_mgr != NULL && p_ext != NULL );
538 return lua_ExecuteFunction( p_mgr, p_ext, "activate", LUA_END );
541 int lua_ExtensionDeactivate( extensions_manager_t *p_mgr, extension_t *p_ext )
543 assert( p_mgr != NULL && p_ext != NULL );
545 if( !p_ext->p_sys->L )
548 // Unset and release input objects
549 if( p_ext->p_sys->p_input )
551 if( p_ext->p_sys->i_capabilities & EXT_META_LISTENER )
554 input_item_t *p_item = input_GetItem( p_ext->p_sys->p_input );
555 vlc_gc_decref( p_item );
557 vlc_object_release( p_ext->p_sys->p_input );
560 int i_ret = lua_ExecuteFunction( p_mgr, p_ext, "deactivate", LUA_END );
562 /* Clear Lua State */
563 lua_close( p_ext->p_sys->L );
564 p_ext->p_sys->L = NULL;
569 int lua_ExtensionWidgetClick( extensions_manager_t *p_mgr,
571 extension_widget_t *p_widget )
573 if( !p_ext->p_sys->L )
576 lua_State *L = GetLuaState( p_mgr, p_ext );
577 lua_pushlightuserdata( L, p_widget );
578 lua_gettable( L, LUA_REGISTRYINDEX );
579 return lua_ExecuteFunction( p_mgr, p_ext, NULL, LUA_END );
584 * Get the list of menu entries from an extension script
587 * @param pppsz_titles Pointer to NULL. All strings must be freed by the caller
588 * @param ppi_ids Pointer to NULL. Must be freed by the caller.
589 * @note This function is allowed to run in the UI thread. This means
590 * that it MUST respond very fast.
591 * @todo Remove the menu() hook and provide a new function vlc.set_menu()
593 static int GetMenuEntries( extensions_manager_t *p_mgr, extension_t *p_ext,
594 char ***pppsz_titles, uint16_t **ppi_ids )
596 assert( *pppsz_titles == NULL );
597 assert( *ppi_ids == NULL );
599 if( !IsActivated( p_mgr, p_ext ) )
601 msg_Dbg( p_mgr, "Can't get menu before activating the extension!" );
605 if( !LockExtension( p_ext ) )
607 /* Dying extension, fail. */
611 int i_ret = VLC_EGENERIC;
612 lua_State *L = GetLuaState( p_mgr, p_ext );
614 if( ( p_ext->p_sys->i_capabilities & EXT_HAS_MENU ) == 0 )
616 msg_Dbg( p_mgr, "can't get a menu from an extension without menu!" );
620 lua_getglobal( L, "menu" );
622 if( !lua_isfunction( L, -1 ) )
624 msg_Warn( p_mgr, "Error while running script %s, "
625 "function menu() not found", p_ext->psz_name );
629 if( lua_pcall( L, 0, 1, 0 ) )
631 msg_Warn( p_mgr, "Error while running script %s, "
632 "function menu(): %s", p_ext->psz_name,
633 lua_tostring( L, lua_gettop( L ) ) );
637 if( lua_gettop( L ) )
639 if( lua_istable( L, -1 ) )
642 size_t i_size = lua_objlen( L, -1 );
643 *pppsz_titles = ( char** ) calloc( i_size+1, sizeof( char* ) );
644 *ppi_ids = ( uint16_t* ) calloc( i_size+1, sizeof( uint16_t ) );
649 while( lua_next( L, -2 ) != 0 )
651 assert( i_idx < i_size );
652 if( (!lua_isstring( L, -1 )) || (!lua_isnumber( L, -2 )) )
654 msg_Warn( p_mgr, "In script %s, an entry in "
655 "the menu table is invalid!", p_ext->psz_name );
658 (*pppsz_titles)[ i_idx ] = strdup( luaL_checkstring( L, -1 ) );
659 (*ppi_ids)[ i_idx ] = (uint16_t) ( luaL_checkinteger( L, -2 ) & 0xFFFF );
666 msg_Warn( p_mgr, "Function menu() in script %s "
667 "did not return a table", p_ext->psz_name );
673 msg_Warn( p_mgr, "Script %s went completely foobar", p_ext->psz_name );
680 UnlockExtension( p_ext );
681 if( i_ret != VLC_SUCCESS )
683 msg_Dbg( p_mgr, "Something went wrong in %s (%s:%d)",
684 __func__, __FILE__, __LINE__ );
689 /* Must be entered with the Lock on Extension */
690 static lua_State* GetLuaState( extensions_manager_t *p_mgr,
702 msg_Err( p_mgr, "Could not create new Lua State" );
705 vlclua_set_this( L, p_mgr );
706 vlclua_extension_set( L, p_ext );
709 luaL_register( L, "vlc", p_reg );
714 /* Load more libraries */
717 luaopen_dialog( L, p_ext );
725 luaopen_playlist( L );
728 luaopen_strings( L );
729 luaopen_variables( L );
735 /* Register extension specific functions */
736 lua_getglobal( L, "vlc" );
737 lua_pushcfunction( L, vlclua_extension_deactivate );
738 lua_setfield( L, -2, "deactivate" );
740 /* Setup the module search path */
741 if( vlclua_add_modules_path( p_mgr, L, p_ext->psz_name ) )
743 msg_Warn( p_mgr, "Error while setting the module search path for %s", p_ext->psz_name );
747 /* Load and run the script(s) */
748 if( luaL_dofile( L, p_ext->psz_name ) != 0 )
750 msg_Warn( p_mgr, "Error loading script %s: %s", p_ext->psz_name,
751 lua_tostring( L, lua_gettop( L ) ) );
763 int lua_ExecuteFunction( extensions_manager_t *p_mgr, extension_t *p_ext,
764 const char *psz_function, ... )
767 va_start( args, psz_function );
768 int i_ret = lua_ExecuteFunctionVa( p_mgr, p_ext, psz_function, args );
774 * Execute a function in a Lua script
775 * @param psz_function Name of global function to execute. If NULL, assume
776 * that the function object is already on top of the
778 * @return < 0 in case of failure, >= 0 in case of success
779 * @note It's better to call this function from a dedicated thread
780 * (see extension_thread.c)
782 int lua_ExecuteFunctionVa( extensions_manager_t *p_mgr, extension_t *p_ext,
783 const char *psz_function, va_list args )
785 int i_ret = VLC_EGENERIC;
787 assert( p_mgr != NULL );
788 assert( p_ext != NULL );
790 lua_State *L = GetLuaState( p_mgr, p_ext );
792 lua_getglobal( L, psz_function );
794 if( !lua_isfunction( L, -1 ) )
796 msg_Warn( p_mgr, "Error while running script %s, "
797 "function %s() not found", p_ext->psz_name, psz_function );
801 lua_datatype_e type = LUA_END;
802 while( ( type = va_arg( args, int ) ) != LUA_END )
804 if( type == LUA_NUM )
806 lua_pushnumber( L , ( int ) va_arg( args, int ) );
808 else if( type == LUA_TEXT )
810 lua_pushstring( L , ( char * ) va_arg( args, char* ) );
814 msg_Warn( p_mgr, "Undefined argument type %d to lua function %s"
815 "from script %s", type, psz_function, p_ext->psz_name );
820 if( lua_pcall( L, i_args, 1, 0 ) )
822 msg_Warn( p_mgr, "Error while running script %s, "
823 "function %s(): %s", p_ext->psz_name, psz_function,
824 lua_tostring( L, lua_gettop( L ) ) );
828 i_ret = lua_DialogFlush( L );
834 static inline int TriggerMenu( extension_t *p_ext, int i_id )
836 return PushCommand( p_ext, CMD_TRIGGERMENU, i_id );
839 int lua_ExtensionTriggerMenu( extensions_manager_t *p_mgr,
840 extension_t *p_ext, int id )
842 int i_ret = VLC_EGENERIC;
843 lua_State *L = GetLuaState( p_mgr, p_ext );
848 luaopen_dialog( L, p_ext );
850 lua_getglobal( L, "trigger_menu" );
851 if( !lua_isfunction( L, -1 ) )
853 msg_Warn( p_mgr, "Error while running script %s, "
854 "function trigger_menu() not found", p_ext->psz_name );
858 /* Pass id as unique argument to the function */
859 lua_pushinteger( L, id );
861 if( lua_pcall( L, 1, 1, 0 ) != 0 )
863 msg_Warn( p_mgr, "Error while running script %s, "
864 "function trigger_menu(): %s", p_ext->psz_name,
865 lua_tostring( L, lua_gettop( L ) ) );
869 i_ret = lua_DialogFlush( L );
870 if( i_ret < VLC_SUCCESS )
872 msg_Dbg( p_mgr, "Something went wrong in %s (%s:%d)",
873 __func__, __FILE__, __LINE__ );
878 /** Directly trigger an extension, without activating it
879 * This is NOT multithreaded, and this code runs in the UI thread
881 * @param p_ext Extension to trigger
882 * @return Value returned by the lua function "trigger"
884 static int TriggerExtension( extensions_manager_t *p_mgr,
887 int i_ret = lua_ExecuteFunction( p_mgr, p_ext, "trigger", LUA_END );
889 /* Close lua state for trigger-only extensions */
890 if( p_ext->p_sys->L )
891 lua_close( p_ext->p_sys->L );
892 p_ext->p_sys->L = NULL;
897 /** Set extension associated to the current script
898 * @param L current lua_State
899 * @param p_ext the extension
901 void vlclua_extension_set( lua_State *L, extension_t *p_ext )
903 lua_pushlightuserdata( L, vlclua_extension_set );
904 lua_pushlightuserdata( L, p_ext );
905 lua_rawset( L, LUA_REGISTRYINDEX );
908 /** Retrieve extension associated to the current script
909 * @param L current lua_State
910 * @return Extension pointer
912 extension_t *vlclua_extension_get( lua_State *L )
914 lua_pushlightuserdata( L, vlclua_extension_set );
915 lua_rawget( L, LUA_REGISTRYINDEX );
916 extension_t *p_ext = (extension_t*) lua_topointer( L, -1 );
921 /** Deactivate an extension by order from the extension itself
923 * @note This is an asynchronous call. A script calling vlc.deactivate() will
924 * be executed to the end before the last call to deactivate() is done.
926 int vlclua_extension_deactivate( lua_State *L )
928 extension_t *p_ext = vlclua_extension_get( L );
929 int i_ret = Deactivate( p_ext->p_sys->p_mgr, p_ext );
930 return ( i_ret == VLC_SUCCESS ) ? 1 : 0;
933 /** Callback for the variable "dialog-event"
934 * @param p_this Current object owner of the extension and the dialog
935 * @param psz_var "dialog-event"
936 * @param oldval Unused
937 * @param newval Address of the dialog
938 * @param p_data Unused
940 static int vlclua_extension_dialog_callback( vlc_object_t *p_this,
946 /* psz_var == "dialog-event" */
951 extension_dialog_command_t *command = newval.p_address;
952 assert( command != NULL );
953 assert( command->p_dlg != NULL);
955 extension_t *p_ext = command->p_dlg->p_sys;
956 assert( p_ext != NULL );
958 extension_widget_t *p_widget = command->p_data;
960 switch( command->event )
962 case EXTENSION_EVENT_CLICK:
963 assert( p_widget != NULL );
964 PushCommandUnique( p_ext, CMD_CLICK, p_widget );
966 case EXTENSION_EVENT_CLOSE:
967 PushCommandUnique( p_ext, CMD_CLOSE );
970 msg_Dbg( p_this, "Received unknown UI event %d, discarded",
978 /** Callback on vlc_InputItemMetaChanged event
980 static void inputItemMetaChanged( const vlc_event_t *p_event,
983 assert( p_event && p_event->type == vlc_InputItemMetaChanged );
985 extension_t *p_ext = ( extension_t* ) data;
986 assert( p_ext != NULL );
988 PushCommandUnique( p_ext, CMD_UPDATE_META );
991 /* Lock this extension. Can fail. */
992 bool LockExtension( extension_t *p_ext )
994 if( p_ext->p_sys->b_exiting )
997 vlc_mutex_lock( &p_ext->p_sys->running_lock );
998 if( p_ext->p_sys->b_exiting )
1000 vlc_mutex_unlock( &p_ext->p_sys->running_lock );
1007 void UnlockExtension( extension_t *p_ext )
1009 vlc_mutex_unlock( &p_ext->p_sys->running_lock );