1 /*****************************************************************************
2 * objects.c: vlc_object_t handling
3 *****************************************************************************
4 * Copyright (C) 2004-2007 the VideoLAN team
7 * Authors: Samuel Hocevar <sam@zoy.org>
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.
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.
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 *****************************************************************************/
26 * This file contains the functions to handle the vlc_object_t type
30 /*****************************************************************************
32 *****************************************************************************/
35 #include "../libvlc.h"
38 #include "audio_output/aout_internal.h"
40 #include <vlc_access.h>
41 #include <vlc_demux.h>
42 #include <vlc_stream.h>
45 #include "stream_output/stream_output.h"
47 #include "vlc_playlist.h"
48 #include "vlc_interface.h"
49 #include "vlc_codec.h"
50 #include "vlc_filter.h"
52 #include "vlc_httpd.h"
54 #include "input/vlm_internal.h"
61 #include "variables.h"
63 /*****************************************************************************
65 *****************************************************************************/
66 static int DumpCommand( vlc_object_t *, char const *,
67 vlc_value_t, vlc_value_t, void * );
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 );
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 );
83 /*****************************************************************************
84 * Local structure lock
85 *****************************************************************************/
86 static vlc_mutex_t structure_lock;
87 static vlc_object_internals_t global_internals;
89 vlc_object_t *vlc_custom_create( vlc_object_t *p_this, size_t i_size,
90 int i_type, const char *psz_type )
93 vlc_object_internals_t *p_priv;
95 if( i_type == VLC_OBJECT_GLOBAL )
98 p_priv = &global_internals;
99 memset( p_priv, 0, sizeof( *p_priv ) );
103 p_priv = calloc( 1, sizeof( *p_priv ) + i_size );
107 p_new = (vlc_object_t *)(p_priv + 1);
110 p_new->p_internals = p_priv;
111 p_new->i_object_type = i_type;
112 p_new->psz_object_type = psz_type;
114 p_new->psz_object_name = NULL;
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;
122 p_new->psz_header = NULL;
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;
131 p_priv->p_vars = calloc( sizeof( variable_t ), 16 );
133 if( !p_priv->p_vars )
135 if( i_type != VLC_OBJECT_GLOBAL )
140 if( i_type == VLC_OBJECT_GLOBAL )
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;
146 p_libvlc_global->i_counter = 0;
147 p_new->i_object_id = 0;
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;
156 libvlc_global_data_t *p_libvlc_global = vlc_global();
157 if( i_type == VLC_OBJECT_LIBVLC )
159 p_new->p_libvlc = (libvlc_int_t*)p_new;
160 p_priv->b_attached = VLC_TRUE;
164 p_new->p_libvlc = p_this->p_libvlc;
167 vlc_mutex_lock( &structure_lock );
169 p_libvlc_global->i_counter++;
170 p_new->i_object_id = p_libvlc_global->i_counter;
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,
177 vlc_mutex_unlock( &structure_lock );
180 p_new->i_refcount = 0;
181 p_new->p_parent = NULL;
182 p_new->pp_children = NULL;
183 p_new->i_children = 0;
185 p_new->p_private = NULL;
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 );
192 if( i_type == VLC_OBJECT_GLOBAL )
194 vlc_mutex_init( p_new, &structure_lock );
197 if( i_type == VLC_OBJECT_LIBVLC )
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 );
212 * Allocates and initializes a vlc object.
214 * @param i_type known object type (all of them are negative integer values),
215 * or object byte size (always positive).
217 * @return the new object, or NULL on error.
219 void * __vlc_object_create( vlc_object_t *p_this, int i_type )
221 const char * psz_type;
226 case VLC_OBJECT_GLOBAL:
227 i_size = sizeof(libvlc_global_data_t);
230 case VLC_OBJECT_LIBVLC:
231 i_size = sizeof(libvlc_int_t);
234 case VLC_OBJECT_INTF:
235 i_size = sizeof(intf_thread_t);
236 psz_type = "interface";
238 case VLC_OBJECT_DIALOGS:
239 i_size = sizeof(intf_thread_t);
240 psz_type = "dialogs";
242 case VLC_OBJECT_PLAYLIST:
243 i_size = sizeof(playlist_t);
244 psz_type = "playlist";
247 i_size = sizeof(services_discovery_t);
248 psz_type = "services discovery";
250 case VLC_OBJECT_INPUT:
251 i_size = sizeof(input_thread_t);
254 case VLC_OBJECT_DEMUX:
255 i_size = sizeof(demux_t);
258 case VLC_OBJECT_ACCESS:
259 i_size = sizeof(access_t);
262 case VLC_OBJECT_DECODER:
263 i_size = sizeof(decoder_t);
264 psz_type = "decoder";
266 case VLC_OBJECT_PACKETIZER:
267 i_size = sizeof(decoder_t);
268 psz_type = "packetizer";
270 case VLC_OBJECT_ENCODER:
271 i_size = sizeof(encoder_t);
272 psz_type = "encoder";
274 case VLC_OBJECT_FILTER:
275 i_size = sizeof(filter_t);
278 case VLC_OBJECT_VOUT:
279 i_size = sizeof(vout_thread_t);
280 psz_type = "video output";
283 i_size = sizeof(spu_t);
284 psz_type = "subpicture";
286 case VLC_OBJECT_AOUT:
287 i_size = sizeof(aout_instance_t);
288 psz_type = "audio output";
290 case VLC_OBJECT_SOUT:
291 i_size = sizeof(sout_instance_t);
292 psz_type = "stream output";
295 i_size = sizeof( vlm_t );
296 psz_type = "vlm dameon";
299 i_size = sizeof( vod_t );
300 psz_type = "vod server";
303 i_size = sizeof( tls_t );
307 i_size = sizeof( xml_t );
310 case VLC_OBJECT_OPENGL:
311 i_size = sizeof( vout_thread_t );
314 case VLC_OBJECT_ANNOUNCE:
315 i_size = sizeof( announce_handler_t );
316 psz_type = "announce";
318 case VLC_OBJECT_META_ENGINE:
319 i_size = sizeof( meta_engine_t );
320 psz_type = "meta engine";
322 case VLC_OBJECT_OSDMENU:
323 i_size = sizeof( osd_menu_t );
324 psz_type = "osd menu";
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";
334 return vlc_custom_create( p_this, i_size, i_type, psz_type );
339 ****************************************************************************
340 * Destroy a vlc object
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 )
348 vlc_object_internals_t *p_priv = vlc_internals( p_this );
351 if( p_this->i_children )
353 msg_Err( p_this, "cannot delete object (%i, %s) with children" ,
354 p_this->i_object_id, p_this->psz_object_name );
358 if( p_this->p_parent )
360 msg_Err( p_this, "cannot delete object (%i, %s) with a parent",
361 p_this->i_object_id, p_this->psz_object_name );
365 while( p_this->i_refcount )
369 /* Don't warn immediately ... 100ms seems OK */
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 );
377 else if( i_delay == 10 )
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 );
384 else if( i_delay == 20 )
387 "waited too long, cancelling destruction (id=%d,type=%d)",
388 p_this->i_object_id, p_this->i_object_type );
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 )
399 var_Destroy( p_this, p_priv->p_vars[p_priv->i_vars - 1].psz_name );
402 free( p_priv->p_vars );
403 vlc_mutex_destroy( &p_priv->var_lock );
405 if( p_this->psz_header ) free( p_this->psz_header );
407 if( p_this->i_object_type == VLC_OBJECT_GLOBAL )
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--;
415 vlc_mutex_destroy( &structure_lock );
419 libvlc_global_data_t *p_libvlc_global = vlc_global();
422 vlc_mutex_lock( &structure_lock );
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 );
431 vlc_mutex_unlock( &structure_lock );
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);
440 vlc_mutex_destroy( &p_this->object_lock );
441 vlc_cond_destroy( &p_this->object_wait );
443 /* global is not dynamically allocated by vlc_object_create */
444 if( p_this->i_object_type != VLC_OBJECT_GLOBAL )
449 void __vlc_object_kill( vlc_object_t *p_this )
451 vlc_mutex_lock( &p_this->object_lock );
453 if( p_this->i_object_type == VLC_OBJECT_LIBVLC )
454 for( int i = 0; i < p_this->i_children ; i++ )
455 vlc_object_kill( p_this->pp_children[i] );
457 p_this->b_die = VLC_TRUE;
458 vlc_cond_signal( &p_this->object_wait );
459 vlc_mutex_unlock( &p_this->object_lock );
463 vlc_bool_t __vlc_object_dying_unlocked( vlc_object_t *p_this )
465 vlc_assert_locked( &p_this->object_lock );
466 return p_this->b_die;
470 vlc_bool_t __vlc_object_dying( vlc_object_t *p_this )
473 vlc_mutex_lock( &p_this->object_lock );
474 b = __vlc_object_dying_unlocked( p_this );
475 vlc_mutex_unlock( &p_this->object_lock );
481 * find an object given its ID
483 * This function looks for the object whose i_object_id field is i_id. We
484 * use a dichotomy so that lookups are in log2(n).
485 *****************************************************************************/
486 void * __vlc_object_get( vlc_object_t *p_this, int i_id )
489 vlc_object_t **pp_objects;
490 libvlc_global_data_t *p_libvlc_global = vlc_global();
492 vlc_mutex_lock( &structure_lock );
494 pp_objects = p_libvlc_global->pp_objects;
496 /* Perform our dichotomy */
497 for( i_max = p_libvlc_global->i_objects - 1 ; ; )
499 i_middle = i_max / 2;
501 if( pp_objects[i_middle]->i_object_id > i_id )
505 else if( pp_objects[i_middle]->i_object_id < i_id )
509 pp_objects += i_middle;
514 /* This happens when there are only two remaining objects */
515 if( pp_objects[i_middle+1]->i_object_id == i_id )
517 vlc_mutex_unlock( &structure_lock );
518 pp_objects[i_middle+1]->i_refcount++;
519 return pp_objects[i_middle+1];
526 vlc_mutex_unlock( &structure_lock );
527 pp_objects[i_middle]->i_refcount++;
528 return pp_objects[i_middle];
533 /* this means that i_max == i_middle, and since we have already
534 * tested pp_objects[i_middle]), p_found is properly set. */
539 vlc_mutex_unlock( &structure_lock );
544 ****************************************************************************
545 * find a typed object and increment its refcount
546 *****************************************************************************
547 * This function recursively looks for a given object type. i_mode can be one
548 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
549 *****************************************************************************/
550 void * __vlc_object_find( vlc_object_t *p_this, int i_type, int i_mode )
552 vlc_object_t *p_found;
554 vlc_mutex_lock( &structure_lock );
556 /* If we are of the requested type ourselves, don't look further */
557 if( !(i_mode & FIND_STRICT) && p_this->i_object_type == i_type )
559 p_this->i_refcount++;
560 vlc_mutex_unlock( &structure_lock );
564 /* Otherwise, recursively look for the object */
565 if( (i_mode & 0x000f) == FIND_ANYWHERE )
567 vlc_object_t *p_root = p_this;
570 while( p_root->p_parent != NULL &&
571 p_root != VLC_OBJECT( p_this->p_libvlc ) )
573 p_root = p_root->p_parent;
576 p_found = FindObject( p_root, i_type, (i_mode & ~0x000f)|FIND_CHILD );
577 if( p_found == NULL && p_root != VLC_OBJECT( p_this->p_libvlc ) )
579 p_found = FindObject( VLC_OBJECT( p_this->p_libvlc ),
580 i_type, (i_mode & ~0x000f)|FIND_CHILD );
585 p_found = FindObject( p_this, i_type, i_mode );
588 vlc_mutex_unlock( &structure_lock );
594 ****************************************************************************
595 * find a named object and increment its refcount
596 *****************************************************************************
597 * This function recursively looks for a given object name. i_mode can be one
598 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
599 *****************************************************************************/
600 void * __vlc_object_find_name( vlc_object_t *p_this, const char *psz_name,
603 vlc_object_t *p_found;
605 vlc_mutex_lock( &structure_lock );
607 /* If have the requested name ourselves, don't look further */
608 if( !(i_mode & FIND_STRICT)
609 && p_this->psz_object_name
610 && !strcmp( p_this->psz_object_name, psz_name ) )
612 p_this->i_refcount++;
613 vlc_mutex_unlock( &structure_lock );
617 /* Otherwise, recursively look for the object */
618 if( (i_mode & 0x000f) == FIND_ANYWHERE )
620 vlc_object_t *p_root = p_this;
623 while( p_root->p_parent != NULL &&
624 p_root != VLC_OBJECT( p_this->p_libvlc ) )
626 p_root = p_root->p_parent;
629 p_found = FindObjectName( p_root, psz_name,
630 (i_mode & ~0x000f)|FIND_CHILD );
631 if( p_found == NULL && p_root != VLC_OBJECT( p_this->p_libvlc ) )
633 p_found = FindObjectName( VLC_OBJECT( p_this->p_libvlc ),
634 psz_name, (i_mode & ~0x000f)|FIND_CHILD );
639 p_found = FindObjectName( p_this, psz_name, i_mode );
642 vlc_mutex_unlock( &structure_lock );
648 ****************************************************************************
649 * increment an object refcount
650 *****************************************************************************/
651 void __vlc_object_yield( vlc_object_t *p_this )
653 vlc_mutex_lock( &structure_lock );
654 p_this->i_refcount++;
655 vlc_mutex_unlock( &structure_lock );
659 ****************************************************************************
660 * decrement an object refcount
661 *****************************************************************************/
662 void __vlc_object_release( vlc_object_t *p_this )
664 vlc_mutex_lock( &structure_lock );
665 p_this->i_refcount--;
666 vlc_mutex_unlock( &structure_lock );
670 ****************************************************************************
671 * attach object to a parent object
672 *****************************************************************************
673 * This function sets p_this as a child of p_parent, and p_parent as a parent
674 * of p_this. This link can be undone using vlc_object_detach.
675 *****************************************************************************/
676 void __vlc_object_attach( vlc_object_t *p_this, vlc_object_t *p_parent )
678 if( !p_this ) return;
680 vlc_mutex_lock( &structure_lock );
682 /* Attach the parent to its child */
683 p_this->p_parent = p_parent;
685 /* Attach the child to its parent */
686 INSERT_ELEM( p_parent->pp_children, p_parent->i_children,
687 p_parent->i_children, p_this );
689 /* Climb up the tree to see whether we are connected with the root */
690 if( p_parent->p_internals->b_attached )
692 SetAttachment( p_this, VLC_TRUE );
695 vlc_mutex_unlock( &structure_lock );
699 ****************************************************************************
700 * detach object from its parent
701 *****************************************************************************
702 * This function removes all links between an object and its parent.
703 *****************************************************************************/
704 void __vlc_object_detach( vlc_object_t *p_this )
706 if( !p_this ) return;
708 vlc_mutex_lock( &structure_lock );
709 if( !p_this->p_parent )
711 msg_Err( p_this, "object is not attached" );
712 vlc_mutex_unlock( &structure_lock );
716 /* Climb up the tree to see whether we are connected with the root */
717 if( p_this->p_parent->p_internals->b_attached )
719 SetAttachment( p_this, VLC_FALSE );
722 DetachObject( p_this );
723 vlc_mutex_unlock( &structure_lock );
728 ****************************************************************************
729 * find a list typed objects and increment their refcount
730 *****************************************************************************
731 * This function recursively looks for a given object type. i_mode can be one
732 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
733 *****************************************************************************/
734 vlc_list_t * __vlc_list_find( vlc_object_t *p_this, int i_type, int i_mode )
737 vlc_object_t **pp_current, **pp_end;
738 int i_count = 0, i_index = 0;
739 libvlc_global_data_t *p_libvlc_global = vlc_global();
741 vlc_mutex_lock( &structure_lock );
743 /* Look for the objects */
744 switch( i_mode & 0x000f )
747 pp_current = p_libvlc_global->pp_objects;
748 pp_end = pp_current + p_libvlc_global->i_objects;
750 for( ; pp_current < pp_end ; pp_current++ )
752 if( (*pp_current)->p_internals->b_attached
753 && (*pp_current)->i_object_type == i_type )
759 p_list = NewList( i_count );
760 pp_current = p_libvlc_global->pp_objects;
762 for( ; pp_current < pp_end ; pp_current++ )
764 if( (*pp_current)->p_internals->b_attached
765 && (*pp_current)->i_object_type == i_type )
767 ListReplace( p_list, *pp_current, i_index );
768 if( i_index < i_count ) i_index++;
774 i_count = CountChildren( p_this, i_type );
775 p_list = NewList( i_count );
777 /* Check allocation was successful */
778 if( p_list->i_count != i_count )
780 msg_Err( p_this, "list allocation failed!" );
786 ListChildren( p_list, p_this, i_type );
790 msg_Err( p_this, "unimplemented!" );
791 p_list = NewList( 0 );
795 vlc_mutex_unlock( &structure_lock );
800 /*****************************************************************************
801 * DumpCommand: print the current vlc structure
802 *****************************************************************************
803 * This function prints either an ASCII tree showing the connections between
804 * vlc objects, and additional information such as their refcount, thread ID,
805 * etc. (command "tree"), or the same data as a simple list (command "list").
806 *****************************************************************************/
807 static int DumpCommand( vlc_object_t *p_this, char const *psz_cmd,
808 vlc_value_t oldval, vlc_value_t newval, void *p_data )
810 libvlc_global_data_t *p_libvlc_global = vlc_global();
812 (void)oldval; (void)p_data;
813 if( *psz_cmd == 'l' )
815 vlc_mutex_lock( &structure_lock );
817 vlc_object_t **pp_current, **pp_end;
819 pp_current = p_libvlc_global->pp_objects;
820 pp_end = pp_current + p_libvlc_global->i_objects;
822 for( ; pp_current < pp_end ; pp_current++ )
824 if( (*pp_current)->p_internals->b_attached )
826 PrintObject( *pp_current, "" );
830 printf( " o %.8i %s (not attached)\n",
831 (*pp_current)->i_object_id,
832 (*pp_current)->psz_object_type );
836 vlc_mutex_unlock( &structure_lock );
840 vlc_object_t *p_object = NULL;
842 if( *newval.psz_string )
845 int i_id = strtol( newval.psz_string, &end, 0 );
846 if( end != newval.psz_string )
847 p_object = vlc_object_get( p_this, i_id );
850 /* try using the object's name to find it */
851 vlc_object_t *p_libvlc = vlc_object_get( p_this, 1 );
854 /* Look in p_libvlc's children tree */
855 p_object = vlc_object_find_name( p_libvlc,
858 vlc_object_release( p_libvlc );
862 /* If it's not in libvlc, look in libvlc_global (== p_this) */
863 p_object = vlc_object_find_name( p_this,
875 vlc_mutex_lock( &structure_lock );
877 if( *psz_cmd == 't' )
879 char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
882 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
885 DumpStructure( p_object, 0, psz_foo );
887 else if( *psz_cmd == 'v' )
892 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
894 PrintObject( p_object, "" );
896 if( !p_object->p_internals->i_vars )
897 printf( " `-o No variables\n" );
898 for( i = 0; i < p_object->p_internals->i_vars; i++ )
900 variable_t *p_var = p_object->p_internals->p_vars + i;
902 const char *psz_type = "unknown";
903 switch( p_var->i_type & VLC_VAR_TYPE )
905 #define MYCASE( type, nice ) \
906 case VLC_VAR_ ## type: \
909 MYCASE( VOID, "void" );
910 MYCASE( BOOL, "bool" );
911 MYCASE( INTEGER, "integer" );
912 MYCASE( HOTKEY, "hotkey" );
913 MYCASE( STRING, "string" );
914 MYCASE( MODULE, "module" );
915 MYCASE( FILE, "file" );
916 MYCASE( DIRECTORY, "directory" );
917 MYCASE( VARIABLE, "variable" );
918 MYCASE( FLOAT, "float" );
919 MYCASE( TIME, "time" );
920 MYCASE( ADDRESS, "address" );
921 MYCASE( MUTEX, "mutex" );
922 MYCASE( LIST, "list" );
925 printf( " %c-o \"%s\" (%s",
926 i + 1 == p_object->p_internals->i_vars ? '`' : '|',
927 p_var->psz_name, psz_type );
928 if( p_var->psz_text )
929 printf( ", %s", p_var->psz_text );
931 if( p_var->i_type & VLC_VAR_ISCOMMAND )
932 printf( ", command" );
933 if( p_var->i_entries )
934 printf( ", %d callbacks", p_var->i_entries );
935 switch( p_var->i_type & 0x00f0 )
941 printf( ": %s", p_var->val.b_bool ? "true" : "false" );
943 case VLC_VAR_INTEGER:
944 printf( ": %d", p_var->val.i_int );
947 printf( ": \"%s\"", p_var->val.psz_string );
950 printf( ": %f", p_var->val.f_float );
953 printf( ": " I64Fi, (int64_t)p_var->val.i_time );
955 case VLC_VAR_ADDRESS:
956 printf( ": %p", p_var->val.p_address );
966 vlc_mutex_unlock( &structure_lock );
968 if( *newval.psz_string )
970 vlc_object_release( p_object );
977 /*****************************************************************************
978 * vlc_list_release: free a list previously allocated by vlc_list_find
979 *****************************************************************************
980 * This function decreases the refcount of all objects in the list and
982 *****************************************************************************/
983 void vlc_list_release( vlc_list_t *p_list )
987 vlc_mutex_lock( &structure_lock );
988 for( i_index = 0; i_index < p_list->i_count; i_index++ )
990 p_list->p_values[i_index].p_object->i_refcount--;
992 vlc_mutex_unlock( &structure_lock );
994 free( p_list->p_values );
998 /* Following functions are local */
1000 /*****************************************************************************
1001 * FindIndex: find the index of an object in an array of objects
1002 *****************************************************************************
1003 * This function assumes that p_this can be found in pp_objects. It will not
1004 * crash if p_this cannot be found, but will return a wrong value. It is your
1005 * duty to check the return value if you are not certain that the object could
1006 * be found for sure.
1007 *****************************************************************************/
1008 static int FindIndex( vlc_object_t *p_this,
1009 vlc_object_t **pp_objects, int i_count )
1011 int i_middle = i_count / 2;
1018 if( pp_objects[i_middle] == p_this )
1028 /* We take advantage of the sorted array */
1029 if( pp_objects[i_middle]->i_object_id < p_this->i_object_id )
1031 return i_middle + FindIndex( p_this, pp_objects + i_middle,
1032 i_count - i_middle );
1036 return FindIndex( p_this, pp_objects, i_middle );
1040 static vlc_object_t * FindObject( vlc_object_t *p_this, int i_type, int i_mode )
1043 vlc_object_t *p_tmp;
1045 switch( i_mode & 0x000f )
1048 p_tmp = p_this->p_parent;
1051 if( p_tmp->i_object_type == i_type )
1053 p_tmp->i_refcount++;
1058 return FindObject( p_tmp, i_type, i_mode );
1064 for( i = p_this->i_children; i--; )
1066 p_tmp = p_this->pp_children[i];
1067 if( p_tmp->i_object_type == i_type )
1069 p_tmp->i_refcount++;
1072 else if( p_tmp->i_children )
1074 p_tmp = FindObject( p_tmp, i_type, i_mode );
1084 /* Handled in vlc_object_find */
1091 static vlc_object_t * FindObjectName( vlc_object_t *p_this,
1092 const char *psz_name,
1096 vlc_object_t *p_tmp;
1098 switch( i_mode & 0x000f )
1101 p_tmp = p_this->p_parent;
1104 if( p_tmp->psz_object_name
1105 && !strcmp( p_tmp->psz_object_name, psz_name ) )
1107 p_tmp->i_refcount++;
1112 return FindObjectName( p_tmp, psz_name, i_mode );
1118 for( i = p_this->i_children; i--; )
1120 p_tmp = p_this->pp_children[i];
1121 if( p_tmp->psz_object_name
1122 && !strcmp( p_tmp->psz_object_name, psz_name ) )
1124 p_tmp->i_refcount++;
1127 else if( p_tmp->i_children )
1129 p_tmp = FindObjectName( p_tmp, psz_name, i_mode );
1139 /* Handled in vlc_object_find */
1146 static void DetachObject( vlc_object_t *p_this )
1148 vlc_object_t *p_parent = p_this->p_parent;
1151 /* Remove p_this's parent */
1152 p_this->p_parent = NULL;
1154 /* Remove all of p_parent's children which are p_this */
1155 for( i_index = p_parent->i_children ; i_index-- ; )
1157 if( p_parent->pp_children[i_index] == p_this )
1159 p_parent->i_children--;
1160 for( i = i_index ; i < p_parent->i_children ; i++ )
1162 p_parent->pp_children[i] = p_parent->pp_children[i+1];
1167 if( p_parent->i_children )
1169 p_parent->pp_children = (vlc_object_t **)realloc( p_parent->pp_children,
1170 p_parent->i_children * sizeof(vlc_object_t *) );
1174 free( p_parent->pp_children );
1175 p_parent->pp_children = NULL;
1179 /*****************************************************************************
1180 * SetAttachment: recursively set the b_attached flag of a subtree.
1181 *****************************************************************************
1182 * This function is used by the attach and detach functions to propagate
1183 * the b_attached flag in a subtree.
1184 *****************************************************************************/
1185 static void SetAttachment( vlc_object_t *p_this, vlc_bool_t b_attached )
1189 for( i_index = p_this->i_children ; i_index-- ; )
1191 SetAttachment( p_this->pp_children[i_index], b_attached );
1194 p_this->p_internals->b_attached = b_attached;
1197 static void PrintObject( vlc_object_t *p_this, const char *psz_prefix )
1199 char psz_children[20], psz_refcount[20], psz_thread[30], psz_name[50],
1203 if( p_this->psz_object_name )
1205 snprintf( psz_name, 49, " \"%s\"", p_this->psz_object_name );
1207 psz_name[48] = '\"';
1210 psz_children[0] = '\0';
1211 switch( p_this->i_children )
1216 strcpy( psz_children, ", 1 child" );
1219 snprintf( psz_children, 19, ", %i children", p_this->i_children );
1223 psz_refcount[0] = '\0';
1224 if( p_this->i_refcount )
1225 snprintf( psz_refcount, 19, ", refcount %i", p_this->i_refcount );
1227 psz_thread[0] = '\0';
1228 if( p_this->p_internals->b_thread )
1229 snprintf( psz_thread, 29, " (thread %u)",
1230 #if defined(WIN32) || defined(UNDER_CE)
1231 (unsigned)p_this->p_internals->thread_id.id );
1233 (unsigned)p_this->p_internals->thread_id );
1236 psz_parent[0] = '\0';
1237 if( p_this->p_parent )
1238 snprintf( psz_parent, 19, ", parent %i", p_this->p_parent->i_object_id );
1240 printf( " %so %.8i %s%s%s%s%s%s\n", psz_prefix,
1241 p_this->i_object_id, p_this->psz_object_type,
1242 psz_name, psz_thread, psz_refcount, psz_children,
1246 static void DumpStructure( vlc_object_t *p_this, int i_level, char *psz_foo )
1249 char i_back = psz_foo[i_level];
1250 psz_foo[i_level] = '\0';
1252 PrintObject( p_this, psz_foo );
1254 psz_foo[i_level] = i_back;
1256 if( i_level / 2 >= MAX_DUMPSTRUCTURE_DEPTH )
1258 msg_Warn( p_this, "structure tree is too deep" );
1262 for( i = 0 ; i < p_this->i_children ; i++ )
1266 psz_foo[i_level-1] = ' ';
1268 if( psz_foo[i_level-2] == '`' )
1270 psz_foo[i_level-2] = ' ';
1274 if( i == p_this->i_children - 1 )
1276 psz_foo[i_level] = '`';
1280 psz_foo[i_level] = '|';
1283 psz_foo[i_level+1] = '-';
1284 psz_foo[i_level+2] = '\0';
1286 DumpStructure( p_this->pp_children[i], i_level + 2, psz_foo );
1290 static vlc_list_t * NewList( int i_count )
1292 vlc_list_t * p_list = (vlc_list_t *)malloc( sizeof( vlc_list_t ) );
1293 if( p_list == NULL )
1298 p_list->i_count = i_count;
1302 p_list->p_values = NULL;
1306 p_list->p_values = malloc( i_count * sizeof( vlc_value_t ) );
1307 if( p_list->p_values == NULL )
1309 p_list->i_count = 0;
1316 static void ListReplace( vlc_list_t *p_list, vlc_object_t *p_object,
1319 if( p_list == NULL || i_index >= p_list->i_count )
1324 p_object->i_refcount++;
1326 p_list->p_values[i_index].p_object = p_object;
1331 /*static void ListAppend( vlc_list_t *p_list, vlc_object_t *p_object )
1333 if( p_list == NULL )
1338 p_list->p_values = realloc( p_list->p_values, (p_list->i_count + 1)
1339 * sizeof( vlc_value_t ) );
1340 if( p_list->p_values == NULL )
1342 p_list->i_count = 0;
1346 p_object->i_refcount++;
1348 p_list->p_values[p_list->i_count].p_object = p_object;
1354 static int CountChildren( vlc_object_t *p_this, int i_type )
1356 vlc_object_t *p_tmp;
1359 for( i = 0; i < p_this->i_children; i++ )
1361 p_tmp = p_this->pp_children[i];
1363 if( p_tmp->i_object_type == i_type )
1368 if( p_tmp->i_children )
1370 i_count += CountChildren( p_tmp, i_type );
1377 static void ListChildren( vlc_list_t *p_list, vlc_object_t *p_this, int i_type )
1379 vlc_object_t *p_tmp;
1382 for( i = 0; i < p_this->i_children; i++ )
1384 p_tmp = p_this->pp_children[i];
1386 if( p_tmp->i_object_type == i_type )
1388 ListReplace( p_list, p_tmp, p_list->i_count++ );
1391 if( p_tmp->i_children )
1393 ListChildren( p_list, p_tmp, i_type );