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