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 static void vlc_object_dump( vlc_object_t *p_this );
88 /*****************************************************************************
89 * Local structure lock
90 *****************************************************************************/
91 static void libvlc_lock (libvlc_int_t *p_libvlc)
93 vlc_mutex_lock (&(libvlc_priv (p_libvlc)->structure_lock));
96 static void libvlc_unlock (libvlc_int_t *p_libvlc)
98 vlc_mutex_unlock (&(libvlc_priv (p_libvlc)->structure_lock));
101 void *__vlc_custom_create( vlc_object_t *p_this, size_t i_size,
102 int i_type, const char *psz_type )
105 vlc_object_internals_t *p_priv;
108 * VLC objects are laid out as follow:
109 * - first the LibVLC-private per-object data,
110 * - then VLC_COMMON members from vlc_object_t,
111 * - finally, the type-specific data (if any).
113 * This function initializes the LibVLC and common data,
114 * and zeroes the rest.
116 p_priv = calloc( 1, sizeof( *p_priv ) + i_size );
120 assert (i_size >= sizeof (vlc_object_t));
121 p_new = (vlc_object_t *)(p_priv + 1);
123 p_priv->i_object_type = i_type;
124 p_new->psz_object_type = psz_type;
125 p_priv->psz_name = NULL;
127 p_new->b_die = false;
128 p_new->b_error = false;
129 p_new->b_force = false;
131 p_new->psz_header = NULL;
134 p_new->i_flags = p_this->i_flags
135 & (OBJECT_FLAGS_NODBG|OBJECT_FLAGS_QUIET|OBJECT_FLAGS_NOINTERACT);
137 p_priv->p_vars = calloc( 16, sizeof( variable_t ) );
139 if( !p_priv->p_vars )
147 libvlc_int_t *self = (libvlc_int_t*)p_new;
148 p_new->p_libvlc = self;
149 vlc_mutex_init (&(libvlc_priv (self)->structure_lock));
150 p_this = p_priv->next = p_priv->prev = p_new;
153 p_new->p_libvlc = p_this->p_libvlc;
155 vlc_spin_init( &p_priv->ref_spin );
156 p_priv->i_refcount = 1;
157 p_priv->pf_destructor = NULL;
158 p_priv->b_thread = false;
159 p_new->p_parent = NULL;
160 p_priv->pp_children = NULL;
161 p_priv->i_children = 0;
163 /* Initialize mutexes and condvars */
164 vlc_mutex_init( &p_priv->var_lock );
165 vlc_cond_init( &p_priv->var_wait );
166 p_priv->pipes[0] = p_priv->pipes[1] = -1;
168 p_priv->next = p_this;
169 libvlc_lock (p_new->p_libvlc);
170 p_priv->prev = vlc_internals (p_this)->prev;
171 vlc_internals (p_this)->prev = p_new;
172 vlc_internals (p_priv->prev)->next = p_new;
173 libvlc_unlock (p_new->p_libvlc);
175 if (p_new == VLC_OBJECT(p_new->p_libvlc))
176 { /* TODO: should be in src/libvlc.c */
177 int canc = vlc_savecancel ();
178 var_Create( p_new, "list", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
179 var_AddCallback( p_new, "list", DumpCommand, NULL );
180 var_Create( p_new, "tree", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
181 var_AddCallback( p_new, "tree", DumpCommand, NULL );
182 var_Create( p_new, "vars", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
183 var_AddCallback( p_new, "vars", DumpCommand, NULL );
184 vlc_restorecancel (canc);
192 * Allocates and initializes a vlc object.
194 * @param i_type known object type (all of them are negative integer values),
195 * or object byte size (always positive).
197 * @return the new object, or NULL on error.
199 void * __vlc_object_create( vlc_object_t *p_this, int i_type )
201 const char * psz_type;
206 case VLC_OBJECT_DECODER:
207 i_size = sizeof(decoder_t);
208 psz_type = "decoder";
210 case VLC_OBJECT_AOUT:
211 i_size = sizeof(aout_instance_t);
212 psz_type = "audio output";
215 assert( i_type > 0 ); /* unknown type?! */
217 i_type = VLC_OBJECT_GENERIC;
218 psz_type = "generic";
222 return vlc_custom_create( p_this, i_size, i_type, psz_type );
227 ****************************************************************************
228 * Set the destructor of a vlc object
230 * This function sets the destructor of the vlc object. It will be called
231 * when the object is destroyed when the its refcount reaches 0.
232 * (It is called by the internal function vlc_object_destroy())
233 *****************************************************************************/
234 void __vlc_object_set_destructor( vlc_object_t *p_this,
235 vlc_destructor_t pf_destructor )
237 vlc_object_internals_t *p_priv = vlc_internals(p_this );
239 vlc_spin_lock( &p_priv->ref_spin );
240 p_priv->pf_destructor = pf_destructor;
241 vlc_spin_unlock( &p_priv->ref_spin );
244 #undef vlc_object_set_name
245 int vlc_object_set_name(vlc_object_t *obj, const char *name)
247 vlc_object_internals_t *priv = vlc_internals(obj);
249 /* Object must be named before it is attached (or never) */
250 assert(obj->p_parent == NULL);
251 assert(priv->i_children == 0);
253 free(priv->psz_name);
254 priv->psz_name = name ? strdup(name) : NULL;
255 return (priv->psz_name || !name) ? VLC_SUCCESS : VLC_ENOMEM;
258 #undef vlc_object_get_name
259 const char *vlc_object_get_name(const vlc_object_t *obj)
261 vlc_object_internals_t *priv = vlc_internals(obj);
263 return priv->psz_name;
267 ****************************************************************************
268 * Destroy a vlc object (Internal)
270 * This function destroys an object that has been previously allocated with
271 * vlc_object_create. The object's refcount must be zero and it must not be
272 * attached to other objects in any way.
274 * This function must be called with cancellation disabled (currently).
275 *****************************************************************************/
276 static void vlc_object_destroy( vlc_object_t *p_this )
278 vlc_object_internals_t *p_priv = vlc_internals( p_this );
280 /* Objects are always detached beforehand */
281 assert( !p_this->p_parent );
283 /* Send a kill to the object's thread if applicable */
284 vlc_object_kill( p_this );
286 /* Call the custom "subclass" destructor */
287 if( p_priv->pf_destructor )
288 p_priv->pf_destructor( p_this );
290 /* Any thread must have been cleaned up at this point. */
291 assert( !p_priv->b_thread );
293 /* Destroy the associated variables, starting from the end so that
294 * no memmove calls have to be done. */
295 while( p_priv->i_vars )
297 var_Destroy( p_this, p_priv->p_vars[p_priv->i_vars - 1].psz_name );
300 free( p_priv->p_vars );
301 vlc_cond_destroy( &p_priv->var_wait );
302 vlc_mutex_destroy( &p_priv->var_lock );
304 free( p_this->psz_header );
306 free( p_priv->psz_name );
308 vlc_spin_destroy( &p_priv->ref_spin );
309 if( p_priv->pipes[1] != -1 && p_priv->pipes[1] != p_priv->pipes[0] )
310 close( p_priv->pipes[1] );
311 if( p_priv->pipes[0] != -1 )
312 close( p_priv->pipes[0] );
313 if( VLC_OBJECT(p_this->p_libvlc) == p_this )
314 vlc_mutex_destroy (&(libvlc_priv ((libvlc_int_t *)p_this)->structure_lock));
321 # include <winsock2.h>
322 # include <ws2tcpip.h>
325 * select()-able pipes emulated using Winsock
327 static int pipe (int fd[2])
330 int addrlen = sizeof (addr);
332 SOCKET l = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP), a,
333 c = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
334 if ((l == INVALID_SOCKET) || (c == INVALID_SOCKET))
337 memset (&addr, 0, sizeof (addr));
338 addr.sin_family = AF_INET;
339 addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
340 if (bind (l, (PSOCKADDR)&addr, sizeof (addr))
341 || getsockname (l, (PSOCKADDR)&addr, &addrlen)
343 || connect (c, (PSOCKADDR)&addr, addrlen))
346 a = accept (l, NULL, NULL);
347 if (a == INVALID_SOCKET)
358 if (l != INVALID_SOCKET)
360 if (c != INVALID_SOCKET)
366 #define read( a, b, c ) recv (a, b, c, 0)
368 #define write( a, b, c ) send (a, b, c, 0)
370 #define close( a ) closesocket (a)
373 static vlc_mutex_t pipe_lock = VLC_STATIC_MUTEX;
376 * Returns the readable end of a pipe that becomes readable once termination
377 * of the object is requested (vlc_object_kill()).
378 * This can be used to wake-up out of a select() or poll() event loop, such
379 * typically when doing network I/O.
381 * Note that the pipe will remain the same for the lifetime of the object.
382 * DO NOT read the pipe nor close it yourself. Ever.
384 * @param obj object that would be "killed"
385 * @return a readable pipe descriptor, or -1 on error.
387 int vlc_object_waitpipe( vlc_object_t *obj )
389 vlc_object_internals_t *internals = vlc_internals( obj );
391 vlc_mutex_lock (&pipe_lock);
392 if (internals->pipes[0] == -1)
394 /* This can only ever happen if someone killed us without locking: */
395 assert (internals->pipes[1] == -1);
398 internals->pipes[0] = internals->pipes[1] = eventfd (0, 0);
399 if (internals->pipes[0] == -1)
402 if (pipe (internals->pipes))
403 internals->pipes[0] = internals->pipes[1] = -1;
406 if (internals->pipes[0] != -1 && obj->b_die)
407 { /* Race condition: vlc_object_kill() already invoked! */
408 msg_Dbg (obj, "waitpipe: object already dying");
409 write (internals->pipes[1], &(uint64_t){ 1 }, sizeof (uint64_t));
412 vlc_mutex_unlock (&pipe_lock);
413 return internals->pipes[0];
418 * Requests termination of an object, cancels the object thread, and make the
419 * object wait pipe (if it exists) readable. Not a cancellation point.
421 void __vlc_object_kill( vlc_object_t *p_this )
423 vlc_object_internals_t *priv = vlc_internals( p_this );
426 vlc_thread_cancel( p_this );
427 vlc_mutex_lock( &pipe_lock );
431 p_this->b_die = true;
434 /* This also serves as a memory barrier toward vlc_object_alive(): */
435 vlc_mutex_unlock( &pipe_lock );
439 int canc = vlc_savecancel ();
441 /* write _after_ setting b_die, so vlc_object_alive() returns false */
442 write (fd, &(uint64_t){ 1 }, sizeof (uint64_t));
443 msg_Dbg (p_this, "waitpipe: object killed");
444 vlc_restorecancel (canc);
449 /*****************************************************************************
450 * find a typed object and increment its refcount
451 *****************************************************************************
452 * This function recursively looks for a given object type. i_mode can be one
453 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
454 *****************************************************************************/
455 void * __vlc_object_find( vlc_object_t *p_this, int i_type, int i_mode )
457 vlc_object_t *p_found;
459 /* If we are of the requested type ourselves, don't look further */
460 if( !(i_mode & FIND_STRICT)
461 && vlc_internals (p_this)->i_object_type == i_type )
463 vlc_object_hold( p_this );
467 /* Otherwise, recursively look for the object */
468 if ((i_mode & 0x000f) == FIND_ANYWHERE)
469 return vlc_object_find (p_this->p_libvlc, i_type,
470 (i_mode & ~0x000f)|FIND_CHILD);
472 libvlc_lock (p_this->p_libvlc);
473 p_found = FindObject( p_this, i_type, i_mode );
474 libvlc_unlock (p_this->p_libvlc);
479 static int objnamecmp(const vlc_object_t *obj, const char *name)
481 if (!vlc_object_get_name(obj))
483 return strcmp( vlc_object_get_name(obj), 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( !(i_mode & FIND_STRICT) && !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 & 0x000f) == 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,
533 (i_mode & ~0x000f)|FIND_CHILD );
534 if( p_found == NULL && p_root != VLC_OBJECT( p_this->p_libvlc ) )
536 p_found = FindObjectName( VLC_OBJECT( p_this->p_libvlc ),
537 psz_name, (i_mode & ~0x000f)|FIND_CHILD );
542 p_found = FindObjectName( p_this, psz_name, i_mode );
545 libvlc_unlock (p_this->p_libvlc);
550 * Increment an object reference counter.
552 void * __vlc_object_hold( vlc_object_t *p_this )
554 vlc_object_internals_t *internals = vlc_internals( p_this );
556 vlc_spin_lock( &internals->ref_spin );
557 /* Avoid obvious freed object uses */
558 assert( internals->i_refcount > 0 );
559 /* Increment the counter */
560 internals->i_refcount++;
561 vlc_spin_unlock( &internals->ref_spin );
565 /*****************************************************************************
566 * Decrement an object refcount
567 * And destroy the object if its refcount reach zero.
568 *****************************************************************************/
569 void __vlc_object_release( vlc_object_t *p_this )
571 vlc_object_internals_t *internals = vlc_internals( p_this );
572 vlc_object_t *parent = NULL;
573 bool b_should_destroy;
575 vlc_spin_lock( &internals->ref_spin );
576 assert( internals->i_refcount > 0 );
578 if( internals->i_refcount > 1 )
581 /* There are still other references to the object */
582 internals->i_refcount--;
583 vlc_spin_unlock( &internals->ref_spin );
586 vlc_spin_unlock( &internals->ref_spin );
589 /* Remember that we cannot hold the spin while waiting on the mutex */
590 libvlc_lock (p_this->p_libvlc);
591 /* Take the spin again. Note that another thread may have held the
592 * object in the (very short) mean time. */
593 vlc_spin_lock( &internals->ref_spin );
594 b_should_destroy = --internals->i_refcount == 0;
595 vlc_spin_unlock( &internals->ref_spin );
597 if( b_should_destroy )
599 parent = p_this->p_parent;
602 if( VLC_OBJECT(p_this->p_libvlc) == p_this )
605 vlc_object_t *leaked = internals->next;
606 while( leaked != p_this )
608 /* We are leaking this object */
610 "ERROR: leaking object (%p, type:%s, name:%s)\n",
611 leaked, leaked->psz_object_type,
612 vlc_object_get_name(leaked) );
613 /* Dump object to ease debugging */
614 vlc_object_dump( leaked );
616 leaked = vlc_internals (leaked)->next;
619 if( internals->next != p_this )
620 /* Dump libvlc object to ease debugging */
621 vlc_object_dump( p_this );
624 /* Remove the object from object list
625 * so that it cannot be encountered by vlc_object_get() */
626 vlc_internals (internals->next)->prev = internals->prev;
627 vlc_internals (internals->prev)->next = internals->next;
630 /* Detach from parent to protect against FIND_CHILDREN */
631 vlc_object_detach_unlocked (p_this);
633 /* We have no children */
634 assert (internals->i_children == 0);
636 libvlc_unlock (p_this->p_libvlc);
638 if( b_should_destroy )
642 canc = vlc_savecancel ();
643 vlc_object_destroy( p_this );
644 vlc_restorecancel (canc);
646 vlc_object_release (parent);
651 ****************************************************************************
652 * attach object to a parent object
653 *****************************************************************************
654 * This function sets p_this as a child of p_parent, and p_parent as a parent
655 * of p_this. This link can be undone using vlc_object_detach.
656 *****************************************************************************/
657 void __vlc_object_attach( vlc_object_t *p_this, vlc_object_t *p_parent )
659 if( !p_this ) return;
661 vlc_object_hold (p_parent);
662 libvlc_lock (p_this->p_libvlc);
664 /* Attach the parent to its child */
665 assert (!p_this->p_parent);
666 p_this->p_parent = p_parent;
668 /* Attach the child to its parent */
669 vlc_object_internals_t *priv = vlc_internals( p_parent );
670 INSERT_ELEM( priv->pp_children, priv->i_children, priv->i_children,
672 libvlc_unlock (p_this->p_libvlc);
676 static void vlc_object_detach_unlocked (vlc_object_t *p_this)
678 if (p_this->p_parent == NULL)
681 vlc_object_internals_t *priv = vlc_internals( p_this->p_parent );
685 /* Remove p_this's parent */
686 p_this->p_parent = NULL;
688 /* Remove all of p_parent's children which are p_this */
689 for( i_index = priv->i_children ; i_index-- ; )
691 if( priv->pp_children[i_index] == p_this )
694 for( i = i_index ; i < priv->i_children ; i++ )
695 priv->pp_children[i] = priv->pp_children[i+1];
699 if( priv->i_children )
701 vlc_object_t **pp_children = (vlc_object_t **)
702 realloc( priv->pp_children,
703 priv->i_children * sizeof(vlc_object_t *) );
705 priv->pp_children = pp_children;
709 /* Special case - don't realloc() to zero to avoid leaking */
710 free( priv->pp_children );
711 priv->pp_children = NULL;
717 ****************************************************************************
718 * detach object from its parent
719 *****************************************************************************
720 * This function removes all links between an object and its parent.
721 *****************************************************************************/
722 void __vlc_object_detach( vlc_object_t *p_this )
724 vlc_object_t *p_parent;
725 if( !p_this ) return;
727 libvlc_lock (p_this->p_libvlc);
728 p_parent = p_this->p_parent;
730 vlc_object_detach_unlocked( p_this );
731 libvlc_unlock (p_this->p_libvlc);
734 vlc_object_release (p_parent);
739 ****************************************************************************
740 * find a list typed objects and increment their refcount
741 *****************************************************************************
742 * This function recursively looks for a given object type. i_mode can be one
743 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
744 *****************************************************************************/
745 vlc_list_t * vlc_list_find( vlc_object_t *p_this, int i_type, int i_mode )
750 /* Look for the objects */
751 switch( i_mode & 0x000f )
754 return vlc_list_find (VLC_OBJECT(p_this->p_libvlc), i_type, FIND_CHILD);
757 libvlc_lock (p_this->p_libvlc);
758 i_count = CountChildren( p_this, i_type );
759 p_list = NewList( i_count );
761 /* Check allocation was successful */
762 if( p_list->i_count != i_count )
764 libvlc_unlock (p_this->p_libvlc);
770 ListChildren( p_list, p_this, i_type );
771 libvlc_unlock (p_this->p_libvlc);
775 msg_Err( p_this, "unimplemented!" );
776 p_list = NewList( 0 );
784 * Gets the list of children of an objects, and increment their reference
786 * @return a list (possibly empty) or NULL in case of error.
788 vlc_list_t *__vlc_list_children( vlc_object_t *obj )
791 vlc_object_internals_t *priv = vlc_internals( obj );
793 libvlc_lock (obj->p_libvlc);
794 l = NewList( priv->i_children );
795 for (int i = 0; i < l->i_count; i++)
797 vlc_object_hold( priv->pp_children[i] );
798 l->p_values[i].p_object = priv->pp_children[i];
800 libvlc_unlock (obj->p_libvlc);
804 /*****************************************************************************
805 * DumpCommand: print the current vlc structure
806 *****************************************************************************
807 * This function prints either an ASCII tree showing the connections between
808 * vlc objects, and additional information such as their refcount, thread ID,
809 * etc. (command "tree"), or the same data as a simple list (command "list").
810 *****************************************************************************/
811 static int DumpCommand( vlc_object_t *p_this, char const *psz_cmd,
812 vlc_value_t oldval, vlc_value_t newval, void *p_data )
814 libvlc_int_t *p_libvlc = p_this->p_libvlc;
816 (void)oldval; (void)p_data;
817 if( *psz_cmd == 'l' )
819 vlc_object_t *cur = VLC_OBJECT (p_libvlc);
821 libvlc_lock (p_this->p_libvlc);
824 PrintObject (cur, "");
825 cur = vlc_internals (cur)->next;
827 while (cur != VLC_OBJECT(p_libvlc));
828 libvlc_unlock (p_this->p_libvlc);
832 vlc_object_t *p_object = NULL;
834 if( *newval.psz_string )
836 /* try using the object's name to find it */
837 p_object = vlc_object_find_name( p_this, newval.psz_string,
845 libvlc_lock (p_this->p_libvlc);
846 if( *psz_cmd == 't' )
848 char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
851 p_object = VLC_OBJECT(p_this->p_libvlc);
854 DumpStructure( p_object, 0, psz_foo );
856 else if( *psz_cmd == 'v' )
861 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
863 PrintObject( p_object, "" );
865 if( !vlc_internals( p_object )->i_vars )
866 printf( " `-o No variables\n" );
867 for( i = 0; i < vlc_internals( p_object )->i_vars; i++ )
869 variable_t *p_var = vlc_internals( p_object )->p_vars + i;
871 const char *psz_type = "unknown";
872 switch( p_var->i_type & VLC_VAR_TYPE )
874 #define MYCASE( type, nice ) \
875 case VLC_VAR_ ## type: \
878 MYCASE( VOID, "void" );
879 MYCASE( BOOL, "bool" );
880 MYCASE( INTEGER, "integer" );
881 MYCASE( HOTKEY, "hotkey" );
882 MYCASE( STRING, "string" );
883 MYCASE( MODULE, "module" );
884 MYCASE( FILE, "file" );
885 MYCASE( DIRECTORY, "directory" );
886 MYCASE( VARIABLE, "variable" );
887 MYCASE( FLOAT, "float" );
888 MYCASE( TIME, "time" );
889 MYCASE( ADDRESS, "address" );
890 MYCASE( MUTEX, "mutex" );
891 MYCASE( LIST, "list" );
894 printf( " %c-o \"%s\" (%s",
895 i + 1 == vlc_internals( p_object )->i_vars ? '`' : '|',
896 p_var->psz_name, psz_type );
897 if( p_var->psz_text )
898 printf( ", %s", p_var->psz_text );
900 if( p_var->i_type & VLC_VAR_HASCHOICE )
901 printf( ", has choices" );
902 if( p_var->i_type & VLC_VAR_ISCOMMAND )
903 printf( ", command" );
904 if( p_var->i_entries )
905 printf( ", %d callbacks", p_var->i_entries );
906 switch( p_var->i_type & VLC_VAR_CLASS )
912 printf( ": %s", p_var->val.b_bool ? "true" : "false" );
914 case VLC_VAR_INTEGER:
915 printf( ": %d", p_var->val.i_int );
918 printf( ": \"%s\"", p_var->val.psz_string );
921 printf( ": %f", p_var->val.f_float );
924 printf( ": %"PRIi64, (int64_t)p_var->val.i_time );
926 case VLC_VAR_ADDRESS:
927 printf( ": %p", p_var->val.p_address );
936 libvlc_unlock (p_this->p_libvlc);
938 if( *newval.psz_string )
940 vlc_object_release( p_object );
947 /*****************************************************************************
948 * vlc_list_release: free a list previously allocated by vlc_list_find
949 *****************************************************************************
950 * This function decreases the refcount of all objects in the list and
952 *****************************************************************************/
953 void vlc_list_release( vlc_list_t *p_list )
957 for( i_index = 0; i_index < p_list->i_count; i_index++ )
959 vlc_object_release( p_list->p_values[i_index].p_object );
962 free( p_list->p_values );
966 /*****************************************************************************
967 * dump an object. (Debug function)
968 *****************************************************************************/
970 static void vlc_object_dump( vlc_object_t *p_this )
972 char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
975 DumpStructure( p_this, 0, psz_foo );
979 /* Following functions are local */
981 static vlc_object_t * FindObject( vlc_object_t *p_this, int i_type, int i_mode )
986 switch( i_mode & 0x000f )
989 p_tmp = p_this->p_parent;
992 if( vlc_internals( p_tmp )->i_object_type == i_type )
994 vlc_object_hold( p_tmp );
999 return FindObject( p_tmp, i_type, i_mode );
1005 for( i = vlc_internals( p_this )->i_children; i--; )
1007 p_tmp = vlc_internals( p_this )->pp_children[i];
1008 if( vlc_internals( p_tmp )->i_object_type == i_type )
1010 vlc_object_hold( p_tmp );
1013 else if( vlc_internals( p_tmp )->i_children )
1015 p_tmp = FindObject( p_tmp, i_type, i_mode );
1025 /* Handled in vlc_object_find */
1032 static vlc_object_t * FindObjectName( vlc_object_t *p_this,
1033 const char *psz_name,
1037 vlc_object_t *p_tmp;
1039 switch( i_mode & 0x000f )
1042 p_tmp = p_this->p_parent;
1045 if( !objnamecmp(p_tmp, psz_name) )
1047 vlc_object_hold( p_tmp );
1052 return FindObjectName( p_tmp, psz_name, i_mode );
1058 for( i = vlc_internals( p_this )->i_children; i--; )
1060 p_tmp = vlc_internals( p_this )->pp_children[i];
1061 if( !objnamecmp(p_tmp, psz_name ) )
1063 vlc_object_hold( p_tmp );
1066 else if( vlc_internals( p_tmp )->i_children )
1068 p_tmp = FindObjectName( p_tmp, psz_name, i_mode );
1078 /* Handled in vlc_object_find */
1086 static void PrintObject( vlc_object_t *p_this, const char *psz_prefix )
1088 char psz_children[20], psz_refcount[20], psz_thread[30], psz_name[50],
1091 int canc = vlc_savecancel ();
1092 memset( &psz_name, 0, sizeof(psz_name) );
1093 if( vlc_object_get_name(p_this) )
1095 snprintf( psz_name, 49, " \"%s\"",
1096 vlc_object_get_name(p_this) );
1098 psz_name[48] = '\"';
1101 psz_children[0] = '\0';
1102 switch( vlc_internals( p_this )->i_children )
1107 strcpy( psz_children, ", 1 child" );
1110 snprintf( psz_children, 19, ", %i children",
1111 vlc_internals( p_this )->i_children );
1115 psz_refcount[0] = '\0';
1116 if( vlc_internals( p_this )->i_refcount > 0 )
1117 snprintf( psz_refcount, 19, ", refcount %u",
1118 vlc_internals( p_this )->i_refcount );
1120 psz_thread[0] = '\0';
1121 if( vlc_internals( p_this )->b_thread )
1122 snprintf( psz_thread, 29, " (thread %lu)",
1123 (unsigned long)vlc_internals( p_this )->thread_id );
1125 psz_parent[0] = '\0';
1126 if( p_this->p_parent )
1127 snprintf( psz_parent, 19, ", parent %p", p_this->p_parent );
1129 printf( " %so %p %s%s%s%s%s%s\n", psz_prefix,
1130 p_this, p_this->psz_object_type,
1131 psz_name, psz_thread, psz_refcount, psz_children,
1133 vlc_restorecancel (canc);
1136 static void DumpStructure( vlc_object_t *p_this, int i_level, char *psz_foo )
1139 char i_back = psz_foo[i_level];
1140 psz_foo[i_level] = '\0';
1142 PrintObject( p_this, psz_foo );
1144 psz_foo[i_level] = i_back;
1146 if( i_level / 2 >= MAX_DUMPSTRUCTURE_DEPTH )
1148 msg_Warn( p_this, "structure tree is too deep" );
1152 for( i = 0 ; i < vlc_internals( p_this )->i_children ; i++ )
1156 psz_foo[i_level-1] = ' ';
1158 if( psz_foo[i_level-2] == '`' )
1160 psz_foo[i_level-2] = ' ';
1164 if( i == vlc_internals( p_this )->i_children - 1 )
1166 psz_foo[i_level] = '`';
1170 psz_foo[i_level] = '|';
1173 psz_foo[i_level+1] = '-';
1174 psz_foo[i_level+2] = '\0';
1176 DumpStructure( vlc_internals( p_this )->pp_children[i], i_level + 2,
1181 static vlc_list_t * NewList( int i_count )
1183 vlc_list_t * p_list = (vlc_list_t *)malloc( sizeof( vlc_list_t ) );
1184 if( p_list == NULL )
1189 p_list->i_count = i_count;
1193 p_list->p_values = NULL;
1197 p_list->p_values = malloc( i_count * sizeof( vlc_value_t ) );
1198 if( p_list->p_values == NULL )
1200 p_list->i_count = 0;
1207 static void ListReplace( vlc_list_t *p_list, vlc_object_t *p_object,
1210 if( p_list == NULL || i_index >= p_list->i_count )
1215 vlc_object_hold( p_object );
1217 p_list->p_values[i_index].p_object = p_object;
1222 /*static void ListAppend( vlc_list_t *p_list, vlc_object_t *p_object )
1224 if( p_list == NULL )
1229 p_list->p_values = realloc( p_list->p_values, (p_list->i_count + 1)
1230 * sizeof( vlc_value_t ) );
1231 if( p_list->p_values == NULL )
1233 p_list->i_count = 0;
1237 vlc_object_hold( p_object );
1239 p_list->p_values[p_list->i_count].p_object = p_object;
1245 static int CountChildren( vlc_object_t *p_this, int i_type )
1247 vlc_object_t *p_tmp;
1250 for( i = 0; i < vlc_internals( p_this )->i_children; i++ )
1252 p_tmp = vlc_internals( p_this )->pp_children[i];
1254 if( vlc_internals( p_tmp )->i_object_type == i_type )
1258 i_count += CountChildren( p_tmp, i_type );
1264 static void ListChildren( vlc_list_t *p_list, vlc_object_t *p_this, int i_type )
1266 vlc_object_t *p_tmp;
1269 for( i = 0; i < vlc_internals( p_this )->i_children; i++ )
1271 p_tmp = vlc_internals( p_this )->pp_children[i];
1273 if( vlc_internals( p_tmp )->i_object_type == i_type )
1274 ListReplace( p_list, p_tmp, p_list->i_count++ );
1276 ListChildren( p_list, p_tmp, i_type );