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", 9 );
64 set_callbacks( E_(Import_LuaPlaylist), E_(Close_LuaPlaylist) );
67 /*****************************************************************************
69 *****************************************************************************/
76 /*****************************************************************************
78 *****************************************************************************/
79 static demux_t *vlclua_get_demux( lua_State *p_state )
82 lua_getglobal( p_state, "vlc" );
83 lua_getfield( p_state, lua_gettop( p_state ), "private" );
84 p_demux = (demux_t*)lua_topointer( p_state, lua_gettop( p_state ) );
85 lua_pop( p_state, 2 );
89 static int vlclua_peek( lua_State *p_state )
91 demux_t *p_demux = vlclua_get_demux( p_state );
92 int i = lua_gettop( p_state );
97 n = lua_tonumber( p_state, 1 );
98 lua_pop( p_state, i );
99 i_peek = stream_Peek( p_demux->s, &p_peek, n );
100 lua_pushlstring( p_state, (const char *)p_peek, i_peek );
104 static int vlclua_read( lua_State *p_state )
106 demux_t *p_demux = vlclua_get_demux( p_state );
107 int i = lua_gettop( p_state );
112 n = lua_tonumber( p_state, 1 );
113 lua_pop( p_state, i );
114 i_read = stream_Read( p_demux->s, &p_read, n );
115 lua_pushlstring( p_state, (const char *)p_read, i_read );
119 static int vlclua_readline( lua_State *p_state )
121 demux_t *p_demux = vlclua_get_demux( p_state );
122 char *psz_line = stream_ReadLine( p_demux->s );
125 lua_pushstring( p_state, psz_line );
130 lua_pushnil( p_state );
135 static int vlclua_decode_uri( lua_State *p_state )
137 int i = lua_gettop( p_state );
139 const char *psz_cstring = lua_tostring( p_state, 1 );
140 if( !psz_cstring ) return 0;
141 char *psz_string = strdup( psz_cstring );
142 lua_pop( p_state, i );
143 decode_URI( psz_string );
144 lua_pushstring( p_state, psz_string );
149 static int vlclua_resolve_xml_special_chars( lua_State *p_state )
151 int i = lua_gettop( p_state );
153 const char *psz_cstring = lua_tostring( p_state, 1 );
154 if( !psz_cstring ) return 0;
155 char *psz_string = strdup( psz_cstring );
156 lua_pop( p_state, i );
157 resolve_xml_special_chars( psz_string );
158 lua_pushstring( p_state, psz_string );
163 static int vlclua_msg_dbg( lua_State *p_state )
165 demux_t *p_demux = vlclua_get_demux( p_state );
166 int i = lua_gettop( p_state );
168 const char *psz_cstring = lua_tostring( p_state, 1 );
169 if( !psz_cstring ) return 0;
170 msg_Dbg( p_demux, "%s: %s", p_demux->p_sys->psz_filename, psz_cstring );
173 static int vlclua_msg_warn( lua_State *p_state )
175 demux_t *p_demux = vlclua_get_demux( p_state );
176 int i = lua_gettop( p_state );
178 const char *psz_cstring = lua_tostring( p_state, 1 );
179 if( !psz_cstring ) return 0;
180 msg_Warn( p_demux, "%s: %s", p_demux->p_sys->psz_filename, psz_cstring );
183 static int vlclua_msg_err( lua_State *p_state )
185 demux_t *p_demux = vlclua_get_demux( p_state );
186 int i = lua_gettop( p_state );
188 const char *psz_cstring = lua_tostring( p_state, 1 );
189 if( !psz_cstring ) return 0;
190 msg_Err( p_demux, "%s: %s", p_demux->p_sys->psz_filename, psz_cstring );
193 static int vlclua_msg_info( lua_State *p_state )
195 demux_t *p_demux = vlclua_get_demux( p_state );
196 int i = lua_gettop( p_state );
198 const char *psz_cstring = lua_tostring( p_state, 1 );
199 if( !psz_cstring ) return 0;
200 msg_Info( p_demux, "%s: %s", p_demux->p_sys->psz_filename, psz_cstring );
204 /* Functions to register */
205 static luaL_Reg p_reg[] =
207 { "peek", vlclua_peek },
208 { "decode_uri", vlclua_decode_uri },
209 { "resolve_xml_special_chars", vlclua_resolve_xml_special_chars },
210 { "msg_dbg", vlclua_msg_dbg },
211 { "msg_warn", vlclua_msg_warn },
212 { "msg_err", vlclua_msg_err },
213 { "msg_info", vlclua_msg_info },
217 /* Functions to register for parse() function call only */
218 static luaL_Reg p_reg_parse[] =
220 { "read", vlclua_read },
221 { "readline", vlclua_readline },
225 /*****************************************************************************
227 *****************************************************************************/
228 static int file_select( const char *file )
230 int i = strlen( file );
231 return i > 4 && !strcmp( file+i-4, ".lua" );
234 static int file_compare( const char **a, const char **b )
236 return strcmp( *a, *b );
239 /*****************************************************************************
240 * Import_LuaPlaylist: main import function
241 *****************************************************************************/
242 int E_(Import_LuaPlaylist)( vlc_object_t *p_this )
244 demux_t *p_demux = (demux_t *)p_this;
246 int i_ret = VLC_EGENERIC;
248 char *psz_filename = NULL;
251 char **ppsz_filelist = NULL;
252 char **ppsz_fileend = NULL;
255 char *ppsz_dir_list[] = { NULL, NULL, NULL };
258 p_demux->p_sys = (demux_sys_t*)malloc( sizeof( demux_sys_t ) );
259 if( !p_demux->p_sys )
264 p_demux->p_sys->psz_filename = NULL;
266 p_demux->pf_control = Control;
267 p_demux->pf_demux = Demux;
269 /* Initialise Lua state structure */
270 p_state = luaL_newstate();
273 msg_Err( p_demux, "Could not create new Lua State" );
274 free( p_demux->p_sys );
277 p_demux->p_sys->p_state = p_state;
279 /* Load Lua libraries */
280 luaL_openlibs( p_state ); /* FIXME: Don't open all the libs? */
282 luaL_register( p_state, "vlc", p_reg );
283 lua_pushlightuserdata( p_state, p_demux );
284 lua_setfield( p_state, lua_gettop( p_state ) - 1, "private" );
285 lua_pushstring( p_state, p_demux->psz_path );
286 lua_setfield( p_state, lua_gettop( p_state ) - 1, "path" );
287 lua_pushstring( p_state, p_demux->psz_access );
288 lua_setfield( p_state, lua_gettop( p_state ) - 1, "access" );
290 lua_pop( p_state, 1 );
292 ppsz_dir_list[0] = malloc( strlen( p_demux->p_libvlc->psz_homedir )
293 + strlen( "/"CONFIG_DIR"/luaplaylist" ) + 1 );
294 sprintf( ppsz_dir_list[0], "%s/"CONFIG_DIR"/luaplaylist",
295 p_demux->p_libvlc->psz_homedir );
297 # if defined(__APPLE__) || defined(SYS_BEOS) || defined(WIN32)
299 char *psz_vlcpath = p_demux->p_libvlc_global->psz_vlcpath;
300 ppsz_dir_list[1] = malloc( strlen( psz_vlcpath ) + strlen( "/share/luaplaylist" ) + 1 );
301 if( !ppsz_dir_list[1] ) return VLC_ENOMEM;
302 # if defined( WIN32 )
303 sprintf( ppsz_dir_list[1], "%s/luaplaylist", psz_vlcpath );
305 sprintf( ppsz_dir_list[1], "%s/share/luaplaylist", psz_vlcpath );
310 # ifdef HAVE_SYS_STAT_H
311 struct stat stat_info;
312 if( ( utf8_stat( "share/luaplaylist", &stat_info ) == -1 )
313 || !S_ISDIR( stat_info.st_mode ) )
315 ppsz_dir_list[1] = strdup( DATA_PATH "/luaplaylist" );
320 ppsz_dir_list[1] = strdup( "share/luaplaylist" );
325 for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
331 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
334 free( ppsz_filelist );
335 ppsz_filelist = NULL;
343 msg_Dbg( p_demux, "Trying Lua scripts in %s", *ppsz_dir );
344 dir = utf8_opendir( *ppsz_dir );
347 i_files = utf8_loaddir( dir, &ppsz_filelist, file_select, file_compare );
348 if( i_files < 1 ) continue;
349 ppsz_fileend = ppsz_filelist + i_files;
351 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend; ppsz_file++ )
353 free( psz_filename ); psz_filename = NULL;
354 asprintf( &psz_filename, "%s/%s", *ppsz_dir, *ppsz_file );
355 msg_Dbg( p_demux, "Trying Lua playlist script %s", psz_filename );
356 p_demux->p_sys->psz_filename = psz_filename;
358 /* Ugly hack to delete previous versions of the probe() and parse()
360 lua_pushnil( p_state );
361 lua_pushnil( p_state );
362 lua_setglobal( p_state, "probe" );
363 lua_setglobal( p_state, "parse" );
365 /* Load and run the script(s) */
366 if( luaL_dofile( p_state, psz_filename ) )
368 msg_Warn( p_demux, "Error loading script %s: %s", psz_filename,
369 lua_tostring( p_state, lua_gettop( p_state ) ) );
370 lua_pop( p_state, 1 );
374 lua_getglobal( p_state, "probe" );
376 if( !lua_isfunction( p_state, lua_gettop( p_state ) ) )
378 msg_Warn( p_demux, "Error while runing script %s, "
379 "function probe() not found", psz_filename );
380 lua_pop( p_state, 1 );
384 if( lua_pcall( p_state, 0, 1, 0 ) )
386 msg_Warn( p_demux, "Error while runing script %s, "
387 "function probe(): %s", psz_filename,
388 lua_tostring( p_state, lua_gettop( p_state ) ) );
389 lua_pop( p_state, 1 );
393 if( lua_gettop( p_state ) )
395 if( lua_toboolean( p_state, 1 ) )
397 msg_Dbg( p_demux, "Lua playlist script %s's "
398 "probe() function was successful", psz_filename );
401 lua_pop( p_state, 1 );
403 if( i_ret == VLC_SUCCESS ) break;
406 if( i_ret == VLC_SUCCESS ) break;
411 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
414 free( ppsz_filelist );
416 for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
419 if( dir ) closedir( dir );
420 if( i_ret != VLC_SUCCESS )
421 E_(Close_LuaPlaylist)( p_this );
425 /*****************************************************************************
426 * Deactivate: frees unused data
427 *****************************************************************************/
428 void E_(Close_LuaPlaylist)( vlc_object_t *p_this )
430 demux_t *p_demux = (demux_t *)p_this;
431 lua_close( p_demux->p_sys->p_state );
432 free( p_demux->p_sys->psz_filename );
433 free( p_demux->p_sys );
436 static int Demux( demux_t *p_demux )
438 input_item_t *p_input;
439 lua_State *p_state = p_demux->p_sys->p_state;
440 char *psz_filename = p_demux->p_sys->psz_filename;
444 luaL_register( p_state, "vlc", p_reg_parse );
446 lua_getglobal( p_state, "parse" );
448 if( !lua_isfunction( p_state, lua_gettop( p_state ) ) )
450 msg_Warn( p_demux, "Error while runing script %s, "
451 "function parse() not found", psz_filename );
452 E_(Close_LuaPlaylist)( VLC_OBJECT( p_demux ) );
456 if( lua_pcall( p_state, 0, 1, 0 ) )
458 msg_Warn( p_demux, "Error while runing script %s, "
459 "function parse(): %s", psz_filename,
460 lua_tostring( p_state, lua_gettop( p_state ) ) );
461 E_(Close_LuaPlaylist)( VLC_OBJECT( p_demux ) );
465 if( lua_gettop( p_state ) )
467 int t = lua_gettop( p_state );
468 if( lua_istable( p_state, t ) )
470 lua_pushnil( p_state );
471 while( lua_next( p_state, t ) )
473 if( lua_istable( p_state, t+2 ) )
475 const char *psz_url = NULL;
476 const char *psz_title = NULL;
477 lua_getfield( p_state, t+2, "path" );
478 if( lua_isstring( p_state, t+3 ) )
480 psz_url = lua_tostring( p_state, t+3 );
481 msg_Dbg( p_demux, "Path: %s", psz_url );
482 lua_getfield( p_state, t+2, "name" );
483 if( lua_isstring( p_state, t+4 ) )
485 psz_title = lua_tostring( p_state, t+4 );
486 msg_Dbg( p_demux, "Name: %s", psz_title );
492 p_input = input_ItemNewExt( p_playlist, psz_url,
493 psz_title, 0, NULL, -1 );
494 p_input->p_meta = vlc_meta_New();
496 #define TRY_META( a, b ) \
497 lua_getfield( p_state, t+2, a ); \
498 if( lua_isstring( p_state, t+5 ) ) \
500 psz_title = lua_tostring( p_state, t+5 ); \
501 msg_Dbg( p_demux, #b ": %s", psz_title ); \
502 vlc_meta_Set ## b ( p_input->p_meta, psz_title ); \
504 lua_pop( p_state, 1 ); /* pop a */
505 TRY_META( "title", Title );
506 TRY_META( "artist", Artist );
507 TRY_META( "genre", Genre );
508 TRY_META( "copyright", Copyright );
509 TRY_META( "album", Album );
510 TRY_META( "tracknum", Tracknum );
511 TRY_META( "description", Description );
512 TRY_META( "rating", Rating );
513 TRY_META( "date", Date );
514 TRY_META( "setting", Setting );
515 TRY_META( "url", URL );
516 TRY_META( "language", Language );
517 TRY_META( "nowplaying", NowPlaying );
518 TRY_META( "publisher", Publisher );
519 TRY_META( "encodedby", EncodedBy );
520 TRY_META( "arturl", ArtURL );
521 TRY_META( "trackid", TrackID );
523 playlist_BothAddInput(
526 PLAYLIST_APPEND | PLAYLIST_SPREPARSE,
527 PLAYLIST_END, NULL, NULL, VLC_FALSE );
528 lua_pop( p_state, 1 ); /* pop "name" */
533 "Playlist item's path should be a string" );
535 lua_pop( p_state, 1 ); /* pop "path" */
539 msg_Warn( p_demux, "Playlist item should be a table" );
541 lua_pop( p_state, 1 ); /* pop the value, keep the key for
542 * the next lua_next() call */
544 lua_pop( p_state, 1 ); /* pop the last key */
548 msg_Warn( p_demux, "Script didn't return a table" );
553 msg_Err( p_demux, "Script went completely foobar" );
556 HANDLE_PLAY_AND_RELEASE;
558 return -1; /* Needed for correct operation of go back */
561 static int Control( demux_t *p_demux, int i_query, va_list args )