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 * \return the new node
51 playlist_item_t * playlist_NodeCreate( playlist_t *p_playlist, const 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;
64 if( p_item == NULL ) return NULL;
66 ARRAY_APPEND(p_playlist->all_items, p_item);
68 if( p_parent != NULL )
69 playlist_NodeAppend( p_playlist, p_item, p_parent );
70 playlist_SendAddNotify( p_playlist, p_item->i_id,
71 p_parent ? p_parent->i_id : -1 );
76 * Remove all the children of a node
78 * This function must be entered with the playlist lock
80 * \param p_playlist the playlist
81 * \param p_root the node
82 * \param b_delete_items do we have to delete the children items ?
83 * \return VLC_SUCCESS or an error
85 int playlist_NodeEmpty( playlist_t *p_playlist, playlist_item_t *p_root,
86 vlc_bool_t b_delete_items )
89 if( p_root->i_children == -1 )
94 /* Delete the children */
95 for( i = p_root->i_children-1 ; i >= 0 ;i-- )
97 if( p_root->pp_children[i]->i_children > -1 )
99 playlist_NodeDelete( p_playlist, p_root->pp_children[i],
100 b_delete_items , VLC_FALSE );
102 else if( b_delete_items )
104 /* Delete the item here */
105 playlist_DeleteFromItemId( p_playlist,
106 p_root->pp_children[i]->i_id );
113 * Remove all the children of a node and removes the node
115 * \param p_playlist the playlist
116 * \param p_root the node
117 * \param b_delete_items do we have to delete the children items ?
118 * \return VLC_SUCCESS or an error
120 int playlist_NodeDelete( playlist_t *p_playlist, playlist_item_t *p_root,
121 vlc_bool_t b_delete_items, vlc_bool_t b_force )
125 if( p_root->i_children == -1 )
130 /* Delete the children */
131 for( i = p_root->i_children - 1 ; i >= 0; i-- )
133 if( p_root->pp_children[i]->i_children > -1 )
135 playlist_NodeDelete( p_playlist, p_root->pp_children[i],
136 b_delete_items , b_force );
138 else if( b_delete_items )
140 playlist_DeleteFromItemId( p_playlist,
141 p_root->pp_children[i]->i_id );
144 /* Delete the node */
145 if( p_root->i_flags & PLAYLIST_RO_FLAG && !b_force )
151 var_SetInteger( p_playlist, "item-deleted", p_root->i_id );
152 ARRAY_BSEARCH( p_playlist->all_items, ->p_input->i_id, int,
153 p_root->p_input->i_id, i );
155 ARRAY_REMOVE( p_playlist->all_items, i );
157 /* Remove the item from its parent */
158 if( p_root->p_parent )
159 playlist_NodeRemoveItem( p_playlist, p_root, p_root->p_parent );
161 playlist_ItemDelete( p_root );
168 * Adds an item to the children of a node
170 * \param p_playlist the playlist
171 * \param p_item the item to append
172 * \param p_parent the parent node
173 * \return VLC_SUCCESS or an error
175 int playlist_NodeAppend( playlist_t *p_playlist,
176 playlist_item_t *p_item,
177 playlist_item_t *p_parent )
179 return playlist_NodeInsert( p_playlist, p_item, p_parent, -1 );
182 int playlist_NodeInsert( playlist_t *p_playlist,
183 playlist_item_t *p_item,
184 playlist_item_t *p_parent,
187 assert( p_parent && p_parent->i_children != -1 );
188 if( i_position == -1 ) i_position = p_parent->i_children ;
190 INSERT_ELEM( p_parent->pp_children,
191 p_parent->i_children,
194 p_item->p_parent = p_parent;
199 * Deletes an item from the children of a node
201 * \param p_playlist the playlist
202 * \param p_item the item to remove
203 * \param p_parent the parent node
204 * \return VLC_SUCCESS or an error
206 int playlist_NodeRemoveItem( playlist_t *p_playlist,
207 playlist_item_t *p_item,
208 playlist_item_t *p_parent )
211 for( i= 0; i< p_parent->i_children ; i++ )
213 if( p_parent->pp_children[i] == p_item )
215 REMOVE_ELEM( p_parent->pp_children, p_parent->i_children, i );
224 * Count the children of a node
226 * \param p_playlist the playlist
227 * \param p_node the node
228 * \return the number of children
230 int playlist_NodeChildrenCount( playlist_t *p_playlist, playlist_item_t*p_node)
235 if( p_node->i_children == -1 )
238 i_nb = p_node->i_children;
239 for( i=0 ; i< p_node->i_children;i++ )
241 if( p_node->pp_children[i]->i_children == -1 )
244 i_nb += playlist_NodeChildrenCount( p_playlist,
245 p_node->pp_children[i] );
251 * Search a child of a node by its name
253 * \param p_node the node
254 * \param psz_search the name of the child to search
255 * \return the child item or NULL if not found or error
257 playlist_item_t *playlist_ChildSearchName( playlist_item_t *p_node,
258 const char *psz_search )
262 if( p_node->i_children < 0 )
266 for( i = 0 ; i< p_node->i_children; i++ )
268 if( !strcmp( p_node->pp_children[i]->p_input->psz_name, psz_search ) )
270 return p_node->pp_children[i];
277 * Create a pair of nodes in the category and onelevel trees.
278 * They share the same input ID.
279 * \todo really share the input item
280 * \param p_playlist the playlist
281 * \param psz_name the name of the nodes
282 * \param pp_node_cat pointer to return the node in category tree
283 * \param pp_node_one pointer to return the node in onelevel tree
284 * \param b_for_sd For Services Discovery ? (make node read-only and unskipping)
286 void playlist_NodesPairCreate( playlist_t *p_playlist, const char *psz_name,
287 playlist_item_t **pp_node_cat,
288 playlist_item_t **pp_node_one,
289 vlc_bool_t b_for_sd )
291 *pp_node_cat = playlist_NodeCreate( p_playlist, psz_name,
292 p_playlist->p_root_category );
293 *pp_node_one = playlist_NodeCreate( p_playlist, psz_name,
294 p_playlist->p_root_onelevel );
295 (*pp_node_one)->p_input->i_id = (*pp_node_cat)->p_input->i_id;
298 (*pp_node_cat)->i_flags |= PLAYLIST_RO_FLAG;
299 (*pp_node_cat)->i_flags |= PLAYLIST_SKIP_FLAG;
300 (*pp_node_one)->i_flags |= PLAYLIST_RO_FLAG;
301 (*pp_node_one)->i_flags |= PLAYLIST_SKIP_FLAG;
306 * Get the node in the preferred tree from a node in one of category
308 * For example, for the SAP node, it will return the node in the category
309 * tree if --playlist-tree is not set to never, because the SAP node prefers
312 playlist_item_t * playlist_GetPreferredNode( playlist_t *p_playlist,
313 playlist_item_t *p_node )
316 if( p_node->p_parent == p_playlist->p_root_category )
318 if( p_playlist->b_always_tree ||
319 p_node->p_input->b_prefers_tree ) return p_node;
320 for( i = 0 ; i< p_playlist->p_root_onelevel->i_children; i++ )
322 if( p_playlist->p_root_onelevel->pp_children[i]->p_input->i_id ==
323 p_node->p_input->i_id )
324 return p_playlist->p_root_onelevel->pp_children[i];
327 else if( p_node->p_parent == p_playlist->p_root_onelevel )
329 if( p_playlist->b_never_tree || !p_node->p_input->b_prefers_tree )
331 for( i = 0 ; i< p_playlist->p_root_category->i_children; i++ )
333 if( p_playlist->p_root_category->pp_children[i]->p_input->i_id ==
334 p_node->p_input->i_id )
335 return p_playlist->p_root_category->pp_children[i];
341 /**********************************************************************
342 * Tree walking functions
343 **********************************************************************/
345 playlist_item_t *playlist_GetLastLeaf(playlist_t *p_playlist,
346 playlist_item_t *p_root )
349 playlist_item_t *p_item;
350 for ( i = p_root->i_children - 1; i >= 0; i-- )
352 if( p_root->pp_children[i]->i_children == -1 )
353 return p_root->pp_children[i];
354 else if( p_root->pp_children[i]->i_children > 0)
356 p_item = playlist_GetLastLeaf( p_playlist,
357 p_root->pp_children[i] );
358 if ( p_item != NULL )
367 int playlist_GetAllEnabledChildren( playlist_t *p_playlist,
368 playlist_item_t *p_node,
369 playlist_item_t ***ppp_items )
372 playlist_item_t *p_next = NULL;
375 p_next = playlist_GetNextLeaf( p_playlist, p_node,
376 p_next, VLC_TRUE, VLC_FALSE );
378 INSERT_ELEM( *ppp_items, i_count, i_count, p_next );
386 * Finds the next item to play
388 * \param p_playlist the playlist
389 * \param p_root the root node
390 * \param p_item the previous item (NULL if none )
391 * \return the next item to play, or NULL if none found
393 playlist_item_t *playlist_GetNextLeaf( playlist_t *p_playlist,
394 playlist_item_t *p_root,
395 playlist_item_t *p_item,
396 vlc_bool_t b_ena, vlc_bool_t b_unplayed )
398 playlist_item_t *p_next;
400 assert( p_root && p_root->i_children != -1 );
402 PL_DEBUG2( "finding next of %s within %s",
403 PLI_NAME( p_item ), PLI_NAME( p_root ) );
405 /* Now, walk the tree until we find a suitable next item */
409 vlc_bool_t b_ena_ok = VLC_TRUE, b_unplayed_ok = VLC_TRUE;
410 p_next = GetNextItem( p_playlist, p_root, p_next );
411 if( !p_next || p_next == p_root )
413 if( p_next->i_children == -1 )
415 if( b_ena && p_next->i_flags & PLAYLIST_DBL_FLAG )
416 b_ena_ok = VLC_FALSE;
417 if( b_unplayed && p_next->p_input->i_nb_played != 0 )
418 b_unplayed_ok = VLC_FALSE;
419 if( b_ena_ok && b_unplayed_ok ) break;
422 if( p_next == NULL ) PL_DEBUG2( "at end of node" );
427 * Finds the previous item to play
429 * \param p_playlist the playlist
430 * \param p_root the root node
431 * \param p_item the previous item (NULL if none )
432 * \return the next item to play, or NULL if none found
434 playlist_item_t *playlist_GetPrevLeaf( playlist_t *p_playlist,
435 playlist_item_t *p_root,
436 playlist_item_t *p_item,
437 vlc_bool_t b_ena, vlc_bool_t b_unplayed )
439 playlist_item_t *p_prev;
441 PL_DEBUG2( "finding previous os %s within %s", PLI_NAME( p_item ),
442 PLI_NAME( p_root ) );
443 assert( p_root && p_root->i_children != -1 );
445 /* Now, walk the tree until we find a suitable previous item */
449 vlc_bool_t b_ena_ok = VLC_TRUE, b_unplayed_ok = VLC_TRUE;
450 p_prev = GetPrevItem( p_playlist, p_root, p_prev );
451 if( !p_prev || p_prev == p_root )
453 if( p_prev->i_children == -1 )
455 if( b_ena && p_prev->i_flags & PLAYLIST_DBL_FLAG )
456 b_ena_ok = VLC_FALSE;
457 if( b_unplayed && p_prev->p_input->i_nb_played != 0 )
458 b_unplayed_ok = VLC_FALSE;
459 if( b_ena_ok && b_unplayed_ok ) break;
462 if( p_prev == NULL ) PL_DEBUG2( "at beginning of node" );
466 /************************************************************************
467 * Following functions are local
468 ***********************************************************************/
471 * Get the next item in the tree
472 * If p_item is NULL, return the first child of root
474 playlist_item_t *GetNextItem( playlist_t *p_playlist,
475 playlist_item_t *p_root,
476 playlist_item_t *p_item )
478 playlist_item_t *p_parent;
481 /* Node with children, get the first one */
482 if( p_item && p_item->i_children > 0 )
483 return p_item->pp_children[0];
486 p_parent = p_item->p_parent;
489 for( i= 0 ; i < p_parent->i_children ; i++ )
491 if( p_item == NULL || p_parent->pp_children[i] == p_item )
496 if( i+1 >= p_parent->i_children )
498 /* Was already the last sibling. Look for uncles */
499 PL_DEBUG2( "Current item is the last of the node,"
500 "looking for uncle from %s",
501 p_parent->p_input->psz_name );
503 if( p_parent == p_root )
505 PL_DEBUG2( "already at root" );
508 return GetNextUncle( p_playlist, p_item, p_root );
512 return p_parent->pp_children[i+1];
519 playlist_item_t *GetNextUncle( playlist_t *p_playlist, playlist_item_t *p_item,
520 playlist_item_t *p_root )
522 playlist_item_t *p_parent = p_item->p_parent;
523 playlist_item_t *p_grandparent;
524 vlc_bool_t b_found = VLC_FALSE;
526 if( p_parent != NULL )
528 p_grandparent = p_parent->p_parent;
532 for( i = 0 ; i< p_grandparent->i_children ; i++ )
534 if( p_parent == p_grandparent->pp_children[i] )
536 PL_DEBUG2( "parent %s found as child %i of grandparent %s",
537 p_parent->p_input->psz_name, i,
538 p_grandparent->p_input->psz_name );
543 if( b_found && i + 1 < p_grandparent->i_children )
545 return p_grandparent->pp_children[i+1];
547 /* Not found at root */
548 if( p_grandparent == p_root )
554 p_parent = p_grandparent;
555 p_grandparent = p_parent->p_parent;
559 /* We reached root */
563 playlist_item_t *GetPrevUncle( playlist_t *p_playlist, playlist_item_t *p_item,
564 playlist_item_t *p_root )
566 playlist_item_t *p_parent = p_item->p_parent;
567 playlist_item_t *p_grandparent;
568 vlc_bool_t b_found = VLC_FALSE;
570 if( p_parent != NULL )
572 p_grandparent = p_parent->p_parent;
576 for( i = p_grandparent->i_children -1 ; i >= 0; i-- )
578 if( p_parent == p_grandparent->pp_children[i] )
584 if( b_found && i - 1 > 0 )
586 return p_grandparent->pp_children[i-1];
588 /* Not found at root */
589 if( p_grandparent == p_root )
595 p_parent = p_grandparent;
596 p_grandparent = p_parent->p_parent;
600 /* We reached root */
605 /* Recursively search the tree for previous item */
606 playlist_item_t *GetPrevItem( playlist_t *p_playlist,
607 playlist_item_t *p_root,
608 playlist_item_t *p_item )
610 playlist_item_t *p_parent;
613 /* Node with children, get the last one */
614 if( p_item && p_item->i_children > 0 )
615 return p_item->pp_children[p_item->i_children - 1];
617 /* Last child of its parent ? */
619 p_parent = p_item->p_parent;
622 msg_Err( p_playlist, "Get the last one" );
626 for( i = p_parent->i_children -1 ; i >= 0 ; i-- )
628 if( p_parent->pp_children[i] == p_item )
632 /* Was already the first sibling. Look for uncles */
633 PL_DEBUG2( "current item is the first of its node,"
634 "looking for uncle from %s",
635 p_parent->p_input->psz_name );
636 if( p_parent == p_root )
638 PL_DEBUG2( "already at root" );
641 return GetPrevUncle( p_playlist, p_item, p_root );
645 return p_parent->pp_children[i-1];
652 /* Dump the contents of a node */
653 void playlist_NodeDump( playlist_t *p_playlist, playlist_item_t *p_item,
661 msg_Dbg( p_playlist, "%s (%i)",
662 p_item->p_input->psz_name, p_item->i_children );
665 if( p_item->i_children == -1 )
670 for( i = 0; i< p_item->i_children; i++ )
672 memset( str, 32, 512 );
673 sprintf( str + 2 * i_level , "%s (%i)",
674 p_item->pp_children[i]->p_input->psz_name,
675 p_item->pp_children[i]->i_children );
676 msg_Dbg( p_playlist, "%s",str );
677 if( p_item->pp_children[i]->i_children >= 0 )
679 playlist_NodeDump( p_playlist, p_item->pp_children[i],