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 */
65 #ifdef HAVE_SYS_EVENTFD_H
66 # include <sys/eventfd.h>
70 /*****************************************************************************
72 *****************************************************************************/
73 static int DumpCommand( vlc_object_t *, char const *,
74 vlc_value_t, vlc_value_t, void * );
76 static vlc_object_t * FindObject ( vlc_object_t *, int, int );
77 static vlc_object_t * FindObjectName( vlc_object_t *, const char *, int );
78 static void PrintObject ( vlc_object_t *, const char * );
79 static void DumpStructure ( vlc_object_t *, int, char * );
81 static vlc_list_t * NewList ( int );
82 static void ListReplace ( vlc_list_t *, vlc_object_t *, int );
83 /*static void ListAppend ( vlc_list_t *, vlc_object_t * );*/
84 static int CountChildren ( vlc_object_t *, int );
85 static void ListChildren ( vlc_list_t *, vlc_object_t *, int );
87 static void vlc_object_destroy( vlc_object_t *p_this );
88 static void vlc_object_detach_unlocked (vlc_object_t *p_this);
90 static void vlc_object_dump( vlc_object_t *p_this );
93 /*****************************************************************************
94 * Local structure lock
95 *****************************************************************************/
96 static void close_nocancel (int fd)
98 int canc = vlc_savecancel ();
100 vlc_restorecancel (canc);
103 static void libvlc_lock (libvlc_int_t *p_libvlc)
105 vlc_mutex_lock (&(libvlc_priv (p_libvlc)->structure_lock));
108 static void libvlc_unlock (libvlc_int_t *p_libvlc)
110 vlc_mutex_unlock (&(libvlc_priv (p_libvlc)->structure_lock));
113 void *__vlc_custom_create( vlc_object_t *p_this, size_t i_size,
114 int i_type, const char *psz_type )
117 vlc_object_internals_t *p_priv;
120 * VLC objects are laid out as follow:
121 * - first the LibVLC-private per-object data,
122 * - then VLC_COMMON members from vlc_object_t,
123 * - finally, the type-specific data (if any).
125 * This function initializes the LibVLC and common data,
126 * and zeroes the rest.
128 p_priv = calloc( 1, sizeof( *p_priv ) + i_size );
132 assert (i_size >= sizeof (vlc_object_t));
133 p_new = (vlc_object_t *)(p_priv + 1);
135 p_priv->i_object_type = i_type;
136 p_new->psz_object_type = psz_type;
137 p_new->psz_object_name = NULL;
139 p_new->b_die = false;
140 p_new->b_error = false;
141 p_new->b_force = false;
143 p_new->psz_header = NULL;
146 p_new->i_flags = p_this->i_flags
147 & (OBJECT_FLAGS_NODBG|OBJECT_FLAGS_QUIET|OBJECT_FLAGS_NOINTERACT);
149 p_priv->p_vars = calloc( 16, sizeof( variable_t ) );
151 if( !p_priv->p_vars )
159 libvlc_int_t *self = (libvlc_int_t*)p_new;
160 p_new->p_libvlc = self;
161 vlc_mutex_init (&(libvlc_priv (self)->structure_lock));
162 p_this = p_priv->next = p_priv->prev = p_new;
165 p_new->p_libvlc = p_this->p_libvlc;
167 vlc_spin_init( &p_priv->ref_spin );
168 p_priv->i_refcount = 1;
169 p_priv->pf_destructor = NULL;
170 p_priv->b_thread = false;
171 p_new->p_parent = NULL;
172 p_priv->pp_children = NULL;
173 p_priv->i_children = 0;
175 p_new->p_private = NULL;
177 /* Initialize mutexes and condvars */
178 vlc_mutex_init( &p_priv->lock );
179 vlc_cond_init( &p_priv->wait );
180 vlc_mutex_init( &p_priv->var_lock );
181 vlc_cond_init( &p_priv->var_wait );
182 p_priv->pipes[0] = p_priv->pipes[1] = -1;
184 p_priv->next = p_this;
185 libvlc_lock (p_new->p_libvlc);
186 p_priv->prev = vlc_internals (p_this)->prev;
187 vlc_internals (p_this)->prev = p_new;
188 vlc_internals (p_priv->prev)->next = p_new;
189 libvlc_unlock (p_new->p_libvlc);
191 if (p_new == VLC_OBJECT(p_new->p_libvlc))
192 { /* TODO: should be in src/libvlc.c */
193 int canc = vlc_savecancel ();
194 var_Create( p_new, "list", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
195 var_AddCallback( p_new, "list", DumpCommand, NULL );
196 var_Create( p_new, "tree", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
197 var_AddCallback( p_new, "tree", DumpCommand, NULL );
198 var_Create( p_new, "vars", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
199 var_AddCallback( p_new, "vars", DumpCommand, NULL );
200 vlc_restorecancel (canc);
208 * Allocates and initializes a vlc object.
210 * @param i_type known object type (all of them are negative integer values),
211 * or object byte size (always positive).
213 * @return the new object, or NULL on error.
215 void * __vlc_object_create( vlc_object_t *p_this, int i_type )
217 const char * psz_type;
222 case VLC_OBJECT_INTF:
223 i_size = sizeof(intf_thread_t);
224 psz_type = "interface";
226 case VLC_OBJECT_DECODER:
227 i_size = sizeof(decoder_t);
228 psz_type = "decoder";
230 case VLC_OBJECT_PACKETIZER:
231 i_size = sizeof(decoder_t);
232 psz_type = "packetizer";
234 case VLC_OBJECT_ENCODER:
235 i_size = sizeof(encoder_t);
236 psz_type = "encoder";
238 case VLC_OBJECT_AOUT:
239 i_size = sizeof(aout_instance_t);
240 psz_type = "audio output";
243 assert( i_type > 0 ); /* unknown type?! */
245 i_type = VLC_OBJECT_GENERIC;
246 psz_type = "generic";
250 return vlc_custom_create( p_this, i_size, i_type, psz_type );
255 ****************************************************************************
256 * Set the destructor of a vlc object
258 * This function sets the destructor of the vlc object. It will be called
259 * when the object is destroyed when the its refcount reaches 0.
260 * (It is called by the internal function vlc_object_destroy())
261 *****************************************************************************/
262 void __vlc_object_set_destructor( vlc_object_t *p_this,
263 vlc_destructor_t pf_destructor )
265 vlc_object_internals_t *p_priv = vlc_internals(p_this );
266 p_priv->pf_destructor = pf_destructor;
270 ****************************************************************************
271 * Destroy a vlc object (Internal)
273 * This function destroys an object that has been previously allocated with
274 * vlc_object_create. The object's refcount must be zero and it must not be
275 * attached to other objects in any way.
277 * This function must be called with cancellation disabled (currently).
278 *****************************************************************************/
279 static void vlc_object_destroy( vlc_object_t *p_this )
281 vlc_object_internals_t *p_priv = vlc_internals( p_this );
283 /* Objects are always detached beforehand */
284 assert( !p_this->p_parent );
286 /* Send a kill to the object's thread if applicable */
287 vlc_object_kill( p_this );
289 /* Call the custom "subclass" destructor */
290 if( p_priv->pf_destructor )
291 p_priv->pf_destructor( p_this );
293 /* Any thread must have been cleaned up at this point. */
294 assert( !p_priv->b_thread );
296 /* Destroy the associated variables, starting from the end so that
297 * no memmove calls have to be done. */
298 while( p_priv->i_vars )
300 var_Destroy( p_this, p_priv->p_vars[p_priv->i_vars - 1].psz_name );
303 free( p_priv->p_vars );
304 vlc_cond_destroy( &p_priv->var_wait );
305 vlc_mutex_destroy( &p_priv->var_lock );
307 free( p_this->psz_header );
309 FREENULL( p_this->psz_object_name );
311 vlc_spin_destroy( &p_priv->ref_spin );
312 vlc_mutex_destroy( &p_priv->lock );
313 vlc_cond_destroy( &p_priv->wait );
314 if( p_priv->pipes[1] != -1 && p_priv->pipes[1] != p_priv->pipes[0] )
315 close( p_priv->pipes[1] );
316 if( p_priv->pipes[0] != -1 )
317 close( p_priv->pipes[0] );
318 if( VLC_OBJECT(p_this->p_libvlc) == p_this )
319 vlc_mutex_destroy (&(libvlc_priv ((libvlc_int_t *)p_this)->structure_lock));
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) );
337 void __vlc_object_assert_locked( vlc_object_t *obj )
339 vlc_assert_locked( &(vlc_internals(obj)->lock) );
343 # include <winsock2.h>
344 # include <ws2tcpip.h>
347 * select()-able pipes emulated using Winsock
349 static int pipe (int fd[2])
352 int addrlen = sizeof (addr);
354 SOCKET l = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP), a,
355 c = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
356 if ((l == INVALID_SOCKET) || (c == INVALID_SOCKET))
359 memset (&addr, 0, sizeof (addr));
360 addr.sin_family = AF_INET;
361 addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
362 if (bind (l, (PSOCKADDR)&addr, sizeof (addr))
363 || getsockname (l, (PSOCKADDR)&addr, &addrlen)
365 || connect (c, (PSOCKADDR)&addr, addrlen))
368 a = accept (l, NULL, NULL);
369 if (a == INVALID_SOCKET)
380 if (l != INVALID_SOCKET)
382 if (c != INVALID_SOCKET)
388 #define read( a, b, c ) recv (a, b, c, 0)
390 #define write( a, b, c ) send (a, b, c, 0)
392 #define close( a ) closesocket (a)
396 * Returns the readable end of a pipe that becomes readable once termination
397 * of the object is requested (vlc_object_kill()).
398 * This can be used to wake-up out of a select() or poll() event loop, such
399 * typically when doing network I/O.
401 * Note that the pipe will remain the same for the lifetime of the object.
402 * DO NOT read the pipe nor close it yourself. Ever.
404 * @param obj object that would be "killed"
405 * @return a readable pipe descriptor, or -1 on error.
407 int vlc_object_waitpipe( vlc_object_t *obj )
409 vlc_object_internals_t *internals = vlc_internals( obj );
411 vlc_object_lock (obj);
412 if (internals->pipes[0] == -1)
414 /* This can only ever happen if someone killed us without locking: */
415 assert (internals->pipes[1] == -1);
418 internals->pipes[0] = internals->pipes[1] = eventfd (0, 0);
419 if (internals->pipes[0] == -1)
422 if (pipe (internals->pipes))
423 internals->pipes[0] = internals->pipes[1] = -1;
426 if (internals->pipes[0] != -1 && obj->b_die)
427 { /* Race condition: vlc_object_kill() already invoked! */
428 msg_Dbg (obj, "waitpipe: object already dying");
429 write (internals->pipes[1], &(uint64_t){ 1 }, sizeof (uint64_t));
432 vlc_object_unlock (obj);
433 return internals->pipes[0];
438 * Suspends until another thread calls vlc_object_signal_unlocked().
439 * The thread may be woken up earlier due to limitations of the underlying
442 * In new code, please use vlc_cond_wait() instead.
444 * This function is a cancellation point. In case of cancellation, the object
445 * will be in locked state.
447 void __vlc_object_wait( vlc_object_t *obj )
449 vlc_object_internals_t *priv = vlc_internals( obj );
450 vlc_assert_locked( &priv->lock);
451 vlc_cond_wait( &priv->wait, &priv->lock );
456 * Wakes up one thread waiting on the object. If no thread are (yet) waiting,
459 * Please do not use this function in new code as we are trying to untangle
460 * objects and threads. Use vlc_cond_wait() instead.
462 void __vlc_object_signal_unlocked( vlc_object_t *obj )
464 vlc_assert_locked (&(vlc_internals(obj)->lock));
465 vlc_cond_signal( &(vlc_internals(obj)->wait) );
470 * Requests termination of an object, cancels the object thread, and make the
471 * object wait pipe (if it exists) readable. Not a cancellation point.
473 void __vlc_object_kill( vlc_object_t *p_this )
475 vlc_object_internals_t *priv = vlc_internals( p_this );
478 vlc_thread_cancel( p_this );
479 vlc_object_lock( p_this );
483 p_this->b_die = true;
486 vlc_cond_broadcast (&priv->wait);
487 /* This also serves as a memory barrier toward vlc_object_alive(): */
488 vlc_object_unlock( p_this );
492 int canc = vlc_savecancel ();
494 /* write _after_ setting b_die, so vlc_object_alive() returns false */
495 write (fd, &(uint64_t){ 1 }, sizeof (uint64_t));
496 msg_Dbg (p_this, "waitpipe: object killed");
497 vlc_restorecancel (canc);
502 /*****************************************************************************
503 * find a typed object and increment its refcount
504 *****************************************************************************
505 * This function recursively looks for a given object type. i_mode can be one
506 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
507 *****************************************************************************/
508 void * __vlc_object_find( vlc_object_t *p_this, int i_type, int i_mode )
510 vlc_object_t *p_found;
512 /* If we are of the requested type ourselves, don't look further */
513 if( !(i_mode & FIND_STRICT)
514 && vlc_internals (p_this)->i_object_type == i_type )
516 vlc_object_hold( p_this );
520 /* Otherwise, recursively look for the object */
521 if ((i_mode & 0x000f) == FIND_ANYWHERE)
522 return vlc_object_find (p_this->p_libvlc, i_type,
523 (i_mode & ~0x000f)|FIND_CHILD);
525 libvlc_lock (p_this->p_libvlc);
526 p_found = FindObject( p_this, i_type, i_mode );
527 libvlc_unlock (p_this->p_libvlc);
531 #undef vlc_object_find_name
533 * Finds a named object and increment its reference count.
534 * Beware that objects found in this manner can be "owned" by another thread,
535 * be of _any_ type, and be attached to any module (if any). With such an
536 * object reference, you can set or get object variables, emit log messages,
537 * and read write-once object parameters (psz_object_type, etc).
538 * You CANNOT cast the object to a more specific object type, and you
539 * definitely cannot invoke object type-specific callbacks with this.
541 * @param p_this object to search from
542 * @param psz_name name of the object to search for
543 * @param i_mode search direction: FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
545 * @return a matching object (must be released by the caller),
548 vlc_object_t *vlc_object_find_name( vlc_object_t *p_this,
549 const char *psz_name, int i_mode )
551 vlc_object_t *p_found;
553 /* If have the requested name ourselves, don't look further */
554 if( !(i_mode & FIND_STRICT)
555 && p_this->psz_object_name
556 && !strcmp( p_this->psz_object_name, psz_name ) )
558 vlc_object_hold( p_this );
562 libvlc_lock (p_this->p_libvlc);
564 /* Otherwise, recursively look for the object */
565 if( (i_mode & 0x000f) == FIND_ANYWHERE )
567 vlc_object_t *p_root = p_this;
570 while( p_root->p_parent != NULL &&
571 p_root != VLC_OBJECT( p_this->p_libvlc ) )
573 p_root = p_root->p_parent;
576 p_found = FindObjectName( p_root, psz_name,
577 (i_mode & ~0x000f)|FIND_CHILD );
578 if( p_found == NULL && p_root != VLC_OBJECT( p_this->p_libvlc ) )
580 p_found = FindObjectName( VLC_OBJECT( p_this->p_libvlc ),
581 psz_name, (i_mode & ~0x000f)|FIND_CHILD );
586 p_found = FindObjectName( p_this, psz_name, i_mode );
589 libvlc_unlock (p_this->p_libvlc);
594 * Increment an object reference counter.
596 void * __vlc_object_hold( vlc_object_t *p_this )
598 vlc_object_internals_t *internals = vlc_internals( p_this );
600 vlc_spin_lock( &internals->ref_spin );
601 /* Avoid obvious freed object uses */
602 assert( internals->i_refcount > 0 );
603 /* Increment the counter */
604 internals->i_refcount++;
605 vlc_spin_unlock( &internals->ref_spin );
609 /*****************************************************************************
610 * Decrement an object refcount
611 * And destroy the object if its refcount reach zero.
612 *****************************************************************************/
613 void __vlc_object_release( vlc_object_t *p_this )
615 vlc_object_internals_t *internals = vlc_internals( p_this );
616 vlc_object_t *parent = NULL;
617 bool b_should_destroy;
619 vlc_spin_lock( &internals->ref_spin );
620 assert( internals->i_refcount > 0 );
622 if( internals->i_refcount > 1 )
625 /* There are still other references to the object */
626 internals->i_refcount--;
627 vlc_spin_unlock( &internals->ref_spin );
630 vlc_spin_unlock( &internals->ref_spin );
633 /* Remember that we cannot hold the spin while waiting on the mutex */
634 libvlc_lock (p_this->p_libvlc);
635 /* Take the spin again. Note that another thread may have held the
636 * object in the (very short) mean time. */
637 vlc_spin_lock( &internals->ref_spin );
638 b_should_destroy = --internals->i_refcount == 0;
639 vlc_spin_unlock( &internals->ref_spin );
641 if( b_should_destroy )
643 /* We have no children */
644 assert (internals->i_children == 0);
645 parent = p_this->p_parent;
648 if( VLC_OBJECT(p_this->p_libvlc) == p_this )
651 vlc_object_t *leaked = internals->next;
652 while( leaked != p_this )
654 /* We are leaking this object */
656 "ERROR: leaking object (%p, type:%s, name:%s)\n",
657 leaked, leaked->psz_object_type,
658 leaked->psz_object_name );
659 /* Dump object to ease debugging */
660 vlc_object_dump( leaked );
662 leaked = vlc_internals (leaked)->next;
665 if( internals->next != p_this )
666 /* Dump libvlc object to ease debugging */
667 vlc_object_dump( p_this );
670 /* Remove the object from object list
671 * so that it cannot be encountered by vlc_object_get() */
672 vlc_internals (internals->next)->prev = internals->prev;
673 vlc_internals (internals->prev)->next = internals->next;
676 /* Detach from parent to protect against FIND_CHILDREN */
677 vlc_object_detach_unlocked (p_this);
679 libvlc_unlock (p_this->p_libvlc);
681 if( b_should_destroy )
685 canc = vlc_savecancel ();
686 vlc_object_destroy( p_this );
687 vlc_restorecancel (canc);
689 vlc_object_release (parent);
694 ****************************************************************************
695 * attach object to a parent object
696 *****************************************************************************
697 * This function sets p_this as a child of p_parent, and p_parent as a parent
698 * of p_this. This link can be undone using vlc_object_detach.
699 *****************************************************************************/
700 void __vlc_object_attach( vlc_object_t *p_this, vlc_object_t *p_parent )
702 if( !p_this ) return;
704 vlc_object_hold (p_parent);
705 libvlc_lock (p_this->p_libvlc);
707 /* Attach the parent to its child */
708 assert (!p_this->p_parent);
709 p_this->p_parent = p_parent;
711 /* Attach the child to its parent */
712 vlc_object_internals_t *priv = vlc_internals( p_parent );
713 INSERT_ELEM( priv->pp_children, priv->i_children, priv->i_children,
715 libvlc_unlock (p_this->p_libvlc);
719 static void vlc_object_detach_unlocked (vlc_object_t *p_this)
721 if (p_this->p_parent == NULL)
724 vlc_object_internals_t *priv = vlc_internals( p_this->p_parent );
728 /* Remove p_this's parent */
729 p_this->p_parent = NULL;
731 /* Remove all of p_parent's children which are p_this */
732 for( i_index = priv->i_children ; i_index-- ; )
734 if( priv->pp_children[i_index] == p_this )
737 for( i = i_index ; i < priv->i_children ; i++ )
738 priv->pp_children[i] = priv->pp_children[i+1];
742 if( priv->i_children )
744 vlc_object_t **pp_children = (vlc_object_t **)
745 realloc( priv->pp_children,
746 priv->i_children * sizeof(vlc_object_t *) );
748 priv->pp_children = pp_children;
752 /* Special case - don't realloc() to zero to avoid leaking */
753 free( priv->pp_children );
754 priv->pp_children = NULL;
760 ****************************************************************************
761 * detach object from its parent
762 *****************************************************************************
763 * This function removes all links between an object and its parent.
764 *****************************************************************************/
765 void __vlc_object_detach( vlc_object_t *p_this )
767 vlc_object_t *p_parent;
768 if( !p_this ) return;
770 libvlc_lock (p_this->p_libvlc);
771 p_parent = p_this->p_parent;
773 vlc_object_detach_unlocked( p_this );
774 libvlc_unlock (p_this->p_libvlc);
777 vlc_object_release (p_parent);
782 ****************************************************************************
783 * find a list typed objects and increment their refcount
784 *****************************************************************************
785 * This function recursively looks for a given object type. i_mode can be one
786 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
787 *****************************************************************************/
788 vlc_list_t * vlc_list_find( vlc_object_t *p_this, int i_type, int i_mode )
793 /* Look for the objects */
794 switch( i_mode & 0x000f )
797 return vlc_list_find (p_this->p_libvlc, i_type, FIND_CHILD);
800 libvlc_lock (p_this->p_libvlc);
801 i_count = CountChildren( p_this, i_type );
802 p_list = NewList( i_count );
804 /* Check allocation was successful */
805 if( p_list->i_count != i_count )
807 libvlc_unlock (p_this->p_libvlc);
813 ListChildren( p_list, p_this, i_type );
814 libvlc_unlock (p_this->p_libvlc);
818 msg_Err( p_this, "unimplemented!" );
819 p_list = NewList( 0 );
827 * Gets the list of children of an objects, and increment their reference
829 * @return a list (possibly empty) or NULL in case of error.
831 vlc_list_t *__vlc_list_children( vlc_object_t *obj )
834 vlc_object_internals_t *priv = vlc_internals( obj );
836 libvlc_lock (obj->p_libvlc);
837 l = NewList( priv->i_children );
838 for (int i = 0; i < l->i_count; i++)
840 vlc_object_hold( priv->pp_children[i] );
841 l->p_values[i].p_object = priv->pp_children[i];
843 libvlc_unlock (obj->p_libvlc);
847 /*****************************************************************************
848 * DumpCommand: print the current vlc structure
849 *****************************************************************************
850 * This function prints either an ASCII tree showing the connections between
851 * vlc objects, and additional information such as their refcount, thread ID,
852 * etc. (command "tree"), or the same data as a simple list (command "list").
853 *****************************************************************************/
854 static int DumpCommand( vlc_object_t *p_this, char const *psz_cmd,
855 vlc_value_t oldval, vlc_value_t newval, void *p_data )
857 libvlc_int_t *p_libvlc = p_this->p_libvlc;
859 (void)oldval; (void)p_data;
860 if( *psz_cmd == 'l' )
862 vlc_object_t *cur = VLC_OBJECT (p_libvlc);
864 libvlc_lock (p_this->p_libvlc);
867 PrintObject (cur, "");
868 cur = vlc_internals (cur)->next;
870 while (cur != VLC_OBJECT(p_libvlc));
871 libvlc_unlock (p_this->p_libvlc);
875 vlc_object_t *p_object = NULL;
877 if( *newval.psz_string )
879 /* try using the object's name to find it */
880 p_object = vlc_object_find_name( p_this, newval.psz_string,
888 libvlc_lock (p_this->p_libvlc);
889 if( *psz_cmd == 't' )
891 char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
894 p_object = VLC_OBJECT(p_this->p_libvlc);
897 DumpStructure( p_object, 0, psz_foo );
899 else if( *psz_cmd == 'v' )
904 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
906 PrintObject( p_object, "" );
908 if( !vlc_internals( p_object )->i_vars )
909 printf( " `-o No variables\n" );
910 for( i = 0; i < vlc_internals( p_object )->i_vars; i++ )
912 variable_t *p_var = vlc_internals( p_object )->p_vars + i;
914 const char *psz_type = "unknown";
915 switch( p_var->i_type & VLC_VAR_TYPE )
917 #define MYCASE( type, nice ) \
918 case VLC_VAR_ ## type: \
921 MYCASE( VOID, "void" );
922 MYCASE( BOOL, "bool" );
923 MYCASE( INTEGER, "integer" );
924 MYCASE( HOTKEY, "hotkey" );
925 MYCASE( STRING, "string" );
926 MYCASE( MODULE, "module" );
927 MYCASE( FILE, "file" );
928 MYCASE( DIRECTORY, "directory" );
929 MYCASE( VARIABLE, "variable" );
930 MYCASE( FLOAT, "float" );
931 MYCASE( TIME, "time" );
932 MYCASE( ADDRESS, "address" );
933 MYCASE( MUTEX, "mutex" );
934 MYCASE( LIST, "list" );
937 printf( " %c-o \"%s\" (%s",
938 i + 1 == vlc_internals( p_object )->i_vars ? '`' : '|',
939 p_var->psz_name, psz_type );
940 if( p_var->psz_text )
941 printf( ", %s", p_var->psz_text );
943 if( p_var->i_type & VLC_VAR_HASCHOICE )
944 printf( ", has choices" );
945 if( p_var->i_type & VLC_VAR_ISCOMMAND )
946 printf( ", command" );
947 if( p_var->i_entries )
948 printf( ", %d callbacks", p_var->i_entries );
949 switch( p_var->i_type & VLC_VAR_CLASS )
955 printf( ": %s", p_var->val.b_bool ? "true" : "false" );
957 case VLC_VAR_INTEGER:
958 printf( ": %d", p_var->val.i_int );
961 printf( ": \"%s\"", p_var->val.psz_string );
964 printf( ": %f", p_var->val.f_float );
967 printf( ": %"PRIi64, (int64_t)p_var->val.i_time );
969 case VLC_VAR_ADDRESS:
970 printf( ": %p", p_var->val.p_address );
979 libvlc_unlock (p_this->p_libvlc);
981 if( *newval.psz_string )
983 vlc_object_release( p_object );
990 /*****************************************************************************
991 * vlc_list_release: free a list previously allocated by vlc_list_find
992 *****************************************************************************
993 * This function decreases the refcount of all objects in the list and
995 *****************************************************************************/
996 void vlc_list_release( vlc_list_t *p_list )
1000 for( i_index = 0; i_index < p_list->i_count; i_index++ )
1002 vlc_object_release( p_list->p_values[i_index].p_object );
1005 free( p_list->p_values );
1009 /*****************************************************************************
1010 * dump an object. (Debug function)
1011 *****************************************************************************/
1013 static void vlc_object_dump( vlc_object_t *p_this )
1015 char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
1018 DumpStructure( p_this, 0, psz_foo );
1022 /* Following functions are local */
1024 static vlc_object_t * FindObject( vlc_object_t *p_this, int i_type, int i_mode )
1027 vlc_object_t *p_tmp;
1029 switch( i_mode & 0x000f )
1032 p_tmp = p_this->p_parent;
1035 if( vlc_internals( p_tmp )->i_object_type == i_type )
1037 vlc_object_hold( p_tmp );
1042 return FindObject( p_tmp, i_type, i_mode );
1048 for( i = vlc_internals( p_this )->i_children; i--; )
1050 p_tmp = vlc_internals( p_this )->pp_children[i];
1051 if( vlc_internals( p_tmp )->i_object_type == i_type )
1053 vlc_object_hold( p_tmp );
1056 else if( vlc_internals( p_tmp )->i_children )
1058 p_tmp = FindObject( p_tmp, i_type, i_mode );
1068 /* Handled in vlc_object_find */
1075 static vlc_object_t * FindObjectName( vlc_object_t *p_this,
1076 const char *psz_name,
1080 vlc_object_t *p_tmp;
1082 switch( i_mode & 0x000f )
1085 p_tmp = p_this->p_parent;
1088 if( p_tmp->psz_object_name
1089 && !strcmp( p_tmp->psz_object_name, psz_name ) )
1091 vlc_object_hold( p_tmp );
1096 return FindObjectName( p_tmp, psz_name, i_mode );
1102 for( i = vlc_internals( p_this )->i_children; i--; )
1104 p_tmp = vlc_internals( p_this )->pp_children[i];
1105 if( p_tmp->psz_object_name
1106 && !strcmp( p_tmp->psz_object_name, psz_name ) )
1108 vlc_object_hold( p_tmp );
1111 else if( vlc_internals( p_tmp )->i_children )
1113 p_tmp = FindObjectName( p_tmp, psz_name, i_mode );
1123 /* Handled in vlc_object_find */
1131 static void PrintObject( vlc_object_t *p_this, const char *psz_prefix )
1133 char psz_children[20], psz_refcount[20], psz_thread[30], psz_name[50],
1136 int canc = vlc_savecancel ();
1137 memset( &psz_name, 0, sizeof(psz_name) );
1138 if( p_this->psz_object_name )
1140 snprintf( psz_name, 49, " \"%s\"", p_this->psz_object_name );
1142 psz_name[48] = '\"';
1145 psz_children[0] = '\0';
1146 switch( vlc_internals( p_this )->i_children )
1151 strcpy( psz_children, ", 1 child" );
1154 snprintf( psz_children, 19, ", %i children",
1155 vlc_internals( p_this )->i_children );
1159 psz_refcount[0] = '\0';
1160 if( vlc_internals( p_this )->i_refcount > 0 )
1161 snprintf( psz_refcount, 19, ", refcount %u",
1162 vlc_internals( p_this )->i_refcount );
1164 psz_thread[0] = '\0';
1165 if( vlc_internals( p_this )->b_thread )
1166 snprintf( psz_thread, 29, " (thread %lu)",
1167 (unsigned long)vlc_internals( p_this )->thread_id );
1169 psz_parent[0] = '\0';
1170 if( p_this->p_parent )
1171 snprintf( psz_parent, 19, ", parent %p", p_this->p_parent );
1173 printf( " %so %p %s%s%s%s%s%s\n", psz_prefix,
1174 p_this, p_this->psz_object_type,
1175 psz_name, psz_thread, psz_refcount, psz_children,
1177 vlc_restorecancel (canc);
1180 static void DumpStructure( vlc_object_t *p_this, int i_level, char *psz_foo )
1183 char i_back = psz_foo[i_level];
1184 psz_foo[i_level] = '\0';
1186 PrintObject( p_this, psz_foo );
1188 psz_foo[i_level] = i_back;
1190 if( i_level / 2 >= MAX_DUMPSTRUCTURE_DEPTH )
1192 msg_Warn( p_this, "structure tree is too deep" );
1196 for( i = 0 ; i < vlc_internals( p_this )->i_children ; i++ )
1200 psz_foo[i_level-1] = ' ';
1202 if( psz_foo[i_level-2] == '`' )
1204 psz_foo[i_level-2] = ' ';
1208 if( i == vlc_internals( p_this )->i_children - 1 )
1210 psz_foo[i_level] = '`';
1214 psz_foo[i_level] = '|';
1217 psz_foo[i_level+1] = '-';
1218 psz_foo[i_level+2] = '\0';
1220 DumpStructure( vlc_internals( p_this )->pp_children[i], i_level + 2,
1225 static vlc_list_t * NewList( int i_count )
1227 vlc_list_t * p_list = (vlc_list_t *)malloc( sizeof( vlc_list_t ) );
1228 if( p_list == NULL )
1233 p_list->i_count = i_count;
1237 p_list->p_values = NULL;
1241 p_list->p_values = malloc( i_count * sizeof( vlc_value_t ) );
1242 if( p_list->p_values == NULL )
1244 p_list->i_count = 0;
1251 static void ListReplace( vlc_list_t *p_list, vlc_object_t *p_object,
1254 if( p_list == NULL || i_index >= p_list->i_count )
1259 vlc_object_hold( p_object );
1261 p_list->p_values[i_index].p_object = p_object;
1266 /*static void ListAppend( vlc_list_t *p_list, vlc_object_t *p_object )
1268 if( p_list == NULL )
1273 p_list->p_values = realloc( p_list->p_values, (p_list->i_count + 1)
1274 * sizeof( vlc_value_t ) );
1275 if( p_list->p_values == NULL )
1277 p_list->i_count = 0;
1281 vlc_object_hold( p_object );
1283 p_list->p_values[p_list->i_count].p_object = p_object;
1289 static int CountChildren( vlc_object_t *p_this, int i_type )
1291 vlc_object_t *p_tmp;
1294 for( i = 0; i < vlc_internals( p_this )->i_children; i++ )
1296 p_tmp = vlc_internals( p_this )->pp_children[i];
1298 if( vlc_internals( p_tmp )->i_object_type == i_type )
1302 i_count += CountChildren( p_tmp, i_type );
1308 static void ListChildren( vlc_list_t *p_list, vlc_object_t *p_this, int i_type )
1310 vlc_object_t *p_tmp;
1313 for( i = 0; i < vlc_internals( p_this )->i_children; i++ )
1315 p_tmp = vlc_internals( p_this )->pp_children[i];
1317 if( vlc_internals( p_tmp )->i_object_type == i_type )
1318 ListReplace( p_list, p_tmp, p_list->i_count++ );
1320 ListChildren( p_list, p_tmp, i_type );