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 /************************************************************************
30 ************************************************************************/
31 playlist_item_t *GetNextUncle( playlist_t *p_playlist, playlist_item_t *p_item,
32 playlist_item_t *p_root );
33 playlist_item_t *GetPrevUncle( playlist_t *p_playlist, playlist_item_t *p_item,
34 playlist_item_t *p_root );
36 playlist_item_t *GetNextItem( playlist_t *p_playlist,
37 playlist_item_t *p_root,
38 playlist_item_t *p_item );
39 playlist_item_t *GetPrevItem( playlist_t *p_playlist,
40 playlist_item_t *p_item,
41 playlist_item_t *p_root );
44 * Create a playlist node
46 * \param p_playlist the playlist
47 * \paam psz_name the name of the node
48 * \param p_parent the parent node to attach to or NULL if no attach
49 * \return the new node
51 playlist_item_t * playlist_NodeCreate( playlist_t *p_playlist, char *psz_name,
52 playlist_item_t *p_parent )
54 input_item_t *p_input;
55 playlist_item_t *p_item;
57 if( !psz_name ) psz_name = _("Undefined");
58 p_input = input_ItemNewWithType( VLC_OBJECT(p_playlist), NULL, psz_name,
59 0, NULL, -1, ITEM_TYPE_NODE );
60 p_input->i_id = ++p_playlist->i_last_input_id;
61 p_item = playlist_ItemNewFromInput( VLC_OBJECT(p_playlist), p_input );
62 p_item->i_children = 0;
68 INSERT_ELEM( p_playlist->pp_all_items,
69 p_playlist->i_all_size,
70 p_playlist->i_all_size,
73 if( p_parent != NULL )
75 playlist_NodeAppend( p_playlist, p_item, p_parent );
78 playlist_SendAddNotify( p_playlist, p_item->i_id,
79 p_parent ? p_parent->i_id : -1 );
84 * Remove all the children of a node
86 * This function must be entered with the playlist lock
88 * \param p_playlist the playlist
89 * \param p_root the node
90 * \param b_delete_items do we have to delete the children items ?
91 * \return VLC_SUCCESS or an error
93 int playlist_NodeEmpty( playlist_t *p_playlist, playlist_item_t *p_root,
94 vlc_bool_t b_delete_items )
97 if( p_root->i_children == -1 )
102 /* Delete the children */
103 for( i = p_root->i_children-1 ; i >= 0 ;i-- )
105 if( p_root->pp_children[i]->i_children > -1 )
107 playlist_NodeDelete( p_playlist, p_root->pp_children[i],
108 b_delete_items , VLC_FALSE );
110 else if( b_delete_items )
112 /* Delete the item here */
113 playlist_DeleteFromItemId( p_playlist,
114 p_root->pp_children[i]->i_id );
121 * Remove all the children of a node and removes the node
123 * \param p_playlist the playlist
124 * \param p_root the node
125 * \param b_delete_items do we have to delete the children items ?
126 * \return VLC_SUCCESS or an error
128 int playlist_NodeDelete( playlist_t *p_playlist, playlist_item_t *p_root,
129 vlc_bool_t b_delete_items, vlc_bool_t b_force )
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 );
159 for( i = 0 ; i< p_playlist->i_all_size; i ++ )
161 if( p_playlist->pp_all_items[i]->p_input->i_id ==
162 p_root->p_input->i_id )
164 REMOVE_ELEM( p_playlist->pp_all_items, p_playlist->i_all_size,
170 /* Remove the item from its parent */
171 if( p_root->p_parent )
173 playlist_NodeRemoveItem( p_playlist, p_root, p_root->p_parent );
176 playlist_ItemDelete( p_root );
183 * Adds an item to the children of a node
185 * \param p_playlist the playlist
186 * \param p_item the item to append
187 * \param p_parent the parent node
188 * \return VLC_SUCCESS or an error
190 int playlist_NodeAppend( playlist_t *p_playlist,
191 playlist_item_t *p_item,
192 playlist_item_t *p_parent )
194 return playlist_NodeInsert( p_playlist, p_item, p_parent, -1 );
197 int playlist_NodeInsert( playlist_t *p_playlist,
198 playlist_item_t *p_item,
199 playlist_item_t *p_parent,
202 assert( p_parent && p_parent->i_children != -1 );
203 if( i_position == -1 ) i_position = p_parent->i_children ;
205 INSERT_ELEM( p_parent->pp_children,
206 p_parent->i_children,
209 p_item->p_parent = p_parent;
214 * Deletes an item from the children of a node
216 * \param p_playlist the playlist
217 * \param p_item the item to remove
218 * \param p_parent the parent node
219 * \return VLC_SUCCESS or an error
221 int playlist_NodeRemoveItem( playlist_t *p_playlist,
222 playlist_item_t *p_item,
223 playlist_item_t *p_parent )
226 for( i= 0; i< p_parent->i_children ; i++ )
228 if( p_parent->pp_children[i] == p_item )
230 REMOVE_ELEM( p_parent->pp_children, p_parent->i_children, i );
239 * Count the children of a node
241 * \param p_playlist the playlist
242 * \param p_node the node
243 * \return the number of children
245 int playlist_NodeChildrenCount( playlist_t *p_playlist, playlist_item_t*p_node)
249 if( p_node->i_children == -1 )
252 for( i=0 ; i< p_node->i_children;i++ )
254 if( p_node->pp_children[i]->i_children == -1 )
257 i_nb += playlist_NodeChildrenCount( p_playlist,
258 p_node->pp_children[i] );
264 * Search a child of a node by its name
266 * \param p_node the node
267 * \param psz_search the name of the child to search
268 * \return the child item or NULL if not found or error
270 playlist_item_t *playlist_ChildSearchName( playlist_item_t *p_node,
271 const char *psz_search )
275 if( p_node->i_children < 0 )
279 for( i = 0 ; i< p_node->i_children; i++ )
281 if( !strcmp( p_node->pp_children[i]->p_input->psz_name, psz_search ) )
283 return p_node->pp_children[i];
290 void playlist_NodesCreateForSD( playlist_t *p_playlist, char *psz_name,
291 playlist_item_t **pp_node_cat,
292 playlist_item_t **pp_node_one )
294 *pp_node_cat = playlist_NodeCreate( p_playlist, psz_name,
295 p_playlist->p_root_category );
296 (*pp_node_cat)->i_flags |= PLAYLIST_RO_FLAG;
297 (*pp_node_cat)->i_flags |= PLAYLIST_SKIP_FLAG;
299 *pp_node_one = playlist_NodeCreate( p_playlist, psz_name,
300 p_playlist->p_root_onelevel );
301 (*pp_node_one)->i_flags |= PLAYLIST_RO_FLAG;
302 (*pp_node_one)->i_flags |= PLAYLIST_SKIP_FLAG;
304 (*pp_node_one)->p_input->i_id = (*pp_node_cat)->p_input->i_id;
308 /**********************************************************************
309 * Tree walking functions
310 **********************************************************************/
312 playlist_item_t *playlist_GetLastLeaf(playlist_t *p_playlist,
313 playlist_item_t *p_root )
316 playlist_item_t *p_item;
317 for ( i = p_root->i_children - 1; i >= 0; i-- )
319 if( p_root->pp_children[i]->i_children == -1 )
320 return p_root->pp_children[i];
321 else if( p_root->pp_children[i]->i_children > 0)
323 p_item = playlist_GetLastLeaf( p_playlist,
324 p_root->pp_children[i] );
325 if ( p_item != NULL )
335 * Finds the next item to play
337 * \param p_playlist the playlist
338 * \param p_root the root node
339 * \param p_item the previous item (NULL if none )
340 * \return the next item to play, or NULL if none found
342 playlist_item_t *playlist_GetNextLeaf( playlist_t *p_playlist,
343 playlist_item_t *p_root,
344 playlist_item_t *p_item )
346 playlist_item_t *p_next;
348 assert( p_root && p_root->i_children != -1 );
350 #ifdef PLAYLIST_DEBUG
352 msg_Dbg( p_playlist, "finding next of %s within %s",
353 p_item->p_input->psz_name, p_root->p_input->psz_name );
355 msg_Dbg( p_playlist, "finding something to play within %s",
356 p_root->p_input->psz_name );
360 /* Now, walk the tree until we find a suitable next item */
364 p_next = GetNextItem( p_playlist, p_root, p_next );
365 } while ( p_next && p_next != p_root && p_next->i_children != -1 );
367 #ifdef PLAYLIST_DEBUG
369 msg_Dbg( p_playlist, "At end of node" );
374 playlist_item_t *playlist_GetNextEnabledLeaf( playlist_t *p_playlist,
375 playlist_item_t *p_root,
376 playlist_item_t *p_item )
378 playlist_item_t *p_next;
380 #ifdef PLAYLIST_DEBUG
382 msg_Dbg( p_playlist, "finding next of %s within %s",
383 p_item->p_input->psz_name, p_root->p_input->psz_name );
385 msg_Dbg( p_playlist, "finding something to play within %s",
386 p_root->p_input->psz_name );
389 assert( p_root && p_root->i_children != -1 );
391 /* Now, walk the tree until we find a suitable next item */
395 p_next = GetNextItem( p_playlist, p_root, p_next );
396 } while ( p_next && p_next != p_root &&
397 !( p_next->i_children == -1 &&
398 !(p_next->i_flags & PLAYLIST_DBL_FLAG) ) );
400 #ifdef PLAYLIST_DEBUG
402 msg_Dbg( p_playlist, "At end of node" );
408 * Finds the previous item to play
410 * \param p_playlist the playlist
411 * \param p_root the root node
412 * \param p_item the previous item (NULL if none )
413 * \return the next item to play, or NULL if none found
415 playlist_item_t *playlist_GetPrevLeaf( playlist_t *p_playlist,
416 playlist_item_t *p_root,
417 playlist_item_t *p_item )
419 playlist_item_t *p_prev;
421 #ifdef PLAYLIST_DEBUG
423 msg_Dbg( p_playlist, "finding previous of %s within %s",
424 p_item->p_input->psz_name, p_root->p_input->psz_name );
426 msg_Dbg( p_playlist, "finding previous to play within %s",
427 p_root->p_input->psz_name );
429 assert( p_root && p_root->i_children != -1 );
431 /* Now, walk the tree until we find a suitable previous item */
435 p_prev = GetPrevItem( p_playlist, p_root, p_prev );
436 } while ( p_prev && p_prev != p_root && p_prev->i_children != -1 );
438 #ifdef PLAYLIST_DEBUG
440 msg_Dbg( p_playlist, "At beginning of node" );
445 /************************************************************************
446 * Following functions are local
447 ***********************************************************************/
450 * Get the next item in the tree
451 * If p_item is NULL, return the first child of root
453 playlist_item_t *GetNextItem( playlist_t *p_playlist,
454 playlist_item_t *p_root,
455 playlist_item_t *p_item )
457 playlist_item_t *p_parent;
460 /* Node with children, get the first one */
461 if( p_item && p_item->i_children > 0 )
462 return p_item->pp_children[0];
465 p_parent = p_item->p_parent;
469 for( i= 0 ; i < p_parent->i_children ; i++ )
471 if( p_item == NULL || p_parent->pp_children[i] == p_item )
476 if( i+1 >= p_parent->i_children )
478 /* Was already the last sibling. Look for uncles */
479 PL_DEBUG( "Current item is the last of the node,"
480 "looking for uncle from %s",
481 p_parent->p_input->psz_name );
483 if( p_parent == p_root )
485 PL_DEBUG( "already at root" );
488 return GetNextUncle( p_playlist, p_item, p_root );
492 return p_parent->pp_children[i+1];
496 msg_Err( p_playlist, "I should not be here" );
500 playlist_item_t *GetNextUncle( playlist_t *p_playlist, playlist_item_t *p_item,
501 playlist_item_t *p_root )
503 playlist_item_t *p_parent = p_item->p_parent;
504 playlist_item_t *p_grandparent;
505 vlc_bool_t b_found = VLC_FALSE;
507 if( p_parent != NULL )
509 p_grandparent = p_parent->p_parent;
513 for( i = 0 ; i< p_grandparent->i_children ; i++ )
515 if( p_parent == p_grandparent->pp_children[i] )
517 PL_DEBUG( "parent %s found as child %i of grandparent %s",
518 p_parent->p_input->psz_name, i,
519 p_grandparent->p_input->psz_name );
524 if( b_found && i + 1 < p_grandparent->i_children )
526 return p_grandparent->pp_children[i+1];
528 /* Not found at root */
529 if( p_grandparent == p_root )
535 p_parent = p_grandparent;
536 p_grandparent = p_parent->p_parent;
540 /* We reached root */
544 playlist_item_t *GetPrevUncle( playlist_t *p_playlist, playlist_item_t *p_item,
545 playlist_item_t *p_root )
547 playlist_item_t *p_parent = p_item->p_parent;
548 playlist_item_t *p_grandparent;
549 vlc_bool_t b_found = VLC_FALSE;
551 if( p_parent != NULL )
553 p_grandparent = p_parent->p_parent;
557 for( i = p_grandparent->i_children -1 ; i >= 0; i-- )
559 if( p_parent == p_grandparent->pp_children[i] )
565 if( b_found && i - 1 > 0 )
567 return p_grandparent->pp_children[i-1];
569 /* Not found at root */
570 if( p_grandparent == p_root )
576 p_parent = p_grandparent;
577 p_grandparent = p_parent->p_parent;
581 /* We reached root */
586 /* Recursively search the tree for previous item */
587 playlist_item_t *GetPrevItem( playlist_t *p_playlist,
588 playlist_item_t *p_root,
589 playlist_item_t *p_item )
591 playlist_item_t *p_parent;
594 /* Node with children, get the last one */
595 if( p_item && p_item->i_children > 0 )
596 return p_item->pp_children[p_item->i_children - 1];
598 /* Last child of its parent ? */
600 p_parent = p_item->p_parent;
603 msg_Err( p_playlist, "Get the last one" );
607 for( i = p_parent->i_children -1 ; i >= 0 ; i-- )
609 if( p_parent->pp_children[i] == p_item )
613 /* Was already the first sibling. Look for uncles */
614 PL_DEBUG( "Current item is the first of the node,"
615 "looking for uncle from %s",
616 p_parent->p_input->psz_name );
617 return GetPrevUncle( p_playlist, p_item, p_root );
621 return p_parent->pp_children[i-1];
625 msg_Err( p_playlist, "I should not be here" );
629 /* Dump the contents of a node */
630 void playlist_NodeDump( playlist_t *p_playlist, playlist_item_t *p_item,
638 msg_Dbg( p_playlist, "%s (%i)",
639 p_item->p_input->psz_name, p_item->i_children );
642 if( p_item->i_children == -1 )
647 for( i = 0; i< p_item->i_children; i++ )
649 memset( str, 32, 512 );
650 sprintf( str + 2 * i_level , "%s (%i)",
651 p_item->pp_children[i]->p_input->psz_name,
652 p_item->pp_children[i]->i_children );
653 msg_Dbg( p_playlist, "%s",str );
654 if( p_item->pp_children[i]->i_children >= 0 )
656 playlist_NodeDump( p_playlist, p_item->pp_children[i],