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 libvlc_global_data_t *p_libvlc_global = (libvlc_global_data_t *)p_new;
143 p_new->p_libvlc_global = 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_new->b_attached = VLC_TRUE;
156 libvlc_global_data_t *p_libvlc_global = vlc_global( p_this );
157 p_new->p_libvlc_global = VLC_OBJECT (p_libvlc_global);
158 p_new->p_libvlc = ( i_type == VLC_OBJECT_LIBVLC ) ? (libvlc_int_t*)p_new
161 vlc_mutex_lock( &structure_lock );
163 p_libvlc_global->i_counter++;
164 p_new->i_object_id = p_libvlc_global->i_counter;
166 /* Wooohaa! If *this* fails, we're in serious trouble! Anyway it's
167 * useless to try and recover anything if pp_objects gets smashed. */
168 TAB_APPEND( p_libvlc_global->i_objects, p_libvlc_global->pp_objects,
171 vlc_mutex_unlock( &structure_lock );
174 p_new->i_refcount = 0;
175 p_new->p_parent = NULL;
176 p_new->pp_children = NULL;
177 p_new->i_children = 0;
179 p_new->p_private = NULL;
181 /* Initialize mutexes and condvars */
182 vlc_mutex_init( p_new, &p_new->object_lock );
183 vlc_cond_init( p_new, &p_new->object_wait );
184 vlc_mutex_init( p_new, &p_new->var_lock );
186 if( i_type == VLC_OBJECT_GLOBAL )
188 vlc_mutex_init( p_new, &structure_lock );
190 var_Create( p_new, "list", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
191 var_AddCallback( p_new, "list", DumpCommand, NULL );
192 var_Create( p_new, "tree", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
193 var_AddCallback( p_new, "tree", DumpCommand, NULL );
194 var_Create( p_new, "vars", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
195 var_AddCallback( p_new, "vars", DumpCommand, NULL );
203 * Allocates and initializes a vlc object.
205 * @param i_type known object type (all of them are negative integer values),
206 * or object byte size (always positive).
208 * @return the new object, or NULL on error.
210 void * __vlc_object_create( vlc_object_t *p_this, int i_type )
212 const char * psz_type;
217 case VLC_OBJECT_GLOBAL:
218 i_size = sizeof(libvlc_global_data_t);
221 case VLC_OBJECT_LIBVLC:
222 i_size = sizeof(libvlc_int_t);
225 case VLC_OBJECT_INTF:
226 i_size = sizeof(intf_thread_t);
227 psz_type = "interface";
229 case VLC_OBJECT_DIALOGS:
230 i_size = sizeof(intf_thread_t);
231 psz_type = "dialogs";
233 case VLC_OBJECT_PLAYLIST:
234 i_size = sizeof(playlist_t);
235 psz_type = "playlist";
238 i_size = sizeof(services_discovery_t);
239 psz_type = "services discovery";
241 case VLC_OBJECT_INPUT:
242 i_size = sizeof(input_thread_t);
245 case VLC_OBJECT_DEMUX:
246 i_size = sizeof(demux_t);
249 case VLC_OBJECT_ACCESS:
250 i_size = sizeof(access_t);
253 case VLC_OBJECT_DECODER:
254 i_size = sizeof(decoder_t);
255 psz_type = "decoder";
257 case VLC_OBJECT_PACKETIZER:
258 i_size = sizeof(decoder_t);
259 psz_type = "packetizer";
261 case VLC_OBJECT_ENCODER:
262 i_size = sizeof(encoder_t);
263 psz_type = "encoder";
265 case VLC_OBJECT_FILTER:
266 i_size = sizeof(filter_t);
269 case VLC_OBJECT_VOUT:
270 i_size = sizeof(vout_thread_t);
271 psz_type = "video output";
274 i_size = sizeof(spu_t);
275 psz_type = "subpicture";
277 case VLC_OBJECT_AOUT:
278 i_size = sizeof(aout_instance_t);
279 psz_type = "audio output";
281 case VLC_OBJECT_SOUT:
282 i_size = sizeof(sout_instance_t);
283 psz_type = "stream output";
286 i_size = sizeof( vlm_t );
287 psz_type = "vlm dameon";
290 i_size = sizeof( vod_t );
291 psz_type = "vod server";
294 i_size = sizeof( tls_t );
298 i_size = sizeof( xml_t );
301 case VLC_OBJECT_OPENGL:
302 i_size = sizeof( vout_thread_t );
305 case VLC_OBJECT_ANNOUNCE:
306 i_size = sizeof( announce_handler_t );
307 psz_type = "announce";
309 case VLC_OBJECT_META_ENGINE:
310 i_size = sizeof( meta_engine_t );
311 psz_type = "meta engine";
313 case VLC_OBJECT_OSDMENU:
314 i_size = sizeof( osd_menu_t );
315 psz_type = "osd menu";
318 i_size = i_type > (int)sizeof(vlc_object_t)
319 ? i_type : (int)sizeof(vlc_object_t);
320 i_type = VLC_OBJECT_GENERIC;
321 psz_type = "generic";
325 return vlc_custom_create( p_this, i_size, i_type, psz_type );
330 ****************************************************************************
331 * Destroy a vlc object
333 * This function destroys an object that has been previously allocated with
334 * vlc_object_create. The object's refcount must be zero and it must not be
335 * attached to other objects in any way.
336 *****************************************************************************/
337 void __vlc_object_destroy( vlc_object_t *p_this )
341 if( p_this->i_children )
343 msg_Err( p_this, "cannot delete object (%i, %s) with children" ,
344 p_this->i_object_id, p_this->psz_object_name );
348 if( p_this->p_parent )
350 msg_Err( p_this, "cannot delete object (%i, %s) with a parent",
351 p_this->i_object_id, p_this->psz_object_name );
355 while( p_this->i_refcount )
359 /* Don't warn immediately ... 100ms seems OK */
363 "refcount is %i, delaying before deletion (id=%d,type=%d)",
364 p_this->i_refcount, p_this->i_object_id,
365 p_this->i_object_type );
367 else if( i_delay == 10 )
370 "refcount is %i, delaying again (id=%d,type=%d)",
371 p_this->i_refcount, p_this->i_object_id,
372 p_this->i_object_type );
374 else if( i_delay == 20 )
377 "waited too long, cancelling destruction (id=%d,type=%d)",
378 p_this->i_object_id, p_this->i_object_type );
385 /* Destroy the associated variables, starting from the end so that
386 * no memmove calls have to be done. */
387 while( p_this->i_vars )
389 var_Destroy( p_this, p_this->p_vars[p_this->i_vars - 1].psz_name );
392 free( p_this->p_vars );
393 vlc_mutex_destroy( &p_this->var_lock );
395 if( p_this->psz_header ) free( p_this->psz_header );
397 if( p_this->i_object_type == VLC_OBJECT_GLOBAL )
399 libvlc_global_data_t *p_global = (libvlc_global_data_t *)p_this;
400 /* We are the global object ... no need to lock. */
401 free( p_global->pp_objects );
402 p_global->pp_objects = NULL;
403 p_global->i_objects--;
405 vlc_mutex_destroy( &structure_lock );
409 libvlc_global_data_t *p_libvlc_global = vlc_global( p_this );
412 vlc_mutex_lock( &structure_lock );
414 /* Wooohaa! If *this* fails, we're in serious trouble! Anyway it's
415 * useless to try and recover anything if pp_objects gets smashed. */
416 i_index = FindIndex( p_this, p_libvlc_global->pp_objects,
417 p_libvlc_global->i_objects );
418 REMOVE_ELEM( p_libvlc_global->pp_objects,
419 p_libvlc_global->i_objects, i_index );
421 vlc_mutex_unlock( &structure_lock );
424 vlc_mutex_destroy( &p_this->object_lock );
425 vlc_cond_destroy( &p_this->object_wait );
427 /* global is not dynamically allocated by vlc_object_create */
428 if( p_this->i_object_type != VLC_OBJECT_GLOBAL )
436 void __vlc_object_kill( vlc_object_t *p_this )
438 vlc_mutex_lock( &p_this->object_lock );
439 p_this->b_die = VLC_TRUE;
440 vlc_mutex_unlock( &p_this->object_lock );
444 vlc_bool_t __vlc_object_dying_unlocked( vlc_object_t *p_this )
446 vlc_assert_locked( &p_this->object_lock );
447 return p_this->b_die;
451 vlc_bool_t __vlc_object_dying( vlc_object_t *p_this )
454 vlc_mutex_lock( &p_this->object_lock );
455 b = __vlc_object_dying_unlocked( p_this );
456 vlc_mutex_unlock( &p_this->object_lock );
462 * find an object given its ID
464 * This function looks for the object whose i_object_id field is i_id. We
465 * use a dichotomy so that lookups are in log2(n).
466 *****************************************************************************/
467 void * __vlc_object_get( vlc_object_t *p_this, int i_id )
470 vlc_object_t **pp_objects;
471 libvlc_global_data_t *p_libvlc_global = vlc_global( p_this );
473 vlc_mutex_lock( &structure_lock );
475 pp_objects = p_libvlc_global->pp_objects;
477 /* Perform our dichotomy */
478 for( i_max = p_libvlc_global->i_objects - 1 ; ; )
480 i_middle = i_max / 2;
482 if( pp_objects[i_middle]->i_object_id > i_id )
486 else if( pp_objects[i_middle]->i_object_id < i_id )
490 pp_objects += i_middle;
495 /* This happens when there are only two remaining objects */
496 if( pp_objects[i_middle+1]->i_object_id == i_id )
498 vlc_mutex_unlock( &structure_lock );
499 pp_objects[i_middle+1]->i_refcount++;
500 return pp_objects[i_middle+1];
507 vlc_mutex_unlock( &structure_lock );
508 pp_objects[i_middle]->i_refcount++;
509 return pp_objects[i_middle];
514 /* this means that i_max == i_middle, and since we have already
515 * tested pp_objects[i_middle]), p_found is properly set. */
520 vlc_mutex_unlock( &structure_lock );
525 ****************************************************************************
526 * find a typed object and increment its refcount
527 *****************************************************************************
528 * This function recursively looks for a given object type. i_mode can be one
529 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
530 *****************************************************************************/
531 void * __vlc_object_find( vlc_object_t *p_this, int i_type, int i_mode )
533 vlc_object_t *p_found;
535 vlc_mutex_lock( &structure_lock );
537 /* If we are of the requested type ourselves, don't look further */
538 if( !(i_mode & FIND_STRICT) && p_this->i_object_type == i_type )
540 p_this->i_refcount++;
541 vlc_mutex_unlock( &structure_lock );
545 /* Otherwise, recursively look for the object */
546 if( (i_mode & 0x000f) == FIND_ANYWHERE )
548 vlc_object_t *p_root = p_this;
551 while( p_root->p_parent != NULL &&
552 p_root != VLC_OBJECT( p_this->p_libvlc ) )
554 p_root = p_root->p_parent;
557 p_found = FindObject( p_root, i_type, (i_mode & ~0x000f)|FIND_CHILD );
558 if( p_found == NULL && p_root != VLC_OBJECT( p_this->p_libvlc ) )
560 p_found = FindObject( VLC_OBJECT( p_this->p_libvlc ),
561 i_type, (i_mode & ~0x000f)|FIND_CHILD );
566 p_found = FindObject( p_this, i_type, i_mode );
569 vlc_mutex_unlock( &structure_lock );
575 ****************************************************************************
576 * find a named object and increment its refcount
577 *****************************************************************************
578 * This function recursively looks for a given object name. i_mode can be one
579 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
580 *****************************************************************************/
581 void * __vlc_object_find_name( vlc_object_t *p_this, const char *psz_name,
584 vlc_object_t *p_found;
586 vlc_mutex_lock( &structure_lock );
588 /* If have the requested name ourselves, don't look further */
589 if( !(i_mode & FIND_STRICT)
590 && p_this->psz_object_name
591 && !strcmp( p_this->psz_object_name, psz_name ) )
593 p_this->i_refcount++;
594 vlc_mutex_unlock( &structure_lock );
598 /* Otherwise, recursively look for the object */
599 if( (i_mode & 0x000f) == FIND_ANYWHERE )
601 vlc_object_t *p_root = p_this;
604 while( p_root->p_parent != NULL &&
605 p_root != VLC_OBJECT( p_this->p_libvlc ) )
607 p_root = p_root->p_parent;
610 p_found = FindObjectName( p_root, psz_name,
611 (i_mode & ~0x000f)|FIND_CHILD );
612 if( p_found == NULL && p_root != VLC_OBJECT( p_this->p_libvlc ) )
614 p_found = FindObjectName( VLC_OBJECT( p_this->p_libvlc ),
615 psz_name, (i_mode & ~0x000f)|FIND_CHILD );
620 p_found = FindObjectName( p_this, psz_name, i_mode );
623 vlc_mutex_unlock( &structure_lock );
629 ****************************************************************************
630 * increment an object refcount
631 *****************************************************************************/
632 void __vlc_object_yield( vlc_object_t *p_this )
634 vlc_mutex_lock( &structure_lock );
635 p_this->i_refcount++;
636 vlc_mutex_unlock( &structure_lock );
640 ****************************************************************************
641 * decrement an object refcount
642 *****************************************************************************/
643 void __vlc_object_release( vlc_object_t *p_this )
645 vlc_mutex_lock( &structure_lock );
646 p_this->i_refcount--;
647 vlc_mutex_unlock( &structure_lock );
651 ****************************************************************************
652 * attach object to a parent object
653 *****************************************************************************
654 * This function sets p_this as a child of p_parent, and p_parent as a parent
655 * of p_this. This link can be undone using vlc_object_detach.
656 *****************************************************************************/
657 void __vlc_object_attach( vlc_object_t *p_this, vlc_object_t *p_parent )
659 if( !p_this ) return;
661 vlc_mutex_lock( &structure_lock );
663 /* Attach the parent to its child */
664 p_this->p_parent = p_parent;
666 /* Attach the child to its parent */
667 INSERT_ELEM( p_parent->pp_children, p_parent->i_children,
668 p_parent->i_children, p_this );
670 /* Climb up the tree to see whether we are connected with the root */
671 if( p_parent->b_attached )
673 SetAttachment( p_this, VLC_TRUE );
676 vlc_mutex_unlock( &structure_lock );
680 ****************************************************************************
681 * detach object from its parent
682 *****************************************************************************
683 * This function removes all links between an object and its parent.
684 *****************************************************************************/
685 void __vlc_object_detach( vlc_object_t *p_this )
687 if( !p_this ) return;
689 vlc_mutex_lock( &structure_lock );
690 if( !p_this->p_parent )
692 msg_Err( p_this, "object is not attached" );
693 vlc_mutex_unlock( &structure_lock );
697 /* Climb up the tree to see whether we are connected with the root */
698 if( p_this->p_parent->b_attached )
700 SetAttachment( p_this, VLC_FALSE );
703 DetachObject( p_this );
704 vlc_mutex_unlock( &structure_lock );
709 ****************************************************************************
710 * find a list typed objects and increment their refcount
711 *****************************************************************************
712 * This function recursively looks for a given object type. i_mode can be one
713 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
714 *****************************************************************************/
715 vlc_list_t * __vlc_list_find( vlc_object_t *p_this, int i_type, int i_mode )
718 vlc_object_t **pp_current, **pp_end;
719 int i_count = 0, i_index = 0;
720 libvlc_global_data_t *p_libvlc_global = vlc_global( p_this );
722 vlc_mutex_lock( &structure_lock );
724 /* Look for the objects */
725 switch( i_mode & 0x000f )
728 pp_current = p_libvlc_global->pp_objects;
729 pp_end = pp_current + p_libvlc_global->i_objects;
731 for( ; pp_current < pp_end ; pp_current++ )
733 if( (*pp_current)->b_attached
734 && (*pp_current)->i_object_type == i_type )
740 p_list = NewList( i_count );
741 pp_current = p_libvlc_global->pp_objects;
743 for( ; pp_current < pp_end ; pp_current++ )
745 if( (*pp_current)->b_attached
746 && (*pp_current)->i_object_type == i_type )
748 ListReplace( p_list, *pp_current, i_index );
749 if( i_index < i_count ) i_index++;
755 i_count = CountChildren( p_this, i_type );
756 p_list = NewList( i_count );
758 /* Check allocation was successful */
759 if( p_list->i_count != i_count )
761 msg_Err( p_this, "list allocation failed!" );
767 ListChildren( p_list, p_this, i_type );
771 msg_Err( p_this, "unimplemented!" );
772 p_list = NewList( 0 );
776 vlc_mutex_unlock( &structure_lock );
781 /*****************************************************************************
782 * DumpCommand: print the current vlc structure
783 *****************************************************************************
784 * This function prints either an ASCII tree showing the connections between
785 * vlc objects, and additional information such as their refcount, thread ID,
786 * etc. (command "tree"), or the same data as a simple list (command "list").
787 *****************************************************************************/
788 static int DumpCommand( vlc_object_t *p_this, char const *psz_cmd,
789 vlc_value_t oldval, vlc_value_t newval, void *p_data )
791 libvlc_global_data_t *p_libvlc_global = vlc_global( p_this );
793 (void)oldval; (void)p_data;
794 if( *psz_cmd == 'l' )
796 vlc_mutex_lock( &structure_lock );
798 vlc_object_t **pp_current, **pp_end;
800 pp_current = p_libvlc_global->pp_objects;
801 pp_end = pp_current + p_libvlc_global->i_objects;
803 for( ; pp_current < pp_end ; pp_current++ )
805 if( (*pp_current)->b_attached )
807 PrintObject( *pp_current, "" );
811 printf( " o %.8i %s (not attached)\n",
812 (*pp_current)->i_object_id,
813 (*pp_current)->psz_object_type );
817 vlc_mutex_unlock( &structure_lock );
821 vlc_object_t *p_object = NULL;
823 if( *newval.psz_string )
826 int i_id = strtol( newval.psz_string, &end, 0 );
827 if( end != newval.psz_string )
828 p_object = vlc_object_get( p_this, i_id );
831 /* try using the object's name to find it */
832 vlc_object_t *p_libvlc = vlc_object_get( p_this, 1 );
835 /* Look in p_libvlc's children tree */
836 p_object = vlc_object_find_name( p_libvlc,
839 vlc_object_release( p_libvlc );
843 /* If it's not in libvlc, look in libvlc_global (== p_this) */
844 p_object = vlc_object_find_name( p_this,
856 vlc_mutex_lock( &structure_lock );
858 if( *psz_cmd == 't' )
860 char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
863 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
866 DumpStructure( p_object, 0, psz_foo );
868 else if( *psz_cmd == 'v' )
873 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
875 PrintObject( p_object, "" );
877 if( !p_object->i_vars )
878 printf( " `-o No variables\n" );
879 for( i = 0; i < p_object->i_vars; i++ )
881 variable_t *p_var = p_object->p_vars + i;
883 const char *psz_type = "unknown";
884 switch( p_var->i_type & VLC_VAR_TYPE )
886 #define MYCASE( type, nice ) \
887 case VLC_VAR_ ## type: \
890 MYCASE( VOID, "void" );
891 MYCASE( BOOL, "bool" );
892 MYCASE( INTEGER, "integer" );
893 MYCASE( HOTKEY, "hotkey" );
894 MYCASE( STRING, "string" );
895 MYCASE( MODULE, "module" );
896 MYCASE( FILE, "file" );
897 MYCASE( DIRECTORY, "directory" );
898 MYCASE( VARIABLE, "variable" );
899 MYCASE( FLOAT, "float" );
900 MYCASE( TIME, "time" );
901 MYCASE( ADDRESS, "address" );
902 MYCASE( MUTEX, "mutex" );
903 MYCASE( LIST, "list" );
906 printf( " %c-o \"%s\" (%s",
907 i + 1 == p_object->i_vars ? '`' : '|',
908 p_var->psz_name, psz_type );
909 if( p_var->psz_text )
910 printf( ", %s", p_var->psz_text );
912 if( p_var->i_type & VLC_VAR_ISCOMMAND )
913 printf( ", command" );
914 if( p_var->i_entries )
915 printf( ", %d callbacks", p_var->i_entries );
916 switch( p_var->i_type & 0x00f0 )
922 printf( ": %s", p_var->val.b_bool ? "true" : "false" );
924 case VLC_VAR_INTEGER:
925 printf( ": %d", p_var->val.i_int );
928 printf( ": \"%s\"", p_var->val.psz_string );
931 printf( ": %f", p_var->val.f_float );
934 printf( ": " I64Fi, p_var->val.i_time );
936 case VLC_VAR_ADDRESS:
937 printf( ": %p", p_var->val.p_address );
947 vlc_mutex_unlock( &structure_lock );
949 if( *newval.psz_string )
951 vlc_object_release( p_object );
958 /*****************************************************************************
959 * vlc_list_release: free a list previously allocated by vlc_list_find
960 *****************************************************************************
961 * This function decreases the refcount of all objects in the list and
963 *****************************************************************************/
964 void vlc_list_release( vlc_list_t *p_list )
968 vlc_mutex_lock( &structure_lock );
969 for( i_index = 0; i_index < p_list->i_count; i_index++ )
971 p_list->p_values[i_index].p_object->i_refcount--;
973 vlc_mutex_unlock( &structure_lock );
975 free( p_list->p_values );
979 /* Following functions are local */
981 /*****************************************************************************
982 * FindIndex: find the index of an object in an array of objects
983 *****************************************************************************
984 * This function assumes that p_this can be found in pp_objects. It will not
985 * crash if p_this cannot be found, but will return a wrong value. It is your
986 * duty to check the return value if you are not certain that the object could
988 *****************************************************************************/
989 static int FindIndex( vlc_object_t *p_this,
990 vlc_object_t **pp_objects, int i_count )
992 int i_middle = i_count / 2;
999 if( pp_objects[i_middle] == p_this )
1009 /* We take advantage of the sorted array */
1010 if( pp_objects[i_middle]->i_object_id < p_this->i_object_id )
1012 return i_middle + FindIndex( p_this, pp_objects + i_middle,
1013 i_count - i_middle );
1017 return FindIndex( p_this, pp_objects, i_middle );
1021 static vlc_object_t * FindObject( vlc_object_t *p_this, int i_type, int i_mode )
1024 vlc_object_t *p_tmp;
1026 switch( i_mode & 0x000f )
1029 p_tmp = p_this->p_parent;
1032 if( p_tmp->i_object_type == i_type )
1034 p_tmp->i_refcount++;
1039 return FindObject( p_tmp, i_type, i_mode );
1045 for( i = p_this->i_children; i--; )
1047 p_tmp = p_this->pp_children[i];
1048 if( p_tmp->i_object_type == i_type )
1050 p_tmp->i_refcount++;
1053 else if( p_tmp->i_children )
1055 p_tmp = FindObject( p_tmp, i_type, i_mode );
1065 /* Handled in vlc_object_find */
1072 static vlc_object_t * FindObjectName( vlc_object_t *p_this,
1073 const char *psz_name,
1077 vlc_object_t *p_tmp;
1079 switch( i_mode & 0x000f )
1082 p_tmp = p_this->p_parent;
1085 if( p_tmp->psz_object_name
1086 && !strcmp( p_tmp->psz_object_name, psz_name ) )
1088 p_tmp->i_refcount++;
1093 return FindObjectName( p_tmp, psz_name, i_mode );
1099 for( i = p_this->i_children; i--; )
1101 p_tmp = p_this->pp_children[i];
1102 if( p_tmp->psz_object_name
1103 && !strcmp( p_tmp->psz_object_name, psz_name ) )
1105 p_tmp->i_refcount++;
1108 else if( p_tmp->i_children )
1110 p_tmp = FindObjectName( p_tmp, psz_name, i_mode );
1120 /* Handled in vlc_object_find */
1127 static void DetachObject( vlc_object_t *p_this )
1129 vlc_object_t *p_parent = p_this->p_parent;
1132 /* Remove p_this's parent */
1133 p_this->p_parent = NULL;
1135 /* Remove all of p_parent's children which are p_this */
1136 for( i_index = p_parent->i_children ; i_index-- ; )
1138 if( p_parent->pp_children[i_index] == p_this )
1140 p_parent->i_children--;
1141 for( i = i_index ; i < p_parent->i_children ; i++ )
1143 p_parent->pp_children[i] = p_parent->pp_children[i+1];
1148 if( p_parent->i_children )
1150 p_parent->pp_children = (vlc_object_t **)realloc( p_parent->pp_children,
1151 p_parent->i_children * sizeof(vlc_object_t *) );
1155 free( p_parent->pp_children );
1156 p_parent->pp_children = NULL;
1160 /*****************************************************************************
1161 * SetAttachment: recursively set the b_attached flag of a subtree.
1162 *****************************************************************************
1163 * This function is used by the attach and detach functions to propagate
1164 * the b_attached flag in a subtree.
1165 *****************************************************************************/
1166 static void SetAttachment( vlc_object_t *p_this, vlc_bool_t b_attached )
1170 for( i_index = p_this->i_children ; i_index-- ; )
1172 SetAttachment( p_this->pp_children[i_index], b_attached );
1175 p_this->b_attached = b_attached;
1178 static void PrintObject( vlc_object_t *p_this, const char *psz_prefix )
1180 char psz_children[20], psz_refcount[20], psz_thread[30], psz_name[50],
1184 if( p_this->psz_object_name )
1186 snprintf( psz_name, 49, " \"%s\"", p_this->psz_object_name );
1188 psz_name[48] = '\"';
1191 psz_children[0] = '\0';
1192 switch( p_this->i_children )
1197 strcpy( psz_children, ", 1 child" );
1200 snprintf( psz_children, 19, ", %i children", p_this->i_children );
1204 psz_refcount[0] = '\0';
1205 if( p_this->i_refcount )
1206 snprintf( psz_refcount, 19, ", refcount %i", p_this->i_refcount );
1208 psz_thread[0] = '\0';
1209 if( p_this->b_thread )
1210 snprintf( psz_thread, 29, " (thread %d)", (int)p_this->thread_id );
1212 psz_parent[0] = '\0';
1213 if( p_this->p_parent )
1214 snprintf( psz_parent, 19, ", parent %i", p_this->p_parent->i_object_id );
1216 printf( " %so %.8i %s%s%s%s%s%s\n", psz_prefix,
1217 p_this->i_object_id, p_this->psz_object_type,
1218 psz_name, psz_thread, psz_refcount, psz_children,
1222 static void DumpStructure( vlc_object_t *p_this, int i_level, char *psz_foo )
1225 char i_back = psz_foo[i_level];
1226 psz_foo[i_level] = '\0';
1228 PrintObject( p_this, psz_foo );
1230 psz_foo[i_level] = i_back;
1232 if( i_level / 2 >= MAX_DUMPSTRUCTURE_DEPTH )
1234 msg_Warn( p_this, "structure tree is too deep" );
1238 for( i = 0 ; i < p_this->i_children ; i++ )
1242 psz_foo[i_level-1] = ' ';
1244 if( psz_foo[i_level-2] == '`' )
1246 psz_foo[i_level-2] = ' ';
1250 if( i == p_this->i_children - 1 )
1252 psz_foo[i_level] = '`';
1256 psz_foo[i_level] = '|';
1259 psz_foo[i_level+1] = '-';
1260 psz_foo[i_level+2] = '\0';
1262 DumpStructure( p_this->pp_children[i], i_level + 2, psz_foo );
1266 static vlc_list_t * NewList( int i_count )
1268 vlc_list_t * p_list = (vlc_list_t *)malloc( sizeof( vlc_list_t ) );
1269 if( p_list == NULL )
1274 p_list->i_count = i_count;
1278 p_list->p_values = NULL;
1282 p_list->p_values = malloc( i_count * sizeof( vlc_value_t ) );
1283 if( p_list->p_values == NULL )
1285 p_list->i_count = 0;
1292 static void ListReplace( vlc_list_t *p_list, vlc_object_t *p_object,
1295 if( p_list == NULL || i_index >= p_list->i_count )
1300 p_object->i_refcount++;
1302 p_list->p_values[i_index].p_object = p_object;
1307 /*static void ListAppend( vlc_list_t *p_list, vlc_object_t *p_object )
1309 if( p_list == NULL )
1314 p_list->p_values = realloc( p_list->p_values, (p_list->i_count + 1)
1315 * sizeof( vlc_value_t ) );
1316 if( p_list->p_values == NULL )
1318 p_list->i_count = 0;
1322 p_object->i_refcount++;
1324 p_list->p_values[p_list->i_count].p_object = p_object;
1330 static int CountChildren( vlc_object_t *p_this, int i_type )
1332 vlc_object_t *p_tmp;
1335 for( i = 0; i < p_this->i_children; i++ )
1337 p_tmp = p_this->pp_children[i];
1339 if( p_tmp->i_object_type == i_type )
1344 if( p_tmp->i_children )
1346 i_count += CountChildren( p_tmp, i_type );
1353 static void ListChildren( vlc_list_t *p_list, vlc_object_t *p_this, int i_type )
1355 vlc_object_t *p_tmp;
1358 for( i = 0; i < p_this->i_children; i++ )
1360 p_tmp = p_this->pp_children[i];
1362 if( p_tmp->i_object_type == i_type )
1364 ListReplace( p_list, p_tmp, p_list->i_count++ );
1367 if( p_tmp->i_children )
1369 ListChildren( p_list, p_tmp, i_type );