]> git.sesse.net Git - vlc/blob - modules/misc/lua/intf.c
70e590a2c7fe121390707d995e60290a7e146305
[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 #include <sys/types.h>
43 #include <sys/stat.h>
44
45 #include <lua.h>        /* Low level lua C API */
46 #include <lauxlib.h>    /* Higher level C API */
47 #include <lualib.h>     /* Lua libs */
48
49 #include "vlc.h"
50 #include "libs.h"
51
52 /*****************************************************************************
53  * Prototypes
54  *****************************************************************************/
55 static void *Run( void * );
56
57 static const char * const ppsz_intf_options[] = { "intf", "config", NULL };
58
59 /*****************************************************************************
60  *
61  *****************************************************************************/
62 static char *FindFile( vlc_object_t *p_this, const char *psz_name )
63 {
64     char  *ppsz_dir_list[] = { NULL, NULL, NULL, NULL };
65     char **ppsz_dir;
66     vlclua_dir_list( p_this, "intf", ppsz_dir_list );
67     for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
68     {
69         char *psz_filename;
70         struct stat st;
71
72         if( asprintf( &psz_filename, "%s"DIR_SEP"%s.lua", *ppsz_dir,
73                       psz_name ) < 0 )
74         {
75             vlclua_dir_list_free( ppsz_dir_list );
76             return NULL;
77         }
78
79         if( utf8_stat( psz_filename, &st ) == 0
80          && S_ISREG( st.st_mode ) )
81         {
82             vlclua_dir_list_free( ppsz_dir_list );
83             return psz_filename;
84         }
85         free( psz_filename );
86     }
87     vlclua_dir_list_free( ppsz_dir_list );
88     return NULL;
89 }
90
91 static inline void luaL_register_submodule( lua_State *L, const char *psz_name,
92                                             const luaL_Reg *l )
93 {
94     lua_newtable( L );
95     luaL_register( L, NULL, l );
96     lua_setfield( L, -2, psz_name );
97 }
98
99 static const struct
100 {
101     const char *psz_shortcut;
102     const char *psz_name;
103 } pp_shortcuts[] = {
104     { "luarc", "rc" },
105     { "rc", "rc" },
106     { "luahotkeys", "hotkeys" },
107     /* { "hotkeys", "hotkeys" }, */
108     { "luatelnet", "telnet" },
109     { "telnet", "telnet" },
110     { "luahttp", "http" },
111     { "http", "http" },
112     { NULL, NULL } };
113
114 static const char *WordInList( const char *psz_list, const char *psz_word )
115 {
116     size_t i_len = strlen( psz_word );
117
118     for( const char *s = psz_list; s; s = strchr( s, ',' ) )
119     {
120         if( !strncmp( s, psz_word, i_len )
121          && memchr( ",", s[i_len], 2 ) )
122             return s;
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->psz_intf || !*p_intf->psz_intf )
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         goto error;
191     }
192     msg_Dbg( p_intf, "Found lua interface script: %s", p_sys->psz_filename );
193
194     L = luaL_newstate();
195     if( !L )
196     {
197         msg_Err( p_intf, "Could not create new Lua State" );
198         goto error;
199     }
200
201     luaL_openlibs( L );
202
203     /* register our functions */
204     luaL_register( L, "vlc", p_reg );
205
206     /* store a pointer to p_intf (FIXME: user could overwrite this) */
207     lua_pushlightuserdata( L, p_intf );
208     lua_setfield( L, -2, "private" );
209
210     /* register submodules */
211     luaopen_acl( L );
212     luaopen_config( L );
213     luaopen_volume( L );
214     luaopen_httpd( L );
215     luaopen_input( L );
216     luaopen_msg( L );
217     luaopen_misc( L );
218     luaopen_net( L );
219     luaopen_object( L );
220     luaopen_osd( L );
221     luaopen_playlist( L );
222     luaopen_sd( L );
223     luaopen_stream( L );
224     luaopen_strings( L );
225     luaopen_variables( L );
226     luaopen_video( L );
227     luaopen_vlm( L );
228     luaopen_volume( L );
229     luaopen_gettext( L );
230
231     /* clean up */
232     lua_pop( L, 1 );
233
234     /* <gruik> */
235     /* Setup the module search path */
236     {
237     char *psz_command;
238     char *psz_char = strrchr(p_sys->psz_filename,DIR_SEP_CHAR);
239     *psz_char = '\0';
240     /* FIXME: don't use luaL_dostring */
241     if( asprintf( &psz_command,
242                   "package.path = [[%s"DIR_SEP"modules"DIR_SEP"?.lua;]]..package.path",
243                   p_sys->psz_filename ) < 0 )
244     {
245         goto error;
246     }
247     *psz_char = DIR_SEP_CHAR;
248     if( luaL_dostring( L, psz_command ) )
249     {
250         free( psz_command );
251         goto error;
252     }
253     free( psz_command );
254     }
255     /* </gruik> */
256
257     psz_config = var_CreateGetString( p_intf, "lua-config" );
258     if( psz_config && *psz_config )
259     {
260         char *psz_buffer;
261         if( asprintf( &psz_buffer, "config={%s}", psz_config ) != -1 )
262         {
263             printf("%s\n", psz_buffer);
264             if( luaL_dostring( L, psz_buffer ) == 1 )
265                 msg_Err( p_intf, "Error while parsing \"lua-config\"." );
266             free( psz_buffer );
267             lua_getglobal( L, "config" );
268             if( lua_istable( L, -1 ) )
269             {
270                 lua_getfield( L, -1, psz_name );
271                 if( lua_istable( L, -1 ) )
272                 {
273                     lua_setglobal( L, "config" );
274                     b_config_set = true;
275                 }
276             }
277         }
278     }
279     free( psz_config );
280
281     if( b_config_set == false )
282     {
283         lua_newtable( L );
284         lua_setglobal( L, "config" );
285     }
286
287     p_sys->L = L;
288
289     p_intf->psz_header = psz_name;
290     /* ^^ Do I need to clean that up myself in Close_LuaIntf? */
291
292     vlc_mutex_init( &p_sys->lock );
293     vlc_cond_init( &p_sys->wait );
294     p_sys->exiting = false;
295
296     if( vlc_clone( &p_sys->thread, Run, p_intf, VLC_THREAD_PRIORITY_LOW ) )
297     {
298         p_intf->psz_header = NULL;
299         vlc_cond_destroy( &p_sys->wait );
300         vlc_mutex_destroy( &p_sys->lock );
301         lua_close( p_sys->L );
302         goto error;
303     }
304
305     return VLC_SUCCESS;
306 error:
307     free( p_sys->psz_filename );
308     free( p_sys );
309     free( psz_name );
310     return VLC_EGENERIC;
311 }
312
313 void Close_LuaIntf( vlc_object_t *p_this )
314 {
315     intf_thread_t *p_intf = (intf_thread_t*)p_this;
316     intf_sys_t *p_sys = p_intf->p_sys;
317
318     vlc_cancel( p_sys->thread );
319
320     vlc_mutex_lock( &p_sys->lock );
321     p_sys->exiting = true;
322     vlc_cond_signal( &p_sys->wait );
323     vlc_mutex_unlock( &p_sys->lock );
324     vlc_join( p_sys->thread, NULL );
325     vlc_cond_destroy( &p_sys->wait );
326     vlc_mutex_destroy( &p_sys->lock );
327
328     lua_close( p_sys->L );
329
330     free( p_sys->psz_filename );
331     free( p_sys );
332 }
333
334 static void *Run( void *data )
335 {
336     intf_thread_t *p_intf = data;
337     intf_sys_t *p_sys = p_intf->p_sys;
338     lua_State *L = p_sys->L;
339
340     if( luaL_dofile( L, p_sys->psz_filename ) )
341     {
342         msg_Err( p_intf, "Error loading script %s: %s", p_sys->psz_filename,
343                  lua_tostring( L, lua_gettop( L ) ) );
344         lua_pop( L, 1 );
345     }
346     return NULL;
347 }