]> git.sesse.net Git - vlc/blob - modules/misc/lua/extension.c
Extensions/Lua: free extension strings
[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         free( p_ext->psz_url );
393         free( p_ext->psz_author );
394         free( p_ext->psz_description );
395         free( p_ext->psz_version );
396         vlc_mutex_destroy( &p_ext->p_sys->command_lock );
397         vlc_mutex_destroy( &p_ext->p_sys->running_lock );
398         vlc_cond_destroy( &p_ext->p_sys->wait );
399         free( p_ext->p_sys );
400         free( p_ext );
401     }
402     else
403     {
404         /* Add the extension to the list of known extensions */
405         ARRAY_APPEND( p_mgr->extensions, p_ext );
406     }
407
408     vlc_mutex_unlock( &p_mgr->lock );
409     /* Continue batch execution */
410     return pb_continue ? ( (* (bool*)pb_continue) ? -1 : 0 ) : -1;
411 }
412
413 static int Control( extensions_manager_t *p_mgr, int i_control, va_list args )
414 {
415     extension_t *p_ext = NULL;
416     bool *pb = NULL;
417     uint16_t **ppus = NULL;
418     char ***pppsz = NULL;
419     int i = 0;
420
421     switch( i_control )
422     {
423         case EXTENSION_ACTIVATE:
424             p_ext = ( extension_t* ) va_arg( args, extension_t* );
425             return Activate( p_mgr, p_ext );
426
427         case EXTENSION_DEACTIVATE:
428             p_ext = ( extension_t* ) va_arg( args, extension_t* );
429             return Deactivate( p_mgr, p_ext );
430
431         case EXTENSION_IS_ACTIVATED:
432             p_ext = ( extension_t* ) va_arg( args, extension_t* );
433             pb = ( bool* ) va_arg( args, bool* );
434             *pb = IsActivated( p_mgr, p_ext );
435             break;
436
437         case EXTENSION_HAS_MENU:
438             p_ext = ( extension_t* ) va_arg( args, extension_t* );
439             pb = ( bool* ) va_arg( args, bool* );
440             *pb = ( p_ext->p_sys->i_capabilities & EXT_HAS_MENU ) ? 1 : 0;
441             break;
442
443         case EXTENSION_GET_MENU:
444             p_ext = ( extension_t* ) va_arg( args, extension_t* );
445             pppsz = ( char*** ) va_arg( args, char*** );
446             ppus = ( uint16_t** ) va_arg( args, uint16_t** );
447             return GetMenuEntries( p_mgr, p_ext, pppsz, ppus );
448
449         case EXTENSION_TRIGGER_ONLY:
450             p_ext = ( extension_t* ) va_arg( args, extension_t* );
451             pb = ( bool* ) va_arg( args, bool* );
452             *pb = ( p_ext->p_sys->i_capabilities & EXT_TRIGGER_ONLY ) ? 1 : 0;
453             break;
454
455         case EXTENSION_TRIGGER:
456             p_ext = ( extension_t* ) va_arg( args, extension_t* );
457             return TriggerExtension( p_mgr, p_ext );
458
459         case EXTENSION_TRIGGER_MENU:
460             p_ext = ( extension_t* ) va_arg( args, extension_t* );
461             // GCC: 'uint16_t' is promoted to 'int' when passed through '...'
462             i = ( int ) va_arg( args, int );
463             return TriggerMenu( p_ext, i );
464
465         default:
466             msg_Err( p_mgr, "Control '%d' not yet implemented in Extension",
467                      i_control );
468             return VLC_EGENERIC;
469     }
470
471     return VLC_SUCCESS;
472 }
473
474 int lua_ExtensionActivate( extensions_manager_t *p_mgr, extension_t *p_ext )
475 {
476     assert( p_mgr != NULL && p_ext != NULL );
477     return lua_ExecuteFunction( p_mgr, p_ext, "activate" );
478 }
479
480 int lua_ExtensionDeactivate( extensions_manager_t *p_mgr, extension_t *p_ext )
481 {
482     assert( p_mgr != NULL && p_ext != NULL );
483
484     if( !p_ext->p_sys->L )
485         return VLC_SUCCESS;
486
487     int i_ret = lua_ExecuteFunction( p_mgr, p_ext, "deactivate" );
488
489     /* Clear Lua State */
490     lua_close( p_ext->p_sys->L );
491     p_ext->p_sys->L = NULL;
492
493     return i_ret;
494 }
495
496 int lua_ExtensionWidgetClick( extensions_manager_t *p_mgr,
497                               extension_t *p_ext,
498                               extension_widget_t *p_widget )
499 {
500     if( !p_ext->p_sys->L )
501         return VLC_SUCCESS;
502
503     return lua_ExecuteFunction( p_mgr, p_ext, (const char*) p_widget->p_sys );
504 }
505
506
507 /**
508  * Get the list of menu entries from an extension script
509  * @param p_mgr
510  * @param p_ext
511  * @param pppsz_titles Pointer to NULL. All strings must be freed by the caller
512  * @param ppi_ids Pointer to NULL. Must be feed by the caller.
513  * @note This function is allowed to run in the UI thread. This means
514  *       that it MUST respond very fast.
515  * @todo Remove the menu() hook and provide a new function vlc.set_menu()
516  **/
517 static int GetMenuEntries( extensions_manager_t *p_mgr, extension_t *p_ext,
518                     char ***pppsz_titles, uint16_t **ppi_ids )
519 {
520     assert( *pppsz_titles == NULL );
521     assert( *ppi_ids == NULL );
522
523     if( !IsActivated( p_mgr, p_ext ) )
524     {
525         msg_Dbg( p_mgr, "Can't get menu before activating the extension!" );
526         return VLC_EGENERIC;
527     }
528
529     if( !LockExtension( p_ext ) )
530     {
531         /* Dying extension, fail. */
532         return VLC_EGENERIC;
533     }
534
535     int i_ret = VLC_EGENERIC;
536     lua_State *L = GetLuaState( p_mgr, p_ext );
537
538     if( ( p_ext->p_sys->i_capabilities & EXT_HAS_MENU ) == 0 )
539     {
540         msg_Dbg( p_mgr, "can't get a menu from an extension without menu!" );
541         goto exit;
542     }
543
544     lua_getglobal( L, "menu" );
545
546     if( !lua_isfunction( L, -1 ) )
547     {
548         msg_Warn( p_mgr, "Error while runing script %s, "
549                   "function menu() not found", p_ext->psz_name );
550         goto exit;
551     }
552
553     if( lua_pcall( L, 0, 1, 0 ) )
554     {
555         msg_Warn( p_mgr, "Error while runing script %s, "
556                   "function menu(): %s", p_ext->psz_name,
557                   lua_tostring( L, lua_gettop( L ) ) );
558         goto exit;
559     }
560
561     if( lua_gettop( L ) )
562     {
563         if( lua_istable( L, -1 ) )
564         {
565             /* Get table size */
566             size_t i_size = lua_objlen( L, -1 );
567             *pppsz_titles = ( char** ) calloc( i_size+1, sizeof( char* ) );
568             *ppi_ids = ( uint16_t* ) calloc( i_size+1, sizeof( uint16_t ) );
569
570             /* Walk table */
571             size_t i_idx = 0;
572             lua_pushnil( L );
573             while( lua_next( L, -2 ) != 0 )
574             {
575                 assert( i_idx < i_size );
576                 if( (!lua_isstring( L, -1 )) || (!lua_isnumber( L, -2 )) )
577                 {
578                     msg_Warn( p_mgr, "In script %s, an entry in "
579                               "the menu table is invalid!", p_ext->psz_name );
580                     goto exit;
581                 }
582                 (*pppsz_titles)[ i_idx ] = strdup( luaL_checkstring( L, -1 ) );
583                 (*ppi_ids)[ i_idx ] = (uint16_t) ( luaL_checkinteger( L, -2 ) & 0xFFFF );
584                 i_idx++;
585                 lua_pop( L, 1 );
586             }
587         }
588         else
589         {
590             msg_Warn( p_mgr, "Function menu() in script %s "
591                       "did not return a table", p_ext->psz_name );
592             goto exit;
593         }
594     }
595     else
596     {
597         msg_Warn( p_mgr, "Script %s went completely foobar", p_ext->psz_name );
598         goto exit;
599     }
600
601     i_ret = VLC_SUCCESS;
602
603 exit:
604     UnlockExtension( p_ext );
605     if( i_ret != VLC_SUCCESS )
606     {
607         msg_Dbg( p_mgr, "Something went wrong in %s (%s:%d)",
608                  __func__, __FILE__, __LINE__ );
609     }
610     return i_ret;
611 }
612
613 /* Must be entered with the Lock on Extension */
614 static lua_State* GetLuaState( extensions_manager_t *p_mgr,
615                                extension_t *p_ext )
616 {
617     lua_State *L = NULL;
618     if( p_ext )
619         L = p_ext->p_sys->L;
620
621     if( !L )
622     {
623         L = luaL_newstate();
624         if( !L )
625         {
626             msg_Err( p_mgr, "Could not create new Lua State" );
627             return NULL;
628         }
629         luaL_openlibs( L );
630         luaL_register( L, "vlc", p_reg );
631         luaopen_msg( L );
632
633         lua_pushlightuserdata( L, p_mgr );
634         lua_setfield( L, -2, "private" );
635
636         lua_pushlightuserdata( L, p_ext );
637         lua_setfield( L, -2, "extension" );
638
639         if( p_ext )
640         {
641             /* Load more libraries */
642             luaopen_acl( L );
643             luaopen_config( L );
644             luaopen_dialog( L, p_ext );
645             luaopen_input( L );
646             luaopen_msg( L );
647             luaopen_misc( L );
648             luaopen_net( L );
649             luaopen_object( L );
650             luaopen_osd( L );
651             luaopen_playlist( L );
652             luaopen_sd( L );
653             luaopen_stream( L );
654             luaopen_strings( L );
655             luaopen_variables( L );
656             luaopen_video( L );
657             luaopen_vlm( L );
658             luaopen_volume( L );
659
660             /* Register extension specific functions */
661             lua_getglobal( L, "vlc" );
662             lua_pushcfunction( L, vlclua_extension_deactivate );
663             lua_setfield( L, -2, "deactivate" );
664
665             /* Load and run the script(s) */
666             if( luaL_dofile( L, p_ext->psz_name ) != 0 )
667             {
668                 msg_Warn( p_mgr, "Error loading script %s: %s", p_ext->psz_name,
669                           lua_tostring( L, lua_gettop( L ) ) );
670                 lua_pop( L, 1 );
671                 return NULL;
672             }
673
674             p_ext->p_sys->L = L;
675         }
676     }
677 #ifndef NDEBUG
678     else
679     {
680         msg_Dbg( p_mgr, "Reusing old Lua state for extension '%s'",
681                  p_ext->psz_name );
682     }
683 #endif
684
685     return L;
686 }
687
688 /**
689  * Execute a function in a Lua script
690  * @return < 0 in case of failure, >= 0 in case of success
691  * @note It's better to call this function from a dedicated thread
692  * (see extension_thread.c)
693  **/
694 int lua_ExecuteFunction( extensions_manager_t *p_mgr,
695                          extension_t *p_ext,
696                          const char *psz_function )
697 {
698     int i_ret = VLC_EGENERIC;
699     assert( p_mgr != NULL );
700     assert( p_ext != NULL );
701
702     lua_State *L = GetLuaState( p_mgr, p_ext );
703     lua_getglobal( L, psz_function );
704
705     if( !lua_isfunction( L, -1 ) )
706     {
707         msg_Warn( p_mgr, "Error while runing script %s, "
708                   "function %s() not found", p_ext->psz_name, psz_function );
709         goto exit;
710     }
711
712     if( lua_pcall( L, 0, 1, 0 ) )
713     {
714         msg_Warn( p_mgr, "Error while runing script %s, "
715                   "function %s(): %s", p_ext->psz_name, psz_function,
716                   lua_tostring( L, lua_gettop( L ) ) );
717         goto exit;
718     }
719
720     i_ret = VLC_SUCCESS;
721 exit:
722     return i_ret;
723 }
724
725 static inline int TriggerMenu( extension_t *p_ext, int i_id )
726 {
727     return PushCommand( p_ext, CMD_TRIGGERMENU, i_id );
728 }
729
730 int lua_ExtensionTriggerMenu( extensions_manager_t *p_mgr,
731                               extension_t *p_ext, int id )
732 {
733     int i_ret = VLC_EGENERIC;
734     lua_State *L = GetLuaState( p_mgr, p_ext );
735
736     if( !L )
737         return VLC_EGENERIC;
738
739     luaopen_dialog( L, p_ext );
740
741     lua_getglobal( L, "trigger_menu" );
742     if( !lua_isfunction( L, -1 ) )
743     {
744         msg_Warn( p_mgr, "Error while runing script %s, "
745                   "function trigger_menu() not found", p_ext->psz_name );
746         return VLC_EGENERIC;
747     }
748
749     /* Pass id as unique argument to the function */
750     lua_pushinteger( L, id );
751
752     if( lua_pcall( L, 1, 1, 0 ) != 0 )
753     {
754         msg_Warn( p_mgr, "Error while runing script %s, "
755                   "function trigger_menu(): %s", p_ext->psz_name,
756                   lua_tostring( L, lua_gettop( L ) ) );
757         return VLC_EGENERIC;
758     }
759
760     if( i_ret < VLC_SUCCESS )
761     {
762         msg_Dbg( p_mgr, "Something went wrong in %s (%s:%d)",
763                  __func__, __FILE__, __LINE__ );
764     }
765     return i_ret;
766 }
767
768 /** Directly trigger an extension, without activating it
769  * This is NOT multithreaded, and this code runs in the UI thread
770  * @param p_mgr
771  * @param p_ext Extension to trigger
772  * @return Value returned by the lua function "trigger"
773  **/
774 static int TriggerExtension( extensions_manager_t *p_mgr,
775                              extension_t *p_ext )
776 {
777     int i_ret = lua_ExecuteFunction( p_mgr, p_ext, "trigger" );
778
779     /* Close lua state for trigger-only extensions */
780     if( p_ext->p_sys->L )
781         lua_close( p_ext->p_sys->L );
782     p_ext->p_sys->L = NULL;
783
784     return i_ret;
785 }
786
787 /** Retrieve extension associated to the current script
788  * @param L current lua_State
789  * @return Lua userdata "vlc.extension"
790  **/
791 extension_t *vlclua_extension_get( lua_State *L )
792 {
793     extension_t *p_ext = NULL;
794     lua_getglobal( L, "vlc" );
795     lua_getfield( L, -1, "extension" );
796     p_ext = (extension_t*) lua_topointer( L, lua_gettop( L ) );
797     lua_pop( L, 2 );
798     return p_ext;
799 }
800
801 /** Deactivate an extension by order from the extension itself
802  * @param L lua_State
803  * @note This is an asynchronous call. A script calling vlc.deactivate() will
804  * be executed to the end before the last call to deactivate() is done.
805  **/
806 int vlclua_extension_deactivate( lua_State *L )
807 {
808     extension_t *p_ext = vlclua_extension_get( L );
809     int i_ret = Deactivate( p_ext->p_sys->p_mgr, p_ext );
810     return ( i_ret == VLC_SUCCESS ) ? 1 : 0;
811 }
812
813 /** Callback for the variable "dialog-event"
814  * @param p_this Current object owner of the extension and the dialog
815  * @param psz_var "dialog-event"
816  * @param oldval Unused
817  * @param newval Address of the dialog
818  * @param p_data Unused
819  **/
820 static int vlclua_extension_dialog_callback( vlc_object_t *p_this,
821                                              char const *psz_var,
822                                              vlc_value_t oldval,
823                                              vlc_value_t newval,
824                                              void *p_data )
825 {
826     /* psz_var == "dialog-event" */
827     ( void ) psz_var;
828     ( void ) oldval;
829     ( void ) p_data;
830
831     extension_dialog_command_t *command = newval.p_address;
832     assert( command != NULL );
833     assert( command->p_dlg != NULL);
834
835     extension_t *p_ext = command->p_dlg->p_sys;
836     assert( p_ext != NULL );
837
838     extension_widget_t *p_widget = command->p_data;
839
840     switch( command->event )
841     {
842         case EXTENSION_EVENT_CLICK:
843             assert( p_widget != NULL );
844             PushCommand( p_ext, CMD_CLICK, p_widget );
845             break;
846         case EXTENSION_EVENT_CLOSE:
847             PushCommand( p_ext, CMD_CLOSE );
848             break;
849         default:
850             msg_Dbg( p_this, "Received unknown UI event %d, discarded",
851                      command->event );
852             break;
853     }
854
855     return VLC_SUCCESS;
856 }
857
858 /* Lock this extension. Can fail. */
859 bool LockExtension( extension_t *p_ext )
860 {
861     if( p_ext->p_sys->b_exiting )
862         return false;
863
864     vlc_mutex_lock( &p_ext->p_sys->running_lock );
865     if( p_ext->p_sys->b_exiting )
866     {
867         vlc_mutex_unlock( &p_ext->p_sys->running_lock );
868         return false;
869     }
870
871     return true;
872 }
873
874 void UnlockExtension( extension_t *p_ext )
875 {
876     vlc_mutex_unlock( &p_ext->p_sys->running_lock );
877 }