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