]> git.sesse.net Git - vlc/blob - modules/lua/libs/playlist.c
playlist: move i_nb_played to playlist item, not the input item
[vlc] / modules / lua / libs / playlist.c
1 /*****************************************************************************
2  * playlist.c
3  *****************************************************************************
4  * Copyright (C) 2007-2011 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Antoine Cellerier <dionoea at videolan tod org>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #ifndef  _GNU_SOURCE
28 #   define  _GNU_SOURCE
29 #endif
30
31 #ifdef HAVE_CONFIG_H
32 # include "config.h"
33 #endif
34
35 #include <vlc_common.h>
36
37 #include <vlc_interface.h>
38 #include <vlc_playlist.h>
39
40 #include "../vlc.h"
41 #include "../libs.h"
42 #include "input.h"
43 #include "variables.h"
44 #include "misc.h"
45
46 void vlclua_set_playlist_internal( lua_State *L, playlist_t *pl )
47 {
48     vlclua_set_object( L, vlclua_set_playlist_internal, pl );
49 }
50
51 playlist_t *vlclua_get_playlist_internal( lua_State *L )
52 {
53     return vlclua_get_object( L, vlclua_set_playlist_internal );
54 }
55
56 static int vlclua_playlist_prev( lua_State * L )
57 {
58     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
59     playlist_Prev( p_playlist );
60     return 0;
61 }
62
63 static int vlclua_playlist_next( lua_State * L )
64 {
65     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
66     playlist_Next( p_playlist );
67     return 0;
68 }
69
70 static int vlclua_playlist_skip( lua_State * L )
71 {
72     int i_skip = luaL_checkint( L, 1 );
73     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
74     playlist_Skip( p_playlist, i_skip );
75     return 0;
76 }
77
78 static int vlclua_playlist_play( lua_State * L )
79 {
80     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
81     playlist_Play( p_playlist );
82     return 0;
83 }
84
85 static int vlclua_playlist_pause( lua_State * L )
86 {
87     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
88     playlist_Pause( p_playlist );
89     return 0;
90 }
91
92 static int vlclua_playlist_stop( lua_State * L )
93 {
94     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
95     playlist_Stop( p_playlist );
96     return 0;
97 }
98
99 static int vlclua_playlist_clear( lua_State * L )
100 {
101     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
102     playlist_Stop( p_playlist ); /* Isn't this already implied by Clear? */
103     playlist_Clear( p_playlist, pl_Unlocked );
104     return 0;
105 }
106
107 static int vlclua_playlist_repeat( lua_State * L )
108 {
109     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
110     int i_ret = vlclua_var_toggle_or_set( L, p_playlist, "repeat" );
111     return i_ret;
112 }
113
114 static int vlclua_playlist_loop( lua_State * L )
115 {
116     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
117     int i_ret = vlclua_var_toggle_or_set( L, p_playlist, "loop" );
118     return i_ret;
119 }
120
121 static int vlclua_playlist_random( lua_State * L )
122 {
123     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
124     int i_ret = vlclua_var_toggle_or_set( L, p_playlist, "random" );
125     return i_ret;
126 }
127
128 static int vlclua_playlist_gotoitem( lua_State * L )
129 {
130     int i_id = luaL_checkint( L, 1 );
131     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
132     PL_LOCK;
133     int i_ret = playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
134                                   true, NULL,
135                                   playlist_ItemGetById( p_playlist, i_id ) );
136     PL_UNLOCK;
137     return vlclua_push_ret( L, i_ret );
138 }
139
140 static int vlclua_playlist_delete( lua_State * L )
141 {
142     int i_id = luaL_checkint( L, 1 );
143     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
144     PL_LOCK;
145     playlist_item_t *p_item = playlist_ItemGetById( p_playlist, i_id );
146     if( !p_item )
147     {
148        PL_UNLOCK;
149        return vlclua_push_ret( L, -1 );
150     }
151     int i_ret = playlist_DeleteFromInput( p_playlist, p_item -> p_input, true );
152     PL_UNLOCK;
153     return vlclua_push_ret( L, i_ret );
154 }
155
156 static int vlclua_playlist_move( lua_State * L )
157 {
158     int i_item = luaL_checkint( L, 1 );
159     int i_target = luaL_checkint( L, 2 );
160     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
161     PL_LOCK;
162     playlist_item_t *p_item = playlist_ItemGetById( p_playlist, i_item );
163     playlist_item_t *p_target = playlist_ItemGetById( p_playlist, i_target );
164     if( !p_item || !p_target )
165     {
166        PL_UNLOCK;
167        return vlclua_push_ret( L, -1 );
168     }
169     int i_ret;
170     if( p_target->i_children != -1 )
171         i_ret = playlist_TreeMove( p_playlist, p_item, p_target, 0 );
172     else
173         i_ret = playlist_TreeMove( p_playlist, p_item, p_target->p_parent, p_target->i_id - p_target->p_parent->pp_children[0]->i_id + 1 );
174     PL_UNLOCK;
175     return vlclua_push_ret( L, i_ret );
176 }
177
178 static int vlclua_playlist_add( lua_State *L )
179 {
180     int i_count;
181     vlc_object_t *p_this = vlclua_get_this( L );
182     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
183     i_count = vlclua_playlist_add_internal( p_this, L, p_playlist,
184                                             NULL, true );
185     lua_pushinteger( L, i_count );
186     return 1;
187 }
188
189 static int vlclua_playlist_enqueue( lua_State *L )
190 {
191     int i_count;
192     vlc_object_t *p_this = vlclua_get_this( L );
193     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
194     i_count = vlclua_playlist_add_internal( p_this, L, p_playlist,
195                                             NULL, false );
196     lua_pushinteger( L, i_count );
197     return 1;
198 }
199
200 static void push_playlist_item( lua_State *L, playlist_item_t *p_item )
201 {
202     input_item_t *p_input = p_item->p_input;
203     int i_flags = 0;
204     i_flags = p_item->i_flags;
205     lua_newtable( L );
206     lua_pushinteger( L, p_item->i_id );
207     lua_setfield( L, -2, "id" );
208     lua_newtable( L );
209 #define CHECK_AND_SET_FLAG( name, label ) \
210     if( i_flags & PLAYLIST_ ## name ## _FLAG ) \
211     { \
212         lua_pushboolean( L, 1 ); \
213         lua_setfield( L, -2, #label ); \
214     }
215     CHECK_AND_SET_FLAG( SAVE, save )
216     CHECK_AND_SET_FLAG( SKIP, skip )
217     CHECK_AND_SET_FLAG( DBL, disabled )
218     CHECK_AND_SET_FLAG( RO, ro )
219     CHECK_AND_SET_FLAG( REMOVE, remove )
220     CHECK_AND_SET_FLAG( EXPANDED, expanded )
221 #undef CHECK_AND_SET_FLAG
222     lua_setfield( L, -2, "flags" );
223     if( p_input )
224     {
225         char *psz_name = input_item_GetTitleFbName( p_input );
226         lua_pushstring( L, psz_name );
227         free( psz_name );
228         lua_setfield( L, -2, "name" );
229         lua_pushstring( L, p_input->psz_uri );
230         lua_setfield( L, -2, "path" );
231         if( p_input->i_duration < 0 )
232             lua_pushnumber( L, -1 );
233         else
234             lua_pushnumber( L, ((double)p_input->i_duration)*1e-6 );
235         lua_setfield( L, -2, "duration" );
236         lua_pushinteger( L, p_item->i_nb_played );
237         lua_setfield( L, -2, "nb_played" );
238         luaopen_input_item( L, p_input );
239         /* TODO: add (optional) info categories, meta, options, es */
240     }
241     if( p_item->i_children >= 0 )
242     {
243         int i;
244         lua_createtable( L, p_item->i_children, 0 );
245         for( i = 0; i < p_item->i_children; i++ )
246         {
247             push_playlist_item( L, p_item->pp_children[i] );
248             lua_rawseti( L, -2, i+1 );
249         }
250         lua_setfield( L, -2, "children" );
251     }
252 }
253
254 static int vlclua_playlist_get( lua_State *L )
255 {
256     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
257     PL_LOCK;
258     playlist_item_t *p_item = NULL;
259
260     if( lua_isnumber( L, 1 ) )
261     {
262         int i_id = lua_tointeger( L, 1 );
263         p_item = playlist_ItemGetById( p_playlist, i_id );
264         if( !p_item )
265         {
266             PL_UNLOCK;
267             return 0; /* Should we return an error instead? */
268         }
269     }
270     else if( lua_isstring( L, 1 ) )
271     {
272         const char *psz_what = lua_tostring( L, 1 );
273         if( !strcasecmp( psz_what, "normal" )
274          || !strcasecmp( psz_what, "playlist" ) )
275             p_item = p_playlist->p_playing;
276         else if( !strcasecmp( psz_what, "ml" )
277               || !strcasecmp( psz_what, "media library" ) )
278             p_item = p_playlist->p_media_library;
279         else if( !strcasecmp( psz_what, "root" ) )
280             p_item = p_playlist->p_root;
281         else
282         {
283             /* currently, psz_what must be SD module's longname! */
284             p_item = playlist_ChildSearchName( p_playlist->p_root, psz_what );
285
286             if( !p_item )
287             {
288                 PL_UNLOCK;
289                 return 0; /* Should we return an error instead? */
290             }
291         }
292     }
293     else
294     {
295         p_item = p_playlist->p_root;
296     }
297     push_playlist_item( L, p_item );
298     PL_UNLOCK;
299     return 1;
300 }
301
302 static int vlclua_playlist_search( lua_State *L )
303 {
304     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
305     const char *psz_string = luaL_optstring( L, 1, "" );
306     PL_LOCK;
307     playlist_LiveSearchUpdate( p_playlist, p_playlist->p_root, psz_string, true );
308     PL_UNLOCK;
309     push_playlist_item( L, p_playlist->p_root );
310     return 1;
311 }
312
313 static int vlclua_playlist_current( lua_State *L )
314 {
315     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
316     input_thread_t *p_input = playlist_CurrentInput( p_playlist );
317     int id = -1;
318
319     if( p_input )
320     {
321         input_item_t *p_item = input_GetItem( p_input );
322         if( p_item )
323             id = p_item->i_id;
324         vlc_object_release( p_input );
325     }
326
327 #warning Indexing input items by ID is unsafe,
328     lua_pushinteger( L, id );
329     return 1;
330 }
331
332 static int vlc_sort_key_from_string( const char *psz_name )
333 {
334     static const struct
335     {
336         const char *psz_name;
337         int i_key;
338     } pp_keys[] =
339         { { "id", SORT_ID },
340           { "title", SORT_TITLE },
341           { "title nodes first", SORT_TITLE_NODES_FIRST },
342           { "artist", SORT_ARTIST },
343           { "genre", SORT_GENRE },
344           { "random", SORT_RANDOM },
345           { "duration", SORT_DURATION },
346           { "title numeric", SORT_TITLE_NUMERIC },
347           { "album", SORT_ALBUM },
348           { NULL, -1 } };
349     int i;
350     for( i = 0; pp_keys[i].psz_name; i++ )
351     {
352         if( !strcmp( psz_name, pp_keys[i].psz_name ) )
353             return pp_keys[i].i_key;
354     }
355     return -1;
356 }
357
358 static int vlclua_playlist_sort( lua_State *L )
359 {
360     /* allow setting the different sort keys */
361     int i_mode = vlc_sort_key_from_string( luaL_checkstring( L, 1 ) );
362     if( i_mode == -1 )
363         return luaL_error( L, "Invalid search key." );
364     int i_type = luaL_optboolean( L, 2, 0 ) ? ORDER_REVERSE : ORDER_NORMAL;
365     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
366     PL_LOCK;
367     int i_ret = playlist_RecursiveNodeSort( p_playlist, p_playlist->p_playing,
368                                             i_mode, i_type );
369     PL_UNLOCK;
370     return vlclua_push_ret( L, i_ret );
371 }
372
373 static int vlclua_playlist_status( lua_State *L )
374 {
375     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
376     PL_LOCK;
377     switch( playlist_Status( p_playlist ) )
378     {
379         case PLAYLIST_STOPPED:
380             lua_pushliteral( L, "stopped" );
381             break;
382         case PLAYLIST_RUNNING:
383             lua_pushliteral( L, "playing" );
384             break;
385         case PLAYLIST_PAUSED:
386             lua_pushliteral( L, "paused" );
387             break;
388         default:
389             lua_pushliteral( L, "unknown" );
390             break;
391     }
392     PL_UNLOCK;
393     return 1;
394 }
395
396 /*****************************************************************************
397  *
398  *****************************************************************************/
399 static const luaL_Reg vlclua_playlist_reg[] = {
400     { "prev", vlclua_playlist_prev },
401     { "next", vlclua_playlist_next },
402     { "skip", vlclua_playlist_skip },
403     { "play", vlclua_playlist_play },
404     { "pause", vlclua_playlist_pause },
405     { "stop", vlclua_playlist_stop },
406     { "clear", vlclua_playlist_clear },
407     { "repeat", vlclua_playlist_repeat }, // repeat is a reserved lua keyword...
408     { "repeat_", vlclua_playlist_repeat }, // ... provide repeat_ too.
409     { "loop", vlclua_playlist_loop },
410     { "random", vlclua_playlist_random },
411     { "goto", vlclua_playlist_gotoitem },
412     { "gotoitem", vlclua_playlist_gotoitem },
413     { "add", vlclua_playlist_add },
414     { "enqueue", vlclua_playlist_enqueue },
415     { "get", vlclua_playlist_get },
416     { "search", vlclua_playlist_search },
417     { "current", vlclua_playlist_current },
418     { "sort", vlclua_playlist_sort },
419     { "status", vlclua_playlist_status },
420     { "delete", vlclua_playlist_delete },
421     { "move", vlclua_playlist_move },
422     { NULL, NULL }
423 };
424
425 void luaopen_playlist( lua_State *L )
426 {
427     lua_newtable( L );
428     luaL_register( L, NULL, vlclua_playlist_reg );
429     lua_setfield( L, -2, "playlist" );
430 }