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"
59 #if defined (HAVE_SYS_EVENTFD_H)
60 # include <sys/eventfd.h>
64 /*****************************************************************************
66 *****************************************************************************/
67 static int DumpCommand( vlc_object_t *, char const *,
68 vlc_value_t, vlc_value_t, void * );
70 static vlc_object_t * FindObject ( vlc_object_t *, int, int );
71 static vlc_object_t * FindObjectName( vlc_object_t *, const char *, int );
72 static void PrintObject ( vlc_object_t *, const char * );
73 static void DumpStructure ( vlc_object_t *, int, char * );
75 static vlc_list_t * NewList ( int );
76 static void ListReplace ( vlc_list_t *, vlc_object_t *, int );
77 /*static void ListAppend ( vlc_list_t *, vlc_object_t * );*/
78 static int CountChildren ( vlc_object_t *, int );
79 static void ListChildren ( vlc_list_t *, vlc_object_t *, int );
81 static void vlc_object_destroy( vlc_object_t *p_this );
82 static void vlc_object_detach_unlocked (vlc_object_t *p_this);
84 /*****************************************************************************
85 * Local structure lock
86 *****************************************************************************/
87 static void libvlc_lock (libvlc_int_t *p_libvlc)
89 vlc_mutex_lock (&(libvlc_priv (p_libvlc)->structure_lock));
92 static void libvlc_unlock (libvlc_int_t *p_libvlc)
94 vlc_mutex_unlock (&(libvlc_priv (p_libvlc)->structure_lock));
97 void *__vlc_custom_create( vlc_object_t *p_this, size_t i_size,
98 int i_type, const char *psz_type )
101 vlc_object_internals_t *p_priv;
104 * VLC objects are laid out as follow:
105 * - first the LibVLC-private per-object data,
106 * - then VLC_COMMON members from vlc_object_t,
107 * - finally, the type-specific data (if any).
109 * This function initializes the LibVLC and common data,
110 * and zeroes the rest.
112 p_priv = calloc( 1, sizeof( *p_priv ) + i_size );
116 assert (i_size >= sizeof (vlc_object_t));
117 p_new = (vlc_object_t *)(p_priv + 1);
119 p_priv->i_object_type = i_type;
120 p_new->psz_object_type = psz_type;
121 p_priv->psz_name = NULL;
123 p_new->b_die = false;
124 p_new->b_error = false;
125 p_new->b_force = false;
127 p_new->psz_header = NULL;
130 p_new->i_flags = p_this->i_flags
131 & (OBJECT_FLAGS_NODBG|OBJECT_FLAGS_QUIET|OBJECT_FLAGS_NOINTERACT);
133 p_priv->var_root = NULL;
137 libvlc_int_t *self = (libvlc_int_t*)p_new;
138 p_new->p_libvlc = self;
139 vlc_mutex_init (&(libvlc_priv (self)->structure_lock));
143 p_new->p_libvlc = p_this->p_libvlc;
145 vlc_spin_init( &p_priv->ref_spin );
146 p_priv->i_refcount = 1;
147 p_priv->pf_destructor = NULL;
148 p_priv->b_thread = false;
149 p_new->p_parent = NULL;
150 p_priv->pp_children = NULL;
151 p_priv->i_children = 0;
153 /* Initialize mutexes and condvars */
154 vlc_mutex_init( &p_priv->var_lock );
155 vlc_cond_init( &p_priv->var_wait );
156 p_priv->pipes[0] = p_priv->pipes[1] = -1;
158 if (p_new == VLC_OBJECT(p_new->p_libvlc))
159 { /* TODO: should be in src/libvlc.c */
160 int canc = vlc_savecancel ();
161 var_Create( p_new, "tree", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
162 var_AddCallback( p_new, "tree", DumpCommand, NULL );
163 var_Create( p_new, "vars", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
164 var_AddCallback( p_new, "vars", DumpCommand, NULL );
165 vlc_restorecancel (canc);
173 * Allocates and initializes a vlc object.
175 * @param i_type known object type (all of them are negative integer values),
176 * or object byte size (always positive).
178 * @return the new object, or NULL on error.
180 void * __vlc_object_create( vlc_object_t *p_this, int i_type )
182 const char * psz_type;
187 case VLC_OBJECT_DECODER:
188 i_size = sizeof(decoder_t);
189 psz_type = "decoder";
191 case VLC_OBJECT_AOUT:
192 i_size = sizeof(aout_instance_t);
193 psz_type = "audio output";
196 assert( i_type > 0 ); /* unknown type?! */
198 i_type = VLC_OBJECT_GENERIC;
199 psz_type = "generic";
203 return vlc_custom_create( p_this, i_size, i_type, psz_type );
208 ****************************************************************************
209 * Set the destructor of a vlc object
211 * This function sets the destructor of the vlc object. It will be called
212 * when the object is destroyed when the its refcount reaches 0.
213 * (It is called by the internal function vlc_object_destroy())
214 *****************************************************************************/
215 void __vlc_object_set_destructor( vlc_object_t *p_this,
216 vlc_destructor_t pf_destructor )
218 vlc_object_internals_t *p_priv = vlc_internals(p_this );
220 vlc_spin_lock( &p_priv->ref_spin );
221 p_priv->pf_destructor = pf_destructor;
222 vlc_spin_unlock( &p_priv->ref_spin );
225 static vlc_mutex_t name_lock = VLC_STATIC_MUTEX;
227 #undef vlc_object_set_name
228 int vlc_object_set_name(vlc_object_t *obj, const char *name)
230 vlc_object_internals_t *priv = vlc_internals(obj);
231 char *newname = name ? strdup (name) : NULL;
234 vlc_mutex_lock (&name_lock);
235 oldname = priv->psz_name;
236 priv->psz_name = newname;
237 vlc_mutex_unlock (&name_lock);
240 return (priv->psz_name || !name) ? VLC_SUCCESS : VLC_ENOMEM;
243 #undef vlc_object_get_name
244 char *vlc_object_get_name(const vlc_object_t *obj)
246 vlc_object_internals_t *priv = vlc_internals(obj);
249 vlc_mutex_lock (&name_lock);
250 name = priv->psz_name ? strdup (priv->psz_name) : NULL;
251 vlc_mutex_unlock (&name_lock);
257 ****************************************************************************
258 * Destroy a vlc object (Internal)
260 * This function destroys an object that has been previously allocated with
261 * vlc_object_create. The object's refcount must be zero and it must not be
262 * attached to other objects in any way.
264 * This function must be called with cancellation disabled (currently).
265 *****************************************************************************/
266 static void vlc_object_destroy( vlc_object_t *p_this )
268 vlc_object_internals_t *p_priv = vlc_internals( p_this );
270 /* Objects are always detached beforehand */
271 assert( !p_this->p_parent );
273 /* Send a kill to the object's thread if applicable */
274 vlc_object_kill( p_this );
276 /* Call the custom "subclass" destructor */
277 if( p_priv->pf_destructor )
278 p_priv->pf_destructor( p_this );
280 /* Any thread must have been cleaned up at this point. */
281 assert( !p_priv->b_thread );
283 /* Destroy the associated variables. */
284 var_DestroyAll( p_this );
286 vlc_cond_destroy( &p_priv->var_wait );
287 vlc_mutex_destroy( &p_priv->var_lock );
289 free( p_this->psz_header );
291 free( p_priv->psz_name );
293 vlc_spin_destroy( &p_priv->ref_spin );
294 if( p_priv->pipes[1] != -1 && p_priv->pipes[1] != p_priv->pipes[0] )
295 close( p_priv->pipes[1] );
296 if( p_priv->pipes[0] != -1 )
297 close( p_priv->pipes[0] );
298 if( VLC_OBJECT(p_this->p_libvlc) == p_this )
299 vlc_mutex_destroy (&(libvlc_priv ((libvlc_int_t *)p_this)->structure_lock));
306 # include <winsock2.h>
307 # include <ws2tcpip.h>
310 * select()-able pipes emulated using Winsock
312 static int pipe (int fd[2])
315 int addrlen = sizeof (addr);
317 SOCKET l = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP), a,
318 c = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
319 if ((l == INVALID_SOCKET) || (c == INVALID_SOCKET))
322 memset (&addr, 0, sizeof (addr));
323 addr.sin_family = AF_INET;
324 addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
325 if (bind (l, (PSOCKADDR)&addr, sizeof (addr))
326 || getsockname (l, (PSOCKADDR)&addr, &addrlen)
328 || connect (c, (PSOCKADDR)&addr, addrlen))
331 a = accept (l, NULL, NULL);
332 if (a == INVALID_SOCKET)
343 if (l != INVALID_SOCKET)
345 if (c != INVALID_SOCKET)
351 #define read( a, b, c ) recv (a, b, c, 0)
353 #define write( a, b, c ) send (a, b, c, 0)
355 #define close( a ) closesocket (a)
358 static vlc_mutex_t pipe_lock = VLC_STATIC_MUTEX;
361 * Returns the readable end of a pipe that becomes readable once termination
362 * of the object is requested (vlc_object_kill()).
363 * This can be used to wake-up out of a select() or poll() event loop, such
364 * typically when doing network I/O.
366 * Note that the pipe will remain the same for the lifetime of the object.
367 * DO NOT read the pipe nor close it yourself. Ever.
369 * @param obj object that would be "killed"
370 * @return a readable pipe descriptor, or -1 on error.
372 int vlc_object_waitpipe( vlc_object_t *obj )
374 vlc_object_internals_t *internals = vlc_internals( obj );
376 vlc_mutex_lock (&pipe_lock);
377 if (internals->pipes[0] == -1)
379 /* This can only ever happen if someone killed us without locking: */
380 assert (internals->pipes[1] == -1);
382 #if defined (HAVE_SYS_EVENTFD_H)
383 internals->pipes[0] = internals->pipes[1] = eventfd (0, 0);
384 if (internals->pipes[0] == -1)
387 if (pipe (internals->pipes))
388 internals->pipes[0] = internals->pipes[1] = -1;
391 if (internals->pipes[0] != -1 && obj->b_die)
392 { /* Race condition: vlc_object_kill() already invoked! */
393 msg_Dbg (obj, "waitpipe: object already dying");
394 write (internals->pipes[1], &(uint64_t){ 1 }, sizeof (uint64_t));
397 vlc_mutex_unlock (&pipe_lock);
398 return internals->pipes[0];
403 * Requests termination of an object, cancels the object thread, and make the
404 * object wait pipe (if it exists) readable. Not a cancellation point.
406 void __vlc_object_kill( vlc_object_t *p_this )
408 vlc_object_internals_t *priv = vlc_internals( p_this );
411 vlc_thread_cancel( p_this );
412 vlc_mutex_lock( &pipe_lock );
416 p_this->b_die = true;
419 /* This also serves as a memory barrier toward vlc_object_alive(): */
420 vlc_mutex_unlock( &pipe_lock );
424 int canc = vlc_savecancel ();
426 /* write _after_ setting b_die, so vlc_object_alive() returns false */
427 write (fd, &(uint64_t){ 1 }, sizeof (uint64_t));
428 msg_Dbg (p_this, "waitpipe: object killed");
429 vlc_restorecancel (canc);
434 /*****************************************************************************
435 * find a typed object and increment its refcount
436 *****************************************************************************
437 * This function recursively looks for a given object type. i_mode can be one
438 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
439 *****************************************************************************/
440 void * __vlc_object_find( vlc_object_t *p_this, int i_type, int i_mode )
442 vlc_object_t *p_found;
444 /* If we are of the requested type ourselves, don't look further */
445 if( vlc_internals (p_this)->i_object_type == i_type )
447 vlc_object_hold( p_this );
451 /* Otherwise, recursively look for the object */
452 if (i_mode == FIND_ANYWHERE)
453 return vlc_object_find (p_this->p_libvlc, i_type, FIND_CHILD);
455 libvlc_lock (p_this->p_libvlc);
456 p_found = FindObject( p_this, i_type, i_mode );
457 libvlc_unlock (p_this->p_libvlc);
462 static int objnamecmp(const vlc_object_t *obj, const char *name)
464 char *objname = vlc_object_get_name(obj);
468 int ret = strcmp (objname, name);
473 #undef vlc_object_find_name
475 * Finds a named object and increment its reference count.
476 * Beware that objects found in this manner can be "owned" by another thread,
477 * be of _any_ type, and be attached to any module (if any). With such an
478 * object reference, you can set or get object variables, emit log messages,
479 * and read write-once object parameters (psz_object_type, etc).
480 * You CANNOT cast the object to a more specific object type, and you
481 * definitely cannot invoke object type-specific callbacks with this.
483 * @param p_this object to search from
484 * @param psz_name name of the object to search for
485 * @param i_mode search direction: FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
487 * @return a matching object (must be released by the caller),
490 vlc_object_t *vlc_object_find_name( vlc_object_t *p_this,
491 const char *psz_name, int i_mode )
493 vlc_object_t *p_found;
495 /* Reading psz_object_name from a separate inhibits thread-safety.
496 * Use a libvlc address variable instead for that sort of things! */
497 msg_Warn( p_this, "%s(%s) is not safe!", __func__, psz_name );
498 /* If have the requested name ourselves, don't look further */
499 if( !objnamecmp(p_this, psz_name) )
501 vlc_object_hold( p_this );
505 libvlc_lock (p_this->p_libvlc);
507 /* Otherwise, recursively look for the object */
508 if( i_mode == FIND_ANYWHERE )
510 vlc_object_t *p_root = p_this;
513 while( p_root->p_parent != NULL &&
514 p_root != VLC_OBJECT( p_this->p_libvlc ) )
516 p_root = p_root->p_parent;
519 p_found = FindObjectName( p_root, psz_name, FIND_CHILD );
520 if( p_found == NULL && p_root != VLC_OBJECT( p_this->p_libvlc ) )
522 p_found = FindObjectName( VLC_OBJECT( p_this->p_libvlc ),
523 psz_name, FIND_CHILD );
528 p_found = FindObjectName( p_this, psz_name, i_mode );
531 libvlc_unlock (p_this->p_libvlc);
536 * Increment an object reference counter.
538 void * __vlc_object_hold( vlc_object_t *p_this )
540 vlc_object_internals_t *internals = vlc_internals( p_this );
542 vlc_spin_lock( &internals->ref_spin );
543 /* Avoid obvious freed object uses */
544 assert( internals->i_refcount > 0 );
545 /* Increment the counter */
546 internals->i_refcount++;
547 vlc_spin_unlock( &internals->ref_spin );
551 /*****************************************************************************
552 * Decrement an object refcount
553 * And destroy the object if its refcount reach zero.
554 *****************************************************************************/
555 void __vlc_object_release( vlc_object_t *p_this )
557 vlc_object_internals_t *internals = vlc_internals( p_this );
558 vlc_object_t *parent = NULL;
559 bool b_should_destroy;
561 vlc_spin_lock( &internals->ref_spin );
562 assert( internals->i_refcount > 0 );
564 if( internals->i_refcount > 1 )
567 /* There are still other references to the object */
568 internals->i_refcount--;
569 vlc_spin_unlock( &internals->ref_spin );
572 vlc_spin_unlock( &internals->ref_spin );
575 /* Remember that we cannot hold the spin while waiting on the mutex */
576 libvlc_lock (p_this->p_libvlc);
577 /* Take the spin again. Note that another thread may have held the
578 * object in the (very short) mean time. */
579 vlc_spin_lock( &internals->ref_spin );
580 b_should_destroy = --internals->i_refcount == 0;
581 vlc_spin_unlock( &internals->ref_spin );
583 if( b_should_destroy )
585 parent = p_this->p_parent;
587 /* Detach from parent to protect against FIND_CHILDREN */
588 vlc_object_detach_unlocked (p_this);
590 /* We have no children */
591 assert (internals->i_children == 0);
593 libvlc_unlock (p_this->p_libvlc);
595 if( b_should_destroy )
599 canc = vlc_savecancel ();
600 vlc_object_destroy( p_this );
601 vlc_restorecancel (canc);
603 vlc_object_release (parent);
608 ****************************************************************************
609 * attach object to a parent object
610 *****************************************************************************
611 * This function sets p_this as a child of p_parent, and p_parent as a parent
612 * of p_this. This link can be undone using vlc_object_detach.
613 *****************************************************************************/
614 void __vlc_object_attach( vlc_object_t *p_this, vlc_object_t *p_parent )
616 if( !p_this ) return;
618 vlc_object_hold (p_parent);
619 libvlc_lock (p_this->p_libvlc);
621 /* Attach the parent to its child */
622 assert (!p_this->p_parent);
623 p_this->p_parent = p_parent;
625 /* Attach the child to its parent */
626 vlc_object_internals_t *priv = vlc_internals( p_parent );
627 INSERT_ELEM( priv->pp_children, priv->i_children, priv->i_children,
629 libvlc_unlock (p_this->p_libvlc);
633 static void vlc_object_detach_unlocked (vlc_object_t *p_this)
635 if (p_this->p_parent == NULL)
638 vlc_object_internals_t *priv = vlc_internals( p_this->p_parent );
642 /* Remove p_this's parent */
643 p_this->p_parent = NULL;
645 /* Remove all of p_parent's children which are p_this */
646 for( i_index = priv->i_children ; i_index-- ; )
648 if( priv->pp_children[i_index] == p_this )
651 for( i = i_index ; i < priv->i_children ; i++ )
652 priv->pp_children[i] = priv->pp_children[i+1];
656 if( priv->i_children )
658 vlc_object_t **pp_children = (vlc_object_t **)
659 realloc( priv->pp_children,
660 priv->i_children * sizeof(vlc_object_t *) );
662 priv->pp_children = pp_children;
666 /* Special case - don't realloc() to zero to avoid leaking */
667 free( priv->pp_children );
668 priv->pp_children = NULL;
674 ****************************************************************************
675 * detach object from its parent
676 *****************************************************************************
677 * This function removes all links between an object and its parent.
678 *****************************************************************************/
679 void __vlc_object_detach( vlc_object_t *p_this )
681 vlc_object_t *p_parent;
682 if( !p_this ) return;
684 libvlc_lock (p_this->p_libvlc);
685 p_parent = p_this->p_parent;
687 vlc_object_detach_unlocked( p_this );
688 libvlc_unlock (p_this->p_libvlc);
691 vlc_object_release (p_parent);
696 ****************************************************************************
697 * find a list typed objects and increment their refcount
698 *****************************************************************************
699 * This function recursively looks for a given object type. i_mode can be one
700 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
701 *****************************************************************************/
702 vlc_list_t * vlc_list_find( vlc_object_t *p_this, int i_type, int i_mode )
707 /* Look for the objects */
711 return vlc_list_find (VLC_OBJECT(p_this->p_libvlc), i_type, FIND_CHILD);
714 libvlc_lock (p_this->p_libvlc);
715 i_count = CountChildren( p_this, i_type );
716 p_list = NewList( i_count );
718 /* Check allocation was successful */
719 if( p_list->i_count != i_count )
721 libvlc_unlock (p_this->p_libvlc);
727 ListChildren( p_list, p_this, i_type );
728 libvlc_unlock (p_this->p_libvlc);
732 msg_Err( p_this, "unimplemented!" );
733 p_list = NewList( 0 );
741 * Gets the list of children of an objects, and increment their reference
743 * @return a list (possibly empty) or NULL in case of error.
745 vlc_list_t *__vlc_list_children( vlc_object_t *obj )
748 vlc_object_internals_t *priv = vlc_internals( obj );
750 libvlc_lock (obj->p_libvlc);
751 l = NewList( priv->i_children );
752 for (int i = 0; i < l->i_count; i++)
754 vlc_object_hold( priv->pp_children[i] );
755 l->p_values[i].p_object = priv->pp_children[i];
757 libvlc_unlock (obj->p_libvlc);
761 static void DumpVariable (const void *data, const VISIT which, const int depth)
763 if (which != postorder && which != leaf)
767 const variable_t *p_var = *(const variable_t **)data;
768 const char *psz_type = "unknown";
770 switch( p_var->i_type & VLC_VAR_TYPE )
772 #define MYCASE( type, nice ) \
773 case VLC_VAR_ ## type: \
776 MYCASE( VOID, "void" );
777 MYCASE( BOOL, "bool" );
778 MYCASE( INTEGER, "integer" );
779 MYCASE( HOTKEY, "hotkey" );
780 MYCASE( STRING, "string" );
781 MYCASE( MODULE, "module" );
782 MYCASE( FILE, "file" );
783 MYCASE( DIRECTORY, "directory" );
784 MYCASE( VARIABLE, "variable" );
785 MYCASE( FLOAT, "float" );
786 MYCASE( TIME, "time" );
787 MYCASE( ADDRESS, "address" );
788 MYCASE( MUTEX, "mutex" );
789 MYCASE( LIST, "list" );
792 printf( " *-o \"%s\" (%s", p_var->psz_name, psz_type );
793 if( p_var->psz_text )
794 printf( ", %s", p_var->psz_text );
795 fputc( ')', stdout );
796 if( p_var->i_type & VLC_VAR_HASCHOICE )
797 fputs( ", has choices", stdout );
798 if( p_var->i_type & VLC_VAR_ISCOMMAND )
799 fputs( ", command", stdout );
800 if( p_var->i_entries )
801 printf( ", %d callbacks", p_var->i_entries );
802 switch( p_var->i_type & VLC_VAR_CLASS )
808 printf( ": %s", p_var->val.b_bool ? "true" : "false" );
810 case VLC_VAR_INTEGER:
811 printf( ": %d", p_var->val.i_int );
814 printf( ": \"%s\"", p_var->val.psz_string );
817 printf( ": %f", p_var->val.f_float );
820 printf( ": %"PRIi64, (int64_t)p_var->val.i_time );
822 case VLC_VAR_ADDRESS:
823 printf( ": %p", p_var->val.p_address );
826 fputs( ": TODO", stdout );
829 fputc( '\n', stdout );
832 /*****************************************************************************
833 * DumpCommand: print the current vlc structure
834 *****************************************************************************
835 * This function prints either an ASCII tree showing the connections between
836 * vlc objects, and additional information such as their refcount, thread ID,
837 * etc. (command "tree"), or the same data as a simple list (command "list").
838 *****************************************************************************/
839 static int DumpCommand( vlc_object_t *p_this, char const *psz_cmd,
840 vlc_value_t oldval, vlc_value_t newval, void *p_data )
842 (void)oldval; (void)p_data;
843 vlc_object_t *p_object = NULL;
845 if( *newval.psz_string )
847 /* try using the object's name to find it */
848 p_object = vlc_object_find_name( p_this, newval.psz_string,
856 libvlc_lock (p_this->p_libvlc);
857 if( *psz_cmd == 't' )
859 char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
862 p_object = VLC_OBJECT(p_this->p_libvlc);
865 DumpStructure( p_object, 0, psz_foo );
867 else if( *psz_cmd == 'v' )
870 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
872 PrintObject( p_object, "" );
873 vlc_mutex_lock( &vlc_internals( p_object )->var_lock );
874 if( vlc_internals( p_object )->var_root == NULL )
875 puts( " `-o No variables" );
877 twalk( vlc_internals( p_object )->var_root, DumpVariable );
878 vlc_mutex_unlock( &vlc_internals( p_object )->var_lock );
880 libvlc_unlock (p_this->p_libvlc);
882 if( *newval.psz_string )
884 vlc_object_release( p_object );
889 /*****************************************************************************
890 * vlc_list_release: free a list previously allocated by vlc_list_find
891 *****************************************************************************
892 * This function decreases the refcount of all objects in the list and
894 *****************************************************************************/
895 void vlc_list_release( vlc_list_t *p_list )
899 for( i_index = 0; i_index < p_list->i_count; i_index++ )
901 vlc_object_release( p_list->p_values[i_index].p_object );
904 free( p_list->p_values );
908 /* Following functions are local */
910 static vlc_object_t * FindObject( vlc_object_t *p_this, int i_type, int i_mode )
918 p_tmp = p_this->p_parent;
921 if( vlc_internals( p_tmp )->i_object_type == i_type )
923 vlc_object_hold( p_tmp );
928 return FindObject( p_tmp, i_type, i_mode );
934 for( i = vlc_internals( p_this )->i_children; i--; )
936 p_tmp = vlc_internals( p_this )->pp_children[i];
937 if( vlc_internals( p_tmp )->i_object_type == i_type )
939 vlc_object_hold( p_tmp );
942 else if( vlc_internals( p_tmp )->i_children )
944 p_tmp = FindObject( p_tmp, i_type, i_mode );
954 /* Handled in vlc_object_find */
961 static vlc_object_t * FindObjectName( vlc_object_t *p_this,
962 const char *psz_name,
971 p_tmp = p_this->p_parent;
974 if( !objnamecmp(p_tmp, psz_name) )
976 vlc_object_hold( p_tmp );
981 return FindObjectName( p_tmp, psz_name, i_mode );
987 for( i = vlc_internals( p_this )->i_children; i--; )
989 p_tmp = vlc_internals( p_this )->pp_children[i];
990 if( !objnamecmp(p_tmp, psz_name ) )
992 vlc_object_hold( p_tmp );
995 else if( vlc_internals( p_tmp )->i_children )
997 p_tmp = FindObjectName( p_tmp, psz_name, i_mode );
1007 /* Handled in vlc_object_find */
1015 static void PrintObject( vlc_object_t *p_this, const char *psz_prefix )
1017 char psz_children[20], psz_refcount[20], psz_thread[30], psz_name[50],
1020 int canc = vlc_savecancel ();
1021 memset( &psz_name, 0, sizeof(psz_name) );
1022 char *name = vlc_object_get_name(p_this);
1025 snprintf( psz_name, 49, " \"%s\"", name );
1028 psz_name[48] = '\"';
1031 psz_children[0] = '\0';
1032 switch( vlc_internals( p_this )->i_children )
1037 strcpy( psz_children, ", 1 child" );
1040 snprintf( psz_children, 19, ", %i children",
1041 vlc_internals( p_this )->i_children );
1045 psz_refcount[0] = '\0';
1046 if( vlc_internals( p_this )->i_refcount > 0 )
1047 snprintf( psz_refcount, 19, ", refcount %u",
1048 vlc_internals( p_this )->i_refcount );
1050 psz_thread[0] = '\0';
1051 if( vlc_internals( p_this )->b_thread )
1052 snprintf( psz_thread, 29, " (thread %lu)",
1053 (unsigned long)vlc_internals( p_this )->thread_id );
1055 psz_parent[0] = '\0';
1056 if( p_this->p_parent )
1057 snprintf( psz_parent, 19, ", parent %p", p_this->p_parent );
1059 printf( " %so %p %s%s%s%s%s%s\n", psz_prefix,
1060 p_this, p_this->psz_object_type,
1061 psz_name, psz_thread, psz_refcount, psz_children,
1063 vlc_restorecancel (canc);
1066 static void DumpStructure( vlc_object_t *p_this, int i_level, char *psz_foo )
1069 char i_back = psz_foo[i_level];
1070 psz_foo[i_level] = '\0';
1072 PrintObject( p_this, psz_foo );
1074 psz_foo[i_level] = i_back;
1076 if( i_level / 2 >= MAX_DUMPSTRUCTURE_DEPTH )
1078 msg_Warn( p_this, "structure tree is too deep" );
1082 for( i = 0 ; i < vlc_internals( p_this )->i_children ; i++ )
1086 psz_foo[i_level-1] = ' ';
1088 if( psz_foo[i_level-2] == '`' )
1090 psz_foo[i_level-2] = ' ';
1094 if( i == vlc_internals( p_this )->i_children - 1 )
1096 psz_foo[i_level] = '`';
1100 psz_foo[i_level] = '|';
1103 psz_foo[i_level+1] = '-';
1104 psz_foo[i_level+2] = '\0';
1106 DumpStructure( vlc_internals( p_this )->pp_children[i], i_level + 2,
1111 static vlc_list_t * NewList( int i_count )
1113 vlc_list_t * p_list = malloc( sizeof( vlc_list_t ) );
1114 if( p_list == NULL )
1117 p_list->i_count = i_count;
1121 p_list->p_values = NULL;
1125 p_list->p_values = malloc( i_count * sizeof( vlc_value_t ) );
1126 if( p_list->p_values == NULL )
1128 p_list->i_count = 0;
1135 static void ListReplace( vlc_list_t *p_list, vlc_object_t *p_object,
1138 if( p_list == NULL || i_index >= p_list->i_count )
1143 vlc_object_hold( p_object );
1145 p_list->p_values[i_index].p_object = p_object;
1150 /*static void ListAppend( vlc_list_t *p_list, vlc_object_t *p_object )
1152 if( p_list == NULL )
1157 p_list->p_values = realloc_or_free( p_list->p_values,
1158 (p_list->i_count + 1) * sizeof( vlc_value_t ) );
1159 if( p_list->p_values == NULL )
1161 p_list->i_count = 0;
1165 vlc_object_hold( p_object );
1167 p_list->p_values[p_list->i_count].p_object = p_object;
1173 static int CountChildren( vlc_object_t *p_this, int i_type )
1175 vlc_object_t *p_tmp;
1178 for( i = 0; i < vlc_internals( p_this )->i_children; i++ )
1180 p_tmp = vlc_internals( p_this )->pp_children[i];
1182 if( vlc_internals( p_tmp )->i_object_type == i_type )
1186 i_count += CountChildren( p_tmp, i_type );
1192 static void ListChildren( vlc_list_t *p_list, vlc_object_t *p_this, int i_type )
1194 vlc_object_t *p_tmp;
1197 for( i = 0; i < vlc_internals( p_this )->i_children; i++ )
1199 p_tmp = vlc_internals( p_this )->pp_children[i];
1201 if( vlc_internals( p_tmp )->i_object_type == i_type )
1202 ListReplace( p_list, p_tmp, p_list->i_count++ );
1204 ListChildren( p_list, p_tmp, i_type );