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