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>
40 #include <sys/types.h>
43 #include <lua.h> /* Low level lua C API */
44 #include <lauxlib.h> /* Higher level C API */
45 #include <lualib.h> /* Lua libs */
50 /*****************************************************************************
52 *****************************************************************************/
53 static void *Run( void * );
55 static const char * const ppsz_intf_options[] = { "intf", "config", NULL };
57 /*****************************************************************************
59 *****************************************************************************/
60 static inline void luaL_register_submodule( lua_State *L, const char *psz_name,
64 luaL_register( L, NULL, l );
65 lua_setfield( L, -2, psz_name );
70 const char *psz_shortcut;
75 { "luahotkeys", "hotkeys" },
76 /* { "hotkeys", "hotkeys" }, */
77 { "luatelnet", "telnet" },
78 { "telnet", "telnet" },
79 { "luahttp", "http" },
80 /* { "http", "http" }, */
83 static const char *WordInList( const char *psz_list, const char *psz_word )
87 const char *end = strchr( psz_list, ',' );
91 if( !strncmp( psz_list, psz_word, end - psz_list ) )
95 return !strcmp( psz_list, psz_word ) ? psz_list : NULL;
98 static char *GetModuleName( intf_thread_t *p_intf )
101 const char *psz_intf;
102 /*if( *p_intf->psz_intf == '$' )
103 psz_intf = var_GetString( p_intf, p_intf->psz_intf+1 );
105 psz_intf = p_intf->psz_intf;
107 int i_candidate = -1;
108 const char *psz_candidate = NULL;
109 for( i = 0; pp_shortcuts[i].psz_name; i++ )
111 const char *psz_match;
112 if( ( psz_match = WordInList( psz_intf, pp_shortcuts[i].psz_shortcut ) ) )
114 if( !psz_candidate || psz_match < psz_candidate )
116 psz_candidate = psz_match;
122 if( i_candidate >= 0 )
123 return strdup( pp_shortcuts[i_candidate].psz_name );
125 return var_CreateGetString( p_intf, "lua-intf" );
128 static const luaL_Reg p_reg[] = { { NULL, NULL } };
130 int Open_LuaIntf( vlc_object_t *p_this )
132 intf_thread_t *p_intf = (intf_thread_t*)p_this;
136 config_ChainParse( p_intf, "lua-", ppsz_intf_options, p_intf->p_cfg );
137 char *psz_name = NULL;
139 if( !p_intf->psz_intf || !*p_intf->psz_intf )
140 psz_name = strdup( "rc" );
142 psz_name = GetModuleName( p_intf );
144 if( !psz_name ) psz_name = strdup( "dummy" );
147 bool b_config_set = false;
149 p_intf->p_sys = (intf_sys_t*)malloc( sizeof(intf_sys_t) );
155 p_sys = p_intf->p_sys;
156 p_sys->psz_filename = vlclua_find_file( p_this, "intf", psz_name );
157 if( !p_sys->psz_filename )
159 msg_Err( p_intf, "Couldn't find lua interface script \"%s\".",
163 msg_Dbg( p_intf, "Found lua interface script: %s", p_sys->psz_filename );
168 msg_Err( p_intf, "Could not create new Lua State" );
172 vlclua_set_this( L, p_intf );
173 vlclua_set_intf( L, p_sys );
177 /* register our functions */
178 luaL_register( L, "vlc", p_reg );
180 /* register submodules */
191 luaopen_playlist( L );
194 luaopen_strings( L );
195 luaopen_variables( L );
199 luaopen_gettext( L );
206 /* Setup the module search path */
207 if( vlclua_add_modules_path( p_intf, L, p_sys->psz_filename ) )
209 msg_Warn( p_intf, "Error while setting the module search path for %s",
210 p_sys->psz_filename );
216 * Get the lua-config string.
217 * If the string is empty, try with the old http-* options and build the righr line
219 psz_config = var_CreateGetNonEmptyString( p_intf, "lua-config" );
220 if( !psz_config && !strcmp( psz_name, "http" ) )
222 char *psz_http_host = var_CreateGetNonEmptyString( p_intf, "http-host" );
223 char *psz_http_src = var_CreateGetNonEmptyString( p_intf, "http-src" );
224 bool b_http_index = var_CreateGetBool( p_intf, "http-index" );
227 char *psz_esc = config_StringEscape( psz_http_host );
228 asprintf( &psz_config, "http={host='%s'", psz_esc );
230 free( psz_http_host );
234 char *psz_esc = config_StringEscape( psz_http_src );
238 asprintf( &psz_tmp, "%s,dir='%s'", psz_config, psz_esc );
240 psz_config = psz_tmp;
243 asprintf( &psz_config, "http={dir='%s'", psz_esc );
245 free( psz_http_src );
250 asprintf( &psz_tmp, "%s,no_index=%s}", psz_config, b_http_index ? "true" : "false" );
252 psz_config = psz_tmp;
255 asprintf( &psz_config, "http={no_index=%s}", b_http_index ? "true" : "false" );
261 if( asprintf( &psz_buffer, "config={%s}", psz_config ) != -1 )
263 msg_Dbg( p_intf, "Setting config variable: %s", psz_buffer );
264 if( luaL_dostring( L, psz_buffer ) == 1 )
265 msg_Err( p_intf, "Error while parsing \"lua-config\"." );
267 lua_getglobal( L, "config" );
268 if( lua_istable( L, -1 ) )
270 lua_getfield( L, -1, psz_name );
271 if( lua_istable( L, -1 ) )
273 lua_setglobal( L, "config" );
281 if( b_config_set == false )
284 lua_setglobal( L, "config" );
289 p_intf->psz_header = psz_name;
290 /* ^^ Do I need to clean that up myself in Close_LuaIntf? */
292 vlc_mutex_init( &p_sys->lock );
293 vlc_cond_init( &p_sys->wait );
294 p_sys->exiting = false;
296 if( vlc_clone( &p_sys->thread, Run, p_intf, VLC_THREAD_PRIORITY_LOW ) )
298 p_intf->psz_header = NULL;
299 vlc_cond_destroy( &p_sys->wait );
300 vlc_mutex_destroy( &p_sys->lock );
301 lua_close( p_sys->L );
307 free( p_sys->psz_filename );
313 void Close_LuaIntf( vlc_object_t *p_this )
315 intf_thread_t *p_intf = (intf_thread_t*)p_this;
316 intf_sys_t *p_sys = p_intf->p_sys;
318 vlc_cancel( p_sys->thread );
320 vlc_mutex_lock( &p_sys->lock );
321 p_sys->exiting = true;
322 vlc_cond_signal( &p_sys->wait );
323 vlc_mutex_unlock( &p_sys->lock );
324 vlc_join( p_sys->thread, NULL );
325 vlc_cond_destroy( &p_sys->wait );
326 vlc_mutex_destroy( &p_sys->lock );
328 lua_close( p_sys->L );
330 free( p_sys->psz_filename );
334 static void *Run( void *data )
336 intf_thread_t *p_intf = data;
337 intf_sys_t *p_sys = p_intf->p_sys;
338 lua_State *L = p_sys->L;
340 if( luaL_dofile( L, p_sys->psz_filename ) )
342 msg_Err( p_intf, "Error loading script %s: %s", p_sys->psz_filename,
343 lua_tostring( L, lua_gettop( L ) ) );