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;
93 static void close_nocancel (int fd)
95 int canc = vlc_savecancel ();
97 vlc_restorecancel (canc);
100 void *__vlc_custom_create( vlc_object_t *p_this, size_t i_size,
101 int i_type, const char *psz_type )
104 vlc_object_internals_t *p_priv;
107 * VLC objects are laid out as follow:
108 * - first the LibVLC-private per-object data,
109 * - then VLC_COMMON members from vlc_object_t,
110 * - finally, the type-specific data (if any).
112 * This function initializes the LibVLC and common data,
113 * and zeroes the rest.
115 p_priv = calloc( 1, sizeof( *p_priv ) + i_size );
119 assert (i_size >= sizeof (vlc_object_t));
120 p_new = (vlc_object_t *)(p_priv + 1);
122 p_new->i_object_type = i_type;
123 p_new->psz_object_type = psz_type;
124 p_new->psz_object_name = NULL;
126 p_new->b_die = false;
127 p_new->b_error = false;
128 p_new->b_force = false;
130 p_new->psz_header = NULL;
133 p_new->i_flags = p_this->i_flags
134 & (OBJECT_FLAGS_NODBG|OBJECT_FLAGS_QUIET|OBJECT_FLAGS_NOINTERACT);
136 p_priv->p_vars = calloc( sizeof( variable_t ), 16 );
138 if( !p_priv->p_vars )
146 if( i_type == VLC_OBJECT_LIBVLC )
147 p_new->p_libvlc = (libvlc_int_t*)p_new;
150 p_new->p_libvlc = NULL;
151 vlc_mutex_init( &structure_lock );
154 p_this = p_priv->next = p_priv->prev = p_new;
157 p_new->p_libvlc = p_this->p_libvlc;
159 vlc_spin_init( &p_priv->ref_spin );
160 p_priv->i_refcount = 1;
161 p_priv->pf_destructor = NULL;
162 p_priv->b_thread = false;
163 p_new->p_parent = NULL;
164 p_priv->pp_children = NULL;
165 p_priv->i_children = 0;
167 p_new->p_private = NULL;
169 /* Initialize mutexes and condvars */
170 vlc_mutex_init( &p_priv->lock );
171 vlc_cond_init( &p_priv->wait );
172 vlc_mutex_init( &p_priv->var_lock );
173 vlc_spin_init( &p_priv->spin );
174 p_priv->pipes[0] = p_priv->pipes[1] = -1;
176 p_priv->next = p_this;
177 vlc_mutex_lock( &structure_lock );
178 p_priv->prev = vlc_internals (p_this)->prev;
179 vlc_internals (p_this)->prev = p_new;
180 vlc_internals (p_priv->prev)->next = p_new;
181 vlc_mutex_unlock( &structure_lock );
183 if( i_type == VLC_OBJECT_LIBVLC )
185 int canc = vlc_savecancel ();
186 var_Create( p_new, "list", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
187 var_AddCallback( p_new, "list", DumpCommand, NULL );
188 var_Create( p_new, "tree", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
189 var_AddCallback( p_new, "tree", DumpCommand, NULL );
190 var_Create( p_new, "vars", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
191 var_AddCallback( p_new, "vars", DumpCommand, NULL );
192 vlc_restorecancel (canc);
200 * Allocates and initializes a vlc object.
202 * @param i_type known object type (all of them are negative integer values),
203 * or object byte size (always positive).
205 * @return the new object, or NULL on error.
207 void * __vlc_object_create( vlc_object_t *p_this, int i_type )
209 const char * psz_type;
214 case VLC_OBJECT_INTF:
215 i_size = sizeof(intf_thread_t);
216 psz_type = "interface";
218 case VLC_OBJECT_DECODER:
219 i_size = sizeof(decoder_t);
220 psz_type = "decoder";
222 case VLC_OBJECT_PACKETIZER:
223 i_size = sizeof(decoder_t);
224 psz_type = "packetizer";
226 case VLC_OBJECT_ENCODER:
227 i_size = sizeof(encoder_t);
228 psz_type = "encoder";
230 case VLC_OBJECT_AOUT:
231 i_size = sizeof(aout_instance_t);
232 psz_type = "audio output";
234 case VLC_OBJECT_OPENGL:
235 i_size = sizeof( vout_thread_t );
239 assert( i_type > 0 ); /* unknown type?! */
241 i_type = VLC_OBJECT_GENERIC;
242 psz_type = "generic";
246 return vlc_custom_create( p_this, i_size, i_type, psz_type );
251 ****************************************************************************
252 * Set the destructor of a vlc object
254 * This function sets the destructor of the vlc object. It will be called
255 * when the object is destroyed when the its refcount reaches 0.
256 * (It is called by the internal function vlc_object_destroy())
257 *****************************************************************************/
258 void __vlc_object_set_destructor( vlc_object_t *p_this,
259 vlc_destructor_t pf_destructor )
261 vlc_object_internals_t *p_priv = vlc_internals(p_this );
262 p_priv->pf_destructor = pf_destructor;
266 ****************************************************************************
267 * Destroy a vlc object (Internal)
269 * This function destroys an object that has been previously allocated with
270 * vlc_object_create. The object's refcount must be zero and it must not be
271 * attached to other objects in any way.
273 * This function must be called with cancellation disabled (currently).
274 *****************************************************************************/
275 static void vlc_object_destroy( vlc_object_t *p_this )
277 vlc_object_internals_t *p_priv = vlc_internals( p_this );
279 /* Objects are always detached beforehand */
280 assert( !p_this->p_parent );
282 /* Send a kill to the object's thread if applicable */
283 vlc_object_kill( p_this );
285 /* Call the custom "subclass" destructor */
286 if( p_priv->pf_destructor )
287 p_priv->pf_destructor( p_this );
289 /* Any thread must have been cleaned up at this point. */
290 assert( !p_priv->b_thread );
292 /* Destroy the associated variables, starting from the end so that
293 * no memmove calls have to be done. */
294 while( p_priv->i_vars )
296 var_Destroy( p_this, p_priv->p_vars[p_priv->i_vars - 1].psz_name );
299 free( p_priv->p_vars );
300 vlc_mutex_destroy( &p_priv->var_lock );
302 free( p_this->psz_header );
304 if( p_this->p_libvlc == NULL )
305 /* We are the global object ... no need to lock. */
306 vlc_mutex_destroy( &structure_lock );
308 FREENULL( p_this->psz_object_name );
310 vlc_spin_destroy( &p_priv->ref_spin );
311 vlc_mutex_destroy( &p_priv->lock );
312 vlc_cond_destroy( &p_priv->wait );
313 vlc_spin_destroy( &p_priv->spin );
314 if( p_priv->pipes[1] != -1 )
315 close( p_priv->pipes[1] );
316 if( p_priv->pipes[0] != -1 )
317 close( p_priv->pipes[0] );
323 /** Inter-object signaling */
325 void __vlc_object_lock( vlc_object_t *obj )
327 vlc_mutex_lock( &(vlc_internals(obj)->lock) );
330 void __vlc_object_unlock( vlc_object_t *obj )
332 vlc_assert_locked( &(vlc_internals(obj)->lock) );
333 vlc_mutex_unlock( &(vlc_internals(obj)->lock) );
337 # include <winsock2.h>
338 # include <ws2tcpip.h>
341 * select()-able pipes emulated using Winsock
343 static int pipe (int fd[2])
346 int addrlen = sizeof (addr);
348 SOCKET l = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP), a,
349 c = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
350 if ((l == INVALID_SOCKET) || (c == INVALID_SOCKET))
353 memset (&addr, 0, sizeof (addr));
354 addr.sin_family = AF_INET;
355 addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
356 if (bind (l, (PSOCKADDR)&addr, sizeof (addr))
357 || getsockname (l, (PSOCKADDR)&addr, &addrlen)
359 || connect (c, (PSOCKADDR)&addr, addrlen))
362 a = accept (l, NULL, NULL);
363 if (a == INVALID_SOCKET)
374 if (l != INVALID_SOCKET)
376 if (c != INVALID_SOCKET)
382 #define read( a, b, c ) recv (a, b, c, 0)
384 #define write( a, b, c ) send (a, b, c, 0)
386 #define close( a ) closesocket (a)
390 * Returns the readable end of a pipe that becomes readable once termination
391 * of the object is requested (vlc_object_kill()).
392 * This can be used to wake-up out of a select() or poll() event loop, such
393 * typically when doing network I/O.
395 * Note that the pipe will remain the same for the lifetime of the object.
396 * DO NOT read the pipe nor close it yourself. Ever.
398 * @param obj object that would be "killed"
399 * @return a readable pipe descriptor, or -1 on error.
401 int __vlc_object_waitpipe( vlc_object_t *obj )
403 int pfd[2] = { -1, -1 };
404 vlc_object_internals_t *internals = vlc_internals( obj );
407 vlc_spin_lock (&internals->spin);
408 if (internals->pipes[0] == -1)
410 /* This can only ever happen if someone killed us without locking: */
411 assert (internals->pipes[1] == -1);
412 vlc_spin_unlock (&internals->spin);
417 vlc_spin_lock (&internals->spin);
418 if (internals->pipes[0] == -1)
420 internals->pipes[0] = pfd[0];
421 internals->pipes[1] = pfd[1];
422 pfd[0] = pfd[1] = -1;
426 vlc_spin_unlock (&internals->spin);
430 /* Race condition: vlc_object_kill() already invoked! */
433 vlc_spin_lock (&internals->spin);
434 fd = internals->pipes[1];
435 internals->pipes[1] = -1;
436 vlc_spin_unlock (&internals->spin);
438 msg_Dbg (obj, "waitpipe: object already dying");
443 /* Race condition: two threads call pipe() - unlikely */
449 return internals->pipes[0];
454 * Suspends until another thread calls vlc_object_signal_unlocked().
455 * The thread may be woken up earlier due to limitations of the underlying
458 * In new code, please use vlc_cond_wait() instead.
460 * This function is a cancellation point. In case of cancellation, the object
461 * will be in locked state.
463 void __vlc_object_wait( vlc_object_t *obj )
465 vlc_object_internals_t *priv = vlc_internals( obj );
466 vlc_assert_locked( &priv->lock);
467 vlc_cond_wait( &priv->wait, &priv->lock );
472 * Wakes up one thread waiting on the object. If no thread are (yet) waiting,
475 * Please do not use this function in new code as we are trying to untangle
476 * objects and threads. Use vlc_cond_wait() instead.
478 void __vlc_object_signal_unlocked( vlc_object_t *obj )
480 vlc_assert_locked (&(vlc_internals(obj)->lock));
481 vlc_cond_signal( &(vlc_internals(obj)->wait) );
486 * Requests termination of an object.
487 * If the object is LibVLC, also request to terminate all its children.
489 void __vlc_object_kill( vlc_object_t *p_this )
491 vlc_object_internals_t *priv = vlc_internals( p_this );
494 vlc_thread_cancel( p_this );
495 vlc_object_lock( p_this );
496 p_this->b_die = true;
498 vlc_spin_lock (&priv->spin);
501 vlc_spin_unlock (&priv->spin);
505 msg_Dbg (p_this, "waitpipe: object killed");
509 vlc_cond_broadcast (&priv->wait);
510 /* This also serves as a memory barrier toward vlc_object_alive(): */
511 vlc_object_unlock( p_this );
515 /*****************************************************************************
516 * find a typed object and increment its refcount
517 *****************************************************************************
518 * This function recursively looks for a given object type. i_mode can be one
519 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
520 *****************************************************************************/
521 void * __vlc_object_find( vlc_object_t *p_this, int i_type, int i_mode )
523 vlc_object_t *p_found;
525 /* If we are of the requested type ourselves, don't look further */
526 if( !(i_mode & FIND_STRICT) && p_this->i_object_type == i_type )
528 vlc_object_yield( p_this );
532 /* Otherwise, recursively look for the object */
533 if ((i_mode & 0x000f) == FIND_ANYWHERE)
536 if (i_type == VLC_OBJECT_PLAYLIST)
537 msg_Err (p_this, "using vlc_object_find(VLC_OBJECT_PLAYLIST) "
538 "instead of pl_Yield()");
540 return vlc_object_find (p_this->p_libvlc, i_type,
541 (i_mode & ~0x000f)|FIND_CHILD);
544 vlc_mutex_lock( &structure_lock );
545 p_found = FindObject( p_this, i_type, i_mode );
546 vlc_mutex_unlock( &structure_lock );
550 #undef vlc_object_find_name
552 * Finds a named object and increment its reference count.
553 * Beware that objects found in this manner can be "owned" by another thread,
554 * be of _any_ type, and be attached to any module (if any). With such an
555 * object reference, you can set or get object variables, emit log messages,
556 * and read write-once object parameters (psz_object_type, etc).
557 * You CANNOT cast the object to a more specific object type, and you
558 * definitely cannot invoke object type-specific callbacks with this.
560 * @param p_this object to search from
561 * @param psz_name name of the object to search for
562 * @param i_mode search direction: FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
564 * @return a matching object (must be released by the caller),
567 vlc_object_t *vlc_object_find_name( vlc_object_t *p_this,
568 const char *psz_name, int i_mode )
570 vlc_object_t *p_found;
572 /* If have the requested name ourselves, don't look further */
573 if( !(i_mode & FIND_STRICT)
574 && p_this->psz_object_name
575 && !strcmp( p_this->psz_object_name, psz_name ) )
577 vlc_object_yield( p_this );
581 vlc_mutex_lock( &structure_lock );
583 /* Otherwise, recursively look for the object */
584 if( (i_mode & 0x000f) == FIND_ANYWHERE )
586 vlc_object_t *p_root = p_this;
589 while( p_root->p_parent != NULL &&
590 p_root != VLC_OBJECT( p_this->p_libvlc ) )
592 p_root = p_root->p_parent;
595 p_found = FindObjectName( p_root, psz_name,
596 (i_mode & ~0x000f)|FIND_CHILD );
597 if( p_found == NULL && p_root != VLC_OBJECT( p_this->p_libvlc ) )
599 p_found = FindObjectName( VLC_OBJECT( p_this->p_libvlc ),
600 psz_name, (i_mode & ~0x000f)|FIND_CHILD );
605 p_found = FindObjectName( p_this, psz_name, i_mode );
608 vlc_mutex_unlock( &structure_lock );
614 * Increment an object reference counter.
616 void * __vlc_object_yield( vlc_object_t *p_this )
618 vlc_object_internals_t *internals = vlc_internals( p_this );
620 vlc_spin_lock( &internals->ref_spin );
621 /* Avoid obvious freed object uses */
622 assert( internals->i_refcount > 0 );
623 /* Increment the counter */
624 internals->i_refcount++;
625 vlc_spin_unlock( &internals->ref_spin );
629 /*****************************************************************************
630 * Decrement an object refcount
631 * And destroy the object if its refcount reach zero.
632 *****************************************************************************/
633 void __vlc_object_release( vlc_object_t *p_this )
635 vlc_object_internals_t *internals = vlc_internals( p_this );
636 bool b_should_destroy;
638 vlc_spin_lock( &internals->ref_spin );
639 assert( internals->i_refcount > 0 );
641 if( internals->i_refcount > 1 )
644 /* There are still other references to the object */
645 internals->i_refcount--;
646 vlc_spin_unlock( &internals->ref_spin );
649 vlc_spin_unlock( &internals->ref_spin );
652 /* Remember that we cannot hold the spin while waiting on the mutex */
653 vlc_mutex_lock( &structure_lock );
654 /* Take the spin again. Note that another thread may have yielded the
655 * object in the (very short) mean time. */
656 vlc_spin_lock( &internals->ref_spin );
657 b_should_destroy = --internals->i_refcount == 0;
658 vlc_spin_unlock( &internals->ref_spin );
660 if( b_should_destroy )
663 if( VLC_OBJECT(p_this->p_libvlc) == p_this )
666 vlc_object_t *leaked = internals->next;
667 while( leaked != p_this )
669 /* We are leaking this object */
671 "ERROR: leaking object (%p, type:%s, name:%s)\n",
672 leaked, leaked->psz_object_type,
673 leaked->psz_object_name );
674 /* Dump object to ease debugging */
675 vlc_object_dump( leaked );
677 leaked = vlc_internals (leaked)->next;
680 if( internals->next != p_this )
681 /* Dump libvlc object to ease debugging */
682 vlc_object_dump( p_this );
685 /* Remove the object from object list
686 * so that it cannot be encountered by vlc_object_get() */
687 vlc_internals (internals->next)->prev = internals->prev;
688 vlc_internals (internals->prev)->next = internals->next;
690 /* Detach from parent to protect against FIND_CHILDREN */
691 vlc_object_detach_unlocked (p_this);
692 /* Detach from children to protect against FIND_PARENT */
693 for (int i = 0; i < internals->i_children; i++)
694 internals->pp_children[i]->p_parent = NULL;
697 vlc_mutex_unlock( &structure_lock );
699 if( b_should_destroy )
703 free( internals->pp_children );
704 internals->pp_children = NULL;
705 internals->i_children = 0;
706 canc = vlc_savecancel ();
707 vlc_object_destroy( p_this );
708 vlc_restorecancel (canc);
713 ****************************************************************************
714 * attach object to a parent object
715 *****************************************************************************
716 * This function sets p_this as a child of p_parent, and p_parent as a parent
717 * of p_this. This link can be undone using vlc_object_detach.
718 *****************************************************************************/
719 void __vlc_object_attach( vlc_object_t *p_this, vlc_object_t *p_parent )
721 if( !p_this ) return;
723 vlc_mutex_lock( &structure_lock );
725 /* Attach the parent to its child */
726 assert (!p_this->p_parent);
727 p_this->p_parent = p_parent;
729 /* Attach the child to its parent */
730 vlc_object_internals_t *priv = vlc_internals( p_parent );
731 INSERT_ELEM( priv->pp_children, priv->i_children, priv->i_children,
734 vlc_mutex_unlock( &structure_lock );
738 static void vlc_object_detach_unlocked (vlc_object_t *p_this)
740 vlc_assert_locked (&structure_lock);
742 if (p_this->p_parent == NULL)
745 vlc_object_internals_t *priv = vlc_internals( p_this->p_parent );
749 /* Remove p_this's parent */
750 p_this->p_parent = NULL;
752 /* Remove all of p_parent's children which are p_this */
753 for( i_index = priv->i_children ; i_index-- ; )
755 if( priv->pp_children[i_index] == p_this )
758 for( i = i_index ; i < priv->i_children ; i++ )
759 priv->pp_children[i] = priv->pp_children[i+1];
763 if( priv->i_children )
765 vlc_object_t **pp_children = (vlc_object_t **)
766 realloc( priv->pp_children,
767 priv->i_children * sizeof(vlc_object_t *) );
769 priv->pp_children = pp_children;
773 /* Special case - don't realloc() to zero to avoid leaking */
774 free( priv->pp_children );
775 priv->pp_children = NULL;
781 ****************************************************************************
782 * detach object from its parent
783 *****************************************************************************
784 * This function removes all links between an object and its parent.
785 *****************************************************************************/
786 void __vlc_object_detach( vlc_object_t *p_this )
788 if( !p_this ) return;
790 vlc_mutex_lock( &structure_lock );
791 if( !p_this->p_parent )
792 msg_Err( p_this, "object is not attached" );
794 vlc_object_detach_unlocked( p_this );
795 vlc_mutex_unlock( &structure_lock );
800 ****************************************************************************
801 * find a list typed objects and increment their refcount
802 *****************************************************************************
803 * This function recursively looks for a given object type. i_mode can be one
804 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
805 *****************************************************************************/
806 vlc_list_t * __vlc_list_find( vlc_object_t *p_this, int i_type, int i_mode )
811 /* Look for the objects */
812 switch( i_mode & 0x000f )
815 /* Modules should probably not be object, and the module should perhaps
816 * not be shared across LibVLC instances. In the mean time, this ugly
817 * hack is brought to you by Courmisch. */
818 if (i_type == VLC_OBJECT_MODULE)
819 return vlc_list_find ((vlc_object_t *)p_module_bank,
821 return vlc_list_find (p_this->p_libvlc, i_type, FIND_CHILD);
824 vlc_mutex_lock( &structure_lock );
825 i_count = CountChildren( p_this, i_type );
826 p_list = NewList( i_count );
828 /* Check allocation was successful */
829 if( p_list->i_count != i_count )
831 vlc_mutex_unlock( &structure_lock );
832 msg_Err( p_this, "list allocation failed!" );
838 ListChildren( p_list, p_this, i_type );
839 vlc_mutex_unlock( &structure_lock );
843 msg_Err( p_this, "unimplemented!" );
844 p_list = NewList( 0 );
852 * Gets the list of children of an objects, and increment their reference
854 * @return a list (possibly empty) or NULL in case of error.
856 vlc_list_t *__vlc_list_children( vlc_object_t *obj )
859 vlc_object_internals_t *priv = vlc_internals( obj );
861 vlc_mutex_lock( &structure_lock );
862 l = NewList( priv->i_children );
863 for (int i = 0; i < l->i_count; i++)
865 vlc_object_yield( priv->pp_children[i] );
866 l->p_values[i].p_object = priv->pp_children[i];
868 vlc_mutex_unlock( &structure_lock );
872 /*****************************************************************************
873 * DumpCommand: print the current vlc structure
874 *****************************************************************************
875 * This function prints either an ASCII tree showing the connections between
876 * vlc objects, and additional information such as their refcount, thread ID,
877 * etc. (command "tree"), or the same data as a simple list (command "list").
878 *****************************************************************************/
879 static int DumpCommand( vlc_object_t *p_this, char const *psz_cmd,
880 vlc_value_t oldval, vlc_value_t newval, void *p_data )
882 libvlc_int_t *p_libvlc = p_this->p_libvlc;
884 (void)oldval; (void)p_data;
885 if( *psz_cmd == 'l' )
887 vlc_object_t *cur = VLC_OBJECT (p_libvlc);
889 vlc_mutex_lock( &structure_lock );
892 PrintObject (cur, "");
893 cur = vlc_internals (cur)->next;
895 while (cur != VLC_OBJECT(p_libvlc));
896 vlc_mutex_unlock( &structure_lock );
900 vlc_object_t *p_object = NULL;
902 if( *newval.psz_string )
905 int i_id = strtol( newval.psz_string, &end, 0 );
906 /* try using the object's name to find it */
907 p_object = vlc_object_find_name( p_this, newval.psz_string,
915 vlc_mutex_lock( &structure_lock );
917 if( *psz_cmd == 't' )
919 char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
922 p_object = VLC_OBJECT(p_this->p_libvlc);
925 DumpStructure( p_object, 0, psz_foo );
927 else if( *psz_cmd == 'v' )
932 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
934 PrintObject( p_object, "" );
936 if( !vlc_internals( p_object )->i_vars )
937 printf( " `-o No variables\n" );
938 for( i = 0; i < vlc_internals( p_object )->i_vars; i++ )
940 variable_t *p_var = vlc_internals( p_object )->p_vars + i;
942 const char *psz_type = "unknown";
943 switch( p_var->i_type & VLC_VAR_TYPE )
945 #define MYCASE( type, nice ) \
946 case VLC_VAR_ ## type: \
949 MYCASE( VOID, "void" );
950 MYCASE( BOOL, "bool" );
951 MYCASE( INTEGER, "integer" );
952 MYCASE( HOTKEY, "hotkey" );
953 MYCASE( STRING, "string" );
954 MYCASE( MODULE, "module" );
955 MYCASE( FILE, "file" );
956 MYCASE( DIRECTORY, "directory" );
957 MYCASE( VARIABLE, "variable" );
958 MYCASE( FLOAT, "float" );
959 MYCASE( TIME, "time" );
960 MYCASE( ADDRESS, "address" );
961 MYCASE( MUTEX, "mutex" );
962 MYCASE( LIST, "list" );
965 printf( " %c-o \"%s\" (%s",
966 i + 1 == vlc_internals( p_object )->i_vars ? '`' : '|',
967 p_var->psz_name, psz_type );
968 if( p_var->psz_text )
969 printf( ", %s", p_var->psz_text );
971 if( p_var->i_type & VLC_VAR_HASCHOICE )
972 printf( ", has choices" );
973 if( p_var->i_type & VLC_VAR_ISCOMMAND )
974 printf( ", command" );
975 if( p_var->i_entries )
976 printf( ", %d callbacks", p_var->i_entries );
977 switch( p_var->i_type & VLC_VAR_CLASS )
983 printf( ": %s", p_var->val.b_bool ? "true" : "false" );
985 case VLC_VAR_INTEGER:
986 printf( ": %d", p_var->val.i_int );
989 printf( ": \"%s\"", p_var->val.psz_string );
992 printf( ": %f", p_var->val.f_float );
995 printf( ": %"PRIi64, (int64_t)p_var->val.i_time );
997 case VLC_VAR_ADDRESS:
998 printf( ": %p", p_var->val.p_address );
1008 vlc_mutex_unlock( &structure_lock );
1010 if( *newval.psz_string )
1012 vlc_object_release( p_object );
1019 /*****************************************************************************
1020 * vlc_list_release: free a list previously allocated by vlc_list_find
1021 *****************************************************************************
1022 * This function decreases the refcount of all objects in the list and
1024 *****************************************************************************/
1025 void vlc_list_release( vlc_list_t *p_list )
1029 for( i_index = 0; i_index < p_list->i_count; i_index++ )
1031 vlc_object_release( p_list->p_values[i_index].p_object );
1034 free( p_list->p_values );
1038 /*****************************************************************************
1039 * dump an object. (Debug function)
1040 *****************************************************************************/
1041 static void vlc_object_dump( vlc_object_t *p_this )
1043 char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
1046 vlc_assert_locked( &structure_lock );
1047 DumpStructure( p_this, 0, psz_foo );
1050 /* Following functions are local */
1052 static vlc_object_t * FindObject( vlc_object_t *p_this, int i_type, int i_mode )
1055 vlc_object_t *p_tmp;
1057 switch( i_mode & 0x000f )
1060 p_tmp = p_this->p_parent;
1063 if( p_tmp->i_object_type == i_type )
1065 vlc_object_yield( p_tmp );
1070 return FindObject( p_tmp, i_type, i_mode );
1076 for( i = vlc_internals( p_this )->i_children; i--; )
1078 p_tmp = vlc_internals( p_this )->pp_children[i];
1079 if( p_tmp->i_object_type == i_type )
1081 vlc_object_yield( p_tmp );
1084 else if( vlc_internals( p_tmp )->i_children )
1086 p_tmp = FindObject( p_tmp, i_type, i_mode );
1096 /* Handled in vlc_object_find */
1103 static vlc_object_t * FindObjectName( vlc_object_t *p_this,
1104 const char *psz_name,
1108 vlc_object_t *p_tmp;
1110 switch( i_mode & 0x000f )
1113 p_tmp = p_this->p_parent;
1116 if( p_tmp->psz_object_name
1117 && !strcmp( p_tmp->psz_object_name, psz_name ) )
1119 vlc_object_yield( p_tmp );
1124 return FindObjectName( p_tmp, psz_name, i_mode );
1130 for( i = vlc_internals( p_this )->i_children; i--; )
1132 p_tmp = vlc_internals( p_this )->pp_children[i];
1133 if( p_tmp->psz_object_name
1134 && !strcmp( p_tmp->psz_object_name, psz_name ) )
1136 vlc_object_yield( p_tmp );
1139 else if( vlc_internals( p_tmp )->i_children )
1141 p_tmp = FindObjectName( p_tmp, psz_name, i_mode );
1151 /* Handled in vlc_object_find */
1159 static void PrintObject( vlc_object_t *p_this, const char *psz_prefix )
1161 char psz_children[20], psz_refcount[20], psz_thread[30], psz_name[50],
1164 int canc = vlc_savecancel ();
1165 memset( &psz_name, 0, sizeof(psz_name) );
1166 if( p_this->psz_object_name )
1168 snprintf( psz_name, 49, " \"%s\"", p_this->psz_object_name );
1170 psz_name[48] = '\"';
1173 psz_children[0] = '\0';
1174 switch( vlc_internals( p_this )->i_children )
1179 strcpy( psz_children, ", 1 child" );
1182 snprintf( psz_children, 19, ", %i children",
1183 vlc_internals( p_this )->i_children );
1187 psz_refcount[0] = '\0';
1188 if( vlc_internals( p_this )->i_refcount > 0 )
1189 snprintf( psz_refcount, 19, ", refcount %u",
1190 vlc_internals( p_this )->i_refcount );
1192 psz_thread[0] = '\0';
1193 if( vlc_internals( p_this )->b_thread )
1194 snprintf( psz_thread, 29, " (thread %lu)",
1195 (unsigned long)vlc_internals( p_this )->thread_id );
1197 psz_parent[0] = '\0';
1198 if( p_this->p_parent )
1199 snprintf( psz_parent, 19, ", parent %p", p_this->p_parent );
1201 printf( " %so %p %s%s%s%s%s%s\n", psz_prefix,
1202 p_this, p_this->psz_object_type,
1203 psz_name, psz_thread, psz_refcount, psz_children,
1205 vlc_restorecancel (canc);
1208 static void DumpStructure( vlc_object_t *p_this, int i_level, char *psz_foo )
1211 char i_back = psz_foo[i_level];
1212 psz_foo[i_level] = '\0';
1214 PrintObject( p_this, psz_foo );
1216 psz_foo[i_level] = i_back;
1218 if( i_level / 2 >= MAX_DUMPSTRUCTURE_DEPTH )
1220 msg_Warn( p_this, "structure tree is too deep" );
1224 for( i = 0 ; i < vlc_internals( p_this )->i_children ; i++ )
1228 psz_foo[i_level-1] = ' ';
1230 if( psz_foo[i_level-2] == '`' )
1232 psz_foo[i_level-2] = ' ';
1236 if( i == vlc_internals( p_this )->i_children - 1 )
1238 psz_foo[i_level] = '`';
1242 psz_foo[i_level] = '|';
1245 psz_foo[i_level+1] = '-';
1246 psz_foo[i_level+2] = '\0';
1248 DumpStructure( vlc_internals( p_this )->pp_children[i], i_level + 2,
1253 static vlc_list_t * NewList( int i_count )
1255 vlc_list_t * p_list = (vlc_list_t *)malloc( sizeof( vlc_list_t ) );
1256 if( p_list == NULL )
1261 p_list->i_count = i_count;
1265 p_list->p_values = NULL;
1269 p_list->p_values = malloc( i_count * sizeof( vlc_value_t ) );
1270 if( p_list->p_values == NULL )
1272 p_list->i_count = 0;
1279 static void ListReplace( vlc_list_t *p_list, vlc_object_t *p_object,
1282 if( p_list == NULL || i_index >= p_list->i_count )
1287 vlc_object_yield( p_object );
1289 p_list->p_values[i_index].p_object = p_object;
1294 /*static void ListAppend( vlc_list_t *p_list, vlc_object_t *p_object )
1296 if( p_list == NULL )
1301 p_list->p_values = realloc( p_list->p_values, (p_list->i_count + 1)
1302 * sizeof( vlc_value_t ) );
1303 if( p_list->p_values == NULL )
1305 p_list->i_count = 0;
1309 vlc_object_yield( p_object );
1311 p_list->p_values[p_list->i_count].p_object = p_object;
1317 static int CountChildren( vlc_object_t *p_this, int i_type )
1319 vlc_object_t *p_tmp;
1322 for( i = 0; i < vlc_internals( p_this )->i_children; i++ )
1324 p_tmp = vlc_internals( p_this )->pp_children[i];
1326 if( p_tmp->i_object_type == i_type )
1330 i_count += CountChildren( p_tmp, i_type );
1336 static void ListChildren( vlc_list_t *p_list, vlc_object_t *p_this, int i_type )
1338 vlc_object_t *p_tmp;
1341 for( i = 0; i < vlc_internals( p_this )->i_children; i++ )
1343 p_tmp = vlc_internals( p_this )->pp_children[i];
1345 if( p_tmp->i_object_type == i_type )
1346 ListReplace( p_list, p_tmp, p_list->i_count++ );
1348 ListChildren( p_list, p_tmp, i_type );