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