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