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_interface.h>
40 #include <vlc_events.h>
41 #include <vlc_dialog.h>
43 /* Functions to register */
44 static const luaL_Reg p_reg
[] =
50 * Extensions capabilities
51 * Note: #define and ppsz_capabilities must be in sync
53 static const char caps
[][20] = {
54 #define EXT_HAS_MENU (1 << 0) ///< Hook: menu
56 #define EXT_TRIGGER_ONLY (1 << 1) ///< Hook: trigger. Not activable
58 #define EXT_INPUT_LISTENER (1 << 2) ///< Hook: input_changed
60 #define EXT_META_LISTENER (1 << 3) ///< Hook: meta_changed
62 #define EXT_PLAYING_LISTENER (1 << 4) ///< Hook: status_changed
66 static int ScanExtensions( extensions_manager_t
*p_this
);
67 static int ScanLuaCallback( vlc_object_t
*p_this
, const char *psz_script
,
68 const struct luabatch_context_t
* );
69 static int Control( extensions_manager_t
*, int, va_list );
70 static int GetMenuEntries( extensions_manager_t
*p_mgr
, extension_t
*p_ext
,
71 char ***pppsz_titles
, uint16_t **ppi_ids
);
72 static lua_State
* GetLuaState( extensions_manager_t
*p_mgr
,
74 static int TriggerMenu( extension_t
*p_ext
, int id
);
75 static int TriggerExtension( extensions_manager_t
*p_mgr
,
77 static void WatchTimerCallback( void* );
79 static int vlclua_extension_deactivate( lua_State
*L
);
80 static int vlclua_extension_keep_alive( lua_State
*L
);
83 static int vlclua_extension_dialog_callback( vlc_object_t
*p_this
,
89 /* Input item callback: vlc_InputItemMetaChanged */
90 static void inputItemMetaChanged( const vlc_event_t
*p_event
,
97 int Open_Extension( vlc_object_t
*p_this
)
99 msg_Dbg( p_this
, "Opening Lua Extension module" );
101 extensions_manager_t
*p_mgr
= ( extensions_manager_t
* ) p_this
;
103 p_mgr
->pf_control
= Control
;
106 vlc_mutex_init( &p_mgr
->lock
);
108 /* Scan available Lua Extensions */
109 if( ScanExtensions( p_mgr
) != VLC_SUCCESS
)
111 msg_Err( p_mgr
, "Can't load extensions modules" );
115 // Create the dialog-event variable
116 var_Create( p_this
, "dialog-event", VLC_VAR_ADDRESS
);
117 var_AddCallback( p_this
, "dialog-event",
118 vlclua_extension_dialog_callback
, NULL
);
124 * Module unload function
126 void Close_Extension( vlc_object_t
*p_this
)
128 extensions_manager_t
*p_mgr
= ( extensions_manager_t
* ) p_this
;
130 var_DelCallback( p_this
, "dialog-event",
131 vlclua_extension_dialog_callback
, NULL
);
132 var_Destroy( p_mgr
, "dialog-event" );
134 extension_t
*p_ext
= NULL
;
136 /* Free extensions' memory */
137 FOREACH_ARRAY( p_ext
, p_mgr
->extensions
)
142 vlc_mutex_lock( &p_ext
->p_sys
->command_lock
);
143 if( p_ext
->p_sys
->b_activated
== true && p_ext
->p_sys
->p_progress_id
== NULL
)
145 p_ext
->p_sys
->b_exiting
= true;
146 // QueueDeactivateCommand will signal the wait condition.
147 QueueDeactivateCommand( p_ext
);
151 if ( p_ext
->p_sys
->L
!= NULL
)
152 vlclua_fd_interrupt( &p_ext
->p_sys
->dtable
);
153 // however here we need to manually signal the wait cond, since no command is queued.
154 p_ext
->p_sys
->b_exiting
= true;
155 vlc_cond_signal( &p_ext
->p_sys
->wait
);
157 vlc_mutex_unlock( &p_ext
->p_sys
->command_lock
);
159 if( p_ext
->p_sys
->b_thread_running
== true )
160 vlc_join( p_ext
->p_sys
->thread
, NULL
);
162 /* Clear Lua State */
163 if( p_ext
->p_sys
->L
)
165 lua_close( p_ext
->p_sys
->L
);
166 vlclua_fd_cleanup( &p_ext
->p_sys
->dtable
);
169 free( p_ext
->psz_name
);
170 free( p_ext
->psz_title
);
171 free( p_ext
->psz_author
);
172 free( p_ext
->psz_description
);
173 free( p_ext
->psz_shortdescription
);
174 free( p_ext
->psz_url
);
175 free( p_ext
->psz_version
);
176 free( p_ext
->p_icondata
);
178 vlc_mutex_destroy( &p_ext
->p_sys
->running_lock
);
179 vlc_mutex_destroy( &p_ext
->p_sys
->command_lock
);
180 vlc_cond_destroy( &p_ext
->p_sys
->wait
);
181 vlc_timer_destroy( p_ext
->p_sys
->timer
);
183 free( p_ext
->p_sys
);
188 vlc_mutex_destroy( &p_mgr
->lock
);
190 ARRAY_RESET( p_mgr
->extensions
);
194 * Batch scan all Lua files in folder "extensions"
195 * @param p_mgr This extensions_manager_t object
197 static int ScanExtensions( extensions_manager_t
*p_mgr
)
200 vlclua_scripts_batch_execute( VLC_OBJECT( p_mgr
),
212 * Dummy Lua function: does nothing
213 * @note This function can be used to replace "require" while scanning for
215 * Even the built-in libraries are not loaded when calling descriptor()
217 static int vlclua_dummy_require( lua_State
*L
)
224 * Replacement for "require", adding support for packaged extensions
225 * @note Loads modules in the modules/ folder of a package
226 * @note Try first with .luac and then with .lua
228 static int vlclua_extension_require( lua_State
*L
)
230 const char *psz_module
= luaL_checkstring( L
, 1 );
231 vlc_object_t
*p_this
= vlclua_get_this( L
);
232 extension_t
*p_ext
= vlclua_extension_get( L
);
233 msg_Dbg( p_this
, "loading module '%s' from extension package",
235 char *psz_fullpath
, *psz_package
, *sep
;
236 psz_package
= strdup( p_ext
->psz_name
);
237 sep
= strrchr( psz_package
, '/' );
241 return luaL_error( L
, "could not find package name" );
244 if( -1 == asprintf( &psz_fullpath
,
245 "%s/modules/%s.luac", psz_package
, psz_module
) )
250 int i_ret
= vlclua_dofile( p_this
, L
, psz_fullpath
);
253 // Remove trailing 'c' --> try with .lua script
254 psz_fullpath
[ strlen( psz_fullpath
) - 1 ] = '\0';
255 i_ret
= vlclua_dofile( p_this
, L
, psz_fullpath
);
257 free( psz_fullpath
);
261 return luaL_error( L
, "unable to load module '%s' from package",
268 * Batch scan all Lua files in folder "extensions": callback
269 * @param p_this This extensions_manager_t object
270 * @param psz_filename Name of the script to run
271 * @param L Lua State, common to all scripts here
272 * @param dummy: unused
274 int ScanLuaCallback( vlc_object_t
*p_this
, const char *psz_filename
,
275 const struct luabatch_context_t
*dummy
)
278 extensions_manager_t
*p_mgr
= ( extensions_manager_t
* ) p_this
;
281 msg_Dbg( p_mgr
, "Scanning Lua script %s", psz_filename
);
283 /* Experimental: read .vle packages (Zip archives) */
284 char *psz_script
= NULL
;
285 int i_flen
= strlen( psz_filename
);
286 if( !strncasecmp( psz_filename
+ i_flen
- 4, ".vle", 4 ) )
288 msg_Dbg( p_this
, "reading Lua script in a zip archive" );
289 psz_script
= calloc( 1, i_flen
+ 6 + 12 + 1 );
292 strcpy( psz_script
, "zip://" );
293 strncat( psz_script
, psz_filename
, i_flen
+ 19 );
294 strncat( psz_script
, "!/script.lua", i_flen
+ 19 );
298 psz_script
= strdup( psz_filename
);
303 /* Create new script descriptor */
304 extension_t
*p_ext
= ( extension_t
* ) calloc( 1, sizeof( extension_t
) );
311 p_ext
->psz_name
= psz_script
;
312 p_ext
->p_sys
= (extension_sys_t
*) calloc( 1, sizeof( extension_sys_t
) );
313 if( !p_ext
->p_sys
|| !p_ext
->psz_name
)
315 free( p_ext
->psz_name
);
316 free( p_ext
->p_sys
);
320 p_ext
->p_sys
->p_mgr
= p_mgr
;
323 if( vlc_timer_create( &p_ext
->p_sys
->timer
, WatchTimerCallback
, p_ext
) )
325 free( p_ext
->psz_name
);
326 free( p_ext
->p_sys
);
331 /* Mutexes and conditions */
332 vlc_mutex_init( &p_ext
->p_sys
->command_lock
);
333 vlc_mutex_init( &p_ext
->p_sys
->running_lock
);
334 vlc_cond_init( &p_ext
->p_sys
->wait
);
336 /* Prepare Lua state */
337 lua_State
*L
= luaL_newstate();
338 lua_register( L
, "require", &vlclua_dummy_require
);
341 if( vlclua_dofile( p_this
, L
, psz_script
) ) // luaL_dofile
343 msg_Warn( p_mgr
, "Error loading script %s: %s", psz_script
,
344 lua_tostring( L
, lua_gettop( L
) ) );
349 /* Scan script for capabilities */
350 lua_getglobal( L
, "descriptor" );
352 if( !lua_isfunction( L
, -1 ) )
354 msg_Warn( p_mgr
, "Error while running script %s, "
355 "function descriptor() not found", psz_script
);
359 if( lua_pcall( L
, 0, 1, 0 ) )
361 msg_Warn( p_mgr
, "Error while running script %s, "
362 "function descriptor(): %s", psz_script
,
363 lua_tostring( L
, lua_gettop( L
) ) );
367 if( lua_gettop( L
) )
369 if( lua_istable( L
, -1 ) )
372 lua_getfield( L
, -1, "capabilities" );
373 if( lua_istable( L
, -1 ) )
376 while( lua_next( L
, -2 ) != 0 )
378 /* Key is at index -2 and value at index -1. Discard key */
379 const char *psz_cap
= luaL_checkstring( L
, -1 );
381 /* Find this capability's flag */
382 for( size_t i
= 0; i
< sizeof(caps
)/sizeof(caps
[0]); i
++ )
384 if( !strcmp( caps
[i
], psz_cap
) )
387 p_ext
->p_sys
->i_capabilities
|= 1 << i
;
394 msg_Warn( p_mgr
, "Extension capability '%s' unknown in"
395 " script %s", psz_cap
, psz_script
);
397 /* Removes 'value'; keeps 'key' for next iteration */
403 msg_Warn( p_mgr
, "In script %s, function descriptor() "
404 "did not return a table of capabilities.",
410 lua_getfield( L
, -1, "title" );
411 if( lua_isstring( L
, -1 ) )
413 p_ext
->psz_title
= strdup( luaL_checkstring( L
, -1 ) );
417 msg_Dbg( p_mgr
, "In script %s, function descriptor() "
418 "did not return a string as title.",
420 p_ext
->psz_title
= strdup( psz_script
);
425 lua_getfield( L
, -1, "author" );
426 p_ext
->psz_author
= luaL_strdupornull( L
, -1 );
429 /* Get description */
430 lua_getfield( L
, -1, "description" );
431 p_ext
->psz_description
= luaL_strdupornull( L
, -1 );
434 /* Get short description */
435 lua_getfield( L
, -1, "shortdesc" );
436 p_ext
->psz_shortdescription
= luaL_strdupornull( L
, -1 );
440 lua_getfield( L
, -1, "url" );
441 p_ext
->psz_url
= luaL_strdupornull( L
, -1 );
445 lua_getfield( L
, -1, "version" );
446 p_ext
->psz_version
= luaL_strdupornull( L
, -1 );
450 lua_getfield( L
, -1, "icon" );
451 if( !lua_isnil( L
, -1 ) && lua_isstring( L
, -1 ) )
453 int len
= lua_strlen( L
, -1 );
454 p_ext
->p_icondata
= malloc( len
);
455 if( p_ext
->p_icondata
)
457 p_ext
->i_icondata_size
= len
;
458 memcpy( p_ext
->p_icondata
, lua_tostring( L
, -1 ), len
);
465 msg_Warn( p_mgr
, "In script %s, function descriptor() "
466 "did not return a table!", psz_script
);
472 msg_Err( p_mgr
, "Script %s went completely foobar", psz_script
);
476 msg_Dbg( p_mgr
, "Script %s has the following capability flags: 0x%x",
477 psz_script
, p_ext
->p_sys
->i_capabilities
);
484 free( p_ext
->psz_name
);
485 free( p_ext
->psz_title
);
486 free( p_ext
->psz_url
);
487 free( p_ext
->psz_author
);
488 free( p_ext
->psz_description
);
489 free( p_ext
->psz_shortdescription
);
490 free( p_ext
->psz_version
);
491 vlc_mutex_destroy( &p_ext
->p_sys
->command_lock
);
492 vlc_mutex_destroy( &p_ext
->p_sys
->running_lock
);
493 vlc_cond_destroy( &p_ext
->p_sys
->wait
);
494 free( p_ext
->p_sys
);
499 /* Add the extension to the list of known extensions */
500 ARRAY_APPEND( p_mgr
->extensions
, p_ext
);
503 /* Continue batch execution */
507 static int Control( extensions_manager_t
*p_mgr
, int i_control
, va_list args
)
509 extension_t
*p_ext
= NULL
;
511 uint16_t **ppus
= NULL
;
512 char ***pppsz
= NULL
;
517 case EXTENSION_ACTIVATE
:
518 p_ext
= ( extension_t
* ) va_arg( args
, extension_t
* );
519 return Activate( p_mgr
, p_ext
);
521 case EXTENSION_DEACTIVATE
:
522 p_ext
= ( extension_t
* ) va_arg( args
, extension_t
* );
523 return Deactivate( p_mgr
, p_ext
);
525 case EXTENSION_IS_ACTIVATED
:
526 p_ext
= ( extension_t
* ) va_arg( args
, extension_t
* );
527 pb
= ( bool* ) va_arg( args
, bool* );
528 vlc_mutex_lock( &p_ext
->p_sys
->command_lock
);
529 *pb
= p_ext
->p_sys
->b_activated
;
530 vlc_mutex_unlock( &p_ext
->p_sys
->command_lock
);
533 case EXTENSION_HAS_MENU
:
534 p_ext
= ( extension_t
* ) va_arg( args
, extension_t
* );
535 pb
= ( bool* ) va_arg( args
, bool* );
536 *pb
= ( p_ext
->p_sys
->i_capabilities
& EXT_HAS_MENU
) ? 1 : 0;
539 case EXTENSION_GET_MENU
:
540 p_ext
= ( extension_t
* ) va_arg( args
, extension_t
* );
541 pppsz
= ( char*** ) va_arg( args
, char*** );
542 ppus
= ( uint16_t** ) va_arg( args
, uint16_t** );
545 return GetMenuEntries( p_mgr
, p_ext
, pppsz
, ppus
);
547 case EXTENSION_TRIGGER_ONLY
:
548 p_ext
= ( extension_t
* ) va_arg( args
, extension_t
* );
549 pb
= ( bool* ) va_arg( args
, bool* );
550 *pb
= ( p_ext
->p_sys
->i_capabilities
& EXT_TRIGGER_ONLY
) ? 1 : 0;
553 case EXTENSION_TRIGGER
:
554 p_ext
= ( extension_t
* ) va_arg( args
, extension_t
* );
555 return TriggerExtension( p_mgr
, p_ext
);
557 case EXTENSION_TRIGGER_MENU
:
558 p_ext
= ( extension_t
* ) va_arg( args
, extension_t
* );
559 // GCC: 'uint16_t' is promoted to 'int' when passed through '...'
560 i
= ( int ) va_arg( args
, int );
561 return TriggerMenu( p_ext
, i
);
563 case EXTENSION_SET_INPUT
:
565 p_ext
= ( extension_t
* ) va_arg( args
, extension_t
* );
566 input_thread_t
*p_input
= va_arg( args
, struct input_thread_t
* );
570 vlc_mutex_lock( &p_ext
->p_sys
->command_lock
);
571 if ( p_ext
->p_sys
->b_exiting
== true )
573 vlc_mutex_unlock( &p_ext
->p_sys
->command_lock
);
576 vlc_mutex_unlock( &p_ext
->p_sys
->command_lock
);
578 vlc_mutex_lock( &p_ext
->p_sys
->running_lock
);
581 input_thread_t
*old
= p_ext
->p_sys
->p_input
;
582 input_item_t
*p_item
;
585 // Untrack meta fetched events
586 if( p_ext
->p_sys
->i_capabilities
& EXT_META_LISTENER
)
588 p_item
= input_GetItem( old
);
589 vlc_event_detach( &p_item
->event_manager
,
590 vlc_InputItemMetaChanged
,
591 inputItemMetaChanged
,
593 input_item_Release( p_item
);
595 vlc_object_release( old
);
598 p_ext
->p_sys
->p_input
= p_input
? vlc_object_hold( p_input
)
601 // Tell the script the input changed
602 if( p_ext
->p_sys
->i_capabilities
& EXT_INPUT_LISTENER
)
604 PushCommandUnique( p_ext
, CMD_SET_INPUT
);
607 // Track meta fetched events
608 if( p_ext
->p_sys
->p_input
&&
609 p_ext
->p_sys
->i_capabilities
& EXT_META_LISTENER
)
611 p_item
= input_GetItem( p_ext
->p_sys
->p_input
);
612 input_item_Hold( p_item
);
613 vlc_event_attach( &p_item
->event_manager
,
614 vlc_InputItemMetaChanged
,
615 inputItemMetaChanged
,
619 vlc_mutex_unlock( &p_ext
->p_sys
->running_lock
);
622 case EXTENSION_PLAYING_CHANGED
:
625 p_ext
= ( extension_t
* ) va_arg( args
, extension_t
* );
626 assert( p_ext
->psz_name
!= NULL
);
627 i
= ( int ) va_arg( args
, int );
628 if( p_ext
->p_sys
->i_capabilities
& EXT_PLAYING_LISTENER
)
630 PushCommand( p_ext
, CMD_PLAYING_CHANGED
, i
);
634 case EXTENSION_META_CHANGED
:
637 p_ext
= ( extension_t
* ) va_arg( args
, extension_t
* );
638 PushCommand( p_ext
, CMD_UPDATE_META
);
642 msg_Warn( p_mgr
, "Control '%d' not yet implemented in Extension",
650 int lua_ExtensionActivate( extensions_manager_t
*p_mgr
, extension_t
*p_ext
)
652 assert( p_mgr
!= NULL
&& p_ext
!= NULL
);
653 return lua_ExecuteFunction( p_mgr
, p_ext
, "activate", LUA_END
);
656 int lua_ExtensionDeactivate( extensions_manager_t
*p_mgr
, extension_t
*p_ext
)
658 assert( p_mgr
!= NULL
&& p_ext
!= NULL
);
660 if( p_ext
->p_sys
->b_activated
== false )
663 vlclua_fd_interrupt( &p_ext
->p_sys
->dtable
);
665 // Unset and release input objects
666 if( p_ext
->p_sys
->p_input
)
668 if( p_ext
->p_sys
->i_capabilities
& EXT_META_LISTENER
)
671 input_item_t
*p_item
= input_GetItem( p_ext
->p_sys
->p_input
);
672 input_item_Release( p_item
);
674 vlc_object_release( p_ext
->p_sys
->p_input
);
675 p_ext
->p_sys
->p_input
= NULL
;
678 return lua_ExecuteFunction( p_mgr
, p_ext
, "deactivate", LUA_END
);
681 int lua_ExtensionWidgetClick( extensions_manager_t
*p_mgr
,
683 extension_widget_t
*p_widget
)
685 if( !p_ext
->p_sys
->L
)
688 lua_State
*L
= GetLuaState( p_mgr
, p_ext
);
689 lua_pushlightuserdata( L
, p_widget
);
690 lua_gettable( L
, LUA_REGISTRYINDEX
);
691 return lua_ExecuteFunction( p_mgr
, p_ext
, NULL
, LUA_END
);
696 * Get the list of menu entries from an extension script
699 * @param pppsz_titles Pointer to NULL. All strings must be freed by the caller
700 * @param ppi_ids Pointer to NULL. Must be freed by the caller.
701 * @note This function is allowed to run in the UI thread. This means
702 * that it MUST respond very fast.
703 * @todo Remove the menu() hook and provide a new function vlc.set_menu()
705 static int GetMenuEntries( extensions_manager_t
*p_mgr
, extension_t
*p_ext
,
706 char ***pppsz_titles
, uint16_t **ppi_ids
)
708 assert( *pppsz_titles
== NULL
);
709 assert( *ppi_ids
== NULL
);
711 vlc_mutex_lock( &p_ext
->p_sys
->command_lock
);
712 if( p_ext
->p_sys
->b_activated
== false || p_ext
->p_sys
->b_exiting
== true )
714 vlc_mutex_unlock( &p_ext
->p_sys
->command_lock
);
715 msg_Dbg( p_mgr
, "Can't get menu of an unactivated/dying extension!" );
718 vlc_mutex_unlock( &p_ext
->p_sys
->command_lock
);
720 vlc_mutex_lock( &p_ext
->p_sys
->running_lock
);
722 int i_ret
= VLC_EGENERIC
;
723 lua_State
*L
= GetLuaState( p_mgr
, p_ext
);
725 if( ( p_ext
->p_sys
->i_capabilities
& EXT_HAS_MENU
) == 0 )
727 msg_Dbg( p_mgr
, "can't get a menu from an extension without menu!" );
731 lua_getglobal( L
, "menu" );
733 if( !lua_isfunction( L
, -1 ) )
735 msg_Warn( p_mgr
, "Error while running script %s, "
736 "function menu() not found", p_ext
->psz_name
);
740 if( lua_pcall( L
, 0, 1, 0 ) )
742 msg_Warn( p_mgr
, "Error while running script %s, "
743 "function menu(): %s", p_ext
->psz_name
,
744 lua_tostring( L
, lua_gettop( L
) ) );
748 if( lua_gettop( L
) )
750 if( lua_istable( L
, -1 ) )
753 size_t i_size
= lua_objlen( L
, -1 );
754 *pppsz_titles
= ( char** ) calloc( i_size
+1, sizeof( char* ) );
755 *ppi_ids
= ( uint16_t* ) calloc( i_size
+1, sizeof( uint16_t ) );
760 while( lua_next( L
, -2 ) != 0 )
762 assert( i_idx
< i_size
);
763 if( (!lua_isstring( L
, -1 )) || (!lua_isnumber( L
, -2 )) )
765 msg_Warn( p_mgr
, "In script %s, an entry in "
766 "the menu table is invalid!", p_ext
->psz_name
);
769 (*pppsz_titles
)[ i_idx
] = strdup( luaL_checkstring( L
, -1 ) );
770 (*ppi_ids
)[ i_idx
] = luaL_checkinteger( L
, -2 ) & 0xFFFF;
777 msg_Warn( p_mgr
, "Function menu() in script %s "
778 "did not return a table", p_ext
->psz_name
);
784 msg_Warn( p_mgr
, "Script %s went completely foobar", p_ext
->psz_name
);
791 vlc_mutex_unlock( &p_ext
->p_sys
->running_lock
);
792 if( i_ret
!= VLC_SUCCESS
)
794 msg_Dbg( p_mgr
, "Something went wrong in %s (%s:%d)",
795 __func__
, __FILE__
, __LINE__
);
800 /* Must be entered with the Lock on Extension */
801 static lua_State
* GetLuaState( extensions_manager_t
*p_mgr
,
804 assert( p_ext
!= NULL
);
805 lua_State
*L
= p_ext
->p_sys
->L
;
812 msg_Err( p_mgr
, "Could not create new Lua State" );
815 vlclua_set_this( L
, p_mgr
);
816 vlclua_set_playlist_internal( L
,
817 pl_Get((intf_thread_t
*)(p_mgr
->obj
.parent
)) );
818 vlclua_extension_set( L
, p_ext
);
821 luaL_register_namespace( L
, "vlc", p_reg
);
824 /* Load more libraries */
826 luaopen_dialog( L
, p_ext
);
829 if( vlclua_fd_init( L
, &p_ext
->p_sys
->dtable
) )
836 luaopen_playlist( L
);
837 luaopen_sd_intf( L
);
839 luaopen_strings( L
);
840 luaopen_variables( L
);
845 #if defined(_WIN32) && !VLC_WINSTORE_APP
849 /* Register extension specific functions */
850 lua_getglobal( L
, "vlc" );
851 lua_pushcfunction( L
, vlclua_extension_deactivate
);
852 lua_setfield( L
, -2, "deactivate" );
853 lua_pushcfunction( L
, vlclua_extension_keep_alive
);
854 lua_setfield( L
, -2, "keep_alive" );
856 /* Setup the module search path */
857 if( !strncmp( p_ext
->psz_name
, "zip://", 6 ) )
859 /* Load all required modules manually */
860 lua_register( L
, "require", &vlclua_extension_require
);
864 if( vlclua_add_modules_path( L
, p_ext
->psz_name
) )
866 msg_Warn( p_mgr
, "Error while setting the module "
867 "search path for %s", p_ext
->psz_name
);
868 vlclua_fd_cleanup( &p_ext
->p_sys
->dtable
);
873 /* Load and run the script(s) */
874 if( vlclua_dofile( VLC_OBJECT( p_mgr
), L
, p_ext
->psz_name
) )
876 msg_Warn( p_mgr
, "Error loading script %s: %s", p_ext
->psz_name
,
877 lua_tostring( L
, lua_gettop( L
) ) );
878 vlclua_fd_cleanup( &p_ext
->p_sys
->dtable
);
889 int lua_ExecuteFunction( extensions_manager_t
*p_mgr
, extension_t
*p_ext
,
890 const char *psz_function
, ... )
893 va_start( args
, psz_function
);
894 int i_ret
= lua_ExecuteFunctionVa( p_mgr
, p_ext
, psz_function
, args
);
900 * Execute a function in a Lua script
901 * @param psz_function Name of global function to execute. If NULL, assume
902 * that the function object is already on top of the
904 * @return < 0 in case of failure, >= 0 in case of success
905 * @note It's better to call this function from a dedicated thread
906 * (see extension_thread.c)
908 int lua_ExecuteFunctionVa( extensions_manager_t
*p_mgr
, extension_t
*p_ext
,
909 const char *psz_function
, va_list args
)
911 int i_ret
= VLC_SUCCESS
;
913 assert( p_mgr
!= NULL
);
914 assert( p_ext
!= NULL
);
916 lua_State
*L
= GetLuaState( p_mgr
, p_ext
);
921 lua_getglobal( L
, psz_function
);
923 if( !lua_isfunction( L
, -1 ) )
925 msg_Warn( p_mgr
, "Error while running script %s, "
926 "function %s() not found", p_ext
->psz_name
, psz_function
);
931 lua_datatype_e type
= LUA_END
;
932 while( ( type
= va_arg( args
, int ) ) != LUA_END
)
934 if( type
== LUA_NUM
)
936 lua_pushnumber( L
, ( int ) va_arg( args
, int ) );
938 else if( type
== LUA_TEXT
)
940 lua_pushstring( L
, ( char * ) va_arg( args
, char* ) );
944 msg_Warn( p_mgr
, "Undefined argument type %d to lua function %s"
945 "from script %s", type
, psz_function
, p_ext
->psz_name
);
947 lua_pop( L
, i_args
);
953 // Start actual call to Lua
954 if( lua_pcall( L
, i_args
, 1, 0 ) )
956 msg_Warn( p_mgr
, "Error while running script %s, "
957 "function %s(): %s", p_ext
->psz_name
, psz_function
,
958 lua_tostring( L
, lua_gettop( L
) ) );
959 i_ret
= VLC_EGENERIC
;
962 i_ret
|= lua_DialogFlush( L
);
969 static inline int TriggerMenu( extension_t
*p_ext
, int i_id
)
971 return PushCommand( p_ext
, CMD_TRIGGERMENU
, i_id
);
974 int lua_ExtensionTriggerMenu( extensions_manager_t
*p_mgr
,
975 extension_t
*p_ext
, int id
)
977 int i_ret
= VLC_SUCCESS
;
978 lua_State
*L
= GetLuaState( p_mgr
, p_ext
);
983 luaopen_dialog( L
, p_ext
);
985 lua_getglobal( L
, "trigger_menu" );
986 if( !lua_isfunction( L
, -1 ) )
988 msg_Warn( p_mgr
, "Error while running script %s, "
989 "function trigger_menu() not found", p_ext
->psz_name
);
993 /* Pass id as unique argument to the function */
994 lua_pushinteger( L
, id
);
996 if( lua_pcall( L
, 1, 1, 0 ) != 0 )
998 msg_Warn( p_mgr
, "Error while running script %s, "
999 "function trigger_menu(): %s", p_ext
->psz_name
,
1000 lua_tostring( L
, lua_gettop( L
) ) );
1001 i_ret
= VLC_EGENERIC
;
1004 i_ret
|= lua_DialogFlush( L
);
1005 if( i_ret
< VLC_SUCCESS
)
1007 msg_Dbg( p_mgr
, "Something went wrong in %s (%s:%d)",
1008 __func__
, __FILE__
, __LINE__
);
1014 /** Directly trigger an extension, without activating it
1015 * This is NOT multithreaded, and this code runs in the UI thread
1017 * @param p_ext Extension to trigger
1018 * @return Value returned by the lua function "trigger"
1020 static int TriggerExtension( extensions_manager_t
*p_mgr
,
1021 extension_t
*p_ext
)
1023 int i_ret
= lua_ExecuteFunction( p_mgr
, p_ext
, "trigger", LUA_END
);
1025 /* Close lua state for trigger-only extensions */
1026 if( p_ext
->p_sys
->L
)
1028 vlclua_fd_cleanup( &p_ext
->p_sys
->dtable
);
1029 lua_close( p_ext
->p_sys
->L
);
1031 p_ext
->p_sys
->L
= NULL
;
1036 /** Set extension associated to the current script
1037 * @param L current lua_State
1038 * @param p_ext the extension
1040 void vlclua_extension_set( lua_State
*L
, extension_t
*p_ext
)
1042 lua_pushlightuserdata( L
, vlclua_extension_set
);
1043 lua_pushlightuserdata( L
, p_ext
);
1044 lua_rawset( L
, LUA_REGISTRYINDEX
);
1047 /** Retrieve extension associated to the current script
1048 * @param L current lua_State
1049 * @return Extension pointer
1051 extension_t
*vlclua_extension_get( lua_State
*L
)
1053 lua_pushlightuserdata( L
, vlclua_extension_set
);
1054 lua_rawget( L
, LUA_REGISTRYINDEX
);
1055 extension_t
*p_ext
= (extension_t
*) lua_topointer( L
, -1 );
1060 /** Deactivate an extension by order from the extension itself
1061 * @param L lua_State
1062 * @note This is an asynchronous call. A script calling vlc.deactivate() will
1063 * be executed to the end before the last call to deactivate() is done.
1065 int vlclua_extension_deactivate( lua_State
*L
)
1067 extension_t
*p_ext
= vlclua_extension_get( L
);
1068 vlc_mutex_lock( &p_ext
->p_sys
->command_lock
);
1069 bool b_ret
= QueueDeactivateCommand( p_ext
);
1070 vlc_mutex_unlock( &p_ext
->p_sys
->command_lock
);
1071 return ( b_ret
== true ) ? 1 : 0;
1074 /** Keep an extension alive. This resets the watch timer to 0
1075 * @param L lua_State
1076 * @note This is the "vlc.keep_alive()" function
1078 int vlclua_extension_keep_alive( lua_State
*L
)
1080 extension_t
*p_ext
= vlclua_extension_get( L
);
1082 vlc_mutex_lock( &p_ext
->p_sys
->command_lock
);
1083 if( p_ext
->p_sys
->p_progress_id
!= NULL
)
1085 vlc_dialog_release( p_ext
->p_sys
->p_mgr
, p_ext
->p_sys
->p_progress_id
);
1086 p_ext
->p_sys
->p_progress_id
= NULL
;
1088 vlc_timer_schedule( p_ext
->p_sys
->timer
, false, WATCH_TIMER_PERIOD
, 0 );
1089 vlc_mutex_unlock( &p_ext
->p_sys
->command_lock
);
1094 /** Callback for the variable "dialog-event"
1095 * @param p_this Current object owner of the extension and the dialog
1096 * @param psz_var "dialog-event"
1097 * @param oldval Unused
1098 * @param newval Address of the dialog
1099 * @param p_data Unused
1101 static int vlclua_extension_dialog_callback( vlc_object_t
*p_this
,
1102 char const *psz_var
,
1107 /* psz_var == "dialog-event" */
1112 extension_dialog_command_t
*command
= newval
.p_address
;
1113 assert( command
!= NULL
);
1114 assert( command
->p_dlg
!= NULL
);
1116 extension_t
*p_ext
= command
->p_dlg
->p_sys
;
1117 assert( p_ext
!= NULL
);
1119 extension_widget_t
*p_widget
= command
->p_data
;
1121 switch( command
->event
)
1123 case EXTENSION_EVENT_CLICK
:
1124 assert( p_widget
!= NULL
);
1125 PushCommandUnique( p_ext
, CMD_CLICK
, p_widget
);
1127 case EXTENSION_EVENT_CLOSE
:
1128 PushCommandUnique( p_ext
, CMD_CLOSE
);
1131 msg_Dbg( p_this
, "Received unknown UI event %d, discarded",
1139 /** Callback on vlc_InputItemMetaChanged event
1141 static void inputItemMetaChanged( const vlc_event_t
*p_event
,
1144 assert( p_event
&& p_event
->type
== vlc_InputItemMetaChanged
);
1146 extension_t
*p_ext
= ( extension_t
* ) data
;
1147 assert( p_ext
!= NULL
);
1149 PushCommandUnique( p_ext
, CMD_UPDATE_META
);
1152 /** Watch timer callback
1153 * The timer expired, Lua may be stuck, ask the user what to do now
1155 static void WatchTimerCallback( void *data
)
1157 extension_t
*p_ext
= data
;
1158 extensions_manager_t
*p_mgr
= p_ext
->p_sys
->p_mgr
;
1160 vlc_mutex_lock( &p_ext
->p_sys
->command_lock
);
1162 for( struct command_t
*cmd
= p_ext
->p_sys
->command
;
1165 if( cmd
->i_command
== CMD_DEACTIVATE
)
1166 { /* We have a pending Deactivate command... */
1167 if( p_ext
->p_sys
->p_progress_id
!= NULL
)
1169 vlc_dialog_release( p_mgr
, p_ext
->p_sys
->p_progress_id
);
1170 p_ext
->p_sys
->p_progress_id
= NULL
;
1172 KillExtension( p_mgr
, p_ext
);
1173 vlc_mutex_unlock( &p_ext
->p_sys
->command_lock
);
1177 if( p_ext
->p_sys
->p_progress_id
== NULL
)
1179 p_ext
->p_sys
->p_progress_id
=
1180 vlc_dialog_display_progress( p_mgr
, true, 0.0,
1182 _( "Extension not responding!" ),
1183 _( "Extension '%s' does not respond.\n"
1184 "Do you want to kill it now? " ),
1186 if( p_ext
->p_sys
->p_progress_id
== NULL
)
1188 KillExtension( p_mgr
, p_ext
);
1189 vlc_mutex_unlock( &p_ext
->p_sys
->command_lock
);
1192 vlc_timer_schedule( p_ext
->p_sys
->timer
, false, 100000, 0 );
1196 if( vlc_dialog_is_cancelled( p_mgr
, p_ext
->p_sys
->p_progress_id
) )
1198 vlc_dialog_release( p_mgr
, p_ext
->p_sys
->p_progress_id
);
1199 p_ext
->p_sys
->p_progress_id
= NULL
;
1200 KillExtension( p_mgr
, p_ext
);
1201 vlc_mutex_unlock( &p_ext
->p_sys
->command_lock
);
1204 vlc_timer_schedule( p_ext
->p_sys
->timer
, false, 100000, 0 );
1206 vlc_mutex_unlock( &p_ext
->p_sys
->command_lock
);