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