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_cond_init( &p_priv->var_wait );
177 vlc_spin_init( &p_priv->spin );
178 p_priv->pipes[0] = p_priv->pipes[1] = -1;
180 p_priv->next = p_this;
181 libvlc_lock (p_new->p_libvlc);
182 p_priv->prev = vlc_internals (p_this)->prev;
183 vlc_internals (p_this)->prev = p_new;
184 vlc_internals (p_priv->prev)->next = p_new;
185 libvlc_unlock (p_new->p_libvlc);
187 if (p_new == VLC_OBJECT(p_new->p_libvlc))
188 { /* TODO: should be in src/libvlc.c */
189 int canc = vlc_savecancel ();
190 var_Create( p_new, "list", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
191 var_AddCallback( p_new, "list", DumpCommand, NULL );
192 var_Create( p_new, "tree", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
193 var_AddCallback( p_new, "tree", DumpCommand, NULL );
194 var_Create( p_new, "vars", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
195 var_AddCallback( p_new, "vars", DumpCommand, NULL );
196 vlc_restorecancel (canc);
204 * Allocates and initializes a vlc object.
206 * @param i_type known object type (all of them are negative integer values),
207 * or object byte size (always positive).
209 * @return the new object, or NULL on error.
211 void * __vlc_object_create( vlc_object_t *p_this, int i_type )
213 const char * psz_type;
218 case VLC_OBJECT_INTF:
219 i_size = sizeof(intf_thread_t);
220 psz_type = "interface";
222 case VLC_OBJECT_DECODER:
223 i_size = sizeof(decoder_t);
224 psz_type = "decoder";
226 case VLC_OBJECT_PACKETIZER:
227 i_size = sizeof(decoder_t);
228 psz_type = "packetizer";
230 case VLC_OBJECT_ENCODER:
231 i_size = sizeof(encoder_t);
232 psz_type = "encoder";
234 case VLC_OBJECT_AOUT:
235 i_size = sizeof(aout_instance_t);
236 psz_type = "audio output";
239 assert( i_type > 0 ); /* unknown type?! */
241 i_type = VLC_OBJECT_GENERIC;
242 psz_type = "generic";
246 return vlc_custom_create( p_this, i_size, i_type, psz_type );
251 ****************************************************************************
252 * Set the destructor of a vlc object
254 * This function sets the destructor of the vlc object. It will be called
255 * when the object is destroyed when the its refcount reaches 0.
256 * (It is called by the internal function vlc_object_destroy())
257 *****************************************************************************/
258 void __vlc_object_set_destructor( vlc_object_t *p_this,
259 vlc_destructor_t pf_destructor )
261 vlc_object_internals_t *p_priv = vlc_internals(p_this );
262 p_priv->pf_destructor = pf_destructor;
266 ****************************************************************************
267 * Destroy a vlc object (Internal)
269 * This function destroys an object that has been previously allocated with
270 * vlc_object_create. The object's refcount must be zero and it must not be
271 * attached to other objects in any way.
273 * This function must be called with cancellation disabled (currently).
274 *****************************************************************************/
275 static void vlc_object_destroy( vlc_object_t *p_this )
277 vlc_object_internals_t *p_priv = vlc_internals( p_this );
279 /* Objects are always detached beforehand */
280 assert( !p_this->p_parent );
282 /* Send a kill to the object's thread if applicable */
283 vlc_object_kill( p_this );
285 /* Call the custom "subclass" destructor */
286 if( p_priv->pf_destructor )
287 p_priv->pf_destructor( p_this );
289 /* Any thread must have been cleaned up at this point. */
290 assert( !p_priv->b_thread );
292 /* Destroy the associated variables, starting from the end so that
293 * no memmove calls have to be done. */
294 while( p_priv->i_vars )
296 var_Destroy( p_this, p_priv->p_vars[p_priv->i_vars - 1].psz_name );
299 free( p_priv->p_vars );
300 vlc_cond_destroy( &p_priv->var_wait );
301 vlc_mutex_destroy( &p_priv->var_lock );
303 free( p_this->psz_header );
305 FREENULL( p_this->psz_object_name );
307 vlc_spin_destroy( &p_priv->ref_spin );
308 vlc_mutex_destroy( &p_priv->lock );
309 vlc_cond_destroy( &p_priv->wait );
310 vlc_spin_destroy( &p_priv->spin );
311 if( p_priv->pipes[1] != -1 )
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) );
336 # include <winsock2.h>
337 # include <ws2tcpip.h>
340 * select()-able pipes emulated using Winsock
342 static int pipe (int fd[2])
345 int addrlen = sizeof (addr);
347 SOCKET l = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP), a,
348 c = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
349 if ((l == INVALID_SOCKET) || (c == INVALID_SOCKET))
352 memset (&addr, 0, sizeof (addr));
353 addr.sin_family = AF_INET;
354 addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
355 if (bind (l, (PSOCKADDR)&addr, sizeof (addr))
356 || getsockname (l, (PSOCKADDR)&addr, &addrlen)
358 || connect (c, (PSOCKADDR)&addr, addrlen))
361 a = accept (l, NULL, NULL);
362 if (a == INVALID_SOCKET)
373 if (l != INVALID_SOCKET)
375 if (c != INVALID_SOCKET)
381 #define read( a, b, c ) recv (a, b, c, 0)
383 #define write( a, b, c ) send (a, b, c, 0)
385 #define close( a ) closesocket (a)
389 * Returns the readable end of a pipe that becomes readable once termination
390 * of the object is requested (vlc_object_kill()).
391 * This can be used to wake-up out of a select() or poll() event loop, such
392 * typically when doing network I/O.
394 * Note that the pipe will remain the same for the lifetime of the object.
395 * DO NOT read the pipe nor close it yourself. Ever.
397 * @param obj object that would be "killed"
398 * @return a readable pipe descriptor, or -1 on error.
400 int vlc_object_waitpipe( vlc_object_t *obj )
402 int pfd[2] = { -1, -1 };
403 vlc_object_internals_t *internals = vlc_internals( obj );
406 vlc_spin_lock (&internals->spin);
407 if (internals->pipes[0] == -1)
409 /* This can only ever happen if someone killed us without locking: */
410 assert (internals->pipes[1] == -1);
411 vlc_spin_unlock (&internals->spin);
416 vlc_spin_lock (&internals->spin);
417 if (internals->pipes[0] == -1)
419 internals->pipes[0] = pfd[0];
420 internals->pipes[1] = pfd[1];
421 pfd[0] = pfd[1] = -1;
425 vlc_spin_unlock (&internals->spin);
429 /* Race condition: vlc_object_kill() already invoked! */
432 vlc_spin_lock (&internals->spin);
433 fd = internals->pipes[1];
434 internals->pipes[1] = -1;
435 vlc_spin_unlock (&internals->spin);
437 msg_Dbg (obj, "waitpipe: object already dying");
442 /* Race condition: two threads call pipe() - unlikely */
448 return internals->pipes[0];
453 * Suspends until another thread calls vlc_object_signal_unlocked().
454 * The thread may be woken up earlier due to limitations of the underlying
457 * In new code, please use vlc_cond_wait() instead.
459 * This function is a cancellation point. In case of cancellation, the object
460 * will be in locked state.
462 void __vlc_object_wait( vlc_object_t *obj )
464 vlc_object_internals_t *priv = vlc_internals( obj );
465 vlc_assert_locked( &priv->lock);
466 vlc_cond_wait( &priv->wait, &priv->lock );
471 * Wakes up one thread waiting on the object. If no thread are (yet) waiting,
474 * Please do not use this function in new code as we are trying to untangle
475 * objects and threads. Use vlc_cond_wait() instead.
477 void __vlc_object_signal_unlocked( vlc_object_t *obj )
479 vlc_assert_locked (&(vlc_internals(obj)->lock));
480 vlc_cond_signal( &(vlc_internals(obj)->wait) );
485 * Requests termination of an object.
486 * If the object is LibVLC, also request to terminate all its children.
488 void __vlc_object_kill( vlc_object_t *p_this )
490 vlc_object_internals_t *priv = vlc_internals( p_this );
493 vlc_thread_cancel( p_this );
494 vlc_object_lock( p_this );
495 p_this->b_die = true;
497 vlc_spin_lock (&priv->spin);
500 vlc_spin_unlock (&priv->spin);
504 msg_Dbg (p_this, "waitpipe: object killed");
508 vlc_cond_broadcast (&priv->wait);
509 /* This also serves as a memory barrier toward vlc_object_alive(): */
510 vlc_object_unlock( p_this );
514 /*****************************************************************************
515 * find a typed object and increment its refcount
516 *****************************************************************************
517 * This function recursively looks for a given object type. i_mode can be one
518 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
519 *****************************************************************************/
520 void * __vlc_object_find( vlc_object_t *p_this, int i_type, int i_mode )
522 vlc_object_t *p_found;
524 /* If we are of the requested type ourselves, don't look further */
525 if( !(i_mode & FIND_STRICT) && p_this->i_object_type == i_type )
527 vlc_object_hold( p_this );
531 /* Otherwise, recursively look for the object */
532 if ((i_mode & 0x000f) == FIND_ANYWHERE)
533 return vlc_object_find (p_this->p_libvlc, i_type,
534 (i_mode & ~0x000f)|FIND_CHILD);
536 libvlc_lock (p_this->p_libvlc);
537 p_found = FindObject( p_this, i_type, i_mode );
538 libvlc_unlock (p_this->p_libvlc);
542 #undef vlc_object_find_name
544 * Finds a named object and increment its reference count.
545 * Beware that objects found in this manner can be "owned" by another thread,
546 * be of _any_ type, and be attached to any module (if any). With such an
547 * object reference, you can set or get object variables, emit log messages,
548 * and read write-once object parameters (psz_object_type, etc).
549 * You CANNOT cast the object to a more specific object type, and you
550 * definitely cannot invoke object type-specific callbacks with this.
552 * @param p_this object to search from
553 * @param psz_name name of the object to search for
554 * @param i_mode search direction: FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
556 * @return a matching object (must be released by the caller),
559 vlc_object_t *vlc_object_find_name( vlc_object_t *p_this,
560 const char *psz_name, int i_mode )
562 vlc_object_t *p_found;
564 /* If have the requested name ourselves, don't look further */
565 if( !(i_mode & FIND_STRICT)
566 && p_this->psz_object_name
567 && !strcmp( p_this->psz_object_name, psz_name ) )
569 vlc_object_hold( p_this );
573 libvlc_lock (p_this->p_libvlc);
575 /* Otherwise, recursively look for the object */
576 if( (i_mode & 0x000f) == FIND_ANYWHERE )
578 vlc_object_t *p_root = p_this;
581 while( p_root->p_parent != NULL &&
582 p_root != VLC_OBJECT( p_this->p_libvlc ) )
584 p_root = p_root->p_parent;
587 p_found = FindObjectName( p_root, psz_name,
588 (i_mode & ~0x000f)|FIND_CHILD );
589 if( p_found == NULL && p_root != VLC_OBJECT( p_this->p_libvlc ) )
591 p_found = FindObjectName( VLC_OBJECT( p_this->p_libvlc ),
592 psz_name, (i_mode & ~0x000f)|FIND_CHILD );
597 p_found = FindObjectName( p_this, psz_name, i_mode );
600 libvlc_unlock (p_this->p_libvlc);
605 * Increment an object reference counter.
607 void * __vlc_object_hold( vlc_object_t *p_this )
609 vlc_object_internals_t *internals = vlc_internals( p_this );
611 vlc_spin_lock( &internals->ref_spin );
612 /* Avoid obvious freed object uses */
613 assert( internals->i_refcount > 0 );
614 /* Increment the counter */
615 internals->i_refcount++;
616 vlc_spin_unlock( &internals->ref_spin );
620 /*****************************************************************************
621 * Decrement an object refcount
622 * And destroy the object if its refcount reach zero.
623 *****************************************************************************/
624 void __vlc_object_release( vlc_object_t *p_this )
626 vlc_object_internals_t *internals = vlc_internals( p_this );
627 vlc_object_t *parent = NULL;
628 bool b_should_destroy;
630 vlc_spin_lock( &internals->ref_spin );
631 assert( internals->i_refcount > 0 );
633 if( internals->i_refcount > 1 )
636 /* There are still other references to the object */
637 internals->i_refcount--;
638 vlc_spin_unlock( &internals->ref_spin );
641 vlc_spin_unlock( &internals->ref_spin );
644 /* Remember that we cannot hold the spin while waiting on the mutex */
645 libvlc_lock (p_this->p_libvlc);
646 /* Take the spin again. Note that another thread may have held the
647 * object in the (very short) mean time. */
648 vlc_spin_lock( &internals->ref_spin );
649 b_should_destroy = --internals->i_refcount == 0;
650 vlc_spin_unlock( &internals->ref_spin );
652 if( b_should_destroy )
654 /* We have no children */
655 assert (internals->i_children == 0);
656 parent = p_this->p_parent;
659 if( VLC_OBJECT(p_this->p_libvlc) == p_this )
662 vlc_object_t *leaked = internals->next;
663 while( leaked != p_this )
665 /* We are leaking this object */
667 "ERROR: leaking object (%p, type:%s, name:%s)\n",
668 leaked, leaked->psz_object_type,
669 leaked->psz_object_name );
670 /* Dump object to ease debugging */
671 vlc_object_dump( leaked );
673 leaked = vlc_internals (leaked)->next;
676 if( internals->next != p_this )
677 /* Dump libvlc object to ease debugging */
678 vlc_object_dump( p_this );
681 /* Remove the object from object list
682 * so that it cannot be encountered by vlc_object_get() */
683 vlc_internals (internals->next)->prev = internals->prev;
684 vlc_internals (internals->prev)->next = internals->next;
687 /* Detach from parent to protect against FIND_CHILDREN */
688 vlc_object_detach_unlocked (p_this);
690 libvlc_unlock (p_this->p_libvlc);
692 if( b_should_destroy )
696 canc = vlc_savecancel ();
697 vlc_object_destroy( p_this );
698 vlc_restorecancel (canc);
700 vlc_object_release (parent);
705 ****************************************************************************
706 * attach object to a parent object
707 *****************************************************************************
708 * This function sets p_this as a child of p_parent, and p_parent as a parent
709 * of p_this. This link can be undone using vlc_object_detach.
710 *****************************************************************************/
711 void __vlc_object_attach( vlc_object_t *p_this, vlc_object_t *p_parent )
713 if( !p_this ) return;
715 vlc_object_hold (p_parent);
716 libvlc_lock (p_this->p_libvlc);
718 /* Attach the parent to its child */
719 assert (!p_this->p_parent);
720 p_this->p_parent = p_parent;
722 /* Attach the child to its parent */
723 vlc_object_internals_t *priv = vlc_internals( p_parent );
724 INSERT_ELEM( priv->pp_children, priv->i_children, priv->i_children,
726 libvlc_unlock (p_this->p_libvlc);
730 static void vlc_object_detach_unlocked (vlc_object_t *p_this)
732 if (p_this->p_parent == NULL)
735 vlc_object_internals_t *priv = vlc_internals( p_this->p_parent );
739 /* Remove p_this's parent */
740 p_this->p_parent = NULL;
742 /* Remove all of p_parent's children which are p_this */
743 for( i_index = priv->i_children ; i_index-- ; )
745 if( priv->pp_children[i_index] == p_this )
748 for( i = i_index ; i < priv->i_children ; i++ )
749 priv->pp_children[i] = priv->pp_children[i+1];
753 if( priv->i_children )
755 vlc_object_t **pp_children = (vlc_object_t **)
756 realloc( priv->pp_children,
757 priv->i_children * sizeof(vlc_object_t *) );
759 priv->pp_children = pp_children;
763 /* Special case - don't realloc() to zero to avoid leaking */
764 free( priv->pp_children );
765 priv->pp_children = NULL;
771 ****************************************************************************
772 * detach object from its parent
773 *****************************************************************************
774 * This function removes all links between an object and its parent.
775 *****************************************************************************/
776 void __vlc_object_detach( vlc_object_t *p_this )
778 vlc_object_t *p_parent;
779 if( !p_this ) return;
781 libvlc_lock (p_this->p_libvlc);
782 p_parent = p_this->p_parent;
784 vlc_object_detach_unlocked( p_this );
785 libvlc_unlock (p_this->p_libvlc);
788 vlc_object_release (p_parent);
793 ****************************************************************************
794 * find a list typed objects and increment their refcount
795 *****************************************************************************
796 * This function recursively looks for a given object type. i_mode can be one
797 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
798 *****************************************************************************/
799 vlc_list_t * __vlc_list_find( vlc_object_t *p_this, int i_type, int i_mode )
804 /* Look for the objects */
805 switch( i_mode & 0x000f )
808 return vlc_list_find (p_this->p_libvlc, i_type, FIND_CHILD);
811 libvlc_lock (p_this->p_libvlc);
812 i_count = CountChildren( p_this, i_type );
813 p_list = NewList( i_count );
815 /* Check allocation was successful */
816 if( p_list->i_count != i_count )
818 libvlc_unlock (p_this->p_libvlc);
824 ListChildren( p_list, p_this, i_type );
825 libvlc_unlock (p_this->p_libvlc);
829 msg_Err( p_this, "unimplemented!" );
830 p_list = NewList( 0 );
838 * Gets the list of children of an objects, and increment their reference
840 * @return a list (possibly empty) or NULL in case of error.
842 vlc_list_t *__vlc_list_children( vlc_object_t *obj )
845 vlc_object_internals_t *priv = vlc_internals( obj );
847 libvlc_lock (obj->p_libvlc);
848 l = NewList( priv->i_children );
849 for (int i = 0; i < l->i_count; i++)
851 vlc_object_hold( priv->pp_children[i] );
852 l->p_values[i].p_object = priv->pp_children[i];
854 libvlc_unlock (obj->p_libvlc);
858 /*****************************************************************************
859 * DumpCommand: print the current vlc structure
860 *****************************************************************************
861 * This function prints either an ASCII tree showing the connections between
862 * vlc objects, and additional information such as their refcount, thread ID,
863 * etc. (command "tree"), or the same data as a simple list (command "list").
864 *****************************************************************************/
865 static int DumpCommand( vlc_object_t *p_this, char const *psz_cmd,
866 vlc_value_t oldval, vlc_value_t newval, void *p_data )
868 libvlc_int_t *p_libvlc = p_this->p_libvlc;
870 (void)oldval; (void)p_data;
871 if( *psz_cmd == 'l' )
873 vlc_object_t *cur = VLC_OBJECT (p_libvlc);
875 libvlc_lock (p_this->p_libvlc);
878 PrintObject (cur, "");
879 cur = vlc_internals (cur)->next;
881 while (cur != VLC_OBJECT(p_libvlc));
882 libvlc_unlock (p_this->p_libvlc);
886 vlc_object_t *p_object = NULL;
888 if( *newval.psz_string )
890 /* try using the object's name to find it */
891 p_object = vlc_object_find_name( p_this, newval.psz_string,
899 libvlc_lock (p_this->p_libvlc);
900 if( *psz_cmd == 't' )
902 char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
905 p_object = VLC_OBJECT(p_this->p_libvlc);
908 DumpStructure( p_object, 0, psz_foo );
910 else if( *psz_cmd == 'v' )
915 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
917 PrintObject( p_object, "" );
919 if( !vlc_internals( p_object )->i_vars )
920 printf( " `-o No variables\n" );
921 for( i = 0; i < vlc_internals( p_object )->i_vars; i++ )
923 variable_t *p_var = vlc_internals( p_object )->p_vars + i;
925 const char *psz_type = "unknown";
926 switch( p_var->i_type & VLC_VAR_TYPE )
928 #define MYCASE( type, nice ) \
929 case VLC_VAR_ ## type: \
932 MYCASE( VOID, "void" );
933 MYCASE( BOOL, "bool" );
934 MYCASE( INTEGER, "integer" );
935 MYCASE( HOTKEY, "hotkey" );
936 MYCASE( STRING, "string" );
937 MYCASE( MODULE, "module" );
938 MYCASE( FILE, "file" );
939 MYCASE( DIRECTORY, "directory" );
940 MYCASE( VARIABLE, "variable" );
941 MYCASE( FLOAT, "float" );
942 MYCASE( TIME, "time" );
943 MYCASE( ADDRESS, "address" );
944 MYCASE( MUTEX, "mutex" );
945 MYCASE( LIST, "list" );
948 printf( " %c-o \"%s\" (%s",
949 i + 1 == vlc_internals( p_object )->i_vars ? '`' : '|',
950 p_var->psz_name, psz_type );
951 if( p_var->psz_text )
952 printf( ", %s", p_var->psz_text );
954 if( p_var->i_type & VLC_VAR_HASCHOICE )
955 printf( ", has choices" );
956 if( p_var->i_type & VLC_VAR_ISCOMMAND )
957 printf( ", command" );
958 if( p_var->i_entries )
959 printf( ", %d callbacks", p_var->i_entries );
960 switch( p_var->i_type & VLC_VAR_CLASS )
966 printf( ": %s", p_var->val.b_bool ? "true" : "false" );
968 case VLC_VAR_INTEGER:
969 printf( ": %d", p_var->val.i_int );
972 printf( ": \"%s\"", p_var->val.psz_string );
975 printf( ": %f", p_var->val.f_float );
978 printf( ": %"PRIi64, (int64_t)p_var->val.i_time );
980 case VLC_VAR_ADDRESS:
981 printf( ": %p", p_var->val.p_address );
990 libvlc_unlock (p_this->p_libvlc);
992 if( *newval.psz_string )
994 vlc_object_release( p_object );
1001 /*****************************************************************************
1002 * vlc_list_release: free a list previously allocated by vlc_list_find
1003 *****************************************************************************
1004 * This function decreases the refcount of all objects in the list and
1006 *****************************************************************************/
1007 void vlc_list_release( vlc_list_t *p_list )
1011 for( i_index = 0; i_index < p_list->i_count; i_index++ )
1013 vlc_object_release( p_list->p_values[i_index].p_object );
1016 free( p_list->p_values );
1020 /*****************************************************************************
1021 * dump an object. (Debug function)
1022 *****************************************************************************/
1023 static void vlc_object_dump( vlc_object_t *p_this )
1025 char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
1028 DumpStructure( p_this, 0, psz_foo );
1031 /* Following functions are local */
1033 static vlc_object_t * FindObject( vlc_object_t *p_this, int i_type, int i_mode )
1036 vlc_object_t *p_tmp;
1038 switch( i_mode & 0x000f )
1041 p_tmp = p_this->p_parent;
1044 if( p_tmp->i_object_type == i_type )
1046 vlc_object_hold( p_tmp );
1051 return FindObject( p_tmp, i_type, i_mode );
1057 for( i = vlc_internals( p_this )->i_children; i--; )
1059 p_tmp = vlc_internals( p_this )->pp_children[i];
1060 if( p_tmp->i_object_type == i_type )
1062 vlc_object_hold( p_tmp );
1065 else if( vlc_internals( p_tmp )->i_children )
1067 p_tmp = FindObject( p_tmp, i_type, i_mode );
1077 /* Handled in vlc_object_find */
1084 static vlc_object_t * FindObjectName( vlc_object_t *p_this,
1085 const char *psz_name,
1089 vlc_object_t *p_tmp;
1091 switch( i_mode & 0x000f )
1094 p_tmp = p_this->p_parent;
1097 if( p_tmp->psz_object_name
1098 && !strcmp( p_tmp->psz_object_name, psz_name ) )
1100 vlc_object_hold( p_tmp );
1105 return FindObjectName( p_tmp, psz_name, i_mode );
1111 for( i = vlc_internals( p_this )->i_children; i--; )
1113 p_tmp = vlc_internals( p_this )->pp_children[i];
1114 if( p_tmp->psz_object_name
1115 && !strcmp( p_tmp->psz_object_name, psz_name ) )
1117 vlc_object_hold( p_tmp );
1120 else if( vlc_internals( p_tmp )->i_children )
1122 p_tmp = FindObjectName( p_tmp, psz_name, i_mode );
1132 /* Handled in vlc_object_find */
1140 static void PrintObject( vlc_object_t *p_this, const char *psz_prefix )
1142 char psz_children[20], psz_refcount[20], psz_thread[30], psz_name[50],
1145 int canc = vlc_savecancel ();
1146 memset( &psz_name, 0, sizeof(psz_name) );
1147 if( p_this->psz_object_name )
1149 snprintf( psz_name, 49, " \"%s\"", p_this->psz_object_name );
1151 psz_name[48] = '\"';
1154 psz_children[0] = '\0';
1155 switch( vlc_internals( p_this )->i_children )
1160 strcpy( psz_children, ", 1 child" );
1163 snprintf( psz_children, 19, ", %i children",
1164 vlc_internals( p_this )->i_children );
1168 psz_refcount[0] = '\0';
1169 if( vlc_internals( p_this )->i_refcount > 0 )
1170 snprintf( psz_refcount, 19, ", refcount %u",
1171 vlc_internals( p_this )->i_refcount );
1173 psz_thread[0] = '\0';
1174 if( vlc_internals( p_this )->b_thread )
1175 snprintf( psz_thread, 29, " (thread %lu)",
1176 (unsigned long)vlc_internals( p_this )->thread_id );
1178 psz_parent[0] = '\0';
1179 if( p_this->p_parent )
1180 snprintf( psz_parent, 19, ", parent %p", p_this->p_parent );
1182 printf( " %so %p %s%s%s%s%s%s\n", psz_prefix,
1183 p_this, p_this->psz_object_type,
1184 psz_name, psz_thread, psz_refcount, psz_children,
1186 vlc_restorecancel (canc);
1189 static void DumpStructure( vlc_object_t *p_this, int i_level, char *psz_foo )
1192 char i_back = psz_foo[i_level];
1193 psz_foo[i_level] = '\0';
1195 PrintObject( p_this, psz_foo );
1197 psz_foo[i_level] = i_back;
1199 if( i_level / 2 >= MAX_DUMPSTRUCTURE_DEPTH )
1201 msg_Warn( p_this, "structure tree is too deep" );
1205 for( i = 0 ; i < vlc_internals( p_this )->i_children ; i++ )
1209 psz_foo[i_level-1] = ' ';
1211 if( psz_foo[i_level-2] == '`' )
1213 psz_foo[i_level-2] = ' ';
1217 if( i == vlc_internals( p_this )->i_children - 1 )
1219 psz_foo[i_level] = '`';
1223 psz_foo[i_level] = '|';
1226 psz_foo[i_level+1] = '-';
1227 psz_foo[i_level+2] = '\0';
1229 DumpStructure( vlc_internals( p_this )->pp_children[i], i_level + 2,
1234 static vlc_list_t * NewList( int i_count )
1236 vlc_list_t * p_list = (vlc_list_t *)malloc( sizeof( vlc_list_t ) );
1237 if( p_list == NULL )
1242 p_list->i_count = i_count;
1246 p_list->p_values = NULL;
1250 p_list->p_values = malloc( i_count * sizeof( vlc_value_t ) );
1251 if( p_list->p_values == NULL )
1253 p_list->i_count = 0;
1260 static void ListReplace( vlc_list_t *p_list, vlc_object_t *p_object,
1263 if( p_list == NULL || i_index >= p_list->i_count )
1268 vlc_object_hold( p_object );
1270 p_list->p_values[i_index].p_object = p_object;
1275 /*static void ListAppend( vlc_list_t *p_list, vlc_object_t *p_object )
1277 if( p_list == NULL )
1282 p_list->p_values = realloc( p_list->p_values, (p_list->i_count + 1)
1283 * sizeof( vlc_value_t ) );
1284 if( p_list->p_values == NULL )
1286 p_list->i_count = 0;
1290 vlc_object_hold( p_object );
1292 p_list->p_values[p_list->i_count].p_object = p_object;
1298 static int CountChildren( vlc_object_t *p_this, int i_type )
1300 vlc_object_t *p_tmp;
1303 for( i = 0; i < vlc_internals( p_this )->i_children; i++ )
1305 p_tmp = vlc_internals( p_this )->pp_children[i];
1307 if( p_tmp->i_object_type == i_type )
1311 i_count += CountChildren( p_tmp, i_type );
1317 static void ListChildren( vlc_list_t *p_list, vlc_object_t *p_this, int i_type )
1319 vlc_object_t *p_tmp;
1322 for( i = 0; i < vlc_internals( p_this )->i_children; i++ )
1324 p_tmp = vlc_internals( p_this )->pp_children[i];
1326 if( p_tmp->i_object_type == i_type )
1327 ListReplace( p_list, p_tmp, p_list->i_count++ );
1329 ListChildren( p_list, p_tmp, i_type );