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