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_priv->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_priv->b_attached = VLC_TRUE;
160 libvlc_global_data_t *p_libvlc_global = vlc_global();
161 if( i_type == VLC_OBJECT_LIBVLC )
163 p_new->p_libvlc = (libvlc_int_t*)p_new;
164 p_priv->b_attached = VLC_TRUE;
168 p_new->p_libvlc = p_this->p_libvlc;
171 vlc_mutex_lock( &structure_lock );
173 p_libvlc_global->i_counter++;
174 p_new->i_object_id = p_libvlc_global->i_counter;
176 /* Wooohaa! If *this* fails, we're in serious trouble! Anyway it's
177 * useless to try and recover anything if pp_objects gets smashed. */
178 TAB_APPEND( p_libvlc_global->i_objects, p_libvlc_global->pp_objects,
181 vlc_mutex_unlock( &structure_lock );
184 p_new->i_refcount = 0;
185 p_new->p_parent = NULL;
186 p_new->pp_children = NULL;
187 p_new->i_children = 0;
189 p_new->p_private = NULL;
191 /* Initialize mutexes and condvars */
192 vlc_mutex_init( p_new, &p_new->object_lock );
193 vlc_cond_init( p_new, &p_new->object_wait );
194 vlc_mutex_init( p_new, &p_priv->var_lock );
196 if( i_type == VLC_OBJECT_GLOBAL )
198 vlc_mutex_init( p_new, &structure_lock );
200 var_Create( p_new, "list", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
201 var_AddCallback( p_new, "list", DumpCommand, NULL );
202 var_Create( p_new, "tree", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
203 var_AddCallback( p_new, "tree", DumpCommand, NULL );
204 var_Create( p_new, "vars", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
205 var_AddCallback( p_new, "vars", DumpCommand, NULL );
213 * Allocates and initializes a vlc object.
215 * @param i_type known object type (all of them are negative integer values),
216 * or object byte size (always positive).
218 * @return the new object, or NULL on error.
220 void * __vlc_object_create( vlc_object_t *p_this, int i_type )
222 const char * psz_type;
227 case VLC_OBJECT_GLOBAL:
228 i_size = sizeof(libvlc_global_data_t);
231 case VLC_OBJECT_LIBVLC:
232 i_size = sizeof(libvlc_int_t);
235 case VLC_OBJECT_INTF:
236 i_size = sizeof(intf_thread_t);
237 psz_type = "interface";
239 case VLC_OBJECT_DIALOGS:
240 i_size = sizeof(intf_thread_t);
241 psz_type = "dialogs";
243 case VLC_OBJECT_PLAYLIST:
244 i_size = sizeof(playlist_t);
245 psz_type = "playlist";
248 i_size = sizeof(services_discovery_t);
249 psz_type = "services discovery";
251 case VLC_OBJECT_INPUT:
252 i_size = sizeof(input_thread_t);
255 case VLC_OBJECT_DEMUX:
256 i_size = sizeof(demux_t);
259 case VLC_OBJECT_ACCESS:
260 i_size = sizeof(access_t);
263 case VLC_OBJECT_DECODER:
264 i_size = sizeof(decoder_t);
265 psz_type = "decoder";
267 case VLC_OBJECT_PACKETIZER:
268 i_size = sizeof(decoder_t);
269 psz_type = "packetizer";
271 case VLC_OBJECT_ENCODER:
272 i_size = sizeof(encoder_t);
273 psz_type = "encoder";
275 case VLC_OBJECT_FILTER:
276 i_size = sizeof(filter_t);
279 case VLC_OBJECT_VOUT:
280 i_size = sizeof(vout_thread_t);
281 psz_type = "video output";
284 i_size = sizeof(spu_t);
285 psz_type = "subpicture";
287 case VLC_OBJECT_AOUT:
288 i_size = sizeof(aout_instance_t);
289 psz_type = "audio output";
291 case VLC_OBJECT_SOUT:
292 i_size = sizeof(sout_instance_t);
293 psz_type = "stream output";
296 i_size = sizeof( vlm_t );
297 psz_type = "vlm dameon";
300 i_size = sizeof( vod_t );
301 psz_type = "vod server";
304 i_size = sizeof( tls_t );
308 i_size = sizeof( xml_t );
311 case VLC_OBJECT_OPENGL:
312 i_size = sizeof( vout_thread_t );
315 case VLC_OBJECT_ANNOUNCE:
316 i_size = sizeof( announce_handler_t );
317 psz_type = "announce";
319 case VLC_OBJECT_META_ENGINE:
320 i_size = sizeof( meta_engine_t );
321 psz_type = "meta engine";
323 case VLC_OBJECT_OSDMENU:
324 i_size = sizeof( osd_menu_t );
325 psz_type = "osd menu";
328 i_size = i_type > (int)sizeof(vlc_object_t)
329 ? i_type : (int)sizeof(vlc_object_t);
330 i_type = VLC_OBJECT_GENERIC;
331 psz_type = "generic";
335 return vlc_custom_create( p_this, i_size, i_type, psz_type );
340 ****************************************************************************
341 * Destroy a vlc object
343 * This function destroys an object that has been previously allocated with
344 * vlc_object_create. The object's refcount must be zero and it must not be
345 * attached to other objects in any way.
346 *****************************************************************************/
347 void __vlc_object_destroy( vlc_object_t *p_this )
349 vlc_object_internals_t *p_priv = vlc_internals( p_this );
352 if( p_this->i_children )
354 msg_Err( p_this, "cannot delete object (%i, %s) with children" ,
355 p_this->i_object_id, p_this->psz_object_name );
359 if( p_this->p_parent )
361 msg_Err( p_this, "cannot delete object (%i, %s) with a parent",
362 p_this->i_object_id, p_this->psz_object_name );
366 while( p_this->i_refcount )
370 /* Don't warn immediately ... 100ms seems OK */
374 "refcount is %i, delaying before deletion (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 == 10 )
381 "refcount is %i, delaying again (id=%d,type=%d)",
382 p_this->i_refcount, p_this->i_object_id,
383 p_this->i_object_type );
385 else if( i_delay == 20 )
388 "waited too long, cancelling destruction (id=%d,type=%d)",
389 p_this->i_object_id, p_this->i_object_type );
396 /* Destroy the associated variables, starting from the end so that
397 * no memmove calls have to be done. */
398 while( p_priv->i_vars )
400 var_Destroy( p_this, p_priv->p_vars[p_priv->i_vars - 1].psz_name );
403 free( p_priv->p_vars );
404 vlc_mutex_destroy( &p_priv->var_lock );
406 if( p_this->psz_header ) free( p_this->psz_header );
408 if( p_this->i_object_type == VLC_OBJECT_GLOBAL )
410 libvlc_global_data_t *p_global = (libvlc_global_data_t *)p_this;
411 /* We are the global object ... no need to lock. */
412 free( p_global->pp_objects );
413 p_global->pp_objects = NULL;
414 p_global->i_objects--;
416 vlc_mutex_destroy( &structure_lock );
420 libvlc_global_data_t *p_libvlc_global = vlc_global();
423 vlc_mutex_lock( &structure_lock );
425 /* Wooohaa! If *this* fails, we're in serious trouble! Anyway it's
426 * useless to try and recover anything if pp_objects gets smashed. */
427 i_index = FindIndex( p_this, p_libvlc_global->pp_objects,
428 p_libvlc_global->i_objects );
429 REMOVE_ELEM( p_libvlc_global->pp_objects,
430 p_libvlc_global->i_objects, i_index );
432 vlc_mutex_unlock( &structure_lock );
435 #if defined(WIN32) || defined(UNDER_CE)
436 /* if object has an associated thread, close it now */
437 if( p_priv->thread_id.hThread )
438 CloseHandle(p_priv->thread_id.hThread);
441 vlc_mutex_destroy( &p_this->object_lock );
442 vlc_cond_destroy( &p_this->object_wait );
444 /* global is not dynamically allocated by vlc_object_create */
445 if( p_this->i_object_type != VLC_OBJECT_GLOBAL )
450 void __vlc_object_kill( vlc_object_t *p_this )
452 vlc_mutex_lock( &p_this->object_lock );
453 p_this->b_die = VLC_TRUE;
454 vlc_mutex_unlock( &p_this->object_lock );
458 vlc_bool_t __vlc_object_dying_unlocked( vlc_object_t *p_this )
460 vlc_assert_locked( &p_this->object_lock );
461 return p_this->b_die;
465 vlc_bool_t __vlc_object_dying( vlc_object_t *p_this )
468 vlc_mutex_lock( &p_this->object_lock );
469 b = __vlc_object_dying_unlocked( p_this );
470 vlc_mutex_unlock( &p_this->object_lock );
476 * find an object given its ID
478 * This function looks for the object whose i_object_id field is i_id. We
479 * use a dichotomy so that lookups are in log2(n).
480 *****************************************************************************/
481 void * __vlc_object_get( vlc_object_t *p_this, int i_id )
484 vlc_object_t **pp_objects;
485 libvlc_global_data_t *p_libvlc_global = vlc_global();
487 vlc_mutex_lock( &structure_lock );
489 pp_objects = p_libvlc_global->pp_objects;
491 /* Perform our dichotomy */
492 for( i_max = p_libvlc_global->i_objects - 1 ; ; )
494 i_middle = i_max / 2;
496 if( pp_objects[i_middle]->i_object_id > i_id )
500 else if( pp_objects[i_middle]->i_object_id < i_id )
504 pp_objects += i_middle;
509 /* This happens when there are only two remaining objects */
510 if( pp_objects[i_middle+1]->i_object_id == i_id )
512 vlc_mutex_unlock( &structure_lock );
513 pp_objects[i_middle+1]->i_refcount++;
514 return pp_objects[i_middle+1];
521 vlc_mutex_unlock( &structure_lock );
522 pp_objects[i_middle]->i_refcount++;
523 return pp_objects[i_middle];
528 /* this means that i_max == i_middle, and since we have already
529 * tested pp_objects[i_middle]), p_found is properly set. */
534 vlc_mutex_unlock( &structure_lock );
539 ****************************************************************************
540 * find a typed object and increment its refcount
541 *****************************************************************************
542 * This function recursively looks for a given object type. i_mode can be one
543 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
544 *****************************************************************************/
545 void * __vlc_object_find( vlc_object_t *p_this, int i_type, int i_mode )
547 vlc_object_t *p_found;
549 vlc_mutex_lock( &structure_lock );
551 /* If we are of the requested type ourselves, don't look further */
552 if( !(i_mode & FIND_STRICT) && p_this->i_object_type == i_type )
554 p_this->i_refcount++;
555 vlc_mutex_unlock( &structure_lock );
559 /* Otherwise, recursively look for the object */
560 if( (i_mode & 0x000f) == FIND_ANYWHERE )
562 vlc_object_t *p_root = p_this;
565 while( p_root->p_parent != NULL &&
566 p_root != VLC_OBJECT( p_this->p_libvlc ) )
568 p_root = p_root->p_parent;
571 p_found = FindObject( p_root, i_type, (i_mode & ~0x000f)|FIND_CHILD );
572 if( p_found == NULL && p_root != VLC_OBJECT( p_this->p_libvlc ) )
574 p_found = FindObject( VLC_OBJECT( p_this->p_libvlc ),
575 i_type, (i_mode & ~0x000f)|FIND_CHILD );
580 p_found = FindObject( p_this, i_type, i_mode );
583 vlc_mutex_unlock( &structure_lock );
589 ****************************************************************************
590 * find a named object and increment its refcount
591 *****************************************************************************
592 * This function recursively looks for a given object name. i_mode can be one
593 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
594 *****************************************************************************/
595 void * __vlc_object_find_name( vlc_object_t *p_this, const char *psz_name,
598 vlc_object_t *p_found;
600 vlc_mutex_lock( &structure_lock );
602 /* If have the requested name ourselves, don't look further */
603 if( !(i_mode & FIND_STRICT)
604 && p_this->psz_object_name
605 && !strcmp( p_this->psz_object_name, psz_name ) )
607 p_this->i_refcount++;
608 vlc_mutex_unlock( &structure_lock );
612 /* Otherwise, recursively look for the object */
613 if( (i_mode & 0x000f) == FIND_ANYWHERE )
615 vlc_object_t *p_root = p_this;
618 while( p_root->p_parent != NULL &&
619 p_root != VLC_OBJECT( p_this->p_libvlc ) )
621 p_root = p_root->p_parent;
624 p_found = FindObjectName( p_root, psz_name,
625 (i_mode & ~0x000f)|FIND_CHILD );
626 if( p_found == NULL && p_root != VLC_OBJECT( p_this->p_libvlc ) )
628 p_found = FindObjectName( VLC_OBJECT( p_this->p_libvlc ),
629 psz_name, (i_mode & ~0x000f)|FIND_CHILD );
634 p_found = FindObjectName( p_this, psz_name, i_mode );
637 vlc_mutex_unlock( &structure_lock );
643 ****************************************************************************
644 * increment an object refcount
645 *****************************************************************************/
646 void __vlc_object_yield( vlc_object_t *p_this )
648 vlc_mutex_lock( &structure_lock );
649 p_this->i_refcount++;
650 vlc_mutex_unlock( &structure_lock );
654 ****************************************************************************
655 * decrement an object refcount
656 *****************************************************************************/
657 void __vlc_object_release( vlc_object_t *p_this )
659 vlc_mutex_lock( &structure_lock );
660 p_this->i_refcount--;
661 vlc_mutex_unlock( &structure_lock );
665 ****************************************************************************
666 * attach object to a parent object
667 *****************************************************************************
668 * This function sets p_this as a child of p_parent, and p_parent as a parent
669 * of p_this. This link can be undone using vlc_object_detach.
670 *****************************************************************************/
671 void __vlc_object_attach( vlc_object_t *p_this, vlc_object_t *p_parent )
673 if( !p_this ) return;
675 vlc_mutex_lock( &structure_lock );
677 /* Attach the parent to its child */
678 p_this->p_parent = p_parent;
680 /* Attach the child to its parent */
681 INSERT_ELEM( p_parent->pp_children, p_parent->i_children,
682 p_parent->i_children, p_this );
684 /* Climb up the tree to see whether we are connected with the root */
685 if( p_parent->p_internals->b_attached )
687 SetAttachment( p_this, VLC_TRUE );
690 vlc_mutex_unlock( &structure_lock );
694 ****************************************************************************
695 * detach object from its parent
696 *****************************************************************************
697 * This function removes all links between an object and its parent.
698 *****************************************************************************/
699 void __vlc_object_detach( vlc_object_t *p_this )
701 if( !p_this ) return;
703 vlc_mutex_lock( &structure_lock );
704 if( !p_this->p_parent )
706 msg_Err( p_this, "object is not attached" );
707 vlc_mutex_unlock( &structure_lock );
711 /* Climb up the tree to see whether we are connected with the root */
712 if( p_this->p_parent->p_internals->b_attached )
714 SetAttachment( p_this, VLC_FALSE );
717 DetachObject( p_this );
718 vlc_mutex_unlock( &structure_lock );
723 ****************************************************************************
724 * find a list typed objects and increment their refcount
725 *****************************************************************************
726 * This function recursively looks for a given object type. i_mode can be one
727 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
728 *****************************************************************************/
729 vlc_list_t * __vlc_list_find( vlc_object_t *p_this, int i_type, int i_mode )
732 vlc_object_t **pp_current, **pp_end;
733 int i_count = 0, i_index = 0;
734 libvlc_global_data_t *p_libvlc_global = vlc_global();
736 vlc_mutex_lock( &structure_lock );
738 /* Look for the objects */
739 switch( i_mode & 0x000f )
742 pp_current = p_libvlc_global->pp_objects;
743 pp_end = pp_current + p_libvlc_global->i_objects;
745 for( ; pp_current < pp_end ; pp_current++ )
747 if( (*pp_current)->p_internals->b_attached
748 && (*pp_current)->i_object_type == i_type )
754 p_list = NewList( i_count );
755 pp_current = p_libvlc_global->pp_objects;
757 for( ; pp_current < pp_end ; pp_current++ )
759 if( (*pp_current)->p_internals->b_attached
760 && (*pp_current)->i_object_type == i_type )
762 ListReplace( p_list, *pp_current, i_index );
763 if( i_index < i_count ) i_index++;
769 i_count = CountChildren( p_this, i_type );
770 p_list = NewList( i_count );
772 /* Check allocation was successful */
773 if( p_list->i_count != i_count )
775 msg_Err( p_this, "list allocation failed!" );
781 ListChildren( p_list, p_this, i_type );
785 msg_Err( p_this, "unimplemented!" );
786 p_list = NewList( 0 );
790 vlc_mutex_unlock( &structure_lock );
795 /*****************************************************************************
796 * DumpCommand: print the current vlc structure
797 *****************************************************************************
798 * This function prints either an ASCII tree showing the connections between
799 * vlc objects, and additional information such as their refcount, thread ID,
800 * etc. (command "tree"), or the same data as a simple list (command "list").
801 *****************************************************************************/
802 static int DumpCommand( vlc_object_t *p_this, char const *psz_cmd,
803 vlc_value_t oldval, vlc_value_t newval, void *p_data )
805 libvlc_global_data_t *p_libvlc_global = vlc_global();
807 (void)oldval; (void)p_data;
808 if( *psz_cmd == 'l' )
810 vlc_mutex_lock( &structure_lock );
812 vlc_object_t **pp_current, **pp_end;
814 pp_current = p_libvlc_global->pp_objects;
815 pp_end = pp_current + p_libvlc_global->i_objects;
817 for( ; pp_current < pp_end ; pp_current++ )
819 if( (*pp_current)->p_internals->b_attached )
821 PrintObject( *pp_current, "" );
825 printf( " o %.8i %s (not attached)\n",
826 (*pp_current)->i_object_id,
827 (*pp_current)->psz_object_type );
831 vlc_mutex_unlock( &structure_lock );
835 vlc_object_t *p_object = NULL;
837 if( *newval.psz_string )
840 int i_id = strtol( newval.psz_string, &end, 0 );
841 if( end != newval.psz_string )
842 p_object = vlc_object_get( p_this, i_id );
845 /* try using the object's name to find it */
846 vlc_object_t *p_libvlc = vlc_object_get( p_this, 1 );
849 /* Look in p_libvlc's children tree */
850 p_object = vlc_object_find_name( p_libvlc,
853 vlc_object_release( p_libvlc );
857 /* If it's not in libvlc, look in libvlc_global (== p_this) */
858 p_object = vlc_object_find_name( p_this,
870 vlc_mutex_lock( &structure_lock );
872 if( *psz_cmd == 't' )
874 char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
877 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
880 DumpStructure( p_object, 0, psz_foo );
882 else if( *psz_cmd == 'v' )
887 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
889 PrintObject( p_object, "" );
891 if( !p_object->p_internals->i_vars )
892 printf( " `-o No variables\n" );
893 for( i = 0; i < p_object->p_internals->i_vars; i++ )
895 variable_t *p_var = p_object->p_internals->p_vars + i;
897 const char *psz_type = "unknown";
898 switch( p_var->i_type & VLC_VAR_TYPE )
900 #define MYCASE( type, nice ) \
901 case VLC_VAR_ ## type: \
904 MYCASE( VOID, "void" );
905 MYCASE( BOOL, "bool" );
906 MYCASE( INTEGER, "integer" );
907 MYCASE( HOTKEY, "hotkey" );
908 MYCASE( STRING, "string" );
909 MYCASE( MODULE, "module" );
910 MYCASE( FILE, "file" );
911 MYCASE( DIRECTORY, "directory" );
912 MYCASE( VARIABLE, "variable" );
913 MYCASE( FLOAT, "float" );
914 MYCASE( TIME, "time" );
915 MYCASE( ADDRESS, "address" );
916 MYCASE( MUTEX, "mutex" );
917 MYCASE( LIST, "list" );
920 printf( " %c-o \"%s\" (%s",
921 i + 1 == p_object->p_internals->i_vars ? '`' : '|',
922 p_var->psz_name, psz_type );
923 if( p_var->psz_text )
924 printf( ", %s", p_var->psz_text );
926 if( p_var->i_type & VLC_VAR_ISCOMMAND )
927 printf( ", command" );
928 if( p_var->i_entries )
929 printf( ", %d callbacks", p_var->i_entries );
930 switch( p_var->i_type & 0x00f0 )
936 printf( ": %s", p_var->val.b_bool ? "true" : "false" );
938 case VLC_VAR_INTEGER:
939 printf( ": %d", p_var->val.i_int );
942 printf( ": \"%s\"", p_var->val.psz_string );
945 printf( ": %f", p_var->val.f_float );
948 printf( ": " I64Fi, (int64_t)p_var->val.i_time );
950 case VLC_VAR_ADDRESS:
951 printf( ": %p", p_var->val.p_address );
961 vlc_mutex_unlock( &structure_lock );
963 if( *newval.psz_string )
965 vlc_object_release( p_object );
972 /*****************************************************************************
973 * vlc_list_release: free a list previously allocated by vlc_list_find
974 *****************************************************************************
975 * This function decreases the refcount of all objects in the list and
977 *****************************************************************************/
978 void vlc_list_release( vlc_list_t *p_list )
982 vlc_mutex_lock( &structure_lock );
983 for( i_index = 0; i_index < p_list->i_count; i_index++ )
985 p_list->p_values[i_index].p_object->i_refcount--;
987 vlc_mutex_unlock( &structure_lock );
989 free( p_list->p_values );
993 /* Following functions are local */
995 /*****************************************************************************
996 * FindIndex: find the index of an object in an array of objects
997 *****************************************************************************
998 * This function assumes that p_this can be found in pp_objects. It will not
999 * crash if p_this cannot be found, but will return a wrong value. It is your
1000 * duty to check the return value if you are not certain that the object could
1001 * be found for sure.
1002 *****************************************************************************/
1003 static int FindIndex( vlc_object_t *p_this,
1004 vlc_object_t **pp_objects, int i_count )
1006 int i_middle = i_count / 2;
1013 if( pp_objects[i_middle] == p_this )
1023 /* We take advantage of the sorted array */
1024 if( pp_objects[i_middle]->i_object_id < p_this->i_object_id )
1026 return i_middle + FindIndex( p_this, pp_objects + i_middle,
1027 i_count - i_middle );
1031 return FindIndex( p_this, pp_objects, i_middle );
1035 static vlc_object_t * FindObject( vlc_object_t *p_this, int i_type, int i_mode )
1038 vlc_object_t *p_tmp;
1040 switch( i_mode & 0x000f )
1043 p_tmp = p_this->p_parent;
1046 if( p_tmp->i_object_type == i_type )
1048 p_tmp->i_refcount++;
1053 return FindObject( p_tmp, i_type, i_mode );
1059 for( i = p_this->i_children; i--; )
1061 p_tmp = p_this->pp_children[i];
1062 if( p_tmp->i_object_type == i_type )
1064 p_tmp->i_refcount++;
1067 else if( p_tmp->i_children )
1069 p_tmp = FindObject( p_tmp, i_type, i_mode );
1079 /* Handled in vlc_object_find */
1086 static vlc_object_t * FindObjectName( vlc_object_t *p_this,
1087 const char *psz_name,
1091 vlc_object_t *p_tmp;
1093 switch( i_mode & 0x000f )
1096 p_tmp = p_this->p_parent;
1099 if( p_tmp->psz_object_name
1100 && !strcmp( p_tmp->psz_object_name, psz_name ) )
1102 p_tmp->i_refcount++;
1107 return FindObjectName( p_tmp, psz_name, i_mode );
1113 for( i = p_this->i_children; i--; )
1115 p_tmp = p_this->pp_children[i];
1116 if( p_tmp->psz_object_name
1117 && !strcmp( p_tmp->psz_object_name, psz_name ) )
1119 p_tmp->i_refcount++;
1122 else if( p_tmp->i_children )
1124 p_tmp = FindObjectName( p_tmp, psz_name, i_mode );
1134 /* Handled in vlc_object_find */
1141 static void DetachObject( vlc_object_t *p_this )
1143 vlc_object_t *p_parent = p_this->p_parent;
1146 /* Remove p_this's parent */
1147 p_this->p_parent = NULL;
1149 /* Remove all of p_parent's children which are p_this */
1150 for( i_index = p_parent->i_children ; i_index-- ; )
1152 if( p_parent->pp_children[i_index] == p_this )
1154 p_parent->i_children--;
1155 for( i = i_index ; i < p_parent->i_children ; i++ )
1157 p_parent->pp_children[i] = p_parent->pp_children[i+1];
1162 if( p_parent->i_children )
1164 p_parent->pp_children = (vlc_object_t **)realloc( p_parent->pp_children,
1165 p_parent->i_children * sizeof(vlc_object_t *) );
1169 free( p_parent->pp_children );
1170 p_parent->pp_children = NULL;
1174 /*****************************************************************************
1175 * SetAttachment: recursively set the b_attached flag of a subtree.
1176 *****************************************************************************
1177 * This function is used by the attach and detach functions to propagate
1178 * the b_attached flag in a subtree.
1179 *****************************************************************************/
1180 static void SetAttachment( vlc_object_t *p_this, vlc_bool_t b_attached )
1184 for( i_index = p_this->i_children ; i_index-- ; )
1186 SetAttachment( p_this->pp_children[i_index], b_attached );
1189 p_this->p_internals->b_attached = b_attached;
1192 static void PrintObject( vlc_object_t *p_this, const char *psz_prefix )
1194 char psz_children[20], psz_refcount[20], psz_thread[30], psz_name[50],
1198 if( p_this->psz_object_name )
1200 snprintf( psz_name, 49, " \"%s\"", p_this->psz_object_name );
1202 psz_name[48] = '\"';
1205 psz_children[0] = '\0';
1206 switch( p_this->i_children )
1211 strcpy( psz_children, ", 1 child" );
1214 snprintf( psz_children, 19, ", %i children", p_this->i_children );
1218 psz_refcount[0] = '\0';
1219 if( p_this->i_refcount )
1220 snprintf( psz_refcount, 19, ", refcount %i", p_this->i_refcount );
1222 psz_thread[0] = '\0';
1223 if( p_this->p_internals->b_thread )
1224 snprintf( psz_thread, 29, " (thread %u)",
1225 #if defined(WIN32) || defined(UNDER_CE)
1226 (unsigned)p_this->p_internals->thread_id.id );
1228 (unsigned)p_this->p_internals->thread_id );
1231 psz_parent[0] = '\0';
1232 if( p_this->p_parent )
1233 snprintf( psz_parent, 19, ", parent %i", p_this->p_parent->i_object_id );
1235 printf( " %so %.8i %s%s%s%s%s%s\n", psz_prefix,
1236 p_this->i_object_id, p_this->psz_object_type,
1237 psz_name, psz_thread, psz_refcount, psz_children,
1241 static void DumpStructure( vlc_object_t *p_this, int i_level, char *psz_foo )
1244 char i_back = psz_foo[i_level];
1245 psz_foo[i_level] = '\0';
1247 PrintObject( p_this, psz_foo );
1249 psz_foo[i_level] = i_back;
1251 if( i_level / 2 >= MAX_DUMPSTRUCTURE_DEPTH )
1253 msg_Warn( p_this, "structure tree is too deep" );
1257 for( i = 0 ; i < p_this->i_children ; i++ )
1261 psz_foo[i_level-1] = ' ';
1263 if( psz_foo[i_level-2] == '`' )
1265 psz_foo[i_level-2] = ' ';
1269 if( i == p_this->i_children - 1 )
1271 psz_foo[i_level] = '`';
1275 psz_foo[i_level] = '|';
1278 psz_foo[i_level+1] = '-';
1279 psz_foo[i_level+2] = '\0';
1281 DumpStructure( p_this->pp_children[i], i_level + 2, psz_foo );
1285 static vlc_list_t * NewList( int i_count )
1287 vlc_list_t * p_list = (vlc_list_t *)malloc( sizeof( vlc_list_t ) );
1288 if( p_list == NULL )
1293 p_list->i_count = i_count;
1297 p_list->p_values = NULL;
1301 p_list->p_values = malloc( i_count * sizeof( vlc_value_t ) );
1302 if( p_list->p_values == NULL )
1304 p_list->i_count = 0;
1311 static void ListReplace( vlc_list_t *p_list, vlc_object_t *p_object,
1314 if( p_list == NULL || i_index >= p_list->i_count )
1319 p_object->i_refcount++;
1321 p_list->p_values[i_index].p_object = p_object;
1326 /*static void ListAppend( vlc_list_t *p_list, vlc_object_t *p_object )
1328 if( p_list == NULL )
1333 p_list->p_values = realloc( p_list->p_values, (p_list->i_count + 1)
1334 * sizeof( vlc_value_t ) );
1335 if( p_list->p_values == NULL )
1337 p_list->i_count = 0;
1341 p_object->i_refcount++;
1343 p_list->p_values[p_list->i_count].p_object = p_object;
1349 static int CountChildren( vlc_object_t *p_this, int i_type )
1351 vlc_object_t *p_tmp;
1354 for( i = 0; i < p_this->i_children; i++ )
1356 p_tmp = p_this->pp_children[i];
1358 if( p_tmp->i_object_type == i_type )
1363 if( p_tmp->i_children )
1365 i_count += CountChildren( p_tmp, i_type );
1372 static void ListChildren( vlc_list_t *p_list, vlc_object_t *p_this, int i_type )
1374 vlc_object_t *p_tmp;
1377 for( i = 0; i < p_this->i_children; i++ )
1379 p_tmp = p_this->pp_children[i];
1381 if( p_tmp->i_object_type == i_type )
1383 ListReplace( p_list, p_tmp, p_list->i_count++ );
1386 if( p_tmp->i_children )
1388 ListChildren( p_list, p_tmp, i_type );