* src/control/playlist.c: Fix typo vlc_mutex_unlokc -> vlc_mutex_unlock
[vlc.git] / 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 <vlc/libvlc.h>
26
27 #include <vlc/intf.h>
28
29 #include <assert.h>
30
31 #define PL p_instance->p_libvlc_int->p_playlist
32
33 void libvlc_playlist_loop( libvlc_instance_t *p_instance, vlc_bool_t loop,
34                            libvlc_exception_t *p_e)
35 {
36     assert( PL );
37     var_SetBool(PL,"loop",loop);
38 }
39
40 void libvlc_playlist_play( libvlc_instance_t *p_instance, int i_id,
41                            int i_options, char **ppsz_options,
42                            libvlc_exception_t *p_e )
43 {
44     assert( PL );
45     ///\todo Handle additionnal options
46
47     if( PL->items.i_size == 0 ) RAISEVOID( "Empty playlist" );
48     if( i_id > 0 )
49     {
50         playlist_item_t *p_item = playlist_ItemGetById( PL, i_id, VLC_TRUE );
51         if( !p_item ) RAISEVOID( "Unable to find item" );
52
53         playlist_Control( PL, PLAYLIST_VIEWPLAY, VLC_FALSE,
54                           PL->status.p_node, p_item );
55     }
56     else
57     {
58         playlist_Play( PL );
59     }
60 }
61
62 void libvlc_playlist_pause( libvlc_instance_t *p_instance,
63                            libvlc_exception_t *p_e )
64 {
65     assert( PL );
66     if( playlist_Pause( PL ) != VLC_SUCCESS )
67         RAISEVOID( "Empty playlist" );
68 }
69
70
71 void libvlc_playlist_stop( libvlc_instance_t *p_instance,
72                            libvlc_exception_t *p_e )
73 {
74     assert( PL );
75     if( playlist_Stop( PL ) != VLC_SUCCESS )
76         RAISEVOID( "Empty playlist" );
77 }
78
79 void libvlc_playlist_clear( libvlc_instance_t *p_instance,
80                            libvlc_exception_t *p_e )
81 {
82     assert( PL );
83     playlist_Clear( PL, VLC_FALSE );
84 }
85
86 void libvlc_playlist_next( libvlc_instance_t *p_instance,
87                            libvlc_exception_t *p_e )
88 {
89     assert( PL );
90     if( playlist_Next( PL ) != VLC_SUCCESS )
91         RAISEVOID( "Empty playlist" );
92 }
93
94 void libvlc_playlist_prev( libvlc_instance_t *p_instance,
95                            libvlc_exception_t *p_e )
96 {
97     if( playlist_Prev( PL ) != VLC_SUCCESS )
98         RAISEVOID( "Empty playlist" );
99 }
100
101 int libvlc_playlist_add( libvlc_instance_t *p_instance, const char *psz_uri,
102                          const char *psz_name, libvlc_exception_t *p_e )
103 {
104     return libvlc_playlist_add_extended( p_instance, psz_uri, psz_name,
105                                          0, NULL, p_e );
106 }
107
108 int libvlc_playlist_add_extended( libvlc_instance_t *p_instance,
109                                   const char *psz_uri, const char *psz_name,
110                                   int i_options, const char **ppsz_options,
111                                   libvlc_exception_t *p_e )
112 {
113     assert( PL );
114     return playlist_AddExt( PL, psz_uri, psz_name,
115                             PLAYLIST_INSERT, PLAYLIST_END, -1, ppsz_options,
116                             i_options, 1 );
117 }
118
119 int libvlc_playlist_delete_item( libvlc_instance_t *p_instance, int i_id,
120                                  libvlc_exception_t *p_e )
121 {
122     playlist_item_t *p_item;
123     assert( PL );
124     vlc_mutex_lock( &PL->object_lock );
125     p_item = playlist_ItemGetById( PL, i_id, VLC_TRUE );
126     if( p_item && p_item->p_input ) {
127         int i_ret = playlist_DeleteFromInput( PL, p_item->p_input->i_id, VLC_TRUE );
128         if( i_ret ) {
129             libvlc_exception_raise( p_e, "delete failed" );
130             vlc_mutex_unlock( &PL->object_lock );
131             return VLC_EGENERIC;
132         }
133         else {
134             vlc_mutex_unlock( &PL->object_lock );
135             return VLC_SUCCESS;
136         }
137     }
138     libvlc_exception_raise( p_e, "item not found" );
139     vlc_mutex_unlock( &PL->object_lock );
140     return VLC_EGENERIC;
141 }
142
143
144 int libvlc_playlist_isplaying( libvlc_instance_t *p_instance,
145                                libvlc_exception_t *p_e )
146 {
147     assert( PL );
148     return playlist_IsPlaying( PL );
149 }
150
151 int libvlc_playlist_items_count( libvlc_instance_t *p_instance,
152                                  libvlc_exception_t *p_e )
153 {
154     assert( PL );
155     return PL->items.i_size;
156 }
157
158 libvlc_input_t * libvlc_playlist_get_input( libvlc_instance_t *p_instance,
159                                             libvlc_exception_t *p_e )
160 {
161     libvlc_input_t *p_input;
162     assert( PL );
163
164     vlc_mutex_lock( &PL->object_lock );
165     if( PL->p_input == NULL )
166     {
167         libvlc_exception_raise( p_e, "No active input" );
168         vlc_mutex_unlock( &PL->object_lock );
169         return NULL;
170     }
171     p_input = (libvlc_input_t *)malloc( sizeof( libvlc_input_t ) );
172
173     p_input->i_input_id = PL->p_input->i_object_id;
174     p_input->p_instance = p_instance;
175     vlc_mutex_unlock( &PL->object_lock );
176
177     return p_input;
178 }