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 );
164 static void push_playlist_item( lua_State *L, playlist_item_t *p_item )
166 input_item_t *p_input = p_item->p_input;
168 i_flags = p_item->i_flags;
170 lua_pushinteger( L, p_item->i_id );
171 lua_setfield( L, -2, "id" );
173 #define CHECK_AND_SET_FLAG( name, label ) \
174 if( i_flags & PLAYLIST_ ## name ## _FLAG ) \
176 lua_pushboolean( L, 1 ); \
177 lua_setfield( L, -2, #label ); \
179 CHECK_AND_SET_FLAG( SAVE, save )
180 CHECK_AND_SET_FLAG( SKIP, skip )
181 CHECK_AND_SET_FLAG( DBL, disabled )
182 CHECK_AND_SET_FLAG( RO, ro )
183 CHECK_AND_SET_FLAG( REMOVE, remove )
184 CHECK_AND_SET_FLAG( EXPANDED, expanded )
185 #undef CHECK_AND_SET_FLAG
186 lua_setfield( L, -2, "flags" );
189 lua_pushstring( L, p_input->psz_name );
190 lua_setfield( L, -2, "name" );
191 lua_pushstring( L, p_input->psz_uri );
192 lua_setfield( L, -2, "path" );
193 if( p_input->i_duration < 0 )
194 lua_pushnumber( L, -1 );
196 lua_pushnumber( L, ((double)p_input->i_duration)*1e-6 );
197 lua_setfield( L, -2, "duration" );
198 lua_pushinteger( L, p_input->i_nb_played );
199 lua_setfield( L, -2, "nb_played" );
200 /* TODO: add (optional) info categories, meta, options, es */
202 if( p_item->i_children >= 0 )
205 lua_createtable( L, p_item->i_children, 0 );
206 for( i = 0; i < p_item->i_children; i++ )
208 push_playlist_item( L, p_item->pp_children[i] );
209 lua_rawseti( L, -2, i+1 );
211 lua_setfield( L, -2, "children" );
215 static int vlclua_playlist_get( lua_State *L )
217 playlist_t *p_playlist = vlclua_get_playlist_internal( L );
219 playlist_item_t *p_item = NULL;
221 if( lua_isnumber( L, 1 ) )
223 int i_id = lua_tointeger( L, 1 );
224 p_item = playlist_ItemGetById( p_playlist, i_id );
228 return 0; /* Should we return an error instead? */
231 else if( lua_isstring( L, 1 ) )
233 const char *psz_what = lua_tostring( L, 1 );
234 if( !strcasecmp( psz_what, "normal" )
235 || !strcasecmp( psz_what, "playlist" ) )
236 p_item = p_playlist->p_playing;
237 else if( !strcasecmp( psz_what, "ml" )
238 || !strcasecmp( psz_what, "media library" ) )
239 p_item = p_playlist->p_media_library;
240 else if( !strcasecmp( psz_what, "root" ) )
241 p_item = p_playlist->p_root;
244 /* currently, psz_what must be SD module's longname! */
245 p_item = playlist_ChildSearchName( p_playlist->p_root, psz_what );
250 return 0; /* Should we return an error instead? */
256 p_item = p_playlist->p_root;
258 push_playlist_item( L, p_item );
263 static int vlclua_playlist_search( lua_State *L )
265 playlist_t *p_playlist = vlclua_get_playlist_internal( L );
266 const char *psz_string = luaL_optstring( L, 1, "" );
268 playlist_LiveSearchUpdate( p_playlist, p_playlist->p_root, psz_string, true );
270 push_playlist_item( L, p_playlist->p_root );
274 static int vlclua_playlist_current( lua_State *L )
276 playlist_t *p_playlist = vlclua_get_playlist_internal( L );
277 input_thread_t *p_input = playlist_CurrentInput( p_playlist );
282 input_item_t *p_item = input_GetItem( p_input );
285 vlc_object_release( p_input );
288 #warning Indexing input items by ID is unsafe,
289 lua_pushinteger( L, id );
293 static int vlc_sort_key_from_string( const char *psz_name )
297 const char *psz_name;
301 { "title", SORT_TITLE },
302 { "title nodes first", SORT_TITLE_NODES_FIRST },
303 { "artist", SORT_ARTIST },
304 { "genre", SORT_GENRE },
305 { "random", SORT_RANDOM },
306 { "duration", SORT_DURATION },
307 { "title numeric", SORT_TITLE_NUMERIC },
308 { "album", SORT_ALBUM },
311 for( i = 0; pp_keys[i].psz_name; i++ )
313 if( !strcmp( psz_name, pp_keys[i].psz_name ) )
314 return pp_keys[i].i_key;
319 static int vlclua_playlist_sort( lua_State *L )
321 /* allow setting the different sort keys */
322 int i_mode = vlc_sort_key_from_string( luaL_checkstring( L, 1 ) );
324 return luaL_error( L, "Invalid search key." );
325 int i_type = luaL_optboolean( L, 2, 0 ) ? ORDER_REVERSE : ORDER_NORMAL;
326 playlist_t *p_playlist = vlclua_get_playlist_internal( L );
328 int i_ret = playlist_RecursiveNodeSort( p_playlist, p_playlist->p_playing,
331 return vlclua_push_ret( L, i_ret );
334 static int vlclua_playlist_status( lua_State *L )
336 playlist_t *p_playlist = vlclua_get_playlist_internal( L );
338 switch( playlist_Status( p_playlist ) )
340 case PLAYLIST_STOPPED:
341 lua_pushstring( L, "stopped" );
343 case PLAYLIST_RUNNING:
344 lua_pushstring( L, "playing" );
346 case PLAYLIST_PAUSED:
347 lua_pushstring( L, "paused" );
350 lua_pushstring( L, "unknown" );
357 /*****************************************************************************
359 *****************************************************************************/
360 static const luaL_Reg vlclua_playlist_reg[] = {
361 { "prev", vlclua_playlist_prev },
362 { "next", vlclua_playlist_next },
363 { "skip", vlclua_playlist_skip },
364 { "play", vlclua_playlist_play },
365 { "pause", vlclua_playlist_pause },
366 { "stop", vlclua_playlist_stop },
367 { "clear", vlclua_playlist_clear },
368 { "repeat", vlclua_playlist_repeat }, // repeat is a reserved lua keyword...
369 { "repeat_", vlclua_playlist_repeat }, // ... provide repeat_ too.
370 { "loop", vlclua_playlist_loop },
371 { "random", vlclua_playlist_random },
372 { "goto", vlclua_playlist_goto },
373 { "add", vlclua_playlist_add },
374 { "enqueue", vlclua_playlist_enqueue },
375 { "get", vlclua_playlist_get },
376 { "search", vlclua_playlist_search },
377 { "current", vlclua_playlist_current },
378 { "sort", vlclua_playlist_sort },
379 { "status", vlclua_playlist_status },
383 void luaopen_playlist( lua_State *L )
386 luaL_register( L, NULL, vlclua_playlist_reg );
387 lua_setfield( L, -2, "playlist" );