]> git.sesse.net Git - vlc/blob - src/misc/objects.c
cde9aa2f8ddbe3a30f97c9420f2c6e9fba1639f8
[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 vlc_bool_t __vlc_object_dying_unlocked( vlc_object_t *p_this )
442 {
443     vlc_assert_locked( &p_this->object_lock );
444     return p_this->b_die;
445 }
446
447
448 vlc_bool_t __vlc_object_dying( vlc_object_t *p_this )
449 {
450      vlc_bool_t b;
451      vlc_mutex_lock( &p_this->object_lock );
452      b = __vlc_object_dying_unlocked( p_this );
453      vlc_mutex_unlock( &p_this->object_lock );
454      return b;
455 }
456
457
458 /**
459  * find an object given its ID
460  *
461  * This function looks for the object whose i_object_id field is i_id. We
462  * use a dichotomy so that lookups are in log2(n).
463  *****************************************************************************/
464 void * __vlc_object_get( vlc_object_t *p_this, int i_id )
465 {
466     int i_max, i_middle;
467     vlc_object_t **pp_objects;
468
469     vlc_mutex_lock( &structure_lock );
470
471     pp_objects = p_this->p_libvlc_global->pp_objects;
472
473     /* Perform our dichotomy */
474     for( i_max = p_this->p_libvlc_global->i_objects - 1 ; ; )
475     {
476         i_middle = i_max / 2;
477
478         if( pp_objects[i_middle]->i_object_id > i_id )
479         {
480             i_max = i_middle;
481         }
482         else if( pp_objects[i_middle]->i_object_id < i_id )
483         {
484             if( i_middle )
485             {
486                 pp_objects += i_middle;
487                 i_max -= i_middle;
488             }
489             else
490             {
491                 /* This happens when there are only two remaining objects */
492                 if( pp_objects[i_middle+1]->i_object_id == i_id )
493                 {
494                     vlc_mutex_unlock( &structure_lock );
495                     pp_objects[i_middle+1]->i_refcount++;
496                     return pp_objects[i_middle+1];
497                 }
498                 break;
499             }
500         }
501         else
502         {
503             vlc_mutex_unlock( &structure_lock );
504             pp_objects[i_middle]->i_refcount++;
505             return pp_objects[i_middle];
506         }
507
508         if( i_max == 0 )
509         {
510             /* this means that i_max == i_middle, and since we have already
511              * tested pp_objects[i_middle]), p_found is properly set. */
512             break;
513         }
514     }
515
516     vlc_mutex_unlock( &structure_lock );
517     return NULL;
518 }
519
520 /**
521  ****************************************************************************
522  * find a typed object and increment its refcount
523  *****************************************************************************
524  * This function recursively looks for a given object type. i_mode can be one
525  * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
526  *****************************************************************************/
527 void * __vlc_object_find( vlc_object_t *p_this, int i_type, int i_mode )
528 {
529     vlc_object_t *p_found;
530
531     vlc_mutex_lock( &structure_lock );
532
533     /* If we are of the requested type ourselves, don't look further */
534     if( !(i_mode & FIND_STRICT) && p_this->i_object_type == i_type )
535     {
536         p_this->i_refcount++;
537         vlc_mutex_unlock( &structure_lock );
538         return p_this;
539     }
540
541     /* Otherwise, recursively look for the object */
542     if( (i_mode & 0x000f) == FIND_ANYWHERE )
543     {
544         vlc_object_t *p_root = p_this;
545
546         /* Find the root */
547         while( p_root->p_parent != NULL &&
548                p_root != VLC_OBJECT( p_this->p_libvlc ) )
549         {
550             p_root = p_root->p_parent;
551         }
552
553         p_found = FindObject( p_root, i_type, (i_mode & ~0x000f)|FIND_CHILD );
554         if( p_found == NULL && p_root != VLC_OBJECT( p_this->p_libvlc ) )
555         {
556             p_found = FindObject( VLC_OBJECT( p_this->p_libvlc ),
557                                   i_type, (i_mode & ~0x000f)|FIND_CHILD );
558         }
559     }
560     else
561     {
562         p_found = FindObject( p_this, i_type, i_mode );
563     }
564
565     vlc_mutex_unlock( &structure_lock );
566
567     return p_found;
568 }
569
570 /**
571  ****************************************************************************
572  * find a named object and increment its refcount
573  *****************************************************************************
574  * This function recursively looks for a given object name. i_mode can be one
575  * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
576  *****************************************************************************/
577 void * __vlc_object_find_name( vlc_object_t *p_this, const char *psz_name,
578                                int i_mode )
579 {
580     vlc_object_t *p_found;
581
582     vlc_mutex_lock( &structure_lock );
583
584     /* If have the requested name ourselves, don't look further */
585     if( !(i_mode & FIND_STRICT)
586         && p_this->psz_object_name
587         && !strcmp( p_this->psz_object_name, psz_name ) )
588     {
589         p_this->i_refcount++;
590         vlc_mutex_unlock( &structure_lock );
591         return p_this;
592     }
593
594     /* Otherwise, recursively look for the object */
595     if( (i_mode & 0x000f) == FIND_ANYWHERE )
596     {
597         vlc_object_t *p_root = p_this;
598
599         /* Find the root */
600         while( p_root->p_parent != NULL &&
601                p_root != VLC_OBJECT( p_this->p_libvlc ) )
602         {
603             p_root = p_root->p_parent;
604         }
605
606         p_found = FindObjectName( p_root, psz_name,
607                                  (i_mode & ~0x000f)|FIND_CHILD );
608         if( p_found == NULL && p_root != VLC_OBJECT( p_this->p_libvlc ) )
609         {
610             p_found = FindObjectName( VLC_OBJECT( p_this->p_libvlc ),
611                                       psz_name, (i_mode & ~0x000f)|FIND_CHILD );
612         }
613     }
614     else
615     {
616         p_found = FindObjectName( p_this, psz_name, i_mode );
617     }
618
619     vlc_mutex_unlock( &structure_lock );
620
621     return p_found;
622 }
623
624 /**
625  ****************************************************************************
626  * increment an object refcount
627  *****************************************************************************/
628 void __vlc_object_yield( vlc_object_t *p_this )
629 {
630     vlc_mutex_lock( &structure_lock );
631     p_this->i_refcount++;
632     vlc_mutex_unlock( &structure_lock );
633 }
634
635 /**
636  ****************************************************************************
637  * decrement an object refcount
638  *****************************************************************************/
639 void __vlc_object_release( vlc_object_t *p_this )
640 {
641     vlc_mutex_lock( &structure_lock );
642     p_this->i_refcount--;
643     vlc_mutex_unlock( &structure_lock );
644 }
645
646 /**
647  ****************************************************************************
648  * attach object to a parent object
649  *****************************************************************************
650  * This function sets p_this as a child of p_parent, and p_parent as a parent
651  * of p_this. This link can be undone using vlc_object_detach.
652  *****************************************************************************/
653 void __vlc_object_attach( vlc_object_t *p_this, vlc_object_t *p_parent )
654 {
655     if( !p_this ) return;
656
657     vlc_mutex_lock( &structure_lock );
658
659     /* Attach the parent to its child */
660     p_this->p_parent = p_parent;
661
662     /* Attach the child to its parent */
663     INSERT_ELEM( p_parent->pp_children, p_parent->i_children,
664                  p_parent->i_children, p_this );
665
666     /* Climb up the tree to see whether we are connected with the root */
667     if( p_parent->b_attached )
668     {
669         SetAttachment( p_this, VLC_TRUE );
670     }
671
672     vlc_mutex_unlock( &structure_lock );
673 }
674
675 /**
676  ****************************************************************************
677  * detach object from its parent
678  *****************************************************************************
679  * This function removes all links between an object and its parent.
680  *****************************************************************************/
681 void __vlc_object_detach( vlc_object_t *p_this )
682 {
683     if( !p_this ) return;
684
685     vlc_mutex_lock( &structure_lock );
686     if( !p_this->p_parent )
687     {
688         msg_Err( p_this, "object is not attached" );
689         vlc_mutex_unlock( &structure_lock );
690         return;
691     }
692
693     /* Climb up the tree to see whether we are connected with the root */
694     if( p_this->p_parent->b_attached )
695     {
696         SetAttachment( p_this, VLC_FALSE );
697     }
698
699     DetachObject( p_this );
700     vlc_mutex_unlock( &structure_lock );
701     p_this = NULL;
702 }
703
704 /**
705  ****************************************************************************
706  * find a list typed objects and increment their refcount
707  *****************************************************************************
708  * This function recursively looks for a given object type. i_mode can be one
709  * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
710  *****************************************************************************/
711 vlc_list_t * __vlc_list_find( vlc_object_t *p_this, int i_type, int i_mode )
712 {
713     vlc_list_t *p_list;
714     vlc_object_t **pp_current, **pp_end;
715     int i_count = 0, i_index = 0;
716
717     vlc_mutex_lock( &structure_lock );
718
719     /* Look for the objects */
720     switch( i_mode & 0x000f )
721     {
722     case FIND_ANYWHERE:
723         pp_current = p_this->p_libvlc_global->pp_objects;
724         pp_end = pp_current + p_this->p_libvlc_global->i_objects;
725
726         for( ; pp_current < pp_end ; pp_current++ )
727         {
728             if( (*pp_current)->b_attached
729                  && (*pp_current)->i_object_type == i_type )
730             {
731                 i_count++;
732             }
733         }
734
735         p_list = NewList( i_count );
736         pp_current = p_this->p_libvlc_global->pp_objects;
737
738         for( ; pp_current < pp_end ; pp_current++ )
739         {
740             if( (*pp_current)->b_attached
741                  && (*pp_current)->i_object_type == i_type )
742             {
743                 ListReplace( p_list, *pp_current, i_index );
744                 if( i_index < i_count ) i_index++;
745             }
746         }
747     break;
748
749     case FIND_CHILD:
750         i_count = CountChildren( p_this, i_type );
751         p_list = NewList( i_count );
752
753         /* Check allocation was successful */
754         if( p_list->i_count != i_count )
755         {
756             msg_Err( p_this, "list allocation failed!" );
757             p_list->i_count = 0;
758             break;
759         }
760
761         p_list->i_count = 0;
762         ListChildren( p_list, p_this, i_type );
763         break;
764
765     default:
766         msg_Err( p_this, "unimplemented!" );
767         p_list = NewList( 0 );
768         break;
769     }
770
771     vlc_mutex_unlock( &structure_lock );
772
773     return p_list;
774 }
775
776 /*****************************************************************************
777  * DumpCommand: print the current vlc structure
778  *****************************************************************************
779  * This function prints either an ASCII tree showing the connections between
780  * vlc objects, and additional information such as their refcount, thread ID,
781  * etc. (command "tree"), or the same data as a simple list (command "list").
782  *****************************************************************************/
783 static int DumpCommand( vlc_object_t *p_this, char const *psz_cmd,
784                         vlc_value_t oldval, vlc_value_t newval, void *p_data )
785 {
786     (void)oldval; (void)p_data;
787     if( *psz_cmd == 'l' )
788     {
789         vlc_mutex_lock( &structure_lock );
790
791         vlc_object_t **pp_current, **pp_end;
792
793         pp_current = p_this->p_libvlc_global->pp_objects;
794         pp_end = pp_current + p_this->p_libvlc_global->i_objects;
795
796         for( ; pp_current < pp_end ; pp_current++ )
797         {
798             if( (*pp_current)->b_attached )
799             {
800                 PrintObject( *pp_current, "" );
801             }
802             else
803             {
804                 printf( " o %.8i %s (not attached)\n",
805                         (*pp_current)->i_object_id,
806                         (*pp_current)->psz_object_type );
807             }
808         }
809
810         vlc_mutex_unlock( &structure_lock );
811     }
812     else
813     {
814         vlc_object_t *p_object = NULL;
815
816         if( *newval.psz_string )
817         {
818             char *end;
819             int i_id = strtol( newval.psz_string, &end, 0 );
820             if( end != newval.psz_string )
821                 p_object = vlc_object_get( p_this, i_id );
822             else
823             {
824                 /* try using the object's name to find it */
825                 vlc_object_t *p_libvlc = vlc_object_get( p_this, 1 );
826                 if( p_libvlc )
827                 {
828                     /* Look in p_libvlc's children tree */
829                     p_object = vlc_object_find_name( p_libvlc,
830                                                      newval.psz_string,
831                                                      FIND_CHILD );
832                     vlc_object_release( p_libvlc );
833                 }
834                 if( !p_object )
835                 {
836                     /* If it's not in libvlc, look in libvlc_global (== p_this) */
837                     p_object = vlc_object_find_name( p_this,
838                                                      newval.psz_string,
839                                                      FIND_CHILD );
840                 }
841             }
842
843             if( !p_object )
844             {
845                 return VLC_ENOOBJ;
846             }
847         }
848
849         vlc_mutex_lock( &structure_lock );
850
851         if( *psz_cmd == 't' )
852         {
853             char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
854
855             if( !p_object )
856                 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
857
858             psz_foo[0] = '|';
859             DumpStructure( p_object, 0, psz_foo );
860         }
861         else if( *psz_cmd == 'v' )
862         {
863             int i;
864
865             if( !p_object )
866                 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
867
868             PrintObject( p_object, "" );
869
870             if( !p_object->i_vars )
871                 printf( " `-o No variables\n" );
872             for( i = 0; i < p_object->i_vars; i++ )
873             {
874                 variable_t *p_var = p_object->p_vars + i;
875
876                 const char *psz_type = "unknown";
877                 switch( p_var->i_type & VLC_VAR_TYPE )
878                 {
879 #define MYCASE( type, nice )                \
880                     case VLC_VAR_ ## type:  \
881                         psz_type = nice;    \
882                         break;
883                     MYCASE( VOID, "void" );
884                     MYCASE( BOOL, "bool" );
885                     MYCASE( INTEGER, "integer" );
886                     MYCASE( HOTKEY, "hotkey" );
887                     MYCASE( STRING, "string" );
888                     MYCASE( MODULE, "module" );
889                     MYCASE( FILE, "file" );
890                     MYCASE( DIRECTORY, "directory" );
891                     MYCASE( VARIABLE, "variable" );
892                     MYCASE( FLOAT, "float" );
893                     MYCASE( TIME, "time" );
894                     MYCASE( ADDRESS, "address" );
895                     MYCASE( MUTEX, "mutex" );
896                     MYCASE( LIST, "list" );
897 #undef MYCASE
898                 }
899                 printf( " %c-o \"%s\" (%s",
900                         i + 1 == p_object->i_vars ? '`' : '|',
901                         p_var->psz_name, psz_type );
902                 if( p_var->psz_text )
903                     printf( ", %s", p_var->psz_text );
904                 printf( ")" );
905                 if( p_var->i_type & VLC_VAR_ISCOMMAND )
906                     printf( ", command" );
907                 if( p_var->i_entries )
908                     printf( ", %d callbacks", p_var->i_entries );
909                 switch( p_var->i_type & 0x00f0 )
910                 {
911                     case VLC_VAR_VOID:
912                     case VLC_VAR_MUTEX:
913                         break;
914                     case VLC_VAR_BOOL:
915                         printf( ": %s", p_var->val.b_bool ? "true" : "false" );
916                         break;
917                     case VLC_VAR_INTEGER:
918                         printf( ": %d", p_var->val.i_int );
919                         break;
920                     case VLC_VAR_STRING:
921                         printf( ": \"%s\"", p_var->val.psz_string );
922                         break;
923                     case VLC_VAR_FLOAT:
924                         printf( ": %f", p_var->val.f_float );
925                         break;
926                     case VLC_VAR_TIME:
927                         printf( ": " I64Fi, p_var->val.i_time );
928                         break;
929                     case VLC_VAR_ADDRESS:
930                         printf( ": %p", p_var->val.p_address );
931                         break;
932                     case VLC_VAR_LIST:
933                         printf( ": TODO" );
934                         break;
935                 }
936                 printf( "\n" );
937             }
938         }
939
940         vlc_mutex_unlock( &structure_lock );
941
942         if( *newval.psz_string )
943         {
944             vlc_object_release( p_object );
945         }
946     }
947
948     return VLC_SUCCESS;
949 }
950
951 /*****************************************************************************
952  * vlc_list_release: free a list previously allocated by vlc_list_find
953  *****************************************************************************
954  * This function decreases the refcount of all objects in the list and
955  * frees the list.
956  *****************************************************************************/
957 void vlc_list_release( vlc_list_t *p_list )
958 {
959     int i_index;
960
961     vlc_mutex_lock( &structure_lock );
962     for( i_index = 0; i_index < p_list->i_count; i_index++ )
963     {
964         p_list->p_values[i_index].p_object->i_refcount--;
965     }
966     vlc_mutex_unlock( &structure_lock );
967
968     free( p_list->p_values );
969     free( p_list );
970 }
971
972 /* Following functions are local */
973
974 /*****************************************************************************
975  * FindIndex: find the index of an object in an array of objects
976  *****************************************************************************
977  * This function assumes that p_this can be found in pp_objects. It will not
978  * crash if p_this cannot be found, but will return a wrong value. It is your
979  * duty to check the return value if you are not certain that the object could
980  * be found for sure.
981  *****************************************************************************/
982 static int FindIndex( vlc_object_t *p_this,
983                       vlc_object_t **pp_objects, int i_count )
984 {
985     int i_middle = i_count / 2;
986
987     if( i_count == 0 )
988     {
989         return 0;
990     }
991
992     if( pp_objects[i_middle] == p_this )
993     {
994         return i_middle;
995     }
996
997     if( i_count == 1 )
998     {
999         return 0;
1000     }
1001
1002     /* We take advantage of the sorted array */
1003     if( pp_objects[i_middle]->i_object_id < p_this->i_object_id )
1004     {
1005         return i_middle + FindIndex( p_this, pp_objects + i_middle,
1006                                              i_count - i_middle );
1007     }
1008     else
1009     {
1010         return FindIndex( p_this, pp_objects, i_middle );
1011     }
1012 }
1013
1014 static vlc_object_t * FindObject( vlc_object_t *p_this, int i_type, int i_mode )
1015 {
1016     int i;
1017     vlc_object_t *p_tmp;
1018
1019     switch( i_mode & 0x000f )
1020     {
1021     case FIND_PARENT:
1022         p_tmp = p_this->p_parent;
1023         if( p_tmp )
1024         {
1025             if( p_tmp->i_object_type == i_type )
1026             {
1027                 p_tmp->i_refcount++;
1028                 return p_tmp;
1029             }
1030             else
1031             {
1032                 return FindObject( p_tmp, i_type, i_mode );
1033             }
1034         }
1035         break;
1036
1037     case FIND_CHILD:
1038         for( i = p_this->i_children; i--; )
1039         {
1040             p_tmp = p_this->pp_children[i];
1041             if( p_tmp->i_object_type == i_type )
1042             {
1043                 p_tmp->i_refcount++;
1044                 return p_tmp;
1045             }
1046             else if( p_tmp->i_children )
1047             {
1048                 p_tmp = FindObject( p_tmp, i_type, i_mode );
1049                 if( p_tmp )
1050                 {
1051                     return p_tmp;
1052                 }
1053             }
1054         }
1055         break;
1056
1057     case FIND_ANYWHERE:
1058         /* Handled in vlc_object_find */
1059         break;
1060     }
1061
1062     return NULL;
1063 }
1064
1065 static vlc_object_t * FindObjectName( vlc_object_t *p_this,
1066                                       const char *psz_name,
1067                                       int i_mode )
1068 {
1069     int i;
1070     vlc_object_t *p_tmp;
1071
1072     switch( i_mode & 0x000f )
1073     {
1074     case FIND_PARENT:
1075         p_tmp = p_this->p_parent;
1076         if( p_tmp )
1077         {
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
1085             {
1086                 return FindObjectName( p_tmp, psz_name, i_mode );
1087             }
1088         }
1089         break;
1090
1091     case FIND_CHILD:
1092         for( i = p_this->i_children; i--; )
1093         {
1094             p_tmp = p_this->pp_children[i];
1095             if( p_tmp->psz_object_name
1096                 && !strcmp( p_tmp->psz_object_name, psz_name ) )
1097             {
1098                 p_tmp->i_refcount++;
1099                 return p_tmp;
1100             }
1101             else if( p_tmp->i_children )
1102             {
1103                 p_tmp = FindObjectName( p_tmp, psz_name, i_mode );
1104                 if( p_tmp )
1105                 {
1106                     return p_tmp;
1107                 }
1108             }
1109         }
1110         break;
1111
1112     case FIND_ANYWHERE:
1113         /* Handled in vlc_object_find */
1114         break;
1115     }
1116
1117     return NULL;
1118 }
1119
1120 static void DetachObject( vlc_object_t *p_this )
1121 {
1122     vlc_object_t *p_parent = p_this->p_parent;
1123     int i_index, i;
1124
1125     /* Remove p_this's parent */
1126     p_this->p_parent = NULL;
1127
1128     /* Remove all of p_parent's children which are p_this */
1129     for( i_index = p_parent->i_children ; i_index-- ; )
1130     {
1131         if( p_parent->pp_children[i_index] == p_this )
1132         {
1133             p_parent->i_children--;
1134             for( i = i_index ; i < p_parent->i_children ; i++ )
1135             {
1136                 p_parent->pp_children[i] = p_parent->pp_children[i+1];
1137             }
1138         }
1139     }
1140
1141     if( p_parent->i_children )
1142     {
1143         p_parent->pp_children = (vlc_object_t **)realloc( p_parent->pp_children,
1144                                p_parent->i_children * sizeof(vlc_object_t *) );
1145     }
1146     else
1147     {
1148         free( p_parent->pp_children );
1149         p_parent->pp_children = NULL;
1150     }
1151 }
1152
1153 /*****************************************************************************
1154  * SetAttachment: recursively set the b_attached flag of a subtree.
1155  *****************************************************************************
1156  * This function is used by the attach and detach functions to propagate
1157  * the b_attached flag in a subtree.
1158  *****************************************************************************/
1159 static void SetAttachment( vlc_object_t *p_this, vlc_bool_t b_attached )
1160 {
1161     int i_index;
1162
1163     for( i_index = p_this->i_children ; i_index-- ; )
1164     {
1165         SetAttachment( p_this->pp_children[i_index], b_attached );
1166     }
1167
1168     p_this->b_attached = b_attached;
1169 }
1170
1171 static void PrintObject( vlc_object_t *p_this, const char *psz_prefix )
1172 {
1173     char psz_children[20], psz_refcount[20], psz_thread[30], psz_name[50],
1174          psz_parent[20];
1175
1176     psz_name[0] = '\0';
1177     if( p_this->psz_object_name )
1178     {
1179         snprintf( psz_name, 49, " \"%s\"", p_this->psz_object_name );
1180         if( psz_name[48] )
1181             psz_name[48] = '\"';
1182     }
1183
1184     psz_children[0] = '\0';
1185     switch( p_this->i_children )
1186     {
1187         case 0:
1188             break;
1189         case 1:
1190             strcpy( psz_children, ", 1 child" );
1191             break;
1192         default:
1193             snprintf( psz_children, 19, ", %i children", p_this->i_children );
1194             break;
1195     }
1196
1197     psz_refcount[0] = '\0';
1198     if( p_this->i_refcount )
1199         snprintf( psz_refcount, 19, ", refcount %i", p_this->i_refcount );
1200
1201     psz_thread[0] = '\0';
1202     if( p_this->b_thread )
1203         snprintf( psz_thread, 29, " (thread %d)", (int)p_this->thread_id );
1204
1205     psz_parent[0] = '\0';
1206     if( p_this->p_parent )
1207         snprintf( psz_parent, 19, ", parent %i", p_this->p_parent->i_object_id );
1208
1209     printf( " %so %.8i %s%s%s%s%s%s\n", psz_prefix,
1210             p_this->i_object_id, p_this->psz_object_type,
1211             psz_name, psz_thread, psz_refcount, psz_children,
1212             psz_parent );
1213 }
1214
1215 static void DumpStructure( vlc_object_t *p_this, int i_level, char *psz_foo )
1216 {
1217     int i;
1218     char i_back = psz_foo[i_level];
1219     psz_foo[i_level] = '\0';
1220
1221     PrintObject( p_this, psz_foo );
1222
1223     psz_foo[i_level] = i_back;
1224
1225     if( i_level / 2 >= MAX_DUMPSTRUCTURE_DEPTH )
1226     {
1227         msg_Warn( p_this, "structure tree is too deep" );
1228         return;
1229     }
1230
1231     for( i = 0 ; i < p_this->i_children ; i++ )
1232     {
1233         if( i_level )
1234         {
1235             psz_foo[i_level-1] = ' ';
1236
1237             if( psz_foo[i_level-2] == '`' )
1238             {
1239                 psz_foo[i_level-2] = ' ';
1240             }
1241         }
1242
1243         if( i == p_this->i_children - 1 )
1244         {
1245             psz_foo[i_level] = '`';
1246         }
1247         else
1248         {
1249             psz_foo[i_level] = '|';
1250         }
1251
1252         psz_foo[i_level+1] = '-';
1253         psz_foo[i_level+2] = '\0';
1254
1255         DumpStructure( p_this->pp_children[i], i_level + 2, psz_foo );
1256     }
1257 }
1258
1259 static vlc_list_t * NewList( int i_count )
1260 {
1261     vlc_list_t * p_list = (vlc_list_t *)malloc( sizeof( vlc_list_t ) );
1262     if( p_list == NULL )
1263     {
1264         return NULL;
1265     }
1266
1267     p_list->i_count = i_count;
1268
1269     if( i_count == 0 )
1270     {
1271         p_list->p_values = NULL;
1272         return p_list;
1273     }
1274
1275     p_list->p_values = malloc( i_count * sizeof( vlc_value_t ) );
1276     if( p_list->p_values == NULL )
1277     {
1278         p_list->i_count = 0;
1279         return p_list;
1280     }
1281
1282     return p_list;
1283 }
1284
1285 static void ListReplace( vlc_list_t *p_list, vlc_object_t *p_object,
1286                          int i_index )
1287 {
1288     if( p_list == NULL || i_index >= p_list->i_count )
1289     {
1290         return;
1291     }
1292
1293     p_object->i_refcount++;
1294
1295     p_list->p_values[i_index].p_object = p_object;
1296
1297     return;
1298 }
1299
1300 /*static void ListAppend( vlc_list_t *p_list, vlc_object_t *p_object )
1301 {
1302     if( p_list == NULL )
1303     {
1304         return;
1305     }
1306
1307     p_list->p_values = realloc( p_list->p_values, (p_list->i_count + 1)
1308                                 * sizeof( vlc_value_t ) );
1309     if( p_list->p_values == NULL )
1310     {
1311         p_list->i_count = 0;
1312         return;
1313     }
1314
1315     p_object->i_refcount++;
1316
1317     p_list->p_values[p_list->i_count].p_object = p_object;
1318     p_list->i_count++;
1319
1320     return;
1321 }*/
1322
1323 static int CountChildren( vlc_object_t *p_this, int i_type )
1324 {
1325     vlc_object_t *p_tmp;
1326     int i, i_count = 0;
1327
1328     for( i = 0; i < p_this->i_children; i++ )
1329     {
1330         p_tmp = p_this->pp_children[i];
1331
1332         if( p_tmp->i_object_type == i_type )
1333         {
1334             i_count++;
1335         }
1336
1337         if( p_tmp->i_children )
1338         {
1339             i_count += CountChildren( p_tmp, i_type );
1340         }
1341     }
1342
1343     return i_count;
1344 }
1345
1346 static void ListChildren( vlc_list_t *p_list, vlc_object_t *p_this, int i_type )
1347 {
1348     vlc_object_t *p_tmp;
1349     int i;
1350
1351     for( i = 0; i < p_this->i_children; i++ )
1352     {
1353         p_tmp = p_this->pp_children[i];
1354
1355         if( p_tmp->i_object_type == i_type )
1356         {
1357             ListReplace( p_list, p_tmp, p_list->i_count++ );
1358         }
1359
1360         if( p_tmp->i_children )
1361         {
1362             ListChildren( p_list, p_tmp, i_type );
1363         }
1364     }
1365 }