1 /*****************************************************************************
2 * tree.c : Playlist tree walking functions
3 *****************************************************************************
4 * Copyright (C) 1999-2007 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_playlist.h"
26 #include "playlist_internal.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 * \param p_flags miscellaneous flags
50 * \param p_input the input_item to attach to or NULL if it has to be created
51 * \return the new node
53 playlist_item_t * playlist_NodeCreate( playlist_t *p_playlist,
55 playlist_item_t *p_parent, int i_flags,
56 input_item_t *p_input )
58 input_item_t *p_new_input;
59 playlist_item_t *p_item;
61 if( !psz_name ) psz_name = _("Undefined");
64 p_new_input = input_ItemNewWithType( VLC_OBJECT(p_playlist), NULL,
65 psz_name, 0, NULL, -1, ITEM_TYPE_NODE );
66 p_item = playlist_ItemNewFromInput( VLC_OBJECT(p_playlist),
67 p_input ? p_input : p_new_input );
69 if( p_item == NULL ) return NULL;
70 p_item->i_children = 0;
72 ARRAY_APPEND(p_playlist->all_items, p_item);
74 if( p_parent != NULL )
75 playlist_NodeAppend( p_playlist, p_item, p_parent );
76 playlist_SendAddNotify( p_playlist, p_item->i_id,
77 p_parent ? p_parent->i_id : -1,
78 !( i_flags & PLAYLIST_NO_REBUILD ));
83 * Remove all the children of a node
85 * This function must be entered with the playlist lock
87 * \param p_playlist the playlist
88 * \param p_root the node
89 * \param b_delete_items do we have to delete the children items ?
90 * \return VLC_SUCCESS or an error
92 int playlist_NodeEmpty( playlist_t *p_playlist, playlist_item_t *p_root,
93 vlc_bool_t b_delete_items )
96 if( p_root->i_children == -1 )
101 /* Delete the children */
102 for( i = p_root->i_children-1 ; i >= 0 ;i-- )
104 if( p_root->pp_children[i]->i_children > -1 )
106 playlist_NodeDelete( p_playlist, p_root->pp_children[i],
107 b_delete_items , VLC_FALSE );
109 else if( b_delete_items )
111 /* Delete the item here */
112 playlist_DeleteFromItemId( p_playlist,
113 p_root->pp_children[i]->i_id );
120 * Remove all the children of a node and removes the node
122 * \param p_playlist the playlist
123 * \param p_root the node
124 * \param b_delete_items do we have to delete the children items ?
125 * \return VLC_SUCCESS or an error
127 int playlist_NodeDelete( playlist_t *p_playlist, playlist_item_t *p_root,
128 vlc_bool_t b_delete_items, vlc_bool_t b_force )
132 if( p_root->i_children == -1 )
137 /* Delete the children */
138 for( i = p_root->i_children - 1 ; i >= 0; i-- )
140 if( p_root->pp_children[i]->i_children > -1 )
142 playlist_NodeDelete( p_playlist, p_root->pp_children[i],
143 b_delete_items , b_force );
145 else if( b_delete_items )
147 playlist_DeleteFromItemId( p_playlist,
148 p_root->pp_children[i]->i_id );
151 /* Delete the node */
152 if( p_root->i_flags & PLAYLIST_RO_FLAG && !b_force )
158 var_SetInteger( p_playlist, "item-deleted", p_root->i_id );
159 ARRAY_BSEARCH( p_playlist->all_items, ->i_id, int,
162 ARRAY_REMOVE( p_playlist->all_items, i );
164 /* Remove the item from its parent */
165 if( p_root->p_parent )
166 playlist_NodeRemoveItem( p_playlist, p_root, p_root->p_parent );
168 playlist_ItemDelete( p_root );
175 * Adds an item to the children of a node
177 * \param p_playlist the playlist
178 * \param p_item the item to append
179 * \param p_parent the parent node
180 * \return VLC_SUCCESS or an error
182 int playlist_NodeAppend( playlist_t *p_playlist,
183 playlist_item_t *p_item,
184 playlist_item_t *p_parent )
186 return playlist_NodeInsert( p_playlist, p_item, p_parent, -1 );
189 int playlist_NodeInsert( playlist_t *p_playlist,
190 playlist_item_t *p_item,
191 playlist_item_t *p_parent,
195 assert( p_parent && p_parent->i_children != -1 );
196 if( i_position == -1 ) i_position = p_parent->i_children ;
198 INSERT_ELEM( p_parent->pp_children,
199 p_parent->i_children,
202 p_item->p_parent = p_parent;
207 * Deletes an item from the children of a node
209 * \param p_playlist the playlist
210 * \param p_item the item to remove
211 * \param p_parent the parent node
212 * \return VLC_SUCCESS or an error
214 int playlist_NodeRemoveItem( playlist_t *p_playlist,
215 playlist_item_t *p_item,
216 playlist_item_t *p_parent )
220 for(int i= 0; i< p_parent->i_children ; i++ )
222 if( p_parent->pp_children[i] == p_item )
224 REMOVE_ELEM( p_parent->pp_children, p_parent->i_children, i );
233 * Count the children of a node
235 * \param p_playlist the playlist
236 * \param p_node the node
237 * \return the number of children
239 int playlist_NodeChildrenCount( playlist_t *p_playlist, playlist_item_t*p_node)
244 if( p_node->i_children == -1 )
247 i_nb = p_node->i_children;
248 for( i=0 ; i< p_node->i_children;i++ )
250 if( p_node->pp_children[i]->i_children == -1 )
253 i_nb += playlist_NodeChildrenCount( p_playlist,
254 p_node->pp_children[i] );
260 * Search a child of a node by its name
262 * \param p_node the node
263 * \param psz_search the name of the child to search
264 * \return the child item or NULL if not found or error
266 playlist_item_t *playlist_ChildSearchName( playlist_item_t *p_node,
267 const char *psz_search )
271 if( p_node->i_children < 0 )
275 for( i = 0 ; i< p_node->i_children; i++ )
277 if( !strcmp( p_node->pp_children[i]->p_input->psz_name, psz_search ) )
279 return p_node->pp_children[i];
286 * Create a pair of nodes in the category and onelevel trees.
287 * They share the same input item.
288 * \param p_playlist the playlist
289 * \param psz_name the name of the nodes
290 * \param pp_node_cat pointer to return the node in category tree
291 * \param pp_node_one pointer to return the node in onelevel tree
292 * \param b_for_sd For Services Discovery ? (make node read-only and unskipping)
294 void playlist_NodesPairCreate( playlist_t *p_playlist, const char *psz_name,
295 playlist_item_t **pp_node_cat,
296 playlist_item_t **pp_node_one,
297 vlc_bool_t b_for_sd )
299 *pp_node_cat = playlist_NodeCreate( p_playlist, psz_name,
300 p_playlist->p_root_category, 0, NULL );
301 *pp_node_one = playlist_NodeCreate( p_playlist, psz_name,
302 p_playlist->p_root_onelevel, 0,
303 (*pp_node_cat)->p_input );
306 (*pp_node_cat)->i_flags |= PLAYLIST_RO_FLAG;
307 (*pp_node_cat)->i_flags |= PLAYLIST_SKIP_FLAG;
308 (*pp_node_one)->i_flags |= PLAYLIST_RO_FLAG;
309 (*pp_node_one)->i_flags |= PLAYLIST_SKIP_FLAG;
314 * Get the node in the preferred tree from a node in one of category
316 * For example, for the SAP node, it will return the node in the category
317 * tree if --playlist-tree is not set to never, because the SAP node prefers
320 playlist_item_t * playlist_GetPreferredNode( playlist_t *p_playlist,
321 playlist_item_t *p_node )
324 if( p_node->p_parent == p_playlist->p_root_category )
326 if( p_playlist->b_always_tree ||
327 p_node->p_input->b_prefers_tree ) return p_node;
328 for( i = 0 ; i< p_playlist->p_root_onelevel->i_children; i++ )
330 if( p_playlist->p_root_onelevel->pp_children[i]->p_input->i_id ==
331 p_node->p_input->i_id )
332 return p_playlist->p_root_onelevel->pp_children[i];
335 else if( p_node->p_parent == p_playlist->p_root_onelevel )
337 if( p_playlist->b_never_tree || !p_node->p_input->b_prefers_tree )
339 for( i = 0 ; i< p_playlist->p_root_category->i_children; i++ )
341 if( p_playlist->p_root_category->pp_children[i]->p_input->i_id ==
342 p_node->p_input->i_id )
343 return p_playlist->p_root_category->pp_children[i];
349 /**********************************************************************
350 * Tree walking functions
351 **********************************************************************/
353 playlist_item_t *playlist_GetLastLeaf(playlist_t *p_playlist,
354 playlist_item_t *p_root )
357 playlist_item_t *p_item;
358 for ( i = p_root->i_children - 1; i >= 0; i-- )
360 if( p_root->pp_children[i]->i_children == -1 )
361 return p_root->pp_children[i];
362 else if( p_root->pp_children[i]->i_children > 0)
364 p_item = playlist_GetLastLeaf( p_playlist,
365 p_root->pp_children[i] );
366 if ( p_item != NULL )
375 int playlist_GetAllEnabledChildren( playlist_t *p_playlist,
376 playlist_item_t *p_node,
377 playlist_item_t ***ppp_items )
380 playlist_item_t *p_next = NULL;
383 p_next = playlist_GetNextLeaf( p_playlist, p_node,
384 p_next, VLC_TRUE, VLC_FALSE );
386 INSERT_ELEM( *ppp_items, i_count, i_count, p_next );
394 * Finds the next 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_GetNextLeaf( playlist_t *p_playlist,
402 playlist_item_t *p_root,
403 playlist_item_t *p_item,
404 vlc_bool_t b_ena, vlc_bool_t b_unplayed )
406 playlist_item_t *p_next;
408 assert( p_root && p_root->i_children != -1 );
410 PL_DEBUG2( "finding next of %s within %s",
411 PLI_NAME( p_item ), PLI_NAME( p_root ) );
413 /* Now, walk the tree until we find a suitable next item */
417 vlc_bool_t b_ena_ok = VLC_TRUE, b_unplayed_ok = VLC_TRUE;
418 p_next = GetNextItem( p_playlist, p_root, p_next );
419 if( !p_next || p_next == p_root )
421 if( p_next->i_children == -1 )
423 if( b_ena && p_next->i_flags & PLAYLIST_DBL_FLAG )
424 b_ena_ok = VLC_FALSE;
425 if( b_unplayed && p_next->p_input->i_nb_played != 0 )
426 b_unplayed_ok = VLC_FALSE;
427 if( b_ena_ok && b_unplayed_ok ) break;
430 if( p_next == NULL ) PL_DEBUG2( "at end of node" );
435 * Finds the previous item to play
437 * \param p_playlist the playlist
438 * \param p_root the root node
439 * \param p_item the previous item (NULL if none )
440 * \return the next item to play, or NULL if none found
442 playlist_item_t *playlist_GetPrevLeaf( playlist_t *p_playlist,
443 playlist_item_t *p_root,
444 playlist_item_t *p_item,
445 vlc_bool_t b_ena, vlc_bool_t b_unplayed )
447 playlist_item_t *p_prev;
449 PL_DEBUG2( "finding previous os %s within %s", PLI_NAME( p_item ),
450 PLI_NAME( p_root ) );
451 assert( p_root && p_root->i_children != -1 );
453 /* Now, walk the tree until we find a suitable previous item */
457 vlc_bool_t b_ena_ok = VLC_TRUE, b_unplayed_ok = VLC_TRUE;
458 p_prev = GetPrevItem( p_playlist, p_root, p_prev );
459 if( !p_prev || p_prev == p_root )
461 if( p_prev->i_children == -1 )
463 if( b_ena && p_prev->i_flags & PLAYLIST_DBL_FLAG )
464 b_ena_ok = VLC_FALSE;
465 if( b_unplayed && p_prev->p_input->i_nb_played != 0 )
466 b_unplayed_ok = VLC_FALSE;
467 if( b_ena_ok && b_unplayed_ok ) break;
470 if( p_prev == NULL ) PL_DEBUG2( "at beginning of node" );
474 /************************************************************************
475 * Following functions are local
476 ***********************************************************************/
479 * Get the next item in the tree
480 * If p_item is NULL, return the first child of root
482 playlist_item_t *GetNextItem( playlist_t *p_playlist,
483 playlist_item_t *p_root,
484 playlist_item_t *p_item )
486 playlist_item_t *p_parent;
489 /* Node with children, get the first one */
490 if( p_item && p_item->i_children > 0 )
491 return p_item->pp_children[0];
494 p_parent = p_item->p_parent;
497 for( i= 0 ; i < p_parent->i_children ; i++ )
499 if( p_item == NULL || p_parent->pp_children[i] == p_item )
504 if( i+1 >= p_parent->i_children )
506 /* Was already the last sibling. Look for uncles */
507 PL_DEBUG2( "Current item is the last of the node,"
508 "looking for uncle from %s",
509 p_parent->p_input->psz_name );
511 if( p_parent == p_root )
513 PL_DEBUG2( "already at root" );
516 return GetNextUncle( p_playlist, p_item, p_root );
520 return p_parent->pp_children[i+1];
527 playlist_item_t *GetNextUncle( playlist_t *p_playlist, playlist_item_t *p_item,
528 playlist_item_t *p_root )
530 playlist_item_t *p_parent = p_item->p_parent;
531 playlist_item_t *p_grandparent;
532 vlc_bool_t b_found = VLC_FALSE;
536 if( p_parent != NULL )
538 p_grandparent = p_parent->p_parent;
539 while( p_grandparent )
542 for( i = 0 ; i< p_grandparent->i_children ; i++ )
544 if( p_parent == p_grandparent->pp_children[i] )
546 PL_DEBUG2( "parent %s found as child %i of grandparent %s",
547 p_parent->p_input->psz_name, i,
548 p_grandparent->p_input->psz_name );
553 if( b_found && i + 1 < p_grandparent->i_children )
555 return p_grandparent->pp_children[i+1];
557 /* Not found at root */
558 if( p_grandparent == p_root )
564 p_parent = p_grandparent;
565 p_grandparent = p_parent->p_parent;
569 /* We reached root */
573 playlist_item_t *GetPrevUncle( playlist_t *p_playlist, playlist_item_t *p_item,
574 playlist_item_t *p_root )
576 playlist_item_t *p_parent = p_item->p_parent;
577 playlist_item_t *p_grandparent;
578 vlc_bool_t b_found = VLC_FALSE;
582 if( p_parent != NULL )
584 p_grandparent = p_parent->p_parent;
588 for( i = p_grandparent->i_children -1 ; i >= 0; i-- )
590 if( p_parent == p_grandparent->pp_children[i] )
596 if( b_found && i - 1 > 0 )
598 return p_grandparent->pp_children[i-1];
600 /* Not found at root */
601 if( p_grandparent == p_root )
607 p_parent = p_grandparent;
608 p_grandparent = p_parent->p_parent;
612 /* We reached root */
617 /* Recursively search the tree for previous item */
618 playlist_item_t *GetPrevItem( playlist_t *p_playlist,
619 playlist_item_t *p_root,
620 playlist_item_t *p_item )
622 playlist_item_t *p_parent;
625 /* Node with children, get the last one */
626 if( p_item && p_item->i_children > 0 )
627 return p_item->pp_children[p_item->i_children - 1];
629 /* Last child of its parent ? */
631 p_parent = p_item->p_parent;
634 msg_Err( p_playlist, "Get the last one" );
638 for( i = p_parent->i_children -1 ; i >= 0 ; i-- )
640 if( p_parent->pp_children[i] == p_item )
644 /* Was already the first sibling. Look for uncles */
645 PL_DEBUG2( "current item is the first of its node,"
646 "looking for uncle from %s",
647 p_parent->p_input->psz_name );
648 if( p_parent == p_root )
650 PL_DEBUG2( "already at root" );
653 return GetPrevUncle( p_playlist, p_item, p_root );
657 return p_parent->pp_children[i-1];
664 /* Dump the contents of a node */
665 void playlist_NodeDump( playlist_t *p_playlist, playlist_item_t *p_item,
673 msg_Dbg( p_playlist, "%s (%i)",
674 p_item->p_input->psz_name, p_item->i_children );
677 if( p_item->i_children == -1 )
682 for( i = 0; i< p_item->i_children; i++ )
684 memset( str, 32, 512 );
685 sprintf( str + 2 * i_level , "%s (%i)",
686 p_item->pp_children[i]->p_input->psz_name,
687 p_item->pp_children[i]->i_children );
688 msg_Dbg( p_playlist, "%s",str );
689 if( p_item->pp_children[i]->i_children >= 0 )
691 playlist_NodeDump( p_playlist, p_item->pp_children[i],