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"
53 # include <winsock2.h>
54 # include <ws2tcpip.h>
56 # define read( a, b, c ) recv (a, b, c, 0)
58 # define write( a, b, c ) send (a, b, c, 0)
60 # define close( a ) closesocket (a)
67 #if defined (HAVE_SYS_EVENTFD_H)
68 # include <sys/eventfd.h>
72 /*****************************************************************************
74 *****************************************************************************/
75 static int DumpCommand( vlc_object_t *, char const *,
76 vlc_value_t, vlc_value_t, void * );
78 static vlc_object_t * FindObject ( vlc_object_t *, int, int );
79 static vlc_object_t * FindObjectName( vlc_object_t *, const char *, int );
80 static void PrintObject ( vlc_object_t *, const char * );
81 static void DumpStructure ( vlc_object_t *, int, char * );
83 static vlc_list_t * NewList ( int );
84 static void ListReplace ( vlc_list_t *, vlc_object_t *, int );
85 /*static void ListAppend ( vlc_list_t *, vlc_object_t * );*/
86 static int CountChildren ( vlc_object_t *, int );
87 static void ListChildren ( vlc_list_t *, vlc_object_t *, int );
89 static void vlc_object_destroy( vlc_object_t *p_this );
90 static void vlc_object_detach_unlocked (vlc_object_t *p_this);
92 /*****************************************************************************
93 * Local structure lock
94 *****************************************************************************/
95 static void libvlc_lock (libvlc_int_t *p_libvlc)
97 vlc_mutex_lock (&(libvlc_priv (p_libvlc)->structure_lock));
100 static void libvlc_unlock (libvlc_int_t *p_libvlc)
102 vlc_mutex_unlock (&(libvlc_priv (p_libvlc)->structure_lock));
105 void *__vlc_custom_create( vlc_object_t *p_this, size_t i_size,
106 int i_type, const char *psz_type )
109 vlc_object_internals_t *p_priv;
112 * VLC objects are laid out as follow:
113 * - first the LibVLC-private per-object data,
114 * - then VLC_COMMON members from vlc_object_t,
115 * - finally, the type-specific data (if any).
117 * This function initializes the LibVLC and common data,
118 * and zeroes the rest.
120 p_priv = calloc( 1, sizeof( *p_priv ) + i_size );
124 assert (i_size >= sizeof (vlc_object_t));
125 p_new = (vlc_object_t *)(p_priv + 1);
127 p_priv->i_object_type = i_type;
128 p_new->psz_object_type = psz_type;
129 p_priv->psz_name = NULL;
131 p_new->b_die = false;
132 p_new->b_error = false;
133 p_new->b_force = false;
135 p_new->psz_header = NULL;
138 p_new->i_flags = p_this->i_flags
139 & (OBJECT_FLAGS_NODBG|OBJECT_FLAGS_QUIET|OBJECT_FLAGS_NOINTERACT);
141 p_priv->var_root = NULL;
145 libvlc_int_t *self = (libvlc_int_t*)p_new;
146 p_new->p_libvlc = self;
147 vlc_mutex_init (&(libvlc_priv (self)->structure_lock));
151 p_new->p_libvlc = p_this->p_libvlc;
153 vlc_spin_init( &p_priv->ref_spin );
154 p_priv->i_refcount = 1;
155 p_priv->pf_destructor = NULL;
156 p_priv->b_thread = false;
157 p_new->p_parent = NULL;
158 p_priv->pp_children = NULL;
159 p_priv->i_children = 0;
161 /* Initialize mutexes and condvars */
162 vlc_mutex_init( &p_priv->var_lock );
163 vlc_cond_init( &p_priv->var_wait );
164 p_priv->pipes[0] = p_priv->pipes[1] = -1;
166 if (p_new == VLC_OBJECT(p_new->p_libvlc))
167 { /* TODO: should be in src/libvlc.c */
168 int canc = vlc_savecancel ();
169 var_Create( p_new, "tree", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
170 var_AddCallback( p_new, "tree", DumpCommand, NULL );
171 var_Create( p_new, "vars", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
172 var_AddCallback( p_new, "vars", DumpCommand, NULL );
173 vlc_restorecancel (canc);
181 * Allocates and initializes a vlc object.
183 * @param i_type known object type (all of them are negative integer values),
184 * or object byte size (always positive).
186 * @return the new object, or NULL on error.
188 void * __vlc_object_create( vlc_object_t *p_this, int i_type )
190 const char * psz_type;
195 case VLC_OBJECT_DECODER:
196 i_size = sizeof(decoder_t);
197 psz_type = "decoder";
199 case VLC_OBJECT_AOUT:
200 i_size = sizeof(aout_instance_t);
201 psz_type = "audio output";
204 assert( i_type > 0 ); /* unknown type?! */
206 i_type = VLC_OBJECT_GENERIC;
207 psz_type = "generic";
211 return vlc_custom_create( p_this, i_size, i_type, psz_type );
216 ****************************************************************************
217 * Set the destructor of a vlc object
219 * This function sets the destructor of the vlc object. It will be called
220 * when the object is destroyed when the its refcount reaches 0.
221 * (It is called by the internal function vlc_object_destroy())
222 *****************************************************************************/
223 void __vlc_object_set_destructor( vlc_object_t *p_this,
224 vlc_destructor_t pf_destructor )
226 vlc_object_internals_t *p_priv = vlc_internals(p_this );
228 vlc_spin_lock( &p_priv->ref_spin );
229 p_priv->pf_destructor = pf_destructor;
230 vlc_spin_unlock( &p_priv->ref_spin );
233 static vlc_mutex_t name_lock = VLC_STATIC_MUTEX;
235 #undef vlc_object_set_name
236 int vlc_object_set_name(vlc_object_t *obj, const char *name)
238 vlc_object_internals_t *priv = vlc_internals(obj);
239 char *newname = name ? strdup (name) : NULL;
242 vlc_mutex_lock (&name_lock);
243 oldname = priv->psz_name;
244 priv->psz_name = newname;
245 vlc_mutex_unlock (&name_lock);
248 return (priv->psz_name || !name) ? VLC_SUCCESS : VLC_ENOMEM;
251 #undef vlc_object_get_name
252 char *vlc_object_get_name(const vlc_object_t *obj)
254 vlc_object_internals_t *priv = vlc_internals(obj);
257 vlc_mutex_lock (&name_lock);
258 name = priv->psz_name ? strdup (priv->psz_name) : NULL;
259 vlc_mutex_unlock (&name_lock);
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. */
292 var_DestroyAll( p_this );
294 vlc_cond_destroy( &p_priv->var_wait );
295 vlc_mutex_destroy( &p_priv->var_lock );
297 free( p_this->psz_header );
299 free( p_priv->psz_name );
301 vlc_spin_destroy( &p_priv->ref_spin );
302 if( p_priv->pipes[1] != -1 && p_priv->pipes[1] != p_priv->pipes[0] )
303 close( p_priv->pipes[1] );
304 if( p_priv->pipes[0] != -1 )
305 close( p_priv->pipes[0] );
306 if( VLC_OBJECT(p_this->p_libvlc) == p_this )
307 vlc_mutex_destroy (&(libvlc_priv ((libvlc_int_t *)p_this)->structure_lock));
315 * select()-able pipes emulated using Winsock
317 static int pipe (int fd[2])
320 int addrlen = sizeof (addr);
322 SOCKET l = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP), a,
323 c = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
324 if ((l == INVALID_SOCKET) || (c == INVALID_SOCKET))
327 memset (&addr, 0, sizeof (addr));
328 addr.sin_family = AF_INET;
329 addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
330 if (bind (l, (PSOCKADDR)&addr, sizeof (addr))
331 || getsockname (l, (PSOCKADDR)&addr, &addrlen)
333 || connect (c, (PSOCKADDR)&addr, addrlen))
336 a = accept (l, NULL, NULL);
337 if (a == INVALID_SOCKET)
348 if (l != INVALID_SOCKET)
350 if (c != INVALID_SOCKET)
356 static vlc_mutex_t pipe_lock = VLC_STATIC_MUTEX;
359 * Returns the readable end of a pipe that becomes readable once termination
360 * of the object is requested (vlc_object_kill()).
361 * This can be used to wake-up out of a select() or poll() event loop, such
362 * typically when doing network I/O.
364 * Note that the pipe will remain the same for the lifetime of the object.
365 * DO NOT read the pipe nor close it yourself. Ever.
367 * @param obj object that would be "killed"
368 * @return a readable pipe descriptor, or -1 on error.
370 int vlc_object_waitpipe( vlc_object_t *obj )
372 vlc_object_internals_t *internals = vlc_internals( obj );
374 vlc_mutex_lock (&pipe_lock);
375 if (internals->pipes[0] == -1)
377 /* This can only ever happen if someone killed us without locking: */
378 assert (internals->pipes[1] == -1);
380 #if defined (HAVE_SYS_EVENTFD_H)
381 internals->pipes[0] = internals->pipes[1] = eventfd (0, 0);
382 if (internals->pipes[0] == -1)
385 if (pipe (internals->pipes))
386 internals->pipes[0] = internals->pipes[1] = -1;
389 if (internals->pipes[0] != -1 && obj->b_die)
390 { /* Race condition: vlc_object_kill() already invoked! */
391 msg_Dbg (obj, "waitpipe: object already dying");
392 write (internals->pipes[1], &(uint64_t){ 1 }, sizeof (uint64_t));
395 vlc_mutex_unlock (&pipe_lock);
396 return internals->pipes[0];
401 * Requests termination of an object, cancels the object thread, and make the
402 * object wait pipe (if it exists) readable. Not a cancellation point.
404 void __vlc_object_kill( vlc_object_t *p_this )
406 vlc_object_internals_t *priv = vlc_internals( p_this );
409 vlc_thread_cancel( p_this );
410 vlc_mutex_lock( &pipe_lock );
414 p_this->b_die = true;
417 /* This also serves as a memory barrier toward vlc_object_alive(): */
418 vlc_mutex_unlock( &pipe_lock );
422 int canc = vlc_savecancel ();
424 /* write _after_ setting b_die, so vlc_object_alive() returns false */
425 write (fd, &(uint64_t){ 1 }, sizeof (uint64_t));
426 msg_Dbg (p_this, "waitpipe: object killed");
427 vlc_restorecancel (canc);
432 /*****************************************************************************
433 * find a typed object and increment its refcount
434 *****************************************************************************
435 * This function recursively looks for a given object type. i_mode can be one
436 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
437 *****************************************************************************/
438 void * __vlc_object_find( vlc_object_t *p_this, int i_type, int i_mode )
440 vlc_object_t *p_found;
442 /* If we are of the requested type ourselves, don't look further */
443 if( vlc_internals (p_this)->i_object_type == i_type )
445 vlc_object_hold( p_this );
449 /* Otherwise, recursively look for the object */
450 if (i_mode == FIND_ANYWHERE)
451 return vlc_object_find (p_this->p_libvlc, i_type, FIND_CHILD);
455 case VLC_OBJECT_VOUT:
456 case VLC_OBJECT_AOUT:
458 case VLC_OBJECT_INPUT:
459 /* input can only be accessed like this from children,
460 * otherwise we could not promise that it is initialized */
461 if (i_mode != FIND_PARENT)
468 libvlc_lock (p_this->p_libvlc);
469 p_found = FindObject( p_this, i_type, i_mode );
470 libvlc_unlock (p_this->p_libvlc);
475 static int objnamecmp(const vlc_object_t *obj, const char *name)
477 char *objname = vlc_object_get_name(obj);
481 int ret = strcmp (objname, name);
486 #undef vlc_object_find_name
488 * Finds a named object and increment its reference count.
489 * Beware that objects found in this manner can be "owned" by another thread,
490 * be of _any_ type, and be attached to any module (if any). With such an
491 * object reference, you can set or get object variables, emit log messages,
492 * and read write-once object parameters (psz_object_type, etc).
493 * You CANNOT cast the object to a more specific object type, and you
494 * definitely cannot invoke object type-specific callbacks with this.
496 * @param p_this object to search from
497 * @param psz_name name of the object to search for
498 * @param i_mode search direction: FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
500 * @return a matching object (must be released by the caller),
503 vlc_object_t *vlc_object_find_name( vlc_object_t *p_this,
504 const char *psz_name, int i_mode )
506 vlc_object_t *p_found;
508 /* Reading psz_object_name from a separate inhibits thread-safety.
509 * Use a libvlc address variable instead for that sort of things! */
510 msg_Warn( p_this, "%s(%s) is not safe!", __func__, psz_name );
511 /* If have the requested name ourselves, don't look further */
512 if( !objnamecmp(p_this, psz_name) )
514 vlc_object_hold( p_this );
518 libvlc_lock (p_this->p_libvlc);
520 /* Otherwise, recursively look for the object */
521 if( i_mode == FIND_ANYWHERE )
523 vlc_object_t *p_root = p_this;
526 while( p_root->p_parent != NULL &&
527 p_root != VLC_OBJECT( p_this->p_libvlc ) )
529 p_root = p_root->p_parent;
532 p_found = FindObjectName( p_root, psz_name, FIND_CHILD );
533 if( p_found == NULL && p_root != VLC_OBJECT( p_this->p_libvlc ) )
535 p_found = FindObjectName( VLC_OBJECT( p_this->p_libvlc ),
536 psz_name, FIND_CHILD );
541 p_found = FindObjectName( p_this, psz_name, i_mode );
544 libvlc_unlock (p_this->p_libvlc);
549 * Increment an object reference counter.
551 void * __vlc_object_hold( vlc_object_t *p_this )
553 vlc_object_internals_t *internals = vlc_internals( p_this );
555 vlc_spin_lock( &internals->ref_spin );
556 /* Avoid obvious freed object uses */
557 assert( internals->i_refcount > 0 );
558 /* Increment the counter */
559 internals->i_refcount++;
560 vlc_spin_unlock( &internals->ref_spin );
564 /*****************************************************************************
565 * Decrement an object refcount
566 * And destroy the object if its refcount reach zero.
567 *****************************************************************************/
568 void __vlc_object_release( vlc_object_t *p_this )
570 vlc_object_internals_t *internals = vlc_internals( p_this );
571 vlc_object_t *parent = NULL;
572 bool b_should_destroy;
574 vlc_spin_lock( &internals->ref_spin );
575 assert( internals->i_refcount > 0 );
577 if( internals->i_refcount > 1 )
580 /* There are still other references to the object */
581 internals->i_refcount--;
582 vlc_spin_unlock( &internals->ref_spin );
585 vlc_spin_unlock( &internals->ref_spin );
588 /* Remember that we cannot hold the spin while waiting on the mutex */
589 libvlc_lock (p_this->p_libvlc);
590 /* Take the spin again. Note that another thread may have held the
591 * object in the (very short) mean time. */
592 vlc_spin_lock( &internals->ref_spin );
593 b_should_destroy = --internals->i_refcount == 0;
594 vlc_spin_unlock( &internals->ref_spin );
596 if( b_should_destroy )
598 parent = p_this->p_parent;
600 /* Detach from parent to protect against FIND_CHILDREN */
601 vlc_object_detach_unlocked (p_this);
603 /* We have no children */
604 assert (internals->i_children == 0);
606 libvlc_unlock (p_this->p_libvlc);
608 if( b_should_destroy )
612 canc = vlc_savecancel ();
613 vlc_object_destroy( p_this );
614 vlc_restorecancel (canc);
616 vlc_object_release (parent);
621 ****************************************************************************
622 * attach object to a parent object
623 *****************************************************************************
624 * This function sets p_this as a child of p_parent, and p_parent as a parent
625 * of p_this. This link can be undone using vlc_object_detach.
626 *****************************************************************************/
627 void __vlc_object_attach( vlc_object_t *p_this, vlc_object_t *p_parent )
629 if( !p_this ) return;
631 vlc_object_hold (p_parent);
632 libvlc_lock (p_this->p_libvlc);
634 /* Attach the parent to its child */
635 assert (!p_this->p_parent);
636 p_this->p_parent = p_parent;
638 /* Attach the child to its parent */
639 vlc_object_internals_t *priv = vlc_internals( p_parent );
640 INSERT_ELEM( priv->pp_children, priv->i_children, priv->i_children,
642 libvlc_unlock (p_this->p_libvlc);
646 static void vlc_object_detach_unlocked (vlc_object_t *p_this)
648 if (p_this->p_parent == NULL)
651 vlc_object_internals_t *priv = vlc_internals( p_this->p_parent );
655 /* Remove p_this's parent */
656 p_this->p_parent = NULL;
658 /* Remove all of p_parent's children which are p_this */
659 for( i_index = priv->i_children ; i_index-- ; )
661 if( priv->pp_children[i_index] == p_this )
664 for( i = i_index ; i < priv->i_children ; i++ )
665 priv->pp_children[i] = priv->pp_children[i+1];
669 if( priv->i_children )
671 vlc_object_t **pp_children = (vlc_object_t **)
672 realloc( priv->pp_children,
673 priv->i_children * sizeof(vlc_object_t *) );
675 priv->pp_children = pp_children;
679 /* Special case - don't realloc() to zero to avoid leaking */
680 free( priv->pp_children );
681 priv->pp_children = NULL;
687 ****************************************************************************
688 * detach object from its parent
689 *****************************************************************************
690 * This function removes all links between an object and its parent.
691 *****************************************************************************/
692 void __vlc_object_detach( vlc_object_t *p_this )
694 vlc_object_t *p_parent;
695 if( !p_this ) return;
697 libvlc_lock (p_this->p_libvlc);
698 p_parent = p_this->p_parent;
700 vlc_object_detach_unlocked( p_this );
701 libvlc_unlock (p_this->p_libvlc);
704 vlc_object_release (p_parent);
709 ****************************************************************************
710 * find a list typed objects and increment their refcount
711 *****************************************************************************
712 * This function recursively looks for a given object type. i_mode can be one
713 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
714 *****************************************************************************/
715 vlc_list_t * vlc_list_find( vlc_object_t *p_this, int i_type, int i_mode )
720 /* Look for the objects */
724 return vlc_list_find (VLC_OBJECT(p_this->p_libvlc), i_type, FIND_CHILD);
727 libvlc_lock (p_this->p_libvlc);
728 i_count = CountChildren( p_this, i_type );
729 p_list = NewList( i_count );
731 /* Check allocation was successful */
732 if( p_list->i_count != i_count )
734 libvlc_unlock (p_this->p_libvlc);
740 ListChildren( p_list, p_this, i_type );
741 libvlc_unlock (p_this->p_libvlc);
745 msg_Err( p_this, "unimplemented!" );
746 p_list = NewList( 0 );
754 * Gets the list of children of an objects, and increment their reference
756 * @return a list (possibly empty) or NULL in case of error.
758 vlc_list_t *__vlc_list_children( vlc_object_t *obj )
761 vlc_object_internals_t *priv = vlc_internals( obj );
763 libvlc_lock (obj->p_libvlc);
764 l = NewList( priv->i_children );
765 for (int i = 0; i < l->i_count; i++)
767 vlc_object_hold( priv->pp_children[i] );
768 l->p_values[i].p_object = priv->pp_children[i];
770 libvlc_unlock (obj->p_libvlc);
774 static void DumpVariable (const void *data, const VISIT which, const int depth)
776 if (which != postorder && which != leaf)
780 const variable_t *p_var = *(const variable_t **)data;
781 const char *psz_type = "unknown";
783 switch( p_var->i_type & VLC_VAR_TYPE )
785 #define MYCASE( type, nice ) \
786 case VLC_VAR_ ## type: \
789 MYCASE( VOID, "void" );
790 MYCASE( BOOL, "bool" );
791 MYCASE( INTEGER, "integer" );
792 MYCASE( HOTKEY, "hotkey" );
793 MYCASE( STRING, "string" );
794 MYCASE( MODULE, "module" );
795 MYCASE( FILE, "file" );
796 MYCASE( DIRECTORY, "directory" );
797 MYCASE( VARIABLE, "variable" );
798 MYCASE( FLOAT, "float" );
799 MYCASE( TIME, "time" );
800 MYCASE( ADDRESS, "address" );
801 MYCASE( MUTEX, "mutex" );
802 MYCASE( LIST, "list" );
805 printf( " *-o \"%s\" (%s", p_var->psz_name, psz_type );
806 if( p_var->psz_text )
807 printf( ", %s", p_var->psz_text );
808 fputc( ')', stdout );
809 if( p_var->i_type & VLC_VAR_HASCHOICE )
810 fputs( ", has choices", stdout );
811 if( p_var->i_type & VLC_VAR_ISCOMMAND )
812 fputs( ", command", stdout );
813 if( p_var->i_entries )
814 printf( ", %d callbacks", p_var->i_entries );
815 switch( p_var->i_type & VLC_VAR_CLASS )
821 printf( ": %s", p_var->val.b_bool ? "true" : "false" );
823 case VLC_VAR_INTEGER:
824 printf( ": %d", p_var->val.i_int );
827 printf( ": \"%s\"", p_var->val.psz_string );
830 printf( ": %f", p_var->val.f_float );
833 printf( ": %"PRIi64, (int64_t)p_var->val.i_time );
835 case VLC_VAR_ADDRESS:
836 printf( ": %p", p_var->val.p_address );
839 fputs( ": TODO", stdout );
842 fputc( '\n', stdout );
845 /*****************************************************************************
846 * DumpCommand: print the current vlc structure
847 *****************************************************************************
848 * This function prints either an ASCII tree showing the connections between
849 * vlc objects, and additional information such as their refcount, thread ID,
850 * etc. (command "tree"), or the same data as a simple list (command "list").
851 *****************************************************************************/
852 static int DumpCommand( vlc_object_t *p_this, char const *psz_cmd,
853 vlc_value_t oldval, vlc_value_t newval, void *p_data )
855 (void)oldval; (void)p_data;
856 vlc_object_t *p_object = NULL;
858 if( *newval.psz_string )
860 /* try using the object's name to find it */
861 p_object = vlc_object_find_name( p_this, newval.psz_string,
869 libvlc_lock (p_this->p_libvlc);
870 if( *psz_cmd == 't' )
872 char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
875 p_object = VLC_OBJECT(p_this->p_libvlc);
878 DumpStructure( p_object, 0, psz_foo );
880 else if( *psz_cmd == 'v' )
883 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
885 PrintObject( p_object, "" );
886 vlc_mutex_lock( &vlc_internals( p_object )->var_lock );
887 if( vlc_internals( p_object )->var_root == NULL )
888 puts( " `-o No variables" );
890 twalk( vlc_internals( p_object )->var_root, DumpVariable );
891 vlc_mutex_unlock( &vlc_internals( p_object )->var_lock );
893 libvlc_unlock (p_this->p_libvlc);
895 if( *newval.psz_string )
897 vlc_object_release( p_object );
902 /*****************************************************************************
903 * vlc_list_release: free a list previously allocated by vlc_list_find
904 *****************************************************************************
905 * This function decreases the refcount of all objects in the list and
907 *****************************************************************************/
908 void vlc_list_release( vlc_list_t *p_list )
912 for( i_index = 0; i_index < p_list->i_count; i_index++ )
914 vlc_object_release( p_list->p_values[i_index].p_object );
917 free( p_list->p_values );
921 /* Following functions are local */
923 static vlc_object_t * FindObject( vlc_object_t *p_this, int i_type, int i_mode )
931 p_tmp = p_this->p_parent;
934 if( vlc_internals( p_tmp )->i_object_type == i_type )
936 vlc_object_hold( p_tmp );
941 return FindObject( p_tmp, i_type, i_mode );
947 for( i = vlc_internals( p_this )->i_children; i--; )
949 p_tmp = vlc_internals( p_this )->pp_children[i];
950 if( vlc_internals( p_tmp )->i_object_type == i_type )
952 vlc_object_hold( p_tmp );
955 else if( vlc_internals( p_tmp )->i_children )
957 p_tmp = FindObject( p_tmp, i_type, i_mode );
967 /* Handled in vlc_object_find */
974 static vlc_object_t * FindObjectName( vlc_object_t *p_this,
975 const char *psz_name,
984 p_tmp = p_this->p_parent;
987 if( !objnamecmp(p_tmp, psz_name) )
989 vlc_object_hold( p_tmp );
994 return FindObjectName( p_tmp, psz_name, i_mode );
1000 for( i = vlc_internals( p_this )->i_children; i--; )
1002 p_tmp = vlc_internals( p_this )->pp_children[i];
1003 if( !objnamecmp(p_tmp, psz_name ) )
1005 vlc_object_hold( p_tmp );
1008 else if( vlc_internals( p_tmp )->i_children )
1010 p_tmp = FindObjectName( p_tmp, psz_name, i_mode );
1020 /* Handled in vlc_object_find */
1028 static void PrintObject( vlc_object_t *p_this, const char *psz_prefix )
1030 char psz_children[20], psz_refcount[20], psz_thread[30], psz_name[50],
1033 int canc = vlc_savecancel ();
1034 memset( &psz_name, 0, sizeof(psz_name) );
1035 char *name = vlc_object_get_name(p_this);
1038 snprintf( psz_name, 49, " \"%s\"", name );
1041 psz_name[48] = '\"';
1044 psz_children[0] = '\0';
1045 switch( vlc_internals( p_this )->i_children )
1050 strcpy( psz_children, ", 1 child" );
1053 snprintf( psz_children, 19, ", %i children",
1054 vlc_internals( p_this )->i_children );
1058 psz_refcount[0] = '\0';
1059 if( vlc_internals( p_this )->i_refcount > 0 )
1060 snprintf( psz_refcount, 19, ", refcount %u",
1061 vlc_internals( p_this )->i_refcount );
1063 psz_thread[0] = '\0';
1064 if( vlc_internals( p_this )->b_thread )
1065 snprintf( psz_thread, 29, " (thread %lu)",
1066 (unsigned long)vlc_internals( p_this )->thread_id );
1068 psz_parent[0] = '\0';
1069 if( p_this->p_parent )
1070 snprintf( psz_parent, 19, ", parent %p", p_this->p_parent );
1072 printf( " %so %p %s%s%s%s%s%s\n", psz_prefix,
1073 p_this, p_this->psz_object_type,
1074 psz_name, psz_thread, psz_refcount, psz_children,
1076 vlc_restorecancel (canc);
1079 static void DumpStructure( vlc_object_t *p_this, int i_level, char *psz_foo )
1082 char i_back = psz_foo[i_level];
1083 psz_foo[i_level] = '\0';
1085 PrintObject( p_this, psz_foo );
1087 psz_foo[i_level] = i_back;
1089 if( i_level / 2 >= MAX_DUMPSTRUCTURE_DEPTH )
1091 msg_Warn( p_this, "structure tree is too deep" );
1095 for( i = 0 ; i < vlc_internals( p_this )->i_children ; i++ )
1099 psz_foo[i_level-1] = ' ';
1101 if( psz_foo[i_level-2] == '`' )
1103 psz_foo[i_level-2] = ' ';
1107 if( i == vlc_internals( p_this )->i_children - 1 )
1109 psz_foo[i_level] = '`';
1113 psz_foo[i_level] = '|';
1116 psz_foo[i_level+1] = '-';
1117 psz_foo[i_level+2] = '\0';
1119 DumpStructure( vlc_internals( p_this )->pp_children[i], i_level + 2,
1124 static vlc_list_t * NewList( int i_count )
1126 vlc_list_t * p_list = malloc( sizeof( vlc_list_t ) );
1127 if( p_list == NULL )
1130 p_list->i_count = i_count;
1134 p_list->p_values = NULL;
1138 p_list->p_values = malloc( i_count * sizeof( vlc_value_t ) );
1139 if( p_list->p_values == NULL )
1141 p_list->i_count = 0;
1148 static void ListReplace( vlc_list_t *p_list, vlc_object_t *p_object,
1151 if( p_list == NULL || i_index >= p_list->i_count )
1156 vlc_object_hold( p_object );
1158 p_list->p_values[i_index].p_object = p_object;
1163 /*static void ListAppend( vlc_list_t *p_list, vlc_object_t *p_object )
1165 if( p_list == NULL )
1170 p_list->p_values = realloc_or_free( p_list->p_values,
1171 (p_list->i_count + 1) * sizeof( vlc_value_t ) );
1172 if( p_list->p_values == NULL )
1174 p_list->i_count = 0;
1178 vlc_object_hold( p_object );
1180 p_list->p_values[p_list->i_count].p_object = p_object;
1186 static int CountChildren( vlc_object_t *p_this, int i_type )
1188 vlc_object_t *p_tmp;
1191 for( i = 0; i < vlc_internals( p_this )->i_children; i++ )
1193 p_tmp = vlc_internals( p_this )->pp_children[i];
1195 if( vlc_internals( p_tmp )->i_object_type == i_type )
1199 i_count += CountChildren( p_tmp, i_type );
1205 static void ListChildren( vlc_list_t *p_list, vlc_object_t *p_this, int i_type )
1207 vlc_object_t *p_tmp;
1210 for( i = 0; i < vlc_internals( p_this )->i_children; i++ )
1212 p_tmp = vlc_internals( p_this )->pp_children[i];
1214 if( vlc_internals( p_tmp )->i_object_type == i_type )
1215 ListReplace( p_list, p_tmp, p_list->i_count++ );
1217 ListChildren( p_list, p_tmp, i_type );