]> git.sesse.net Git - vlc/blob - modules/misc/lua/intf.c
2dedafd6538a39e9d0a9e02b336bd13b27f01a6b
[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     const char *psz_str = strstr( psz_list, psz_word );
114     int i_len = strlen( psz_word );
115     while( psz_str )
116     {
117         if( (psz_str == psz_list || *(psz_str-1) == ',' )
118          /* it doesn't start in middle of a word */
119          /* it doest end in middle of a word */
120          && ( psz_str[i_len] == '\0' || psz_str[i_len] == ',' ) )
121             return psz_str;
122         psz_str = strstr( psz_str, psz_word );
123     }
124     return NULL;
125 }
126
127 static char *GetModuleName( intf_thread_t *p_intf )
128 {
129     int i;
130     const char *psz_intf;
131     /*if( *p_intf->psz_intf == '$' )
132         psz_intf = var_GetString( p_intf, p_intf->psz_intf+1 );
133     else*/
134         psz_intf = p_intf->psz_intf;
135
136     int i_candidate = -1;
137     const char *psz_candidate = NULL;
138     for( i = 0; pp_shortcuts[i].psz_name; i++ )
139     {
140         const char *psz_match;
141         if( ( psz_match = WordInList( psz_intf, pp_shortcuts[i].psz_shortcut ) ) )
142         {
143             if( !psz_candidate || psz_match < psz_candidate )
144             {
145                 psz_candidate = psz_match;
146                 i_candidate = i;
147             }
148         }
149     }
150
151     if( i_candidate >= 0 )
152         return strdup( pp_shortcuts[i_candidate].psz_name );
153
154     return var_CreateGetString( p_intf, "lua-intf" );
155 }
156
157 static const luaL_Reg p_reg[] = { { NULL, NULL } };
158
159 int Open_LuaIntf( vlc_object_t *p_this )
160 {
161     intf_thread_t *p_intf = (intf_thread_t*)p_this;
162     intf_sys_t *p_sys;
163     lua_State *L;
164
165     config_ChainParse( p_intf, "lua-", ppsz_intf_options, p_intf->p_cfg );
166     char *psz_name = NULL;
167
168     if( !p_intf->b_force )
169         psz_name = strdup( "rc" );
170     else
171         psz_name = GetModuleName( p_intf );
172
173     if( !psz_name ) psz_name = strdup( "dummy" );
174
175     char *psz_config;
176     bool b_config_set = false;
177
178     p_intf->p_sys = (intf_sys_t*)malloc( sizeof(intf_sys_t) );
179     if( !p_intf->p_sys )
180     {
181         free( psz_name );
182         return VLC_ENOMEM;
183     }
184     p_sys = p_intf->p_sys;
185     p_sys->psz_filename = FindFile( p_this, psz_name );
186     if( !p_sys->psz_filename )
187     {
188         msg_Err( p_intf, "Couldn't find lua interface script \"%s\".",
189                  psz_name );
190         free( psz_name );
191         free( p_sys );
192         return VLC_EGENERIC;
193     }
194     msg_Dbg( p_intf, "Found lua interface script: %s", p_sys->psz_filename );
195
196     L = luaL_newstate();
197     if( !L )
198     {
199         msg_Err( p_intf, "Could not create new Lua State" );
200         free( p_sys->psz_filename );
201         free( psz_name );
202         free( p_sys );
203         return VLC_EGENERIC;
204     }
205
206     luaL_openlibs( L );
207
208     /* register our functions */
209     luaL_register( L, "vlc", p_reg );
210
211     /* store a pointer to p_intf (FIXME: user could overwrite this) */
212     lua_pushlightuserdata( L, p_intf );
213     lua_setfield( L, -2, "private" );
214
215     /* register submodules */
216     luaopen_acl( L );
217     luaopen_config( L );
218     luaopen_volume( L );
219     luaopen_httpd( L );
220     luaopen_input( L );
221     luaopen_msg( L );
222     luaopen_misc( L );
223     luaopen_net( L );
224     luaopen_object( L );
225     luaopen_osd( L );
226     luaopen_playlist( L );
227     luaopen_sd( L );
228     luaopen_stream( L );
229     luaopen_strings( L );
230     luaopen_variables( L );
231     luaopen_video( L );
232     luaopen_vlm( L );
233     luaopen_volume( L );
234     luaopen_gettext( L );
235
236     /* clean up */
237     lua_pop( L, 1 );
238
239     /* <gruik> */
240     /* Setup the module search path */
241     {
242     char *psz_command;
243     char *psz_char = strrchr(p_sys->psz_filename,DIR_SEP_CHAR);
244     *psz_char = '\0';
245     /* FIXME: don't use luaL_dostring */
246     if( asprintf( &psz_command,
247                   "package.path = \"%s"DIR_SEP"modules"DIR_SEP"?.lua;\"..package.path",
248                   p_sys->psz_filename ) < 0 )
249     {
250         free( p_sys->psz_filename );
251         free( psz_name );
252         free( p_sys );
253         return VLC_EGENERIC;
254     }
255     *psz_char = DIR_SEP_CHAR;
256     if( luaL_dostring( L, psz_command ) )
257     {
258         free( psz_command );
259         free( p_sys->psz_filename );
260         free( psz_name );
261         free( p_sys );
262         return VLC_EGENERIC;
263     }
264     free( psz_command );
265     }
266     /* </gruik> */
267
268     psz_config = var_CreateGetString( p_intf, "lua-config" );
269     if( psz_config && *psz_config )
270     {
271         char *psz_buffer;
272         if( asprintf( &psz_buffer, "config={%s}", psz_config ) != -1 )
273         {
274             printf("%s\n", psz_buffer);
275             if( luaL_dostring( L, psz_buffer ) == 1 )
276                 msg_Err( p_intf, "Error while parsing \"lua-config\"." );
277             free( psz_buffer );
278             lua_getglobal( L, "config" );
279             if( lua_istable( L, -1 ) )
280             {
281                 lua_getfield( L, -1, psz_name );
282                 if( lua_istable( L, -1 ) )
283                 {
284                     lua_setglobal( L, "config" );
285                     b_config_set = true;
286                 }
287             }
288         }
289     }
290     free( psz_config );
291
292     if( b_config_set == false )
293     {
294         lua_newtable( L );
295         lua_setglobal( L, "config" );
296     }
297
298     p_sys->L = L;
299
300     p_intf->psz_header = psz_name;
301     /* ^^ Do I need to clean that up myself in Close_LuaIntf? */
302
303     vlc_mutex_init( &p_sys->lock );
304     vlc_cond_init( &p_sys->wait );
305     p_sys->exiting = false;
306
307     if( vlc_clone( &p_sys->thread, Run, p_intf, VLC_THREAD_PRIORITY_LOW ) )
308     {
309         p_sys->exiting = true;
310         Close_LuaIntf( p_this );
311         return VLC_ENOMEM;
312     }
313
314     return VLC_SUCCESS;
315 }
316
317 void Close_LuaIntf( vlc_object_t *p_this )
318 {
319     intf_thread_t *p_intf = (intf_thread_t*)p_this;
320     intf_sys_t *p_sys = p_intf->p_sys;
321
322     vlc_cancel( p_sys->thread );
323
324     if( !p_sys->exiting ) /* <- Read-only here and in thread: no locking */
325     {
326         vlc_mutex_lock( &p_sys->lock );
327         p_sys->exiting = true;
328         vlc_cond_signal( &p_sys->wait );
329         vlc_mutex_unlock( &p_sys->lock );
330         vlc_join( p_sys->thread, NULL );
331     }
332     vlc_cond_destroy( &p_sys->wait );
333     vlc_mutex_destroy( &p_sys->lock );
334
335     lua_close( p_sys->L );
336
337     free( p_sys->psz_filename );
338     free( p_sys );
339 }
340
341 static void *Run( void *data )
342 {
343     intf_thread_t *p_intf = data;
344     intf_sys_t *p_sys = p_intf->p_sys;
345     lua_State *L = p_sys->L;
346
347     if( luaL_dofile( L, p_sys->psz_filename ) )
348     {
349         msg_Err( p_intf, "Error loading script %s: %s", p_sys->psz_filename,
350                  lua_tostring( L, lua_gettop( L ) ) );
351         lua_pop( L, 1 );
352     }
353     return NULL;
354 }