1 /*****************************************************************************
2 * objects.c: vlc_object_t handling
3 *****************************************************************************
4 * Copyright (C) 2004-2008 the VideoLAN team
6 * Authors: Samuel Hocevar <sam@zoy.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
25 * This file contains the functions to handle the vlc_object_t type
27 * Unless otherwise stated, functions in this file are not cancellation point.
28 * All functions in this file are safe w.r.t. deferred cancellation.
32 /*****************************************************************************
34 *****************************************************************************/
39 #include <vlc_common.h>
41 #include "../libvlc.h"
44 #include "audio_output/aout_internal.h"
46 #include <vlc_access.h>
47 #include <vlc_demux.h>
48 #include <vlc_stream.h>
51 #include "stream_output/stream_output.h"
53 #include "vlc_interface.h"
54 #include "vlc_codec.h"
55 #include "vlc_filter.h"
57 #include "variables.h"
63 # include <errno.h> /* ENOSYS */
67 /*****************************************************************************
69 *****************************************************************************/
70 static int DumpCommand( vlc_object_t *, char const *,
71 vlc_value_t, vlc_value_t, void * );
73 static vlc_object_t * FindObject ( vlc_object_t *, int, int );
74 static vlc_object_t * FindObjectName( vlc_object_t *, const char *, int );
75 static void PrintObject ( vlc_object_t *, const char * );
76 static void DumpStructure ( vlc_object_t *, int, char * );
78 static vlc_list_t * NewList ( int );
79 static void ListReplace ( vlc_list_t *, vlc_object_t *, int );
80 /*static void ListAppend ( vlc_list_t *, vlc_object_t * );*/
81 static int CountChildren ( vlc_object_t *, int );
82 static void ListChildren ( vlc_list_t *, vlc_object_t *, int );
84 static void vlc_object_destroy( vlc_object_t *p_this );
85 static void vlc_object_detach_unlocked (vlc_object_t *p_this);
86 static void vlc_object_dump( vlc_object_t *p_this );
88 /*****************************************************************************
89 * Local structure lock
90 *****************************************************************************/
91 static void close_nocancel (int fd)
93 int canc = vlc_savecancel ();
95 vlc_restorecancel (canc);
98 static void libvlc_lock (libvlc_int_t *p_libvlc)
100 vlc_mutex_lock (&(libvlc_priv (p_libvlc)->structure_lock));
103 static void libvlc_unlock (libvlc_int_t *p_libvlc)
105 vlc_mutex_unlock (&(libvlc_priv (p_libvlc)->structure_lock));
108 void *__vlc_custom_create( vlc_object_t *p_this, size_t i_size,
109 int i_type, const char *psz_type )
112 vlc_object_internals_t *p_priv;
115 * VLC objects are laid out as follow:
116 * - first the LibVLC-private per-object data,
117 * - then VLC_COMMON members from vlc_object_t,
118 * - finally, the type-specific data (if any).
120 * This function initializes the LibVLC and common data,
121 * and zeroes the rest.
123 p_priv = calloc( 1, sizeof( *p_priv ) + i_size );
127 assert (i_size >= sizeof (vlc_object_t));
128 p_new = (vlc_object_t *)(p_priv + 1);
130 p_new->i_object_type = i_type;
131 p_new->psz_object_type = psz_type;
132 p_new->psz_object_name = NULL;
134 p_new->b_die = false;
135 p_new->b_error = false;
136 p_new->b_force = false;
138 p_new->psz_header = NULL;
141 p_new->i_flags = p_this->i_flags
142 & (OBJECT_FLAGS_NODBG|OBJECT_FLAGS_QUIET|OBJECT_FLAGS_NOINTERACT);
144 p_priv->p_vars = calloc( sizeof( variable_t ), 16 );
146 if( !p_priv->p_vars )
154 libvlc_int_t *self = (libvlc_int_t*)p_new;
155 p_new->p_libvlc = self;
156 vlc_mutex_init (&(libvlc_priv (self)->structure_lock));
157 p_this = p_priv->next = p_priv->prev = p_new;
160 p_new->p_libvlc = p_this->p_libvlc;
162 vlc_spin_init( &p_priv->ref_spin );
163 p_priv->i_refcount = 1;
164 p_priv->pf_destructor = NULL;
165 p_priv->b_thread = false;
166 p_new->p_parent = NULL;
167 p_priv->pp_children = NULL;
168 p_priv->i_children = 0;
170 p_new->p_private = NULL;
172 /* Initialize mutexes and condvars */
173 vlc_mutex_init( &p_priv->lock );
174 vlc_cond_init( &p_priv->wait );
175 vlc_mutex_init( &p_priv->var_lock );
176 vlc_spin_init( &p_priv->spin );
177 p_priv->pipes[0] = p_priv->pipes[1] = -1;
179 p_priv->next = p_this;
180 libvlc_lock (p_new->p_libvlc);
181 p_priv->prev = vlc_internals (p_this)->prev;
182 vlc_internals (p_this)->prev = p_new;
183 vlc_internals (p_priv->prev)->next = p_new;
184 libvlc_unlock (p_new->p_libvlc);
186 if (p_new == VLC_OBJECT(p_new->p_libvlc))
187 { /* TODO: should be in src/libvlc.c */
188 int canc = vlc_savecancel ();
189 var_Create( p_new, "list", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
190 var_AddCallback( p_new, "list", DumpCommand, NULL );
191 var_Create( p_new, "tree", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
192 var_AddCallback( p_new, "tree", DumpCommand, NULL );
193 var_Create( p_new, "vars", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
194 var_AddCallback( p_new, "vars", DumpCommand, NULL );
195 vlc_restorecancel (canc);
203 * Allocates and initializes a vlc object.
205 * @param i_type known object type (all of them are negative integer values),
206 * or object byte size (always positive).
208 * @return the new object, or NULL on error.
210 void * __vlc_object_create( vlc_object_t *p_this, int i_type )
212 const char * psz_type;
217 case VLC_OBJECT_INTF:
218 i_size = sizeof(intf_thread_t);
219 psz_type = "interface";
221 case VLC_OBJECT_DECODER:
222 i_size = sizeof(decoder_t);
223 psz_type = "decoder";
225 case VLC_OBJECT_PACKETIZER:
226 i_size = sizeof(decoder_t);
227 psz_type = "packetizer";
229 case VLC_OBJECT_ENCODER:
230 i_size = sizeof(encoder_t);
231 psz_type = "encoder";
233 case VLC_OBJECT_AOUT:
234 i_size = sizeof(aout_instance_t);
235 psz_type = "audio output";
238 assert( i_type > 0 ); /* unknown type?! */
240 i_type = VLC_OBJECT_GENERIC;
241 psz_type = "generic";
245 return vlc_custom_create( p_this, i_size, i_type, psz_type );
250 ****************************************************************************
251 * Set the destructor of a vlc object
253 * This function sets the destructor of the vlc object. It will be called
254 * when the object is destroyed when the its refcount reaches 0.
255 * (It is called by the internal function vlc_object_destroy())
256 *****************************************************************************/
257 void __vlc_object_set_destructor( vlc_object_t *p_this,
258 vlc_destructor_t pf_destructor )
260 vlc_object_internals_t *p_priv = vlc_internals(p_this );
261 p_priv->pf_destructor = pf_destructor;
265 ****************************************************************************
266 * Destroy a vlc object (Internal)
268 * This function destroys an object that has been previously allocated with
269 * vlc_object_create. The object's refcount must be zero and it must not be
270 * attached to other objects in any way.
272 * This function must be called with cancellation disabled (currently).
273 *****************************************************************************/
274 static void vlc_object_destroy( vlc_object_t *p_this )
276 vlc_object_internals_t *p_priv = vlc_internals( p_this );
278 /* Objects are always detached beforehand */
279 assert( !p_this->p_parent );
281 /* Send a kill to the object's thread if applicable */
282 vlc_object_kill( p_this );
284 /* Call the custom "subclass" destructor */
285 if( p_priv->pf_destructor )
286 p_priv->pf_destructor( p_this );
288 /* Any thread must have been cleaned up at this point. */
289 assert( !p_priv->b_thread );
291 /* Destroy the associated variables, starting from the end so that
292 * no memmove calls have to be done. */
293 while( p_priv->i_vars )
295 var_Destroy( p_this, p_priv->p_vars[p_priv->i_vars - 1].psz_name );
298 free( p_priv->p_vars );
299 vlc_mutex_destroy( &p_priv->var_lock );
301 free( p_this->psz_header );
303 FREENULL( p_this->psz_object_name );
305 vlc_spin_destroy( &p_priv->ref_spin );
306 vlc_mutex_destroy( &p_priv->lock );
307 vlc_cond_destroy( &p_priv->wait );
308 vlc_spin_destroy( &p_priv->spin );
309 if( p_priv->pipes[1] != -1 )
310 close( p_priv->pipes[1] );
311 if( p_priv->pipes[0] != -1 )
312 close( p_priv->pipes[0] );
313 if( VLC_OBJECT(p_this->p_libvlc) == p_this )
314 vlc_mutex_destroy (&(libvlc_priv ((libvlc_int_t *)p_this)->structure_lock));
320 /** Inter-object signaling */
322 void __vlc_object_lock( vlc_object_t *obj )
324 vlc_mutex_lock( &(vlc_internals(obj)->lock) );
327 void __vlc_object_unlock( vlc_object_t *obj )
329 vlc_assert_locked( &(vlc_internals(obj)->lock) );
330 vlc_mutex_unlock( &(vlc_internals(obj)->lock) );
334 # include <winsock2.h>
335 # include <ws2tcpip.h>
338 * select()-able pipes emulated using Winsock
340 static int pipe (int fd[2])
343 int addrlen = sizeof (addr);
345 SOCKET l = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP), a,
346 c = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
347 if ((l == INVALID_SOCKET) || (c == INVALID_SOCKET))
350 memset (&addr, 0, sizeof (addr));
351 addr.sin_family = AF_INET;
352 addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
353 if (bind (l, (PSOCKADDR)&addr, sizeof (addr))
354 || getsockname (l, (PSOCKADDR)&addr, &addrlen)
356 || connect (c, (PSOCKADDR)&addr, addrlen))
359 a = accept (l, NULL, NULL);
360 if (a == INVALID_SOCKET)
371 if (l != INVALID_SOCKET)
373 if (c != INVALID_SOCKET)
379 #define read( a, b, c ) recv (a, b, c, 0)
381 #define write( a, b, c ) send (a, b, c, 0)
383 #define close( a ) closesocket (a)
387 * Returns the readable end of a pipe that becomes readable once termination
388 * of the object is requested (vlc_object_kill()).
389 * This can be used to wake-up out of a select() or poll() event loop, such
390 * typically when doing network I/O.
392 * Note that the pipe will remain the same for the lifetime of the object.
393 * DO NOT read the pipe nor close it yourself. Ever.
395 * @param obj object that would be "killed"
396 * @return a readable pipe descriptor, or -1 on error.
398 int vlc_object_waitpipe( vlc_object_t *obj )
400 int pfd[2] = { -1, -1 };
401 vlc_object_internals_t *internals = vlc_internals( obj );
404 vlc_spin_lock (&internals->spin);
405 if (internals->pipes[0] == -1)
407 /* This can only ever happen if someone killed us without locking: */
408 assert (internals->pipes[1] == -1);
409 vlc_spin_unlock (&internals->spin);
414 vlc_spin_lock (&internals->spin);
415 if (internals->pipes[0] == -1)
417 internals->pipes[0] = pfd[0];
418 internals->pipes[1] = pfd[1];
419 pfd[0] = pfd[1] = -1;
423 vlc_spin_unlock (&internals->spin);
427 /* Race condition: vlc_object_kill() already invoked! */
430 vlc_spin_lock (&internals->spin);
431 fd = internals->pipes[1];
432 internals->pipes[1] = -1;
433 vlc_spin_unlock (&internals->spin);
435 msg_Dbg (obj, "waitpipe: object already dying");
440 /* Race condition: two threads call pipe() - unlikely */
446 return internals->pipes[0];
451 * Suspends until another thread calls vlc_object_signal_unlocked().
452 * The thread may be woken up earlier due to limitations of the underlying
455 * In new code, please use vlc_cond_wait() instead.
457 * This function is a cancellation point. In case of cancellation, the object
458 * will be in locked state.
460 void __vlc_object_wait( vlc_object_t *obj )
462 vlc_object_internals_t *priv = vlc_internals( obj );
463 vlc_assert_locked( &priv->lock);
464 vlc_cond_wait( &priv->wait, &priv->lock );
469 * Wakes up one thread waiting on the object. If no thread are (yet) waiting,
472 * Please do not use this function in new code as we are trying to untangle
473 * objects and threads. Use vlc_cond_wait() instead.
475 void __vlc_object_signal_unlocked( vlc_object_t *obj )
477 vlc_assert_locked (&(vlc_internals(obj)->lock));
478 vlc_cond_signal( &(vlc_internals(obj)->wait) );
483 * Requests termination of an object.
484 * If the object is LibVLC, also request to terminate all its children.
486 void __vlc_object_kill( vlc_object_t *p_this )
488 vlc_object_internals_t *priv = vlc_internals( p_this );
491 vlc_thread_cancel( p_this );
492 vlc_object_lock( p_this );
493 p_this->b_die = true;
495 vlc_spin_lock (&priv->spin);
498 vlc_spin_unlock (&priv->spin);
502 msg_Dbg (p_this, "waitpipe: object killed");
506 vlc_cond_broadcast (&priv->wait);
507 /* This also serves as a memory barrier toward vlc_object_alive(): */
508 vlc_object_unlock( p_this );
512 /*****************************************************************************
513 * find a typed object and increment its refcount
514 *****************************************************************************
515 * This function recursively looks for a given object type. i_mode can be one
516 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
517 *****************************************************************************/
518 void * __vlc_object_find( vlc_object_t *p_this, int i_type, int i_mode )
520 vlc_object_t *p_found;
522 /* If we are of the requested type ourselves, don't look further */
523 if( !(i_mode & FIND_STRICT) && p_this->i_object_type == i_type )
525 vlc_object_hold( p_this );
529 /* Otherwise, recursively look for the object */
530 if ((i_mode & 0x000f) == FIND_ANYWHERE)
531 return vlc_object_find (p_this->p_libvlc, i_type,
532 (i_mode & ~0x000f)|FIND_CHILD);
534 libvlc_lock (p_this->p_libvlc);
535 p_found = FindObject( p_this, i_type, i_mode );
536 libvlc_unlock (p_this->p_libvlc);
540 #undef vlc_object_find_name
542 * Finds a named object and increment its reference count.
543 * Beware that objects found in this manner can be "owned" by another thread,
544 * be of _any_ type, and be attached to any module (if any). With such an
545 * object reference, you can set or get object variables, emit log messages,
546 * and read write-once object parameters (psz_object_type, etc).
547 * You CANNOT cast the object to a more specific object type, and you
548 * definitely cannot invoke object type-specific callbacks with this.
550 * @param p_this object to search from
551 * @param psz_name name of the object to search for
552 * @param i_mode search direction: FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
554 * @return a matching object (must be released by the caller),
557 vlc_object_t *vlc_object_find_name( vlc_object_t *p_this,
558 const char *psz_name, int i_mode )
560 vlc_object_t *p_found;
562 /* If have the requested name ourselves, don't look further */
563 if( !(i_mode & FIND_STRICT)
564 && p_this->psz_object_name
565 && !strcmp( p_this->psz_object_name, psz_name ) )
567 vlc_object_hold( p_this );
571 libvlc_lock (p_this->p_libvlc);
573 /* Otherwise, recursively look for the object */
574 if( (i_mode & 0x000f) == FIND_ANYWHERE )
576 vlc_object_t *p_root = p_this;
579 while( p_root->p_parent != NULL &&
580 p_root != VLC_OBJECT( p_this->p_libvlc ) )
582 p_root = p_root->p_parent;
585 p_found = FindObjectName( p_root, psz_name,
586 (i_mode & ~0x000f)|FIND_CHILD );
587 if( p_found == NULL && p_root != VLC_OBJECT( p_this->p_libvlc ) )
589 p_found = FindObjectName( VLC_OBJECT( p_this->p_libvlc ),
590 psz_name, (i_mode & ~0x000f)|FIND_CHILD );
595 p_found = FindObjectName( p_this, psz_name, i_mode );
598 libvlc_unlock (p_this->p_libvlc);
603 * Increment an object reference counter.
605 void * __vlc_object_hold( vlc_object_t *p_this )
607 vlc_object_internals_t *internals = vlc_internals( p_this );
609 vlc_spin_lock( &internals->ref_spin );
610 /* Avoid obvious freed object uses */
611 assert( internals->i_refcount > 0 );
612 /* Increment the counter */
613 internals->i_refcount++;
614 vlc_spin_unlock( &internals->ref_spin );
618 /*****************************************************************************
619 * Decrement an object refcount
620 * And destroy the object if its refcount reach zero.
621 *****************************************************************************/
622 void __vlc_object_release( vlc_object_t *p_this )
624 vlc_object_internals_t *internals = vlc_internals( p_this );
625 vlc_object_t *parent = NULL;
626 bool b_should_destroy;
628 vlc_spin_lock( &internals->ref_spin );
629 assert( internals->i_refcount > 0 );
631 if( internals->i_refcount > 1 )
634 /* There are still other references to the object */
635 internals->i_refcount--;
636 vlc_spin_unlock( &internals->ref_spin );
639 vlc_spin_unlock( &internals->ref_spin );
642 /* Remember that we cannot hold the spin while waiting on the mutex */
643 libvlc_lock (p_this->p_libvlc);
644 /* Take the spin again. Note that another thread may have held the
645 * object in the (very short) mean time. */
646 vlc_spin_lock( &internals->ref_spin );
647 b_should_destroy = --internals->i_refcount == 0;
648 vlc_spin_unlock( &internals->ref_spin );
650 if( b_should_destroy )
652 /* We have no children */
653 assert (internals->i_children == 0);
654 parent = p_this->p_parent;
657 if( VLC_OBJECT(p_this->p_libvlc) == p_this )
660 vlc_object_t *leaked = internals->next;
661 while( leaked != p_this )
663 /* We are leaking this object */
665 "ERROR: leaking object (%p, type:%s, name:%s)\n",
666 leaked, leaked->psz_object_type,
667 leaked->psz_object_name );
668 /* Dump object to ease debugging */
669 vlc_object_dump( leaked );
671 leaked = vlc_internals (leaked)->next;
674 if( internals->next != p_this )
675 /* Dump libvlc object to ease debugging */
676 vlc_object_dump( p_this );
679 /* Remove the object from object list
680 * so that it cannot be encountered by vlc_object_get() */
681 vlc_internals (internals->next)->prev = internals->prev;
682 vlc_internals (internals->prev)->next = internals->next;
685 /* Detach from parent to protect against FIND_CHILDREN */
686 vlc_object_detach_unlocked (p_this);
688 libvlc_unlock (p_this->p_libvlc);
690 if( b_should_destroy )
694 canc = vlc_savecancel ();
695 vlc_object_destroy( p_this );
696 vlc_restorecancel (canc);
698 vlc_object_release (parent);
703 ****************************************************************************
704 * attach object to a parent object
705 *****************************************************************************
706 * This function sets p_this as a child of p_parent, and p_parent as a parent
707 * of p_this. This link can be undone using vlc_object_detach.
708 *****************************************************************************/
709 void __vlc_object_attach( vlc_object_t *p_this, vlc_object_t *p_parent )
711 if( !p_this ) return;
713 vlc_object_hold (p_parent);
714 libvlc_lock (p_this->p_libvlc);
716 /* Attach the parent to its child */
717 assert (!p_this->p_parent);
718 p_this->p_parent = p_parent;
720 /* Attach the child to its parent */
721 vlc_object_internals_t *priv = vlc_internals( p_parent );
722 INSERT_ELEM( priv->pp_children, priv->i_children, priv->i_children,
724 libvlc_unlock (p_this->p_libvlc);
728 static void vlc_object_detach_unlocked (vlc_object_t *p_this)
730 if (p_this->p_parent == NULL)
733 vlc_object_internals_t *priv = vlc_internals( p_this->p_parent );
737 /* Remove p_this's parent */
738 p_this->p_parent = NULL;
740 /* Remove all of p_parent's children which are p_this */
741 for( i_index = priv->i_children ; i_index-- ; )
743 if( priv->pp_children[i_index] == p_this )
746 for( i = i_index ; i < priv->i_children ; i++ )
747 priv->pp_children[i] = priv->pp_children[i+1];
751 if( priv->i_children )
753 vlc_object_t **pp_children = (vlc_object_t **)
754 realloc( priv->pp_children,
755 priv->i_children * sizeof(vlc_object_t *) );
757 priv->pp_children = pp_children;
761 /* Special case - don't realloc() to zero to avoid leaking */
762 free( priv->pp_children );
763 priv->pp_children = NULL;
769 ****************************************************************************
770 * detach object from its parent
771 *****************************************************************************
772 * This function removes all links between an object and its parent.
773 *****************************************************************************/
774 void __vlc_object_detach( vlc_object_t *p_this )
776 vlc_object_t *p_parent;
777 if( !p_this ) return;
779 libvlc_lock (p_this->p_libvlc);
780 p_parent = p_this->p_parent;
782 vlc_object_detach_unlocked( p_this );
783 libvlc_unlock (p_this->p_libvlc);
786 vlc_object_release (p_parent);
791 ****************************************************************************
792 * find a list typed objects and increment their refcount
793 *****************************************************************************
794 * This function recursively looks for a given object type. i_mode can be one
795 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
796 *****************************************************************************/
797 vlc_list_t * __vlc_list_find( vlc_object_t *p_this, int i_type, int i_mode )
802 /* Look for the objects */
803 switch( i_mode & 0x000f )
806 return vlc_list_find (p_this->p_libvlc, i_type, FIND_CHILD);
809 libvlc_lock (p_this->p_libvlc);
810 i_count = CountChildren( p_this, i_type );
811 p_list = NewList( i_count );
813 /* Check allocation was successful */
814 if( p_list->i_count != i_count )
816 libvlc_unlock (p_this->p_libvlc);
822 ListChildren( p_list, p_this, i_type );
823 libvlc_unlock (p_this->p_libvlc);
827 msg_Err( p_this, "unimplemented!" );
828 p_list = NewList( 0 );
836 * Gets the list of children of an objects, and increment their reference
838 * @return a list (possibly empty) or NULL in case of error.
840 vlc_list_t *__vlc_list_children( vlc_object_t *obj )
843 vlc_object_internals_t *priv = vlc_internals( obj );
845 libvlc_lock (obj->p_libvlc);
846 l = NewList( priv->i_children );
847 for (int i = 0; i < l->i_count; i++)
849 vlc_object_hold( priv->pp_children[i] );
850 l->p_values[i].p_object = priv->pp_children[i];
852 libvlc_unlock (obj->p_libvlc);
856 /*****************************************************************************
857 * DumpCommand: print the current vlc structure
858 *****************************************************************************
859 * This function prints either an ASCII tree showing the connections between
860 * vlc objects, and additional information such as their refcount, thread ID,
861 * etc. (command "tree"), or the same data as a simple list (command "list").
862 *****************************************************************************/
863 static int DumpCommand( vlc_object_t *p_this, char const *psz_cmd,
864 vlc_value_t oldval, vlc_value_t newval, void *p_data )
866 libvlc_int_t *p_libvlc = p_this->p_libvlc;
868 (void)oldval; (void)p_data;
869 if( *psz_cmd == 'l' )
871 vlc_object_t *cur = VLC_OBJECT (p_libvlc);
873 libvlc_lock (p_this->p_libvlc);
876 PrintObject (cur, "");
877 cur = vlc_internals (cur)->next;
879 while (cur != VLC_OBJECT(p_libvlc));
880 libvlc_unlock (p_this->p_libvlc);
884 vlc_object_t *p_object = NULL;
886 if( *newval.psz_string )
888 /* try using the object's name to find it */
889 p_object = vlc_object_find_name( p_this, newval.psz_string,
897 libvlc_lock (p_this->p_libvlc);
898 if( *psz_cmd == 't' )
900 char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
903 p_object = VLC_OBJECT(p_this->p_libvlc);
906 DumpStructure( p_object, 0, psz_foo );
908 else if( *psz_cmd == 'v' )
913 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
915 PrintObject( p_object, "" );
917 if( !vlc_internals( p_object )->i_vars )
918 printf( " `-o No variables\n" );
919 for( i = 0; i < vlc_internals( p_object )->i_vars; i++ )
921 variable_t *p_var = vlc_internals( p_object )->p_vars + i;
923 const char *psz_type = "unknown";
924 switch( p_var->i_type & VLC_VAR_TYPE )
926 #define MYCASE( type, nice ) \
927 case VLC_VAR_ ## type: \
930 MYCASE( VOID, "void" );
931 MYCASE( BOOL, "bool" );
932 MYCASE( INTEGER, "integer" );
933 MYCASE( HOTKEY, "hotkey" );
934 MYCASE( STRING, "string" );
935 MYCASE( MODULE, "module" );
936 MYCASE( FILE, "file" );
937 MYCASE( DIRECTORY, "directory" );
938 MYCASE( VARIABLE, "variable" );
939 MYCASE( FLOAT, "float" );
940 MYCASE( TIME, "time" );
941 MYCASE( ADDRESS, "address" );
942 MYCASE( MUTEX, "mutex" );
943 MYCASE( LIST, "list" );
946 printf( " %c-o \"%s\" (%s",
947 i + 1 == vlc_internals( p_object )->i_vars ? '`' : '|',
948 p_var->psz_name, psz_type );
949 if( p_var->psz_text )
950 printf( ", %s", p_var->psz_text );
952 if( p_var->i_type & VLC_VAR_HASCHOICE )
953 printf( ", has choices" );
954 if( p_var->i_type & VLC_VAR_ISCOMMAND )
955 printf( ", command" );
956 if( p_var->i_entries )
957 printf( ", %d callbacks", p_var->i_entries );
958 switch( p_var->i_type & VLC_VAR_CLASS )
964 printf( ": %s", p_var->val.b_bool ? "true" : "false" );
966 case VLC_VAR_INTEGER:
967 printf( ": %d", p_var->val.i_int );
970 printf( ": \"%s\"", p_var->val.psz_string );
973 printf( ": %f", p_var->val.f_float );
976 printf( ": %"PRIi64, (int64_t)p_var->val.i_time );
978 case VLC_VAR_ADDRESS:
979 printf( ": %p", p_var->val.p_address );
988 libvlc_unlock (p_this->p_libvlc);
990 if( *newval.psz_string )
992 vlc_object_release( p_object );
999 /*****************************************************************************
1000 * vlc_list_release: free a list previously allocated by vlc_list_find
1001 *****************************************************************************
1002 * This function decreases the refcount of all objects in the list and
1004 *****************************************************************************/
1005 void vlc_list_release( vlc_list_t *p_list )
1009 for( i_index = 0; i_index < p_list->i_count; i_index++ )
1011 vlc_object_release( p_list->p_values[i_index].p_object );
1014 free( p_list->p_values );
1018 /*****************************************************************************
1019 * dump an object. (Debug function)
1020 *****************************************************************************/
1021 static void vlc_object_dump( vlc_object_t *p_this )
1023 char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
1026 DumpStructure( p_this, 0, psz_foo );
1029 /* Following functions are local */
1031 static vlc_object_t * FindObject( vlc_object_t *p_this, int i_type, int i_mode )
1034 vlc_object_t *p_tmp;
1036 switch( i_mode & 0x000f )
1039 p_tmp = p_this->p_parent;
1042 if( p_tmp->i_object_type == i_type )
1044 vlc_object_hold( p_tmp );
1049 return FindObject( p_tmp, i_type, i_mode );
1055 for( i = vlc_internals( p_this )->i_children; i--; )
1057 p_tmp = vlc_internals( p_this )->pp_children[i];
1058 if( p_tmp->i_object_type == i_type )
1060 vlc_object_hold( p_tmp );
1063 else if( vlc_internals( p_tmp )->i_children )
1065 p_tmp = FindObject( p_tmp, i_type, i_mode );
1075 /* Handled in vlc_object_find */
1082 static vlc_object_t * FindObjectName( vlc_object_t *p_this,
1083 const char *psz_name,
1087 vlc_object_t *p_tmp;
1089 switch( i_mode & 0x000f )
1092 p_tmp = p_this->p_parent;
1095 if( p_tmp->psz_object_name
1096 && !strcmp( p_tmp->psz_object_name, psz_name ) )
1098 vlc_object_hold( p_tmp );
1103 return FindObjectName( p_tmp, psz_name, i_mode );
1109 for( i = vlc_internals( p_this )->i_children; i--; )
1111 p_tmp = vlc_internals( p_this )->pp_children[i];
1112 if( p_tmp->psz_object_name
1113 && !strcmp( p_tmp->psz_object_name, psz_name ) )
1115 vlc_object_hold( p_tmp );
1118 else if( vlc_internals( p_tmp )->i_children )
1120 p_tmp = FindObjectName( p_tmp, psz_name, i_mode );
1130 /* Handled in vlc_object_find */
1138 static void PrintObject( vlc_object_t *p_this, const char *psz_prefix )
1140 char psz_children[20], psz_refcount[20], psz_thread[30], psz_name[50],
1143 int canc = vlc_savecancel ();
1144 memset( &psz_name, 0, sizeof(psz_name) );
1145 if( p_this->psz_object_name )
1147 snprintf( psz_name, 49, " \"%s\"", p_this->psz_object_name );
1149 psz_name[48] = '\"';
1152 psz_children[0] = '\0';
1153 switch( vlc_internals( p_this )->i_children )
1158 strcpy( psz_children, ", 1 child" );
1161 snprintf( psz_children, 19, ", %i children",
1162 vlc_internals( p_this )->i_children );
1166 psz_refcount[0] = '\0';
1167 if( vlc_internals( p_this )->i_refcount > 0 )
1168 snprintf( psz_refcount, 19, ", refcount %u",
1169 vlc_internals( p_this )->i_refcount );
1171 psz_thread[0] = '\0';
1172 if( vlc_internals( p_this )->b_thread )
1173 snprintf( psz_thread, 29, " (thread %lu)",
1174 (unsigned long)vlc_internals( p_this )->thread_id );
1176 psz_parent[0] = '\0';
1177 if( p_this->p_parent )
1178 snprintf( psz_parent, 19, ", parent %p", p_this->p_parent );
1180 printf( " %so %p %s%s%s%s%s%s\n", psz_prefix,
1181 p_this, p_this->psz_object_type,
1182 psz_name, psz_thread, psz_refcount, psz_children,
1184 vlc_restorecancel (canc);
1187 static void DumpStructure( vlc_object_t *p_this, int i_level, char *psz_foo )
1190 char i_back = psz_foo[i_level];
1191 psz_foo[i_level] = '\0';
1193 PrintObject( p_this, psz_foo );
1195 psz_foo[i_level] = i_back;
1197 if( i_level / 2 >= MAX_DUMPSTRUCTURE_DEPTH )
1199 msg_Warn( p_this, "structure tree is too deep" );
1203 for( i = 0 ; i < vlc_internals( p_this )->i_children ; i++ )
1207 psz_foo[i_level-1] = ' ';
1209 if( psz_foo[i_level-2] == '`' )
1211 psz_foo[i_level-2] = ' ';
1215 if( i == vlc_internals( p_this )->i_children - 1 )
1217 psz_foo[i_level] = '`';
1221 psz_foo[i_level] = '|';
1224 psz_foo[i_level+1] = '-';
1225 psz_foo[i_level+2] = '\0';
1227 DumpStructure( vlc_internals( p_this )->pp_children[i], i_level + 2,
1232 static vlc_list_t * NewList( int i_count )
1234 vlc_list_t * p_list = (vlc_list_t *)malloc( sizeof( vlc_list_t ) );
1235 if( p_list == NULL )
1240 p_list->i_count = i_count;
1244 p_list->p_values = NULL;
1248 p_list->p_values = malloc( i_count * sizeof( vlc_value_t ) );
1249 if( p_list->p_values == NULL )
1251 p_list->i_count = 0;
1258 static void ListReplace( vlc_list_t *p_list, vlc_object_t *p_object,
1261 if( p_list == NULL || i_index >= p_list->i_count )
1266 vlc_object_hold( p_object );
1268 p_list->p_values[i_index].p_object = p_object;
1273 /*static void ListAppend( vlc_list_t *p_list, vlc_object_t *p_object )
1275 if( p_list == NULL )
1280 p_list->p_values = realloc( p_list->p_values, (p_list->i_count + 1)
1281 * sizeof( vlc_value_t ) );
1282 if( p_list->p_values == NULL )
1284 p_list->i_count = 0;
1288 vlc_object_hold( p_object );
1290 p_list->p_values[p_list->i_count].p_object = p_object;
1296 static int CountChildren( vlc_object_t *p_this, int i_type )
1298 vlc_object_t *p_tmp;
1301 for( i = 0; i < vlc_internals( p_this )->i_children; i++ )
1303 p_tmp = vlc_internals( p_this )->pp_children[i];
1305 if( p_tmp->i_object_type == i_type )
1309 i_count += CountChildren( p_tmp, i_type );
1315 static void ListChildren( vlc_list_t *p_list, vlc_object_t *p_this, int i_type )
1317 vlc_object_t *p_tmp;
1320 for( i = 0; i < vlc_internals( p_this )->i_children; i++ )
1322 p_tmp = vlc_internals( p_this )->pp_children[i];
1324 if( p_tmp->i_object_type == i_type )
1325 ListReplace( p_list, p_tmp, p_list->i_count++ );
1327 ListChildren( p_list, p_tmp, i_type );