qt: playlist: use item title if available
[vlc.git] / modules / lua / extension.c
blob998ba5a0cd8d5c43e558a8f46c865a52f78012e7
1 /*****************************************************************************
2 * extension.c: Lua Extensions (meta data, web information, ...)
3 *****************************************************************************
4 * Copyright (C) 2009-2010 VideoLAN and authors
6 * Authors: Jean-Philippe André < jpeg # videolan.org >
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
23 #ifndef _GNU_SOURCE
24 # define _GNU_SOURCE
25 #endif
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
31 #include "vlc.h"
32 #include "libs.h"
33 #include "extension.h"
34 #include "assert.h"
36 #include <vlc_common.h>
37 #include <vlc_interface.h>
38 #include <vlc_events.h>
39 #include <vlc_dialog.h>
41 /* Functions to register */
42 static const luaL_Reg p_reg[] =
44 { NULL, NULL }
48 * Extensions capabilities
49 * Note: #define and ppsz_capabilities must be in sync
51 static const char caps[][20] = {
52 #define EXT_HAS_MENU (1 << 0) ///< Hook: menu
53 "menu",
54 #define EXT_TRIGGER_ONLY (1 << 1) ///< Hook: trigger. Not activable
55 "trigger",
56 #define EXT_INPUT_LISTENER (1 << 2) ///< Hook: input_changed
57 "input-listener",
58 #define EXT_META_LISTENER (1 << 3) ///< Hook: meta_changed
59 "meta-listener",
60 #define EXT_PLAYING_LISTENER (1 << 4) ///< Hook: status_changed
61 "playing-listener",
64 static int ScanExtensions( extensions_manager_t *p_this );
65 static int ScanLuaCallback( vlc_object_t *p_this, const char *psz_script,
66 const struct luabatch_context_t * );
67 static int Control( extensions_manager_t *, int, va_list );
68 static int GetMenuEntries( extensions_manager_t *p_mgr, extension_t *p_ext,
69 char ***pppsz_titles, uint16_t **ppi_ids );
70 static lua_State* GetLuaState( extensions_manager_t *p_mgr,
71 extension_t *p_ext );
72 static int TriggerMenu( extension_t *p_ext, int id );
73 static int TriggerExtension( extensions_manager_t *p_mgr,
74 extension_t *p_ext );
75 static void WatchTimerCallback( void* );
77 static int vlclua_extension_deactivate( lua_State *L );
78 static int vlclua_extension_keep_alive( lua_State *L );
80 /* Interactions */
81 static int vlclua_extension_dialog_callback( vlc_object_t *p_this,
82 char const *psz_var,
83 vlc_value_t oldval,
84 vlc_value_t newval,
85 void *p_data );
87 /* Input item callback: vlc_InputItemMetaChanged */
88 static void inputItemMetaChanged( const vlc_event_t *p_event,
89 void *data );
92 /**
93 * Module entry-point
94 **/
95 int Open_Extension( vlc_object_t *p_this )
97 if( lua_Disabled( p_this ) )
98 return VLC_EGENERIC;
100 msg_Dbg( p_this, "Opening Lua Extension module" );
102 extensions_manager_t *p_mgr = ( extensions_manager_t* ) p_this;
104 p_mgr->pf_control = Control;
106 p_mgr->p_sys = NULL;
107 vlc_mutex_init( &p_mgr->lock );
109 /* Scan available Lua Extensions */
110 if( ScanExtensions( p_mgr ) != VLC_SUCCESS )
112 msg_Err( p_mgr, "Can't load extensions modules" );
113 return VLC_EGENERIC;
116 // Create the dialog-event variable
117 var_Create( p_this, "dialog-event", VLC_VAR_ADDRESS );
118 var_AddCallback( p_this, "dialog-event",
119 vlclua_extension_dialog_callback, NULL );
121 return VLC_SUCCESS;
125 * Module unload function
127 void Close_Extension( vlc_object_t *p_this )
129 extensions_manager_t *p_mgr = ( extensions_manager_t* ) p_this;
131 var_DelCallback( p_this, "dialog-event",
132 vlclua_extension_dialog_callback, NULL );
133 var_Destroy( p_mgr, "dialog-event" );
135 extension_t *p_ext = NULL;
137 /* Free extensions' memory */
138 ARRAY_FOREACH( p_ext, p_mgr->extensions )
140 if( !p_ext )
141 break;
143 vlc_mutex_lock( &p_ext->p_sys->command_lock );
144 if( p_ext->p_sys->b_activated == true && p_ext->p_sys->p_progress_id == NULL )
146 p_ext->p_sys->b_exiting = true;
147 // QueueDeactivateCommand will signal the wait condition.
148 QueueDeactivateCommand( p_ext );
150 else
152 if ( p_ext->p_sys->L != NULL )
153 vlclua_fd_interrupt( &p_ext->p_sys->dtable );
154 // however here we need to manually signal the wait cond, since no command is queued.
155 p_ext->p_sys->b_exiting = true;
156 vlc_cond_signal( &p_ext->p_sys->wait );
158 vlc_mutex_unlock( &p_ext->p_sys->command_lock );
160 if( p_ext->p_sys->b_thread_running == true )
161 vlc_join( p_ext->p_sys->thread, NULL );
163 /* Clear Lua State */
164 if( p_ext->p_sys->L )
166 lua_close( p_ext->p_sys->L );
167 vlclua_fd_cleanup( &p_ext->p_sys->dtable );
170 free( p_ext->psz_name );
171 free( p_ext->psz_title );
172 free( p_ext->psz_author );
173 free( p_ext->psz_description );
174 free( p_ext->psz_shortdescription );
175 free( p_ext->psz_url );
176 free( p_ext->psz_version );
177 free( p_ext->p_icondata );
179 vlc_timer_destroy( p_ext->p_sys->timer );
181 free( p_ext->p_sys );
182 free( p_ext );
185 ARRAY_RESET( p_mgr->extensions );
189 * Batch scan all Lua files in folder "extensions"
190 * @param p_mgr This extensions_manager_t object
192 static int ScanExtensions( extensions_manager_t *p_mgr )
194 int i_ret =
195 vlclua_scripts_batch_execute( VLC_OBJECT( p_mgr ),
196 "extensions",
197 &ScanLuaCallback,
198 NULL );
200 if( !i_ret )
201 return VLC_EGENERIC;
203 return VLC_SUCCESS;
207 * Dummy Lua function: does nothing
208 * @note This function can be used to replace "require" while scanning for
209 * extensions
210 * Even the built-in libraries are not loaded when calling descriptor()
212 static int vlclua_dummy_require( lua_State *L )
214 (void) L;
215 return 0;
219 * Replacement for "require", adding support for packaged extensions
220 * @note Loads modules in the modules/ folder of a package
221 * @note Try first with .luac and then with .lua
223 static int vlclua_extension_require( lua_State *L )
225 const char *psz_module = luaL_checkstring( L, 1 );
226 vlc_object_t *p_this = vlclua_get_this( L );
227 extension_t *p_ext = vlclua_extension_get( L );
228 msg_Dbg( p_this, "loading module '%s' from extension package",
229 psz_module );
230 char *psz_fullpath, *psz_package, *sep;
231 psz_package = strdup( p_ext->psz_name );
232 sep = strrchr( psz_package, '/' );
233 if( !sep )
235 free( psz_package );
236 return luaL_error( L, "could not find package name" );
238 *sep = '\0';
239 if( -1 == asprintf( &psz_fullpath,
240 "%s/modules/%s.luac", psz_package, psz_module ) )
242 free( psz_package );
243 return 1;
245 int i_ret = vlclua_dofile( p_this, L, psz_fullpath );
246 if( i_ret != 0 )
248 // Remove trailing 'c' --> try with .lua script
249 psz_fullpath[ strlen( psz_fullpath ) - 1 ] = '\0';
250 i_ret = vlclua_dofile( p_this, L, psz_fullpath );
252 free( psz_fullpath );
253 free( psz_package );
254 if( i_ret != 0 )
256 return luaL_error( L, "unable to load module '%s' from package",
257 psz_module );
259 return 0;
263 * Batch scan all Lua files in folder "extensions": callback
264 * @param p_this This extensions_manager_t object
265 * @param psz_filename Name of the script to run
266 * @param L Lua State, common to all scripts here
267 * @param dummy: unused
269 int ScanLuaCallback( vlc_object_t *p_this, const char *psz_filename,
270 const struct luabatch_context_t *dummy )
272 VLC_UNUSED(dummy);
273 extensions_manager_t *p_mgr = ( extensions_manager_t* ) p_this;
274 bool b_ok = false;
276 msg_Dbg( p_mgr, "Scanning Lua script %s", psz_filename );
278 /* Experimental: read .vle packages (Zip archives) */
279 char *psz_script = NULL;
280 int i_flen = strlen( psz_filename );
281 if( !strncasecmp( psz_filename + i_flen - 4, ".vle", 4 ) )
283 msg_Dbg( p_this, "reading Lua script in a zip archive" );
284 psz_script = calloc( 1, i_flen + 6 + 12 + 1 );
285 if( !psz_script )
286 return 0;
287 strcpy( psz_script, "zip://" );
288 strncat( psz_script, psz_filename, i_flen + 19 );
289 strncat( psz_script, "!/script.lua", i_flen + 19 );
291 else
293 psz_script = strdup( psz_filename );
294 if( !psz_script )
295 return 0;
298 /* Create new script descriptor */
299 extension_t *p_ext = ( extension_t* ) calloc( 1, sizeof( extension_t ) );
300 if( !p_ext )
302 free( psz_script );
303 return 0;
306 p_ext->psz_name = psz_script;
307 p_ext->p_sys = (extension_sys_t*) calloc( 1, sizeof( extension_sys_t ) );
308 if( !p_ext->p_sys || !p_ext->psz_name )
310 free( p_ext->psz_name );
311 free( p_ext->p_sys );
312 free( p_ext );
313 return 0;
315 p_ext->p_sys->p_mgr = p_mgr;
317 /* Watch timer */
318 if( vlc_timer_create( &p_ext->p_sys->timer, WatchTimerCallback, p_ext ) )
320 free( p_ext->psz_name );
321 free( p_ext->p_sys );
322 free( p_ext );
323 return 0;
326 /* Mutexes and conditions */
327 vlc_mutex_init( &p_ext->p_sys->command_lock );
328 vlc_mutex_init( &p_ext->p_sys->running_lock );
329 vlc_cond_init( &p_ext->p_sys->wait );
331 /* Prepare Lua state */
332 lua_State *L = luaL_newstate();
333 lua_register( L, "require", &vlclua_dummy_require );
335 /* Let's run it */
336 if( vlclua_dofile( p_this, L, psz_script ) ) // luaL_dofile
338 msg_Warn( p_mgr, "Error loading script %s: %s", psz_script,
339 lua_tostring( L, lua_gettop( L ) ) );
340 lua_pop( L, 1 );
341 goto exit;
344 /* Scan script for capabilities */
345 lua_getglobal( L, "descriptor" );
347 if( !lua_isfunction( L, -1 ) )
349 msg_Warn( p_mgr, "Error while running script %s, "
350 "function descriptor() not found", psz_script );
351 goto exit;
354 if( lua_pcall( L, 0, 1, 0 ) )
356 msg_Warn( p_mgr, "Error while running script %s, "
357 "function descriptor(): %s", psz_script,
358 lua_tostring( L, lua_gettop( L ) ) );
359 goto exit;
362 if( lua_gettop( L ) )
364 if( lua_istable( L, -1 ) )
366 /* Get caps */
367 lua_getfield( L, -1, "capabilities" );
368 if( lua_istable( L, -1 ) )
370 lua_pushnil( L );
371 while( lua_next( L, -2 ) != 0 )
373 /* Key is at index -2 and value at index -1. Discard key */
374 const char *psz_cap = luaL_checkstring( L, -1 );
375 bool found = false;
376 /* Find this capability's flag */
377 for( size_t i = 0; i < sizeof(caps)/sizeof(caps[0]); i++ )
379 if( !strcmp( caps[i], psz_cap ) )
381 /* Flag it! */
382 p_ext->p_sys->i_capabilities |= 1 << i;
383 found = true;
384 break;
387 if( !found )
389 msg_Warn( p_mgr, "Extension capability '%s' unknown in"
390 " script %s", psz_cap, psz_script );
392 /* Removes 'value'; keeps 'key' for next iteration */
393 lua_pop( L, 1 );
396 else
398 msg_Warn( p_mgr, "In script %s, function descriptor() "
399 "did not return a table of capabilities.",
400 psz_script );
402 lua_pop( L, 1 );
404 /* Get title */
405 lua_getfield( L, -1, "title" );
406 if( lua_isstring( L, -1 ) )
408 p_ext->psz_title = strdup( luaL_checkstring( L, -1 ) );
410 else
412 msg_Dbg( p_mgr, "In script %s, function descriptor() "
413 "did not return a string as title.",
414 psz_script );
415 p_ext->psz_title = strdup( psz_script );
417 lua_pop( L, 1 );
419 /* Get author */
420 lua_getfield( L, -1, "author" );
421 p_ext->psz_author = luaL_strdupornull( L, -1 );
422 lua_pop( L, 1 );
424 /* Get description */
425 lua_getfield( L, -1, "description" );
426 p_ext->psz_description = luaL_strdupornull( L, -1 );
427 lua_pop( L, 1 );
429 /* Get short description */
430 lua_getfield( L, -1, "shortdesc" );
431 p_ext->psz_shortdescription = luaL_strdupornull( L, -1 );
432 lua_pop( L, 1 );
434 /* Get URL */
435 lua_getfield( L, -1, "url" );
436 p_ext->psz_url = luaL_strdupornull( L, -1 );
437 lua_pop( L, 1 );
439 /* Get version */
440 lua_getfield( L, -1, "version" );
441 p_ext->psz_version = luaL_strdupornull( L, -1 );
442 lua_pop( L, 1 );
444 /* Get icon data */
445 lua_getfield( L, -1, "icon" );
446 if( !lua_isnil( L, -1 ) && lua_isstring( L, -1 ) )
448 int len = lua_strlen( L, -1 );
449 p_ext->p_icondata = malloc( len );
450 if( p_ext->p_icondata )
452 p_ext->i_icondata_size = len;
453 memcpy( p_ext->p_icondata, lua_tostring( L, -1 ), len );
456 lua_pop( L, 1 );
458 else
460 msg_Warn( p_mgr, "In script %s, function descriptor() "
461 "did not return a table!", psz_script );
462 goto exit;
465 else
467 msg_Err( p_mgr, "Script %s went completely foobar", psz_script );
468 goto exit;
471 msg_Dbg( p_mgr, "Script %s has the following capability flags: 0x%x",
472 psz_script, p_ext->p_sys->i_capabilities );
474 b_ok = true;
475 exit:
476 lua_close( L );
477 if( !b_ok )
479 free( p_ext->psz_name );
480 free( p_ext->psz_title );
481 free( p_ext->psz_url );
482 free( p_ext->psz_author );
483 free( p_ext->psz_description );
484 free( p_ext->psz_shortdescription );
485 free( p_ext->psz_version );
486 free( p_ext->p_sys );
487 free( p_ext );
489 else
491 /* Add the extension to the list of known extensions */
492 ARRAY_APPEND( p_mgr->extensions, p_ext );
495 /* Continue batch execution */
496 return VLC_EGENERIC;
499 static int Control( extensions_manager_t *p_mgr, int i_control, va_list args )
501 extension_t *p_ext = NULL;
502 bool *pb = NULL;
503 uint16_t **ppus = NULL;
504 char ***pppsz = NULL;
505 int i = 0;
507 switch( i_control )
509 case EXTENSION_ACTIVATE:
510 p_ext = va_arg( args, extension_t* );
511 return Activate( p_mgr, p_ext );
513 case EXTENSION_DEACTIVATE:
514 p_ext = va_arg( args, extension_t* );
515 return Deactivate( p_mgr, p_ext );
517 case EXTENSION_IS_ACTIVATED:
518 p_ext = va_arg( args, extension_t* );
519 pb = va_arg( args, bool* );
520 vlc_mutex_lock( &p_ext->p_sys->command_lock );
521 *pb = p_ext->p_sys->b_activated;
522 vlc_mutex_unlock( &p_ext->p_sys->command_lock );
523 break;
525 case EXTENSION_HAS_MENU:
526 p_ext = va_arg( args, extension_t* );
527 pb = va_arg( args, bool* );
528 *pb = ( p_ext->p_sys->i_capabilities & EXT_HAS_MENU ) ? 1 : 0;
529 break;
531 case EXTENSION_GET_MENU:
532 p_ext = va_arg( args, extension_t* );
533 pppsz = va_arg( args, char*** );
534 ppus = va_arg( args, uint16_t** );
535 if( p_ext == NULL )
536 return VLC_EGENERIC;
537 return GetMenuEntries( p_mgr, p_ext, pppsz, ppus );
539 case EXTENSION_TRIGGER_ONLY:
540 p_ext = va_arg( args, extension_t* );
541 pb = va_arg( args, bool* );
542 *pb = ( p_ext->p_sys->i_capabilities & EXT_TRIGGER_ONLY ) ? 1 : 0;
543 break;
545 case EXTENSION_TRIGGER:
546 p_ext = va_arg( args, extension_t* );
547 return TriggerExtension( p_mgr, p_ext );
549 case EXTENSION_TRIGGER_MENU:
550 p_ext = va_arg( args, extension_t* );
551 i = va_arg( args, int );
552 return TriggerMenu( p_ext, i );
554 case EXTENSION_SET_INPUT:
556 p_ext = va_arg( args, extension_t* );
557 input_item_t *p_item = va_arg( args, struct input_item_t * );
559 if( p_ext == NULL )
560 return VLC_EGENERIC;
561 vlc_mutex_lock( &p_ext->p_sys->command_lock );
562 if ( p_ext->p_sys->b_exiting == true )
564 vlc_mutex_unlock( &p_ext->p_sys->command_lock );
565 return VLC_EGENERIC;
567 vlc_mutex_unlock( &p_ext->p_sys->command_lock );
569 vlc_mutex_lock( &p_ext->p_sys->running_lock );
571 // Change input
572 input_item_t *old = p_ext->p_sys->p_item;
573 if( old )
575 // Untrack meta fetched events
576 if( p_ext->p_sys->i_capabilities & EXT_META_LISTENER )
578 vlc_event_detach( &old->event_manager,
579 vlc_InputItemMetaChanged,
580 inputItemMetaChanged,
581 p_ext );
583 input_item_Release( old );
586 p_ext->p_sys->p_item = p_item ? input_item_Hold(p_item) : NULL;
588 // Tell the script the input changed
589 if( p_ext->p_sys->i_capabilities & EXT_INPUT_LISTENER )
591 PushCommandUnique( p_ext, CMD_SET_INPUT );
594 // Track meta fetched events
595 if( p_ext->p_sys->p_item &&
596 p_ext->p_sys->i_capabilities & EXT_META_LISTENER )
598 vlc_event_attach( &p_item->event_manager,
599 vlc_InputItemMetaChanged,
600 inputItemMetaChanged,
601 p_ext );
604 vlc_mutex_unlock( &p_ext->p_sys->running_lock );
605 break;
607 case EXTENSION_PLAYING_CHANGED:
609 p_ext = va_arg( args, extension_t* );
610 assert( p_ext->psz_name != NULL );
611 i = va_arg( args, int );
612 if( p_ext->p_sys->i_capabilities & EXT_PLAYING_LISTENER )
614 PushCommand( p_ext, CMD_PLAYING_CHANGED, i );
616 break;
618 case EXTENSION_META_CHANGED:
620 p_ext = va_arg( args, extension_t* );
621 PushCommand( p_ext, CMD_UPDATE_META );
622 break;
624 default:
625 msg_Warn( p_mgr, "Control '%d' not yet implemented in Extension",
626 i_control );
627 return VLC_EGENERIC;
630 return VLC_SUCCESS;
633 int lua_ExtensionActivate( extensions_manager_t *p_mgr, extension_t *p_ext )
635 assert( p_mgr != NULL && p_ext != NULL );
636 return lua_ExecuteFunction( p_mgr, p_ext, "activate", LUA_END );
639 int lua_ExtensionDeactivate( extensions_manager_t *p_mgr, extension_t *p_ext )
641 assert( p_mgr != NULL && p_ext != NULL );
643 if( p_ext->p_sys->b_activated == false )
644 return VLC_SUCCESS;
646 vlclua_fd_interrupt( &p_ext->p_sys->dtable );
648 // Unset and release input objects
649 if( p_ext->p_sys->p_item )
651 if( p_ext->p_sys->i_capabilities & EXT_META_LISTENER )
652 vlc_event_detach( &p_ext->p_sys->p_item->event_manager,
653 vlc_InputItemMetaChanged,
654 inputItemMetaChanged,
655 p_ext );
656 input_item_Release(p_ext->p_sys->p_item);
657 p_ext->p_sys->p_item = NULL;
660 int i_ret = lua_ExecuteFunction( p_mgr, p_ext, "deactivate", LUA_END );
662 if ( p_ext->p_sys->L == NULL )
663 return VLC_EGENERIC;
664 lua_close( p_ext->p_sys->L );
665 p_ext->p_sys->L = NULL;
667 return i_ret;
670 int lua_ExtensionWidgetClick( extensions_manager_t *p_mgr,
671 extension_t *p_ext,
672 extension_widget_t *p_widget )
674 if( !p_ext->p_sys->L )
675 return VLC_SUCCESS;
677 lua_State *L = GetLuaState( p_mgr, p_ext );
678 lua_pushlightuserdata( L, p_widget );
679 lua_gettable( L, LUA_REGISTRYINDEX );
680 return lua_ExecuteFunction( p_mgr, p_ext, NULL, LUA_END );
685 * Get the list of menu entries from an extension script
686 * @param p_mgr
687 * @param p_ext
688 * @param pppsz_titles Pointer to NULL. All strings must be freed by the caller
689 * @param ppi_ids Pointer to NULL. Must be freed by the caller.
690 * @note This function is allowed to run in the UI thread. This means
691 * that it MUST respond very fast.
692 * @todo Remove the menu() hook and provide a new function vlc.set_menu()
694 static int GetMenuEntries( extensions_manager_t *p_mgr, extension_t *p_ext,
695 char ***pppsz_titles, uint16_t **ppi_ids )
697 assert( *pppsz_titles == NULL );
698 assert( *ppi_ids == NULL );
700 vlc_mutex_lock( &p_ext->p_sys->command_lock );
701 if( p_ext->p_sys->b_activated == false || p_ext->p_sys->b_exiting == true )
703 vlc_mutex_unlock( &p_ext->p_sys->command_lock );
704 msg_Dbg( p_mgr, "Can't get menu of an unactivated/dying extension!" );
705 return VLC_EGENERIC;
707 vlc_mutex_unlock( &p_ext->p_sys->command_lock );
709 vlc_mutex_lock( &p_ext->p_sys->running_lock );
711 int i_ret = VLC_EGENERIC;
712 lua_State *L = GetLuaState( p_mgr, p_ext );
714 if( ( p_ext->p_sys->i_capabilities & EXT_HAS_MENU ) == 0 )
716 msg_Dbg( p_mgr, "can't get a menu from an extension without menu!" );
717 goto exit;
720 lua_getglobal( L, "menu" );
722 if( !lua_isfunction( L, -1 ) )
724 msg_Warn( p_mgr, "Error while running script %s, "
725 "function menu() not found", p_ext->psz_name );
726 goto exit;
729 if( lua_pcall( L, 0, 1, 0 ) )
731 msg_Warn( p_mgr, "Error while running script %s, "
732 "function menu(): %s", p_ext->psz_name,
733 lua_tostring( L, lua_gettop( L ) ) );
734 goto exit;
737 if( lua_gettop( L ) )
739 if( lua_istable( L, -1 ) )
741 /* Get table size */
742 size_t i_size = lua_objlen( L, -1 );
743 *pppsz_titles = ( char** ) calloc( i_size+1, sizeof( char* ) );
744 *ppi_ids = ( uint16_t* ) calloc( i_size+1, sizeof( uint16_t ) );
746 /* Walk table */
747 size_t i_idx = 0;
748 lua_pushnil( L );
749 while( lua_next( L, -2 ) != 0 )
751 assert( i_idx < i_size );
752 if( (!lua_isstring( L, -1 )) || (!lua_isnumber( L, -2 )) )
754 msg_Warn( p_mgr, "In script %s, an entry in "
755 "the menu table is invalid!", p_ext->psz_name );
756 goto exit;
758 (*pppsz_titles)[ i_idx ] = strdup( luaL_checkstring( L, -1 ) );
759 (*ppi_ids)[ i_idx ] = luaL_checkinteger( L, -2 ) & 0xFFFF;
760 i_idx++;
761 lua_pop( L, 1 );
764 else
766 msg_Warn( p_mgr, "Function menu() in script %s "
767 "did not return a table", p_ext->psz_name );
768 goto exit;
771 else
773 msg_Warn( p_mgr, "Script %s went completely foobar", p_ext->psz_name );
774 goto exit;
777 i_ret = VLC_SUCCESS;
779 exit:
780 vlc_mutex_unlock( &p_ext->p_sys->running_lock );
781 if( i_ret != VLC_SUCCESS )
783 msg_Dbg( p_mgr, "Something went wrong in %s (%s:%d)",
784 __func__, __FILE__, __LINE__ );
786 return i_ret;
789 /* Must be entered with the Lock on Extension */
790 static lua_State* GetLuaState( extensions_manager_t *p_mgr,
791 extension_t *p_ext )
793 assert( p_ext != NULL );
794 lua_State *L = p_ext->p_sys->L;
796 if( !L )
798 L = luaL_newstate();
799 if( !L )
801 msg_Err( p_mgr, "Could not create new Lua State" );
802 return NULL;
804 vlclua_set_this( L, p_mgr );
805 intf_thread_t *intf = (intf_thread_t *) vlc_object_parent(p_mgr);
806 vlc_playlist_t *playlist = vlc_intf_GetMainPlaylist(intf);
807 vlclua_set_playlist_internal(L, playlist);
808 vlclua_extension_set( L, p_ext );
810 luaL_openlibs( L );
811 luaL_register_namespace( L, "vlc", p_reg );
812 luaopen_msg( L );
814 /* Load more libraries */
815 luaopen_config( L );
816 luaopen_dialog( L, p_ext );
817 luaopen_input( L );
818 luaopen_msg( L );
819 if( vlclua_fd_init( L, &p_ext->p_sys->dtable ) )
821 lua_close( L );
822 return NULL;
824 luaopen_object( L );
825 luaopen_osd( L );
826 luaopen_playlist( L );
827 luaopen_stream( L );
828 luaopen_strings( L );
829 luaopen_variables( L );
830 luaopen_video( L );
831 luaopen_vlm( L );
832 luaopen_volume( L );
833 luaopen_xml( L );
834 luaopen_vlcio( L );
835 luaopen_errno( L );
836 luaopen_rand( L );
837 luaopen_rd( L );
838 #if defined(_WIN32) && !VLC_WINSTORE_APP
839 luaopen_win( L );
840 #endif
842 /* Register extension specific functions */
843 lua_getglobal( L, "vlc" );
844 lua_pushcfunction( L, vlclua_extension_deactivate );
845 lua_setfield( L, -2, "deactivate" );
846 lua_pushcfunction( L, vlclua_extension_keep_alive );
847 lua_setfield( L, -2, "keep_alive" );
849 /* Setup the module search path */
850 if( !strncmp( p_ext->psz_name, "zip://", 6 ) )
852 /* Load all required modules manually */
853 lua_register( L, "require", &vlclua_extension_require );
855 else
857 if( vlclua_add_modules_path( L, p_ext->psz_name ) )
859 msg_Warn( p_mgr, "Error while setting the module "
860 "search path for %s", p_ext->psz_name );
861 vlclua_fd_cleanup( &p_ext->p_sys->dtable );
862 lua_close( L );
863 return NULL;
866 /* Load and run the script(s) */
867 if( vlclua_dofile( VLC_OBJECT( p_mgr ), L, p_ext->psz_name ) )
869 msg_Warn( p_mgr, "Error loading script %s: %s", p_ext->psz_name,
870 lua_tostring( L, lua_gettop( L ) ) );
871 vlclua_fd_cleanup( &p_ext->p_sys->dtable );
872 lua_close( L );
873 return NULL;
876 p_ext->p_sys->L = L;
879 return L;
882 int lua_ExecuteFunction( extensions_manager_t *p_mgr, extension_t *p_ext,
883 const char *psz_function, ... )
885 va_list args;
886 va_start( args, psz_function );
887 int i_ret = lua_ExecuteFunctionVa( p_mgr, p_ext, psz_function, args );
888 va_end( args );
889 return i_ret;
893 * Execute a function in a Lua script
894 * @param psz_function Name of global function to execute. If NULL, assume
895 * that the function object is already on top of the
896 * stack.
897 * @return < 0 in case of failure, >= 0 in case of success
898 * @note It's better to call this function from a dedicated thread
899 * (see extension_thread.c)
901 int lua_ExecuteFunctionVa( extensions_manager_t *p_mgr, extension_t *p_ext,
902 const char *psz_function, va_list args )
904 int i_ret = VLC_SUCCESS;
905 int i_args = 0;
906 assert( p_mgr != NULL );
907 assert( p_ext != NULL );
909 lua_State *L = GetLuaState( p_mgr, p_ext );
910 if( !L )
911 return -1;
913 if( psz_function )
914 lua_getglobal( L, psz_function );
916 if( !lua_isfunction( L, -1 ) )
918 msg_Warn( p_mgr, "Error while running script %s, "
919 "function %s() not found", p_ext->psz_name, psz_function );
920 lua_pop( L, 1 );
921 goto exit;
924 lua_datatype_e type = LUA_END;
925 while( ( type = va_arg( args, int ) ) != LUA_END )
927 if( type == LUA_NUM )
929 lua_pushnumber( L , va_arg( args, int ) );
931 else if( type == LUA_TEXT )
933 lua_pushstring( L , va_arg( args, char* ) );
935 else
937 msg_Warn( p_mgr, "Undefined argument type %d to lua function %s"
938 "from script %s", type, psz_function, p_ext->psz_name );
939 if( i_args > 0 )
940 lua_pop( L, i_args );
941 goto exit;
943 i_args ++;
946 // Start actual call to Lua
947 if( lua_pcall( L, i_args, 1, 0 ) )
949 msg_Warn( p_mgr, "Error while running script %s, "
950 "function %s(): %s", p_ext->psz_name, psz_function,
951 lua_tostring( L, lua_gettop( L ) ) );
952 i_ret = VLC_EGENERIC;
955 i_ret |= lua_DialogFlush( L );
957 exit:
958 return i_ret;
962 static inline int TriggerMenu( extension_t *p_ext, int i_id )
964 return PushCommand( p_ext, CMD_TRIGGERMENU, i_id );
967 int lua_ExtensionTriggerMenu( extensions_manager_t *p_mgr,
968 extension_t *p_ext, int id )
970 int i_ret = VLC_SUCCESS;
971 lua_State *L = GetLuaState( p_mgr, p_ext );
973 if( !L )
974 return VLC_EGENERIC;
976 luaopen_dialog( L, p_ext );
978 lua_getglobal( L, "trigger_menu" );
979 if( !lua_isfunction( L, -1 ) )
981 msg_Warn( p_mgr, "Error while running script %s, "
982 "function trigger_menu() not found", p_ext->psz_name );
983 return VLC_EGENERIC;
986 /* Pass id as unique argument to the function */
987 lua_pushinteger( L, id );
989 if( lua_pcall( L, 1, 1, 0 ) != 0 )
991 msg_Warn( p_mgr, "Error while running script %s, "
992 "function trigger_menu(): %s", p_ext->psz_name,
993 lua_tostring( L, lua_gettop( L ) ) );
994 i_ret = VLC_EGENERIC;
997 i_ret |= lua_DialogFlush( L );
998 if( i_ret < VLC_SUCCESS )
1000 msg_Dbg( p_mgr, "Something went wrong in %s (%s:%d)",
1001 __func__, __FILE__, __LINE__ );
1004 return i_ret;
1007 /** Directly trigger an extension, without activating it
1008 * This is NOT multithreaded, and this code runs in the UI thread
1009 * @param p_mgr
1010 * @param p_ext Extension to trigger
1011 * @return Value returned by the lua function "trigger"
1013 static int TriggerExtension( extensions_manager_t *p_mgr,
1014 extension_t *p_ext )
1016 int i_ret = lua_ExecuteFunction( p_mgr, p_ext, "trigger", LUA_END );
1018 /* Close lua state for trigger-only extensions */
1019 if( p_ext->p_sys->L )
1021 vlclua_fd_cleanup( &p_ext->p_sys->dtable );
1022 lua_close( p_ext->p_sys->L );
1024 p_ext->p_sys->L = NULL;
1026 return i_ret;
1029 /** Set extension associated to the current script
1030 * @param L current lua_State
1031 * @param p_ext the extension
1033 void vlclua_extension_set( lua_State *L, extension_t *p_ext )
1035 lua_pushlightuserdata( L, vlclua_extension_set );
1036 lua_pushlightuserdata( L, p_ext );
1037 lua_rawset( L, LUA_REGISTRYINDEX );
1040 /** Retrieve extension associated to the current script
1041 * @param L current lua_State
1042 * @return Extension pointer
1044 extension_t *vlclua_extension_get( lua_State *L )
1046 lua_pushlightuserdata( L, vlclua_extension_set );
1047 lua_rawget( L, LUA_REGISTRYINDEX );
1048 extension_t *p_ext = (extension_t*) lua_topointer( L, -1 );
1049 lua_pop( L, 1 );
1050 return p_ext;
1053 /** Deactivate an extension by order from the extension itself
1054 * @param L lua_State
1055 * @note This is an asynchronous call. A script calling vlc.deactivate() will
1056 * be executed to the end before the last call to deactivate() is done.
1058 int vlclua_extension_deactivate( lua_State *L )
1060 extension_t *p_ext = vlclua_extension_get( L );
1061 vlc_mutex_lock( &p_ext->p_sys->command_lock );
1062 bool b_ret = QueueDeactivateCommand( p_ext );
1063 vlc_mutex_unlock( &p_ext->p_sys->command_lock );
1064 return ( b_ret == true ) ? 1 : 0;
1067 /** Keep an extension alive. This resets the watch timer to 0
1068 * @param L lua_State
1069 * @note This is the "vlc.keep_alive()" function
1071 int vlclua_extension_keep_alive( lua_State *L )
1073 extension_t *p_ext = vlclua_extension_get( L );
1075 vlc_mutex_lock( &p_ext->p_sys->command_lock );
1076 if( p_ext->p_sys->p_progress_id != NULL )
1078 vlc_dialog_release( p_ext->p_sys->p_mgr, p_ext->p_sys->p_progress_id );
1079 p_ext->p_sys->p_progress_id = NULL;
1081 vlc_timer_schedule( p_ext->p_sys->timer, false, WATCH_TIMER_PERIOD,
1082 VLC_TIMER_FIRE_ONCE );
1083 vlc_mutex_unlock( &p_ext->p_sys->command_lock );
1085 return 1;
1088 /** Callback for the variable "dialog-event"
1089 * @param p_this Current object owner of the extension and the dialog
1090 * @param psz_var "dialog-event"
1091 * @param oldval Unused
1092 * @param newval Address of the dialog
1093 * @param p_data Unused
1095 static int vlclua_extension_dialog_callback( vlc_object_t *p_this,
1096 char const *psz_var,
1097 vlc_value_t oldval,
1098 vlc_value_t newval,
1099 void *p_data )
1101 /* psz_var == "dialog-event" */
1102 ( void ) psz_var;
1103 ( void ) oldval;
1104 ( void ) p_data;
1106 extension_dialog_command_t *command = newval.p_address;
1107 assert( command != NULL );
1108 assert( command->p_dlg != NULL);
1110 extension_t *p_ext = command->p_dlg->p_sys;
1111 assert( p_ext != NULL );
1113 extension_widget_t *p_widget = command->p_data;
1115 switch( command->event )
1117 case EXTENSION_EVENT_CLICK:
1118 assert( p_widget != NULL );
1119 PushCommandUnique( p_ext, CMD_CLICK, p_widget );
1120 break;
1121 case EXTENSION_EVENT_CLOSE:
1122 PushCommandUnique( p_ext, CMD_CLOSE );
1123 break;
1124 default:
1125 msg_Dbg( p_this, "Received unknown UI event %d, discarded",
1126 command->event );
1127 break;
1130 return VLC_SUCCESS;
1133 /** Callback on vlc_InputItemMetaChanged event
1135 static void inputItemMetaChanged( const vlc_event_t *p_event,
1136 void *data )
1138 assert( p_event && p_event->type == vlc_InputItemMetaChanged );
1140 extension_t *p_ext = ( extension_t* ) data;
1141 assert( p_ext != NULL );
1143 PushCommandUnique( p_ext, CMD_UPDATE_META );
1146 /** Watch timer callback
1147 * The timer expired, Lua may be stuck, ask the user what to do now
1149 static void WatchTimerCallback( void *data )
1151 extension_t *p_ext = data;
1152 extensions_manager_t *p_mgr = p_ext->p_sys->p_mgr;
1154 vlc_mutex_lock( &p_ext->p_sys->command_lock );
1156 for( struct command_t *cmd = p_ext->p_sys->command;
1157 cmd != NULL;
1158 cmd = cmd->next )
1159 if( cmd->i_command == CMD_DEACTIVATE )
1160 { /* We have a pending Deactivate command... */
1161 if( p_ext->p_sys->p_progress_id != NULL )
1163 vlc_dialog_release( p_mgr, p_ext->p_sys->p_progress_id );
1164 p_ext->p_sys->p_progress_id = NULL;
1166 KillExtension( p_mgr, p_ext );
1167 vlc_mutex_unlock( &p_ext->p_sys->command_lock );
1168 return;
1171 if( p_ext->p_sys->p_progress_id == NULL )
1173 p_ext->p_sys->p_progress_id =
1174 vlc_dialog_display_progress( p_mgr, true, 0.0,
1175 _( "Yes" ),
1176 _( "Extension not responding!" ),
1177 _( "Extension '%s' does not respond.\n"
1178 "Do you want to kill it now? " ),
1179 p_ext->psz_title );
1180 if( 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;
1186 vlc_timer_schedule( p_ext->p_sys->timer, false, VLC_TICK_FROM_MS(100),
1187 VLC_TIMER_FIRE_ONCE );
1189 else
1191 if( vlc_dialog_is_cancelled( p_mgr, p_ext->p_sys->p_progress_id ) )
1193 vlc_dialog_release( p_mgr, p_ext->p_sys->p_progress_id );
1194 p_ext->p_sys->p_progress_id = NULL;
1195 KillExtension( p_mgr, p_ext );
1196 vlc_mutex_unlock( &p_ext->p_sys->command_lock );
1197 return;
1199 vlc_timer_schedule( p_ext->p_sys->timer, false, VLC_TICK_FROM_MS(100),
1200 VLC_TIMER_FIRE_ONCE );
1202 vlc_mutex_unlock( &p_ext->p_sys->command_lock );