char **ppsz_fileend = NULL;
char **ppsz_file;
- char *ppsz_dir_list[] = { NULL, NULL, NULL };
+ char *ppsz_dir_list[] = { NULL, NULL, NULL, NULL };
char **ppsz_dir;
p_demux->p_sys = (demux_sys_t*)malloc( sizeof( demux_sys_t ) );
lua_pop( p_state, 1 );
- ppsz_dir_list[0] = malloc( strlen( p_demux->p_libvlc->psz_homedir )
- + strlen( "/"CONFIG_DIR"/luaplaylist" ) + 1 );
- sprintf( ppsz_dir_list[0], "%s/"CONFIG_DIR"/luaplaylist",
- p_demux->p_libvlc->psz_homedir );
+ if( asprintf( &ppsz_dir_list[0], "%s" DIR_SEP CONFIG_DIR DIR_SEP "luaplaylist",
+ p_demux->p_libvlc->psz_homedir ) < -1 )
+ return VLC_ENOMEM;
# if defined(__APPLE__) || defined(SYS_BEOS) || defined(WIN32)
{
- char *psz_vlcpath = p_demux->p_libvlc_global->psz_vlcpath;
- ppsz_dir_list[1] = malloc( strlen( psz_vlcpath ) + strlen( "/share/luaplaylist" ) + 1 );
- if( !ppsz_dir_list[1] ) return VLC_ENOMEM;
-# if defined( WIN32 )
- sprintf( ppsz_dir_list[1], "%s/luaplaylist", psz_vlcpath );
-# else
- sprintf( ppsz_dir_list[1], "%s/share/luaplaylist", psz_vlcpath );
-# endif
+ const char *psz_vlcpath = config_GetDataDir();
+ if( asprintf( &ppsz_dir_list[1], "%s" DIR_SEP "luaplaylist", psz_vlcpath ) < 0 )
+ return VLC_ENOMEM;
+ if( asprintf( &ppsz_dir_list[2], "%s" DIR_SEP "share" DIR_SEP "luaplaylist", psz_vlcpath ) < 0 )
+ return VLC_ENOMEM;
}
# else
{
for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend; ppsz_file++ )
{
free( psz_filename ); psz_filename = NULL;
- asprintf( &psz_filename, "%s/%s", *ppsz_dir, *ppsz_file );
+ asprintf( &psz_filename, "%s" DIR_SEP "%s", *ppsz_dir, *ppsz_file );
msg_Dbg( p_demux, "Trying Lua playlist script %s", psz_filename );
p_demux->p_sys->psz_filename = psz_filename;
free( p_demux->p_sys );
}
+static inline void read_options( demux_t *p_demux, lua_State *p_state,
+ int o, int t, int *pi_options,
+ char ***pppsz_options )
+{
+ lua_getfield( p_state, o, "options" );
+ if( lua_istable( p_state, t ) )
+ {
+ lua_pushnil( p_state );
+ while( lua_next( p_state, t ) )
+ {
+ if( lua_isstring( p_state, t+2 ) )
+ {
+ char *psz_option = strdup( lua_tostring( p_state, t+2 ) );
+ msg_Dbg( p_demux, "Option: %s", psz_option );
+ INSERT_ELEM( *pppsz_options, *pi_options, *pi_options,
+ psz_option );
+ }
+ else
+ {
+ msg_Warn( p_demux, "Option should be a string" );
+ }
+ lua_pop( p_state, 1 ); /* pop option */
+ }
+ }
+ lua_pop( p_state, 1 ); /* pop "options" */
+}
+
+static inline void read_meta_data( demux_t *p_demux,
+ lua_State *p_state, int o, int t,
+ input_item_t *p_input )
+{
+ const char *psz_value;
+#define TRY_META( a, b ) \
+ lua_getfield( p_state, o, a ); \
+ if( lua_isstring( p_state, t ) ) \
+ { \
+ psz_value = lua_tostring( p_state, t ); \
+ msg_Dbg( p_demux, #b ": %s", psz_value ); \
+ input_item_Set ## b ( p_input, psz_value ); \
+ } \
+ lua_pop( p_state, 1 ); /* pop a */
+ TRY_META( "title", Title );
+ TRY_META( "artist", Artist );
+ TRY_META( "genre", Genre );
+ TRY_META( "copyright", Copyright );
+ TRY_META( "album", Album );
+ TRY_META( "tracknum", TrackNum );
+ TRY_META( "description", Description );
+ TRY_META( "rating", Rating );
+ TRY_META( "date", Date );
+ TRY_META( "setting", Setting );
+ TRY_META( "url", URL );
+ TRY_META( "language", Language );
+ TRY_META( "nowplaying", NowPlaying );
+ TRY_META( "publisher", Publisher );
+ TRY_META( "encodedby", EncodedBy );
+ TRY_META( "arturl", ArtURL );
+ TRY_META( "trackid", TrackID );
+}
+
+static inline void read_custom_meta_data( demux_t *p_demux,
+ lua_State *p_state, int o, int t,
+ input_item_t *p_input )
+{
+ lua_getfield( p_state, o, "meta" );
+ if( lua_istable( p_state, t ) )
+ {
+ lua_pushnil( p_state );
+ while( lua_next( p_state, t ) )
+ {
+ if( !lua_isstring( p_state, t+1 ) )
+ {
+ msg_Warn( p_demux, "Custom meta data category name must be "
+ "a string" );
+ }
+ else if( !lua_istable( p_state, t+2 ) )
+ {
+ msg_Warn( p_demux, "Custom meta data category contents "
+ "must be a table" );
+ }
+ else
+ {
+ const char *psz_meta_category = lua_tostring( p_state, t+1 );
+ msg_Dbg( p_demux, "Found custom meta data category: %s",
+ psz_meta_category );
+ lua_pushnil( p_state );
+ while( lua_next( p_state, t+2 ) )
+ {
+ if( !lua_isstring( p_state, t+3 ) )
+ {
+ msg_Warn( p_demux, "Custom meta category item name "
+ "must be a string." );
+ }
+ else if( !lua_isstring( p_state, t+4 ) )
+ {
+ msg_Warn( p_demux, "Custom meta category item value "
+ "must be a string." );
+ }
+ else
+ {
+ const char *psz_meta_name =
+ lua_tostring( p_state, t+3 );
+ const char *psz_meta_value =
+ lua_tostring( p_state, t+4 );
+ msg_Dbg( p_demux, "Custom meta %s, %s: %s",
+ psz_meta_category, psz_meta_name,
+ psz_meta_value );
+ input_ItemAddInfo( p_input, psz_meta_category,
+ psz_meta_name, psz_meta_value );
+ }
+ lua_pop( p_state, 1 ); /* pop item */
+ }
+ }
+ lua_pop( p_state, 1 ); /* pop category */
+ }
+ }
+ lua_pop( p_state, 1 ); /* pop "meta" */
+}
+
static int Demux( demux_t *p_demux )
{
input_item_t *p_input;
lua_State *p_state = p_demux->p_sys->p_state;
char *psz_filename = p_demux->p_sys->psz_filename;
+ int t;
INIT_PLAYLIST_STUFF;
return VLC_EGENERIC;
}
- if( lua_gettop( p_state ) )
+ /* Check that the Lua stack is big enough and grow it if needed.
+ * Should be ok since LUA_MINSTACK is 20 but we never know. */
+ lua_checkstack( p_state, 8 );
+
+ if( ( t = lua_gettop( p_state ) ) )
{
- int t = lua_gettop( p_state );
+
if( lua_istable( p_state, t ) )
{
lua_pushnil( p_state );
{
if( lua_istable( p_state, t+2 ) )
{
- const char *psz_url = NULL;
- const char *psz_title = NULL;
lua_getfield( p_state, t+2, "path" );
if( lua_isstring( p_state, t+3 ) )
{
- psz_url = lua_tostring( p_state, t+3 );
- msg_Dbg( p_demux, "Path: %s", psz_url );
+ const char *psz_path = NULL;
+ const char *psz_name = NULL;
+ char **ppsz_options = NULL;
+ int i_options = 0;
+ mtime_t i_duration = -1;
+
+ /* Read path and name */
+ psz_path = lua_tostring( p_state, t+3 );
+ msg_Dbg( p_demux, "Path: %s", psz_path );
lua_getfield( p_state, t+2, "name" );
if( lua_isstring( p_state, t+4 ) )
{
- psz_title = lua_tostring( p_state, t+4 );
- msg_Dbg( p_demux, "Name: %s", psz_title );
+ psz_name = lua_tostring( p_state, t+4 );
+ msg_Dbg( p_demux, "Name: %s", psz_name );
}
else
{
- psz_title = psz_url;
+ psz_name = psz_path;
+ }
+
+ /* Read duration */
+ lua_getfield( p_state, t+2, "duration" );
+ if( lua_isnumber( p_state, t+5 ) )
+ {
+ i_duration = (mtime_t)lua_tointeger( p_state, t+5 );
+ i_duration *= 1000000;
}
- p_input = input_ItemNewExt( p_playlist, psz_url,
- psz_title, 0, NULL, -1 );
- p_input->p_meta = vlc_meta_New();
-
-#define TRY_META( a, b ) \
- lua_getfield( p_state, t+2, a ); \
- if( lua_isstring( p_state, t+5 ) ) \
- { \
- psz_title = lua_tostring( p_state, t+5 ); \
- msg_Dbg( p_demux, #b ": %s", psz_title ); \
- vlc_meta_Set ## b ( p_input->p_meta, psz_title ); \
- } \
- lua_pop( p_state, 1 ); /* pop a */
- TRY_META( "title", Title );
- TRY_META( "artist", Artist );
- TRY_META( "genre", Genre );
- TRY_META( "copyright", Copyright );
- TRY_META( "album", Album );
- TRY_META( "tracknum", Tracknum );
- TRY_META( "description", Description );
- TRY_META( "rating", Rating );
- TRY_META( "date", Date );
- TRY_META( "setting", Setting );
- TRY_META( "url", URL );
- TRY_META( "language", Language );
- TRY_META( "nowplaying", NowPlaying );
- TRY_META( "publisher", Publisher );
- TRY_META( "encodedby", EncodedBy );
- TRY_META( "arturl", ArtURL );
- TRY_META( "trackid", TrackID );
+ lua_pop( p_state, 1 ); /* pop "duration" */
+
+ /* Read options */
+ read_options( p_demux, p_state, t+2, t+5,
+ &i_options, &ppsz_options );
+ /* Create input item */
+ p_input = input_ItemNewExt( p_playlist, psz_path,
+ psz_name, i_options,
+ (const char **)ppsz_options,
+ i_duration );
+ lua_pop( p_state, 1 ); /* pop "name" */
+
+ /* Read meta data */
+ read_meta_data( p_demux, p_state, t+2, t+4, p_input );
+
+ /* Read custom meta data */
+ read_custom_meta_data( p_demux, p_state, t+2, t+4,
+ p_input );
+
+ /* Append item to playlist */
playlist_BothAddInput(
p_playlist, p_input,
p_item_in_category,
PLAYLIST_APPEND | PLAYLIST_SPREPARSE,
PLAYLIST_END, NULL, NULL, VLC_FALSE );
- lua_pop( p_state, 1 ); /* pop "name" */
+
+ while( i_options > 0 )
+ free( ppsz_options[--i_options] );
+ free( ppsz_options );
}
else
{
lua_pop( p_state, 1 ); /* pop the value, keep the key for
* the next lua_next() call */
}
- lua_pop( p_state, 1 ); /* pop the last key */
}
else
{