]> git.sesse.net Git - vlc/blob - modules/misc/lua/libs/playlist.c
Use vlc_object_lock and vlc_object_unlock
[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_Yield( 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     vlc_object_release( p_playlist );
62     return 0;
63 }
64
65 static int vlclua_playlist_next( lua_State * L )
66 {
67     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
68     playlist_Next( p_playlist );
69     vlc_object_release( p_playlist );
70     return 0;
71 }
72
73 static int vlclua_playlist_skip( lua_State * L )
74 {
75     int i_skip = luaL_checkint( L, 1 );
76     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
77     playlist_Skip( p_playlist, i_skip );
78     vlc_object_release( p_playlist );
79     return 0;
80 }
81
82 static int vlclua_playlist_play( lua_State * L )
83 {
84     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
85     playlist_Play( p_playlist );
86     vlc_object_release( p_playlist );
87     return 0;
88 }
89
90 static int vlclua_playlist_pause( lua_State * L )
91 {
92     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
93     playlist_Pause( p_playlist );
94     vlc_object_release( p_playlist );
95     return 0;
96 }
97
98 static int vlclua_playlist_stop( lua_State * L )
99 {
100     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
101     playlist_Stop( p_playlist );
102     vlc_object_release( p_playlist );
103     return 0;
104 }
105
106 static int vlclua_playlist_clear( lua_State * L )
107 {
108     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
109     playlist_Stop( p_playlist ); /* Isn't this already implied by Clear? */
110     playlist_Clear( p_playlist, false );
111     vlc_object_release( p_playlist );
112     return 0;
113 }
114
115 static int vlclua_playlist_repeat( 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, "repeat" );
119     vlc_object_release( p_playlist );
120     return i_ret;
121 }
122
123 static int vlclua_playlist_loop( lua_State * L )
124 {
125     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
126     int i_ret = vlclua_var_toggle_or_set( L, p_playlist, "loop" );
127     vlc_object_release( p_playlist );
128     return i_ret;
129 }
130
131 static int vlclua_playlist_random( lua_State * L )
132 {
133     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
134     int i_ret = vlclua_var_toggle_or_set( L, p_playlist, "random" );
135     vlc_object_release( p_playlist );
136     return i_ret;
137 }
138
139 static int vlclua_playlist_goto( lua_State * L )
140 {
141     int i_id = luaL_checkint( L, 1 );
142     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
143     int i_ret = playlist_Control( p_playlist, PLAYLIST_VIEWPLAY,
144                                   true, NULL,
145                                   playlist_ItemGetById( p_playlist, i_id,
146                                                         true ) );
147     vlc_object_release( p_playlist );
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     vlc_object_release( p_playlist );
159     lua_pushinteger( L, i_count );
160     return 1;
161 }
162
163 static int vlclua_playlist_enqueue( lua_State *L )
164 {
165     int i_count;
166     vlc_object_t *p_this = vlclua_get_this( L );
167     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
168     i_count = vlclua_playlist_add_internal( p_this, L, p_playlist,
169                                             NULL, false );
170     vlc_object_release( p_playlist );
171     lua_pushinteger( L, i_count );
172     return 1;
173 }
174
175 static void push_playlist_item( lua_State *L, playlist_item_t *p_item );
176 static void push_playlist_item( lua_State *L, playlist_item_t *p_item )
177 {
178     input_item_t *p_input = p_item->p_input;
179     int i_flags = p_item->i_flags;
180     lua_newtable( L );
181     lua_pushinteger( L, p_item->i_id );
182     lua_setfield( L, -2, "id" );
183     lua_newtable( L );
184 #define CHECK_AND_SET_FLAG( name, label ) \
185     if( i_flags & PLAYLIST_ ## name ## _FLAG ) \
186     { \
187         lua_pushboolean( L, 1 ); \
188         lua_setfield( L, -2, #label ); \
189     }
190     CHECK_AND_SET_FLAG( SAVE, save )
191     CHECK_AND_SET_FLAG( SKIP, skip )
192     CHECK_AND_SET_FLAG( DBL, disabled )
193     CHECK_AND_SET_FLAG( RO, ro )
194     CHECK_AND_SET_FLAG( REMOVE, remove )
195     CHECK_AND_SET_FLAG( EXPANDED, expanded )
196 #undef CHECK_AND_SET_FLAG
197     lua_setfield( L, -2, "flags" );
198     if( p_input )
199     {
200         lua_pushstring( L, p_input->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     int b_category = luaL_optboolean( L, 2, 1 ); /* Default to tree playlist (discared when 1st argument is a playlist_item's id) */
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, true );
236         if( !p_item )
237         {
238             vlc_object_release( p_playlist );
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 = b_category ? p_playlist->p_local_category
248                                 : p_playlist->p_local_onelevel;
249         else if( !strcasecmp( psz_what, "ml" )
250               || !strcasecmp( psz_what, "media library" ) )
251             p_item = b_category ? p_playlist->p_ml_category
252                                 : p_playlist->p_ml_onelevel;
253         else if( !strcasecmp( psz_what, "root" ) )
254             p_item = b_category ? p_playlist->p_root_category
255                                 : p_playlist->p_root_onelevel;
256         else
257         {
258             int i;
259             for( i = 0; i < p_playlist->i_sds; i++ )
260             {
261                 if( !strcasecmp( psz_what,
262                                  p_playlist->pp_sds[i]->p_sd->psz_module ) )
263                 {
264                     p_item = b_category ? p_playlist->pp_sds[i]->p_cat
265                                         : p_playlist->pp_sds[i]->p_one;
266                     break;
267                 }
268             }
269             if( !p_item )
270             {
271                 vlc_object_release( p_playlist );
272                 return 0; /* Should we return an error instead? */
273             }
274         }
275     }
276     else
277     {
278         p_item = b_category ? p_playlist->p_root_category
279                             : p_playlist->p_root_onelevel;
280     }
281     push_playlist_item( L, p_item );
282     vlc_object_release( p_playlist );
283     return 1;
284 }
285
286 static int vlclua_playlist_search( lua_State *L )
287 {
288     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
289     const char *psz_string = luaL_optstring( L, 1, "" );
290     int b_category = luaL_optboolean( L, 2, 1 ); /* default to category */
291     playlist_item_t *p_item = b_category ? p_playlist->p_root_category
292                                          : p_playlist->p_root_onelevel;
293     playlist_LiveSearchUpdate( p_playlist, p_item, psz_string );
294     push_playlist_item( L, p_item );
295     vlc_object_release( p_playlist );
296     return 1;
297 }
298
299 static int vlclua_playlist_current( lua_State *L )
300 {
301     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
302     lua_pushinteger( L, var_GetInteger( p_playlist, "playlist-current" ) );
303     vlc_object_release( p_playlist );
304     return 1;
305 }
306
307 static int vlc_sort_key_from_string( const char *psz_name )
308 {
309     static const struct
310     {
311         const char *psz_name;
312         int i_key;
313     } pp_keys[] =
314         { { "id", SORT_ID },
315           { "title", SORT_TITLE },
316           { "title nodes first", SORT_TITLE_NODES_FIRST },
317           { "artist", SORT_ARTIST },
318           { "genre", SORT_GENRE },
319           { "random", SORT_RANDOM },
320           { "duration", SORT_DURATION },
321           { "title numeric", SORT_TITLE_NUMERIC },
322           { "album", SORT_ALBUM },
323           { NULL, -1 } };
324     int i;
325     for( i = 0; pp_keys[i].psz_name; i++ )
326     {
327         if( !strcmp( psz_name, pp_keys[i].psz_name ) )
328             return pp_keys[i].i_key;
329     }
330     return -1;
331 }
332
333 static int vlclua_playlist_sort( lua_State *L )
334 {
335     /* allow setting the different sort keys */
336     int i_mode = vlc_sort_key_from_string( luaL_checkstring( L, 1 ) );
337     if( i_mode == -1 )
338         return luaL_error( L, "Invalid search key." );
339     int i_type = luaL_optboolean( L, 2, 0 ) ? ORDER_REVERSE : ORDER_NORMAL;
340     int b_category = luaL_optboolean( L, 3, 1 ); /* default to category */
341     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
342     playlist_item_t *p_root = b_category ? p_playlist->p_local_category
343                                          : p_playlist->p_local_onelevel;
344     int i_ret = playlist_RecursiveNodeSort( p_playlist, p_root, i_mode,
345                                             i_type );
346     vlc_object_release( p_playlist );
347     return vlclua_push_ret( L, i_ret );
348 }
349
350 /* FIXME: split this in 3 different functions? */
351 static int vlclua_playlist_status( lua_State *L )
352 {
353     intf_thread_t *p_intf = (intf_thread_t *)vlclua_get_this( L );
354     playlist_t *p_playlist = pl_Yield( p_intf );
355     /*
356     int i_count = 0;
357     lua_settop( L, 0 );*/
358     if( p_playlist->p_input )
359     {
360         /*char *psz_uri =
361             input_item_GetURI( input_GetItem( p_playlist->p_input ) );
362         lua_pushstring( L, psz_uri );
363         free( psz_uri );
364         lua_pushnumber( L, config_GetInt( p_intf, "volume" ) );*/
365         vlc_object_lock( p_playlist );
366         switch( p_playlist->status.i_status )
367         {
368             case PLAYLIST_STOPPED:
369                 lua_pushstring( L, "stopped" );
370                 break;
371             case PLAYLIST_RUNNING:
372                 lua_pushstring( L, "playing" );
373                 break;
374             case PLAYLIST_PAUSED:
375                 lua_pushstring( L, "paused" );
376                 break;
377             default:
378                 lua_pushstring( L, "unknown" );
379                 break;
380         }
381         vlc_object_unlock( p_playlist );
382         /*i_count += 3;*/
383     }
384     else
385     {
386         lua_pushstring( L, "stopped" );
387     }
388     vlc_object_release( p_playlist );
389     return 1;
390 }
391
392 /*****************************************************************************
393  *
394  *****************************************************************************/
395 static const luaL_Reg vlclua_playlist_reg[] = {
396     { "prev", vlclua_playlist_prev },
397     { "next", vlclua_playlist_next },
398     { "skip", vlclua_playlist_skip },
399     { "play", vlclua_playlist_play },
400     { "pause", vlclua_playlist_pause },
401     { "stop", vlclua_playlist_stop },
402     { "clear", vlclua_playlist_clear },
403     { "repeat", vlclua_playlist_repeat },
404     { "loop", vlclua_playlist_loop },
405     { "random", vlclua_playlist_random },
406     { "goto", vlclua_playlist_goto },
407     { "add", vlclua_playlist_add },
408     { "enqueue", vlclua_playlist_enqueue },
409     { "get", vlclua_playlist_get },
410     { "search", vlclua_playlist_search },
411     { "current", vlclua_playlist_current },
412     { "sort", vlclua_playlist_sort },
413     { "status", vlclua_playlist_status },
414     { NULL, NULL }
415 };
416
417 void luaopen_playlist( lua_State *L )
418 {
419     lua_newtable( L );
420     luaL_register( L, NULL, vlclua_playlist_reg );
421     lua_setfield( L, -2, "playlist" );
422 }