]> git.sesse.net Git - vlc/blob - modules/misc/lua/intf.c
Don't use printf to print debug output.
[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     size_t i_len = strlen( psz_word );
87
88     for( const char *s = psz_list; s; s = strchr( s, ',' ) )
89     {
90         if( !strncmp( s, psz_word, i_len )
91          && memchr( ",", s[i_len], 2 ) )
92             return s;
93     }
94     return NULL;
95 }
96
97 static char *GetModuleName( intf_thread_t *p_intf )
98 {
99     int i;
100     const char *psz_intf;
101     /*if( *p_intf->psz_intf == '$' )
102         psz_intf = var_GetString( p_intf, p_intf->psz_intf+1 );
103     else*/
104         psz_intf = p_intf->psz_intf;
105
106     int i_candidate = -1;
107     const char *psz_candidate = NULL;
108     for( i = 0; pp_shortcuts[i].psz_name; i++ )
109     {
110         const char *psz_match;
111         if( ( psz_match = WordInList( psz_intf, pp_shortcuts[i].psz_shortcut ) ) )
112         {
113             if( !psz_candidate || psz_match < psz_candidate )
114             {
115                 psz_candidate = psz_match;
116                 i_candidate = i;
117             }
118         }
119     }
120
121     if( i_candidate >= 0 )
122         return strdup( pp_shortcuts[i_candidate].psz_name );
123
124     return var_CreateGetString( p_intf, "lua-intf" );
125 }
126
127 static const luaL_Reg p_reg[] = { { NULL, NULL } };
128
129 int Open_LuaIntf( vlc_object_t *p_this )
130 {
131     intf_thread_t *p_intf = (intf_thread_t*)p_this;
132     intf_sys_t *p_sys;
133     lua_State *L;
134
135     config_ChainParse( p_intf, "lua-", ppsz_intf_options, p_intf->p_cfg );
136     char *psz_name = NULL;
137
138     if( !p_intf->psz_intf || !*p_intf->psz_intf )
139         psz_name = strdup( "rc" );
140     else
141         psz_name = GetModuleName( p_intf );
142
143     if( !psz_name ) psz_name = strdup( "dummy" );
144
145     char *psz_config;
146     bool b_config_set = false;
147
148     p_intf->p_sys = (intf_sys_t*)malloc( sizeof(intf_sys_t) );
149     if( !p_intf->p_sys )
150     {
151         free( psz_name );
152         return VLC_ENOMEM;
153     }
154     p_sys = p_intf->p_sys;
155     p_sys->psz_filename = vlclua_find_file( p_this, "intf", psz_name );
156     if( !p_sys->psz_filename )
157     {
158         msg_Err( p_intf, "Couldn't find lua interface script \"%s\".",
159                  psz_name );
160         goto error;
161     }
162     msg_Dbg( p_intf, "Found lua interface script: %s", p_sys->psz_filename );
163
164     L = luaL_newstate();
165     if( !L )
166     {
167         msg_Err( p_intf, "Could not create new Lua State" );
168         goto error;
169     }
170
171     vlclua_set_this( L, p_intf );
172
173     luaL_openlibs( L );
174
175     /* register our functions */
176     luaL_register( L, "vlc", p_reg );
177
178     /* register submodules */
179     luaopen_acl( L );
180     luaopen_config( L );
181     luaopen_volume( L );
182     luaopen_httpd( L );
183     luaopen_input( L );
184     luaopen_msg( L );
185     luaopen_misc( L );
186     luaopen_net( L );
187     luaopen_object( L );
188     luaopen_osd( L );
189     luaopen_playlist( L );
190     luaopen_sd( L );
191     luaopen_stream( L );
192     luaopen_strings( L );
193     luaopen_variables( L );
194     luaopen_video( L );
195     luaopen_vlm( L );
196     luaopen_volume( L );
197     luaopen_gettext( L );
198     luaopen_xml( L );
199     luaopen_md5( L );
200
201     /* clean up */
202     lua_pop( L, 1 );
203
204     /* Setup the module search path */
205     if( vlclua_add_modules_path( p_intf, L, p_sys->psz_filename ) )
206     {
207         msg_Warn( p_intf, "Error while setting the module search path for %s",
208                   p_sys->psz_filename );
209         lua_close( L );
210         goto error;
211     }
212
213     psz_config = var_CreateGetString( p_intf, "lua-config" );
214     if( psz_config && *psz_config )
215     {
216         char *psz_buffer;
217         if( asprintf( &psz_buffer, "config={%s}", psz_config ) != -1 )
218         {
219             msg_Dbg( p_intf, "Setting config variable: %s", psz_buffer );
220             if( luaL_dostring( L, psz_buffer ) == 1 )
221                 msg_Err( p_intf, "Error while parsing \"lua-config\"." );
222             free( psz_buffer );
223             lua_getglobal( L, "config" );
224             if( lua_istable( L, -1 ) )
225             {
226                 lua_getfield( L, -1, psz_name );
227                 if( lua_istable( L, -1 ) )
228                 {
229                     lua_setglobal( L, "config" );
230                     b_config_set = true;
231                 }
232             }
233         }
234     }
235     free( psz_config );
236
237     if( b_config_set == false )
238     {
239         lua_newtable( L );
240         lua_setglobal( L, "config" );
241     }
242
243     p_sys->L = L;
244
245     p_intf->psz_header = psz_name;
246     /* ^^ Do I need to clean that up myself in Close_LuaIntf? */
247
248     vlc_mutex_init( &p_sys->lock );
249     vlc_cond_init( &p_sys->wait );
250     p_sys->exiting = false;
251
252     if( vlc_clone( &p_sys->thread, Run, p_intf, VLC_THREAD_PRIORITY_LOW ) )
253     {
254         p_intf->psz_header = NULL;
255         vlc_cond_destroy( &p_sys->wait );
256         vlc_mutex_destroy( &p_sys->lock );
257         lua_close( p_sys->L );
258         goto error;
259     }
260
261     return VLC_SUCCESS;
262 error:
263     free( p_sys->psz_filename );
264     free( p_sys );
265     free( psz_name );
266     return VLC_EGENERIC;
267 }
268
269 void Close_LuaIntf( vlc_object_t *p_this )
270 {
271     intf_thread_t *p_intf = (intf_thread_t*)p_this;
272     intf_sys_t *p_sys = p_intf->p_sys;
273
274     vlc_cancel( p_sys->thread );
275
276     vlc_mutex_lock( &p_sys->lock );
277     p_sys->exiting = true;
278     vlc_cond_signal( &p_sys->wait );
279     vlc_mutex_unlock( &p_sys->lock );
280     vlc_join( p_sys->thread, NULL );
281     vlc_cond_destroy( &p_sys->wait );
282     vlc_mutex_destroy( &p_sys->lock );
283
284     lua_close( p_sys->L );
285
286     free( p_sys->psz_filename );
287     free( p_sys );
288 }
289
290 static void *Run( void *data )
291 {
292     intf_thread_t *p_intf = data;
293     intf_sys_t *p_sys = p_intf->p_sys;
294     lua_State *L = p_sys->L;
295
296     if( luaL_dofile( L, p_sys->psz_filename ) )
297     {
298         msg_Err( p_intf, "Error loading script %s: %s", p_sys->psz_filename,
299                  lua_tostring( L, lua_gettop( L ) ) );
300         lua_pop( L, 1 );
301     }
302     return NULL;
303 }