1 /*****************************************************************************
2 * item-ext.c : Playlist item management functions (act on the playlist)
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 p_item = 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,
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 The id of the playlist item
116 int playlist_AddItem( playlist_t *p_playlist, playlist_item_t *p_item,
117 int i_mode, int i_pos)
120 vlc_bool_t b_end = VLC_FALSE;
121 playlist_view_t *p_view = NULL;
123 playlist_add_t *p_add = (playlist_add_t *)malloc(sizeof( playlist_add_t));
125 vlc_mutex_lock( &p_playlist->object_lock );
128 * CHECK_INSERT : checks if the item is already enqued before
132 /* That should not change */
133 if ( i_mode & PLAYLIST_CHECK_INSERT )
137 if ( p_playlist->pp_items )
139 for ( j = 0; j < p_playlist->i_size; j++ )
141 if ( !strcmp( p_playlist->pp_items[j]->input.psz_uri,
142 p_item->input.psz_uri ) )
144 playlist_ItemDelete( p_item );
145 vlc_mutex_unlock( &p_playlist->object_lock );
150 i_mode &= ~PLAYLIST_CHECK_INSERT;
151 i_mode |= PLAYLIST_APPEND;
154 msg_Dbg( p_playlist, "adding playlist item `%s' ( %s )",
155 p_item->input.psz_name, p_item->input.psz_uri );
157 p_item->input.i_id = ++p_playlist->i_last_id;
159 /* Do a few boundary checks and allocate space for the item */
160 if( i_pos == PLAYLIST_END )
163 if( i_mode & PLAYLIST_INSERT )
165 i_mode &= ~PLAYLIST_INSERT;
166 i_mode |= PLAYLIST_APPEND;
169 i_pos = p_playlist->i_size - 1;
172 if( !(i_mode & PLAYLIST_REPLACE)
173 || i_pos < 0 || i_pos >= p_playlist->i_size )
175 /* Additional boundary checks */
176 if( i_mode & PLAYLIST_APPEND )
185 else if( i_pos > p_playlist->i_size )
187 i_pos = p_playlist->i_size;
190 INSERT_ELEM( p_playlist->pp_items, p_playlist->i_size, i_pos, p_item );
191 INSERT_ELEM( p_playlist->pp_all_items, p_playlist->i_all_size,
192 p_playlist->i_all_size, p_item );
193 p_playlist->i_enabled ++;
195 /* We update the ALL view directly */
196 playlist_ViewUpdate( p_playlist, VIEW_ALL );
198 /* Add the item to the General category */
199 if( b_end == VLC_TRUE )
201 playlist_NodeAppend( p_playlist, VIEW_CATEGORY, p_item,
202 p_playlist->p_general );
203 p_add->i_item = p_item->input.i_id;
204 p_add->i_node = p_playlist->p_general->input.i_id;
205 p_add->i_view = VIEW_CATEGORY;
206 val.p_address = p_add;
207 var_Set( p_playlist, "item-append", val );
211 playlist_NodeInsert( p_playlist, VIEW_CATEGORY, p_item,
212 p_playlist->p_general, i_pos );
216 p_view = playlist_ViewFind( p_playlist, VIEW_ALL );
217 playlist_ItemAddParent( p_item, VIEW_ALL, p_view->p_root );
219 /* FIXME : Update sorted views */
221 if( p_playlist->i_index >= i_pos )
223 p_playlist->i_index++;
228 msg_Err( p_playlist, "Insert mode not implemented" );
231 if( (i_mode & PLAYLIST_GO ) && p_view )
233 p_playlist->request.b_request = VLC_TRUE;
235 p_playlist->request.i_view = VIEW_CATEGORY;
236 p_playlist->request.p_node = p_view->p_root;
237 p_playlist->request.p_item = p_item;
239 if( p_playlist->p_input )
241 input_StopThread( p_playlist->p_input );
243 p_playlist->status.i_status = PLAYLIST_RUNNING;
246 vlc_mutex_unlock( &p_playlist->object_lock );
248 if( b_end == VLC_FALSE )
250 val.b_bool = VLC_TRUE;
251 var_Set( p_playlist, "intf-change", val );
256 return p_item->input.i_id;
261 * Add a playlist item to a given node (in the category view )
263 * \param p_playlist the playlist to insert into
264 * \param p_item the playlist item to insert
265 * \param i_view the view for which to add or TODO: ALL_VIEWS
266 * \param p_parent the parent node
267 * \param i_mode the mode used when adding
268 * \param i_pos the possition in the node where to add. If this is
269 * PLAYLIST_END the item will be added at the end of the node
270 ** \return The id of the playlist item
272 int playlist_NodeAddItem( playlist_t *p_playlist, playlist_item_t *p_item,
273 int i_view,playlist_item_t *p_parent,
274 int i_mode, int i_pos)
278 playlist_view_t *p_view;
280 playlist_add_t *p_add = (playlist_add_t *)malloc(sizeof( playlist_add_t));
282 vlc_mutex_lock( &p_playlist->object_lock );
285 if( !p_parent || p_parent->i_children == -1 )
287 msg_Err( p_playlist, "invalid node" );
291 * CHECK_INSERT : checks if the item is already enqued before
294 if ( i_mode & PLAYLIST_CHECK_INSERT )
298 if ( p_playlist->pp_items )
300 for ( j = 0; j < p_playlist->i_size; j++ )
302 if ( !strcmp( p_playlist->pp_items[j]->input.psz_uri,
303 p_item->input.psz_uri ) )
305 playlist_ItemDelete( p_item );
306 vlc_mutex_unlock( &p_playlist->object_lock );
312 i_mode &= ~PLAYLIST_CHECK_INSERT;
313 i_mode |= PLAYLIST_APPEND;
316 msg_Dbg( p_playlist, "adding playlist item `%s' ( %s )",
317 p_item->input.psz_name, p_item->input.psz_uri );
319 p_item->input.i_id = ++p_playlist->i_last_id;
321 /* First, add the item at the right position in the item bank */
323 //i_position = p_playlist->i_index == -1 ? 0 : p_playlist->i_index;
324 i_position = p_playlist->i_size ;
326 INSERT_ELEM( p_playlist->pp_items,
330 INSERT_ELEM( p_playlist->pp_all_items,
331 p_playlist->i_all_size,
332 p_playlist->i_all_size,
334 p_playlist->i_enabled ++;
336 /* TODO: Handle modes */
337 playlist_NodeAppend( p_playlist, i_view, p_item, p_parent );
339 p_add->i_item = p_item->input.i_id;
340 p_add->i_node = p_parent->input.i_id;
341 p_add->i_view = i_view;
342 val.p_address = p_add;
343 var_Set( p_playlist, "item-append", val );
345 /* We update the ALL view directly */
346 p_view = playlist_ViewFind( p_playlist, VIEW_ALL );
347 playlist_ItemAddParent( p_item, VIEW_ALL, p_view->p_root );
348 playlist_ViewUpdate( p_playlist, VIEW_ALL );
350 /* TODO : Update sorted views*/
352 if( i_mode & PLAYLIST_GO )
354 p_playlist->request.b_request = VLC_TRUE;
355 p_playlist->request.i_view = VIEW_CATEGORY;
356 p_playlist->request.p_node = p_parent;
357 p_playlist->request.p_item = p_item;
358 if( p_playlist->p_input )
360 input_StopThread( p_playlist->p_input );
362 p_playlist->status.i_status = PLAYLIST_RUNNING;
365 vlc_mutex_unlock( &p_playlist->object_lock );
367 val.b_bool = VLC_TRUE;
368 // var_Set( p_playlist, "intf-change", val );
372 return p_item->input.i_id;
375 /***************************************************************************
376 * Item search functions
377 ***************************************************************************/
380 * Search the position of an item by its id
381 * This function must be entered with the playlist lock
383 * \param p_playlist the playlist
384 * \param i_id the id to find
385 * \return the position, or VLC_EGENERIC on failure
387 int playlist_GetPositionById( playlist_t * p_playlist , int i_id )
390 for( i = 0 ; i < p_playlist->i_size ; i++ )
392 if( p_playlist->pp_items[i]->input.i_id == i_id )
402 * Search an item by its position
403 * This function must be entered with the playlist lock
405 * \param p_playlist the playlist
406 * \param i_pos the position of the item to find
407 * \return the item, or NULL on failure
409 playlist_item_t * playlist_ItemGetByPos( playlist_t * p_playlist , int i_pos )
411 if( i_pos >= 0 && i_pos < p_playlist->i_size)
413 return p_playlist->pp_items[i_pos];
415 else if( p_playlist->i_size > 0)
417 return p_playlist->pp_items[p_playlist->i_index];
425 playlist_item_t *playlist_LockItemGetByPos( playlist_t *p_playlist, int i_pos )
427 playlist_item_t *p_ret;
428 vlc_mutex_lock( &p_playlist->object_lock );
429 p_ret = playlist_ItemGetByPos( p_playlist, i_pos );
430 vlc_mutex_unlock( &p_playlist->object_lock );
435 * Search an item by its id
437 * \param p_playlist the playlist
438 * \param i_id the id to find
439 * \return the item, or NULL on failure
441 playlist_item_t * playlist_ItemGetById( playlist_t * p_playlist , int i_id )
443 int i, i_top, i_bottom;
444 i_bottom = 0; i_top = p_playlist->i_all_size - 1;
446 while( p_playlist->pp_all_items[i]->input.i_id != i_id &&
449 if( p_playlist->pp_all_items[i]->input.i_id < i_id )
457 i = i_bottom + ( i_top - i_bottom ) / 2;
459 if( p_playlist->pp_all_items[i]->input.i_id == i_id )
461 return p_playlist->pp_all_items[i];
466 playlist_item_t *playlist_LockItemGetById( playlist_t *p_playlist, int i_id)
468 playlist_item_t *p_ret;
469 vlc_mutex_lock( &p_playlist->object_lock );
470 p_ret = playlist_ItemGetById( p_playlist, i_id );
471 vlc_mutex_unlock( &p_playlist->object_lock );
476 * Search an item by its input_item_t
478 * \param p_playlist the playlist
479 * \param p_item the input_item_t to find
480 * \return the item, or NULL on failure
482 playlist_item_t * playlist_ItemGetByInput( playlist_t * p_playlist ,
483 input_item_t *p_item )
486 if( &p_playlist->status.p_item->input == p_item )
488 return p_playlist->status.p_item;
491 for( i = 0 ; i < p_playlist->i_size ; i++ )
493 if( &p_playlist->pp_items[i]->input == p_item )
495 return p_playlist->pp_items[i];
501 playlist_item_t *playlist_LockItemGetByInput( playlist_t *p_playlist,
502 input_item_t *p_item )
504 playlist_item_t *p_ret;
505 vlc_mutex_lock( &p_playlist->object_lock );
506 p_ret = playlist_ItemGetByInput( p_playlist, p_item );
507 vlc_mutex_unlock( &p_playlist->object_lock );
512 /***********************************************************************
514 ***********************************************************************/
517 * Transform an item to a node
519 * This function must be entered without the playlist lock
521 * \param p_playlist the playlist object
522 * \param p_item the item to transform
525 int playlist_ItemToNode( playlist_t *p_playlist,playlist_item_t *p_item )
528 if( p_item->i_children == -1 )
530 p_item->i_children = 0;
533 /* Remove it from the array of available items */
534 for( i = 0 ; i < p_playlist->i_size ; i++ )
536 if( p_item == p_playlist->pp_items[i] )
538 REMOVE_ELEM( p_playlist->pp_items, p_playlist->i_size, i );
541 var_SetInteger( p_playlist, "item-change", p_item->input.i_id );
546 int playlist_LockItemToNode( playlist_t *p_playlist, playlist_item_t *p_item )
549 vlc_mutex_lock( &p_playlist->object_lock );
550 i_ret = playlist_ItemToNode( p_playlist, p_item );
551 vlc_mutex_unlock( &p_playlist->object_lock );
556 * Replaces an item with another one
557 * This function must be entered without the playlist lock
559 * \see playlist_Replace
561 int playlist_LockReplace( playlist_t *p_playlist,
562 playlist_item_t *p_olditem,
563 input_item_t *p_new )
566 vlc_mutex_lock( &p_playlist->object_lock );
567 i_ret = playlist_Replace( p_playlist, p_olditem, p_new );
568 vlc_mutex_unlock( &p_playlist->object_lock );
573 * Replaces an item with another one
574 * This function must be entered with the playlist lock:
576 * \param p_playlist the playlist
577 * \param p_olditem the item to replace
578 * \param p_new the new input_item
579 * \return VLC_SUCCESS or an error
581 int playlist_Replace( playlist_t *p_playlist, playlist_item_t *p_olditem,
582 input_item_t *p_new )
587 if( p_olditem->i_children != -1 )
589 msg_Err( p_playlist, "playlist_Replace can only be used on leafs");
593 p_olditem->i_nb_played = 0;
594 memcpy( &p_olditem->input, p_new, sizeof( input_item_t ) );
596 p_olditem->i_nb_played = 0;
598 for( i = 0 ; i< p_olditem->i_parents ; i++ )
600 playlist_item_t *p_parent = p_olditem->pp_parents[i]->p_parent;
602 for( j = 0 ; j< p_parent->i_children ; i++ )
604 if( p_parent->pp_children[j] == p_olditem )
606 p_parent->i_serial++;
614 * Deletes an item from a playlist.
616 * This function must be entered without the playlist lock
618 * \param p_playlist the playlist to remove from.
619 * \param i_id the identifier of the item to delete
620 * \return returns VLC_SUCCESS or an error
622 int playlist_Delete( playlist_t * p_playlist, int i_id )
624 int i, i_top, i_bottom;
625 vlc_bool_t b_flag = VLC_FALSE;
627 playlist_item_t *p_item = playlist_ItemGetById( p_playlist, i_id );
633 var_SetInteger( p_playlist, "item-deleted", i_id );
635 i_bottom = 0; i_top = p_playlist->i_all_size - 1;
637 while( p_playlist->pp_all_items[i]->input.i_id != i_id &&
640 if( p_playlist->pp_all_items[i]->input.i_id < i_id )
648 i = i_bottom + ( i_top - i_bottom ) / 2;
650 if( p_playlist->pp_all_items[i]->input.i_id == i_id )
652 REMOVE_ELEM( p_playlist->pp_all_items, p_playlist->i_all_size, i );
655 /* Check if it is the current item */
656 if( p_playlist->status.p_item == p_item )
658 /* Hack we don't call playlist_Control for lock reasons */
659 p_playlist->status.i_status = PLAYLIST_STOPPED;
660 p_playlist->request.b_request = VLC_TRUE;
661 // p_playlist->status.p_item = NULL;
662 msg_Info( p_playlist, "stopping playback" );
666 msg_Dbg( p_playlist, "deleting playlist item `%s'",
667 p_item->input.psz_name );
669 /* Remove the item from all its parent nodes */
670 for ( i= 0 ; i < p_item->i_parents ; i++ )
672 playlist_NodeRemoveItem( p_playlist, p_item,
673 p_item->pp_parents[i]->p_parent );
674 if( p_item->pp_parents[i]->i_view == VIEW_ALL )
676 p_playlist->i_size--;
680 /* TODO : Update views */
682 if( b_flag == VLC_FALSE )
683 playlist_ItemDelete( p_item );
685 p_item->i_flags |= PLAYLIST_REMOVE_FLAG;
690 int playlist_LockDelete( playlist_t * p_playlist, int i_id )
693 vlc_mutex_lock( &p_playlist->object_lock );
694 i_ret = playlist_Delete( p_playlist, i_id );
695 vlc_mutex_unlock( &p_playlist->object_lock );
700 * Clear all playlist items
702 * \param p_playlist the playlist to be cleared.
705 int playlist_Clear( playlist_t * p_playlist )
708 for( i = p_playlist->i_size; i > 0 ; i-- )
710 playlist_Delete( p_playlist, p_playlist->pp_items[0]->input.i_id );
712 for( i = 0 ; i< p_playlist->i_views; i++ )
714 playlist_ViewEmpty( p_playlist, i, VLC_TRUE );
719 int playlist_LockClear( playlist_t *p_playlist )
722 vlc_mutex_lock( &p_playlist->object_lock );
723 i_ret = playlist_Clear( p_playlist );
724 vlc_mutex_unlock( &p_playlist->object_lock );
730 * Disables a playlist item
732 * \param p_playlist the playlist to disable from.
733 * \param i_pos the position of the item to disable
736 int playlist_Disable( playlist_t * p_playlist, playlist_item_t *p_item )
738 if( !p_item ) return VLC_EGENERIC;
740 msg_Dbg( p_playlist, "disabling playlist item `%s'",
741 p_item->input.psz_name );
743 if( p_item->i_flags & PLAYLIST_ENA_FLAG )
745 p_playlist->i_enabled--;
747 p_item->i_flags &= ~PLAYLIST_ENA_FLAG;
749 var_SetInteger( p_playlist, "item-change", p_item->input.i_id );
754 * Enables a playlist item
756 * \param p_playlist the playlist to enable from.
757 * \param i_pos the position of the item to enable
760 int playlist_Enable( playlist_t * p_playlist, playlist_item_t *p_item )
762 if( !p_item ) return VLC_EGENERIC;
764 msg_Dbg( p_playlist, "enabling playlist item `%s'",
765 p_item->input.psz_name );
767 if( p_item->i_flags & ~PLAYLIST_ENA_FLAG )
769 p_playlist->i_enabled++;
771 p_item->i_flags |= PLAYLIST_ENA_FLAG;
773 var_SetInteger( p_playlist, "item-change", p_item->input.i_id );
778 * Move an item in a playlist
780 * This function must be entered without the playlist lock
782 * Move the item in the playlist with position i_pos before the current item
783 * at position i_newpos.
784 * \param p_playlist the playlist to move items in
785 * \param i_pos the position of the item to move
786 * \param i_newpos the position of the item that will be behind the moved item
788 * \return returns VLC_SUCCESS
790 int playlist_Move( playlist_t * p_playlist, int i_pos, int i_newpos )
793 vlc_mutex_lock( &p_playlist->object_lock );
795 /* take into account that our own row disappears. */
796 if( i_pos < i_newpos ) i_newpos--;
798 if( i_pos >= 0 && i_newpos >=0 && i_pos <= p_playlist->i_size &&
799 i_newpos <= p_playlist->i_size )
801 playlist_item_t * temp;
803 msg_Dbg( p_playlist, "moving playlist item `%s' (%i -> %i)",
804 p_playlist->pp_items[i_pos]->input.psz_name, i_pos, i_newpos);
806 if( i_pos == p_playlist->i_index )
808 p_playlist->i_index = i_newpos;
810 else if( i_pos > p_playlist->i_index &&
811 i_newpos <= p_playlist->i_index )
813 p_playlist->i_index++;
815 else if( i_pos < p_playlist->i_index &&
816 i_newpos >= p_playlist->i_index )
818 p_playlist->i_index--;
821 if ( i_pos < i_newpos )
823 temp = p_playlist->pp_items[i_pos];
824 while ( i_pos < i_newpos )
826 p_playlist->pp_items[i_pos] = p_playlist->pp_items[i_pos+1];
829 p_playlist->pp_items[i_newpos] = temp;
831 else if ( i_pos > i_newpos )
833 temp = p_playlist->pp_items[i_pos];
834 while ( i_pos > i_newpos )
836 p_playlist->pp_items[i_pos] = p_playlist->pp_items[i_pos-1];
839 p_playlist->pp_items[i_newpos] = temp;
843 vlc_mutex_unlock( &p_playlist->object_lock );
845 val.b_bool = VLC_TRUE;
846 var_Set( p_playlist, "intf-change", val );
854 * \param p_playlist the playlist
855 * \param p_item the item to move
856 * \param p_node the new parent of the item
857 * \param i_newpos the new position under this new parent
858 * \param i_view the view in which the move must be done or ALL_VIEWS
859 * \return VLC_SUCCESS or an error
861 int playlist_TreeMove( playlist_t * p_playlist, playlist_item_t *p_item,
862 playlist_item_t *p_node, int i_newpos, int i_view )
865 playlist_item_t *p_detach = NULL;
867 if( i_view == ALL_VIEWS )
869 for( i = 0 ; i < p_playlist->i_views; i++ )
871 playlist_TreeMove( p_playlist, p_item, p_node, i_newpos,
872 p_playlist->pp_views[i] );
877 /* Find the parent */
878 for( i = 0 ; i< p_item->i_parents; i++ )
880 if( p_item->pp_parents[i]->i_view == i_view )
882 p_detach = p_item->pp_parents[i]->p_parent;
886 if( p_detach == NULL )
888 msg_Err( p_playlist, "item not found in view %i", i_view );
892 /* Detach from the parent */
893 // playlist_NodeDetach( p_detach, p_item );
895 /* Attach to new parent */