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