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