1 /*****************************************************************************
2 * intf.c: Generic lua interface functions
3 *****************************************************************************
4 * Copyright (C) 2007-2008 the VideoLAN team
7 * Authors: Antoine Cellerier <dionoea at videolan tod org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
35 #include <vlc_common.h>
38 #include <vlc_interface.h>
39 #include <vlc_playlist.h>
41 #include <sys/types.h>
44 #include <lua.h> /* Low level lua C API */
45 #include <lauxlib.h> /* Higher level C API */
46 #include <lualib.h> /* Lua libs */
51 /*****************************************************************************
53 *****************************************************************************/
54 static void *Run( void * );
56 static const char * const ppsz_intf_options[] = { "intf", "config", NULL };
58 /*****************************************************************************
60 *****************************************************************************/
61 static inline void luaL_register_submodule( lua_State *L, const char *psz_name,
65 luaL_register( L, NULL, l );
66 lua_setfield( L, -2, psz_name );
71 const char *psz_shortcut;
76 { "luahotkeys", "hotkeys" },
77 /* { "hotkeys", "hotkeys" }, */
78 { "luatelnet", "telnet" },
79 { "telnet", "telnet" },
80 { "luahttp", "http" },
84 static const char *WordInList( const char *psz_list, const char *psz_word )
88 const char *end = strchr( psz_list, ',' );
92 if( !strncmp( psz_list, psz_word, end - psz_list ) )
96 return !strcmp( psz_list, psz_word ) ? psz_list : NULL;
99 static char *GetModuleName( intf_thread_t *p_intf )
102 const char *psz_intf;
103 /*if( *p_intf->psz_intf == '$' )
104 psz_intf = var_GetString( p_intf, p_intf->psz_intf+1 );
106 psz_intf = p_intf->psz_intf;
108 int i_candidate = -1;
109 const char *psz_candidate = NULL;
110 for( i = 0; pp_shortcuts[i].psz_name; i++ )
112 const char *psz_match;
113 if( ( psz_match = WordInList( psz_intf, pp_shortcuts[i].psz_shortcut ) ) )
115 if( !psz_candidate || psz_match < psz_candidate )
117 psz_candidate = psz_match;
123 if( i_candidate >= 0 )
124 return strdup( pp_shortcuts[i_candidate].psz_name );
126 return var_CreateGetString( p_intf, "lua-intf" );
129 static const luaL_Reg p_reg[] = { { NULL, NULL } };
131 int Open_LuaIntf( vlc_object_t *p_this )
133 intf_thread_t *p_intf = (intf_thread_t*)p_this;
137 config_ChainParse( p_intf, "lua-", ppsz_intf_options, p_intf->p_cfg );
138 char *psz_name = NULL;
140 if( !p_intf->psz_intf || !*p_intf->psz_intf )
141 psz_name = strdup( "rc" );
143 psz_name = GetModuleName( p_intf );
145 if( !psz_name ) psz_name = strdup( "dummy" );
148 bool b_config_set = false;
150 p_intf->p_sys = (intf_sys_t*)malloc( sizeof(intf_sys_t) );
156 p_sys = p_intf->p_sys;
157 p_sys->psz_filename = vlclua_find_file( p_this, "intf", psz_name );
158 if( !p_sys->psz_filename )
160 msg_Err( p_intf, "Couldn't find lua interface script \"%s\".",
164 msg_Dbg( p_intf, "Found lua interface script: %s", p_sys->psz_filename );
169 msg_Err( p_intf, "Could not create new Lua State" );
173 vlclua_set_this( L, p_intf );
174 vlclua_set_intf( L, p_sys );
178 /* register our functions */
179 luaL_register( L, "vlc", p_reg );
181 /* register submodules */
192 luaopen_playlist( L );
195 luaopen_strings( L );
196 luaopen_variables( L );
200 luaopen_gettext( L );
207 /* Setup the module search path */
208 if( vlclua_add_modules_path( p_intf, L, p_sys->psz_filename ) )
210 msg_Warn( p_intf, "Error while setting the module search path for %s",
211 p_sys->psz_filename );
216 psz_config = var_CreateGetString( p_intf, "lua-config" );
217 if( psz_config && *psz_config )
220 if( asprintf( &psz_buffer, "config={%s}", psz_config ) != -1 )
222 msg_Dbg( p_intf, "Setting config variable: %s", psz_buffer );
223 if( luaL_dostring( L, psz_buffer ) == 1 )
224 msg_Err( p_intf, "Error while parsing \"lua-config\"." );
226 lua_getglobal( L, "config" );
227 if( lua_istable( L, -1 ) )
229 lua_getfield( L, -1, psz_name );
230 if( lua_istable( L, -1 ) )
232 lua_setglobal( L, "config" );
240 if( b_config_set == false )
243 lua_setglobal( L, "config" );
248 p_intf->psz_header = psz_name;
249 /* ^^ Do I need to clean that up myself in Close_LuaIntf? */
251 vlc_mutex_init( &p_sys->lock );
252 vlc_cond_init( &p_sys->wait );
253 p_sys->exiting = false;
255 if( vlc_clone( &p_sys->thread, Run, p_intf, VLC_THREAD_PRIORITY_LOW ) )
257 p_intf->psz_header = NULL;
258 vlc_cond_destroy( &p_sys->wait );
259 vlc_mutex_destroy( &p_sys->lock );
260 lua_close( p_sys->L );
266 free( p_sys->psz_filename );
272 void Close_LuaIntf( vlc_object_t *p_this )
274 intf_thread_t *p_intf = (intf_thread_t*)p_this;
275 intf_sys_t *p_sys = p_intf->p_sys;
277 vlc_cancel( p_sys->thread );
279 vlc_mutex_lock( &p_sys->lock );
280 p_sys->exiting = true;
281 vlc_cond_signal( &p_sys->wait );
282 vlc_mutex_unlock( &p_sys->lock );
283 vlc_join( p_sys->thread, NULL );
284 vlc_cond_destroy( &p_sys->wait );
285 vlc_mutex_destroy( &p_sys->lock );
287 lua_close( p_sys->L );
289 free( p_sys->psz_filename );
293 static void *Run( void *data )
295 intf_thread_t *p_intf = data;
296 intf_sys_t *p_sys = p_intf->p_sys;
297 lua_State *L = p_sys->L;
299 if( luaL_dofile( L, p_sys->psz_filename ) )
301 msg_Err( p_intf, "Error loading script %s: %s", p_sys->psz_filename,
302 lua_tostring( L, lua_gettop( L ) ) );