1 /*****************************************************************************
2 * intf_playlist.c : Playlist management functions
3 *****************************************************************************
4 * Copyright (C) 1999-2001 VideoLAN
5 * $Id: intf_playlist.c,v 1.11 2001/12/07 18:33:08 sam 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 *****************************************************************************/
25 #include <stdlib.h> /* free(), strtol() */
26 #include <stdio.h> /* sprintf() */
27 #include <string.h> /* strerror() */
28 #include <errno.h> /* ENOMEM */
34 #include "intf_playlist.h"
36 /*****************************************************************************
38 *****************************************************************************/
39 static void NextItem( playlist_t * p_playlist );
41 /*****************************************************************************
42 * intf_PlaylistCreate: create playlist
43 *****************************************************************************
44 * Create a playlist structure.
45 *****************************************************************************/
46 playlist_t * intf_PlaylistCreate ( void )
48 playlist_t *p_playlist;
50 /* Allocate structure */
51 p_playlist = malloc( sizeof( playlist_t ) );
54 intf_ErrMsg( "intf error: couldn't create playlist (%s)",
62 /*****************************************************************************
63 * intf_PlaylistInit: initialize playlist
64 *****************************************************************************
65 * Initialize a playlist structure.
66 *****************************************************************************/
67 void intf_PlaylistInit ( playlist_t * p_playlist )
69 vlc_mutex_init( &p_playlist->change_lock );
71 p_playlist->i_index = -1; /* -1 means we are not playing anything yet */
72 p_playlist->i_size = 0;
74 p_playlist->i_mode = PLAYLIST_FORWARD;
75 p_playlist->i_seed = 0;
76 p_playlist->b_stopped = 0;
78 /* There is no current item */
79 p_playlist->current.i_type = 0;
80 p_playlist->current.i_status = 0;
81 p_playlist->current.psz_name = NULL;
83 /* The playlist is empty */
84 p_playlist->p_item = NULL;
86 intf_WarnMsg( 3, "intf: playlist initialized" );
89 /*****************************************************************************
90 * intf_PlaylistAdd: add an item to the playlist
91 *****************************************************************************
92 * Add an item to the playlist at position i_pos. If i_pos is PLAYLIST_END,
93 * add it at the end regardless of the playlist current size.
94 *****************************************************************************/
95 int intf_PlaylistAdd( playlist_t * p_playlist, int i_pos,
96 const char * psz_item )
99 playlist_item_t * p_item;
101 vlc_mutex_lock( &p_playlist->change_lock );
103 if( i_pos == PLAYLIST_END )
105 i_pos = p_playlist->i_size;
107 else if( i_pos > p_playlist->i_size )
109 intf_ErrMsg( "intf error: inserting item beyond playlist size" );
110 vlc_mutex_unlock( &p_playlist->change_lock );
114 /* Increment playlist size */
115 p_playlist->i_size++;
116 p_playlist->p_item = realloc( p_playlist->p_item,
117 p_playlist->i_size * sizeof( playlist_item_t ) );
119 /* Move second place of the playlist to make room for new item */
120 for( i_index = p_playlist->i_size - 1; i_index > i_pos; i_index-- )
122 p_playlist->p_item[ i_index ] = p_playlist->p_item[ i_index - 1 ];
125 /* Insert the new item */
126 p_item = &p_playlist->p_item[ i_pos ];
129 p_item->i_status = 0;
130 p_item->psz_name = strdup( psz_item );
132 intf_WarnMsg( 3, "intf: added `%s' to playlist", psz_item );
134 vlc_mutex_unlock( &p_playlist->change_lock );
139 /*****************************************************************************
140 * intf_PlaylistNext: switch to next playlist item
141 *****************************************************************************
142 * Switch to the next item of the playlist. If there is no next item, the
143 * position of the resulting item is set to -1.
144 *****************************************************************************/
145 void intf_PlaylistNext( playlist_t * p_playlist )
147 vlc_mutex_lock( &p_playlist->change_lock );
149 NextItem( p_playlist );
151 vlc_mutex_unlock( &p_playlist->change_lock );
154 /*****************************************************************************
155 * intf_PlaylistPrev: switch to previous playlist item
156 *****************************************************************************
157 * Switch to the previous item of the playlist. If there is no previous
158 * item, the position of the resulting item is set to -1.
159 *****************************************************************************/
160 void intf_PlaylistPrev( playlist_t * p_playlist )
162 vlc_mutex_lock( &p_playlist->change_lock );
164 p_playlist->i_mode = -p_playlist->i_mode;
165 NextItem( p_playlist );
166 p_playlist->i_mode = -p_playlist->i_mode;
168 vlc_mutex_unlock( &p_playlist->change_lock );
171 /*****************************************************************************
172 * intf_PlaylistDelete: delete an item from the playlist
173 *****************************************************************************
174 * Delete the item in the playlist with position i_pos.
175 *****************************************************************************/
176 int intf_PlaylistDelete( playlist_t * p_playlist, int i_pos )
181 vlc_mutex_lock( &p_playlist->change_lock );
183 if( !p_playlist->i_size || i_pos >= p_playlist->i_size )
185 intf_ErrMsg( "intf error: deleting item beyond playlist size" );
186 vlc_mutex_unlock( &p_playlist->change_lock );
190 /* Store the location of the item's name */
191 psz_name = p_playlist->p_item[ i_pos ].psz_name;
193 /* Fill the room by moving the next items */
194 for( i_index = i_pos; i_index < p_playlist->i_size - 1; i_index++ )
196 p_playlist->p_item[ i_index ] = p_playlist->p_item[ i_index + 1 ];
199 if( i_pos < p_playlist->i_index )
200 p_playlist->i_index--;
203 /* Decrement playlist size */
204 p_playlist->i_size--;
205 p_playlist->p_item = realloc( p_playlist->p_item,
206 p_playlist->i_size * sizeof( playlist_item_t ) );
208 intf_WarnMsg( 3, "intf: removed `%s' from playlist", psz_name );
211 /* Delete the item */
214 vlc_mutex_unlock( &p_playlist->change_lock );
219 /*****************************************************************************
220 * intf_PlaylistDestroy: destroy the playlist
221 *****************************************************************************
222 * Delete all items in the playlist and free the playlist structure.
223 *****************************************************************************/
224 void intf_PlaylistDestroy( playlist_t * p_playlist )
228 for( i_index = p_playlist->i_size - 1; p_playlist->i_size; i_index-- )
230 intf_PlaylistDelete( p_playlist, i_index );
233 vlc_mutex_destroy( &p_playlist->change_lock );
235 if( p_playlist->current.psz_name != NULL )
237 free( p_playlist->current.psz_name );
242 intf_WarnMsg( 3, "intf: playlist destroyed" );
245 /*****************************************************************************
246 * intf_PlaylistJumpto: go to a specified position in playlist.
247 *****************************************************************************/
248 void intf_PlaylistJumpto( playlist_t * p_playlist , int i_pos)
250 vlc_mutex_lock( &p_playlist->change_lock );
252 p_playlist->i_index = i_pos;
254 if( p_playlist->i_index != -1 )
256 if( p_playlist->current.psz_name != NULL )
258 free( p_playlist->current.psz_name );
261 p_playlist->current = p_playlist->p_item[ p_playlist->i_index ];
262 p_playlist->current.psz_name
263 = strdup( p_playlist->current.psz_name );
266 p_main->p_playlist->b_stopped = 0;
268 vlc_mutex_unlock( &p_playlist->change_lock );
271 /* URL-decode a file: URL path, return NULL if it's not what we expect */
272 void intf_UrlDecode( char *encoded_path )
274 char *tmp = NULL, *cur = NULL, *ext = NULL;
277 if( !encoded_path || *encoded_path == '\0' )
284 tmp = calloc(strlen(encoded_path) + 1, sizeof(char) );
286 while ( ( ext = strchr(cur, '%') ) != NULL)
288 strncat(tmp, cur, (ext - cur) / sizeof(char));
291 if (!sscanf(ext, "%2x", &realchar))
297 tmp[strlen(tmp)] = (char)realchar;
303 strcpy(encoded_path,tmp);
306 /*****************************************************************************
307 * Following functions are local
308 *****************************************************************************/
310 /*****************************************************************************
311 * NextItem: select next playlist item
312 *****************************************************************************
313 * This function copies the next playlist item to the current structure,
314 * depending on the playlist browsing mode.
315 *****************************************************************************/
316 static void NextItem( playlist_t * p_playlist )
318 if( !p_playlist->i_size )
320 p_playlist->i_index = -1;
324 switch( p_playlist->i_mode )
326 case PLAYLIST_FORWARD:
327 p_playlist->i_index++;
328 if( p_playlist->i_index > p_playlist->i_size - 1 )
330 p_playlist->i_index = -1;
334 case PLAYLIST_FORWARD_LOOP:
335 p_playlist->i_index++;
336 if( p_playlist->i_index > p_playlist->i_size - 1 )
338 p_playlist->i_index = 0;
342 case PLAYLIST_BACKWARD:
343 p_playlist->i_index--;
344 if( p_playlist->i_index < 0 )
346 p_playlist->i_index = -1;
350 case PLAYLIST_BACKWARD_LOOP:
351 p_playlist->i_index--;
352 if( p_playlist->i_index < 0 )
354 p_playlist->i_index = p_playlist->i_size - 1;
358 case PLAYLIST_REPEAT_CURRENT:
359 /* Just repeat what we were doing */
360 if( p_playlist->i_index < 0
361 || p_playlist->i_index > p_playlist->i_size - 1 )
363 p_playlist->i_index = 0;
367 case PLAYLIST_RANDOM:
369 p_playlist->i_index++;
370 if( p_playlist->i_index > p_playlist->i_size - 1 )
372 p_playlist->i_index = 0;
377 /* Duplicate the playlist entry */
378 if( p_playlist->i_index != -1 )
380 if( p_playlist->current.psz_name != NULL )
382 free( p_playlist->current.psz_name );
384 p_playlist->current = p_playlist->p_item[ p_playlist->i_index ];
385 p_playlist->current.psz_name
386 = strdup( p_playlist->current.psz_name );