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