1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2007-2008 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 *****************************************************************************/
35 #include <vlc_common.h>
37 #include <vlc_interface.h>
38 #include <vlc_playlist.h>
40 #include <lua.h> /* Low level lua C API */
41 #include <lauxlib.h> /* Higher level C API */
46 #include "variables.h"
48 /*****************************************************************************
49 * Internal lua<->vlc utils
50 *****************************************************************************/
51 playlist_t *vlclua_get_playlist_internal( lua_State *L )
53 vlc_object_t *p_this = vlclua_get_this( L );
54 return pl_Get( p_this );
57 static int vlclua_playlist_prev( lua_State * L )
59 playlist_t *p_playlist = vlclua_get_playlist_internal( L );
60 playlist_Prev( p_playlist );
64 static int vlclua_playlist_next( lua_State * L )
66 playlist_t *p_playlist = vlclua_get_playlist_internal( L );
67 playlist_Next( p_playlist );
71 static int vlclua_playlist_skip( lua_State * L )
73 int i_skip = luaL_checkint( L, 1 );
74 playlist_t *p_playlist = vlclua_get_playlist_internal( L );
75 playlist_Skip( p_playlist, i_skip );
79 static int vlclua_playlist_play( lua_State * L )
81 playlist_t *p_playlist = vlclua_get_playlist_internal( L );
82 playlist_Play( p_playlist );
86 static int vlclua_playlist_pause( lua_State * L )
88 playlist_t *p_playlist = vlclua_get_playlist_internal( L );
89 playlist_Pause( p_playlist );
93 static int vlclua_playlist_stop( lua_State * L )
95 playlist_t *p_playlist = vlclua_get_playlist_internal( L );
96 playlist_Stop( p_playlist );
100 static int vlclua_playlist_clear( lua_State * L )
102 playlist_t *p_playlist = vlclua_get_playlist_internal( L );
103 playlist_Stop( p_playlist ); /* Isn't this already implied by Clear? */
104 playlist_Clear( p_playlist, pl_Unlocked );
108 static int vlclua_playlist_repeat( lua_State * L )
110 playlist_t *p_playlist = vlclua_get_playlist_internal( L );
111 int i_ret = vlclua_var_toggle_or_set( L, p_playlist, "repeat" );
115 static int vlclua_playlist_loop( lua_State * L )
117 playlist_t *p_playlist = vlclua_get_playlist_internal( L );
118 int i_ret = vlclua_var_toggle_or_set( L, p_playlist, "loop" );
122 static int vlclua_playlist_random( lua_State * L )
124 playlist_t *p_playlist = vlclua_get_playlist_internal( L );
125 int i_ret = vlclua_var_toggle_or_set( L, p_playlist, "random" );
129 static int vlclua_playlist_goto( lua_State * L )
131 int i_id = luaL_checkint( L, 1 );
132 playlist_t *p_playlist = vlclua_get_playlist_internal( L );
134 int i_ret = playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
136 playlist_ItemGetById( p_playlist, i_id ) );
138 return vlclua_push_ret( L, i_ret );
141 static int vlclua_playlist_add( lua_State *L )
144 vlc_object_t *p_this = vlclua_get_this( L );
145 playlist_t *p_playlist = vlclua_get_playlist_internal( L );
146 i_count = vlclua_playlist_add_internal( p_this, L, p_playlist,
148 lua_pushinteger( L, i_count );
152 static int vlclua_playlist_enqueue( lua_State *L )
155 vlc_object_t *p_this = vlclua_get_this( L );
156 playlist_t *p_playlist = vlclua_get_playlist_internal( L );
157 i_count = vlclua_playlist_add_internal( p_this, L, p_playlist,
159 lua_pushinteger( L, i_count );
163 static void push_playlist_item( lua_State *L, playlist_item_t *p_item )
165 input_item_t *p_input = p_item->p_input;
167 i_flags = p_item->i_flags;
169 lua_pushinteger( L, p_item->i_id );
170 lua_setfield( L, -2, "id" );
172 #define CHECK_AND_SET_FLAG( name, label ) \
173 if( i_flags & PLAYLIST_ ## name ## _FLAG ) \
175 lua_pushboolean( L, 1 ); \
176 lua_setfield( L, -2, #label ); \
178 CHECK_AND_SET_FLAG( SAVE, save )
179 CHECK_AND_SET_FLAG( SKIP, skip )
180 CHECK_AND_SET_FLAG( DBL, disabled )
181 CHECK_AND_SET_FLAG( RO, ro )
182 CHECK_AND_SET_FLAG( REMOVE, remove )
183 CHECK_AND_SET_FLAG( EXPANDED, expanded )
184 #undef CHECK_AND_SET_FLAG
185 lua_setfield( L, -2, "flags" );
188 char *psz_name = input_item_GetTitleFbName( p_input );
189 lua_pushstring( L, psz_name );
191 lua_setfield( L, -2, "name" );
192 lua_pushstring( L, p_input->psz_uri );
193 lua_setfield( L, -2, "path" );
194 if( p_input->i_duration < 0 )
195 lua_pushnumber( L, -1 );
197 lua_pushnumber( L, ((double)p_input->i_duration)*1e-6 );
198 lua_setfield( L, -2, "duration" );
199 lua_pushinteger( L, p_input->i_nb_played );
200 lua_setfield( L, -2, "nb_played" );
201 /* TODO: add (optional) info categories, meta, options, es */
203 if( p_item->i_children >= 0 )
206 lua_createtable( L, p_item->i_children, 0 );
207 for( i = 0; i < p_item->i_children; i++ )
209 push_playlist_item( L, p_item->pp_children[i] );
210 lua_rawseti( L, -2, i+1 );
212 lua_setfield( L, -2, "children" );
216 static int vlclua_playlist_get( lua_State *L )
218 playlist_t *p_playlist = vlclua_get_playlist_internal( L );
220 playlist_item_t *p_item = NULL;
222 if( lua_isnumber( L, 1 ) )
224 int i_id = lua_tointeger( L, 1 );
225 p_item = playlist_ItemGetById( p_playlist, i_id );
229 return 0; /* Should we return an error instead? */
232 else if( lua_isstring( L, 1 ) )
234 const char *psz_what = lua_tostring( L, 1 );
235 if( !strcasecmp( psz_what, "normal" )
236 || !strcasecmp( psz_what, "playlist" ) )
237 p_item = p_playlist->p_playing;
238 else if( !strcasecmp( psz_what, "ml" )
239 || !strcasecmp( psz_what, "media library" ) )
240 p_item = p_playlist->p_media_library;
241 else if( !strcasecmp( psz_what, "root" ) )
242 p_item = p_playlist->p_root;
245 /* currently, psz_what must be SD module's longname! */
246 p_item = playlist_ChildSearchName( p_playlist->p_root, psz_what );
251 return 0; /* Should we return an error instead? */
257 p_item = p_playlist->p_root;
259 push_playlist_item( L, p_item );
264 static int vlclua_playlist_search( lua_State *L )
266 playlist_t *p_playlist = vlclua_get_playlist_internal( L );
267 const char *psz_string = luaL_optstring( L, 1, "" );
269 playlist_LiveSearchUpdate( p_playlist, p_playlist->p_root, psz_string, true );
271 push_playlist_item( L, p_playlist->p_root );
275 static int vlclua_playlist_current( lua_State *L )
277 playlist_t *p_playlist = vlclua_get_playlist_internal( L );
278 input_thread_t *p_input = playlist_CurrentInput( p_playlist );
283 input_item_t *p_item = input_GetItem( p_input );
286 vlc_object_release( p_input );
289 #warning Indexing input items by ID is unsafe,
290 lua_pushinteger( L, id );
294 static int vlc_sort_key_from_string( const char *psz_name )
298 const char *psz_name;
302 { "title", SORT_TITLE },
303 { "title nodes first", SORT_TITLE_NODES_FIRST },
304 { "artist", SORT_ARTIST },
305 { "genre", SORT_GENRE },
306 { "random", SORT_RANDOM },
307 { "duration", SORT_DURATION },
308 { "title numeric", SORT_TITLE_NUMERIC },
309 { "album", SORT_ALBUM },
312 for( i = 0; pp_keys[i].psz_name; i++ )
314 if( !strcmp( psz_name, pp_keys[i].psz_name ) )
315 return pp_keys[i].i_key;
320 static int vlclua_playlist_sort( lua_State *L )
322 /* allow setting the different sort keys */
323 int i_mode = vlc_sort_key_from_string( luaL_checkstring( L, 1 ) );
325 return luaL_error( L, "Invalid search key." );
326 int i_type = luaL_optboolean( L, 2, 0 ) ? ORDER_REVERSE : ORDER_NORMAL;
327 playlist_t *p_playlist = vlclua_get_playlist_internal( L );
329 int i_ret = playlist_RecursiveNodeSort( p_playlist, p_playlist->p_playing,
332 return vlclua_push_ret( L, i_ret );
335 static int vlclua_playlist_status( lua_State *L )
337 playlist_t *p_playlist = vlclua_get_playlist_internal( L );
339 switch( playlist_Status( p_playlist ) )
341 case PLAYLIST_STOPPED:
342 lua_pushliteral( L, "stopped" );
344 case PLAYLIST_RUNNING:
345 lua_pushliteral( L, "playing" );
347 case PLAYLIST_PAUSED:
348 lua_pushliteral( L, "paused" );
351 lua_pushliteral( L, "unknown" );
358 /*****************************************************************************
360 *****************************************************************************/
361 static const luaL_Reg vlclua_playlist_reg[] = {
362 { "prev", vlclua_playlist_prev },
363 { "next", vlclua_playlist_next },
364 { "skip", vlclua_playlist_skip },
365 { "play", vlclua_playlist_play },
366 { "pause", vlclua_playlist_pause },
367 { "stop", vlclua_playlist_stop },
368 { "clear", vlclua_playlist_clear },
369 { "repeat", vlclua_playlist_repeat }, // repeat is a reserved lua keyword...
370 { "repeat_", vlclua_playlist_repeat }, // ... provide repeat_ too.
371 { "loop", vlclua_playlist_loop },
372 { "random", vlclua_playlist_random },
373 { "goto", vlclua_playlist_goto },
374 { "add", vlclua_playlist_add },
375 { "enqueue", vlclua_playlist_enqueue },
376 { "get", vlclua_playlist_get },
377 { "search", vlclua_playlist_search },
378 { "current", vlclua_playlist_current },
379 { "sort", vlclua_playlist_sort },
380 { "status", vlclua_playlist_status },
384 void luaopen_playlist( lua_State *L )
387 luaL_register( L, NULL, vlclua_playlist_reg );
388 lua_setfield( L, -2, "playlist" );