1 /*****************************************************************************
2 * item.c : Playlist item functions
3 *****************************************************************************
4 * Copyright (C) 1999-2001 VideoLAN
5 * $Id: item.c,v 1.9 2003/12/13 17:16:11 gbazin 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 *****************************************************************************/
23 #include <stdlib.h> /* free(), strtol() */
24 #include <stdio.h> /* sprintf() */
25 #include <string.h> /* strerror() */
30 #include <vlc/input.h>
32 #include "vlc_playlist.h"
35 * Add an MRL to the playlist. This is a simplified version of
36 * playlist_AddExt inculded for convenince. It equals calling playlist_AddExt
37 * with psz_name == psz_target and i_duration == -1
40 int playlist_Add( playlist_t *p_playlist, const char *psz_target,
41 const char **ppsz_options, int i_options,
42 int i_mode, int i_pos )
44 return playlist_AddExt( p_playlist, psz_target, psz_target, -1,
45 ppsz_options, i_options, i_mode, i_pos );
49 * Add a MRL into the playlist.
51 * \param p_playlist the playlist to add into
52 * \param psz_uri the mrl to add to the playlist
53 * \param psz_name a text giving a name or description of this item
54 * \param i_duration a hint about the duration of this item, in microseconds,
56 * \param ppsz_options array of options
57 * \param i_options number of items in ppsz_options
58 * \param i_mode the mode used when adding
59 * \param i_pos the position in the playlist where to add. If this is
60 * PLAYLIST_END the item will be added at the end of the playlist
61 * regardless of it's size
62 * \return always returns 0
64 int playlist_AddExt( playlist_t *p_playlist, const char * psz_uri,
65 const char * psz_name, mtime_t i_duration,
66 const char **ppsz_options, int i_options, int i_mode,
69 playlist_item_t * p_item;
71 p_item = malloc( sizeof( playlist_item_t ) );
74 msg_Err( p_playlist, "out of memory" );
77 p_item->psz_name = strdup( psz_name );
78 p_item->psz_uri = strdup( psz_uri );
79 p_item->psz_author = strdup( "" );
80 p_item->i_duration = i_duration;
83 p_item->b_autodeletion = VLC_FALSE;
84 p_item->b_enabled = VLC_TRUE;
85 p_item->i_group = PLAYLIST_TYPE_MANUAL;
87 p_item->ppsz_options = NULL;
88 p_item->i_options = i_options;
94 p_item->ppsz_options = malloc( i_options * sizeof(char *) );
95 for( i = 0; i < i_options; i++ )
97 p_item->ppsz_options[i] = strdup( ppsz_options[i] );
102 return playlist_AddItem( p_playlist, p_item, i_mode, i_pos );
106 * Add a playlist item into a playlist
108 * \param p_playlist the playlist to insert into
109 * \param p_item the playlist item to insert
110 * \param i_mode the mode used when adding
111 * \param i_pos the possition in the playlist where to add. If this is
112 * PLAYLIST_END the item will be added at the end of the playlist
113 * regardless of it's size
114 * \return always returns 0
116 int playlist_AddItem( playlist_t *p_playlist, playlist_item_t * p_item,
117 int i_mode, int i_pos)
121 vlc_mutex_lock( &p_playlist->object_lock );
124 * CHECK_INSERT : checks if the item is already enqued before
127 if ( i_mode & PLAYLIST_CHECK_INSERT )
131 if ( p_playlist->pp_items )
133 for ( j = 0; j < p_playlist->i_size; j++ )
135 if ( !strcmp( p_playlist->pp_items[j]->psz_uri, p_item->psz_uri ) )
137 if( p_item->psz_name )
139 free( p_item->psz_name );
141 if( p_item->psz_uri )
143 free( p_item->psz_uri );
145 if( p_item->i_options )
148 for( i_opt = 0; i_opt < p_item->i_options; i_opt++ )
150 free( p_item->ppsz_options[i_opt] );
152 free( p_item->ppsz_options );
154 if( p_item->psz_author )
156 free( p_item->psz_author );
159 vlc_mutex_unlock( &p_playlist->object_lock );
164 i_mode &= ~PLAYLIST_CHECK_INSERT;
165 i_mode |= PLAYLIST_APPEND;
169 msg_Dbg( p_playlist, "adding playlist item « %s » ( %s )", p_item->psz_name, p_item->psz_uri);
171 /* Create the new playlist item */
174 /* Do a few boundary checks and allocate space for the item */
175 if( i_pos == PLAYLIST_END )
177 if( i_mode & PLAYLIST_INSERT )
179 i_mode &= ~PLAYLIST_INSERT;
180 i_mode |= PLAYLIST_APPEND;
183 i_pos = p_playlist->i_size - 1;
186 if( !(i_mode & PLAYLIST_REPLACE)
187 || i_pos < 0 || i_pos >= p_playlist->i_size )
189 /* Additional boundary checks */
190 if( i_mode & PLAYLIST_APPEND )
199 else if( i_pos > p_playlist->i_size )
201 i_pos = p_playlist->i_size;
204 INSERT_ELEM( p_playlist->pp_items,
208 p_playlist->i_enabled ++;
210 if( p_playlist->i_index >= i_pos )
212 p_playlist->i_index++;
217 /* i_mode == PLAYLIST_REPLACE and 0 <= i_pos < p_playlist->i_size */
218 if( p_playlist->pp_items[i_pos]->psz_name )
220 free( p_playlist->pp_items[i_pos]->psz_name );
222 if( p_playlist->pp_items[i_pos]->psz_uri )
224 free( p_playlist->pp_items[i_pos]->psz_uri );
226 if( p_playlist->pp_items[i_pos]->psz_author )
228 free( p_playlist->pp_items[i_pos]->psz_author );
230 /* XXX: what if the item is still in use? */
231 free( p_playlist->pp_items[i_pos] );
232 p_playlist->pp_items[i_pos] = p_item;
235 if( i_mode & PLAYLIST_GO )
237 p_playlist->i_index = i_pos;
238 if( p_playlist->p_input )
240 input_StopThread( p_playlist->p_input );
242 p_playlist->i_status = PLAYLIST_RUNNING;
245 vlc_mutex_unlock( &p_playlist->object_lock );
247 val.b_bool = VLC_TRUE;
248 var_Set( p_playlist, "intf-change", val );
254 * delete an item from a playlist.
256 * \param p_playlist the playlist to remove from.
257 * \param i_pos the position of the item to remove
260 int playlist_Delete( playlist_t * p_playlist, int i_pos )
264 /* if i_pos is the current played item, playlist should stop playing it */
265 if( p_playlist->i_status == PLAYLIST_RUNNING &&
266 p_playlist->i_index == i_pos )
268 playlist_Command( p_playlist, PLAYLIST_SKIP, 1 );
271 vlc_mutex_lock( &p_playlist->object_lock );
272 if( i_pos >= 0 && i_pos < p_playlist->i_size )
274 playlist_item_t *p_item = p_playlist->pp_items[i_pos];
276 msg_Dbg( p_playlist, "deleting playlist item « %s »",
279 if( p_item->psz_name )
281 free( p_item->psz_name );
283 if( p_item->psz_uri )
285 free( p_item->psz_uri );
287 if( p_item->psz_author )
289 free( p_item->psz_author );
291 if( p_item->i_options > 0 )
295 for( i = 0; i < p_item->i_options; i++ )
297 free( p_item->ppsz_options[i] );
300 free( p_item->ppsz_options );
303 /* XXX: what if the item is still in use? */
306 if( i_pos <= p_playlist->i_index )
308 p_playlist->i_index--;
311 /* Renumber the playlist */
312 REMOVE_ELEM( p_playlist->pp_items,
315 if( p_playlist->i_enabled > 0 )
316 p_playlist->i_enabled--;
319 vlc_mutex_unlock( &p_playlist->object_lock );
321 val.b_bool = VLC_TRUE;
322 var_Set( p_playlist, "intf-change", val );
328 * Disables a playlist item
330 * \param p_playlist the playlist to disable from.
331 * \param i_pos the position of the item to disable
334 int playlist_Disable( playlist_t * p_playlist, int i_pos )
337 vlc_mutex_lock( &p_playlist->object_lock );
340 if( i_pos >= 0 && i_pos < p_playlist->i_size )
342 msg_Dbg( p_playlist, "disabling playlist item « %s »",
343 p_playlist->pp_items[i_pos]->psz_name );
345 if( p_playlist->pp_items[i_pos]->b_enabled == VLC_TRUE )
346 p_playlist->i_enabled--;
347 p_playlist->pp_items[i_pos]->b_enabled = VLC_FALSE;
350 vlc_mutex_unlock( &p_playlist->object_lock );
352 val.b_bool = VLC_TRUE;
353 var_Set( p_playlist, "intf-change", val );
359 * Enables a playlist item
361 * \param p_playlist the playlist to enable from.
362 * \param i_pos the position of the item to enable
365 int playlist_Enable( playlist_t * p_playlist, int i_pos )
368 vlc_mutex_lock( &p_playlist->object_lock );
370 if( i_pos >= 0 && i_pos < p_playlist->i_size )
372 msg_Dbg( p_playlist, "enabling playlist item « %s »",
373 p_playlist->pp_items[i_pos]->psz_name );
375 if( p_playlist->pp_items[i_pos]->b_enabled == VLC_FALSE )
376 p_playlist->i_enabled++;
378 p_playlist->pp_items[i_pos]->b_enabled = VLC_TRUE;
381 vlc_mutex_unlock( &p_playlist->object_lock );
383 val.b_bool = VLC_TRUE;
384 var_Set( p_playlist, "intf-change", val );
390 * Disables a playlist group
392 * \param p_playlist the playlist to disable from.
393 * \param i_pos the id of the group to disable
396 int playlist_DisableGroup( playlist_t * p_playlist, int i_group)
400 vlc_mutex_lock( &p_playlist->object_lock );
402 msg_Dbg(p_playlist,"Disabling group %i",i_group);
403 for( i = 0 ; i< p_playlist->i_size; i++ )
405 if( p_playlist->pp_items[i]->i_group == i_group )
407 msg_Dbg( p_playlist, "disabling playlist item « %s »",
408 p_playlist->pp_items[i]->psz_name );
410 if( p_playlist->pp_items[i]->b_enabled == VLC_TRUE )
411 p_playlist->i_enabled--;
413 p_playlist->pp_items[i]->b_enabled = VLC_FALSE;
416 vlc_mutex_unlock( &p_playlist->object_lock );
418 val.b_bool = VLC_TRUE;
419 var_Set( p_playlist, "intf-change", val );
425 * Enables a playlist group
427 * \param p_playlist the playlist to enable from.
428 * \param i_pos the id of the group to enable
431 int playlist_EnableGroup( playlist_t * p_playlist, int i_group)
435 vlc_mutex_lock( &p_playlist->object_lock );
437 for( i = 0 ; i< p_playlist->i_size; i++ )
439 if( p_playlist->pp_items[i]->i_group == i_group )
441 msg_Dbg( p_playlist, "enabling playlist item « %s »",
442 p_playlist->pp_items[i]->psz_name );
444 if( p_playlist->pp_items[i]->b_enabled == VLC_FALSE )
445 p_playlist->i_enabled++;
447 p_playlist->pp_items[i]->b_enabled = VLC_TRUE;
450 vlc_mutex_unlock( &p_playlist->object_lock );
452 val.b_bool = VLC_TRUE;
453 var_Set( p_playlist, "intf-change", val );
459 * Move an item in a playlist
461 * Move the item in the playlist with position i_pos before the current item
462 * at position i_newpos.
463 * \param p_playlist the playlist to move items in
464 * \param i_pos the position of the item to move
465 * \param i_newpos the position of the item that will be behind the moved item
469 int playlist_Move( playlist_t * p_playlist, int i_pos, int i_newpos)
472 vlc_mutex_lock( &p_playlist->object_lock );
474 /* take into account that our own row disappears. */
475 if ( i_pos < i_newpos ) i_newpos--;
477 if( i_pos >= 0 && i_newpos >=0 && i_pos <= p_playlist->i_size
478 && i_newpos <= p_playlist->i_size )
480 playlist_item_t * temp;
482 msg_Dbg( p_playlist, "moving playlist item « %s » (%i -> %i)",
483 p_playlist->pp_items[i_pos]->psz_name, i_pos,
486 if( i_pos == p_playlist->i_index )
488 p_playlist->i_index = i_newpos;
490 else if( i_pos > p_playlist->i_index && i_newpos <= p_playlist->i_index )
492 p_playlist->i_index++;
494 else if( i_pos < p_playlist->i_index && i_newpos >= p_playlist->i_index )
496 p_playlist->i_index--;
499 if ( i_pos < i_newpos )
501 temp = p_playlist->pp_items[i_pos];
502 while ( i_pos < i_newpos )
504 p_playlist->pp_items[i_pos] = p_playlist->pp_items[i_pos+1];
507 p_playlist->pp_items[i_newpos] = temp;
509 else if ( i_pos > i_newpos )
511 temp = p_playlist->pp_items[i_pos];
512 while ( i_pos > i_newpos )
514 p_playlist->pp_items[i_pos] = p_playlist->pp_items[i_pos-1];
517 p_playlist->pp_items[i_newpos] = temp;
521 vlc_mutex_unlock( &p_playlist->object_lock );
523 val.b_bool = VLC_TRUE;
524 var_Set( p_playlist, "intf-change", val );