]> git.sesse.net Git - vlc/blob - modules/misc/lua/libs/playlist.c
Cleanup playlist status.
[vlc] / modules / misc / lua / libs / playlist.c
1 /*****************************************************************************
2  * playlist.c
3  *****************************************************************************
4  * Copyright (C) 2007-2008 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 <lua.h>        /* Low level lua C API */
41 #include <lauxlib.h>    /* Higher level C API */
42
43 #include "../vlc.h"
44 #include "../libs.h"
45 #include "playlist.h"
46 #include "variables.h"
47
48 /*****************************************************************************
49  * Internal lua<->vlc utils
50  *****************************************************************************/
51 playlist_t *vlclua_get_playlist_internal( lua_State *L )
52 {
53     vlc_object_t *p_this = vlclua_get_this( L );
54     return pl_Hold( p_this );
55 }
56
57 void vlclua_release_playlist_internal( playlist_t *p_playlist )
58 {
59     vlc_object_release( p_playlist );
60 }
61
62 static int vlclua_playlist_prev( lua_State * L )
63 {
64     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
65     playlist_Prev( p_playlist );
66     vlclua_release_playlist_internal( p_playlist );
67     return 0;
68 }
69
70 static int vlclua_playlist_next( lua_State * L )
71 {
72     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
73     playlist_Next( p_playlist );
74     vlclua_release_playlist_internal( p_playlist );
75     return 0;
76 }
77
78 static int vlclua_playlist_skip( lua_State * L )
79 {
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 );
84     return 0;
85 }
86
87 static int vlclua_playlist_play( lua_State * L )
88 {
89     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
90     playlist_Play( p_playlist );
91     vlclua_release_playlist_internal( p_playlist );
92     return 0;
93 }
94
95 static int vlclua_playlist_pause( lua_State * L )
96 {
97     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
98     playlist_Pause( p_playlist );
99     vlclua_release_playlist_internal( p_playlist );
100     return 0;
101 }
102
103 static int vlclua_playlist_stop( lua_State * L )
104 {
105     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
106     playlist_Stop( p_playlist );
107     vlclua_release_playlist_internal( p_playlist );
108     return 0;
109 }
110
111 static int vlclua_playlist_clear( lua_State * L )
112 {
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 );
117     return 0;
118 }
119
120 static int vlclua_playlist_repeat( lua_State * L )
121 {
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 );
125     return i_ret;
126 }
127
128 static int vlclua_playlist_loop( lua_State * L )
129 {
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 );
133     return i_ret;
134 }
135
136 static int vlclua_playlist_random( lua_State * L )
137 {
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 );
141     return i_ret;
142 }
143
144 static int vlclua_playlist_goto( lua_State * L )
145 {
146     int i_id = luaL_checkint( L, 1 );
147     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
148     PL_LOCK;
149     int i_ret = playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
150                                   true, NULL,
151                                   playlist_ItemGetById( p_playlist, i_id ) );
152     PL_UNLOCK;
153     vlclua_release_playlist_internal( p_playlist );
154     return vlclua_push_ret( L, i_ret );
155 }
156
157 static int vlclua_playlist_add( lua_State *L )
158 {
159     int i_count;
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,
163                                             NULL, true );
164     vlclua_release_playlist_internal( p_playlist );
165     lua_pushinteger( L, i_count );
166     return 1;
167 }
168
169 static int vlclua_playlist_enqueue( lua_State *L )
170 {
171     int i_count;
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,
175                                             NULL, false );
176     vlclua_release_playlist_internal( p_playlist );
177     lua_pushinteger( L, i_count );
178     return 1;
179 }
180
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 )
183 {
184     input_item_t *p_input = p_item->p_input;
185     int i_flags = 0;
186     i_flags = p_item->i_flags;
187     lua_newtable( L );
188     lua_pushinteger( L, p_item->i_id );
189     lua_setfield( L, -2, "id" );
190     lua_newtable( L );
191 #define CHECK_AND_SET_FLAG( name, label ) \
192     if( i_flags & PLAYLIST_ ## name ## _FLAG ) \
193     { \
194         lua_pushboolean( L, 1 ); \
195         lua_setfield( L, -2, #label ); \
196     }
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" );
205     if( p_input )
206     {
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 );
213         else
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 */
219     }
220     if( p_item->i_children >= 0 )
221     {
222         int i;
223         lua_createtable( L, p_item->i_children, 0 );
224         for( i = 0; i < p_item->i_children; i++ )
225         {
226             push_playlist_item( L, p_item->pp_children[i] );
227             lua_rawseti( L, -2, i+1 );
228         }
229         lua_setfield( L, -2, "children" );
230     }
231 }
232
233 static int vlclua_playlist_get( lua_State *L )
234 {
235     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
236     PL_LOCK;
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;
239
240     if( lua_isnumber( L, 1 ) )
241     {
242         int i_id = lua_tointeger( L, 1 );
243         p_item = playlist_ItemGetById( p_playlist, i_id );
244         if( !p_item )
245         {
246             PL_UNLOCK;
247             vlclua_release_playlist_internal( p_playlist );
248             return 0; /* Should we return an error instead? */
249         }
250     }
251     else if( lua_isstring( L, 1 ) )
252     {
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;
265         else
266         {
267 #ifdef FIX_THAT_CODE_NOT_TO_MESS_WITH_PLAYLIST_INTERNALS
268             for( int i = 0; i < p_playlist->i_sds; i++ )
269             {
270                 if( !strcasecmp( psz_what,
271                                  p_playlist->pp_sds[i]->p_sd->psz_module ) )
272                 {
273                     p_item = b_category ? p_playlist->pp_sds[i]->p_cat
274                                         : p_playlist->pp_sds[i]->p_one;
275                     break;
276                 }
277             }
278 #else
279 # warning "Don't access playlist iternal, broken code here."
280             abort();
281 #endif
282             if( !p_item )
283             {
284                 PL_UNLOCK;
285                 vlclua_release_playlist_internal( p_playlist );
286                 return 0; /* Should we return an error instead? */
287             }
288         }
289     }
290     else
291     {
292         p_item = b_category ? p_playlist->p_root_category
293                             : p_playlist->p_root_onelevel;
294     }
295     push_playlist_item( L, p_item );
296     PL_UNLOCK;
297     vlclua_release_playlist_internal( p_playlist );
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     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;
308     PL_LOCK;
309     playlist_LiveSearchUpdate( p_playlist, p_item, psz_string );
310     PL_UNLOCK;
311     push_playlist_item( L, p_item );
312     vlclua_release_playlist_internal( p_playlist );
313     return 1;
314 }
315
316 static int vlclua_playlist_current( lua_State *L )
317 {
318     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
319     input_thread_t *p_input = playlist_CurrentInput( p_playlist );
320     int id = -1;
321
322     if( p_input )
323     {
324         input_item_t *p_item = input_GetItem( p_input );
325         if( p_item )
326             id = p_item->i_id;
327         vlc_object_release( p_input );
328     }
329
330 #warning Indexing input items by ID is unsafe,
331     lua_pushinteger( L, id );
332     vlclua_release_playlist_internal( p_playlist );
333     return 1;
334 }
335
336 static int vlc_sort_key_from_string( const char *psz_name )
337 {
338     static const struct
339     {
340         const char *psz_name;
341         int i_key;
342     } pp_keys[] =
343         { { "id", SORT_ID },
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 },
352           { NULL, -1 } };
353     int i;
354     for( i = 0; pp_keys[i].psz_name; i++ )
355     {
356         if( !strcmp( psz_name, pp_keys[i].psz_name ) )
357             return pp_keys[i].i_key;
358     }
359     return -1;
360 }
361
362 static int vlclua_playlist_sort( lua_State *L )
363 {
364     /* allow setting the different sort keys */
365     int i_mode = vlc_sort_key_from_string( luaL_checkstring( L, 1 ) );
366     if( i_mode == -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 );
371     PL_LOCK;
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,
375                                             i_type );
376     PL_UNLOCK;
377     vlclua_release_playlist_internal( p_playlist );
378     return vlclua_push_ret( L, i_ret );
379 }
380
381 static int vlclua_playlist_status( lua_State *L )
382 {
383     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
384     PL_LOCK;
385     switch( playlist_Status( p_playlist ) )
386     {
387         case PLAYLIST_STOPPED:
388             lua_pushstring( L, "stopped" );
389             break;
390         case PLAYLIST_RUNNING:
391             lua_pushstring( L, "playing" );
392             break;
393         case PLAYLIST_PAUSED:
394             lua_pushstring( L, "paused" );
395             break;
396         default:
397             lua_pushstring( L, "unknown" );
398             break;
399     }
400     PL_UNLOCK;
401     vlclua_release_playlist_internal( p_playlist );
402     return 1;
403 }
404
405 /*****************************************************************************
406  *
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 },
428     { NULL, NULL }
429 };
430
431 void luaopen_playlist( lua_State *L )
432 {
433     lua_newtable( L );
434     luaL_register( L, NULL, vlclua_playlist_reg );
435     lua_setfield( L, -2, "playlist" );
436 }