Include modules.h whenever needed
[vlc.git] / 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 #include <vlc/vlc.h>
34
35 #ifdef HAVE_STDLIB_H
36 #   include <stdlib.h>                                          /* realloc() */
37 #endif
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
67 /*****************************************************************************
68  * Local prototypes
69  *****************************************************************************/
70 static int  DumpCommand( vlc_object_t *, char const *,
71                          vlc_value_t, vlc_value_t, void * );
72
73 static vlc_object_t * FindObject    ( vlc_object_t *, int, int );
74 static vlc_object_t * FindObjectName( vlc_object_t *, const char *, int );
75 static void           DetachObject  ( vlc_object_t * );
76 static void           PrintObject   ( vlc_object_t *, const char * );
77 static void           DumpStructure ( vlc_object_t *, int, char * );
78 static int            FindIndex     ( vlc_object_t *, vlc_object_t **, int );
79 static void           SetAttachment ( vlc_object_t *, vlc_bool_t );
80
81 static vlc_list_t   * NewList       ( int );
82 static void           ListReplace   ( vlc_list_t *, vlc_object_t *, int );
83 /*static void           ListAppend    ( vlc_list_t *, vlc_object_t * );*/
84 static int            CountChildren ( vlc_object_t *, int );
85 static void           ListChildren  ( vlc_list_t *, vlc_object_t *, int );
86
87 /*****************************************************************************
88  * Local structure lock
89  *****************************************************************************/
90 static vlc_mutex_t    structure_lock;
91
92 vlc_object_t *vlc_custom_create( vlc_object_t *p_this, size_t i_size,
93                                  int i_type, const char *psz_type )
94 {
95     vlc_object_t * p_new = NULL;
96
97     if( i_type == VLC_OBJECT_GLOBAL )
98     {
99         p_new = p_this;
100     }
101     else
102     {
103         p_new = malloc( i_size );
104         if( !p_new ) return NULL;
105         memset( p_new, 0, i_size );
106     }
107
108     p_new->i_object_type = i_type;
109     p_new->psz_object_type = psz_type;
110
111     p_new->psz_object_name = NULL;
112
113     p_new->b_die = VLC_FALSE;
114     p_new->b_error = VLC_FALSE;
115     p_new->b_dead = VLC_FALSE;
116     p_new->b_attached = VLC_FALSE;
117     p_new->b_force = VLC_FALSE;
118
119     p_new->psz_header = NULL;
120
121     p_new->i_flags = 0;
122     if( p_this->i_flags & OBJECT_FLAGS_NODBG )
123         p_new->i_flags |= OBJECT_FLAGS_NODBG;
124     if( p_this->i_flags & OBJECT_FLAGS_QUIET )
125         p_new->i_flags |= OBJECT_FLAGS_QUIET;
126     if( p_this->i_flags & OBJECT_FLAGS_NOINTERACT )
127         p_new->i_flags |= OBJECT_FLAGS_NOINTERACT;
128
129     p_new->i_vars = 0;
130     p_new->p_vars = (variable_t *)malloc( 16 * sizeof( variable_t ) );
131
132     if( !p_new->p_vars )
133     {
134         if( i_type != VLC_OBJECT_GLOBAL )
135             free( p_new );
136         return NULL;
137     }
138
139     if( i_type == VLC_OBJECT_GLOBAL )
140     {
141         /* If i_type is global, then p_new is actually p_libvlc_global */
142         p_new->p_libvlc_global = (libvlc_global_data_t*)p_new;
143         p_new->p_libvlc = NULL;
144
145         p_new->p_libvlc_global->i_counter = 0;
146         p_new->i_object_id = 0;
147
148         p_new->p_libvlc_global->i_objects = 1;
149         p_new->p_libvlc_global->pp_objects = malloc( sizeof(vlc_object_t *) );
150         p_new->p_libvlc_global->pp_objects[0] = p_new;
151         p_new->b_attached = VLC_TRUE;
152     }
153     else
154     {
155         p_new->p_libvlc_global = p_this->p_libvlc_global;
156         p_new->p_libvlc = ( i_type == VLC_OBJECT_LIBVLC ) ? (libvlc_int_t*)p_new
157                                                        : p_this->p_libvlc;
158
159         vlc_mutex_lock( &structure_lock );
160
161         p_new->p_libvlc_global->i_counter++;
162         p_new->i_object_id = p_new->p_libvlc_global->i_counter;
163
164         /* Wooohaa! If *this* fails, we're in serious trouble! Anyway it's
165          * useless to try and recover anything if pp_objects gets smashed. */
166         TAB_APPEND( p_new->p_libvlc_global->i_objects,
167                     p_new->p_libvlc_global->pp_objects,
168                     p_new );
169
170         vlc_mutex_unlock( &structure_lock );
171     }
172
173     p_new->i_refcount = 0;
174     p_new->p_parent = NULL;
175     p_new->pp_children = NULL;
176     p_new->i_children = 0;
177
178     p_new->p_private = NULL;
179
180     /* Initialize mutexes and condvars */
181     vlc_mutex_init( p_new, &p_new->object_lock );
182     vlc_cond_init( p_new, &p_new->object_wait );
183     vlc_mutex_init( p_new, &p_new->var_lock );
184
185     if( i_type == VLC_OBJECT_GLOBAL )
186     {
187         vlc_mutex_init( p_new, &structure_lock );
188
189         var_Create( p_new, "list", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
190         var_AddCallback( p_new, "list", DumpCommand, NULL );
191         var_Create( p_new, "tree", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
192         var_AddCallback( p_new, "tree", DumpCommand, NULL );
193         var_Create( p_new, "vars", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
194         var_AddCallback( p_new, "vars", DumpCommand, NULL );
195     }
196
197     return p_new;
198 }
199
200
201 /**
202  * Allocates and initializes a vlc object.
203  *
204  * @param i_type known object type (all of them are negative integer values),
205  *               or object byte size (always positive).
206  *
207  * @return the new object, or NULL on error.
208  */
209 void * __vlc_object_create( vlc_object_t *p_this, int i_type )
210 {
211     const char   * psz_type;
212     size_t         i_size;
213
214     switch( i_type )
215     {
216         case VLC_OBJECT_GLOBAL:
217             i_size = sizeof(libvlc_global_data_t);
218             psz_type = "global";
219             break;
220         case VLC_OBJECT_LIBVLC:
221             i_size = sizeof(libvlc_int_t);
222             psz_type = "libvlc";
223             break;
224         case VLC_OBJECT_INTF:
225             i_size = sizeof(intf_thread_t);
226             psz_type = "interface";
227             break;
228         case VLC_OBJECT_DIALOGS:
229             i_size = sizeof(intf_thread_t);
230             psz_type = "dialogs";
231             break;
232         case VLC_OBJECT_PLAYLIST:
233             i_size = sizeof(playlist_t);
234             psz_type = "playlist";
235             break;
236         case VLC_OBJECT_SD:
237             i_size = sizeof(services_discovery_t);
238             psz_type = "services discovery";
239             break;
240         case VLC_OBJECT_INPUT:
241             i_size = sizeof(input_thread_t);
242             psz_type = "input";
243             break;
244         case VLC_OBJECT_DEMUX:
245             i_size = sizeof(demux_t);
246             psz_type = "demux";
247             break;
248         case VLC_OBJECT_STREAM:
249             i_size = sizeof(stream_t);
250             psz_type = "stream";
251             break;
252         case VLC_OBJECT_ACCESS:
253             i_size = sizeof(access_t);
254             psz_type = "access";
255             break;
256         case VLC_OBJECT_DECODER:
257             i_size = sizeof(decoder_t);
258             psz_type = "decoder";
259             break;
260         case VLC_OBJECT_PACKETIZER:
261             i_size = sizeof(decoder_t);
262             psz_type = "packetizer";
263             break;
264         case VLC_OBJECT_ENCODER:
265             i_size = sizeof(encoder_t);
266             psz_type = "encoder";
267             break;
268         case VLC_OBJECT_FILTER:
269             i_size = sizeof(filter_t);
270             psz_type = "filter";
271             break;
272         case VLC_OBJECT_VOUT:
273             i_size = sizeof(vout_thread_t);
274             psz_type = "video output";
275             break;
276         case VLC_OBJECT_SPU:
277             i_size = sizeof(spu_t);
278             psz_type = "subpicture";
279             break;
280         case VLC_OBJECT_AOUT:
281             i_size = sizeof(aout_instance_t);
282             psz_type = "audio output";
283             break;
284         case VLC_OBJECT_SOUT:
285             i_size = sizeof(sout_instance_t);
286             psz_type = "stream output";
287             break;
288         case VLC_OBJECT_VLM:
289             i_size = sizeof( vlm_t );
290             psz_type = "vlm dameon";
291             break;
292         case VLC_OBJECT_VOD:
293             i_size = sizeof( vod_t );
294             psz_type = "vod server";
295             break;
296         case VLC_OBJECT_TLS:
297             i_size = sizeof( tls_t );
298             psz_type = "tls";
299             break;
300         case VLC_OBJECT_XML:
301             i_size = sizeof( xml_t );
302             psz_type = "xml";
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_META_ENGINE:
313             i_size = sizeof( meta_engine_t );
314             psz_type = "meta engine";
315             break;
316         case VLC_OBJECT_OSDMENU:
317             i_size = sizeof( osd_menu_t );
318             psz_type = "osd menu";
319             break;
320         default:
321             i_size = i_type > (int)sizeof(vlc_object_t)
322                          ? i_type : (int)sizeof(vlc_object_t);
323             i_type = VLC_OBJECT_GENERIC;
324             psz_type = "generic";
325             break;
326     }
327
328     return vlc_custom_create( p_this, i_size, i_type, psz_type );
329 }
330
331
332 /**
333  ****************************************************************************
334  * Destroy a vlc object
335  *
336  * This function destroys an object that has been previously allocated with
337  * vlc_object_create. The object's refcount must be zero and it must not be
338  * attached to other objects in any way.
339  *****************************************************************************/
340 void __vlc_object_destroy( vlc_object_t *p_this )
341 {
342     int i_delay = 0;
343
344     if( p_this->i_children )
345     {
346         msg_Err( p_this, "cannot delete object (%i, %s) with children" ,
347                  p_this->i_object_id, p_this->psz_object_name );
348         return;
349     }
350
351     if( p_this->p_parent )
352     {
353         msg_Err( p_this, "cannot delete object (%i, %s) with a parent",
354                  p_this->i_object_id, p_this->psz_object_name );
355         return;
356     }
357
358     while( p_this->i_refcount )
359     {
360         i_delay++;
361
362         /* Don't warn immediately ... 100ms seems OK */
363         if( i_delay == 2 )
364         {
365             msg_Warn( p_this,
366                   "refcount is %i, delaying before deletion (id=%d,type=%d)",
367                   p_this->i_refcount, p_this->i_object_id,
368                   p_this->i_object_type );
369         }
370         else if( i_delay == 10 )
371         {
372             msg_Err( p_this,
373                   "refcount is %i, delaying again (id=%d,type=%d)",
374                   p_this->i_refcount, p_this->i_object_id,
375                   p_this->i_object_type );
376         }
377         else if( i_delay == 20 )
378         {
379             msg_Err( p_this,
380                   "waited too long, cancelling destruction (id=%d,type=%d)",
381                   p_this->i_object_id, p_this->i_object_type );
382             return;
383         }
384
385         msleep( 100000 );
386     }
387
388     /* Destroy the associated variables, starting from the end so that
389      * no memmove calls have to be done. */
390     while( p_this->i_vars )
391     {
392         var_Destroy( p_this, p_this->p_vars[p_this->i_vars - 1].psz_name );
393     }
394
395     free( p_this->p_vars );
396     vlc_mutex_destroy( &p_this->var_lock );
397
398     if( p_this->psz_header ) free( p_this->psz_header );
399
400     if( p_this->i_object_type == VLC_OBJECT_GLOBAL )
401     {
402         /* We are the global object ... no need to lock. */
403         free( p_this->p_libvlc_global->pp_objects );
404         p_this->p_libvlc_global->pp_objects = NULL;
405         p_this->p_libvlc_global->i_objects--;
406
407         vlc_mutex_destroy( &structure_lock );
408     }
409     else
410     {
411         int i_index;
412
413         vlc_mutex_lock( &structure_lock );
414
415         /* Wooohaa! If *this* fails, we're in serious trouble! Anyway it's
416          * useless to try and recover anything if pp_objects gets smashed. */
417         i_index = FindIndex( p_this, p_this->p_libvlc_global->pp_objects,
418                              p_this->p_libvlc_global->i_objects );
419         REMOVE_ELEM( p_this->p_libvlc_global->pp_objects,
420                      p_this->p_libvlc_global->i_objects, i_index );
421
422         vlc_mutex_unlock( &structure_lock );
423     }
424
425     vlc_mutex_destroy( &p_this->object_lock );
426     vlc_cond_destroy( &p_this->object_wait );
427
428     /* global is not dynamically allocated by vlc_object_create */
429     if( p_this->i_object_type != VLC_OBJECT_GLOBAL )
430     {
431         free( p_this );
432         p_this = NULL;
433     }
434 }
435
436
437 void __vlc_object_kill( vlc_object_t *p_this )
438 {
439     vlc_mutex_lock( &p_this->object_lock );
440     p_this->b_die = VLC_TRUE;
441     vlc_mutex_unlock( &p_this->object_lock );
442 }
443
444
445 /**
446  * find an object given its ID
447  *
448  * This function looks for the object whose i_object_id field is i_id. We
449  * use a dichotomy so that lookups are in log2(n).
450  *****************************************************************************/
451 void * __vlc_object_get( vlc_object_t *p_this, int i_id )
452 {
453     int i_max, i_middle;
454     vlc_object_t **pp_objects;
455
456     vlc_mutex_lock( &structure_lock );
457
458     pp_objects = p_this->p_libvlc_global->pp_objects;
459
460     /* Perform our dichotomy */
461     for( i_max = p_this->p_libvlc_global->i_objects - 1 ; ; )
462     {
463         i_middle = i_max / 2;
464
465         if( pp_objects[i_middle]->i_object_id > i_id )
466         {
467             i_max = i_middle;
468         }
469         else if( pp_objects[i_middle]->i_object_id < i_id )
470         {
471             if( i_middle )
472             {
473                 pp_objects += i_middle;
474                 i_max -= i_middle;
475             }
476             else
477             {
478                 /* This happens when there are only two remaining objects */
479                 if( pp_objects[i_middle+1]->i_object_id == i_id )
480                 {
481                     vlc_mutex_unlock( &structure_lock );
482                     pp_objects[i_middle+1]->i_refcount++;
483                     return pp_objects[i_middle+1];
484                 }
485                 break;
486             }
487         }
488         else
489         {
490             vlc_mutex_unlock( &structure_lock );
491             pp_objects[i_middle]->i_refcount++;
492             return pp_objects[i_middle];
493         }
494
495         if( i_max == 0 )
496         {
497             /* this means that i_max == i_middle, and since we have already
498              * tested pp_objects[i_middle]), p_found is properly set. */
499             break;
500         }
501     }
502
503     vlc_mutex_unlock( &structure_lock );
504     return NULL;
505 }
506
507 /**
508  ****************************************************************************
509  * find a typed object and increment its refcount
510  *****************************************************************************
511  * This function recursively looks for a given object type. i_mode can be one
512  * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
513  *****************************************************************************/
514 void * __vlc_object_find( vlc_object_t *p_this, int i_type, int i_mode )
515 {
516     vlc_object_t *p_found;
517
518     vlc_mutex_lock( &structure_lock );
519
520     /* If we are of the requested type ourselves, don't look further */
521     if( !(i_mode & FIND_STRICT) && p_this->i_object_type == i_type )
522     {
523         p_this->i_refcount++;
524         vlc_mutex_unlock( &structure_lock );
525         return p_this;
526     }
527
528     /* Otherwise, recursively look for the object */
529     if( (i_mode & 0x000f) == FIND_ANYWHERE )
530     {
531         vlc_object_t *p_root = p_this;
532
533         /* Find the root */
534         while( p_root->p_parent != NULL &&
535                p_root != VLC_OBJECT( p_this->p_libvlc ) )
536         {
537             p_root = p_root->p_parent;
538         }
539
540         p_found = FindObject( p_root, i_type, (i_mode & ~0x000f)|FIND_CHILD );
541         if( p_found == NULL && p_root != VLC_OBJECT( p_this->p_libvlc ) )
542         {
543             p_found = FindObject( VLC_OBJECT( p_this->p_libvlc ),
544                                   i_type, (i_mode & ~0x000f)|FIND_CHILD );
545         }
546     }
547     else
548     {
549         p_found = FindObject( p_this, i_type, i_mode );
550     }
551
552     vlc_mutex_unlock( &structure_lock );
553
554     return p_found;
555 }
556
557 /**
558  ****************************************************************************
559  * find a named object and increment its refcount
560  *****************************************************************************
561  * This function recursively looks for a given object name. i_mode can be one
562  * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
563  *****************************************************************************/
564 void * __vlc_object_find_name( vlc_object_t *p_this, const char *psz_name,
565                                int i_mode )
566 {
567     vlc_object_t *p_found;
568
569     vlc_mutex_lock( &structure_lock );
570
571     /* If have the requested name ourselves, don't look further */
572     if( !(i_mode & FIND_STRICT)
573         && p_this->psz_object_name
574         && !strcmp( p_this->psz_object_name, psz_name ) )
575     {
576         p_this->i_refcount++;
577         vlc_mutex_unlock( &structure_lock );
578         return p_this;
579     }
580
581     /* Otherwise, recursively look for the object */
582     if( (i_mode & 0x000f) == FIND_ANYWHERE )
583     {
584         vlc_object_t *p_root = p_this;
585
586         /* Find the root */
587         while( p_root->p_parent != NULL &&
588                p_root != VLC_OBJECT( p_this->p_libvlc ) )
589         {
590             p_root = p_root->p_parent;
591         }
592
593         p_found = FindObjectName( p_root, psz_name,
594                                  (i_mode & ~0x000f)|FIND_CHILD );
595         if( p_found == NULL && p_root != VLC_OBJECT( p_this->p_libvlc ) )
596         {
597             p_found = FindObjectName( VLC_OBJECT( p_this->p_libvlc ),
598                                       psz_name, (i_mode & ~0x000f)|FIND_CHILD );
599         }
600     }
601     else
602     {
603         p_found = FindObjectName( p_this, psz_name, i_mode );
604     }
605
606     vlc_mutex_unlock( &structure_lock );
607
608     return p_found;
609 }
610
611 /**
612  ****************************************************************************
613  * increment an object refcount
614  *****************************************************************************/
615 void __vlc_object_yield( vlc_object_t *p_this )
616 {
617     vlc_mutex_lock( &structure_lock );
618     p_this->i_refcount++;
619     vlc_mutex_unlock( &structure_lock );
620 }
621
622 /**
623  ****************************************************************************
624  * decrement an object refcount
625  *****************************************************************************/
626 void __vlc_object_release( vlc_object_t *p_this )
627 {
628     vlc_mutex_lock( &structure_lock );
629     p_this->i_refcount--;
630     vlc_mutex_unlock( &structure_lock );
631 }
632
633 /**
634  ****************************************************************************
635  * attach object to a parent object
636  *****************************************************************************
637  * This function sets p_this as a child of p_parent, and p_parent as a parent
638  * of p_this. This link can be undone using vlc_object_detach.
639  *****************************************************************************/
640 void __vlc_object_attach( vlc_object_t *p_this, vlc_object_t *p_parent )
641 {
642     if( !p_this ) return;
643
644     vlc_mutex_lock( &structure_lock );
645
646     /* Attach the parent to its child */
647     p_this->p_parent = p_parent;
648
649     /* Attach the child to its parent */
650     INSERT_ELEM( p_parent->pp_children, p_parent->i_children,
651                  p_parent->i_children, p_this );
652
653     /* Climb up the tree to see whether we are connected with the root */
654     if( p_parent->b_attached )
655     {
656         SetAttachment( p_this, VLC_TRUE );
657     }
658
659     vlc_mutex_unlock( &structure_lock );
660 }
661
662 /**
663  ****************************************************************************
664  * detach object from its parent
665  *****************************************************************************
666  * This function removes all links between an object and its parent.
667  *****************************************************************************/
668 void __vlc_object_detach( vlc_object_t *p_this )
669 {
670     if( !p_this ) return;
671
672     vlc_mutex_lock( &structure_lock );
673     if( !p_this->p_parent )
674     {
675         msg_Err( p_this, "object is not attached" );
676         vlc_mutex_unlock( &structure_lock );
677         return;
678     }
679
680     /* Climb up the tree to see whether we are connected with the root */
681     if( p_this->p_parent->b_attached )
682     {
683         SetAttachment( p_this, VLC_FALSE );
684     }
685
686     DetachObject( p_this );
687     vlc_mutex_unlock( &structure_lock );
688     p_this = NULL;
689 }
690
691 /**
692  ****************************************************************************
693  * find a list typed objects and increment their refcount
694  *****************************************************************************
695  * This function recursively looks for a given object type. i_mode can be one
696  * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
697  *****************************************************************************/
698 vlc_list_t * __vlc_list_find( vlc_object_t *p_this, int i_type, int i_mode )
699 {
700     vlc_list_t *p_list;
701     vlc_object_t **pp_current, **pp_end;
702     int i_count = 0, i_index = 0;
703
704     vlc_mutex_lock( &structure_lock );
705
706     /* Look for the objects */
707     switch( i_mode & 0x000f )
708     {
709     case FIND_ANYWHERE:
710         pp_current = p_this->p_libvlc_global->pp_objects;
711         pp_end = pp_current + p_this->p_libvlc_global->i_objects;
712
713         for( ; pp_current < pp_end ; pp_current++ )
714         {
715             if( (*pp_current)->b_attached
716                  && (*pp_current)->i_object_type == i_type )
717             {
718                 i_count++;
719             }
720         }
721
722         p_list = NewList( i_count );
723         pp_current = p_this->p_libvlc_global->pp_objects;
724
725         for( ; pp_current < pp_end ; pp_current++ )
726         {
727             if( (*pp_current)->b_attached
728                  && (*pp_current)->i_object_type == i_type )
729             {
730                 ListReplace( p_list, *pp_current, i_index );
731                 if( i_index < i_count ) i_index++;
732             }
733         }
734     break;
735
736     case FIND_CHILD:
737         i_count = CountChildren( p_this, i_type );
738         p_list = NewList( i_count );
739
740         /* Check allocation was successful */
741         if( p_list->i_count != i_count )
742         {
743             msg_Err( p_this, "list allocation failed!" );
744             p_list->i_count = 0;
745             break;
746         }
747
748         p_list->i_count = 0;
749         ListChildren( p_list, p_this, i_type );
750         break;
751
752     default:
753         msg_Err( p_this, "unimplemented!" );
754         p_list = NewList( 0 );
755         break;
756     }
757
758     vlc_mutex_unlock( &structure_lock );
759
760     return p_list;
761 }
762
763 /*****************************************************************************
764  * DumpCommand: print the current vlc structure
765  *****************************************************************************
766  * This function prints either an ASCII tree showing the connections between
767  * vlc objects, and additional information such as their refcount, thread ID,
768  * etc. (command "tree"), or the same data as a simple list (command "list").
769  *****************************************************************************/
770 static int DumpCommand( vlc_object_t *p_this, char const *psz_cmd,
771                         vlc_value_t oldval, vlc_value_t newval, void *p_data )
772 {
773     (void)oldval; (void)p_data;
774     if( *psz_cmd == 'l' )
775     {
776         vlc_mutex_lock( &structure_lock );
777
778         vlc_object_t **pp_current, **pp_end;
779
780         pp_current = p_this->p_libvlc_global->pp_objects;
781         pp_end = pp_current + p_this->p_libvlc_global->i_objects;
782
783         for( ; pp_current < pp_end ; pp_current++ )
784         {
785             if( (*pp_current)->b_attached )
786             {
787                 PrintObject( *pp_current, "" );
788             }
789             else
790             {
791                 printf( " o %.8i %s (not attached)\n",
792                         (*pp_current)->i_object_id,
793                         (*pp_current)->psz_object_type );
794             }
795         }
796
797         vlc_mutex_unlock( &structure_lock );
798     }
799     else
800     {
801         vlc_object_t *p_object = NULL;
802
803         if( *newval.psz_string )
804         {
805             char *end;
806             int i_id = strtol( newval.psz_string, &end, 0 );
807             if( end != newval.psz_string )
808                 p_object = vlc_object_get( p_this, i_id );
809             else
810             {
811                 /* try using the object's name to find it */
812                 vlc_object_t *p_libvlc = vlc_object_get( p_this, 1 );
813                 if( p_libvlc )
814                 {
815                     /* Look in p_libvlc's children tree */
816                     p_object = vlc_object_find_name( p_libvlc,
817                                                      newval.psz_string,
818                                                      FIND_CHILD );
819                     vlc_object_release( p_libvlc );
820                 }
821                 if( !p_object )
822                 {
823                     /* If it's not in libvlc, look in libvlc_global (== p_this) */
824                     p_object = vlc_object_find_name( p_this,
825                                                      newval.psz_string,
826                                                      FIND_CHILD );
827                 }
828             }
829
830             if( !p_object )
831             {
832                 return VLC_ENOOBJ;
833             }
834         }
835
836         vlc_mutex_lock( &structure_lock );
837
838         if( *psz_cmd == 't' )
839         {
840             char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
841
842             if( !p_object )
843                 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
844
845             psz_foo[0] = '|';
846             DumpStructure( p_object, 0, psz_foo );
847         }
848         else if( *psz_cmd == 'v' )
849         {
850             int i;
851
852             if( !p_object )
853                 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
854
855             PrintObject( p_object, "" );
856
857             if( !p_object->i_vars )
858                 printf( " `-o No variables\n" );
859             for( i = 0; i < p_object->i_vars; i++ )
860             {
861                 variable_t *p_var = p_object->p_vars + i;
862
863                 const char *psz_type = "unknown";
864                 switch( p_var->i_type & VLC_VAR_TYPE )
865                 {
866 #define MYCASE( type, nice )                \
867                     case VLC_VAR_ ## type:  \
868                         psz_type = nice;    \
869                         break;
870                     MYCASE( VOID, "void" );
871                     MYCASE( BOOL, "bool" );
872                     MYCASE( INTEGER, "integer" );
873                     MYCASE( HOTKEY, "hotkey" );
874                     MYCASE( STRING, "string" );
875                     MYCASE( MODULE, "module" );
876                     MYCASE( FILE, "file" );
877                     MYCASE( DIRECTORY, "directory" );
878                     MYCASE( VARIABLE, "variable" );
879                     MYCASE( FLOAT, "float" );
880                     MYCASE( TIME, "time" );
881                     MYCASE( ADDRESS, "address" );
882                     MYCASE( MUTEX, "mutex" );
883                     MYCASE( LIST, "list" );
884 #undef MYCASE
885                 }
886                 printf( " %c-o \"%s\" (%s",
887                         i + 1 == p_object->i_vars ? '`' : '|',
888                         p_var->psz_name, psz_type );
889                 if( p_var->psz_text )
890                     printf( ", %s", p_var->psz_text );
891                 printf( ")" );
892                 if( p_var->i_type & VLC_VAR_ISCOMMAND )
893                     printf( ", command" );
894                 if( p_var->i_entries )
895                     printf( ", %d callbacks", p_var->i_entries );
896                 switch( p_var->i_type & 0x00f0 )
897                 {
898                     case VLC_VAR_VOID:
899                     case VLC_VAR_MUTEX:
900                         break;
901                     case VLC_VAR_BOOL:
902                         printf( ": %s", p_var->val.b_bool ? "true" : "false" );
903                         break;
904                     case VLC_VAR_INTEGER:
905                         printf( ": %d", p_var->val.i_int );
906                         break;
907                     case VLC_VAR_STRING:
908                         printf( ": \"%s\"", p_var->val.psz_string );
909                         break;
910                     case VLC_VAR_FLOAT:
911                         printf( ": %f", p_var->val.f_float );
912                         break;
913                     case VLC_VAR_TIME:
914                         printf( ": " I64Fi, p_var->val.i_time );
915                         break;
916                     case VLC_VAR_ADDRESS:
917                         printf( ": %p", p_var->val.p_address );
918                         break;
919                     case VLC_VAR_LIST:
920                         printf( ": TODO" );
921                         break;
922                 }
923                 printf( "\n" );
924             }
925         }
926
927         vlc_mutex_unlock( &structure_lock );
928
929         if( *newval.psz_string )
930         {
931             vlc_object_release( p_object );
932         }
933     }
934
935     return VLC_SUCCESS;
936 }
937
938 /*****************************************************************************
939  * vlc_list_release: free a list previously allocated by vlc_list_find
940  *****************************************************************************
941  * This function decreases the refcount of all objects in the list and
942  * frees the list.
943  *****************************************************************************/
944 void vlc_list_release( vlc_list_t *p_list )
945 {
946     int i_index;
947
948     vlc_mutex_lock( &structure_lock );
949     for( i_index = 0; i_index < p_list->i_count; i_index++ )
950     {
951         p_list->p_values[i_index].p_object->i_refcount--;
952     }
953     vlc_mutex_unlock( &structure_lock );
954
955     free( p_list->p_values );
956     free( p_list );
957 }
958
959 /* Following functions are local */
960
961 /*****************************************************************************
962  * FindIndex: find the index of an object in an array of objects
963  *****************************************************************************
964  * This function assumes that p_this can be found in pp_objects. It will not
965  * crash if p_this cannot be found, but will return a wrong value. It is your
966  * duty to check the return value if you are not certain that the object could
967  * be found for sure.
968  *****************************************************************************/
969 static int FindIndex( vlc_object_t *p_this,
970                       vlc_object_t **pp_objects, int i_count )
971 {
972     int i_middle = i_count / 2;
973
974     if( i_count == 0 )
975     {
976         return 0;
977     }
978
979     if( pp_objects[i_middle] == p_this )
980     {
981         return i_middle;
982     }
983
984     if( i_count == 1 )
985     {
986         return 0;
987     }
988
989     /* We take advantage of the sorted array */
990     if( pp_objects[i_middle]->i_object_id < p_this->i_object_id )
991     {
992         return i_middle + FindIndex( p_this, pp_objects + i_middle,
993                                              i_count - i_middle );
994     }
995     else
996     {
997         return FindIndex( p_this, pp_objects, i_middle );
998     }
999 }
1000
1001 static vlc_object_t * FindObject( vlc_object_t *p_this, int i_type, int i_mode )
1002 {
1003     int i;
1004     vlc_object_t *p_tmp;
1005
1006     switch( i_mode & 0x000f )
1007     {
1008     case FIND_PARENT:
1009         p_tmp = p_this->p_parent;
1010         if( p_tmp )
1011         {
1012             if( p_tmp->i_object_type == i_type )
1013             {
1014                 p_tmp->i_refcount++;
1015                 return p_tmp;
1016             }
1017             else
1018             {
1019                 return FindObject( p_tmp, i_type, i_mode );
1020             }
1021         }
1022         break;
1023
1024     case FIND_CHILD:
1025         for( i = p_this->i_children; i--; )
1026         {
1027             p_tmp = p_this->pp_children[i];
1028             if( p_tmp->i_object_type == i_type )
1029             {
1030                 p_tmp->i_refcount++;
1031                 return p_tmp;
1032             }
1033             else if( p_tmp->i_children )
1034             {
1035                 p_tmp = FindObject( p_tmp, i_type, i_mode );
1036                 if( p_tmp )
1037                 {
1038                     return p_tmp;
1039                 }
1040             }
1041         }
1042         break;
1043
1044     case FIND_ANYWHERE:
1045         /* Handled in vlc_object_find */
1046         break;
1047     }
1048
1049     return NULL;
1050 }
1051
1052 static vlc_object_t * FindObjectName( vlc_object_t *p_this,
1053                                       const char *psz_name,
1054                                       int i_mode )
1055 {
1056     int i;
1057     vlc_object_t *p_tmp;
1058
1059     switch( i_mode & 0x000f )
1060     {
1061     case FIND_PARENT:
1062         p_tmp = p_this->p_parent;
1063         if( p_tmp )
1064         {
1065             if( p_tmp->psz_object_name
1066                 && !strcmp( p_tmp->psz_object_name, psz_name ) )
1067             {
1068                 p_tmp->i_refcount++;
1069                 return p_tmp;
1070             }
1071             else
1072             {
1073                 return FindObjectName( p_tmp, psz_name, i_mode );
1074             }
1075         }
1076         break;
1077
1078     case FIND_CHILD:
1079         for( i = p_this->i_children; i--; )
1080         {
1081             p_tmp = p_this->pp_children[i];
1082             if( p_tmp->psz_object_name
1083                 && !strcmp( p_tmp->psz_object_name, psz_name ) )
1084             {
1085                 p_tmp->i_refcount++;
1086                 return p_tmp;
1087             }
1088             else if( p_tmp->i_children )
1089             {
1090                 p_tmp = FindObjectName( p_tmp, psz_name, i_mode );
1091                 if( p_tmp )
1092                 {
1093                     return p_tmp;
1094                 }
1095             }
1096         }
1097         break;
1098
1099     case FIND_ANYWHERE:
1100         /* Handled in vlc_object_find */
1101         break;
1102     }
1103
1104     return NULL;
1105 }
1106
1107 static void DetachObject( vlc_object_t *p_this )
1108 {
1109     vlc_object_t *p_parent = p_this->p_parent;
1110     int i_index, i;
1111
1112     /* Remove p_this's parent */
1113     p_this->p_parent = NULL;
1114
1115     /* Remove all of p_parent's children which are p_this */
1116     for( i_index = p_parent->i_children ; i_index-- ; )
1117     {
1118         if( p_parent->pp_children[i_index] == p_this )
1119         {
1120             p_parent->i_children--;
1121             for( i = i_index ; i < p_parent->i_children ; i++ )
1122             {
1123                 p_parent->pp_children[i] = p_parent->pp_children[i+1];
1124             }
1125         }
1126     }
1127
1128     if( p_parent->i_children )
1129     {
1130         p_parent->pp_children = (vlc_object_t **)realloc( p_parent->pp_children,
1131                                p_parent->i_children * sizeof(vlc_object_t *) );
1132     }
1133     else
1134     {
1135         free( p_parent->pp_children );
1136         p_parent->pp_children = NULL;
1137     }
1138 }
1139
1140 /*****************************************************************************
1141  * SetAttachment: recursively set the b_attached flag of a subtree.
1142  *****************************************************************************
1143  * This function is used by the attach and detach functions to propagate
1144  * the b_attached flag in a subtree.
1145  *****************************************************************************/
1146 static void SetAttachment( vlc_object_t *p_this, vlc_bool_t b_attached )
1147 {
1148     int i_index;
1149
1150     for( i_index = p_this->i_children ; i_index-- ; )
1151     {
1152         SetAttachment( p_this->pp_children[i_index], b_attached );
1153     }
1154
1155     p_this->b_attached = b_attached;
1156 }
1157
1158 static void PrintObject( vlc_object_t *p_this, const char *psz_prefix )
1159 {
1160     char psz_children[20], psz_refcount[20], psz_thread[30], psz_name[50],
1161          psz_parent[20];
1162
1163     psz_name[0] = '\0';
1164     if( p_this->psz_object_name )
1165     {
1166         snprintf( psz_name, 49, " \"%s\"", p_this->psz_object_name );
1167         if( psz_name[48] )
1168             psz_name[48] = '\"';
1169     }
1170
1171     psz_children[0] = '\0';
1172     switch( p_this->i_children )
1173     {
1174         case 0:
1175             break;
1176         case 1:
1177             strcpy( psz_children, ", 1 child" );
1178             break;
1179         default:
1180             snprintf( psz_children, 19, ", %i children", p_this->i_children );
1181             break;
1182     }
1183
1184     psz_refcount[0] = '\0';
1185     if( p_this->i_refcount )
1186         snprintf( psz_refcount, 19, ", refcount %i", p_this->i_refcount );
1187
1188     psz_thread[0] = '\0';
1189     if( p_this->b_thread )
1190         snprintf( psz_thread, 29, " (thread %d)", (int)p_this->thread_id );
1191
1192     psz_parent[0] = '\0';
1193     if( p_this->p_parent )
1194         snprintf( psz_parent, 19, ", parent %i", p_this->p_parent->i_object_id );
1195
1196     printf( " %so %.8i %s%s%s%s%s%s\n", psz_prefix,
1197             p_this->i_object_id, p_this->psz_object_type,
1198             psz_name, psz_thread, psz_refcount, psz_children,
1199             psz_parent );
1200 }
1201
1202 static void DumpStructure( vlc_object_t *p_this, int i_level, char *psz_foo )
1203 {
1204     int i;
1205     char i_back = psz_foo[i_level];
1206     psz_foo[i_level] = '\0';
1207
1208     PrintObject( p_this, psz_foo );
1209
1210     psz_foo[i_level] = i_back;
1211
1212     if( i_level / 2 >= MAX_DUMPSTRUCTURE_DEPTH )
1213     {
1214         msg_Warn( p_this, "structure tree is too deep" );
1215         return;
1216     }
1217
1218     for( i = 0 ; i < p_this->i_children ; i++ )
1219     {
1220         if( i_level )
1221         {
1222             psz_foo[i_level-1] = ' ';
1223
1224             if( psz_foo[i_level-2] == '`' )
1225             {
1226                 psz_foo[i_level-2] = ' ';
1227             }
1228         }
1229
1230         if( i == p_this->i_children - 1 )
1231         {
1232             psz_foo[i_level] = '`';
1233         }
1234         else
1235         {
1236             psz_foo[i_level] = '|';
1237         }
1238
1239         psz_foo[i_level+1] = '-';
1240         psz_foo[i_level+2] = '\0';
1241
1242         DumpStructure( p_this->pp_children[i], i_level + 2, psz_foo );
1243     }
1244 }
1245
1246 static vlc_list_t * NewList( int i_count )
1247 {
1248     vlc_list_t * p_list = (vlc_list_t *)malloc( sizeof( vlc_list_t ) );
1249     if( p_list == NULL )
1250     {
1251         return NULL;
1252     }
1253
1254     p_list->i_count = i_count;
1255
1256     if( i_count == 0 )
1257     {
1258         p_list->p_values = NULL;
1259         return p_list;
1260     }
1261
1262     p_list->p_values = malloc( i_count * sizeof( vlc_value_t ) );
1263     if( p_list->p_values == NULL )
1264     {
1265         p_list->i_count = 0;
1266         return p_list;
1267     }
1268
1269     return p_list;
1270 }
1271
1272 static void ListReplace( vlc_list_t *p_list, vlc_object_t *p_object,
1273                          int i_index )
1274 {
1275     if( p_list == NULL || i_index >= p_list->i_count )
1276     {
1277         return;
1278     }
1279
1280     p_object->i_refcount++;
1281
1282     p_list->p_values[i_index].p_object = p_object;
1283
1284     return;
1285 }
1286
1287 /*static void ListAppend( vlc_list_t *p_list, vlc_object_t *p_object )
1288 {
1289     if( p_list == NULL )
1290     {
1291         return;
1292     }
1293
1294     p_list->p_values = realloc( p_list->p_values, (p_list->i_count + 1)
1295                                 * sizeof( vlc_value_t ) );
1296     if( p_list->p_values == NULL )
1297     {
1298         p_list->i_count = 0;
1299         return;
1300     }
1301
1302     p_object->i_refcount++;
1303
1304     p_list->p_values[p_list->i_count].p_object = p_object;
1305     p_list->i_count++;
1306
1307     return;
1308 }*/
1309
1310 static int CountChildren( vlc_object_t *p_this, int i_type )
1311 {
1312     vlc_object_t *p_tmp;
1313     int i, i_count = 0;
1314
1315     for( i = 0; i < p_this->i_children; i++ )
1316     {
1317         p_tmp = p_this->pp_children[i];
1318
1319         if( p_tmp->i_object_type == i_type )
1320         {
1321             i_count++;
1322         }
1323
1324         if( p_tmp->i_children )
1325         {
1326             i_count += CountChildren( p_tmp, i_type );
1327         }
1328     }
1329
1330     return i_count;
1331 }
1332
1333 static void ListChildren( vlc_list_t *p_list, vlc_object_t *p_this, int i_type )
1334 {
1335     vlc_object_t *p_tmp;
1336     int i;
1337
1338     for( i = 0; i < p_this->i_children; i++ )
1339     {
1340         p_tmp = p_this->pp_children[i];
1341
1342         if( p_tmp->i_object_type == i_type )
1343         {
1344             ListReplace( p_list, p_tmp, p_list->i_count++ );
1345         }
1346
1347         if( p_tmp->i_children )
1348         {
1349             ListChildren( p_list, p_tmp, i_type );
1350         }
1351     }
1352 }