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 vlc_mutex_lock( &p_playlist->object_lock );
126 * CHECK_INSERT : checks if the item is already enqued before
130 /* That should not change */
131 if ( i_mode & PLAYLIST_CHECK_INSERT )
135 if ( p_playlist->pp_items )
137 for ( j = 0; j < p_playlist->i_size; j++ )
139 if ( !strcmp( p_playlist->pp_items[j]->input.psz_uri,
140 p_item->input.psz_uri ) )
142 playlist_ItemDelete( p_item );
143 vlc_mutex_unlock( &p_playlist->object_lock );
148 i_mode &= ~PLAYLIST_CHECK_INSERT;
149 i_mode |= PLAYLIST_APPEND;
152 msg_Dbg( p_playlist, "adding playlist item `%s' ( %s )",
153 p_item->input.psz_name, p_item->input.psz_uri );
155 p_item->input.i_id = ++p_playlist->i_last_id;
157 /* Do a few boundary checks and allocate space for the item */
158 if( i_pos == PLAYLIST_END )
161 if( i_mode & PLAYLIST_INSERT )
163 i_mode &= ~PLAYLIST_INSERT;
164 i_mode |= PLAYLIST_APPEND;
167 i_pos = p_playlist->i_size - 1;
170 if( !(i_mode & PLAYLIST_REPLACE)
171 || i_pos < 0 || i_pos >= p_playlist->i_size )
173 /* Additional boundary checks */
174 if( i_mode & PLAYLIST_APPEND )
183 else if( i_pos > p_playlist->i_size )
185 i_pos = p_playlist->i_size;
188 INSERT_ELEM( p_playlist->pp_items, p_playlist->i_size, i_pos, p_item );
189 p_playlist->i_enabled ++;
191 /* We update the ALL view directly */
192 playlist_ViewUpdate( p_playlist, VIEW_ALL );
194 /* Add the item to the General category */
195 if( b_end == VLC_TRUE )
197 playlist_NodeAppend( p_playlist, VIEW_CATEGORY, p_item,
198 p_playlist->p_general );
202 playlist_NodeInsert( p_playlist, VIEW_CATEGORY, p_item,
203 p_playlist->p_general, i_pos );
205 p_view = playlist_ViewFind( p_playlist, VIEW_ALL );
206 playlist_ItemAddParent( p_item, VIEW_ALL, p_view->p_root );
208 /* Also add the item to the "simple" view */
209 p_view = playlist_ViewFind( p_playlist, VIEW_SIMPLE );
211 if( b_end == VLC_TRUE )
213 playlist_NodeAppend( p_playlist, VIEW_SIMPLE,p_item,
218 playlist_NodeInsert( p_playlist, VIEW_SIMPLE,p_item,
219 p_view->p_root, i_pos );
222 /* FIXME : Update sorted views */
224 if( p_playlist->i_index >= i_pos )
226 p_playlist->i_index++;
231 msg_Err( p_playlist, "Insert mode not implemented" );
234 if( (i_mode & PLAYLIST_GO ) && p_view )
236 p_playlist->request.b_request = VLC_TRUE;
238 p_playlist->request.i_view = VIEW_SIMPLE;
239 p_playlist->request.p_node = p_view->p_root;
240 p_playlist->request.p_item = p_item;
242 if( p_playlist->p_input )
244 input_StopThread( p_playlist->p_input );
246 p_playlist->status.i_status = PLAYLIST_RUNNING;
249 vlc_mutex_unlock( &p_playlist->object_lock );
251 val.b_bool = VLC_TRUE;
252 var_Set( p_playlist, "intf-change", val );
254 return p_item->input.i_id;
259 * Add a playlist item to a given node (in the category view )
261 * \param p_playlist the playlist to insert into
262 * \param p_item the playlist item to insert
263 * \param i_view the view for which to add or TODO: ALL_VIEWS
264 * \param p_parent the parent node
265 * \param i_mode the mode used when adding
266 * \param i_pos the possition in the node where to add. If this is
267 * PLAYLIST_END the item will be added at the end of the node
268 ** \return The id of the playlist item
270 int playlist_NodeAddItem( playlist_t *p_playlist, playlist_item_t *p_item,
271 int i_view,playlist_item_t *p_parent,
272 int i_mode, int i_pos)
276 playlist_view_t *p_view;
278 vlc_mutex_lock( &p_playlist->object_lock );
281 if( !p_parent || p_parent->i_children == -1 )
283 msg_Err( p_playlist, "invalid node" );
287 * CHECK_INSERT : checks if the item is already enqued before
290 if ( i_mode & PLAYLIST_CHECK_INSERT )
294 if ( p_playlist->pp_items )
296 for ( j = 0; j < p_playlist->i_size; j++ )
298 if ( !strcmp( p_playlist->pp_items[j]->input.psz_uri,
299 p_item->input.psz_uri ) )
301 playlist_ItemDelete( p_item );
302 vlc_mutex_unlock( &p_playlist->object_lock );
307 i_mode &= ~PLAYLIST_CHECK_INSERT;
308 i_mode |= PLAYLIST_APPEND;
311 msg_Dbg( p_playlist, "adding playlist item `%s' ( %s )",
312 p_item->input.psz_name, p_item->input.psz_uri );
314 p_item->input.i_id = ++p_playlist->i_last_id;
316 /* First, add the item at the right position in the item bank */
318 //i_position = p_playlist->i_index == -1 ? 0 : p_playlist->i_index;
319 i_position = p_playlist->i_size ;
321 INSERT_ELEM( p_playlist->pp_items,
325 p_playlist->i_enabled ++;
327 /* TODO: Handle modes */
328 playlist_NodeAppend( p_playlist, i_view, p_item, p_parent );
330 /* We update the ALL view directly */
331 p_view = playlist_ViewFind( p_playlist, VIEW_ALL );
332 playlist_ItemAddParent( p_item, VIEW_ALL, p_view->p_root );
333 playlist_ViewUpdate( p_playlist, VIEW_ALL );
335 /* TODO : Update sorted views*/
337 if( i_mode & PLAYLIST_GO )
339 p_playlist->request.b_request = VLC_TRUE;
340 p_playlist->request.i_view = VIEW_CATEGORY;
341 p_playlist->request.p_node = p_parent;
342 p_playlist->request.p_item = p_item;
343 if( p_playlist->p_input )
345 input_StopThread( p_playlist->p_input );
347 p_playlist->status.i_status = PLAYLIST_RUNNING;
350 vlc_mutex_unlock( &p_playlist->object_lock );
352 val.b_bool = VLC_TRUE;
353 var_Set( p_playlist, "intf-change", val );
355 return p_item->input.i_id;
358 /***************************************************************************
359 * Item search functions
360 ***************************************************************************/
363 * Search the position of an item by its id
364 * This function must be entered with the playlist lock
366 * \param p_playlist the playlist
367 * \param i_id the id to find
368 * \return the position, or VLC_EGENERIC on failure
370 int playlist_GetPositionById( playlist_t * p_playlist , int i_id )
373 for( i = 0 ; i < p_playlist->i_size ; i++ )
375 if( p_playlist->pp_items[i]->input.i_id == i_id )
385 * Search an item by its position
386 * This function must be entered with the playlist lock
388 * \param p_playlist the playlist
389 * \param i_pos the position of the item to find
390 * \return the item, or NULL on failure
392 playlist_item_t * playlist_ItemGetByPos( playlist_t * p_playlist , int i_pos )
394 if( i_pos >= 0 && i_pos < p_playlist->i_size)
396 return p_playlist->pp_items[i_pos];
398 else if( p_playlist->i_size > 0)
400 return p_playlist->pp_items[p_playlist->i_index];
409 * Search an item by its id
411 * \param p_playlist the playlist
412 * \param i_id the id to find
413 * \return the item, or NULL on failure
415 playlist_item_t * playlist_ItemGetById( playlist_t * p_playlist , int i_id )
418 for( i = 0 ; i < p_playlist->i_size ; i++ )
420 if( p_playlist->pp_items[i]->input.i_id == i_id )
422 return p_playlist->pp_items[i];
429 * Search an item by its input_item_t
431 * \param p_playlist the playlist
432 * \param p_item the input_item_t to find
433 * \return the item, or NULL on failure
435 playlist_item_t * playlist_ItemGetByInput( playlist_t * p_playlist ,
436 input_item_t *p_item )
439 for( i = 0 ; i < p_playlist->i_size ; i++ )
441 if( &p_playlist->pp_items[i]->input == p_item )
443 return p_playlist->pp_items[i];
451 /***********************************************************************
453 ***********************************************************************/
456 * Transform an item to a node
458 * This function must be entered without the playlist lock
460 * \param p_playlist the playlist object
461 * \param p_item the item to transform
464 int playlist_ItemToNode( playlist_t *p_playlist,playlist_item_t *p_item )
467 if( p_item->i_children == -1 )
469 p_item->i_children = 0;
472 vlc_mutex_lock( &p_playlist->object_lock );
474 /* Remove it from the array of available items */
475 for( i = 0 ; i < p_playlist->i_size ; i++ )
477 if( p_item == p_playlist->pp_items[i] )
479 REMOVE_ELEM( p_playlist->pp_items, p_playlist->i_size, i );
482 vlc_mutex_unlock( &p_playlist->object_lock );
488 * Replaces an item with another one
489 * This function must be entered without the playlist lock
491 * \see playlist_Replace
493 int playlist_LockAndReplace( playlist_t *p_playlist,
494 playlist_item_t *p_olditem,
495 input_item_t *p_new )
498 vlc_mutex_lock( &p_playlist->object_lock );
499 i_ret = playlist_Replace( p_playlist, p_olditem, p_new );
500 vlc_mutex_unlock( &p_playlist->object_lock );
505 * Replaces an item with another one
506 * This function must be entered with the playlist lock:
508 * \param p_playlist the playlist
509 * \param p_olditem the item to replace
510 * \param p_new the new input_item
511 * \return VLC_SUCCESS or an error
513 int playlist_Replace( playlist_t *p_playlist, playlist_item_t *p_olditem,
514 input_item_t *p_new )
519 if( p_olditem->i_children != -1 )
521 msg_Err( p_playlist, "playlist_Replace can only be used on leafs");
525 p_olditem->i_nb_played = 0;
526 memcpy( &p_olditem->input, p_new, sizeof( input_item_t ) );
528 p_olditem->i_nb_played = 0;
530 for( i = 0 ; i< p_olditem->i_parents ; i++ )
532 playlist_item_t *p_parent = p_olditem->pp_parents[i]->p_parent;
534 for( j = 0 ; j< p_parent->i_children ; i++ )
536 if( p_parent->pp_children[j] == p_olditem )
538 p_parent->i_serial++;
546 * Deletes an item from a playlist.
548 * This function must be entered without the playlist lock
550 * \param p_playlist the playlist to remove from.
551 * \param i_id the identifier of the item to delete
552 * \return returns VLC_SUCCESS or an error
554 int playlist_Delete( playlist_t * p_playlist, int i_id )
559 playlist_item_t *p_item = playlist_ItemGetById( p_playlist, i_id );
566 /* Check if it is the current item */
567 if( p_playlist->status.p_item == p_item )
569 playlist_Control( p_playlist, PLAYLIST_STOP );
570 p_playlist->status.p_item = NULL;
573 vlc_mutex_lock( &p_playlist->object_lock );
575 msg_Dbg( p_playlist, "deleting playlist item `%s'",
576 p_item->input.psz_name );
578 /* Remove the item from all its parent nodes */
579 for ( i= 0 ; i < p_item->i_parents ; i++ )
581 playlist_NodeRemoveItem( p_playlist, p_item,
582 p_item->pp_parents[i]->p_parent );
583 if( p_item->pp_parents[i]->i_view == VIEW_ALL )
585 p_playlist->i_size--;
589 /* TODO : Update views */
591 playlist_ItemDelete( p_item );
593 vlc_mutex_unlock( &p_playlist->object_lock );
595 val.b_bool = VLC_TRUE;
596 var_Set( p_playlist, "intf-change", val );
602 * Clear all playlist items
604 * \param p_playlist the playlist to be cleared.
607 int playlist_Clear( playlist_t * p_playlist )
610 for( i = p_playlist->i_size; i > 0 ; i-- )
612 playlist_Delete( p_playlist, p_playlist->pp_items[0]->input.i_id );
619 * Disables a playlist item
621 * \param p_playlist the playlist to disable from.
622 * \param i_pos the position of the item to disable
625 int playlist_Disable( playlist_t * p_playlist, playlist_item_t *p_item )
627 if( !p_item ) return VLC_EGENERIC;
629 msg_Dbg( p_playlist, "disabling playlist item `%s'",
630 p_item->input.psz_name );
632 if( p_item->i_flags & PLAYLIST_ENA_FLAG )
634 p_playlist->i_enabled--;
636 p_item->i_flags &= ~PLAYLIST_ENA_FLAG;
638 var_SetInteger( p_playlist, "item-change", p_item->input.i_id );
643 * Enables a playlist item
645 * \param p_playlist the playlist to enable from.
646 * \param i_pos the position of the item to enable
649 int playlist_Enable( playlist_t * p_playlist, playlist_item_t *p_item )
651 if( !p_item ) return VLC_EGENERIC;
653 msg_Dbg( p_playlist, "enabling playlist item `%s'",
654 p_item->input.psz_name );
656 if( p_item->i_flags & ~PLAYLIST_ENA_FLAG )
658 p_playlist->i_enabled++;
660 p_item->i_flags |= PLAYLIST_ENA_FLAG;
662 var_SetInteger( p_playlist, "item-change", p_item->input.i_id );
667 * Move an item in a playlist
669 * This function must be entered without the playlist lock
671 * Move the item in the playlist with position i_pos before the current item
672 * at position i_newpos.
673 * \param p_playlist the playlist to move items in
674 * \param i_pos the position of the item to move
675 * \param i_newpos the position of the item that will be behind the moved item
677 * \return returns VLC_SUCCESS
679 int playlist_Move( playlist_t * p_playlist, int i_pos, int i_newpos )
682 vlc_mutex_lock( &p_playlist->object_lock );
684 /* take into account that our own row disappears. */
685 if( i_pos < i_newpos ) i_newpos--;
687 if( i_pos >= 0 && i_newpos >=0 && i_pos <= p_playlist->i_size &&
688 i_newpos <= p_playlist->i_size )
690 playlist_item_t * temp;
692 msg_Dbg( p_playlist, "moving playlist item `%s' (%i -> %i)",
693 p_playlist->pp_items[i_pos]->input.psz_name, i_pos, i_newpos);
695 if( i_pos == p_playlist->i_index )
697 p_playlist->i_index = i_newpos;
699 else if( i_pos > p_playlist->i_index &&
700 i_newpos <= p_playlist->i_index )
702 p_playlist->i_index++;
704 else if( i_pos < p_playlist->i_index &&
705 i_newpos >= p_playlist->i_index )
707 p_playlist->i_index--;
710 if ( i_pos < i_newpos )
712 temp = p_playlist->pp_items[i_pos];
713 while ( i_pos < i_newpos )
715 p_playlist->pp_items[i_pos] = p_playlist->pp_items[i_pos+1];
718 p_playlist->pp_items[i_newpos] = temp;
720 else if ( i_pos > i_newpos )
722 temp = p_playlist->pp_items[i_pos];
723 while ( i_pos > i_newpos )
725 p_playlist->pp_items[i_pos] = p_playlist->pp_items[i_pos-1];
728 p_playlist->pp_items[i_newpos] = temp;
732 vlc_mutex_unlock( &p_playlist->object_lock );
734 val.b_bool = VLC_TRUE;
735 var_Set( p_playlist, "intf-change", val );
743 * \param p_playlist the playlist
744 * \param p_item the item to move
745 * \param p_node the new parent of the item
746 * \param i_newpos the new position under this new parent
747 * \param i_view the view in which the move must be done or ALL_VIEWS
748 * \return VLC_SUCCESS or an error
750 int playlist_TreeMove( playlist_t * p_playlist, playlist_item_t *p_item,
751 playlist_item_t *p_node, int i_newpos, int i_view )
754 playlist_item_t *p_detach = NULL;
756 if( i_view == ALL_VIEWS )
758 for( i = 0 ; i < p_playlist->i_views; i++ )
760 playlist_TreeMove( p_playlist, p_item, p_node, i_newpos,
761 p_playlist->pp_views[i] );
766 /* Find the parent */
767 for( i = 0 ; i< p_item->i_parents; i++ )
769 if( p_item->pp_parents[i]->i_view == i_view )
771 p_detach = p_item->pp_parents[i]->p_parent;
775 if( p_detach == NULL )
777 msg_Err( p_playlist, "item not found in view %i", i_view );
781 /* Detach from the parent */
782 // playlist_NodeDetach( p_detach, p_item );
784 /* Attach to new parent */