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 );
505 * Waits for the object to be signaled (using vlc_object_signal()).
506 * If the object already has a signal pending, this function will return
507 * immediately. It is asserted that the caller holds the object lock.
509 * @return true if the object is dying and should terminate.
511 vlc_bool_t __vlc_object_wait( vlc_object_t *obj )
513 vlc_assert_locked( &obj->object_lock );
515 int fd = obj->p_internals->pipes[0];
518 if( read( fd, &(char){ 0 }, 1 ) == 0 )
521 obj->p_internals->pipes[1] = -1;
526 vlc_cond_wait( &obj->object_wait, &obj->object_lock );
532 * Waits for the object to be signaled (using vlc_object_signal()), or for
534 * If the object already has a signal pending, this function will return
535 * immediately. It is asserted that the caller holds the object lock.
537 * @return negative if the object is dying and should terminate,
538 * positive if the the object has been signaled but is not dying,
539 * 0 if timeout has been reached.
541 int __vlc_object_timedwait( vlc_object_t *obj, mtime_t deadline )
545 vlc_assert_locked( &obj->object_lock );
546 v = vlc_cond_timedwait( &obj->object_wait, &obj->object_lock, deadline );
547 if( v == 0 ) /* signaled */
548 return obj->b_die ? -1 : 1;
554 * Checks whether an object has been "killed".
555 * The object lock must be held.
557 * Typical code for an object thread could be:
559 vlc_object_lock (self);
561 while (vlc_object_alive (self))
565 if (vlc_object_wait (self))
570 ...deinitialization...
571 vlc_object_unlock (self);
574 * @return true iff the object has not been killed yet
576 vlc_bool_t __vlc_object_alive( vlc_object_t *obj )
578 vlc_assert_locked( &obj->object_lock );
584 * Signals an object for which the lock is held.
586 void __vlc_object_signal_unlocked( vlc_object_t *obj )
588 vlc_assert_locked( &obj->object_lock );
590 int fd = obj->p_internals->pipes[1];
592 while( write( fd, &(char){ 0 }, 1 ) < 0 );
594 vlc_cond_signal( &obj->object_wait );
599 * Requests termination of an object.
600 * If the object is LibVLC, also request to terminate all its children.
602 void __vlc_object_kill( vlc_object_t *p_this )
604 vlc_mutex_lock( &p_this->object_lock );
605 p_this->b_die = VLC_TRUE;
607 if( p_this->i_object_type == VLC_OBJECT_LIBVLC )
608 for( int i = 0; i < p_this->i_children ; i++ )
609 vlc_object_kill( p_this->pp_children[i] );
611 int fd = p_this->p_internals->pipes[1];
614 close( fd ); /* closing a pipe makes it readable too */
615 p_this->p_internals->pipes[1] = -1;
618 vlc_object_signal_unlocked( p_this );
619 vlc_mutex_unlock( &p_this->object_lock );
624 * find an object given its ID
626 * This function looks for the object whose i_object_id field is i_id. We
627 * use a dichotomy so that lookups are in log2(n).
628 *****************************************************************************/
629 void * __vlc_object_get( vlc_object_t *p_this, int i_id )
632 vlc_object_t **pp_objects;
633 libvlc_global_data_t *p_libvlc_global = vlc_global();
635 vlc_mutex_lock( &structure_lock );
637 pp_objects = p_libvlc_global->pp_objects;
639 /* Perform our dichotomy */
640 for( i_max = p_libvlc_global->i_objects - 1 ; ; )
642 i_middle = i_max / 2;
644 if( pp_objects[i_middle]->i_object_id > i_id )
648 else if( pp_objects[i_middle]->i_object_id < i_id )
652 pp_objects += i_middle;
657 /* This happens when there are only two remaining objects */
658 if( pp_objects[i_middle+1]->i_object_id == i_id )
660 vlc_mutex_unlock( &structure_lock );
661 pp_objects[i_middle+1]->p_internals->i_refcount++;
662 return pp_objects[i_middle+1];
669 vlc_mutex_unlock( &structure_lock );
670 pp_objects[i_middle]->p_internals->i_refcount++;
671 return pp_objects[i_middle];
676 /* this means that i_max == i_middle, and since we have already
677 * tested pp_objects[i_middle]), p_found is properly set. */
682 vlc_mutex_unlock( &structure_lock );
687 ****************************************************************************
688 * find a typed object and increment its refcount
689 *****************************************************************************
690 * This function recursively looks for a given object type. i_mode can be one
691 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
692 *****************************************************************************/
693 void * __vlc_object_find( vlc_object_t *p_this, int i_type, int i_mode )
695 vlc_object_t *p_found;
697 vlc_mutex_lock( &structure_lock );
699 /* If we are of the requested type ourselves, don't look further */
700 if( !(i_mode & FIND_STRICT) && p_this->i_object_type == i_type )
702 p_this->p_internals->i_refcount++;
703 vlc_mutex_unlock( &structure_lock );
707 /* Otherwise, recursively look for the object */
708 if( (i_mode & 0x000f) == FIND_ANYWHERE )
710 vlc_object_t *p_root = p_this;
713 while( p_root->p_parent != NULL &&
714 p_root != VLC_OBJECT( p_this->p_libvlc ) )
716 p_root = p_root->p_parent;
719 p_found = FindObject( p_root, i_type, (i_mode & ~0x000f)|FIND_CHILD );
720 if( p_found == NULL && p_root != VLC_OBJECT( p_this->p_libvlc ) )
722 p_found = FindObject( VLC_OBJECT( p_this->p_libvlc ),
723 i_type, (i_mode & ~0x000f)|FIND_CHILD );
728 p_found = FindObject( p_this, i_type, i_mode );
731 vlc_mutex_unlock( &structure_lock );
737 ****************************************************************************
738 * find a named object and increment its refcount
739 *****************************************************************************
740 * This function recursively looks for a given object name. i_mode can be one
741 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
742 *****************************************************************************/
743 void * __vlc_object_find_name( vlc_object_t *p_this, const char *psz_name,
746 vlc_object_t *p_found;
748 vlc_mutex_lock( &structure_lock );
750 /* If have the requested name ourselves, don't look further */
751 if( !(i_mode & FIND_STRICT)
752 && p_this->psz_object_name
753 && !strcmp( p_this->psz_object_name, psz_name ) )
755 p_this->p_internals->i_refcount++;
756 vlc_mutex_unlock( &structure_lock );
760 /* Otherwise, recursively look for the object */
761 if( (i_mode & 0x000f) == FIND_ANYWHERE )
763 vlc_object_t *p_root = p_this;
766 while( p_root->p_parent != NULL &&
767 p_root != VLC_OBJECT( p_this->p_libvlc ) )
769 p_root = p_root->p_parent;
772 p_found = FindObjectName( p_root, psz_name,
773 (i_mode & ~0x000f)|FIND_CHILD );
774 if( p_found == NULL && p_root != VLC_OBJECT( p_this->p_libvlc ) )
776 p_found = FindObjectName( VLC_OBJECT( p_this->p_libvlc ),
777 psz_name, (i_mode & ~0x000f)|FIND_CHILD );
782 p_found = FindObjectName( p_this, psz_name, i_mode );
785 vlc_mutex_unlock( &structure_lock );
791 ****************************************************************************
792 * increment an object refcount
793 *****************************************************************************/
794 void __vlc_object_yield( vlc_object_t *p_this )
796 vlc_mutex_lock( &structure_lock );
797 p_this->p_internals->i_refcount++;
798 vlc_mutex_unlock( &structure_lock );
801 static inline void Release( vlc_object_t *obj )
803 assert( obj->p_internals->i_refcount > 0 );
804 obj->p_internals->i_refcount--;
807 /*****************************************************************************
808 * decrement an object refcount
809 *****************************************************************************/
810 void __vlc_object_release( vlc_object_t *p_this )
812 vlc_mutex_lock( &structure_lock );
814 vlc_mutex_unlock( &structure_lock );
818 ****************************************************************************
819 * attach object to a parent object
820 *****************************************************************************
821 * This function sets p_this as a child of p_parent, and p_parent as a parent
822 * of p_this. This link can be undone using vlc_object_detach.
823 *****************************************************************************/
824 void __vlc_object_attach( vlc_object_t *p_this, vlc_object_t *p_parent )
826 if( !p_this ) return;
828 vlc_mutex_lock( &structure_lock );
830 /* Attach the parent to its child */
831 p_this->p_parent = p_parent;
833 /* Attach the child to its parent */
834 INSERT_ELEM( p_parent->pp_children, p_parent->i_children,
835 p_parent->i_children, p_this );
837 /* Climb up the tree to see whether we are connected with the root */
838 if( p_parent->p_internals->b_attached )
840 SetAttachment( p_this, VLC_TRUE );
843 vlc_mutex_unlock( &structure_lock );
847 ****************************************************************************
848 * detach object from its parent
849 *****************************************************************************
850 * This function removes all links between an object and its parent.
851 *****************************************************************************/
852 void __vlc_object_detach( vlc_object_t *p_this )
854 if( !p_this ) return;
856 vlc_mutex_lock( &structure_lock );
857 if( !p_this->p_parent )
859 msg_Err( p_this, "object is not attached" );
860 vlc_mutex_unlock( &structure_lock );
864 /* Climb up the tree to see whether we are connected with the root */
865 if( p_this->p_parent->p_internals->b_attached )
867 SetAttachment( p_this, VLC_FALSE );
870 DetachObject( p_this );
871 vlc_mutex_unlock( &structure_lock );
876 ****************************************************************************
877 * find a list typed objects and increment their refcount
878 *****************************************************************************
879 * This function recursively looks for a given object type. i_mode can be one
880 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
881 *****************************************************************************/
882 vlc_list_t * __vlc_list_find( vlc_object_t *p_this, int i_type, int i_mode )
885 vlc_object_t **pp_current, **pp_end;
886 int i_count = 0, i_index = 0;
887 libvlc_global_data_t *p_libvlc_global = vlc_global();
889 vlc_mutex_lock( &structure_lock );
891 /* Look for the objects */
892 switch( i_mode & 0x000f )
895 pp_current = p_libvlc_global->pp_objects;
896 pp_end = pp_current + p_libvlc_global->i_objects;
898 for( ; pp_current < pp_end ; pp_current++ )
900 if( (*pp_current)->p_internals->b_attached
901 && (*pp_current)->i_object_type == i_type )
907 p_list = NewList( i_count );
908 pp_current = p_libvlc_global->pp_objects;
910 for( ; pp_current < pp_end ; pp_current++ )
912 if( (*pp_current)->p_internals->b_attached
913 && (*pp_current)->i_object_type == i_type )
915 ListReplace( p_list, *pp_current, i_index );
916 if( i_index < i_count ) i_index++;
922 i_count = CountChildren( p_this, i_type );
923 p_list = NewList( i_count );
925 /* Check allocation was successful */
926 if( p_list->i_count != i_count )
928 msg_Err( p_this, "list allocation failed!" );
934 ListChildren( p_list, p_this, i_type );
938 msg_Err( p_this, "unimplemented!" );
939 p_list = NewList( 0 );
943 vlc_mutex_unlock( &structure_lock );
948 /*****************************************************************************
949 * DumpCommand: print the current vlc structure
950 *****************************************************************************
951 * This function prints either an ASCII tree showing the connections between
952 * vlc objects, and additional information such as their refcount, thread ID,
953 * etc. (command "tree"), or the same data as a simple list (command "list").
954 *****************************************************************************/
955 static int DumpCommand( vlc_object_t *p_this, char const *psz_cmd,
956 vlc_value_t oldval, vlc_value_t newval, void *p_data )
958 libvlc_global_data_t *p_libvlc_global = vlc_global();
960 (void)oldval; (void)p_data;
961 if( *psz_cmd == 'l' )
963 vlc_mutex_lock( &structure_lock );
965 vlc_object_t **pp_current, **pp_end;
967 pp_current = p_libvlc_global->pp_objects;
968 pp_end = pp_current + p_libvlc_global->i_objects;
970 for( ; pp_current < pp_end ; pp_current++ )
972 if( (*pp_current)->p_internals->b_attached )
974 PrintObject( *pp_current, "" );
978 printf( " o %.8i %s (not attached)\n",
979 (*pp_current)->i_object_id,
980 (*pp_current)->psz_object_type );
984 vlc_mutex_unlock( &structure_lock );
988 vlc_object_t *p_object = NULL;
990 if( *newval.psz_string )
993 int i_id = strtol( newval.psz_string, &end, 0 );
994 if( end != newval.psz_string )
995 p_object = vlc_object_get( p_this, i_id );
998 /* try using the object's name to find it */
999 vlc_object_t *p_libvlc = vlc_object_get( p_this, 1 );
1002 /* Look in p_libvlc's children tree */
1003 p_object = vlc_object_find_name( p_libvlc,
1006 vlc_object_release( p_libvlc );
1010 /* If it's not in libvlc, look in libvlc_global (== p_this) */
1011 p_object = vlc_object_find_name( p_this,
1023 vlc_mutex_lock( &structure_lock );
1025 if( *psz_cmd == 't' )
1027 char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
1030 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
1033 DumpStructure( p_object, 0, psz_foo );
1035 else if( *psz_cmd == 'v' )
1040 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
1042 PrintObject( p_object, "" );
1044 if( !p_object->p_internals->i_vars )
1045 printf( " `-o No variables\n" );
1046 for( i = 0; i < p_object->p_internals->i_vars; i++ )
1048 variable_t *p_var = p_object->p_internals->p_vars + i;
1050 const char *psz_type = "unknown";
1051 switch( p_var->i_type & VLC_VAR_TYPE )
1053 #define MYCASE( type, nice ) \
1054 case VLC_VAR_ ## type: \
1057 MYCASE( VOID, "void" );
1058 MYCASE( BOOL, "bool" );
1059 MYCASE( INTEGER, "integer" );
1060 MYCASE( HOTKEY, "hotkey" );
1061 MYCASE( STRING, "string" );
1062 MYCASE( MODULE, "module" );
1063 MYCASE( FILE, "file" );
1064 MYCASE( DIRECTORY, "directory" );
1065 MYCASE( VARIABLE, "variable" );
1066 MYCASE( FLOAT, "float" );
1067 MYCASE( TIME, "time" );
1068 MYCASE( ADDRESS, "address" );
1069 MYCASE( MUTEX, "mutex" );
1070 MYCASE( LIST, "list" );
1073 printf( " %c-o \"%s\" (%s",
1074 i + 1 == p_object->p_internals->i_vars ? '`' : '|',
1075 p_var->psz_name, psz_type );
1076 if( p_var->psz_text )
1077 printf( ", %s", p_var->psz_text );
1079 if( p_var->i_type & VLC_VAR_ISCOMMAND )
1080 printf( ", command" );
1081 if( p_var->i_entries )
1082 printf( ", %d callbacks", p_var->i_entries );
1083 switch( p_var->i_type & 0x00f0 )
1089 printf( ": %s", p_var->val.b_bool ? "true" : "false" );
1091 case VLC_VAR_INTEGER:
1092 printf( ": %d", p_var->val.i_int );
1094 case VLC_VAR_STRING:
1095 printf( ": \"%s\"", p_var->val.psz_string );
1098 printf( ": %f", p_var->val.f_float );
1101 printf( ": " I64Fi, (int64_t)p_var->val.i_time );
1103 case VLC_VAR_ADDRESS:
1104 printf( ": %p", p_var->val.p_address );
1114 vlc_mutex_unlock( &structure_lock );
1116 if( *newval.psz_string )
1118 vlc_object_release( p_object );
1125 /*****************************************************************************
1126 * vlc_list_release: free a list previously allocated by vlc_list_find
1127 *****************************************************************************
1128 * This function decreases the refcount of all objects in the list and
1130 *****************************************************************************/
1131 void vlc_list_release( vlc_list_t *p_list )
1135 vlc_mutex_lock( &structure_lock );
1136 for( i_index = 0; i_index < p_list->i_count; i_index++ )
1138 Release( p_list->p_values[i_index].p_object );
1140 vlc_mutex_unlock( &structure_lock );
1142 free( p_list->p_values );
1146 /* Following functions are local */
1148 /*****************************************************************************
1149 * FindIndex: find the index of an object in an array of objects
1150 *****************************************************************************
1151 * This function assumes that p_this can be found in pp_objects. It will not
1152 * crash if p_this cannot be found, but will return a wrong value. It is your
1153 * duty to check the return value if you are not certain that the object could
1154 * be found for sure.
1155 *****************************************************************************/
1156 static int FindIndex( vlc_object_t *p_this,
1157 vlc_object_t **pp_objects, int i_count )
1159 int i_middle = i_count / 2;
1166 if( pp_objects[i_middle] == p_this )
1176 /* We take advantage of the sorted array */
1177 if( pp_objects[i_middle]->i_object_id < p_this->i_object_id )
1179 return i_middle + FindIndex( p_this, pp_objects + i_middle,
1180 i_count - i_middle );
1184 return FindIndex( p_this, pp_objects, i_middle );
1188 static vlc_object_t * FindObject( vlc_object_t *p_this, int i_type, int i_mode )
1191 vlc_object_t *p_tmp;
1193 switch( i_mode & 0x000f )
1196 p_tmp = p_this->p_parent;
1199 if( p_tmp->i_object_type == i_type )
1201 p_tmp->p_internals->i_refcount++;
1206 return FindObject( p_tmp, i_type, i_mode );
1212 for( i = p_this->i_children; i--; )
1214 p_tmp = p_this->pp_children[i];
1215 if( p_tmp->i_object_type == i_type )
1217 p_tmp->p_internals->i_refcount++;
1220 else if( p_tmp->i_children )
1222 p_tmp = FindObject( p_tmp, i_type, i_mode );
1232 /* Handled in vlc_object_find */
1239 static vlc_object_t * FindObjectName( vlc_object_t *p_this,
1240 const char *psz_name,
1244 vlc_object_t *p_tmp;
1246 switch( i_mode & 0x000f )
1249 p_tmp = p_this->p_parent;
1252 if( p_tmp->psz_object_name
1253 && !strcmp( p_tmp->psz_object_name, psz_name ) )
1255 p_tmp->p_internals->i_refcount++;
1260 return FindObjectName( p_tmp, psz_name, i_mode );
1266 for( i = p_this->i_children; i--; )
1268 p_tmp = p_this->pp_children[i];
1269 if( p_tmp->psz_object_name
1270 && !strcmp( p_tmp->psz_object_name, psz_name ) )
1272 p_tmp->p_internals->i_refcount++;
1275 else if( p_tmp->i_children )
1277 p_tmp = FindObjectName( p_tmp, psz_name, i_mode );
1287 /* Handled in vlc_object_find */
1294 static void DetachObject( vlc_object_t *p_this )
1296 vlc_object_t *p_parent = p_this->p_parent;
1299 /* Remove p_this's parent */
1300 p_this->p_parent = NULL;
1302 /* Remove all of p_parent's children which are p_this */
1303 for( i_index = p_parent->i_children ; i_index-- ; )
1305 if( p_parent->pp_children[i_index] == p_this )
1307 p_parent->i_children--;
1308 for( i = i_index ; i < p_parent->i_children ; i++ )
1310 p_parent->pp_children[i] = p_parent->pp_children[i+1];
1315 if( p_parent->i_children )
1317 p_parent->pp_children = (vlc_object_t **)realloc( p_parent->pp_children,
1318 p_parent->i_children * sizeof(vlc_object_t *) );
1322 free( p_parent->pp_children );
1323 p_parent->pp_children = NULL;
1327 /*****************************************************************************
1328 * SetAttachment: recursively set the b_attached flag of a subtree.
1329 *****************************************************************************
1330 * This function is used by the attach and detach functions to propagate
1331 * the b_attached flag in a subtree.
1332 *****************************************************************************/
1333 static void SetAttachment( vlc_object_t *p_this, vlc_bool_t b_attached )
1337 for( i_index = p_this->i_children ; i_index-- ; )
1339 SetAttachment( p_this->pp_children[i_index], b_attached );
1342 p_this->p_internals->b_attached = b_attached;
1345 static void PrintObject( vlc_object_t *p_this, const char *psz_prefix )
1347 char psz_children[20], psz_refcount[20], psz_thread[30], psz_name[50],
1351 if( p_this->psz_object_name )
1353 snprintf( psz_name, 49, " \"%s\"", p_this->psz_object_name );
1355 psz_name[48] = '\"';
1358 psz_children[0] = '\0';
1359 switch( p_this->i_children )
1364 strcpy( psz_children, ", 1 child" );
1367 snprintf( psz_children, 19, ", %i children", p_this->i_children );
1371 psz_refcount[0] = '\0';
1372 if( p_this->p_internals->i_refcount > 0 )
1373 snprintf( psz_refcount, 19, ", refcount %u",
1374 p_this->p_internals->i_refcount );
1376 psz_thread[0] = '\0';
1377 if( p_this->p_internals->b_thread )
1378 snprintf( psz_thread, 29, " (thread %u)",
1379 #if defined(WIN32) || defined(UNDER_CE)
1380 (unsigned)p_this->p_internals->thread_id.id );
1382 (unsigned)p_this->p_internals->thread_id );
1385 psz_parent[0] = '\0';
1386 if( p_this->p_parent )
1387 snprintf( psz_parent, 19, ", parent %i", p_this->p_parent->i_object_id );
1389 printf( " %so %.8i %s%s%s%s%s%s\n", psz_prefix,
1390 p_this->i_object_id, p_this->psz_object_type,
1391 psz_name, psz_thread, psz_refcount, psz_children,
1395 static void DumpStructure( vlc_object_t *p_this, int i_level, char *psz_foo )
1398 char i_back = psz_foo[i_level];
1399 psz_foo[i_level] = '\0';
1401 PrintObject( p_this, psz_foo );
1403 psz_foo[i_level] = i_back;
1405 if( i_level / 2 >= MAX_DUMPSTRUCTURE_DEPTH )
1407 msg_Warn( p_this, "structure tree is too deep" );
1411 for( i = 0 ; i < p_this->i_children ; i++ )
1415 psz_foo[i_level-1] = ' ';
1417 if( psz_foo[i_level-2] == '`' )
1419 psz_foo[i_level-2] = ' ';
1423 if( i == p_this->i_children - 1 )
1425 psz_foo[i_level] = '`';
1429 psz_foo[i_level] = '|';
1432 psz_foo[i_level+1] = '-';
1433 psz_foo[i_level+2] = '\0';
1435 DumpStructure( p_this->pp_children[i], i_level + 2, psz_foo );
1439 static vlc_list_t * NewList( int i_count )
1441 vlc_list_t * p_list = (vlc_list_t *)malloc( sizeof( vlc_list_t ) );
1442 if( p_list == NULL )
1447 p_list->i_count = i_count;
1451 p_list->p_values = NULL;
1455 p_list->p_values = malloc( i_count * sizeof( vlc_value_t ) );
1456 if( p_list->p_values == NULL )
1458 p_list->i_count = 0;
1465 static void ListReplace( vlc_list_t *p_list, vlc_object_t *p_object,
1468 if( p_list == NULL || i_index >= p_list->i_count )
1473 p_object->p_internals->i_refcount++;
1475 p_list->p_values[i_index].p_object = p_object;
1480 /*static void ListAppend( vlc_list_t *p_list, vlc_object_t *p_object )
1482 if( p_list == NULL )
1487 p_list->p_values = realloc( p_list->p_values, (p_list->i_count + 1)
1488 * sizeof( vlc_value_t ) );
1489 if( p_list->p_values == NULL )
1491 p_list->i_count = 0;
1495 p_object->p_internals->i_refcount++;
1497 p_list->p_values[p_list->i_count].p_object = p_object;
1503 static int CountChildren( vlc_object_t *p_this, int i_type )
1505 vlc_object_t *p_tmp;
1508 for( i = 0; i < p_this->i_children; i++ )
1510 p_tmp = p_this->pp_children[i];
1512 if( p_tmp->i_object_type == i_type )
1517 if( p_tmp->i_children )
1519 i_count += CountChildren( p_tmp, i_type );
1526 static void ListChildren( vlc_list_t *p_list, vlc_object_t *p_this, int i_type )
1528 vlc_object_t *p_tmp;
1531 for( i = 0; i < p_this->i_children; i++ )
1533 p_tmp = p_this->pp_children[i];
1535 if( p_tmp->i_object_type == i_type )
1537 ListReplace( p_list, p_tmp, p_list->i_count++ );
1540 if( p_tmp->i_children )
1542 ListChildren( p_list, p_tmp, i_type );