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>
37 #include <vlc_charset.h>
39 #include <vlc_interface.h>
40 #include <vlc_playlist.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 char *FindFile( vlc_object_t *p_this, const char *psz_name )
62 char *ppsz_dir_list[] = { NULL, NULL, NULL, NULL };
64 vlclua_dir_list( p_this, "intf", ppsz_dir_list );
65 for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
69 if( asprintf( &psz_filename, "%s"DIR_SEP"%s.lua", *ppsz_dir,
72 vlclua_dir_list_free( ppsz_dir_list );
75 fp = fopen( psz_filename, "r" );
79 vlclua_dir_list_free( ppsz_dir_list );
84 vlclua_dir_list_free( ppsz_dir_list );
88 static inline void luaL_register_submodule( lua_State *L, const char *psz_name,
92 luaL_register( L, NULL, l );
93 lua_setfield( L, -2, psz_name );
98 const char *psz_shortcut;
102 /* { "rc", "rc" }, */
103 { "luahotkeys", "hotkeys" },
104 /* { "hotkeys", "hotkeys" }, */
105 { "luatelnet", "telnet" },
106 /* { "telnet", "telnet" }, */
107 { "luahttp", "http" },
108 /* { "http", "http" }, */
111 static bool WordInList( const char *psz_list, const char *psz_word )
113 const char *psz_str = strstr( psz_list, psz_word );
114 int i_len = strlen( psz_word );
117 if( (psz_str == psz_list || *(psz_str-1) == ',' )
118 /* it doesn't start in middle of a word */
119 /* it doest end in middle of a word */
120 && ( psz_str[i_len] == '\0' || psz_str[i_len] == ',' ) )
122 psz_str = strstr( psz_str, psz_word );
127 static char *GetModuleName( intf_thread_t *p_intf )
130 const char *psz_intf;
131 /*if( *p_intf->psz_intf == '$' )
132 psz_intf = var_GetString( p_intf, p_intf->psz_intf+1 );
134 psz_intf = p_intf->psz_intf;
135 for( i = 0; pp_shortcuts[i].psz_name; i++ )
137 if( WordInList( psz_intf, pp_shortcuts[i].psz_shortcut ) )
138 return strdup( pp_shortcuts[i].psz_name );
141 return var_CreateGetString( p_intf, "lua-intf" );
144 static const luaL_Reg p_reg[] = { { NULL, NULL } };
146 int Open_LuaIntf( vlc_object_t *p_this )
148 intf_thread_t *p_intf = (intf_thread_t*)p_this;
152 config_ChainParse( p_intf, "lua-", ppsz_intf_options, p_intf->p_cfg );
153 char *psz_name = GetModuleName( p_intf );
155 bool b_config_set = false;
156 if( !psz_name ) psz_name = strdup( "dummy" );
158 p_intf->p_sys = (intf_sys_t*)malloc( sizeof(intf_sys_t) );
164 p_sys = p_intf->p_sys;
165 p_sys->psz_filename = FindFile( p_this, psz_name );
166 if( !p_sys->psz_filename )
168 msg_Err( p_intf, "Couldn't find lua interface script \"%s\".",
174 msg_Dbg( p_intf, "Found lua interface script: %s", p_sys->psz_filename );
179 msg_Err( p_intf, "Could not create new Lua State" );
180 free( p_sys->psz_filename );
188 /* register our functions */
189 luaL_register( L, "vlc", p_reg );
191 /* store a pointer to p_intf (FIXME: user could overwrite this) */
192 lua_pushlightuserdata( L, p_intf );
193 lua_setfield( L, -2, "private" );
195 /* register submodules */
206 luaopen_playlist( L );
209 luaopen_strings( L );
210 luaopen_variables( L );
219 /* Setup the module search path */
222 char *psz_char = strrchr(p_sys->psz_filename,DIR_SEP_CHAR);
224 /* FIXME: don't use luaL_dostring */
225 if( asprintf( &psz_command,
226 "package.path = \"%s"DIR_SEP"modules"DIR_SEP"?.lua;\"..package.path",
227 p_sys->psz_filename ) < 0 )
229 free( p_sys->psz_filename );
234 *psz_char = DIR_SEP_CHAR;
235 if( luaL_dostring( L, psz_command ) )
238 free( p_sys->psz_filename );
247 psz_config = var_CreateGetString( p_intf, "lua-config" );
248 if( psz_config && *psz_config )
251 if( asprintf( &psz_buffer, "config={%s}", psz_config ) != -1 )
253 printf("%s\n", psz_buffer);
254 if( luaL_dostring( L, psz_buffer ) == 1 )
255 msg_Err( p_intf, "Error while parsing \"lua-config\"." );
257 lua_getglobal( L, "config" );
258 if( lua_istable( L, -1 ) )
260 lua_getfield( L, -1, psz_name );
261 if( lua_istable( L, -1 ) )
263 lua_setglobal( L, "config" );
271 if( b_config_set == false )
274 lua_setglobal( L, "config" );
279 p_intf->psz_header = psz_name;
280 /* ^^ Do I need to clean that up myself in Close_LuaIntf? */
282 vlc_mutex_init( &p_sys->lock );
283 vlc_cond_init( &p_sys->wait );
284 p_sys->exiting = false;
286 if( vlc_clone( &p_sys->thread, Run, p_intf, VLC_THREAD_PRIORITY_LOW ) )
288 p_sys->exiting = true;
289 Close_LuaIntf( p_this );
296 void Close_LuaIntf( vlc_object_t *p_this )
298 intf_thread_t *p_intf = (intf_thread_t*)p_this;
299 intf_sys_t *p_sys = p_intf->p_sys;
301 vlc_cancel( p_sys->thread );
303 if( !p_sys->exiting ) /* <- Read-only here and in thread: no locking */
305 vlc_mutex_lock( &p_sys->lock );
306 p_sys->exiting = true;
307 vlc_cond_signal( &p_sys->wait );
308 vlc_mutex_unlock( &p_sys->lock );
309 vlc_join( p_sys->thread, NULL );
311 vlc_cond_destroy( &p_sys->wait );
312 vlc_mutex_destroy( &p_sys->lock );
314 lua_close( p_sys->L );
316 free( p_sys->psz_filename );
320 static void *Run( void *data )
322 intf_thread_t *p_intf = data;
323 intf_sys_t *p_sys = p_intf->p_sys;
324 lua_State *L = p_sys->L;
326 if( luaL_dofile( L, p_sys->psz_filename ) )
328 msg_Err( p_intf, "Error loading script %s: %s", p_sys->psz_filename,
329 lua_tostring( L, lua_gettop( L ) ) );