]> git.sesse.net Git - vlc/blob - src/control/playlist.c
macosx: fixed menubar appearance in fullscreen mode by partially reverting [46c93c9cc...
[vlc] / src / control / playlist.c
1 /*****************************************************************************
2  * playlist.c: libvlc new API playlist handling functions
3  *****************************************************************************
4  * Copyright (C) 2005 the VideoLAN team
5  * $Id$
6  *
7  * Authors: ClĂ©ment Stenac <zorglub@videolan.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 #include "libvlc_internal.h"
25 #include "libvlc.h"
26
27 #include <vlc/libvlc.h>
28 #include <vlc_playlist.h>
29
30 #include <assert.h>
31
32 #define PL (libvlc_priv (p_instance->p_libvlc_int)->p_playlist)
33
34 static inline int playlist_was_locked( libvlc_instance_t *p_instance )
35 {
36     int was_locked;
37     vlc_mutex_lock( &p_instance->instance_lock );
38     was_locked = p_instance->b_playlist_locked;
39     vlc_mutex_unlock( &p_instance->instance_lock );
40     return was_locked;
41 }
42
43 static inline void playlist_mark_locked( libvlc_instance_t *p_instance,
44                                          int locked )
45 {
46     vlc_mutex_lock( &p_instance->instance_lock );
47     p_instance->b_playlist_locked = locked;
48     vlc_mutex_unlock( &p_instance->instance_lock );
49 }
50
51 void libvlc_playlist_loop( libvlc_instance_t *p_instance, int loop,
52                            libvlc_exception_t *p_e)
53 {
54     VLC_UNUSED(p_e);
55
56     assert( PL );
57     var_SetBool( PL, "loop", loop );
58 }
59
60 void libvlc_playlist_play( libvlc_instance_t *p_instance, int i_id,
61                            int i_options, char **ppsz_options,
62                            libvlc_exception_t *p_e )
63 {
64     VLC_UNUSED(p_e); VLC_UNUSED(i_options); VLC_UNUSED(ppsz_options);
65
66     int did_lock = 0;
67     assert( PL );
68     ///\todo Handle additionnal options
69
70     if( PL->items.i_size == 0 ) RAISEVOID( "Empty playlist" );
71     if( i_id > 0 )
72     {
73         playlist_item_t *p_item;
74         if (! playlist_was_locked( p_instance ) )
75         {
76             playlist_mark_locked( p_instance, 1 );
77             playlist_Lock( PL );
78             did_lock = 1;
79         }
80
81         p_item = playlist_ItemGetByInputId( PL, i_id,
82                                             PL->p_root_category );
83         if( p_item )
84             playlist_Control( PL, PLAYLIST_VIEWPLAY, pl_Locked,
85                               PL->p_root_category, p_item );
86        else
87             RAISEVOID( "Unable to find item" );
88
89         if( did_lock == 1 )
90         {
91             playlist_Unlock( PL );
92             playlist_mark_locked( p_instance, 0 );
93         }
94     }
95     else
96     {
97         playlist_Control( PL, PLAYLIST_PLAY,
98                           playlist_was_locked( p_instance ) );
99     }
100 }
101
102 void libvlc_playlist_pause( libvlc_instance_t *p_instance,
103                             libvlc_exception_t *p_e )
104 {
105     assert( PL );
106     if( playlist_Control( PL, PLAYLIST_PAUSE,
107                           playlist_was_locked( p_instance ) ) != VLC_SUCCESS )
108         RAISEVOID( "Empty playlist" );
109 }
110
111
112 void libvlc_playlist_stop( libvlc_instance_t *p_instance,
113                            libvlc_exception_t *p_e )
114 {
115     assert( PL );
116     if( playlist_Control( PL, PLAYLIST_STOP,
117                           playlist_was_locked( p_instance ) ) != VLC_SUCCESS )
118         RAISEVOID( "Empty playlist" );
119 }
120
121 void libvlc_playlist_clear( libvlc_instance_t *p_instance,
122                             libvlc_exception_t *p_e )
123 {
124     VLC_UNUSED(p_e);
125
126     assert( PL );
127     playlist_Clear( PL, playlist_was_locked( p_instance ) );
128 }
129
130 void libvlc_playlist_next( libvlc_instance_t *p_instance,
131                            libvlc_exception_t *p_e )
132 {
133     assert( PL );
134     if( playlist_Control( PL, PLAYLIST_SKIP, playlist_was_locked( p_instance ),
135                           1 ) != VLC_SUCCESS )
136         RAISEVOID( "Empty playlist" );
137 }
138
139 void libvlc_playlist_prev( libvlc_instance_t *p_instance,
140                            libvlc_exception_t *p_e )
141 {
142     if( playlist_Control( PL, PLAYLIST_SKIP, playlist_was_locked( p_instance ),
143                           -1  ) != VLC_SUCCESS )
144         RAISEVOID( "Empty playlist" );
145 }
146
147 int libvlc_playlist_add( libvlc_instance_t *p_instance, const char *psz_uri,
148                          const char *psz_name, libvlc_exception_t *p_e )
149 {
150     return libvlc_playlist_add_extended( p_instance, psz_uri, psz_name,
151                                          0, NULL, p_e );
152 }
153
154 static int PlaylistAddExtended( libvlc_instance_t *p_instance,
155                                 const char *psz_uri, const char *psz_name,
156                                 int i_options, const char **ppsz_options,
157                                 unsigned i_option_flags,
158                                 libvlc_exception_t *p_e )
159 {
160     assert( PL );
161     if( playlist_was_locked( p_instance ) )
162     {
163         libvlc_exception_raise( p_e, "You must unlock playlist before "
164                                "calling libvlc_playlist_add" );
165         return VLC_EGENERIC;
166     }
167     return playlist_AddExt( PL, psz_uri, psz_name,
168                             PLAYLIST_INSERT, PLAYLIST_END, -1,
169                             i_options, ppsz_options, i_option_flags,
170                             true, pl_Unlocked );
171 }
172 int libvlc_playlist_add_extended( libvlc_instance_t *p_instance,
173                                   const char *psz_uri, const char *psz_name,
174                                   int i_options, const char **ppsz_options,
175                                   libvlc_exception_t *p_e )
176 {
177     return PlaylistAddExtended( p_instance, psz_uri, psz_name,
178                                 i_options, ppsz_options, VLC_INPUT_OPTION_TRUSTED,
179                                 p_e );
180 }
181 int libvlc_playlist_add_extended_untrusted( libvlc_instance_t *p_instance,
182                                             const char *psz_uri, const char *psz_name,
183                                             int i_options, const char **ppsz_options,
184                                             libvlc_exception_t *p_e )
185 {
186     return PlaylistAddExtended( p_instance, psz_uri, psz_name,
187                                 i_options, ppsz_options, 0,
188                                 p_e );
189 }
190
191 int libvlc_playlist_delete_item( libvlc_instance_t *p_instance, int i_id,
192                                  libvlc_exception_t *p_e )
193 {
194     assert( PL );
195
196     if( playlist_DeleteFromInput( PL, i_id,
197                                   playlist_was_locked( p_instance ) ) )
198     {
199         libvlc_exception_raise( p_e, "deletion failed" );
200         return VLC_ENOITEM;
201     }
202     return VLC_SUCCESS;
203 }
204
205 int libvlc_playlist_isplaying( libvlc_instance_t *p_instance,
206                                libvlc_exception_t *p_e )
207 {
208     VLC_UNUSED(p_e);
209
210     assert( PL );
211     return playlist_Status( PL ) == PLAYLIST_RUNNING;
212 }
213
214 int libvlc_playlist_items_count( libvlc_instance_t *p_instance,
215                                  libvlc_exception_t *p_e )
216 {
217     VLC_UNUSED(p_e);
218
219     assert( PL );
220     return playlist_CurrentSize( PL );
221 }
222
223 int libvlc_playlist_get_current_index ( libvlc_instance_t *p_instance,
224                                         libvlc_exception_t *p_e )
225 {
226     VLC_UNUSED(p_e);
227
228     assert( PL );
229     playlist_item_t *p_item = playlist_CurrentPlayingItem( PL );
230     if( !p_item )
231         return -1;
232     return p_item->i_id;
233 }
234
235 void libvlc_playlist_lock( libvlc_instance_t *p_instance )
236 {
237     assert( PL );
238     playlist_Lock( PL );
239     p_instance->b_playlist_locked = 1;
240 }
241
242 void libvlc_playlist_unlock( libvlc_instance_t *p_instance )
243 {
244     assert( PL );
245     p_instance->b_playlist_locked = 0;
246     playlist_Unlock( PL );
247 }
248
249 libvlc_media_player_t * libvlc_playlist_get_media_player(
250                                 libvlc_instance_t *p_instance,
251                                 libvlc_exception_t *p_e )
252 {
253     libvlc_media_player_t *p_mi;
254     assert( PL );
255     input_thread_t * input = playlist_CurrentInput( PL );
256     if( input )
257     {
258         p_mi = libvlc_media_player_new_from_input_thread(
259                             p_instance, input, p_e );
260         vlc_object_release( input );
261     }
262     else
263     {
264         /* no active input */
265         p_mi = NULL;
266         libvlc_exception_raise( p_e, "No active input" );
267     }
268
269     return p_mi;
270 }
271