]> git.sesse.net Git - vlc/blob - modules/misc/lua/extension.c
Extensions: export author, version, url and description
[vlc] / modules / misc / lua / extension.c
1 /*****************************************************************************
2  * extension.c: Lua Extensions (meta data, web information, ...)
3  *****************************************************************************
4  * Copyright (C) 2009-2010 VideoLAN and authors
5  * $Id$
6  *
7  * Authors: Jean-Philippe AndrĂ© < jpeg # videolan.org >
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 #include "vlc.h"
25 #include "libs.h"
26 #include "extension.h"
27 #include "assert.h"
28
29 /* Functions to register */
30 static const luaL_Reg p_reg[] =
31 {
32     { NULL, NULL }
33 };
34
35 /*
36  * Extensions capabilities
37  * Note: #define and ppsz_capabilities must be in sync
38  */
39 #define EXT_HAS_MENU          (1 << 0)
40 #define EXT_TRIGGER_ONLY      (1 << 1)
41
42 const char* const ppsz_capabilities[] = {
43     "menu",
44     "trigger",
45     NULL
46 };
47
48 static int ScanExtensions( extensions_manager_t *p_this );
49 static int ScanLuaCallback( vlc_object_t *p_this, const char *psz_script,
50                             lua_State *L, void *pb_continue );
51 static int Control( extensions_manager_t *, int, va_list );
52 static int GetMenuEntries( extensions_manager_t *p_mgr, extension_t *p_ext,
53                     char ***pppsz_titles, uint16_t **ppi_ids );
54 static lua_State* GetLuaState( extensions_manager_t *p_mgr,
55                                extension_t *p_ext );
56 static int TriggerMenu( extension_t *p_ext, int id );
57 static int TriggerExtension( extensions_manager_t *p_mgr,
58                              extension_t *p_ext );
59
60 int vlclua_extension_deactivate( lua_State *L );
61
62 /* Interactions */
63 static int vlclua_extension_dialog_callback( vlc_object_t *p_this,
64                                              char const *psz_var,
65                                              vlc_value_t oldval,
66                                              vlc_value_t newval,
67                                              void *p_data );
68
69
70 /**
71  * Module entry-point
72  **/
73 int Open_Extension( vlc_object_t *p_this )
74 {
75     msg_Dbg( p_this, "Opening EXPERIMENTAL Lua Extension module" );
76
77     extensions_manager_t *p_mgr = ( extensions_manager_t* ) p_this;
78
79     p_mgr->pf_control = Control;
80
81     extensions_manager_sys_t *p_sys = ( extensions_manager_sys_t* )
82                     calloc( 1, sizeof( extensions_manager_sys_t ) );
83     if( !p_sys ) return VLC_ENOMEM;
84
85     p_mgr->p_sys = p_sys;
86     ARRAY_INIT( p_sys->activated_extensions );
87     ARRAY_INIT( p_mgr->extensions );
88     vlc_mutex_init( &p_mgr->lock );
89     vlc_mutex_init( &p_mgr->p_sys->lock );
90
91     /* Initialise Lua state structure */
92     lua_State *L = GetLuaState( p_mgr, NULL );
93     if( !L )
94     {
95         free( p_sys );
96         return VLC_EGENERIC;
97     }
98     p_sys->L = L;
99
100     /* Scan available Lua Extensions */
101     if( ScanExtensions( p_mgr ) != VLC_SUCCESS )
102     {
103         msg_Err( p_mgr, "Can't load extensions modules" );
104         return VLC_EGENERIC;
105     }
106
107     lua_close( L );
108     p_sys->L = NULL;
109
110     // Create the dialog-event variable
111     var_Create( p_this, "dialog-event", VLC_VAR_ADDRESS );
112     var_AddCallback( p_this, "dialog-event",
113                      vlclua_extension_dialog_callback, NULL );
114
115     return VLC_SUCCESS;
116 }
117
118 /**
119  * Module unload function
120  **/
121 void Close_Extension( vlc_object_t *p_this )
122 {
123     extensions_manager_t *p_mgr = ( extensions_manager_t* ) p_this;
124     msg_Dbg( p_mgr, "Deactivating all loaded extensions" );
125
126     vlc_mutex_lock( &p_mgr->lock );
127     p_mgr->p_sys->b_killed = true;
128     vlc_mutex_unlock( &p_mgr->lock );
129
130     var_Destroy( p_mgr, "dialog-event" );
131
132     extension_t *p_ext = NULL;
133     FOREACH_ARRAY( p_ext, p_mgr->p_sys->activated_extensions )
134     {
135         if( !p_ext ) break;
136         Deactivate( p_mgr, p_ext );
137         WaitForDeactivation( p_ext );
138     }
139     FOREACH_END()
140
141     msg_Dbg( p_mgr, "All extensions are now deactivated" );
142     ARRAY_RESET( p_mgr->p_sys->activated_extensions );
143
144     if( p_mgr->p_sys && p_mgr->p_sys->L )
145         lua_close( p_mgr->p_sys->L );
146
147     vlc_mutex_destroy( &p_mgr->lock );
148     vlc_mutex_destroy( &p_mgr->p_sys->lock );
149     free( p_mgr->p_sys );
150     p_mgr->p_sys = NULL;
151
152     /* Free extensions' memory */
153     FOREACH_ARRAY( p_ext, p_mgr->extensions )
154     {
155         if( !p_ext )
156             break;
157         if( p_ext->p_sys->L )
158             lua_close( p_ext->p_sys->L );
159         free( p_ext->psz_name );
160         free( p_ext->psz_title );
161
162         vlc_mutex_destroy( &p_ext->p_sys->running_lock );
163         vlc_mutex_destroy( &p_ext->p_sys->command_lock );
164         vlc_cond_destroy( &p_ext->p_sys->wait );
165
166         free( p_ext->p_sys );
167         free( p_ext );
168     }
169     FOREACH_END()
170
171     ARRAY_RESET( p_mgr->extensions );
172 }
173
174 /**
175  * Batch scan all Lua files in folder "extensions"
176  * @param p_mgr This extensions_manager_t object
177  **/
178 static int ScanExtensions( extensions_manager_t *p_mgr )
179 {
180     bool b_true = true;
181     int i_ret =
182         vlclua_scripts_batch_execute( VLC_OBJECT( p_mgr ),
183                                       "extensions",
184                                       &ScanLuaCallback,
185                                       p_mgr->p_sys->L, &b_true );
186
187     if( !i_ret )
188         return VLC_EGENERIC;
189
190     return VLC_SUCCESS;
191 }
192
193 /**
194  * Batch scan all Lua files in folder "extensions": callback
195  * @param p_this This extensions_manager_t object
196  * @param psz_script Name of the script to run
197  * @param L Lua State, common to all scripts here
198  * @param pb_continue bool* that indicates whether to continue batch or not
199  **/
200 int ScanLuaCallback( vlc_object_t *p_this, const char *psz_script,
201                      lua_State *L, void *pb_continue )
202 {
203     extensions_manager_t *p_mgr = ( extensions_manager_t* ) p_this;
204     bool b_ok = false;
205
206     msg_Dbg( p_mgr, "Scanning Lua script %s", psz_script );
207
208     vlc_mutex_lock( &p_mgr->lock );
209
210     /* Create new script descriptor */
211     extension_t *p_ext = ( extension_t* ) calloc( 1, sizeof( extension_t ) );
212     if( !p_ext )
213     {
214         vlc_mutex_unlock( &p_mgr->lock );
215         return 0;
216     }
217
218     p_ext->psz_name = strdup( psz_script );
219     p_ext->p_sys = (extension_sys_t*) calloc( 1, sizeof( extension_sys_t ) );
220     if( !p_ext->p_sys || !p_ext->psz_name )
221     {
222         free( p_ext->psz_name );
223         free( p_ext->p_sys );
224         free( p_ext );
225         vlc_mutex_unlock( &p_mgr->lock );
226         return 0;
227     }
228     p_ext->p_sys->p_mgr = p_mgr;
229
230     /* Mutexes and conditions */
231     vlc_mutex_init( &p_ext->p_sys->command_lock );
232     vlc_mutex_init( &p_ext->p_sys->running_lock );
233     vlc_cond_init( &p_ext->p_sys->wait );
234
235     /* Load and run the script(s) */
236     if( luaL_dofile( L, psz_script ) )
237     {
238         msg_Warn( p_mgr, "Error loading script %s: %s", psz_script,
239                   lua_tostring( L, lua_gettop( L ) ) );
240         lua_pop( L, 1 );
241         goto exit;
242     }
243
244     /* Scan script for capabilities */
245     lua_getglobal( L, "descriptor" );
246
247     if( !lua_isfunction( L, -1 ) )
248     {
249         msg_Warn( p_mgr, "Error while runing script %s, "
250                   "function descriptor() not found", psz_script );
251         goto exit;
252     }
253
254     if( lua_pcall( L, 0, 1, 0 ) )
255     {
256         msg_Warn( p_mgr, "Error while runing script %s, "
257                   "function descriptor(): %s", psz_script,
258                   lua_tostring( L, lua_gettop( L ) ) );
259         goto exit;
260     }
261
262     if( lua_gettop( L ) )
263     {
264         if( lua_istable( L, -1 ) )
265         {
266             /* Get caps */
267             lua_getfield( L, -1, "capabilities" );
268             if( lua_istable( L, -1 ) )
269             {
270                 lua_pushnil( L );
271                 while( lua_next( L, -2 ) != 0 )
272                 {
273                     /* Key is at index -2 and value at index -1. Discard key */
274                     const char *psz_cap = luaL_checkstring( L, -1 );
275                     int i_cap = 0;
276                     bool b_ok = false;
277                     /* Find this capability's flag */
278                     for( const char *iter = *ppsz_capabilities;
279                          iter != NULL;
280                          iter = ppsz_capabilities[ ++i_cap ])
281                     {
282                         if( !strcmp( iter, psz_cap ) )
283                         {
284                             /* Flag it! */
285                             p_ext->p_sys->i_capabilities |= 1 << i_cap;
286                             b_ok = true;
287                             break;
288                         }
289                     }
290                     if( !b_ok )
291                     {
292                         msg_Warn( p_mgr, "Extension capability '%s' unknown in"
293                                   " script %s", psz_cap, psz_script );
294                     }
295                     /* Removes 'value'; keeps 'key' for next iteration */
296                     lua_pop( L, 1 );
297                 }
298             }
299             else
300             {
301                 msg_Warn( p_mgr, "In script %s, function descriptor() "
302                               "did not return a table of capabilities.",
303                               psz_script );
304             }
305             lua_pop( L, 1 );
306
307             /* Get title */
308             lua_getfield( L, -1, "title" );
309             if( lua_isstring( L, -1 ) )
310             {
311                 p_ext->psz_title = strdup( luaL_checkstring( L, -1 ) );
312             }
313             else
314             {
315                 msg_Dbg( p_mgr, "In script %s, function descriptor() "
316                                 "did not return a string as title.",
317                                 psz_script );
318                 p_ext->psz_title = strdup( psz_script );
319             }
320             lua_pop( L, 1 );
321
322             /* Get author */
323             lua_getfield( L, -1, "author" );
324             if( lua_isstring( L, -1 ) )
325             {
326                 p_ext->psz_author = strdup( luaL_checkstring( L, -1 ) );
327             }
328             else
329             {
330                 p_ext->psz_author = NULL;
331             }
332             lua_pop( L, 1 );
333
334             /* Get description */
335             lua_getfield( L, -1, "description" );
336             if( lua_isstring( L, -1 ) )
337             {
338                 p_ext->psz_description = strdup( luaL_checkstring( L, -1 ) );
339             }
340             else
341             {
342                 p_ext->psz_description = NULL;
343             }
344             lua_pop( L, 1 );
345
346             /* Get URL */
347             lua_getfield( L, -1, "url" );
348             if( lua_isstring( L, -1 ) )
349             {
350                 p_ext->psz_url = strdup( luaL_checkstring( L, -1 ) );
351             }
352             else
353             {
354                 p_ext->psz_url = NULL;
355             }
356             lua_pop( L, 1 );
357
358             /* Get version */
359             lua_getfield( L, -1, "version" );
360             if( lua_isstring( L, -1 ) )
361             {
362                 p_ext->psz_version = strdup( luaL_checkstring( L, -1 ) );
363             }
364             else
365             {
366                 p_ext->psz_version = NULL;
367             }
368             lua_pop( L, 1 );
369         }
370         else
371         {
372             msg_Warn( p_mgr, "In script %s, function descriptor() "
373                       "did not return a table!", psz_script );
374             goto exit;
375         }
376     }
377     else
378     {
379         msg_Err( p_mgr, "Script %s went completely foobar", psz_script );
380         goto exit;
381     }
382
383     msg_Dbg( p_mgr, "Script %s has the following capability flags: 0x%x",
384              psz_script, p_ext->p_sys->i_capabilities );
385
386     b_ok = true;
387 exit:
388     if( !b_ok )
389     {
390         free( p_ext->psz_name );
391         free( p_ext->psz_title );
392         vlc_mutex_destroy( &p_ext->p_sys->command_lock );
393         vlc_mutex_destroy( &p_ext->p_sys->running_lock );
394         vlc_cond_destroy( &p_ext->p_sys->wait );
395         free( p_ext->p_sys );
396         free( p_ext );
397     }
398     else
399     {
400         /* Add the extension to the list of known extensions */
401         ARRAY_APPEND( p_mgr->extensions, p_ext );
402     }
403
404     vlc_mutex_unlock( &p_mgr->lock );
405     /* Continue batch execution */
406     return pb_continue ? ( (* (bool*)pb_continue) ? -1 : 0 ) : -1;
407 }
408
409 static int Control( extensions_manager_t *p_mgr, int i_control, va_list args )
410 {
411     extension_t *p_ext = NULL;
412     bool *pb = NULL;
413     uint16_t **ppus = NULL;
414     char ***pppsz = NULL;
415     int i = 0;
416
417     switch( i_control )
418     {
419         case EXTENSION_ACTIVATE:
420             p_ext = ( extension_t* ) va_arg( args, extension_t* );
421             return Activate( p_mgr, p_ext );
422
423         case EXTENSION_DEACTIVATE:
424             p_ext = ( extension_t* ) va_arg( args, extension_t* );
425             return Deactivate( p_mgr, p_ext );
426
427         case EXTENSION_IS_ACTIVATED:
428             p_ext = ( extension_t* ) va_arg( args, extension_t* );
429             pb = ( bool* ) va_arg( args, bool* );
430             *pb = IsActivated( p_mgr, p_ext );
431             break;
432
433         case EXTENSION_HAS_MENU:
434             p_ext = ( extension_t* ) va_arg( args, extension_t* );
435             pb = ( bool* ) va_arg( args, bool* );
436             *pb = ( p_ext->p_sys->i_capabilities & EXT_HAS_MENU ) ? 1 : 0;
437             break;
438
439         case EXTENSION_GET_MENU:
440             p_ext = ( extension_t* ) va_arg( args, extension_t* );
441             pppsz = ( char*** ) va_arg( args, char*** );
442             ppus = ( uint16_t** ) va_arg( args, uint16_t** );
443             return GetMenuEntries( p_mgr, p_ext, pppsz, ppus );
444
445         case EXTENSION_TRIGGER_ONLY:
446             p_ext = ( extension_t* ) va_arg( args, extension_t* );
447             pb = ( bool* ) va_arg( args, bool* );
448             *pb = ( p_ext->p_sys->i_capabilities & EXT_TRIGGER_ONLY ) ? 1 : 0;
449             break;
450
451         case EXTENSION_TRIGGER:
452             p_ext = ( extension_t* ) va_arg( args, extension_t* );
453             return TriggerExtension( p_mgr, p_ext );
454
455         case EXTENSION_TRIGGER_MENU:
456             p_ext = ( extension_t* ) va_arg( args, extension_t* );
457             // GCC: 'uint16_t' is promoted to 'int' when passed through '...'
458             i = ( int ) va_arg( args, int );
459             return TriggerMenu( p_ext, i );
460
461         default:
462             msg_Err( p_mgr, "Control '%d' not yet implemented in Extension",
463                      i_control );
464             return VLC_EGENERIC;
465     }
466
467     return VLC_SUCCESS;
468 }
469
470 int lua_ExtensionActivate( extensions_manager_t *p_mgr, extension_t *p_ext )
471 {
472     assert( p_mgr != NULL && p_ext != NULL );
473     return lua_ExecuteFunction( p_mgr, p_ext, "activate" );
474 }
475
476 int lua_ExtensionDeactivate( extensions_manager_t *p_mgr, extension_t *p_ext )
477 {
478     assert( p_mgr != NULL && p_ext != NULL );
479
480     if( !p_ext->p_sys->L )
481         return VLC_SUCCESS;
482
483     int i_ret = lua_ExecuteFunction( p_mgr, p_ext, "deactivate" );
484
485     /* Clear Lua State */
486     lua_close( p_ext->p_sys->L );
487     p_ext->p_sys->L = NULL;
488
489     return i_ret;
490 }
491
492 int lua_ExtensionWidgetClick( extensions_manager_t *p_mgr,
493                               extension_t *p_ext,
494                               extension_widget_t *p_widget )
495 {
496     if( !p_ext->p_sys->L )
497         return VLC_SUCCESS;
498
499     return lua_ExecuteFunction( p_mgr, p_ext, (const char*) p_widget->p_sys );
500 }
501
502
503 /**
504  * Get the list of menu entries from an extension script
505  * @param p_mgr
506  * @param p_ext
507  * @param pppsz_titles Pointer to NULL. All strings must be freed by the caller
508  * @param ppi_ids Pointer to NULL. Must be feed by the caller.
509  * @note This function is allowed to run in the UI thread. This means
510  *       that it MUST respond very fast.
511  * @todo Remove the menu() hook and provide a new function vlc.set_menu()
512  **/
513 static int GetMenuEntries( extensions_manager_t *p_mgr, extension_t *p_ext,
514                     char ***pppsz_titles, uint16_t **ppi_ids )
515 {
516     assert( *pppsz_titles == NULL );
517     assert( *ppi_ids == NULL );
518
519     if( !IsActivated( p_mgr, p_ext ) )
520     {
521         msg_Dbg( p_mgr, "Can't get menu before activating the extension!" );
522         return VLC_EGENERIC;
523     }
524
525     if( !LockExtension( p_ext ) )
526     {
527         /* Dying extension, fail. */
528         return VLC_EGENERIC;
529     }
530
531     int i_ret = VLC_EGENERIC;
532     lua_State *L = GetLuaState( p_mgr, p_ext );
533
534     if( ( p_ext->p_sys->i_capabilities & EXT_HAS_MENU ) == 0 )
535     {
536         msg_Dbg( p_mgr, "can't get a menu from an extension without menu!" );
537         goto exit;
538     }
539
540     lua_getglobal( L, "menu" );
541
542     if( !lua_isfunction( L, -1 ) )
543     {
544         msg_Warn( p_mgr, "Error while runing script %s, "
545                   "function menu() not found", p_ext->psz_name );
546         goto exit;
547     }
548
549     if( lua_pcall( L, 0, 1, 0 ) )
550     {
551         msg_Warn( p_mgr, "Error while runing script %s, "
552                   "function menu(): %s", p_ext->psz_name,
553                   lua_tostring( L, lua_gettop( L ) ) );
554         goto exit;
555     }
556
557     if( lua_gettop( L ) )
558     {
559         if( lua_istable( L, -1 ) )
560         {
561             /* Get table size */
562             size_t i_size = lua_objlen( L, -1 );
563             *pppsz_titles = ( char** ) calloc( i_size+1, sizeof( char* ) );
564             *ppi_ids = ( uint16_t* ) calloc( i_size+1, sizeof( uint16_t ) );
565
566             /* Walk table */
567             size_t i_idx = 0;
568             lua_pushnil( L );
569             while( lua_next( L, -2 ) != 0 )
570             {
571                 assert( i_idx < i_size );
572                 if( (!lua_isstring( L, -1 )) || (!lua_isnumber( L, -2 )) )
573                 {
574                     msg_Warn( p_mgr, "In script %s, an entry in "
575                               "the menu table is invalid!", p_ext->psz_name );
576                     goto exit;
577                 }
578                 (*pppsz_titles)[ i_idx ] = strdup( luaL_checkstring( L, -1 ) );
579                 (*ppi_ids)[ i_idx ] = (uint16_t) ( luaL_checkinteger( L, -2 ) & 0xFFFF );
580                 i_idx++;
581                 lua_pop( L, 1 );
582             }
583         }
584         else
585         {
586             msg_Warn( p_mgr, "Function menu() in script %s "
587                       "did not return a table", p_ext->psz_name );
588             goto exit;
589         }
590     }
591     else
592     {
593         msg_Warn( p_mgr, "Script %s went completely foobar", p_ext->psz_name );
594         goto exit;
595     }
596
597     i_ret = VLC_SUCCESS;
598
599 exit:
600     UnlockExtension( p_ext );
601     if( i_ret != VLC_SUCCESS )
602     {
603         msg_Dbg( p_mgr, "Something went wrong in %s (%s:%d)",
604                  __func__, __FILE__, __LINE__ );
605     }
606     return i_ret;
607 }
608
609 /* Must be entered with the Lock on Extension */
610 static lua_State* GetLuaState( extensions_manager_t *p_mgr,
611                                extension_t *p_ext )
612 {
613     lua_State *L = NULL;
614     if( p_ext )
615         L = p_ext->p_sys->L;
616
617     if( !L )
618     {
619         L = luaL_newstate();
620         if( !L )
621         {
622             msg_Err( p_mgr, "Could not create new Lua State" );
623             return NULL;
624         }
625         luaL_openlibs( L );
626         luaL_register( L, "vlc", p_reg );
627         luaopen_msg( L );
628
629         lua_pushlightuserdata( L, p_mgr );
630         lua_setfield( L, -2, "private" );
631
632         lua_pushlightuserdata( L, p_ext );
633         lua_setfield( L, -2, "extension" );
634
635         if( p_ext )
636         {
637             /* Load more libraries */
638             luaopen_acl( L );
639             luaopen_config( L );
640             luaopen_dialog( L, p_ext );
641             luaopen_input( L );
642             luaopen_msg( L );
643             luaopen_misc( L );
644             luaopen_net( L );
645             luaopen_object( L );
646             luaopen_osd( L );
647             luaopen_playlist( L );
648             luaopen_sd( L );
649             luaopen_stream( L );
650             luaopen_strings( L );
651             luaopen_variables( L );
652             luaopen_video( L );
653             luaopen_vlm( L );
654             luaopen_volume( L );
655
656             /* Register extension specific functions */
657             lua_getglobal( L, "vlc" );
658             lua_pushcfunction( L, vlclua_extension_deactivate );
659             lua_setfield( L, -2, "deactivate" );
660
661             /* Load and run the script(s) */
662             if( luaL_dofile( L, p_ext->psz_name ) != 0 )
663             {
664                 msg_Warn( p_mgr, "Error loading script %s: %s", p_ext->psz_name,
665                           lua_tostring( L, lua_gettop( L ) ) );
666                 lua_pop( L, 1 );
667                 return NULL;
668             }
669
670             p_ext->p_sys->L = L;
671         }
672     }
673 #ifndef NDEBUG
674     else
675     {
676         msg_Dbg( p_mgr, "Reusing old Lua state for extension '%s'",
677                  p_ext->psz_name );
678     }
679 #endif
680
681     return L;
682 }
683
684 /**
685  * Execute a function in a Lua script
686  * @return < 0 in case of failure, >= 0 in case of success
687  * @note It's better to call this function from a dedicated thread
688  * (see extension_thread.c)
689  **/
690 int lua_ExecuteFunction( extensions_manager_t *p_mgr,
691                          extension_t *p_ext,
692                          const char *psz_function )
693 {
694     int i_ret = VLC_EGENERIC;
695     assert( p_mgr != NULL );
696     assert( p_ext != NULL );
697
698     lua_State *L = GetLuaState( p_mgr, p_ext );
699     lua_getglobal( L, psz_function );
700
701     if( !lua_isfunction( L, -1 ) )
702     {
703         msg_Warn( p_mgr, "Error while runing script %s, "
704                   "function %s() not found", p_ext->psz_name, psz_function );
705         goto exit;
706     }
707
708     if( lua_pcall( L, 0, 1, 0 ) )
709     {
710         msg_Warn( p_mgr, "Error while runing script %s, "
711                   "function %s(): %s", p_ext->psz_name, psz_function,
712                   lua_tostring( L, lua_gettop( L ) ) );
713         goto exit;
714     }
715
716     i_ret = VLC_SUCCESS;
717 exit:
718     return i_ret;
719 }
720
721 static inline int TriggerMenu( extension_t *p_ext, int i_id )
722 {
723     return PushCommand( p_ext, CMD_TRIGGERMENU, i_id );
724 }
725
726 int lua_ExtensionTriggerMenu( extensions_manager_t *p_mgr,
727                               extension_t *p_ext, int id )
728 {
729     int i_ret = VLC_EGENERIC;
730     lua_State *L = GetLuaState( p_mgr, p_ext );
731
732     if( !L )
733         return VLC_EGENERIC;
734
735     luaopen_dialog( L, p_ext );
736
737     lua_getglobal( L, "trigger_menu" );
738     if( !lua_isfunction( L, -1 ) )
739     {
740         msg_Warn( p_mgr, "Error while runing script %s, "
741                   "function trigger_menu() not found", p_ext->psz_name );
742         return VLC_EGENERIC;
743     }
744
745     /* Pass id as unique argument to the function */
746     lua_pushinteger( L, id );
747
748     if( lua_pcall( L, 1, 1, 0 ) != 0 )
749     {
750         msg_Warn( p_mgr, "Error while runing script %s, "
751                   "function trigger_menu(): %s", p_ext->psz_name,
752                   lua_tostring( L, lua_gettop( L ) ) );
753         return VLC_EGENERIC;
754     }
755
756     if( i_ret < VLC_SUCCESS )
757     {
758         msg_Dbg( p_mgr, "Something went wrong in %s (%s:%d)",
759                  __func__, __FILE__, __LINE__ );
760     }
761     return i_ret;
762 }
763
764 /** Directly trigger an extension, without activating it
765  * This is NOT multithreaded, and this code runs in the UI thread
766  * @param p_mgr
767  * @param p_ext Extension to trigger
768  * @return Value returned by the lua function "trigger"
769  **/
770 static int TriggerExtension( extensions_manager_t *p_mgr,
771                              extension_t *p_ext )
772 {
773     int i_ret = lua_ExecuteFunction( p_mgr, p_ext, "trigger" );
774
775     /* Close lua state for trigger-only extensions */
776     if( p_ext->p_sys->L )
777         lua_close( p_ext->p_sys->L );
778     p_ext->p_sys->L = NULL;
779
780     return i_ret;
781 }
782
783 /** Retrieve extension associated to the current script
784  * @param L current lua_State
785  * @return Lua userdata "vlc.extension"
786  **/
787 extension_t *vlclua_extension_get( lua_State *L )
788 {
789     extension_t *p_ext = NULL;
790     lua_getglobal( L, "vlc" );
791     lua_getfield( L, -1, "extension" );
792     p_ext = (extension_t*) lua_topointer( L, lua_gettop( L ) );
793     lua_pop( L, 2 );
794     return p_ext;
795 }
796
797 /** Deactivate an extension by order from the extension itself
798  * @param L lua_State
799  * @note This is an asynchronous call. A script calling vlc.deactivate() will
800  * be executed to the end before the last call to deactivate() is done.
801  **/
802 int vlclua_extension_deactivate( lua_State *L )
803 {
804     extension_t *p_ext = vlclua_extension_get( L );
805     int i_ret = Deactivate( p_ext->p_sys->p_mgr, p_ext );
806     return ( i_ret == VLC_SUCCESS ) ? 1 : 0;
807 }
808
809 /** Callback for the variable "dialog-event"
810  * @param p_this Current object owner of the extension and the dialog
811  * @param psz_var "dialog-event"
812  * @param oldval Unused
813  * @param newval Address of the dialog
814  * @param p_data Unused
815  **/
816 static int vlclua_extension_dialog_callback( vlc_object_t *p_this,
817                                              char const *psz_var,
818                                              vlc_value_t oldval,
819                                              vlc_value_t newval,
820                                              void *p_data )
821 {
822     /* psz_var == "dialog-event" */
823     ( void ) psz_var;
824     ( void ) oldval;
825     ( void ) p_data;
826
827     extension_dialog_command_t *command = newval.p_address;
828     assert( command != NULL );
829     assert( command->p_dlg != NULL);
830
831     extension_t *p_ext = command->p_dlg->p_sys;
832     assert( p_ext != NULL );
833
834     extension_widget_t *p_widget = command->p_data;
835
836     switch( command->event )
837     {
838         case EXTENSION_EVENT_CLICK:
839             assert( p_widget != NULL );
840             PushCommand( p_ext, CMD_CLICK, p_widget );
841             break;
842         case EXTENSION_EVENT_CLOSE:
843             PushCommand( p_ext, CMD_CLOSE );
844             break;
845         default:
846             msg_Dbg( p_this, "Received unknown UI event %d, discarded",
847                      command->event );
848             break;
849     }
850
851     return VLC_SUCCESS;
852 }
853
854 /* Lock this extension. Can fail. */
855 bool LockExtension( extension_t *p_ext )
856 {
857     if( p_ext->p_sys->b_exiting )
858         return false;
859
860     vlc_mutex_lock( &p_ext->p_sys->running_lock );
861     if( p_ext->p_sys->b_exiting )
862     {
863         vlc_mutex_unlock( &p_ext->p_sys->running_lock );
864         return false;
865     }
866
867     return true;
868 }
869
870 void UnlockExtension( extension_t *p_ext )
871 {
872     vlc_mutex_unlock( &p_ext->p_sys->running_lock );
873 }