]> git.sesse.net Git - vlc/blob - src/misc/objects.c
9ccb557d5073c4c45152d81334b5830b61108a07
[vlc] / src / misc / objects.c
1 /*****************************************************************************
2  * objects.c: vlc_object_t handling
3  *****************************************************************************
4  * Copyright (C) 2004-2007 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Samuel Hocevar <sam@zoy.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /**
25  * \file
26  * This file contains the functions to handle the vlc_object_t type
27  */
28
29
30 /*****************************************************************************
31  * Preamble
32  *****************************************************************************/
33 #ifdef HAVE_CONFIG_H
34 # include "config.h"
35 #endif
36
37 #include <vlc/vlc.h>
38
39 #include "../libvlc.h"
40 #include <vlc_vout.h>
41 #include <vlc_aout.h>
42 #include "audio_output/aout_internal.h"
43
44 #include <vlc_access.h>
45 #include <vlc_demux.h>
46 #include <vlc_stream.h>
47
48 #include <vlc_sout.h>
49 #include "stream_output/stream_output.h"
50
51 #include "vlc_playlist.h"
52 #include "vlc_interface.h"
53 #include "vlc_codec.h"
54 #include "vlc_filter.h"
55
56 #include "vlc_httpd.h"
57 #include "vlc_vlm.h"
58 #include "input/vlm_internal.h"
59 #include "vlc_vod.h"
60 #include "vlc_tls.h"
61 #include "vlc_xml.h"
62 #include "vlc_osd.h"
63 #include "vlc_meta.h"
64
65 #include "variables.h"
66 #ifndef WIN32
67 # include <unistd.h>
68 #else
69 # include <io.h>
70 # include <fcntl.h>
71 # include <errno.h> /* ENOSYS */
72 #endif
73 #include <assert.h>
74
75 /*****************************************************************************
76  * Local prototypes
77  *****************************************************************************/
78 static int  DumpCommand( vlc_object_t *, char const *,
79                          vlc_value_t, vlc_value_t, void * );
80
81 static vlc_object_t * FindObject    ( vlc_object_t *, int, int );
82 static vlc_object_t * FindObjectName( vlc_object_t *, const char *, int );
83 static void           DetachObject  ( vlc_object_t * );
84 static void           PrintObject   ( vlc_object_t *, const char * );
85 static void           DumpStructure ( vlc_object_t *, int, char * );
86 static int            FindIndex     ( vlc_object_t *, vlc_object_t **, int );
87 static void           SetAttachment ( vlc_object_t *, vlc_bool_t );
88
89 static vlc_list_t   * NewList       ( int );
90 static void           ListReplace   ( vlc_list_t *, vlc_object_t *, int );
91 /*static void           ListAppend    ( vlc_list_t *, vlc_object_t * );*/
92 static int            CountChildren ( vlc_object_t *, int );
93 static void           ListChildren  ( vlc_list_t *, vlc_object_t *, int );
94
95 static void vlc_object_destroy( vlc_object_t *p_this );
96
97 /*****************************************************************************
98  * Local structure lock
99  *****************************************************************************/
100 static vlc_mutex_t    structure_lock;
101 static vlc_object_internals_t global_internals;
102
103 vlc_object_t *vlc_custom_create( vlc_object_t *p_this, size_t i_size,
104                                  int i_type, const char *psz_type )
105 {
106     vlc_object_t *p_new;
107     vlc_object_internals_t *p_priv;
108
109     if( i_type == VLC_OBJECT_GLOBAL )
110     {
111         p_new = p_this;
112         p_priv = &global_internals;
113         memset( p_priv, 0, sizeof( *p_priv ) );
114     }
115     else
116     {
117         p_priv = calloc( 1, sizeof( *p_priv ) + i_size );
118         if( p_priv == NULL )
119             return NULL;
120
121         p_new = (vlc_object_t *)(p_priv + 1);
122     }
123
124     p_new->p_internals = p_priv;
125     p_new->i_object_type = i_type;
126     p_new->psz_object_type = psz_type;
127
128     p_new->psz_object_name = NULL;
129
130     p_new->b_die = VLC_FALSE;
131     p_new->b_error = VLC_FALSE;
132     p_new->b_dead = VLC_FALSE;
133     p_priv->b_attached = VLC_FALSE;
134     p_new->b_force = VLC_FALSE;
135
136     p_new->psz_header = NULL;
137
138     if( p_this->i_flags & OBJECT_FLAGS_NODBG )
139         p_new->i_flags |= OBJECT_FLAGS_NODBG;
140     if( p_this->i_flags & OBJECT_FLAGS_QUIET )
141         p_new->i_flags |= OBJECT_FLAGS_QUIET;
142     if( p_this->i_flags & OBJECT_FLAGS_NOINTERACT )
143         p_new->i_flags |= OBJECT_FLAGS_NOINTERACT;
144
145     p_priv->p_vars = calloc( sizeof( variable_t ), 16 );
146
147     if( !p_priv->p_vars )
148     {
149         if( i_type != VLC_OBJECT_GLOBAL )
150             free( p_priv );
151         return NULL;
152     }
153
154     if( i_type == VLC_OBJECT_GLOBAL )
155     {
156         /* If i_type is global, then p_new is actually p_libvlc_global */
157         libvlc_global_data_t *p_libvlc_global = (libvlc_global_data_t *)p_new;
158         p_new->p_libvlc = NULL;
159
160         p_libvlc_global->i_counter = 0;
161         p_new->i_object_id = 0;
162
163         p_libvlc_global->i_objects = 1;
164         p_libvlc_global->pp_objects = malloc( sizeof(vlc_object_t *) );
165         p_libvlc_global->pp_objects[0] = p_new;
166         p_priv->b_attached = VLC_TRUE;
167     }
168     else
169     {
170         libvlc_global_data_t *p_libvlc_global = vlc_global();
171         if( i_type == VLC_OBJECT_LIBVLC )
172         {
173             p_new->p_libvlc = (libvlc_int_t*)p_new;
174             p_priv->b_attached = VLC_TRUE;
175         }
176         else
177         {
178             p_new->p_libvlc = p_this->p_libvlc;
179         }
180
181         vlc_mutex_lock( &structure_lock );
182
183         p_libvlc_global->i_counter++;
184         p_new->i_object_id = p_libvlc_global->i_counter;
185
186         /* Wooohaa! If *this* fails, we're in serious trouble! Anyway it's
187          * useless to try and recover anything if pp_objects gets smashed. */
188         TAB_APPEND( p_libvlc_global->i_objects, p_libvlc_global->pp_objects,
189                     p_new );
190
191         vlc_mutex_unlock( &structure_lock );
192     }
193
194     p_priv->i_refcount = 1;
195     p_new->p_parent = NULL;
196     p_new->pp_children = NULL;
197     p_new->i_children = 0;
198
199     p_new->p_private = NULL;
200
201     /* Initialize mutexes and condvars */
202     vlc_mutex_init( p_new, &p_new->object_lock );
203     vlc_cond_init( p_new, &p_new->object_wait );
204     vlc_mutex_init( p_new, &p_priv->var_lock );
205     vlc_spin_init( &p_priv->spin );
206     p_priv->pipes[0] = p_priv->pipes[1] = -1;
207
208     if( i_type == VLC_OBJECT_GLOBAL )
209     {
210         vlc_mutex_init( p_new, &structure_lock );
211     }
212
213     if( i_type == VLC_OBJECT_LIBVLC )
214     {
215         var_Create( p_new, "list", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
216         var_AddCallback( p_new, "list", DumpCommand, NULL );
217         var_Create( p_new, "tree", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
218         var_AddCallback( p_new, "tree", DumpCommand, NULL );
219         var_Create( p_new, "vars", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
220         var_AddCallback( p_new, "vars", DumpCommand, NULL );
221     }
222
223     return p_new;
224 }
225
226
227 /**
228  * Allocates and initializes a vlc object.
229  *
230  * @param i_type known object type (all of them are negative integer values),
231  *               or object byte size (always positive).
232  *
233  * @return the new object, or NULL on error.
234  */
235 void * __vlc_object_create( vlc_object_t *p_this, int i_type )
236 {
237     const char   * psz_type;
238     size_t         i_size;
239
240     switch( i_type )
241     {
242         case VLC_OBJECT_GLOBAL:
243             i_size = sizeof(libvlc_global_data_t);
244             psz_type = "global";
245             break;
246         case VLC_OBJECT_LIBVLC:
247             i_size = sizeof(libvlc_int_t);
248             psz_type = "libvlc";
249             break;
250         case VLC_OBJECT_INTF:
251             i_size = sizeof(intf_thread_t);
252             psz_type = "interface";
253             break;
254         case VLC_OBJECT_DIALOGS:
255             i_size = sizeof(intf_thread_t);
256             psz_type = "dialogs";
257             break;
258         case VLC_OBJECT_PLAYLIST:
259             i_size = sizeof(playlist_t);
260             psz_type = "playlist";
261             break;
262         case VLC_OBJECT_SD:
263             i_size = sizeof(services_discovery_t);
264             psz_type = "services discovery";
265             break;
266         case VLC_OBJECT_INPUT:
267             i_size = sizeof(input_thread_t);
268             psz_type = "input";
269             break;
270         case VLC_OBJECT_DEMUX:
271             i_size = sizeof(demux_t);
272             psz_type = "demux";
273             break;
274         case VLC_OBJECT_ACCESS:
275             i_size = sizeof(access_t);
276             psz_type = "access";
277             break;
278         case VLC_OBJECT_DECODER:
279             i_size = sizeof(decoder_t);
280             psz_type = "decoder";
281             break;
282         case VLC_OBJECT_PACKETIZER:
283             i_size = sizeof(decoder_t);
284             psz_type = "packetizer";
285             break;
286         case VLC_OBJECT_ENCODER:
287             i_size = sizeof(encoder_t);
288             psz_type = "encoder";
289             break;
290         case VLC_OBJECT_FILTER:
291             i_size = sizeof(filter_t);
292             psz_type = "filter";
293             break;
294         case VLC_OBJECT_VOUT:
295             i_size = sizeof(vout_thread_t);
296             psz_type = "video output";
297             break;
298         case VLC_OBJECT_SPU:
299             i_size = sizeof(spu_t);
300             psz_type = "subpicture";
301             break;
302         case VLC_OBJECT_AOUT:
303             i_size = sizeof(aout_instance_t);
304             psz_type = "audio output";
305             break;
306         case VLC_OBJECT_SOUT:
307             i_size = sizeof(sout_instance_t);
308             psz_type = "stream output";
309             break;
310         case VLC_OBJECT_VLM:
311             i_size = sizeof( vlm_t );
312             psz_type = "vlm dameon";
313             break;
314         case VLC_OBJECT_VOD:
315             i_size = sizeof( vod_t );
316             psz_type = "vod server";
317             break;
318         case VLC_OBJECT_XML:
319             i_size = sizeof( xml_t );
320             psz_type = "xml";
321             break;
322         case VLC_OBJECT_OPENGL:
323             i_size = sizeof( vout_thread_t );
324             psz_type = "opengl";
325             break;
326         case VLC_OBJECT_ANNOUNCE:
327             i_size = sizeof( announce_handler_t );
328             psz_type = "announce";
329             break;
330         case VLC_OBJECT_META_ENGINE:
331             i_size = sizeof( meta_engine_t );
332             psz_type = "meta engine";
333             break;
334         case VLC_OBJECT_OSDMENU:
335             i_size = sizeof( osd_menu_t );
336             psz_type = "osd menu";
337             break;
338         default:
339             i_size = i_type > (int)sizeof(vlc_object_t)
340                          ? i_type : (int)sizeof(vlc_object_t);
341             i_type = VLC_OBJECT_GENERIC;
342             psz_type = "generic";
343             break;
344     }
345
346     return vlc_custom_create( p_this, i_size, i_type, psz_type );
347 }
348
349
350 /**
351  ****************************************************************************
352  * Destroy a vlc object (Internal)
353  *
354  * This function destroys an object that has been previously allocated with
355  * vlc_object_create. The object's refcount must be zero and it must not be
356  * attached to other objects in any way.
357  *****************************************************************************/
358 static void vlc_object_destroy( vlc_object_t *p_this )
359 {
360     vlc_object_internals_t *p_priv = vlc_internals( p_this );
361
362     /* FIXME: ugly hack - we cannot use the message queue after
363          * msg_Destroy(). */
364     vlc_object_t *logger = p_this;
365     if( (vlc_object_t *)p_this->p_libvlc == p_this )
366         logger = NULL;
367
368     if( p_this->i_children )
369     {
370         int i;
371
372         fprintf( stderr, "ERROR: cannot delete object (%i, %s) with %d children\n",
373                           p_this->i_object_id, p_this->psz_object_name, p_this->i_children );
374
375         for( i = 0; i < p_this->i_children; i++ )
376         {
377             fprintf( stderr, "ERROR: Remaining children object (id:%i, type:%s, name:%s)\n",
378                               p_this->pp_children[i]->i_object_id,
379                               p_this->pp_children[i]->psz_object_type,
380                               p_this->pp_children[i]->psz_object_name );
381         }
382         fflush(stderr);
383         abort();
384     }
385
386     if( p_this->p_parent )
387     {
388         fprintf( stderr, "ERROR: cannot delete object (id:%i, type:%s, name:%s) with a parent (id:%i, type:%s, name:%s)\n",
389                           p_this->i_object_id,
390                           p_this->psz_object_type,
391                           p_this->psz_object_name,
392                           p_this->p_parent->i_object_id,
393                           p_this->p_parent->psz_object_type,
394                           p_this->p_parent->psz_object_name );
395         fflush(stderr);
396         abort();
397     }
398
399     /* Destroy the associated variables, starting from the end so that
400      * no memmove calls have to be done. */
401     while( p_priv->i_vars )
402     {
403         var_Destroy( p_this, p_priv->p_vars[p_priv->i_vars - 1].psz_name );
404     }
405
406     free( p_priv->p_vars );
407     vlc_mutex_destroy( &p_priv->var_lock );
408
409     if( p_this->psz_header ) free( p_this->psz_header );
410
411     if( p_this->i_object_type == VLC_OBJECT_GLOBAL )
412     {
413         libvlc_global_data_t *p_global = (libvlc_global_data_t *)p_this;
414
415         /* Remove ourselves */
416         int i_index = FindIndex( p_this, p_global->pp_objects,
417                              p_global->i_objects );
418         REMOVE_ELEM( p_global->pp_objects,
419                      p_global->i_objects, i_index );
420
421         /* Test for leaks */
422         if( p_global->i_objects > 0 )
423         {
424             int i;
425             for( i = 0; i < p_global->i_objects; i++ )
426             {
427                 /* We are leaking this object */
428                 fprintf( stderr,
429                       "ERROR: We are leaking object (id:%i, type:%s, name:%s)\n",
430                       p_global->pp_objects[i]->i_object_id,
431                       p_global->pp_objects[i]->psz_object_type,
432                       p_global->pp_objects[i]->psz_object_name );
433                 fflush(stderr);
434             }
435             /* Strongly abort, cause we want these to be fixed */
436             abort();
437         }
438
439         /* We are the global object ... no need to lock. */
440         free( p_global->pp_objects );
441         p_global->pp_objects = NULL;
442
443         vlc_mutex_destroy( &structure_lock );
444     }
445     else
446     {
447         libvlc_global_data_t *p_libvlc_global = vlc_global();
448         int i_index;
449
450         vlc_mutex_lock( &structure_lock );
451
452         /* Wooohaa! If *this* fails, we're in serious trouble! Anyway it's
453          * useless to try and recover anything if pp_objects gets smashed. */
454         i_index = FindIndex( p_this, p_libvlc_global->pp_objects,
455                              p_libvlc_global->i_objects );
456         REMOVE_ELEM( p_libvlc_global->pp_objects,
457                      p_libvlc_global->i_objects, i_index );
458
459         vlc_mutex_unlock( &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                 {
758                     vlc_mutex_unlock( &structure_lock );
759                     pp_objects[i_middle+1]->p_internals->i_refcount++;
760                     return pp_objects[i_middle+1];
761                 }
762                 break;
763             }
764         }
765         else
766         {
767             vlc_mutex_unlock( &structure_lock );
768             pp_objects[i_middle]->p_internals->i_refcount++;
769             return pp_objects[i_middle];
770         }
771
772         if( i_max == 0 )
773         {
774             /* this means that i_max == i_middle, and since we have already
775              * tested pp_objects[i_middle]), p_found is properly set. */
776             break;
777         }
778     }
779
780     vlc_mutex_unlock( &structure_lock );
781     return NULL;
782 }
783
784 /**
785  ****************************************************************************
786  * find a typed object and increment its refcount
787  *****************************************************************************
788  * This function recursively looks for a given object type. i_mode can be one
789  * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
790  *****************************************************************************/
791 void * __vlc_object_find( vlc_object_t *p_this, int i_type, int i_mode )
792 {
793     vlc_object_t *p_found;
794
795     vlc_mutex_lock( &structure_lock );
796
797     /* If we are of the requested type ourselves, don't look further */
798     if( !(i_mode & FIND_STRICT) && p_this->i_object_type == i_type )
799     {
800         p_this->p_internals->i_refcount++;
801         vlc_mutex_unlock( &structure_lock );
802         return p_this;
803     }
804
805     /* Otherwise, recursively look for the object */
806     if( (i_mode & 0x000f) == FIND_ANYWHERE )
807     {
808         vlc_object_t *p_root = p_this;
809
810         /* Find the root */
811         while( p_root->p_parent != NULL &&
812                p_root != VLC_OBJECT( p_this->p_libvlc ) )
813         {
814             p_root = p_root->p_parent;
815         }
816
817         p_found = FindObject( p_root, i_type, (i_mode & ~0x000f)|FIND_CHILD );
818         if( p_found == NULL && p_root != VLC_OBJECT( p_this->p_libvlc ) )
819         {
820             p_found = FindObject( VLC_OBJECT( p_this->p_libvlc ),
821                                   i_type, (i_mode & ~0x000f)|FIND_CHILD );
822         }
823     }
824     else
825     {
826         p_found = FindObject( p_this, i_type, i_mode );
827     }
828
829     vlc_mutex_unlock( &structure_lock );
830
831     return p_found;
832 }
833
834 /**
835  ****************************************************************************
836  * find a named object and increment its refcount
837  *****************************************************************************
838  * This function recursively looks for a given object name. i_mode can be one
839  * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
840  *****************************************************************************/
841 void * __vlc_object_find_name( vlc_object_t *p_this, const char *psz_name,
842                                int i_mode )
843 {
844     vlc_object_t *p_found;
845
846     vlc_mutex_lock( &structure_lock );
847
848     /* If have the requested name ourselves, don't look further */
849     if( !(i_mode & FIND_STRICT)
850         && p_this->psz_object_name
851         && !strcmp( p_this->psz_object_name, psz_name ) )
852     {
853         p_this->p_internals->i_refcount++;
854         vlc_mutex_unlock( &structure_lock );
855         return p_this;
856     }
857
858     /* Otherwise, recursively look for the object */
859     if( (i_mode & 0x000f) == FIND_ANYWHERE )
860     {
861         vlc_object_t *p_root = p_this;
862
863         /* Find the root */
864         while( p_root->p_parent != NULL &&
865                p_root != VLC_OBJECT( p_this->p_libvlc ) )
866         {
867             p_root = p_root->p_parent;
868         }
869
870         p_found = FindObjectName( p_root, psz_name,
871                                  (i_mode & ~0x000f)|FIND_CHILD );
872         if( p_found == NULL && p_root != VLC_OBJECT( p_this->p_libvlc ) )
873         {
874             p_found = FindObjectName( VLC_OBJECT( p_this->p_libvlc ),
875                                       psz_name, (i_mode & ~0x000f)|FIND_CHILD );
876         }
877     }
878     else
879     {
880         p_found = FindObjectName( p_this, psz_name, i_mode );
881     }
882
883     vlc_mutex_unlock( &structure_lock );
884
885     return p_found;
886 }
887
888 /**
889  ****************************************************************************
890  * increment an object refcount
891  *****************************************************************************/
892 void __vlc_object_yield( vlc_object_t *p_this )
893 {
894     vlc_mutex_lock( &structure_lock );
895     p_this->p_internals->i_refcount++;
896     vlc_mutex_unlock( &structure_lock );
897 }
898
899 /*****************************************************************************
900  * decrement an object refcount
901  * And destroy the object if its refcount reach zero.
902  *****************************************************************************/
903 void __vlc_object_release( vlc_object_t *p_this )
904 {
905     vlc_bool_t b_should_destroy;
906
907     vlc_mutex_lock( &structure_lock );
908
909     assert( p_this->p_internals->i_refcount > 0 );
910     p_this->p_internals->i_refcount--;
911     b_should_destroy = (p_this->p_internals->i_refcount == 0);
912
913     vlc_mutex_unlock( &structure_lock );
914
915     if( b_should_destroy )
916         vlc_object_destroy( p_this );
917 }
918
919 /* Version without the lock */
920 static void vlc_object_release_locked( vlc_object_t *p_this )
921 {
922     vlc_bool_t b_should_destroy;
923
924     assert( p_this->p_internals->i_refcount > 0 );
925     p_this->p_internals->i_refcount--;
926     b_should_destroy = (p_this->p_internals->i_refcount == 0);
927
928     if( b_should_destroy )
929         vlc_object_destroy( p_this );
930 }
931
932 /**
933  ****************************************************************************
934  * attach object to a parent object
935  *****************************************************************************
936  * This function sets p_this as a child of p_parent, and p_parent as a parent
937  * of p_this. This link can be undone using vlc_object_detach.
938  *****************************************************************************/
939 void __vlc_object_attach( vlc_object_t *p_this, vlc_object_t *p_parent )
940 {
941     if( !p_this ) return;
942
943     vlc_mutex_lock( &structure_lock );
944
945     /* Attach the parent to its child */
946     p_this->p_parent = p_parent;
947
948     /* Attach the child to its parent */
949     INSERT_ELEM( p_parent->pp_children, p_parent->i_children,
950                  p_parent->i_children, p_this );
951
952     /* Climb up the tree to see whether we are connected with the root */
953     if( p_parent->p_internals->b_attached )
954     {
955         SetAttachment( p_this, VLC_TRUE );
956     }
957
958     vlc_mutex_unlock( &structure_lock );
959 }
960
961 /**
962  ****************************************************************************
963  * detach object from its parent
964  *****************************************************************************
965  * This function removes all links between an object and its parent.
966  *****************************************************************************/
967 void __vlc_object_detach( vlc_object_t *p_this )
968 {
969     if( !p_this ) return;
970
971     vlc_mutex_lock( &structure_lock );
972     if( !p_this->p_parent )
973     {
974         msg_Err( p_this, "object is not attached" );
975         vlc_mutex_unlock( &structure_lock );
976         return;
977     }
978
979     /* Climb up the tree to see whether we are connected with the root */
980     if( p_this->p_parent->p_internals->b_attached )
981     {
982         SetAttachment( p_this, VLC_FALSE );
983     }
984
985     DetachObject( p_this );
986     vlc_mutex_unlock( &structure_lock );
987     p_this = NULL;
988 }
989
990 /**
991  ****************************************************************************
992  * find a list typed objects and increment their refcount
993  *****************************************************************************
994  * This function recursively looks for a given object type. i_mode can be one
995  * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
996  *****************************************************************************/
997 vlc_list_t * __vlc_list_find( vlc_object_t *p_this, int i_type, int i_mode )
998 {
999     vlc_list_t *p_list;
1000     vlc_object_t **pp_current, **pp_end;
1001     int i_count = 0, i_index = 0;
1002     libvlc_global_data_t *p_libvlc_global = vlc_global();
1003
1004     vlc_mutex_lock( &structure_lock );
1005
1006     /* Look for the objects */
1007     switch( i_mode & 0x000f )
1008     {
1009     case FIND_ANYWHERE:
1010         pp_current = p_libvlc_global->pp_objects;
1011         pp_end = pp_current + p_libvlc_global->i_objects;
1012
1013         for( ; pp_current < pp_end ; pp_current++ )
1014         {
1015             if( (*pp_current)->p_internals->b_attached
1016                  && (*pp_current)->i_object_type == i_type )
1017             {
1018                 i_count++;
1019             }
1020         }
1021
1022         p_list = NewList( i_count );
1023         pp_current = p_libvlc_global->pp_objects;
1024
1025         for( ; pp_current < pp_end ; pp_current++ )
1026         {
1027             if( (*pp_current)->p_internals->b_attached
1028                  && (*pp_current)->i_object_type == i_type )
1029             {
1030                 ListReplace( p_list, *pp_current, i_index );
1031                 if( i_index < i_count ) i_index++;
1032             }
1033         }
1034     break;
1035
1036     case FIND_CHILD:
1037         i_count = CountChildren( p_this, i_type );
1038         p_list = NewList( i_count );
1039
1040         /* Check allocation was successful */
1041         if( p_list->i_count != i_count )
1042         {
1043             msg_Err( p_this, "list allocation failed!" );
1044             p_list->i_count = 0;
1045             break;
1046         }
1047
1048         p_list->i_count = 0;
1049         ListChildren( p_list, p_this, i_type );
1050         break;
1051
1052     default:
1053         msg_Err( p_this, "unimplemented!" );
1054         p_list = NewList( 0 );
1055         break;
1056     }
1057
1058     vlc_mutex_unlock( &structure_lock );
1059
1060     return p_list;
1061 }
1062
1063 /*****************************************************************************
1064  * DumpCommand: print the current vlc structure
1065  *****************************************************************************
1066  * This function prints either an ASCII tree showing the connections between
1067  * vlc objects, and additional information such as their refcount, thread ID,
1068  * etc. (command "tree"), or the same data as a simple list (command "list").
1069  *****************************************************************************/
1070 static int DumpCommand( vlc_object_t *p_this, char const *psz_cmd,
1071                         vlc_value_t oldval, vlc_value_t newval, void *p_data )
1072 {
1073     libvlc_global_data_t *p_libvlc_global = vlc_global();
1074
1075     (void)oldval; (void)p_data;
1076     if( *psz_cmd == 'l' )
1077     {
1078         vlc_mutex_lock( &structure_lock );
1079
1080         vlc_object_t **pp_current, **pp_end;
1081
1082         pp_current = p_libvlc_global->pp_objects;
1083         pp_end = pp_current + p_libvlc_global->i_objects;
1084
1085         for( ; pp_current < pp_end ; pp_current++ )
1086         {
1087             if( (*pp_current)->p_internals->b_attached )
1088             {
1089                 PrintObject( *pp_current, "" );
1090             }
1091             else
1092             {
1093                 printf( " o %.8i %s (not attached)\n",
1094                         (*pp_current)->i_object_id,
1095                         (*pp_current)->psz_object_type );
1096             }
1097         }
1098
1099         vlc_mutex_unlock( &structure_lock );
1100     }
1101     else
1102     {
1103         vlc_object_t *p_object = NULL;
1104
1105         if( *newval.psz_string )
1106         {
1107             char *end;
1108             int i_id = strtol( newval.psz_string, &end, 0 );
1109             if( end != newval.psz_string )
1110                 p_object = vlc_object_get( i_id );
1111             else
1112             {
1113                 /* try using the object's name to find it */
1114                 vlc_object_t *p_libvlc = vlc_object_get( 1 );
1115                 if( p_libvlc )
1116                 {
1117                     /* Look in p_libvlc's children tree */
1118                     p_object = vlc_object_find_name( p_libvlc,
1119                                                      newval.psz_string,
1120                                                      FIND_CHILD );
1121                     vlc_object_release( p_libvlc );
1122                 }
1123                 if( !p_object )
1124                 {
1125                     /* If it's not in libvlc, look in libvlc_global (== p_this) */
1126                     p_object = vlc_object_find_name( p_this,
1127                                                      newval.psz_string,
1128                                                      FIND_CHILD );
1129                 }
1130             }
1131
1132             if( !p_object )
1133             {
1134                 return VLC_ENOOBJ;
1135             }
1136         }
1137
1138         vlc_mutex_lock( &structure_lock );
1139
1140         if( *psz_cmd == 't' )
1141         {
1142             char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
1143
1144             if( !p_object )
1145                 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
1146
1147             psz_foo[0] = '|';
1148             DumpStructure( p_object, 0, psz_foo );
1149         }
1150         else if( *psz_cmd == 'v' )
1151         {
1152             int i;
1153
1154             if( !p_object )
1155                 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
1156
1157             PrintObject( p_object, "" );
1158
1159             if( !p_object->p_internals->i_vars )
1160                 printf( " `-o No variables\n" );
1161             for( i = 0; i < p_object->p_internals->i_vars; i++ )
1162             {
1163                 variable_t *p_var = p_object->p_internals->p_vars + i;
1164
1165                 const char *psz_type = "unknown";
1166                 switch( p_var->i_type & VLC_VAR_TYPE )
1167                 {
1168 #define MYCASE( type, nice )                \
1169                     case VLC_VAR_ ## type:  \
1170                         psz_type = nice;    \
1171                         break;
1172                     MYCASE( VOID, "void" );
1173                     MYCASE( BOOL, "bool" );
1174                     MYCASE( INTEGER, "integer" );
1175                     MYCASE( HOTKEY, "hotkey" );
1176                     MYCASE( STRING, "string" );
1177                     MYCASE( MODULE, "module" );
1178                     MYCASE( FILE, "file" );
1179                     MYCASE( DIRECTORY, "directory" );
1180                     MYCASE( VARIABLE, "variable" );
1181                     MYCASE( FLOAT, "float" );
1182                     MYCASE( TIME, "time" );
1183                     MYCASE( ADDRESS, "address" );
1184                     MYCASE( MUTEX, "mutex" );
1185                     MYCASE( LIST, "list" );
1186 #undef MYCASE
1187                 }
1188                 printf( " %c-o \"%s\" (%s",
1189                         i + 1 == p_object->p_internals->i_vars ? '`' : '|',
1190                         p_var->psz_name, psz_type );
1191                 if( p_var->psz_text )
1192                     printf( ", %s", p_var->psz_text );
1193                 printf( ")" );
1194                 if( p_var->i_type & VLC_VAR_ISCOMMAND )
1195                     printf( ", command" );
1196                 if( p_var->i_entries )
1197                     printf( ", %d callbacks", p_var->i_entries );
1198                 switch( p_var->i_type & 0x00f0 )
1199                 {
1200                     case VLC_VAR_VOID:
1201                     case VLC_VAR_MUTEX:
1202                         break;
1203                     case VLC_VAR_BOOL:
1204                         printf( ": %s", p_var->val.b_bool ? "true" : "false" );
1205                         break;
1206                     case VLC_VAR_INTEGER:
1207                         printf( ": %d", p_var->val.i_int );
1208                         break;
1209                     case VLC_VAR_STRING:
1210                         printf( ": \"%s\"", p_var->val.psz_string );
1211                         break;
1212                     case VLC_VAR_FLOAT:
1213                         printf( ": %f", p_var->val.f_float );
1214                         break;
1215                     case VLC_VAR_TIME:
1216                         printf( ": " I64Fi, (int64_t)p_var->val.i_time );
1217                         break;
1218                     case VLC_VAR_ADDRESS:
1219                         printf( ": %p", p_var->val.p_address );
1220                         break;
1221                     case VLC_VAR_LIST:
1222                         printf( ": TODO" );
1223                         break;
1224                 }
1225                 printf( "\n" );
1226             }
1227         }
1228
1229         vlc_mutex_unlock( &structure_lock );
1230
1231         if( *newval.psz_string )
1232         {
1233             vlc_object_release( p_object );
1234         }
1235     }
1236
1237     return VLC_SUCCESS;
1238 }
1239
1240 /*****************************************************************************
1241  * vlc_list_release: free a list previously allocated by vlc_list_find
1242  *****************************************************************************
1243  * This function decreases the refcount of all objects in the list and
1244  * frees the list.
1245  *****************************************************************************/
1246 void vlc_list_release( vlc_list_t *p_list )
1247 {
1248     int i_index;
1249
1250     vlc_mutex_lock( &structure_lock );
1251     for( i_index = 0; i_index < p_list->i_count; i_index++ )
1252     {
1253         vlc_object_release_locked( p_list->p_values[i_index].p_object );
1254     }
1255     vlc_mutex_unlock( &structure_lock );
1256
1257     free( p_list->p_values );
1258     free( p_list );
1259 }
1260
1261 /* Following functions are local */
1262
1263 /*****************************************************************************
1264  * FindIndex: find the index of an object in an array of objects
1265  *****************************************************************************
1266  * This function assumes that p_this can be found in pp_objects. It will not
1267  * crash if p_this cannot be found, but will return a wrong value. It is your
1268  * duty to check the return value if you are not certain that the object could
1269  * be found for sure.
1270  *****************************************************************************/
1271 static int FindIndex( vlc_object_t *p_this,
1272                       vlc_object_t **pp_objects, int i_count )
1273 {
1274     int i_middle = i_count / 2;
1275
1276     if( i_count == 0 )
1277     {
1278         return 0;
1279     }
1280
1281     if( pp_objects[i_middle] == p_this )
1282     {
1283         return i_middle;
1284     }
1285
1286     if( i_count == 1 )
1287     {
1288         return 0;
1289     }
1290
1291     /* We take advantage of the sorted array */
1292     if( pp_objects[i_middle]->i_object_id < p_this->i_object_id )
1293     {
1294         return i_middle + FindIndex( p_this, pp_objects + i_middle,
1295                                              i_count - i_middle );
1296     }
1297     else
1298     {
1299         return FindIndex( p_this, pp_objects, i_middle );
1300     }
1301 }
1302
1303 static vlc_object_t * FindObject( vlc_object_t *p_this, int i_type, int i_mode )
1304 {
1305     int i;
1306     vlc_object_t *p_tmp;
1307
1308     switch( i_mode & 0x000f )
1309     {
1310     case FIND_PARENT:
1311         p_tmp = p_this->p_parent;
1312         if( p_tmp )
1313         {
1314             if( p_tmp->i_object_type == i_type )
1315             {
1316                 p_tmp->p_internals->i_refcount++;
1317                 return p_tmp;
1318             }
1319             else
1320             {
1321                 return FindObject( p_tmp, i_type, i_mode );
1322             }
1323         }
1324         break;
1325
1326     case FIND_CHILD:
1327         for( i = p_this->i_children; i--; )
1328         {
1329             p_tmp = p_this->pp_children[i];
1330             if( p_tmp->i_object_type == i_type )
1331             {
1332                 p_tmp->p_internals->i_refcount++;
1333                 return p_tmp;
1334             }
1335             else if( p_tmp->i_children )
1336             {
1337                 p_tmp = FindObject( p_tmp, i_type, i_mode );
1338                 if( p_tmp )
1339                 {
1340                     return p_tmp;
1341                 }
1342             }
1343         }
1344         break;
1345
1346     case FIND_ANYWHERE:
1347         /* Handled in vlc_object_find */
1348         break;
1349     }
1350
1351     return NULL;
1352 }
1353
1354 static vlc_object_t * FindObjectName( vlc_object_t *p_this,
1355                                       const char *psz_name,
1356                                       int i_mode )
1357 {
1358     int i;
1359     vlc_object_t *p_tmp;
1360
1361     switch( i_mode & 0x000f )
1362     {
1363     case FIND_PARENT:
1364         p_tmp = p_this->p_parent;
1365         if( p_tmp )
1366         {
1367             if( p_tmp->psz_object_name
1368                 && !strcmp( p_tmp->psz_object_name, psz_name ) )
1369             {
1370                 p_tmp->p_internals->i_refcount++;
1371                 return p_tmp;
1372             }
1373             else
1374             {
1375                 return FindObjectName( p_tmp, psz_name, i_mode );
1376             }
1377         }
1378         break;
1379
1380     case FIND_CHILD:
1381         for( i = p_this->i_children; i--; )
1382         {
1383             p_tmp = p_this->pp_children[i];
1384             if( p_tmp->psz_object_name
1385                 && !strcmp( p_tmp->psz_object_name, psz_name ) )
1386             {
1387                 p_tmp->p_internals->i_refcount++;
1388                 return p_tmp;
1389             }
1390             else if( p_tmp->i_children )
1391             {
1392                 p_tmp = FindObjectName( p_tmp, psz_name, i_mode );
1393                 if( p_tmp )
1394                 {
1395                     return p_tmp;
1396                 }
1397             }
1398         }
1399         break;
1400
1401     case FIND_ANYWHERE:
1402         /* Handled in vlc_object_find */
1403         break;
1404     }
1405
1406     return NULL;
1407 }
1408
1409 static void DetachObject( vlc_object_t *p_this )
1410 {
1411     vlc_object_t *p_parent = p_this->p_parent;
1412     int i_index, i;
1413
1414     /* Remove p_this's parent */
1415     p_this->p_parent = NULL;
1416
1417     /* Remove all of p_parent's children which are p_this */
1418     for( i_index = p_parent->i_children ; i_index-- ; )
1419     {
1420         if( p_parent->pp_children[i_index] == p_this )
1421         {
1422             p_parent->i_children--;
1423             for( i = i_index ; i < p_parent->i_children ; i++ )
1424             {
1425                 p_parent->pp_children[i] = p_parent->pp_children[i+1];
1426             }
1427         }
1428     }
1429
1430     if( p_parent->i_children )
1431     {
1432         p_parent->pp_children = (vlc_object_t **)realloc( p_parent->pp_children,
1433                                p_parent->i_children * sizeof(vlc_object_t *) );
1434     }
1435     else
1436     {
1437         free( p_parent->pp_children );
1438         p_parent->pp_children = NULL;
1439     }
1440 }
1441
1442 /*****************************************************************************
1443  * SetAttachment: recursively set the b_attached flag of a subtree.
1444  *****************************************************************************
1445  * This function is used by the attach and detach functions to propagate
1446  * the b_attached flag in a subtree.
1447  *****************************************************************************/
1448 static void SetAttachment( vlc_object_t *p_this, vlc_bool_t b_attached )
1449 {
1450     int i_index;
1451
1452     for( i_index = p_this->i_children ; i_index-- ; )
1453     {
1454         SetAttachment( p_this->pp_children[i_index], b_attached );
1455     }
1456
1457     p_this->p_internals->b_attached = b_attached;
1458 }
1459
1460 static void PrintObject( vlc_object_t *p_this, const char *psz_prefix )
1461 {
1462     char psz_children[20], psz_refcount[20], psz_thread[30], psz_name[50],
1463          psz_parent[20];
1464
1465     psz_name[0] = '\0';
1466     if( p_this->psz_object_name )
1467     {
1468         snprintf( psz_name, 49, " \"%s\"", p_this->psz_object_name );
1469         if( psz_name[48] )
1470             psz_name[48] = '\"';
1471     }
1472
1473     psz_children[0] = '\0';
1474     switch( p_this->i_children )
1475     {
1476         case 0:
1477             break;
1478         case 1:
1479             strcpy( psz_children, ", 1 child" );
1480             break;
1481         default:
1482             snprintf( psz_children, 19, ", %i children", p_this->i_children );
1483             break;
1484     }
1485
1486     psz_refcount[0] = '\0';
1487     if( p_this->p_internals->i_refcount > 0 )
1488         snprintf( psz_refcount, 19, ", refcount %u",
1489                   p_this->p_internals->i_refcount );
1490
1491     psz_thread[0] = '\0';
1492     if( p_this->p_internals->b_thread )
1493         snprintf( psz_thread, 29, " (thread %u)",
1494 #if defined(WIN32) || defined(UNDER_CE)
1495                   (unsigned)p_this->p_internals->thread_id.id );
1496 #else
1497                   (unsigned)p_this->p_internals->thread_id );
1498 #endif
1499
1500     psz_parent[0] = '\0';
1501     if( p_this->p_parent )
1502         snprintf( psz_parent, 19, ", parent %i", p_this->p_parent->i_object_id );
1503
1504     printf( " %so %.8i %s%s%s%s%s%s\n", psz_prefix,
1505             p_this->i_object_id, p_this->psz_object_type,
1506             psz_name, psz_thread, psz_refcount, psz_children,
1507             psz_parent );
1508 }
1509
1510 static void DumpStructure( vlc_object_t *p_this, int i_level, char *psz_foo )
1511 {
1512     int i;
1513     char i_back = psz_foo[i_level];
1514     psz_foo[i_level] = '\0';
1515
1516     PrintObject( p_this, psz_foo );
1517
1518     psz_foo[i_level] = i_back;
1519
1520     if( i_level / 2 >= MAX_DUMPSTRUCTURE_DEPTH )
1521     {
1522         msg_Warn( p_this, "structure tree is too deep" );
1523         return;
1524     }
1525
1526     for( i = 0 ; i < p_this->i_children ; i++ )
1527     {
1528         if( i_level )
1529         {
1530             psz_foo[i_level-1] = ' ';
1531
1532             if( psz_foo[i_level-2] == '`' )
1533             {
1534                 psz_foo[i_level-2] = ' ';
1535             }
1536         }
1537
1538         if( i == p_this->i_children - 1 )
1539         {
1540             psz_foo[i_level] = '`';
1541         }
1542         else
1543         {
1544             psz_foo[i_level] = '|';
1545         }
1546
1547         psz_foo[i_level+1] = '-';
1548         psz_foo[i_level+2] = '\0';
1549
1550         DumpStructure( p_this->pp_children[i], i_level + 2, psz_foo );
1551     }
1552 }
1553
1554 static vlc_list_t * NewList( int i_count )
1555 {
1556     vlc_list_t * p_list = (vlc_list_t *)malloc( sizeof( vlc_list_t ) );
1557     if( p_list == NULL )
1558     {
1559         return NULL;
1560     }
1561
1562     p_list->i_count = i_count;
1563
1564     if( i_count == 0 )
1565     {
1566         p_list->p_values = NULL;
1567         return p_list;
1568     }
1569
1570     p_list->p_values = malloc( i_count * sizeof( vlc_value_t ) );
1571     if( p_list->p_values == NULL )
1572     {
1573         p_list->i_count = 0;
1574         return p_list;
1575     }
1576
1577     return p_list;
1578 }
1579
1580 static void ListReplace( vlc_list_t *p_list, vlc_object_t *p_object,
1581                          int i_index )
1582 {
1583     if( p_list == NULL || i_index >= p_list->i_count )
1584     {
1585         return;
1586     }
1587
1588     p_object->p_internals->i_refcount++;
1589
1590     p_list->p_values[i_index].p_object = p_object;
1591
1592     return;
1593 }
1594
1595 /*static void ListAppend( vlc_list_t *p_list, vlc_object_t *p_object )
1596 {
1597     if( p_list == NULL )
1598     {
1599         return;
1600     }
1601
1602     p_list->p_values = realloc( p_list->p_values, (p_list->i_count + 1)
1603                                 * sizeof( vlc_value_t ) );
1604     if( p_list->p_values == NULL )
1605     {
1606         p_list->i_count = 0;
1607         return;
1608     }
1609
1610     p_object->p_internals->i_refcount++;
1611
1612     p_list->p_values[p_list->i_count].p_object = p_object;
1613     p_list->i_count++;
1614
1615     return;
1616 }*/
1617
1618 static int CountChildren( vlc_object_t *p_this, int i_type )
1619 {
1620     vlc_object_t *p_tmp;
1621     int i, i_count = 0;
1622
1623     for( i = 0; i < p_this->i_children; i++ )
1624     {
1625         p_tmp = p_this->pp_children[i];
1626
1627         if( p_tmp->i_object_type == i_type )
1628         {
1629             i_count++;
1630         }
1631
1632         if( p_tmp->i_children )
1633         {
1634             i_count += CountChildren( p_tmp, i_type );
1635         }
1636     }
1637
1638     return i_count;
1639 }
1640
1641 static void ListChildren( vlc_list_t *p_list, vlc_object_t *p_this, int i_type )
1642 {
1643     vlc_object_t *p_tmp;
1644     int i;
1645
1646     for( i = 0; i < p_this->i_children; i++ )
1647     {
1648         p_tmp = p_this->pp_children[i];
1649
1650         if( p_tmp->i_object_type == i_type )
1651         {
1652             ListReplace( p_list, p_tmp, p_list->i_count++ );
1653         }
1654
1655         if( p_tmp->i_children )
1656         {
1657             ListChildren( p_list, p_tmp, i_type );
1658         }
1659     }
1660 }