1 /*****************************************************************************
2 * intf_playlist.c : Playlist management functions
3 *****************************************************************************
4 * Copyright (C) 1999, 2000 VideoLAN
5 * $Id: intf_playlist.c,v 1.4 2001/05/07 03:14:09 stef Exp $
7 * Authors: Samuel Hocevar <sam@zoy.org>
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.
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.
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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
27 #include <stdlib.h> /* free(), strtol() */
28 #include <stdio.h> /* sprintf() */
29 #include <string.h> /* strerror() */
30 #include <errno.h> /* ENOMEM */
36 #include "intf_playlist.h"
40 /*****************************************************************************
42 *****************************************************************************/
43 static void NextItem( playlist_t * p_playlist );
45 /*****************************************************************************
46 * intf_PlaylistCreate: create playlist
47 *****************************************************************************
48 * Create a playlist structure.
49 *****************************************************************************/
50 playlist_t * intf_PlaylistCreate ( void )
52 playlist_t *p_playlist;
54 /* Allocate structure */
55 p_playlist = malloc( sizeof( playlist_t ) );
58 intf_ErrMsg( "intf error: couldn't create playlist (%s)",
66 /*****************************************************************************
67 * intf_PlaylistInit: initialize playlist
68 *****************************************************************************
69 * Initialize a playlist structure.
70 *****************************************************************************/
71 void intf_PlaylistInit ( playlist_t * p_playlist )
73 vlc_mutex_init( &p_playlist->change_lock );
75 p_playlist->i_index = -1; /* -1 means we are not playing anything yet */
76 p_playlist->i_size = 0;
78 p_playlist->i_mode = PLAYLIST_FORWARD;
79 p_playlist->i_seed = 0;
81 /* There is no current item */
82 p_playlist->current.i_type = 0;
83 p_playlist->current.i_status = 0;
84 p_playlist->current.psz_name = NULL;
86 /* The playlist is empty */
87 p_playlist->p_item = NULL;
89 intf_WarnMsg( 3, "intf: playlist initialized" );
92 /*****************************************************************************
93 * intf_PlaylistAdd: add an item to the playlist
94 *****************************************************************************
95 * Add an item to the playlist at position i_pos. If i_pos is PLAYLIST_END,
96 * add it at the end regardless of the playlist current size.
97 *****************************************************************************/
98 int intf_PlaylistAdd( playlist_t * p_playlist, int i_pos,
99 const char * psz_item )
102 playlist_item_t * p_item;
104 vlc_mutex_lock( &p_playlist->change_lock );
106 if( i_pos == PLAYLIST_END )
108 i_pos = p_playlist->i_size;
110 else if( i_pos > p_playlist->i_size )
112 intf_ErrMsg( "intf error: inserting item beyond playlist size" );
113 vlc_mutex_unlock( &p_playlist->change_lock );
117 /* Increment playlist size */
118 p_playlist->i_size++;
119 p_playlist->p_item = realloc( p_playlist->p_item,
120 p_playlist->i_size * sizeof( playlist_item_t ) );
122 /* Move second place of the playlist to make room for new item */
123 for( i_index = p_playlist->i_size - 1; i_index > i_pos; i_index-- )
125 p_playlist->p_item[ i_index ] = p_playlist->p_item[ i_index - 1 ];
128 /* Insert the new item */
129 p_item = &p_playlist->p_item[ i_pos ];
132 p_item->i_status = 0;
133 p_item->psz_name = strdup( psz_item );
135 intf_WarnMsg( 3, "intf: added `%s' to playlist", psz_item );
137 vlc_mutex_unlock( &p_playlist->change_lock );
142 /*****************************************************************************
143 * intf_PlaylistNext: switch to next playlist item
144 *****************************************************************************
145 * Switch to the next item of the playlist. If there is no next item, the
146 * position of the resulting item is set to -1.
147 *****************************************************************************/
148 void intf_PlaylistNext( playlist_t * p_playlist )
150 vlc_mutex_lock( &p_playlist->change_lock );
152 NextItem( p_playlist );
154 vlc_mutex_unlock( &p_playlist->change_lock );
157 /*****************************************************************************
158 * intf_PlaylistPrev: switch to previous playlist item
159 *****************************************************************************
160 * Switch to the previous item of the playlist. If there is no previous
161 * item, the position of the resulting item is set to -1.
162 *****************************************************************************/
163 void intf_PlaylistPrev( playlist_t * p_playlist )
165 vlc_mutex_lock( &p_playlist->change_lock );
166 p_playlist->i_mode = -p_playlist->i_mode;
168 NextItem( p_playlist );
170 p_playlist->i_mode = -p_playlist->i_mode;
171 vlc_mutex_unlock( &p_playlist->change_lock );
174 /*****************************************************************************
175 * intf_PlaylistDelete: delete an item from the playlist
176 *****************************************************************************
177 * Delete the item in the playlist with position i_pos.
178 *****************************************************************************/
179 int intf_PlaylistDelete( playlist_t * p_playlist, int i_pos )
184 vlc_mutex_lock( &p_playlist->change_lock );
186 if( !p_playlist->i_size || i_pos >= p_playlist->i_size )
188 intf_ErrMsg( "intf error: deleting item beyond playlist size" );
189 vlc_mutex_unlock( &p_playlist->change_lock );
193 /* Store the location of the item's name */
194 psz_name = p_playlist->p_item[ i_pos ].psz_name;
196 /* Fill the room by moving the next items */
197 for( i_index = i_pos; i_index < p_playlist->i_size - 1; i_index++ )
199 p_playlist->p_item[ i_index ] = p_playlist->p_item[ i_index + 1 ];
202 if( i_pos < p_playlist->i_index )
203 p_playlist->i_index--;
206 /* Decrement playlist size */
207 p_playlist->i_size--;
208 p_playlist->p_item = realloc( p_playlist->p_item,
209 p_playlist->i_size * sizeof( playlist_item_t ) );
211 intf_WarnMsg( 3, "intf: removed `%s' from playlist", psz_name );
214 /* Delete the item */
217 vlc_mutex_unlock( &p_playlist->change_lock );
222 /*****************************************************************************
223 * intf_PlaylistDestroy: destroy the playlist
224 *****************************************************************************
225 * Delete all items in the playlist and free the playlist structure.
226 *****************************************************************************/
227 void intf_PlaylistDestroy( playlist_t * p_playlist )
231 for( i_index = p_playlist->i_size - 1; p_playlist->i_size; i_index-- )
233 intf_PlaylistDelete( p_playlist, i_index );
236 vlc_mutex_destroy( &p_playlist->change_lock );
238 if( p_playlist->current.psz_name != NULL )
240 free( p_playlist->current.psz_name );
245 intf_WarnMsg( 3, "intf: playlist destroyed" );
248 /*****************************************************************************
249 * intf_PlaylistJumpto
250 *****************************************************************************/
251 void intf_PlaylistJumpto( playlist_t * p_playlist , int i_pos)
253 vlc_mutex_lock( &p_playlist->change_lock );
255 p_playlist->i_index = i_pos;
257 if( p_playlist->i_index != -1 )
259 if( p_playlist->current.psz_name != NULL )
261 free( p_playlist->current.psz_name );
263 p_playlist->current = p_playlist->p_item[ p_playlist->i_index ];
264 p_playlist->current.psz_name
265 = strdup( p_playlist->current.psz_name );
268 vlc_mutex_unlock( &p_playlist->change_lock );
272 /*****************************************************************************
273 * Following functions are local
274 *****************************************************************************/
276 /*****************************************************************************
277 * NextItem: select next playlist item
278 *****************************************************************************
279 * This function copies the next playlist item to the current structure,
280 * depending on the playlist browsing mode.
281 *****************************************************************************/
282 static void NextItem( playlist_t * p_playlist )
284 if( !p_playlist->i_size )
286 p_playlist->i_index = -1;
290 switch( p_playlist->i_mode )
292 case PLAYLIST_FORWARD:
293 p_playlist->i_index++;
294 if( p_playlist->i_index > p_playlist->i_size - 1 )
296 p_playlist->i_index = -1;
300 case PLAYLIST_FORWARD_LOOP:
301 p_playlist->i_index++;
302 if( p_playlist->i_index > p_playlist->i_size - 1 )
304 p_playlist->i_index = 0;
308 case PLAYLIST_BACKWARD:
309 p_playlist->i_index--;
310 if( p_playlist->i_index < 0 )
312 p_playlist->i_index = -1;
316 case PLAYLIST_BACKWARD_LOOP:
317 p_playlist->i_index--;
318 if( p_playlist->i_index < 0 )
320 p_playlist->i_index = p_playlist->i_size - 1;
324 case PLAYLIST_REPEAT_CURRENT:
325 /* Just repeat what we were doing */
326 if( p_playlist->i_index < 0
327 || p_playlist->i_index > p_playlist->i_size - 1 )
329 p_playlist->i_index = 0;
333 case PLAYLIST_RANDOM:
335 p_playlist->i_index++;
336 if( p_playlist->i_index > p_playlist->i_size - 1 )
338 p_playlist->i_index = 0;
343 /* Duplicate the playlist entry */
344 if( p_playlist->i_index != -1 )
346 if( p_playlist->current.psz_name != NULL )
348 free( p_playlist->current.psz_name );
350 p_playlist->current = p_playlist->p_item[ p_playlist->i_index ];
351 p_playlist->current.psz_name
352 = strdup( p_playlist->current.psz_name );