1 /*****************************************************************************
2 * objects.c: vlc_object_t handling
3 *****************************************************************************
4 * Copyright (C) 2004 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 void DetachObject ( vlc_object_t * );
75 static void PrintObject ( vlc_object_t *, const char * );
76 static void DumpStructure ( vlc_object_t *, int, char * );
77 static int FindIndex ( vlc_object_t *, vlc_object_t **, int );
78 static void SetAttachment ( vlc_object_t *, vlc_bool_t );
80 static vlc_list_t * NewList ( int );
81 static void ListReplace ( vlc_list_t *, vlc_object_t *, int );
82 /*static void ListAppend ( vlc_list_t *, vlc_object_t * );*/
83 static int CountChildren ( vlc_object_t *, int );
84 static void ListChildren ( vlc_list_t *, vlc_object_t *, int );
86 /*****************************************************************************
87 * Local structure lock
88 *****************************************************************************/
89 static vlc_mutex_t structure_lock;
91 vlc_object_t *vlc_custom_create( vlc_object_t *p_this, size_t i_size,
92 int i_type, const char *psz_type )
94 vlc_object_t * p_new = NULL;
96 if( i_type == VLC_OBJECT_GLOBAL )
102 p_new = malloc( i_size );
103 if( !p_new ) return NULL;
104 memset( p_new, 0, i_size );
107 p_new->i_object_type = i_type;
108 p_new->psz_object_type = psz_type;
110 p_new->psz_object_name = NULL;
112 p_new->b_die = VLC_FALSE;
113 p_new->b_error = VLC_FALSE;
114 p_new->b_dead = VLC_FALSE;
115 p_new->b_attached = VLC_FALSE;
116 p_new->b_force = VLC_FALSE;
118 p_new->psz_header = NULL;
121 if( p_this->i_flags & OBJECT_FLAGS_NODBG )
122 p_new->i_flags |= OBJECT_FLAGS_NODBG;
123 if( p_this->i_flags & OBJECT_FLAGS_QUIET )
124 p_new->i_flags |= OBJECT_FLAGS_QUIET;
125 if( p_this->i_flags & OBJECT_FLAGS_NOINTERACT )
126 p_new->i_flags |= OBJECT_FLAGS_NOINTERACT;
129 p_new->p_vars = (variable_t *)malloc( 16 * sizeof( variable_t ) );
133 if( i_type != VLC_OBJECT_GLOBAL )
138 if( i_type == VLC_OBJECT_GLOBAL )
140 /* If i_type is global, then p_new is actually p_libvlc_global */
141 p_new->p_libvlc_global = (libvlc_global_data_t*)p_new;
142 p_new->p_libvlc = NULL;
144 p_new->p_libvlc_global->i_counter = 0;
145 p_new->i_object_id = 0;
147 p_new->p_libvlc_global->i_objects = 1;
148 p_new->p_libvlc_global->pp_objects = malloc( sizeof(vlc_object_t *) );
149 p_new->p_libvlc_global->pp_objects[0] = p_new;
150 p_new->b_attached = VLC_TRUE;
154 p_new->p_libvlc_global = p_this->p_libvlc_global;
155 p_new->p_libvlc = ( i_type == VLC_OBJECT_LIBVLC ) ? (libvlc_int_t*)p_new
158 vlc_mutex_lock( &structure_lock );
160 p_new->p_libvlc_global->i_counter++;
161 p_new->i_object_id = p_new->p_libvlc_global->i_counter;
163 /* Wooohaa! If *this* fails, we're in serious trouble! Anyway it's
164 * useless to try and recover anything if pp_objects gets smashed. */
165 TAB_APPEND( p_new->p_libvlc_global->i_objects,
166 p_new->p_libvlc_global->pp_objects,
169 vlc_mutex_unlock( &structure_lock );
172 p_new->i_refcount = 0;
173 p_new->p_parent = NULL;
174 p_new->pp_children = NULL;
175 p_new->i_children = 0;
177 p_new->p_private = NULL;
179 /* Initialize mutexes and condvars */
180 vlc_mutex_init( p_new, &p_new->object_lock );
181 vlc_cond_init( p_new, &p_new->object_wait );
182 vlc_mutex_init( p_new, &p_new->var_lock );
184 if( i_type == VLC_OBJECT_GLOBAL )
186 vlc_mutex_init( p_new, &structure_lock );
188 var_Create( p_new, "list", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
189 var_AddCallback( p_new, "list", DumpCommand, NULL );
190 var_Create( p_new, "tree", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
191 var_AddCallback( p_new, "tree", DumpCommand, NULL );
192 var_Create( p_new, "vars", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
193 var_AddCallback( p_new, "vars", DumpCommand, NULL );
201 * Allocates and initializes a vlc object.
203 * @param i_type known object type (all of them are negative integer values),
204 * or object byte size (always positive).
206 * @return the new object, or NULL on error.
208 void * __vlc_object_create( vlc_object_t *p_this, int i_type )
210 const char * psz_type;
215 case VLC_OBJECT_GLOBAL:
216 i_size = sizeof(libvlc_global_data_t);
219 case VLC_OBJECT_LIBVLC:
220 i_size = sizeof(libvlc_int_t);
223 case VLC_OBJECT_MODULE:
224 i_size = sizeof(module_t);
227 case VLC_OBJECT_INTF:
228 i_size = sizeof(intf_thread_t);
229 psz_type = "interface";
231 case VLC_OBJECT_DIALOGS:
232 i_size = sizeof(intf_thread_t);
233 psz_type = "dialogs";
235 case VLC_OBJECT_PLAYLIST:
236 i_size = sizeof(playlist_t);
237 psz_type = "playlist";
240 i_size = sizeof(services_discovery_t);
241 psz_type = "services discovery";
243 case VLC_OBJECT_INPUT:
244 i_size = sizeof(input_thread_t);
247 case VLC_OBJECT_DEMUX:
248 i_size = sizeof(demux_t);
251 case VLC_OBJECT_STREAM:
252 i_size = sizeof(stream_t);
255 case VLC_OBJECT_ACCESS:
256 i_size = sizeof(access_t);
259 case VLC_OBJECT_DECODER:
260 i_size = sizeof(decoder_t);
261 psz_type = "decoder";
263 case VLC_OBJECT_PACKETIZER:
264 i_size = sizeof(decoder_t);
265 psz_type = "packetizer";
267 case VLC_OBJECT_ENCODER:
268 i_size = sizeof(encoder_t);
269 psz_type = "encoder";
271 case VLC_OBJECT_FILTER:
272 i_size = sizeof(filter_t);
275 case VLC_OBJECT_VOUT:
276 i_size = sizeof(vout_thread_t);
277 psz_type = "video output";
280 i_size = sizeof(spu_t);
281 psz_type = "subpicture";
283 case VLC_OBJECT_AOUT:
284 i_size = sizeof(aout_instance_t);
285 psz_type = "audio output";
287 case VLC_OBJECT_SOUT:
288 i_size = sizeof(sout_instance_t);
289 psz_type = "stream output";
292 i_size = sizeof( vlm_t );
293 psz_type = "vlm dameon";
296 i_size = sizeof( vod_t );
297 psz_type = "vod server";
300 i_size = sizeof( tls_t );
304 i_size = sizeof( xml_t );
307 case VLC_OBJECT_OPENGL:
308 i_size = sizeof( vout_thread_t );
311 case VLC_OBJECT_ANNOUNCE:
312 i_size = sizeof( announce_handler_t );
313 psz_type = "announce";
315 case VLC_OBJECT_META_ENGINE:
316 i_size = sizeof( meta_engine_t );
317 psz_type = "meta engine";
319 case VLC_OBJECT_OSDMENU:
320 i_size = sizeof( osd_menu_t );
321 psz_type = "osd menu";
324 i_size = i_type > (int)sizeof(vlc_object_t)
325 ? i_type : (int)sizeof(vlc_object_t);
326 i_type = VLC_OBJECT_GENERIC;
327 psz_type = "generic";
331 return vlc_custom_create( p_this, i_size, i_type, psz_type );
336 ****************************************************************************
337 * Destroy a vlc object
339 * This function destroys an object that has been previously allocated with
340 * vlc_object_create. The object's refcount must be zero and it must not be
341 * attached to other objects in any way.
342 *****************************************************************************/
343 void __vlc_object_destroy( vlc_object_t *p_this )
347 if( p_this->i_children )
349 msg_Err( p_this, "cannot delete object (%i, %s) with children" ,
350 p_this->i_object_id, p_this->psz_object_name );
354 if( p_this->p_parent )
356 msg_Err( p_this, "cannot delete object (%i, %s) with a parent",
357 p_this->i_object_id, p_this->psz_object_name );
361 while( p_this->i_refcount )
365 /* Don't warn immediately ... 100ms seems OK */
369 "refcount is %i, delaying before deletion (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 == 10 )
376 "refcount is %i, delaying again (id=%d,type=%d)",
377 p_this->i_refcount, p_this->i_object_id,
378 p_this->i_object_type );
380 else if( i_delay == 20 )
383 "waited too long, cancelling destruction (id=%d,type=%d)",
384 p_this->i_object_id, p_this->i_object_type );
391 /* Destroy the associated variables, starting from the end so that
392 * no memmove calls have to be done. */
393 while( p_this->i_vars )
395 var_Destroy( p_this, p_this->p_vars[p_this->i_vars - 1].psz_name );
398 free( p_this->p_vars );
399 vlc_mutex_destroy( &p_this->var_lock );
401 if( p_this->psz_header ) free( p_this->psz_header );
403 if( p_this->i_object_type == VLC_OBJECT_GLOBAL )
405 /* We are the global object ... no need to lock. */
406 free( p_this->p_libvlc_global->pp_objects );
407 p_this->p_libvlc_global->pp_objects = NULL;
408 p_this->p_libvlc_global->i_objects--;
410 vlc_mutex_destroy( &structure_lock );
416 vlc_mutex_lock( &structure_lock );
418 /* Wooohaa! If *this* fails, we're in serious trouble! Anyway it's
419 * useless to try and recover anything if pp_objects gets smashed. */
420 i_index = FindIndex( p_this, p_this->p_libvlc_global->pp_objects,
421 p_this->p_libvlc_global->i_objects );
422 REMOVE_ELEM( p_this->p_libvlc_global->pp_objects,
423 p_this->p_libvlc_global->i_objects, i_index );
425 vlc_mutex_unlock( &structure_lock );
428 vlc_mutex_destroy( &p_this->object_lock );
429 vlc_cond_destroy( &p_this->object_wait );
431 /* global is not dynamically allocated by vlc_object_create */
432 if( p_this->i_object_type != VLC_OBJECT_GLOBAL )
440 void __vlc_object_kill( vlc_object_t *p_this )
442 vlc_mutex_lock( &p_this->object_lock );
443 p_this->b_die = VLC_TRUE;
444 vlc_mutex_unlock( &p_this->object_lock );
449 * find an object given its ID
451 * This function looks for the object whose i_object_id field is i_id. We
452 * use a dichotomy so that lookups are in log2(n).
453 *****************************************************************************/
454 void * __vlc_object_get( vlc_object_t *p_this, int i_id )
457 vlc_object_t **pp_objects;
459 vlc_mutex_lock( &structure_lock );
461 pp_objects = p_this->p_libvlc_global->pp_objects;
463 /* Perform our dichotomy */
464 for( i_max = p_this->p_libvlc_global->i_objects - 1 ; ; )
466 i_middle = i_max / 2;
468 if( pp_objects[i_middle]->i_object_id > i_id )
472 else if( pp_objects[i_middle]->i_object_id < i_id )
476 pp_objects += i_middle;
481 /* This happens when there are only two remaining objects */
482 if( pp_objects[i_middle+1]->i_object_id == i_id )
484 vlc_mutex_unlock( &structure_lock );
485 pp_objects[i_middle+1]->i_refcount++;
486 return pp_objects[i_middle+1];
493 vlc_mutex_unlock( &structure_lock );
494 pp_objects[i_middle]->i_refcount++;
495 return pp_objects[i_middle];
500 /* this means that i_max == i_middle, and since we have already
501 * tested pp_objects[i_middle]), p_found is properly set. */
506 vlc_mutex_unlock( &structure_lock );
511 ****************************************************************************
512 * find a typed object and increment its refcount
513 *****************************************************************************
514 * This function recursively looks for a given object type. i_mode can be one
515 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
516 *****************************************************************************/
517 void * __vlc_object_find( vlc_object_t *p_this, int i_type, int i_mode )
519 vlc_object_t *p_found;
521 vlc_mutex_lock( &structure_lock );
523 /* If we are of the requested type ourselves, don't look further */
524 if( !(i_mode & FIND_STRICT) && p_this->i_object_type == i_type )
526 p_this->i_refcount++;
527 vlc_mutex_unlock( &structure_lock );
531 /* Otherwise, recursively look for the object */
532 if( (i_mode & 0x000f) == FIND_ANYWHERE )
534 vlc_object_t *p_root = p_this;
537 while( p_root->p_parent != NULL &&
538 p_root != VLC_OBJECT( p_this->p_libvlc ) )
540 p_root = p_root->p_parent;
543 p_found = FindObject( p_root, i_type, (i_mode & ~0x000f)|FIND_CHILD );
544 if( p_found == NULL && p_root != VLC_OBJECT( p_this->p_libvlc ) )
546 p_found = FindObject( VLC_OBJECT( p_this->p_libvlc ),
547 i_type, (i_mode & ~0x000f)|FIND_CHILD );
552 p_found = FindObject( p_this, i_type, i_mode );
555 vlc_mutex_unlock( &structure_lock );
561 ****************************************************************************
562 * increment an object refcount
563 *****************************************************************************/
564 void __vlc_object_yield( vlc_object_t *p_this )
566 vlc_mutex_lock( &structure_lock );
567 p_this->i_refcount++;
568 vlc_mutex_unlock( &structure_lock );
572 ****************************************************************************
573 * decrement an object refcount
574 *****************************************************************************/
575 void __vlc_object_release( vlc_object_t *p_this )
577 vlc_mutex_lock( &structure_lock );
578 p_this->i_refcount--;
579 vlc_mutex_unlock( &structure_lock );
583 ****************************************************************************
584 * attach object to a parent object
585 *****************************************************************************
586 * This function sets p_this as a child of p_parent, and p_parent as a parent
587 * of p_this. This link can be undone using vlc_object_detach.
588 *****************************************************************************/
589 void __vlc_object_attach( vlc_object_t *p_this, vlc_object_t *p_parent )
591 if( !p_this ) return;
593 vlc_mutex_lock( &structure_lock );
595 /* Attach the parent to its child */
596 p_this->p_parent = p_parent;
598 /* Attach the child to its parent */
599 INSERT_ELEM( p_parent->pp_children, p_parent->i_children,
600 p_parent->i_children, p_this );
602 /* Climb up the tree to see whether we are connected with the root */
603 if( p_parent->b_attached )
605 SetAttachment( p_this, VLC_TRUE );
608 vlc_mutex_unlock( &structure_lock );
612 ****************************************************************************
613 * detach object from its parent
614 *****************************************************************************
615 * This function removes all links between an object and its parent.
616 *****************************************************************************/
617 void __vlc_object_detach( vlc_object_t *p_this )
619 if( !p_this ) return;
621 vlc_mutex_lock( &structure_lock );
622 if( !p_this->p_parent )
624 msg_Err( p_this, "object is not attached" );
625 vlc_mutex_unlock( &structure_lock );
629 /* Climb up the tree to see whether we are connected with the root */
630 if( p_this->p_parent->b_attached )
632 SetAttachment( p_this, VLC_FALSE );
635 DetachObject( p_this );
636 vlc_mutex_unlock( &structure_lock );
641 ****************************************************************************
642 * find a list typed objects and increment their refcount
643 *****************************************************************************
644 * This function recursively looks for a given object type. i_mode can be one
645 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
646 *****************************************************************************/
647 vlc_list_t * __vlc_list_find( vlc_object_t *p_this, int i_type, int i_mode )
650 vlc_object_t **pp_current, **pp_end;
651 int i_count = 0, i_index = 0;
653 vlc_mutex_lock( &structure_lock );
655 /* Look for the objects */
656 switch( i_mode & 0x000f )
659 pp_current = p_this->p_libvlc_global->pp_objects;
660 pp_end = pp_current + p_this->p_libvlc_global->i_objects;
662 for( ; pp_current < pp_end ; pp_current++ )
664 if( (*pp_current)->b_attached
665 && (*pp_current)->i_object_type == i_type )
671 p_list = NewList( i_count );
672 pp_current = p_this->p_libvlc_global->pp_objects;
674 for( ; pp_current < pp_end ; pp_current++ )
676 if( (*pp_current)->b_attached
677 && (*pp_current)->i_object_type == i_type )
679 ListReplace( p_list, *pp_current, i_index );
680 if( i_index < i_count ) i_index++;
686 i_count = CountChildren( p_this, i_type );
687 p_list = NewList( i_count );
689 /* Check allocation was successful */
690 if( p_list->i_count != i_count )
692 msg_Err( p_this, "list allocation failed!" );
698 ListChildren( p_list, p_this, i_type );
702 msg_Err( p_this, "unimplemented!" );
703 p_list = NewList( 0 );
707 vlc_mutex_unlock( &structure_lock );
712 /*****************************************************************************
713 * DumpCommand: print the current vlc structure
714 *****************************************************************************
715 * This function prints either an ASCII tree showing the connections between
716 * vlc objects, and additional information such as their refcount, thread ID,
717 * etc. (command "tree"), or the same data as a simple list (command "list").
718 *****************************************************************************/
719 static int DumpCommand( vlc_object_t *p_this, char const *psz_cmd,
720 vlc_value_t oldval, vlc_value_t newval, void *p_data )
722 if( *psz_cmd == 'l' )
724 vlc_mutex_lock( &structure_lock );
726 vlc_object_t **pp_current, **pp_end;
728 pp_current = p_this->p_libvlc_global->pp_objects;
729 pp_end = pp_current + p_this->p_libvlc_global->i_objects;
731 for( ; pp_current < pp_end ; pp_current++ )
733 if( (*pp_current)->b_attached )
735 PrintObject( *pp_current, "" );
739 printf( " o %.8i %s (not attached)\n",
740 (*pp_current)->i_object_id,
741 (*pp_current)->psz_object_type );
745 vlc_mutex_unlock( &structure_lock );
749 vlc_object_t *p_object = NULL;
751 if( *newval.psz_string )
753 p_object = vlc_object_get( p_this, atoi(newval.psz_string) );
761 vlc_mutex_lock( &structure_lock );
763 if( *psz_cmd == 't' )
765 char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
768 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
771 DumpStructure( p_object, 0, psz_foo );
773 else if( *psz_cmd == 'v' )
778 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
780 PrintObject( p_object, "" );
782 if( !p_object->i_vars )
783 printf( " `-o No variables\n" );
784 for( i = 0; i < p_object->i_vars; i++ )
786 variable_t *p_var = p_object->p_vars + i;
788 const char *psz_type = "unknown";
789 switch( p_var->i_type & VLC_VAR_TYPE )
791 #define MYCASE( type, nice ) \
792 case VLC_VAR_ ## type: \
795 MYCASE( VOID, "void" );
796 MYCASE( BOOL, "bool" );
797 MYCASE( INTEGER, "integer" );
798 MYCASE( HOTKEY, "hotkey" );
799 MYCASE( STRING, "string" );
800 MYCASE( MODULE, "module" );
801 MYCASE( FILE, "file" );
802 MYCASE( DIRECTORY, "directory" );
803 MYCASE( VARIABLE, "variable" );
804 MYCASE( FLOAT, "float" );
805 MYCASE( TIME, "time" );
806 MYCASE( ADDRESS, "address" );
807 MYCASE( MUTEX, "mutex" );
808 MYCASE( LIST, "list" );
811 printf( " %c-o \"%s\" (%s",
812 i + 1 == p_object->i_vars ? '`' : '|',
813 p_var->psz_name, psz_type );
814 if( p_var->psz_text )
815 printf( ", %s", p_var->psz_text );
817 if( p_var->i_type & VLC_VAR_ISCOMMAND )
818 printf( ", command" );
819 if( p_var->i_entries )
820 printf( ", %d callbacks", p_var->i_entries );
821 switch( p_var->i_type & 0x00f0 )
827 printf( ": %s", p_var->val.b_bool ? "true" : "false" );
829 case VLC_VAR_INTEGER:
830 printf( ": %d", p_var->val.i_int );
833 printf( ": \"%s\"", p_var->val.psz_string );
836 printf( ": %f", p_var->val.f_float );
839 printf( ": " I64Fi, p_var->val.i_time );
841 case VLC_VAR_ADDRESS:
842 printf( ": %p", p_var->val.p_address );
852 vlc_mutex_unlock( &structure_lock );
854 if( *newval.psz_string )
856 vlc_object_release( p_object );
863 /*****************************************************************************
864 * vlc_list_release: free a list previously allocated by vlc_list_find
865 *****************************************************************************
866 * This function decreases the refcount of all objects in the list and
868 *****************************************************************************/
869 void vlc_list_release( vlc_list_t *p_list )
873 vlc_mutex_lock( &structure_lock );
874 for( i_index = 0; i_index < p_list->i_count; i_index++ )
876 p_list->p_values[i_index].p_object->i_refcount--;
878 vlc_mutex_unlock( &structure_lock );
880 free( p_list->p_values );
884 /* Following functions are local */
886 /*****************************************************************************
887 * FindIndex: find the index of an object in an array of objects
888 *****************************************************************************
889 * This function assumes that p_this can be found in pp_objects. It will not
890 * crash if p_this cannot be found, but will return a wrong value. It is your
891 * duty to check the return value if you are not certain that the object could
893 *****************************************************************************/
894 static int FindIndex( vlc_object_t *p_this,
895 vlc_object_t **pp_objects, int i_count )
897 int i_middle = i_count / 2;
904 if( pp_objects[i_middle] == p_this )
914 /* We take advantage of the sorted array */
915 if( pp_objects[i_middle]->i_object_id < p_this->i_object_id )
917 return i_middle + FindIndex( p_this, pp_objects + i_middle,
918 i_count - i_middle );
922 return FindIndex( p_this, pp_objects, i_middle );
926 static vlc_object_t * FindObject( vlc_object_t *p_this, int i_type, int i_mode )
931 switch( i_mode & 0x000f )
934 p_tmp = p_this->p_parent;
937 if( p_tmp->i_object_type == i_type )
944 return FindObject( p_tmp, i_type, i_mode );
950 for( i = p_this->i_children; i--; )
952 p_tmp = p_this->pp_children[i];
953 if( p_tmp->i_object_type == i_type )
958 else if( p_tmp->i_children )
960 p_tmp = FindObject( p_tmp, i_type, i_mode );
970 /* Handled in vlc_object_find */
977 static void DetachObject( vlc_object_t *p_this )
979 vlc_object_t *p_parent = p_this->p_parent;
982 /* Remove p_this's parent */
983 p_this->p_parent = NULL;
985 /* Remove all of p_parent's children which are p_this */
986 for( i_index = p_parent->i_children ; i_index-- ; )
988 if( p_parent->pp_children[i_index] == p_this )
990 p_parent->i_children--;
991 for( i = i_index ; i < p_parent->i_children ; i++ )
993 p_parent->pp_children[i] = p_parent->pp_children[i+1];
998 if( p_parent->i_children )
1000 p_parent->pp_children = (vlc_object_t **)realloc( p_parent->pp_children,
1001 p_parent->i_children * sizeof(vlc_object_t *) );
1005 free( p_parent->pp_children );
1006 p_parent->pp_children = NULL;
1010 /*****************************************************************************
1011 * SetAttachment: recursively set the b_attached flag of a subtree.
1012 *****************************************************************************
1013 * This function is used by the attach and detach functions to propagate
1014 * the b_attached flag in a subtree.
1015 *****************************************************************************/
1016 static void SetAttachment( vlc_object_t *p_this, vlc_bool_t b_attached )
1020 for( i_index = p_this->i_children ; i_index-- ; )
1022 SetAttachment( p_this->pp_children[i_index], b_attached );
1025 p_this->b_attached = b_attached;
1028 static void PrintObject( vlc_object_t *p_this, const char *psz_prefix )
1030 char psz_children[20], psz_refcount[20], psz_thread[30], psz_name[50],
1034 if( p_this->psz_object_name )
1036 snprintf( psz_name, 49, " \"%s\"", p_this->psz_object_name );
1038 psz_name[48] = '\"';
1041 psz_children[0] = '\0';
1042 switch( p_this->i_children )
1047 strcpy( psz_children, ", 1 child" );
1050 snprintf( psz_children, 19, ", %i children", p_this->i_children );
1054 psz_refcount[0] = '\0';
1055 if( p_this->i_refcount )
1056 snprintf( psz_refcount, 19, ", refcount %i", p_this->i_refcount );
1058 psz_thread[0] = '\0';
1059 if( p_this->b_thread )
1060 snprintf( psz_thread, 29, " (thread %d)", (int)p_this->thread_id );
1062 psz_parent[0] = '\0';
1063 if( p_this->p_parent )
1064 snprintf( psz_parent, 19, ", parent %i", p_this->p_parent->i_object_id );
1066 printf( " %so %.8i %s%s%s%s%s%s\n", psz_prefix,
1067 p_this->i_object_id, p_this->psz_object_type,
1068 psz_name, psz_thread, psz_refcount, psz_children,
1072 static void DumpStructure( vlc_object_t *p_this, int i_level, char *psz_foo )
1075 char i_back = psz_foo[i_level];
1076 psz_foo[i_level] = '\0';
1078 PrintObject( p_this, psz_foo );
1080 psz_foo[i_level] = i_back;
1082 if( i_level / 2 >= MAX_DUMPSTRUCTURE_DEPTH )
1084 msg_Warn( p_this, "structure tree is too deep" );
1088 for( i = 0 ; i < p_this->i_children ; i++ )
1092 psz_foo[i_level-1] = ' ';
1094 if( psz_foo[i_level-2] == '`' )
1096 psz_foo[i_level-2] = ' ';
1100 if( i == p_this->i_children - 1 )
1102 psz_foo[i_level] = '`';
1106 psz_foo[i_level] = '|';
1109 psz_foo[i_level+1] = '-';
1110 psz_foo[i_level+2] = '\0';
1112 DumpStructure( p_this->pp_children[i], i_level + 2, psz_foo );
1116 static vlc_list_t * NewList( int i_count )
1118 vlc_list_t * p_list = (vlc_list_t *)malloc( sizeof( vlc_list_t ) );
1119 if( p_list == NULL )
1124 p_list->i_count = i_count;
1128 p_list->p_values = NULL;
1132 p_list->p_values = malloc( i_count * sizeof( vlc_value_t ) );
1133 if( p_list->p_values == NULL )
1135 p_list->i_count = 0;
1142 static void ListReplace( vlc_list_t *p_list, vlc_object_t *p_object,
1145 if( p_list == NULL || i_index >= p_list->i_count )
1150 p_object->i_refcount++;
1152 p_list->p_values[i_index].p_object = p_object;
1157 /*static void ListAppend( vlc_list_t *p_list, vlc_object_t *p_object )
1159 if( p_list == NULL )
1164 p_list->p_values = realloc( p_list->p_values, (p_list->i_count + 1)
1165 * sizeof( vlc_value_t ) );
1166 if( p_list->p_values == NULL )
1168 p_list->i_count = 0;
1172 p_object->i_refcount++;
1174 p_list->p_values[p_list->i_count].p_object = p_object;
1180 static int CountChildren( vlc_object_t *p_this, int i_type )
1182 vlc_object_t *p_tmp;
1185 for( i = 0; i < p_this->i_children; i++ )
1187 p_tmp = p_this->pp_children[i];
1189 if( p_tmp->i_object_type == i_type )
1194 if( p_tmp->i_children )
1196 i_count += CountChildren( p_tmp, i_type );
1203 static void ListChildren( vlc_list_t *p_list, vlc_object_t *p_this, int i_type )
1205 vlc_object_t *p_tmp;
1208 for( i = 0; i < p_this->i_children; i++ )
1210 p_tmp = p_this->pp_children[i];
1212 if( p_tmp->i_object_type == i_type )
1214 ListReplace( p_list, p_tmp, p_list->i_count++ );
1217 if( p_tmp->i_children )
1219 ListChildren( p_list, p_tmp, i_type );