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 * FindParent ( vlc_object_t *, int );
79 static vlc_object_t * FindChild ( vlc_object_t *, int );
80 static vlc_object_t * FindObjectName( vlc_object_t *, const char *, int );
81 static void PrintObject ( vlc_object_t *, const char * );
82 static void DumpStructure ( vlc_object_t *, int, char * );
84 static vlc_list_t * NewList ( int );
85 static void ListReplace ( vlc_list_t *, vlc_object_t *, int );
86 /*static void ListAppend ( vlc_list_t *, vlc_object_t * );*/
87 static int CountChildren ( vlc_object_t *, int );
88 static void ListChildren ( vlc_list_t *, vlc_object_t *, int );
90 static void vlc_object_destroy( vlc_object_t *p_this );
91 static void vlc_object_detach_unlocked (vlc_object_t *p_this);
93 /*****************************************************************************
94 * Local structure lock
95 *****************************************************************************/
96 static void libvlc_lock (libvlc_int_t *p_libvlc)
98 vlc_mutex_lock (&(libvlc_priv (p_libvlc)->structure_lock));
101 static void libvlc_unlock (libvlc_int_t *p_libvlc)
103 vlc_mutex_unlock (&(libvlc_priv (p_libvlc)->structure_lock));
106 void *__vlc_custom_create( vlc_object_t *p_this, size_t i_size,
107 int i_type, const char *psz_type )
110 vlc_object_internals_t *p_priv;
113 * VLC objects are laid out as follow:
114 * - first the LibVLC-private per-object data,
115 * - then VLC_COMMON members from vlc_object_t,
116 * - finally, the type-specific data (if any).
118 * This function initializes the LibVLC and common data,
119 * and zeroes the rest.
121 p_priv = calloc( 1, sizeof( *p_priv ) + i_size );
125 assert (i_size >= sizeof (vlc_object_t));
126 p_new = (vlc_object_t *)(p_priv + 1);
128 p_priv->i_object_type = i_type;
129 p_new->psz_object_type = psz_type;
130 p_priv->psz_name = NULL;
132 p_new->b_die = false;
133 p_new->b_error = false;
134 p_new->b_force = false;
136 p_new->psz_header = NULL;
139 p_new->i_flags = p_this->i_flags
140 & (OBJECT_FLAGS_NODBG|OBJECT_FLAGS_QUIET|OBJECT_FLAGS_NOINTERACT);
142 p_priv->var_root = NULL;
146 libvlc_int_t *self = (libvlc_int_t*)p_new;
147 p_new->p_libvlc = self;
148 vlc_mutex_init (&(libvlc_priv (self)->structure_lock));
152 p_new->p_libvlc = p_this->p_libvlc;
154 vlc_spin_init( &p_priv->ref_spin );
155 p_priv->i_refcount = 1;
156 p_priv->pf_destructor = NULL;
157 p_priv->b_thread = false;
158 p_new->p_parent = NULL;
159 p_priv->pp_children = NULL;
160 p_priv->i_children = 0;
162 /* Initialize mutexes and condvars */
163 vlc_mutex_init( &p_priv->var_lock );
164 vlc_cond_init( &p_priv->var_wait );
165 p_priv->pipes[0] = p_priv->pipes[1] = -1;
167 if (p_new == VLC_OBJECT(p_new->p_libvlc))
168 { /* TODO: should be in src/libvlc.c */
169 int canc = vlc_savecancel ();
170 var_Create( p_new, "tree", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
171 var_AddCallback( p_new, "tree", DumpCommand, NULL );
172 var_Create( p_new, "vars", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
173 var_AddCallback( p_new, "vars", DumpCommand, NULL );
174 vlc_restorecancel (canc);
182 * Allocates and initializes a vlc object.
184 * @param i_type known object type (all of them are negative integer values),
185 * or object byte size (always positive).
187 * @return the new object, or NULL on error.
189 void * __vlc_object_create( vlc_object_t *p_this, int i_type )
191 const char * psz_type;
196 case VLC_OBJECT_DECODER:
197 i_size = sizeof(decoder_t);
198 psz_type = "decoder";
200 case VLC_OBJECT_AOUT:
201 i_size = sizeof(aout_instance_t);
202 psz_type = "audio output";
205 assert( i_type > 0 ); /* unknown type?! */
207 i_type = VLC_OBJECT_GENERIC;
208 psz_type = "generic";
212 return vlc_custom_create( p_this, i_size, i_type, psz_type );
217 ****************************************************************************
218 * Set the destructor of a vlc object
220 * This function sets the destructor of the vlc object. It will be called
221 * when the object is destroyed when the its refcount reaches 0.
222 * (It is called by the internal function vlc_object_destroy())
223 *****************************************************************************/
224 void __vlc_object_set_destructor( vlc_object_t *p_this,
225 vlc_destructor_t pf_destructor )
227 vlc_object_internals_t *p_priv = vlc_internals(p_this );
229 vlc_spin_lock( &p_priv->ref_spin );
230 p_priv->pf_destructor = pf_destructor;
231 vlc_spin_unlock( &p_priv->ref_spin );
234 static vlc_mutex_t name_lock = VLC_STATIC_MUTEX;
236 #undef vlc_object_set_name
237 int vlc_object_set_name(vlc_object_t *obj, const char *name)
239 vlc_object_internals_t *priv = vlc_internals(obj);
240 char *newname = name ? strdup (name) : NULL;
243 vlc_mutex_lock (&name_lock);
244 oldname = priv->psz_name;
245 priv->psz_name = newname;
246 vlc_mutex_unlock (&name_lock);
249 return (priv->psz_name || !name) ? VLC_SUCCESS : VLC_ENOMEM;
252 #undef vlc_object_get_name
253 char *vlc_object_get_name(const vlc_object_t *obj)
255 vlc_object_internals_t *priv = vlc_internals(obj);
258 vlc_mutex_lock (&name_lock);
259 name = priv->psz_name ? strdup (priv->psz_name) : NULL;
260 vlc_mutex_unlock (&name_lock);
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. */
293 var_DestroyAll( p_this );
295 vlc_cond_destroy( &p_priv->var_wait );
296 vlc_mutex_destroy( &p_priv->var_lock );
298 free( p_this->psz_header );
300 free( p_priv->psz_name );
302 vlc_spin_destroy( &p_priv->ref_spin );
303 if( p_priv->pipes[1] != -1 && p_priv->pipes[1] != p_priv->pipes[0] )
304 close( p_priv->pipes[1] );
305 if( p_priv->pipes[0] != -1 )
306 close( p_priv->pipes[0] );
307 if( VLC_OBJECT(p_this->p_libvlc) == p_this )
308 vlc_mutex_destroy (&(libvlc_priv ((libvlc_int_t *)p_this)->structure_lock));
316 * select()-able pipes emulated using Winsock
318 static int pipe (int fd[2])
321 int addrlen = sizeof (addr);
323 SOCKET l = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP), a,
324 c = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
325 if ((l == INVALID_SOCKET) || (c == INVALID_SOCKET))
328 memset (&addr, 0, sizeof (addr));
329 addr.sin_family = AF_INET;
330 addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
331 if (bind (l, (PSOCKADDR)&addr, sizeof (addr))
332 || getsockname (l, (PSOCKADDR)&addr, &addrlen)
334 || connect (c, (PSOCKADDR)&addr, addrlen))
337 a = accept (l, NULL, NULL);
338 if (a == INVALID_SOCKET)
349 if (l != INVALID_SOCKET)
351 if (c != INVALID_SOCKET)
357 static vlc_mutex_t pipe_lock = VLC_STATIC_MUTEX;
360 * Returns the readable end of a pipe that becomes readable once termination
361 * of the object is requested (vlc_object_kill()).
362 * This can be used to wake-up out of a select() or poll() event loop, such
363 * typically when doing network I/O.
365 * Note that the pipe will remain the same for the lifetime of the object.
366 * DO NOT read the pipe nor close it yourself. Ever.
368 * @param obj object that would be "killed"
369 * @return a readable pipe descriptor, or -1 on error.
371 int vlc_object_waitpipe( vlc_object_t *obj )
373 vlc_object_internals_t *internals = vlc_internals( obj );
375 vlc_mutex_lock (&pipe_lock);
376 if (internals->pipes[0] == -1)
378 /* This can only ever happen if someone killed us without locking: */
379 assert (internals->pipes[1] == -1);
381 #if defined (HAVE_SYS_EVENTFD_H)
382 internals->pipes[0] = internals->pipes[1] = eventfd (0, 0);
383 if (internals->pipes[0] == -1)
386 if (pipe (internals->pipes))
387 internals->pipes[0] = internals->pipes[1] = -1;
390 if (internals->pipes[0] != -1 && obj->b_die)
391 { /* Race condition: vlc_object_kill() already invoked! */
392 msg_Dbg (obj, "waitpipe: object already dying");
393 write (internals->pipes[1], &(uint64_t){ 1 }, sizeof (uint64_t));
396 vlc_mutex_unlock (&pipe_lock);
397 return internals->pipes[0];
402 * Requests termination of an object, cancels the object thread, and make the
403 * object wait pipe (if it exists) readable. Not a cancellation point.
405 void __vlc_object_kill( vlc_object_t *p_this )
407 vlc_object_internals_t *priv = vlc_internals( p_this );
410 vlc_thread_cancel( p_this );
411 vlc_mutex_lock( &pipe_lock );
415 p_this->b_die = true;
418 /* This also serves as a memory barrier toward vlc_object_alive(): */
419 vlc_mutex_unlock( &pipe_lock );
423 int canc = vlc_savecancel ();
425 /* write _after_ setting b_die, so vlc_object_alive() returns false */
426 write (fd, &(uint64_t){ 1 }, sizeof (uint64_t));
427 msg_Dbg (p_this, "waitpipe: object killed");
428 vlc_restorecancel (canc);
433 /*****************************************************************************
434 * find a typed object and increment its refcount
435 *****************************************************************************
436 * This function recursively looks for a given object type. i_mode can be one
437 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
438 *****************************************************************************/
439 void * __vlc_object_find( vlc_object_t *p_this, int i_type, int i_mode )
441 vlc_object_t *p_found;
443 /* If we are of the requested type ourselves, don't look further */
444 if( vlc_internals (p_this)->i_object_type == i_type )
446 vlc_object_hold( p_this );
450 /* Otherwise, recursively look for the object */
451 if (i_mode == FIND_ANYWHERE)
452 return vlc_object_find (p_this->p_libvlc, i_type, FIND_CHILD);
456 case VLC_OBJECT_VOUT:
457 case VLC_OBJECT_AOUT:
459 case VLC_OBJECT_INPUT:
460 /* input can only be accessed like this from children,
461 * otherwise we could not promise that it is initialized */
462 if (i_mode != FIND_PARENT)
469 libvlc_lock (p_this->p_libvlc);
473 p_found = FindParent (p_this, i_type);
476 p_found = FindChild (p_this, i_type);
481 libvlc_unlock (p_this->p_libvlc);
486 static int objnamecmp(const vlc_object_t *obj, const char *name)
488 char *objname = vlc_object_get_name(obj);
492 int ret = strcmp (objname, name);
497 #undef vlc_object_find_name
499 * Finds a named object and increment its reference count.
500 * Beware that objects found in this manner can be "owned" by another thread,
501 * be of _any_ type, and be attached to any module (if any). With such an
502 * object reference, you can set or get object variables, emit log messages,
503 * and read write-once object parameters (psz_object_type, etc).
504 * You CANNOT cast the object to a more specific object type, and you
505 * definitely cannot invoke object type-specific callbacks with this.
507 * @param p_this object to search from
508 * @param psz_name name of the object to search for
509 * @param i_mode search direction: FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
511 * @return a matching object (must be released by the caller),
514 vlc_object_t *vlc_object_find_name( vlc_object_t *p_this,
515 const char *psz_name, int i_mode )
517 vlc_object_t *p_found;
519 /* Reading psz_object_name from a separate inhibits thread-safety.
520 * Use a libvlc address variable instead for that sort of things! */
521 msg_Warn( p_this, "%s(%s) is not safe!", __func__, psz_name );
522 /* If have the requested name ourselves, don't look further */
523 if( !objnamecmp(p_this, psz_name) )
525 vlc_object_hold( p_this );
529 /* Otherwise, recursively look for the object */
530 if (i_mode == FIND_ANYWHERE)
531 return vlc_object_find_name (VLC_OBJECT(p_this->p_libvlc), psz_name,
534 libvlc_lock (p_this->p_libvlc);
536 /* Otherwise, recursively look for the object */
537 p_found = FindObjectName( p_this, psz_name, i_mode );
539 libvlc_unlock (p_this->p_libvlc);
544 * Increment an object reference counter.
546 void * __vlc_object_hold( vlc_object_t *p_this )
548 vlc_object_internals_t *internals = vlc_internals( p_this );
550 vlc_spin_lock( &internals->ref_spin );
551 /* Avoid obvious freed object uses */
552 assert( internals->i_refcount > 0 );
553 /* Increment the counter */
554 internals->i_refcount++;
555 vlc_spin_unlock( &internals->ref_spin );
559 /*****************************************************************************
560 * Decrement an object refcount
561 * And destroy the object if its refcount reach zero.
562 *****************************************************************************/
563 void __vlc_object_release( vlc_object_t *p_this )
565 vlc_object_internals_t *internals = vlc_internals( p_this );
566 vlc_object_t *parent = NULL;
567 bool b_should_destroy;
569 vlc_spin_lock( &internals->ref_spin );
570 assert( internals->i_refcount > 0 );
572 if( internals->i_refcount > 1 )
575 /* There are still other references to the object */
576 internals->i_refcount--;
577 vlc_spin_unlock( &internals->ref_spin );
580 vlc_spin_unlock( &internals->ref_spin );
583 /* Remember that we cannot hold the spin while waiting on the mutex */
584 libvlc_lock (p_this->p_libvlc);
585 /* Take the spin again. Note that another thread may have held the
586 * object in the (very short) mean time. */
587 vlc_spin_lock( &internals->ref_spin );
588 b_should_destroy = --internals->i_refcount == 0;
589 vlc_spin_unlock( &internals->ref_spin );
591 if( b_should_destroy )
593 parent = p_this->p_parent;
595 /* Detach from parent to protect against FIND_CHILDREN */
596 vlc_object_detach_unlocked (p_this);
598 /* We have no children */
599 assert (internals->i_children == 0);
601 libvlc_unlock (p_this->p_libvlc);
603 if( b_should_destroy )
607 canc = vlc_savecancel ();
608 vlc_object_destroy( p_this );
609 vlc_restorecancel (canc);
611 vlc_object_release (parent);
616 ****************************************************************************
617 * attach object to a parent object
618 *****************************************************************************
619 * This function sets p_this as a child of p_parent, and p_parent as a parent
620 * of p_this. This link can be undone using vlc_object_detach.
621 *****************************************************************************/
622 void __vlc_object_attach( vlc_object_t *p_this, vlc_object_t *p_parent )
624 if( !p_this ) return;
626 vlc_object_hold (p_parent);
627 libvlc_lock (p_this->p_libvlc);
629 /* Attach the parent to its child */
630 assert (!p_this->p_parent);
631 p_this->p_parent = p_parent;
633 /* Attach the child to its parent */
634 vlc_object_internals_t *priv = vlc_internals( p_parent );
635 INSERT_ELEM( priv->pp_children, priv->i_children, priv->i_children,
637 libvlc_unlock (p_this->p_libvlc);
641 static void vlc_object_detach_unlocked (vlc_object_t *p_this)
643 if (p_this->p_parent == NULL)
646 vlc_object_internals_t *priv = vlc_internals( p_this->p_parent );
650 /* Remove p_this's parent */
651 p_this->p_parent = NULL;
653 /* Remove all of p_parent's children which are p_this */
654 for( i_index = priv->i_children ; i_index-- ; )
656 if( priv->pp_children[i_index] == p_this )
659 for( i = i_index ; i < priv->i_children ; i++ )
660 priv->pp_children[i] = priv->pp_children[i+1];
664 if( priv->i_children )
666 vlc_object_t **pp_children = (vlc_object_t **)
667 realloc( priv->pp_children,
668 priv->i_children * sizeof(vlc_object_t *) );
670 priv->pp_children = pp_children;
674 /* Special case - don't realloc() to zero to avoid leaking */
675 free( priv->pp_children );
676 priv->pp_children = NULL;
682 ****************************************************************************
683 * detach object from its parent
684 *****************************************************************************
685 * This function removes all links between an object and its parent.
686 *****************************************************************************/
687 void __vlc_object_detach( vlc_object_t *p_this )
689 vlc_object_t *p_parent;
690 if( !p_this ) return;
692 libvlc_lock (p_this->p_libvlc);
693 p_parent = p_this->p_parent;
695 vlc_object_detach_unlocked( p_this );
696 libvlc_unlock (p_this->p_libvlc);
699 vlc_object_release (p_parent);
704 ****************************************************************************
705 * find a list typed objects and increment their refcount
706 *****************************************************************************
707 * This function recursively looks for a given object type. i_mode can be one
708 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
709 *****************************************************************************/
710 vlc_list_t * vlc_list_find( vlc_object_t *p_this, int i_type, int i_mode )
715 /* Look for the objects */
719 return vlc_list_find (VLC_OBJECT(p_this->p_libvlc), i_type, FIND_CHILD);
722 libvlc_lock (p_this->p_libvlc);
723 i_count = CountChildren( p_this, i_type );
724 p_list = NewList( i_count );
726 /* Check allocation was successful */
727 if( p_list->i_count != i_count )
729 libvlc_unlock (p_this->p_libvlc);
735 ListChildren( p_list, p_this, i_type );
736 libvlc_unlock (p_this->p_libvlc);
740 msg_Err( p_this, "unimplemented!" );
741 p_list = NewList( 0 );
749 * Gets the list of children of an objects, and increment their reference
751 * @return a list (possibly empty) or NULL in case of error.
753 vlc_list_t *__vlc_list_children( vlc_object_t *obj )
756 vlc_object_internals_t *priv = vlc_internals( obj );
758 libvlc_lock (obj->p_libvlc);
759 l = NewList( priv->i_children );
760 for (int i = 0; i < l->i_count; i++)
762 vlc_object_hold( priv->pp_children[i] );
763 l->p_values[i].p_object = priv->pp_children[i];
765 libvlc_unlock (obj->p_libvlc);
769 static void DumpVariable (const void *data, const VISIT which, const int depth)
771 if (which != postorder && which != leaf)
775 const variable_t *p_var = *(const variable_t **)data;
776 const char *psz_type = "unknown";
778 switch( p_var->i_type & VLC_VAR_TYPE )
780 #define MYCASE( type, nice ) \
781 case VLC_VAR_ ## type: \
784 MYCASE( VOID, "void" );
785 MYCASE( BOOL, "bool" );
786 MYCASE( INTEGER, "integer" );
787 MYCASE( HOTKEY, "hotkey" );
788 MYCASE( STRING, "string" );
789 MYCASE( MODULE, "module" );
790 MYCASE( FILE, "file" );
791 MYCASE( DIRECTORY, "directory" );
792 MYCASE( VARIABLE, "variable" );
793 MYCASE( FLOAT, "float" );
794 MYCASE( TIME, "time" );
795 MYCASE( ADDRESS, "address" );
796 MYCASE( MUTEX, "mutex" );
797 MYCASE( LIST, "list" );
800 printf( " *-o \"%s\" (%s", p_var->psz_name, psz_type );
801 if( p_var->psz_text )
802 printf( ", %s", p_var->psz_text );
803 fputc( ')', stdout );
804 if( p_var->i_type & VLC_VAR_HASCHOICE )
805 fputs( ", has choices", stdout );
806 if( p_var->i_type & VLC_VAR_ISCOMMAND )
807 fputs( ", command", stdout );
808 if( p_var->i_entries )
809 printf( ", %d callbacks", p_var->i_entries );
810 switch( p_var->i_type & VLC_VAR_CLASS )
816 printf( ": %s", p_var->val.b_bool ? "true" : "false" );
818 case VLC_VAR_INTEGER:
819 printf( ": %d", p_var->val.i_int );
822 printf( ": \"%s\"", p_var->val.psz_string );
825 printf( ": %f", p_var->val.f_float );
828 printf( ": %"PRIi64, (int64_t)p_var->val.i_time );
830 case VLC_VAR_ADDRESS:
831 printf( ": %p", p_var->val.p_address );
834 fputs( ": TODO", stdout );
837 fputc( '\n', stdout );
840 /*****************************************************************************
841 * DumpCommand: print the current vlc structure
842 *****************************************************************************
843 * This function prints either an ASCII tree showing the connections between
844 * vlc objects, and additional information such as their refcount, thread ID,
845 * etc. (command "tree"), or the same data as a simple list (command "list").
846 *****************************************************************************/
847 static int DumpCommand( vlc_object_t *p_this, char const *psz_cmd,
848 vlc_value_t oldval, vlc_value_t newval, void *p_data )
850 (void)oldval; (void)p_data;
851 vlc_object_t *p_object = NULL;
853 if( *newval.psz_string )
855 /* try using the object's name to find it */
856 p_object = vlc_object_find_name( p_this, newval.psz_string,
864 libvlc_lock (p_this->p_libvlc);
865 if( *psz_cmd == 't' )
867 char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
870 p_object = VLC_OBJECT(p_this->p_libvlc);
873 DumpStructure( p_object, 0, psz_foo );
875 else if( *psz_cmd == 'v' )
878 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
880 PrintObject( p_object, "" );
881 vlc_mutex_lock( &vlc_internals( p_object )->var_lock );
882 if( vlc_internals( p_object )->var_root == NULL )
883 puts( " `-o No variables" );
885 twalk( vlc_internals( p_object )->var_root, DumpVariable );
886 vlc_mutex_unlock( &vlc_internals( p_object )->var_lock );
888 libvlc_unlock (p_this->p_libvlc);
890 if( *newval.psz_string )
892 vlc_object_release( p_object );
897 /*****************************************************************************
898 * vlc_list_release: free a list previously allocated by vlc_list_find
899 *****************************************************************************
900 * This function decreases the refcount of all objects in the list and
902 *****************************************************************************/
903 void vlc_list_release( vlc_list_t *p_list )
907 for( i_index = 0; i_index < p_list->i_count; i_index++ )
909 vlc_object_release( p_list->p_values[i_index].p_object );
912 free( p_list->p_values );
916 /* Following functions are local */
918 static vlc_object_t *FindParent (vlc_object_t *p_this, int i_type)
920 for (vlc_object_t *parent = p_this->p_parent;
922 parent = parent->p_parent)
924 if (vlc_internals (parent)->i_object_type == i_type)
925 return vlc_object_hold (parent);
930 static vlc_object_t *FindChild (vlc_object_t *p_this, int i_type)
932 for (int i = vlc_internals( p_this )->i_children; i--; )
934 vlc_object_t *child = vlc_internals (p_this)->pp_children[i];
935 if (vlc_internals (child)->i_object_type == i_type)
936 return vlc_object_hold (child);
938 child = FindChild (child, i_type);
946 static vlc_object_t * FindObjectName( vlc_object_t *p_this,
947 const char *psz_name,
956 p_tmp = p_this->p_parent;
959 if( !objnamecmp(p_tmp, psz_name) )
961 vlc_object_hold( p_tmp );
966 return FindObjectName( p_tmp, psz_name, i_mode );
972 for( i = vlc_internals( p_this )->i_children; i--; )
974 p_tmp = vlc_internals( p_this )->pp_children[i];
975 if( !objnamecmp(p_tmp, psz_name ) )
977 vlc_object_hold( p_tmp );
980 else if( vlc_internals( p_tmp )->i_children )
982 p_tmp = FindObjectName( p_tmp, psz_name, i_mode );
992 /* Handled in vlc_object_find */
1000 static void PrintObject( vlc_object_t *p_this, const char *psz_prefix )
1002 char psz_children[20], psz_refcount[20], psz_thread[30], psz_name[50],
1005 int canc = vlc_savecancel ();
1006 memset( &psz_name, 0, sizeof(psz_name) );
1007 char *name = vlc_object_get_name(p_this);
1010 snprintf( psz_name, 49, " \"%s\"", name );
1013 psz_name[48] = '\"';
1016 psz_children[0] = '\0';
1017 switch( vlc_internals( p_this )->i_children )
1022 strcpy( psz_children, ", 1 child" );
1025 snprintf( psz_children, 19, ", %i children",
1026 vlc_internals( p_this )->i_children );
1030 psz_refcount[0] = '\0';
1031 if( vlc_internals( p_this )->i_refcount > 0 )
1032 snprintf( psz_refcount, 19, ", refcount %u",
1033 vlc_internals( p_this )->i_refcount );
1035 psz_thread[0] = '\0';
1036 if( vlc_internals( p_this )->b_thread )
1037 snprintf( psz_thread, 29, " (thread %lu)",
1038 (unsigned long)vlc_internals( p_this )->thread_id );
1040 psz_parent[0] = '\0';
1041 if( p_this->p_parent )
1042 snprintf( psz_parent, 19, ", parent %p", p_this->p_parent );
1044 printf( " %so %p %s%s%s%s%s%s\n", psz_prefix,
1045 p_this, p_this->psz_object_type,
1046 psz_name, psz_thread, psz_refcount, psz_children,
1048 vlc_restorecancel (canc);
1051 static void DumpStructure( vlc_object_t *p_this, int i_level, char *psz_foo )
1054 char i_back = psz_foo[i_level];
1055 psz_foo[i_level] = '\0';
1057 PrintObject( p_this, psz_foo );
1059 psz_foo[i_level] = i_back;
1061 if( i_level / 2 >= MAX_DUMPSTRUCTURE_DEPTH )
1063 msg_Warn( p_this, "structure tree is too deep" );
1067 for( i = 0 ; i < vlc_internals( p_this )->i_children ; i++ )
1071 psz_foo[i_level-1] = ' ';
1073 if( psz_foo[i_level-2] == '`' )
1075 psz_foo[i_level-2] = ' ';
1079 if( i == vlc_internals( p_this )->i_children - 1 )
1081 psz_foo[i_level] = '`';
1085 psz_foo[i_level] = '|';
1088 psz_foo[i_level+1] = '-';
1089 psz_foo[i_level+2] = '\0';
1091 DumpStructure( vlc_internals( p_this )->pp_children[i], i_level + 2,
1096 static vlc_list_t * NewList( int i_count )
1098 vlc_list_t * p_list = malloc( sizeof( vlc_list_t ) );
1099 if( p_list == NULL )
1102 p_list->i_count = i_count;
1106 p_list->p_values = NULL;
1110 p_list->p_values = malloc( i_count * sizeof( vlc_value_t ) );
1111 if( p_list->p_values == NULL )
1113 p_list->i_count = 0;
1120 static void ListReplace( vlc_list_t *p_list, vlc_object_t *p_object,
1123 if( p_list == NULL || i_index >= p_list->i_count )
1128 vlc_object_hold( p_object );
1130 p_list->p_values[i_index].p_object = p_object;
1135 /*static void ListAppend( vlc_list_t *p_list, vlc_object_t *p_object )
1137 if( p_list == NULL )
1142 p_list->p_values = realloc_or_free( p_list->p_values,
1143 (p_list->i_count + 1) * sizeof( vlc_value_t ) );
1144 if( p_list->p_values == NULL )
1146 p_list->i_count = 0;
1150 vlc_object_hold( p_object );
1152 p_list->p_values[p_list->i_count].p_object = p_object;
1158 static int CountChildren( vlc_object_t *p_this, int i_type )
1160 vlc_object_t *p_tmp;
1163 for( i = 0; i < vlc_internals( p_this )->i_children; i++ )
1165 p_tmp = vlc_internals( p_this )->pp_children[i];
1167 if( vlc_internals( p_tmp )->i_object_type == i_type )
1171 i_count += CountChildren( p_tmp, i_type );
1177 static void ListChildren( vlc_list_t *p_list, vlc_object_t *p_this, int i_type )
1179 vlc_object_t *p_tmp;
1182 for( i = 0; i < vlc_internals( p_this )->i_children; i++ )
1184 p_tmp = vlc_internals( p_this )->pp_children[i];
1186 if( vlc_internals( p_tmp )->i_object_type == i_type )
1187 ListReplace( p_list, p_tmp, p_list->i_count++ );
1189 ListChildren( p_list, p_tmp, i_type );