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