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 *****************************************************************************/
36 # include <stdlib.h> /* realloc() */
39 #include "../libvlc.h"
42 #include "audio_output/aout_internal.h"
44 #include <vlc_access.h>
45 #include <vlc_demux.h>
46 #include <vlc_stream.h>
49 #include "stream_output/stream_output.h"
51 #include "vlc_playlist.h"
52 #include "vlc_interface.h"
53 #include "vlc_codec.h"
54 #include "vlc_filter.h"
56 #include "vlc_httpd.h"
58 #include "input/vlm_internal.h"
65 #include "variables.h"
67 /*****************************************************************************
69 *****************************************************************************/
70 static int DumpCommand( vlc_object_t *, char const *,
71 vlc_value_t, vlc_value_t, void * );
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 );
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 );
87 /*****************************************************************************
88 * Local structure lock
89 *****************************************************************************/
90 static vlc_mutex_t structure_lock;
92 vlc_object_t *vlc_custom_create( vlc_object_t *p_this, size_t i_size,
93 int i_type, const char *psz_type )
95 vlc_object_t * p_new = NULL;
97 if( i_type == VLC_OBJECT_GLOBAL )
103 p_new = malloc( i_size );
104 if( !p_new ) return NULL;
105 memset( p_new, 0, i_size );
108 p_new->i_object_type = i_type;
109 p_new->psz_object_type = psz_type;
111 p_new->psz_object_name = NULL;
113 p_new->b_die = VLC_FALSE;
114 p_new->b_error = VLC_FALSE;
115 p_new->b_dead = VLC_FALSE;
116 p_new->b_attached = VLC_FALSE;
117 p_new->b_force = VLC_FALSE;
119 p_new->psz_header = NULL;
122 if( p_this->i_flags & OBJECT_FLAGS_NODBG )
123 p_new->i_flags |= OBJECT_FLAGS_NODBG;
124 if( p_this->i_flags & OBJECT_FLAGS_QUIET )
125 p_new->i_flags |= OBJECT_FLAGS_QUIET;
126 if( p_this->i_flags & OBJECT_FLAGS_NOINTERACT )
127 p_new->i_flags |= OBJECT_FLAGS_NOINTERACT;
130 p_new->p_vars = (variable_t *)malloc( 16 * sizeof( variable_t ) );
134 if( i_type != VLC_OBJECT_GLOBAL )
139 if( i_type == VLC_OBJECT_GLOBAL )
141 /* If i_type is global, then p_new is actually p_libvlc_global */
142 p_new->p_libvlc_global = (libvlc_global_data_t*)p_new;
143 p_new->p_libvlc = NULL;
145 p_new->p_libvlc_global->i_counter = 0;
146 p_new->i_object_id = 0;
148 p_new->p_libvlc_global->i_objects = 1;
149 p_new->p_libvlc_global->pp_objects = malloc( sizeof(vlc_object_t *) );
150 p_new->p_libvlc_global->pp_objects[0] = p_new;
151 p_new->b_attached = VLC_TRUE;
155 p_new->p_libvlc_global = p_this->p_libvlc_global;
156 p_new->p_libvlc = ( i_type == VLC_OBJECT_LIBVLC ) ? (libvlc_int_t*)p_new
159 vlc_mutex_lock( &structure_lock );
161 p_new->p_libvlc_global->i_counter++;
162 p_new->i_object_id = p_new->p_libvlc_global->i_counter;
164 /* Wooohaa! If *this* fails, we're in serious trouble! Anyway it's
165 * useless to try and recover anything if pp_objects gets smashed. */
166 TAB_APPEND( p_new->p_libvlc_global->i_objects,
167 p_new->p_libvlc_global->pp_objects,
170 vlc_mutex_unlock( &structure_lock );
173 p_new->i_refcount = 0;
174 p_new->p_parent = NULL;
175 p_new->pp_children = NULL;
176 p_new->i_children = 0;
178 p_new->p_private = NULL;
180 /* Initialize mutexes and condvars */
181 vlc_mutex_init( p_new, &p_new->object_lock );
182 vlc_cond_init( p_new, &p_new->object_wait );
183 vlc_mutex_init( p_new, &p_new->var_lock );
185 if( i_type == VLC_OBJECT_GLOBAL )
187 vlc_mutex_init( p_new, &structure_lock );
189 var_Create( p_new, "list", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
190 var_AddCallback( p_new, "list", DumpCommand, NULL );
191 var_Create( p_new, "tree", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
192 var_AddCallback( p_new, "tree", DumpCommand, NULL );
193 var_Create( p_new, "vars", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
194 var_AddCallback( p_new, "vars", DumpCommand, NULL );
202 * Allocates and initializes a vlc object.
204 * @param i_type known object type (all of them are negative integer values),
205 * or object byte size (always positive).
207 * @return the new object, or NULL on error.
209 void * __vlc_object_create( vlc_object_t *p_this, int i_type )
211 const char * psz_type;
216 case VLC_OBJECT_GLOBAL:
217 i_size = sizeof(libvlc_global_data_t);
220 case VLC_OBJECT_LIBVLC:
221 i_size = sizeof(libvlc_int_t);
224 case VLC_OBJECT_INTF:
225 i_size = sizeof(intf_thread_t);
226 psz_type = "interface";
228 case VLC_OBJECT_DIALOGS:
229 i_size = sizeof(intf_thread_t);
230 psz_type = "dialogs";
232 case VLC_OBJECT_PLAYLIST:
233 i_size = sizeof(playlist_t);
234 psz_type = "playlist";
237 i_size = sizeof(services_discovery_t);
238 psz_type = "services discovery";
240 case VLC_OBJECT_INPUT:
241 i_size = sizeof(input_thread_t);
244 case VLC_OBJECT_DEMUX:
245 i_size = sizeof(demux_t);
248 case VLC_OBJECT_ACCESS:
249 i_size = sizeof(access_t);
252 case VLC_OBJECT_DECODER:
253 i_size = sizeof(decoder_t);
254 psz_type = "decoder";
256 case VLC_OBJECT_PACKETIZER:
257 i_size = sizeof(decoder_t);
258 psz_type = "packetizer";
260 case VLC_OBJECT_ENCODER:
261 i_size = sizeof(encoder_t);
262 psz_type = "encoder";
264 case VLC_OBJECT_FILTER:
265 i_size = sizeof(filter_t);
268 case VLC_OBJECT_VOUT:
269 i_size = sizeof(vout_thread_t);
270 psz_type = "video output";
273 i_size = sizeof(spu_t);
274 psz_type = "subpicture";
276 case VLC_OBJECT_AOUT:
277 i_size = sizeof(aout_instance_t);
278 psz_type = "audio output";
280 case VLC_OBJECT_SOUT:
281 i_size = sizeof(sout_instance_t);
282 psz_type = "stream output";
285 i_size = sizeof( vlm_t );
286 psz_type = "vlm dameon";
289 i_size = sizeof( vod_t );
290 psz_type = "vod server";
293 i_size = sizeof( tls_t );
297 i_size = sizeof( xml_t );
300 case VLC_OBJECT_OPENGL:
301 i_size = sizeof( vout_thread_t );
304 case VLC_OBJECT_ANNOUNCE:
305 i_size = sizeof( announce_handler_t );
306 psz_type = "announce";
308 case VLC_OBJECT_META_ENGINE:
309 i_size = sizeof( meta_engine_t );
310 psz_type = "meta engine";
312 case VLC_OBJECT_OSDMENU:
313 i_size = sizeof( osd_menu_t );
314 psz_type = "osd menu";
317 i_size = i_type > (int)sizeof(vlc_object_t)
318 ? i_type : (int)sizeof(vlc_object_t);
319 i_type = VLC_OBJECT_GENERIC;
320 psz_type = "generic";
324 return vlc_custom_create( p_this, i_size, i_type, psz_type );
329 ****************************************************************************
330 * Destroy a vlc object
332 * This function destroys an object that has been previously allocated with
333 * vlc_object_create. The object's refcount must be zero and it must not be
334 * attached to other objects in any way.
335 *****************************************************************************/
336 void __vlc_object_destroy( vlc_object_t *p_this )
340 if( p_this->i_children )
342 msg_Err( p_this, "cannot delete object (%i, %s) with children" ,
343 p_this->i_object_id, p_this->psz_object_name );
347 if( p_this->p_parent )
349 msg_Err( p_this, "cannot delete object (%i, %s) with a parent",
350 p_this->i_object_id, p_this->psz_object_name );
354 while( p_this->i_refcount )
358 /* Don't warn immediately ... 100ms seems OK */
362 "refcount is %i, delaying before deletion (id=%d,type=%d)",
363 p_this->i_refcount, p_this->i_object_id,
364 p_this->i_object_type );
366 else if( i_delay == 10 )
369 "refcount is %i, delaying again (id=%d,type=%d)",
370 p_this->i_refcount, p_this->i_object_id,
371 p_this->i_object_type );
373 else if( i_delay == 20 )
376 "waited too long, cancelling destruction (id=%d,type=%d)",
377 p_this->i_object_id, p_this->i_object_type );
384 /* Destroy the associated variables, starting from the end so that
385 * no memmove calls have to be done. */
386 while( p_this->i_vars )
388 var_Destroy( p_this, p_this->p_vars[p_this->i_vars - 1].psz_name );
391 free( p_this->p_vars );
392 vlc_mutex_destroy( &p_this->var_lock );
394 if( p_this->psz_header ) free( p_this->psz_header );
396 if( p_this->i_object_type == VLC_OBJECT_GLOBAL )
398 /* We are the global object ... no need to lock. */
399 free( p_this->p_libvlc_global->pp_objects );
400 p_this->p_libvlc_global->pp_objects = NULL;
401 p_this->p_libvlc_global->i_objects--;
403 vlc_mutex_destroy( &structure_lock );
409 vlc_mutex_lock( &structure_lock );
411 /* Wooohaa! If *this* fails, we're in serious trouble! Anyway it's
412 * useless to try and recover anything if pp_objects gets smashed. */
413 i_index = FindIndex( p_this, p_this->p_libvlc_global->pp_objects,
414 p_this->p_libvlc_global->i_objects );
415 REMOVE_ELEM( p_this->p_libvlc_global->pp_objects,
416 p_this->p_libvlc_global->i_objects, i_index );
418 vlc_mutex_unlock( &structure_lock );
421 vlc_mutex_destroy( &p_this->object_lock );
422 vlc_cond_destroy( &p_this->object_wait );
424 /* global is not dynamically allocated by vlc_object_create */
425 if( p_this->i_object_type != VLC_OBJECT_GLOBAL )
433 void __vlc_object_kill( vlc_object_t *p_this )
435 vlc_mutex_lock( &p_this->object_lock );
436 p_this->b_die = VLC_TRUE;
437 vlc_mutex_unlock( &p_this->object_lock );
441 vlc_bool_t __vlc_object_dying_unlocked( vlc_object_t *p_this )
443 vlc_assert_locked( &p_this->object_lock );
444 return p_this->b_die;
448 vlc_bool_t __vlc_object_dying( vlc_object_t *p_this )
451 vlc_mutex_lock( &p_this->object_lock );
452 b = __vlc_object_dying_unlocked( p_this );
453 vlc_mutex_unlock( &p_this->object_lock );
459 * find an object given its ID
461 * This function looks for the object whose i_object_id field is i_id. We
462 * use a dichotomy so that lookups are in log2(n).
463 *****************************************************************************/
464 void * __vlc_object_get( vlc_object_t *p_this, int i_id )
467 vlc_object_t **pp_objects;
469 vlc_mutex_lock( &structure_lock );
471 pp_objects = p_this->p_libvlc_global->pp_objects;
473 /* Perform our dichotomy */
474 for( i_max = p_this->p_libvlc_global->i_objects - 1 ; ; )
476 i_middle = i_max / 2;
478 if( pp_objects[i_middle]->i_object_id > i_id )
482 else if( pp_objects[i_middle]->i_object_id < i_id )
486 pp_objects += i_middle;
491 /* This happens when there are only two remaining objects */
492 if( pp_objects[i_middle+1]->i_object_id == i_id )
494 vlc_mutex_unlock( &structure_lock );
495 pp_objects[i_middle+1]->i_refcount++;
496 return pp_objects[i_middle+1];
503 vlc_mutex_unlock( &structure_lock );
504 pp_objects[i_middle]->i_refcount++;
505 return pp_objects[i_middle];
510 /* this means that i_max == i_middle, and since we have already
511 * tested pp_objects[i_middle]), p_found is properly set. */
516 vlc_mutex_unlock( &structure_lock );
521 ****************************************************************************
522 * find a typed object and increment its refcount
523 *****************************************************************************
524 * This function recursively looks for a given object type. i_mode can be one
525 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
526 *****************************************************************************/
527 void * __vlc_object_find( vlc_object_t *p_this, int i_type, int i_mode )
529 vlc_object_t *p_found;
531 vlc_mutex_lock( &structure_lock );
533 /* If we are of the requested type ourselves, don't look further */
534 if( !(i_mode & FIND_STRICT) && p_this->i_object_type == i_type )
536 p_this->i_refcount++;
537 vlc_mutex_unlock( &structure_lock );
541 /* Otherwise, recursively look for the object */
542 if( (i_mode & 0x000f) == FIND_ANYWHERE )
544 vlc_object_t *p_root = p_this;
547 while( p_root->p_parent != NULL &&
548 p_root != VLC_OBJECT( p_this->p_libvlc ) )
550 p_root = p_root->p_parent;
553 p_found = FindObject( p_root, i_type, (i_mode & ~0x000f)|FIND_CHILD );
554 if( p_found == NULL && p_root != VLC_OBJECT( p_this->p_libvlc ) )
556 p_found = FindObject( VLC_OBJECT( p_this->p_libvlc ),
557 i_type, (i_mode & ~0x000f)|FIND_CHILD );
562 p_found = FindObject( p_this, i_type, i_mode );
565 vlc_mutex_unlock( &structure_lock );
571 ****************************************************************************
572 * find a named object and increment its refcount
573 *****************************************************************************
574 * This function recursively looks for a given object name. i_mode can be one
575 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
576 *****************************************************************************/
577 void * __vlc_object_find_name( vlc_object_t *p_this, const char *psz_name,
580 vlc_object_t *p_found;
582 vlc_mutex_lock( &structure_lock );
584 /* If have the requested name ourselves, don't look further */
585 if( !(i_mode & FIND_STRICT)
586 && p_this->psz_object_name
587 && !strcmp( p_this->psz_object_name, psz_name ) )
589 p_this->i_refcount++;
590 vlc_mutex_unlock( &structure_lock );
594 /* Otherwise, recursively look for the object */
595 if( (i_mode & 0x000f) == FIND_ANYWHERE )
597 vlc_object_t *p_root = p_this;
600 while( p_root->p_parent != NULL &&
601 p_root != VLC_OBJECT( p_this->p_libvlc ) )
603 p_root = p_root->p_parent;
606 p_found = FindObjectName( p_root, psz_name,
607 (i_mode & ~0x000f)|FIND_CHILD );
608 if( p_found == NULL && p_root != VLC_OBJECT( p_this->p_libvlc ) )
610 p_found = FindObjectName( VLC_OBJECT( p_this->p_libvlc ),
611 psz_name, (i_mode & ~0x000f)|FIND_CHILD );
616 p_found = FindObjectName( p_this, psz_name, i_mode );
619 vlc_mutex_unlock( &structure_lock );
625 ****************************************************************************
626 * increment an object refcount
627 *****************************************************************************/
628 void __vlc_object_yield( vlc_object_t *p_this )
630 vlc_mutex_lock( &structure_lock );
631 p_this->i_refcount++;
632 vlc_mutex_unlock( &structure_lock );
636 ****************************************************************************
637 * decrement an object refcount
638 *****************************************************************************/
639 void __vlc_object_release( vlc_object_t *p_this )
641 vlc_mutex_lock( &structure_lock );
642 p_this->i_refcount--;
643 vlc_mutex_unlock( &structure_lock );
647 ****************************************************************************
648 * attach object to a parent object
649 *****************************************************************************
650 * This function sets p_this as a child of p_parent, and p_parent as a parent
651 * of p_this. This link can be undone using vlc_object_detach.
652 *****************************************************************************/
653 void __vlc_object_attach( vlc_object_t *p_this, vlc_object_t *p_parent )
655 if( !p_this ) return;
657 vlc_mutex_lock( &structure_lock );
659 /* Attach the parent to its child */
660 p_this->p_parent = p_parent;
662 /* Attach the child to its parent */
663 INSERT_ELEM( p_parent->pp_children, p_parent->i_children,
664 p_parent->i_children, p_this );
666 /* Climb up the tree to see whether we are connected with the root */
667 if( p_parent->b_attached )
669 SetAttachment( p_this, VLC_TRUE );
672 vlc_mutex_unlock( &structure_lock );
676 ****************************************************************************
677 * detach object from its parent
678 *****************************************************************************
679 * This function removes all links between an object and its parent.
680 *****************************************************************************/
681 void __vlc_object_detach( vlc_object_t *p_this )
683 if( !p_this ) return;
685 vlc_mutex_lock( &structure_lock );
686 if( !p_this->p_parent )
688 msg_Err( p_this, "object is not attached" );
689 vlc_mutex_unlock( &structure_lock );
693 /* Climb up the tree to see whether we are connected with the root */
694 if( p_this->p_parent->b_attached )
696 SetAttachment( p_this, VLC_FALSE );
699 DetachObject( p_this );
700 vlc_mutex_unlock( &structure_lock );
705 ****************************************************************************
706 * find a list typed objects and increment their refcount
707 *****************************************************************************
708 * This function recursively looks for a given object type. i_mode can be one
709 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
710 *****************************************************************************/
711 vlc_list_t * __vlc_list_find( vlc_object_t *p_this, int i_type, int i_mode )
714 vlc_object_t **pp_current, **pp_end;
715 int i_count = 0, i_index = 0;
717 vlc_mutex_lock( &structure_lock );
719 /* Look for the objects */
720 switch( i_mode & 0x000f )
723 pp_current = p_this->p_libvlc_global->pp_objects;
724 pp_end = pp_current + p_this->p_libvlc_global->i_objects;
726 for( ; pp_current < pp_end ; pp_current++ )
728 if( (*pp_current)->b_attached
729 && (*pp_current)->i_object_type == i_type )
735 p_list = NewList( i_count );
736 pp_current = p_this->p_libvlc_global->pp_objects;
738 for( ; pp_current < pp_end ; pp_current++ )
740 if( (*pp_current)->b_attached
741 && (*pp_current)->i_object_type == i_type )
743 ListReplace( p_list, *pp_current, i_index );
744 if( i_index < i_count ) i_index++;
750 i_count = CountChildren( p_this, i_type );
751 p_list = NewList( i_count );
753 /* Check allocation was successful */
754 if( p_list->i_count != i_count )
756 msg_Err( p_this, "list allocation failed!" );
762 ListChildren( p_list, p_this, i_type );
766 msg_Err( p_this, "unimplemented!" );
767 p_list = NewList( 0 );
771 vlc_mutex_unlock( &structure_lock );
776 /*****************************************************************************
777 * DumpCommand: print the current vlc structure
778 *****************************************************************************
779 * This function prints either an ASCII tree showing the connections between
780 * vlc objects, and additional information such as their refcount, thread ID,
781 * etc. (command "tree"), or the same data as a simple list (command "list").
782 *****************************************************************************/
783 static int DumpCommand( vlc_object_t *p_this, char const *psz_cmd,
784 vlc_value_t oldval, vlc_value_t newval, void *p_data )
786 (void)oldval; (void)p_data;
787 if( *psz_cmd == 'l' )
789 vlc_mutex_lock( &structure_lock );
791 vlc_object_t **pp_current, **pp_end;
793 pp_current = p_this->p_libvlc_global->pp_objects;
794 pp_end = pp_current + p_this->p_libvlc_global->i_objects;
796 for( ; pp_current < pp_end ; pp_current++ )
798 if( (*pp_current)->b_attached )
800 PrintObject( *pp_current, "" );
804 printf( " o %.8i %s (not attached)\n",
805 (*pp_current)->i_object_id,
806 (*pp_current)->psz_object_type );
810 vlc_mutex_unlock( &structure_lock );
814 vlc_object_t *p_object = NULL;
816 if( *newval.psz_string )
819 int i_id = strtol( newval.psz_string, &end, 0 );
820 if( end != newval.psz_string )
821 p_object = vlc_object_get( p_this, i_id );
824 /* try using the object's name to find it */
825 vlc_object_t *p_libvlc = vlc_object_get( p_this, 1 );
828 /* Look in p_libvlc's children tree */
829 p_object = vlc_object_find_name( p_libvlc,
832 vlc_object_release( p_libvlc );
836 /* If it's not in libvlc, look in libvlc_global (== p_this) */
837 p_object = vlc_object_find_name( p_this,
849 vlc_mutex_lock( &structure_lock );
851 if( *psz_cmd == 't' )
853 char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
856 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
859 DumpStructure( p_object, 0, psz_foo );
861 else if( *psz_cmd == 'v' )
866 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
868 PrintObject( p_object, "" );
870 if( !p_object->i_vars )
871 printf( " `-o No variables\n" );
872 for( i = 0; i < p_object->i_vars; i++ )
874 variable_t *p_var = p_object->p_vars + i;
876 const char *psz_type = "unknown";
877 switch( p_var->i_type & VLC_VAR_TYPE )
879 #define MYCASE( type, nice ) \
880 case VLC_VAR_ ## type: \
883 MYCASE( VOID, "void" );
884 MYCASE( BOOL, "bool" );
885 MYCASE( INTEGER, "integer" );
886 MYCASE( HOTKEY, "hotkey" );
887 MYCASE( STRING, "string" );
888 MYCASE( MODULE, "module" );
889 MYCASE( FILE, "file" );
890 MYCASE( DIRECTORY, "directory" );
891 MYCASE( VARIABLE, "variable" );
892 MYCASE( FLOAT, "float" );
893 MYCASE( TIME, "time" );
894 MYCASE( ADDRESS, "address" );
895 MYCASE( MUTEX, "mutex" );
896 MYCASE( LIST, "list" );
899 printf( " %c-o \"%s\" (%s",
900 i + 1 == p_object->i_vars ? '`' : '|',
901 p_var->psz_name, psz_type );
902 if( p_var->psz_text )
903 printf( ", %s", p_var->psz_text );
905 if( p_var->i_type & VLC_VAR_ISCOMMAND )
906 printf( ", command" );
907 if( p_var->i_entries )
908 printf( ", %d callbacks", p_var->i_entries );
909 switch( p_var->i_type & 0x00f0 )
915 printf( ": %s", p_var->val.b_bool ? "true" : "false" );
917 case VLC_VAR_INTEGER:
918 printf( ": %d", p_var->val.i_int );
921 printf( ": \"%s\"", p_var->val.psz_string );
924 printf( ": %f", p_var->val.f_float );
927 printf( ": " I64Fi, p_var->val.i_time );
929 case VLC_VAR_ADDRESS:
930 printf( ": %p", p_var->val.p_address );
940 vlc_mutex_unlock( &structure_lock );
942 if( *newval.psz_string )
944 vlc_object_release( p_object );
951 /*****************************************************************************
952 * vlc_list_release: free a list previously allocated by vlc_list_find
953 *****************************************************************************
954 * This function decreases the refcount of all objects in the list and
956 *****************************************************************************/
957 void vlc_list_release( vlc_list_t *p_list )
961 vlc_mutex_lock( &structure_lock );
962 for( i_index = 0; i_index < p_list->i_count; i_index++ )
964 p_list->p_values[i_index].p_object->i_refcount--;
966 vlc_mutex_unlock( &structure_lock );
968 free( p_list->p_values );
972 /* Following functions are local */
974 /*****************************************************************************
975 * FindIndex: find the index of an object in an array of objects
976 *****************************************************************************
977 * This function assumes that p_this can be found in pp_objects. It will not
978 * crash if p_this cannot be found, but will return a wrong value. It is your
979 * duty to check the return value if you are not certain that the object could
981 *****************************************************************************/
982 static int FindIndex( vlc_object_t *p_this,
983 vlc_object_t **pp_objects, int i_count )
985 int i_middle = i_count / 2;
992 if( pp_objects[i_middle] == p_this )
1002 /* We take advantage of the sorted array */
1003 if( pp_objects[i_middle]->i_object_id < p_this->i_object_id )
1005 return i_middle + FindIndex( p_this, pp_objects + i_middle,
1006 i_count - i_middle );
1010 return FindIndex( p_this, pp_objects, i_middle );
1014 static vlc_object_t * FindObject( vlc_object_t *p_this, int i_type, int i_mode )
1017 vlc_object_t *p_tmp;
1019 switch( i_mode & 0x000f )
1022 p_tmp = p_this->p_parent;
1025 if( p_tmp->i_object_type == i_type )
1027 p_tmp->i_refcount++;
1032 return FindObject( p_tmp, i_type, i_mode );
1038 for( i = p_this->i_children; i--; )
1040 p_tmp = p_this->pp_children[i];
1041 if( p_tmp->i_object_type == i_type )
1043 p_tmp->i_refcount++;
1046 else if( p_tmp->i_children )
1048 p_tmp = FindObject( p_tmp, i_type, i_mode );
1058 /* Handled in vlc_object_find */
1065 static vlc_object_t * FindObjectName( vlc_object_t *p_this,
1066 const char *psz_name,
1070 vlc_object_t *p_tmp;
1072 switch( i_mode & 0x000f )
1075 p_tmp = p_this->p_parent;
1078 if( p_tmp->psz_object_name
1079 && !strcmp( p_tmp->psz_object_name, psz_name ) )
1081 p_tmp->i_refcount++;
1086 return FindObjectName( p_tmp, psz_name, i_mode );
1092 for( i = p_this->i_children; i--; )
1094 p_tmp = p_this->pp_children[i];
1095 if( p_tmp->psz_object_name
1096 && !strcmp( p_tmp->psz_object_name, psz_name ) )
1098 p_tmp->i_refcount++;
1101 else if( p_tmp->i_children )
1103 p_tmp = FindObjectName( p_tmp, psz_name, i_mode );
1113 /* Handled in vlc_object_find */
1120 static void DetachObject( vlc_object_t *p_this )
1122 vlc_object_t *p_parent = p_this->p_parent;
1125 /* Remove p_this's parent */
1126 p_this->p_parent = NULL;
1128 /* Remove all of p_parent's children which are p_this */
1129 for( i_index = p_parent->i_children ; i_index-- ; )
1131 if( p_parent->pp_children[i_index] == p_this )
1133 p_parent->i_children--;
1134 for( i = i_index ; i < p_parent->i_children ; i++ )
1136 p_parent->pp_children[i] = p_parent->pp_children[i+1];
1141 if( p_parent->i_children )
1143 p_parent->pp_children = (vlc_object_t **)realloc( p_parent->pp_children,
1144 p_parent->i_children * sizeof(vlc_object_t *) );
1148 free( p_parent->pp_children );
1149 p_parent->pp_children = NULL;
1153 /*****************************************************************************
1154 * SetAttachment: recursively set the b_attached flag of a subtree.
1155 *****************************************************************************
1156 * This function is used by the attach and detach functions to propagate
1157 * the b_attached flag in a subtree.
1158 *****************************************************************************/
1159 static void SetAttachment( vlc_object_t *p_this, vlc_bool_t b_attached )
1163 for( i_index = p_this->i_children ; i_index-- ; )
1165 SetAttachment( p_this->pp_children[i_index], b_attached );
1168 p_this->b_attached = b_attached;
1171 static void PrintObject( vlc_object_t *p_this, const char *psz_prefix )
1173 char psz_children[20], psz_refcount[20], psz_thread[30], psz_name[50],
1177 if( p_this->psz_object_name )
1179 snprintf( psz_name, 49, " \"%s\"", p_this->psz_object_name );
1181 psz_name[48] = '\"';
1184 psz_children[0] = '\0';
1185 switch( p_this->i_children )
1190 strcpy( psz_children, ", 1 child" );
1193 snprintf( psz_children, 19, ", %i children", p_this->i_children );
1197 psz_refcount[0] = '\0';
1198 if( p_this->i_refcount )
1199 snprintf( psz_refcount, 19, ", refcount %i", p_this->i_refcount );
1201 psz_thread[0] = '\0';
1202 if( p_this->b_thread )
1203 snprintf( psz_thread, 29, " (thread %d)", (int)p_this->thread_id );
1205 psz_parent[0] = '\0';
1206 if( p_this->p_parent )
1207 snprintf( psz_parent, 19, ", parent %i", p_this->p_parent->i_object_id );
1209 printf( " %so %.8i %s%s%s%s%s%s\n", psz_prefix,
1210 p_this->i_object_id, p_this->psz_object_type,
1211 psz_name, psz_thread, psz_refcount, psz_children,
1215 static void DumpStructure( vlc_object_t *p_this, int i_level, char *psz_foo )
1218 char i_back = psz_foo[i_level];
1219 psz_foo[i_level] = '\0';
1221 PrintObject( p_this, psz_foo );
1223 psz_foo[i_level] = i_back;
1225 if( i_level / 2 >= MAX_DUMPSTRUCTURE_DEPTH )
1227 msg_Warn( p_this, "structure tree is too deep" );
1231 for( i = 0 ; i < p_this->i_children ; i++ )
1235 psz_foo[i_level-1] = ' ';
1237 if( psz_foo[i_level-2] == '`' )
1239 psz_foo[i_level-2] = ' ';
1243 if( i == p_this->i_children - 1 )
1245 psz_foo[i_level] = '`';
1249 psz_foo[i_level] = '|';
1252 psz_foo[i_level+1] = '-';
1253 psz_foo[i_level+2] = '\0';
1255 DumpStructure( p_this->pp_children[i], i_level + 2, psz_foo );
1259 static vlc_list_t * NewList( int i_count )
1261 vlc_list_t * p_list = (vlc_list_t *)malloc( sizeof( vlc_list_t ) );
1262 if( p_list == NULL )
1267 p_list->i_count = i_count;
1271 p_list->p_values = NULL;
1275 p_list->p_values = malloc( i_count * sizeof( vlc_value_t ) );
1276 if( p_list->p_values == NULL )
1278 p_list->i_count = 0;
1285 static void ListReplace( vlc_list_t *p_list, vlc_object_t *p_object,
1288 if( p_list == NULL || i_index >= p_list->i_count )
1293 p_object->i_refcount++;
1295 p_list->p_values[i_index].p_object = p_object;
1300 /*static void ListAppend( vlc_list_t *p_list, vlc_object_t *p_object )
1302 if( p_list == NULL )
1307 p_list->p_values = realloc( p_list->p_values, (p_list->i_count + 1)
1308 * sizeof( vlc_value_t ) );
1309 if( p_list->p_values == NULL )
1311 p_list->i_count = 0;
1315 p_object->i_refcount++;
1317 p_list->p_values[p_list->i_count].p_object = p_object;
1323 static int CountChildren( vlc_object_t *p_this, int i_type )
1325 vlc_object_t *p_tmp;
1328 for( i = 0; i < p_this->i_children; i++ )
1330 p_tmp = p_this->pp_children[i];
1332 if( p_tmp->i_object_type == i_type )
1337 if( p_tmp->i_children )
1339 i_count += CountChildren( p_tmp, i_type );
1346 static void ListChildren( vlc_list_t *p_list, vlc_object_t *p_this, int i_type )
1348 vlc_object_t *p_tmp;
1351 for( i = 0; i < p_this->i_children; i++ )
1353 p_tmp = p_this->pp_children[i];
1355 if( p_tmp->i_object_type == i_type )
1357 ListReplace( p_list, p_tmp, p_list->i_count++ );
1360 if( p_tmp->i_children )
1362 ListChildren( p_list, p_tmp, i_type );