1 /*****************************************************************************
2 * extension.c: Lua Extensions (meta data, web information, ...)
3 *****************************************************************************
4 * Copyright (C) 2009-2010 VideoLAN and authors
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 *****************************************************************************/
26 #include "extension.h"
29 #include <vlc_input.h>
31 /* Functions to register */
32 static const luaL_Reg p_reg[] =
38 * Extensions capabilities
39 * Note: #define and ppsz_capabilities must be in sync
41 #define EXT_HAS_MENU (1 << 0)
42 #define EXT_TRIGGER_ONLY (1 << 1)
43 #define EXT_INPUT_LISTENER (1 << 2)
45 const char* const ppsz_capabilities[] = {
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,
60 static int TriggerMenu( extension_t *p_ext, int id );
61 static int TriggerExtension( extensions_manager_t *p_mgr,
64 int vlclua_extension_deactivate( lua_State *L );
67 static int vlclua_extension_dialog_callback( vlc_object_t *p_this,
77 int Open_Extension( vlc_object_t *p_this )
79 msg_Dbg( p_this, "Opening EXPERIMENTAL Lua Extension module" );
81 extensions_manager_t *p_mgr = ( extensions_manager_t* ) p_this;
83 p_mgr->pf_control = Control;
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;
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 );
95 /* Initialise Lua state structure */
96 lua_State *L = GetLuaState( p_mgr, NULL );
104 /* Scan available Lua Extensions */
105 if( ScanExtensions( p_mgr ) != VLC_SUCCESS )
107 msg_Err( p_mgr, "Can't load extensions modules" );
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 );
123 * Module unload function
125 void Close_Extension( vlc_object_t *p_this )
127 extensions_manager_t *p_mgr = ( extensions_manager_t* ) p_this;
128 msg_Dbg( p_mgr, "Deactivating all loaded extensions" );
130 vlc_mutex_lock( &p_mgr->lock );
131 p_mgr->p_sys->b_killed = true;
132 vlc_mutex_unlock( &p_mgr->lock );
134 var_Destroy( p_mgr, "dialog-event" );
136 extension_t *p_ext = NULL;
137 FOREACH_ARRAY( p_ext, p_mgr->p_sys->activated_extensions )
140 Deactivate( p_mgr, p_ext );
141 WaitForDeactivation( p_ext );
145 msg_Dbg( p_mgr, "All extensions are now deactivated" );
146 ARRAY_RESET( p_mgr->p_sys->activated_extensions );
148 if( p_mgr->p_sys && p_mgr->p_sys->L )
149 lua_close( p_mgr->p_sys->L );
151 vlc_mutex_destroy( &p_mgr->lock );
152 vlc_mutex_destroy( &p_mgr->p_sys->lock );
153 free( p_mgr->p_sys );
156 /* Free extensions' memory */
157 FOREACH_ARRAY( p_ext, p_mgr->extensions )
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 );
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 );
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 )
186 vlclua_scripts_batch_execute( VLC_OBJECT( p_mgr ),
189 p_mgr->p_sys->L, &b_true );
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
204 int ScanLuaCallback( vlc_object_t *p_this, const char *psz_script,
205 lua_State *L, void *pb_continue )
207 extensions_manager_t *p_mgr = ( extensions_manager_t* ) p_this;
210 msg_Dbg( p_mgr, "Scanning Lua script %s", psz_script );
212 vlc_mutex_lock( &p_mgr->lock );
214 /* Create new script descriptor */
215 extension_t *p_ext = ( extension_t* ) calloc( 1, sizeof( extension_t ) );
218 vlc_mutex_unlock( &p_mgr->lock );
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 )
226 free( p_ext->psz_name );
227 free( p_ext->p_sys );
229 vlc_mutex_unlock( &p_mgr->lock );
232 p_ext->p_sys->p_mgr = p_mgr;
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 );
239 /* Load and run the script(s) */
240 if( luaL_dofile( L, psz_script ) )
242 msg_Warn( p_mgr, "Error loading script %s: %s", psz_script,
243 lua_tostring( L, lua_gettop( L ) ) );
248 /* Scan script for capabilities */
249 lua_getglobal( L, "descriptor" );
251 if( !lua_isfunction( L, -1 ) )
253 msg_Warn( p_mgr, "Error while runing script %s, "
254 "function descriptor() not found", psz_script );
258 if( lua_pcall( L, 0, 1, 0 ) )
260 msg_Warn( p_mgr, "Error while runing script %s, "
261 "function descriptor(): %s", psz_script,
262 lua_tostring( L, lua_gettop( L ) ) );
266 if( lua_gettop( L ) )
268 if( lua_istable( L, -1 ) )
271 lua_getfield( L, -1, "capabilities" );
272 if( lua_istable( L, -1 ) )
275 while( lua_next( L, -2 ) != 0 )
277 /* Key is at index -2 and value at index -1. Discard key */
278 const char *psz_cap = luaL_checkstring( L, -1 );
281 /* Find this capability's flag */
282 for( const char *iter = *ppsz_capabilities;
284 iter = ppsz_capabilities[ ++i_cap ])
286 if( !strcmp( iter, psz_cap ) )
289 p_ext->p_sys->i_capabilities |= 1 << i_cap;
296 msg_Warn( p_mgr, "Extension capability '%s' unknown in"
297 " script %s", psz_cap, psz_script );
299 /* Removes 'value'; keeps 'key' for next iteration */
305 msg_Warn( p_mgr, "In script %s, function descriptor() "
306 "did not return a table of capabilities.",
312 lua_getfield( L, -1, "title" );
313 if( lua_isstring( L, -1 ) )
315 p_ext->psz_title = strdup( luaL_checkstring( L, -1 ) );
319 msg_Dbg( p_mgr, "In script %s, function descriptor() "
320 "did not return a string as title.",
322 p_ext->psz_title = strdup( psz_script );
327 lua_getfield( L, -1, "author" );
328 if( lua_isstring( L, -1 ) )
330 p_ext->psz_author = strdup( luaL_checkstring( L, -1 ) );
334 p_ext->psz_author = NULL;
338 /* Get description */
339 lua_getfield( L, -1, "description" );
340 if( lua_isstring( L, -1 ) )
342 p_ext->psz_description = strdup( luaL_checkstring( L, -1 ) );
346 p_ext->psz_description = NULL;
351 lua_getfield( L, -1, "url" );
352 if( lua_isstring( L, -1 ) )
354 p_ext->psz_url = strdup( luaL_checkstring( L, -1 ) );
358 p_ext->psz_url = NULL;
363 lua_getfield( L, -1, "version" );
364 if( lua_isstring( L, -1 ) )
366 p_ext->psz_version = strdup( luaL_checkstring( L, -1 ) );
370 p_ext->psz_version = NULL;
376 msg_Warn( p_mgr, "In script %s, function descriptor() "
377 "did not return a table!", psz_script );
383 msg_Err( p_mgr, "Script %s went completely foobar", psz_script );
387 msg_Dbg( p_mgr, "Script %s has the following capability flags: 0x%x",
388 psz_script, p_ext->p_sys->i_capabilities );
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 );
408 /* Add the extension to the list of known extensions */
409 ARRAY_APPEND( p_mgr->extensions, p_ext );
412 vlc_mutex_unlock( &p_mgr->lock );
413 /* Continue batch execution */
414 return pb_continue ? ( (* (bool*)pb_continue) ? -1 : 0 ) : -1;
417 static int Control( extensions_manager_t *p_mgr, int i_control, va_list args )
419 extension_t *p_ext = NULL;
421 uint16_t **ppus = NULL;
422 char ***pppsz = NULL;
427 case EXTENSION_ACTIVATE:
428 p_ext = ( extension_t* ) va_arg( args, extension_t* );
429 return Activate( p_mgr, p_ext );
431 case EXTENSION_DEACTIVATE:
432 p_ext = ( extension_t* ) va_arg( args, extension_t* );
433 return Deactivate( p_mgr, p_ext );
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 );
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;
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 );
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;
459 case EXTENSION_TRIGGER:
460 p_ext = ( extension_t* ) va_arg( args, extension_t* );
461 return TriggerExtension( p_mgr, p_ext );
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 );
469 case EXTENSION_SET_INPUT:
471 p_ext = ( extension_t* ) va_arg( args, extension_t* );
472 input_thread_t *p_input = va_arg( args, struct input_thread_t * );
474 if( !LockExtension( p_ext ) )
478 input_thread_t *old = p_ext->p_sys->p_input;
480 vlc_object_release( old );
481 p_ext->p_sys->p_input = p_input ? vlc_object_hold( p_input )
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 );
488 UnlockExtension( p_ext );
493 msg_Err( p_mgr, "Control '%d' not yet implemented in Extension",
501 int lua_ExtensionActivate( extensions_manager_t *p_mgr, extension_t *p_ext )
503 assert( p_mgr != NULL && p_ext != NULL );
504 return lua_ExecuteFunction( p_mgr, p_ext, "activate" );
507 int lua_ExtensionDeactivate( extensions_manager_t *p_mgr, extension_t *p_ext )
509 assert( p_mgr != NULL && p_ext != NULL );
511 if( !p_ext->p_sys->L )
514 int i_ret = lua_ExecuteFunction( p_mgr, p_ext, "deactivate" );
516 /* Clear Lua State */
517 lua_close( p_ext->p_sys->L );
518 p_ext->p_sys->L = NULL;
523 int lua_ExtensionWidgetClick( extensions_manager_t *p_mgr,
525 extension_widget_t *p_widget )
527 if( !p_ext->p_sys->L )
530 return lua_ExecuteFunction( p_mgr, p_ext, (const char*) p_widget->p_sys );
535 * Get the list of menu entries from an extension script
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()
544 static int GetMenuEntries( extensions_manager_t *p_mgr, extension_t *p_ext,
545 char ***pppsz_titles, uint16_t **ppi_ids )
547 assert( *pppsz_titles == NULL );
548 assert( *ppi_ids == NULL );
550 if( !IsActivated( p_mgr, p_ext ) )
552 msg_Dbg( p_mgr, "Can't get menu before activating the extension!" );
556 if( !LockExtension( p_ext ) )
558 /* Dying extension, fail. */
562 int i_ret = VLC_EGENERIC;
563 lua_State *L = GetLuaState( p_mgr, p_ext );
565 if( ( p_ext->p_sys->i_capabilities & EXT_HAS_MENU ) == 0 )
567 msg_Dbg( p_mgr, "can't get a menu from an extension without menu!" );
571 lua_getglobal( L, "menu" );
573 if( !lua_isfunction( L, -1 ) )
575 msg_Warn( p_mgr, "Error while runing script %s, "
576 "function menu() not found", p_ext->psz_name );
580 if( lua_pcall( L, 0, 1, 0 ) )
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 ) ) );
588 if( lua_gettop( L ) )
590 if( lua_istable( L, -1 ) )
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 ) );
600 while( lua_next( L, -2 ) != 0 )
602 assert( i_idx < i_size );
603 if( (!lua_isstring( L, -1 )) || (!lua_isnumber( L, -2 )) )
605 msg_Warn( p_mgr, "In script %s, an entry in "
606 "the menu table is invalid!", p_ext->psz_name );
609 (*pppsz_titles)[ i_idx ] = strdup( luaL_checkstring( L, -1 ) );
610 (*ppi_ids)[ i_idx ] = (uint16_t) ( luaL_checkinteger( L, -2 ) & 0xFFFF );
617 msg_Warn( p_mgr, "Function menu() in script %s "
618 "did not return a table", p_ext->psz_name );
624 msg_Warn( p_mgr, "Script %s went completely foobar", p_ext->psz_name );
631 UnlockExtension( p_ext );
632 if( i_ret != VLC_SUCCESS )
634 msg_Dbg( p_mgr, "Something went wrong in %s (%s:%d)",
635 __func__, __FILE__, __LINE__ );
640 /* Must be entered with the Lock on Extension */
641 static lua_State* GetLuaState( extensions_manager_t *p_mgr,
653 msg_Err( p_mgr, "Could not create new Lua State" );
657 luaL_register( L, "vlc", p_reg );
660 lua_pushlightuserdata( L, p_mgr );
661 lua_setfield( L, -2, "private" );
663 lua_pushlightuserdata( L, p_ext );
664 lua_setfield( L, -2, "extension" );
668 /* Load more libraries */
671 luaopen_dialog( L, p_ext );
678 luaopen_playlist( L );
681 luaopen_strings( L );
682 luaopen_variables( L );
687 /* Register extension specific functions */
688 lua_getglobal( L, "vlc" );
689 lua_pushcfunction( L, vlclua_extension_deactivate );
690 lua_setfield( L, -2, "deactivate" );
692 /* Load and run the script(s) */
693 if( luaL_dofile( L, p_ext->psz_name ) != 0 )
695 msg_Warn( p_mgr, "Error loading script %s: %s", p_ext->psz_name,
696 lua_tostring( L, lua_gettop( L ) ) );
707 msg_Dbg( p_mgr, "Reusing old Lua state for extension '%s'",
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)
721 int lua_ExecuteFunction( extensions_manager_t *p_mgr,
723 const char *psz_function )
725 int i_ret = VLC_EGENERIC;
726 assert( p_mgr != NULL );
727 assert( p_ext != NULL );
729 lua_State *L = GetLuaState( p_mgr, p_ext );
730 lua_getglobal( L, psz_function );
732 if( !lua_isfunction( L, -1 ) )
734 msg_Warn( p_mgr, "Error while runing script %s, "
735 "function %s() not found", p_ext->psz_name, psz_function );
739 if( lua_pcall( L, 0, 1, 0 ) )
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 ) ) );
752 static inline int TriggerMenu( extension_t *p_ext, int i_id )
754 return PushCommand( p_ext, CMD_TRIGGERMENU, i_id );
757 int lua_ExtensionTriggerMenu( extensions_manager_t *p_mgr,
758 extension_t *p_ext, int id )
760 int i_ret = VLC_EGENERIC;
761 lua_State *L = GetLuaState( p_mgr, p_ext );
766 luaopen_dialog( L, p_ext );
768 lua_getglobal( L, "trigger_menu" );
769 if( !lua_isfunction( L, -1 ) )
771 msg_Warn( p_mgr, "Error while runing script %s, "
772 "function trigger_menu() not found", p_ext->psz_name );
776 /* Pass id as unique argument to the function */
777 lua_pushinteger( L, id );
779 if( lua_pcall( L, 1, 1, 0 ) != 0 )
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 ) ) );
787 if( i_ret < VLC_SUCCESS )
789 msg_Dbg( p_mgr, "Something went wrong in %s (%s:%d)",
790 __func__, __FILE__, __LINE__ );
795 /** Directly trigger an extension, without activating it
796 * This is NOT multithreaded, and this code runs in the UI thread
798 * @param p_ext Extension to trigger
799 * @return Value returned by the lua function "trigger"
801 static int TriggerExtension( extensions_manager_t *p_mgr,
804 int i_ret = lua_ExecuteFunction( p_mgr, p_ext, "trigger" );
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;
814 /** Retrieve extension associated to the current script
815 * @param L current lua_State
816 * @return Lua userdata "vlc.extension"
818 extension_t *vlclua_extension_get( lua_State *L )
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 ) );
828 /** Deactivate an extension by order from the extension itself
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.
833 int vlclua_extension_deactivate( lua_State *L )
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;
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
847 static int vlclua_extension_dialog_callback( vlc_object_t *p_this,
853 /* psz_var == "dialog-event" */
858 extension_dialog_command_t *command = newval.p_address;
859 assert( command != NULL );
860 assert( command->p_dlg != NULL);
862 extension_t *p_ext = command->p_dlg->p_sys;
863 assert( p_ext != NULL );
865 extension_widget_t *p_widget = command->p_data;
867 switch( command->event )
869 case EXTENSION_EVENT_CLICK:
870 assert( p_widget != NULL );
871 PushCommand( p_ext, CMD_CLICK, p_widget );
873 case EXTENSION_EVENT_CLOSE:
874 PushCommand( p_ext, CMD_CLOSE );
877 msg_Dbg( p_this, "Received unknown UI event %d, discarded",
885 /* Lock this extension. Can fail. */
886 bool LockExtension( extension_t *p_ext )
888 if( p_ext->p_sys->b_exiting )
891 vlc_mutex_lock( &p_ext->p_sys->running_lock );
892 if( p_ext->p_sys->b_exiting )
894 vlc_mutex_unlock( &p_ext->p_sys->running_lock );
901 void UnlockExtension( extension_t *p_ext )
903 vlc_mutex_unlock( &p_ext->p_sys->running_lock );