* src/misc/objects.c: implemented the FIND_CHILD mode for vlc_list_find().
[vlc.git] / src / misc / objects.c
1 /*****************************************************************************
2  * objects.c: vlc_object_t handling
3  *****************************************************************************
4  * Copyright (C) 2002 VideoLAN
5  * $Id: objects.c,v 1.33 2002/12/14 19:34:06 gbazin Exp $
6  *
7  * Authors: Samuel Hocevar <sam@zoy.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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <vlc/vlc.h>
28
29 #ifdef HAVE_STDLIB_H
30 #   include <stdlib.h>                                          /* realloc() */
31 #endif
32
33 #include "stream_control.h"
34 #include "input_ext-intf.h"
35 #include "input_ext-dec.h"
36
37 #include "video.h"
38 #include "video_output.h"
39
40 #include "audio_output.h"
41 #include "aout_internal.h"
42 #include "stream_output.h"
43
44 #include "vlc_playlist.h"
45 #include "interface.h"
46
47 /*****************************************************************************
48  * Local prototypes
49  *****************************************************************************/
50 static int  DumpCommand( vlc_object_t *, char const *,
51                          vlc_value_t, vlc_value_t, void * );
52
53 static vlc_object_t * FindObject    ( vlc_object_t *, int, int );
54 static void           DetachObject  ( vlc_object_t * );
55 static void           PrintObject   ( vlc_object_t *, const char * );
56 static void           DumpStructure ( vlc_object_t *, int, char * );
57 static int            FindIndex     ( vlc_object_t *, vlc_object_t **, int );
58 static void           SetAttachment ( vlc_object_t *, vlc_bool_t );
59
60 static vlc_list_t     NewList       ( int );
61 static void           ListReplace   ( vlc_list_t *, vlc_object_t *, int );
62 static void           ListAppend    ( vlc_list_t *, vlc_object_t * );
63 static int            CountChildren ( vlc_object_t *, int );
64 static void           ListChildren  ( vlc_list_t *, vlc_object_t *, int );
65
66 /*****************************************************************************
67  * Local structure lock
68  *****************************************************************************/
69 static vlc_mutex_t    structure_lock;
70
71 /*****************************************************************************
72  * vlc_object_create: initialize a vlc object
73  *****************************************************************************
74  * This function allocates memory for a vlc object and initializes it. If
75  * i_type is not a known value such as VLC_OBJECT_ROOT, VLC_OBJECT_VOUT and
76  * so on, vlc_object_create will use its value for the object size.
77  *****************************************************************************/
78 void * __vlc_object_create( vlc_object_t *p_this, int i_type )
79 {
80     vlc_object_t * p_new;
81     char *         psz_type;
82     size_t         i_size;
83
84     switch( i_type )
85     {
86         case VLC_OBJECT_ROOT:
87             i_size = sizeof(libvlc_t);
88             psz_type = "root";
89             break;
90         case VLC_OBJECT_VLC:
91             i_size = sizeof(vlc_t);
92             psz_type = "vlc";
93             break;
94         case VLC_OBJECT_MODULE:
95             i_size = sizeof(module_t);
96             psz_type = "module";
97             break;
98         case VLC_OBJECT_INTF:
99             i_size = sizeof(intf_thread_t);
100             psz_type = "interface";
101             break;
102         case VLC_OBJECT_PLAYLIST:
103             i_size = sizeof(playlist_t);
104             psz_type = "playlist";
105             break;
106         case VLC_OBJECT_INPUT:
107             i_size = sizeof(input_thread_t);
108             psz_type = "input";
109             break;
110         case VLC_OBJECT_DECODER:
111             i_size = sizeof(decoder_fifo_t);
112             psz_type = "decoder";
113             break;
114         case VLC_OBJECT_VOUT:
115             i_size = sizeof(vout_thread_t);
116             psz_type = "video output";
117             break;
118         case VLC_OBJECT_AOUT:
119             i_size = sizeof(aout_instance_t);
120             psz_type = "audio output";
121             break;
122         case VLC_OBJECT_SOUT:
123             i_size = sizeof(sout_instance_t);
124             psz_type = "stream output";
125             break;
126         default:
127             i_size = i_type > 0
128                       ? i_type > (int)sizeof(vlc_object_t)
129                          ? i_type : (int)sizeof(vlc_object_t)
130                       : (int)sizeof(vlc_object_t);
131             i_type = VLC_OBJECT_GENERIC;
132             psz_type = "generic";
133             break;
134     }
135
136     if( i_type == VLC_OBJECT_ROOT )
137     {
138         p_new = p_this;
139     }
140     else
141     {
142         p_new = malloc( i_size );
143
144         if( !p_new )
145         {
146             return NULL;
147         }
148
149         memset( p_new, 0, i_size );
150     }
151
152     p_new->i_object_type = i_type;
153     p_new->psz_object_type = psz_type;
154
155     p_new->psz_object_name = NULL;
156
157     p_new->b_die = VLC_FALSE;
158     p_new->b_error = VLC_FALSE;
159     p_new->b_dead = VLC_FALSE;
160     p_new->b_attached = VLC_FALSE;
161
162     p_new->i_vars = 0;
163     p_new->p_vars = (variable_t *)malloc( 16 * sizeof( variable_t ) );
164
165     if( !p_new->p_vars )
166     {
167         free( p_new );
168         return NULL;
169     }
170
171     if( i_type == VLC_OBJECT_ROOT )
172     {
173         /* If i_type is root, then p_new is actually p_libvlc */
174         p_new->p_libvlc = (libvlc_t*)p_new;
175         p_new->p_vlc = NULL;
176
177         p_new->p_libvlc->i_counter = 0;
178         p_new->i_object_id = 0;
179
180         p_new->p_libvlc->i_objects = 1;
181         p_new->p_libvlc->pp_objects = malloc( sizeof(vlc_object_t *) );
182         p_new->p_libvlc->pp_objects[0] = p_new;
183         p_new->b_attached = VLC_TRUE;
184     }
185     else
186     {
187         p_new->p_libvlc = p_this->p_libvlc;
188         p_new->p_vlc = ( i_type == VLC_OBJECT_VLC ) ? (vlc_t*)p_new
189                                                     : p_this->p_vlc;
190
191         vlc_mutex_lock( &structure_lock );
192
193         p_new->p_libvlc->i_counter++;
194         p_new->i_object_id = p_new->p_libvlc->i_counter;
195
196         /* Wooohaa! If *this* fails, we're in serious trouble! Anyway it's
197          * useless to try and recover anything if pp_objects gets smashed. */
198         INSERT_ELEM( p_new->p_libvlc->pp_objects,
199                      p_new->p_libvlc->i_objects,
200                      p_new->p_libvlc->i_objects,
201                      p_new );
202
203         vlc_mutex_unlock( &structure_lock );
204     }
205
206     p_new->i_refcount = 0;
207     p_new->p_parent = NULL;
208     p_new->pp_children = NULL;
209     p_new->i_children = 0;
210
211     p_new->p_private = NULL;
212
213     /* Initialize mutexes and condvars */
214     vlc_mutex_init( p_new, &p_new->object_lock );
215     vlc_cond_init( p_new, &p_new->object_wait );
216     vlc_mutex_init( p_new, &p_new->var_lock );
217
218     if( i_type == VLC_OBJECT_ROOT )
219     {
220         vlc_mutex_init( p_new, &structure_lock );
221
222         var_Create( p_new, "list", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
223         var_AddCallback( p_new, "list", DumpCommand, NULL );
224         var_Create( p_new, "tree", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
225         var_AddCallback( p_new, "tree", DumpCommand, NULL );
226     }
227
228     return p_new;
229 }
230
231 /*****************************************************************************
232  * vlc_object_destroy: destroy a vlc object
233  *****************************************************************************
234  * This function destroys an object that has been previously allocated with
235  * vlc_object_create. The object's refcount must be zero and it must not be
236  * attached to other objects in any way.
237  *****************************************************************************/
238 void __vlc_object_destroy( vlc_object_t *p_this )
239 {
240     int i_delay = 0;
241
242     if( p_this->i_children )
243     {
244         msg_Err( p_this, "cannot delete object with children" );
245         return;
246     }
247
248     if( p_this->p_parent )
249     {
250         msg_Err( p_this, "cannot delete object with a parent" );
251         return;
252     }
253
254     while( p_this->i_refcount )
255     {
256         i_delay++;
257
258         /* Don't warn immediately ... 100ms seems OK */
259         if( i_delay == 2 )
260         {
261             msg_Warn( p_this, "refcount is %i, delaying before deletion",
262                               p_this->i_refcount );
263         }
264         else if( i_delay == 12 )
265         {
266             msg_Err( p_this, "refcount is %i, I have a bad feeling about this",
267                              p_this->i_refcount );
268         }
269         else if( i_delay == 42 )
270         {
271             msg_Err( p_this, "we waited too long, cancelling destruction" );
272             return;
273         }
274
275         msleep( 100000 );
276     }
277
278     /* Destroy the associated variables, starting from the end so that
279      * no memmove calls have to be done. */
280     while( p_this->i_vars )
281     {
282         var_Destroy( p_this, p_this->p_vars[p_this->i_vars - 1].psz_name );
283     }
284
285     free( p_this->p_vars );
286     vlc_mutex_destroy( &p_this->var_lock );
287
288     if( p_this->i_object_type == VLC_OBJECT_ROOT )
289     {
290         /* We are the root object ... no need to lock. */
291         free( p_this->p_libvlc->pp_objects );
292         p_this->p_libvlc->pp_objects = NULL;
293         p_this->p_libvlc->i_objects--;
294
295         vlc_mutex_destroy( &structure_lock );
296     }
297     else
298     {
299         int i_index;
300
301         vlc_mutex_lock( &structure_lock );
302
303         /* Wooohaa! If *this* fails, we're in serious trouble! Anyway it's
304          * useless to try and recover anything if pp_objects gets smashed. */
305         i_index = FindIndex( p_this, p_this->p_libvlc->pp_objects,
306                              p_this->p_libvlc->i_objects );
307         REMOVE_ELEM( p_this->p_libvlc->pp_objects,
308                      p_this->p_libvlc->i_objects, i_index );
309
310         vlc_mutex_unlock( &structure_lock );
311     }
312
313     vlc_mutex_destroy( &p_this->object_lock );
314     vlc_cond_destroy( &p_this->object_wait );
315
316     free( p_this );
317 }
318
319 /*****************************************************************************
320  * vlc_object_get: find an object given its ID
321  *****************************************************************************
322  * This function looks for the object whose i_object_id field is i_id. We
323  * use a dichotomy so that lookups are in log2(n).
324  *****************************************************************************/
325 void * __vlc_object_get( vlc_object_t *p_this, int i_id )
326 {
327     int i_max, i_middle;
328     vlc_object_t **pp_objects;
329
330     vlc_mutex_lock( &structure_lock );
331
332     pp_objects = p_this->p_libvlc->pp_objects;
333
334     /* Perform our dichotomy */
335     for( i_max = p_this->p_libvlc->i_objects - 1 ; ; )
336     {
337         i_middle = i_max / 2;
338
339         if( pp_objects[i_middle]->i_object_id > i_id )
340         {
341             i_max = i_middle;
342         }
343         else if( pp_objects[i_middle]->i_object_id < i_id )
344         {
345             if( i_middle )
346             {
347                 pp_objects += i_middle;
348                 i_max -= i_middle;
349             }
350             else
351             {
352                 /* This happens when there are only two remaining objects */
353                 if( pp_objects[i_middle+1]->i_object_id == i_id )
354                 {
355                     vlc_mutex_unlock( &structure_lock );
356                     pp_objects[i_middle+1]->i_refcount++;
357                     return pp_objects[i_middle+1];
358                 }
359                 break;
360             }
361         }
362         else
363         {
364             vlc_mutex_unlock( &structure_lock );
365             pp_objects[i_middle]->i_refcount++;
366             return pp_objects[i_middle];
367         }
368
369         if( i_max == 0 )
370         {
371             /* this means that i_max == i_middle, and since we have already
372              * tested pp_objects[i_middle]), p_found is properly set. */
373             break;
374         }
375     }
376
377     vlc_mutex_unlock( &structure_lock );
378     return NULL;
379 }
380
381 /*****************************************************************************
382  * vlc_object_find: find a typed object and increment its refcount
383  *****************************************************************************
384  * This function recursively looks for a given object type. i_mode can be one
385  * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
386  *****************************************************************************/
387 void * __vlc_object_find( vlc_object_t *p_this, int i_type, int i_mode )
388 {
389     vlc_object_t *p_found;
390
391     vlc_mutex_lock( &structure_lock );
392
393     /* If we are of the requested type ourselves, don't look further */
394     if( !(i_mode & FIND_STRICT) && p_this->i_object_type == i_type )
395     {
396         p_this->i_refcount++;
397         vlc_mutex_unlock( &structure_lock );
398         return p_this;
399     }
400
401     /* Otherwise, recursively look for the object */
402     if( (i_mode & 0x000f) == FIND_ANYWHERE )
403     {
404         p_found = FindObject( VLC_OBJECT(p_this->p_vlc), i_type,
405                               (i_mode & ~0x000f) | FIND_CHILD );
406     }
407     else
408     {
409         p_found = FindObject( p_this, i_type, i_mode );
410     }
411
412     vlc_mutex_unlock( &structure_lock );
413
414     return p_found;
415 }
416
417 /*****************************************************************************
418  * vlc_object_yield: increment an object refcount
419  *****************************************************************************/
420 void __vlc_object_yield( vlc_object_t *p_this )
421 {
422     vlc_mutex_lock( &structure_lock );
423     p_this->i_refcount++;
424     vlc_mutex_unlock( &structure_lock );
425 }
426
427 /*****************************************************************************
428  * vlc_object_release: decrement an object refcount
429  *****************************************************************************/
430 void __vlc_object_release( vlc_object_t *p_this )
431 {
432     vlc_mutex_lock( &structure_lock );
433     p_this->i_refcount--;
434     vlc_mutex_unlock( &structure_lock );
435 }
436
437 /*****************************************************************************
438  * vlc_object_attach: attach object to a parent object
439  *****************************************************************************
440  * This function sets p_this as a child of p_parent, and p_parent as a parent
441  * of p_this. This link can be undone using vlc_object_detach.
442  *****************************************************************************/
443 void __vlc_object_attach( vlc_object_t *p_this, vlc_object_t *p_parent )
444 {
445     vlc_mutex_lock( &structure_lock );
446
447     /* Attach the parent to its child */
448     p_this->p_parent = p_parent;
449
450     /* Attach the child to its parent */
451     INSERT_ELEM( p_parent->pp_children, p_parent->i_children,
452                  p_parent->i_children, p_this );
453
454     /* Climb up the tree to see whether we are connected with the root */
455     if( p_parent->b_attached )
456     {
457         SetAttachment( p_this, VLC_TRUE );
458     }
459
460     vlc_mutex_unlock( &structure_lock );
461 }
462
463 /*****************************************************************************
464  * vlc_object_detach: detach object from its parent
465  *****************************************************************************
466  * This function removes all links between an object and its parent.
467  *****************************************************************************/
468 void __vlc_object_detach( vlc_object_t *p_this )
469 {
470     vlc_mutex_lock( &structure_lock );
471     if( !p_this->p_parent )
472     {
473         msg_Err( p_this, "object is not attached" );
474         vlc_mutex_unlock( &structure_lock );
475         return;
476     }
477
478     /* Climb up the tree to see whether we are connected with the root */
479     if( p_this->p_parent->b_attached )
480     {
481         SetAttachment( p_this, VLC_FALSE );
482     }
483
484     DetachObject( p_this );
485     vlc_mutex_unlock( &structure_lock );
486 }
487
488 /*****************************************************************************
489  * vlc_list_find: find a list typed objects and increment their refcount
490  *****************************************************************************
491  * This function recursively looks for a given object type. i_mode can be one
492  * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
493  *****************************************************************************/
494 vlc_list_t __vlc_list_find( vlc_object_t *p_this, int i_type, int i_mode )
495 {
496     vlc_list_t list;
497     vlc_object_t **pp_current, **pp_end;
498     int i_count = 0, i_index = 0;
499
500     vlc_mutex_lock( &structure_lock );
501
502     /* Look for the objects */
503     switch( i_mode & 0x000f )
504     {
505     case FIND_ANYWHERE:
506         pp_current = p_this->p_libvlc->pp_objects;
507         pp_end = pp_current + p_this->p_libvlc->i_objects;
508
509         for( ; pp_current < pp_end ; pp_current++ )
510         {
511             if( (*pp_current)->b_attached
512                  && (*pp_current)->i_object_type == i_type )
513             {
514                 i_count++;
515             }
516         }
517
518         list = NewList( i_count );
519         pp_current = p_this->p_libvlc->pp_objects;
520
521         for( ; pp_current < pp_end ; pp_current++ )
522         {
523             if( (*pp_current)->b_attached
524                  && (*pp_current)->i_object_type == i_type )
525             {
526                 ListReplace( &list, *pp_current, i_index );
527                 if( i_index < i_count ) i_index++;
528             }
529         }
530     break;
531
532     case FIND_CHILD:
533         i_count = CountChildren( p_this, i_type );
534         list = NewList( i_count );
535
536         /* Check allocation was successful */
537         if( list.i_count != i_count )
538         {
539             msg_Err( p_this, "list allocation failed!" );
540             list.i_count = 0;
541             break;
542         }
543
544         list.i_count = 0;
545         ListChildren( &list, p_this, i_type );
546         break;
547
548     default:
549         msg_Err( p_this, "unimplemented!" );
550         list = NewList( 0 );
551         break;
552     }
553
554     vlc_mutex_unlock( &structure_lock );
555
556     return list;
557 }
558
559 /*****************************************************************************
560  * DumpCommand: print the current vlc structure
561  *****************************************************************************
562  * This function prints either an ASCII tree showing the connections between
563  * vlc objects, and additional information such as their refcount, thread ID,
564  * etc. (command "tree"), or the same data as a simple list (command "list").
565  *****************************************************************************/
566 static int DumpCommand( vlc_object_t *p_this, char const *psz_cmd,
567                         vlc_value_t oldval, vlc_value_t newval, void *p_data )
568 {
569     if( *psz_cmd == 't' )
570     {
571         char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
572         vlc_object_t *p_object;
573
574         if( *newval.psz_string )
575         {
576             p_object = vlc_object_get( p_this, atoi(newval.psz_string) );
577
578             if( !p_object )
579             {
580                 return VLC_ENOOBJ;
581             }
582         }
583         else
584         {
585             p_object = p_this->p_vlc ? VLC_OBJECT(p_this->p_vlc) : p_this;
586         }
587
588         vlc_mutex_lock( &structure_lock );
589
590         psz_foo[0] = '|';
591         DumpStructure( p_object, 0, psz_foo );
592
593         vlc_mutex_unlock( &structure_lock );
594
595         if( *newval.psz_string )
596         {
597             vlc_object_release( p_this );
598         }
599     }
600     else if( *psz_cmd == 'l' )
601     {
602         vlc_object_t **pp_current, **pp_end;
603
604         vlc_mutex_lock( &structure_lock );
605
606         pp_current = p_this->p_libvlc->pp_objects;
607         pp_end = pp_current + p_this->p_libvlc->i_objects;
608
609         for( ; pp_current < pp_end ; pp_current++ )
610         {
611             if( (*pp_current)->b_attached )
612             {
613                 PrintObject( *pp_current, "" );
614             }
615             else
616             {
617                 printf( " o %.8i %s (not attached)\n",
618                         (*pp_current)->i_object_id,
619                         (*pp_current)->psz_object_type );
620             }
621         }
622
623         vlc_mutex_unlock( &structure_lock );
624     }
625
626     return VLC_SUCCESS;
627 }
628
629 /*****************************************************************************
630  * vlc_list_release: free a list previously allocated by vlc_list_find
631  *****************************************************************************
632  * This function decreases the refcount of all objects in the list and
633  * frees the list.
634  *****************************************************************************/
635 void vlc_list_release( vlc_list_t *p_list )
636 {
637     int i_index;
638
639     for( i_index = 0; i_index < p_list->i_count; i_index++ )
640     {
641         vlc_mutex_lock( &structure_lock );
642
643         p_list->p_values[i_index].p_object->i_refcount--;
644
645         vlc_mutex_unlock( &structure_lock );
646     }
647
648     free( p_list->p_values );
649 }
650
651 /* Following functions are local */
652
653 /*****************************************************************************
654  * FindIndex: find the index of an object in an array of objects
655  *****************************************************************************
656  * This function assumes that p_this can be found in pp_objects. It will not
657  * crash if p_this cannot be found, but will return a wrong value. It is your
658  * duty to check the return value if you are not certain that the object could
659  * be found for sure.
660  *****************************************************************************/
661 static int FindIndex( vlc_object_t *p_this,
662                       vlc_object_t **pp_objects, int i_count )
663 {
664     int i_middle = i_count / 2;
665
666     if( i_count == 0 )
667     {
668         return 0;
669     }
670
671     if( pp_objects[i_middle] == p_this )
672     {
673         return i_middle;
674     }
675
676     if( i_count == 1 )
677     {
678         return 0;
679     }
680
681     /* We take advantage of the sorted array */
682     if( pp_objects[i_middle]->i_object_id < p_this->i_object_id )
683     {
684         return i_middle + FindIndex( p_this, pp_objects + i_middle,
685                                              i_count - i_middle );
686     }
687     else
688     {
689         return FindIndex( p_this, pp_objects, i_middle );
690     }
691 }
692
693 static vlc_object_t * FindObject( vlc_object_t *p_this, int i_type, int i_mode )
694 {
695     int i;
696     vlc_object_t *p_tmp;
697
698     switch( i_mode & 0x000f )
699     {
700     case FIND_PARENT:
701         p_tmp = p_this->p_parent;
702         if( p_tmp )
703         {
704             if( p_tmp->i_object_type == i_type )
705             {
706                 p_tmp->i_refcount++;
707                 return p_tmp;
708             }
709             else
710             {
711                 return FindObject( p_tmp, i_type, i_mode );
712             }
713         }
714         break;
715
716     case FIND_CHILD:
717         for( i = p_this->i_children; i--; )
718         {
719             p_tmp = p_this->pp_children[i];
720             if( p_tmp->i_object_type == i_type )
721             {
722                 p_tmp->i_refcount++;
723                 return p_tmp;
724             }
725             else if( p_tmp->i_children )
726             {
727                 p_tmp = FindObject( p_tmp, i_type, i_mode );
728                 if( p_tmp )
729                 {
730                     return p_tmp;
731                 }
732             }
733         }
734         break;
735
736     case FIND_ANYWHERE:
737         /* Handled in vlc_object_find */
738         break;
739     }
740
741     return NULL;
742 }
743
744 static void DetachObject( vlc_object_t *p_this )
745 {
746     vlc_object_t *p_parent = p_this->p_parent;
747     int i_index, i;
748
749     /* Remove p_this's parent */
750     p_this->p_parent = NULL;
751
752     /* Remove all of p_parent's children which are p_this */
753     for( i_index = p_parent->i_children ; i_index-- ; )
754     {
755         if( p_parent->pp_children[i_index] == p_this )
756         {
757             p_parent->i_children--;
758             for( i = i_index ; i < p_parent->i_children ; i++ )
759             {
760                 p_parent->pp_children[i] = p_parent->pp_children[i+1];
761             }
762         }
763     }
764
765     if( p_parent->i_children )
766     {
767         p_parent->pp_children = (vlc_object_t **)realloc( p_parent->pp_children,
768                                p_parent->i_children * sizeof(vlc_object_t *) );
769     }
770     else
771     {
772         free( p_parent->pp_children );
773         p_parent->pp_children = NULL;
774     }
775 }
776
777 /*****************************************************************************
778  * SetAttachment: recursively set the b_attached flag of a subtree.
779  *****************************************************************************
780  * This function is used by the attach and detach functions to propagate
781  * the b_attached flag in a subtree.
782  *****************************************************************************/
783 static void SetAttachment( vlc_object_t *p_this, vlc_bool_t b_attached )
784 {
785     int i_index;
786
787     for( i_index = p_this->i_children ; i_index-- ; )
788     {
789         SetAttachment( p_this->pp_children[i_index], b_attached );
790     }
791
792     p_this->b_attached = b_attached;
793 }
794
795 static void PrintObject( vlc_object_t *p_this, const char *psz_prefix )
796 {
797     char psz_children[20], psz_refcount[20], psz_thread[20], psz_name[50];
798
799     psz_name[0] = '\0';
800     if( p_this->psz_object_name )
801     {
802         snprintf( psz_name, 50, " \"%s\"", p_this->psz_object_name );
803         psz_name[48] = '\"';
804         psz_name[49] = '\0';
805     }
806
807     psz_children[0] = '\0';
808     switch( p_this->i_children )
809     {
810         case 0:
811             break;
812         case 1:
813             strcpy( psz_children, ", 1 child" );
814             break;
815         default:
816             snprintf( psz_children, 20,
817                       ", %i children", p_this->i_children );
818             psz_children[19] = '\0';
819             break;
820     }
821
822     psz_refcount[0] = '\0';
823     if( p_this->i_refcount )
824     {
825         snprintf( psz_refcount, 20, ", refcount %i", p_this->i_refcount );
826         psz_refcount[19] = '\0';
827     }
828
829     psz_thread[0] = '\0';
830     if( p_this->b_thread )
831     {
832         snprintf( psz_thread, 20, " (thread %d)", (int)p_this->thread_id );
833         psz_thread[19] = '\0';
834     }
835
836     printf( " %so %.8i %s%s%s%s%s\n", psz_prefix,
837             p_this->i_object_id, p_this->psz_object_type,
838             psz_name, psz_thread, psz_refcount, psz_children );
839 }
840
841 static void DumpStructure( vlc_object_t *p_this, int i_level, char *psz_foo )
842 {
843     int i;
844     char i_back = psz_foo[i_level];
845     psz_foo[i_level] = '\0';
846
847     PrintObject( p_this, psz_foo );
848
849     psz_foo[i_level] = i_back;
850
851     if( i_level / 2 >= MAX_DUMPSTRUCTURE_DEPTH )
852     {
853         msg_Warn( p_this, "structure tree is too deep" );
854         return;
855     }
856
857     for( i = 0 ; i < p_this->i_children ; i++ )
858     {
859         if( i_level )
860         {
861             psz_foo[i_level-1] = ' ';
862
863             if( psz_foo[i_level-2] == '`' )
864             {
865                 psz_foo[i_level-2] = ' ';
866             }
867         }
868
869         if( i == p_this->i_children - 1 )
870         {
871             psz_foo[i_level] = '`';
872         }
873         else
874         {
875             psz_foo[i_level] = '|';
876         }
877
878         psz_foo[i_level+1] = '-';
879         psz_foo[i_level+2] = '\0';
880
881         DumpStructure( p_this->pp_children[i], i_level + 2, psz_foo );
882     }
883 }
884
885 static vlc_list_t NewList( int i_count )
886 {
887     vlc_list_t list;
888
889     list.i_count = i_count;
890
891     if( i_count == 0 )
892     {
893         list.p_values = NULL;
894         return list;
895     }
896
897     list.p_values = malloc( i_count * sizeof( vlc_value_t ) );
898     if( list.p_values == NULL )
899     {
900         list.i_count = 0;
901         return list;
902     }
903
904     return list;
905 }
906
907 static void ListReplace( vlc_list_t *p_list, vlc_object_t *p_object,
908                          int i_index )
909 {
910     if( p_list == NULL || i_index >= p_list->i_count )
911     {
912         return;
913     }
914
915     p_object->i_refcount++;
916
917     p_list->p_values[i_index].p_object = p_object;
918
919     return;
920 }
921
922 static void ListAppend( vlc_list_t *p_list, vlc_object_t *p_object )
923 {
924     if( p_list == NULL )
925     {
926         return;
927     }
928
929     p_list->p_values = realloc( p_list->p_values, (p_list->i_count + 1)
930                                 * sizeof( vlc_value_t ) );
931     if( p_list->p_values == NULL )
932     {
933         p_list->i_count = 0;
934         return;
935     }
936
937     p_object->i_refcount++;
938
939     p_list->p_values[p_list->i_count].p_object = p_object;
940     p_list->i_count++;
941
942     return;
943 }
944
945 static int CountChildren( vlc_object_t *p_this, int i_type )
946 {
947     vlc_object_t *p_tmp;
948     int i, i_count = 0;
949
950     for( i = 0; i < p_this->i_children; i++ )
951     {
952         p_tmp = p_this->pp_children[i];
953
954         if( p_tmp->i_object_type == i_type )
955         {
956             i_count++;
957         }
958
959         if( p_tmp->i_children )
960         {
961             i_count += CountChildren( p_tmp, i_type );
962         }
963     }
964
965     return i_count;
966 }
967
968 static void ListChildren( vlc_list_t *p_list, vlc_object_t *p_this, int i_type )
969 {
970     vlc_object_t *p_tmp;
971     int i;
972
973     for( i = 0; i < p_this->i_children; i++ )
974     {
975         p_tmp = p_this->pp_children[i];
976
977         if( p_tmp->i_object_type == i_type )
978         {
979             ListReplace( p_list, p_tmp, p_list->i_count++ );
980         }
981
982         if( p_tmp->i_children )
983         {
984             ListChildren( p_list, p_tmp, i_type );
985         }
986     }
987 }