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 return lua_ExecuteFunction( p_mgr, p_ext, (const char*) p_widget->p_sys, LUA_END );
601 * Get the list of menu entries from an extension script
604 * @param pppsz_titles Pointer to NULL. All strings must be freed by the caller
605 * @param ppi_ids Pointer to NULL. Must be feed by the caller.
606 * @note This function is allowed to run in the UI thread. This means
607 * that it MUST respond very fast.
608 * @todo Remove the menu() hook and provide a new function vlc.set_menu()
610 static int GetMenuEntries( extensions_manager_t *p_mgr, extension_t *p_ext,
611 char ***pppsz_titles, uint16_t **ppi_ids )
613 assert( *pppsz_titles == NULL );
614 assert( *ppi_ids == NULL );
616 if( !IsActivated( p_mgr, p_ext ) )
618 msg_Dbg( p_mgr, "Can't get menu before activating the extension!" );
622 if( !LockExtension( p_ext ) )
624 /* Dying extension, fail. */
628 int i_ret = VLC_EGENERIC;
629 lua_State *L = GetLuaState( p_mgr, p_ext );
631 if( ( p_ext->p_sys->i_capabilities & EXT_HAS_MENU ) == 0 )
633 msg_Dbg( p_mgr, "can't get a menu from an extension without menu!" );
637 lua_getglobal( L, "menu" );
639 if( !lua_isfunction( L, -1 ) )
641 msg_Warn( p_mgr, "Error while runing script %s, "
642 "function menu() not found", p_ext->psz_name );
646 if( lua_pcall( L, 0, 1, 0 ) )
648 msg_Warn( p_mgr, "Error while runing script %s, "
649 "function menu(): %s", p_ext->psz_name,
650 lua_tostring( L, lua_gettop( L ) ) );
654 if( lua_gettop( L ) )
656 if( lua_istable( L, -1 ) )
659 size_t i_size = lua_objlen( L, -1 );
660 *pppsz_titles = ( char** ) calloc( i_size+1, sizeof( char* ) );
661 *ppi_ids = ( uint16_t* ) calloc( i_size+1, sizeof( uint16_t ) );
666 while( lua_next( L, -2 ) != 0 )
668 assert( i_idx < i_size );
669 if( (!lua_isstring( L, -1 )) || (!lua_isnumber( L, -2 )) )
671 msg_Warn( p_mgr, "In script %s, an entry in "
672 "the menu table is invalid!", p_ext->psz_name );
675 (*pppsz_titles)[ i_idx ] = strdup( luaL_checkstring( L, -1 ) );
676 (*ppi_ids)[ i_idx ] = (uint16_t) ( luaL_checkinteger( L, -2 ) & 0xFFFF );
683 msg_Warn( p_mgr, "Function menu() in script %s "
684 "did not return a table", p_ext->psz_name );
690 msg_Warn( p_mgr, "Script %s went completely foobar", p_ext->psz_name );
697 UnlockExtension( p_ext );
698 if( i_ret != VLC_SUCCESS )
700 msg_Dbg( p_mgr, "Something went wrong in %s (%s:%d)",
701 __func__, __FILE__, __LINE__ );
706 /* Must be entered with the Lock on Extension */
707 static lua_State* GetLuaState( extensions_manager_t *p_mgr,
719 msg_Err( p_mgr, "Could not create new Lua State" );
722 vlclua_set_this( L, p_mgr );
723 vlclua_extension_set( L, p_ext );
726 luaL_register( L, "vlc", p_reg );
731 /* Load more libraries */
734 luaopen_dialog( L, p_ext );
742 luaopen_playlist( L );
745 luaopen_strings( L );
746 luaopen_variables( L );
752 /* Register extension specific functions */
753 lua_getglobal( L, "vlc" );
754 lua_pushcfunction( L, vlclua_extension_deactivate );
755 lua_setfield( L, -2, "deactivate" );
757 /* Setup the module search path */
758 if( vlclua_add_modules_path( p_mgr, L, p_ext->psz_name ) )
760 msg_Warn( p_mgr, "Error while setting the module search path for %s", p_ext->psz_name );
764 /* Load and run the script(s) */
765 if( luaL_dofile( L, p_ext->psz_name ) != 0 )
767 msg_Warn( p_mgr, "Error loading script %s: %s", p_ext->psz_name,
768 lua_tostring( L, lua_gettop( L ) ) );
779 msg_Dbg( p_mgr, "Reusing old Lua state for extension '%s'",
787 int lua_ExecuteFunction( extensions_manager_t *p_mgr, extension_t *p_ext,
788 const char *psz_function, ... )
791 va_start( args, psz_function );
792 int i_ret = lua_ExecuteFunctionVa( p_mgr, p_ext, psz_function, args );
798 * Execute a function in a Lua script
799 * @return < 0 in case of failure, >= 0 in case of success
800 * @note It's better to call this function from a dedicated thread
801 * (see extension_thread.c)
803 int lua_ExecuteFunctionVa( extensions_manager_t *p_mgr, extension_t *p_ext,
804 const char *psz_function, va_list args )
806 int i_ret = VLC_EGENERIC;
808 assert( p_mgr != NULL );
809 assert( p_ext != NULL );
811 lua_State *L = GetLuaState( p_mgr, p_ext );
812 lua_getglobal( L, psz_function );
814 if( !lua_isfunction( L, -1 ) )
816 msg_Warn( p_mgr, "Error while runing script %s, "
817 "function %s() not found", p_ext->psz_name, psz_function );
821 lua_datatype_e type = LUA_END;
822 while( ( type = va_arg( args, int ) ) != LUA_END )
824 if( type == LUA_NUM )
826 lua_pushnumber( L , ( int ) va_arg( args, int ) );
828 else if( type == LUA_TEXT )
830 lua_pushstring( L , ( char * ) va_arg( args, char* ) );
834 msg_Warn( p_mgr, "Undefined argument type %d to lua function %s"
835 "from script %s", type, psz_function, p_ext->psz_name );
840 if( lua_pcall( L, i_args, 1, 0 ) )
842 msg_Warn( p_mgr, "Error while runing script %s, "
843 "function %s(): %s", p_ext->psz_name, psz_function,
844 lua_tostring( L, lua_gettop( L ) ) );
848 i_ret = lua_DialogFlush( L );
854 static inline int TriggerMenu( extension_t *p_ext, int i_id )
856 return PushCommand( p_ext, CMD_TRIGGERMENU, i_id );
859 int lua_ExtensionTriggerMenu( extensions_manager_t *p_mgr,
860 extension_t *p_ext, int id )
862 int i_ret = VLC_EGENERIC;
863 lua_State *L = GetLuaState( p_mgr, p_ext );
868 luaopen_dialog( L, p_ext );
870 lua_getglobal( L, "trigger_menu" );
871 if( !lua_isfunction( L, -1 ) )
873 msg_Warn( p_mgr, "Error while runing script %s, "
874 "function trigger_menu() not found", p_ext->psz_name );
878 /* Pass id as unique argument to the function */
879 lua_pushinteger( L, id );
881 if( lua_pcall( L, 1, 1, 0 ) != 0 )
883 msg_Warn( p_mgr, "Error while runing script %s, "
884 "function trigger_menu(): %s", p_ext->psz_name,
885 lua_tostring( L, lua_gettop( L ) ) );
889 i_ret = lua_DialogFlush( L );
890 if( i_ret < VLC_SUCCESS )
892 msg_Dbg( p_mgr, "Something went wrong in %s (%s:%d)",
893 __func__, __FILE__, __LINE__ );
898 /** Directly trigger an extension, without activating it
899 * This is NOT multithreaded, and this code runs in the UI thread
901 * @param p_ext Extension to trigger
902 * @return Value returned by the lua function "trigger"
904 static int TriggerExtension( extensions_manager_t *p_mgr,
907 int i_ret = lua_ExecuteFunction( p_mgr, p_ext, "trigger", LUA_END );
909 /* Close lua state for trigger-only extensions */
910 if( p_ext->p_sys->L )
911 lua_close( p_ext->p_sys->L );
912 p_ext->p_sys->L = NULL;
917 /** Set extension associated to the current script
918 * @param L current lua_State
919 * @param p_ext the extension
921 void vlclua_extension_set( lua_State *L, extension_t *p_ext )
923 lua_pushlightuserdata( L, vlclua_extension_set );
924 lua_pushlightuserdata( L, p_ext );
925 lua_rawset( L, LUA_REGISTRYINDEX );
928 /** Retrieve extension associated to the current script
929 * @param L current lua_State
930 * @return Extension pointer
932 extension_t *vlclua_extension_get( lua_State *L )
934 lua_pushlightuserdata( L, vlclua_extension_set );
935 lua_rawget( L, LUA_REGISTRYINDEX );
936 extension_t *p_ext = (extension_t*) lua_topointer( L, -1 );
941 /** Deactivate an extension by order from the extension itself
943 * @note This is an asynchronous call. A script calling vlc.deactivate() will
944 * be executed to the end before the last call to deactivate() is done.
946 int vlclua_extension_deactivate( lua_State *L )
948 extension_t *p_ext = vlclua_extension_get( L );
949 int i_ret = Deactivate( p_ext->p_sys->p_mgr, p_ext );
950 return ( i_ret == VLC_SUCCESS ) ? 1 : 0;
953 /** Callback for the variable "dialog-event"
954 * @param p_this Current object owner of the extension and the dialog
955 * @param psz_var "dialog-event"
956 * @param oldval Unused
957 * @param newval Address of the dialog
958 * @param p_data Unused
960 static int vlclua_extension_dialog_callback( vlc_object_t *p_this,
966 /* psz_var == "dialog-event" */
971 extension_dialog_command_t *command = newval.p_address;
972 assert( command != NULL );
973 assert( command->p_dlg != NULL);
975 extension_t *p_ext = command->p_dlg->p_sys;
976 assert( p_ext != NULL );
978 extension_widget_t *p_widget = command->p_data;
980 switch( command->event )
982 case EXTENSION_EVENT_CLICK:
983 assert( p_widget != NULL );
984 PushCommand( p_ext, CMD_CLICK, p_widget );
986 case EXTENSION_EVENT_CLOSE:
987 PushCommandUnique( p_ext, CMD_CLOSE );
990 msg_Dbg( p_this, "Received unknown UI event %d, discarded",
998 /** Callback on vlc_InputItemMetaChanged event
1000 static void inputItemMetaChanged( const vlc_event_t *p_event,
1003 assert( p_event && p_event->type == vlc_InputItemMetaChanged );
1005 extension_t *p_ext = ( extension_t* ) data;
1006 assert( p_ext != NULL );
1008 PushCommandUnique( p_ext, CMD_UPDATE_META );
1011 /* Lock this extension. Can fail. */
1012 bool LockExtension( extension_t *p_ext )
1014 if( p_ext->p_sys->b_exiting )
1017 vlc_mutex_lock( &p_ext->p_sys->running_lock );
1018 if( p_ext->p_sys->b_exiting )
1020 vlc_mutex_unlock( &p_ext->p_sys->running_lock );
1027 void UnlockExtension( extension_t *p_ext )
1029 vlc_mutex_unlock( &p_ext->p_sys->running_lock );