]> git.sesse.net Git - vlc/blob - modules/lua/libs/playlist.c
f657217fe94eff38bcc6d663cd5feb6007cf0f07
[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     playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, true, NULL,
134                       playlist_ItemGetById( p_playlist, i_id ) );
135     PL_UNLOCK;
136     return vlclua_push_ret( L, VLC_SUCCESS );
137 }
138
139 static int vlclua_playlist_delete( lua_State * L )
140 {
141     int i_id = luaL_checkint( L, 1 );
142     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
143     PL_LOCK;
144     playlist_item_t *p_item = playlist_ItemGetById( p_playlist, i_id );
145     if( !p_item )
146     {
147        PL_UNLOCK;
148        return vlclua_push_ret( L, -1 );
149     }
150     int i_ret = playlist_DeleteFromInput( p_playlist, p_item -> p_input, true );
151     PL_UNLOCK;
152     return vlclua_push_ret( L, i_ret );
153 }
154
155 static int vlclua_playlist_move( lua_State * L )
156 {
157     int i_item = luaL_checkint( L, 1 );
158     int i_target = luaL_checkint( L, 2 );
159     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
160     PL_LOCK;
161     playlist_item_t *p_item = playlist_ItemGetById( p_playlist, i_item );
162     playlist_item_t *p_target = playlist_ItemGetById( p_playlist, i_target );
163     if( !p_item || !p_target )
164     {
165        PL_UNLOCK;
166        return vlclua_push_ret( L, -1 );
167     }
168     int i_ret;
169     if( p_target->i_children != -1 )
170         i_ret = playlist_TreeMove( p_playlist, p_item, p_target, 0 );
171     else
172         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 );
173     PL_UNLOCK;
174     return vlclua_push_ret( L, i_ret );
175 }
176
177 static int vlclua_playlist_add( lua_State *L )
178 {
179     int i_count;
180     vlc_object_t *p_this = vlclua_get_this( L );
181     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
182     i_count = vlclua_playlist_add_internal( p_this, L, p_playlist,
183                                             NULL, true );
184     lua_pushinteger( L, i_count );
185     return 1;
186 }
187
188 static int vlclua_playlist_enqueue( lua_State *L )
189 {
190     int i_count;
191     vlc_object_t *p_this = vlclua_get_this( L );
192     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
193     i_count = vlclua_playlist_add_internal( p_this, L, p_playlist,
194                                             NULL, false );
195     lua_pushinteger( L, i_count );
196     return 1;
197 }
198
199 static void push_playlist_item( lua_State *L, playlist_item_t *p_item )
200 {
201     input_item_t *p_input = p_item->p_input;
202     int i_flags = 0;
203     i_flags = p_item->i_flags;
204     lua_newtable( L );
205     lua_pushinteger( L, p_item->i_id );
206     lua_setfield( L, -2, "id" );
207     lua_newtable( L );
208 #define CHECK_AND_SET_FLAG( name, label ) \
209     if( i_flags & PLAYLIST_ ## name ## _FLAG ) \
210     { \
211         lua_pushboolean( L, 1 ); \
212         lua_setfield( L, -2, #label ); \
213     }
214     CHECK_AND_SET_FLAG( SAVE, save )
215     CHECK_AND_SET_FLAG( SKIP, skip )
216     CHECK_AND_SET_FLAG( DBL, disabled )
217     CHECK_AND_SET_FLAG( RO, ro )
218     CHECK_AND_SET_FLAG( REMOVE, remove )
219     CHECK_AND_SET_FLAG( EXPANDED, expanded )
220 #undef CHECK_AND_SET_FLAG
221     lua_setfield( L, -2, "flags" );
222     if( p_input )
223     {
224         char *psz_name = input_item_GetTitleFbName( p_input );
225         lua_pushstring( L, psz_name );
226         free( psz_name );
227         lua_setfield( L, -2, "name" );
228         lua_pushstring( L, p_input->psz_uri );
229         lua_setfield( L, -2, "path" );
230         if( p_input->i_duration < 0 )
231             lua_pushnumber( L, -1 );
232         else
233             lua_pushnumber( L, ((double)p_input->i_duration)*1e-6 );
234         lua_setfield( L, -2, "duration" );
235         lua_pushinteger( L, p_item->i_nb_played );
236         lua_setfield( L, -2, "nb_played" );
237         luaopen_input_item( L, p_input );
238         /* TODO: add (optional) info categories, meta, options, es */
239     }
240     if( p_item->i_children >= 0 )
241     {
242         int i;
243         lua_createtable( L, p_item->i_children, 0 );
244         for( i = 0; i < p_item->i_children; i++ )
245         {
246             push_playlist_item( L, p_item->pp_children[i] );
247             lua_rawseti( L, -2, i+1 );
248         }
249         lua_setfield( L, -2, "children" );
250     }
251 }
252
253 static int vlclua_playlist_get( lua_State *L )
254 {
255     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
256     PL_LOCK;
257     playlist_item_t *p_item = NULL;
258
259     if( lua_isnumber( L, 1 ) )
260     {
261         int i_id = lua_tointeger( L, 1 );
262         p_item = playlist_ItemGetById( p_playlist, i_id );
263         if( !p_item )
264         {
265             PL_UNLOCK;
266             return 0; /* Should we return an error instead? */
267         }
268     }
269     else if( lua_isstring( L, 1 ) )
270     {
271         const char *psz_what = lua_tostring( L, 1 );
272         if( !strcasecmp( psz_what, "normal" )
273          || !strcasecmp( psz_what, "playlist" ) )
274             p_item = p_playlist->p_playing;
275         else if( !strcasecmp( psz_what, "ml" )
276               || !strcasecmp( psz_what, "media library" ) )
277             p_item = p_playlist->p_media_library;
278         else if( !strcasecmp( psz_what, "root" ) )
279             p_item = p_playlist->p_root;
280         else
281         {
282             /* currently, psz_what must be SD module's longname! */
283             p_item = playlist_ChildSearchName( p_playlist->p_root, psz_what );
284
285             if( !p_item )
286             {
287                 PL_UNLOCK;
288                 return 0; /* Should we return an error instead? */
289             }
290         }
291     }
292     else
293     {
294         p_item = p_playlist->p_root;
295     }
296     push_playlist_item( L, p_item );
297     PL_UNLOCK;
298     return 1;
299 }
300
301 static int vlclua_playlist_search( lua_State *L )
302 {
303     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
304     const char *psz_string = luaL_optstring( L, 1, "" );
305     PL_LOCK;
306     playlist_LiveSearchUpdate( p_playlist, p_playlist->p_root, psz_string, true );
307     PL_UNLOCK;
308     push_playlist_item( L, p_playlist->p_root );
309     return 1;
310 }
311
312 static int vlclua_playlist_current( lua_State *L )
313 {
314     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
315     input_thread_t *p_input = playlist_CurrentInput( p_playlist );
316     int id = -1;
317
318     if( p_input )
319     {
320         input_item_t *p_item = input_GetItem( p_input );
321         if( p_item )
322             id = p_item->i_id;
323         vlc_object_release( p_input );
324     }
325
326 #warning Indexing input items by ID is unsafe,
327     lua_pushinteger( L, id );
328     return 1;
329 }
330
331 static int vlc_sort_key_from_string( const char *psz_name )
332 {
333     static const struct
334     {
335         const char *psz_name;
336         int i_key;
337     } pp_keys[] =
338         { { "id", SORT_ID },
339           { "title", SORT_TITLE },
340           { "title nodes first", SORT_TITLE_NODES_FIRST },
341           { "artist", SORT_ARTIST },
342           { "genre", SORT_GENRE },
343           { "random", SORT_RANDOM },
344           { "duration", SORT_DURATION },
345           { "title numeric", SORT_TITLE_NUMERIC },
346           { "album", SORT_ALBUM },
347           { NULL, -1 } };
348     int i;
349     for( i = 0; pp_keys[i].psz_name; i++ )
350     {
351         if( !strcmp( psz_name, pp_keys[i].psz_name ) )
352             return pp_keys[i].i_key;
353     }
354     return -1;
355 }
356
357 static int vlclua_playlist_sort( lua_State *L )
358 {
359     /* allow setting the different sort keys */
360     int i_mode = vlc_sort_key_from_string( luaL_checkstring( L, 1 ) );
361     if( i_mode == -1 )
362         return luaL_error( L, "Invalid search key." );
363     int i_type = luaL_optboolean( L, 2, 0 ) ? ORDER_REVERSE : ORDER_NORMAL;
364     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
365     PL_LOCK;
366     int i_ret = playlist_RecursiveNodeSort( p_playlist, p_playlist->p_playing,
367                                             i_mode, i_type );
368     PL_UNLOCK;
369     return vlclua_push_ret( L, i_ret );
370 }
371
372 static int vlclua_playlist_status( lua_State *L )
373 {
374     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
375     PL_LOCK;
376     switch( playlist_Status( p_playlist ) )
377     {
378         case PLAYLIST_STOPPED:
379             lua_pushliteral( L, "stopped" );
380             break;
381         case PLAYLIST_RUNNING:
382             lua_pushliteral( L, "playing" );
383             break;
384         case PLAYLIST_PAUSED:
385             lua_pushliteral( L, "paused" );
386             break;
387         default:
388             lua_pushliteral( L, "unknown" );
389             break;
390     }
391     PL_UNLOCK;
392     return 1;
393 }
394
395 /*****************************************************************************
396  *
397  *****************************************************************************/
398 static const luaL_Reg vlclua_playlist_reg[] = {
399     { "prev", vlclua_playlist_prev },
400     { "next", vlclua_playlist_next },
401     { "skip", vlclua_playlist_skip },
402     { "play", vlclua_playlist_play },
403     { "pause", vlclua_playlist_pause },
404     { "stop", vlclua_playlist_stop },
405     { "clear", vlclua_playlist_clear },
406     { "repeat", vlclua_playlist_repeat }, // repeat is a reserved lua keyword...
407     { "repeat_", vlclua_playlist_repeat }, // ... provide repeat_ too.
408     { "loop", vlclua_playlist_loop },
409     { "random", vlclua_playlist_random },
410     { "goto", vlclua_playlist_gotoitem },
411     { "gotoitem", vlclua_playlist_gotoitem },
412     { "add", vlclua_playlist_add },
413     { "enqueue", vlclua_playlist_enqueue },
414     { "get", vlclua_playlist_get },
415     { "search", vlclua_playlist_search },
416     { "current", vlclua_playlist_current },
417     { "sort", vlclua_playlist_sort },
418     { "status", vlclua_playlist_status },
419     { "delete", vlclua_playlist_delete },
420     { "move", vlclua_playlist_move },
421     { NULL, NULL }
422 };
423
424 void luaopen_playlist( lua_State *L )
425 {
426     lua_newtable( L );
427     luaL_register( L, NULL, vlclua_playlist_reg );
428     lua_setfield( L, -2, "playlist" );
429 }