1 /*****************************************************************************
2 * objects.c: vlc_object_t handling
3 *****************************************************************************
4 * Copyright (C) 2004-2008 the VideoLAN team
6 * Authors: Samuel Hocevar <sam@zoy.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
25 * This file contains the functions to handle the vlc_object_t type
27 * Unless otherwise stated, functions in this file are not cancellation point.
28 * All functions in this file are safe w.r.t. deferred cancellation.
32 /*****************************************************************************
34 *****************************************************************************/
39 #include <vlc_common.h>
41 #include "../libvlc.h"
44 #include "audio_output/aout_internal.h"
46 #include <vlc_access.h>
47 #include <vlc_demux.h>
48 #include <vlc_stream.h>
51 #include "stream_output/stream_output.h"
53 #include "vlc_interface.h"
54 #include "vlc_codec.h"
55 #include "vlc_filter.h"
57 #include "variables.h"
63 # include <errno.h> /* ENOSYS */
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 PrintObject ( vlc_object_t *, const char * );
76 static void DumpStructure ( vlc_object_t *, int, char * );
78 static vlc_list_t * NewList ( int );
79 static void ListReplace ( vlc_list_t *, vlc_object_t *, int );
80 /*static void ListAppend ( vlc_list_t *, vlc_object_t * );*/
81 static int CountChildren ( vlc_object_t *, int );
82 static void ListChildren ( vlc_list_t *, vlc_object_t *, int );
84 static void vlc_object_destroy( vlc_object_t *p_this );
85 static void vlc_object_detach_unlocked (vlc_object_t *p_this);
86 static void vlc_object_dump( vlc_object_t *p_this );
88 /*****************************************************************************
89 * Local structure lock
90 *****************************************************************************/
91 static vlc_mutex_t structure_lock;
92 static unsigned object_counter = 0;
94 static void close_nocancel (int fd)
96 int canc = vlc_savecancel ();
98 vlc_restorecancel (canc);
101 void *__vlc_custom_create( vlc_object_t *p_this, size_t i_size,
102 int i_type, const char *psz_type )
105 vlc_object_internals_t *p_priv;
108 * VLC objects are laid out as follow:
109 * - first the LibVLC-private per-object data,
110 * - then VLC_COMMON members from vlc_object_t,
111 * - finally, the type-specific data (if any).
113 * This function initializes the LibVLC and common data,
114 * and zeroes the rest.
116 p_priv = calloc( 1, sizeof( *p_priv ) + i_size );
120 assert (i_size >= sizeof (vlc_object_t));
121 p_new = (vlc_object_t *)(p_priv + 1);
123 p_new->i_object_type = i_type;
124 p_new->psz_object_type = psz_type;
125 p_new->psz_object_name = NULL;
127 p_new->b_die = false;
128 p_new->b_error = false;
129 p_new->b_force = false;
131 p_new->psz_header = NULL;
134 p_new->i_flags = p_this->i_flags
135 & (OBJECT_FLAGS_NODBG|OBJECT_FLAGS_QUIET|OBJECT_FLAGS_NOINTERACT);
137 p_priv->p_vars = calloc( sizeof( variable_t ), 16 );
139 if( !p_priv->p_vars )
147 if( i_type == VLC_OBJECT_LIBVLC )
148 p_new->p_libvlc = (libvlc_int_t*)p_new;
151 p_new->p_libvlc = NULL;
152 vlc_mutex_init( &structure_lock );
155 p_this = p_priv->next = p_priv->prev = p_new;
158 p_new->p_libvlc = p_this->p_libvlc;
160 vlc_spin_init( &p_priv->ref_spin );
161 p_priv->i_refcount = 1;
162 p_priv->pf_destructor = NULL;
163 p_priv->b_thread = false;
164 p_new->p_parent = NULL;
165 p_priv->pp_children = NULL;
166 p_priv->i_children = 0;
168 p_new->p_private = NULL;
170 /* Initialize mutexes and condvars */
171 vlc_mutex_init( &p_priv->lock );
172 vlc_cond_init( &p_priv->wait );
173 vlc_mutex_init( &p_priv->var_lock );
174 vlc_spin_init( &p_priv->spin );
175 p_priv->pipes[0] = p_priv->pipes[1] = -1;
177 p_priv->next = p_this;
178 vlc_mutex_lock( &structure_lock );
179 p_priv->prev = vlc_internals (p_this)->prev;
180 vlc_internals (p_this)->prev = p_new;
181 vlc_internals (p_priv->prev)->next = p_new;
182 p_new->i_object_id = object_counter++; /* fetch THEN increment */
183 vlc_mutex_unlock( &structure_lock );
185 if( i_type == VLC_OBJECT_LIBVLC )
187 int canc = vlc_savecancel ();
188 var_Create( p_new, "list", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
189 var_AddCallback( p_new, "list", DumpCommand, NULL );
190 var_Create( p_new, "tree", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
191 var_AddCallback( p_new, "tree", DumpCommand, NULL );
192 var_Create( p_new, "vars", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
193 var_AddCallback( p_new, "vars", DumpCommand, NULL );
194 vlc_restorecancel (canc);
202 * Allocates and initializes a vlc object.
204 * @param i_type known object type (all of them are negative integer values),
205 * or object byte size (always positive).
207 * @return the new object, or NULL on error.
209 void * __vlc_object_create( vlc_object_t *p_this, int i_type )
211 const char * psz_type;
216 case VLC_OBJECT_INTF:
217 i_size = sizeof(intf_thread_t);
218 psz_type = "interface";
220 case VLC_OBJECT_DECODER:
221 i_size = sizeof(decoder_t);
222 psz_type = "decoder";
224 case VLC_OBJECT_PACKETIZER:
225 i_size = sizeof(decoder_t);
226 psz_type = "packetizer";
228 case VLC_OBJECT_ENCODER:
229 i_size = sizeof(encoder_t);
230 psz_type = "encoder";
232 case VLC_OBJECT_AOUT:
233 i_size = sizeof(aout_instance_t);
234 psz_type = "audio output";
236 case VLC_OBJECT_OPENGL:
237 i_size = sizeof( vout_thread_t );
241 assert( i_type > 0 ); /* unknown type?! */
243 i_type = VLC_OBJECT_GENERIC;
244 psz_type = "generic";
248 return vlc_custom_create( p_this, i_size, i_type, psz_type );
253 ****************************************************************************
254 * Set the destructor of a vlc object
256 * This function sets the destructor of the vlc object. It will be called
257 * when the object is destroyed when the its refcount reaches 0.
258 * (It is called by the internal function vlc_object_destroy())
259 *****************************************************************************/
260 void __vlc_object_set_destructor( vlc_object_t *p_this,
261 vlc_destructor_t pf_destructor )
263 vlc_object_internals_t *p_priv = vlc_internals(p_this );
264 p_priv->pf_destructor = pf_destructor;
268 ****************************************************************************
269 * Destroy a vlc object (Internal)
271 * This function destroys an object that has been previously allocated with
272 * vlc_object_create. The object's refcount must be zero and it must not be
273 * attached to other objects in any way.
275 * This function must be called with cancellation disabled (currently).
276 *****************************************************************************/
277 static void vlc_object_destroy( vlc_object_t *p_this )
279 vlc_object_internals_t *p_priv = vlc_internals( p_this );
281 /* Objects are always detached beforehand */
282 assert( !p_this->p_parent );
284 /* Send a kill to the object's thread if applicable */
285 vlc_object_kill( p_this );
287 /* Call the custom "subclass" destructor */
288 if( p_priv->pf_destructor )
289 p_priv->pf_destructor( p_this );
291 /* Any thread must have been cleaned up at this point. */
292 assert( !p_priv->b_thread );
294 /* Destroy the associated variables, starting from the end so that
295 * no memmove calls have to be done. */
296 while( p_priv->i_vars )
298 var_Destroy( p_this, p_priv->p_vars[p_priv->i_vars - 1].psz_name );
301 free( p_priv->p_vars );
302 vlc_mutex_destroy( &p_priv->var_lock );
304 free( p_this->psz_header );
306 if( p_this->p_libvlc == NULL )
307 /* We are the global object ... no need to lock. */
308 vlc_mutex_destroy( &structure_lock );
310 FREENULL( p_this->psz_object_name );
312 vlc_spin_destroy( &p_priv->ref_spin );
313 vlc_mutex_destroy( &p_priv->lock );
314 vlc_cond_destroy( &p_priv->wait );
315 vlc_spin_destroy( &p_priv->spin );
316 if( p_priv->pipes[1] != -1 )
317 close( p_priv->pipes[1] );
318 if( p_priv->pipes[0] != -1 )
319 close( p_priv->pipes[0] );
325 /** Inter-object signaling */
327 void __vlc_object_lock( vlc_object_t *obj )
329 vlc_mutex_lock( &(vlc_internals(obj)->lock) );
332 void __vlc_object_unlock( vlc_object_t *obj )
334 vlc_assert_locked( &(vlc_internals(obj)->lock) );
335 vlc_mutex_unlock( &(vlc_internals(obj)->lock) );
339 # include <winsock2.h>
340 # include <ws2tcpip.h>
343 * select()-able pipes emulated using Winsock
345 static int pipe (int fd[2])
348 int addrlen = sizeof (addr);
350 SOCKET l = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP), a,
351 c = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
352 if ((l == INVALID_SOCKET) || (c == INVALID_SOCKET))
355 memset (&addr, 0, sizeof (addr));
356 addr.sin_family = AF_INET;
357 addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
358 if (bind (l, (PSOCKADDR)&addr, sizeof (addr))
359 || getsockname (l, (PSOCKADDR)&addr, &addrlen)
361 || connect (c, (PSOCKADDR)&addr, addrlen))
364 a = accept (l, NULL, NULL);
365 if (a == INVALID_SOCKET)
376 if (l != INVALID_SOCKET)
378 if (c != INVALID_SOCKET)
384 #define read( a, b, c ) recv (a, b, c, 0)
386 #define write( a, b, c ) send (a, b, c, 0)
388 #define close( a ) closesocket (a)
392 * Returns the readable end of a pipe that becomes readable once termination
393 * of the object is requested (vlc_object_kill()).
394 * This can be used to wake-up out of a select() or poll() event loop, such
395 * typically when doing network I/O.
397 * Note that the pipe will remain the same for the lifetime of the object.
398 * DO NOT read the pipe nor close it yourself. Ever.
400 * @param obj object that would be "killed"
401 * @return a readable pipe descriptor, or -1 on error.
403 int __vlc_object_waitpipe( vlc_object_t *obj )
405 int pfd[2] = { -1, -1 };
406 vlc_object_internals_t *internals = vlc_internals( obj );
409 vlc_spin_lock (&internals->spin);
410 if (internals->pipes[0] == -1)
412 /* This can only ever happen if someone killed us without locking: */
413 assert (internals->pipes[1] == -1);
414 vlc_spin_unlock (&internals->spin);
419 vlc_spin_lock (&internals->spin);
420 if (internals->pipes[0] == -1)
422 internals->pipes[0] = pfd[0];
423 internals->pipes[1] = pfd[1];
424 pfd[0] = pfd[1] = -1;
428 vlc_spin_unlock (&internals->spin);
432 /* Race condition: vlc_object_kill() already invoked! */
435 vlc_spin_lock (&internals->spin);
436 fd = internals->pipes[1];
437 internals->pipes[1] = -1;
438 vlc_spin_unlock (&internals->spin);
440 msg_Dbg (obj, "waitpipe: object already dying");
445 /* Race condition: two threads call pipe() - unlikely */
451 return internals->pipes[0];
456 * Suspends until another thread calls vlc_object_signal_unlocked().
457 * The thread may be woken up earlier due to limitations of the underlying
460 * In new code, please use vlc_cond_wait() instead.
462 * This function is a cancellation point. In case of cancellation, the object
463 * will be in locked state.
465 void __vlc_object_wait( vlc_object_t *obj )
467 vlc_object_internals_t *priv = vlc_internals( obj );
468 vlc_assert_locked( &priv->lock);
469 vlc_cond_wait( &priv->wait, &priv->lock );
474 * Wakes up one thread waiting on the object. If no thread are (yet) waiting,
477 * Please do not use this function in new code as we are trying to untangle
478 * objects and threads. Use vlc_cond_wait() instead.
480 void __vlc_object_signal_unlocked( vlc_object_t *obj )
482 vlc_assert_locked (&(vlc_internals(obj)->lock));
483 vlc_cond_signal( &(vlc_internals(obj)->wait) );
488 * Requests termination of an object.
489 * If the object is LibVLC, also request to terminate all its children.
491 void __vlc_object_kill( vlc_object_t *p_this )
493 vlc_object_internals_t *priv = vlc_internals( p_this );
496 vlc_thread_cancel( p_this );
497 vlc_object_lock( p_this );
498 p_this->b_die = true;
500 vlc_spin_lock (&priv->spin);
503 vlc_spin_unlock (&priv->spin);
507 msg_Dbg (p_this, "waitpipe: object killed");
511 vlc_cond_broadcast (&priv->wait);
512 /* This also serves as a memory barrier toward vlc_object_alive(): */
513 vlc_object_unlock( p_this );
517 /*****************************************************************************
518 * find a typed object and increment its refcount
519 *****************************************************************************
520 * This function recursively looks for a given object type. i_mode can be one
521 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
522 *****************************************************************************/
523 void * __vlc_object_find( vlc_object_t *p_this, int i_type, int i_mode )
525 vlc_object_t *p_found;
527 /* If we are of the requested type ourselves, don't look further */
528 if( !(i_mode & FIND_STRICT) && p_this->i_object_type == i_type )
530 vlc_object_yield( p_this );
534 /* Otherwise, recursively look for the object */
535 if ((i_mode & 0x000f) == FIND_ANYWHERE)
538 if (i_type == VLC_OBJECT_PLAYLIST)
539 msg_Err (p_this, "using vlc_object_find(VLC_OBJECT_PLAYLIST) "
540 "instead of pl_Yield()");
542 return vlc_object_find (p_this->p_libvlc, i_type,
543 (i_mode & ~0x000f)|FIND_CHILD);
546 vlc_mutex_lock( &structure_lock );
547 p_found = FindObject( p_this, i_type, i_mode );
548 vlc_mutex_unlock( &structure_lock );
552 #undef vlc_object_find_name
554 * Finds a named object and increment its reference count.
555 * Beware that objects found in this manner can be "owned" by another thread,
556 * be of _any_ type, and be attached to any module (if any). With such an
557 * object reference, you can set or get object variables, emit log messages,
558 * and read write-once object parameters (psz_object_type, etc).
559 * You CANNOT cast the object to a more specific object type, and you
560 * definitely cannot invoke object type-specific callbacks with this.
562 * @param p_this object to search from
563 * @param psz_name name of the object to search for
564 * @param i_mode search direction: FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
566 * @return a matching object (must be released by the caller),
569 vlc_object_t *vlc_object_find_name( vlc_object_t *p_this,
570 const char *psz_name, int i_mode )
572 vlc_object_t *p_found;
574 /* If have the requested name ourselves, don't look further */
575 if( !(i_mode & FIND_STRICT)
576 && p_this->psz_object_name
577 && !strcmp( p_this->psz_object_name, psz_name ) )
579 vlc_object_yield( p_this );
583 vlc_mutex_lock( &structure_lock );
585 /* Otherwise, recursively look for the object */
586 if( (i_mode & 0x000f) == FIND_ANYWHERE )
588 vlc_object_t *p_root = p_this;
591 while( p_root->p_parent != NULL &&
592 p_root != VLC_OBJECT( p_this->p_libvlc ) )
594 p_root = p_root->p_parent;
597 p_found = FindObjectName( p_root, psz_name,
598 (i_mode & ~0x000f)|FIND_CHILD );
599 if( p_found == NULL && p_root != VLC_OBJECT( p_this->p_libvlc ) )
601 p_found = FindObjectName( VLC_OBJECT( p_this->p_libvlc ),
602 psz_name, (i_mode & ~0x000f)|FIND_CHILD );
607 p_found = FindObjectName( p_this, psz_name, i_mode );
610 vlc_mutex_unlock( &structure_lock );
616 * Increment an object reference counter.
618 void __vlc_object_yield( vlc_object_t *p_this )
620 vlc_object_internals_t *internals = vlc_internals( p_this );
622 vlc_spin_lock( &internals->ref_spin );
623 /* Avoid obvious freed object uses */
624 assert( internals->i_refcount > 0 );
625 /* Increment the counter */
626 internals->i_refcount++;
627 vlc_spin_unlock( &internals->ref_spin );
630 /*****************************************************************************
631 * Decrement an object refcount
632 * And destroy the object if its refcount reach zero.
633 *****************************************************************************/
634 void __vlc_object_release( vlc_object_t *p_this )
636 vlc_object_internals_t *internals = vlc_internals( p_this );
637 bool b_should_destroy;
639 vlc_spin_lock( &internals->ref_spin );
640 assert( internals->i_refcount > 0 );
642 if( internals->i_refcount > 1 )
645 /* There are still other references to the object */
646 internals->i_refcount--;
647 vlc_spin_unlock( &internals->ref_spin );
650 vlc_spin_unlock( &internals->ref_spin );
653 /* Remember that we cannot hold the spin while waiting on the mutex */
654 vlc_mutex_lock( &structure_lock );
655 /* Take the spin again. Note that another thread may have yielded the
656 * object in the (very short) mean time. */
657 vlc_spin_lock( &internals->ref_spin );
658 b_should_destroy = --internals->i_refcount == 0;
659 vlc_spin_unlock( &internals->ref_spin );
661 if( b_should_destroy )
664 if( VLC_OBJECT(p_this->p_libvlc) == p_this )
667 vlc_object_t *leaked = internals->next;
668 while( leaked != p_this )
670 /* We are leaking this object */
672 "ERROR: leaking object (%p, type:%s, name:%s)\n",
673 leaked, leaked->psz_object_type,
674 leaked->psz_object_name );
675 /* Dump object to ease debugging */
676 vlc_object_dump( leaked );
678 leaked = vlc_internals (leaked)->next;
681 if( internals->next != p_this )
682 /* Dump libvlc object to ease debugging */
683 vlc_object_dump( p_this );
686 /* Remove the object from object list
687 * so that it cannot be encountered by vlc_object_get() */
688 vlc_internals (internals->next)->prev = internals->prev;
689 vlc_internals (internals->prev)->next = internals->next;
691 /* Detach from parent to protect against FIND_CHILDREN */
692 vlc_object_detach_unlocked (p_this);
693 /* Detach from children to protect against FIND_PARENT */
694 for (int i = 0; i < internals->i_children; i++)
695 internals->pp_children[i]->p_parent = NULL;
698 vlc_mutex_unlock( &structure_lock );
700 if( b_should_destroy )
704 free( internals->pp_children );
705 internals->pp_children = NULL;
706 internals->i_children = 0;
707 canc = vlc_savecancel ();
708 vlc_object_destroy( p_this );
709 vlc_restorecancel (canc);
714 ****************************************************************************
715 * attach object to a parent object
716 *****************************************************************************
717 * This function sets p_this as a child of p_parent, and p_parent as a parent
718 * of p_this. This link can be undone using vlc_object_detach.
719 *****************************************************************************/
720 void __vlc_object_attach( vlc_object_t *p_this, vlc_object_t *p_parent )
722 if( !p_this ) return;
724 vlc_mutex_lock( &structure_lock );
726 /* Attach the parent to its child */
727 assert (!p_this->p_parent);
728 p_this->p_parent = p_parent;
730 /* Attach the child to its parent */
731 vlc_object_internals_t *priv = vlc_internals( p_parent );
732 INSERT_ELEM( priv->pp_children, priv->i_children, priv->i_children,
735 vlc_mutex_unlock( &structure_lock );
739 static void vlc_object_detach_unlocked (vlc_object_t *p_this)
741 vlc_assert_locked (&structure_lock);
743 if (p_this->p_parent == NULL)
746 vlc_object_internals_t *priv = vlc_internals( p_this->p_parent );
750 /* Remove p_this's parent */
751 p_this->p_parent = NULL;
753 /* Remove all of p_parent's children which are p_this */
754 for( i_index = priv->i_children ; i_index-- ; )
756 if( priv->pp_children[i_index] == p_this )
759 for( i = i_index ; i < priv->i_children ; i++ )
760 priv->pp_children[i] = priv->pp_children[i+1];
764 if( priv->i_children )
766 vlc_object_t **pp_children = (vlc_object_t **)
767 realloc( priv->pp_children,
768 priv->i_children * sizeof(vlc_object_t *) );
770 priv->pp_children = pp_children;
774 /* Special case - don't realloc() to zero to avoid leaking */
775 free( priv->pp_children );
776 priv->pp_children = NULL;
782 ****************************************************************************
783 * detach object from its parent
784 *****************************************************************************
785 * This function removes all links between an object and its parent.
786 *****************************************************************************/
787 void __vlc_object_detach( vlc_object_t *p_this )
789 if( !p_this ) return;
791 vlc_mutex_lock( &structure_lock );
792 if( !p_this->p_parent )
793 msg_Err( p_this, "object is not attached" );
795 vlc_object_detach_unlocked( p_this );
796 vlc_mutex_unlock( &structure_lock );
801 ****************************************************************************
802 * find a list typed objects and increment their refcount
803 *****************************************************************************
804 * This function recursively looks for a given object type. i_mode can be one
805 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
806 *****************************************************************************/
807 vlc_list_t * __vlc_list_find( vlc_object_t *p_this, int i_type, int i_mode )
812 /* Look for the objects */
813 switch( i_mode & 0x000f )
816 /* Modules should probably not be object, and the module should perhaps
817 * not be shared across LibVLC instances. In the mean time, this ugly
818 * hack is brought to you by Courmisch. */
819 if (i_type == VLC_OBJECT_MODULE)
820 return vlc_list_find ((vlc_object_t *)p_module_bank,
822 return vlc_list_find (p_this->p_libvlc, i_type, FIND_CHILD);
825 vlc_mutex_lock( &structure_lock );
826 i_count = CountChildren( p_this, i_type );
827 p_list = NewList( i_count );
829 /* Check allocation was successful */
830 if( p_list->i_count != i_count )
832 vlc_mutex_unlock( &structure_lock );
833 msg_Err( p_this, "list allocation failed!" );
839 ListChildren( p_list, p_this, i_type );
840 vlc_mutex_unlock( &structure_lock );
844 msg_Err( p_this, "unimplemented!" );
845 p_list = NewList( 0 );
853 * Gets the list of children of an objects, and increment their reference
855 * @return a list (possibly empty) or NULL in case of error.
857 vlc_list_t *__vlc_list_children( vlc_object_t *obj )
860 vlc_object_internals_t *priv = vlc_internals( obj );
862 vlc_mutex_lock( &structure_lock );
863 l = NewList( priv->i_children );
864 for (int i = 0; i < l->i_count; i++)
866 vlc_object_yield( priv->pp_children[i] );
867 l->p_values[i].p_object = priv->pp_children[i];
869 vlc_mutex_unlock( &structure_lock );
873 /*****************************************************************************
874 * DumpCommand: print the current vlc structure
875 *****************************************************************************
876 * This function prints either an ASCII tree showing the connections between
877 * vlc objects, and additional information such as their refcount, thread ID,
878 * etc. (command "tree"), or the same data as a simple list (command "list").
879 *****************************************************************************/
880 static int DumpCommand( vlc_object_t *p_this, char const *psz_cmd,
881 vlc_value_t oldval, vlc_value_t newval, void *p_data )
883 libvlc_int_t *p_libvlc = p_this->p_libvlc;
885 (void)oldval; (void)p_data;
886 if( *psz_cmd == 'l' )
888 vlc_object_t *cur = VLC_OBJECT (p_libvlc);
890 vlc_mutex_lock( &structure_lock );
893 PrintObject (cur, "");
894 cur = vlc_internals (cur)->next;
896 while (cur != VLC_OBJECT(p_libvlc));
897 vlc_mutex_unlock( &structure_lock );
901 vlc_object_t *p_object = NULL;
903 if( *newval.psz_string )
906 int i_id = strtol( newval.psz_string, &end, 0 );
907 /* try using the object's name to find it */
908 p_object = vlc_object_find_name( p_this, newval.psz_string,
916 vlc_mutex_lock( &structure_lock );
918 if( *psz_cmd == 't' )
920 char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
923 p_object = VLC_OBJECT(p_this->p_libvlc);
926 DumpStructure( p_object, 0, psz_foo );
928 else if( *psz_cmd == 'v' )
933 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
935 PrintObject( p_object, "" );
937 if( !vlc_internals( p_object )->i_vars )
938 printf( " `-o No variables\n" );
939 for( i = 0; i < vlc_internals( p_object )->i_vars; i++ )
941 variable_t *p_var = vlc_internals( p_object )->p_vars + i;
943 const char *psz_type = "unknown";
944 switch( p_var->i_type & VLC_VAR_TYPE )
946 #define MYCASE( type, nice ) \
947 case VLC_VAR_ ## type: \
950 MYCASE( VOID, "void" );
951 MYCASE( BOOL, "bool" );
952 MYCASE( INTEGER, "integer" );
953 MYCASE( HOTKEY, "hotkey" );
954 MYCASE( STRING, "string" );
955 MYCASE( MODULE, "module" );
956 MYCASE( FILE, "file" );
957 MYCASE( DIRECTORY, "directory" );
958 MYCASE( VARIABLE, "variable" );
959 MYCASE( FLOAT, "float" );
960 MYCASE( TIME, "time" );
961 MYCASE( ADDRESS, "address" );
962 MYCASE( MUTEX, "mutex" );
963 MYCASE( LIST, "list" );
966 printf( " %c-o \"%s\" (%s",
967 i + 1 == vlc_internals( p_object )->i_vars ? '`' : '|',
968 p_var->psz_name, psz_type );
969 if( p_var->psz_text )
970 printf( ", %s", p_var->psz_text );
972 if( p_var->i_type & VLC_VAR_HASCHOICE )
973 printf( ", has choices" );
974 if( p_var->i_type & VLC_VAR_ISCOMMAND )
975 printf( ", command" );
976 if( p_var->i_entries )
977 printf( ", %d callbacks", p_var->i_entries );
978 switch( p_var->i_type & VLC_VAR_CLASS )
984 printf( ": %s", p_var->val.b_bool ? "true" : "false" );
986 case VLC_VAR_INTEGER:
987 printf( ": %d", p_var->val.i_int );
990 printf( ": \"%s\"", p_var->val.psz_string );
993 printf( ": %f", p_var->val.f_float );
996 printf( ": %"PRIi64, (int64_t)p_var->val.i_time );
998 case VLC_VAR_ADDRESS:
999 printf( ": %p", p_var->val.p_address );
1009 vlc_mutex_unlock( &structure_lock );
1011 if( *newval.psz_string )
1013 vlc_object_release( p_object );
1020 /*****************************************************************************
1021 * vlc_list_release: free a list previously allocated by vlc_list_find
1022 *****************************************************************************
1023 * This function decreases the refcount of all objects in the list and
1025 *****************************************************************************/
1026 void vlc_list_release( vlc_list_t *p_list )
1030 for( i_index = 0; i_index < p_list->i_count; i_index++ )
1032 vlc_object_release( p_list->p_values[i_index].p_object );
1035 free( p_list->p_values );
1039 /*****************************************************************************
1040 * dump an object. (Debug function)
1041 *****************************************************************************/
1042 static void vlc_object_dump( vlc_object_t *p_this )
1044 char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
1047 vlc_assert_locked( &structure_lock );
1048 DumpStructure( p_this, 0, psz_foo );
1051 /* Following functions are local */
1053 static vlc_object_t * FindObject( vlc_object_t *p_this, int i_type, int i_mode )
1056 vlc_object_t *p_tmp;
1058 switch( i_mode & 0x000f )
1061 p_tmp = p_this->p_parent;
1064 if( p_tmp->i_object_type == i_type )
1066 vlc_object_yield( p_tmp );
1071 return FindObject( p_tmp, i_type, i_mode );
1077 for( i = vlc_internals( p_this )->i_children; i--; )
1079 p_tmp = vlc_internals( p_this )->pp_children[i];
1080 if( p_tmp->i_object_type == i_type )
1082 vlc_object_yield( p_tmp );
1085 else if( vlc_internals( p_tmp )->i_children )
1087 p_tmp = FindObject( p_tmp, i_type, i_mode );
1097 /* Handled in vlc_object_find */
1104 static vlc_object_t * FindObjectName( vlc_object_t *p_this,
1105 const char *psz_name,
1109 vlc_object_t *p_tmp;
1111 switch( i_mode & 0x000f )
1114 p_tmp = p_this->p_parent;
1117 if( p_tmp->psz_object_name
1118 && !strcmp( p_tmp->psz_object_name, psz_name ) )
1120 vlc_object_yield( p_tmp );
1125 return FindObjectName( p_tmp, psz_name, i_mode );
1131 for( i = vlc_internals( p_this )->i_children; i--; )
1133 p_tmp = vlc_internals( p_this )->pp_children[i];
1134 if( p_tmp->psz_object_name
1135 && !strcmp( p_tmp->psz_object_name, psz_name ) )
1137 vlc_object_yield( p_tmp );
1140 else if( vlc_internals( p_tmp )->i_children )
1142 p_tmp = FindObjectName( p_tmp, psz_name, i_mode );
1152 /* Handled in vlc_object_find */
1160 static void PrintObject( vlc_object_t *p_this, const char *psz_prefix )
1162 char psz_children[20], psz_refcount[20], psz_thread[30], psz_name[50],
1165 int canc = vlc_savecancel ();
1166 memset( &psz_name, 0, sizeof(psz_name) );
1167 if( p_this->psz_object_name )
1169 snprintf( psz_name, 49, " \"%s\"", p_this->psz_object_name );
1171 psz_name[48] = '\"';
1174 psz_children[0] = '\0';
1175 switch( vlc_internals( p_this )->i_children )
1180 strcpy( psz_children, ", 1 child" );
1183 snprintf( psz_children, 19, ", %i children",
1184 vlc_internals( p_this )->i_children );
1188 psz_refcount[0] = '\0';
1189 if( vlc_internals( p_this )->i_refcount > 0 )
1190 snprintf( psz_refcount, 19, ", refcount %u",
1191 vlc_internals( p_this )->i_refcount );
1193 psz_thread[0] = '\0';
1194 if( vlc_internals( p_this )->b_thread )
1195 snprintf( psz_thread, 29, " (thread %lu)",
1196 (unsigned long)vlc_internals( p_this )->thread_id );
1198 psz_parent[0] = '\0';
1199 if( p_this->p_parent )
1200 snprintf( psz_parent, 19, ", parent %p", p_this->p_parent );
1202 printf( " %so %p %s%s%s%s%s%s\n", psz_prefix,
1203 p_this, p_this->psz_object_type,
1204 psz_name, psz_thread, psz_refcount, psz_children,
1206 vlc_restorecancel (canc);
1209 static void DumpStructure( vlc_object_t *p_this, int i_level, char *psz_foo )
1212 char i_back = psz_foo[i_level];
1213 psz_foo[i_level] = '\0';
1215 PrintObject( p_this, psz_foo );
1217 psz_foo[i_level] = i_back;
1219 if( i_level / 2 >= MAX_DUMPSTRUCTURE_DEPTH )
1221 msg_Warn( p_this, "structure tree is too deep" );
1225 for( i = 0 ; i < vlc_internals( p_this )->i_children ; i++ )
1229 psz_foo[i_level-1] = ' ';
1231 if( psz_foo[i_level-2] == '`' )
1233 psz_foo[i_level-2] = ' ';
1237 if( i == vlc_internals( p_this )->i_children - 1 )
1239 psz_foo[i_level] = '`';
1243 psz_foo[i_level] = '|';
1246 psz_foo[i_level+1] = '-';
1247 psz_foo[i_level+2] = '\0';
1249 DumpStructure( vlc_internals( p_this )->pp_children[i], i_level + 2,
1254 static vlc_list_t * NewList( int i_count )
1256 vlc_list_t * p_list = (vlc_list_t *)malloc( sizeof( vlc_list_t ) );
1257 if( p_list == NULL )
1262 p_list->i_count = i_count;
1266 p_list->p_values = NULL;
1270 p_list->p_values = malloc( i_count * sizeof( vlc_value_t ) );
1271 if( p_list->p_values == NULL )
1273 p_list->i_count = 0;
1280 static void ListReplace( vlc_list_t *p_list, vlc_object_t *p_object,
1283 if( p_list == NULL || i_index >= p_list->i_count )
1288 vlc_object_yield( p_object );
1290 p_list->p_values[i_index].p_object = p_object;
1295 /*static void ListAppend( vlc_list_t *p_list, vlc_object_t *p_object )
1297 if( p_list == NULL )
1302 p_list->p_values = realloc( p_list->p_values, (p_list->i_count + 1)
1303 * sizeof( vlc_value_t ) );
1304 if( p_list->p_values == NULL )
1306 p_list->i_count = 0;
1310 vlc_object_yield( p_object );
1312 p_list->p_values[p_list->i_count].p_object = p_object;
1318 static int CountChildren( vlc_object_t *p_this, int i_type )
1320 vlc_object_t *p_tmp;
1323 for( i = 0; i < vlc_internals( p_this )->i_children; i++ )
1325 p_tmp = vlc_internals( p_this )->pp_children[i];
1327 if( p_tmp->i_object_type == i_type )
1331 i_count += CountChildren( p_tmp, i_type );
1337 static void ListChildren( vlc_list_t *p_list, vlc_object_t *p_this, int i_type )
1339 vlc_object_t *p_tmp;
1342 for( i = 0; i < vlc_internals( p_this )->i_children; i++ )
1344 p_tmp = vlc_internals( p_this )->pp_children[i];
1346 if( p_tmp->i_object_type == i_type )
1347 ListReplace( p_list, p_tmp, p_list->i_count++ );
1349 ListChildren( p_list, p_tmp, i_type );