1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2007-2011 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_delete( lua_State * L )
143 int i_id = luaL_checkint( L, 1 );
144 playlist_t *p_playlist = vlclua_get_playlist_internal( L );
146 int i_ret = playlist_DeleteFromInput(p_playlist, playlist_ItemGetById( p_playlist, i_id ) -> p_input, true );
148 return vlclua_push_ret( L, i_ret );
151 static int vlclua_playlist_add( lua_State *L )
154 vlc_object_t *p_this = vlclua_get_this( L );
155 playlist_t *p_playlist = vlclua_get_playlist_internal( L );
156 i_count = vlclua_playlist_add_internal( p_this, L, p_playlist,
158 lua_pushinteger( L, i_count );
162 static int vlclua_playlist_enqueue( lua_State *L )
165 vlc_object_t *p_this = vlclua_get_this( L );
166 playlist_t *p_playlist = vlclua_get_playlist_internal( L );
167 i_count = vlclua_playlist_add_internal( p_this, L, p_playlist,
169 lua_pushinteger( L, i_count );
173 static void push_playlist_item( lua_State *L, playlist_item_t *p_item )
175 input_item_t *p_input = p_item->p_input;
177 i_flags = p_item->i_flags;
179 lua_pushinteger( L, p_item->i_id );
180 lua_setfield( L, -2, "id" );
182 #define CHECK_AND_SET_FLAG( name, label ) \
183 if( i_flags & PLAYLIST_ ## name ## _FLAG ) \
185 lua_pushboolean( L, 1 ); \
186 lua_setfield( L, -2, #label ); \
188 CHECK_AND_SET_FLAG( SAVE, save )
189 CHECK_AND_SET_FLAG( SKIP, skip )
190 CHECK_AND_SET_FLAG( DBL, disabled )
191 CHECK_AND_SET_FLAG( RO, ro )
192 CHECK_AND_SET_FLAG( REMOVE, remove )
193 CHECK_AND_SET_FLAG( EXPANDED, expanded )
194 #undef CHECK_AND_SET_FLAG
195 lua_setfield( L, -2, "flags" );
198 char *psz_name = input_item_GetTitleFbName( p_input );
199 lua_pushstring( L, psz_name );
201 lua_setfield( L, -2, "name" );
202 lua_pushstring( L, p_input->psz_uri );
203 lua_setfield( L, -2, "path" );
204 if( p_input->i_duration < 0 )
205 lua_pushnumber( L, -1 );
207 lua_pushnumber( L, ((double)p_input->i_duration)*1e-6 );
208 lua_setfield( L, -2, "duration" );
209 lua_pushinteger( L, p_input->i_nb_played );
210 lua_setfield( L, -2, "nb_played" );
211 /* TODO: add (optional) info categories, meta, options, es */
213 if( p_item->i_children >= 0 )
216 lua_createtable( L, p_item->i_children, 0 );
217 for( i = 0; i < p_item->i_children; i++ )
219 push_playlist_item( L, p_item->pp_children[i] );
220 lua_rawseti( L, -2, i+1 );
222 lua_setfield( L, -2, "children" );
226 static int vlclua_playlist_get( lua_State *L )
228 playlist_t *p_playlist = vlclua_get_playlist_internal( L );
230 playlist_item_t *p_item = NULL;
232 if( lua_isnumber( L, 1 ) )
234 int i_id = lua_tointeger( L, 1 );
235 p_item = playlist_ItemGetById( p_playlist, i_id );
239 return 0; /* Should we return an error instead? */
242 else if( lua_isstring( L, 1 ) )
244 const char *psz_what = lua_tostring( L, 1 );
245 if( !strcasecmp( psz_what, "normal" )
246 || !strcasecmp( psz_what, "playlist" ) )
247 p_item = p_playlist->p_playing;
248 else if( !strcasecmp( psz_what, "ml" )
249 || !strcasecmp( psz_what, "media library" ) )
250 p_item = p_playlist->p_media_library;
251 else if( !strcasecmp( psz_what, "root" ) )
252 p_item = p_playlist->p_root;
255 /* currently, psz_what must be SD module's longname! */
256 p_item = playlist_ChildSearchName( p_playlist->p_root, psz_what );
261 return 0; /* Should we return an error instead? */
267 p_item = p_playlist->p_root;
269 push_playlist_item( L, p_item );
274 static int vlclua_playlist_search( lua_State *L )
276 playlist_t *p_playlist = vlclua_get_playlist_internal( L );
277 const char *psz_string = luaL_optstring( L, 1, "" );
279 playlist_LiveSearchUpdate( p_playlist, p_playlist->p_root, psz_string, true );
281 push_playlist_item( L, p_playlist->p_root );
285 static int vlclua_playlist_current( lua_State *L )
287 playlist_t *p_playlist = vlclua_get_playlist_internal( L );
288 input_thread_t *p_input = playlist_CurrentInput( p_playlist );
293 input_item_t *p_item = input_GetItem( p_input );
296 vlc_object_release( p_input );
299 #warning Indexing input items by ID is unsafe,
300 lua_pushinteger( L, id );
304 static int vlc_sort_key_from_string( const char *psz_name )
308 const char *psz_name;
312 { "title", SORT_TITLE },
313 { "title nodes first", SORT_TITLE_NODES_FIRST },
314 { "artist", SORT_ARTIST },
315 { "genre", SORT_GENRE },
316 { "random", SORT_RANDOM },
317 { "duration", SORT_DURATION },
318 { "title numeric", SORT_TITLE_NUMERIC },
319 { "album", SORT_ALBUM },
322 for( i = 0; pp_keys[i].psz_name; i++ )
324 if( !strcmp( psz_name, pp_keys[i].psz_name ) )
325 return pp_keys[i].i_key;
330 static int vlclua_playlist_sort( lua_State *L )
332 /* allow setting the different sort keys */
333 int i_mode = vlc_sort_key_from_string( luaL_checkstring( L, 1 ) );
335 return luaL_error( L, "Invalid search key." );
336 int i_type = luaL_optboolean( L, 2, 0 ) ? ORDER_REVERSE : ORDER_NORMAL;
337 playlist_t *p_playlist = vlclua_get_playlist_internal( L );
339 int i_ret = playlist_RecursiveNodeSort( p_playlist, p_playlist->p_playing,
342 return vlclua_push_ret( L, i_ret );
345 static int vlclua_playlist_status( lua_State *L )
347 playlist_t *p_playlist = vlclua_get_playlist_internal( L );
349 switch( playlist_Status( p_playlist ) )
351 case PLAYLIST_STOPPED:
352 lua_pushliteral( L, "stopped" );
354 case PLAYLIST_RUNNING:
355 lua_pushliteral( L, "playing" );
357 case PLAYLIST_PAUSED:
358 lua_pushliteral( L, "paused" );
361 lua_pushliteral( L, "unknown" );
368 /*****************************************************************************
370 *****************************************************************************/
371 static const luaL_Reg vlclua_playlist_reg[] = {
372 { "prev", vlclua_playlist_prev },
373 { "next", vlclua_playlist_next },
374 { "skip", vlclua_playlist_skip },
375 { "play", vlclua_playlist_play },
376 { "pause", vlclua_playlist_pause },
377 { "stop", vlclua_playlist_stop },
378 { "clear", vlclua_playlist_clear },
379 { "repeat", vlclua_playlist_repeat }, // repeat is a reserved lua keyword...
380 { "repeat_", vlclua_playlist_repeat }, // ... provide repeat_ too.
381 { "loop", vlclua_playlist_loop },
382 { "random", vlclua_playlist_random },
383 { "goto", vlclua_playlist_goto },
384 { "add", vlclua_playlist_add },
385 { "enqueue", vlclua_playlist_enqueue },
386 { "get", vlclua_playlist_get },
387 { "search", vlclua_playlist_search },
388 { "current", vlclua_playlist_current },
389 { "sort", vlclua_playlist_sort },
390 { "status", vlclua_playlist_status },
391 { "delete", vlclua_playlist_delete },
395 void luaopen_playlist( lua_State *L )
398 luaL_register( L, NULL, vlclua_playlist_reg );
399 lua_setfield( L, -2, "playlist" );