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_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 * \return the new node
52 playlist_item_t * playlist_NodeCreate( playlist_t *p_playlist,
54 playlist_item_t *p_parent, int i_flags )
56 input_item_t *p_input;
57 playlist_item_t *p_item;
59 if( !psz_name ) psz_name = _("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;
66 if( p_item == NULL ) return NULL;
68 ARRAY_APPEND(p_playlist->all_items, p_item);
70 if( p_parent != NULL )
71 playlist_NodeAppend( p_playlist, p_item, p_parent );
72 playlist_SendAddNotify( p_playlist, p_item->i_id,
73 p_parent ? p_parent->i_id : -1,
74 !( i_flags & PLAYLIST_NO_REBUILD ));
79 * Remove all the children of a node
81 * This function must be entered with the playlist lock
83 * \param p_playlist the playlist
84 * \param p_root the node
85 * \param b_delete_items do we have to delete the children items ?
86 * \return VLC_SUCCESS or an error
88 int playlist_NodeEmpty( playlist_t *p_playlist, playlist_item_t *p_root,
89 vlc_bool_t b_delete_items )
92 if( p_root->i_children == -1 )
97 /* Delete the children */
98 for( i = p_root->i_children-1 ; i >= 0 ;i-- )
100 if( p_root->pp_children[i]->i_children > -1 )
102 playlist_NodeDelete( p_playlist, p_root->pp_children[i],
103 b_delete_items , VLC_FALSE );
105 else if( b_delete_items )
107 /* Delete the item here */
108 playlist_DeleteFromItemId( p_playlist,
109 p_root->pp_children[i]->i_id );
116 * Remove all the children of a node and removes the node
118 * \param p_playlist the playlist
119 * \param p_root the node
120 * \param b_delete_items do we have to delete the children items ?
121 * \return VLC_SUCCESS or an error
123 int playlist_NodeDelete( playlist_t *p_playlist, playlist_item_t *p_root,
124 vlc_bool_t b_delete_items, vlc_bool_t b_force )
128 if( p_root->i_children == -1 )
133 /* Delete the children */
134 for( i = p_root->i_children - 1 ; i >= 0; i-- )
136 if( p_root->pp_children[i]->i_children > -1 )
138 playlist_NodeDelete( p_playlist, p_root->pp_children[i],
139 b_delete_items , b_force );
141 else if( b_delete_items )
143 playlist_DeleteFromItemId( p_playlist,
144 p_root->pp_children[i]->i_id );
147 /* Delete the node */
148 if( p_root->i_flags & PLAYLIST_RO_FLAG && !b_force )
154 var_SetInteger( p_playlist, "item-deleted", p_root->i_id );
155 ARRAY_BSEARCH( p_playlist->all_items, ->p_input->i_id, int,
156 p_root->p_input->i_id, i );
158 ARRAY_REMOVE( p_playlist->all_items, i );
160 /* Remove the item from its parent */
161 if( p_root->p_parent )
162 playlist_NodeRemoveItem( p_playlist, p_root, p_root->p_parent );
164 playlist_ItemDelete( p_root );
171 * Adds an item to the children of a node
173 * \param p_playlist the playlist
174 * \param p_item the item to append
175 * \param p_parent the parent node
176 * \return VLC_SUCCESS or an error
178 int playlist_NodeAppend( playlist_t *p_playlist,
179 playlist_item_t *p_item,
180 playlist_item_t *p_parent )
182 return playlist_NodeInsert( p_playlist, p_item, p_parent, -1 );
185 int playlist_NodeInsert( playlist_t *p_playlist,
186 playlist_item_t *p_item,
187 playlist_item_t *p_parent,
190 assert( p_parent && p_parent->i_children != -1 );
191 if( i_position == -1 ) i_position = p_parent->i_children ;
193 INSERT_ELEM( p_parent->pp_children,
194 p_parent->i_children,
197 p_item->p_parent = p_parent;
202 * Deletes an item from the children of a node
204 * \param p_playlist the playlist
205 * \param p_item the item to remove
206 * \param p_parent the parent node
207 * \return VLC_SUCCESS or an error
209 int playlist_NodeRemoveItem( playlist_t *p_playlist,
210 playlist_item_t *p_item,
211 playlist_item_t *p_parent )
214 for( i= 0; i< p_parent->i_children ; i++ )
216 if( p_parent->pp_children[i] == p_item )
218 REMOVE_ELEM( p_parent->pp_children, p_parent->i_children, i );
227 * Count the children of a node
229 * \param p_playlist the playlist
230 * \param p_node the node
231 * \return the number of children
233 int playlist_NodeChildrenCount( playlist_t *p_playlist, playlist_item_t*p_node)
238 if( p_node->i_children == -1 )
241 i_nb = p_node->i_children;
242 for( i=0 ; i< p_node->i_children;i++ )
244 if( p_node->pp_children[i]->i_children == -1 )
247 i_nb += playlist_NodeChildrenCount( p_playlist,
248 p_node->pp_children[i] );
254 * Search a child of a node by its name
256 * \param p_node the node
257 * \param psz_search the name of the child to search
258 * \return the child item or NULL if not found or error
260 playlist_item_t *playlist_ChildSearchName( playlist_item_t *p_node,
261 const char *psz_search )
265 if( p_node->i_children < 0 )
269 for( i = 0 ; i< p_node->i_children; i++ )
271 if( !strcmp( p_node->pp_children[i]->p_input->psz_name, psz_search ) )
273 return p_node->pp_children[i];
280 * Create a pair of nodes in the category and onelevel trees.
281 * They share the same input ID.
282 * \todo really share the input item
283 * \param p_playlist the playlist
284 * \param psz_name the name of the nodes
285 * \param pp_node_cat pointer to return the node in category tree
286 * \param pp_node_one pointer to return the node in onelevel tree
287 * \param b_for_sd For Services Discovery ? (make node read-only and unskipping)
289 void playlist_NodesPairCreate( playlist_t *p_playlist, const char *psz_name,
290 playlist_item_t **pp_node_cat,
291 playlist_item_t **pp_node_one,
292 vlc_bool_t b_for_sd )
294 *pp_node_cat = playlist_NodeCreate( p_playlist, psz_name,
295 p_playlist->p_root_category, 0 );
296 *pp_node_one = playlist_NodeCreate( p_playlist, psz_name,
297 p_playlist->p_root_onelevel, 0 );
298 (*pp_node_one)->p_input->i_id = (*pp_node_cat)->p_input->i_id;
301 (*pp_node_cat)->i_flags |= PLAYLIST_RO_FLAG;
302 (*pp_node_cat)->i_flags |= PLAYLIST_SKIP_FLAG;
303 (*pp_node_one)->i_flags |= PLAYLIST_RO_FLAG;
304 (*pp_node_one)->i_flags |= PLAYLIST_SKIP_FLAG;
309 * Get the node in the preferred tree from a node in one of category
311 * For example, for the SAP node, it will return the node in the category
312 * tree if --playlist-tree is not set to never, because the SAP node prefers
315 playlist_item_t * playlist_GetPreferredNode( playlist_t *p_playlist,
316 playlist_item_t *p_node )
319 if( p_node->p_parent == p_playlist->p_root_category )
321 if( p_playlist->b_always_tree ||
322 p_node->p_input->b_prefers_tree ) return p_node;
323 for( i = 0 ; i< p_playlist->p_root_onelevel->i_children; i++ )
325 if( p_playlist->p_root_onelevel->pp_children[i]->p_input->i_id ==
326 p_node->p_input->i_id )
327 return p_playlist->p_root_onelevel->pp_children[i];
330 else if( p_node->p_parent == p_playlist->p_root_onelevel )
332 if( p_playlist->b_never_tree || !p_node->p_input->b_prefers_tree )
334 for( i = 0 ; i< p_playlist->p_root_category->i_children; i++ )
336 if( p_playlist->p_root_category->pp_children[i]->p_input->i_id ==
337 p_node->p_input->i_id )
338 return p_playlist->p_root_category->pp_children[i];
344 /**********************************************************************
345 * Tree walking functions
346 **********************************************************************/
348 playlist_item_t *playlist_GetLastLeaf(playlist_t *p_playlist,
349 playlist_item_t *p_root )
352 playlist_item_t *p_item;
353 for ( i = p_root->i_children - 1; i >= 0; i-- )
355 if( p_root->pp_children[i]->i_children == -1 )
356 return p_root->pp_children[i];
357 else if( p_root->pp_children[i]->i_children > 0)
359 p_item = playlist_GetLastLeaf( p_playlist,
360 p_root->pp_children[i] );
361 if ( p_item != NULL )
370 int playlist_GetAllEnabledChildren( playlist_t *p_playlist,
371 playlist_item_t *p_node,
372 playlist_item_t ***ppp_items )
375 playlist_item_t *p_next = NULL;
378 p_next = playlist_GetNextLeaf( p_playlist, p_node,
379 p_next, VLC_TRUE, VLC_FALSE );
381 INSERT_ELEM( *ppp_items, i_count, i_count, p_next );
389 * Finds the next item to play
391 * \param p_playlist the playlist
392 * \param p_root the root node
393 * \param p_item the previous item (NULL if none )
394 * \return the next item to play, or NULL if none found
396 playlist_item_t *playlist_GetNextLeaf( playlist_t *p_playlist,
397 playlist_item_t *p_root,
398 playlist_item_t *p_item,
399 vlc_bool_t b_ena, vlc_bool_t b_unplayed )
401 playlist_item_t *p_next;
403 assert( p_root && p_root->i_children != -1 );
405 PL_DEBUG2( "finding next of %s within %s",
406 PLI_NAME( p_item ), PLI_NAME( p_root ) );
408 /* Now, walk the tree until we find a suitable next item */
412 vlc_bool_t b_ena_ok = VLC_TRUE, b_unplayed_ok = VLC_TRUE;
413 p_next = GetNextItem( p_playlist, p_root, p_next );
414 if( !p_next || p_next == p_root )
416 if( p_next->i_children == -1 )
418 if( b_ena && p_next->i_flags & PLAYLIST_DBL_FLAG )
419 b_ena_ok = VLC_FALSE;
420 if( b_unplayed && p_next->p_input->i_nb_played != 0 )
421 b_unplayed_ok = VLC_FALSE;
422 if( b_ena_ok && b_unplayed_ok ) break;
425 if( p_next == NULL ) PL_DEBUG2( "at end of node" );
430 * Finds the previous item to play
432 * \param p_playlist the playlist
433 * \param p_root the root node
434 * \param p_item the previous item (NULL if none )
435 * \return the next item to play, or NULL if none found
437 playlist_item_t *playlist_GetPrevLeaf( playlist_t *p_playlist,
438 playlist_item_t *p_root,
439 playlist_item_t *p_item,
440 vlc_bool_t b_ena, vlc_bool_t b_unplayed )
442 playlist_item_t *p_prev;
444 PL_DEBUG2( "finding previous os %s within %s", PLI_NAME( p_item ),
445 PLI_NAME( p_root ) );
446 assert( p_root && p_root->i_children != -1 );
448 /* Now, walk the tree until we find a suitable previous item */
452 vlc_bool_t b_ena_ok = VLC_TRUE, b_unplayed_ok = VLC_TRUE;
453 p_prev = GetPrevItem( p_playlist, p_root, p_prev );
454 if( !p_prev || p_prev == p_root )
456 if( p_prev->i_children == -1 )
458 if( b_ena && p_prev->i_flags & PLAYLIST_DBL_FLAG )
459 b_ena_ok = VLC_FALSE;
460 if( b_unplayed && p_prev->p_input->i_nb_played != 0 )
461 b_unplayed_ok = VLC_FALSE;
462 if( b_ena_ok && b_unplayed_ok ) break;
465 if( p_prev == NULL ) PL_DEBUG2( "at beginning of node" );
469 /************************************************************************
470 * Following functions are local
471 ***********************************************************************/
474 * Get the next item in the tree
475 * If p_item is NULL, return the first child of root
477 playlist_item_t *GetNextItem( playlist_t *p_playlist,
478 playlist_item_t *p_root,
479 playlist_item_t *p_item )
481 playlist_item_t *p_parent;
484 /* Node with children, get the first one */
485 if( p_item && p_item->i_children > 0 )
486 return p_item->pp_children[0];
489 p_parent = p_item->p_parent;
492 for( i= 0 ; i < p_parent->i_children ; i++ )
494 if( p_item == NULL || p_parent->pp_children[i] == p_item )
499 if( i+1 >= p_parent->i_children )
501 /* Was already the last sibling. Look for uncles */
502 PL_DEBUG2( "Current item is the last of the node,"
503 "looking for uncle from %s",
504 p_parent->p_input->psz_name );
506 if( p_parent == p_root )
508 PL_DEBUG2( "already at root" );
511 return GetNextUncle( p_playlist, p_item, p_root );
515 return p_parent->pp_children[i+1];
522 playlist_item_t *GetNextUncle( playlist_t *p_playlist, playlist_item_t *p_item,
523 playlist_item_t *p_root )
525 playlist_item_t *p_parent = p_item->p_parent;
526 playlist_item_t *p_grandparent;
527 vlc_bool_t b_found = VLC_FALSE;
529 if( p_parent != NULL )
531 p_grandparent = p_parent->p_parent;
535 for( i = 0 ; i< p_grandparent->i_children ; i++ )
537 if( p_parent == p_grandparent->pp_children[i] )
539 PL_DEBUG2( "parent %s found as child %i of grandparent %s",
540 p_parent->p_input->psz_name, i,
541 p_grandparent->p_input->psz_name );
546 if( b_found && i + 1 < p_grandparent->i_children )
548 return p_grandparent->pp_children[i+1];
550 /* Not found at root */
551 if( p_grandparent == p_root )
557 p_parent = p_grandparent;
558 p_grandparent = p_parent->p_parent;
562 /* We reached root */
566 playlist_item_t *GetPrevUncle( playlist_t *p_playlist, playlist_item_t *p_item,
567 playlist_item_t *p_root )
569 playlist_item_t *p_parent = p_item->p_parent;
570 playlist_item_t *p_grandparent;
571 vlc_bool_t b_found = VLC_FALSE;
573 if( p_parent != NULL )
575 p_grandparent = p_parent->p_parent;
579 for( i = p_grandparent->i_children -1 ; i >= 0; i-- )
581 if( p_parent == p_grandparent->pp_children[i] )
587 if( b_found && i - 1 > 0 )
589 return p_grandparent->pp_children[i-1];
591 /* Not found at root */
592 if( p_grandparent == p_root )
598 p_parent = p_grandparent;
599 p_grandparent = p_parent->p_parent;
603 /* We reached root */
608 /* Recursively search the tree for previous item */
609 playlist_item_t *GetPrevItem( playlist_t *p_playlist,
610 playlist_item_t *p_root,
611 playlist_item_t *p_item )
613 playlist_item_t *p_parent;
616 /* Node with children, get the last one */
617 if( p_item && p_item->i_children > 0 )
618 return p_item->pp_children[p_item->i_children - 1];
620 /* Last child of its parent ? */
622 p_parent = p_item->p_parent;
625 msg_Err( p_playlist, "Get the last one" );
629 for( i = p_parent->i_children -1 ; i >= 0 ; i-- )
631 if( p_parent->pp_children[i] == p_item )
635 /* Was already the first sibling. Look for uncles */
636 PL_DEBUG2( "current item is the first of its node,"
637 "looking for uncle from %s",
638 p_parent->p_input->psz_name );
639 if( p_parent == p_root )
641 PL_DEBUG2( "already at root" );
644 return GetPrevUncle( p_playlist, p_item, p_root );
648 return p_parent->pp_children[i-1];
655 /* Dump the contents of a node */
656 void playlist_NodeDump( playlist_t *p_playlist, playlist_item_t *p_item,
664 msg_Dbg( p_playlist, "%s (%i)",
665 p_item->p_input->psz_name, p_item->i_children );
668 if( p_item->i_children == -1 )
673 for( i = 0; i< p_item->i_children; i++ )
675 memset( str, 32, 512 );
676 sprintf( str + 2 * i_level , "%s (%i)",
677 p_item->pp_children[i]->p_input->psz_name,
678 p_item->pp_children[i]->i_children );
679 msg_Dbg( p_playlist, "%s",str );
680 if( p_item->pp_children[i]->i_children >= 0 )
682 playlist_NodeDump( p_playlist, p_item->pp_children[i],