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"
43 #include "audio_output/aout_internal.h"
45 #include "vlc_interface.h"
46 #include "vlc_codec.h"
48 #include "variables.h"
54 # include <errno.h> /* ENOSYS */
61 #if defined (HAVE_SYS_EVENTFD_H)
62 # include <sys/eventfd.h>
66 /*****************************************************************************
68 *****************************************************************************/
69 static int DumpCommand( vlc_object_t *, char const *,
70 vlc_value_t, vlc_value_t, void * );
72 static vlc_object_t * FindObject ( vlc_object_t *, int, int );
73 static vlc_object_t * FindObjectName( vlc_object_t *, const char *, int );
74 static void PrintObject ( vlc_object_t *, const char * );
75 static void DumpStructure ( vlc_object_t *, int, char * );
77 static vlc_list_t * NewList ( int );
78 static void ListReplace ( vlc_list_t *, vlc_object_t *, int );
79 /*static void ListAppend ( vlc_list_t *, vlc_object_t * );*/
80 static int CountChildren ( vlc_object_t *, int );
81 static void ListChildren ( vlc_list_t *, vlc_object_t *, int );
83 static void vlc_object_destroy( vlc_object_t *p_this );
84 static void vlc_object_detach_unlocked (vlc_object_t *p_this);
86 /*****************************************************************************
87 * Local structure lock
88 *****************************************************************************/
89 static void libvlc_lock (libvlc_int_t *p_libvlc)
91 vlc_mutex_lock (&(libvlc_priv (p_libvlc)->structure_lock));
94 static void libvlc_unlock (libvlc_int_t *p_libvlc)
96 vlc_mutex_unlock (&(libvlc_priv (p_libvlc)->structure_lock));
99 void *__vlc_custom_create( vlc_object_t *p_this, size_t i_size,
100 int i_type, const char *psz_type )
103 vlc_object_internals_t *p_priv;
106 * VLC objects are laid out as follow:
107 * - first the LibVLC-private per-object data,
108 * - then VLC_COMMON members from vlc_object_t,
109 * - finally, the type-specific data (if any).
111 * This function initializes the LibVLC and common data,
112 * and zeroes the rest.
114 p_priv = calloc( 1, sizeof( *p_priv ) + i_size );
118 assert (i_size >= sizeof (vlc_object_t));
119 p_new = (vlc_object_t *)(p_priv + 1);
121 p_priv->i_object_type = i_type;
122 p_new->psz_object_type = psz_type;
123 p_priv->psz_name = NULL;
125 p_new->b_die = false;
126 p_new->b_error = false;
127 p_new->b_force = false;
129 p_new->psz_header = NULL;
132 p_new->i_flags = p_this->i_flags
133 & (OBJECT_FLAGS_NODBG|OBJECT_FLAGS_QUIET|OBJECT_FLAGS_NOINTERACT);
135 p_priv->var_root = NULL;
139 libvlc_int_t *self = (libvlc_int_t*)p_new;
140 p_new->p_libvlc = self;
141 vlc_mutex_init (&(libvlc_priv (self)->structure_lock));
145 p_new->p_libvlc = p_this->p_libvlc;
147 vlc_spin_init( &p_priv->ref_spin );
148 p_priv->i_refcount = 1;
149 p_priv->pf_destructor = NULL;
150 p_priv->b_thread = false;
151 p_new->p_parent = NULL;
152 p_priv->pp_children = NULL;
153 p_priv->i_children = 0;
155 /* Initialize mutexes and condvars */
156 vlc_mutex_init( &p_priv->var_lock );
157 vlc_cond_init( &p_priv->var_wait );
158 p_priv->pipes[0] = p_priv->pipes[1] = -1;
160 if (p_new == VLC_OBJECT(p_new->p_libvlc))
161 { /* TODO: should be in src/libvlc.c */
162 int canc = vlc_savecancel ();
163 var_Create( p_new, "tree", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
164 var_AddCallback( p_new, "tree", DumpCommand, NULL );
165 var_Create( p_new, "vars", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
166 var_AddCallback( p_new, "vars", DumpCommand, NULL );
167 vlc_restorecancel (canc);
175 * Allocates and initializes a vlc object.
177 * @param i_type known object type (all of them are negative integer values),
178 * or object byte size (always positive).
180 * @return the new object, or NULL on error.
182 void * __vlc_object_create( vlc_object_t *p_this, int i_type )
184 const char * psz_type;
189 case VLC_OBJECT_DECODER:
190 i_size = sizeof(decoder_t);
191 psz_type = "decoder";
193 case VLC_OBJECT_AOUT:
194 i_size = sizeof(aout_instance_t);
195 psz_type = "audio output";
198 assert( i_type > 0 ); /* unknown type?! */
200 i_type = VLC_OBJECT_GENERIC;
201 psz_type = "generic";
205 return vlc_custom_create( p_this, i_size, i_type, psz_type );
210 ****************************************************************************
211 * Set the destructor of a vlc object
213 * This function sets the destructor of the vlc object. It will be called
214 * when the object is destroyed when the its refcount reaches 0.
215 * (It is called by the internal function vlc_object_destroy())
216 *****************************************************************************/
217 void __vlc_object_set_destructor( vlc_object_t *p_this,
218 vlc_destructor_t pf_destructor )
220 vlc_object_internals_t *p_priv = vlc_internals(p_this );
222 vlc_spin_lock( &p_priv->ref_spin );
223 p_priv->pf_destructor = pf_destructor;
224 vlc_spin_unlock( &p_priv->ref_spin );
227 static vlc_mutex_t name_lock = VLC_STATIC_MUTEX;
229 #undef vlc_object_set_name
230 int vlc_object_set_name(vlc_object_t *obj, const char *name)
232 vlc_object_internals_t *priv = vlc_internals(obj);
233 char *newname = name ? strdup (name) : NULL;
236 vlc_mutex_lock (&name_lock);
237 oldname = priv->psz_name;
238 priv->psz_name = newname;
239 vlc_mutex_unlock (&name_lock);
242 return (priv->psz_name || !name) ? VLC_SUCCESS : VLC_ENOMEM;
245 #undef vlc_object_get_name
246 char *vlc_object_get_name(const vlc_object_t *obj)
248 vlc_object_internals_t *priv = vlc_internals(obj);
251 vlc_mutex_lock (&name_lock);
252 name = priv->psz_name ? strdup (priv->psz_name) : NULL;
253 vlc_mutex_unlock (&name_lock);
259 ****************************************************************************
260 * Destroy a vlc object (Internal)
262 * This function destroys an object that has been previously allocated with
263 * vlc_object_create. The object's refcount must be zero and it must not be
264 * attached to other objects in any way.
266 * This function must be called with cancellation disabled (currently).
267 *****************************************************************************/
268 static void vlc_object_destroy( vlc_object_t *p_this )
270 vlc_object_internals_t *p_priv = vlc_internals( p_this );
272 /* Objects are always detached beforehand */
273 assert( !p_this->p_parent );
275 /* Send a kill to the object's thread if applicable */
276 vlc_object_kill( p_this );
278 /* Call the custom "subclass" destructor */
279 if( p_priv->pf_destructor )
280 p_priv->pf_destructor( p_this );
282 /* Any thread must have been cleaned up at this point. */
283 assert( !p_priv->b_thread );
285 /* Destroy the associated variables. */
286 var_DestroyAll( p_this );
288 vlc_cond_destroy( &p_priv->var_wait );
289 vlc_mutex_destroy( &p_priv->var_lock );
291 free( p_this->psz_header );
293 free( p_priv->psz_name );
295 vlc_spin_destroy( &p_priv->ref_spin );
296 if( p_priv->pipes[1] != -1 && p_priv->pipes[1] != p_priv->pipes[0] )
297 close( p_priv->pipes[1] );
298 if( p_priv->pipes[0] != -1 )
299 close( p_priv->pipes[0] );
300 if( VLC_OBJECT(p_this->p_libvlc) == p_this )
301 vlc_mutex_destroy (&(libvlc_priv ((libvlc_int_t *)p_this)->structure_lock));
308 # include <winsock2.h>
309 # include <ws2tcpip.h>
312 * select()-able pipes emulated using Winsock
314 static int pipe (int fd[2])
317 int addrlen = sizeof (addr);
319 SOCKET l = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP), a,
320 c = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
321 if ((l == INVALID_SOCKET) || (c == INVALID_SOCKET))
324 memset (&addr, 0, sizeof (addr));
325 addr.sin_family = AF_INET;
326 addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
327 if (bind (l, (PSOCKADDR)&addr, sizeof (addr))
328 || getsockname (l, (PSOCKADDR)&addr, &addrlen)
330 || connect (c, (PSOCKADDR)&addr, addrlen))
333 a = accept (l, NULL, NULL);
334 if (a == INVALID_SOCKET)
345 if (l != INVALID_SOCKET)
347 if (c != INVALID_SOCKET)
353 #define read( a, b, c ) recv (a, b, c, 0)
355 #define write( a, b, c ) send (a, b, c, 0)
357 #define close( a ) closesocket (a)
360 static vlc_mutex_t pipe_lock = VLC_STATIC_MUTEX;
363 * Returns the readable end of a pipe that becomes readable once termination
364 * of the object is requested (vlc_object_kill()).
365 * This can be used to wake-up out of a select() or poll() event loop, such
366 * typically when doing network I/O.
368 * Note that the pipe will remain the same for the lifetime of the object.
369 * DO NOT read the pipe nor close it yourself. Ever.
371 * @param obj object that would be "killed"
372 * @return a readable pipe descriptor, or -1 on error.
374 int vlc_object_waitpipe( vlc_object_t *obj )
376 vlc_object_internals_t *internals = vlc_internals( obj );
378 vlc_mutex_lock (&pipe_lock);
379 if (internals->pipes[0] == -1)
381 /* This can only ever happen if someone killed us without locking: */
382 assert (internals->pipes[1] == -1);
384 #if defined (HAVE_SYS_EVENTFD_H)
385 internals->pipes[0] = internals->pipes[1] = eventfd (0, 0);
386 if (internals->pipes[0] == -1)
389 if (pipe (internals->pipes))
390 internals->pipes[0] = internals->pipes[1] = -1;
393 if (internals->pipes[0] != -1 && obj->b_die)
394 { /* Race condition: vlc_object_kill() already invoked! */
395 msg_Dbg (obj, "waitpipe: object already dying");
396 write (internals->pipes[1], &(uint64_t){ 1 }, sizeof (uint64_t));
399 vlc_mutex_unlock (&pipe_lock);
400 return internals->pipes[0];
405 * Requests termination of an object, cancels the object thread, and make the
406 * object wait pipe (if it exists) readable. Not a cancellation point.
408 void __vlc_object_kill( vlc_object_t *p_this )
410 vlc_object_internals_t *priv = vlc_internals( p_this );
413 vlc_thread_cancel( p_this );
414 vlc_mutex_lock( &pipe_lock );
418 p_this->b_die = true;
421 /* This also serves as a memory barrier toward vlc_object_alive(): */
422 vlc_mutex_unlock( &pipe_lock );
426 int canc = vlc_savecancel ();
428 /* write _after_ setting b_die, so vlc_object_alive() returns false */
429 write (fd, &(uint64_t){ 1 }, sizeof (uint64_t));
430 msg_Dbg (p_this, "waitpipe: object killed");
431 vlc_restorecancel (canc);
436 /*****************************************************************************
437 * find a typed object and increment its refcount
438 *****************************************************************************
439 * This function recursively looks for a given object type. i_mode can be one
440 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
441 *****************************************************************************/
442 void * __vlc_object_find( vlc_object_t *p_this, int i_type, int i_mode )
444 vlc_object_t *p_found;
446 /* If we are of the requested type ourselves, don't look further */
447 if( !(i_mode & FIND_STRICT)
448 && vlc_internals (p_this)->i_object_type == i_type )
450 vlc_object_hold( p_this );
454 /* Otherwise, recursively look for the object */
455 if ((i_mode & 0x000f) == FIND_ANYWHERE)
456 return vlc_object_find (p_this->p_libvlc, i_type,
457 (i_mode & ~0x000f)|FIND_CHILD);
459 libvlc_lock (p_this->p_libvlc);
460 p_found = FindObject( p_this, i_type, i_mode );
461 libvlc_unlock (p_this->p_libvlc);
466 static int objnamecmp(const vlc_object_t *obj, const char *name)
468 char *objname = vlc_object_get_name(obj);
472 int ret = strcmp (objname, name);
477 #undef vlc_object_find_name
479 * Finds a named object and increment its reference count.
480 * Beware that objects found in this manner can be "owned" by another thread,
481 * be of _any_ type, and be attached to any module (if any). With such an
482 * object reference, you can set or get object variables, emit log messages,
483 * and read write-once object parameters (psz_object_type, etc).
484 * You CANNOT cast the object to a more specific object type, and you
485 * definitely cannot invoke object type-specific callbacks with this.
487 * @param p_this object to search from
488 * @param psz_name name of the object to search for
489 * @param i_mode search direction: FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
491 * @return a matching object (must be released by the caller),
494 vlc_object_t *vlc_object_find_name( vlc_object_t *p_this,
495 const char *psz_name, int i_mode )
497 vlc_object_t *p_found;
499 /* Reading psz_object_name from a separate inhibits thread-safety.
500 * Use a libvlc address variable instead for that sort of things! */
501 msg_Warn( p_this, "%s(%s) is not safe!", __func__, psz_name );
502 /* If have the requested name ourselves, don't look further */
503 if( !(i_mode & FIND_STRICT) && !objnamecmp(p_this, psz_name) )
505 vlc_object_hold( p_this );
509 libvlc_lock (p_this->p_libvlc);
511 /* Otherwise, recursively look for the object */
512 if( (i_mode & 0x000f) == FIND_ANYWHERE )
514 vlc_object_t *p_root = p_this;
517 while( p_root->p_parent != NULL &&
518 p_root != VLC_OBJECT( p_this->p_libvlc ) )
520 p_root = p_root->p_parent;
523 p_found = FindObjectName( p_root, psz_name,
524 (i_mode & ~0x000f)|FIND_CHILD );
525 if( p_found == NULL && p_root != VLC_OBJECT( p_this->p_libvlc ) )
527 p_found = FindObjectName( VLC_OBJECT( p_this->p_libvlc ),
528 psz_name, (i_mode & ~0x000f)|FIND_CHILD );
533 p_found = FindObjectName( p_this, psz_name, i_mode );
536 libvlc_unlock (p_this->p_libvlc);
541 * Increment an object reference counter.
543 void * __vlc_object_hold( vlc_object_t *p_this )
545 vlc_object_internals_t *internals = vlc_internals( p_this );
547 vlc_spin_lock( &internals->ref_spin );
548 /* Avoid obvious freed object uses */
549 assert( internals->i_refcount > 0 );
550 /* Increment the counter */
551 internals->i_refcount++;
552 vlc_spin_unlock( &internals->ref_spin );
556 /*****************************************************************************
557 * Decrement an object refcount
558 * And destroy the object if its refcount reach zero.
559 *****************************************************************************/
560 void __vlc_object_release( vlc_object_t *p_this )
562 vlc_object_internals_t *internals = vlc_internals( p_this );
563 vlc_object_t *parent = NULL;
564 bool b_should_destroy;
566 vlc_spin_lock( &internals->ref_spin );
567 assert( internals->i_refcount > 0 );
569 if( internals->i_refcount > 1 )
572 /* There are still other references to the object */
573 internals->i_refcount--;
574 vlc_spin_unlock( &internals->ref_spin );
577 vlc_spin_unlock( &internals->ref_spin );
580 /* Remember that we cannot hold the spin while waiting on the mutex */
581 libvlc_lock (p_this->p_libvlc);
582 /* Take the spin again. Note that another thread may have held the
583 * object in the (very short) mean time. */
584 vlc_spin_lock( &internals->ref_spin );
585 b_should_destroy = --internals->i_refcount == 0;
586 vlc_spin_unlock( &internals->ref_spin );
588 if( b_should_destroy )
590 parent = p_this->p_parent;
592 /* Detach from parent to protect against FIND_CHILDREN */
593 vlc_object_detach_unlocked (p_this);
595 /* We have no children */
596 assert (internals->i_children == 0);
598 libvlc_unlock (p_this->p_libvlc);
600 if( b_should_destroy )
604 canc = vlc_savecancel ();
605 vlc_object_destroy( p_this );
606 vlc_restorecancel (canc);
608 vlc_object_release (parent);
613 ****************************************************************************
614 * attach object to a parent object
615 *****************************************************************************
616 * This function sets p_this as a child of p_parent, and p_parent as a parent
617 * of p_this. This link can be undone using vlc_object_detach.
618 *****************************************************************************/
619 void __vlc_object_attach( vlc_object_t *p_this, vlc_object_t *p_parent )
621 if( !p_this ) return;
623 vlc_object_hold (p_parent);
624 libvlc_lock (p_this->p_libvlc);
626 /* Attach the parent to its child */
627 assert (!p_this->p_parent);
628 p_this->p_parent = p_parent;
630 /* Attach the child to its parent */
631 vlc_object_internals_t *priv = vlc_internals( p_parent );
632 INSERT_ELEM( priv->pp_children, priv->i_children, priv->i_children,
634 libvlc_unlock (p_this->p_libvlc);
638 static void vlc_object_detach_unlocked (vlc_object_t *p_this)
640 if (p_this->p_parent == NULL)
643 vlc_object_internals_t *priv = vlc_internals( p_this->p_parent );
647 /* Remove p_this's parent */
648 p_this->p_parent = NULL;
650 /* Remove all of p_parent's children which are p_this */
651 for( i_index = priv->i_children ; i_index-- ; )
653 if( priv->pp_children[i_index] == p_this )
656 for( i = i_index ; i < priv->i_children ; i++ )
657 priv->pp_children[i] = priv->pp_children[i+1];
661 if( priv->i_children )
663 vlc_object_t **pp_children = (vlc_object_t **)
664 realloc( priv->pp_children,
665 priv->i_children * sizeof(vlc_object_t *) );
667 priv->pp_children = pp_children;
671 /* Special case - don't realloc() to zero to avoid leaking */
672 free( priv->pp_children );
673 priv->pp_children = NULL;
679 ****************************************************************************
680 * detach object from its parent
681 *****************************************************************************
682 * This function removes all links between an object and its parent.
683 *****************************************************************************/
684 void __vlc_object_detach( vlc_object_t *p_this )
686 vlc_object_t *p_parent;
687 if( !p_this ) return;
689 libvlc_lock (p_this->p_libvlc);
690 p_parent = p_this->p_parent;
692 vlc_object_detach_unlocked( p_this );
693 libvlc_unlock (p_this->p_libvlc);
696 vlc_object_release (p_parent);
701 ****************************************************************************
702 * find a list typed objects and increment their refcount
703 *****************************************************************************
704 * This function recursively looks for a given object type. i_mode can be one
705 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
706 *****************************************************************************/
707 vlc_list_t * vlc_list_find( vlc_object_t *p_this, int i_type, int i_mode )
712 /* Look for the objects */
713 switch( i_mode & 0x000f )
716 return vlc_list_find (VLC_OBJECT(p_this->p_libvlc), i_type, FIND_CHILD);
719 libvlc_lock (p_this->p_libvlc);
720 i_count = CountChildren( p_this, i_type );
721 p_list = NewList( i_count );
723 /* Check allocation was successful */
724 if( p_list->i_count != i_count )
726 libvlc_unlock (p_this->p_libvlc);
732 ListChildren( p_list, p_this, i_type );
733 libvlc_unlock (p_this->p_libvlc);
737 msg_Err( p_this, "unimplemented!" );
738 p_list = NewList( 0 );
746 * Gets the list of children of an objects, and increment their reference
748 * @return a list (possibly empty) or NULL in case of error.
750 vlc_list_t *__vlc_list_children( vlc_object_t *obj )
753 vlc_object_internals_t *priv = vlc_internals( obj );
755 libvlc_lock (obj->p_libvlc);
756 l = NewList( priv->i_children );
757 for (int i = 0; i < l->i_count; i++)
759 vlc_object_hold( priv->pp_children[i] );
760 l->p_values[i].p_object = priv->pp_children[i];
762 libvlc_unlock (obj->p_libvlc);
766 static void DumpVariable (const void *data, const VISIT which, const int depth)
768 if (which != postorder && which != leaf)
772 const variable_t *p_var = *(const variable_t **)data;
773 const char *psz_type = "unknown";
775 switch( p_var->i_type & VLC_VAR_TYPE )
777 #define MYCASE( type, nice ) \
778 case VLC_VAR_ ## type: \
781 MYCASE( VOID, "void" );
782 MYCASE( BOOL, "bool" );
783 MYCASE( INTEGER, "integer" );
784 MYCASE( HOTKEY, "hotkey" );
785 MYCASE( STRING, "string" );
786 MYCASE( MODULE, "module" );
787 MYCASE( FILE, "file" );
788 MYCASE( DIRECTORY, "directory" );
789 MYCASE( VARIABLE, "variable" );
790 MYCASE( FLOAT, "float" );
791 MYCASE( TIME, "time" );
792 MYCASE( ADDRESS, "address" );
793 MYCASE( MUTEX, "mutex" );
794 MYCASE( LIST, "list" );
797 printf( " *-o \"%s\" (%s", p_var->psz_name, psz_type );
798 if( p_var->psz_text )
799 printf( ", %s", p_var->psz_text );
801 if( p_var->i_type & VLC_VAR_HASCHOICE )
802 printf( ", has choices" );
803 if( p_var->i_type & VLC_VAR_ISCOMMAND )
804 printf( ", command" );
805 if( p_var->i_entries )
806 printf( ", %d callbacks", p_var->i_entries );
807 switch( p_var->i_type & VLC_VAR_CLASS )
813 printf( ": %s", p_var->val.b_bool ? "true" : "false" );
815 case VLC_VAR_INTEGER:
816 printf( ": %d", p_var->val.i_int );
819 printf( ": \"%s\"", p_var->val.psz_string );
822 printf( ": %f", p_var->val.f_float );
825 printf( ": %"PRIi64, (int64_t)p_var->val.i_time );
827 case VLC_VAR_ADDRESS:
828 printf( ": %p", p_var->val.p_address );
837 /*****************************************************************************
838 * DumpCommand: print the current vlc structure
839 *****************************************************************************
840 * This function prints either an ASCII tree showing the connections between
841 * vlc objects, and additional information such as their refcount, thread ID,
842 * etc. (command "tree"), or the same data as a simple list (command "list").
843 *****************************************************************************/
844 static int DumpCommand( vlc_object_t *p_this, char const *psz_cmd,
845 vlc_value_t oldval, vlc_value_t newval, void *p_data )
847 (void)oldval; (void)p_data;
848 vlc_object_t *p_object = NULL;
850 if( *newval.psz_string )
852 /* try using the object's name to find it */
853 p_object = vlc_object_find_name( p_this, newval.psz_string,
861 libvlc_lock (p_this->p_libvlc);
862 if( *psz_cmd == 't' )
864 char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
867 p_object = VLC_OBJECT(p_this->p_libvlc);
870 DumpStructure( p_object, 0, psz_foo );
872 else if( *psz_cmd == 'v' )
875 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
877 PrintObject( p_object, "" );
878 vlc_mutex_lock( &vlc_internals( p_object )->var_lock );
879 if( vlc_internals( p_object )->var_root == NULL )
880 printf( " `-o No variables\n" );
882 twalk( vlc_internals( p_object )->var_root, DumpVariable );
883 vlc_mutex_unlock( &vlc_internals( p_object )->var_lock );
885 libvlc_unlock (p_this->p_libvlc);
887 if( *newval.psz_string )
889 vlc_object_release( p_object );
894 /*****************************************************************************
895 * vlc_list_release: free a list previously allocated by vlc_list_find
896 *****************************************************************************
897 * This function decreases the refcount of all objects in the list and
899 *****************************************************************************/
900 void vlc_list_release( vlc_list_t *p_list )
904 for( i_index = 0; i_index < p_list->i_count; i_index++ )
906 vlc_object_release( p_list->p_values[i_index].p_object );
909 free( p_list->p_values );
913 /* Following functions are local */
915 static vlc_object_t * FindObject( vlc_object_t *p_this, int i_type, int i_mode )
920 switch( i_mode & 0x000f )
923 p_tmp = p_this->p_parent;
926 if( vlc_internals( p_tmp )->i_object_type == i_type )
928 vlc_object_hold( p_tmp );
933 return FindObject( p_tmp, i_type, i_mode );
939 for( i = vlc_internals( p_this )->i_children; i--; )
941 p_tmp = vlc_internals( p_this )->pp_children[i];
942 if( vlc_internals( p_tmp )->i_object_type == i_type )
944 vlc_object_hold( p_tmp );
947 else if( vlc_internals( p_tmp )->i_children )
949 p_tmp = FindObject( p_tmp, i_type, i_mode );
959 /* Handled in vlc_object_find */
966 static vlc_object_t * FindObjectName( vlc_object_t *p_this,
967 const char *psz_name,
973 switch( i_mode & 0x000f )
976 p_tmp = p_this->p_parent;
979 if( !objnamecmp(p_tmp, psz_name) )
981 vlc_object_hold( p_tmp );
986 return FindObjectName( p_tmp, psz_name, i_mode );
992 for( i = vlc_internals( p_this )->i_children; i--; )
994 p_tmp = vlc_internals( p_this )->pp_children[i];
995 if( !objnamecmp(p_tmp, psz_name ) )
997 vlc_object_hold( p_tmp );
1000 else if( vlc_internals( p_tmp )->i_children )
1002 p_tmp = FindObjectName( p_tmp, psz_name, i_mode );
1012 /* Handled in vlc_object_find */
1020 static void PrintObject( vlc_object_t *p_this, const char *psz_prefix )
1022 char psz_children[20], psz_refcount[20], psz_thread[30], psz_name[50],
1025 int canc = vlc_savecancel ();
1026 memset( &psz_name, 0, sizeof(psz_name) );
1027 char *name = vlc_object_get_name(p_this);
1030 snprintf( psz_name, 49, " \"%s\"", name );
1033 psz_name[48] = '\"';
1036 psz_children[0] = '\0';
1037 switch( vlc_internals( p_this )->i_children )
1042 strcpy( psz_children, ", 1 child" );
1045 snprintf( psz_children, 19, ", %i children",
1046 vlc_internals( p_this )->i_children );
1050 psz_refcount[0] = '\0';
1051 if( vlc_internals( p_this )->i_refcount > 0 )
1052 snprintf( psz_refcount, 19, ", refcount %u",
1053 vlc_internals( p_this )->i_refcount );
1055 psz_thread[0] = '\0';
1056 if( vlc_internals( p_this )->b_thread )
1057 snprintf( psz_thread, 29, " (thread %lu)",
1058 (unsigned long)vlc_internals( p_this )->thread_id );
1060 psz_parent[0] = '\0';
1061 if( p_this->p_parent )
1062 snprintf( psz_parent, 19, ", parent %p", p_this->p_parent );
1064 printf( " %so %p %s%s%s%s%s%s\n", psz_prefix,
1065 p_this, p_this->psz_object_type,
1066 psz_name, psz_thread, psz_refcount, psz_children,
1068 vlc_restorecancel (canc);
1071 static void DumpStructure( vlc_object_t *p_this, int i_level, char *psz_foo )
1074 char i_back = psz_foo[i_level];
1075 psz_foo[i_level] = '\0';
1077 PrintObject( p_this, psz_foo );
1079 psz_foo[i_level] = i_back;
1081 if( i_level / 2 >= MAX_DUMPSTRUCTURE_DEPTH )
1083 msg_Warn( p_this, "structure tree is too deep" );
1087 for( i = 0 ; i < vlc_internals( p_this )->i_children ; i++ )
1091 psz_foo[i_level-1] = ' ';
1093 if( psz_foo[i_level-2] == '`' )
1095 psz_foo[i_level-2] = ' ';
1099 if( i == vlc_internals( p_this )->i_children - 1 )
1101 psz_foo[i_level] = '`';
1105 psz_foo[i_level] = '|';
1108 psz_foo[i_level+1] = '-';
1109 psz_foo[i_level+2] = '\0';
1111 DumpStructure( vlc_internals( p_this )->pp_children[i], i_level + 2,
1116 static vlc_list_t * NewList( int i_count )
1118 vlc_list_t * p_list = malloc( sizeof( vlc_list_t ) );
1119 if( p_list == NULL )
1122 p_list->i_count = i_count;
1126 p_list->p_values = NULL;
1130 p_list->p_values = malloc( i_count * sizeof( vlc_value_t ) );
1131 if( p_list->p_values == NULL )
1133 p_list->i_count = 0;
1140 static void ListReplace( vlc_list_t *p_list, vlc_object_t *p_object,
1143 if( p_list == NULL || i_index >= p_list->i_count )
1148 vlc_object_hold( p_object );
1150 p_list->p_values[i_index].p_object = p_object;
1155 /*static void ListAppend( vlc_list_t *p_list, vlc_object_t *p_object )
1157 if( p_list == NULL )
1162 p_list->p_values = realloc_or_free( p_list->p_values,
1163 (p_list->i_count + 1) * sizeof( vlc_value_t ) );
1164 if( p_list->p_values == NULL )
1166 p_list->i_count = 0;
1170 vlc_object_hold( p_object );
1172 p_list->p_values[p_list->i_count].p_object = p_object;
1178 static int CountChildren( vlc_object_t *p_this, int i_type )
1180 vlc_object_t *p_tmp;
1183 for( i = 0; i < vlc_internals( p_this )->i_children; i++ )
1185 p_tmp = vlc_internals( p_this )->pp_children[i];
1187 if( vlc_internals( p_tmp )->i_object_type == i_type )
1191 i_count += CountChildren( p_tmp, i_type );
1197 static void ListChildren( vlc_list_t *p_list, vlc_object_t *p_this, int i_type )
1199 vlc_object_t *p_tmp;
1202 for( i = 0; i < vlc_internals( p_this )->i_children; i++ )
1204 p_tmp = vlc_internals( p_this )->pp_children[i];
1206 if( vlc_internals( p_tmp )->i_object_type == i_type )
1207 ListReplace( p_list, p_tmp, p_list->i_count++ );
1209 ListChildren( p_list, p_tmp, i_type );