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_Yield( 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 );
61 vlc_object_release( p_playlist );
65 static int vlclua_playlist_next( lua_State * L )
67 playlist_t *p_playlist = vlclua_get_playlist_internal( L );
68 playlist_Next( p_playlist );
69 vlc_object_release( p_playlist );
73 static int vlclua_playlist_skip( lua_State * L )
75 int i_skip = luaL_checkint( L, 1 );
76 playlist_t *p_playlist = vlclua_get_playlist_internal( L );
77 playlist_Skip( p_playlist, i_skip );
78 vlc_object_release( p_playlist );
82 static int vlclua_playlist_play( lua_State * L )
84 playlist_t *p_playlist = vlclua_get_playlist_internal( L );
85 playlist_Play( p_playlist );
86 vlc_object_release( p_playlist );
90 static int vlclua_playlist_pause( lua_State * L )
92 playlist_t *p_playlist = vlclua_get_playlist_internal( L );
93 playlist_Pause( p_playlist );
94 vlc_object_release( p_playlist );
98 static int vlclua_playlist_stop( lua_State * L )
100 playlist_t *p_playlist = vlclua_get_playlist_internal( L );
101 playlist_Stop( p_playlist );
102 vlc_object_release( p_playlist );
106 static int vlclua_playlist_clear( lua_State * L )
108 playlist_t *p_playlist = vlclua_get_playlist_internal( L );
109 playlist_Stop( p_playlist ); /* Isn't this already implied by Clear? */
110 playlist_Clear( p_playlist, false );
111 vlc_object_release( p_playlist );
115 static int vlclua_playlist_repeat( 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, "repeat" );
119 vlc_object_release( p_playlist );
123 static int vlclua_playlist_loop( lua_State * L )
125 playlist_t *p_playlist = vlclua_get_playlist_internal( L );
126 int i_ret = vlclua_var_toggle_or_set( L, p_playlist, "loop" );
127 vlc_object_release( p_playlist );
131 static int vlclua_playlist_random( lua_State * L )
133 playlist_t *p_playlist = vlclua_get_playlist_internal( L );
134 int i_ret = vlclua_var_toggle_or_set( L, p_playlist, "random" );
135 vlc_object_release( p_playlist );
139 static int vlclua_playlist_goto( lua_State * L )
141 int i_id = luaL_checkint( L, 1 );
142 playlist_t *p_playlist = vlclua_get_playlist_internal( L );
143 int i_ret = playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
145 playlist_ItemGetById( p_playlist, i_id,
147 vlc_object_release( p_playlist );
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 vlc_object_release( p_playlist );
159 lua_pushinteger( L, i_count );
163 static int vlclua_playlist_enqueue( lua_State *L )
166 vlc_object_t *p_this = vlclua_get_this( L );
167 playlist_t *p_playlist = vlclua_get_playlist_internal( L );
168 i_count = vlclua_playlist_add_internal( p_this, L, p_playlist,
170 vlc_object_release( p_playlist );
171 lua_pushinteger( L, i_count );
175 static void push_playlist_item( lua_State *L, playlist_item_t *p_item );
176 static void push_playlist_item( lua_State *L, playlist_item_t *p_item )
178 input_item_t *p_input = p_item->p_input;
179 int i_flags = p_item->i_flags;
181 lua_pushinteger( L, p_item->i_id );
182 lua_setfield( L, -2, "id" );
184 #define CHECK_AND_SET_FLAG( name, label ) \
185 if( i_flags & PLAYLIST_ ## name ## _FLAG ) \
187 lua_pushboolean( L, 1 ); \
188 lua_setfield( L, -2, #label ); \
190 CHECK_AND_SET_FLAG( SAVE, save )
191 CHECK_AND_SET_FLAG( SKIP, skip )
192 CHECK_AND_SET_FLAG( DBL, disabled )
193 CHECK_AND_SET_FLAG( RO, ro )
194 CHECK_AND_SET_FLAG( REMOVE, remove )
195 CHECK_AND_SET_FLAG( EXPANDED, expanded )
196 #undef CHECK_AND_SET_FLAG
197 lua_setfield( L, -2, "flags" );
200 lua_pushstring( L, p_input->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 );
229 int b_category = luaL_optboolean( L, 2, 1 ); /* Default to tree playlist (discared when 1st argument is a playlist_item's id) */
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, true );
238 vlc_object_release( p_playlist );
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 = b_category ? p_playlist->p_local_category
248 : p_playlist->p_local_onelevel;
249 else if( !strcasecmp( psz_what, "ml" )
250 || !strcasecmp( psz_what, "media library" ) )
251 p_item = b_category ? p_playlist->p_ml_category
252 : p_playlist->p_ml_onelevel;
253 else if( !strcasecmp( psz_what, "root" ) )
254 p_item = b_category ? p_playlist->p_root_category
255 : p_playlist->p_root_onelevel;
259 for( i = 0; i < p_playlist->i_sds; i++ )
261 if( !strcasecmp( psz_what,
262 p_playlist->pp_sds[i]->p_sd->psz_module ) )
264 p_item = b_category ? p_playlist->pp_sds[i]->p_cat
265 : p_playlist->pp_sds[i]->p_one;
271 vlc_object_release( p_playlist );
272 return 0; /* Should we return an error instead? */
278 p_item = b_category ? p_playlist->p_root_category
279 : p_playlist->p_root_onelevel;
281 push_playlist_item( L, p_item );
282 vlc_object_release( p_playlist );
286 static int vlclua_playlist_search( lua_State *L )
288 playlist_t *p_playlist = vlclua_get_playlist_internal( L );
289 const char *psz_string = luaL_optstring( L, 1, "" );
290 int b_category = luaL_optboolean( L, 2, 1 ); /* default to category */
291 playlist_item_t *p_item = b_category ? p_playlist->p_root_category
292 : p_playlist->p_root_onelevel;
293 playlist_LiveSearchUpdate( p_playlist, p_item, psz_string );
294 push_playlist_item( L, p_item );
295 vlc_object_release( p_playlist );
299 static int vlclua_playlist_current( lua_State *L )
301 playlist_t *p_playlist = vlclua_get_playlist_internal( L );
302 lua_pushinteger( L, var_GetInteger( p_playlist, "playlist-current" ) );
303 vlc_object_release( p_playlist );
307 static int vlc_sort_key_from_string( const char *psz_name )
311 const char *psz_name;
315 { "title", SORT_TITLE },
316 { "title nodes first", SORT_TITLE_NODES_FIRST },
317 { "artist", SORT_ARTIST },
318 { "genre", SORT_GENRE },
319 { "random", SORT_RANDOM },
320 { "duration", SORT_DURATION },
321 { "title numeric", SORT_TITLE_NUMERIC },
322 { "album", SORT_ALBUM },
325 for( i = 0; pp_keys[i].psz_name; i++ )
327 if( !strcmp( psz_name, pp_keys[i].psz_name ) )
328 return pp_keys[i].i_key;
333 static int vlclua_playlist_sort( lua_State *L )
335 /* allow setting the different sort keys */
336 int i_mode = vlc_sort_key_from_string( luaL_checkstring( L, 1 ) );
338 return luaL_error( L, "Invalid search key." );
339 int i_type = luaL_optboolean( L, 2, 0 ) ? ORDER_REVERSE : ORDER_NORMAL;
340 int b_category = luaL_optboolean( L, 3, 1 ); /* default to category */
341 playlist_t *p_playlist = vlclua_get_playlist_internal( L );
342 playlist_item_t *p_root = b_category ? p_playlist->p_local_category
343 : p_playlist->p_local_onelevel;
344 int i_ret = playlist_RecursiveNodeSort( p_playlist, p_root, i_mode,
346 vlc_object_release( p_playlist );
347 return vlclua_push_ret( L, i_ret );
350 /* FIXME: split this in 3 different functions? */
351 static int vlclua_playlist_status( lua_State *L )
353 intf_thread_t *p_intf = (intf_thread_t *)vlclua_get_this( L );
354 playlist_t *p_playlist = pl_Yield( p_intf );
357 lua_settop( L, 0 );*/
358 if( p_playlist->p_input )
361 input_item_GetURI( input_GetItem( p_playlist->p_input ) );
362 lua_pushstring( L, psz_uri );
364 lua_pushnumber( L, config_GetInt( p_intf, "volume" ) );*/
365 vlc_mutex_lock( &p_playlist->object_lock );
366 switch( p_playlist->status.i_status )
368 case PLAYLIST_STOPPED:
369 lua_pushstring( L, "stopped" );
371 case PLAYLIST_RUNNING:
372 lua_pushstring( L, "playing" );
374 case PLAYLIST_PAUSED:
375 lua_pushstring( L, "paused" );
378 lua_pushstring( L, "unknown" );
381 vlc_mutex_unlock( &p_playlist->object_lock );
386 lua_pushstring( L, "stopped" );
388 vlc_object_release( p_playlist );
392 /*****************************************************************************
394 *****************************************************************************/
395 static const luaL_Reg vlclua_playlist_reg[] = {
396 { "prev", vlclua_playlist_prev },
397 { "next", vlclua_playlist_next },
398 { "skip", vlclua_playlist_skip },
399 { "play", vlclua_playlist_play },
400 { "pause", vlclua_playlist_pause },
401 { "stop", vlclua_playlist_stop },
402 { "clear", vlclua_playlist_clear },
403 { "repeat", vlclua_playlist_repeat },
404 { "loop", vlclua_playlist_loop },
405 { "random", vlclua_playlist_random },
406 { "goto", vlclua_playlist_goto },
407 { "add", vlclua_playlist_add },
408 { "enqueue", vlclua_playlist_enqueue },
409 { "get", vlclua_playlist_get },
410 { "search", vlclua_playlist_search },
411 { "current", vlclua_playlist_current },
412 { "sort", vlclua_playlist_sort },
413 { "status", vlclua_playlist_status },
417 void luaopen_playlist( lua_State *L )
420 luaL_register( L, NULL, vlclua_playlist_reg );
421 lua_setfield( L, -2, "playlist" );