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;
91 static vlc_object_internals_t global_internals;
93 vlc_object_t *vlc_custom_create( vlc_object_t *p_this, size_t i_size,
94 int i_type, const char *psz_type )
97 vlc_object_internals_t *p_priv;
99 if( i_type == VLC_OBJECT_GLOBAL )
102 p_priv = &global_internals;
103 memset( p_priv, 0, sizeof( *p_priv ) );
107 p_priv = calloc( 1, sizeof( *p_priv ) + i_size );
111 p_new = (vlc_object_t *)(p_priv + 1);
114 p_new->p_internals = p_priv;
115 p_new->i_object_type = i_type;
116 p_new->psz_object_type = psz_type;
118 p_new->psz_object_name = NULL;
120 p_new->b_die = VLC_FALSE;
121 p_new->b_error = VLC_FALSE;
122 p_new->b_dead = VLC_FALSE;
123 p_new->b_attached = VLC_FALSE;
124 p_new->b_force = VLC_FALSE;
126 p_new->psz_header = NULL;
128 if( p_this->i_flags & OBJECT_FLAGS_NODBG )
129 p_new->i_flags |= OBJECT_FLAGS_NODBG;
130 if( p_this->i_flags & OBJECT_FLAGS_QUIET )
131 p_new->i_flags |= OBJECT_FLAGS_QUIET;
132 if( p_this->i_flags & OBJECT_FLAGS_NOINTERACT )
133 p_new->i_flags |= OBJECT_FLAGS_NOINTERACT;
135 p_priv->p_vars = calloc( sizeof( variable_t ), 16 );
137 if( !p_priv->p_vars )
139 if( i_type != VLC_OBJECT_GLOBAL )
144 if( i_type == VLC_OBJECT_GLOBAL )
146 /* If i_type is global, then p_new is actually p_libvlc_global */
147 libvlc_global_data_t *p_libvlc_global = (libvlc_global_data_t *)p_new;
148 p_new->p_libvlc = NULL;
150 p_libvlc_global->i_counter = 0;
151 p_new->i_object_id = 0;
153 p_libvlc_global->i_objects = 1;
154 p_libvlc_global->pp_objects = malloc( sizeof(vlc_object_t *) );
155 p_libvlc_global->pp_objects[0] = p_new;
156 p_new->b_attached = VLC_TRUE;
160 libvlc_global_data_t *p_libvlc_global = vlc_global();
161 p_new->p_libvlc = ( i_type == VLC_OBJECT_LIBVLC ) ? (libvlc_int_t*)p_new
164 vlc_mutex_lock( &structure_lock );
166 p_libvlc_global->i_counter++;
167 p_new->i_object_id = p_libvlc_global->i_counter;
169 /* Wooohaa! If *this* fails, we're in serious trouble! Anyway it's
170 * useless to try and recover anything if pp_objects gets smashed. */
171 TAB_APPEND( p_libvlc_global->i_objects, p_libvlc_global->pp_objects,
174 vlc_mutex_unlock( &structure_lock );
177 p_new->i_refcount = 0;
178 p_new->p_parent = NULL;
179 p_new->pp_children = NULL;
180 p_new->i_children = 0;
182 p_new->p_private = NULL;
184 /* Initialize mutexes and condvars */
185 vlc_mutex_init( p_new, &p_new->object_lock );
186 vlc_cond_init( p_new, &p_new->object_wait );
187 vlc_mutex_init( p_new, &p_priv->var_lock );
189 if( i_type == VLC_OBJECT_GLOBAL )
191 vlc_mutex_init( p_new, &structure_lock );
193 var_Create( p_new, "list", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
194 var_AddCallback( p_new, "list", DumpCommand, NULL );
195 var_Create( p_new, "tree", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
196 var_AddCallback( p_new, "tree", DumpCommand, NULL );
197 var_Create( p_new, "vars", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
198 var_AddCallback( p_new, "vars", DumpCommand, NULL );
206 * Allocates and initializes a vlc object.
208 * @param i_type known object type (all of them are negative integer values),
209 * or object byte size (always positive).
211 * @return the new object, or NULL on error.
213 void * __vlc_object_create( vlc_object_t *p_this, int i_type )
215 const char * psz_type;
220 case VLC_OBJECT_GLOBAL:
221 i_size = sizeof(libvlc_global_data_t);
224 case VLC_OBJECT_LIBVLC:
225 i_size = sizeof(libvlc_int_t);
228 case VLC_OBJECT_INTF:
229 i_size = sizeof(intf_thread_t);
230 psz_type = "interface";
232 case VLC_OBJECT_DIALOGS:
233 i_size = sizeof(intf_thread_t);
234 psz_type = "dialogs";
236 case VLC_OBJECT_PLAYLIST:
237 i_size = sizeof(playlist_t);
238 psz_type = "playlist";
241 i_size = sizeof(services_discovery_t);
242 psz_type = "services discovery";
244 case VLC_OBJECT_INPUT:
245 i_size = sizeof(input_thread_t);
248 case VLC_OBJECT_DEMUX:
249 i_size = sizeof(demux_t);
252 case VLC_OBJECT_ACCESS:
253 i_size = sizeof(access_t);
256 case VLC_OBJECT_DECODER:
257 i_size = sizeof(decoder_t);
258 psz_type = "decoder";
260 case VLC_OBJECT_PACKETIZER:
261 i_size = sizeof(decoder_t);
262 psz_type = "packetizer";
264 case VLC_OBJECT_ENCODER:
265 i_size = sizeof(encoder_t);
266 psz_type = "encoder";
268 case VLC_OBJECT_FILTER:
269 i_size = sizeof(filter_t);
272 case VLC_OBJECT_VOUT:
273 i_size = sizeof(vout_thread_t);
274 psz_type = "video output";
277 i_size = sizeof(spu_t);
278 psz_type = "subpicture";
280 case VLC_OBJECT_AOUT:
281 i_size = sizeof(aout_instance_t);
282 psz_type = "audio output";
284 case VLC_OBJECT_SOUT:
285 i_size = sizeof(sout_instance_t);
286 psz_type = "stream output";
289 i_size = sizeof( vlm_t );
290 psz_type = "vlm dameon";
293 i_size = sizeof( vod_t );
294 psz_type = "vod server";
297 i_size = sizeof( tls_t );
301 i_size = sizeof( xml_t );
304 case VLC_OBJECT_OPENGL:
305 i_size = sizeof( vout_thread_t );
308 case VLC_OBJECT_ANNOUNCE:
309 i_size = sizeof( announce_handler_t );
310 psz_type = "announce";
312 case VLC_OBJECT_META_ENGINE:
313 i_size = sizeof( meta_engine_t );
314 psz_type = "meta engine";
316 case VLC_OBJECT_OSDMENU:
317 i_size = sizeof( osd_menu_t );
318 psz_type = "osd menu";
321 i_size = i_type > (int)sizeof(vlc_object_t)
322 ? i_type : (int)sizeof(vlc_object_t);
323 i_type = VLC_OBJECT_GENERIC;
324 psz_type = "generic";
328 return vlc_custom_create( p_this, i_size, i_type, psz_type );
333 ****************************************************************************
334 * Destroy a vlc object
336 * This function destroys an object that has been previously allocated with
337 * vlc_object_create. The object's refcount must be zero and it must not be
338 * attached to other objects in any way.
339 *****************************************************************************/
340 void __vlc_object_destroy( vlc_object_t *p_this )
342 vlc_object_internals_t *p_priv = vlc_internals( p_this );
345 if( p_this->i_children )
347 msg_Err( p_this, "cannot delete object (%i, %s) with children" ,
348 p_this->i_object_id, p_this->psz_object_name );
352 if( p_this->p_parent )
354 msg_Err( p_this, "cannot delete object (%i, %s) with a parent",
355 p_this->i_object_id, p_this->psz_object_name );
359 while( p_this->i_refcount )
363 /* Don't warn immediately ... 100ms seems OK */
367 "refcount is %i, delaying before deletion (id=%d,type=%d)",
368 p_this->i_refcount, p_this->i_object_id,
369 p_this->i_object_type );
371 else if( i_delay == 10 )
374 "refcount is %i, delaying again (id=%d,type=%d)",
375 p_this->i_refcount, p_this->i_object_id,
376 p_this->i_object_type );
378 else if( i_delay == 20 )
381 "waited too long, cancelling destruction (id=%d,type=%d)",
382 p_this->i_object_id, p_this->i_object_type );
389 /* Destroy the associated variables, starting from the end so that
390 * no memmove calls have to be done. */
391 while( p_priv->i_vars )
393 var_Destroy( p_this, p_priv->p_vars[p_priv->i_vars - 1].psz_name );
396 free( p_priv->p_vars );
397 vlc_mutex_destroy( &p_priv->var_lock );
399 if( p_this->psz_header ) free( p_this->psz_header );
401 if( p_this->i_object_type == VLC_OBJECT_GLOBAL )
403 libvlc_global_data_t *p_global = (libvlc_global_data_t *)p_this;
404 /* We are the global object ... no need to lock. */
405 free( p_global->pp_objects );
406 p_global->pp_objects = NULL;
407 p_global->i_objects--;
409 vlc_mutex_destroy( &structure_lock );
413 libvlc_global_data_t *p_libvlc_global = vlc_global();
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_libvlc_global->pp_objects,
421 p_libvlc_global->i_objects );
422 REMOVE_ELEM( p_libvlc_global->pp_objects,
423 p_libvlc_global->i_objects, i_index );
425 vlc_mutex_unlock( &structure_lock );
428 #if defined(WIN32) || defined(UNDER_CE)
429 /* if object has an associated thread, close it now */
430 if( p_priv->thread_id.hThread )
431 CloseHandle(p_priv->thread_id.hThread);
434 vlc_mutex_destroy( &p_this->object_lock );
435 vlc_cond_destroy( &p_this->object_wait );
437 /* global is not dynamically allocated by vlc_object_create */
438 if( p_this->i_object_type != VLC_OBJECT_GLOBAL )
443 void __vlc_object_kill( vlc_object_t *p_this )
445 vlc_mutex_lock( &p_this->object_lock );
446 p_this->b_die = VLC_TRUE;
447 vlc_mutex_unlock( &p_this->object_lock );
451 vlc_bool_t __vlc_object_dying_unlocked( vlc_object_t *p_this )
453 vlc_assert_locked( &p_this->object_lock );
454 return p_this->b_die;
458 vlc_bool_t __vlc_object_dying( vlc_object_t *p_this )
461 vlc_mutex_lock( &p_this->object_lock );
462 b = __vlc_object_dying_unlocked( p_this );
463 vlc_mutex_unlock( &p_this->object_lock );
469 * find an object given its ID
471 * This function looks for the object whose i_object_id field is i_id. We
472 * use a dichotomy so that lookups are in log2(n).
473 *****************************************************************************/
474 void * __vlc_object_get( vlc_object_t *p_this, int i_id )
477 vlc_object_t **pp_objects;
478 libvlc_global_data_t *p_libvlc_global = vlc_global();
480 vlc_mutex_lock( &structure_lock );
482 pp_objects = p_libvlc_global->pp_objects;
484 /* Perform our dichotomy */
485 for( i_max = p_libvlc_global->i_objects - 1 ; ; )
487 i_middle = i_max / 2;
489 if( pp_objects[i_middle]->i_object_id > i_id )
493 else if( pp_objects[i_middle]->i_object_id < i_id )
497 pp_objects += i_middle;
502 /* This happens when there are only two remaining objects */
503 if( pp_objects[i_middle+1]->i_object_id == i_id )
505 vlc_mutex_unlock( &structure_lock );
506 pp_objects[i_middle+1]->i_refcount++;
507 return pp_objects[i_middle+1];
514 vlc_mutex_unlock( &structure_lock );
515 pp_objects[i_middle]->i_refcount++;
516 return pp_objects[i_middle];
521 /* this means that i_max == i_middle, and since we have already
522 * tested pp_objects[i_middle]), p_found is properly set. */
527 vlc_mutex_unlock( &structure_lock );
532 ****************************************************************************
533 * find a typed object and increment its refcount
534 *****************************************************************************
535 * This function recursively looks for a given object type. i_mode can be one
536 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
537 *****************************************************************************/
538 void * __vlc_object_find( vlc_object_t *p_this, int i_type, int i_mode )
540 vlc_object_t *p_found;
542 vlc_mutex_lock( &structure_lock );
544 /* If we are of the requested type ourselves, don't look further */
545 if( !(i_mode & FIND_STRICT) && p_this->i_object_type == i_type )
547 p_this->i_refcount++;
548 vlc_mutex_unlock( &structure_lock );
552 /* Otherwise, recursively look for the object */
553 if( (i_mode & 0x000f) == FIND_ANYWHERE )
555 vlc_object_t *p_root = p_this;
558 while( p_root->p_parent != NULL &&
559 p_root != VLC_OBJECT( p_this->p_libvlc ) )
561 p_root = p_root->p_parent;
564 p_found = FindObject( p_root, i_type, (i_mode & ~0x000f)|FIND_CHILD );
565 if( p_found == NULL && p_root != VLC_OBJECT( p_this->p_libvlc ) )
567 p_found = FindObject( VLC_OBJECT( p_this->p_libvlc ),
568 i_type, (i_mode & ~0x000f)|FIND_CHILD );
573 p_found = FindObject( p_this, i_type, i_mode );
576 vlc_mutex_unlock( &structure_lock );
582 ****************************************************************************
583 * find a named object and increment its refcount
584 *****************************************************************************
585 * This function recursively looks for a given object name. i_mode can be one
586 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
587 *****************************************************************************/
588 void * __vlc_object_find_name( vlc_object_t *p_this, const char *psz_name,
591 vlc_object_t *p_found;
593 vlc_mutex_lock( &structure_lock );
595 /* If have the requested name ourselves, don't look further */
596 if( !(i_mode & FIND_STRICT)
597 && p_this->psz_object_name
598 && !strcmp( p_this->psz_object_name, psz_name ) )
600 p_this->i_refcount++;
601 vlc_mutex_unlock( &structure_lock );
605 /* Otherwise, recursively look for the object */
606 if( (i_mode & 0x000f) == FIND_ANYWHERE )
608 vlc_object_t *p_root = p_this;
611 while( p_root->p_parent != NULL &&
612 p_root != VLC_OBJECT( p_this->p_libvlc ) )
614 p_root = p_root->p_parent;
617 p_found = FindObjectName( p_root, psz_name,
618 (i_mode & ~0x000f)|FIND_CHILD );
619 if( p_found == NULL && p_root != VLC_OBJECT( p_this->p_libvlc ) )
621 p_found = FindObjectName( VLC_OBJECT( p_this->p_libvlc ),
622 psz_name, (i_mode & ~0x000f)|FIND_CHILD );
627 p_found = FindObjectName( p_this, psz_name, i_mode );
630 vlc_mutex_unlock( &structure_lock );
636 ****************************************************************************
637 * increment an object refcount
638 *****************************************************************************/
639 void __vlc_object_yield( vlc_object_t *p_this )
641 vlc_mutex_lock( &structure_lock );
642 p_this->i_refcount++;
643 vlc_mutex_unlock( &structure_lock );
647 ****************************************************************************
648 * decrement an object refcount
649 *****************************************************************************/
650 void __vlc_object_release( vlc_object_t *p_this )
652 vlc_mutex_lock( &structure_lock );
653 p_this->i_refcount--;
654 vlc_mutex_unlock( &structure_lock );
658 ****************************************************************************
659 * attach object to a parent object
660 *****************************************************************************
661 * This function sets p_this as a child of p_parent, and p_parent as a parent
662 * of p_this. This link can be undone using vlc_object_detach.
663 *****************************************************************************/
664 void __vlc_object_attach( vlc_object_t *p_this, vlc_object_t *p_parent )
666 if( !p_this ) return;
668 vlc_mutex_lock( &structure_lock );
670 /* Attach the parent to its child */
671 p_this->p_parent = p_parent;
673 /* Attach the child to its parent */
674 INSERT_ELEM( p_parent->pp_children, p_parent->i_children,
675 p_parent->i_children, p_this );
677 /* Climb up the tree to see whether we are connected with the root */
678 if( p_parent->b_attached )
680 SetAttachment( p_this, VLC_TRUE );
683 vlc_mutex_unlock( &structure_lock );
687 ****************************************************************************
688 * detach object from its parent
689 *****************************************************************************
690 * This function removes all links between an object and its parent.
691 *****************************************************************************/
692 void __vlc_object_detach( vlc_object_t *p_this )
694 if( !p_this ) return;
696 vlc_mutex_lock( &structure_lock );
697 if( !p_this->p_parent )
699 msg_Err( p_this, "object is not attached" );
700 vlc_mutex_unlock( &structure_lock );
704 /* Climb up the tree to see whether we are connected with the root */
705 if( p_this->p_parent->b_attached )
707 SetAttachment( p_this, VLC_FALSE );
710 DetachObject( p_this );
711 vlc_mutex_unlock( &structure_lock );
716 ****************************************************************************
717 * find a list typed objects and increment their refcount
718 *****************************************************************************
719 * This function recursively looks for a given object type. i_mode can be one
720 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
721 *****************************************************************************/
722 vlc_list_t * __vlc_list_find( vlc_object_t *p_this, int i_type, int i_mode )
725 vlc_object_t **pp_current, **pp_end;
726 int i_count = 0, i_index = 0;
727 libvlc_global_data_t *p_libvlc_global = vlc_global();
729 vlc_mutex_lock( &structure_lock );
731 /* Look for the objects */
732 switch( i_mode & 0x000f )
735 pp_current = p_libvlc_global->pp_objects;
736 pp_end = pp_current + p_libvlc_global->i_objects;
738 for( ; pp_current < pp_end ; pp_current++ )
740 if( (*pp_current)->b_attached
741 && (*pp_current)->i_object_type == i_type )
747 p_list = NewList( i_count );
748 pp_current = p_libvlc_global->pp_objects;
750 for( ; pp_current < pp_end ; pp_current++ )
752 if( (*pp_current)->b_attached
753 && (*pp_current)->i_object_type == i_type )
755 ListReplace( p_list, *pp_current, i_index );
756 if( i_index < i_count ) i_index++;
762 i_count = CountChildren( p_this, i_type );
763 p_list = NewList( i_count );
765 /* Check allocation was successful */
766 if( p_list->i_count != i_count )
768 msg_Err( p_this, "list allocation failed!" );
774 ListChildren( p_list, p_this, i_type );
778 msg_Err( p_this, "unimplemented!" );
779 p_list = NewList( 0 );
783 vlc_mutex_unlock( &structure_lock );
788 /*****************************************************************************
789 * DumpCommand: print the current vlc structure
790 *****************************************************************************
791 * This function prints either an ASCII tree showing the connections between
792 * vlc objects, and additional information such as their refcount, thread ID,
793 * etc. (command "tree"), or the same data as a simple list (command "list").
794 *****************************************************************************/
795 static int DumpCommand( vlc_object_t *p_this, char const *psz_cmd,
796 vlc_value_t oldval, vlc_value_t newval, void *p_data )
798 libvlc_global_data_t *p_libvlc_global = vlc_global();
800 (void)oldval; (void)p_data;
801 if( *psz_cmd == 'l' )
803 vlc_mutex_lock( &structure_lock );
805 vlc_object_t **pp_current, **pp_end;
807 pp_current = p_libvlc_global->pp_objects;
808 pp_end = pp_current + p_libvlc_global->i_objects;
810 for( ; pp_current < pp_end ; pp_current++ )
812 if( (*pp_current)->b_attached )
814 PrintObject( *pp_current, "" );
818 printf( " o %.8i %s (not attached)\n",
819 (*pp_current)->i_object_id,
820 (*pp_current)->psz_object_type );
824 vlc_mutex_unlock( &structure_lock );
828 vlc_object_t *p_object = NULL;
830 if( *newval.psz_string )
833 int i_id = strtol( newval.psz_string, &end, 0 );
834 if( end != newval.psz_string )
835 p_object = vlc_object_get( p_this, i_id );
838 /* try using the object's name to find it */
839 vlc_object_t *p_libvlc = vlc_object_get( p_this, 1 );
842 /* Look in p_libvlc's children tree */
843 p_object = vlc_object_find_name( p_libvlc,
846 vlc_object_release( p_libvlc );
850 /* If it's not in libvlc, look in libvlc_global (== p_this) */
851 p_object = vlc_object_find_name( p_this,
863 vlc_mutex_lock( &structure_lock );
865 if( *psz_cmd == 't' )
867 char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
870 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
873 DumpStructure( p_object, 0, psz_foo );
875 else if( *psz_cmd == 'v' )
880 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
882 PrintObject( p_object, "" );
884 if( !p_object->p_internals->i_vars )
885 printf( " `-o No variables\n" );
886 for( i = 0; i < p_object->p_internals->i_vars; i++ )
888 variable_t *p_var = p_object->p_internals->p_vars + i;
890 const char *psz_type = "unknown";
891 switch( p_var->i_type & VLC_VAR_TYPE )
893 #define MYCASE( type, nice ) \
894 case VLC_VAR_ ## type: \
897 MYCASE( VOID, "void" );
898 MYCASE( BOOL, "bool" );
899 MYCASE( INTEGER, "integer" );
900 MYCASE( HOTKEY, "hotkey" );
901 MYCASE( STRING, "string" );
902 MYCASE( MODULE, "module" );
903 MYCASE( FILE, "file" );
904 MYCASE( DIRECTORY, "directory" );
905 MYCASE( VARIABLE, "variable" );
906 MYCASE( FLOAT, "float" );
907 MYCASE( TIME, "time" );
908 MYCASE( ADDRESS, "address" );
909 MYCASE( MUTEX, "mutex" );
910 MYCASE( LIST, "list" );
913 printf( " %c-o \"%s\" (%s",
914 i + 1 == p_object->p_internals->i_vars ? '`' : '|',
915 p_var->psz_name, psz_type );
916 if( p_var->psz_text )
917 printf( ", %s", p_var->psz_text );
919 if( p_var->i_type & VLC_VAR_ISCOMMAND )
920 printf( ", command" );
921 if( p_var->i_entries )
922 printf( ", %d callbacks", p_var->i_entries );
923 switch( p_var->i_type & 0x00f0 )
929 printf( ": %s", p_var->val.b_bool ? "true" : "false" );
931 case VLC_VAR_INTEGER:
932 printf( ": %d", p_var->val.i_int );
935 printf( ": \"%s\"", p_var->val.psz_string );
938 printf( ": %f", p_var->val.f_float );
941 printf( ": " I64Fi, (int64_t)p_var->val.i_time );
943 case VLC_VAR_ADDRESS:
944 printf( ": %p", p_var->val.p_address );
954 vlc_mutex_unlock( &structure_lock );
956 if( *newval.psz_string )
958 vlc_object_release( p_object );
965 /*****************************************************************************
966 * vlc_list_release: free a list previously allocated by vlc_list_find
967 *****************************************************************************
968 * This function decreases the refcount of all objects in the list and
970 *****************************************************************************/
971 void vlc_list_release( vlc_list_t *p_list )
975 vlc_mutex_lock( &structure_lock );
976 for( i_index = 0; i_index < p_list->i_count; i_index++ )
978 p_list->p_values[i_index].p_object->i_refcount--;
980 vlc_mutex_unlock( &structure_lock );
982 free( p_list->p_values );
986 /* Following functions are local */
988 /*****************************************************************************
989 * FindIndex: find the index of an object in an array of objects
990 *****************************************************************************
991 * This function assumes that p_this can be found in pp_objects. It will not
992 * crash if p_this cannot be found, but will return a wrong value. It is your
993 * duty to check the return value if you are not certain that the object could
995 *****************************************************************************/
996 static int FindIndex( vlc_object_t *p_this,
997 vlc_object_t **pp_objects, int i_count )
999 int i_middle = i_count / 2;
1006 if( pp_objects[i_middle] == p_this )
1016 /* We take advantage of the sorted array */
1017 if( pp_objects[i_middle]->i_object_id < p_this->i_object_id )
1019 return i_middle + FindIndex( p_this, pp_objects + i_middle,
1020 i_count - i_middle );
1024 return FindIndex( p_this, pp_objects, i_middle );
1028 static vlc_object_t * FindObject( vlc_object_t *p_this, int i_type, int i_mode )
1031 vlc_object_t *p_tmp;
1033 switch( i_mode & 0x000f )
1036 p_tmp = p_this->p_parent;
1039 if( p_tmp->i_object_type == i_type )
1041 p_tmp->i_refcount++;
1046 return FindObject( p_tmp, i_type, i_mode );
1052 for( i = p_this->i_children; i--; )
1054 p_tmp = p_this->pp_children[i];
1055 if( p_tmp->i_object_type == i_type )
1057 p_tmp->i_refcount++;
1060 else if( p_tmp->i_children )
1062 p_tmp = FindObject( p_tmp, i_type, i_mode );
1072 /* Handled in vlc_object_find */
1079 static vlc_object_t * FindObjectName( vlc_object_t *p_this,
1080 const char *psz_name,
1084 vlc_object_t *p_tmp;
1086 switch( i_mode & 0x000f )
1089 p_tmp = p_this->p_parent;
1092 if( p_tmp->psz_object_name
1093 && !strcmp( p_tmp->psz_object_name, psz_name ) )
1095 p_tmp->i_refcount++;
1100 return FindObjectName( p_tmp, psz_name, i_mode );
1106 for( i = p_this->i_children; i--; )
1108 p_tmp = p_this->pp_children[i];
1109 if( p_tmp->psz_object_name
1110 && !strcmp( p_tmp->psz_object_name, psz_name ) )
1112 p_tmp->i_refcount++;
1115 else if( p_tmp->i_children )
1117 p_tmp = FindObjectName( p_tmp, psz_name, i_mode );
1127 /* Handled in vlc_object_find */
1134 static void DetachObject( vlc_object_t *p_this )
1136 vlc_object_t *p_parent = p_this->p_parent;
1139 /* Remove p_this's parent */
1140 p_this->p_parent = NULL;
1142 /* Remove all of p_parent's children which are p_this */
1143 for( i_index = p_parent->i_children ; i_index-- ; )
1145 if( p_parent->pp_children[i_index] == p_this )
1147 p_parent->i_children--;
1148 for( i = i_index ; i < p_parent->i_children ; i++ )
1150 p_parent->pp_children[i] = p_parent->pp_children[i+1];
1155 if( p_parent->i_children )
1157 p_parent->pp_children = (vlc_object_t **)realloc( p_parent->pp_children,
1158 p_parent->i_children * sizeof(vlc_object_t *) );
1162 free( p_parent->pp_children );
1163 p_parent->pp_children = NULL;
1167 /*****************************************************************************
1168 * SetAttachment: recursively set the b_attached flag of a subtree.
1169 *****************************************************************************
1170 * This function is used by the attach and detach functions to propagate
1171 * the b_attached flag in a subtree.
1172 *****************************************************************************/
1173 static void SetAttachment( vlc_object_t *p_this, vlc_bool_t b_attached )
1177 for( i_index = p_this->i_children ; i_index-- ; )
1179 SetAttachment( p_this->pp_children[i_index], b_attached );
1182 p_this->b_attached = b_attached;
1185 static void PrintObject( vlc_object_t *p_this, const char *psz_prefix )
1187 char psz_children[20], psz_refcount[20], psz_thread[30], psz_name[50],
1191 if( p_this->psz_object_name )
1193 snprintf( psz_name, 49, " \"%s\"", p_this->psz_object_name );
1195 psz_name[48] = '\"';
1198 psz_children[0] = '\0';
1199 switch( p_this->i_children )
1204 strcpy( psz_children, ", 1 child" );
1207 snprintf( psz_children, 19, ", %i children", p_this->i_children );
1211 psz_refcount[0] = '\0';
1212 if( p_this->i_refcount )
1213 snprintf( psz_refcount, 19, ", refcount %i", p_this->i_refcount );
1215 psz_thread[0] = '\0';
1216 if( p_this->p_internals->b_thread )
1217 snprintf( psz_thread, 29, " (thread %u)",
1218 #if defined(WIN32) || defined(UNDER_CE)
1219 (unsigned)p_this->p_internals->thread_id.id );
1221 (unsigned)p_this->p_internals->thread_id );
1224 psz_parent[0] = '\0';
1225 if( p_this->p_parent )
1226 snprintf( psz_parent, 19, ", parent %i", p_this->p_parent->i_object_id );
1228 printf( " %so %.8i %s%s%s%s%s%s\n", psz_prefix,
1229 p_this->i_object_id, p_this->psz_object_type,
1230 psz_name, psz_thread, psz_refcount, psz_children,
1234 static void DumpStructure( vlc_object_t *p_this, int i_level, char *psz_foo )
1237 char i_back = psz_foo[i_level];
1238 psz_foo[i_level] = '\0';
1240 PrintObject( p_this, psz_foo );
1242 psz_foo[i_level] = i_back;
1244 if( i_level / 2 >= MAX_DUMPSTRUCTURE_DEPTH )
1246 msg_Warn( p_this, "structure tree is too deep" );
1250 for( i = 0 ; i < p_this->i_children ; i++ )
1254 psz_foo[i_level-1] = ' ';
1256 if( psz_foo[i_level-2] == '`' )
1258 psz_foo[i_level-2] = ' ';
1262 if( i == p_this->i_children - 1 )
1264 psz_foo[i_level] = '`';
1268 psz_foo[i_level] = '|';
1271 psz_foo[i_level+1] = '-';
1272 psz_foo[i_level+2] = '\0';
1274 DumpStructure( p_this->pp_children[i], i_level + 2, psz_foo );
1278 static vlc_list_t * NewList( int i_count )
1280 vlc_list_t * p_list = (vlc_list_t *)malloc( sizeof( vlc_list_t ) );
1281 if( p_list == NULL )
1286 p_list->i_count = i_count;
1290 p_list->p_values = NULL;
1294 p_list->p_values = malloc( i_count * sizeof( vlc_value_t ) );
1295 if( p_list->p_values == NULL )
1297 p_list->i_count = 0;
1304 static void ListReplace( vlc_list_t *p_list, vlc_object_t *p_object,
1307 if( p_list == NULL || i_index >= p_list->i_count )
1312 p_object->i_refcount++;
1314 p_list->p_values[i_index].p_object = p_object;
1319 /*static void ListAppend( vlc_list_t *p_list, vlc_object_t *p_object )
1321 if( p_list == NULL )
1326 p_list->p_values = realloc( p_list->p_values, (p_list->i_count + 1)
1327 * sizeof( vlc_value_t ) );
1328 if( p_list->p_values == NULL )
1330 p_list->i_count = 0;
1334 p_object->i_refcount++;
1336 p_list->p_values[p_list->i_count].p_object = p_object;
1342 static int CountChildren( vlc_object_t *p_this, int i_type )
1344 vlc_object_t *p_tmp;
1347 for( i = 0; i < p_this->i_children; i++ )
1349 p_tmp = p_this->pp_children[i];
1351 if( p_tmp->i_object_type == i_type )
1356 if( p_tmp->i_children )
1358 i_count += CountChildren( p_tmp, i_type );
1365 static void ListChildren( vlc_list_t *p_list, vlc_object_t *p_this, int i_type )
1367 vlc_object_t *p_tmp;
1370 for( i = 0; i < p_this->i_children; i++ )
1372 p_tmp = p_this->pp_children[i];
1374 if( p_tmp->i_object_type == i_type )
1376 ListReplace( p_list, p_tmp, p_list->i_count++ );
1379 if( p_tmp->i_children )
1381 ListChildren( p_list, p_tmp, i_type );