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 *****************************************************************************/
59 static void Run( intf_thread_t *p_intf );
61 /*****************************************************************************
63 *****************************************************************************/
64 static char *FindFile( const char *psz_name )
66 char *ppsz_dir_list[] = { NULL, NULL, NULL, NULL };
68 vlclua_dir_list( "intf", ppsz_dir_list );
69 for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
73 if( asprintf( &psz_filename, "%s"DIR_SEP"%s.lua", *ppsz_dir,
76 vlclua_dir_list_free( ppsz_dir_list );
79 fp = fopen( psz_filename, "r" );
83 vlclua_dir_list_free( ppsz_dir_list );
88 vlclua_dir_list_free( ppsz_dir_list );
92 static inline void luaL_register_submodule( lua_State *L, const char *psz_name,
96 luaL_register( L, NULL, l );
97 lua_setfield( L, -2, psz_name );
102 const char *psz_shortcut;
103 const char *psz_name;
106 /* { "rc", "rc" }, */
107 { "luahotkeys", "hotkeys" },
108 /* { "hotkeys", "hotkeys" }, */
109 { "luatelnet", "telnet" },
110 /* { "telnet", "telnet" }, */
111 { "luahttp", "http" },
112 /* { "http", "http" }, */
115 static bool WordInList( const char *psz_list, const char *psz_word )
117 const char *psz_str = strstr( psz_list, psz_word );
118 int i_len = strlen( psz_word );
121 if( (psz_str == psz_list || *(psz_str-1) == ',' )
122 /* it doesn't start in middle of a word */
123 /* it doest end in middle of a word */
124 && ( psz_str[i_len] == '\0' || psz_str[i_len] == ',' ) )
126 psz_str = strstr( psz_str, psz_word );
131 static char *GetModuleName( intf_thread_t *p_intf )
134 const char *psz_intf;
135 if( *p_intf->psz_intf == '$' )
136 psz_intf = var_GetString( p_intf, p_intf->psz_intf+1 );
138 psz_intf = p_intf->psz_intf;
139 for( i = 0; pp_shortcuts[i].psz_name; i++ )
141 if( WordInList( psz_intf, pp_shortcuts[i].psz_shortcut ) )
142 return strdup( pp_shortcuts[i].psz_name );
145 return config_GetPsz( p_intf, "lua-intf" );
148 static luaL_Reg p_reg[] = { { NULL, NULL } };
150 int Open_LuaIntf( vlc_object_t *p_this )
152 intf_thread_t *p_intf = (intf_thread_t*)p_this;
156 char *psz_name = GetModuleName( p_intf );
157 const char *psz_config;
158 bool b_config_set = false;
159 if( !psz_name ) psz_name = strdup( "dummy" );
161 p_intf->p_sys = (intf_sys_t*)malloc( sizeof(intf_sys_t*) );
167 p_sys = p_intf->p_sys;
168 p_sys->psz_filename = FindFile( psz_name );
169 if( !p_sys->psz_filename )
171 msg_Err( p_intf, "Couldn't find lua interface script \"%s\".",
177 msg_Dbg( p_intf, "Found lua interface script: %s", p_sys->psz_filename );
182 msg_Err( p_intf, "Could not create new Lua State" );
190 /* register our functions */
191 luaL_register( L, "vlc", p_reg );
193 /* store a pointer to p_intf (FIXME: user could overwrite this) */
194 lua_pushlightuserdata( L, p_intf );
195 lua_setfield( L, -2, "private" );
197 /* register submodules */
208 luaopen_playlist( L );
211 luaopen_strings( L );
212 luaopen_variables( L );
221 /* Setup the module search path */
224 char *psz_char = strrchr(p_sys->psz_filename,DIR_SEP_CHAR);
226 /* FIXME: don't use luaL_dostring */
227 if( asprintf( &psz_command,
228 "package.path = \"%s"DIR_SEP"modules"DIR_SEP"?.lua;\"..package.path",
229 p_sys->psz_filename ) < 0 )
231 *psz_char = DIR_SEP_CHAR;
232 if( luaL_dostring( L, psz_command ) )
237 psz_config = config_GetPsz( p_intf, "lua-config" );
238 if( psz_config && *psz_config )
241 if( asprintf( &psz_buffer, "config={%s}", psz_config ) != -1 )
243 printf("%s\n", psz_buffer);
244 if( luaL_dostring( L, psz_buffer ) == 1 )
245 msg_Err( p_intf, "Error while parsing \"lua-config\"." );
247 lua_getglobal( L, "config" );
248 if( lua_istable( L, -1 ) )
250 lua_getfield( L, -1, psz_name );
251 if( lua_istable( L, -1 ) )
253 lua_setglobal( L, "config" );
259 if( b_config_set == false )
262 lua_setglobal( L, "config" );
267 p_intf->pf_run = Run;
268 p_intf->psz_header = strdup( psz_name ); /* Do I need to clean that up myself in Close_LuaIntf? */
274 void Close_LuaIntf( vlc_object_t *p_this )
276 intf_thread_t *p_intf = (intf_thread_t*)p_this;
278 lua_close( p_intf->p_sys->L );
279 free( p_intf->p_sys );
282 static void Run( intf_thread_t *p_intf )
284 lua_State *L = p_intf->p_sys->L;
286 if( luaL_dofile( L, p_intf->p_sys->psz_filename ) )
288 msg_Err( p_intf, "Error loading script %s: %s",
289 p_intf->p_sys->psz_filename,
290 lua_tostring( L, lua_gettop( L ) ) );
292 p_intf->b_die = true;
295 p_intf->b_die = true;