1 /*****************************************************************************
2 * luaplaylist.c : Lua playlist demux module
3 *****************************************************************************
4 * Copyright (C) 2007 the VideoLAN team
7 * Authors: Antoine Cellerier <dionoea at videolan tod org>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
28 #include <vlc_demux.h>
30 #include <vlc_strings.h>
31 #include <vlc_charset.h>
33 #include <errno.h> /* ENOMEM */
36 #ifdef HAVE_SYS_STAT_H
37 # include <sys/stat.h>
40 #include <lua.h> /* Low level lua C API */
41 #include <lauxlib.h> /* Higher level C API */
42 #include <lualib.h> /* Lua libs */
44 /*****************************************************************************
46 *****************************************************************************/
47 static int E_(Import_LuaPlaylist)( vlc_object_t *p_this );
48 static void E_(Close_LuaPlaylist)( vlc_object_t *p_this );
50 static int Demux( demux_t *p_demux );
51 static int Control( demux_t *p_demux, int i_query, va_list args );
53 /*****************************************************************************
55 *****************************************************************************/
57 add_shortcut( "lua" );
58 set_category( CAT_INPUT );
59 set_subcategory( SUBCAT_INPUT_DEMUX );
61 set_shortname( _("Lua Playlist") );
62 set_description( _("Lua Playlist Parser Interface") );
63 set_capability( "demux2", 0 );
64 set_callbacks( E_(Import_LuaPlaylist), E_(Close_LuaPlaylist) );
67 /*****************************************************************************
69 *****************************************************************************/
75 static demux_t *vlclua_get_demux( lua_State *p_state )
78 lua_getglobal( p_state, "vlc" );
79 lua_getfield( p_state, lua_gettop( p_state ), "private" );
80 p_demux = (demux_t*)lua_topointer( p_state, lua_gettop( p_state ) );
81 lua_pop( p_state, 2 );
85 static int vlclua_peek( lua_State *p_state )
87 demux_t *p_demux = vlclua_get_demux( p_state );
88 int i = lua_gettop( p_state );
93 n = lua_tonumber( p_state, 1 );
94 lua_pop( p_state, i );
95 i_peek = stream_Peek( p_demux->s, &p_peek, n );
96 lua_pushlstring( p_state, (const char *)p_peek, i_peek );
100 static int vlclua_read( lua_State *p_state )
102 demux_t *p_demux = vlclua_get_demux( p_state );
103 int i = lua_gettop( p_state );
108 n = lua_tonumber( p_state, 1 );
109 lua_pop( p_state, i );
110 i_read = stream_Read( p_demux->s, &p_read, n );
111 lua_pushlstring( p_state, (const char *)p_read, i_read );
115 static int vlclua_readline( lua_State *p_state )
117 demux_t *p_demux = vlclua_get_demux( p_state );
118 char *psz_line = stream_ReadLine( p_demux->s );
121 lua_pushstring( p_state, psz_line );
126 lua_pushnil( p_state );
131 static int vlclua_decode_uri( lua_State *p_state )
133 int i = lua_gettop( p_state );
135 const char *psz_cstring = lua_tostring( p_state, 1 );
136 if( !psz_cstring ) return 0;
137 char *psz_string = strdup( psz_cstring );
138 lua_pop( p_state, i );
139 decode_URI( psz_string );
140 lua_pushstring( p_state, psz_string );
145 static int vlclua_resolve_xml_special_chars( lua_State *p_state )
147 int i = lua_gettop( p_state );
149 const char *psz_cstring = lua_tostring( p_state, 1 );
150 if( !psz_cstring ) return 0;
151 char *psz_string = strdup( psz_cstring );
152 lua_pop( p_state, i );
153 resolve_xml_special_chars( psz_string );
154 lua_pushstring( p_state, psz_string );
159 static int file_select( const char *file )
161 int i = strlen( file );
162 return i > 4 && !strcmp( file+i-4, ".lua" );
165 static int file_compare( const char **a, const char **b )
167 return strcmp( *a, *b );
170 /*****************************************************************************
171 * Import_LuaPlaylist: main import function
172 *****************************************************************************/
173 int E_(Import_LuaPlaylist)( vlc_object_t *p_this )
175 demux_t *p_demux = (demux_t *)p_this;
177 int i_ret = VLC_EGENERIC;
178 char **ppsz_filelist = NULL;
179 char **ppsz_fileend = NULL;
182 char *psz_filename = NULL;
186 # if defined(__APPLE__) || defined(SYS_BEOS) || defined(WIN32)
188 char *psz_vlcpath = p_intf->p_libvlc_global->psz_vlcpath;
189 psz_dir = malloc( strlen( psz_vlcpath ) + strlen( "/share/luaplaylist" ) + 1 );
190 if( !psz_src ) return VLC_ENOMEM;
191 # if defined( WIN32 )
192 sprintf( psz_src, "%s/luaplaylist", psz_vlcpath );
194 sprintf( psz_src, "%s/share/luaplaylist", psz_vlcpath );
199 # ifdef HAVE_SYS_STAT_H
200 struct stat stat_info;
201 if( ( utf8_stat( "share/luaplaylist", &stat_info ) == -1 )
202 || !S_ISDIR( stat_info.st_mode ) )
204 psz_dir = strdup( DATA_PATH "/luaplaylist" );
209 psz_dir = strdup( "share/luaplaylist" );
214 static luaL_Reg p_reg[] =
216 { "peek", vlclua_peek },
217 { "read", vlclua_read },
218 { "readline", vlclua_readline },
219 { "decode_uri", vlclua_decode_uri },
220 { "resolve_xml_special_chars", vlclua_resolve_xml_special_chars }
223 p_demux->p_sys = (demux_sys_t*)malloc( sizeof( demux_sys_t ) );
224 if( !p_demux->p_sys )
230 p_demux->pf_control = Control;
231 p_demux->pf_demux = Demux;
233 /* Initialise Lua state structure */
234 p_state = luaL_newstate();
237 msg_Err( p_demux, "Could not create new Lua State" );
239 free( p_demux->p_sys );
242 p_demux->p_sys->p_state = p_state;
244 /* Load Lua libraries */
245 luaL_openlibs( p_state ); /* FIXME: Don't open all the libs? */
247 luaL_register( p_state, "vlc", p_reg );
248 lua_pushlightuserdata( p_state, p_demux );
249 lua_setfield( p_state, lua_gettop( p_state ) - 1, "private" );
250 lua_pushstring( p_state, p_demux->psz_path );
251 lua_setfield( p_state, lua_gettop( p_state ) - 1, "path" );
252 lua_pushstring( p_state, p_demux->psz_access );
253 lua_setfield( p_state, lua_gettop( p_state ) - 1, "access" );
255 lua_pop( p_state, 1 );
257 dir = utf8_opendir( psz_dir );
258 if( !dir ) goto error;
259 i_files = utf8_loaddir( dir, &ppsz_filelist, file_select, file_compare );
260 if( i_files < 1 ) goto error;
261 ppsz_fileend = ppsz_filelist + i_files;
263 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend; ppsz_file++ )
265 free( psz_filename ); psz_filename = NULL;
266 asprintf( &psz_filename, "%s/%s", psz_dir, *ppsz_file );
267 msg_Dbg( p_demux, "Trying Lua playlist script %s", psz_filename );
269 /* Ugly hack to delete previous versions of the probe() and parse()
271 lua_pushnil( p_state );
272 lua_pushnil( p_state );
273 lua_setglobal( p_state, "probe" );
274 lua_setglobal( p_state, "parse" );
276 /* Load and run the script(s) */
277 if( luaL_dofile( p_state, psz_filename ) )
279 msg_Warn( p_demux, "Error loading script %s: %s", psz_filename,
280 lua_tostring( p_state, lua_gettop( p_state ) ) );
281 lua_pop( p_state, 1 );
285 lua_getglobal( p_state, "probe" );
287 if( !lua_isfunction( p_state, lua_gettop( p_state ) ) )
289 msg_Warn( p_demux, "Error while runing script %s, "
290 "function probe() not found", psz_filename );
291 lua_pop( p_state, 1 );
295 if( lua_pcall( p_state, 0, 1, 0 ) )
297 msg_Warn( p_demux, "Error while runing script %s, "
298 "function probe(): %s", psz_filename,
299 lua_tostring( p_state, lua_gettop( p_state ) ) );
300 lua_pop( p_state, 1 );
304 if( lua_gettop( p_state ) )
306 if( lua_toboolean( p_state, 1 ) )
308 msg_Dbg( p_demux, "Lua playlist script %s's "
309 "probe() function was successful", psz_filename );
312 lua_pop( p_state, 1 );
314 if( i_ret == VLC_SUCCESS ) break;
319 free( psz_filename );
323 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
326 free( ppsz_filelist );
329 if( dir ) closedir( dir );
331 if( i_ret != VLC_SUCCESS )
332 E_(Close_LuaPlaylist)( p_this );
336 /*****************************************************************************
337 * Deactivate: frees unused data
338 *****************************************************************************/
339 void E_(Close_LuaPlaylist)( vlc_object_t *p_this )
341 demux_t *p_demux = (demux_t *)p_this;
342 lua_close( p_demux->p_sys->p_state );
343 free( p_demux->p_sys );
346 static int Demux( demux_t *p_demux )
348 input_item_t *p_input;
349 lua_State *p_state = p_demux->p_sys->p_state;
350 char psz_filename[] = "FIXME";
354 lua_getglobal( p_state, "parse" );
356 if( !lua_isfunction( p_state, lua_gettop( p_state ) ) )
358 msg_Warn( p_demux, "Error while runing script %s, "
359 "function parse() not found", psz_filename );
360 E_(Close_LuaPlaylist)( VLC_OBJECT( p_demux ) );
364 if( lua_pcall( p_state, 0, 1, 0 ) )
366 msg_Warn( p_demux, "Error while runing script %s, "
367 "function parse(): %s", psz_filename,
368 lua_tostring( p_state, lua_gettop( p_state ) ) );
369 E_(Close_LuaPlaylist)( VLC_OBJECT( p_demux ) );
373 if( lua_gettop( p_state ) )
375 int t = lua_gettop( p_state );
376 if( lua_istable( p_state, t ) )
378 lua_pushnil( p_state );
379 while( lua_next( p_state, t ) )
381 if( lua_istable( p_state, t+2 ) )
383 const char *psz_url = NULL;
384 const char *psz_title = NULL;
385 lua_getfield( p_state, t+2, "path" );
386 if( lua_isstring( p_state, t+3 ) )
388 psz_url = lua_tostring( p_state, t+3 );
389 printf("Path: %s\n", psz_url );
390 lua_getfield( p_state, t+2, "name" );
391 if( lua_isstring( p_state, t+4 ) )
393 psz_title = lua_tostring( p_state, t+4 );
394 printf("Name: %s\n", psz_title );
400 p_input = input_ItemNewExt( p_playlist, psz_url,
401 psz_title, 0, NULL, -1 );
402 p_input->p_meta = vlc_meta_New();
404 #define TRY_META( a, b ) \
405 lua_getfield( p_state, t+2, a ); \
406 if( lua_isstring( p_state, t+5 ) ) \
408 psz_title = lua_tostring( p_state, t+5 ); \
409 printf( #b ": %s\n", psz_title ); \
410 vlc_meta_Set ## b ( p_input->p_meta, psz_title ); \
412 lua_pop( p_state, 1 ); /* pop a */
413 TRY_META( "title", Title );
414 TRY_META( "artist", Artist );
415 TRY_META( "genre", Genre );
416 TRY_META( "copyright", Copyright );
417 TRY_META( "album", Album );
418 TRY_META( "tracknum", Tracknum );
419 TRY_META( "description", Description );
420 TRY_META( "rating", Rating );
421 TRY_META( "date", Date );
422 TRY_META( "setting", Setting );
423 TRY_META( "url", URL );
424 TRY_META( "language", Language );
425 TRY_META( "nowplaying", NowPlaying );
426 TRY_META( "publisher", Publisher );
427 TRY_META( "encodedby", EncodedBy );
428 TRY_META( "arturl", ArtURL );
429 TRY_META( "trackid", TrackID );
431 playlist_BothAddInput(
434 PLAYLIST_APPEND | PLAYLIST_SPREPARSE,
435 PLAYLIST_END, NULL, NULL, VLC_FALSE );
436 lua_pop( p_state, 1 ); /* pop "name" */
440 printf("Path isn't a string\n");
442 lua_pop( p_state, 1 ); /* pop "path" */
446 printf("This isn't a table !!!\n" );
448 lua_pop( p_state, 1 ); /* pop the value, keep the key for
449 * the next lua_next() call */
451 lua_pop( p_state, 1 ); /* pop the last key */
455 msg_Warn( p_demux, "Script didn't return a table" );
460 msg_Err( p_demux, "Script went completely foobar" );
463 HANDLE_PLAY_AND_RELEASE;
465 return -1; /* Needed for correct operation of go back */
468 static int Control( demux_t *p_demux, int i_query, va_list args )