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