]> git.sesse.net Git - vlc/blob - src/misc/objects.c
ddf5a3842b6e296eb334db722d82ee77d3512d68
[vlc] / src / misc / objects.c
1 /*****************************************************************************
2  * objects.c: vlc_object_t handling
3  *****************************************************************************
4  * Copyright (C) 2004-2007 the VideoLAN team
5  * $Id$
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /**
25  * \file
26  * This file contains the functions to handle the vlc_object_t type
27  */
28
29
30 /*****************************************************************************
31  * Preamble
32  *****************************************************************************/
33 #ifdef HAVE_CONFIG_H
34 # include "config.h"
35 #endif
36
37 #include <vlc/vlc.h>
38
39 #include "../libvlc.h"
40 #include <vlc_vout.h>
41 #include <vlc_aout.h>
42 #include "audio_output/aout_internal.h"
43
44 #include <vlc_access.h>
45 #include <vlc_demux.h>
46 #include <vlc_stream.h>
47
48 #include <vlc_sout.h>
49 #include "stream_output/stream_output.h"
50
51 #include "vlc_playlist.h"
52 #include "vlc_interface.h"
53 #include "vlc_codec.h"
54 #include "vlc_filter.h"
55
56 #include "vlc_httpd.h"
57 #include "vlc_vlm.h"
58 #include "input/vlm_internal.h"
59 #include "vlc_vod.h"
60 #include "vlc_tls.h"
61 #include "vlc_xml.h"
62 #include "vlc_osd.h"
63 #include "vlc_meta.h"
64
65 #include "variables.h"
66 #ifndef WIN32
67 # include <unistd.h>
68 #else
69 # include <io.h>
70 # include <fcntl.h>
71 # include <errno.h> /* ENOSYS */
72 #endif
73
74 /*****************************************************************************
75  * Local prototypes
76  *****************************************************************************/
77 static int  DumpCommand( vlc_object_t *, char const *,
78                          vlc_value_t, vlc_value_t, void * );
79
80 static vlc_object_t * FindObject    ( vlc_object_t *, int, int );
81 static vlc_object_t * FindObjectName( vlc_object_t *, const char *, int );
82 static void           DetachObject  ( vlc_object_t * );
83 static void           PrintObject   ( vlc_object_t *, const char * );
84 static void           DumpStructure ( vlc_object_t *, int, char * );
85 static int            FindIndex     ( vlc_object_t *, vlc_object_t **, int );
86 static void           SetAttachment ( vlc_object_t *, vlc_bool_t );
87
88 static vlc_list_t   * NewList       ( int );
89 static void           ListReplace   ( vlc_list_t *, vlc_object_t *, int );
90 /*static void           ListAppend    ( vlc_list_t *, vlc_object_t * );*/
91 static int            CountChildren ( vlc_object_t *, int );
92 static void           ListChildren  ( vlc_list_t *, vlc_object_t *, int );
93
94 /*****************************************************************************
95  * Local structure lock
96  *****************************************************************************/
97 static vlc_mutex_t    structure_lock;
98 static vlc_object_internals_t global_internals;
99
100 vlc_object_t *vlc_custom_create( vlc_object_t *p_this, size_t i_size,
101                                  int i_type, const char *psz_type )
102 {
103     vlc_object_t *p_new;
104     vlc_object_internals_t *p_priv;
105
106     if( i_type == VLC_OBJECT_GLOBAL )
107     {
108         p_new = p_this;
109         p_priv = &global_internals;
110         memset( p_priv, 0, sizeof( *p_priv ) );
111     }
112     else
113     {
114         p_priv = calloc( 1, sizeof( *p_priv ) + i_size );
115         if( p_priv == NULL )
116             return NULL;
117
118         p_new = (vlc_object_t *)(p_priv + 1);
119     }
120
121     p_new->p_internals = p_priv;
122     p_new->i_object_type = i_type;
123     p_new->psz_object_type = psz_type;
124
125     p_new->psz_object_name = NULL;
126
127     p_new->b_die = VLC_FALSE;
128     p_new->b_error = VLC_FALSE;
129     p_new->b_dead = VLC_FALSE;
130     p_priv->b_attached = VLC_FALSE;
131     p_new->b_force = VLC_FALSE;
132
133     p_new->psz_header = NULL;
134
135     if( p_this->i_flags & OBJECT_FLAGS_NODBG )
136         p_new->i_flags |= OBJECT_FLAGS_NODBG;
137     if( p_this->i_flags & OBJECT_FLAGS_QUIET )
138         p_new->i_flags |= OBJECT_FLAGS_QUIET;
139     if( p_this->i_flags & OBJECT_FLAGS_NOINTERACT )
140         p_new->i_flags |= OBJECT_FLAGS_NOINTERACT;
141
142     p_priv->p_vars = calloc( sizeof( variable_t ), 16 );
143
144     if( !p_priv->p_vars )
145     {
146         if( i_type != VLC_OBJECT_GLOBAL )
147             free( p_priv );
148         return NULL;
149     }
150
151     if( i_type == VLC_OBJECT_GLOBAL )
152     {
153         /* If i_type is global, then p_new is actually p_libvlc_global */
154         libvlc_global_data_t *p_libvlc_global = (libvlc_global_data_t *)p_new;
155         p_new->p_libvlc = NULL;
156
157         p_libvlc_global->i_counter = 0;
158         p_new->i_object_id = 0;
159
160         p_libvlc_global->i_objects = 1;
161         p_libvlc_global->pp_objects = malloc( sizeof(vlc_object_t *) );
162         p_libvlc_global->pp_objects[0] = p_new;
163         p_priv->b_attached = VLC_TRUE;
164     }
165     else
166     {
167         libvlc_global_data_t *p_libvlc_global = vlc_global();
168         if( i_type == VLC_OBJECT_LIBVLC )
169         {
170             p_new->p_libvlc = (libvlc_int_t*)p_new;
171             p_priv->b_attached = VLC_TRUE;
172         }
173         else
174         {
175             p_new->p_libvlc = p_this->p_libvlc;
176         }
177
178         vlc_mutex_lock( &structure_lock );
179
180         p_libvlc_global->i_counter++;
181         p_new->i_object_id = p_libvlc_global->i_counter;
182
183         /* Wooohaa! If *this* fails, we're in serious trouble! Anyway it's
184          * useless to try and recover anything if pp_objects gets smashed. */
185         TAB_APPEND( p_libvlc_global->i_objects, p_libvlc_global->pp_objects,
186                     p_new );
187
188         vlc_mutex_unlock( &structure_lock );
189     }
190
191     p_priv->i_refcount = 0;
192     p_new->p_parent = NULL;
193     p_new->pp_children = NULL;
194     p_new->i_children = 0;
195
196     p_new->p_private = NULL;
197
198     /* Initialize mutexes and condvars */
199     vlc_mutex_init( p_new, &p_new->object_lock );
200     vlc_cond_init( p_new, &p_new->object_wait );
201     vlc_mutex_init( p_new, &p_priv->var_lock );
202     vlc_spin_init( &p_priv->spin );
203     p_priv->pipes[0] = p_priv->pipes[1] = -1;
204
205     if( i_type == VLC_OBJECT_GLOBAL )
206     {
207         vlc_mutex_init( p_new, &structure_lock );
208     }
209
210     if( i_type == VLC_OBJECT_LIBVLC )
211     {
212         var_Create( p_new, "list", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
213         var_AddCallback( p_new, "list", DumpCommand, NULL );
214         var_Create( p_new, "tree", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
215         var_AddCallback( p_new, "tree", DumpCommand, NULL );
216         var_Create( p_new, "vars", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
217         var_AddCallback( p_new, "vars", DumpCommand, NULL );
218     }
219
220     return p_new;
221 }
222
223
224 /**
225  * Allocates and initializes a vlc object.
226  *
227  * @param i_type known object type (all of them are negative integer values),
228  *               or object byte size (always positive).
229  *
230  * @return the new object, or NULL on error.
231  */
232 void * __vlc_object_create( vlc_object_t *p_this, int i_type )
233 {
234     const char   * psz_type;
235     size_t         i_size;
236
237     switch( i_type )
238     {
239         case VLC_OBJECT_GLOBAL:
240             i_size = sizeof(libvlc_global_data_t);
241             psz_type = "global";
242             break;
243         case VLC_OBJECT_LIBVLC:
244             i_size = sizeof(libvlc_int_t);
245             psz_type = "libvlc";
246             break;
247         case VLC_OBJECT_INTF:
248             i_size = sizeof(intf_thread_t);
249             psz_type = "interface";
250             break;
251         case VLC_OBJECT_DIALOGS:
252             i_size = sizeof(intf_thread_t);
253             psz_type = "dialogs";
254             break;
255         case VLC_OBJECT_PLAYLIST:
256             i_size = sizeof(playlist_t);
257             psz_type = "playlist";
258             break;
259         case VLC_OBJECT_SD:
260             i_size = sizeof(services_discovery_t);
261             psz_type = "services discovery";
262             break;
263         case VLC_OBJECT_INPUT:
264             i_size = sizeof(input_thread_t);
265             psz_type = "input";
266             break;
267         case VLC_OBJECT_DEMUX:
268             i_size = sizeof(demux_t);
269             psz_type = "demux";
270             break;
271         case VLC_OBJECT_ACCESS:
272             i_size = sizeof(access_t);
273             psz_type = "access";
274             break;
275         case VLC_OBJECT_DECODER:
276             i_size = sizeof(decoder_t);
277             psz_type = "decoder";
278             break;
279         case VLC_OBJECT_PACKETIZER:
280             i_size = sizeof(decoder_t);
281             psz_type = "packetizer";
282             break;
283         case VLC_OBJECT_ENCODER:
284             i_size = sizeof(encoder_t);
285             psz_type = "encoder";
286             break;
287         case VLC_OBJECT_FILTER:
288             i_size = sizeof(filter_t);
289             psz_type = "filter";
290             break;
291         case VLC_OBJECT_VOUT:
292             i_size = sizeof(vout_thread_t);
293             psz_type = "video output";
294             break;
295         case VLC_OBJECT_SPU:
296             i_size = sizeof(spu_t);
297             psz_type = "subpicture";
298             break;
299         case VLC_OBJECT_AOUT:
300             i_size = sizeof(aout_instance_t);
301             psz_type = "audio output";
302             break;
303         case VLC_OBJECT_SOUT:
304             i_size = sizeof(sout_instance_t);
305             psz_type = "stream output";
306             break;
307         case VLC_OBJECT_VLM:
308             i_size = sizeof( vlm_t );
309             psz_type = "vlm dameon";
310             break;
311         case VLC_OBJECT_VOD:
312             i_size = sizeof( vod_t );
313             psz_type = "vod server";
314             break;
315         case VLC_OBJECT_XML:
316             i_size = sizeof( xml_t );
317             psz_type = "xml";
318             break;
319         case VLC_OBJECT_OPENGL:
320             i_size = sizeof( vout_thread_t );
321             psz_type = "opengl";
322             break;
323         case VLC_OBJECT_ANNOUNCE:
324             i_size = sizeof( announce_handler_t );
325             psz_type = "announce";
326             break;
327         case VLC_OBJECT_META_ENGINE:
328             i_size = sizeof( meta_engine_t );
329             psz_type = "meta engine";
330             break;
331         case VLC_OBJECT_OSDMENU:
332             i_size = sizeof( osd_menu_t );
333             psz_type = "osd menu";
334             break;
335         default:
336             i_size = i_type > (int)sizeof(vlc_object_t)
337                          ? i_type : (int)sizeof(vlc_object_t);
338             i_type = VLC_OBJECT_GENERIC;
339             psz_type = "generic";
340             break;
341     }
342
343     return vlc_custom_create( p_this, i_size, i_type, psz_type );
344 }
345
346
347 /**
348  ****************************************************************************
349  * Destroy a vlc object
350  *
351  * This function destroys an object that has been previously allocated with
352  * vlc_object_create. The object's refcount must be zero and it must not be
353  * attached to other objects in any way.
354  *****************************************************************************/
355 void __vlc_object_destroy( vlc_object_t *p_this )
356 {
357     vlc_object_internals_t *p_priv = vlc_internals( p_this );
358     int i_delay = 0;
359
360     if( p_this->i_children )
361     {
362         msg_Err( p_this, "cannot delete object (%i, %s) with children" ,
363                  p_this->i_object_id, p_this->psz_object_name );
364         return;
365     }
366
367     if( p_this->p_parent )
368     {
369         msg_Err( p_this, "cannot delete object (%i, %s) with a parent",
370                  p_this->i_object_id, p_this->psz_object_name );
371         return;
372     }
373
374     while( p_priv->i_refcount > 0 )
375     {
376         i_delay++;
377
378         /* Don't warn immediately ... 100ms seems OK */
379         if( i_delay == 2 )
380         {
381             msg_Warn( p_this,
382                   "refcount is %u, delaying before deletion (id=%d,type=%d)",
383                   p_priv->i_refcount, p_this->i_object_id,
384                   p_this->i_object_type );
385         }
386         else if( i_delay == 10 )
387         {
388             msg_Err( p_this,
389                   "refcount is %u, delaying again (id=%d,type=%d)",
390                   p_priv->i_refcount, p_this->i_object_id,
391                   p_this->i_object_type );
392         }
393         else if( i_delay == 20 )
394         {
395             msg_Err( p_this,
396                   "waited too long, cancelling destruction (id=%d,type=%d)",
397                   p_this->i_object_id, p_this->i_object_type );
398             return;
399         }
400
401         msleep( 100000 );
402     }
403
404     /* Destroy the associated variables, starting from the end so that
405      * no memmove calls have to be done. */
406     while( p_priv->i_vars )
407     {
408         var_Destroy( p_this, p_priv->p_vars[p_priv->i_vars - 1].psz_name );
409     }
410
411     free( p_priv->p_vars );
412     vlc_mutex_destroy( &p_priv->var_lock );
413
414     if( p_this->psz_header ) free( p_this->psz_header );
415
416     if( p_this->i_object_type == VLC_OBJECT_GLOBAL )
417     {
418         libvlc_global_data_t *p_global = (libvlc_global_data_t *)p_this;
419         /* We are the global object ... no need to lock. */
420         free( p_global->pp_objects );
421         p_global->pp_objects = NULL;
422         p_global->i_objects--;
423
424         vlc_mutex_destroy( &structure_lock );
425     }
426     else
427     {
428         libvlc_global_data_t *p_libvlc_global = vlc_global();
429         int i_index;
430
431         vlc_mutex_lock( &structure_lock );
432
433         /* Wooohaa! If *this* fails, we're in serious trouble! Anyway it's
434          * useless to try and recover anything if pp_objects gets smashed. */
435         i_index = FindIndex( p_this, p_libvlc_global->pp_objects,
436                              p_libvlc_global->i_objects );
437         REMOVE_ELEM( p_libvlc_global->pp_objects,
438                      p_libvlc_global->i_objects, i_index );
439
440         vlc_mutex_unlock( &structure_lock );
441     }
442
443 #if defined(WIN32) || defined(UNDER_CE)
444     /* if object has an associated thread, close it now */
445     if( p_priv->thread_id.hThread )
446        CloseHandle(p_priv->thread_id.hThread);
447 #endif
448
449     vlc_mutex_destroy( &p_this->object_lock );
450     vlc_cond_destroy( &p_this->object_wait );
451     vlc_spin_destroy( &p_priv->spin );
452     if( p_priv->pipes[0] != -1 )
453         close( p_priv->pipes[0] );
454     if( p_priv->pipes[1] != -1 )
455         close( p_priv->pipes[1] );
456
457     /* global is not dynamically allocated by vlc_object_create */
458     if( p_this->i_object_type != VLC_OBJECT_GLOBAL )
459         free( p_priv );
460 }
461
462
463 /** Inter-object signaling */
464
465 void __vlc_object_lock( vlc_object_t *obj )
466 {
467     vlc_mutex_lock( &obj->object_lock );
468 }
469
470 void __vlc_object_unlock( vlc_object_t *obj )
471 {
472     vlc_assert_locked( &obj->object_lock );
473     vlc_mutex_unlock( &obj->object_lock );
474 }
475
476 #ifdef WIN32
477 # include <winsock2.h>
478 # include <ws2tcpip.h>
479
480 /**
481  * select()-able pipes emulated using Winsock
482  */
483 static int pipe (int fd[2])
484 {
485     SOCKADDR_IN addr;
486     int addrlen = sizeof (addr);
487
488     SOCKET l = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP), a,
489            c = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
490     if ((l == INVALID_SOCKET) || (c == INVALID_SOCKET))
491         goto error;
492
493     memset (&addr, 0, sizeof (addr));
494     addr.sin_family = AF_INET;
495     addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
496     if (bind (l, (PSOCKADDR)&addr, sizeof (addr))
497      || getsockname (l, (PSOCKADDR)&addr, &addrlen)
498      || listen (l, 1)
499      || connect (c, (PSOCKADDR)&addr, addrlen))
500         goto error;
501
502     a = accept (l, NULL, NULL);
503     if (a == INVALID_SOCKET)
504         goto error;
505
506     closesocket (l);
507     shutdown (a, 0);
508     shutdown (c, 1);
509     fd[0] = c;
510     fd[1] = a;
511     return 0;
512
513 error:
514     if (l != INVALID_SOCKET)
515         closesocket (l);
516     if (c != INVALID_SOCKET)
517         closesocket (c);
518     return -1;
519 }
520
521 #undef  read
522 #define read( a, b, c )  recv (a, b, c, 0)
523 #undef  write
524 #define write( a, b, c ) send (a, b, c, 0)
525 #undef  close
526 #define close( a )       closesocket (a)
527 #endif /* WIN32 */
528
529 /**
530  * Returns the readable end of a pipe that becomes readable whenever
531  * an object is signaled. This can be used to wait for VLC object events
532  * inside select(), poll() loops or frameworks providing an event loop.
533  *
534  * Note that the pipe will remain the same for the lifetime of the object.
535  * DO NOT close it yourself. Ever.
536  *
537  * DO NOT try to read from the pipe either: call vlc_object_wait() instead.
538  * Assuming the pipe is readable, vlc_object_wait() will not block.
539  * Also note that, as with vlc_object_wait(), there may be spurious wakeups.
540  *
541  * @param obj object that would be signaled
542  * @return a readable pipe descriptor, or -1 on error.
543  */
544 int __vlc_object_waitpipe( vlc_object_t *obj )
545 {
546     int pfd[2] = { -1, -1 };
547     struct vlc_object_internals_t *internals = obj->p_internals;
548     vlc_bool_t race = VLC_FALSE, signaled = VLC_FALSE;
549
550     vlc_spin_lock (&internals->spin);
551     if (internals->pipes[0] == -1)
552     {
553         /* This can only ever happen if someone killed us without locking: */
554         assert (internals->pipes[1] == -1);
555         vlc_spin_unlock (&internals->spin);
556
557         if (pipe (pfd))
558             return -1;
559
560         vlc_spin_lock (&internals->spin);
561         signaled = internals->b_signaled;
562         if (internals->pipes[0] == -1)
563         {
564             internals->pipes[0] = pfd[0];
565             internals->pipes[1] = pfd[1];
566         }
567         else
568             race = VLC_TRUE;
569     }
570     vlc_spin_unlock (&internals->spin);
571
572     if (race)
573     {   /* Race condition: two threads call pipe() - unlikely */
574         close (pfd[0]);
575         close (pfd[1]);
576     }
577
578     if (signaled)
579         /* Race condition: lc_object_signal() already invoked! */
580         while (write (internals->pipes[1], &(char){ 0 }, 1) < 0);
581
582     return internals->pipes[0];
583 }
584
585
586 /**
587  * Waits for the object to be signaled (using vlc_object_signal()).
588  * If the object already has a signal pending, this function will return
589  * immediately. It is asserted that the caller holds the object lock.
590  *
591  * @return true if the object is dying and should terminate.
592  */
593 vlc_bool_t __vlc_object_wait( vlc_object_t *obj )
594 {
595     int fd;
596
597     vlc_assert_locked( &obj->object_lock );
598
599     fd = obj->p_internals->pipes[0];
600     if (fd != -1)
601     {
602         while (read (fd, &(char){ 0 }, 1)  < 0);
603         return obj->b_die;
604     }
605
606     vlc_cond_wait( &obj->object_wait, &obj->object_lock );
607     obj->p_internals->b_signaled = VLC_FALSE;
608     return obj->b_die;
609 }
610
611
612 /**
613  * Waits for the object to be signaled (using vlc_object_signal()), or for
614  * a timer to expire.
615  * If the object already has a signal pending, this function will return
616  * immediately. It is asserted that the caller holds the object lock.
617  *
618  * @return negative if the object is dying and should terminate,
619  * positive if the the object has been signaled but is not dying,
620  * 0 if timeout has been reached.
621  */
622 int __vlc_object_timedwait( vlc_object_t *obj, mtime_t deadline )
623 {
624     int v;
625
626     vlc_assert_locked( &obj->object_lock );
627     v = vlc_cond_timedwait( &obj->object_wait, &obj->object_lock, deadline );
628     if( v == 0 ) /* signaled */
629         return obj->b_die ? -1 : 1;
630     return 0;
631 }
632
633
634 /**
635  * Checks whether an object has been "killed".
636  * The object lock must be held.
637  *
638  * Typical code for an object thread could be:
639  *
640    vlc_object_lock (self);
641    ...initialization...
642    while (vlc_object_alive (self))
643    {
644        ...preprocessing...
645
646        if (vlc_object_wait (self))
647            continue;
648
649        ...postprocessing...
650    }
651    ...deinitialization...
652    vlc_object_unlock (self);
653  *
654  *
655  * @return true iff the object has not been killed yet
656  */
657 vlc_bool_t __vlc_object_alive( vlc_object_t *obj )
658 {
659     vlc_assert_locked( &obj->object_lock );
660     return !obj->b_die;
661 }
662
663
664 /**
665  * Signals an object for which the lock is held.
666  */
667 void __vlc_object_signal_unlocked( vlc_object_t *obj )
668 {
669     struct vlc_object_internals_t *internals = obj->p_internals;
670     int fd;
671
672     vlc_assert_locked (&obj->object_lock);
673
674     vlc_spin_lock (&internals->spin);
675     fd = internals->pipes[1];
676     internals->b_signaled = VLC_TRUE;
677     vlc_spin_unlock (&internals->spin);
678
679     if( fd != -1 )
680         while( write( fd, &(char){ 0 }, 1 ) < 0 );
681
682     vlc_cond_signal( &obj->object_wait );
683 }
684
685
686 /**
687  * Requests termination of an object.
688  * If the object is LibVLC, also request to terminate all its children.
689  */
690 void __vlc_object_kill( vlc_object_t *p_this )
691 {
692     vlc_mutex_lock( &p_this->object_lock );
693     p_this->b_die = VLC_TRUE;
694
695     if( p_this->i_object_type == VLC_OBJECT_LIBVLC )
696         for( int i = 0; i < p_this->i_children ; i++ )
697             vlc_object_kill( p_this->pp_children[i] );
698
699     vlc_object_signal_unlocked( p_this );
700     vlc_mutex_unlock( &p_this->object_lock );
701 }
702
703
704 /**
705  * find an object given its ID
706  *
707  * This function looks for the object whose i_object_id field is i_id. We
708  * use a dichotomy so that lookups are in log2(n).
709  *****************************************************************************/
710 void * __vlc_object_get( vlc_object_t *p_this, int i_id )
711 {
712     int i_max, i_middle;
713     vlc_object_t **pp_objects;
714     libvlc_global_data_t *p_libvlc_global = vlc_global();
715
716     vlc_mutex_lock( &structure_lock );
717
718     pp_objects = p_libvlc_global->pp_objects;
719
720     /* Perform our dichotomy */
721     for( i_max = p_libvlc_global->i_objects - 1 ; ; )
722     {
723         i_middle = i_max / 2;
724
725         if( pp_objects[i_middle]->i_object_id > i_id )
726         {
727             i_max = i_middle;
728         }
729         else if( pp_objects[i_middle]->i_object_id < i_id )
730         {
731             if( i_middle )
732             {
733                 pp_objects += i_middle;
734                 i_max -= i_middle;
735             }
736             else
737             {
738                 /* This happens when there are only two remaining objects */
739                 if( pp_objects[i_middle+1]->i_object_id == i_id )
740                 {
741                     vlc_mutex_unlock( &structure_lock );
742                     pp_objects[i_middle+1]->p_internals->i_refcount++;
743                     return pp_objects[i_middle+1];
744                 }
745                 break;
746             }
747         }
748         else
749         {
750             vlc_mutex_unlock( &structure_lock );
751             pp_objects[i_middle]->p_internals->i_refcount++;
752             return pp_objects[i_middle];
753         }
754
755         if( i_max == 0 )
756         {
757             /* this means that i_max == i_middle, and since we have already
758              * tested pp_objects[i_middle]), p_found is properly set. */
759             break;
760         }
761     }
762
763     vlc_mutex_unlock( &structure_lock );
764     return NULL;
765 }
766
767 /**
768  ****************************************************************************
769  * find a typed object and increment its refcount
770  *****************************************************************************
771  * This function recursively looks for a given object type. i_mode can be one
772  * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
773  *****************************************************************************/
774 void * __vlc_object_find( vlc_object_t *p_this, int i_type, int i_mode )
775 {
776     vlc_object_t *p_found;
777
778     vlc_mutex_lock( &structure_lock );
779
780     /* If we are of the requested type ourselves, don't look further */
781     if( !(i_mode & FIND_STRICT) && p_this->i_object_type == i_type )
782     {
783         p_this->p_internals->i_refcount++;
784         vlc_mutex_unlock( &structure_lock );
785         return p_this;
786     }
787
788     /* Otherwise, recursively look for the object */
789     if( (i_mode & 0x000f) == FIND_ANYWHERE )
790     {
791         vlc_object_t *p_root = p_this;
792
793         /* Find the root */
794         while( p_root->p_parent != NULL &&
795                p_root != VLC_OBJECT( p_this->p_libvlc ) )
796         {
797             p_root = p_root->p_parent;
798         }
799
800         p_found = FindObject( p_root, i_type, (i_mode & ~0x000f)|FIND_CHILD );
801         if( p_found == NULL && p_root != VLC_OBJECT( p_this->p_libvlc ) )
802         {
803             p_found = FindObject( VLC_OBJECT( p_this->p_libvlc ),
804                                   i_type, (i_mode & ~0x000f)|FIND_CHILD );
805         }
806     }
807     else
808     {
809         p_found = FindObject( p_this, i_type, i_mode );
810     }
811
812     vlc_mutex_unlock( &structure_lock );
813
814     return p_found;
815 }
816
817 /**
818  ****************************************************************************
819  * find a named object and increment its refcount
820  *****************************************************************************
821  * This function recursively looks for a given object name. i_mode can be one
822  * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
823  *****************************************************************************/
824 void * __vlc_object_find_name( vlc_object_t *p_this, const char *psz_name,
825                                int i_mode )
826 {
827     vlc_object_t *p_found;
828
829     vlc_mutex_lock( &structure_lock );
830
831     /* If have the requested name ourselves, don't look further */
832     if( !(i_mode & FIND_STRICT)
833         && p_this->psz_object_name
834         && !strcmp( p_this->psz_object_name, psz_name ) )
835     {
836         p_this->p_internals->i_refcount++;
837         vlc_mutex_unlock( &structure_lock );
838         return p_this;
839     }
840
841     /* Otherwise, recursively look for the object */
842     if( (i_mode & 0x000f) == FIND_ANYWHERE )
843     {
844         vlc_object_t *p_root = p_this;
845
846         /* Find the root */
847         while( p_root->p_parent != NULL &&
848                p_root != VLC_OBJECT( p_this->p_libvlc ) )
849         {
850             p_root = p_root->p_parent;
851         }
852
853         p_found = FindObjectName( p_root, psz_name,
854                                  (i_mode & ~0x000f)|FIND_CHILD );
855         if( p_found == NULL && p_root != VLC_OBJECT( p_this->p_libvlc ) )
856         {
857             p_found = FindObjectName( VLC_OBJECT( p_this->p_libvlc ),
858                                       psz_name, (i_mode & ~0x000f)|FIND_CHILD );
859         }
860     }
861     else
862     {
863         p_found = FindObjectName( p_this, psz_name, i_mode );
864     }
865
866     vlc_mutex_unlock( &structure_lock );
867
868     return p_found;
869 }
870
871 /**
872  ****************************************************************************
873  * increment an object refcount
874  *****************************************************************************/
875 void __vlc_object_yield( vlc_object_t *p_this )
876 {
877     vlc_mutex_lock( &structure_lock );
878     p_this->p_internals->i_refcount++;
879     vlc_mutex_unlock( &structure_lock );
880 }
881
882 static inline void Release( vlc_object_t *obj )
883 {
884     assert( obj->p_internals->i_refcount > 0 );
885     obj->p_internals->i_refcount--;
886 }
887
888 /*****************************************************************************
889  * decrement an object refcount
890  *****************************************************************************/
891 void __vlc_object_release( vlc_object_t *p_this )
892 {
893     vlc_mutex_lock( &structure_lock );
894     Release( p_this );
895     vlc_mutex_unlock( &structure_lock );
896 }
897
898 /**
899  ****************************************************************************
900  * attach object to a parent object
901  *****************************************************************************
902  * This function sets p_this as a child of p_parent, and p_parent as a parent
903  * of p_this. This link can be undone using vlc_object_detach.
904  *****************************************************************************/
905 void __vlc_object_attach( vlc_object_t *p_this, vlc_object_t *p_parent )
906 {
907     if( !p_this ) return;
908
909     vlc_mutex_lock( &structure_lock );
910
911     /* Attach the parent to its child */
912     p_this->p_parent = p_parent;
913
914     /* Attach the child to its parent */
915     INSERT_ELEM( p_parent->pp_children, p_parent->i_children,
916                  p_parent->i_children, p_this );
917
918     /* Climb up the tree to see whether we are connected with the root */
919     if( p_parent->p_internals->b_attached )
920     {
921         SetAttachment( p_this, VLC_TRUE );
922     }
923
924     vlc_mutex_unlock( &structure_lock );
925 }
926
927 /**
928  ****************************************************************************
929  * detach object from its parent
930  *****************************************************************************
931  * This function removes all links between an object and its parent.
932  *****************************************************************************/
933 void __vlc_object_detach( vlc_object_t *p_this )
934 {
935     if( !p_this ) return;
936
937     vlc_mutex_lock( &structure_lock );
938     if( !p_this->p_parent )
939     {
940         msg_Err( p_this, "object is not attached" );
941         vlc_mutex_unlock( &structure_lock );
942         return;
943     }
944
945     /* Climb up the tree to see whether we are connected with the root */
946     if( p_this->p_parent->p_internals->b_attached )
947     {
948         SetAttachment( p_this, VLC_FALSE );
949     }
950
951     DetachObject( p_this );
952     vlc_mutex_unlock( &structure_lock );
953     p_this = NULL;
954 }
955
956 /**
957  ****************************************************************************
958  * find a list typed objects and increment their refcount
959  *****************************************************************************
960  * This function recursively looks for a given object type. i_mode can be one
961  * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
962  *****************************************************************************/
963 vlc_list_t * __vlc_list_find( vlc_object_t *p_this, int i_type, int i_mode )
964 {
965     vlc_list_t *p_list;
966     vlc_object_t **pp_current, **pp_end;
967     int i_count = 0, i_index = 0;
968     libvlc_global_data_t *p_libvlc_global = vlc_global();
969
970     vlc_mutex_lock( &structure_lock );
971
972     /* Look for the objects */
973     switch( i_mode & 0x000f )
974     {
975     case FIND_ANYWHERE:
976         pp_current = p_libvlc_global->pp_objects;
977         pp_end = pp_current + p_libvlc_global->i_objects;
978
979         for( ; pp_current < pp_end ; pp_current++ )
980         {
981             if( (*pp_current)->p_internals->b_attached
982                  && (*pp_current)->i_object_type == i_type )
983             {
984                 i_count++;
985             }
986         }
987
988         p_list = NewList( i_count );
989         pp_current = p_libvlc_global->pp_objects;
990
991         for( ; pp_current < pp_end ; pp_current++ )
992         {
993             if( (*pp_current)->p_internals->b_attached
994                  && (*pp_current)->i_object_type == i_type )
995             {
996                 ListReplace( p_list, *pp_current, i_index );
997                 if( i_index < i_count ) i_index++;
998             }
999         }
1000     break;
1001
1002     case FIND_CHILD:
1003         i_count = CountChildren( p_this, i_type );
1004         p_list = NewList( i_count );
1005
1006         /* Check allocation was successful */
1007         if( p_list->i_count != i_count )
1008         {
1009             msg_Err( p_this, "list allocation failed!" );
1010             p_list->i_count = 0;
1011             break;
1012         }
1013
1014         p_list->i_count = 0;
1015         ListChildren( p_list, p_this, i_type );
1016         break;
1017
1018     default:
1019         msg_Err( p_this, "unimplemented!" );
1020         p_list = NewList( 0 );
1021         break;
1022     }
1023
1024     vlc_mutex_unlock( &structure_lock );
1025
1026     return p_list;
1027 }
1028
1029 /*****************************************************************************
1030  * DumpCommand: print the current vlc structure
1031  *****************************************************************************
1032  * This function prints either an ASCII tree showing the connections between
1033  * vlc objects, and additional information such as their refcount, thread ID,
1034  * etc. (command "tree"), or the same data as a simple list (command "list").
1035  *****************************************************************************/
1036 static int DumpCommand( vlc_object_t *p_this, char const *psz_cmd,
1037                         vlc_value_t oldval, vlc_value_t newval, void *p_data )
1038 {
1039     libvlc_global_data_t *p_libvlc_global = vlc_global();
1040
1041     (void)oldval; (void)p_data;
1042     if( *psz_cmd == 'l' )
1043     {
1044         vlc_mutex_lock( &structure_lock );
1045
1046         vlc_object_t **pp_current, **pp_end;
1047
1048         pp_current = p_libvlc_global->pp_objects;
1049         pp_end = pp_current + p_libvlc_global->i_objects;
1050
1051         for( ; pp_current < pp_end ; pp_current++ )
1052         {
1053             if( (*pp_current)->p_internals->b_attached )
1054             {
1055                 PrintObject( *pp_current, "" );
1056             }
1057             else
1058             {
1059                 printf( " o %.8i %s (not attached)\n",
1060                         (*pp_current)->i_object_id,
1061                         (*pp_current)->psz_object_type );
1062             }
1063         }
1064
1065         vlc_mutex_unlock( &structure_lock );
1066     }
1067     else
1068     {
1069         vlc_object_t *p_object = NULL;
1070
1071         if( *newval.psz_string )
1072         {
1073             char *end;
1074             int i_id = strtol( newval.psz_string, &end, 0 );
1075             if( end != newval.psz_string )
1076                 p_object = vlc_object_get( p_this, i_id );
1077             else
1078             {
1079                 /* try using the object's name to find it */
1080                 vlc_object_t *p_libvlc = vlc_object_get( p_this, 1 );
1081                 if( p_libvlc )
1082                 {
1083                     /* Look in p_libvlc's children tree */
1084                     p_object = vlc_object_find_name( p_libvlc,
1085                                                      newval.psz_string,
1086                                                      FIND_CHILD );
1087                     vlc_object_release( p_libvlc );
1088                 }
1089                 if( !p_object )
1090                 {
1091                     /* If it's not in libvlc, look in libvlc_global (== p_this) */
1092                     p_object = vlc_object_find_name( p_this,
1093                                                      newval.psz_string,
1094                                                      FIND_CHILD );
1095                 }
1096             }
1097
1098             if( !p_object )
1099             {
1100                 return VLC_ENOOBJ;
1101             }
1102         }
1103
1104         vlc_mutex_lock( &structure_lock );
1105
1106         if( *psz_cmd == 't' )
1107         {
1108             char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
1109
1110             if( !p_object )
1111                 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
1112
1113             psz_foo[0] = '|';
1114             DumpStructure( p_object, 0, psz_foo );
1115         }
1116         else if( *psz_cmd == 'v' )
1117         {
1118             int i;
1119
1120             if( !p_object )
1121                 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
1122
1123             PrintObject( p_object, "" );
1124
1125             if( !p_object->p_internals->i_vars )
1126                 printf( " `-o No variables\n" );
1127             for( i = 0; i < p_object->p_internals->i_vars; i++ )
1128             {
1129                 variable_t *p_var = p_object->p_internals->p_vars + i;
1130
1131                 const char *psz_type = "unknown";
1132                 switch( p_var->i_type & VLC_VAR_TYPE )
1133                 {
1134 #define MYCASE( type, nice )                \
1135                     case VLC_VAR_ ## type:  \
1136                         psz_type = nice;    \
1137                         break;
1138                     MYCASE( VOID, "void" );
1139                     MYCASE( BOOL, "bool" );
1140                     MYCASE( INTEGER, "integer" );
1141                     MYCASE( HOTKEY, "hotkey" );
1142                     MYCASE( STRING, "string" );
1143                     MYCASE( MODULE, "module" );
1144                     MYCASE( FILE, "file" );
1145                     MYCASE( DIRECTORY, "directory" );
1146                     MYCASE( VARIABLE, "variable" );
1147                     MYCASE( FLOAT, "float" );
1148                     MYCASE( TIME, "time" );
1149                     MYCASE( ADDRESS, "address" );
1150                     MYCASE( MUTEX, "mutex" );
1151                     MYCASE( LIST, "list" );
1152 #undef MYCASE
1153                 }
1154                 printf( " %c-o \"%s\" (%s",
1155                         i + 1 == p_object->p_internals->i_vars ? '`' : '|',
1156                         p_var->psz_name, psz_type );
1157                 if( p_var->psz_text )
1158                     printf( ", %s", p_var->psz_text );
1159                 printf( ")" );
1160                 if( p_var->i_type & VLC_VAR_ISCOMMAND )
1161                     printf( ", command" );
1162                 if( p_var->i_entries )
1163                     printf( ", %d callbacks", p_var->i_entries );
1164                 switch( p_var->i_type & 0x00f0 )
1165                 {
1166                     case VLC_VAR_VOID:
1167                     case VLC_VAR_MUTEX:
1168                         break;
1169                     case VLC_VAR_BOOL:
1170                         printf( ": %s", p_var->val.b_bool ? "true" : "false" );
1171                         break;
1172                     case VLC_VAR_INTEGER:
1173                         printf( ": %d", p_var->val.i_int );
1174                         break;
1175                     case VLC_VAR_STRING:
1176                         printf( ": \"%s\"", p_var->val.psz_string );
1177                         break;
1178                     case VLC_VAR_FLOAT:
1179                         printf( ": %f", p_var->val.f_float );
1180                         break;
1181                     case VLC_VAR_TIME:
1182                         printf( ": " I64Fi, (int64_t)p_var->val.i_time );
1183                         break;
1184                     case VLC_VAR_ADDRESS:
1185                         printf( ": %p", p_var->val.p_address );
1186                         break;
1187                     case VLC_VAR_LIST:
1188                         printf( ": TODO" );
1189                         break;
1190                 }
1191                 printf( "\n" );
1192             }
1193         }
1194
1195         vlc_mutex_unlock( &structure_lock );
1196
1197         if( *newval.psz_string )
1198         {
1199             vlc_object_release( p_object );
1200         }
1201     }
1202
1203     return VLC_SUCCESS;
1204 }
1205
1206 /*****************************************************************************
1207  * vlc_list_release: free a list previously allocated by vlc_list_find
1208  *****************************************************************************
1209  * This function decreases the refcount of all objects in the list and
1210  * frees the list.
1211  *****************************************************************************/
1212 void vlc_list_release( vlc_list_t *p_list )
1213 {
1214     int i_index;
1215
1216     vlc_mutex_lock( &structure_lock );
1217     for( i_index = 0; i_index < p_list->i_count; i_index++ )
1218     {
1219         Release( p_list->p_values[i_index].p_object );
1220     }
1221     vlc_mutex_unlock( &structure_lock );
1222
1223     free( p_list->p_values );
1224     free( p_list );
1225 }
1226
1227 /* Following functions are local */
1228
1229 /*****************************************************************************
1230  * FindIndex: find the index of an object in an array of objects
1231  *****************************************************************************
1232  * This function assumes that p_this can be found in pp_objects. It will not
1233  * crash if p_this cannot be found, but will return a wrong value. It is your
1234  * duty to check the return value if you are not certain that the object could
1235  * be found for sure.
1236  *****************************************************************************/
1237 static int FindIndex( vlc_object_t *p_this,
1238                       vlc_object_t **pp_objects, int i_count )
1239 {
1240     int i_middle = i_count / 2;
1241
1242     if( i_count == 0 )
1243     {
1244         return 0;
1245     }
1246
1247     if( pp_objects[i_middle] == p_this )
1248     {
1249         return i_middle;
1250     }
1251
1252     if( i_count == 1 )
1253     {
1254         return 0;
1255     }
1256
1257     /* We take advantage of the sorted array */
1258     if( pp_objects[i_middle]->i_object_id < p_this->i_object_id )
1259     {
1260         return i_middle + FindIndex( p_this, pp_objects + i_middle,
1261                                              i_count - i_middle );
1262     }
1263     else
1264     {
1265         return FindIndex( p_this, pp_objects, i_middle );
1266     }
1267 }
1268
1269 static vlc_object_t * FindObject( vlc_object_t *p_this, int i_type, int i_mode )
1270 {
1271     int i;
1272     vlc_object_t *p_tmp;
1273
1274     switch( i_mode & 0x000f )
1275     {
1276     case FIND_PARENT:
1277         p_tmp = p_this->p_parent;
1278         if( p_tmp )
1279         {
1280             if( p_tmp->i_object_type == i_type )
1281             {
1282                 p_tmp->p_internals->i_refcount++;
1283                 return p_tmp;
1284             }
1285             else
1286             {
1287                 return FindObject( p_tmp, i_type, i_mode );
1288             }
1289         }
1290         break;
1291
1292     case FIND_CHILD:
1293         for( i = p_this->i_children; i--; )
1294         {
1295             p_tmp = p_this->pp_children[i];
1296             if( p_tmp->i_object_type == i_type )
1297             {
1298                 p_tmp->p_internals->i_refcount++;
1299                 return p_tmp;
1300             }
1301             else if( p_tmp->i_children )
1302             {
1303                 p_tmp = FindObject( p_tmp, i_type, i_mode );
1304                 if( p_tmp )
1305                 {
1306                     return p_tmp;
1307                 }
1308             }
1309         }
1310         break;
1311
1312     case FIND_ANYWHERE:
1313         /* Handled in vlc_object_find */
1314         break;
1315     }
1316
1317     return NULL;
1318 }
1319
1320 static vlc_object_t * FindObjectName( vlc_object_t *p_this,
1321                                       const char *psz_name,
1322                                       int i_mode )
1323 {
1324     int i;
1325     vlc_object_t *p_tmp;
1326
1327     switch( i_mode & 0x000f )
1328     {
1329     case FIND_PARENT:
1330         p_tmp = p_this->p_parent;
1331         if( p_tmp )
1332         {
1333             if( p_tmp->psz_object_name
1334                 && !strcmp( p_tmp->psz_object_name, psz_name ) )
1335             {
1336                 p_tmp->p_internals->i_refcount++;
1337                 return p_tmp;
1338             }
1339             else
1340             {
1341                 return FindObjectName( p_tmp, psz_name, i_mode );
1342             }
1343         }
1344         break;
1345
1346     case FIND_CHILD:
1347         for( i = p_this->i_children; i--; )
1348         {
1349             p_tmp = p_this->pp_children[i];
1350             if( p_tmp->psz_object_name
1351                 && !strcmp( p_tmp->psz_object_name, psz_name ) )
1352             {
1353                 p_tmp->p_internals->i_refcount++;
1354                 return p_tmp;
1355             }
1356             else if( p_tmp->i_children )
1357             {
1358                 p_tmp = FindObjectName( p_tmp, psz_name, i_mode );
1359                 if( p_tmp )
1360                 {
1361                     return p_tmp;
1362                 }
1363             }
1364         }
1365         break;
1366
1367     case FIND_ANYWHERE:
1368         /* Handled in vlc_object_find */
1369         break;
1370     }
1371
1372     return NULL;
1373 }
1374
1375 static void DetachObject( vlc_object_t *p_this )
1376 {
1377     vlc_object_t *p_parent = p_this->p_parent;
1378     int i_index, i;
1379
1380     /* Remove p_this's parent */
1381     p_this->p_parent = NULL;
1382
1383     /* Remove all of p_parent's children which are p_this */
1384     for( i_index = p_parent->i_children ; i_index-- ; )
1385     {
1386         if( p_parent->pp_children[i_index] == p_this )
1387         {
1388             p_parent->i_children--;
1389             for( i = i_index ; i < p_parent->i_children ; i++ )
1390             {
1391                 p_parent->pp_children[i] = p_parent->pp_children[i+1];
1392             }
1393         }
1394     }
1395
1396     if( p_parent->i_children )
1397     {
1398         p_parent->pp_children = (vlc_object_t **)realloc( p_parent->pp_children,
1399                                p_parent->i_children * sizeof(vlc_object_t *) );
1400     }
1401     else
1402     {
1403         free( p_parent->pp_children );
1404         p_parent->pp_children = NULL;
1405     }
1406 }
1407
1408 /*****************************************************************************
1409  * SetAttachment: recursively set the b_attached flag of a subtree.
1410  *****************************************************************************
1411  * This function is used by the attach and detach functions to propagate
1412  * the b_attached flag in a subtree.
1413  *****************************************************************************/
1414 static void SetAttachment( vlc_object_t *p_this, vlc_bool_t b_attached )
1415 {
1416     int i_index;
1417
1418     for( i_index = p_this->i_children ; i_index-- ; )
1419     {
1420         SetAttachment( p_this->pp_children[i_index], b_attached );
1421     }
1422
1423     p_this->p_internals->b_attached = b_attached;
1424 }
1425
1426 static void PrintObject( vlc_object_t *p_this, const char *psz_prefix )
1427 {
1428     char psz_children[20], psz_refcount[20], psz_thread[30], psz_name[50],
1429          psz_parent[20];
1430
1431     psz_name[0] = '\0';
1432     if( p_this->psz_object_name )
1433     {
1434         snprintf( psz_name, 49, " \"%s\"", p_this->psz_object_name );
1435         if( psz_name[48] )
1436             psz_name[48] = '\"';
1437     }
1438
1439     psz_children[0] = '\0';
1440     switch( p_this->i_children )
1441     {
1442         case 0:
1443             break;
1444         case 1:
1445             strcpy( psz_children, ", 1 child" );
1446             break;
1447         default:
1448             snprintf( psz_children, 19, ", %i children", p_this->i_children );
1449             break;
1450     }
1451
1452     psz_refcount[0] = '\0';
1453     if( p_this->p_internals->i_refcount > 0 )
1454         snprintf( psz_refcount, 19, ", refcount %u",
1455                   p_this->p_internals->i_refcount );
1456
1457     psz_thread[0] = '\0';
1458     if( p_this->p_internals->b_thread )
1459         snprintf( psz_thread, 29, " (thread %u)",
1460 #if defined(WIN32) || defined(UNDER_CE)
1461                   (unsigned)p_this->p_internals->thread_id.id );
1462 #else
1463                   (unsigned)p_this->p_internals->thread_id );
1464 #endif
1465
1466     psz_parent[0] = '\0';
1467     if( p_this->p_parent )
1468         snprintf( psz_parent, 19, ", parent %i", p_this->p_parent->i_object_id );
1469
1470     printf( " %so %.8i %s%s%s%s%s%s\n", psz_prefix,
1471             p_this->i_object_id, p_this->psz_object_type,
1472             psz_name, psz_thread, psz_refcount, psz_children,
1473             psz_parent );
1474 }
1475
1476 static void DumpStructure( vlc_object_t *p_this, int i_level, char *psz_foo )
1477 {
1478     int i;
1479     char i_back = psz_foo[i_level];
1480     psz_foo[i_level] = '\0';
1481
1482     PrintObject( p_this, psz_foo );
1483
1484     psz_foo[i_level] = i_back;
1485
1486     if( i_level / 2 >= MAX_DUMPSTRUCTURE_DEPTH )
1487     {
1488         msg_Warn( p_this, "structure tree is too deep" );
1489         return;
1490     }
1491
1492     for( i = 0 ; i < p_this->i_children ; i++ )
1493     {
1494         if( i_level )
1495         {
1496             psz_foo[i_level-1] = ' ';
1497
1498             if( psz_foo[i_level-2] == '`' )
1499             {
1500                 psz_foo[i_level-2] = ' ';
1501             }
1502         }
1503
1504         if( i == p_this->i_children - 1 )
1505         {
1506             psz_foo[i_level] = '`';
1507         }
1508         else
1509         {
1510             psz_foo[i_level] = '|';
1511         }
1512
1513         psz_foo[i_level+1] = '-';
1514         psz_foo[i_level+2] = '\0';
1515
1516         DumpStructure( p_this->pp_children[i], i_level + 2, psz_foo );
1517     }
1518 }
1519
1520 static vlc_list_t * NewList( int i_count )
1521 {
1522     vlc_list_t * p_list = (vlc_list_t *)malloc( sizeof( vlc_list_t ) );
1523     if( p_list == NULL )
1524     {
1525         return NULL;
1526     }
1527
1528     p_list->i_count = i_count;
1529
1530     if( i_count == 0 )
1531     {
1532         p_list->p_values = NULL;
1533         return p_list;
1534     }
1535
1536     p_list->p_values = malloc( i_count * sizeof( vlc_value_t ) );
1537     if( p_list->p_values == NULL )
1538     {
1539         p_list->i_count = 0;
1540         return p_list;
1541     }
1542
1543     return p_list;
1544 }
1545
1546 static void ListReplace( vlc_list_t *p_list, vlc_object_t *p_object,
1547                          int i_index )
1548 {
1549     if( p_list == NULL || i_index >= p_list->i_count )
1550     {
1551         return;
1552     }
1553
1554     p_object->p_internals->i_refcount++;
1555
1556     p_list->p_values[i_index].p_object = p_object;
1557
1558     return;
1559 }
1560
1561 /*static void ListAppend( vlc_list_t *p_list, vlc_object_t *p_object )
1562 {
1563     if( p_list == NULL )
1564     {
1565         return;
1566     }
1567
1568     p_list->p_values = realloc( p_list->p_values, (p_list->i_count + 1)
1569                                 * sizeof( vlc_value_t ) );
1570     if( p_list->p_values == NULL )
1571     {
1572         p_list->i_count = 0;
1573         return;
1574     }
1575
1576     p_object->p_internals->i_refcount++;
1577
1578     p_list->p_values[p_list->i_count].p_object = p_object;
1579     p_list->i_count++;
1580
1581     return;
1582 }*/
1583
1584 static int CountChildren( vlc_object_t *p_this, int i_type )
1585 {
1586     vlc_object_t *p_tmp;
1587     int i, i_count = 0;
1588
1589     for( i = 0; i < p_this->i_children; i++ )
1590     {
1591         p_tmp = p_this->pp_children[i];
1592
1593         if( p_tmp->i_object_type == i_type )
1594         {
1595             i_count++;
1596         }
1597
1598         if( p_tmp->i_children )
1599         {
1600             i_count += CountChildren( p_tmp, i_type );
1601         }
1602     }
1603
1604     return i_count;
1605 }
1606
1607 static void ListChildren( vlc_list_t *p_list, vlc_object_t *p_this, int i_type )
1608 {
1609     vlc_object_t *p_tmp;
1610     int i;
1611
1612     for( i = 0; i < p_this->i_children; i++ )
1613     {
1614         p_tmp = p_this->pp_children[i];
1615
1616         if( p_tmp->i_object_type == i_type )
1617         {
1618             ListReplace( p_list, p_tmp, p_list->i_count++ );
1619         }
1620
1621         if( p_tmp->i_children )
1622         {
1623             ListChildren( p_list, p_tmp, i_type );
1624         }
1625     }
1626 }