1 /*****************************************************************************
2 * item-ext.c : Playlist item management functions
3 *****************************************************************************
4 * Copyright (C) 1999-2004 VideoLAN
7 * Authors: Samuel Hocevar <sam@zoy.org>
8 * Clément Stenac <zorglub@videolan.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
24 #include <stdlib.h> /* free(), strtol() */
25 #include <stdio.h> /* sprintf() */
26 #include <string.h> /* strerror() */
29 #include <vlc/input.h>
31 #include "vlc_playlist.h"
33 /***************************************************************************
34 * Item creation/addition functions
35 ***************************************************************************/
38 * Add a MRL into the playlist, duration and options given
40 * \param p_playlist the playlist to add into
41 * \param psz_uri the mrl to add to the playlist
42 * \param psz_name a text giving a name or description of this item
43 * \param i_mode the mode used when adding
44 * \param i_pos the position in the playlist where to add. If this is
45 * PLAYLIST_END the item will be added at the end of the playlist
46 * regardless of it's size
47 * \param i_duration length of the item in milliseconds.
48 * \param ppsz_options an array of options
49 * \param i_options the number of options
50 * \return The id of the playlist item
52 int playlist_AddExt( playlist_t *p_playlist, const char * psz_uri,
53 const char *psz_name, int i_mode, int i_pos,
54 mtime_t i_duration, const char **ppsz_options,
57 playlist_item_t *p_item =
58 playlist_ItemNew( p_playlist , psz_uri, psz_name );
62 msg_Err( p_playlist, "unable to add item to playlist" );
66 p_item->input.i_duration = i_duration;
67 p_item->input.i_options = i_options;
68 p_item->input.ppsz_options = NULL;
70 for( p_item->input.i_options = 0; p_item->input.i_options < i_options;
71 p_item->input.i_options++ )
73 if( !p_item->input.i_options )
75 p_item->input.ppsz_options = malloc( i_options * sizeof(char *) );
76 if( !p_item->input.ppsz_options ) break;
79 p_item->input.ppsz_options[p_item->input.i_options] =
80 strdup( ppsz_options[p_item->input.i_options] );
83 return playlist_AddItem( p_playlist, p_item, i_mode, i_pos );
87 * Add a MRL into the playlist.
89 * \param p_playlist the playlist to add into
90 * \param psz_uri the mrl to add to the playlist
91 * \param psz_name a text giving a name or description of this item
92 * \param i_mode the mode used when adding
93 * \param i_pos the position in the playlist where to add. If this is
94 * PLAYLIST_END the item will be added at the end of the playlist
95 * regardless of it's size
96 * \return The id of the playlist item
98 int playlist_Add( playlist_t *p_playlist, const char *psz_uri,
99 const char *psz_name, int i_mode, int i_pos )
101 return playlist_AddExt( p_playlist, psz_uri, psz_name, i_mode, i_pos,
105 /***************************************************************************
106 * Item search functions
107 ***************************************************************************/
110 * Search the position of an item by its id
111 * This function must be entered with the playlist lock
113 * \param p_playlist the playlist
114 * \param i_id the id to find
115 * \return the position, or VLC_EGENERIC on failure
117 int playlist_GetPositionById( playlist_t * p_playlist , int i_id )
120 for( i = 0 ; i < p_playlist->i_size ; i++ )
122 if( p_playlist->pp_items[i]->i_id == i_id )
131 * Search an item by its id
133 * \param p_playlist the playlist
134 * \param i_id the id to find
135 * \return the item, or NULL on failure
137 playlist_item_t * playlist_ItemGetById( playlist_t * p_playlist , int i_id )
140 for( i = 0 ; i < p_playlist->i_size ; i++ )
142 if( p_playlist->pp_items[i]->i_id == i_id )
144 return p_playlist->pp_items[i];
151 * Search an item by its position
152 * This function must be entered with the playlist lock
154 * \param p_playlist the playlist
155 * \param i_pos the position of the item to find
156 * \return the item, or NULL on failure
158 playlist_item_t * playlist_ItemGetByPos( playlist_t * p_playlist , int i_pos )
160 if( i_pos >= 0 && i_pos < p_playlist->i_size)
162 return p_playlist->pp_items[i_pos];
164 else if( p_playlist->i_size > 0)
166 return p_playlist->pp_items[p_playlist->i_index];
174 /**********************************************************************
175 * playlist_item_t structure accessors
176 * These functions give access to the fields of the playlist_item_t
178 **********************************************************************/
181 * Set the group of a playlist item
183 * \param p_item the item
184 * \param i_group the group to set
185 * \return VLC_SUCCESS on success
187 int playlist_ItemSetGroup( playlist_item_t *p_item, int i_group)
189 p_item->i_group = i_group;
194 * Set the group of a playlist item (by position)
195 * This function must be entered with the playlist lock
196 * Legacy function due to disappear (locks the whole playlist)
198 * \param p_playlist the playlist
199 * \param i_pos the postition of the item of which we change the group
200 * \param i_group the new group
201 * \return VLC_SUCCESS on success, VLC_EGENERIC on failure
203 int playlist_SetGroup( playlist_t *p_playlist, int i_pos, int i_group )
206 playlist_item_t *p_item;
212 p_item = playlist_ItemGetByPos( p_playlist , i_pos );
218 vlc_mutex_lock( &p_item->input.lock );
219 playlist_ItemSetGroup( p_item , i_group );
220 vlc_mutex_unlock( &p_item->input.lock );
222 val.b_bool = (i_pos >= 0 && i_pos < p_playlist->i_size ) ? i_pos : -1;
223 var_Set( p_playlist, "item-change", val );
229 * Set the name of a playlist item
231 * \param p_item the item
232 * \param psz_name the new name
233 * \return VLC_SUCCESS on success, VLC_EGENERIC on failure
235 int playlist_ItemSetName( playlist_item_t *p_item, char *psz_name )
237 if( psz_name && p_item )
239 p_item->input.psz_name = strdup( psz_name );
246 * Set the name of a playlist item (by position)
247 * This function must be entered with the playlist lock
248 * Legacy function due to disappear (locks the whole playlist)
250 * \param p_playlist the playlist
251 * \param i_pos the position of the item of which we change the name
252 * \param psz_name the new name
253 * \return VLC_SUCCESS on success, VLC_EGENERIC on failure
255 int playlist_SetName( playlist_t *p_playlist, int i_pos, char *psz_name )
258 playlist_item_t *p_item;
264 p_item = playlist_ItemGetByPos( p_playlist , i_pos );
270 vlc_mutex_lock( &p_item->input.lock );
271 playlist_ItemSetName( p_item , psz_name );
272 vlc_mutex_unlock( &p_item->input.lock );
274 val.b_bool = (i_pos >= 0 && i_pos < p_playlist->i_size ) ? i_pos : -1;
275 var_Set( p_playlist, "item-change", val );
281 * Set the duration of a playlist item
282 * This function must be entered with the item lock
284 * \param p_item the item
285 * \param i_duration the new duration
286 * \return VLC_SUCCESS on success, VLC_EGENERIC on failure
288 int playlist_ItemSetDuration( playlist_item_t *p_item, mtime_t i_duration )
290 char psz_buffer[MSTRTIME_MAX_SIZE];
293 p_item->input.i_duration = i_duration;
294 if( i_duration != -1 )
296 secstotimestr( psz_buffer, i_duration/1000000 );
300 memcpy( psz_buffer, "--:--:--", sizeof("--:--:--") );
302 playlist_ItemAddInfo( p_item, _("General") , _("Duration"),
311 * Set the duration of a playlist item
312 * This function must be entered with the playlist lock
313 * Legacy function due to disappear (locks the whole playlist)
315 * \param p_playlist the playlist
316 * \param i_pos the position of the item of which we change the duration
317 * \param i_duration the duration to set
318 * \return VLC_SUCCESS on success, VLC_EGENERIC on failure
320 int playlist_SetDuration( playlist_t *p_playlist, int i_pos, mtime_t i_duration )
323 playlist_item_t *p_item;
329 p_item = playlist_ItemGetByPos( p_playlist , i_pos );
335 vlc_mutex_lock( &p_item->input.lock );
336 playlist_ItemSetDuration( p_item , i_duration );
337 vlc_mutex_unlock( &p_item->input.lock );
339 val.b_bool = (i_pos >= 0 && i_pos < p_playlist->i_size ) ? i_pos : -1;
340 var_Set( p_playlist, "item-change", val );
345 /**********************************************************************
346 * Actions on existing playlist items
347 **********************************************************************/
350 * delete an item from a playlist.
352 * \param p_playlist the playlist to remove from.
353 * \param i_pos the position of the item to remove
356 int playlist_Delete( playlist_t * p_playlist, int i_pos )
360 /* if i_pos is the current played item, playlist should stop playing it */
361 if( ( p_playlist->i_status == PLAYLIST_RUNNING) &&
362 (p_playlist->i_index == i_pos) )
364 playlist_Command( p_playlist, PLAYLIST_STOP, 0 );
367 vlc_mutex_lock( &p_playlist->object_lock );
368 if( i_pos >= 0 && i_pos < p_playlist->i_size )
370 playlist_item_t *p_item = p_playlist->pp_items[i_pos];
372 msg_Dbg( p_playlist, "deleting playlist item `%s'",
373 p_item->input.psz_name );
375 playlist_ItemDelete( p_item );
377 if( i_pos <= p_playlist->i_index )
379 p_playlist->i_index--;
382 /* Renumber the playlist */
383 REMOVE_ELEM( p_playlist->pp_items, p_playlist->i_size, i_pos );
385 if( p_playlist->i_enabled > 0 ) p_playlist->i_enabled--;
388 vlc_mutex_unlock( &p_playlist->object_lock );
390 val.b_bool = VLC_TRUE;
391 var_Set( p_playlist, "intf-change", val );
397 * Clear all playlist items
399 * \param p_playlist the playlist to be cleared.
402 int playlist_Clear( playlist_t * p_playlist )
405 while( p_playlist->i_groups > 0 )
407 playlist_DeleteGroup( p_playlist, p_playlist->pp_groups[0]->i_id );
410 while( p_playlist->i_size > 0 )
412 playlist_Delete( p_playlist, 0 );
415 p_playlist->i_index = -1;
416 p_playlist->i_size = 0;
417 p_playlist->pp_items = NULL;
419 p_playlist->i_groups = 0;
420 p_playlist->pp_groups = NULL;
426 * Disables a playlist item
428 * \param p_playlist the playlist to disable from.
429 * \param i_pos the position of the item to disable
432 int playlist_Disable( playlist_t * p_playlist, int i_pos )
435 vlc_mutex_lock( &p_playlist->object_lock );
438 if( i_pos >= 0 && i_pos < p_playlist->i_size )
440 msg_Dbg( p_playlist, "disabling playlist item `%s'",
441 p_playlist->pp_items[i_pos]->input.psz_name );
443 if( p_playlist->pp_items[i_pos]->b_enabled == VLC_TRUE )
444 p_playlist->i_enabled--;
445 p_playlist->pp_items[i_pos]->b_enabled = VLC_FALSE;
448 vlc_mutex_unlock( &p_playlist->object_lock );
451 var_Set( p_playlist, "item-change", val );
457 * Enables a playlist item
459 * \param p_playlist the playlist to enable from.
460 * \param i_pos the position of the item to enable
463 int playlist_Enable( playlist_t * p_playlist, int i_pos )
466 vlc_mutex_lock( &p_playlist->object_lock );
468 if( i_pos >= 0 && i_pos < p_playlist->i_size )
470 msg_Dbg( p_playlist, "enabling playlist item `%s'",
471 p_playlist->pp_items[i_pos]->input.psz_name );
473 if( p_playlist->pp_items[i_pos]->b_enabled == VLC_FALSE )
474 p_playlist->i_enabled++;
476 p_playlist->pp_items[i_pos]->b_enabled = VLC_TRUE;
479 vlc_mutex_unlock( &p_playlist->object_lock );
482 var_Set( p_playlist, "item-change", val );
488 * Disables a playlist group
490 * \param p_playlist the playlist to disable from.
491 * \param i_group the id of the group to disable
494 int playlist_DisableGroup( playlist_t * p_playlist, int i_group )
498 vlc_mutex_lock( &p_playlist->object_lock );
500 msg_Dbg( p_playlist, "disabling group %i", i_group );
501 for( i = 0 ; i< p_playlist->i_size; i++ )
503 if( p_playlist->pp_items[i]->i_group == i_group )
505 msg_Dbg( p_playlist, "disabling playlist item `%s'",
506 p_playlist->pp_items[i]->input.psz_name );
508 if( p_playlist->pp_items[i]->b_enabled == VLC_TRUE )
509 p_playlist->i_enabled--;
511 p_playlist->pp_items[i]->b_enabled = VLC_FALSE;
513 var_Set( p_playlist, "item-change", val );
516 vlc_mutex_unlock( &p_playlist->object_lock );
522 * Enables a playlist group
524 * \param p_playlist the playlist to enable from.
525 * \param i_group the id of the group to enable
528 int playlist_EnableGroup( playlist_t * p_playlist, int i_group )
532 vlc_mutex_lock( &p_playlist->object_lock );
534 for( i = 0; i< p_playlist->i_size; i++ )
536 if( p_playlist->pp_items[i]->i_group == i_group )
538 msg_Dbg( p_playlist, "enabling playlist item `%s'",
539 p_playlist->pp_items[i]->input.psz_name );
541 if( p_playlist->pp_items[i]->b_enabled == VLC_FALSE )
542 p_playlist->i_enabled++;
544 p_playlist->pp_items[i]->b_enabled = VLC_TRUE;
546 var_Set( p_playlist, "item-change", val );
549 vlc_mutex_unlock( &p_playlist->object_lock );
555 * Move an item in a playlist
557 * Move the item in the playlist with position i_pos before the current item
558 * at position i_newpos.
559 * \param p_playlist the playlist to move items in
560 * \param i_pos the position of the item to move
561 * \param i_newpos the position of the item that will be behind the moved item
563 * \return returns VLC_SUCCESS
565 int playlist_Move( playlist_t * p_playlist, int i_pos, int i_newpos )
568 vlc_mutex_lock( &p_playlist->object_lock );
570 /* take into account that our own row disappears. */
571 if( i_pos < i_newpos ) i_newpos--;
573 if( i_pos >= 0 && i_newpos >=0 && i_pos <= p_playlist->i_size &&
574 i_newpos <= p_playlist->i_size )
576 playlist_item_t * temp;
578 msg_Dbg( p_playlist, "moving playlist item `%s' (%i -> %i)",
579 p_playlist->pp_items[i_pos]->input.psz_name, i_pos, i_newpos);
581 if( i_pos == p_playlist->i_index )
583 p_playlist->i_index = i_newpos;
585 else if( i_pos > p_playlist->i_index && i_newpos <= p_playlist->i_index )
587 p_playlist->i_index++;
589 else if( i_pos < p_playlist->i_index && i_newpos >= p_playlist->i_index )
591 p_playlist->i_index--;
594 if ( i_pos < i_newpos )
596 temp = p_playlist->pp_items[i_pos];
597 while ( i_pos < i_newpos )
599 p_playlist->pp_items[i_pos] = p_playlist->pp_items[i_pos+1];
602 p_playlist->pp_items[i_newpos] = temp;
604 else if ( i_pos > i_newpos )
606 temp = p_playlist->pp_items[i_pos];
607 while ( i_pos > i_newpos )
609 p_playlist->pp_items[i_pos] = p_playlist->pp_items[i_pos-1];
612 p_playlist->pp_items[i_newpos] = temp;
616 vlc_mutex_unlock( &p_playlist->object_lock );
618 val.b_bool = VLC_TRUE;
619 var_Set( p_playlist, "intf-change", val );