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