Qt: correctly translate audio effects strings
[vlc.git] / modules / lua / extension.c
blob10ec8c82cc8137c51e3bb79cf106666de08ac351
1 /*****************************************************************************
2 * extension.c: Lua Extensions (meta data, web information, ...)
3 *****************************************************************************
4 * Copyright (C) 2009-2010 VideoLAN and authors
5 * $Id$
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 *****************************************************************************/
24 #ifndef _GNU_SOURCE
25 # define _GNU_SOURCE
26 #endif
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
32 #include "vlc.h"
33 #include "libs.h"
34 #include "extension.h"
35 #include "assert.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[] =
46 { NULL, NULL }
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
55 "menu",
56 #define EXT_TRIGGER_ONLY (1 << 1) ///< Hook: trigger. Not activable
57 "trigger",
58 #define EXT_INPUT_LISTENER (1 << 2) ///< Hook: input_changed
59 "input-listener",
60 #define EXT_META_LISTENER (1 << 3) ///< Hook: meta_changed
61 "meta-listener",
62 #define EXT_PLAYING_LISTENER (1 << 4) ///< Hook: status_changed
63 "playing-listener",
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,
73 extension_t *p_ext );
74 static int TriggerMenu( extension_t *p_ext, int id );
75 static int TriggerExtension( extensions_manager_t *p_mgr,
76 extension_t *p_ext );
77 static void WatchTimerCallback( void* );
79 static int vlclua_extension_deactivate( lua_State *L );
80 static int vlclua_extension_keep_alive( lua_State *L );
82 /* Interactions */
83 static int vlclua_extension_dialog_callback( vlc_object_t *p_this,
84 char const *psz_var,
85 vlc_value_t oldval,
86 vlc_value_t newval,
87 void *p_data );
89 /* Input item callback: vlc_InputItemMetaChanged */
90 static void inputItemMetaChanged( const vlc_event_t *p_event,
91 void *data );
94 /**
95 * Module entry-point
96 **/
97 int Open_Extension( vlc_object_t *p_this )
99 if( lua_Disabled( p_this ) )
100 return VLC_EGENERIC;
102 msg_Dbg( p_this, "Opening Lua Extension module" );
104 extensions_manager_t *p_mgr = ( extensions_manager_t* ) p_this;
106 p_mgr->pf_control = Control;
108 p_mgr->p_sys = NULL;
109 vlc_mutex_init( &p_mgr->lock );
111 /* Scan available Lua Extensions */
112 if( ScanExtensions( p_mgr ) != VLC_SUCCESS )
114 msg_Err( p_mgr, "Can't load extensions modules" );
115 return VLC_EGENERIC;
118 // Create the dialog-event variable
119 var_Create( p_this, "dialog-event", VLC_VAR_ADDRESS );
120 var_AddCallback( p_this, "dialog-event",
121 vlclua_extension_dialog_callback, NULL );
123 return VLC_SUCCESS;
127 * Module unload function
129 void Close_Extension( vlc_object_t *p_this )
131 extensions_manager_t *p_mgr = ( extensions_manager_t* ) p_this;
133 var_DelCallback( p_this, "dialog-event",
134 vlclua_extension_dialog_callback, NULL );
135 var_Destroy( p_mgr, "dialog-event" );
137 extension_t *p_ext = NULL;
139 /* Free extensions' memory */
140 FOREACH_ARRAY( p_ext, p_mgr->extensions )
142 if( !p_ext )
143 break;
145 vlc_mutex_lock( &p_ext->p_sys->command_lock );
146 if( p_ext->p_sys->b_activated == true && p_ext->p_sys->p_progress_id == NULL )
148 p_ext->p_sys->b_exiting = true;
149 // QueueDeactivateCommand will signal the wait condition.
150 QueueDeactivateCommand( p_ext );
152 else
154 if ( p_ext->p_sys->L != NULL )
155 vlclua_fd_interrupt( &p_ext->p_sys->dtable );
156 // however here we need to manually signal the wait cond, since no command is queued.
157 p_ext->p_sys->b_exiting = true;
158 vlc_cond_signal( &p_ext->p_sys->wait );
160 vlc_mutex_unlock( &p_ext->p_sys->command_lock );
162 if( p_ext->p_sys->b_thread_running == true )
163 vlc_join( p_ext->p_sys->thread, NULL );
165 /* Clear Lua State */
166 if( p_ext->p_sys->L )
168 lua_close( p_ext->p_sys->L );
169 vlclua_fd_cleanup( &p_ext->p_sys->dtable );
172 free( p_ext->psz_name );
173 free( p_ext->psz_title );
174 free( p_ext->psz_author );
175 free( p_ext->psz_description );
176 free( p_ext->psz_shortdescription );
177 free( p_ext->psz_url );
178 free( p_ext->psz_version );
179 free( p_ext->p_icondata );
181 vlc_mutex_destroy( &p_ext->p_sys->running_lock );
182 vlc_mutex_destroy( &p_ext->p_sys->command_lock );
183 vlc_cond_destroy( &p_ext->p_sys->wait );
184 vlc_timer_destroy( p_ext->p_sys->timer );
186 free( p_ext->p_sys );
187 free( p_ext );
189 FOREACH_END()
191 vlc_mutex_destroy( &p_mgr->lock );
193 ARRAY_RESET( p_mgr->extensions );
197 * Batch scan all Lua files in folder "extensions"
198 * @param p_mgr This extensions_manager_t object
200 static int ScanExtensions( extensions_manager_t *p_mgr )
202 int i_ret =
203 vlclua_scripts_batch_execute( VLC_OBJECT( p_mgr ),
204 "extensions",
205 &ScanLuaCallback,
206 NULL );
208 if( !i_ret )
209 return VLC_EGENERIC;
211 return VLC_SUCCESS;
215 * Dummy Lua function: does nothing
216 * @note This function can be used to replace "require" while scanning for
217 * extensions
218 * Even the built-in libraries are not loaded when calling descriptor()
220 static int vlclua_dummy_require( lua_State *L )
222 (void) L;
223 return 0;
227 * Replacement for "require", adding support for packaged extensions
228 * @note Loads modules in the modules/ folder of a package
229 * @note Try first with .luac and then with .lua
231 static int vlclua_extension_require( lua_State *L )
233 const char *psz_module = luaL_checkstring( L, 1 );
234 vlc_object_t *p_this = vlclua_get_this( L );
235 extension_t *p_ext = vlclua_extension_get( L );
236 msg_Dbg( p_this, "loading module '%s' from extension package",
237 psz_module );
238 char *psz_fullpath, *psz_package, *sep;
239 psz_package = strdup( p_ext->psz_name );
240 sep = strrchr( psz_package, '/' );
241 if( !sep )
243 free( psz_package );
244 return luaL_error( L, "could not find package name" );
246 *sep = '\0';
247 if( -1 == asprintf( &psz_fullpath,
248 "%s/modules/%s.luac", psz_package, psz_module ) )
250 free( psz_package );
251 return 1;
253 int i_ret = vlclua_dofile( p_this, L, psz_fullpath );
254 if( i_ret != 0 )
256 // Remove trailing 'c' --> try with .lua script
257 psz_fullpath[ strlen( psz_fullpath ) - 1 ] = '\0';
258 i_ret = vlclua_dofile( p_this, L, psz_fullpath );
260 free( psz_fullpath );
261 free( psz_package );
262 if( i_ret != 0 )
264 return luaL_error( L, "unable to load module '%s' from package",
265 psz_module );
267 return 0;
271 * Batch scan all Lua files in folder "extensions": callback
272 * @param p_this This extensions_manager_t object
273 * @param psz_filename Name of the script to run
274 * @param L Lua State, common to all scripts here
275 * @param dummy: unused
277 int ScanLuaCallback( vlc_object_t *p_this, const char *psz_filename,
278 const struct luabatch_context_t *dummy )
280 VLC_UNUSED(dummy);
281 extensions_manager_t *p_mgr = ( extensions_manager_t* ) p_this;
282 bool b_ok = false;
284 msg_Dbg( p_mgr, "Scanning Lua script %s", psz_filename );
286 /* Experimental: read .vle packages (Zip archives) */
287 char *psz_script = NULL;
288 int i_flen = strlen( psz_filename );
289 if( !strncasecmp( psz_filename + i_flen - 4, ".vle", 4 ) )
291 msg_Dbg( p_this, "reading Lua script in a zip archive" );
292 psz_script = calloc( 1, i_flen + 6 + 12 + 1 );
293 if( !psz_script )
294 return 0;
295 strcpy( psz_script, "zip://" );
296 strncat( psz_script, psz_filename, i_flen + 19 );
297 strncat( psz_script, "!/script.lua", i_flen + 19 );
299 else
301 psz_script = strdup( psz_filename );
302 if( !psz_script )
303 return 0;
306 /* Create new script descriptor */
307 extension_t *p_ext = ( extension_t* ) calloc( 1, sizeof( extension_t ) );
308 if( !p_ext )
310 free( psz_script );
311 return 0;
314 p_ext->psz_name = psz_script;
315 p_ext->p_sys = (extension_sys_t*) calloc( 1, sizeof( extension_sys_t ) );
316 if( !p_ext->p_sys || !p_ext->psz_name )
318 free( p_ext->psz_name );
319 free( p_ext->p_sys );
320 free( p_ext );
321 return 0;
323 p_ext->p_sys->p_mgr = p_mgr;
325 /* Watch timer */
326 if( vlc_timer_create( &p_ext->p_sys->timer, WatchTimerCallback, p_ext ) )
328 free( p_ext->psz_name );
329 free( p_ext->p_sys );
330 free( p_ext );
331 return 0;
334 /* Mutexes and conditions */
335 vlc_mutex_init( &p_ext->p_sys->command_lock );
336 vlc_mutex_init( &p_ext->p_sys->running_lock );
337 vlc_cond_init( &p_ext->p_sys->wait );
339 /* Prepare Lua state */
340 lua_State *L = luaL_newstate();
341 lua_register( L, "require", &vlclua_dummy_require );
343 /* Let's run it */
344 if( vlclua_dofile( p_this, L, psz_script ) ) // luaL_dofile
346 msg_Warn( p_mgr, "Error loading script %s: %s", psz_script,
347 lua_tostring( L, lua_gettop( L ) ) );
348 lua_pop( L, 1 );
349 goto exit;
352 /* Scan script for capabilities */
353 lua_getglobal( L, "descriptor" );
355 if( !lua_isfunction( L, -1 ) )
357 msg_Warn( p_mgr, "Error while running script %s, "
358 "function descriptor() not found", psz_script );
359 goto exit;
362 if( lua_pcall( L, 0, 1, 0 ) )
364 msg_Warn( p_mgr, "Error while running script %s, "
365 "function descriptor(): %s", psz_script,
366 lua_tostring( L, lua_gettop( L ) ) );
367 goto exit;
370 if( lua_gettop( L ) )
372 if( lua_istable( L, -1 ) )
374 /* Get caps */
375 lua_getfield( L, -1, "capabilities" );
376 if( lua_istable( L, -1 ) )
378 lua_pushnil( L );
379 while( lua_next( L, -2 ) != 0 )
381 /* Key is at index -2 and value at index -1. Discard key */
382 const char *psz_cap = luaL_checkstring( L, -1 );
383 bool b_ok = false;
384 /* Find this capability's flag */
385 for( size_t i = 0; i < sizeof(caps)/sizeof(caps[0]); i++ )
387 if( !strcmp( caps[i], psz_cap ) )
389 /* Flag it! */
390 p_ext->p_sys->i_capabilities |= 1 << i;
391 b_ok = true;
392 break;
395 if( !b_ok )
397 msg_Warn( p_mgr, "Extension capability '%s' unknown in"
398 " script %s", psz_cap, psz_script );
400 /* Removes 'value'; keeps 'key' for next iteration */
401 lua_pop( L, 1 );
404 else
406 msg_Warn( p_mgr, "In script %s, function descriptor() "
407 "did not return a table of capabilities.",
408 psz_script );
410 lua_pop( L, 1 );
412 /* Get title */
413 lua_getfield( L, -1, "title" );
414 if( lua_isstring( L, -1 ) )
416 p_ext->psz_title = strdup( luaL_checkstring( L, -1 ) );
418 else
420 msg_Dbg( p_mgr, "In script %s, function descriptor() "
421 "did not return a string as title.",
422 psz_script );
423 p_ext->psz_title = strdup( psz_script );
425 lua_pop( L, 1 );
427 /* Get author */
428 lua_getfield( L, -1, "author" );
429 p_ext->psz_author = luaL_strdupornull( L, -1 );
430 lua_pop( L, 1 );
432 /* Get description */
433 lua_getfield( L, -1, "description" );
434 p_ext->psz_description = luaL_strdupornull( L, -1 );
435 lua_pop( L, 1 );
437 /* Get short description */
438 lua_getfield( L, -1, "shortdesc" );
439 p_ext->psz_shortdescription = luaL_strdupornull( L, -1 );
440 lua_pop( L, 1 );
442 /* Get URL */
443 lua_getfield( L, -1, "url" );
444 p_ext->psz_url = luaL_strdupornull( L, -1 );
445 lua_pop( L, 1 );
447 /* Get version */
448 lua_getfield( L, -1, "version" );
449 p_ext->psz_version = luaL_strdupornull( L, -1 );
450 lua_pop( L, 1 );
452 /* Get icon data */
453 lua_getfield( L, -1, "icon" );
454 if( !lua_isnil( L, -1 ) && lua_isstring( L, -1 ) )
456 int len = lua_strlen( L, -1 );
457 p_ext->p_icondata = malloc( len );
458 if( p_ext->p_icondata )
460 p_ext->i_icondata_size = len;
461 memcpy( p_ext->p_icondata, lua_tostring( L, -1 ), len );
464 lua_pop( L, 1 );
466 else
468 msg_Warn( p_mgr, "In script %s, function descriptor() "
469 "did not return a table!", psz_script );
470 goto exit;
473 else
475 msg_Err( p_mgr, "Script %s went completely foobar", psz_script );
476 goto exit;
479 msg_Dbg( p_mgr, "Script %s has the following capability flags: 0x%x",
480 psz_script, p_ext->p_sys->i_capabilities );
482 b_ok = true;
483 exit:
484 lua_close( L );
485 if( !b_ok )
487 free( p_ext->psz_name );
488 free( p_ext->psz_title );
489 free( p_ext->psz_url );
490 free( p_ext->psz_author );
491 free( p_ext->psz_description );
492 free( p_ext->psz_shortdescription );
493 free( p_ext->psz_version );
494 vlc_mutex_destroy( &p_ext->p_sys->command_lock );
495 vlc_mutex_destroy( &p_ext->p_sys->running_lock );
496 vlc_cond_destroy( &p_ext->p_sys->wait );
497 free( p_ext->p_sys );
498 free( p_ext );
500 else
502 /* Add the extension to the list of known extensions */
503 ARRAY_APPEND( p_mgr->extensions, p_ext );
506 /* Continue batch execution */
507 return VLC_EGENERIC;
510 static int Control( extensions_manager_t *p_mgr, int i_control, va_list args )
512 extension_t *p_ext = NULL;
513 bool *pb = NULL;
514 uint16_t **ppus = NULL;
515 char ***pppsz = NULL;
516 int i = 0;
518 switch( i_control )
520 case EXTENSION_ACTIVATE:
521 p_ext = ( extension_t* ) va_arg( args, extension_t* );
522 return Activate( p_mgr, p_ext );
524 case EXTENSION_DEACTIVATE:
525 p_ext = ( extension_t* ) va_arg( args, extension_t* );
526 return Deactivate( p_mgr, p_ext );
528 case EXTENSION_IS_ACTIVATED:
529 p_ext = ( extension_t* ) va_arg( args, extension_t* );
530 pb = ( bool* ) va_arg( args, bool* );
531 vlc_mutex_lock( &p_ext->p_sys->command_lock );
532 *pb = p_ext->p_sys->b_activated;
533 vlc_mutex_unlock( &p_ext->p_sys->command_lock );
534 break;
536 case EXTENSION_HAS_MENU:
537 p_ext = ( extension_t* ) va_arg( args, extension_t* );
538 pb = ( bool* ) va_arg( args, bool* );
539 *pb = ( p_ext->p_sys->i_capabilities & EXT_HAS_MENU ) ? 1 : 0;
540 break;
542 case EXTENSION_GET_MENU:
543 p_ext = ( extension_t* ) va_arg( args, extension_t* );
544 pppsz = ( char*** ) va_arg( args, char*** );
545 ppus = ( uint16_t** ) va_arg( args, uint16_t** );
546 if( p_ext == NULL )
547 return VLC_EGENERIC;
548 return GetMenuEntries( p_mgr, p_ext, pppsz, ppus );
550 case EXTENSION_TRIGGER_ONLY:
551 p_ext = ( extension_t* ) va_arg( args, extension_t* );
552 pb = ( bool* ) va_arg( args, bool* );
553 *pb = ( p_ext->p_sys->i_capabilities & EXT_TRIGGER_ONLY ) ? 1 : 0;
554 break;
556 case EXTENSION_TRIGGER:
557 p_ext = ( extension_t* ) va_arg( args, extension_t* );
558 return TriggerExtension( p_mgr, p_ext );
560 case EXTENSION_TRIGGER_MENU:
561 p_ext = ( extension_t* ) va_arg( args, extension_t* );
562 // GCC: 'uint16_t' is promoted to 'int' when passed through '...'
563 i = ( int ) va_arg( args, int );
564 return TriggerMenu( p_ext, i );
566 case EXTENSION_SET_INPUT:
568 p_ext = ( extension_t* ) va_arg( args, extension_t* );
569 input_thread_t *p_input = va_arg( args, struct input_thread_t * );
571 if( p_ext == NULL )
572 return VLC_EGENERIC;
573 vlc_mutex_lock( &p_ext->p_sys->command_lock );
574 if ( p_ext->p_sys->b_exiting == true )
576 vlc_mutex_unlock( &p_ext->p_sys->command_lock );
577 return VLC_EGENERIC;
579 vlc_mutex_unlock( &p_ext->p_sys->command_lock );
581 vlc_mutex_lock( &p_ext->p_sys->running_lock );
583 // Change input
584 input_thread_t *old = p_ext->p_sys->p_input;
585 input_item_t *p_item;
586 if( old )
588 // Untrack meta fetched events
589 if( p_ext->p_sys->i_capabilities & EXT_META_LISTENER )
591 p_item = input_GetItem( old );
592 vlc_event_detach( &p_item->event_manager,
593 vlc_InputItemMetaChanged,
594 inputItemMetaChanged,
595 p_ext );
596 input_item_Release( p_item );
598 vlc_object_release( old );
601 p_ext->p_sys->p_input = p_input ? vlc_object_hold( p_input )
602 : p_input;
604 // Tell the script the input changed
605 if( p_ext->p_sys->i_capabilities & EXT_INPUT_LISTENER )
607 PushCommandUnique( p_ext, CMD_SET_INPUT );
610 // Track meta fetched events
611 if( p_ext->p_sys->p_input &&
612 p_ext->p_sys->i_capabilities & EXT_META_LISTENER )
614 p_item = input_GetItem( p_ext->p_sys->p_input );
615 input_item_Hold( p_item );
616 vlc_event_attach( &p_item->event_manager,
617 vlc_InputItemMetaChanged,
618 inputItemMetaChanged,
619 p_ext );
622 vlc_mutex_unlock( &p_ext->p_sys->running_lock );
623 break;
625 case EXTENSION_PLAYING_CHANGED:
627 extension_t *p_ext;
628 p_ext = ( extension_t* ) va_arg( args, extension_t* );
629 assert( p_ext->psz_name != NULL );
630 i = ( int ) va_arg( args, int );
631 if( p_ext->p_sys->i_capabilities & EXT_PLAYING_LISTENER )
633 PushCommand( p_ext, CMD_PLAYING_CHANGED, i );
635 break;
637 case EXTENSION_META_CHANGED:
639 extension_t *p_ext;
640 p_ext = ( extension_t* ) va_arg( args, extension_t* );
641 PushCommand( p_ext, CMD_UPDATE_META );
642 break;
644 default:
645 msg_Warn( p_mgr, "Control '%d' not yet implemented in Extension",
646 i_control );
647 return VLC_EGENERIC;
650 return VLC_SUCCESS;
653 int lua_ExtensionActivate( extensions_manager_t *p_mgr, extension_t *p_ext )
655 assert( p_mgr != NULL && p_ext != NULL );
656 return lua_ExecuteFunction( p_mgr, p_ext, "activate", LUA_END );
659 int lua_ExtensionDeactivate( extensions_manager_t *p_mgr, extension_t *p_ext )
661 assert( p_mgr != NULL && p_ext != NULL );
663 if( p_ext->p_sys->b_activated == false )
664 return VLC_SUCCESS;
666 vlclua_fd_interrupt( &p_ext->p_sys->dtable );
668 // Unset and release input objects
669 if( p_ext->p_sys->p_input )
671 if( p_ext->p_sys->i_capabilities & EXT_META_LISTENER )
673 // Release item
674 input_item_t *p_item = input_GetItem( p_ext->p_sys->p_input );
675 input_item_Release( p_item );
677 vlc_object_release( p_ext->p_sys->p_input );
678 p_ext->p_sys->p_input = NULL;
681 int i_ret = lua_ExecuteFunction( p_mgr, p_ext, "deactivate", LUA_END );
683 if ( p_ext->p_sys->L == NULL )
684 return VLC_EGENERIC;
685 lua_close( p_ext->p_sys->L );
686 p_ext->p_sys->L = NULL;
688 return i_ret;
691 int lua_ExtensionWidgetClick( extensions_manager_t *p_mgr,
692 extension_t *p_ext,
693 extension_widget_t *p_widget )
695 if( !p_ext->p_sys->L )
696 return VLC_SUCCESS;
698 lua_State *L = GetLuaState( p_mgr, p_ext );
699 lua_pushlightuserdata( L, p_widget );
700 lua_gettable( L, LUA_REGISTRYINDEX );
701 return lua_ExecuteFunction( p_mgr, p_ext, NULL, LUA_END );
706 * Get the list of menu entries from an extension script
707 * @param p_mgr
708 * @param p_ext
709 * @param pppsz_titles Pointer to NULL. All strings must be freed by the caller
710 * @param ppi_ids Pointer to NULL. Must be freed by the caller.
711 * @note This function is allowed to run in the UI thread. This means
712 * that it MUST respond very fast.
713 * @todo Remove the menu() hook and provide a new function vlc.set_menu()
715 static int GetMenuEntries( extensions_manager_t *p_mgr, extension_t *p_ext,
716 char ***pppsz_titles, uint16_t **ppi_ids )
718 assert( *pppsz_titles == NULL );
719 assert( *ppi_ids == NULL );
721 vlc_mutex_lock( &p_ext->p_sys->command_lock );
722 if( p_ext->p_sys->b_activated == false || p_ext->p_sys->b_exiting == true )
724 vlc_mutex_unlock( &p_ext->p_sys->command_lock );
725 msg_Dbg( p_mgr, "Can't get menu of an unactivated/dying extension!" );
726 return VLC_EGENERIC;
728 vlc_mutex_unlock( &p_ext->p_sys->command_lock );
730 vlc_mutex_lock( &p_ext->p_sys->running_lock );
732 int i_ret = VLC_EGENERIC;
733 lua_State *L = GetLuaState( p_mgr, p_ext );
735 if( ( p_ext->p_sys->i_capabilities & EXT_HAS_MENU ) == 0 )
737 msg_Dbg( p_mgr, "can't get a menu from an extension without menu!" );
738 goto exit;
741 lua_getglobal( L, "menu" );
743 if( !lua_isfunction( L, -1 ) )
745 msg_Warn( p_mgr, "Error while running script %s, "
746 "function menu() not found", p_ext->psz_name );
747 goto exit;
750 if( lua_pcall( L, 0, 1, 0 ) )
752 msg_Warn( p_mgr, "Error while running script %s, "
753 "function menu(): %s", p_ext->psz_name,
754 lua_tostring( L, lua_gettop( L ) ) );
755 goto exit;
758 if( lua_gettop( L ) )
760 if( lua_istable( L, -1 ) )
762 /* Get table size */
763 size_t i_size = lua_objlen( L, -1 );
764 *pppsz_titles = ( char** ) calloc( i_size+1, sizeof( char* ) );
765 *ppi_ids = ( uint16_t* ) calloc( i_size+1, sizeof( uint16_t ) );
767 /* Walk table */
768 size_t i_idx = 0;
769 lua_pushnil( L );
770 while( lua_next( L, -2 ) != 0 )
772 assert( i_idx < i_size );
773 if( (!lua_isstring( L, -1 )) || (!lua_isnumber( L, -2 )) )
775 msg_Warn( p_mgr, "In script %s, an entry in "
776 "the menu table is invalid!", p_ext->psz_name );
777 goto exit;
779 (*pppsz_titles)[ i_idx ] = strdup( luaL_checkstring( L, -1 ) );
780 (*ppi_ids)[ i_idx ] = luaL_checkinteger( L, -2 ) & 0xFFFF;
781 i_idx++;
782 lua_pop( L, 1 );
785 else
787 msg_Warn( p_mgr, "Function menu() in script %s "
788 "did not return a table", p_ext->psz_name );
789 goto exit;
792 else
794 msg_Warn( p_mgr, "Script %s went completely foobar", p_ext->psz_name );
795 goto exit;
798 i_ret = VLC_SUCCESS;
800 exit:
801 vlc_mutex_unlock( &p_ext->p_sys->running_lock );
802 if( i_ret != VLC_SUCCESS )
804 msg_Dbg( p_mgr, "Something went wrong in %s (%s:%d)",
805 __func__, __FILE__, __LINE__ );
807 return i_ret;
810 /* Must be entered with the Lock on Extension */
811 static lua_State* GetLuaState( extensions_manager_t *p_mgr,
812 extension_t *p_ext )
814 assert( p_ext != NULL );
815 lua_State *L = p_ext->p_sys->L;
817 if( !L )
819 L = luaL_newstate();
820 if( !L )
822 msg_Err( p_mgr, "Could not create new Lua State" );
823 return NULL;
825 vlclua_set_this( L, p_mgr );
826 vlclua_set_playlist_internal( L,
827 pl_Get((intf_thread_t *)(p_mgr->obj.parent)) );
828 vlclua_extension_set( L, p_ext );
830 luaL_openlibs( L );
831 luaL_register_namespace( L, "vlc", p_reg );
832 luaopen_msg( L );
834 /* Load more libraries */
835 luaopen_config( L );
836 luaopen_dialog( L, p_ext );
837 luaopen_input( L );
838 luaopen_msg( L );
839 if( vlclua_fd_init( L, &p_ext->p_sys->dtable ) )
841 lua_close( L );
842 return NULL;
844 luaopen_object( L );
845 luaopen_osd( L );
846 luaopen_playlist( L );
847 luaopen_sd_intf( L );
848 luaopen_stream( L );
849 luaopen_strings( L );
850 luaopen_variables( L );
851 luaopen_video( L );
852 luaopen_vlm( L );
853 luaopen_volume( L );
854 luaopen_xml( L );
855 #if defined(_WIN32) && !VLC_WINSTORE_APP
856 luaopen_win( L );
857 #endif
859 /* Register extension specific functions */
860 lua_getglobal( L, "vlc" );
861 lua_pushcfunction( L, vlclua_extension_deactivate );
862 lua_setfield( L, -2, "deactivate" );
863 lua_pushcfunction( L, vlclua_extension_keep_alive );
864 lua_setfield( L, -2, "keep_alive" );
866 /* Setup the module search path */
867 if( !strncmp( p_ext->psz_name, "zip://", 6 ) )
869 /* Load all required modules manually */
870 lua_register( L, "require", &vlclua_extension_require );
872 else
874 if( vlclua_add_modules_path( L, p_ext->psz_name ) )
876 msg_Warn( p_mgr, "Error while setting the module "
877 "search path for %s", p_ext->psz_name );
878 vlclua_fd_cleanup( &p_ext->p_sys->dtable );
879 lua_close( L );
880 return NULL;
883 /* Load and run the script(s) */
884 if( vlclua_dofile( VLC_OBJECT( p_mgr ), L, p_ext->psz_name ) )
886 msg_Warn( p_mgr, "Error loading script %s: %s", p_ext->psz_name,
887 lua_tostring( L, lua_gettop( L ) ) );
888 vlclua_fd_cleanup( &p_ext->p_sys->dtable );
889 lua_close( L );
890 return NULL;
893 p_ext->p_sys->L = L;
896 return L;
899 int lua_ExecuteFunction( extensions_manager_t *p_mgr, extension_t *p_ext,
900 const char *psz_function, ... )
902 va_list args;
903 va_start( args, psz_function );
904 int i_ret = lua_ExecuteFunctionVa( p_mgr, p_ext, psz_function, args );
905 va_end( args );
906 return i_ret;
910 * Execute a function in a Lua script
911 * @param psz_function Name of global function to execute. If NULL, assume
912 * that the function object is already on top of the
913 * stack.
914 * @return < 0 in case of failure, >= 0 in case of success
915 * @note It's better to call this function from a dedicated thread
916 * (see extension_thread.c)
918 int lua_ExecuteFunctionVa( extensions_manager_t *p_mgr, extension_t *p_ext,
919 const char *psz_function, va_list args )
921 int i_ret = VLC_SUCCESS;
922 int i_args = 0;
923 assert( p_mgr != NULL );
924 assert( p_ext != NULL );
926 lua_State *L = GetLuaState( p_mgr, p_ext );
927 if( !L )
928 return -1;
930 if( psz_function )
931 lua_getglobal( L, psz_function );
933 if( !lua_isfunction( L, -1 ) )
935 msg_Warn( p_mgr, "Error while running script %s, "
936 "function %s() not found", p_ext->psz_name, psz_function );
937 lua_pop( L, 1 );
938 goto exit;
941 lua_datatype_e type = LUA_END;
942 while( ( type = va_arg( args, int ) ) != LUA_END )
944 if( type == LUA_NUM )
946 lua_pushnumber( L , ( int ) va_arg( args, int ) );
948 else if( type == LUA_TEXT )
950 lua_pushstring( L , ( char * ) va_arg( args, char* ) );
952 else
954 msg_Warn( p_mgr, "Undefined argument type %d to lua function %s"
955 "from script %s", type, psz_function, p_ext->psz_name );
956 if( i_args > 0 )
957 lua_pop( L, i_args );
958 goto exit;
960 i_args ++;
963 // Start actual call to Lua
964 if( lua_pcall( L, i_args, 1, 0 ) )
966 msg_Warn( p_mgr, "Error while running script %s, "
967 "function %s(): %s", p_ext->psz_name, psz_function,
968 lua_tostring( L, lua_gettop( L ) ) );
969 i_ret = VLC_EGENERIC;
972 i_ret |= lua_DialogFlush( L );
974 exit:
975 return i_ret;
979 static inline int TriggerMenu( extension_t *p_ext, int i_id )
981 return PushCommand( p_ext, CMD_TRIGGERMENU, i_id );
984 int lua_ExtensionTriggerMenu( extensions_manager_t *p_mgr,
985 extension_t *p_ext, int id )
987 int i_ret = VLC_SUCCESS;
988 lua_State *L = GetLuaState( p_mgr, p_ext );
990 if( !L )
991 return VLC_EGENERIC;
993 luaopen_dialog( L, p_ext );
995 lua_getglobal( L, "trigger_menu" );
996 if( !lua_isfunction( L, -1 ) )
998 msg_Warn( p_mgr, "Error while running script %s, "
999 "function trigger_menu() not found", p_ext->psz_name );
1000 return VLC_EGENERIC;
1003 /* Pass id as unique argument to the function */
1004 lua_pushinteger( L, id );
1006 if( lua_pcall( L, 1, 1, 0 ) != 0 )
1008 msg_Warn( p_mgr, "Error while running script %s, "
1009 "function trigger_menu(): %s", p_ext->psz_name,
1010 lua_tostring( L, lua_gettop( L ) ) );
1011 i_ret = VLC_EGENERIC;
1014 i_ret |= lua_DialogFlush( L );
1015 if( i_ret < VLC_SUCCESS )
1017 msg_Dbg( p_mgr, "Something went wrong in %s (%s:%d)",
1018 __func__, __FILE__, __LINE__ );
1021 return i_ret;
1024 /** Directly trigger an extension, without activating it
1025 * This is NOT multithreaded, and this code runs in the UI thread
1026 * @param p_mgr
1027 * @param p_ext Extension to trigger
1028 * @return Value returned by the lua function "trigger"
1030 static int TriggerExtension( extensions_manager_t *p_mgr,
1031 extension_t *p_ext )
1033 int i_ret = lua_ExecuteFunction( p_mgr, p_ext, "trigger", LUA_END );
1035 /* Close lua state for trigger-only extensions */
1036 if( p_ext->p_sys->L )
1038 vlclua_fd_cleanup( &p_ext->p_sys->dtable );
1039 lua_close( p_ext->p_sys->L );
1041 p_ext->p_sys->L = NULL;
1043 return i_ret;
1046 /** Set extension associated to the current script
1047 * @param L current lua_State
1048 * @param p_ext the extension
1050 void vlclua_extension_set( lua_State *L, extension_t *p_ext )
1052 lua_pushlightuserdata( L, vlclua_extension_set );
1053 lua_pushlightuserdata( L, p_ext );
1054 lua_rawset( L, LUA_REGISTRYINDEX );
1057 /** Retrieve extension associated to the current script
1058 * @param L current lua_State
1059 * @return Extension pointer
1061 extension_t *vlclua_extension_get( lua_State *L )
1063 lua_pushlightuserdata( L, vlclua_extension_set );
1064 lua_rawget( L, LUA_REGISTRYINDEX );
1065 extension_t *p_ext = (extension_t*) lua_topointer( L, -1 );
1066 lua_pop( L, 1 );
1067 return p_ext;
1070 /** Deactivate an extension by order from the extension itself
1071 * @param L lua_State
1072 * @note This is an asynchronous call. A script calling vlc.deactivate() will
1073 * be executed to the end before the last call to deactivate() is done.
1075 int vlclua_extension_deactivate( lua_State *L )
1077 extension_t *p_ext = vlclua_extension_get( L );
1078 vlc_mutex_lock( &p_ext->p_sys->command_lock );
1079 bool b_ret = QueueDeactivateCommand( p_ext );
1080 vlc_mutex_unlock( &p_ext->p_sys->command_lock );
1081 return ( b_ret == true ) ? 1 : 0;
1084 /** Keep an extension alive. This resets the watch timer to 0
1085 * @param L lua_State
1086 * @note This is the "vlc.keep_alive()" function
1088 int vlclua_extension_keep_alive( lua_State *L )
1090 extension_t *p_ext = vlclua_extension_get( L );
1092 vlc_mutex_lock( &p_ext->p_sys->command_lock );
1093 if( p_ext->p_sys->p_progress_id != NULL )
1095 vlc_dialog_release( p_ext->p_sys->p_mgr, p_ext->p_sys->p_progress_id );
1096 p_ext->p_sys->p_progress_id = NULL;
1098 vlc_timer_schedule( p_ext->p_sys->timer, false, WATCH_TIMER_PERIOD, 0 );
1099 vlc_mutex_unlock( &p_ext->p_sys->command_lock );
1101 return 1;
1104 /** Callback for the variable "dialog-event"
1105 * @param p_this Current object owner of the extension and the dialog
1106 * @param psz_var "dialog-event"
1107 * @param oldval Unused
1108 * @param newval Address of the dialog
1109 * @param p_data Unused
1111 static int vlclua_extension_dialog_callback( vlc_object_t *p_this,
1112 char const *psz_var,
1113 vlc_value_t oldval,
1114 vlc_value_t newval,
1115 void *p_data )
1117 /* psz_var == "dialog-event" */
1118 ( void ) psz_var;
1119 ( void ) oldval;
1120 ( void ) p_data;
1122 extension_dialog_command_t *command = newval.p_address;
1123 assert( command != NULL );
1124 assert( command->p_dlg != NULL);
1126 extension_t *p_ext = command->p_dlg->p_sys;
1127 assert( p_ext != NULL );
1129 extension_widget_t *p_widget = command->p_data;
1131 switch( command->event )
1133 case EXTENSION_EVENT_CLICK:
1134 assert( p_widget != NULL );
1135 PushCommandUnique( p_ext, CMD_CLICK, p_widget );
1136 break;
1137 case EXTENSION_EVENT_CLOSE:
1138 PushCommandUnique( p_ext, CMD_CLOSE );
1139 break;
1140 default:
1141 msg_Dbg( p_this, "Received unknown UI event %d, discarded",
1142 command->event );
1143 break;
1146 return VLC_SUCCESS;
1149 /** Callback on vlc_InputItemMetaChanged event
1151 static void inputItemMetaChanged( const vlc_event_t *p_event,
1152 void *data )
1154 assert( p_event && p_event->type == vlc_InputItemMetaChanged );
1156 extension_t *p_ext = ( extension_t* ) data;
1157 assert( p_ext != NULL );
1159 PushCommandUnique( p_ext, CMD_UPDATE_META );
1162 /** Watch timer callback
1163 * The timer expired, Lua may be stuck, ask the user what to do now
1165 static void WatchTimerCallback( void *data )
1167 extension_t *p_ext = data;
1168 extensions_manager_t *p_mgr = p_ext->p_sys->p_mgr;
1170 vlc_mutex_lock( &p_ext->p_sys->command_lock );
1172 for( struct command_t *cmd = p_ext->p_sys->command;
1173 cmd != NULL;
1174 cmd = cmd->next )
1175 if( cmd->i_command == CMD_DEACTIVATE )
1176 { /* We have a pending Deactivate command... */
1177 if( p_ext->p_sys->p_progress_id != NULL )
1179 vlc_dialog_release( p_mgr, p_ext->p_sys->p_progress_id );
1180 p_ext->p_sys->p_progress_id = NULL;
1182 KillExtension( p_mgr, p_ext );
1183 vlc_mutex_unlock( &p_ext->p_sys->command_lock );
1184 return;
1187 if( p_ext->p_sys->p_progress_id == NULL )
1189 p_ext->p_sys->p_progress_id =
1190 vlc_dialog_display_progress( p_mgr, true, 0.0,
1191 _( "Yes" ),
1192 _( "Extension not responding!" ),
1193 _( "Extension '%s' does not respond.\n"
1194 "Do you want to kill it now? " ),
1195 p_ext->psz_title );
1196 if( p_ext->p_sys->p_progress_id == NULL )
1198 KillExtension( p_mgr, p_ext );
1199 vlc_mutex_unlock( &p_ext->p_sys->command_lock );
1200 return;
1202 vlc_timer_schedule( p_ext->p_sys->timer, false, 100000, 0 );
1204 else
1206 if( vlc_dialog_is_cancelled( p_mgr, p_ext->p_sys->p_progress_id ) )
1208 vlc_dialog_release( p_mgr, p_ext->p_sys->p_progress_id );
1209 p_ext->p_sys->p_progress_id = NULL;
1210 KillExtension( p_mgr, p_ext );
1211 vlc_mutex_unlock( &p_ext->p_sys->command_lock );
1212 return;
1214 vlc_timer_schedule( p_ext->p_sys->timer, false, 100000, 0 );
1216 vlc_mutex_unlock( &p_ext->p_sys->command_lock );