]> git.sesse.net Git - vlc/blob - modules/misc/lua/libs/playlist.c
b4113bf0267be02aad5898685533141c44e0cf2b
[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     PL_LOCK;
91     playlist_Play( p_playlist );
92     PL_UNLOCK;
93     vlclua_release_playlist_internal( p_playlist );
94     return 0;
95 }
96
97 static int vlclua_playlist_pause( lua_State * L )
98 {
99     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
100     playlist_Pause( p_playlist );
101     vlclua_release_playlist_internal( p_playlist );
102     return 0;
103 }
104
105 static int vlclua_playlist_stop( lua_State * L )
106 {
107     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
108     playlist_Stop( p_playlist );
109     vlclua_release_playlist_internal( p_playlist );
110     return 0;
111 }
112
113 static int vlclua_playlist_clear( lua_State * L )
114 {
115     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
116     playlist_Stop( p_playlist ); /* Isn't this already implied by Clear? */
117     playlist_Clear( p_playlist, pl_Unlocked );
118     vlclua_release_playlist_internal( p_playlist );
119     return 0;
120 }
121
122 static int vlclua_playlist_repeat( lua_State * L )
123 {
124     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
125     int i_ret = vlclua_var_toggle_or_set( L, p_playlist, "repeat" );
126     vlclua_release_playlist_internal( p_playlist );
127     return i_ret;
128 }
129
130 static int vlclua_playlist_loop( lua_State * L )
131 {
132     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
133     int i_ret = vlclua_var_toggle_or_set( L, p_playlist, "loop" );
134     vlclua_release_playlist_internal( p_playlist );
135     return i_ret;
136 }
137
138 static int vlclua_playlist_random( lua_State * L )
139 {
140     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
141     int i_ret = vlclua_var_toggle_or_set( L, p_playlist, "random" );
142     vlclua_release_playlist_internal( p_playlist );
143     return i_ret;
144 }
145
146 static int vlclua_playlist_goto( lua_State * L )
147 {
148     int i_id = luaL_checkint( L, 1 );
149     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
150     PL_LOCK;
151     int i_ret = playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
152                                   true, NULL,
153                                   playlist_ItemGetById( p_playlist, i_id,
154                                                         true ) );
155     PL_UNLOCK;
156     vlclua_release_playlist_internal( p_playlist );
157     return vlclua_push_ret( L, i_ret );
158 }
159
160 static int vlclua_playlist_add( lua_State *L )
161 {
162     int i_count;
163     vlc_object_t *p_this = vlclua_get_this( L );
164     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
165     i_count = vlclua_playlist_add_internal( p_this, L, p_playlist,
166                                             NULL, true );
167     vlclua_release_playlist_internal( p_playlist );
168     lua_pushinteger( L, i_count );
169     return 1;
170 }
171
172 static int vlclua_playlist_enqueue( lua_State *L )
173 {
174     int i_count;
175     vlc_object_t *p_this = vlclua_get_this( L );
176     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
177     i_count = vlclua_playlist_add_internal( p_this, L, p_playlist,
178                                             NULL, false );
179     vlclua_release_playlist_internal( p_playlist );
180     lua_pushinteger( L, i_count );
181     return 1;
182 }
183
184 static void push_playlist_item( lua_State *L, playlist_item_t *p_item );
185 static void push_playlist_item( lua_State *L, playlist_item_t *p_item )
186 {
187     input_item_t *p_input = p_item->p_input;
188     int i_flags = 0;
189     i_flags = p_item->i_flags;
190     lua_newtable( L );
191     lua_pushinteger( L, p_item->i_id );
192     lua_setfield( L, -2, "id" );
193     lua_newtable( L );
194 #define CHECK_AND_SET_FLAG( name, label ) \
195     if( i_flags & PLAYLIST_ ## name ## _FLAG ) \
196     { \
197         lua_pushboolean( L, 1 ); \
198         lua_setfield( L, -2, #label ); \
199     }
200     CHECK_AND_SET_FLAG( SAVE, save )
201     CHECK_AND_SET_FLAG( SKIP, skip )
202     CHECK_AND_SET_FLAG( DBL, disabled )
203     CHECK_AND_SET_FLAG( RO, ro )
204     CHECK_AND_SET_FLAG( REMOVE, remove )
205     CHECK_AND_SET_FLAG( EXPANDED, expanded )
206 #undef CHECK_AND_SET_FLAG
207     lua_setfield( L, -2, "flags" );
208     if( p_input )
209     {
210         lua_pushstring( L, p_input->psz_name );
211         lua_setfield( L, -2, "name" );
212         lua_pushstring( L, p_input->psz_uri );
213         lua_setfield( L, -2, "path" );
214         if( p_input->i_duration < 0 )
215             lua_pushnumber( L, -1 );
216         else
217             lua_pushnumber( L, ((double)p_input->i_duration)*1e-6 );
218         lua_setfield( L, -2, "duration" );
219         lua_pushinteger( L, p_input->i_nb_played );
220         lua_setfield( L, -2, "nb_played" );
221         /* TODO: add (optional) info categories, meta, options, es */
222     }
223     if( p_item->i_children >= 0 )
224     {
225         int i;
226         lua_createtable( L, p_item->i_children, 0 );
227         for( i = 0; i < p_item->i_children; i++ )
228         {
229             push_playlist_item( L, p_item->pp_children[i] );
230             lua_rawseti( L, -2, i+1 );
231         }
232         lua_setfield( L, -2, "children" );
233     }
234 }
235
236 static int vlclua_playlist_get( lua_State *L )
237 {
238     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
239     PL_LOCK;
240     int b_category = luaL_optboolean( L, 2, 1 ); /* Default to tree playlist (discared when 1st argument is a playlist_item's id) */
241     playlist_item_t *p_item = NULL;
242
243     if( lua_isnumber( L, 1 ) )
244     {
245         int i_id = lua_tointeger( L, 1 );
246         p_item = playlist_ItemGetById( p_playlist, i_id, true );
247         if( !p_item )
248         {
249             PL_UNLOCK;
250             vlclua_release_playlist_internal( p_playlist );
251             return 0; /* Should we return an error instead? */
252         }
253     }
254     else if( lua_isstring( L, 1 ) )
255     {
256         const char *psz_what = lua_tostring( L, 1 );
257         if( !strcasecmp( psz_what, "normal" )
258          || !strcasecmp( psz_what, "playlist" ) )
259             p_item = b_category ? p_playlist->p_local_category
260                                 : p_playlist->p_local_onelevel;
261         else if( !strcasecmp( psz_what, "ml" )
262               || !strcasecmp( psz_what, "media library" ) )
263             p_item = b_category ? p_playlist->p_ml_category
264                                 : p_playlist->p_ml_onelevel;
265         else if( !strcasecmp( psz_what, "root" ) )
266             p_item = b_category ? p_playlist->p_root_category
267                                 : p_playlist->p_root_onelevel;
268         else
269         {
270 #ifdef FIX_THAT_CODE_NOT_TO_MESS_WITH_PLAYLIST_INTERNALS
271             for( int i = 0; i < p_playlist->i_sds; i++ )
272             {
273                 if( !strcasecmp( psz_what,
274                                  p_playlist->pp_sds[i]->p_sd->psz_module ) )
275                 {
276                     p_item = b_category ? p_playlist->pp_sds[i]->p_cat
277                                         : p_playlist->pp_sds[i]->p_one;
278                     break;
279                 }
280             }
281 #else
282 # warning "Don't access playlist iternal, broken code here."
283             abort();
284 #endif
285             if( !p_item )
286             {
287                 PL_UNLOCK;
288                 vlclua_release_playlist_internal( p_playlist );
289                 return 0; /* Should we return an error instead? */
290             }
291         }
292     }
293     else
294     {
295         p_item = b_category ? p_playlist->p_root_category
296                             : p_playlist->p_root_onelevel;
297     }
298     push_playlist_item( L, p_item );
299     PL_UNLOCK;
300     vlclua_release_playlist_internal( p_playlist );
301     return 1;
302 }
303
304 static int vlclua_playlist_search( lua_State *L )
305 {
306     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
307     const char *psz_string = luaL_optstring( L, 1, "" );
308     int b_category = luaL_optboolean( L, 2, 1 ); /* default to category */
309     playlist_item_t *p_item = b_category ? p_playlist->p_root_category
310                                          : p_playlist->p_root_onelevel;
311     PL_LOCK;
312     playlist_LiveSearchUpdate( p_playlist, p_item, psz_string );
313     PL_UNLOCK;
314     push_playlist_item( L, p_item );
315     vlclua_release_playlist_internal( p_playlist );
316     return 1;
317 }
318
319 static int vlclua_playlist_current( lua_State *L )
320 {
321     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
322     lua_pushinteger( L, var_GetInteger( p_playlist, "playlist-current" ) );
323     vlclua_release_playlist_internal( p_playlist );
324     return 1;
325 }
326
327 static int vlc_sort_key_from_string( const char *psz_name )
328 {
329     static const struct
330     {
331         const char *psz_name;
332         int i_key;
333     } pp_keys[] =
334         { { "id", SORT_ID },
335           { "title", SORT_TITLE },
336           { "title nodes first", SORT_TITLE_NODES_FIRST },
337           { "artist", SORT_ARTIST },
338           { "genre", SORT_GENRE },
339           { "random", SORT_RANDOM },
340           { "duration", SORT_DURATION },
341           { "title numeric", SORT_TITLE_NUMERIC },
342           { "album", SORT_ALBUM },
343           { NULL, -1 } };
344     int i;
345     for( i = 0; pp_keys[i].psz_name; i++ )
346     {
347         if( !strcmp( psz_name, pp_keys[i].psz_name ) )
348             return pp_keys[i].i_key;
349     }
350     return -1;
351 }
352
353 static int vlclua_playlist_sort( lua_State *L )
354 {
355     /* allow setting the different sort keys */
356     int i_mode = vlc_sort_key_from_string( luaL_checkstring( L, 1 ) );
357     if( i_mode == -1 )
358         return luaL_error( L, "Invalid search key." );
359     int i_type = luaL_optboolean( L, 2, 0 ) ? ORDER_REVERSE : ORDER_NORMAL;
360     int b_category = luaL_optboolean( L, 3, 1 ); /* default to category */
361     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
362     PL_LOCK;
363     playlist_item_t *p_root = b_category ? p_playlist->p_local_category
364                                          : p_playlist->p_local_onelevel;
365     int i_ret = playlist_RecursiveNodeSort( p_playlist, p_root, i_mode,
366                                             i_type );
367     PL_UNLOCK;
368     vlclua_release_playlist_internal( p_playlist );
369     return vlclua_push_ret( L, i_ret );
370 }
371
372 /* FIXME: split this in 3 different functions? */
373 static int vlclua_playlist_status( lua_State *L )
374 {
375     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
376     /*
377     int i_count = 0;
378     lua_settop( L, 0 );*/
379     input_thread_t * p_input = playlist_CurrentInput( p_playlist );
380     if( p_input )
381     {
382         /*char *psz_uri =
383             input_item_GetURI( input_GetItem( p_playlist->p_input ) );
384         lua_pushstring( L, psz_uri );
385         free( psz_uri );
386         lua_pushnumber( L, config_GetInt( p_intf, "volume" ) );*/
387         PL_LOCK;
388         switch( playlist_Status( p_playlist ) )
389         {
390             case PLAYLIST_STOPPED:
391                 lua_pushstring( L, "stopped" );
392                 break;
393             case PLAYLIST_RUNNING:
394                 lua_pushstring( L, "playing" );
395                 break;
396             case PLAYLIST_PAUSED:
397                 lua_pushstring( L, "paused" );
398                 break;
399             default:
400                 lua_pushstring( L, "unknown" );
401                 break;
402         }
403         PL_UNLOCK;
404         /*i_count += 3;*/
405         vlc_object_release( p_input );
406     }
407     else
408     {
409         lua_pushstring( L, "stopped" );
410     }
411     vlclua_release_playlist_internal( p_playlist );
412     return 1;
413 }
414
415 /*****************************************************************************
416  *
417  *****************************************************************************/
418 static const luaL_Reg vlclua_playlist_reg[] = {
419     { "prev", vlclua_playlist_prev },
420     { "next", vlclua_playlist_next },
421     { "skip", vlclua_playlist_skip },
422     { "play", vlclua_playlist_play },
423     { "pause", vlclua_playlist_pause },
424     { "stop", vlclua_playlist_stop },
425     { "clear", vlclua_playlist_clear },
426     { "repeat", vlclua_playlist_repeat },
427     { "loop", vlclua_playlist_loop },
428     { "random", vlclua_playlist_random },
429     { "goto", vlclua_playlist_goto },
430     { "add", vlclua_playlist_add },
431     { "enqueue", vlclua_playlist_enqueue },
432     { "get", vlclua_playlist_get },
433     { "search", vlclua_playlist_search },
434     { "current", vlclua_playlist_current },
435     { "sort", vlclua_playlist_sort },
436     { "status", vlclua_playlist_status },
437     { NULL, NULL }
438 };
439
440 void luaopen_playlist( lua_State *L )
441 {
442     lua_newtable( L );
443     luaL_register( L, NULL, vlclua_playlist_reg );
444     lua_setfield( L, -2, "playlist" );
445 }