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 *****************************************************************************/
25 #include <vlc/input.h>
26 #include "vlc_playlist.h"
28 #define PLAYLIST_DEBUG 1
30 /************************************************************************
32 ************************************************************************/
33 playlist_item_t *GetNextUncle( playlist_t *p_playlist, playlist_item_t *p_item,
34 playlist_item_t *p_root );
35 playlist_item_t *GetPrevUncle( playlist_t *p_playlist, playlist_item_t *p_item,
36 playlist_item_t *p_root );
38 playlist_item_t *GetNextItem( playlist_t *p_playlist,
39 playlist_item_t *p_root,
40 playlist_item_t *p_item );
41 playlist_item_t *GetPrevItem( playlist_t *p_playlist,
42 playlist_item_t *p_item,
43 playlist_item_t *p_root );
46 * Create a playlist node
48 * \param p_playlist the playlist
49 * \paam psz_name the name of the node
50 * \param p_parent the parent node to attach to or NULL if no attach
51 * \return the new node
53 playlist_item_t * playlist_NodeCreate( playlist_t *p_playlist, char *psz_name,
54 playlist_item_t *p_parent )
56 input_item_t *p_input;
57 playlist_item_t *p_item;
59 if( !psz_name ) psz_name = strdup( _("Undefined") );
60 p_input = input_ItemNewWithType( VLC_OBJECT(p_playlist), NULL, psz_name,
61 0, NULL, -1, ITEM_TYPE_NODE );
62 p_input->i_id = ++p_playlist->i_last_input_id;
63 p_item = playlist_ItemNewFromInput( VLC_OBJECT(p_playlist), p_input );
64 p_item->i_children = 0;
70 INSERT_ELEM( p_playlist->pp_all_items,
71 p_playlist->i_all_size,
72 p_playlist->i_all_size,
75 if( p_parent != NULL )
77 playlist_NodeAppend( p_playlist, p_item, p_parent );
80 playlist_SendAddNotify( p_playlist, p_item->i_id,
81 p_parent ? p_parent->i_id : -1 );
86 * Remove all the children of a node
88 * This function must be entered with the playlist lock
90 * \param p_playlist the playlist
91 * \param p_root the node
92 * \param b_delete_items do we have to delete the children items ?
93 * \return VLC_SUCCESS or an error
95 int playlist_NodeEmpty( playlist_t *p_playlist, playlist_item_t *p_root,
96 vlc_bool_t b_delete_items )
99 if( p_root->i_children == -1 )
104 /* Delete the children */
105 for( i = p_root->i_children-1 ; i >= 0 ;i-- )
107 if( p_root->pp_children[i]->i_children > -1 )
109 playlist_NodeDelete( p_playlist, p_root->pp_children[i],
110 b_delete_items , VLC_FALSE );
112 else if( b_delete_items )
114 /* Delete the item here */
115 playlist_DeleteFromItemId( p_playlist,
116 p_root->pp_children[i]->i_id );
123 * Remove all the children of a node and removes the node
125 * \param p_playlist the playlist
126 * \param p_root the node
127 * \param b_delete_items do we have to delete the children items ?
128 * \return VLC_SUCCESS or an error
130 int playlist_NodeDelete( playlist_t *p_playlist, playlist_item_t *p_root,
131 vlc_bool_t b_delete_items, vlc_bool_t b_force )
133 int i, i_top, i_bottom;
134 if( p_root->i_children == -1 )
139 /* Delete the children */
140 for( i = p_root->i_children - 1 ; i >= 0; i-- )
142 if( p_root->pp_children[i]->i_children > -1 )
144 playlist_NodeDelete( p_playlist, p_root->pp_children[i],
145 b_delete_items , b_force );
147 else if( b_delete_items )
149 playlist_DeleteFromItemId( p_playlist,
150 p_root->pp_children[i]->i_id );
153 /* Delete the node */
154 if( p_root->i_flags & PLAYLIST_RO_FLAG && !b_force )
159 var_SetInteger( p_playlist, "item-deleted", p_root->p_input->i_id );
161 i_bottom = 0; i_top = p_playlist->i_all_size - 1;
163 while( p_playlist->pp_all_items[i]->p_input->i_id !=
164 p_root->p_input->i_id && i_top > i_bottom )
166 if( p_playlist->pp_all_items[i]->p_input->i_id <
167 p_root->p_input->i_id )
175 i = i_bottom + ( i_top - i_bottom ) / 2;
177 if( p_playlist->pp_all_items[i]->p_input->i_id ==
178 p_root->p_input->i_id )
180 REMOVE_ELEM( p_playlist->pp_all_items, p_playlist->i_all_size, i );
182 playlist_ItemDelete( p_root );
189 * Adds an item to the children of a node
191 * \param p_playlist the playlist
192 * \param p_item the item to append
193 * \param p_parent the parent node
194 * \return VLC_SUCCESS or an error
196 int playlist_NodeAppend( playlist_t *p_playlist,
197 playlist_item_t *p_item,
198 playlist_item_t *p_parent )
200 return playlist_NodeInsert( p_playlist, p_item, p_parent, -1 );
203 int playlist_NodeInsert( playlist_t *p_playlist,
204 playlist_item_t *p_item,
205 playlist_item_t *p_parent,
208 assert( p_parent && p_parent->i_children != -1 );
209 if( i_position == -1 ) i_position = p_parent->i_children ;
211 INSERT_ELEM( p_parent->pp_children,
212 p_parent->i_children,
215 p_item->p_parent = p_parent;
220 * Deletes an item from the children of a node
222 * \param p_playlist the playlist
223 * \param p_item the item to remove
224 * \param p_parent the parent node
225 * \return VLC_SUCCESS or an error
227 int playlist_NodeRemoveItem( playlist_t *p_playlist,
228 playlist_item_t *p_item,
229 playlist_item_t *p_parent )
232 for( i= 0; i< p_parent->i_children ; i++ )
234 if( p_parent->pp_children[i] == p_item )
236 REMOVE_ELEM( p_parent->pp_children, p_parent->i_children, i );
245 * Count the children of a node
247 * \param p_playlist the playlist
248 * \param p_node the node
249 * \return the number of children
251 int playlist_NodeChildrenCount( playlist_t *p_playlist, playlist_item_t*p_node)
255 if( p_node->i_children == -1 )
258 for( i=0 ; i< p_node->i_children;i++ )
260 if( p_node->pp_children[i]->i_children == -1 )
263 i_nb += playlist_NodeChildrenCount( p_playlist,
264 p_node->pp_children[i] );
270 * Search a child of a node by its name
272 * \param p_node the node
273 * \param psz_search the name of the child to search
274 * \return the child item or NULL if not found or error
276 playlist_item_t *playlist_ChildSearchName( playlist_item_t *p_node,
277 const char *psz_search )
281 if( p_node->i_children < 0 )
285 for( i = 0 ; i< p_node->i_children; i++ )
287 if( !strcmp( p_node->pp_children[i]->p_input->psz_name, psz_search ) )
289 return p_node->pp_children[i];
295 /**********************************************************************
296 * Tree walking functions
297 **********************************************************************/
299 playlist_item_t *playlist_GetLastLeaf(playlist_t *p_playlist,
300 playlist_item_t *p_root )
303 playlist_item_t *p_item;
304 for ( i = p_root->i_children - 1; i >= 0; i-- )
306 if( p_root->pp_children[i]->i_children == -1 )
307 return p_root->pp_children[i];
308 else if( p_root->pp_children[i]->i_children > 0)
310 p_item = playlist_GetLastLeaf( p_playlist,
311 p_root->pp_children[i] );
312 if ( p_item != NULL )
322 * Finds the next item to play
324 * \param p_playlist the playlist
325 * \param p_root the root node
326 * \param p_item the previous item (NULL if none )
327 * \return the next item to play, or NULL if none found
329 playlist_item_t *playlist_GetNextLeaf( playlist_t *p_playlist,
330 playlist_item_t *p_root,
331 playlist_item_t *p_item )
333 playlist_item_t *p_next;
335 #ifdef PLAYLIST_DEBUG
337 msg_Dbg( p_playlist, "finding next of %s within %s",
338 p_item->p_input->psz_name, p_root->p_input->psz_name );
340 msg_Dbg( p_playlist, "finding something to play within %s",
341 p_root->p_input->psz_name );
344 assert( p_root && p_root->i_children != -1 );
346 /* Now, walk the tree until we find a suitable next item */
350 p_next = GetNextItem( p_playlist, p_root, p_next );
351 } while ( p_next && p_next != p_root && p_next->i_children != -1 );
353 #ifdef PLAYLIST_DEBUG
355 msg_Dbg( p_playlist, "At end of node" );
360 playlist_item_t *playlist_GetNextEnabledLeaf( playlist_t *p_playlist,
361 playlist_item_t *p_root,
362 playlist_item_t *p_item )
364 playlist_item_t *p_next;
366 #ifdef PLAYLIST_DEBUG
368 msg_Dbg( p_playlist, "finding next of %s within %s",
369 p_item->p_input->psz_name, p_root->p_input->psz_name );
371 msg_Dbg( p_playlist, "finding something to play within %s",
372 p_root->p_input->psz_name );
375 assert( p_root && p_root->i_children != -1 );
377 /* Now, walk the tree until we find a suitable next item */
381 p_next = GetNextItem( p_playlist, p_root, p_next );
382 } while ( p_next && p_next != p_root &&
383 !( p_next->i_children == -1 &&
384 !(p_next->i_flags & PLAYLIST_DBL_FLAG) ) );
386 #ifdef PLAYLIST_DEBUG
388 msg_Dbg( p_playlist, "At end of node" );
394 * Finds the previous item to play
396 * \param p_playlist the playlist
397 * \param p_root the root node
398 * \param p_item the previous item (NULL if none )
399 * \return the next item to play, or NULL if none found
401 playlist_item_t *playlist_GetPrevLeaf( playlist_t *p_playlist,
402 playlist_item_t *p_root,
403 playlist_item_t *p_item )
405 playlist_item_t *p_prev;
407 #ifdef PLAYLIST_DEBUG
409 msg_Dbg( p_playlist, "finding previous of %s within %s",
410 p_item->p_input->psz_name, p_root->p_input->psz_name );
412 msg_Dbg( p_playlist, "finding previous to play within %s",
413 p_root->p_input->psz_name );
415 assert( p_root && p_root->i_children != -1 );
417 /* Now, walk the tree until we find a suitable previous item */
421 p_prev = GetPrevItem( p_playlist, p_root, p_prev );
422 } while ( p_prev && p_prev != p_root && p_prev->i_children != -1 );
424 #ifdef PLAYLIST_DEBUG
426 msg_Dbg( p_playlist, "At beginning of node" );
431 /************************************************************************
432 * Following functions are local
433 ***********************************************************************/
436 * Get the next item in the tree
437 * If p_item is NULL, return the first child of root
439 playlist_item_t *GetNextItem( playlist_t *p_playlist,
440 playlist_item_t *p_root,
441 playlist_item_t *p_item )
443 playlist_item_t *p_parent;
446 /* Node with children, get the first one */
447 if( p_item && p_item->i_children > 0 )
448 return p_item->pp_children[0];
451 p_parent = p_item->p_parent;
455 for( i= 0 ; i < p_parent->i_children ; i++ )
457 if( p_item == NULL || p_parent->pp_children[i] == p_item )
462 if( i+1 >= p_parent->i_children )
464 /* Was already the last sibling. Look for uncles */
465 #ifdef PLAYLIST_DEBUG
466 msg_Dbg( p_playlist, "Current item is the last of the node,"
467 "looking for uncle from %s",
468 p_parent->p_input->psz_name );
470 if( p_parent == p_root )
472 #ifdef PLAYLIST_DEBUG
473 msg_Dbg( p_playlist, "Already at root" );
477 return GetNextUncle( p_playlist, p_item, p_root );
481 return p_parent->pp_children[i+1];
485 msg_Err( p_playlist, "I should not be here" );
489 playlist_item_t *GetNextUncle( playlist_t *p_playlist, playlist_item_t *p_item,
490 playlist_item_t *p_root )
492 playlist_item_t *p_parent = p_item->p_parent;
493 playlist_item_t *p_grandparent;
494 vlc_bool_t b_found = VLC_FALSE;
496 if( p_parent != NULL )
498 p_grandparent = p_parent->p_parent;
502 for( i = 0 ; i< p_grandparent->i_children ; i++ )
504 if( p_parent == p_grandparent->pp_children[i] )
506 #ifdef PLAYLIST_DEBUG
507 msg_Dbg( p_playlist, "parent %s found as child %i of "
509 p_parent->p_input->psz_name, i,
510 p_grandparent->p_input->psz_name );
516 if( b_found && i + 1 < p_grandparent->i_children )
518 return p_grandparent->pp_children[i+1];
520 /* Not found at root */
521 if( p_grandparent == p_root )
527 p_parent = p_grandparent;
528 p_grandparent = p_parent->p_parent;
532 /* We reached root */
536 playlist_item_t *GetPrevUncle( playlist_t *p_playlist, playlist_item_t *p_item,
537 playlist_item_t *p_root )
539 playlist_item_t *p_parent = p_item->p_parent;
540 playlist_item_t *p_grandparent;
541 vlc_bool_t b_found = VLC_FALSE;
543 if( p_parent != NULL )
545 p_grandparent = p_parent->p_parent;
549 for( i = p_grandparent->i_children -1 ; i >= 0; i-- )
551 if( p_parent == p_grandparent->pp_children[i] )
557 if( b_found && i - 1 > 0 )
559 return p_grandparent->pp_children[i-1];
561 /* Not found at root */
562 if( p_grandparent == p_root )
568 p_parent = p_grandparent;
569 p_grandparent = p_parent->p_parent;
573 /* We reached root */
578 /* Recursively search the tree for previous item */
579 playlist_item_t *GetPrevItem( playlist_t *p_playlist,
580 playlist_item_t *p_root,
581 playlist_item_t *p_item )
583 playlist_item_t *p_parent;
586 /* Node with children, get the last one */
587 if( p_item && p_item->i_children > 0 )
588 return p_item->pp_children[p_item->i_children - 1];
590 /* Last child of its parent ? */
592 p_parent = p_item->p_parent;
595 msg_Err( p_playlist, "Get the last one" );
599 for( i = p_parent->i_children -1 ; i >= 0 ; i-- )
601 if( p_parent->pp_children[i] == p_item )
605 /* Was already the first sibling. Look for uncles */
606 #ifdef PLAYLIST_DEBUG
607 msg_Dbg( p_playlist, "Current item is the first of the node,"
608 "looking for uncle from %s",
609 p_parent->p_input->psz_name );
611 return GetPrevUncle( p_playlist, p_item, p_root );
615 return p_parent->pp_children[i-1];
619 msg_Err( p_playlist, "I should not be here" );
623 /* Dump the contents of a node */
624 void playlist_NodeDump( playlist_t *p_playlist, playlist_item_t *p_item,
632 msg_Dbg( p_playlist, "%s (%i)",
633 p_item->p_input->psz_name, p_item->i_children );
636 if( p_item->i_children == -1 )
641 for( i = 0; i< p_item->i_children; i++ )
643 memset( str, 32, 512 );
644 sprintf( str + 2 * i_level , "%s (%i)",
645 p_item->pp_children[i]->p_input->psz_name,
646 p_item->pp_children[i]->i_children );
647 msg_Dbg( p_playlist, "%s",str );
648 if( p_item->pp_children[i]->i_children >= 0 )
650 playlist_NodeDump( p_playlist, p_item->pp_children[i],