lua_extensions: no need to lock the object as we are inside a call to module need...
[vlc/asuraparaju-public.git] / modules / misc / lua / extension.c
blobdf7a4658c1b34735acea390a13b5bbe78e3e0a67
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 #include "vlc.h"
25 #include "libs.h"
26 #include "extension.h"
27 #include "assert.h"
29 #include <vlc_input.h>
30 #include <vlc_events.h>
32 /* Functions to register */
33 static const luaL_Reg p_reg[] =
35 { NULL, NULL }
39 * Extensions capabilities
40 * Note: #define and ppsz_capabilities must be in sync
42 #define EXT_HAS_MENU (1 << 0) ///< Hook: menu
43 #define EXT_TRIGGER_ONLY (1 << 1) ///< Hook: trigger. Not activable
44 #define EXT_INPUT_LISTENER (1 << 2) ///< Hook: input_changed
45 #define EXT_META_LISTENER (1 << 3) ///< Hook: meta_changed
46 #define EXT_PLAYING_LISTENER (1 << 4) ///< Hook: status_changed
48 const char* const ppsz_capabilities[] = {
49 "menu",
50 "trigger",
51 "input-listener",
52 "meta-listener",
53 "playing-listener",
54 NULL
57 static int ScanExtensions( extensions_manager_t *p_this );
58 static int ScanLuaCallback( vlc_object_t *p_this, const char *psz_script,
59 void *dummy );
60 static int Control( extensions_manager_t *, int, va_list );
61 static int GetMenuEntries( extensions_manager_t *p_mgr, extension_t *p_ext,
62 char ***pppsz_titles, uint16_t **ppi_ids );
63 static lua_State* GetLuaState( extensions_manager_t *p_mgr,
64 extension_t *p_ext );
65 static int TriggerMenu( extension_t *p_ext, int id );
66 static int TriggerExtension( extensions_manager_t *p_mgr,
67 extension_t *p_ext );
69 int vlclua_extension_deactivate( lua_State *L );
71 /* Interactions */
72 static int vlclua_extension_dialog_callback( vlc_object_t *p_this,
73 char const *psz_var,
74 vlc_value_t oldval,
75 vlc_value_t newval,
76 void *p_data );
78 /* Input item callback: vlc_InputItemMetaChanged */
79 static void inputItemMetaChanged( const vlc_event_t *p_event,
80 void *data );
83 /**
84 * Module entry-point
85 **/
86 int Open_Extension( vlc_object_t *p_this )
88 msg_Dbg( p_this, "Opening EXPERIMENTAL Lua Extension module" );
90 extensions_manager_t *p_mgr = ( extensions_manager_t* ) p_this;
92 p_mgr->pf_control = Control;
94 extensions_manager_sys_t *p_sys = ( extensions_manager_sys_t* )
95 calloc( 1, sizeof( extensions_manager_sys_t ) );
96 if( !p_sys ) return VLC_ENOMEM;
98 p_mgr->p_sys = p_sys;
99 ARRAY_INIT( p_sys->activated_extensions );
100 ARRAY_INIT( p_mgr->extensions );
101 vlc_mutex_init( &p_mgr->lock );
102 vlc_mutex_init( &p_mgr->p_sys->lock );
104 /* Scan available Lua Extensions */
105 if( ScanExtensions( p_mgr ) != VLC_SUCCESS )
107 msg_Err( p_mgr, "Can't load extensions modules" );
108 return VLC_EGENERIC;
111 // Create the dialog-event variable
112 var_Create( p_this, "dialog-event", VLC_VAR_ADDRESS );
113 var_AddCallback( p_this, "dialog-event",
114 vlclua_extension_dialog_callback, NULL );
116 return VLC_SUCCESS;
120 * Module unload function
122 void Close_Extension( vlc_object_t *p_this )
124 extensions_manager_t *p_mgr = ( extensions_manager_t* ) p_this;
125 msg_Dbg( p_mgr, "Deactivating all loaded extensions" );
127 vlc_mutex_lock( &p_mgr->lock );
128 p_mgr->p_sys->b_killed = true;
129 vlc_mutex_unlock( &p_mgr->lock );
131 var_Destroy( p_mgr, "dialog-event" );
133 extension_t *p_ext = NULL;
134 FOREACH_ARRAY( p_ext, p_mgr->p_sys->activated_extensions )
136 if( !p_ext ) break;
137 msg_Dbg( p_mgr, "Deactivating '%s'", p_ext->psz_title );
138 Deactivate( p_mgr, p_ext );
139 WaitForDeactivation( p_ext );
141 FOREACH_END()
143 msg_Dbg( p_mgr, "All extensions are now deactivated" );
144 ARRAY_RESET( p_mgr->p_sys->activated_extensions );
146 vlc_mutex_destroy( &p_mgr->lock );
147 vlc_mutex_destroy( &p_mgr->p_sys->lock );
148 free( p_mgr->p_sys );
149 p_mgr->p_sys = NULL;
151 /* Free extensions' memory */
152 FOREACH_ARRAY( p_ext, p_mgr->extensions )
154 if( !p_ext )
155 break;
156 if( p_ext->p_sys->L )
157 lua_close( p_ext->p_sys->L );
158 free( p_ext->psz_name );
159 free( p_ext->psz_title );
160 free( p_ext->psz_author );
161 free( p_ext->psz_description );
162 free( p_ext->psz_shortdescription );
163 free( p_ext->psz_url );
164 free( p_ext->psz_version );
166 vlc_mutex_destroy( &p_ext->p_sys->running_lock );
167 vlc_mutex_destroy( &p_ext->p_sys->command_lock );
168 vlc_cond_destroy( &p_ext->p_sys->wait );
170 free( p_ext->p_sys );
171 free( p_ext );
173 FOREACH_END()
175 ARRAY_RESET( p_mgr->extensions );
179 * Batch scan all Lua files in folder "extensions"
180 * @param p_mgr This extensions_manager_t object
182 static int ScanExtensions( extensions_manager_t *p_mgr )
184 int i_ret =
185 vlclua_scripts_batch_execute( VLC_OBJECT( p_mgr ),
186 "extensions",
187 &ScanLuaCallback,
188 NULL );
190 if( !i_ret )
191 return VLC_EGENERIC;
193 return VLC_SUCCESS;
197 * Dummy Lua function: does nothing
198 * @note This function can be used to replace "require" while scanning for
199 * extensions
200 * Even the built-in libraries are not loaded when calling descriptor()
202 static int vlclua_dummy_require( lua_State *L )
204 (void) L;
205 return 0;
209 * Batch scan all Lua files in folder "extensions": callback
210 * @param p_this This extensions_manager_t object
211 * @param psz_script Name of the script to run
212 * @param L Lua State, common to all scripts here
213 * @param dummy: unused
215 int ScanLuaCallback( vlc_object_t *p_this, const char *psz_script,
216 void *dummy )
218 VLC_UNUSED(dummy);
219 extensions_manager_t *p_mgr = ( extensions_manager_t* ) p_this;
220 bool b_ok = false;
222 msg_Dbg( p_mgr, "Scanning Lua script %s", psz_script );
224 /* Create new script descriptor */
225 extension_t *p_ext = ( extension_t* ) calloc( 1, sizeof( extension_t ) );
226 if( !p_ext )
227 return 0;
229 p_ext->psz_name = strdup( psz_script );
230 p_ext->p_sys = (extension_sys_t*) calloc( 1, sizeof( extension_sys_t ) );
231 if( !p_ext->p_sys || !p_ext->psz_name )
233 free( p_ext->psz_name );
234 free( p_ext->p_sys );
235 free( p_ext );
236 return 0;
238 p_ext->p_sys->p_mgr = p_mgr;
240 /* Mutexes and conditions */
241 vlc_mutex_init( &p_ext->p_sys->command_lock );
242 vlc_mutex_init( &p_ext->p_sys->running_lock );
243 vlc_cond_init( &p_ext->p_sys->wait );
245 /* Prepare Lua state */
246 lua_State *L = luaL_newstate();
247 lua_register( L, "require", &vlclua_dummy_require );
249 /* Let's run it */
250 if( luaL_dofile( L, psz_script ) )
252 msg_Warn( p_mgr, "Error loading script %s: %s", psz_script,
253 lua_tostring( L, lua_gettop( L ) ) );
254 lua_pop( L, 1 );
255 goto exit;
258 /* Scan script for capabilities */
259 lua_getglobal( L, "descriptor" );
261 if( !lua_isfunction( L, -1 ) )
263 msg_Warn( p_mgr, "Error while running script %s, "
264 "function descriptor() not found", psz_script );
265 goto exit;
268 if( lua_pcall( L, 0, 1, 0 ) )
270 msg_Warn( p_mgr, "Error while running script %s, "
271 "function descriptor(): %s", psz_script,
272 lua_tostring( L, lua_gettop( L ) ) );
273 goto exit;
276 if( lua_gettop( L ) )
278 if( lua_istable( L, -1 ) )
280 /* Get caps */
281 lua_getfield( L, -1, "capabilities" );
282 if( lua_istable( L, -1 ) )
284 lua_pushnil( L );
285 while( lua_next( L, -2 ) != 0 )
287 /* Key is at index -2 and value at index -1. Discard key */
288 const char *psz_cap = luaL_checkstring( L, -1 );
289 int i_cap = 0;
290 bool b_ok = false;
291 /* Find this capability's flag */
292 for( const char *iter = *ppsz_capabilities;
293 iter != NULL;
294 iter = ppsz_capabilities[ ++i_cap ])
296 if( !strcmp( iter, psz_cap ) )
298 /* Flag it! */
299 p_ext->p_sys->i_capabilities |= 1 << i_cap;
300 b_ok = true;
301 break;
304 if( !b_ok )
306 msg_Warn( p_mgr, "Extension capability '%s' unknown in"
307 " script %s", psz_cap, psz_script );
309 /* Removes 'value'; keeps 'key' for next iteration */
310 lua_pop( L, 1 );
313 else
315 msg_Warn( p_mgr, "In script %s, function descriptor() "
316 "did not return a table of capabilities.",
317 psz_script );
319 lua_pop( L, 1 );
321 /* Get title */
322 lua_getfield( L, -1, "title" );
323 if( lua_isstring( L, -1 ) )
325 p_ext->psz_title = strdup( luaL_checkstring( L, -1 ) );
327 else
329 msg_Dbg( p_mgr, "In script %s, function descriptor() "
330 "did not return a string as title.",
331 psz_script );
332 p_ext->psz_title = strdup( psz_script );
334 lua_pop( L, 1 );
336 /* Get author */
337 lua_getfield( L, -1, "author" );
338 p_ext->psz_author = luaL_strdupornull( L, -1 );
339 lua_pop( L, 1 );
341 /* Get description */
342 lua_getfield( L, -1, "description" );
343 p_ext->psz_description = luaL_strdupornull( L, -1 );
344 lua_pop( L, 1 );
346 /* Get short description */
347 lua_getfield( L, -1, "shortdesc" );
348 p_ext->psz_shortdescription = luaL_strdupornull( L, -1 );
349 lua_pop( L, 1 );
351 /* Get URL */
352 lua_getfield( L, -1, "url" );
353 p_ext->psz_url = luaL_strdupornull( L, -1 );
354 lua_pop( L, 1 );
356 /* Get version */
357 lua_getfield( L, -1, "version" );
358 p_ext->psz_version = luaL_strdupornull( L, -1 );
359 lua_pop( L, 1 );
361 else
363 msg_Warn( p_mgr, "In script %s, function descriptor() "
364 "did not return a table!", psz_script );
365 goto exit;
368 else
370 msg_Err( p_mgr, "Script %s went completely foobar", psz_script );
371 goto exit;
374 msg_Dbg( p_mgr, "Script %s has the following capability flags: 0x%x",
375 psz_script, p_ext->p_sys->i_capabilities );
377 b_ok = true;
378 exit:
379 lua_close( L );
380 if( !b_ok )
382 free( p_ext->psz_name );
383 free( p_ext->psz_title );
384 free( p_ext->psz_url );
385 free( p_ext->psz_author );
386 free( p_ext->psz_description );
387 free( p_ext->psz_shortdescription );
388 free( p_ext->psz_version );
389 vlc_mutex_destroy( &p_ext->p_sys->command_lock );
390 vlc_mutex_destroy( &p_ext->p_sys->running_lock );
391 vlc_cond_destroy( &p_ext->p_sys->wait );
392 free( p_ext->p_sys );
393 free( p_ext );
395 else
397 /* Add the extension to the list of known extensions */
398 ARRAY_APPEND( p_mgr->extensions, p_ext );
401 /* Continue batch execution */
402 return VLC_EGENERIC;
405 static int Control( extensions_manager_t *p_mgr, int i_control, va_list args )
407 extension_t *p_ext = NULL;
408 bool *pb = NULL;
409 uint16_t **ppus = NULL;
410 char ***pppsz = NULL;
411 int i = 0;
413 switch( i_control )
415 case EXTENSION_ACTIVATE:
416 p_ext = ( extension_t* ) va_arg( args, extension_t* );
417 return Activate( p_mgr, p_ext );
419 case EXTENSION_DEACTIVATE:
420 p_ext = ( extension_t* ) va_arg( args, extension_t* );
421 return Deactivate( p_mgr, p_ext );
423 case EXTENSION_IS_ACTIVATED:
424 p_ext = ( extension_t* ) va_arg( args, extension_t* );
425 pb = ( bool* ) va_arg( args, bool* );
426 *pb = IsActivated( p_mgr, p_ext );
427 break;
429 case EXTENSION_HAS_MENU:
430 p_ext = ( extension_t* ) va_arg( args, extension_t* );
431 pb = ( bool* ) va_arg( args, bool* );
432 *pb = ( p_ext->p_sys->i_capabilities & EXT_HAS_MENU ) ? 1 : 0;
433 break;
435 case EXTENSION_GET_MENU:
436 p_ext = ( extension_t* ) va_arg( args, extension_t* );
437 pppsz = ( char*** ) va_arg( args, char*** );
438 ppus = ( uint16_t** ) va_arg( args, uint16_t** );
439 return GetMenuEntries( p_mgr, p_ext, pppsz, ppus );
441 case EXTENSION_TRIGGER_ONLY:
442 p_ext = ( extension_t* ) va_arg( args, extension_t* );
443 pb = ( bool* ) va_arg( args, bool* );
444 *pb = ( p_ext->p_sys->i_capabilities & EXT_TRIGGER_ONLY ) ? 1 : 0;
445 break;
447 case EXTENSION_TRIGGER:
448 p_ext = ( extension_t* ) va_arg( args, extension_t* );
449 return TriggerExtension( p_mgr, p_ext );
451 case EXTENSION_TRIGGER_MENU:
452 p_ext = ( extension_t* ) va_arg( args, extension_t* );
453 // GCC: 'uint16_t' is promoted to 'int' when passed through '...'
454 i = ( int ) va_arg( args, int );
455 return TriggerMenu( p_ext, i );
457 case EXTENSION_SET_INPUT:
459 p_ext = ( extension_t* ) va_arg( args, extension_t* );
460 input_thread_t *p_input = va_arg( args, struct input_thread_t * );
462 if( !LockExtension( p_ext ) )
463 return VLC_EGENERIC;
465 // Change input
466 input_thread_t *old = p_ext->p_sys->p_input;
467 input_item_t *p_item;
468 if( old )
470 // Untrack meta fetched events
471 if( p_ext->p_sys->i_capabilities & EXT_META_LISTENER )
473 p_item = input_GetItem( old );
474 vlc_event_detach( &p_item->event_manager,
475 vlc_InputItemMetaChanged,
476 inputItemMetaChanged,
477 p_ext );
478 vlc_gc_decref( p_item );
480 vlc_object_release( old );
483 p_ext->p_sys->p_input = p_input ? vlc_object_hold( p_input )
484 : p_input;
486 // Tell the script the input changed
487 if( p_ext->p_sys->i_capabilities & EXT_INPUT_LISTENER )
489 PushCommandUnique( p_ext, CMD_SET_INPUT );
492 // Track meta fetched events
493 if( p_ext->p_sys->p_input &&
494 p_ext->p_sys->i_capabilities & EXT_META_LISTENER )
496 p_item = input_GetItem( p_ext->p_sys->p_input );
497 vlc_gc_incref( p_item );
498 vlc_event_attach( &p_item->event_manager,
499 vlc_InputItemMetaChanged,
500 inputItemMetaChanged,
501 p_ext );
504 UnlockExtension( p_ext );
505 break;
507 case EXTENSION_PLAYING_CHANGED:
509 extension_t *p_ext;
510 p_ext = ( extension_t* ) va_arg( args, extension_t* );
511 assert( p_ext->psz_name != NULL );
512 i = ( int ) va_arg( args, int );
513 if( p_ext->p_sys->i_capabilities & EXT_PLAYING_LISTENER )
515 PushCommand( p_ext, CMD_PLAYING_CHANGED, i );
517 break;
519 default:
520 msg_Warn( p_mgr, "Control '%d' not yet implemented in Extension",
521 i_control );
522 return VLC_EGENERIC;
525 return VLC_SUCCESS;
528 int lua_ExtensionActivate( extensions_manager_t *p_mgr, extension_t *p_ext )
530 assert( p_mgr != NULL && p_ext != NULL );
531 return lua_ExecuteFunction( p_mgr, p_ext, "activate", LUA_END );
534 int lua_ExtensionDeactivate( extensions_manager_t *p_mgr, extension_t *p_ext )
536 assert( p_mgr != NULL && p_ext != NULL );
538 if( !p_ext->p_sys->L )
539 return VLC_SUCCESS;
541 // Unset and release input objects
542 if( p_ext->p_sys->p_input )
544 if( p_ext->p_sys->i_capabilities & EXT_META_LISTENER )
546 // Release item
547 input_item_t *p_item = input_GetItem( p_ext->p_sys->p_input );
548 vlc_gc_decref( p_item );
550 vlc_object_release( p_ext->p_sys->p_input );
553 int i_ret = lua_ExecuteFunction( p_mgr, p_ext, "deactivate", LUA_END );
555 /* Clear Lua State */
556 lua_close( p_ext->p_sys->L );
557 p_ext->p_sys->L = NULL;
559 return i_ret;
562 int lua_ExtensionWidgetClick( extensions_manager_t *p_mgr,
563 extension_t *p_ext,
564 extension_widget_t *p_widget )
566 if( !p_ext->p_sys->L )
567 return VLC_SUCCESS;
569 lua_State *L = GetLuaState( p_mgr, p_ext );
570 lua_pushlightuserdata( L, p_widget );
571 lua_gettable( L, LUA_REGISTRYINDEX );
572 return lua_ExecuteFunction( p_mgr, p_ext, NULL, LUA_END );
577 * Get the list of menu entries from an extension script
578 * @param p_mgr
579 * @param p_ext
580 * @param pppsz_titles Pointer to NULL. All strings must be freed by the caller
581 * @param ppi_ids Pointer to NULL. Must be freed by the caller.
582 * @note This function is allowed to run in the UI thread. This means
583 * that it MUST respond very fast.
584 * @todo Remove the menu() hook and provide a new function vlc.set_menu()
586 static int GetMenuEntries( extensions_manager_t *p_mgr, extension_t *p_ext,
587 char ***pppsz_titles, uint16_t **ppi_ids )
589 assert( *pppsz_titles == NULL );
590 assert( *ppi_ids == NULL );
592 if( !IsActivated( p_mgr, p_ext ) )
594 msg_Dbg( p_mgr, "Can't get menu before activating the extension!" );
595 return VLC_EGENERIC;
598 if( !LockExtension( p_ext ) )
600 /* Dying extension, fail. */
601 return VLC_EGENERIC;
604 int i_ret = VLC_EGENERIC;
605 lua_State *L = GetLuaState( p_mgr, p_ext );
607 if( ( p_ext->p_sys->i_capabilities & EXT_HAS_MENU ) == 0 )
609 msg_Dbg( p_mgr, "can't get a menu from an extension without menu!" );
610 goto exit;
613 lua_getglobal( L, "menu" );
615 if( !lua_isfunction( L, -1 ) )
617 msg_Warn( p_mgr, "Error while running script %s, "
618 "function menu() not found", p_ext->psz_name );
619 goto exit;
622 if( lua_pcall( L, 0, 1, 0 ) )
624 msg_Warn( p_mgr, "Error while running script %s, "
625 "function menu(): %s", p_ext->psz_name,
626 lua_tostring( L, lua_gettop( L ) ) );
627 goto exit;
630 if( lua_gettop( L ) )
632 if( lua_istable( L, -1 ) )
634 /* Get table size */
635 size_t i_size = lua_objlen( L, -1 );
636 *pppsz_titles = ( char** ) calloc( i_size+1, sizeof( char* ) );
637 *ppi_ids = ( uint16_t* ) calloc( i_size+1, sizeof( uint16_t ) );
639 /* Walk table */
640 size_t i_idx = 0;
641 lua_pushnil( L );
642 while( lua_next( L, -2 ) != 0 )
644 assert( i_idx < i_size );
645 if( (!lua_isstring( L, -1 )) || (!lua_isnumber( L, -2 )) )
647 msg_Warn( p_mgr, "In script %s, an entry in "
648 "the menu table is invalid!", p_ext->psz_name );
649 goto exit;
651 (*pppsz_titles)[ i_idx ] = strdup( luaL_checkstring( L, -1 ) );
652 (*ppi_ids)[ i_idx ] = (uint16_t) ( luaL_checkinteger( L, -2 ) & 0xFFFF );
653 i_idx++;
654 lua_pop( L, 1 );
657 else
659 msg_Warn( p_mgr, "Function menu() in script %s "
660 "did not return a table", p_ext->psz_name );
661 goto exit;
664 else
666 msg_Warn( p_mgr, "Script %s went completely foobar", p_ext->psz_name );
667 goto exit;
670 i_ret = VLC_SUCCESS;
672 exit:
673 UnlockExtension( p_ext );
674 if( i_ret != VLC_SUCCESS )
676 msg_Dbg( p_mgr, "Something went wrong in %s (%s:%d)",
677 __func__, __FILE__, __LINE__ );
679 return i_ret;
682 /* Must be entered with the Lock on Extension */
683 static lua_State* GetLuaState( extensions_manager_t *p_mgr,
684 extension_t *p_ext )
686 lua_State *L = NULL;
687 if( p_ext )
688 L = p_ext->p_sys->L;
690 if( !L )
692 L = luaL_newstate();
693 if( !L )
695 msg_Err( p_mgr, "Could not create new Lua State" );
696 return NULL;
698 vlclua_set_this( L, p_mgr );
699 vlclua_extension_set( L, p_ext );
701 luaL_openlibs( L );
702 luaL_register( L, "vlc", p_reg );
703 luaopen_msg( L );
705 if( p_ext )
707 /* Load more libraries */
708 luaopen_acl( L );
709 luaopen_config( L );
710 luaopen_dialog( L, p_ext );
711 luaopen_input( L );
712 luaopen_md5( L );
713 luaopen_msg( L );
714 luaopen_misc( L );
715 luaopen_net( L );
716 luaopen_object( L );
717 luaopen_osd( L );
718 luaopen_playlist( L );
719 luaopen_sd( L );
720 luaopen_stream( L );
721 luaopen_strings( L );
722 luaopen_variables( L );
723 luaopen_video( L );
724 luaopen_vlm( L );
725 luaopen_volume( L );
726 luaopen_xml( L );
728 /* Register extension specific functions */
729 lua_getglobal( L, "vlc" );
730 lua_pushcfunction( L, vlclua_extension_deactivate );
731 lua_setfield( L, -2, "deactivate" );
733 /* Setup the module search path */
734 if( vlclua_add_modules_path( p_mgr, L, p_ext->psz_name ) )
736 msg_Warn( p_mgr, "Error while setting the module search path for %s", p_ext->psz_name );
737 return NULL;
740 /* Load and run the script(s) */
741 if( luaL_dofile( L, p_ext->psz_name ) != 0 )
743 msg_Warn( p_mgr, "Error loading script %s: %s", p_ext->psz_name,
744 lua_tostring( L, lua_gettop( L ) ) );
745 lua_pop( L, 1 );
746 return NULL;
749 p_ext->p_sys->L = L;
753 return L;
756 int lua_ExecuteFunction( extensions_manager_t *p_mgr, extension_t *p_ext,
757 const char *psz_function, ... )
759 va_list args;
760 va_start( args, psz_function );
761 int i_ret = lua_ExecuteFunctionVa( p_mgr, p_ext, psz_function, args );
762 va_end( args );
763 return i_ret;
767 * Execute a function in a Lua script
768 * @param psz_function Name of global function to execute. If NULL, assume
769 * that the function object is already on top of the
770 * stack.
771 * @return < 0 in case of failure, >= 0 in case of success
772 * @note It's better to call this function from a dedicated thread
773 * (see extension_thread.c)
775 int lua_ExecuteFunctionVa( extensions_manager_t *p_mgr, extension_t *p_ext,
776 const char *psz_function, va_list args )
778 int i_ret = VLC_EGENERIC;
779 int i_args = 0;
780 assert( p_mgr != NULL );
781 assert( p_ext != NULL );
783 lua_State *L = GetLuaState( p_mgr, p_ext );
784 if( psz_function )
785 lua_getglobal( L, psz_function );
787 if( !lua_isfunction( L, -1 ) )
789 msg_Warn( p_mgr, "Error while running script %s, "
790 "function %s() not found", p_ext->psz_name, psz_function );
791 goto exit;
794 lua_datatype_e type = LUA_END;
795 while( ( type = va_arg( args, int ) ) != LUA_END )
797 if( type == LUA_NUM )
799 lua_pushnumber( L , ( int ) va_arg( args, int ) );
801 else if( type == LUA_TEXT )
803 lua_pushstring( L , ( char * ) va_arg( args, char* ) );
805 else
807 msg_Warn( p_mgr, "Undefined argument type %d to lua function %s"
808 "from script %s", type, psz_function, p_ext->psz_name );
809 goto exit;
811 i_args ++;
813 if( lua_pcall( L, i_args, 1, 0 ) )
815 msg_Warn( p_mgr, "Error while running script %s, "
816 "function %s(): %s", p_ext->psz_name, psz_function,
817 lua_tostring( L, lua_gettop( L ) ) );
818 goto exit;
821 i_ret = lua_DialogFlush( L );
822 exit:
823 return i_ret;
827 static inline int TriggerMenu( extension_t *p_ext, int i_id )
829 return PushCommand( p_ext, CMD_TRIGGERMENU, i_id );
832 int lua_ExtensionTriggerMenu( extensions_manager_t *p_mgr,
833 extension_t *p_ext, int id )
835 int i_ret = VLC_EGENERIC;
836 lua_State *L = GetLuaState( p_mgr, p_ext );
838 if( !L )
839 return VLC_EGENERIC;
841 luaopen_dialog( L, p_ext );
843 lua_getglobal( L, "trigger_menu" );
844 if( !lua_isfunction( L, -1 ) )
846 msg_Warn( p_mgr, "Error while running script %s, "
847 "function trigger_menu() not found", p_ext->psz_name );
848 return VLC_EGENERIC;
851 /* Pass id as unique argument to the function */
852 lua_pushinteger( L, id );
854 if( lua_pcall( L, 1, 1, 0 ) != 0 )
856 msg_Warn( p_mgr, "Error while running script %s, "
857 "function trigger_menu(): %s", p_ext->psz_name,
858 lua_tostring( L, lua_gettop( L ) ) );
859 return VLC_EGENERIC;
862 i_ret = lua_DialogFlush( L );
863 if( i_ret < VLC_SUCCESS )
865 msg_Dbg( p_mgr, "Something went wrong in %s (%s:%d)",
866 __func__, __FILE__, __LINE__ );
868 return i_ret;
871 /** Directly trigger an extension, without activating it
872 * This is NOT multithreaded, and this code runs in the UI thread
873 * @param p_mgr
874 * @param p_ext Extension to trigger
875 * @return Value returned by the lua function "trigger"
877 static int TriggerExtension( extensions_manager_t *p_mgr,
878 extension_t *p_ext )
880 int i_ret = lua_ExecuteFunction( p_mgr, p_ext, "trigger", LUA_END );
882 /* Close lua state for trigger-only extensions */
883 if( p_ext->p_sys->L )
884 lua_close( p_ext->p_sys->L );
885 p_ext->p_sys->L = NULL;
887 return i_ret;
890 /** Set extension associated to the current script
891 * @param L current lua_State
892 * @param p_ext the extension
894 void vlclua_extension_set( lua_State *L, extension_t *p_ext )
896 lua_pushlightuserdata( L, vlclua_extension_set );
897 lua_pushlightuserdata( L, p_ext );
898 lua_rawset( L, LUA_REGISTRYINDEX );
901 /** Retrieve extension associated to the current script
902 * @param L current lua_State
903 * @return Extension pointer
905 extension_t *vlclua_extension_get( lua_State *L )
907 lua_pushlightuserdata( L, vlclua_extension_set );
908 lua_rawget( L, LUA_REGISTRYINDEX );
909 extension_t *p_ext = (extension_t*) lua_topointer( L, -1 );
910 lua_pop( L, 1 );
911 return p_ext;
914 /** Deactivate an extension by order from the extension itself
915 * @param L lua_State
916 * @note This is an asynchronous call. A script calling vlc.deactivate() will
917 * be executed to the end before the last call to deactivate() is done.
919 int vlclua_extension_deactivate( lua_State *L )
921 extension_t *p_ext = vlclua_extension_get( L );
922 int i_ret = Deactivate( p_ext->p_sys->p_mgr, p_ext );
923 return ( i_ret == VLC_SUCCESS ) ? 1 : 0;
926 /** Callback for the variable "dialog-event"
927 * @param p_this Current object owner of the extension and the dialog
928 * @param psz_var "dialog-event"
929 * @param oldval Unused
930 * @param newval Address of the dialog
931 * @param p_data Unused
933 static int vlclua_extension_dialog_callback( vlc_object_t *p_this,
934 char const *psz_var,
935 vlc_value_t oldval,
936 vlc_value_t newval,
937 void *p_data )
939 /* psz_var == "dialog-event" */
940 ( void ) psz_var;
941 ( void ) oldval;
942 ( void ) p_data;
944 extension_dialog_command_t *command = newval.p_address;
945 assert( command != NULL );
946 assert( command->p_dlg != NULL);
948 extension_t *p_ext = command->p_dlg->p_sys;
949 assert( p_ext != NULL );
951 extension_widget_t *p_widget = command->p_data;
953 switch( command->event )
955 case EXTENSION_EVENT_CLICK:
956 assert( p_widget != NULL );
957 PushCommandUnique( p_ext, CMD_CLICK, p_widget );
958 break;
959 case EXTENSION_EVENT_CLOSE:
960 PushCommandUnique( p_ext, CMD_CLOSE );
961 break;
962 default:
963 msg_Dbg( p_this, "Received unknown UI event %d, discarded",
964 command->event );
965 break;
968 return VLC_SUCCESS;
971 /** Callback on vlc_InputItemMetaChanged event
973 static void inputItemMetaChanged( const vlc_event_t *p_event,
974 void *data )
976 assert( p_event && p_event->type == vlc_InputItemMetaChanged );
978 extension_t *p_ext = ( extension_t* ) data;
979 assert( p_ext != NULL );
981 PushCommandUnique( p_ext, CMD_UPDATE_META );
984 /* Lock this extension. Can fail. */
985 bool LockExtension( extension_t *p_ext )
987 if( p_ext->p_sys->b_exiting )
988 return false;
990 vlc_mutex_lock( &p_ext->p_sys->running_lock );
991 if( p_ext->p_sys->b_exiting )
993 vlc_mutex_unlock( &p_ext->p_sys->running_lock );
994 return false;
997 return true;
1000 void UnlockExtension( extension_t *p_ext )
1002 vlc_mutex_unlock( &p_ext->p_sys->running_lock );