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_Hold( p_this );
57 void vlclua_release_playlist_internal( playlist_t *p_playlist )
59 vlc_object_release( p_playlist );
62 static int vlclua_playlist_prev( lua_State * L )
64 playlist_t *p_playlist = vlclua_get_playlist_internal( L );
65 playlist_Prev( p_playlist );
66 vlclua_release_playlist_internal( p_playlist );
70 static int vlclua_playlist_next( lua_State * L )
72 playlist_t *p_playlist = vlclua_get_playlist_internal( L );
73 playlist_Next( p_playlist );
74 vlclua_release_playlist_internal( p_playlist );
78 static int vlclua_playlist_skip( lua_State * L )
80 int i_skip = luaL_checkint( L, 1 );
81 playlist_t *p_playlist = vlclua_get_playlist_internal( L );
82 playlist_Skip( p_playlist, i_skip );
83 vlclua_release_playlist_internal( p_playlist );
87 static int vlclua_playlist_play( lua_State * L )
89 playlist_t *p_playlist = vlclua_get_playlist_internal( L );
90 playlist_Play( p_playlist );
91 vlclua_release_playlist_internal( p_playlist );
95 static int vlclua_playlist_pause( lua_State * L )
97 playlist_t *p_playlist = vlclua_get_playlist_internal( L );
98 playlist_Pause( p_playlist );
99 vlclua_release_playlist_internal( p_playlist );
103 static int vlclua_playlist_stop( lua_State * L )
105 playlist_t *p_playlist = vlclua_get_playlist_internal( L );
106 playlist_Stop( p_playlist );
107 vlclua_release_playlist_internal( p_playlist );
111 static int vlclua_playlist_clear( lua_State * L )
113 playlist_t *p_playlist = vlclua_get_playlist_internal( L );
114 playlist_Stop( p_playlist ); /* Isn't this already implied by Clear? */
115 playlist_Clear( p_playlist, pl_Unlocked );
116 vlclua_release_playlist_internal( p_playlist );
120 static int vlclua_playlist_repeat( lua_State * L )
122 playlist_t *p_playlist = vlclua_get_playlist_internal( L );
123 int i_ret = vlclua_var_toggle_or_set( L, p_playlist, "repeat" );
124 vlclua_release_playlist_internal( p_playlist );
128 static int vlclua_playlist_loop( lua_State * L )
130 playlist_t *p_playlist = vlclua_get_playlist_internal( L );
131 int i_ret = vlclua_var_toggle_or_set( L, p_playlist, "loop" );
132 vlclua_release_playlist_internal( p_playlist );
136 static int vlclua_playlist_random( lua_State * L )
138 playlist_t *p_playlist = vlclua_get_playlist_internal( L );
139 int i_ret = vlclua_var_toggle_or_set( L, p_playlist, "random" );
140 vlclua_release_playlist_internal( p_playlist );
144 static int vlclua_playlist_goto( lua_State * L )
146 int i_id = luaL_checkint( L, 1 );
147 playlist_t *p_playlist = vlclua_get_playlist_internal( L );
149 int i_ret = playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
151 playlist_ItemGetById( p_playlist, i_id ) );
153 vlclua_release_playlist_internal( p_playlist );
154 return vlclua_push_ret( L, i_ret );
157 static int vlclua_playlist_add( lua_State *L )
160 vlc_object_t *p_this = vlclua_get_this( L );
161 playlist_t *p_playlist = vlclua_get_playlist_internal( L );
162 i_count = vlclua_playlist_add_internal( p_this, L, p_playlist,
164 vlclua_release_playlist_internal( p_playlist );
165 lua_pushinteger( L, i_count );
169 static int vlclua_playlist_enqueue( lua_State *L )
172 vlc_object_t *p_this = vlclua_get_this( L );
173 playlist_t *p_playlist = vlclua_get_playlist_internal( L );
174 i_count = vlclua_playlist_add_internal( p_this, L, p_playlist,
176 vlclua_release_playlist_internal( p_playlist );
177 lua_pushinteger( L, i_count );
181 static void push_playlist_item( lua_State *L, playlist_item_t *p_item );
182 static void push_playlist_item( lua_State *L, playlist_item_t *p_item )
184 input_item_t *p_input = p_item->p_input;
186 i_flags = p_item->i_flags;
188 lua_pushinteger( L, p_item->i_id );
189 lua_setfield( L, -2, "id" );
191 #define CHECK_AND_SET_FLAG( name, label ) \
192 if( i_flags & PLAYLIST_ ## name ## _FLAG ) \
194 lua_pushboolean( L, 1 ); \
195 lua_setfield( L, -2, #label ); \
197 CHECK_AND_SET_FLAG( SAVE, save )
198 CHECK_AND_SET_FLAG( SKIP, skip )
199 CHECK_AND_SET_FLAG( DBL, disabled )
200 CHECK_AND_SET_FLAG( RO, ro )
201 CHECK_AND_SET_FLAG( REMOVE, remove )
202 CHECK_AND_SET_FLAG( EXPANDED, expanded )
203 #undef CHECK_AND_SET_FLAG
204 lua_setfield( L, -2, "flags" );
207 lua_pushstring( L, p_input->psz_name );
208 lua_setfield( L, -2, "name" );
209 lua_pushstring( L, p_input->psz_uri );
210 lua_setfield( L, -2, "path" );
211 if( p_input->i_duration < 0 )
212 lua_pushnumber( L, -1 );
214 lua_pushnumber( L, ((double)p_input->i_duration)*1e-6 );
215 lua_setfield( L, -2, "duration" );
216 lua_pushinteger( L, p_input->i_nb_played );
217 lua_setfield( L, -2, "nb_played" );
218 /* TODO: add (optional) info categories, meta, options, es */
220 if( p_item->i_children >= 0 )
223 lua_createtable( L, p_item->i_children, 0 );
224 for( i = 0; i < p_item->i_children; i++ )
226 push_playlist_item( L, p_item->pp_children[i] );
227 lua_rawseti( L, -2, i+1 );
229 lua_setfield( L, -2, "children" );
233 static int vlclua_playlist_get( lua_State *L )
235 playlist_t *p_playlist = vlclua_get_playlist_internal( L );
237 int b_category = luaL_optboolean( L, 2, 1 ); /* Default to tree playlist (discared when 1st argument is a playlist_item's id) */
238 playlist_item_t *p_item = NULL;
240 if( lua_isnumber( L, 1 ) )
242 int i_id = lua_tointeger( L, 1 );
243 p_item = playlist_ItemGetById( p_playlist, i_id );
247 vlclua_release_playlist_internal( p_playlist );
248 return 0; /* Should we return an error instead? */
251 else if( lua_isstring( L, 1 ) )
253 const char *psz_what = lua_tostring( L, 1 );
254 if( !strcasecmp( psz_what, "normal" )
255 || !strcasecmp( psz_what, "playlist" ) )
256 p_item = b_category ? p_playlist->p_local_category
257 : p_playlist->p_local_onelevel;
258 else if( !strcasecmp( psz_what, "ml" )
259 || !strcasecmp( psz_what, "media library" ) )
260 p_item = b_category ? p_playlist->p_ml_category
261 : p_playlist->p_ml_onelevel;
262 else if( !strcasecmp( psz_what, "root" ) )
263 p_item = b_category ? p_playlist->p_root_category
264 : p_playlist->p_root_onelevel;
267 #ifdef FIX_THAT_CODE_NOT_TO_MESS_WITH_PLAYLIST_INTERNALS
268 for( int i = 0; i < p_playlist->i_sds; i++ )
270 if( !strcasecmp( psz_what,
271 p_playlist->pp_sds[i]->p_sd->psz_module ) )
273 p_item = b_category ? p_playlist->pp_sds[i]->p_cat
274 : p_playlist->pp_sds[i]->p_one;
279 # warning "Don't access playlist iternal, broken code here."
285 vlclua_release_playlist_internal( p_playlist );
286 return 0; /* Should we return an error instead? */
292 p_item = b_category ? p_playlist->p_root_category
293 : p_playlist->p_root_onelevel;
295 push_playlist_item( L, p_item );
297 vlclua_release_playlist_internal( p_playlist );
301 static int vlclua_playlist_search( lua_State *L )
303 playlist_t *p_playlist = vlclua_get_playlist_internal( L );
304 const char *psz_string = luaL_optstring( L, 1, "" );
305 int b_category = luaL_optboolean( L, 2, 1 ); /* default to category */
306 playlist_item_t *p_item = b_category ? p_playlist->p_root_category
307 : p_playlist->p_root_onelevel;
309 playlist_LiveSearchUpdate( p_playlist, p_item, psz_string );
311 push_playlist_item( L, p_item );
312 vlclua_release_playlist_internal( p_playlist );
316 static int vlclua_playlist_current( lua_State *L )
318 playlist_t *p_playlist = vlclua_get_playlist_internal( L );
319 input_thread_t *p_input = playlist_CurrentInput( p_playlist );
324 input_item_t *p_item = input_GetItem( p_input );
327 vlc_object_release( p_input );
330 #warning Indexing input items by ID is unsafe,
331 lua_pushinteger( L, id );
332 vlclua_release_playlist_internal( p_playlist );
336 static int vlc_sort_key_from_string( const char *psz_name )
340 const char *psz_name;
344 { "title", SORT_TITLE },
345 { "title nodes first", SORT_TITLE_NODES_FIRST },
346 { "artist", SORT_ARTIST },
347 { "genre", SORT_GENRE },
348 { "random", SORT_RANDOM },
349 { "duration", SORT_DURATION },
350 { "title numeric", SORT_TITLE_NUMERIC },
351 { "album", SORT_ALBUM },
354 for( i = 0; pp_keys[i].psz_name; i++ )
356 if( !strcmp( psz_name, pp_keys[i].psz_name ) )
357 return pp_keys[i].i_key;
362 static int vlclua_playlist_sort( lua_State *L )
364 /* allow setting the different sort keys */
365 int i_mode = vlc_sort_key_from_string( luaL_checkstring( L, 1 ) );
367 return luaL_error( L, "Invalid search key." );
368 int i_type = luaL_optboolean( L, 2, 0 ) ? ORDER_REVERSE : ORDER_NORMAL;
369 int b_category = luaL_optboolean( L, 3, 1 ); /* default to category */
370 playlist_t *p_playlist = vlclua_get_playlist_internal( L );
372 playlist_item_t *p_root = b_category ? p_playlist->p_local_category
373 : p_playlist->p_local_onelevel;
374 int i_ret = playlist_RecursiveNodeSort( p_playlist, p_root, i_mode,
377 vlclua_release_playlist_internal( p_playlist );
378 return vlclua_push_ret( L, i_ret );
381 static int vlclua_playlist_status( lua_State *L )
383 playlist_t *p_playlist = vlclua_get_playlist_internal( L );
385 switch( playlist_Status( p_playlist ) )
387 case PLAYLIST_STOPPED:
388 lua_pushstring( L, "stopped" );
390 case PLAYLIST_RUNNING:
391 lua_pushstring( L, "playing" );
393 case PLAYLIST_PAUSED:
394 lua_pushstring( L, "paused" );
397 lua_pushstring( L, "unknown" );
401 vlclua_release_playlist_internal( p_playlist );
405 /*****************************************************************************
407 *****************************************************************************/
408 static const luaL_Reg vlclua_playlist_reg[] = {
409 { "prev", vlclua_playlist_prev },
410 { "next", vlclua_playlist_next },
411 { "skip", vlclua_playlist_skip },
412 { "play", vlclua_playlist_play },
413 { "pause", vlclua_playlist_pause },
414 { "stop", vlclua_playlist_stop },
415 { "clear", vlclua_playlist_clear },
416 { "repeat", vlclua_playlist_repeat }, // repeat is a reserved lua keyword...
417 { "repeat_", vlclua_playlist_repeat }, // ... provide repeat_ too.
418 { "loop", vlclua_playlist_loop },
419 { "random", vlclua_playlist_random },
420 { "goto", vlclua_playlist_goto },
421 { "add", vlclua_playlist_add },
422 { "enqueue", vlclua_playlist_enqueue },
423 { "get", vlclua_playlist_get },
424 { "search", vlclua_playlist_search },
425 { "current", vlclua_playlist_current },
426 { "sort", vlclua_playlist_sort },
427 { "status", vlclua_playlist_status },
431 void luaopen_playlist( lua_State *L )
434 luaL_register( L, NULL, vlclua_playlist_reg );
435 lua_setfield( L, -2, "playlist" );