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