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 if ((internals->pipes[0] = eventfd (0, 0)) == -1)
421 if (pipe (internals->pipes))
422 internals->pipes[0] = internals->pipes[1] = -1;
425 if (internals->pipes[0] != -1 && obj->b_die)
426 { /* Race condition: vlc_object_kill() already invoked! */
427 msg_Dbg (obj, "waitpipe: object already dying");
428 write (internals->pipes[1], &(uint64_t){ 1 }, sizeof (uint64_t));
431 vlc_object_unlock (obj);
432 return internals->pipes[0];
437 * Suspends until another thread calls vlc_object_signal_unlocked().
438 * The thread may be woken up earlier due to limitations of the underlying
441 * In new code, please use vlc_cond_wait() instead.
443 * This function is a cancellation point. In case of cancellation, the object
444 * will be in locked state.
446 void __vlc_object_wait( vlc_object_t *obj )
448 vlc_object_internals_t *priv = vlc_internals( obj );
449 vlc_assert_locked( &priv->lock);
450 vlc_cond_wait( &priv->wait, &priv->lock );
455 * Wakes up one thread waiting on the object. If no thread are (yet) waiting,
458 * Please do not use this function in new code as we are trying to untangle
459 * objects and threads. Use vlc_cond_wait() instead.
461 void __vlc_object_signal_unlocked( vlc_object_t *obj )
463 vlc_assert_locked (&(vlc_internals(obj)->lock));
464 vlc_cond_signal( &(vlc_internals(obj)->wait) );
469 * Requests termination of an object, cancels the object thread, and make the
470 * object wait pipe (if it exists) readable. Not a cancellation point.
472 void __vlc_object_kill( vlc_object_t *p_this )
474 vlc_object_internals_t *priv = vlc_internals( p_this );
477 vlc_thread_cancel( p_this );
478 vlc_object_lock( p_this );
482 p_this->b_die = true;
485 vlc_cond_broadcast (&priv->wait);
486 /* This also serves as a memory barrier toward vlc_object_alive(): */
487 vlc_object_unlock( p_this );
491 int canc = vlc_savecancel ();
493 /* write _after_ setting b_die, so vlc_object_alive() returns false */
494 write (fd, &(uint64_t){ 1 }, sizeof (uint64_t));
495 msg_Dbg (p_this, "waitpipe: object killed");
496 vlc_restorecancel (canc);
501 /*****************************************************************************
502 * find a typed object and increment its refcount
503 *****************************************************************************
504 * This function recursively looks for a given object type. i_mode can be one
505 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
506 *****************************************************************************/
507 void * __vlc_object_find( vlc_object_t *p_this, int i_type, int i_mode )
509 vlc_object_t *p_found;
511 /* If we are of the requested type ourselves, don't look further */
512 if( !(i_mode & FIND_STRICT)
513 && vlc_internals (p_this)->i_object_type == i_type )
515 vlc_object_hold( p_this );
519 /* Otherwise, recursively look for the object */
520 if ((i_mode & 0x000f) == FIND_ANYWHERE)
521 return vlc_object_find (p_this->p_libvlc, i_type,
522 (i_mode & ~0x000f)|FIND_CHILD);
524 libvlc_lock (p_this->p_libvlc);
525 p_found = FindObject( p_this, i_type, i_mode );
526 libvlc_unlock (p_this->p_libvlc);
530 #undef vlc_object_find_name
532 * Finds a named object and increment its reference count.
533 * Beware that objects found in this manner can be "owned" by another thread,
534 * be of _any_ type, and be attached to any module (if any). With such an
535 * object reference, you can set or get object variables, emit log messages,
536 * and read write-once object parameters (psz_object_type, etc).
537 * You CANNOT cast the object to a more specific object type, and you
538 * definitely cannot invoke object type-specific callbacks with this.
540 * @param p_this object to search from
541 * @param psz_name name of the object to search for
542 * @param i_mode search direction: FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
544 * @return a matching object (must be released by the caller),
547 vlc_object_t *vlc_object_find_name( vlc_object_t *p_this,
548 const char *psz_name, int i_mode )
550 vlc_object_t *p_found;
552 /* If have the requested name ourselves, don't look further */
553 if( !(i_mode & FIND_STRICT)
554 && p_this->psz_object_name
555 && !strcmp( p_this->psz_object_name, psz_name ) )
557 vlc_object_hold( p_this );
561 libvlc_lock (p_this->p_libvlc);
563 /* Otherwise, recursively look for the object */
564 if( (i_mode & 0x000f) == FIND_ANYWHERE )
566 vlc_object_t *p_root = p_this;
569 while( p_root->p_parent != NULL &&
570 p_root != VLC_OBJECT( p_this->p_libvlc ) )
572 p_root = p_root->p_parent;
575 p_found = FindObjectName( p_root, psz_name,
576 (i_mode & ~0x000f)|FIND_CHILD );
577 if( p_found == NULL && p_root != VLC_OBJECT( p_this->p_libvlc ) )
579 p_found = FindObjectName( VLC_OBJECT( p_this->p_libvlc ),
580 psz_name, (i_mode & ~0x000f)|FIND_CHILD );
585 p_found = FindObjectName( p_this, psz_name, i_mode );
588 libvlc_unlock (p_this->p_libvlc);
593 * Increment an object reference counter.
595 void * __vlc_object_hold( vlc_object_t *p_this )
597 vlc_object_internals_t *internals = vlc_internals( p_this );
599 vlc_spin_lock( &internals->ref_spin );
600 /* Avoid obvious freed object uses */
601 assert( internals->i_refcount > 0 );
602 /* Increment the counter */
603 internals->i_refcount++;
604 vlc_spin_unlock( &internals->ref_spin );
608 /*****************************************************************************
609 * Decrement an object refcount
610 * And destroy the object if its refcount reach zero.
611 *****************************************************************************/
612 void __vlc_object_release( vlc_object_t *p_this )
614 vlc_object_internals_t *internals = vlc_internals( p_this );
615 vlc_object_t *parent = NULL;
616 bool b_should_destroy;
618 vlc_spin_lock( &internals->ref_spin );
619 assert( internals->i_refcount > 0 );
621 if( internals->i_refcount > 1 )
624 /* There are still other references to the object */
625 internals->i_refcount--;
626 vlc_spin_unlock( &internals->ref_spin );
629 vlc_spin_unlock( &internals->ref_spin );
632 /* Remember that we cannot hold the spin while waiting on the mutex */
633 libvlc_lock (p_this->p_libvlc);
634 /* Take the spin again. Note that another thread may have held the
635 * object in the (very short) mean time. */
636 vlc_spin_lock( &internals->ref_spin );
637 b_should_destroy = --internals->i_refcount == 0;
638 vlc_spin_unlock( &internals->ref_spin );
640 if( b_should_destroy )
642 /* We have no children */
643 assert (internals->i_children == 0);
644 parent = p_this->p_parent;
647 if( VLC_OBJECT(p_this->p_libvlc) == p_this )
650 vlc_object_t *leaked = internals->next;
651 while( leaked != p_this )
653 /* We are leaking this object */
655 "ERROR: leaking object (%p, type:%s, name:%s)\n",
656 leaked, leaked->psz_object_type,
657 leaked->psz_object_name );
658 /* Dump object to ease debugging */
659 vlc_object_dump( leaked );
661 leaked = vlc_internals (leaked)->next;
664 if( internals->next != p_this )
665 /* Dump libvlc object to ease debugging */
666 vlc_object_dump( p_this );
669 /* Remove the object from object list
670 * so that it cannot be encountered by vlc_object_get() */
671 vlc_internals (internals->next)->prev = internals->prev;
672 vlc_internals (internals->prev)->next = internals->next;
675 /* Detach from parent to protect against FIND_CHILDREN */
676 vlc_object_detach_unlocked (p_this);
678 libvlc_unlock (p_this->p_libvlc);
680 if( b_should_destroy )
684 canc = vlc_savecancel ();
685 vlc_object_destroy( p_this );
686 vlc_restorecancel (canc);
688 vlc_object_release (parent);
693 ****************************************************************************
694 * attach object to a parent object
695 *****************************************************************************
696 * This function sets p_this as a child of p_parent, and p_parent as a parent
697 * of p_this. This link can be undone using vlc_object_detach.
698 *****************************************************************************/
699 void __vlc_object_attach( vlc_object_t *p_this, vlc_object_t *p_parent )
701 if( !p_this ) return;
703 vlc_object_hold (p_parent);
704 libvlc_lock (p_this->p_libvlc);
706 /* Attach the parent to its child */
707 assert (!p_this->p_parent);
708 p_this->p_parent = p_parent;
710 /* Attach the child to its parent */
711 vlc_object_internals_t *priv = vlc_internals( p_parent );
712 INSERT_ELEM( priv->pp_children, priv->i_children, priv->i_children,
714 libvlc_unlock (p_this->p_libvlc);
718 static void vlc_object_detach_unlocked (vlc_object_t *p_this)
720 if (p_this->p_parent == NULL)
723 vlc_object_internals_t *priv = vlc_internals( p_this->p_parent );
727 /* Remove p_this's parent */
728 p_this->p_parent = NULL;
730 /* Remove all of p_parent's children which are p_this */
731 for( i_index = priv->i_children ; i_index-- ; )
733 if( priv->pp_children[i_index] == p_this )
736 for( i = i_index ; i < priv->i_children ; i++ )
737 priv->pp_children[i] = priv->pp_children[i+1];
741 if( priv->i_children )
743 vlc_object_t **pp_children = (vlc_object_t **)
744 realloc( priv->pp_children,
745 priv->i_children * sizeof(vlc_object_t *) );
747 priv->pp_children = pp_children;
751 /* Special case - don't realloc() to zero to avoid leaking */
752 free( priv->pp_children );
753 priv->pp_children = NULL;
759 ****************************************************************************
760 * detach object from its parent
761 *****************************************************************************
762 * This function removes all links between an object and its parent.
763 *****************************************************************************/
764 void __vlc_object_detach( vlc_object_t *p_this )
766 vlc_object_t *p_parent;
767 if( !p_this ) return;
769 libvlc_lock (p_this->p_libvlc);
770 p_parent = p_this->p_parent;
772 vlc_object_detach_unlocked( p_this );
773 libvlc_unlock (p_this->p_libvlc);
776 vlc_object_release (p_parent);
781 ****************************************************************************
782 * find a list typed objects and increment their refcount
783 *****************************************************************************
784 * This function recursively looks for a given object type. i_mode can be one
785 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
786 *****************************************************************************/
787 vlc_list_t * __vlc_list_find( vlc_object_t *p_this, int i_type, int i_mode )
792 /* Look for the objects */
793 switch( i_mode & 0x000f )
796 return vlc_list_find (p_this->p_libvlc, i_type, FIND_CHILD);
799 libvlc_lock (p_this->p_libvlc);
800 i_count = CountChildren( p_this, i_type );
801 p_list = NewList( i_count );
803 /* Check allocation was successful */
804 if( p_list->i_count != i_count )
806 libvlc_unlock (p_this->p_libvlc);
812 ListChildren( p_list, p_this, i_type );
813 libvlc_unlock (p_this->p_libvlc);
817 msg_Err( p_this, "unimplemented!" );
818 p_list = NewList( 0 );
826 * Gets the list of children of an objects, and increment their reference
828 * @return a list (possibly empty) or NULL in case of error.
830 vlc_list_t *__vlc_list_children( vlc_object_t *obj )
833 vlc_object_internals_t *priv = vlc_internals( obj );
835 libvlc_lock (obj->p_libvlc);
836 l = NewList( priv->i_children );
837 for (int i = 0; i < l->i_count; i++)
839 vlc_object_hold( priv->pp_children[i] );
840 l->p_values[i].p_object = priv->pp_children[i];
842 libvlc_unlock (obj->p_libvlc);
846 /*****************************************************************************
847 * DumpCommand: print the current vlc structure
848 *****************************************************************************
849 * This function prints either an ASCII tree showing the connections between
850 * vlc objects, and additional information such as their refcount, thread ID,
851 * etc. (command "tree"), or the same data as a simple list (command "list").
852 *****************************************************************************/
853 static int DumpCommand( vlc_object_t *p_this, char const *psz_cmd,
854 vlc_value_t oldval, vlc_value_t newval, void *p_data )
856 libvlc_int_t *p_libvlc = p_this->p_libvlc;
858 (void)oldval; (void)p_data;
859 if( *psz_cmd == 'l' )
861 vlc_object_t *cur = VLC_OBJECT (p_libvlc);
863 libvlc_lock (p_this->p_libvlc);
866 PrintObject (cur, "");
867 cur = vlc_internals (cur)->next;
869 while (cur != VLC_OBJECT(p_libvlc));
870 libvlc_unlock (p_this->p_libvlc);
874 vlc_object_t *p_object = NULL;
876 if( *newval.psz_string )
878 /* try using the object's name to find it */
879 p_object = vlc_object_find_name( p_this, newval.psz_string,
887 libvlc_lock (p_this->p_libvlc);
888 if( *psz_cmd == 't' )
890 char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
893 p_object = VLC_OBJECT(p_this->p_libvlc);
896 DumpStructure( p_object, 0, psz_foo );
898 else if( *psz_cmd == 'v' )
903 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
905 PrintObject( p_object, "" );
907 if( !vlc_internals( p_object )->i_vars )
908 printf( " `-o No variables\n" );
909 for( i = 0; i < vlc_internals( p_object )->i_vars; i++ )
911 variable_t *p_var = vlc_internals( p_object )->p_vars + i;
913 const char *psz_type = "unknown";
914 switch( p_var->i_type & VLC_VAR_TYPE )
916 #define MYCASE( type, nice ) \
917 case VLC_VAR_ ## type: \
920 MYCASE( VOID, "void" );
921 MYCASE( BOOL, "bool" );
922 MYCASE( INTEGER, "integer" );
923 MYCASE( HOTKEY, "hotkey" );
924 MYCASE( STRING, "string" );
925 MYCASE( MODULE, "module" );
926 MYCASE( FILE, "file" );
927 MYCASE( DIRECTORY, "directory" );
928 MYCASE( VARIABLE, "variable" );
929 MYCASE( FLOAT, "float" );
930 MYCASE( TIME, "time" );
931 MYCASE( ADDRESS, "address" );
932 MYCASE( MUTEX, "mutex" );
933 MYCASE( LIST, "list" );
936 printf( " %c-o \"%s\" (%s",
937 i + 1 == vlc_internals( p_object )->i_vars ? '`' : '|',
938 p_var->psz_name, psz_type );
939 if( p_var->psz_text )
940 printf( ", %s", p_var->psz_text );
942 if( p_var->i_type & VLC_VAR_HASCHOICE )
943 printf( ", has choices" );
944 if( p_var->i_type & VLC_VAR_ISCOMMAND )
945 printf( ", command" );
946 if( p_var->i_entries )
947 printf( ", %d callbacks", p_var->i_entries );
948 switch( p_var->i_type & VLC_VAR_CLASS )
954 printf( ": %s", p_var->val.b_bool ? "true" : "false" );
956 case VLC_VAR_INTEGER:
957 printf( ": %d", p_var->val.i_int );
960 printf( ": \"%s\"", p_var->val.psz_string );
963 printf( ": %f", p_var->val.f_float );
966 printf( ": %"PRIi64, (int64_t)p_var->val.i_time );
968 case VLC_VAR_ADDRESS:
969 printf( ": %p", p_var->val.p_address );
978 libvlc_unlock (p_this->p_libvlc);
980 if( *newval.psz_string )
982 vlc_object_release( p_object );
989 /*****************************************************************************
990 * vlc_list_release: free a list previously allocated by vlc_list_find
991 *****************************************************************************
992 * This function decreases the refcount of all objects in the list and
994 *****************************************************************************/
995 void vlc_list_release( vlc_list_t *p_list )
999 for( i_index = 0; i_index < p_list->i_count; i_index++ )
1001 vlc_object_release( p_list->p_values[i_index].p_object );
1004 free( p_list->p_values );
1008 /*****************************************************************************
1009 * dump an object. (Debug function)
1010 *****************************************************************************/
1012 static void vlc_object_dump( vlc_object_t *p_this )
1014 char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
1017 DumpStructure( p_this, 0, psz_foo );
1021 /* Following functions are local */
1023 static vlc_object_t * FindObject( vlc_object_t *p_this, int i_type, int i_mode )
1026 vlc_object_t *p_tmp;
1028 switch( i_mode & 0x000f )
1031 p_tmp = p_this->p_parent;
1034 if( vlc_internals( p_tmp )->i_object_type == i_type )
1036 vlc_object_hold( p_tmp );
1041 return FindObject( p_tmp, i_type, i_mode );
1047 for( i = vlc_internals( p_this )->i_children; i--; )
1049 p_tmp = vlc_internals( p_this )->pp_children[i];
1050 if( vlc_internals( p_tmp )->i_object_type == i_type )
1052 vlc_object_hold( p_tmp );
1055 else if( vlc_internals( p_tmp )->i_children )
1057 p_tmp = FindObject( p_tmp, i_type, i_mode );
1067 /* Handled in vlc_object_find */
1074 static vlc_object_t * FindObjectName( vlc_object_t *p_this,
1075 const char *psz_name,
1079 vlc_object_t *p_tmp;
1081 switch( i_mode & 0x000f )
1084 p_tmp = p_this->p_parent;
1087 if( p_tmp->psz_object_name
1088 && !strcmp( p_tmp->psz_object_name, psz_name ) )
1090 vlc_object_hold( p_tmp );
1095 return FindObjectName( p_tmp, psz_name, i_mode );
1101 for( i = vlc_internals( p_this )->i_children; i--; )
1103 p_tmp = vlc_internals( p_this )->pp_children[i];
1104 if( p_tmp->psz_object_name
1105 && !strcmp( p_tmp->psz_object_name, psz_name ) )
1107 vlc_object_hold( p_tmp );
1110 else if( vlc_internals( p_tmp )->i_children )
1112 p_tmp = FindObjectName( p_tmp, psz_name, i_mode );
1122 /* Handled in vlc_object_find */
1130 static void PrintObject( vlc_object_t *p_this, const char *psz_prefix )
1132 char psz_children[20], psz_refcount[20], psz_thread[30], psz_name[50],
1135 int canc = vlc_savecancel ();
1136 memset( &psz_name, 0, sizeof(psz_name) );
1137 if( p_this->psz_object_name )
1139 snprintf( psz_name, 49, " \"%s\"", p_this->psz_object_name );
1141 psz_name[48] = '\"';
1144 psz_children[0] = '\0';
1145 switch( vlc_internals( p_this )->i_children )
1150 strcpy( psz_children, ", 1 child" );
1153 snprintf( psz_children, 19, ", %i children",
1154 vlc_internals( p_this )->i_children );
1158 psz_refcount[0] = '\0';
1159 if( vlc_internals( p_this )->i_refcount > 0 )
1160 snprintf( psz_refcount, 19, ", refcount %u",
1161 vlc_internals( p_this )->i_refcount );
1163 psz_thread[0] = '\0';
1164 if( vlc_internals( p_this )->b_thread )
1165 snprintf( psz_thread, 29, " (thread %lu)",
1166 (unsigned long)vlc_internals( p_this )->thread_id );
1168 psz_parent[0] = '\0';
1169 if( p_this->p_parent )
1170 snprintf( psz_parent, 19, ", parent %p", p_this->p_parent );
1172 printf( " %so %p %s%s%s%s%s%s\n", psz_prefix,
1173 p_this, p_this->psz_object_type,
1174 psz_name, psz_thread, psz_refcount, psz_children,
1176 vlc_restorecancel (canc);
1179 static void DumpStructure( vlc_object_t *p_this, int i_level, char *psz_foo )
1182 char i_back = psz_foo[i_level];
1183 psz_foo[i_level] = '\0';
1185 PrintObject( p_this, psz_foo );
1187 psz_foo[i_level] = i_back;
1189 if( i_level / 2 >= MAX_DUMPSTRUCTURE_DEPTH )
1191 msg_Warn( p_this, "structure tree is too deep" );
1195 for( i = 0 ; i < vlc_internals( p_this )->i_children ; i++ )
1199 psz_foo[i_level-1] = ' ';
1201 if( psz_foo[i_level-2] == '`' )
1203 psz_foo[i_level-2] = ' ';
1207 if( i == vlc_internals( p_this )->i_children - 1 )
1209 psz_foo[i_level] = '`';
1213 psz_foo[i_level] = '|';
1216 psz_foo[i_level+1] = '-';
1217 psz_foo[i_level+2] = '\0';
1219 DumpStructure( vlc_internals( p_this )->pp_children[i], i_level + 2,
1224 static vlc_list_t * NewList( int i_count )
1226 vlc_list_t * p_list = (vlc_list_t *)malloc( sizeof( vlc_list_t ) );
1227 if( p_list == NULL )
1232 p_list->i_count = i_count;
1236 p_list->p_values = NULL;
1240 p_list->p_values = malloc( i_count * sizeof( vlc_value_t ) );
1241 if( p_list->p_values == NULL )
1243 p_list->i_count = 0;
1250 static void ListReplace( vlc_list_t *p_list, vlc_object_t *p_object,
1253 if( p_list == NULL || i_index >= p_list->i_count )
1258 vlc_object_hold( p_object );
1260 p_list->p_values[i_index].p_object = p_object;
1265 /*static void ListAppend( vlc_list_t *p_list, vlc_object_t *p_object )
1267 if( p_list == NULL )
1272 p_list->p_values = realloc( p_list->p_values, (p_list->i_count + 1)
1273 * sizeof( vlc_value_t ) );
1274 if( p_list->p_values == NULL )
1276 p_list->i_count = 0;
1280 vlc_object_hold( p_object );
1282 p_list->p_values[p_list->i_count].p_object = p_object;
1288 static int CountChildren( vlc_object_t *p_this, int i_type )
1290 vlc_object_t *p_tmp;
1293 for( i = 0; i < vlc_internals( p_this )->i_children; i++ )
1295 p_tmp = vlc_internals( p_this )->pp_children[i];
1297 if( vlc_internals( p_tmp )->i_object_type == i_type )
1301 i_count += CountChildren( p_tmp, i_type );
1307 static void ListChildren( vlc_list_t *p_list, vlc_object_t *p_this, int i_type )
1309 vlc_object_t *p_tmp;
1312 for( i = 0; i < vlc_internals( p_this )->i_children; i++ )
1314 p_tmp = vlc_internals( p_this )->pp_children[i];
1316 if( vlc_internals( p_tmp )->i_object_type == i_type )
1317 ListReplace( p_list, p_tmp, p_list->i_count++ );
1319 ListChildren( p_list, p_tmp, i_type );