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 *****************************************************************************/
35 #include "../libvlc.h"
38 #include "audio_output/aout_internal.h"
40 #include <vlc_access.h>
41 #include <vlc_demux.h>
42 #include <vlc_stream.h>
45 #include "stream_output/stream_output.h"
47 #include "vlc_playlist.h"
48 #include "vlc_interface.h"
49 #include "vlc_codec.h"
50 #include "vlc_filter.h"
52 #include "vlc_httpd.h"
54 #include "input/vlm_internal.h"
61 #include "variables.h"
69 /*****************************************************************************
71 *****************************************************************************/
72 static int DumpCommand( vlc_object_t *, char const *,
73 vlc_value_t, vlc_value_t, void * );
75 static vlc_object_t * FindObject ( vlc_object_t *, int, int );
76 static vlc_object_t * FindObjectName( vlc_object_t *, const char *, int );
77 static void DetachObject ( vlc_object_t * );
78 static void PrintObject ( vlc_object_t *, const char * );
79 static void DumpStructure ( vlc_object_t *, int, char * );
80 static int FindIndex ( vlc_object_t *, vlc_object_t **, int );
81 static void SetAttachment ( vlc_object_t *, vlc_bool_t );
83 static vlc_list_t * NewList ( int );
84 static void ListReplace ( vlc_list_t *, vlc_object_t *, int );
85 /*static void ListAppend ( vlc_list_t *, vlc_object_t * );*/
86 static int CountChildren ( vlc_object_t *, int );
87 static void ListChildren ( vlc_list_t *, vlc_object_t *, int );
89 /*****************************************************************************
90 * Local structure lock
91 *****************************************************************************/
92 static vlc_mutex_t structure_lock;
93 static vlc_object_internals_t global_internals;
95 vlc_object_t *vlc_custom_create( vlc_object_t *p_this, size_t i_size,
96 int i_type, const char *psz_type )
99 vlc_object_internals_t *p_priv;
101 if( i_type == VLC_OBJECT_GLOBAL )
104 p_priv = &global_internals;
105 memset( p_priv, 0, sizeof( *p_priv ) );
109 p_priv = calloc( 1, sizeof( *p_priv ) + i_size );
113 p_new = (vlc_object_t *)(p_priv + 1);
116 p_new->p_internals = p_priv;
117 p_new->i_object_type = i_type;
118 p_new->psz_object_type = psz_type;
120 p_new->psz_object_name = NULL;
122 p_new->b_die = VLC_FALSE;
123 p_new->b_error = VLC_FALSE;
124 p_new->b_dead = VLC_FALSE;
125 p_priv->b_attached = VLC_FALSE;
126 p_new->b_force = VLC_FALSE;
128 p_new->psz_header = NULL;
130 if( p_this->i_flags & OBJECT_FLAGS_NODBG )
131 p_new->i_flags |= OBJECT_FLAGS_NODBG;
132 if( p_this->i_flags & OBJECT_FLAGS_QUIET )
133 p_new->i_flags |= OBJECT_FLAGS_QUIET;
134 if( p_this->i_flags & OBJECT_FLAGS_NOINTERACT )
135 p_new->i_flags |= OBJECT_FLAGS_NOINTERACT;
137 p_priv->p_vars = calloc( sizeof( variable_t ), 16 );
139 if( !p_priv->p_vars )
141 if( i_type != VLC_OBJECT_GLOBAL )
146 if( i_type == VLC_OBJECT_GLOBAL )
148 /* If i_type is global, then p_new is actually p_libvlc_global */
149 libvlc_global_data_t *p_libvlc_global = (libvlc_global_data_t *)p_new;
150 p_new->p_libvlc = NULL;
152 p_libvlc_global->i_counter = 0;
153 p_new->i_object_id = 0;
155 p_libvlc_global->i_objects = 1;
156 p_libvlc_global->pp_objects = malloc( sizeof(vlc_object_t *) );
157 p_libvlc_global->pp_objects[0] = p_new;
158 p_priv->b_attached = VLC_TRUE;
162 libvlc_global_data_t *p_libvlc_global = vlc_global();
163 if( i_type == VLC_OBJECT_LIBVLC )
165 p_new->p_libvlc = (libvlc_int_t*)p_new;
166 p_priv->b_attached = VLC_TRUE;
170 p_new->p_libvlc = p_this->p_libvlc;
173 vlc_mutex_lock( &structure_lock );
175 p_libvlc_global->i_counter++;
176 p_new->i_object_id = p_libvlc_global->i_counter;
178 /* Wooohaa! If *this* fails, we're in serious trouble! Anyway it's
179 * useless to try and recover anything if pp_objects gets smashed. */
180 TAB_APPEND( p_libvlc_global->i_objects, p_libvlc_global->pp_objects,
183 vlc_mutex_unlock( &structure_lock );
186 p_priv->i_refcount = 0;
187 p_new->p_parent = NULL;
188 p_new->pp_children = NULL;
189 p_new->i_children = 0;
191 p_new->p_private = NULL;
193 /* Initialize mutexes and condvars */
194 vlc_mutex_init( p_new, &p_new->object_lock );
195 vlc_cond_init( p_new, &p_new->object_wait );
196 vlc_mutex_init( p_new, &p_priv->var_lock );
197 p_priv->pipes[0] = p_priv->pipes[1] = -1;
199 if( i_type == VLC_OBJECT_GLOBAL )
201 vlc_mutex_init( p_new, &structure_lock );
204 if( i_type == VLC_OBJECT_LIBVLC )
206 var_Create( p_new, "list", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
207 var_AddCallback( p_new, "list", DumpCommand, NULL );
208 var_Create( p_new, "tree", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
209 var_AddCallback( p_new, "tree", DumpCommand, NULL );
210 var_Create( p_new, "vars", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
211 var_AddCallback( p_new, "vars", DumpCommand, NULL );
219 * Allocates and initializes a vlc object.
221 * @param i_type known object type (all of them are negative integer values),
222 * or object byte size (always positive).
224 * @return the new object, or NULL on error.
226 void * __vlc_object_create( vlc_object_t *p_this, int i_type )
228 const char * psz_type;
233 case VLC_OBJECT_GLOBAL:
234 i_size = sizeof(libvlc_global_data_t);
237 case VLC_OBJECT_LIBVLC:
238 i_size = sizeof(libvlc_int_t);
241 case VLC_OBJECT_INTF:
242 i_size = sizeof(intf_thread_t);
243 psz_type = "interface";
245 case VLC_OBJECT_DIALOGS:
246 i_size = sizeof(intf_thread_t);
247 psz_type = "dialogs";
249 case VLC_OBJECT_PLAYLIST:
250 i_size = sizeof(playlist_t);
251 psz_type = "playlist";
254 i_size = sizeof(services_discovery_t);
255 psz_type = "services discovery";
257 case VLC_OBJECT_INPUT:
258 i_size = sizeof(input_thread_t);
261 case VLC_OBJECT_DEMUX:
262 i_size = sizeof(demux_t);
265 case VLC_OBJECT_ACCESS:
266 i_size = sizeof(access_t);
269 case VLC_OBJECT_DECODER:
270 i_size = sizeof(decoder_t);
271 psz_type = "decoder";
273 case VLC_OBJECT_PACKETIZER:
274 i_size = sizeof(decoder_t);
275 psz_type = "packetizer";
277 case VLC_OBJECT_ENCODER:
278 i_size = sizeof(encoder_t);
279 psz_type = "encoder";
281 case VLC_OBJECT_FILTER:
282 i_size = sizeof(filter_t);
285 case VLC_OBJECT_VOUT:
286 i_size = sizeof(vout_thread_t);
287 psz_type = "video output";
290 i_size = sizeof(spu_t);
291 psz_type = "subpicture";
293 case VLC_OBJECT_AOUT:
294 i_size = sizeof(aout_instance_t);
295 psz_type = "audio output";
297 case VLC_OBJECT_SOUT:
298 i_size = sizeof(sout_instance_t);
299 psz_type = "stream output";
302 i_size = sizeof( vlm_t );
303 psz_type = "vlm dameon";
306 i_size = sizeof( vod_t );
307 psz_type = "vod server";
310 i_size = sizeof( xml_t );
313 case VLC_OBJECT_OPENGL:
314 i_size = sizeof( vout_thread_t );
317 case VLC_OBJECT_ANNOUNCE:
318 i_size = sizeof( announce_handler_t );
319 psz_type = "announce";
321 case VLC_OBJECT_META_ENGINE:
322 i_size = sizeof( meta_engine_t );
323 psz_type = "meta engine";
325 case VLC_OBJECT_OSDMENU:
326 i_size = sizeof( osd_menu_t );
327 psz_type = "osd menu";
330 i_size = i_type > (int)sizeof(vlc_object_t)
331 ? i_type : (int)sizeof(vlc_object_t);
332 i_type = VLC_OBJECT_GENERIC;
333 psz_type = "generic";
337 return vlc_custom_create( p_this, i_size, i_type, psz_type );
342 ****************************************************************************
343 * Destroy a vlc object
345 * This function destroys an object that has been previously allocated with
346 * vlc_object_create. The object's refcount must be zero and it must not be
347 * attached to other objects in any way.
348 *****************************************************************************/
349 void __vlc_object_destroy( vlc_object_t *p_this )
351 vlc_object_internals_t *p_priv = vlc_internals( p_this );
354 if( p_this->i_children )
356 msg_Err( p_this, "cannot delete object (%i, %s) with children" ,
357 p_this->i_object_id, p_this->psz_object_name );
361 if( p_this->p_parent )
363 msg_Err( p_this, "cannot delete object (%i, %s) with a parent",
364 p_this->i_object_id, p_this->psz_object_name );
368 while( p_priv->i_refcount > 0 )
372 /* Don't warn immediately ... 100ms seems OK */
376 "refcount is %u, delaying before deletion (id=%d,type=%d)",
377 p_priv->i_refcount, p_this->i_object_id,
378 p_this->i_object_type );
380 else if( i_delay == 10 )
383 "refcount is %u, delaying again (id=%d,type=%d)",
384 p_priv->i_refcount, p_this->i_object_id,
385 p_this->i_object_type );
387 else if( i_delay == 20 )
390 "waited too long, cancelling destruction (id=%d,type=%d)",
391 p_this->i_object_id, p_this->i_object_type );
398 /* Destroy the associated variables, starting from the end so that
399 * no memmove calls have to be done. */
400 while( p_priv->i_vars )
402 var_Destroy( p_this, p_priv->p_vars[p_priv->i_vars - 1].psz_name );
405 free( p_priv->p_vars );
406 vlc_mutex_destroy( &p_priv->var_lock );
408 if( p_this->psz_header ) free( p_this->psz_header );
410 if( p_this->i_object_type == VLC_OBJECT_GLOBAL )
412 libvlc_global_data_t *p_global = (libvlc_global_data_t *)p_this;
413 /* We are the global object ... no need to lock. */
414 free( p_global->pp_objects );
415 p_global->pp_objects = NULL;
416 p_global->i_objects--;
418 vlc_mutex_destroy( &structure_lock );
422 libvlc_global_data_t *p_libvlc_global = vlc_global();
425 vlc_mutex_lock( &structure_lock );
427 /* Wooohaa! If *this* fails, we're in serious trouble! Anyway it's
428 * useless to try and recover anything if pp_objects gets smashed. */
429 i_index = FindIndex( p_this, p_libvlc_global->pp_objects,
430 p_libvlc_global->i_objects );
431 REMOVE_ELEM( p_libvlc_global->pp_objects,
432 p_libvlc_global->i_objects, i_index );
434 vlc_mutex_unlock( &structure_lock );
437 #if defined(WIN32) || defined(UNDER_CE)
438 /* if object has an associated thread, close it now */
439 if( p_priv->thread_id.hThread )
440 CloseHandle(p_priv->thread_id.hThread);
443 vlc_mutex_destroy( &p_this->object_lock );
444 vlc_cond_destroy( &p_this->object_wait );
445 if( p_priv->pipes[0] != -1 )
446 close( p_priv->pipes[0] );
447 if( p_priv->pipes[1] != -1 )
448 close( p_priv->pipes[1] );
450 /* global is not dynamically allocated by vlc_object_create */
451 if( p_this->i_object_type != VLC_OBJECT_GLOBAL )
456 /** Inter-object signaling */
458 void __vlc_object_lock( vlc_object_t *obj )
460 vlc_mutex_lock( &obj->object_lock );
463 void __vlc_object_unlock( vlc_object_t *obj )
465 vlc_assert_locked( &obj->object_lock );
466 vlc_mutex_unlock( &obj->object_lock );
470 * Returns the readable end of a pipe that becomes readable whenever
471 * an object is signaled. This can be used to wait for VLC object events
472 * inside select(), poll() loops or frameworks providing an event loop.
474 * Note that the pipe will remain the same for the lifetime of the object.
475 * DO NOT close it yourself. Ever.
477 * DO NOT try to read from the pipe either: call vlc_object_wait() instead.
478 * Assuming the pipe is readable, vlc_object_wait() will not block.
479 * Also note that, as with vlc_object_wait(), there may be spurious wakeups.
481 * @param obj object that would be signaled (object lock MUST hold)
482 * @return a readable pipe descriptor, or -1 on error.
484 int vlc_object_waitpipe( vlc_object_t *obj )
486 int *pipes = obj->p_internals->pipes;
487 vlc_assert_locked( &obj->object_lock );
491 /* This can only ever happen if someone killed us without locking */
492 assert( pipes[0] == -1 );
497 if( _pipe( pipes, 1, _O_BINARY ) )
507 * Waits for the object to be signaled (using vlc_object_signal()).
508 * If the object already has a signal pending, this function will return
509 * immediately. It is asserted that the caller holds the object lock.
511 * @return true if the object is dying and should terminate.
513 vlc_bool_t __vlc_object_wait( vlc_object_t *obj )
515 vlc_assert_locked( &obj->object_lock );
517 int fd = obj->p_internals->pipes[0];
520 if( read( fd, &(char){ 0 }, 1 ) == 0 )
523 obj->p_internals->pipes[1] = -1;
528 vlc_cond_wait( &obj->object_wait, &obj->object_lock );
534 * Waits for the object to be signaled (using vlc_object_signal()), or for
536 * If the object already has a signal pending, this function will return
537 * immediately. It is asserted that the caller holds the object lock.
539 * @return negative if the object is dying and should terminate,
540 * positive if the the object has been signaled but is not dying,
541 * 0 if timeout has been reached.
543 int __vlc_object_timedwait( vlc_object_t *obj, mtime_t deadline )
547 vlc_assert_locked( &obj->object_lock );
548 v = vlc_cond_timedwait( &obj->object_wait, &obj->object_lock, deadline );
549 if( v == 0 ) /* signaled */
550 return obj->b_die ? -1 : 1;
556 * Checks whether an object has been "killed".
557 * The object lock must be held.
559 * Typical code for an object thread could be:
561 vlc_object_lock (self);
563 while (vlc_object_alive (self))
567 if (vlc_object_wait (self))
572 ...deinitialization...
573 vlc_object_unlock (self);
576 * @return true iff the object has not been killed yet
578 vlc_bool_t __vlc_object_alive( vlc_object_t *obj )
580 vlc_assert_locked( &obj->object_lock );
586 * Signals an object for which the lock is held.
588 void __vlc_object_signal_unlocked( vlc_object_t *obj )
590 vlc_assert_locked( &obj->object_lock );
592 int fd = obj->p_internals->pipes[1];
594 while( write( fd, &(char){ 0 }, 1 ) < 0 );
596 vlc_cond_signal( &obj->object_wait );
601 * Requests termination of an object.
602 * If the object is LibVLC, also request to terminate all its children.
604 void __vlc_object_kill( vlc_object_t *p_this )
606 vlc_mutex_lock( &p_this->object_lock );
607 p_this->b_die = VLC_TRUE;
609 if( p_this->i_object_type == VLC_OBJECT_LIBVLC )
610 for( int i = 0; i < p_this->i_children ; i++ )
611 vlc_object_kill( p_this->pp_children[i] );
613 int fd = p_this->p_internals->pipes[1];
616 close( fd ); /* closing a pipe makes it readable too */
617 p_this->p_internals->pipes[1] = -1;
620 vlc_object_signal_unlocked( p_this );
621 vlc_mutex_unlock( &p_this->object_lock );
626 * find an object given its ID
628 * This function looks for the object whose i_object_id field is i_id. We
629 * use a dichotomy so that lookups are in log2(n).
630 *****************************************************************************/
631 void * __vlc_object_get( vlc_object_t *p_this, int i_id )
634 vlc_object_t **pp_objects;
635 libvlc_global_data_t *p_libvlc_global = vlc_global();
637 vlc_mutex_lock( &structure_lock );
639 pp_objects = p_libvlc_global->pp_objects;
641 /* Perform our dichotomy */
642 for( i_max = p_libvlc_global->i_objects - 1 ; ; )
644 i_middle = i_max / 2;
646 if( pp_objects[i_middle]->i_object_id > i_id )
650 else if( pp_objects[i_middle]->i_object_id < i_id )
654 pp_objects += i_middle;
659 /* This happens when there are only two remaining objects */
660 if( pp_objects[i_middle+1]->i_object_id == i_id )
662 vlc_mutex_unlock( &structure_lock );
663 pp_objects[i_middle+1]->p_internals->i_refcount++;
664 return pp_objects[i_middle+1];
671 vlc_mutex_unlock( &structure_lock );
672 pp_objects[i_middle]->p_internals->i_refcount++;
673 return pp_objects[i_middle];
678 /* this means that i_max == i_middle, and since we have already
679 * tested pp_objects[i_middle]), p_found is properly set. */
684 vlc_mutex_unlock( &structure_lock );
689 ****************************************************************************
690 * find a typed object and increment its refcount
691 *****************************************************************************
692 * This function recursively looks for a given object type. i_mode can be one
693 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
694 *****************************************************************************/
695 void * __vlc_object_find( vlc_object_t *p_this, int i_type, int i_mode )
697 vlc_object_t *p_found;
699 vlc_mutex_lock( &structure_lock );
701 /* If we are of the requested type ourselves, don't look further */
702 if( !(i_mode & FIND_STRICT) && p_this->i_object_type == i_type )
704 p_this->p_internals->i_refcount++;
705 vlc_mutex_unlock( &structure_lock );
709 /* Otherwise, recursively look for the object */
710 if( (i_mode & 0x000f) == FIND_ANYWHERE )
712 vlc_object_t *p_root = p_this;
715 while( p_root->p_parent != NULL &&
716 p_root != VLC_OBJECT( p_this->p_libvlc ) )
718 p_root = p_root->p_parent;
721 p_found = FindObject( p_root, i_type, (i_mode & ~0x000f)|FIND_CHILD );
722 if( p_found == NULL && p_root != VLC_OBJECT( p_this->p_libvlc ) )
724 p_found = FindObject( VLC_OBJECT( p_this->p_libvlc ),
725 i_type, (i_mode & ~0x000f)|FIND_CHILD );
730 p_found = FindObject( p_this, i_type, i_mode );
733 vlc_mutex_unlock( &structure_lock );
739 ****************************************************************************
740 * find a named object and increment its refcount
741 *****************************************************************************
742 * This function recursively looks for a given object name. i_mode can be one
743 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
744 *****************************************************************************/
745 void * __vlc_object_find_name( vlc_object_t *p_this, const char *psz_name,
748 vlc_object_t *p_found;
750 vlc_mutex_lock( &structure_lock );
752 /* If have the requested name ourselves, don't look further */
753 if( !(i_mode & FIND_STRICT)
754 && p_this->psz_object_name
755 && !strcmp( p_this->psz_object_name, psz_name ) )
757 p_this->p_internals->i_refcount++;
758 vlc_mutex_unlock( &structure_lock );
762 /* Otherwise, recursively look for the object */
763 if( (i_mode & 0x000f) == FIND_ANYWHERE )
765 vlc_object_t *p_root = p_this;
768 while( p_root->p_parent != NULL &&
769 p_root != VLC_OBJECT( p_this->p_libvlc ) )
771 p_root = p_root->p_parent;
774 p_found = FindObjectName( p_root, psz_name,
775 (i_mode & ~0x000f)|FIND_CHILD );
776 if( p_found == NULL && p_root != VLC_OBJECT( p_this->p_libvlc ) )
778 p_found = FindObjectName( VLC_OBJECT( p_this->p_libvlc ),
779 psz_name, (i_mode & ~0x000f)|FIND_CHILD );
784 p_found = FindObjectName( p_this, psz_name, i_mode );
787 vlc_mutex_unlock( &structure_lock );
793 ****************************************************************************
794 * increment an object refcount
795 *****************************************************************************/
796 void __vlc_object_yield( vlc_object_t *p_this )
798 vlc_mutex_lock( &structure_lock );
799 p_this->p_internals->i_refcount++;
800 vlc_mutex_unlock( &structure_lock );
803 static inline void Release( vlc_object_t *obj )
805 assert( obj->p_internals->i_refcount > 0 );
806 obj->p_internals->i_refcount--;
809 /*****************************************************************************
810 * decrement an object refcount
811 *****************************************************************************/
812 void __vlc_object_release( vlc_object_t *p_this )
814 vlc_mutex_lock( &structure_lock );
816 vlc_mutex_unlock( &structure_lock );
820 ****************************************************************************
821 * attach object to a parent object
822 *****************************************************************************
823 * This function sets p_this as a child of p_parent, and p_parent as a parent
824 * of p_this. This link can be undone using vlc_object_detach.
825 *****************************************************************************/
826 void __vlc_object_attach( vlc_object_t *p_this, vlc_object_t *p_parent )
828 if( !p_this ) return;
830 vlc_mutex_lock( &structure_lock );
832 /* Attach the parent to its child */
833 p_this->p_parent = p_parent;
835 /* Attach the child to its parent */
836 INSERT_ELEM( p_parent->pp_children, p_parent->i_children,
837 p_parent->i_children, p_this );
839 /* Climb up the tree to see whether we are connected with the root */
840 if( p_parent->p_internals->b_attached )
842 SetAttachment( p_this, VLC_TRUE );
845 vlc_mutex_unlock( &structure_lock );
849 ****************************************************************************
850 * detach object from its parent
851 *****************************************************************************
852 * This function removes all links between an object and its parent.
853 *****************************************************************************/
854 void __vlc_object_detach( vlc_object_t *p_this )
856 if( !p_this ) return;
858 vlc_mutex_lock( &structure_lock );
859 if( !p_this->p_parent )
861 msg_Err( p_this, "object is not attached" );
862 vlc_mutex_unlock( &structure_lock );
866 /* Climb up the tree to see whether we are connected with the root */
867 if( p_this->p_parent->p_internals->b_attached )
869 SetAttachment( p_this, VLC_FALSE );
872 DetachObject( p_this );
873 vlc_mutex_unlock( &structure_lock );
878 ****************************************************************************
879 * find a list typed objects and increment their refcount
880 *****************************************************************************
881 * This function recursively looks for a given object type. i_mode can be one
882 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
883 *****************************************************************************/
884 vlc_list_t * __vlc_list_find( vlc_object_t *p_this, int i_type, int i_mode )
887 vlc_object_t **pp_current, **pp_end;
888 int i_count = 0, i_index = 0;
889 libvlc_global_data_t *p_libvlc_global = vlc_global();
891 vlc_mutex_lock( &structure_lock );
893 /* Look for the objects */
894 switch( i_mode & 0x000f )
897 pp_current = p_libvlc_global->pp_objects;
898 pp_end = pp_current + p_libvlc_global->i_objects;
900 for( ; pp_current < pp_end ; pp_current++ )
902 if( (*pp_current)->p_internals->b_attached
903 && (*pp_current)->i_object_type == i_type )
909 p_list = NewList( i_count );
910 pp_current = p_libvlc_global->pp_objects;
912 for( ; pp_current < pp_end ; pp_current++ )
914 if( (*pp_current)->p_internals->b_attached
915 && (*pp_current)->i_object_type == i_type )
917 ListReplace( p_list, *pp_current, i_index );
918 if( i_index < i_count ) i_index++;
924 i_count = CountChildren( p_this, i_type );
925 p_list = NewList( i_count );
927 /* Check allocation was successful */
928 if( p_list->i_count != i_count )
930 msg_Err( p_this, "list allocation failed!" );
936 ListChildren( p_list, p_this, i_type );
940 msg_Err( p_this, "unimplemented!" );
941 p_list = NewList( 0 );
945 vlc_mutex_unlock( &structure_lock );
950 /*****************************************************************************
951 * DumpCommand: print the current vlc structure
952 *****************************************************************************
953 * This function prints either an ASCII tree showing the connections between
954 * vlc objects, and additional information such as their refcount, thread ID,
955 * etc. (command "tree"), or the same data as a simple list (command "list").
956 *****************************************************************************/
957 static int DumpCommand( vlc_object_t *p_this, char const *psz_cmd,
958 vlc_value_t oldval, vlc_value_t newval, void *p_data )
960 libvlc_global_data_t *p_libvlc_global = vlc_global();
962 (void)oldval; (void)p_data;
963 if( *psz_cmd == 'l' )
965 vlc_mutex_lock( &structure_lock );
967 vlc_object_t **pp_current, **pp_end;
969 pp_current = p_libvlc_global->pp_objects;
970 pp_end = pp_current + p_libvlc_global->i_objects;
972 for( ; pp_current < pp_end ; pp_current++ )
974 if( (*pp_current)->p_internals->b_attached )
976 PrintObject( *pp_current, "" );
980 printf( " o %.8i %s (not attached)\n",
981 (*pp_current)->i_object_id,
982 (*pp_current)->psz_object_type );
986 vlc_mutex_unlock( &structure_lock );
990 vlc_object_t *p_object = NULL;
992 if( *newval.psz_string )
995 int i_id = strtol( newval.psz_string, &end, 0 );
996 if( end != newval.psz_string )
997 p_object = vlc_object_get( p_this, i_id );
1000 /* try using the object's name to find it */
1001 vlc_object_t *p_libvlc = vlc_object_get( p_this, 1 );
1004 /* Look in p_libvlc's children tree */
1005 p_object = vlc_object_find_name( p_libvlc,
1008 vlc_object_release( p_libvlc );
1012 /* If it's not in libvlc, look in libvlc_global (== p_this) */
1013 p_object = vlc_object_find_name( p_this,
1025 vlc_mutex_lock( &structure_lock );
1027 if( *psz_cmd == 't' )
1029 char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
1032 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
1035 DumpStructure( p_object, 0, psz_foo );
1037 else if( *psz_cmd == 'v' )
1042 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
1044 PrintObject( p_object, "" );
1046 if( !p_object->p_internals->i_vars )
1047 printf( " `-o No variables\n" );
1048 for( i = 0; i < p_object->p_internals->i_vars; i++ )
1050 variable_t *p_var = p_object->p_internals->p_vars + i;
1052 const char *psz_type = "unknown";
1053 switch( p_var->i_type & VLC_VAR_TYPE )
1055 #define MYCASE( type, nice ) \
1056 case VLC_VAR_ ## type: \
1059 MYCASE( VOID, "void" );
1060 MYCASE( BOOL, "bool" );
1061 MYCASE( INTEGER, "integer" );
1062 MYCASE( HOTKEY, "hotkey" );
1063 MYCASE( STRING, "string" );
1064 MYCASE( MODULE, "module" );
1065 MYCASE( FILE, "file" );
1066 MYCASE( DIRECTORY, "directory" );
1067 MYCASE( VARIABLE, "variable" );
1068 MYCASE( FLOAT, "float" );
1069 MYCASE( TIME, "time" );
1070 MYCASE( ADDRESS, "address" );
1071 MYCASE( MUTEX, "mutex" );
1072 MYCASE( LIST, "list" );
1075 printf( " %c-o \"%s\" (%s",
1076 i + 1 == p_object->p_internals->i_vars ? '`' : '|',
1077 p_var->psz_name, psz_type );
1078 if( p_var->psz_text )
1079 printf( ", %s", p_var->psz_text );
1081 if( p_var->i_type & VLC_VAR_ISCOMMAND )
1082 printf( ", command" );
1083 if( p_var->i_entries )
1084 printf( ", %d callbacks", p_var->i_entries );
1085 switch( p_var->i_type & 0x00f0 )
1091 printf( ": %s", p_var->val.b_bool ? "true" : "false" );
1093 case VLC_VAR_INTEGER:
1094 printf( ": %d", p_var->val.i_int );
1096 case VLC_VAR_STRING:
1097 printf( ": \"%s\"", p_var->val.psz_string );
1100 printf( ": %f", p_var->val.f_float );
1103 printf( ": " I64Fi, (int64_t)p_var->val.i_time );
1105 case VLC_VAR_ADDRESS:
1106 printf( ": %p", p_var->val.p_address );
1116 vlc_mutex_unlock( &structure_lock );
1118 if( *newval.psz_string )
1120 vlc_object_release( p_object );
1127 /*****************************************************************************
1128 * vlc_list_release: free a list previously allocated by vlc_list_find
1129 *****************************************************************************
1130 * This function decreases the refcount of all objects in the list and
1132 *****************************************************************************/
1133 void vlc_list_release( vlc_list_t *p_list )
1137 vlc_mutex_lock( &structure_lock );
1138 for( i_index = 0; i_index < p_list->i_count; i_index++ )
1140 Release( p_list->p_values[i_index].p_object );
1142 vlc_mutex_unlock( &structure_lock );
1144 free( p_list->p_values );
1148 /* Following functions are local */
1150 /*****************************************************************************
1151 * FindIndex: find the index of an object in an array of objects
1152 *****************************************************************************
1153 * This function assumes that p_this can be found in pp_objects. It will not
1154 * crash if p_this cannot be found, but will return a wrong value. It is your
1155 * duty to check the return value if you are not certain that the object could
1156 * be found for sure.
1157 *****************************************************************************/
1158 static int FindIndex( vlc_object_t *p_this,
1159 vlc_object_t **pp_objects, int i_count )
1161 int i_middle = i_count / 2;
1168 if( pp_objects[i_middle] == p_this )
1178 /* We take advantage of the sorted array */
1179 if( pp_objects[i_middle]->i_object_id < p_this->i_object_id )
1181 return i_middle + FindIndex( p_this, pp_objects + i_middle,
1182 i_count - i_middle );
1186 return FindIndex( p_this, pp_objects, i_middle );
1190 static vlc_object_t * FindObject( vlc_object_t *p_this, int i_type, int i_mode )
1193 vlc_object_t *p_tmp;
1195 switch( i_mode & 0x000f )
1198 p_tmp = p_this->p_parent;
1201 if( p_tmp->i_object_type == i_type )
1203 p_tmp->p_internals->i_refcount++;
1208 return FindObject( p_tmp, i_type, i_mode );
1214 for( i = p_this->i_children; i--; )
1216 p_tmp = p_this->pp_children[i];
1217 if( p_tmp->i_object_type == i_type )
1219 p_tmp->p_internals->i_refcount++;
1222 else if( p_tmp->i_children )
1224 p_tmp = FindObject( p_tmp, i_type, i_mode );
1234 /* Handled in vlc_object_find */
1241 static vlc_object_t * FindObjectName( vlc_object_t *p_this,
1242 const char *psz_name,
1246 vlc_object_t *p_tmp;
1248 switch( i_mode & 0x000f )
1251 p_tmp = p_this->p_parent;
1254 if( p_tmp->psz_object_name
1255 && !strcmp( p_tmp->psz_object_name, psz_name ) )
1257 p_tmp->p_internals->i_refcount++;
1262 return FindObjectName( p_tmp, psz_name, i_mode );
1268 for( i = p_this->i_children; i--; )
1270 p_tmp = p_this->pp_children[i];
1271 if( p_tmp->psz_object_name
1272 && !strcmp( p_tmp->psz_object_name, psz_name ) )
1274 p_tmp->p_internals->i_refcount++;
1277 else if( p_tmp->i_children )
1279 p_tmp = FindObjectName( p_tmp, psz_name, i_mode );
1289 /* Handled in vlc_object_find */
1296 static void DetachObject( vlc_object_t *p_this )
1298 vlc_object_t *p_parent = p_this->p_parent;
1301 /* Remove p_this's parent */
1302 p_this->p_parent = NULL;
1304 /* Remove all of p_parent's children which are p_this */
1305 for( i_index = p_parent->i_children ; i_index-- ; )
1307 if( p_parent->pp_children[i_index] == p_this )
1309 p_parent->i_children--;
1310 for( i = i_index ; i < p_parent->i_children ; i++ )
1312 p_parent->pp_children[i] = p_parent->pp_children[i+1];
1317 if( p_parent->i_children )
1319 p_parent->pp_children = (vlc_object_t **)realloc( p_parent->pp_children,
1320 p_parent->i_children * sizeof(vlc_object_t *) );
1324 free( p_parent->pp_children );
1325 p_parent->pp_children = NULL;
1329 /*****************************************************************************
1330 * SetAttachment: recursively set the b_attached flag of a subtree.
1331 *****************************************************************************
1332 * This function is used by the attach and detach functions to propagate
1333 * the b_attached flag in a subtree.
1334 *****************************************************************************/
1335 static void SetAttachment( vlc_object_t *p_this, vlc_bool_t b_attached )
1339 for( i_index = p_this->i_children ; i_index-- ; )
1341 SetAttachment( p_this->pp_children[i_index], b_attached );
1344 p_this->p_internals->b_attached = b_attached;
1347 static void PrintObject( vlc_object_t *p_this, const char *psz_prefix )
1349 char psz_children[20], psz_refcount[20], psz_thread[30], psz_name[50],
1353 if( p_this->psz_object_name )
1355 snprintf( psz_name, 49, " \"%s\"", p_this->psz_object_name );
1357 psz_name[48] = '\"';
1360 psz_children[0] = '\0';
1361 switch( p_this->i_children )
1366 strcpy( psz_children, ", 1 child" );
1369 snprintf( psz_children, 19, ", %i children", p_this->i_children );
1373 psz_refcount[0] = '\0';
1374 if( p_this->p_internals->i_refcount > 0 )
1375 snprintf( psz_refcount, 19, ", refcount %u",
1376 p_this->p_internals->i_refcount );
1378 psz_thread[0] = '\0';
1379 if( p_this->p_internals->b_thread )
1380 snprintf( psz_thread, 29, " (thread %u)",
1381 #if defined(WIN32) || defined(UNDER_CE)
1382 (unsigned)p_this->p_internals->thread_id.id );
1384 (unsigned)p_this->p_internals->thread_id );
1387 psz_parent[0] = '\0';
1388 if( p_this->p_parent )
1389 snprintf( psz_parent, 19, ", parent %i", p_this->p_parent->i_object_id );
1391 printf( " %so %.8i %s%s%s%s%s%s\n", psz_prefix,
1392 p_this->i_object_id, p_this->psz_object_type,
1393 psz_name, psz_thread, psz_refcount, psz_children,
1397 static void DumpStructure( vlc_object_t *p_this, int i_level, char *psz_foo )
1400 char i_back = psz_foo[i_level];
1401 psz_foo[i_level] = '\0';
1403 PrintObject( p_this, psz_foo );
1405 psz_foo[i_level] = i_back;
1407 if( i_level / 2 >= MAX_DUMPSTRUCTURE_DEPTH )
1409 msg_Warn( p_this, "structure tree is too deep" );
1413 for( i = 0 ; i < p_this->i_children ; i++ )
1417 psz_foo[i_level-1] = ' ';
1419 if( psz_foo[i_level-2] == '`' )
1421 psz_foo[i_level-2] = ' ';
1425 if( i == p_this->i_children - 1 )
1427 psz_foo[i_level] = '`';
1431 psz_foo[i_level] = '|';
1434 psz_foo[i_level+1] = '-';
1435 psz_foo[i_level+2] = '\0';
1437 DumpStructure( p_this->pp_children[i], i_level + 2, psz_foo );
1441 static vlc_list_t * NewList( int i_count )
1443 vlc_list_t * p_list = (vlc_list_t *)malloc( sizeof( vlc_list_t ) );
1444 if( p_list == NULL )
1449 p_list->i_count = i_count;
1453 p_list->p_values = NULL;
1457 p_list->p_values = malloc( i_count * sizeof( vlc_value_t ) );
1458 if( p_list->p_values == NULL )
1460 p_list->i_count = 0;
1467 static void ListReplace( vlc_list_t *p_list, vlc_object_t *p_object,
1470 if( p_list == NULL || i_index >= p_list->i_count )
1475 p_object->p_internals->i_refcount++;
1477 p_list->p_values[i_index].p_object = p_object;
1482 /*static void ListAppend( vlc_list_t *p_list, vlc_object_t *p_object )
1484 if( p_list == NULL )
1489 p_list->p_values = realloc( p_list->p_values, (p_list->i_count + 1)
1490 * sizeof( vlc_value_t ) );
1491 if( p_list->p_values == NULL )
1493 p_list->i_count = 0;
1497 p_object->p_internals->i_refcount++;
1499 p_list->p_values[p_list->i_count].p_object = p_object;
1505 static int CountChildren( vlc_object_t *p_this, int i_type )
1507 vlc_object_t *p_tmp;
1510 for( i = 0; i < p_this->i_children; i++ )
1512 p_tmp = p_this->pp_children[i];
1514 if( p_tmp->i_object_type == i_type )
1519 if( p_tmp->i_children )
1521 i_count += CountChildren( p_tmp, i_type );
1528 static void ListChildren( vlc_list_t *p_list, vlc_object_t *p_this, int i_type )
1530 vlc_object_t *p_tmp;
1533 for( i = 0; i < p_this->i_children; i++ )
1535 p_tmp = p_this->pp_children[i];
1537 if( p_tmp->i_object_type == i_type )
1539 ListReplace( p_list, p_tmp, p_list->i_count++ );
1542 if( p_tmp->i_children )
1544 ListChildren( p_list, p_tmp, i_type );