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);
87 static void vlc_object_dump( vlc_object_t *p_this );
90 /*****************************************************************************
91 * Local structure lock
92 *****************************************************************************/
93 static void close_nocancel (int fd)
95 int canc = vlc_savecancel ();
97 vlc_restorecancel (canc);
100 static void libvlc_lock (libvlc_int_t *p_libvlc)
102 vlc_mutex_lock (&(libvlc_priv (p_libvlc)->structure_lock));
105 static void libvlc_unlock (libvlc_int_t *p_libvlc)
107 vlc_mutex_unlock (&(libvlc_priv (p_libvlc)->structure_lock));
110 void *__vlc_custom_create( vlc_object_t *p_this, size_t i_size,
111 int i_type, const char *psz_type )
114 vlc_object_internals_t *p_priv;
117 * VLC objects are laid out as follow:
118 * - first the LibVLC-private per-object data,
119 * - then VLC_COMMON members from vlc_object_t,
120 * - finally, the type-specific data (if any).
122 * This function initializes the LibVLC and common data,
123 * and zeroes the rest.
125 p_priv = calloc( 1, sizeof( *p_priv ) + i_size );
129 assert (i_size >= sizeof (vlc_object_t));
130 p_new = (vlc_object_t *)(p_priv + 1);
132 p_new->i_object_type = i_type;
133 p_new->psz_object_type = psz_type;
134 p_new->psz_object_name = NULL;
136 p_new->b_die = false;
137 p_new->b_error = false;
138 p_new->b_force = false;
140 p_new->psz_header = NULL;
143 p_new->i_flags = p_this->i_flags
144 & (OBJECT_FLAGS_NODBG|OBJECT_FLAGS_QUIET|OBJECT_FLAGS_NOINTERACT);
146 p_priv->p_vars = calloc( 16, sizeof( variable_t ) );
148 if( !p_priv->p_vars )
156 libvlc_int_t *self = (libvlc_int_t*)p_new;
157 p_new->p_libvlc = self;
158 vlc_mutex_init (&(libvlc_priv (self)->structure_lock));
159 p_this = p_priv->next = p_priv->prev = p_new;
162 p_new->p_libvlc = p_this->p_libvlc;
164 vlc_spin_init( &p_priv->ref_spin );
165 p_priv->i_refcount = 1;
166 p_priv->pf_destructor = NULL;
167 p_priv->b_thread = false;
168 p_new->p_parent = NULL;
169 p_priv->pp_children = NULL;
170 p_priv->i_children = 0;
172 p_new->p_private = NULL;
174 /* Initialize mutexes and condvars */
175 vlc_mutex_init( &p_priv->lock );
176 vlc_cond_init( &p_priv->wait );
177 vlc_mutex_init( &p_priv->var_lock );
178 vlc_cond_init( &p_priv->var_wait );
179 p_priv->pipes[0] = p_priv->pipes[1] = -1;
181 p_priv->next = p_this;
182 libvlc_lock (p_new->p_libvlc);
183 p_priv->prev = vlc_internals (p_this)->prev;
184 vlc_internals (p_this)->prev = p_new;
185 vlc_internals (p_priv->prev)->next = p_new;
186 libvlc_unlock (p_new->p_libvlc);
188 if (p_new == VLC_OBJECT(p_new->p_libvlc))
189 { /* TODO: should be in src/libvlc.c */
190 int canc = vlc_savecancel ();
191 var_Create( p_new, "list", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
192 var_AddCallback( p_new, "list", DumpCommand, NULL );
193 var_Create( p_new, "tree", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
194 var_AddCallback( p_new, "tree", DumpCommand, NULL );
195 var_Create( p_new, "vars", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
196 var_AddCallback( p_new, "vars", DumpCommand, NULL );
197 vlc_restorecancel (canc);
205 * Allocates and initializes a vlc object.
207 * @param i_type known object type (all of them are negative integer values),
208 * or object byte size (always positive).
210 * @return the new object, or NULL on error.
212 void * __vlc_object_create( vlc_object_t *p_this, int i_type )
214 const char * psz_type;
219 case VLC_OBJECT_INTF:
220 i_size = sizeof(intf_thread_t);
221 psz_type = "interface";
223 case VLC_OBJECT_DECODER:
224 i_size = sizeof(decoder_t);
225 psz_type = "decoder";
227 case VLC_OBJECT_PACKETIZER:
228 i_size = sizeof(decoder_t);
229 psz_type = "packetizer";
231 case VLC_OBJECT_ENCODER:
232 i_size = sizeof(encoder_t);
233 psz_type = "encoder";
235 case VLC_OBJECT_AOUT:
236 i_size = sizeof(aout_instance_t);
237 psz_type = "audio output";
240 assert( i_type > 0 ); /* unknown type?! */
242 i_type = VLC_OBJECT_GENERIC;
243 psz_type = "generic";
247 return vlc_custom_create( p_this, i_size, i_type, psz_type );
252 ****************************************************************************
253 * Set the destructor of a vlc object
255 * This function sets the destructor of the vlc object. It will be called
256 * when the object is destroyed when the its refcount reaches 0.
257 * (It is called by the internal function vlc_object_destroy())
258 *****************************************************************************/
259 void __vlc_object_set_destructor( vlc_object_t *p_this,
260 vlc_destructor_t pf_destructor )
262 vlc_object_internals_t *p_priv = vlc_internals(p_this );
263 p_priv->pf_destructor = pf_destructor;
267 ****************************************************************************
268 * Destroy a vlc object (Internal)
270 * This function destroys an object that has been previously allocated with
271 * vlc_object_create. The object's refcount must be zero and it must not be
272 * attached to other objects in any way.
274 * This function must be called with cancellation disabled (currently).
275 *****************************************************************************/
276 static void vlc_object_destroy( vlc_object_t *p_this )
278 vlc_object_internals_t *p_priv = vlc_internals( p_this );
280 /* Objects are always detached beforehand */
281 assert( !p_this->p_parent );
283 /* Send a kill to the object's thread if applicable */
284 vlc_object_kill( p_this );
286 /* Call the custom "subclass" destructor */
287 if( p_priv->pf_destructor )
288 p_priv->pf_destructor( p_this );
290 /* Any thread must have been cleaned up at this point. */
291 assert( !p_priv->b_thread );
293 /* Destroy the associated variables, starting from the end so that
294 * no memmove calls have to be done. */
295 while( p_priv->i_vars )
297 var_Destroy( p_this, p_priv->p_vars[p_priv->i_vars - 1].psz_name );
300 free( p_priv->p_vars );
301 vlc_cond_destroy( &p_priv->var_wait );
302 vlc_mutex_destroy( &p_priv->var_lock );
304 free( p_this->psz_header );
306 FREENULL( p_this->psz_object_name );
308 vlc_spin_destroy( &p_priv->ref_spin );
309 vlc_mutex_destroy( &p_priv->lock );
310 vlc_cond_destroy( &p_priv->wait );
311 if( p_priv->pipes[1] != -1 && p_priv->pipes[1] != p_priv->pipes[0] )
312 close( p_priv->pipes[1] );
313 if( p_priv->pipes[0] != -1 )
314 close( p_priv->pipes[0] );
315 if( VLC_OBJECT(p_this->p_libvlc) == p_this )
316 vlc_mutex_destroy (&(libvlc_priv ((libvlc_int_t *)p_this)->structure_lock));
322 /** Inter-object signaling */
324 void __vlc_object_lock( vlc_object_t *obj )
326 vlc_mutex_lock( &(vlc_internals(obj)->lock) );
329 void __vlc_object_unlock( vlc_object_t *obj )
331 vlc_assert_locked( &(vlc_internals(obj)->lock) );
332 vlc_mutex_unlock( &(vlc_internals(obj)->lock) );
334 void __vlc_object_assert_locked( vlc_object_t *obj )
336 vlc_assert_locked( &(vlc_internals(obj)->lock) );
340 # include <winsock2.h>
341 # include <ws2tcpip.h>
344 * select()-able pipes emulated using Winsock
346 static int pipe (int fd[2])
349 int addrlen = sizeof (addr);
351 SOCKET l = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP), a,
352 c = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
353 if ((l == INVALID_SOCKET) || (c == INVALID_SOCKET))
356 memset (&addr, 0, sizeof (addr));
357 addr.sin_family = AF_INET;
358 addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
359 if (bind (l, (PSOCKADDR)&addr, sizeof (addr))
360 || getsockname (l, (PSOCKADDR)&addr, &addrlen)
362 || connect (c, (PSOCKADDR)&addr, addrlen))
365 a = accept (l, NULL, NULL);
366 if (a == INVALID_SOCKET)
377 if (l != INVALID_SOCKET)
379 if (c != INVALID_SOCKET)
385 #define read( a, b, c ) recv (a, b, c, 0)
387 #define write( a, b, c ) send (a, b, c, 0)
389 #define close( a ) closesocket (a)
393 * Returns the readable end of a pipe that becomes readable once termination
394 * of the object is requested (vlc_object_kill()).
395 * This can be used to wake-up out of a select() or poll() event loop, such
396 * typically when doing network I/O.
398 * Note that the pipe will remain the same for the lifetime of the object.
399 * DO NOT read the pipe nor close it yourself. Ever.
401 * @param obj object that would be "killed"
402 * @return a readable pipe descriptor, or -1 on error.
404 int vlc_object_waitpipe( vlc_object_t *obj )
406 vlc_object_internals_t *internals = vlc_internals( obj );
408 vlc_object_lock (obj);
409 if (internals->pipes[0] == -1)
411 /* This can only ever happen if someone killed us without locking: */
412 assert (internals->pipes[1] == -1);
415 if ((internals->pipes[0] = internals->pipes[1] = eventfd (0, 0)) == -1)
417 if (pipe (internals->pipes))
418 internals->pipes[0] = internals->pipes[1] = -1;
421 { /* Race condition: vlc_object_kill() already invoked! */
422 msg_Dbg (obj, "waitpipe: object already dying");
423 write (internals->pipes[1], &(uint64_t){ 1 }, sizeof (uint64_t));
426 vlc_object_unlock (obj);
427 return internals->pipes[0];
432 * Suspends until another thread calls vlc_object_signal_unlocked().
433 * The thread may be woken up earlier due to limitations of the underlying
436 * In new code, please use vlc_cond_wait() instead.
438 * This function is a cancellation point. In case of cancellation, the object
439 * will be in locked state.
441 void __vlc_object_wait( vlc_object_t *obj )
443 vlc_object_internals_t *priv = vlc_internals( obj );
444 vlc_assert_locked( &priv->lock);
445 vlc_cond_wait( &priv->wait, &priv->lock );
450 * Wakes up one thread waiting on the object. If no thread are (yet) waiting,
453 * Please do not use this function in new code as we are trying to untangle
454 * objects and threads. Use vlc_cond_wait() instead.
456 void __vlc_object_signal_unlocked( vlc_object_t *obj )
458 vlc_assert_locked (&(vlc_internals(obj)->lock));
459 vlc_cond_signal( &(vlc_internals(obj)->wait) );
464 * Requests termination of an object, cancels the object thread, and make the
465 * object wait pipe (if it exists) readable. Not a cancellation point.
467 void __vlc_object_kill( vlc_object_t *p_this )
469 vlc_object_internals_t *priv = vlc_internals( p_this );
472 vlc_thread_cancel( p_this );
473 vlc_object_lock( p_this );
477 p_this->b_die = true;
480 vlc_cond_broadcast (&priv->wait);
481 /* This also serves as a memory barrier toward vlc_object_alive(): */
482 vlc_object_unlock( p_this );
486 int canc = vlc_savecancel ();
488 /* write _after_ setting b_die, so vlc_object_alive() returns false */
489 write (fd, &(uint64_t){ 1 }, sizeof (uint64_t));
490 msg_Dbg (p_this, "waitpipe: object killed");
491 vlc_restorecancel (canc);
496 /*****************************************************************************
497 * find a typed object and increment its refcount
498 *****************************************************************************
499 * This function recursively looks for a given object type. i_mode can be one
500 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
501 *****************************************************************************/
502 void * __vlc_object_find( vlc_object_t *p_this, int i_type, int i_mode )
504 vlc_object_t *p_found;
506 /* If we are of the requested type ourselves, don't look further */
507 if( !(i_mode & FIND_STRICT) && p_this->i_object_type == i_type )
509 vlc_object_hold( p_this );
513 /* Otherwise, recursively look for the object */
514 if ((i_mode & 0x000f) == FIND_ANYWHERE)
515 return vlc_object_find (p_this->p_libvlc, i_type,
516 (i_mode & ~0x000f)|FIND_CHILD);
518 libvlc_lock (p_this->p_libvlc);
519 p_found = FindObject( p_this, i_type, i_mode );
520 libvlc_unlock (p_this->p_libvlc);
524 #undef vlc_object_find_name
526 * Finds a named object and increment its reference count.
527 * Beware that objects found in this manner can be "owned" by another thread,
528 * be of _any_ type, and be attached to any module (if any). With such an
529 * object reference, you can set or get object variables, emit log messages,
530 * and read write-once object parameters (psz_object_type, etc).
531 * You CANNOT cast the object to a more specific object type, and you
532 * definitely cannot invoke object type-specific callbacks with this.
534 * @param p_this object to search from
535 * @param psz_name name of the object to search for
536 * @param i_mode search direction: FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
538 * @return a matching object (must be released by the caller),
541 vlc_object_t *vlc_object_find_name( vlc_object_t *p_this,
542 const char *psz_name, int i_mode )
544 vlc_object_t *p_found;
546 /* If have the requested name ourselves, don't look further */
547 if( !(i_mode & FIND_STRICT)
548 && p_this->psz_object_name
549 && !strcmp( p_this->psz_object_name, psz_name ) )
551 vlc_object_hold( p_this );
555 libvlc_lock (p_this->p_libvlc);
557 /* Otherwise, recursively look for the object */
558 if( (i_mode & 0x000f) == FIND_ANYWHERE )
560 vlc_object_t *p_root = p_this;
563 while( p_root->p_parent != NULL &&
564 p_root != VLC_OBJECT( p_this->p_libvlc ) )
566 p_root = p_root->p_parent;
569 p_found = FindObjectName( p_root, psz_name,
570 (i_mode & ~0x000f)|FIND_CHILD );
571 if( p_found == NULL && p_root != VLC_OBJECT( p_this->p_libvlc ) )
573 p_found = FindObjectName( VLC_OBJECT( p_this->p_libvlc ),
574 psz_name, (i_mode & ~0x000f)|FIND_CHILD );
579 p_found = FindObjectName( p_this, psz_name, i_mode );
582 libvlc_unlock (p_this->p_libvlc);
587 * Increment an object reference counter.
589 void * __vlc_object_hold( vlc_object_t *p_this )
591 vlc_object_internals_t *internals = vlc_internals( p_this );
593 vlc_spin_lock( &internals->ref_spin );
594 /* Avoid obvious freed object uses */
595 assert( internals->i_refcount > 0 );
596 /* Increment the counter */
597 internals->i_refcount++;
598 vlc_spin_unlock( &internals->ref_spin );
602 /*****************************************************************************
603 * Decrement an object refcount
604 * And destroy the object if its refcount reach zero.
605 *****************************************************************************/
606 void __vlc_object_release( vlc_object_t *p_this )
608 vlc_object_internals_t *internals = vlc_internals( p_this );
609 vlc_object_t *parent = NULL;
610 bool b_should_destroy;
612 vlc_spin_lock( &internals->ref_spin );
613 assert( internals->i_refcount > 0 );
615 if( internals->i_refcount > 1 )
618 /* There are still other references to the object */
619 internals->i_refcount--;
620 vlc_spin_unlock( &internals->ref_spin );
623 vlc_spin_unlock( &internals->ref_spin );
626 /* Remember that we cannot hold the spin while waiting on the mutex */
627 libvlc_lock (p_this->p_libvlc);
628 /* Take the spin again. Note that another thread may have held the
629 * object in the (very short) mean time. */
630 vlc_spin_lock( &internals->ref_spin );
631 b_should_destroy = --internals->i_refcount == 0;
632 vlc_spin_unlock( &internals->ref_spin );
634 if( b_should_destroy )
636 /* We have no children */
637 assert (internals->i_children == 0);
638 parent = p_this->p_parent;
641 if( VLC_OBJECT(p_this->p_libvlc) == p_this )
644 vlc_object_t *leaked = internals->next;
645 while( leaked != p_this )
647 /* We are leaking this object */
649 "ERROR: leaking object (%p, type:%s, name:%s)\n",
650 leaked, leaked->psz_object_type,
651 leaked->psz_object_name );
652 /* Dump object to ease debugging */
653 vlc_object_dump( leaked );
655 leaked = vlc_internals (leaked)->next;
658 if( internals->next != p_this )
659 /* Dump libvlc object to ease debugging */
660 vlc_object_dump( p_this );
663 /* Remove the object from object list
664 * so that it cannot be encountered by vlc_object_get() */
665 vlc_internals (internals->next)->prev = internals->prev;
666 vlc_internals (internals->prev)->next = internals->next;
669 /* Detach from parent to protect against FIND_CHILDREN */
670 vlc_object_detach_unlocked (p_this);
672 libvlc_unlock (p_this->p_libvlc);
674 if( b_should_destroy )
678 canc = vlc_savecancel ();
679 vlc_object_destroy( p_this );
680 vlc_restorecancel (canc);
682 vlc_object_release (parent);
687 ****************************************************************************
688 * attach object to a parent object
689 *****************************************************************************
690 * This function sets p_this as a child of p_parent, and p_parent as a parent
691 * of p_this. This link can be undone using vlc_object_detach.
692 *****************************************************************************/
693 void __vlc_object_attach( vlc_object_t *p_this, vlc_object_t *p_parent )
695 if( !p_this ) return;
697 vlc_object_hold (p_parent);
698 libvlc_lock (p_this->p_libvlc);
700 /* Attach the parent to its child */
701 assert (!p_this->p_parent);
702 p_this->p_parent = p_parent;
704 /* Attach the child to its parent */
705 vlc_object_internals_t *priv = vlc_internals( p_parent );
706 INSERT_ELEM( priv->pp_children, priv->i_children, priv->i_children,
708 libvlc_unlock (p_this->p_libvlc);
712 static void vlc_object_detach_unlocked (vlc_object_t *p_this)
714 if (p_this->p_parent == NULL)
717 vlc_object_internals_t *priv = vlc_internals( p_this->p_parent );
721 /* Remove p_this's parent */
722 p_this->p_parent = NULL;
724 /* Remove all of p_parent's children which are p_this */
725 for( i_index = priv->i_children ; i_index-- ; )
727 if( priv->pp_children[i_index] == p_this )
730 for( i = i_index ; i < priv->i_children ; i++ )
731 priv->pp_children[i] = priv->pp_children[i+1];
735 if( priv->i_children )
737 vlc_object_t **pp_children = (vlc_object_t **)
738 realloc( priv->pp_children,
739 priv->i_children * sizeof(vlc_object_t *) );
741 priv->pp_children = pp_children;
745 /* Special case - don't realloc() to zero to avoid leaking */
746 free( priv->pp_children );
747 priv->pp_children = NULL;
753 ****************************************************************************
754 * detach object from its parent
755 *****************************************************************************
756 * This function removes all links between an object and its parent.
757 *****************************************************************************/
758 void __vlc_object_detach( vlc_object_t *p_this )
760 vlc_object_t *p_parent;
761 if( !p_this ) return;
763 libvlc_lock (p_this->p_libvlc);
764 p_parent = p_this->p_parent;
766 vlc_object_detach_unlocked( p_this );
767 libvlc_unlock (p_this->p_libvlc);
770 vlc_object_release (p_parent);
775 ****************************************************************************
776 * find a list typed objects and increment their refcount
777 *****************************************************************************
778 * This function recursively looks for a given object type. i_mode can be one
779 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
780 *****************************************************************************/
781 vlc_list_t * __vlc_list_find( vlc_object_t *p_this, int i_type, int i_mode )
786 /* Look for the objects */
787 switch( i_mode & 0x000f )
790 return vlc_list_find (p_this->p_libvlc, i_type, FIND_CHILD);
793 libvlc_lock (p_this->p_libvlc);
794 i_count = CountChildren( p_this, i_type );
795 p_list = NewList( i_count );
797 /* Check allocation was successful */
798 if( p_list->i_count != i_count )
800 libvlc_unlock (p_this->p_libvlc);
806 ListChildren( p_list, p_this, i_type );
807 libvlc_unlock (p_this->p_libvlc);
811 msg_Err( p_this, "unimplemented!" );
812 p_list = NewList( 0 );
820 * Gets the list of children of an objects, and increment their reference
822 * @return a list (possibly empty) or NULL in case of error.
824 vlc_list_t *__vlc_list_children( vlc_object_t *obj )
827 vlc_object_internals_t *priv = vlc_internals( obj );
829 libvlc_lock (obj->p_libvlc);
830 l = NewList( priv->i_children );
831 for (int i = 0; i < l->i_count; i++)
833 vlc_object_hold( priv->pp_children[i] );
834 l->p_values[i].p_object = priv->pp_children[i];
836 libvlc_unlock (obj->p_libvlc);
840 /*****************************************************************************
841 * DumpCommand: print the current vlc structure
842 *****************************************************************************
843 * This function prints either an ASCII tree showing the connections between
844 * vlc objects, and additional information such as their refcount, thread ID,
845 * etc. (command "tree"), or the same data as a simple list (command "list").
846 *****************************************************************************/
847 static int DumpCommand( vlc_object_t *p_this, char const *psz_cmd,
848 vlc_value_t oldval, vlc_value_t newval, void *p_data )
850 libvlc_int_t *p_libvlc = p_this->p_libvlc;
852 (void)oldval; (void)p_data;
853 if( *psz_cmd == 'l' )
855 vlc_object_t *cur = VLC_OBJECT (p_libvlc);
857 libvlc_lock (p_this->p_libvlc);
860 PrintObject (cur, "");
861 cur = vlc_internals (cur)->next;
863 while (cur != VLC_OBJECT(p_libvlc));
864 libvlc_unlock (p_this->p_libvlc);
868 vlc_object_t *p_object = NULL;
870 if( *newval.psz_string )
872 /* try using the object's name to find it */
873 p_object = vlc_object_find_name( p_this, newval.psz_string,
881 libvlc_lock (p_this->p_libvlc);
882 if( *psz_cmd == 't' )
884 char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
887 p_object = VLC_OBJECT(p_this->p_libvlc);
890 DumpStructure( p_object, 0, psz_foo );
892 else if( *psz_cmd == 'v' )
897 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
899 PrintObject( p_object, "" );
901 if( !vlc_internals( p_object )->i_vars )
902 printf( " `-o No variables\n" );
903 for( i = 0; i < vlc_internals( p_object )->i_vars; i++ )
905 variable_t *p_var = vlc_internals( p_object )->p_vars + i;
907 const char *psz_type = "unknown";
908 switch( p_var->i_type & VLC_VAR_TYPE )
910 #define MYCASE( type, nice ) \
911 case VLC_VAR_ ## type: \
914 MYCASE( VOID, "void" );
915 MYCASE( BOOL, "bool" );
916 MYCASE( INTEGER, "integer" );
917 MYCASE( HOTKEY, "hotkey" );
918 MYCASE( STRING, "string" );
919 MYCASE( MODULE, "module" );
920 MYCASE( FILE, "file" );
921 MYCASE( DIRECTORY, "directory" );
922 MYCASE( VARIABLE, "variable" );
923 MYCASE( FLOAT, "float" );
924 MYCASE( TIME, "time" );
925 MYCASE( ADDRESS, "address" );
926 MYCASE( MUTEX, "mutex" );
927 MYCASE( LIST, "list" );
930 printf( " %c-o \"%s\" (%s",
931 i + 1 == vlc_internals( p_object )->i_vars ? '`' : '|',
932 p_var->psz_name, psz_type );
933 if( p_var->psz_text )
934 printf( ", %s", p_var->psz_text );
936 if( p_var->i_type & VLC_VAR_HASCHOICE )
937 printf( ", has choices" );
938 if( p_var->i_type & VLC_VAR_ISCOMMAND )
939 printf( ", command" );
940 if( p_var->i_entries )
941 printf( ", %d callbacks", p_var->i_entries );
942 switch( p_var->i_type & VLC_VAR_CLASS )
948 printf( ": %s", p_var->val.b_bool ? "true" : "false" );
950 case VLC_VAR_INTEGER:
951 printf( ": %d", p_var->val.i_int );
954 printf( ": \"%s\"", p_var->val.psz_string );
957 printf( ": %f", p_var->val.f_float );
960 printf( ": %"PRIi64, (int64_t)p_var->val.i_time );
962 case VLC_VAR_ADDRESS:
963 printf( ": %p", p_var->val.p_address );
972 libvlc_unlock (p_this->p_libvlc);
974 if( *newval.psz_string )
976 vlc_object_release( p_object );
983 /*****************************************************************************
984 * vlc_list_release: free a list previously allocated by vlc_list_find
985 *****************************************************************************
986 * This function decreases the refcount of all objects in the list and
988 *****************************************************************************/
989 void vlc_list_release( vlc_list_t *p_list )
993 for( i_index = 0; i_index < p_list->i_count; i_index++ )
995 vlc_object_release( p_list->p_values[i_index].p_object );
998 free( p_list->p_values );
1002 /*****************************************************************************
1003 * dump an object. (Debug function)
1004 *****************************************************************************/
1006 static void vlc_object_dump( vlc_object_t *p_this )
1008 char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
1011 DumpStructure( p_this, 0, psz_foo );
1015 /* Following functions are local */
1017 static vlc_object_t * FindObject( vlc_object_t *p_this, int i_type, int i_mode )
1020 vlc_object_t *p_tmp;
1022 switch( i_mode & 0x000f )
1025 p_tmp = p_this->p_parent;
1028 if( p_tmp->i_object_type == i_type )
1030 vlc_object_hold( p_tmp );
1035 return FindObject( p_tmp, i_type, i_mode );
1041 for( i = vlc_internals( p_this )->i_children; i--; )
1043 p_tmp = vlc_internals( p_this )->pp_children[i];
1044 if( p_tmp->i_object_type == i_type )
1046 vlc_object_hold( p_tmp );
1049 else if( vlc_internals( p_tmp )->i_children )
1051 p_tmp = FindObject( p_tmp, i_type, i_mode );
1061 /* Handled in vlc_object_find */
1068 static vlc_object_t * FindObjectName( vlc_object_t *p_this,
1069 const char *psz_name,
1073 vlc_object_t *p_tmp;
1075 switch( i_mode & 0x000f )
1078 p_tmp = p_this->p_parent;
1081 if( p_tmp->psz_object_name
1082 && !strcmp( p_tmp->psz_object_name, psz_name ) )
1084 vlc_object_hold( p_tmp );
1089 return FindObjectName( p_tmp, psz_name, i_mode );
1095 for( i = vlc_internals( p_this )->i_children; i--; )
1097 p_tmp = vlc_internals( p_this )->pp_children[i];
1098 if( p_tmp->psz_object_name
1099 && !strcmp( p_tmp->psz_object_name, psz_name ) )
1101 vlc_object_hold( p_tmp );
1104 else if( vlc_internals( p_tmp )->i_children )
1106 p_tmp = FindObjectName( p_tmp, psz_name, i_mode );
1116 /* Handled in vlc_object_find */
1124 static void PrintObject( vlc_object_t *p_this, const char *psz_prefix )
1126 char psz_children[20], psz_refcount[20], psz_thread[30], psz_name[50],
1129 int canc = vlc_savecancel ();
1130 memset( &psz_name, 0, sizeof(psz_name) );
1131 if( p_this->psz_object_name )
1133 snprintf( psz_name, 49, " \"%s\"", p_this->psz_object_name );
1135 psz_name[48] = '\"';
1138 psz_children[0] = '\0';
1139 switch( vlc_internals( p_this )->i_children )
1144 strcpy( psz_children, ", 1 child" );
1147 snprintf( psz_children, 19, ", %i children",
1148 vlc_internals( p_this )->i_children );
1152 psz_refcount[0] = '\0';
1153 if( vlc_internals( p_this )->i_refcount > 0 )
1154 snprintf( psz_refcount, 19, ", refcount %u",
1155 vlc_internals( p_this )->i_refcount );
1157 psz_thread[0] = '\0';
1158 if( vlc_internals( p_this )->b_thread )
1159 snprintf( psz_thread, 29, " (thread %lu)",
1160 (unsigned long)vlc_internals( p_this )->thread_id );
1162 psz_parent[0] = '\0';
1163 if( p_this->p_parent )
1164 snprintf( psz_parent, 19, ", parent %p", p_this->p_parent );
1166 printf( " %so %p %s%s%s%s%s%s\n", psz_prefix,
1167 p_this, p_this->psz_object_type,
1168 psz_name, psz_thread, psz_refcount, psz_children,
1170 vlc_restorecancel (canc);
1173 static void DumpStructure( vlc_object_t *p_this, int i_level, char *psz_foo )
1176 char i_back = psz_foo[i_level];
1177 psz_foo[i_level] = '\0';
1179 PrintObject( p_this, psz_foo );
1181 psz_foo[i_level] = i_back;
1183 if( i_level / 2 >= MAX_DUMPSTRUCTURE_DEPTH )
1185 msg_Warn( p_this, "structure tree is too deep" );
1189 for( i = 0 ; i < vlc_internals( p_this )->i_children ; i++ )
1193 psz_foo[i_level-1] = ' ';
1195 if( psz_foo[i_level-2] == '`' )
1197 psz_foo[i_level-2] = ' ';
1201 if( i == vlc_internals( p_this )->i_children - 1 )
1203 psz_foo[i_level] = '`';
1207 psz_foo[i_level] = '|';
1210 psz_foo[i_level+1] = '-';
1211 psz_foo[i_level+2] = '\0';
1213 DumpStructure( vlc_internals( p_this )->pp_children[i], i_level + 2,
1218 static vlc_list_t * NewList( int i_count )
1220 vlc_list_t * p_list = (vlc_list_t *)malloc( sizeof( vlc_list_t ) );
1221 if( p_list == NULL )
1226 p_list->i_count = i_count;
1230 p_list->p_values = NULL;
1234 p_list->p_values = malloc( i_count * sizeof( vlc_value_t ) );
1235 if( p_list->p_values == NULL )
1237 p_list->i_count = 0;
1244 static void ListReplace( vlc_list_t *p_list, vlc_object_t *p_object,
1247 if( p_list == NULL || i_index >= p_list->i_count )
1252 vlc_object_hold( p_object );
1254 p_list->p_values[i_index].p_object = p_object;
1259 /*static void ListAppend( vlc_list_t *p_list, vlc_object_t *p_object )
1261 if( p_list == NULL )
1266 p_list->p_values = realloc( p_list->p_values, (p_list->i_count + 1)
1267 * sizeof( vlc_value_t ) );
1268 if( p_list->p_values == NULL )
1270 p_list->i_count = 0;
1274 vlc_object_hold( p_object );
1276 p_list->p_values[p_list->i_count].p_object = p_object;
1282 static int CountChildren( vlc_object_t *p_this, int i_type )
1284 vlc_object_t *p_tmp;
1287 for( i = 0; i < vlc_internals( p_this )->i_children; i++ )
1289 p_tmp = vlc_internals( p_this )->pp_children[i];
1291 if( p_tmp->i_object_type == i_type )
1295 i_count += CountChildren( p_tmp, i_type );
1301 static void ListChildren( vlc_list_t *p_list, vlc_object_t *p_this, int i_type )
1303 vlc_object_t *p_tmp;
1306 for( i = 0; i < vlc_internals( p_this )->i_children; i++ )
1308 p_tmp = vlc_internals( p_this )->pp_children[i];
1310 if( p_tmp->i_object_type == i_type )
1311 ListReplace( p_list, p_tmp, p_list->i_count++ );
1313 ListChildren( p_list, p_tmp, i_type );