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