]> git.sesse.net Git - vlc/blob - modules/misc/lua/intf.c
lua_intf: also provide the --rc-host option for backward compatibility.
[vlc] / modules / misc / lua / intf.c
1 /*****************************************************************************
2  * intf.c: Generic lua interface functions
3  *****************************************************************************
4  * Copyright (C) 2007-2008 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Antoine Cellerier <dionoea at videolan tod org>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #ifndef  _GNU_SOURCE
28 #   define  _GNU_SOURCE
29 #endif
30
31 #ifdef HAVE_CONFIG_H
32 # include "config.h"
33 #endif
34
35 #include <vlc_common.h>
36 #include <vlc_meta.h>
37
38 #include <vlc_interface.h>
39 #include <vlc_aout.h>
40 #include <sys/types.h>
41 #include <sys/stat.h>
42
43 #include <lua.h>        /* Low level lua C API */
44 #include <lauxlib.h>    /* Higher level C API */
45 #include <lualib.h>     /* Lua libs */
46
47 #include "vlc.h"
48 #include "libs.h"
49
50 /*****************************************************************************
51  * Prototypes
52  *****************************************************************************/
53 static void *Run( void * );
54
55 static const char * const ppsz_intf_options[] = { "intf", "config", NULL };
56
57 /*****************************************************************************
58  *
59  *****************************************************************************/
60 static inline void luaL_register_submodule( lua_State *L, const char *psz_name,
61                                             const luaL_Reg *l )
62 {
63     lua_newtable( L );
64     luaL_register( L, NULL, l );
65     lua_setfield( L, -2, psz_name );
66 }
67
68 static const struct
69 {
70     const char *psz_shortcut;
71     const char *psz_name;
72 } pp_shortcuts[] = {
73     { "luarc", "rc" },
74     { "rc", "rc" },
75     { "luahotkeys", "hotkeys" },
76     /* { "hotkeys", "hotkeys" }, */
77     { "luatelnet", "telnet" },
78     { "telnet", "telnet" },
79     { "luahttp", "http" },
80     /* { "http", "http" }, */
81     { NULL, NULL } };
82
83 static const char *WordInList( const char *psz_list, const char *psz_word )
84 {
85     for( ;; )
86     {
87         const char *end = strchr( psz_list, ',' );
88         if( end == NULL )
89             break;
90
91         if( !strncmp( psz_list, psz_word, end - psz_list ) )
92             return psz_list;
93         psz_list = end + 1;
94     }
95     return !strcmp( psz_list, psz_word ) ? psz_list : NULL;
96 }
97
98 static char *GetModuleName( intf_thread_t *p_intf )
99 {
100     int i;
101     const char *psz_intf;
102     /*if( *p_intf->psz_intf == '$' )
103         psz_intf = var_GetString( p_intf, p_intf->psz_intf+1 );
104     else*/
105         psz_intf = p_intf->psz_intf;
106
107     int i_candidate = -1;
108     const char *psz_candidate = NULL;
109     for( i = 0; pp_shortcuts[i].psz_name; i++ )
110     {
111         const char *psz_match;
112         if( ( psz_match = WordInList( psz_intf, pp_shortcuts[i].psz_shortcut ) ) )
113         {
114             if( !psz_candidate || psz_match < psz_candidate )
115             {
116                 psz_candidate = psz_match;
117                 i_candidate = i;
118             }
119         }
120     }
121
122     if( i_candidate >= 0 )
123         return strdup( pp_shortcuts[i_candidate].psz_name );
124
125     return var_CreateGetString( p_intf, "lua-intf" );
126 }
127
128 static const luaL_Reg p_reg[] = { { NULL, NULL } };
129
130 int Open_LuaIntf( vlc_object_t *p_this )
131 {
132     intf_thread_t *p_intf = (intf_thread_t*)p_this;
133     intf_sys_t *p_sys;
134     lua_State *L;
135
136     config_ChainParse( p_intf, "lua-", ppsz_intf_options, p_intf->p_cfg );
137     char *psz_name = NULL;
138
139     if( !p_intf->psz_intf || !*p_intf->psz_intf )
140         psz_name = strdup( "rc" );
141     else
142         psz_name = GetModuleName( p_intf );
143
144     if( !psz_name ) psz_name = strdup( "dummy" );
145
146     char *psz_config;
147     bool b_config_set = false;
148
149     p_intf->p_sys = (intf_sys_t*)malloc( sizeof(intf_sys_t) );
150     if( !p_intf->p_sys )
151     {
152         free( psz_name );
153         return VLC_ENOMEM;
154     }
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 )
158     {
159         msg_Err( p_intf, "Couldn't find lua interface script \"%s\".",
160                  psz_name );
161         goto error;
162     }
163     msg_Dbg( p_intf, "Found lua interface script: %s", p_sys->psz_filename );
164
165     L = luaL_newstate();
166     if( !L )
167     {
168         msg_Err( p_intf, "Could not create new Lua State" );
169         goto error;
170     }
171
172     vlclua_set_this( L, p_intf );
173     vlclua_set_intf( L, p_sys );
174
175     luaL_openlibs( L );
176
177     /* register our functions */
178     luaL_register( L, "vlc", p_reg );
179
180     /* register submodules */
181     luaopen_acl( L );
182     luaopen_config( L );
183     luaopen_volume( L );
184     luaopen_httpd( L );
185     luaopen_input( L );
186     luaopen_msg( L );
187     luaopen_misc( L );
188     luaopen_net( L );
189     luaopen_object( L );
190     luaopen_osd( L );
191     luaopen_playlist( L );
192     luaopen_sd( L );
193     luaopen_stream( L );
194     luaopen_strings( L );
195     luaopen_variables( L );
196     luaopen_video( L );
197     luaopen_vlm( L );
198     luaopen_volume( L );
199     luaopen_gettext( L );
200     luaopen_xml( L );
201     luaopen_md5( L );
202
203     /* clean up */
204     lua_pop( L, 1 );
205
206     /* Setup the module search path */
207     if( vlclua_add_modules_path( p_intf, L, p_sys->psz_filename ) )
208     {
209         msg_Warn( p_intf, "Error while setting the module search path for %s",
210                   p_sys->psz_filename );
211         lua_close( L );
212         goto error;
213     }
214
215     /*
216      * Get the lua-config string.
217      * If the string is empty, try with the old http-* or telnet-* options
218      * and build the right configuration line
219      */
220     psz_config = var_CreateGetNonEmptyString( p_intf, "lua-config" );
221     if( !psz_config )
222     {
223         if( !strcmp( psz_name, "http" ) )
224         {
225             char *psz_http_host = var_CreateGetNonEmptyString( p_intf, "http-host" );
226             char *psz_http_src = var_CreateGetNonEmptyString( p_intf, "http-src" );
227             bool b_http_index = var_CreateGetBool( p_intf, "http-index" );
228             if( psz_http_host )
229             {
230                 char *psz_esc = config_StringEscape( psz_http_host );
231                 asprintf( &psz_config, "http={host='%s'", psz_esc );
232                 free( psz_esc );
233                 free( psz_http_host );
234             }
235             if( psz_http_src )
236             {
237                 char *psz_esc = config_StringEscape( psz_http_src );
238                 if( psz_config )
239                 {
240                     char *psz_tmp;
241                     asprintf( &psz_tmp, "%s,dir='%s'", psz_config, psz_esc );
242                     free( psz_config );
243                     psz_config = psz_tmp;
244                 }
245                 else
246                     asprintf( &psz_config, "http={dir='%s'", psz_esc );
247                 free( psz_esc );
248                 free( psz_http_src );
249             }
250             if( psz_config )
251             {
252                 char *psz_tmp;
253                 asprintf( &psz_tmp, "%s,no_index=%s}", psz_config, b_http_index ? "true" : "false" );
254                 free( psz_config );
255                 psz_config = psz_tmp;
256             }
257             else
258                 asprintf( &psz_config, "http={no_index=%s}", b_http_index ? "true" : "false" );
259         }
260         else if( !strcmp( psz_name, "telnet" ) )
261         {
262             char *psz_telnet_host = var_CreateGetString( p_intf, "telnet-host" );
263             int i_telnet_port = var_CreateGetInteger( p_intf, "telnet-port" );
264             char *psz_telnet_passwd = var_CreateGetString( p_intf, "telnet-password" );
265
266             char *psz_esc_host = config_StringEscape( psz_telnet_host );
267             char *psz_esc_passwd = config_StringEscape( psz_telnet_passwd );
268
269             asprintf( &psz_config, "telnet={host='%s:%d',password='%s'}", psz_esc_host ? psz_esc_host : "", i_telnet_port, psz_esc_passwd );
270
271             free( psz_esc_host );
272             free( psz_esc_passwd );
273             free( psz_telnet_passwd );
274             free( psz_telnet_host );
275         }
276         else if( !strcmp( psz_name, "rc" ) )
277         {
278             char *psz_rc_host = var_CreateGetNonEmptyString( p_intf, "rc-host" );
279             if( psz_rc_host )
280             {
281                 char *psz_esc_host = config_StringEscape( psz_rc_host );
282                 asprintf( &psz_config, "rc={host='%s'}", psz_esc_host );
283
284                 free( psz_esc_host );
285                 free( psz_rc_host );
286             }
287         }
288     }
289
290     if( psz_config )
291     {
292         char *psz_buffer;
293         if( asprintf( &psz_buffer, "config={%s}", psz_config ) != -1 )
294         {
295             msg_Dbg( p_intf, "Setting config variable: %s", psz_buffer );
296             if( luaL_dostring( L, psz_buffer ) == 1 )
297                 msg_Err( p_intf, "Error while parsing \"lua-config\"." );
298             free( psz_buffer );
299             lua_getglobal( L, "config" );
300             if( lua_istable( L, -1 ) )
301             {
302                 lua_getfield( L, -1, psz_name );
303                 if( lua_istable( L, -1 ) )
304                 {
305                     lua_setglobal( L, "config" );
306                     b_config_set = true;
307                 }
308             }
309         }
310         free( psz_config );
311     }
312
313     if( b_config_set == false )
314     {
315         lua_newtable( L );
316         lua_setglobal( L, "config" );
317     }
318
319     p_sys->L = L;
320
321     p_intf->psz_header = psz_name;
322     /* ^^ Do I need to clean that up myself in Close_LuaIntf? */
323
324     vlc_mutex_init( &p_sys->lock );
325     vlc_cond_init( &p_sys->wait );
326     p_sys->exiting = false;
327
328     if( vlc_clone( &p_sys->thread, Run, p_intf, VLC_THREAD_PRIORITY_LOW ) )
329     {
330         p_intf->psz_header = NULL;
331         vlc_cond_destroy( &p_sys->wait );
332         vlc_mutex_destroy( &p_sys->lock );
333         lua_close( p_sys->L );
334         goto error;
335     }
336
337     return VLC_SUCCESS;
338 error:
339     free( p_sys->psz_filename );
340     free( p_sys );
341     free( psz_name );
342     return VLC_EGENERIC;
343 }
344
345 void Close_LuaIntf( vlc_object_t *p_this )
346 {
347     intf_thread_t *p_intf = (intf_thread_t*)p_this;
348     intf_sys_t *p_sys = p_intf->p_sys;
349
350     vlc_cancel( p_sys->thread );
351
352     vlc_mutex_lock( &p_sys->lock );
353     p_sys->exiting = true;
354     vlc_cond_signal( &p_sys->wait );
355     vlc_mutex_unlock( &p_sys->lock );
356     vlc_join( p_sys->thread, NULL );
357     vlc_cond_destroy( &p_sys->wait );
358     vlc_mutex_destroy( &p_sys->lock );
359
360     lua_close( p_sys->L );
361
362     free( p_sys->psz_filename );
363     free( p_sys );
364 }
365
366 static void *Run( void *data )
367 {
368     intf_thread_t *p_intf = data;
369     intf_sys_t *p_sys = p_intf->p_sys;
370     lua_State *L = p_sys->L;
371
372     if( luaL_dofile( L, p_sys->psz_filename ) )
373     {
374         msg_Err( p_intf, "Error loading script %s: %s", p_sys->psz_filename,
375                  lua_tostring( L, lua_gettop( L ) ) );
376         lua_pop( L, 1 );
377     }
378     return NULL;
379 }