vlc_arrays: refactor foreach loop
[vlc.git] / modules / lua / extension.c
blobf0b3f3eae1eb3410baf59396e6c586665b90005d
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 ARRAY_FOREACH( 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 );
190 vlc_mutex_destroy( &p_mgr->lock );
192 ARRAY_RESET( p_mgr->extensions );
196 * Batch scan all Lua files in folder "extensions"
197 * @param p_mgr This extensions_manager_t object
199 static int ScanExtensions( extensions_manager_t *p_mgr )
201 int i_ret =
202 vlclua_scripts_batch_execute( VLC_OBJECT( p_mgr ),
203 "extensions",
204 &ScanLuaCallback,
205 NULL );
207 if( !i_ret )
208 return VLC_EGENERIC;
210 return VLC_SUCCESS;
214 * Dummy Lua function: does nothing
215 * @note This function can be used to replace "require" while scanning for
216 * extensions
217 * Even the built-in libraries are not loaded when calling descriptor()
219 static int vlclua_dummy_require( lua_State *L )
221 (void) L;
222 return 0;
226 * Replacement for "require", adding support for packaged extensions
227 * @note Loads modules in the modules/ folder of a package
228 * @note Try first with .luac and then with .lua
230 static int vlclua_extension_require( lua_State *L )
232 const char *psz_module = luaL_checkstring( L, 1 );
233 vlc_object_t *p_this = vlclua_get_this( L );
234 extension_t *p_ext = vlclua_extension_get( L );
235 msg_Dbg( p_this, "loading module '%s' from extension package",
236 psz_module );
237 char *psz_fullpath, *psz_package, *sep;
238 psz_package = strdup( p_ext->psz_name );
239 sep = strrchr( psz_package, '/' );
240 if( !sep )
242 free( psz_package );
243 return luaL_error( L, "could not find package name" );
245 *sep = '\0';
246 if( -1 == asprintf( &psz_fullpath,
247 "%s/modules/%s.luac", psz_package, psz_module ) )
249 free( psz_package );
250 return 1;
252 int i_ret = vlclua_dofile( p_this, L, psz_fullpath );
253 if( i_ret != 0 )
255 // Remove trailing 'c' --> try with .lua script
256 psz_fullpath[ strlen( psz_fullpath ) - 1 ] = '\0';
257 i_ret = vlclua_dofile( p_this, L, psz_fullpath );
259 free( psz_fullpath );
260 free( psz_package );
261 if( i_ret != 0 )
263 return luaL_error( L, "unable to load module '%s' from package",
264 psz_module );
266 return 0;
270 * Batch scan all Lua files in folder "extensions": callback
271 * @param p_this This extensions_manager_t object
272 * @param psz_filename Name of the script to run
273 * @param L Lua State, common to all scripts here
274 * @param dummy: unused
276 int ScanLuaCallback( vlc_object_t *p_this, const char *psz_filename,
277 const struct luabatch_context_t *dummy )
279 VLC_UNUSED(dummy);
280 extensions_manager_t *p_mgr = ( extensions_manager_t* ) p_this;
281 bool b_ok = false;
283 msg_Dbg( p_mgr, "Scanning Lua script %s", psz_filename );
285 /* Experimental: read .vle packages (Zip archives) */
286 char *psz_script = NULL;
287 int i_flen = strlen( psz_filename );
288 if( !strncasecmp( psz_filename + i_flen - 4, ".vle", 4 ) )
290 msg_Dbg( p_this, "reading Lua script in a zip archive" );
291 psz_script = calloc( 1, i_flen + 6 + 12 + 1 );
292 if( !psz_script )
293 return 0;
294 strcpy( psz_script, "zip://" );
295 strncat( psz_script, psz_filename, i_flen + 19 );
296 strncat( psz_script, "!/script.lua", i_flen + 19 );
298 else
300 psz_script = strdup( psz_filename );
301 if( !psz_script )
302 return 0;
305 /* Create new script descriptor */
306 extension_t *p_ext = ( extension_t* ) calloc( 1, sizeof( extension_t ) );
307 if( !p_ext )
309 free( psz_script );
310 return 0;
313 p_ext->psz_name = psz_script;
314 p_ext->p_sys = (extension_sys_t*) calloc( 1, sizeof( extension_sys_t ) );
315 if( !p_ext->p_sys || !p_ext->psz_name )
317 free( p_ext->psz_name );
318 free( p_ext->p_sys );
319 free( p_ext );
320 return 0;
322 p_ext->p_sys->p_mgr = p_mgr;
324 /* Watch timer */
325 if( vlc_timer_create( &p_ext->p_sys->timer, WatchTimerCallback, p_ext ) )
327 free( p_ext->psz_name );
328 free( p_ext->p_sys );
329 free( p_ext );
330 return 0;
333 /* Mutexes and conditions */
334 vlc_mutex_init( &p_ext->p_sys->command_lock );
335 vlc_mutex_init( &p_ext->p_sys->running_lock );
336 vlc_cond_init( &p_ext->p_sys->wait );
338 /* Prepare Lua state */
339 lua_State *L = luaL_newstate();
340 lua_register( L, "require", &vlclua_dummy_require );
342 /* Let's run it */
343 if( vlclua_dofile( p_this, L, psz_script ) ) // luaL_dofile
345 msg_Warn( p_mgr, "Error loading script %s: %s", psz_script,
346 lua_tostring( L, lua_gettop( L ) ) );
347 lua_pop( L, 1 );
348 goto exit;
351 /* Scan script for capabilities */
352 lua_getglobal( L, "descriptor" );
354 if( !lua_isfunction( L, -1 ) )
356 msg_Warn( p_mgr, "Error while running script %s, "
357 "function descriptor() not found", psz_script );
358 goto exit;
361 if( lua_pcall( L, 0, 1, 0 ) )
363 msg_Warn( p_mgr, "Error while running script %s, "
364 "function descriptor(): %s", psz_script,
365 lua_tostring( L, lua_gettop( L ) ) );
366 goto exit;
369 if( lua_gettop( L ) )
371 if( lua_istable( L, -1 ) )
373 /* Get caps */
374 lua_getfield( L, -1, "capabilities" );
375 if( lua_istable( L, -1 ) )
377 lua_pushnil( L );
378 while( lua_next( L, -2 ) != 0 )
380 /* Key is at index -2 and value at index -1. Discard key */
381 const char *psz_cap = luaL_checkstring( L, -1 );
382 bool b_ok = false;
383 /* Find this capability's flag */
384 for( size_t i = 0; i < sizeof(caps)/sizeof(caps[0]); i++ )
386 if( !strcmp( caps[i], psz_cap ) )
388 /* Flag it! */
389 p_ext->p_sys->i_capabilities |= 1 << i;
390 b_ok = true;
391 break;
394 if( !b_ok )
396 msg_Warn( p_mgr, "Extension capability '%s' unknown in"
397 " script %s", psz_cap, psz_script );
399 /* Removes 'value'; keeps 'key' for next iteration */
400 lua_pop( L, 1 );
403 else
405 msg_Warn( p_mgr, "In script %s, function descriptor() "
406 "did not return a table of capabilities.",
407 psz_script );
409 lua_pop( L, 1 );
411 /* Get title */
412 lua_getfield( L, -1, "title" );
413 if( lua_isstring( L, -1 ) )
415 p_ext->psz_title = strdup( luaL_checkstring( L, -1 ) );
417 else
419 msg_Dbg( p_mgr, "In script %s, function descriptor() "
420 "did not return a string as title.",
421 psz_script );
422 p_ext->psz_title = strdup( psz_script );
424 lua_pop( L, 1 );
426 /* Get author */
427 lua_getfield( L, -1, "author" );
428 p_ext->psz_author = luaL_strdupornull( L, -1 );
429 lua_pop( L, 1 );
431 /* Get description */
432 lua_getfield( L, -1, "description" );
433 p_ext->psz_description = luaL_strdupornull( L, -1 );
434 lua_pop( L, 1 );
436 /* Get short description */
437 lua_getfield( L, -1, "shortdesc" );
438 p_ext->psz_shortdescription = luaL_strdupornull( L, -1 );
439 lua_pop( L, 1 );
441 /* Get URL */
442 lua_getfield( L, -1, "url" );
443 p_ext->psz_url = luaL_strdupornull( L, -1 );
444 lua_pop( L, 1 );
446 /* Get version */
447 lua_getfield( L, -1, "version" );
448 p_ext->psz_version = luaL_strdupornull( L, -1 );
449 lua_pop( L, 1 );
451 /* Get icon data */
452 lua_getfield( L, -1, "icon" );
453 if( !lua_isnil( L, -1 ) && lua_isstring( L, -1 ) )
455 int len = lua_strlen( L, -1 );
456 p_ext->p_icondata = malloc( len );
457 if( p_ext->p_icondata )
459 p_ext->i_icondata_size = len;
460 memcpy( p_ext->p_icondata, lua_tostring( L, -1 ), len );
463 lua_pop( L, 1 );
465 else
467 msg_Warn( p_mgr, "In script %s, function descriptor() "
468 "did not return a table!", psz_script );
469 goto exit;
472 else
474 msg_Err( p_mgr, "Script %s went completely foobar", psz_script );
475 goto exit;
478 msg_Dbg( p_mgr, "Script %s has the following capability flags: 0x%x",
479 psz_script, p_ext->p_sys->i_capabilities );
481 b_ok = true;
482 exit:
483 lua_close( L );
484 if( !b_ok )
486 free( p_ext->psz_name );
487 free( p_ext->psz_title );
488 free( p_ext->psz_url );
489 free( p_ext->psz_author );
490 free( p_ext->psz_description );
491 free( p_ext->psz_shortdescription );
492 free( p_ext->psz_version );
493 vlc_mutex_destroy( &p_ext->p_sys->command_lock );
494 vlc_mutex_destroy( &p_ext->p_sys->running_lock );
495 vlc_cond_destroy( &p_ext->p_sys->wait );
496 free( p_ext->p_sys );
497 free( p_ext );
499 else
501 /* Add the extension to the list of known extensions */
502 ARRAY_APPEND( p_mgr->extensions, p_ext );
505 /* Continue batch execution */
506 return VLC_EGENERIC;
509 static int Control( extensions_manager_t *p_mgr, int i_control, va_list args )
511 extension_t *p_ext = NULL;
512 bool *pb = NULL;
513 uint16_t **ppus = NULL;
514 char ***pppsz = NULL;
515 int i = 0;
517 switch( i_control )
519 case EXTENSION_ACTIVATE:
520 p_ext = ( extension_t* ) va_arg( args, extension_t* );
521 return Activate( p_mgr, p_ext );
523 case EXTENSION_DEACTIVATE:
524 p_ext = ( extension_t* ) va_arg( args, extension_t* );
525 return Deactivate( p_mgr, p_ext );
527 case EXTENSION_IS_ACTIVATED:
528 p_ext = ( extension_t* ) va_arg( args, extension_t* );
529 pb = ( bool* ) va_arg( args, bool* );
530 vlc_mutex_lock( &p_ext->p_sys->command_lock );
531 *pb = p_ext->p_sys->b_activated;
532 vlc_mutex_unlock( &p_ext->p_sys->command_lock );
533 break;
535 case EXTENSION_HAS_MENU:
536 p_ext = ( extension_t* ) va_arg( args, extension_t* );
537 pb = ( bool* ) va_arg( args, bool* );
538 *pb = ( p_ext->p_sys->i_capabilities & EXT_HAS_MENU ) ? 1 : 0;
539 break;
541 case EXTENSION_GET_MENU:
542 p_ext = ( extension_t* ) va_arg( args, extension_t* );
543 pppsz = ( char*** ) va_arg( args, char*** );
544 ppus = ( uint16_t** ) va_arg( args, uint16_t** );
545 if( p_ext == NULL )
546 return VLC_EGENERIC;
547 return GetMenuEntries( p_mgr, p_ext, pppsz, ppus );
549 case EXTENSION_TRIGGER_ONLY:
550 p_ext = ( extension_t* ) va_arg( args, extension_t* );
551 pb = ( bool* ) va_arg( args, bool* );
552 *pb = ( p_ext->p_sys->i_capabilities & EXT_TRIGGER_ONLY ) ? 1 : 0;
553 break;
555 case EXTENSION_TRIGGER:
556 p_ext = ( extension_t* ) va_arg( args, extension_t* );
557 return TriggerExtension( p_mgr, p_ext );
559 case EXTENSION_TRIGGER_MENU:
560 p_ext = ( extension_t* ) va_arg( args, extension_t* );
561 // GCC: 'uint16_t' is promoted to 'int' when passed through '...'
562 i = ( int ) va_arg( args, int );
563 return TriggerMenu( p_ext, i );
565 case EXTENSION_SET_INPUT:
567 p_ext = ( extension_t* ) va_arg( args, extension_t* );
568 input_thread_t *p_input = va_arg( args, struct input_thread_t * );
570 if( p_ext == NULL )
571 return VLC_EGENERIC;
572 vlc_mutex_lock( &p_ext->p_sys->command_lock );
573 if ( p_ext->p_sys->b_exiting == true )
575 vlc_mutex_unlock( &p_ext->p_sys->command_lock );
576 return VLC_EGENERIC;
578 vlc_mutex_unlock( &p_ext->p_sys->command_lock );
580 vlc_mutex_lock( &p_ext->p_sys->running_lock );
582 // Change input
583 input_thread_t *old = p_ext->p_sys->p_input;
584 input_item_t *p_item;
585 if( old )
587 // Untrack meta fetched events
588 if( p_ext->p_sys->i_capabilities & EXT_META_LISTENER )
590 p_item = input_GetItem( old );
591 vlc_event_detach( &p_item->event_manager,
592 vlc_InputItemMetaChanged,
593 inputItemMetaChanged,
594 p_ext );
595 input_item_Release( p_item );
597 vlc_object_release( old );
600 p_ext->p_sys->p_input = p_input ? vlc_object_hold( p_input )
601 : p_input;
603 // Tell the script the input changed
604 if( p_ext->p_sys->i_capabilities & EXT_INPUT_LISTENER )
606 PushCommandUnique( p_ext, CMD_SET_INPUT );
609 // Track meta fetched events
610 if( p_ext->p_sys->p_input &&
611 p_ext->p_sys->i_capabilities & EXT_META_LISTENER )
613 p_item = input_GetItem( p_ext->p_sys->p_input );
614 input_item_Hold( p_item );
615 vlc_event_attach( &p_item->event_manager,
616 vlc_InputItemMetaChanged,
617 inputItemMetaChanged,
618 p_ext );
621 vlc_mutex_unlock( &p_ext->p_sys->running_lock );
622 break;
624 case EXTENSION_PLAYING_CHANGED:
626 extension_t *p_ext;
627 p_ext = ( extension_t* ) va_arg( args, extension_t* );
628 assert( p_ext->psz_name != NULL );
629 i = ( int ) va_arg( args, int );
630 if( p_ext->p_sys->i_capabilities & EXT_PLAYING_LISTENER )
632 PushCommand( p_ext, CMD_PLAYING_CHANGED, i );
634 break;
636 case EXTENSION_META_CHANGED:
638 extension_t *p_ext;
639 p_ext = ( extension_t* ) va_arg( args, extension_t* );
640 PushCommand( p_ext, CMD_UPDATE_META );
641 break;
643 default:
644 msg_Warn( p_mgr, "Control '%d' not yet implemented in Extension",
645 i_control );
646 return VLC_EGENERIC;
649 return VLC_SUCCESS;
652 int lua_ExtensionActivate( extensions_manager_t *p_mgr, extension_t *p_ext )
654 assert( p_mgr != NULL && p_ext != NULL );
655 return lua_ExecuteFunction( p_mgr, p_ext, "activate", LUA_END );
658 int lua_ExtensionDeactivate( extensions_manager_t *p_mgr, extension_t *p_ext )
660 assert( p_mgr != NULL && p_ext != NULL );
662 if( p_ext->p_sys->b_activated == false )
663 return VLC_SUCCESS;
665 vlclua_fd_interrupt( &p_ext->p_sys->dtable );
667 // Unset and release input objects
668 if( p_ext->p_sys->p_input )
670 if( p_ext->p_sys->i_capabilities & EXT_META_LISTENER )
672 // Release item
673 input_item_t *p_item = input_GetItem( p_ext->p_sys->p_input );
674 input_item_Release( p_item );
676 vlc_object_release( p_ext->p_sys->p_input );
677 p_ext->p_sys->p_input = NULL;
680 int i_ret = lua_ExecuteFunction( p_mgr, p_ext, "deactivate", LUA_END );
682 if ( p_ext->p_sys->L == NULL )
683 return VLC_EGENERIC;
684 lua_close( p_ext->p_sys->L );
685 p_ext->p_sys->L = NULL;
687 return i_ret;
690 int lua_ExtensionWidgetClick( extensions_manager_t *p_mgr,
691 extension_t *p_ext,
692 extension_widget_t *p_widget )
694 if( !p_ext->p_sys->L )
695 return VLC_SUCCESS;
697 lua_State *L = GetLuaState( p_mgr, p_ext );
698 lua_pushlightuserdata( L, p_widget );
699 lua_gettable( L, LUA_REGISTRYINDEX );
700 return lua_ExecuteFunction( p_mgr, p_ext, NULL, LUA_END );
705 * Get the list of menu entries from an extension script
706 * @param p_mgr
707 * @param p_ext
708 * @param pppsz_titles Pointer to NULL. All strings must be freed by the caller
709 * @param ppi_ids Pointer to NULL. Must be freed by the caller.
710 * @note This function is allowed to run in the UI thread. This means
711 * that it MUST respond very fast.
712 * @todo Remove the menu() hook and provide a new function vlc.set_menu()
714 static int GetMenuEntries( extensions_manager_t *p_mgr, extension_t *p_ext,
715 char ***pppsz_titles, uint16_t **ppi_ids )
717 assert( *pppsz_titles == NULL );
718 assert( *ppi_ids == NULL );
720 vlc_mutex_lock( &p_ext->p_sys->command_lock );
721 if( p_ext->p_sys->b_activated == false || p_ext->p_sys->b_exiting == true )
723 vlc_mutex_unlock( &p_ext->p_sys->command_lock );
724 msg_Dbg( p_mgr, "Can't get menu of an unactivated/dying extension!" );
725 return VLC_EGENERIC;
727 vlc_mutex_unlock( &p_ext->p_sys->command_lock );
729 vlc_mutex_lock( &p_ext->p_sys->running_lock );
731 int i_ret = VLC_EGENERIC;
732 lua_State *L = GetLuaState( p_mgr, p_ext );
734 if( ( p_ext->p_sys->i_capabilities & EXT_HAS_MENU ) == 0 )
736 msg_Dbg( p_mgr, "can't get a menu from an extension without menu!" );
737 goto exit;
740 lua_getglobal( L, "menu" );
742 if( !lua_isfunction( L, -1 ) )
744 msg_Warn( p_mgr, "Error while running script %s, "
745 "function menu() not found", p_ext->psz_name );
746 goto exit;
749 if( lua_pcall( L, 0, 1, 0 ) )
751 msg_Warn( p_mgr, "Error while running script %s, "
752 "function menu(): %s", p_ext->psz_name,
753 lua_tostring( L, lua_gettop( L ) ) );
754 goto exit;
757 if( lua_gettop( L ) )
759 if( lua_istable( L, -1 ) )
761 /* Get table size */
762 size_t i_size = lua_objlen( L, -1 );
763 *pppsz_titles = ( char** ) calloc( i_size+1, sizeof( char* ) );
764 *ppi_ids = ( uint16_t* ) calloc( i_size+1, sizeof( uint16_t ) );
766 /* Walk table */
767 size_t i_idx = 0;
768 lua_pushnil( L );
769 while( lua_next( L, -2 ) != 0 )
771 assert( i_idx < i_size );
772 if( (!lua_isstring( L, -1 )) || (!lua_isnumber( L, -2 )) )
774 msg_Warn( p_mgr, "In script %s, an entry in "
775 "the menu table is invalid!", p_ext->psz_name );
776 goto exit;
778 (*pppsz_titles)[ i_idx ] = strdup( luaL_checkstring( L, -1 ) );
779 (*ppi_ids)[ i_idx ] = luaL_checkinteger( L, -2 ) & 0xFFFF;
780 i_idx++;
781 lua_pop( L, 1 );
784 else
786 msg_Warn( p_mgr, "Function menu() in script %s "
787 "did not return a table", p_ext->psz_name );
788 goto exit;
791 else
793 msg_Warn( p_mgr, "Script %s went completely foobar", p_ext->psz_name );
794 goto exit;
797 i_ret = VLC_SUCCESS;
799 exit:
800 vlc_mutex_unlock( &p_ext->p_sys->running_lock );
801 if( i_ret != VLC_SUCCESS )
803 msg_Dbg( p_mgr, "Something went wrong in %s (%s:%d)",
804 __func__, __FILE__, __LINE__ );
806 return i_ret;
809 /* Must be entered with the Lock on Extension */
810 static lua_State* GetLuaState( extensions_manager_t *p_mgr,
811 extension_t *p_ext )
813 assert( p_ext != NULL );
814 lua_State *L = p_ext->p_sys->L;
816 if( !L )
818 L = luaL_newstate();
819 if( !L )
821 msg_Err( p_mgr, "Could not create new Lua State" );
822 return NULL;
824 vlclua_set_this( L, p_mgr );
825 vlclua_set_playlist_internal( L,
826 pl_Get((intf_thread_t *)(p_mgr->obj.parent)) );
827 vlclua_extension_set( L, p_ext );
829 luaL_openlibs( L );
830 luaL_register_namespace( L, "vlc", p_reg );
831 luaopen_msg( L );
833 /* Load more libraries */
834 luaopen_config( L );
835 luaopen_dialog( L, p_ext );
836 luaopen_input( L );
837 luaopen_msg( L );
838 if( vlclua_fd_init( L, &p_ext->p_sys->dtable ) )
840 lua_close( L );
841 return NULL;
843 luaopen_object( L );
844 luaopen_osd( L );
845 luaopen_playlist( L );
846 luaopen_sd_intf( L );
847 luaopen_stream( L );
848 luaopen_strings( L );
849 luaopen_variables( L );
850 luaopen_video( L );
851 luaopen_vlm( L );
852 luaopen_volume( L );
853 luaopen_xml( L );
854 luaopen_vlcio( L );
855 luaopen_errno( L );
856 luaopen_rand( L );
857 #if defined(_WIN32) && !VLC_WINSTORE_APP
858 luaopen_win( L );
859 #endif
861 /* Register extension specific functions */
862 lua_getglobal( L, "vlc" );
863 lua_pushcfunction( L, vlclua_extension_deactivate );
864 lua_setfield( L, -2, "deactivate" );
865 lua_pushcfunction( L, vlclua_extension_keep_alive );
866 lua_setfield( L, -2, "keep_alive" );
868 /* Setup the module search path */
869 if( !strncmp( p_ext->psz_name, "zip://", 6 ) )
871 /* Load all required modules manually */
872 lua_register( L, "require", &vlclua_extension_require );
874 else
876 if( vlclua_add_modules_path( L, p_ext->psz_name ) )
878 msg_Warn( p_mgr, "Error while setting the module "
879 "search path for %s", p_ext->psz_name );
880 vlclua_fd_cleanup( &p_ext->p_sys->dtable );
881 lua_close( L );
882 return NULL;
885 /* Load and run the script(s) */
886 if( vlclua_dofile( VLC_OBJECT( p_mgr ), L, p_ext->psz_name ) )
888 msg_Warn( p_mgr, "Error loading script %s: %s", p_ext->psz_name,
889 lua_tostring( L, lua_gettop( L ) ) );
890 vlclua_fd_cleanup( &p_ext->p_sys->dtable );
891 lua_close( L );
892 return NULL;
895 p_ext->p_sys->L = L;
898 return L;
901 int lua_ExecuteFunction( extensions_manager_t *p_mgr, extension_t *p_ext,
902 const char *psz_function, ... )
904 va_list args;
905 va_start( args, psz_function );
906 int i_ret = lua_ExecuteFunctionVa( p_mgr, p_ext, psz_function, args );
907 va_end( args );
908 return i_ret;
912 * Execute a function in a Lua script
913 * @param psz_function Name of global function to execute. If NULL, assume
914 * that the function object is already on top of the
915 * stack.
916 * @return < 0 in case of failure, >= 0 in case of success
917 * @note It's better to call this function from a dedicated thread
918 * (see extension_thread.c)
920 int lua_ExecuteFunctionVa( extensions_manager_t *p_mgr, extension_t *p_ext,
921 const char *psz_function, va_list args )
923 int i_ret = VLC_SUCCESS;
924 int i_args = 0;
925 assert( p_mgr != NULL );
926 assert( p_ext != NULL );
928 lua_State *L = GetLuaState( p_mgr, p_ext );
929 if( !L )
930 return -1;
932 if( psz_function )
933 lua_getglobal( L, psz_function );
935 if( !lua_isfunction( L, -1 ) )
937 msg_Warn( p_mgr, "Error while running script %s, "
938 "function %s() not found", p_ext->psz_name, psz_function );
939 lua_pop( L, 1 );
940 goto exit;
943 lua_datatype_e type = LUA_END;
944 while( ( type = va_arg( args, int ) ) != LUA_END )
946 if( type == LUA_NUM )
948 lua_pushnumber( L , ( int ) va_arg( args, int ) );
950 else if( type == LUA_TEXT )
952 lua_pushstring( L , ( char * ) va_arg( args, char* ) );
954 else
956 msg_Warn( p_mgr, "Undefined argument type %d to lua function %s"
957 "from script %s", type, psz_function, p_ext->psz_name );
958 if( i_args > 0 )
959 lua_pop( L, i_args );
960 goto exit;
962 i_args ++;
965 // Start actual call to Lua
966 if( lua_pcall( L, i_args, 1, 0 ) )
968 msg_Warn( p_mgr, "Error while running script %s, "
969 "function %s(): %s", p_ext->psz_name, psz_function,
970 lua_tostring( L, lua_gettop( L ) ) );
971 i_ret = VLC_EGENERIC;
974 i_ret |= lua_DialogFlush( L );
976 exit:
977 return i_ret;
981 static inline int TriggerMenu( extension_t *p_ext, int i_id )
983 return PushCommand( p_ext, CMD_TRIGGERMENU, i_id );
986 int lua_ExtensionTriggerMenu( extensions_manager_t *p_mgr,
987 extension_t *p_ext, int id )
989 int i_ret = VLC_SUCCESS;
990 lua_State *L = GetLuaState( p_mgr, p_ext );
992 if( !L )
993 return VLC_EGENERIC;
995 luaopen_dialog( L, p_ext );
997 lua_getglobal( L, "trigger_menu" );
998 if( !lua_isfunction( L, -1 ) )
1000 msg_Warn( p_mgr, "Error while running script %s, "
1001 "function trigger_menu() not found", p_ext->psz_name );
1002 return VLC_EGENERIC;
1005 /* Pass id as unique argument to the function */
1006 lua_pushinteger( L, id );
1008 if( lua_pcall( L, 1, 1, 0 ) != 0 )
1010 msg_Warn( p_mgr, "Error while running script %s, "
1011 "function trigger_menu(): %s", p_ext->psz_name,
1012 lua_tostring( L, lua_gettop( L ) ) );
1013 i_ret = VLC_EGENERIC;
1016 i_ret |= lua_DialogFlush( L );
1017 if( i_ret < VLC_SUCCESS )
1019 msg_Dbg( p_mgr, "Something went wrong in %s (%s:%d)",
1020 __func__, __FILE__, __LINE__ );
1023 return i_ret;
1026 /** Directly trigger an extension, without activating it
1027 * This is NOT multithreaded, and this code runs in the UI thread
1028 * @param p_mgr
1029 * @param p_ext Extension to trigger
1030 * @return Value returned by the lua function "trigger"
1032 static int TriggerExtension( extensions_manager_t *p_mgr,
1033 extension_t *p_ext )
1035 int i_ret = lua_ExecuteFunction( p_mgr, p_ext, "trigger", LUA_END );
1037 /* Close lua state for trigger-only extensions */
1038 if( p_ext->p_sys->L )
1040 vlclua_fd_cleanup( &p_ext->p_sys->dtable );
1041 lua_close( p_ext->p_sys->L );
1043 p_ext->p_sys->L = NULL;
1045 return i_ret;
1048 /** Set extension associated to the current script
1049 * @param L current lua_State
1050 * @param p_ext the extension
1052 void vlclua_extension_set( lua_State *L, extension_t *p_ext )
1054 lua_pushlightuserdata( L, vlclua_extension_set );
1055 lua_pushlightuserdata( L, p_ext );
1056 lua_rawset( L, LUA_REGISTRYINDEX );
1059 /** Retrieve extension associated to the current script
1060 * @param L current lua_State
1061 * @return Extension pointer
1063 extension_t *vlclua_extension_get( lua_State *L )
1065 lua_pushlightuserdata( L, vlclua_extension_set );
1066 lua_rawget( L, LUA_REGISTRYINDEX );
1067 extension_t *p_ext = (extension_t*) lua_topointer( L, -1 );
1068 lua_pop( L, 1 );
1069 return p_ext;
1072 /** Deactivate an extension by order from the extension itself
1073 * @param L lua_State
1074 * @note This is an asynchronous call. A script calling vlc.deactivate() will
1075 * be executed to the end before the last call to deactivate() is done.
1077 int vlclua_extension_deactivate( lua_State *L )
1079 extension_t *p_ext = vlclua_extension_get( L );
1080 vlc_mutex_lock( &p_ext->p_sys->command_lock );
1081 bool b_ret = QueueDeactivateCommand( p_ext );
1082 vlc_mutex_unlock( &p_ext->p_sys->command_lock );
1083 return ( b_ret == true ) ? 1 : 0;
1086 /** Keep an extension alive. This resets the watch timer to 0
1087 * @param L lua_State
1088 * @note This is the "vlc.keep_alive()" function
1090 int vlclua_extension_keep_alive( lua_State *L )
1092 extension_t *p_ext = vlclua_extension_get( L );
1094 vlc_mutex_lock( &p_ext->p_sys->command_lock );
1095 if( p_ext->p_sys->p_progress_id != NULL )
1097 vlc_dialog_release( p_ext->p_sys->p_mgr, p_ext->p_sys->p_progress_id );
1098 p_ext->p_sys->p_progress_id = NULL;
1100 vlc_timer_schedule( p_ext->p_sys->timer, false, WATCH_TIMER_PERIOD,
1101 VLC_TIMER_FIRE_ONCE );
1102 vlc_mutex_unlock( &p_ext->p_sys->command_lock );
1104 return 1;
1107 /** Callback for the variable "dialog-event"
1108 * @param p_this Current object owner of the extension and the dialog
1109 * @param psz_var "dialog-event"
1110 * @param oldval Unused
1111 * @param newval Address of the dialog
1112 * @param p_data Unused
1114 static int vlclua_extension_dialog_callback( vlc_object_t *p_this,
1115 char const *psz_var,
1116 vlc_value_t oldval,
1117 vlc_value_t newval,
1118 void *p_data )
1120 /* psz_var == "dialog-event" */
1121 ( void ) psz_var;
1122 ( void ) oldval;
1123 ( void ) p_data;
1125 extension_dialog_command_t *command = newval.p_address;
1126 assert( command != NULL );
1127 assert( command->p_dlg != NULL);
1129 extension_t *p_ext = command->p_dlg->p_sys;
1130 assert( p_ext != NULL );
1132 extension_widget_t *p_widget = command->p_data;
1134 switch( command->event )
1136 case EXTENSION_EVENT_CLICK:
1137 assert( p_widget != NULL );
1138 PushCommandUnique( p_ext, CMD_CLICK, p_widget );
1139 break;
1140 case EXTENSION_EVENT_CLOSE:
1141 PushCommandUnique( p_ext, CMD_CLOSE );
1142 break;
1143 default:
1144 msg_Dbg( p_this, "Received unknown UI event %d, discarded",
1145 command->event );
1146 break;
1149 return VLC_SUCCESS;
1152 /** Callback on vlc_InputItemMetaChanged event
1154 static void inputItemMetaChanged( const vlc_event_t *p_event,
1155 void *data )
1157 assert( p_event && p_event->type == vlc_InputItemMetaChanged );
1159 extension_t *p_ext = ( extension_t* ) data;
1160 assert( p_ext != NULL );
1162 PushCommandUnique( p_ext, CMD_UPDATE_META );
1165 /** Watch timer callback
1166 * The timer expired, Lua may be stuck, ask the user what to do now
1168 static void WatchTimerCallback( void *data )
1170 extension_t *p_ext = data;
1171 extensions_manager_t *p_mgr = p_ext->p_sys->p_mgr;
1173 vlc_mutex_lock( &p_ext->p_sys->command_lock );
1175 for( struct command_t *cmd = p_ext->p_sys->command;
1176 cmd != NULL;
1177 cmd = cmd->next )
1178 if( cmd->i_command == CMD_DEACTIVATE )
1179 { /* We have a pending Deactivate command... */
1180 if( p_ext->p_sys->p_progress_id != NULL )
1182 vlc_dialog_release( p_mgr, p_ext->p_sys->p_progress_id );
1183 p_ext->p_sys->p_progress_id = NULL;
1185 KillExtension( p_mgr, p_ext );
1186 vlc_mutex_unlock( &p_ext->p_sys->command_lock );
1187 return;
1190 if( p_ext->p_sys->p_progress_id == NULL )
1192 p_ext->p_sys->p_progress_id =
1193 vlc_dialog_display_progress( p_mgr, true, 0.0,
1194 _( "Yes" ),
1195 _( "Extension not responding!" ),
1196 _( "Extension '%s' does not respond.\n"
1197 "Do you want to kill it now? " ),
1198 p_ext->psz_title );
1199 if( p_ext->p_sys->p_progress_id == NULL )
1201 KillExtension( p_mgr, p_ext );
1202 vlc_mutex_unlock( &p_ext->p_sys->command_lock );
1203 return;
1205 vlc_timer_schedule( p_ext->p_sys->timer, false, VLC_TICK_FROM_MS(100),
1206 VLC_TIMER_FIRE_ONCE );
1208 else
1210 if( vlc_dialog_is_cancelled( p_mgr, p_ext->p_sys->p_progress_id ) )
1212 vlc_dialog_release( p_mgr, p_ext->p_sys->p_progress_id );
1213 p_ext->p_sys->p_progress_id = NULL;
1214 KillExtension( p_mgr, p_ext );
1215 vlc_mutex_unlock( &p_ext->p_sys->command_lock );
1216 return;
1218 vlc_timer_schedule( p_ext->p_sys->timer, false, VLC_TICK_FROM_MS(100),
1219 VLC_TIMER_FIRE_ONCE );
1221 vlc_mutex_unlock( &p_ext->p_sys->command_lock );