]> git.sesse.net Git - vlc/blob - modules/misc/lua/playlist.c
Remove E_()
[vlc] / modules / misc / lua / playlist.c
1 /*****************************************************************************
2  * playlist.c :  Lua playlist demux module
3  *****************************************************************************
4  * Copyright (C) 2007 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 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <vlc/vlc.h>
32 #include <vlc_demux.h>
33 #include <vlc_url.h>
34 #include <vlc_strings.h>
35 #include <vlc_charset.h>
36
37 #include <errno.h>                                                 /* ENOMEM */
38 #ifdef HAVE_SYS_STAT_H
39 #   include <sys/stat.h>
40 #endif
41
42 #include "vlc.h"
43
44
45 /*****************************************************************************
46  * Local prototypes
47  *****************************************************************************/
48 static int Demux( demux_t *p_demux );
49 static int Control( demux_t *p_demux, int i_query, va_list args );
50
51 /*****************************************************************************
52  *
53  *****************************************************************************/
54 struct demux_sys_t
55 {
56     lua_State *L;
57     char *psz_filename;
58 };
59
60 /*****************************************************************************
61  *
62  *****************************************************************************/
63
64 static int vlclua_demux_peek( lua_State *L )
65 {
66     demux_t *p_demux = (demux_t *)vlclua_get_this( L );
67     int n = luaL_checkint( L, 1 );
68     const uint8_t *p_peek;
69     int i_peek = stream_Peek( p_demux->s, &p_peek, n );
70     lua_pushlstring( L, (const char *)p_peek, i_peek );
71     return 1;
72 }
73
74 static int vlclua_demux_read( lua_State *L )
75 {
76     demux_t *p_demux = (demux_t *)vlclua_get_this( L );
77     uint8_t *p_read;
78     int n = luaL_checkint( L, 1 );
79     int i_read = stream_Read( p_demux->s, &p_read, n );
80     lua_pushlstring( L, (const char *)p_read, i_read );
81     return 1;
82 }
83
84 static int vlclua_demux_readline( lua_State *L )
85 {
86     demux_t *p_demux = (demux_t *)vlclua_get_this( L );
87     char *psz_line = stream_ReadLine( p_demux->s );
88     if( psz_line )
89     {
90         lua_pushstring( L, psz_line );
91         free( psz_line );
92     }
93     else
94     {
95         lua_pushnil( L );
96     }
97     return 1;
98 }
99
100
101 /* Functions to register */
102 static luaL_Reg p_reg[] =
103 {
104     { "peek", vlclua_demux_peek },
105     { "decode_uri", vlclua_decode_uri },
106     { "resolve_xml_special_chars", vlclua_resolve_xml_special_chars },
107     { "msg_dbg", vlclua_msg_dbg },
108     { "msg_warn", vlclua_msg_warn },
109     { "msg_err", vlclua_msg_err },
110     { "msg_info", vlclua_msg_info },
111     { NULL, NULL }
112 };
113
114 /* Functions to register for parse() function call only */
115 static luaL_Reg p_reg_parse[] =
116 {
117     { "read", vlclua_demux_read },
118     { "readline", vlclua_demux_readline },
119     { NULL, NULL }
120 };
121
122 /*****************************************************************************
123  * Called through lua_scripts_batch_execute to call 'probe' on
124  * the script pointed by psz_filename.
125  *****************************************************************************/
126 static int probe_luascript( vlc_object_t *p_this, const char * psz_filename,
127                             lua_State * L, void * user_data )
128 {
129     VLC_UNUSED(user_data);
130     demux_t * p_demux = (demux_t *)p_this;
131
132     p_demux->p_sys->psz_filename = strdup(psz_filename);
133
134     /* In lua, setting a variable's value to nil is equivalent to deleting it */
135     lua_pushnil( L );
136     lua_pushnil( L );
137     lua_setglobal( L, "probe" );
138     lua_setglobal( L, "parse" );
139
140     /* Load and run the script(s) */
141     if( luaL_dofile( L, psz_filename ) )
142     {
143         msg_Warn( p_demux, "Error loading script %s: %s", psz_filename,
144                   lua_tostring( L, lua_gettop( L ) ) );
145         lua_pop( L, 1 );
146         return VLC_EGENERIC;
147     }
148
149     lua_getglobal( L, "probe" );
150
151     if( !lua_isfunction( L, -1 ) )
152     {
153         msg_Warn( p_demux, "Error while runing script %s, "
154                   "function probe() not found", psz_filename );
155         lua_pop( L, 1 );
156         return VLC_EGENERIC;
157     }
158
159     if( lua_pcall( L, 0, 1, 0 ) )
160     {
161         msg_Warn( p_demux, "Error while runing script %s, "
162                   "function probe(): %s", psz_filename,
163                   lua_tostring( L, lua_gettop( L ) ) );
164         lua_pop( L, 1 );
165         return VLC_EGENERIC;
166     }
167
168     if( lua_gettop( L ) )
169     {
170         int i_ret = VLC_EGENERIC;
171         if( lua_toboolean( L, 1 ) )
172         {
173             msg_Dbg( p_demux, "Lua playlist script %s's "
174                      "probe() function was successful", psz_filename );
175             i_ret = VLC_SUCCESS;
176         }
177         lua_pop( L, 1 );
178
179         return i_ret;
180     }
181     return VLC_EGENERIC;
182 }
183
184 /*****************************************************************************
185  * Import_LuaPlaylist: main import function
186  *****************************************************************************/
187 int Import_LuaPlaylist( vlc_object_t *p_this )
188 {
189     demux_t *p_demux = (demux_t *)p_this;
190     lua_State *L;
191     int ret;
192
193     p_demux->p_sys = (demux_sys_t*)malloc( sizeof( demux_sys_t ) );
194     if( !p_demux->p_sys )
195     {
196         return VLC_ENOMEM;
197     }
198
199     p_demux->p_sys->psz_filename = NULL;
200
201     p_demux->pf_control = Control;
202     p_demux->pf_demux = Demux;
203
204     /* Initialise Lua state structure */
205     L = luaL_newstate();
206     if( !L )
207     {
208         msg_Err( p_demux, "Could not create new Lua State" );
209         free( p_demux->p_sys );
210         return VLC_EGENERIC;
211     }
212     p_demux->p_sys->L = L;
213
214     /* Load Lua libraries */
215     luaL_openlibs( L ); /* FIXME: Don't open all the libs? */
216
217     luaL_register( L, "vlc", p_reg );
218     lua_pushlightuserdata( L, p_demux );
219     lua_setfield( L, -2, "private" );
220     lua_pushstring( L, p_demux->psz_path );
221     lua_setfield( L, -2, "path" );
222     lua_pushstring( L, p_demux->psz_access );
223     lua_setfield( L, -2, "access" );
224
225     lua_pop( L, 1 );
226
227     ret = vlclua_scripts_batch_execute( p_this, "playlist",
228                                         &probe_luascript, L, NULL );
229     if( ret )
230         Close_LuaPlaylist( p_this );
231     return ret;
232 }
233
234
235 /*****************************************************************************
236  * Deactivate: frees unused data
237  *****************************************************************************/
238 void Close_LuaPlaylist( vlc_object_t *p_this )
239 {
240     demux_t *p_demux = (demux_t *)p_this;
241     lua_close( p_demux->p_sys->L );
242     free( p_demux->p_sys->psz_filename );
243     free( p_demux->p_sys );
244 }
245
246 static int Demux( demux_t *p_demux )
247 {
248     lua_State *L = p_demux->p_sys->L;
249     char *psz_filename = p_demux->p_sys->psz_filename;
250
251     playlist_t *p_playlist = pl_Yield( p_demux );
252     input_thread_t *p_input_thread = (input_thread_t *)
253         vlc_object_find( p_demux, VLC_OBJECT_INPUT, FIND_PARENT );
254     input_item_t *p_current_input = input_GetItem( p_input_thread );
255
256     luaL_register( L, "vlc", p_reg_parse );
257
258     lua_getglobal( L, "parse" );
259
260     if( !lua_isfunction( L, -1 ) )
261     {
262         msg_Warn( p_demux, "Error while runing script %s, "
263                   "function parse() not found", psz_filename );
264         Close_LuaPlaylist( VLC_OBJECT( p_demux ) );
265         return VLC_EGENERIC;
266     }
267
268     if( lua_pcall( L, 0, 1, 0 ) )
269     {
270         msg_Warn( p_demux, "Error while runing script %s, "
271                   "function parse(): %s", psz_filename,
272                   lua_tostring( L, lua_gettop( L ) ) );
273         Close_LuaPlaylist( VLC_OBJECT( p_demux ) );
274         return VLC_EGENERIC;
275     }
276
277     if( lua_gettop( L ) )
278         vlclua_playlist_add_internal( p_demux, L, p_playlist,
279                                       p_current_input, 0 );
280     else
281         msg_Err( p_demux, "Script went completely foobar" );
282
283     vlc_object_release( p_input_thread );
284     vlc_object_release( p_playlist );
285
286     return -1; /* Needed for correct operation of go back */
287 }
288
289 static int Control( demux_t *p_demux, int i_query, va_list args )
290 {
291     VLC_UNUSED(p_demux); VLC_UNUSED(i_query); VLC_UNUSED(args);
292     return VLC_EGENERIC;
293 }