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