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