]> git.sesse.net Git - vlc/blob - modules/misc/lua/intf.c
4cf530241f694dd3984c8bca08721b1b7ca023f7
[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 struct intf_sys_t
54 {
55     char *psz_filename;
56     lua_State *L;
57 };
58
59 static void Run( intf_thread_t *p_intf );
60
61 /*****************************************************************************
62  *
63  *****************************************************************************/
64 static char *FindFile( const char *psz_name )
65 {
66     char  *ppsz_dir_list[] = { NULL, NULL, NULL, NULL };
67     char **ppsz_dir;
68     vlclua_dir_list( "intf", ppsz_dir_list );
69     for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
70     {
71         char *psz_filename;
72         FILE *fp;
73         if( asprintf( &psz_filename, "%s"DIR_SEP"%s.lua", *ppsz_dir,
74                       psz_name ) < 0 )
75         {
76             return NULL;
77         }
78         fp = fopen( psz_filename, "r" );
79         if( fp )
80         {
81             fclose( fp );
82             return psz_filename;
83         }
84         free( psz_filename );
85     }
86     return NULL;
87 }
88
89 static inline void luaL_register_submodule( lua_State *L, const char *psz_name,
90                                             const luaL_Reg *l )
91 {
92     lua_newtable( L );
93     luaL_register( L, NULL, l );
94     lua_setfield( L, -2, psz_name );
95 }
96
97 static struct
98 {
99     const char *psz_shortcut;
100     const char *psz_name;
101 } pp_shortcuts[] = {
102     { "luarc", "rc" },
103     /* { "rc", "rc" }, */
104     { "luahotkeys", "hotkeys" },
105     /* { "hotkeys", "hotkeys" }, */
106     { "luatelnet", "telnet" },
107     /* { "telnet", "telnet" }, */
108     { "luahttp", "http" },
109     /* { "http", "http" }, */
110     { NULL, NULL } };
111
112 static bool WordInList( const char *psz_list, const char *psz_word )
113 {
114     const char *psz_str = strstr( psz_list, psz_word );
115     int i_len = strlen( psz_word );
116     while( psz_str )
117     {
118         if( (psz_str == psz_list || *(psz_str-1) == ',' )
119          /* it doesn't start in middle of a word */
120          /* it doest end in middle of a word */
121          && ( psz_str[i_len] == '\0' || psz_str[i_len] == ',' ) )
122             return true;
123         psz_str = strstr( psz_str, psz_word );
124     }
125     return false;
126 }
127
128 static const char *GetModuleName( intf_thread_t *p_intf )
129 {
130     int i;
131     const char *psz_intf;
132     if( *p_intf->psz_intf == '$' )
133         psz_intf = var_GetString( p_intf, p_intf->psz_intf+1 );
134     else
135         psz_intf = p_intf->psz_intf;
136     for( i = 0; pp_shortcuts[i].psz_name; i++ )
137     {
138         if( WordInList( psz_intf, pp_shortcuts[i].psz_shortcut ) )
139             return pp_shortcuts[i].psz_name;
140     }
141
142     return config_GetPsz( p_intf, "lua-intf" );
143 }
144
145 static luaL_Reg p_reg[] = { { NULL, NULL } };
146
147 int Open_LuaIntf( vlc_object_t *p_this )
148 {
149     intf_thread_t *p_intf = (intf_thread_t*)p_this;
150     intf_sys_t *p_sys;
151     lua_State *L;
152
153     const char *psz_name = GetModuleName( p_intf );
154     const char *psz_config;
155     bool b_config_set = false;
156     if( !psz_name ) psz_name = "dummy";
157
158     p_intf->p_sys = (intf_sys_t*)malloc( sizeof(intf_sys_t*) );
159     if( !p_intf->p_sys )
160     {
161         return VLC_ENOMEM;
162     }
163     p_sys = p_intf->p_sys;
164     p_sys->psz_filename = FindFile( psz_name );
165     if( !p_sys->psz_filename )
166     {
167         msg_Err( p_intf, "Couldn't find lua interface script \"%s\".",
168                  psz_name );
169         return VLC_EGENERIC;
170     }
171     msg_Dbg( p_intf, "Found lua interface script: %s", p_sys->psz_filename );
172
173     L = luaL_newstate();
174     if( !L )
175     {
176         msg_Err( p_intf, "Could not create new Lua State" );
177         free( p_sys );
178         return VLC_EGENERIC;
179     }
180
181     luaL_openlibs( L );
182
183     /* register our functions */
184     luaL_register( L, "vlc", p_reg );
185
186     /* store a pointer to p_intf (FIXME: user could overwrite this) */
187     lua_pushlightuserdata( L, p_intf );
188     lua_setfield( L, -2, "private" );
189
190     /* register submodules */
191     luaopen_acl( L );
192     luaopen_config( L );
193     luaopen_volume( L );
194     luaopen_httpd( L );
195     luaopen_input( L );
196     luaopen_msg( L );
197     luaopen_misc( L );
198     luaopen_net( L );
199     luaopen_object( L );
200     luaopen_osd( L );
201     luaopen_playlist( L );
202     luaopen_sd( L );
203     luaopen_stream( L );
204     luaopen_strings( L );
205     luaopen_variables( L );
206     luaopen_video( L );
207     luaopen_vlm( L );
208     luaopen_volume( L );
209
210     /* clean up */
211     lua_pop( L, 1 );
212
213     /* <gruik> */
214     /* Setup the module search path */
215     {
216     char *psz_command;
217     char *psz_char = strrchr(p_sys->psz_filename,DIR_SEP_CHAR);
218     *psz_char = '\0';
219     /* FIXME: don't use luaL_dostring */
220     if( asprintf( &psz_command,
221                   "package.path = \"%s"DIR_SEP"modules"DIR_SEP"?.lua;\"..package.path",
222                   p_sys->psz_filename ) < 0 )
223         return VLC_EGENERIC;
224     *psz_char = DIR_SEP_CHAR;
225     if( luaL_dostring( L, psz_command ) )
226         return VLC_EGENERIC;
227     }
228     /* </gruik> */
229
230     psz_config = config_GetPsz( p_intf, "lua-config" );
231     if( psz_config && *psz_config )
232     {
233         char *psz_buffer;
234         if( asprintf( &psz_buffer, "config={%s}", psz_config ) != -1 )
235         {
236             printf("%s\n", psz_buffer);
237             if( luaL_dostring( L, psz_buffer ) == 1 )
238                 msg_Err( p_intf, "Error while parsing \"lua-config\"." );
239             free( psz_buffer );
240             lua_getglobal( L, "config" );
241             if( lua_istable( L, -1 ) )
242             {
243                 lua_getfield( L, -1, psz_name );
244                 if( lua_istable( L, -1 ) )
245                 {
246                     lua_setglobal( L, "config" );
247                     b_config_set = true;
248                 }
249             }
250         }
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->pf_run = Run;
261     p_intf->psz_header = strdup( psz_name ); /* Do I need to clean that up myself in Close_LuaIntf? */
262
263     return VLC_SUCCESS;
264 }
265
266 void Close_LuaIntf( vlc_object_t *p_this )
267 {
268     intf_thread_t *p_intf = (intf_thread_t*)p_this;
269
270     lua_close( p_intf->p_sys->L );
271     free( p_intf->p_sys );
272 }
273
274 static void Run( intf_thread_t *p_intf )
275 {
276     lua_State *L = p_intf->p_sys->L;
277
278     if( luaL_dofile( L, p_intf->p_sys->psz_filename ) )
279     {
280         msg_Err( p_intf, "Error loading script %s: %s",
281                  p_intf->p_sys->psz_filename,
282                  lua_tostring( L, lua_gettop( L ) ) );
283         lua_pop( L, 1 );
284         p_intf->b_die = true;
285         return;
286     }
287     p_intf->b_die = true;
288 }