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