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 );
64 msg_Err( p_playlist, "unable to add item to playlist" );
68 p_item->input.i_duration = i_duration;
70 p_item->input.i_options = i_options;
71 p_item->input.ppsz_options = NULL;
72 for( i = 0; i < i_options; i++ )
76 p_item->input.ppsz_options = malloc( i_options * sizeof(char *) );
77 if( !p_item->input.ppsz_options )
79 p_item->input.i_options = 0;
84 p_item->input.ppsz_options[i] = strdup( ppsz_options[i] );
87 return playlist_AddItem( p_playlist, p_item, i_mode, i_pos );
91 * Add a MRL into the playlist.
93 * \param p_playlist the playlist to add into
94 * \param psz_uri the mrl to add to the playlist
95 * \param psz_name a text giving a name or description of this item
96 * \param i_mode the mode used when adding
97 * \param i_pos the position in the playlist where to add. If this is
98 * PLAYLIST_END the item will be added at the end of the playlist
99 * regardless of it's size
100 * \return The id of the playlist item
102 int playlist_Add( playlist_t *p_playlist, const char *psz_uri,
103 const char *psz_name, int i_mode, int i_pos )
105 return playlist_AddExt( p_playlist, psz_uri, psz_name, i_mode, i_pos,
109 /***************************************************************************
110 * Item search functions
111 ***************************************************************************/
114 * Search the position of an item by its id
115 * This function must be entered with the playlist lock
117 * \param p_playlist the playlist
118 * \param i_id the id to find
119 * \return the position, or VLC_EGENERIC on failure
121 int playlist_GetPositionById( playlist_t * p_playlist , int i_id )
124 for( i = 0 ; i < p_playlist->i_size ; i++ )
126 if( p_playlist->pp_items[i]->i_id == i_id )
135 * Search an item by its id
137 * \param p_playlist the playlist
138 * \param i_id the id to find
139 * \return the item, or NULL on failure
141 playlist_item_t * playlist_ItemGetById( playlist_t * p_playlist , int i_id )
144 for( i = 0 ; i < p_playlist->i_size ; i++ )
146 if( p_playlist->pp_items[i]->i_id == i_id )
148 return p_playlist->pp_items[i];
155 * Search an item by its position
156 * This function must be entered with the playlist lock
158 * \param p_playlist the playlist
159 * \param i_id the id to find
160 * \return the item, or NULL on failure
162 playlist_item_t * playlist_ItemGetByPos( playlist_t * p_playlist , int i_pos )
164 if( i_pos >= 0 && i_pos < p_playlist->i_size)
166 return p_playlist->pp_items[i_pos];
168 else if( p_playlist->i_size > 0)
170 return p_playlist->pp_items[p_playlist->i_index];
178 /**********************************************************************
179 * playlist_item_t structure accessors
180 * These functions give access to the fields of the playlist_item_t
182 **********************************************************************/
185 * Set the group of a playlist item
187 * \param p_item the item
188 * \return VLC_SUCCESS on success
190 int playlist_ItemSetGroup( playlist_item_t *p_item, int i_group)
192 p_item->i_group = i_group;
197 * Set the group of a playlist item (by position)
198 * This function must be entered with the playlist lock
199 * Legacy function due to disappear (locks the whole playlist)
201 * \param p_playlist the playlist
202 * \param i_pos the postition of the item of which we change the group
203 * \param i_group the new group
204 * \return VLC_SUCCESS on success, VLC_EGENERIC on failure
206 int playlist_SetGroup( playlist_t *p_playlist, int i_pos, int i_group )
209 playlist_item_t *p_item;
215 p_item = playlist_ItemGetByPos( p_playlist , i_pos );
221 vlc_mutex_lock( &p_item->input.lock );
222 playlist_ItemSetGroup( p_item , i_group );
223 vlc_mutex_unlock( &p_item->input.lock );
225 val.b_bool = (i_pos >= 0 && i_pos < p_playlist->i_size ) ? i_pos : -1;
226 var_Set( p_playlist, "item-change", val );
232 * Set the name of a playlist item
234 * \param p_item the item
235 * \param psz_name the new name
236 * \return VLC_SUCCESS on success, VLC_EGENERIC on failure
238 int playlist_ItemSetName( playlist_item_t *p_item, char *psz_name )
240 if( psz_name && p_item )
242 p_item->input.psz_name = strdup( psz_name );
249 * Set the name of a playlist item (by position)
250 * This function must be entered with the playlist lock
251 * Legacy function due to disappear (locks the whole playlist)
253 * \param p_playlist the playlist
254 * \param i_pos the position of the item of which we change the name
255 * \param psz_name the new name
256 * \return VLC_SUCCESS on success, VLC_EGENERIC on failure
258 int playlist_SetName( playlist_t *p_playlist, int i_pos, char *psz_name )
261 playlist_item_t *p_item;
267 p_item = playlist_ItemGetByPos( p_playlist , i_pos );
273 vlc_mutex_lock( &p_item->input.lock );
274 playlist_ItemSetName( p_item , psz_name );
275 vlc_mutex_unlock( &p_item->input.lock );
277 val.b_bool = (i_pos >= 0 && i_pos < p_playlist->i_size ) ? i_pos : -1;
278 var_Set( p_playlist, "item-change", val );
284 * Set the duration of a playlist item
285 * This function must be entered with the item lock
287 * \param p_item the item
288 * \param psz_name the new name
289 * \return VLC_SUCCESS on success, VLC_EGENERIC on failure
291 int playlist_ItemSetDuration( playlist_item_t *p_item, mtime_t i_duration )
293 char psz_buffer[MSTRTIME_MAX_SIZE];
296 p_item->input.i_duration = i_duration;
297 if( i_duration != -1 )
299 secstotimestr( psz_buffer, i_duration/1000000 );
303 memcpy( psz_buffer, "--:--:--", sizeof("--:--:--") );
305 playlist_ItemAddInfo( p_item, _("General") , _("Duration"),
314 * Set the duration of a playlist item
315 * This function must be entered with the playlist lock
316 * Legacy function due to disappear (locks the whole playlist)
318 * \param p_playlist the playlist
319 * \param i_pos the position of the item of which we change the duration
320 * \param i_duration the duration to set
321 * \return VLC_SUCCESS on success, VLC_EGENERIC on failure
323 int playlist_SetDuration( playlist_t *p_playlist, int i_pos, mtime_t i_duration )
326 playlist_item_t *p_item;
332 p_item = playlist_ItemGetByPos( p_playlist , i_pos );
338 vlc_mutex_lock( &p_item->input.lock );
339 playlist_ItemSetDuration( p_item , i_duration );
340 vlc_mutex_unlock( &p_item->input.lock );
342 val.b_bool = (i_pos >= 0 && i_pos < p_playlist->i_size ) ? i_pos : -1;
343 var_Set( p_playlist, "item-change", val );
348 /**********************************************************************
349 * Actions on existing playlist items
350 **********************************************************************/
353 * delete an item from a playlist.
355 * \param p_playlist the playlist to remove from.
356 * \param i_pos the position of the item to remove
359 int playlist_Delete( playlist_t * p_playlist, int i_pos )
363 /* if i_pos is the current played item, playlist should stop playing it */
364 if( ( p_playlist->i_status == PLAYLIST_RUNNING) &&
365 (p_playlist->i_index == i_pos) )
367 playlist_Command( p_playlist, PLAYLIST_STOP, 0 );
370 vlc_mutex_lock( &p_playlist->object_lock );
371 if( i_pos >= 0 && i_pos < p_playlist->i_size )
373 playlist_item_t *p_item = p_playlist->pp_items[i_pos];
375 msg_Dbg( p_playlist, "deleting playlist item `%s'",
376 p_item->input.psz_name );
380 vlc_mutex_lock( &p_item->lock );
382 if( p_item->psz_name )
384 free( p_item->psz_name );
386 if( p_item->psz_uri )
388 free( p_item->psz_uri );
391 /* Free the info categories. Welcome to the segfault factory */
392 if( p_item->i_categories > 0 )
394 for( i = 0; i < p_item->i_categories; i++ )
396 for( j= 0 ; j < p_item->pp_categories[i]->i_infos; j++)
398 if( p_item->pp_categories[i]->pp_infos[j]->psz_name)
400 free( p_item->pp_categories[i]->
401 pp_infos[j]->psz_name);
403 if( p_item->pp_categories[i]->pp_infos[j]->psz_value)
405 free( p_item->pp_categories[i]->
406 pp_infos[j]->psz_value);
408 free( p_item->pp_categories[i]->pp_infos[j] );
410 if( p_item->pp_categories[i]->i_infos )
411 free( p_item->pp_categories[i]->pp_infos );
413 if( p_item->pp_categories[i]->psz_name)
415 free( p_item->pp_categories[i]->psz_name );
417 free( p_item->pp_categories[i] );
419 free( p_item->pp_categories );
422 /* XXX: what if the item is still in use? */
424 playlist_ItemDelete( p_item );
428 if( i_pos <= p_playlist->i_index )
430 p_playlist->i_index--;
433 /* Renumber the playlist */
434 REMOVE_ELEM( p_playlist->pp_items, p_playlist->i_size, i_pos );
436 if( p_playlist->i_enabled > 0 ) p_playlist->i_enabled--;
439 vlc_mutex_unlock( &p_playlist->object_lock );
441 val.b_bool = VLC_TRUE;
442 var_Set( p_playlist, "intf-change", val );
448 * Clear all playlist items
450 * \param p_playlist the playlist to be cleared.
453 int playlist_Clear( playlist_t * p_playlist )
456 while( p_playlist->i_groups > 0 )
458 playlist_DeleteGroup( p_playlist, p_playlist->pp_groups[0]->i_id );
461 while( p_playlist->i_size > 0 )
463 playlist_Delete( p_playlist, 0 );
466 p_playlist->i_index = -1;
467 p_playlist->i_size = 0;
468 p_playlist->pp_items = NULL;
470 p_playlist->i_groups = 0;
471 p_playlist->pp_groups = NULL;
477 * Disables a playlist item
479 * \param p_playlist the playlist to disable from.
480 * \param i_pos the position of the item to disable
483 int playlist_Disable( playlist_t * p_playlist, int i_pos )
486 vlc_mutex_lock( &p_playlist->object_lock );
489 if( i_pos >= 0 && i_pos < p_playlist->i_size )
491 msg_Dbg( p_playlist, "disabling playlist item `%s'",
492 p_playlist->pp_items[i_pos]->input.psz_name );
494 if( p_playlist->pp_items[i_pos]->b_enabled == VLC_TRUE )
495 p_playlist->i_enabled--;
496 p_playlist->pp_items[i_pos]->b_enabled = VLC_FALSE;
499 vlc_mutex_unlock( &p_playlist->object_lock );
502 var_Set( p_playlist, "item-change", val );
508 * Enables a playlist item
510 * \param p_playlist the playlist to enable from.
511 * \param i_pos the position of the item to enable
514 int playlist_Enable( playlist_t * p_playlist, int i_pos )
517 vlc_mutex_lock( &p_playlist->object_lock );
519 if( i_pos >= 0 && i_pos < p_playlist->i_size )
521 msg_Dbg( p_playlist, "enabling playlist item `%s'",
522 p_playlist->pp_items[i_pos]->input.psz_name );
524 if( p_playlist->pp_items[i_pos]->b_enabled == VLC_FALSE )
525 p_playlist->i_enabled++;
527 p_playlist->pp_items[i_pos]->b_enabled = VLC_TRUE;
530 vlc_mutex_unlock( &p_playlist->object_lock );
533 var_Set( p_playlist, "item-change", val );
539 * Disables a playlist group
541 * \param p_playlist the playlist to disable from.
542 * \param i_group the id of the group to disable
545 int playlist_DisableGroup( playlist_t * p_playlist, int i_group )
549 vlc_mutex_lock( &p_playlist->object_lock );
551 msg_Dbg( p_playlist, "disabling group %i", i_group );
552 for( i = 0 ; i< p_playlist->i_size; i++ )
554 if( p_playlist->pp_items[i]->i_group == i_group )
556 msg_Dbg( p_playlist, "disabling playlist item `%s'",
557 p_playlist->pp_items[i]->input.psz_name );
559 if( p_playlist->pp_items[i]->b_enabled == VLC_TRUE )
560 p_playlist->i_enabled--;
562 p_playlist->pp_items[i]->b_enabled = VLC_FALSE;
564 var_Set( p_playlist, "item-change", val );
567 vlc_mutex_unlock( &p_playlist->object_lock );
573 * Enables a playlist group
575 * \param p_playlist the playlist to enable from.
576 * \param i_group the id of the group to enable
579 int playlist_EnableGroup( playlist_t * p_playlist, int i_group )
583 vlc_mutex_lock( &p_playlist->object_lock );
585 for( i = 0; i< p_playlist->i_size; i++ )
587 if( p_playlist->pp_items[i]->i_group == i_group )
589 msg_Dbg( p_playlist, "enabling playlist item `%s'",
590 p_playlist->pp_items[i]->input.psz_name );
592 if( p_playlist->pp_items[i]->b_enabled == VLC_FALSE )
593 p_playlist->i_enabled++;
595 p_playlist->pp_items[i]->b_enabled = VLC_TRUE;
597 var_Set( p_playlist, "item-change", val );
600 vlc_mutex_unlock( &p_playlist->object_lock );
606 * Move an item in a playlist
608 * Move the item in the playlist with position i_pos before the current item
609 * at position i_newpos.
610 * \param p_playlist the playlist to move items in
611 * \param i_pos the position of the item to move
612 * \param i_newpos the position of the item that will be behind the moved item
614 * \return returns VLC_SUCCESS
616 int playlist_Move( playlist_t * p_playlist, int i_pos, int i_newpos )
619 vlc_mutex_lock( &p_playlist->object_lock );
621 /* take into account that our own row disappears. */
622 if( i_pos < i_newpos ) i_newpos--;
624 if( i_pos >= 0 && i_newpos >=0 && i_pos <= p_playlist->i_size &&
625 i_newpos <= p_playlist->i_size )
627 playlist_item_t * temp;
629 msg_Dbg( p_playlist, "moving playlist item `%s' (%i -> %i)",
630 p_playlist->pp_items[i_pos]->input.psz_name, i_pos, i_newpos);
632 if( i_pos == p_playlist->i_index )
634 p_playlist->i_index = i_newpos;
636 else if( i_pos > p_playlist->i_index && i_newpos <= p_playlist->i_index )
638 p_playlist->i_index++;
640 else if( i_pos < p_playlist->i_index && i_newpos >= p_playlist->i_index )
642 p_playlist->i_index--;
645 if ( i_pos < i_newpos )
647 temp = p_playlist->pp_items[i_pos];
648 while ( i_pos < i_newpos )
650 p_playlist->pp_items[i_pos] = p_playlist->pp_items[i_pos+1];
653 p_playlist->pp_items[i_newpos] = temp;
655 else if ( i_pos > i_newpos )
657 temp = p_playlist->pp_items[i_pos];
658 while ( i_pos > i_newpos )
660 p_playlist->pp_items[i_pos] = p_playlist->pp_items[i_pos-1];
663 p_playlist->pp_items[i_newpos] = temp;
667 vlc_mutex_unlock( &p_playlist->object_lock );
669 val.b_bool = VLC_TRUE;
670 var_Set( p_playlist, "intf-change", val );