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