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 )
186 vlclua_scripts_batch_execute( VLC_OBJECT( p_mgr ),
198 * Batch scan all Lua files in folder "extensions": callback
199 * @param p_this This extensions_manager_t object
200 * @param psz_script Name of the script to run
201 * @param L Lua State, common to all scripts here
202 * @param pb_continue bool* that indicates whether to continue batch or not
204 int ScanLuaCallback( vlc_object_t *p_this, const char *psz_script,
207 extensions_manager_t *p_mgr = ( extensions_manager_t* ) p_this;
210 msg_Dbg( p_mgr, "Scanning Lua script %s", psz_script );
212 vlc_mutex_lock( &p_mgr->lock );
214 /* Create new script descriptor */
215 extension_t *p_ext = ( extension_t* ) calloc( 1, sizeof( extension_t ) );
218 vlc_mutex_unlock( &p_mgr->lock );
222 p_ext->psz_name = strdup( psz_script );
223 p_ext->p_sys = (extension_sys_t*) calloc( 1, sizeof( extension_sys_t ) );
224 if( !p_ext->p_sys || !p_ext->psz_name )
226 free( p_ext->psz_name );
227 free( p_ext->p_sys );
229 vlc_mutex_unlock( &p_mgr->lock );
232 p_ext->p_sys->p_mgr = p_mgr;
234 /* Mutexes and conditions */
235 vlc_mutex_init( &p_ext->p_sys->command_lock );
236 vlc_mutex_init( &p_ext->p_sys->running_lock );
237 vlc_cond_init( &p_ext->p_sys->wait );
239 /* Load and run the script(s) */
240 lua_State *L = luaL_newstate();
241 if( luaL_dofile( L, psz_script ) )
243 msg_Warn( p_mgr, "Error loading script %s: %s", psz_script,
244 lua_tostring( L, lua_gettop( L ) ) );
249 /* Scan script for capabilities */
250 lua_getglobal( L, "descriptor" );
252 if( !lua_isfunction( L, -1 ) )
254 msg_Warn( p_mgr, "Error while runing script %s, "
255 "function descriptor() not found", psz_script );
259 if( lua_pcall( L, 0, 1, 0 ) )
261 msg_Warn( p_mgr, "Error while runing script %s, "
262 "function descriptor(): %s", psz_script,
263 lua_tostring( L, lua_gettop( L ) ) );
267 if( lua_gettop( L ) )
269 if( lua_istable( L, -1 ) )
272 lua_getfield( L, -1, "capabilities" );
273 if( lua_istable( L, -1 ) )
276 while( lua_next( L, -2 ) != 0 )
278 /* Key is at index -2 and value at index -1. Discard key */
279 const char *psz_cap = luaL_checkstring( L, -1 );
282 /* Find this capability's flag */
283 for( const char *iter = *ppsz_capabilities;
285 iter = ppsz_capabilities[ ++i_cap ])
287 if( !strcmp( iter, psz_cap ) )
290 p_ext->p_sys->i_capabilities |= 1 << i_cap;
297 msg_Warn( p_mgr, "Extension capability '%s' unknown in"
298 " script %s", psz_cap, psz_script );
300 /* Removes 'value'; keeps 'key' for next iteration */
306 msg_Warn( p_mgr, "In script %s, function descriptor() "
307 "did not return a table of capabilities.",
313 lua_getfield( L, -1, "title" );
314 if( lua_isstring( L, -1 ) )
316 p_ext->psz_title = strdup( luaL_checkstring( L, -1 ) );
320 msg_Dbg( p_mgr, "In script %s, function descriptor() "
321 "did not return a string as title.",
323 p_ext->psz_title = strdup( psz_script );
328 lua_getfield( L, -1, "author" );
329 if( lua_isstring( L, -1 ) )
331 p_ext->psz_author = strdup( luaL_checkstring( L, -1 ) );
335 p_ext->psz_author = NULL;
339 /* Get description */
340 lua_getfield( L, -1, "description" );
341 if( lua_isstring( L, -1 ) )
343 p_ext->psz_description = strdup( luaL_checkstring( L, -1 ) );
347 p_ext->psz_description = NULL;
351 /* Get short description */
352 lua_getfield( L, -1, "shortdesc" );
353 if( lua_isstring( L, -1 ) )
355 p_ext->psz_shortdescription = strdup( luaL_checkstring( L, -1 ) );
359 p_ext->psz_shortdescription = NULL;
364 lua_getfield( L, -1, "url" );
365 if( lua_isstring( L, -1 ) )
367 p_ext->psz_url = strdup( luaL_checkstring( L, -1 ) );
371 p_ext->psz_url = NULL;
376 lua_getfield( L, -1, "version" );
377 if( lua_isstring( L, -1 ) )
379 p_ext->psz_version = strdup( luaL_checkstring( L, -1 ) );
383 p_ext->psz_version = NULL;
389 msg_Warn( p_mgr, "In script %s, function descriptor() "
390 "did not return a table!", psz_script );
396 msg_Err( p_mgr, "Script %s went completely foobar", psz_script );
400 msg_Dbg( p_mgr, "Script %s has the following capability flags: 0x%x",
401 psz_script, p_ext->p_sys->i_capabilities );
408 free( p_ext->psz_name );
409 free( p_ext->psz_title );
410 free( p_ext->psz_url );
411 free( p_ext->psz_author );
412 free( p_ext->psz_description );
413 free( p_ext->psz_shortdescription );
414 free( p_ext->psz_version );
415 vlc_mutex_destroy( &p_ext->p_sys->command_lock );
416 vlc_mutex_destroy( &p_ext->p_sys->running_lock );
417 vlc_cond_destroy( &p_ext->p_sys->wait );
418 free( p_ext->p_sys );
423 /* Add the extension to the list of known extensions */
424 ARRAY_APPEND( p_mgr->extensions, p_ext );
427 vlc_mutex_unlock( &p_mgr->lock );
428 /* Continue batch execution */
429 return pb_continue ? ( (* (bool*)pb_continue) ? -1 : 0 ) : -1;
432 static int Control( extensions_manager_t *p_mgr, int i_control, va_list args )
434 extension_t *p_ext = NULL;
436 uint16_t **ppus = NULL;
437 char ***pppsz = NULL;
442 case EXTENSION_ACTIVATE:
443 p_ext = ( extension_t* ) va_arg( args, extension_t* );
444 return Activate( p_mgr, p_ext );
446 case EXTENSION_DEACTIVATE:
447 p_ext = ( extension_t* ) va_arg( args, extension_t* );
448 return Deactivate( p_mgr, p_ext );
450 case EXTENSION_IS_ACTIVATED:
451 p_ext = ( extension_t* ) va_arg( args, extension_t* );
452 pb = ( bool* ) va_arg( args, bool* );
453 *pb = IsActivated( p_mgr, p_ext );
456 case EXTENSION_HAS_MENU:
457 p_ext = ( extension_t* ) va_arg( args, extension_t* );
458 pb = ( bool* ) va_arg( args, bool* );
459 *pb = ( p_ext->p_sys->i_capabilities & EXT_HAS_MENU ) ? 1 : 0;
462 case EXTENSION_GET_MENU:
463 p_ext = ( extension_t* ) va_arg( args, extension_t* );
464 pppsz = ( char*** ) va_arg( args, char*** );
465 ppus = ( uint16_t** ) va_arg( args, uint16_t** );
466 return GetMenuEntries( p_mgr, p_ext, pppsz, ppus );
468 case EXTENSION_TRIGGER_ONLY:
469 p_ext = ( extension_t* ) va_arg( args, extension_t* );
470 pb = ( bool* ) va_arg( args, bool* );
471 *pb = ( p_ext->p_sys->i_capabilities & EXT_TRIGGER_ONLY ) ? 1 : 0;
474 case EXTENSION_TRIGGER:
475 p_ext = ( extension_t* ) va_arg( args, extension_t* );
476 return TriggerExtension( p_mgr, p_ext );
478 case EXTENSION_TRIGGER_MENU:
479 p_ext = ( extension_t* ) va_arg( args, extension_t* );
480 // GCC: 'uint16_t' is promoted to 'int' when passed through '...'
481 i = ( int ) va_arg( args, int );
482 return TriggerMenu( p_ext, i );
484 case EXTENSION_SET_INPUT:
486 p_ext = ( extension_t* ) va_arg( args, extension_t* );
487 input_thread_t *p_input = va_arg( args, struct input_thread_t * );
489 if( !LockExtension( p_ext ) )
493 input_thread_t *old = p_ext->p_sys->p_input;
494 input_item_t *p_item;
497 // Untrack meta fetched events
498 if( p_ext->p_sys->i_capabilities & EXT_META_LISTENER )
500 p_item = input_GetItem( old );
501 vlc_event_detach( &p_item->event_manager,
502 vlc_InputItemMetaChanged,
503 inputItemMetaChanged,
505 vlc_gc_decref( p_item );
507 vlc_object_release( old );
510 p_ext->p_sys->p_input = p_input ? vlc_object_hold( p_input )
513 // Tell the script the input changed
514 if( p_ext->p_sys->i_capabilities & EXT_INPUT_LISTENER )
516 PushCommandUnique( p_ext, CMD_SET_INPUT );
519 // Track meta fetched events
520 if( p_ext->p_sys->p_input &&
521 p_ext->p_sys->i_capabilities & EXT_META_LISTENER )
523 p_item = input_GetItem( p_ext->p_sys->p_input );
524 vlc_gc_incref( p_item );
525 vlc_event_attach( &p_item->event_manager,
526 vlc_InputItemMetaChanged,
527 inputItemMetaChanged,
531 UnlockExtension( p_ext );
534 case EXTENSION_PLAYING_CHANGED:
537 p_ext = ( extension_t* ) va_arg( args, extension_t* );
538 assert( p_ext->psz_name != NULL );
539 i = ( int ) va_arg( args, int );
540 if( p_ext->p_sys->i_capabilities & EXT_PLAYING_LISTENER )
542 PushCommand( p_ext, CMD_PLAYING_CHANGED, i );
547 msg_Warn( p_mgr, "Control '%d' not yet implemented in Extension",
555 int lua_ExtensionActivate( extensions_manager_t *p_mgr, extension_t *p_ext )
557 assert( p_mgr != NULL && p_ext != NULL );
558 return lua_ExecuteFunction( p_mgr, p_ext, "activate", LUA_END );
561 int lua_ExtensionDeactivate( extensions_manager_t *p_mgr, extension_t *p_ext )
563 assert( p_mgr != NULL && p_ext != NULL );
565 if( !p_ext->p_sys->L )
568 // Unset and release input objects
569 if( p_ext->p_sys->p_input )
571 if( p_ext->p_sys->i_capabilities & EXT_META_LISTENER )
574 input_item_t *p_item = input_GetItem( p_ext->p_sys->p_input );
575 vlc_gc_decref( p_item );
577 vlc_object_release( p_ext->p_sys->p_input );
580 int i_ret = lua_ExecuteFunction( p_mgr, p_ext, "deactivate", LUA_END );
582 /* Clear Lua State */
583 lua_close( p_ext->p_sys->L );
584 p_ext->p_sys->L = NULL;
589 int lua_ExtensionWidgetClick( extensions_manager_t *p_mgr,
591 extension_widget_t *p_widget )
593 if( !p_ext->p_sys->L )
596 lua_State *L = GetLuaState( p_mgr, p_ext );
597 lua_pushlightuserdata( L, p_widget );
598 lua_gettable( L, LUA_REGISTRYINDEX );
599 return lua_ExecuteFunction( p_mgr, p_ext, NULL, LUA_END );
604 * Get the list of menu entries from an extension script
607 * @param pppsz_titles Pointer to NULL. All strings must be freed by the caller
608 * @param ppi_ids Pointer to NULL. Must be feed by the caller.
609 * @note This function is allowed to run in the UI thread. This means
610 * that it MUST respond very fast.
611 * @todo Remove the menu() hook and provide a new function vlc.set_menu()
613 static int GetMenuEntries( extensions_manager_t *p_mgr, extension_t *p_ext,
614 char ***pppsz_titles, uint16_t **ppi_ids )
616 assert( *pppsz_titles == NULL );
617 assert( *ppi_ids == NULL );
619 if( !IsActivated( p_mgr, p_ext ) )
621 msg_Dbg( p_mgr, "Can't get menu before activating the extension!" );
625 if( !LockExtension( p_ext ) )
627 /* Dying extension, fail. */
631 int i_ret = VLC_EGENERIC;
632 lua_State *L = GetLuaState( p_mgr, p_ext );
634 if( ( p_ext->p_sys->i_capabilities & EXT_HAS_MENU ) == 0 )
636 msg_Dbg( p_mgr, "can't get a menu from an extension without menu!" );
640 lua_getglobal( L, "menu" );
642 if( !lua_isfunction( L, -1 ) )
644 msg_Warn( p_mgr, "Error while runing script %s, "
645 "function menu() not found", p_ext->psz_name );
649 if( lua_pcall( L, 0, 1, 0 ) )
651 msg_Warn( p_mgr, "Error while runing script %s, "
652 "function menu(): %s", p_ext->psz_name,
653 lua_tostring( L, lua_gettop( L ) ) );
657 if( lua_gettop( L ) )
659 if( lua_istable( L, -1 ) )
662 size_t i_size = lua_objlen( L, -1 );
663 *pppsz_titles = ( char** ) calloc( i_size+1, sizeof( char* ) );
664 *ppi_ids = ( uint16_t* ) calloc( i_size+1, sizeof( uint16_t ) );
669 while( lua_next( L, -2 ) != 0 )
671 assert( i_idx < i_size );
672 if( (!lua_isstring( L, -1 )) || (!lua_isnumber( L, -2 )) )
674 msg_Warn( p_mgr, "In script %s, an entry in "
675 "the menu table is invalid!", p_ext->psz_name );
678 (*pppsz_titles)[ i_idx ] = strdup( luaL_checkstring( L, -1 ) );
679 (*ppi_ids)[ i_idx ] = (uint16_t) ( luaL_checkinteger( L, -2 ) & 0xFFFF );
686 msg_Warn( p_mgr, "Function menu() in script %s "
687 "did not return a table", p_ext->psz_name );
693 msg_Warn( p_mgr, "Script %s went completely foobar", p_ext->psz_name );
700 UnlockExtension( p_ext );
701 if( i_ret != VLC_SUCCESS )
703 msg_Dbg( p_mgr, "Something went wrong in %s (%s:%d)",
704 __func__, __FILE__, __LINE__ );
709 /* Must be entered with the Lock on Extension */
710 static lua_State* GetLuaState( extensions_manager_t *p_mgr,
722 msg_Err( p_mgr, "Could not create new Lua State" );
725 vlclua_set_this( L, p_mgr );
726 vlclua_extension_set( L, p_ext );
729 luaL_register( L, "vlc", p_reg );
734 /* Load more libraries */
737 luaopen_dialog( L, p_ext );
745 luaopen_playlist( L );
748 luaopen_strings( L );
749 luaopen_variables( L );
755 /* Register extension specific functions */
756 lua_getglobal( L, "vlc" );
757 lua_pushcfunction( L, vlclua_extension_deactivate );
758 lua_setfield( L, -2, "deactivate" );
760 /* Setup the module search path */
761 if( vlclua_add_modules_path( p_mgr, L, p_ext->psz_name ) )
763 msg_Warn( p_mgr, "Error while setting the module search path for %s", p_ext->psz_name );
767 /* Load and run the script(s) */
768 if( luaL_dofile( L, p_ext->psz_name ) != 0 )
770 msg_Warn( p_mgr, "Error loading script %s: %s", p_ext->psz_name,
771 lua_tostring( L, lua_gettop( L ) ) );
782 msg_Dbg( p_mgr, "Reusing old Lua state for extension '%s'",
790 int lua_ExecuteFunction( extensions_manager_t *p_mgr, extension_t *p_ext,
791 const char *psz_function, ... )
794 va_start( args, psz_function );
795 int i_ret = lua_ExecuteFunctionVa( p_mgr, p_ext, psz_function, args );
801 * Execute a function in a Lua script
802 * @param psz_function Name of global function to execute. If NULL, assume
803 * that the function object is already on top of the
805 * @return < 0 in case of failure, >= 0 in case of success
806 * @note It's better to call this function from a dedicated thread
807 * (see extension_thread.c)
809 int lua_ExecuteFunctionVa( extensions_manager_t *p_mgr, extension_t *p_ext,
810 const char *psz_function, va_list args )
812 int i_ret = VLC_EGENERIC;
814 assert( p_mgr != NULL );
815 assert( p_ext != NULL );
817 lua_State *L = GetLuaState( p_mgr, p_ext );
819 lua_getglobal( L, psz_function );
821 if( !lua_isfunction( L, -1 ) )
823 msg_Warn( p_mgr, "Error while runing script %s, "
824 "function %s() not found", p_ext->psz_name, psz_function );
828 lua_datatype_e type = LUA_END;
829 while( ( type = va_arg( args, int ) ) != LUA_END )
831 if( type == LUA_NUM )
833 lua_pushnumber( L , ( int ) va_arg( args, int ) );
835 else if( type == LUA_TEXT )
837 lua_pushstring( L , ( char * ) va_arg( args, char* ) );
841 msg_Warn( p_mgr, "Undefined argument type %d to lua function %s"
842 "from script %s", type, psz_function, p_ext->psz_name );
847 if( lua_pcall( L, i_args, 1, 0 ) )
849 msg_Warn( p_mgr, "Error while runing script %s, "
850 "function %s(): %s", p_ext->psz_name, psz_function,
851 lua_tostring( L, lua_gettop( L ) ) );
855 i_ret = lua_DialogFlush( L );
861 static inline int TriggerMenu( extension_t *p_ext, int i_id )
863 return PushCommand( p_ext, CMD_TRIGGERMENU, i_id );
866 int lua_ExtensionTriggerMenu( extensions_manager_t *p_mgr,
867 extension_t *p_ext, int id )
869 int i_ret = VLC_EGENERIC;
870 lua_State *L = GetLuaState( p_mgr, p_ext );
875 luaopen_dialog( L, p_ext );
877 lua_getglobal( L, "trigger_menu" );
878 if( !lua_isfunction( L, -1 ) )
880 msg_Warn( p_mgr, "Error while runing script %s, "
881 "function trigger_menu() not found", p_ext->psz_name );
885 /* Pass id as unique argument to the function */
886 lua_pushinteger( L, id );
888 if( lua_pcall( L, 1, 1, 0 ) != 0 )
890 msg_Warn( p_mgr, "Error while runing script %s, "
891 "function trigger_menu(): %s", p_ext->psz_name,
892 lua_tostring( L, lua_gettop( L ) ) );
896 i_ret = lua_DialogFlush( L );
897 if( i_ret < VLC_SUCCESS )
899 msg_Dbg( p_mgr, "Something went wrong in %s (%s:%d)",
900 __func__, __FILE__, __LINE__ );
905 /** Directly trigger an extension, without activating it
906 * This is NOT multithreaded, and this code runs in the UI thread
908 * @param p_ext Extension to trigger
909 * @return Value returned by the lua function "trigger"
911 static int TriggerExtension( extensions_manager_t *p_mgr,
914 int i_ret = lua_ExecuteFunction( p_mgr, p_ext, "trigger", LUA_END );
916 /* Close lua state for trigger-only extensions */
917 if( p_ext->p_sys->L )
918 lua_close( p_ext->p_sys->L );
919 p_ext->p_sys->L = NULL;
924 /** Set extension associated to the current script
925 * @param L current lua_State
926 * @param p_ext the extension
928 void vlclua_extension_set( lua_State *L, extension_t *p_ext )
930 lua_pushlightuserdata( L, vlclua_extension_set );
931 lua_pushlightuserdata( L, p_ext );
932 lua_rawset( L, LUA_REGISTRYINDEX );
935 /** Retrieve extension associated to the current script
936 * @param L current lua_State
937 * @return Extension pointer
939 extension_t *vlclua_extension_get( lua_State *L )
941 lua_pushlightuserdata( L, vlclua_extension_set );
942 lua_rawget( L, LUA_REGISTRYINDEX );
943 extension_t *p_ext = (extension_t*) lua_topointer( L, -1 );
948 /** Deactivate an extension by order from the extension itself
950 * @note This is an asynchronous call. A script calling vlc.deactivate() will
951 * be executed to the end before the last call to deactivate() is done.
953 int vlclua_extension_deactivate( lua_State *L )
955 extension_t *p_ext = vlclua_extension_get( L );
956 int i_ret = Deactivate( p_ext->p_sys->p_mgr, p_ext );
957 return ( i_ret == VLC_SUCCESS ) ? 1 : 0;
960 /** Callback for the variable "dialog-event"
961 * @param p_this Current object owner of the extension and the dialog
962 * @param psz_var "dialog-event"
963 * @param oldval Unused
964 * @param newval Address of the dialog
965 * @param p_data Unused
967 static int vlclua_extension_dialog_callback( vlc_object_t *p_this,
973 /* psz_var == "dialog-event" */
978 extension_dialog_command_t *command = newval.p_address;
979 assert( command != NULL );
980 assert( command->p_dlg != NULL);
982 extension_t *p_ext = command->p_dlg->p_sys;
983 assert( p_ext != NULL );
985 extension_widget_t *p_widget = command->p_data;
987 switch( command->event )
989 case EXTENSION_EVENT_CLICK:
990 assert( p_widget != NULL );
991 PushCommand( p_ext, CMD_CLICK, p_widget );
993 case EXTENSION_EVENT_CLOSE:
994 PushCommandUnique( p_ext, CMD_CLOSE );
997 msg_Dbg( p_this, "Received unknown UI event %d, discarded",
1005 /** Callback on vlc_InputItemMetaChanged event
1007 static void inputItemMetaChanged( const vlc_event_t *p_event,
1010 assert( p_event && p_event->type == vlc_InputItemMetaChanged );
1012 extension_t *p_ext = ( extension_t* ) data;
1013 assert( p_ext != NULL );
1015 PushCommandUnique( p_ext, CMD_UPDATE_META );
1018 /* Lock this extension. Can fail. */
1019 bool LockExtension( extension_t *p_ext )
1021 if( p_ext->p_sys->b_exiting )
1024 vlc_mutex_lock( &p_ext->p_sys->running_lock );
1025 if( p_ext->p_sys->b_exiting )
1027 vlc_mutex_unlock( &p_ext->p_sys->running_lock );
1034 void UnlockExtension( extension_t *p_ext )
1036 vlc_mutex_unlock( &p_ext->p_sys->running_lock );