]> git.sesse.net Git - vlc/blob - src/playlist/tree.c
* Do not take and release the structure lock for each element of the list while
[vlc] / src / playlist / tree.c
1 /*****************************************************************************
2  * tree.c : Playlist tree walking functions
3  *****************************************************************************
4  * Copyright (C) 1999-2004 the VideoLAN team
5  * $Id$
6  *
7  * Authors: ClĂ©ment Stenac <zorglub@videolan.org>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23 #include <vlc/vlc.h>
24 #include <assert.h>
25 #include "vlc_playlist.h"
26 #include "playlist_internal.h"
27
28 /************************************************************************
29  * Local prototypes
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 );
35
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 );
42
43 /**
44  * Create a playlist node
45  *
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
51  */
52 playlist_item_t * playlist_NodeCreate( playlist_t *p_playlist,
53                                        const char *psz_name,
54                                        playlist_item_t *p_parent, int i_flags )
55 {
56     input_item_t *p_input;
57     playlist_item_t *p_item;
58
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;
65
66     if( p_item == NULL )  return NULL;
67
68     ARRAY_APPEND(p_playlist->all_items, p_item);
69
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 ));
75     return p_item;
76 }
77
78 /**
79  * Remove all the children of a node
80  *
81  * This function must be entered with the playlist lock
82  *
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
87  */
88 int playlist_NodeEmpty( playlist_t *p_playlist, playlist_item_t *p_root,
89                         vlc_bool_t b_delete_items )
90 {
91     int i;
92     if( p_root->i_children == -1 )
93     {
94         return VLC_EGENERIC;
95     }
96
97     /* Delete the children */
98     for( i =  p_root->i_children-1 ; i >= 0 ;i-- )
99     {
100         if( p_root->pp_children[i]->i_children > -1 )
101         {
102             playlist_NodeDelete( p_playlist, p_root->pp_children[i],
103                                  b_delete_items , VLC_FALSE );
104         }
105         else if( b_delete_items )
106         {
107             /* Delete the item here */
108             playlist_DeleteFromItemId( p_playlist,
109                                        p_root->pp_children[i]->i_id );
110         }
111     }
112     return VLC_SUCCESS;
113 }
114
115 /**
116  * Remove all the children of a node and removes the node
117  *
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
122  */
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 )
125 {
126     int i;
127
128     if( p_root->i_children == -1 )
129     {
130         return VLC_EGENERIC;
131     }
132
133     /* Delete the children */
134     for( i =  p_root->i_children - 1 ; i >= 0; i-- )
135     {
136         if( p_root->pp_children[i]->i_children > -1 )
137         {
138             playlist_NodeDelete( p_playlist, p_root->pp_children[i],
139                                  b_delete_items , b_force );
140         }
141         else if( b_delete_items )
142         {
143             playlist_DeleteFromItemId( p_playlist,
144                                        p_root->pp_children[i]->i_id );
145         }
146     }
147     /* Delete the node */
148     if( p_root->i_flags & PLAYLIST_RO_FLAG && !b_force )
149     {
150     }
151     else
152     {
153         int i;
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 );
157         if( i != -1 )
158             ARRAY_REMOVE( p_playlist->all_items, i );
159
160         /* Remove the item from its parent */
161         if( p_root->p_parent )
162             playlist_NodeRemoveItem( p_playlist, p_root, p_root->p_parent );
163
164         playlist_ItemDelete( p_root );
165     }
166     return VLC_SUCCESS;
167 }
168
169
170 /**
171  * Adds an item to the children of a node
172  *
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
177  */
178 int playlist_NodeAppend( playlist_t *p_playlist,
179                          playlist_item_t *p_item,
180                          playlist_item_t *p_parent )
181 {
182     return playlist_NodeInsert( p_playlist, p_item, p_parent, -1 );
183 }
184
185 int playlist_NodeInsert( playlist_t *p_playlist,
186                          playlist_item_t *p_item,
187                          playlist_item_t *p_parent,
188                          int i_position )
189 {
190    assert( p_parent && p_parent->i_children != -1 );
191    if( i_position == -1 ) i_position = p_parent->i_children ;
192
193    INSERT_ELEM( p_parent->pp_children,
194                 p_parent->i_children,
195                 i_position,
196                 p_item );
197    p_item->p_parent = p_parent;
198    return VLC_SUCCESS;
199 }
200
201 /**
202  * Deletes an item from the children of a node
203  *
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
208  */
209 int playlist_NodeRemoveItem( playlist_t *p_playlist,
210                         playlist_item_t *p_item,
211                         playlist_item_t *p_parent )
212 {
213    int i;
214    for( i= 0; i< p_parent->i_children ; i++ )
215    {
216        if( p_parent->pp_children[i] == p_item )
217        {
218            REMOVE_ELEM( p_parent->pp_children, p_parent->i_children, i );
219        }
220    }
221
222    return VLC_SUCCESS;
223 }
224
225
226 /**
227  * Count the children of a node
228  *
229  * \param p_playlist the playlist
230  * \param p_node the node
231  * \return the number of children
232  */
233 int playlist_NodeChildrenCount( playlist_t *p_playlist, playlist_item_t*p_node)
234 {
235     int i;
236     int i_nb = 0;
237
238     if( p_node->i_children == -1 )
239         return 0;
240
241     i_nb = p_node->i_children;
242     for( i=0 ; i< p_node->i_children;i++ )
243     {
244         if( p_node->pp_children[i]->i_children == -1 )
245             break;
246         else
247             i_nb += playlist_NodeChildrenCount( p_playlist,
248                                                 p_node->pp_children[i] );
249     }
250     return i_nb;
251 }
252
253 /**
254  * Search a child of a node by its name
255  *
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
259  */
260 playlist_item_t *playlist_ChildSearchName( playlist_item_t *p_node,
261                                            const char *psz_search )
262 {
263     int i;
264
265     if( p_node->i_children < 0 )
266     {
267          return NULL;
268     }
269     for( i = 0 ; i< p_node->i_children; i++ )
270     {
271         if( !strcmp( p_node->pp_children[i]->p_input->psz_name, psz_search ) )
272         {
273             return p_node->pp_children[i];
274         }
275     }
276     return NULL;
277 }
278
279 /**
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)
288  */
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 )
293 {
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;
299     if( b_for_sd )
300     {
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;
305     }
306 }
307
308 /**
309  * Get the node in the preferred tree from a node in one of category
310  * or onelevel tree. 
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
313  * category
314  */
315 playlist_item_t * playlist_GetPreferredNode( playlist_t *p_playlist,
316                                              playlist_item_t *p_node )
317 {
318     int i;
319     if( p_node->p_parent == p_playlist->p_root_category )
320     {
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++ )
324         {
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];
328         }
329     }
330     else if( p_node->p_parent == p_playlist->p_root_onelevel )
331     {
332         if( p_playlist->b_never_tree || !p_node->p_input->b_prefers_tree )
333             return p_node;
334         for( i = 0 ; i< p_playlist->p_root_category->i_children; i++ )
335         {
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];
339         }
340     }
341     return NULL;
342 }
343
344 /**********************************************************************
345  * Tree walking functions
346  **********************************************************************/
347
348 playlist_item_t *playlist_GetLastLeaf(playlist_t *p_playlist,
349                                       playlist_item_t *p_root )
350 {
351     int i;
352     playlist_item_t *p_item;
353     for ( i = p_root->i_children - 1; i >= 0; i-- )
354     {
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)
358         {
359              p_item = playlist_GetLastLeaf( p_playlist,
360                                             p_root->pp_children[i] );
361             if ( p_item != NULL )
362                 return p_item;
363         }
364         else if( i == 0 )
365             return NULL;
366     }
367     return NULL;
368 }
369
370 int playlist_GetAllEnabledChildren( playlist_t *p_playlist,
371                                     playlist_item_t *p_node,
372                                     playlist_item_t ***ppp_items )
373 {
374     int i_count = 0;
375     playlist_item_t *p_next = NULL;
376     while( 1 )
377     {
378         p_next = playlist_GetNextLeaf( p_playlist, p_node,
379                                        p_next, VLC_TRUE, VLC_FALSE );
380         if( p_next )
381             INSERT_ELEM( *ppp_items, i_count, i_count, p_next );
382         else
383             break;
384     }
385     return i_count;
386 }
387
388 /**
389  * Finds the next item to play
390  *
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
395  */
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 )
400 {
401     playlist_item_t *p_next;
402
403     assert( p_root && p_root->i_children != -1 );
404
405     PL_DEBUG2( "finding next of %s within %s",
406                PLI_NAME( p_item ), PLI_NAME( p_root ) );
407
408     /* Now, walk the tree until we find a suitable next item */
409     p_next = p_item;
410     while( 1 )
411     {
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 )
415             break;
416         if( p_next->i_children == -1 )
417         {
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;
423         }
424     }
425     if( p_next == NULL ) PL_DEBUG2( "at end of node" );
426     return p_next;
427 }
428
429 /**
430  * Finds the previous item to play
431  *
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
436  */
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 )
441 {
442     playlist_item_t *p_prev;
443
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 );
447
448     /* Now, walk the tree until we find a suitable previous item */
449     p_prev = p_item;
450     while( 1 )
451     {
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 )
455             break;
456         if( p_prev->i_children == -1 )
457         {
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;
463         }
464     }
465     if( p_prev == NULL ) PL_DEBUG2( "at beginning of node" );
466     return p_prev;
467 }
468
469 /************************************************************************
470  * Following functions are local
471  ***********************************************************************/
472
473 /**
474  * Get the next item in the tree
475  * If p_item is NULL, return the first child of root
476  **/
477 playlist_item_t *GetNextItem( playlist_t *p_playlist,
478                               playlist_item_t *p_root,
479                               playlist_item_t *p_item )
480 {
481     playlist_item_t *p_parent;
482     int i;
483
484     /* Node with children, get the first one */
485     if( p_item && p_item->i_children > 0 )
486         return p_item->pp_children[0];
487
488     if( p_item != NULL )
489         p_parent = p_item->p_parent;
490     else
491         p_parent = p_root;
492     for( i= 0 ; i < p_parent->i_children ; i++ )
493     {
494         if( p_item == NULL || p_parent->pp_children[i] == p_item )
495         {
496             if( p_item == NULL )
497                 i = -1;
498
499             if( i+1 >= p_parent->i_children )
500             {
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 );
505
506                 if( p_parent == p_root )
507                 {
508                     PL_DEBUG2( "already at root" );
509                     return NULL;
510                 }
511                 return GetNextUncle( p_playlist, p_item, p_root );
512             }
513             else
514             {
515                 return  p_parent->pp_children[i+1];
516             }
517         }
518     }
519     return NULL;
520 }
521
522 playlist_item_t *GetNextUncle( playlist_t *p_playlist, playlist_item_t *p_item,
523                                playlist_item_t *p_root )
524 {
525     playlist_item_t *p_parent = p_item->p_parent;
526     playlist_item_t *p_grandparent;
527     vlc_bool_t b_found = VLC_FALSE;
528
529     if( p_parent != NULL )
530     {
531         p_grandparent = p_parent->p_parent;
532         while( 1 )
533         {
534             int i;
535             for( i = 0 ; i< p_grandparent->i_children ; i++ )
536             {
537                 if( p_parent == p_grandparent->pp_children[i] )
538                 {
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 );
542                     b_found = VLC_TRUE;
543                     break;
544                 }
545             }
546             if( b_found && i + 1 < p_grandparent->i_children )
547             {
548                     return p_grandparent->pp_children[i+1];
549             }
550             /* Not found at root */
551             if( p_grandparent == p_root )
552             {
553                 return NULL;
554             }
555             else
556             {
557                 p_parent = p_grandparent;
558                 p_grandparent = p_parent->p_parent;
559             }
560         }
561     }
562     /* We reached root */
563     return NULL;
564 }
565
566 playlist_item_t *GetPrevUncle( playlist_t *p_playlist, playlist_item_t *p_item,
567                                playlist_item_t *p_root )
568 {
569     playlist_item_t *p_parent = p_item->p_parent;
570     playlist_item_t *p_grandparent;
571     vlc_bool_t b_found = VLC_FALSE;
572
573     if( p_parent != NULL )
574     {
575         p_grandparent = p_parent->p_parent;
576         while( 1 )
577         {
578             int i;
579             for( i = p_grandparent->i_children -1 ; i >= 0; i-- )
580             {
581                 if( p_parent == p_grandparent->pp_children[i] )
582                 {
583                     b_found = VLC_TRUE;
584                     break;
585                 }
586             }
587             if( b_found && i - 1 > 0 )
588             {
589                 return p_grandparent->pp_children[i-1];
590             }
591             /* Not found at root */
592             if( p_grandparent == p_root )
593             {
594                 return NULL;
595             }
596             else
597             {
598                 p_parent = p_grandparent;
599                 p_grandparent = p_parent->p_parent;
600             }
601         }
602     }
603     /* We reached root */
604     return NULL;
605 }
606
607
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 )
612 {
613     playlist_item_t *p_parent;
614     int i;
615
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];
619
620     /* Last child of its parent ? */
621     if( p_item != NULL )
622         p_parent = p_item->p_parent;
623     else
624     {
625         msg_Err( p_playlist, "Get the last one" );
626         abort();
627     };
628
629     for( i = p_parent->i_children -1 ; i >= 0 ;  i-- )
630     {
631         if( p_parent->pp_children[i] == p_item )
632         {
633             if( i-1 < 0 )
634             {
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 )
640                 {
641                     PL_DEBUG2( "already at root" );
642                     return NULL;
643                 }
644                 return GetPrevUncle( p_playlist, p_item, p_root );
645             }
646             else
647             {
648                 return p_parent->pp_children[i-1];
649             }
650         }
651     }
652     return NULL;
653 }
654
655 /* Dump the contents of a node */
656 void playlist_NodeDump( playlist_t *p_playlist, playlist_item_t *p_item,
657                         int i_level )
658 {
659     char str[512];
660     int i;
661
662     if( i_level == 1 )
663     {
664         msg_Dbg( p_playlist, "%s (%i)",
665                         p_item->p_input->psz_name, p_item->i_children );
666     }
667
668     if( p_item->i_children == -1 )
669     {
670         return;
671     }
672
673     for( i = 0; i< p_item->i_children; i++ )
674     {
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 )
681         {
682             playlist_NodeDump( p_playlist, p_item->pp_children[i],
683                               i_level + 1 );
684         }
685     }
686     return;
687 }