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 /*****************************************************************************
57 *****************************************************************************/
58 static char *FindFile( const char *psz_name )
60 char *ppsz_dir_list[] = { NULL, NULL, NULL, NULL };
62 vlclua_dir_list( "intf", ppsz_dir_list );
63 for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
67 if( asprintf( &psz_filename, "%s"DIR_SEP"%s.lua", *ppsz_dir,
70 vlclua_dir_list_free( ppsz_dir_list );
73 fp = fopen( psz_filename, "r" );
77 vlclua_dir_list_free( ppsz_dir_list );
82 vlclua_dir_list_free( ppsz_dir_list );
86 static inline void luaL_register_submodule( lua_State *L, const char *psz_name,
90 luaL_register( L, NULL, l );
91 lua_setfield( L, -2, psz_name );
96 const char *psz_shortcut;
100 /* { "rc", "rc" }, */
101 { "luahotkeys", "hotkeys" },
102 /* { "hotkeys", "hotkeys" }, */
103 { "luatelnet", "telnet" },
104 /* { "telnet", "telnet" }, */
105 { "luahttp", "http" },
106 /* { "http", "http" }, */
109 static bool WordInList( const char *psz_list, const char *psz_word )
111 const char *psz_str = strstr( psz_list, psz_word );
112 int i_len = strlen( psz_word );
115 if( (psz_str == psz_list || *(psz_str-1) == ',' )
116 /* it doesn't start in middle of a word */
117 /* it doest end in middle of a word */
118 && ( psz_str[i_len] == '\0' || psz_str[i_len] == ',' ) )
120 psz_str = strstr( psz_str, psz_word );
125 static char *GetModuleName( intf_thread_t *p_intf )
128 const char *psz_intf;
129 if( *p_intf->psz_intf == '$' )
130 psz_intf = var_GetString( p_intf, p_intf->psz_intf+1 );
132 psz_intf = p_intf->psz_intf;
133 for( i = 0; pp_shortcuts[i].psz_name; i++ )
135 if( WordInList( psz_intf, pp_shortcuts[i].psz_shortcut ) )
136 return strdup( pp_shortcuts[i].psz_name );
139 return config_GetPsz( p_intf, "lua-intf" );
142 static const luaL_Reg p_reg[] = { { NULL, NULL } };
144 int Open_LuaIntf( vlc_object_t *p_this )
146 intf_thread_t *p_intf = (intf_thread_t*)p_this;
150 char *psz_name = GetModuleName( p_intf );
151 const char *psz_config;
152 bool b_config_set = false;
153 if( !psz_name ) psz_name = strdup( "dummy" );
155 p_intf->p_sys = (intf_sys_t*)malloc( sizeof(intf_sys_t) );
161 p_sys = p_intf->p_sys;
162 p_sys->psz_filename = FindFile( psz_name );
163 if( !p_sys->psz_filename )
165 msg_Err( p_intf, "Couldn't find lua interface script \"%s\".",
171 msg_Dbg( p_intf, "Found lua interface script: %s", p_sys->psz_filename );
176 msg_Err( p_intf, "Could not create new Lua State" );
184 /* register our functions */
185 luaL_register( L, "vlc", p_reg );
187 /* store a pointer to p_intf (FIXME: user could overwrite this) */
188 lua_pushlightuserdata( L, p_intf );
189 lua_setfield( L, -2, "private" );
191 /* register submodules */
202 luaopen_playlist( L );
205 luaopen_strings( L );
206 luaopen_variables( L );
215 /* Setup the module search path */
218 char *psz_char = strrchr(p_sys->psz_filename,DIR_SEP_CHAR);
220 /* FIXME: don't use luaL_dostring */
221 if( asprintf( &psz_command,
222 "package.path = \"%s"DIR_SEP"modules"DIR_SEP"?.lua;\"..package.path",
223 p_sys->psz_filename ) < 0 )
225 *psz_char = DIR_SEP_CHAR;
226 if( luaL_dostring( L, psz_command ) )
231 psz_config = config_GetPsz( p_intf, "lua-config" );
232 if( psz_config && *psz_config )
235 if( asprintf( &psz_buffer, "config={%s}", psz_config ) != -1 )
237 printf("%s\n", psz_buffer);
238 if( luaL_dostring( L, psz_buffer ) == 1 )
239 msg_Err( p_intf, "Error while parsing \"lua-config\"." );
241 lua_getglobal( L, "config" );
242 if( lua_istable( L, -1 ) )
244 lua_getfield( L, -1, psz_name );
245 if( lua_istable( L, -1 ) )
247 lua_setglobal( L, "config" );
253 if( b_config_set == false )
256 lua_setglobal( L, "config" );
261 p_intf->psz_header = psz_name;
262 /* ^^ Do I need to clean that up myself in Close_LuaIntf? */
264 vlc_mutex_init( &p_sys->lock );
265 vlc_cond_init( &p_sys->wait );
266 p_sys->exiting = false;
268 if( vlc_clone( &p_sys->thread, Run, p_intf, VLC_THREAD_PRIORITY_LOW ) )
270 p_sys->exiting = true;
271 Close_LuaIntf( p_this );
278 void Close_LuaIntf( vlc_object_t *p_this )
280 intf_thread_t *p_intf = (intf_thread_t*)p_this;
281 intf_sys_t *p_sys = p_intf->p_sys;
283 if( !p_sys->exiting ) /* <- Read-only here and in thread: no locking */
285 vlc_mutex_lock( &p_sys->lock );
286 p_sys->exiting = true;
287 vlc_cond_signal( &p_sys->wait );
288 vlc_mutex_unlock( &p_sys->lock );
289 vlc_join( p_sys->thread, NULL );
291 vlc_cond_destroy( &p_sys->wait );
292 vlc_mutex_destroy( &p_sys->lock );
294 lua_close( p_sys->L );
298 static void *Run( void *data )
300 intf_thread_t *p_intf = data;
301 intf_sys_t *p_sys = p_intf->p_sys;
302 lua_State *L = p_sys->L;
304 if( luaL_dofile( L, p_sys->psz_filename ) )
306 msg_Err( p_intf, "Error loading script %s: %s", p_sys->psz_filename,
307 lua_tostring( L, lua_gettop( L ) ) );