1 /*****************************************************************************
2 * tree.c : Playlist tree walking functions
3 *****************************************************************************
4 * Copyright (C) 1999-2004 the VideoLAN team
7 * Authors: Clément Stenac <zorglub@videolan.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 #include <vlc/input.h>
25 #include "vlc_playlist.h"
27 #define PLAYLIST_DEBUG 1
29 /************************************************************************
31 ************************************************************************/
32 playlist_item_t *GetNextUncle( playlist_t *p_playlist, playlist_item_t *p_item,
33 playlist_item_t *p_root );
34 playlist_item_t *GetPrevUncle( playlist_t *p_playlist, playlist_item_t *p_item,
35 playlist_item_t *p_root );
37 playlist_item_t *GetNextItem( playlist_t *p_playlist,
38 playlist_item_t *p_root,
39 playlist_item_t *p_item );
40 playlist_item_t *GetPrevItem( playlist_t *p_playlist,
41 playlist_item_t *p_item,
42 playlist_item_t *p_root );
45 * Create a playlist node
47 * \param p_playlist the playlist
48 * \paam psz_name the name of the node
49 * \param p_parent the parent node to attach to or NULL if no attach
50 * \return the new node
52 playlist_item_t * playlist_NodeCreate( playlist_t *p_playlist, char *psz_name,
53 playlist_item_t *p_parent )
55 input_item_t *p_input;
56 playlist_item_t *p_item;
58 if( !psz_name ) psz_name = strdup( _("Undefined") );
59 p_input = input_ItemNewWithType( VLC_OBJECT(p_playlist), NULL, psz_name,
60 0, NULL, -1, ITEM_TYPE_NODE );
61 p_input->i_id = ++p_playlist->i_last_input_id;
62 p_item = playlist_ItemNewFromInput( VLC_OBJECT(p_playlist), p_input );
63 p_item->i_children = 0;
69 INSERT_ELEM( p_playlist->pp_all_items,
70 p_playlist->i_all_size,
71 p_playlist->i_all_size,
74 if( p_parent != NULL )
76 playlist_NodeAppend( p_playlist, p_item, p_parent );
79 playlist_SendAddNotify( p_playlist, p_item->i_id,
80 p_parent ? p_parent->i_id : -1 );
85 * Remove all the children of a node
87 * This function must be entered with the playlist lock
89 * \param p_playlist the playlist
90 * \param p_root the node
91 * \param b_delete_items do we have to delete the children items ?
92 * \return VLC_SUCCESS or an error
94 int playlist_NodeEmpty( playlist_t *p_playlist, playlist_item_t *p_root,
95 vlc_bool_t b_delete_items )
98 if( p_root->i_children == -1 )
103 /* Delete the children */
104 for( i = p_root->i_children-1 ; i >= 0 ;i-- )
106 if( p_root->pp_children[i]->i_children > -1 )
108 playlist_NodeDelete( p_playlist, p_root->pp_children[i],
109 b_delete_items , VLC_FALSE );
111 else if( b_delete_items )
113 /* Delete the item here */
114 playlist_DeleteFromItemId( p_playlist,
115 p_root->pp_children[i]->i_id );
122 * Remove all the children of a node and removes the node
124 * \param p_playlist the playlist
125 * \param p_root the node
126 * \param b_delete_items do we have to delete the children items ?
127 * \return VLC_SUCCESS or an error
129 int playlist_NodeDelete( playlist_t *p_playlist, playlist_item_t *p_root,
130 vlc_bool_t b_delete_items, vlc_bool_t b_force )
132 int i, i_top, i_bottom;
133 if( p_root->i_children == -1 )
138 /* Delete the children */
139 for( i = p_root->i_children - 1 ; i >= 0; i-- )
141 if( p_root->pp_children[i]->i_children > -1 )
143 playlist_NodeDelete( p_playlist, p_root->pp_children[i],
144 b_delete_items , b_force );
146 else if( b_delete_items )
148 playlist_DeleteFromItemId( p_playlist,
149 p_root->pp_children[i]->i_id );
152 /* Delete the node */
153 if( p_root->i_flags & PLAYLIST_RO_FLAG && !b_force )
158 var_SetInteger( p_playlist, "item-deleted", p_root->p_input->i_id );
160 i_bottom = 0; i_top = p_playlist->i_all_size - 1;
162 while( p_playlist->pp_all_items[i]->p_input->i_id !=
163 p_root->p_input->i_id && i_top > i_bottom )
165 if( p_playlist->pp_all_items[i]->p_input->i_id <
166 p_root->p_input->i_id )
174 i = i_bottom + ( i_top - i_bottom ) / 2;
176 if( p_playlist->pp_all_items[i]->p_input->i_id ==
177 p_root->p_input->i_id )
179 REMOVE_ELEM( p_playlist->pp_all_items, p_playlist->i_all_size, i );
181 playlist_ItemDelete( p_root );
188 * Adds an item to the children of a node
190 * \param p_playlist the playlist
191 * \param p_item the item to append
192 * \param p_parent the parent node
193 * \return VLC_SUCCESS or an error
195 int playlist_NodeAppend( playlist_t *p_playlist,
196 playlist_item_t *p_item,
197 playlist_item_t *p_parent )
199 return playlist_NodeInsert( p_playlist, p_item, p_parent, -1 );
202 int playlist_NodeInsert( playlist_t *p_playlist,
203 playlist_item_t *p_item,
204 playlist_item_t *p_parent,
207 if( !p_parent || p_parent->i_children == -1 )
209 msg_Err( p_playlist, "invalid node" );
212 if( i_position == -1 ) i_position = p_parent->i_children ;
214 INSERT_ELEM( p_parent->pp_children,
215 p_parent->i_children,
219 p_item->p_parent = p_parent;
225 * Deletes an item from the children of a node
227 * \param p_playlist the playlist
228 * \param p_item the item to remove
229 * \param p_parent the parent node
230 * \return VLC_SUCCESS or an error
232 int playlist_NodeRemoveItem( playlist_t *p_playlist,
233 playlist_item_t *p_item,
234 playlist_item_t *p_parent )
237 for( i= 0; i< p_parent->i_children ; i++ )
239 if( p_parent->pp_children[i] == p_item )
241 REMOVE_ELEM( p_parent->pp_children, p_parent->i_children, i );
250 * Count the children of a node
252 * \param p_playlist the playlist
253 * \param p_node the node
254 * \return the number of children
256 int playlist_NodeChildrenCount( playlist_t *p_playlist, playlist_item_t*p_node)
260 if( p_node->i_children == -1 )
263 for( i=0 ; i< p_node->i_children;i++ )
265 if( p_node->pp_children[i]->i_children == -1 )
268 i_nb += playlist_NodeChildrenCount( p_playlist,
269 p_node->pp_children[i] );
275 * Search a child of a node by its name
277 * \param p_node the node
278 * \param psz_search the name of the child to search
279 * \return the child item or NULL if not found or error
281 playlist_item_t *playlist_ChildSearchName( playlist_item_t *p_node,
282 const char *psz_search )
286 if( p_node->i_children < 0 )
290 for( i = 0 ; i< p_node->i_children; i++ )
292 if( !strcmp( p_node->pp_children[i]->p_input->psz_name, psz_search ) )
294 return p_node->pp_children[i];
300 /**********************************************************************
301 * Tree walking functions
302 **********************************************************************/
304 playlist_item_t *playlist_GetLastLeaf(playlist_t *p_playlist,
305 playlist_item_t *p_root )
308 playlist_item_t *p_item;
309 for ( i = p_root->i_children - 1; i >= 0; i-- )
311 if( p_root->pp_children[i]->i_children == -1 )
312 return p_root->pp_children[i];
313 else if( p_root->pp_children[i]->i_children > 0)
315 p_item = playlist_GetLastLeaf( p_playlist,
316 p_root->pp_children[i] );
317 if ( p_item != NULL )
327 * Finds the next item to play
329 * \param p_playlist the playlist
330 * \param p_root the root node
331 * \param p_item the previous item (NULL if none )
332 * \return the next item to play, or NULL if none found
334 playlist_item_t *playlist_GetNextLeaf( playlist_t *p_playlist,
335 playlist_item_t *p_root,
336 playlist_item_t *p_item )
338 playlist_item_t *p_next;
340 #ifdef PLAYLIST_DEBUG
342 msg_Dbg( p_playlist, "finding next of %s within %s",
343 p_item->p_input->psz_name, p_root->p_input->psz_name );
345 msg_Dbg( p_playlist, "finding something to play within %s",
346 p_root->p_input->psz_name );
349 if( !p_root || p_root->i_children == -1 )
351 msg_Err( p_playlist,"invalid arguments for GetNextLeaf" );
355 /* Now, walk the tree until we find a suitable next item */
359 p_next = GetNextItem( p_playlist, p_root, p_next );
360 } while ( p_next && p_next != p_root && p_next->i_children != -1 );
362 #ifdef PLAYLIST_DEBUG
364 msg_Dbg( p_playlist, "At end of node" );
369 playlist_item_t *playlist_GetNextEnabledLeaf( playlist_t *p_playlist,
370 playlist_item_t *p_root,
371 playlist_item_t *p_item )
373 playlist_item_t *p_next;
375 #ifdef PLAYLIST_DEBUG
377 msg_Dbg( p_playlist, "finding next of %s within %s",
378 p_item->p_input->psz_name, p_root->p_input->psz_name );
380 msg_Dbg( p_playlist, "finding something to play within %s",
381 p_root->p_input->psz_name );
384 if( !p_root || p_root->i_children == -1 )
386 msg_Err( p_playlist,"invalid arguments for GetNextEnabledLeaf" );
390 /* Now, walk the tree until we find a suitable next item */
394 p_next = GetNextItem( p_playlist, p_root, p_next );
395 } while ( p_next && p_next != p_root &&
396 !( p_next->i_children == -1 &&
397 !(p_next->i_flags & PLAYLIST_DBL_FLAG) ) );
399 #ifdef PLAYLIST_DEBUG
401 msg_Dbg( p_playlist, "At end of node" );
407 * Finds the previous item to play
409 * \param p_playlist the playlist
410 * \param p_root the root node
411 * \param p_item the previous item (NULL if none )
412 * \return the next item to play, or NULL if none found
414 playlist_item_t *playlist_GetPrevLeaf( playlist_t *p_playlist,
415 playlist_item_t *p_root,
416 playlist_item_t *p_item )
418 playlist_item_t *p_prev;
420 #ifdef PLAYLIST_DEBUG
422 msg_Dbg( p_playlist, "finding previous of %s within %s",
423 p_item->p_input->psz_name, p_root->p_input->psz_name );
425 msg_Dbg( p_playlist, "finding previous to play within %s",
426 p_root->p_input->psz_name );
429 if( !p_root || p_root->i_children == -1 )
431 msg_Err( p_playlist,"invalid arguments for GetPrevLeaf" );
435 /* Now, walk the tree until we find a suitable previous item */
439 p_prev = GetPrevItem( p_playlist, p_root, p_prev );
440 } while ( p_prev && p_prev != p_root && p_prev->i_children != -1 );
442 #ifdef PLAYLIST_DEBUG
444 msg_Dbg( p_playlist, "At beginning of node" );
449 /************************************************************************
450 * Following functions are local
451 ***********************************************************************/
454 * Get the next item in the tree
455 * If p_item is NULL, return the first child of root
457 playlist_item_t *GetNextItem( playlist_t *p_playlist,
458 playlist_item_t *p_root,
459 playlist_item_t *p_item )
461 playlist_item_t *p_parent;
464 /* Node with children, get the first one */
465 if( p_item && p_item->i_children > 0 )
466 return p_item->pp_children[0];
469 p_parent = p_item->p_parent;
473 for( i= 0 ; i < p_parent->i_children ; i++ )
475 if( p_item == NULL || p_parent->pp_children[i] == p_item )
480 if( i+1 >= p_parent->i_children )
482 /* Was already the last sibling. Look for uncles */
483 #ifdef PLAYLIST_DEBUG
484 msg_Dbg( p_playlist, "Current item is the last of the node,"
485 "looking for uncle from %s",
486 p_parent->p_input->psz_name );
488 if( p_parent == p_root )
490 #ifdef PLAYLIST_DEBUG
491 msg_Dbg( p_playlist, "Already at root" );
495 return GetNextUncle( p_playlist, p_item, p_root );
499 return p_parent->pp_children[i+1];
503 msg_Err( p_playlist, "I should not be here" );
507 playlist_item_t *GetNextUncle( playlist_t *p_playlist, playlist_item_t *p_item,
508 playlist_item_t *p_root )
510 playlist_item_t *p_parent = p_item->p_parent;
511 playlist_item_t *p_grandparent;
512 vlc_bool_t b_found = VLC_FALSE;
514 if( p_parent != NULL )
516 p_grandparent = p_parent->p_parent;
520 for( i = 0 ; i< p_grandparent->i_children ; i++ )
522 if( p_parent == p_grandparent->pp_children[i] )
524 #ifdef PLAYLIST_DEBUG
525 msg_Dbg( p_playlist, "parent %s found as child %i of "
527 p_parent->p_input->psz_name, i,
528 p_grandparent->p_input->psz_name );
534 if( b_found && i + 1 < p_grandparent->i_children )
536 return p_grandparent->pp_children[i+1];
538 /* Not found at root */
539 if( p_grandparent == p_root )
545 p_parent = p_grandparent;
546 p_grandparent = p_parent->p_parent;
550 /* We reached root */
554 playlist_item_t *GetPrevUncle( playlist_t *p_playlist, playlist_item_t *p_item,
555 playlist_item_t *p_root )
557 playlist_item_t *p_parent = p_item->p_parent;
558 playlist_item_t *p_grandparent;
559 vlc_bool_t b_found = VLC_FALSE;
561 if( p_parent != NULL )
563 p_grandparent = p_parent->p_parent;
567 for( i = p_grandparent->i_children -1 ; i >= 0; i-- )
569 if( p_parent == p_grandparent->pp_children[i] )
575 if( b_found && i - 1 > 0 )
577 return p_grandparent->pp_children[i-1];
579 /* Not found at root */
580 if( p_grandparent == p_root )
586 p_parent = p_grandparent;
587 p_grandparent = p_parent->p_parent;
591 /* We reached root */
596 /* Recursively search the tree for previous item */
597 playlist_item_t *GetPrevItem( playlist_t *p_playlist,
598 playlist_item_t *p_root,
599 playlist_item_t *p_item )
601 playlist_item_t *p_parent;
604 /* Node with children, get the last one */
605 if( p_item && p_item->i_children > 0 )
606 return p_item->pp_children[p_item->i_children - 1];
608 /* Last child of its parent ? */
610 p_parent = p_item->p_parent;
613 msg_Err( p_playlist, "Get the last one" );
617 for( i = p_parent->i_children -1 ; i >= 0 ; i-- )
619 if( p_parent->pp_children[i] == p_item )
623 /* Was already the first sibling. Look for uncles */
624 #ifdef PLAYLIST_DEBUG
625 msg_Dbg( p_playlist, "Current item is the first of the node,"
626 "looking for uncle from %s",
627 p_parent->p_input->psz_name );
629 return GetPrevUncle( p_playlist, p_item, p_root );
633 return p_parent->pp_children[i-1];
637 msg_Err( p_playlist, "I should not be here" );
641 /* Dump the contents of a node */
642 void playlist_NodeDump( playlist_t *p_playlist, playlist_item_t *p_item,
650 msg_Dbg( p_playlist, "%s (%i)",
651 p_item->p_input->psz_name, p_item->i_children );
654 if( p_item->i_children == -1 )
659 for( i = 0; i< p_item->i_children; i++ )
661 memset( str, 32, 512 );
662 sprintf( str + 2 * i_level , "%s (%i)",
663 p_item->pp_children[i]->p_input->psz_name,
664 p_item->pp_children[i]->i_children );
665 msg_Dbg( p_playlist, "%s",str );
666 if( p_item->pp_children[i]->i_children >= 0 )
668 playlist_NodeDump( p_playlist, p_item->pp_children[i],