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 */
60 #if defined (HAVE_SYS_EVENTFD_H)
61 # include <sys/eventfd.h>
65 /*****************************************************************************
67 *****************************************************************************/
68 static int DumpCommand( vlc_object_t *, char const *,
69 vlc_value_t, vlc_value_t, void * );
71 static vlc_object_t * FindObject ( vlc_object_t *, int, int );
72 static vlc_object_t * FindObjectName( vlc_object_t *, const char *, int );
73 static void PrintObject ( vlc_object_t *, const char * );
74 static void DumpStructure ( vlc_object_t *, int, char * );
76 static vlc_list_t * NewList ( int );
77 static void ListReplace ( vlc_list_t *, vlc_object_t *, int );
78 /*static void ListAppend ( vlc_list_t *, vlc_object_t * );*/
79 static int CountChildren ( vlc_object_t *, int );
80 static void ListChildren ( vlc_list_t *, vlc_object_t *, int );
82 static void vlc_object_destroy( vlc_object_t *p_this );
83 static void vlc_object_detach_unlocked (vlc_object_t *p_this);
85 /*****************************************************************************
86 * Local structure lock
87 *****************************************************************************/
88 static void libvlc_lock (libvlc_int_t *p_libvlc)
90 vlc_mutex_lock (&(libvlc_priv (p_libvlc)->structure_lock));
93 static void libvlc_unlock (libvlc_int_t *p_libvlc)
95 vlc_mutex_unlock (&(libvlc_priv (p_libvlc)->structure_lock));
98 void *__vlc_custom_create( vlc_object_t *p_this, size_t i_size,
99 int i_type, const char *psz_type )
102 vlc_object_internals_t *p_priv;
105 * VLC objects are laid out as follow:
106 * - first the LibVLC-private per-object data,
107 * - then VLC_COMMON members from vlc_object_t,
108 * - finally, the type-specific data (if any).
110 * This function initializes the LibVLC and common data,
111 * and zeroes the rest.
113 p_priv = calloc( 1, sizeof( *p_priv ) + i_size );
117 assert (i_size >= sizeof (vlc_object_t));
118 p_new = (vlc_object_t *)(p_priv + 1);
120 p_priv->i_object_type = i_type;
121 p_new->psz_object_type = psz_type;
122 p_priv->psz_name = NULL;
124 p_new->b_die = false;
125 p_new->b_error = false;
126 p_new->b_force = false;
128 p_new->psz_header = NULL;
131 p_new->i_flags = p_this->i_flags
132 & (OBJECT_FLAGS_NODBG|OBJECT_FLAGS_QUIET|OBJECT_FLAGS_NOINTERACT);
134 p_priv->p_vars = calloc( 16, sizeof( variable_t ) );
136 if( !p_priv->p_vars )
144 libvlc_int_t *self = (libvlc_int_t*)p_new;
145 p_new->p_libvlc = self;
146 vlc_mutex_init (&(libvlc_priv (self)->structure_lock));
150 p_new->p_libvlc = p_this->p_libvlc;
152 vlc_spin_init( &p_priv->ref_spin );
153 p_priv->i_refcount = 1;
154 p_priv->pf_destructor = NULL;
155 p_priv->b_thread = false;
156 p_new->p_parent = NULL;
157 p_priv->pp_children = NULL;
158 p_priv->i_children = 0;
160 /* Initialize mutexes and condvars */
161 vlc_mutex_init( &p_priv->var_lock );
162 vlc_cond_init( &p_priv->var_wait );
163 p_priv->pipes[0] = p_priv->pipes[1] = -1;
165 if (p_new == VLC_OBJECT(p_new->p_libvlc))
166 { /* TODO: should be in src/libvlc.c */
167 int canc = vlc_savecancel ();
168 var_Create( p_new, "tree", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
169 var_AddCallback( p_new, "tree", DumpCommand, NULL );
170 var_Create( p_new, "vars", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
171 var_AddCallback( p_new, "vars", DumpCommand, NULL );
172 vlc_restorecancel (canc);
180 * Allocates and initializes a vlc object.
182 * @param i_type known object type (all of them are negative integer values),
183 * or object byte size (always positive).
185 * @return the new object, or NULL on error.
187 void * __vlc_object_create( vlc_object_t *p_this, int i_type )
189 const char * psz_type;
194 case VLC_OBJECT_DECODER:
195 i_size = sizeof(decoder_t);
196 psz_type = "decoder";
198 case VLC_OBJECT_AOUT:
199 i_size = sizeof(aout_instance_t);
200 psz_type = "audio output";
203 assert( i_type > 0 ); /* unknown type?! */
205 i_type = VLC_OBJECT_GENERIC;
206 psz_type = "generic";
210 return vlc_custom_create( p_this, i_size, i_type, psz_type );
215 ****************************************************************************
216 * Set the destructor of a vlc object
218 * This function sets the destructor of the vlc object. It will be called
219 * when the object is destroyed when the its refcount reaches 0.
220 * (It is called by the internal function vlc_object_destroy())
221 *****************************************************************************/
222 void __vlc_object_set_destructor( vlc_object_t *p_this,
223 vlc_destructor_t pf_destructor )
225 vlc_object_internals_t *p_priv = vlc_internals(p_this );
227 vlc_spin_lock( &p_priv->ref_spin );
228 p_priv->pf_destructor = pf_destructor;
229 vlc_spin_unlock( &p_priv->ref_spin );
232 static vlc_mutex_t name_lock = VLC_STATIC_MUTEX;
234 #undef vlc_object_set_name
235 int vlc_object_set_name(vlc_object_t *obj, const char *name)
237 vlc_object_internals_t *priv = vlc_internals(obj);
238 char *newname = name ? strdup (name) : NULL;
241 vlc_mutex_lock (&name_lock);
242 oldname = priv->psz_name;
243 priv->psz_name = newname;
244 vlc_mutex_unlock (&name_lock);
247 return (priv->psz_name || !name) ? VLC_SUCCESS : VLC_ENOMEM;
250 #undef vlc_object_get_name
251 char *vlc_object_get_name(const vlc_object_t *obj)
253 vlc_object_internals_t *priv = vlc_internals(obj);
256 vlc_mutex_lock (&name_lock);
257 name = priv->psz_name ? strdup (priv->psz_name) : NULL;
258 vlc_mutex_unlock (&name_lock);
264 ****************************************************************************
265 * Destroy a vlc object (Internal)
267 * This function destroys an object that has been previously allocated with
268 * vlc_object_create. The object's refcount must be zero and it must not be
269 * attached to other objects in any way.
271 * This function must be called with cancellation disabled (currently).
272 *****************************************************************************/
273 static void vlc_object_destroy( vlc_object_t *p_this )
275 vlc_object_internals_t *p_priv = vlc_internals( p_this );
277 /* Objects are always detached beforehand */
278 assert( !p_this->p_parent );
280 /* Send a kill to the object's thread if applicable */
281 vlc_object_kill( p_this );
283 /* Call the custom "subclass" destructor */
284 if( p_priv->pf_destructor )
285 p_priv->pf_destructor( p_this );
287 /* Any thread must have been cleaned up at this point. */
288 assert( !p_priv->b_thread );
290 /* Destroy the associated variables, starting from the end so that
291 * no memmove calls have to be done. */
292 while( p_priv->i_vars )
294 var_Destroy( p_this, p_priv->p_vars[p_priv->i_vars - 1].psz_name );
297 free( p_priv->p_vars );
298 vlc_cond_destroy( &p_priv->var_wait );
299 vlc_mutex_destroy( &p_priv->var_lock );
301 free( p_this->psz_header );
303 free( p_priv->psz_name );
305 vlc_spin_destroy( &p_priv->ref_spin );
306 if( p_priv->pipes[1] != -1 && p_priv->pipes[1] != p_priv->pipes[0] )
307 close( p_priv->pipes[1] );
308 if( p_priv->pipes[0] != -1 )
309 close( p_priv->pipes[0] );
310 if( VLC_OBJECT(p_this->p_libvlc) == p_this )
311 vlc_mutex_destroy (&(libvlc_priv ((libvlc_int_t *)p_this)->structure_lock));
318 # include <winsock2.h>
319 # include <ws2tcpip.h>
322 * select()-able pipes emulated using Winsock
324 static int pipe (int fd[2])
327 int addrlen = sizeof (addr);
329 SOCKET l = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP), a,
330 c = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
331 if ((l == INVALID_SOCKET) || (c == INVALID_SOCKET))
334 memset (&addr, 0, sizeof (addr));
335 addr.sin_family = AF_INET;
336 addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
337 if (bind (l, (PSOCKADDR)&addr, sizeof (addr))
338 || getsockname (l, (PSOCKADDR)&addr, &addrlen)
340 || connect (c, (PSOCKADDR)&addr, addrlen))
343 a = accept (l, NULL, NULL);
344 if (a == INVALID_SOCKET)
355 if (l != INVALID_SOCKET)
357 if (c != INVALID_SOCKET)
363 #define read( a, b, c ) recv (a, b, c, 0)
365 #define write( a, b, c ) send (a, b, c, 0)
367 #define close( a ) closesocket (a)
370 static vlc_mutex_t pipe_lock = VLC_STATIC_MUTEX;
373 * Returns the readable end of a pipe that becomes readable once termination
374 * of the object is requested (vlc_object_kill()).
375 * This can be used to wake-up out of a select() or poll() event loop, such
376 * typically when doing network I/O.
378 * Note that the pipe will remain the same for the lifetime of the object.
379 * DO NOT read the pipe nor close it yourself. Ever.
381 * @param obj object that would be "killed"
382 * @return a readable pipe descriptor, or -1 on error.
384 int vlc_object_waitpipe( vlc_object_t *obj )
386 vlc_object_internals_t *internals = vlc_internals( obj );
388 vlc_mutex_lock (&pipe_lock);
389 if (internals->pipes[0] == -1)
391 /* This can only ever happen if someone killed us without locking: */
392 assert (internals->pipes[1] == -1);
394 #if defined (HAVE_SYS_EVENTFD_H)
395 internals->pipes[0] = internals->pipes[1] = eventfd (0, 0);
396 if (internals->pipes[0] == -1)
399 if (pipe (internals->pipes))
400 internals->pipes[0] = internals->pipes[1] = -1;
403 if (internals->pipes[0] != -1 && obj->b_die)
404 { /* Race condition: vlc_object_kill() already invoked! */
405 msg_Dbg (obj, "waitpipe: object already dying");
406 write (internals->pipes[1], &(uint64_t){ 1 }, sizeof (uint64_t));
409 vlc_mutex_unlock (&pipe_lock);
410 return internals->pipes[0];
415 * Requests termination of an object, cancels the object thread, and make the
416 * object wait pipe (if it exists) readable. Not a cancellation point.
418 void __vlc_object_kill( vlc_object_t *p_this )
420 vlc_object_internals_t *priv = vlc_internals( p_this );
423 vlc_thread_cancel( p_this );
424 vlc_mutex_lock( &pipe_lock );
428 p_this->b_die = true;
431 /* This also serves as a memory barrier toward vlc_object_alive(): */
432 vlc_mutex_unlock( &pipe_lock );
436 int canc = vlc_savecancel ();
438 /* write _after_ setting b_die, so vlc_object_alive() returns false */
439 write (fd, &(uint64_t){ 1 }, sizeof (uint64_t));
440 msg_Dbg (p_this, "waitpipe: object killed");
441 vlc_restorecancel (canc);
446 /*****************************************************************************
447 * find a typed object and increment its refcount
448 *****************************************************************************
449 * This function recursively looks for a given object type. i_mode can be one
450 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
451 *****************************************************************************/
452 void * __vlc_object_find( vlc_object_t *p_this, int i_type, int i_mode )
454 vlc_object_t *p_found;
456 /* If we are of the requested type ourselves, don't look further */
457 if( !(i_mode & FIND_STRICT)
458 && vlc_internals (p_this)->i_object_type == i_type )
460 vlc_object_hold( p_this );
464 /* Otherwise, recursively look for the object */
465 if ((i_mode & 0x000f) == FIND_ANYWHERE)
466 return vlc_object_find (p_this->p_libvlc, i_type,
467 (i_mode & ~0x000f)|FIND_CHILD);
469 libvlc_lock (p_this->p_libvlc);
470 p_found = FindObject( p_this, i_type, i_mode );
471 libvlc_unlock (p_this->p_libvlc);
476 static int objnamecmp(const vlc_object_t *obj, const char *name)
478 char *objname = vlc_object_get_name(obj);
482 int ret = strcmp (objname, name);
487 #undef vlc_object_find_name
489 * Finds a named object and increment its reference count.
490 * Beware that objects found in this manner can be "owned" by another thread,
491 * be of _any_ type, and be attached to any module (if any). With such an
492 * object reference, you can set or get object variables, emit log messages,
493 * and read write-once object parameters (psz_object_type, etc).
494 * You CANNOT cast the object to a more specific object type, and you
495 * definitely cannot invoke object type-specific callbacks with this.
497 * @param p_this object to search from
498 * @param psz_name name of the object to search for
499 * @param i_mode search direction: FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
501 * @return a matching object (must be released by the caller),
504 vlc_object_t *vlc_object_find_name( vlc_object_t *p_this,
505 const char *psz_name, int i_mode )
507 vlc_object_t *p_found;
509 /* Reading psz_object_name from a separate inhibits thread-safety.
510 * Use a libvlc address variable instead for that sort of things! */
511 msg_Warn( p_this, "%s(%s) is not safe!", __func__, psz_name );
512 /* If have the requested name ourselves, don't look further */
513 if( !(i_mode & FIND_STRICT) && !objnamecmp(p_this, psz_name) )
515 vlc_object_hold( p_this );
519 libvlc_lock (p_this->p_libvlc);
521 /* Otherwise, recursively look for the object */
522 if( (i_mode & 0x000f) == FIND_ANYWHERE )
524 vlc_object_t *p_root = p_this;
527 while( p_root->p_parent != NULL &&
528 p_root != VLC_OBJECT( p_this->p_libvlc ) )
530 p_root = p_root->p_parent;
533 p_found = FindObjectName( p_root, psz_name,
534 (i_mode & ~0x000f)|FIND_CHILD );
535 if( p_found == NULL && p_root != VLC_OBJECT( p_this->p_libvlc ) )
537 p_found = FindObjectName( VLC_OBJECT( p_this->p_libvlc ),
538 psz_name, (i_mode & ~0x000f)|FIND_CHILD );
543 p_found = FindObjectName( p_this, psz_name, i_mode );
546 libvlc_unlock (p_this->p_libvlc);
551 * Increment an object reference counter.
553 void * __vlc_object_hold( vlc_object_t *p_this )
555 vlc_object_internals_t *internals = vlc_internals( p_this );
557 vlc_spin_lock( &internals->ref_spin );
558 /* Avoid obvious freed object uses */
559 assert( internals->i_refcount > 0 );
560 /* Increment the counter */
561 internals->i_refcount++;
562 vlc_spin_unlock( &internals->ref_spin );
566 /*****************************************************************************
567 * Decrement an object refcount
568 * And destroy the object if its refcount reach zero.
569 *****************************************************************************/
570 void __vlc_object_release( vlc_object_t *p_this )
572 vlc_object_internals_t *internals = vlc_internals( p_this );
573 vlc_object_t *parent = NULL;
574 bool b_should_destroy;
576 vlc_spin_lock( &internals->ref_spin );
577 assert( internals->i_refcount > 0 );
579 if( internals->i_refcount > 1 )
582 /* There are still other references to the object */
583 internals->i_refcount--;
584 vlc_spin_unlock( &internals->ref_spin );
587 vlc_spin_unlock( &internals->ref_spin );
590 /* Remember that we cannot hold the spin while waiting on the mutex */
591 libvlc_lock (p_this->p_libvlc);
592 /* Take the spin again. Note that another thread may have held the
593 * object in the (very short) mean time. */
594 vlc_spin_lock( &internals->ref_spin );
595 b_should_destroy = --internals->i_refcount == 0;
596 vlc_spin_unlock( &internals->ref_spin );
598 if( b_should_destroy )
600 parent = p_this->p_parent;
602 /* Detach from parent to protect against FIND_CHILDREN */
603 vlc_object_detach_unlocked (p_this);
605 /* We have no children */
606 assert (internals->i_children == 0);
608 libvlc_unlock (p_this->p_libvlc);
610 if( b_should_destroy )
614 canc = vlc_savecancel ();
615 vlc_object_destroy( p_this );
616 vlc_restorecancel (canc);
618 vlc_object_release (parent);
623 ****************************************************************************
624 * attach object to a parent object
625 *****************************************************************************
626 * This function sets p_this as a child of p_parent, and p_parent as a parent
627 * of p_this. This link can be undone using vlc_object_detach.
628 *****************************************************************************/
629 void __vlc_object_attach( vlc_object_t *p_this, vlc_object_t *p_parent )
631 if( !p_this ) return;
633 vlc_object_hold (p_parent);
634 libvlc_lock (p_this->p_libvlc);
636 /* Attach the parent to its child */
637 assert (!p_this->p_parent);
638 p_this->p_parent = p_parent;
640 /* Attach the child to its parent */
641 vlc_object_internals_t *priv = vlc_internals( p_parent );
642 INSERT_ELEM( priv->pp_children, priv->i_children, priv->i_children,
644 libvlc_unlock (p_this->p_libvlc);
648 static void vlc_object_detach_unlocked (vlc_object_t *p_this)
650 if (p_this->p_parent == NULL)
653 vlc_object_internals_t *priv = vlc_internals( p_this->p_parent );
657 /* Remove p_this's parent */
658 p_this->p_parent = NULL;
660 /* Remove all of p_parent's children which are p_this */
661 for( i_index = priv->i_children ; i_index-- ; )
663 if( priv->pp_children[i_index] == p_this )
666 for( i = i_index ; i < priv->i_children ; i++ )
667 priv->pp_children[i] = priv->pp_children[i+1];
671 if( priv->i_children )
673 vlc_object_t **pp_children = (vlc_object_t **)
674 realloc( priv->pp_children,
675 priv->i_children * sizeof(vlc_object_t *) );
677 priv->pp_children = pp_children;
681 /* Special case - don't realloc() to zero to avoid leaking */
682 free( priv->pp_children );
683 priv->pp_children = NULL;
689 ****************************************************************************
690 * detach object from its parent
691 *****************************************************************************
692 * This function removes all links between an object and its parent.
693 *****************************************************************************/
694 void __vlc_object_detach( vlc_object_t *p_this )
696 vlc_object_t *p_parent;
697 if( !p_this ) return;
699 libvlc_lock (p_this->p_libvlc);
700 p_parent = p_this->p_parent;
702 vlc_object_detach_unlocked( p_this );
703 libvlc_unlock (p_this->p_libvlc);
706 vlc_object_release (p_parent);
711 ****************************************************************************
712 * find a list typed objects and increment their refcount
713 *****************************************************************************
714 * This function recursively looks for a given object type. i_mode can be one
715 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
716 *****************************************************************************/
717 vlc_list_t * vlc_list_find( vlc_object_t *p_this, int i_type, int i_mode )
722 /* Look for the objects */
723 switch( i_mode & 0x000f )
726 return vlc_list_find (VLC_OBJECT(p_this->p_libvlc), i_type, FIND_CHILD);
729 libvlc_lock (p_this->p_libvlc);
730 i_count = CountChildren( p_this, i_type );
731 p_list = NewList( i_count );
733 /* Check allocation was successful */
734 if( p_list->i_count != i_count )
736 libvlc_unlock (p_this->p_libvlc);
742 ListChildren( p_list, p_this, i_type );
743 libvlc_unlock (p_this->p_libvlc);
747 msg_Err( p_this, "unimplemented!" );
748 p_list = NewList( 0 );
756 * Gets the list of children of an objects, and increment their reference
758 * @return a list (possibly empty) or NULL in case of error.
760 vlc_list_t *__vlc_list_children( vlc_object_t *obj )
763 vlc_object_internals_t *priv = vlc_internals( obj );
765 libvlc_lock (obj->p_libvlc);
766 l = NewList( priv->i_children );
767 for (int i = 0; i < l->i_count; i++)
769 vlc_object_hold( priv->pp_children[i] );
770 l->p_values[i].p_object = priv->pp_children[i];
772 libvlc_unlock (obj->p_libvlc);
776 /*****************************************************************************
777 * DumpCommand: print the current vlc structure
778 *****************************************************************************
779 * This function prints either an ASCII tree showing the connections between
780 * vlc objects, and additional information such as their refcount, thread ID,
781 * etc. (command "tree"), or the same data as a simple list (command "list").
782 *****************************************************************************/
783 static int DumpCommand( vlc_object_t *p_this, char const *psz_cmd,
784 vlc_value_t oldval, vlc_value_t newval, void *p_data )
786 (void)oldval; (void)p_data;
787 vlc_object_t *p_object = NULL;
789 if( *newval.psz_string )
791 /* try using the object's name to find it */
792 p_object = vlc_object_find_name( p_this, newval.psz_string,
800 libvlc_lock (p_this->p_libvlc);
801 if( *psz_cmd == 't' )
803 char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
806 p_object = VLC_OBJECT(p_this->p_libvlc);
809 DumpStructure( p_object, 0, psz_foo );
811 else if( *psz_cmd == 'v' )
816 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
818 PrintObject( p_object, "" );
820 vlc_mutex_lock( &vlc_internals( p_object )->var_lock );
821 if( !vlc_internals( p_object )->i_vars )
822 printf( " `-o No variables\n" );
823 for( i = 0; i < vlc_internals( p_object )->i_vars; i++ )
825 variable_t *p_var = vlc_internals( p_object )->p_vars + i;
826 const char *psz_type = "unknown";
828 switch( p_var->i_type & VLC_VAR_TYPE )
830 #define MYCASE( type, nice ) \
831 case VLC_VAR_ ## type: \
834 MYCASE( VOID, "void" );
835 MYCASE( BOOL, "bool" );
836 MYCASE( INTEGER, "integer" );
837 MYCASE( HOTKEY, "hotkey" );
838 MYCASE( STRING, "string" );
839 MYCASE( MODULE, "module" );
840 MYCASE( FILE, "file" );
841 MYCASE( DIRECTORY, "directory" );
842 MYCASE( VARIABLE, "variable" );
843 MYCASE( FLOAT, "float" );
844 MYCASE( TIME, "time" );
845 MYCASE( ADDRESS, "address" );
846 MYCASE( MUTEX, "mutex" );
847 MYCASE( LIST, "list" );
850 printf( " %c-o \"%s\" (%s",
851 i + 1 == vlc_internals( p_object )->i_vars ? '`' : '|',
852 p_var->psz_name, psz_type );
853 if( p_var->psz_text )
854 printf( ", %s", p_var->psz_text );
856 if( p_var->i_type & VLC_VAR_HASCHOICE )
857 printf( ", has choices" );
858 if( p_var->i_type & VLC_VAR_ISCOMMAND )
859 printf( ", command" );
860 if( p_var->i_entries )
861 printf( ", %d callbacks", p_var->i_entries );
862 switch( p_var->i_type & VLC_VAR_CLASS )
868 printf( ": %s", p_var->val.b_bool ? "true" : "false" );
870 case VLC_VAR_INTEGER:
871 printf( ": %d", p_var->val.i_int );
874 printf( ": \"%s\"", p_var->val.psz_string );
877 printf( ": %f", p_var->val.f_float );
880 printf( ": %"PRIi64, (int64_t)p_var->val.i_time );
882 case VLC_VAR_ADDRESS:
883 printf( ": %p", p_var->val.p_address );
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 )
928 switch( i_mode & 0x000f )
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,
981 switch( i_mode & 0x000f )
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 = (vlc_list_t *)malloc( sizeof( vlc_list_t ) );
1127 if( p_list == NULL )
1132 p_list->i_count = i_count;
1136 p_list->p_values = NULL;
1140 p_list->p_values = malloc( i_count * sizeof( vlc_value_t ) );
1141 if( p_list->p_values == NULL )
1143 p_list->i_count = 0;
1150 static void ListReplace( vlc_list_t *p_list, vlc_object_t *p_object,
1153 if( p_list == NULL || i_index >= p_list->i_count )
1158 vlc_object_hold( p_object );
1160 p_list->p_values[i_index].p_object = p_object;
1165 /*static void ListAppend( vlc_list_t *p_list, vlc_object_t *p_object )
1167 if( p_list == NULL )
1172 p_list->p_values = realloc( p_list->p_values, (p_list->i_count + 1)
1173 * sizeof( vlc_value_t ) );
1174 if( p_list->p_values == NULL )
1176 p_list->i_count = 0;
1180 vlc_object_hold( p_object );
1182 p_list->p_values[p_list->i_count].p_object = p_object;
1188 static int CountChildren( vlc_object_t *p_this, int i_type )
1190 vlc_object_t *p_tmp;
1193 for( i = 0; i < vlc_internals( p_this )->i_children; i++ )
1195 p_tmp = vlc_internals( p_this )->pp_children[i];
1197 if( vlc_internals( p_tmp )->i_object_type == i_type )
1201 i_count += CountChildren( p_tmp, i_type );
1207 static void ListChildren( vlc_list_t *p_list, vlc_object_t *p_this, int i_type )
1209 vlc_object_t *p_tmp;
1212 for( i = 0; i < vlc_internals( p_this )->i_children; i++ )
1214 p_tmp = vlc_internals( p_this )->pp_children[i];
1216 if( vlc_internals( p_tmp )->i_object_type == i_type )
1217 ListReplace( p_list, p_tmp, p_list->i_count++ );
1219 ListChildren( p_list, p_tmp, i_type );