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 p_playlist->i_enabled ++;
193 /* We update the ALL view directly */
194 playlist_ViewUpdate( p_playlist, VIEW_ALL );
196 /* Add the item to the General category */
197 if( b_end == VLC_TRUE )
199 playlist_NodeAppend( p_playlist, VIEW_CATEGORY, p_item,
200 p_playlist->p_general );
201 p_add->p_item = p_item;
202 p_add->p_node = p_playlist->p_general;
203 p_add->i_view = VIEW_CATEGORY;
204 val.p_address = p_add;
205 var_Set( p_playlist, "item-append", val );
209 playlist_NodeInsert( p_playlist, VIEW_CATEGORY, p_item,
210 p_playlist->p_general, i_pos );
214 p_view = playlist_ViewFind( p_playlist, VIEW_ALL );
215 playlist_ItemAddParent( p_item, VIEW_ALL, p_view->p_root );
217 /* Also add the item to the "simple" view */
218 p_view = playlist_ViewFind( p_playlist, VIEW_SIMPLE );
220 if( b_end == VLC_TRUE )
222 playlist_NodeAppend( p_playlist, VIEW_SIMPLE,p_item,
224 p_add->p_item = p_item;
225 p_add->p_node = p_view->p_root;
226 p_add->i_view = VIEW_SIMPLE;
227 val.p_address = p_add;
228 var_Set( p_playlist, "item-append", val );
232 playlist_NodeInsert( p_playlist, VIEW_SIMPLE,p_item,
233 p_view->p_root, i_pos );
237 /* FIXME : Update sorted views */
239 if( p_playlist->i_index >= i_pos )
241 p_playlist->i_index++;
246 msg_Err( p_playlist, "Insert mode not implemented" );
249 if( (i_mode & PLAYLIST_GO ) && p_view )
251 p_playlist->request.b_request = VLC_TRUE;
253 p_playlist->request.i_view = VIEW_SIMPLE;
254 p_playlist->request.p_node = p_view->p_root;
255 p_playlist->request.p_item = p_item;
257 if( p_playlist->p_input )
259 input_StopThread( p_playlist->p_input );
261 p_playlist->status.i_status = PLAYLIST_RUNNING;
264 vlc_mutex_unlock( &p_playlist->object_lock );
266 if( b_end == VLC_FALSE )
268 val.b_bool = VLC_TRUE;
269 var_Set( p_playlist, "intf-change", val );
274 return p_item->input.i_id;
279 * Add a playlist item to a given node (in the category view )
281 * \param p_playlist the playlist to insert into
282 * \param p_item the playlist item to insert
283 * \param i_view the view for which to add or TODO: ALL_VIEWS
284 * \param p_parent the parent node
285 * \param i_mode the mode used when adding
286 * \param i_pos the possition in the node where to add. If this is
287 * PLAYLIST_END the item will be added at the end of the node
288 ** \return The id of the playlist item
290 int playlist_NodeAddItem( playlist_t *p_playlist, playlist_item_t *p_item,
291 int i_view,playlist_item_t *p_parent,
292 int i_mode, int i_pos)
296 playlist_view_t *p_view;
298 playlist_add_t *p_add = (playlist_add_t *)malloc(sizeof( playlist_add_t));
300 vlc_mutex_lock( &p_playlist->object_lock );
303 if( !p_parent || p_parent->i_children == -1 )
305 msg_Err( p_playlist, "invalid node" );
309 * CHECK_INSERT : checks if the item is already enqued before
312 if ( i_mode & PLAYLIST_CHECK_INSERT )
316 if ( p_playlist->pp_items )
318 for ( j = 0; j < p_playlist->i_size; j++ )
320 if ( !strcmp( p_playlist->pp_items[j]->input.psz_uri,
321 p_item->input.psz_uri ) )
323 playlist_ItemDelete( p_item );
324 vlc_mutex_unlock( &p_playlist->object_lock );
330 i_mode &= ~PLAYLIST_CHECK_INSERT;
331 i_mode |= PLAYLIST_APPEND;
334 msg_Dbg( p_playlist, "adding playlist item `%s' ( %s )",
335 p_item->input.psz_name, p_item->input.psz_uri );
337 p_item->input.i_id = ++p_playlist->i_last_id;
339 /* First, add the item at the right position in the item bank */
341 //i_position = p_playlist->i_index == -1 ? 0 : p_playlist->i_index;
342 i_position = p_playlist->i_size ;
344 INSERT_ELEM( p_playlist->pp_items,
348 p_playlist->i_enabled ++;
350 /* TODO: Handle modes */
351 playlist_NodeAppend( p_playlist, i_view, p_item, p_parent );
353 p_add->p_item = p_item;
354 p_add->p_node = p_parent;
355 p_add->i_view = i_view;
356 val.p_address = p_add;
357 var_Set( p_playlist, "item-append", val );
359 /* We update the ALL view directly */
360 p_view = playlist_ViewFind( p_playlist, VIEW_ALL );
361 playlist_ItemAddParent( p_item, VIEW_ALL, p_view->p_root );
362 playlist_ViewUpdate( p_playlist, VIEW_ALL );
364 /* TODO : Update sorted views*/
366 if( i_mode & PLAYLIST_GO )
368 p_playlist->request.b_request = VLC_TRUE;
369 p_playlist->request.i_view = VIEW_CATEGORY;
370 p_playlist->request.p_node = p_parent;
371 p_playlist->request.p_item = p_item;
372 if( p_playlist->p_input )
374 input_StopThread( p_playlist->p_input );
376 p_playlist->status.i_status = PLAYLIST_RUNNING;
379 vlc_mutex_unlock( &p_playlist->object_lock );
381 val.b_bool = VLC_TRUE;
382 // var_Set( p_playlist, "intf-change", val );
386 return p_item->input.i_id;
389 /***************************************************************************
390 * Item search functions
391 ***************************************************************************/
394 * Search the position of an item by its id
395 * This function must be entered with the playlist lock
397 * \param p_playlist the playlist
398 * \param i_id the id to find
399 * \return the position, or VLC_EGENERIC on failure
401 int playlist_GetPositionById( playlist_t * p_playlist , int i_id )
404 for( i = 0 ; i < p_playlist->i_size ; i++ )
406 if( p_playlist->pp_items[i]->input.i_id == i_id )
416 * Search an item by its position
417 * This function must be entered with the playlist lock
419 * \param p_playlist the playlist
420 * \param i_pos the position of the item to find
421 * \return the item, or NULL on failure
423 playlist_item_t * playlist_ItemGetByPos( playlist_t * p_playlist , int i_pos )
425 if( i_pos >= 0 && i_pos < p_playlist->i_size)
427 return p_playlist->pp_items[i_pos];
429 else if( p_playlist->i_size > 0)
431 return p_playlist->pp_items[p_playlist->i_index];
440 * Search an item by its id
442 * \param p_playlist the playlist
443 * \param i_id the id to find
444 * \return the item, or NULL on failure
446 playlist_item_t * playlist_ItemGetById( playlist_t * p_playlist , int i_id )
449 for( i = 0 ; i < p_playlist->i_size ; i++ )
451 if( p_playlist->pp_items[i]->input.i_id == i_id )
453 return p_playlist->pp_items[i];
460 * Search an item by its input_item_t
462 * \param p_playlist the playlist
463 * \param p_item the input_item_t to find
464 * \return the item, or NULL on failure
466 playlist_item_t * playlist_ItemGetByInput( playlist_t * p_playlist ,
467 input_item_t *p_item )
470 if( &p_playlist->status.p_item->input == p_item )
472 return p_playlist->status.p_item;
475 for( i = 0 ; i < p_playlist->i_size ; i++ )
477 if( &p_playlist->pp_items[i]->input == p_item )
479 return p_playlist->pp_items[i];
487 /***********************************************************************
489 ***********************************************************************/
492 * Transform an item to a node
494 * This function must be entered without the playlist lock
496 * \param p_playlist the playlist object
497 * \param p_item the item to transform
500 int playlist_ItemToNode( playlist_t *p_playlist,playlist_item_t *p_item )
503 if( p_item->i_children == -1 )
505 p_item->i_children = 0;
508 vlc_mutex_lock( &p_playlist->object_lock );
510 /* Remove it from the array of available items */
511 for( i = 0 ; i < p_playlist->i_size ; i++ )
513 if( p_item == p_playlist->pp_items[i] )
515 REMOVE_ELEM( p_playlist->pp_items, p_playlist->i_size, i );
518 vlc_mutex_unlock( &p_playlist->object_lock );
524 * Replaces an item with another one
525 * This function must be entered without the playlist lock
527 * \see playlist_Replace
529 int playlist_LockAndReplace( playlist_t *p_playlist,
530 playlist_item_t *p_olditem,
531 input_item_t *p_new )
534 vlc_mutex_lock( &p_playlist->object_lock );
535 i_ret = playlist_Replace( p_playlist, p_olditem, p_new );
536 vlc_mutex_unlock( &p_playlist->object_lock );
541 * Replaces an item with another one
542 * This function must be entered with the playlist lock:
544 * \param p_playlist the playlist
545 * \param p_olditem the item to replace
546 * \param p_new the new input_item
547 * \return VLC_SUCCESS or an error
549 int playlist_Replace( playlist_t *p_playlist, playlist_item_t *p_olditem,
550 input_item_t *p_new )
555 if( p_olditem->i_children != -1 )
557 msg_Err( p_playlist, "playlist_Replace can only be used on leafs");
561 p_olditem->i_nb_played = 0;
562 memcpy( &p_olditem->input, p_new, sizeof( input_item_t ) );
564 p_olditem->i_nb_played = 0;
566 for( i = 0 ; i< p_olditem->i_parents ; i++ )
568 playlist_item_t *p_parent = p_olditem->pp_parents[i]->p_parent;
570 for( j = 0 ; j< p_parent->i_children ; i++ )
572 if( p_parent->pp_children[j] == p_olditem )
574 p_parent->i_serial++;
582 * Deletes an item from a playlist.
584 * This function must be entered without the playlist lock
586 * \param p_playlist the playlist to remove from.
587 * \param i_id the identifier of the item to delete
588 * \return returns VLC_SUCCESS or an error
590 int playlist_Delete( playlist_t * p_playlist, int i_id )
595 playlist_item_t *p_item = playlist_ItemGetById( p_playlist, i_id );
602 /* Check if it is the current item */
603 if( p_playlist->status.p_item == p_item )
605 playlist_Control( p_playlist, PLAYLIST_STOP );
606 p_playlist->status.p_item = NULL;
609 vlc_mutex_lock( &p_playlist->object_lock );
611 msg_Dbg( p_playlist, "deleting playlist item `%s'",
612 p_item->input.psz_name );
614 /* Remove the item from all its parent nodes */
615 for ( i= 0 ; i < p_item->i_parents ; i++ )
617 playlist_NodeRemoveItem( p_playlist, p_item,
618 p_item->pp_parents[i]->p_parent );
619 if( p_item->pp_parents[i]->i_view == VIEW_ALL )
621 p_playlist->i_size--;
625 /* TODO : Update views */
627 playlist_ItemDelete( p_item );
629 vlc_mutex_unlock( &p_playlist->object_lock );
631 val.b_bool = VLC_TRUE;
632 var_Set( p_playlist, "intf-change", val );
638 * Clear all playlist items
640 * \param p_playlist the playlist to be cleared.
643 int playlist_Clear( playlist_t * p_playlist )
646 playlist_view_t *p_view;
647 for( i = p_playlist->i_size; i > 0 ; i-- )
649 playlist_Delete( p_playlist, p_playlist->pp_items[0]->input.i_id );
651 for( i = 0 ; i< p_playlist->i_views; i++ )
653 playlist_ViewEmpty( p_playlist, i, VLC_TRUE );
660 * Disables a playlist item
662 * \param p_playlist the playlist to disable from.
663 * \param i_pos the position of the item to disable
666 int playlist_Disable( playlist_t * p_playlist, playlist_item_t *p_item )
668 if( !p_item ) return VLC_EGENERIC;
670 msg_Dbg( p_playlist, "disabling playlist item `%s'",
671 p_item->input.psz_name );
673 if( p_item->i_flags & PLAYLIST_ENA_FLAG )
675 p_playlist->i_enabled--;
677 p_item->i_flags &= ~PLAYLIST_ENA_FLAG;
679 var_SetInteger( p_playlist, "item-change", p_item->input.i_id );
684 * Enables a playlist item
686 * \param p_playlist the playlist to enable from.
687 * \param i_pos the position of the item to enable
690 int playlist_Enable( playlist_t * p_playlist, playlist_item_t *p_item )
692 if( !p_item ) return VLC_EGENERIC;
694 msg_Dbg( p_playlist, "enabling playlist item `%s'",
695 p_item->input.psz_name );
697 if( p_item->i_flags & ~PLAYLIST_ENA_FLAG )
699 p_playlist->i_enabled++;
701 p_item->i_flags |= PLAYLIST_ENA_FLAG;
703 var_SetInteger( p_playlist, "item-change", p_item->input.i_id );
708 * Move an item in a playlist
710 * This function must be entered without the playlist lock
712 * Move the item in the playlist with position i_pos before the current item
713 * at position i_newpos.
714 * \param p_playlist the playlist to move items in
715 * \param i_pos the position of the item to move
716 * \param i_newpos the position of the item that will be behind the moved item
718 * \return returns VLC_SUCCESS
720 int playlist_Move( playlist_t * p_playlist, int i_pos, int i_newpos )
723 vlc_mutex_lock( &p_playlist->object_lock );
725 /* take into account that our own row disappears. */
726 if( i_pos < i_newpos ) i_newpos--;
728 if( i_pos >= 0 && i_newpos >=0 && i_pos <= p_playlist->i_size &&
729 i_newpos <= p_playlist->i_size )
731 playlist_item_t * temp;
733 msg_Dbg( p_playlist, "moving playlist item `%s' (%i -> %i)",
734 p_playlist->pp_items[i_pos]->input.psz_name, i_pos, i_newpos);
736 if( i_pos == p_playlist->i_index )
738 p_playlist->i_index = i_newpos;
740 else if( i_pos > p_playlist->i_index &&
741 i_newpos <= p_playlist->i_index )
743 p_playlist->i_index++;
745 else if( i_pos < p_playlist->i_index &&
746 i_newpos >= p_playlist->i_index )
748 p_playlist->i_index--;
751 if ( i_pos < i_newpos )
753 temp = p_playlist->pp_items[i_pos];
754 while ( i_pos < i_newpos )
756 p_playlist->pp_items[i_pos] = p_playlist->pp_items[i_pos+1];
759 p_playlist->pp_items[i_newpos] = temp;
761 else if ( i_pos > i_newpos )
763 temp = p_playlist->pp_items[i_pos];
764 while ( i_pos > i_newpos )
766 p_playlist->pp_items[i_pos] = p_playlist->pp_items[i_pos-1];
769 p_playlist->pp_items[i_newpos] = temp;
773 vlc_mutex_unlock( &p_playlist->object_lock );
775 val.b_bool = VLC_TRUE;
776 var_Set( p_playlist, "intf-change", val );
784 * \param p_playlist the playlist
785 * \param p_item the item to move
786 * \param p_node the new parent of the item
787 * \param i_newpos the new position under this new parent
788 * \param i_view the view in which the move must be done or ALL_VIEWS
789 * \return VLC_SUCCESS or an error
791 int playlist_TreeMove( playlist_t * p_playlist, playlist_item_t *p_item,
792 playlist_item_t *p_node, int i_newpos, int i_view )
795 playlist_item_t *p_detach = NULL;
797 if( i_view == ALL_VIEWS )
799 for( i = 0 ; i < p_playlist->i_views; i++ )
801 playlist_TreeMove( p_playlist, p_item, p_node, i_newpos,
802 p_playlist->pp_views[i] );
807 /* Find the parent */
808 for( i = 0 ; i< p_item->i_parents; i++ )
810 if( p_item->pp_parents[i]->i_view == i_view )
812 p_detach = p_item->pp_parents[i]->p_parent;
816 if( p_detach == NULL )
818 msg_Err( p_playlist, "item not found in view %i", i_view );
822 /* Detach from the parent */
823 // playlist_NodeDetach( p_detach, p_item );
825 /* Attach to new parent */