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 */
59 #if defined (HAVE_SYS_EVENTFD_H)
60 # include <sys/eventfd.h>
61 # define HAVE_EVENTFD 1
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_new->psz_object_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 p_new->p_private = NULL;
165 /* Initialize mutexes and condvars */
166 vlc_mutex_init( &p_priv->lock );
167 vlc_mutex_init( &p_priv->var_lock );
168 vlc_cond_init( &p_priv->var_wait );
169 p_priv->pipes[0] = p_priv->pipes[1] = -1;
171 p_priv->next = p_this;
172 libvlc_lock (p_new->p_libvlc);
173 p_priv->prev = vlc_internals (p_this)->prev;
174 vlc_internals (p_this)->prev = p_new;
175 vlc_internals (p_priv->prev)->next = p_new;
176 libvlc_unlock (p_new->p_libvlc);
178 if (p_new == VLC_OBJECT(p_new->p_libvlc))
179 { /* TODO: should be in src/libvlc.c */
180 int canc = vlc_savecancel ();
181 var_Create( p_new, "list", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
182 var_AddCallback( p_new, "list", DumpCommand, NULL );
183 var_Create( p_new, "tree", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
184 var_AddCallback( p_new, "tree", DumpCommand, NULL );
185 var_Create( p_new, "vars", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
186 var_AddCallback( p_new, "vars", DumpCommand, NULL );
187 vlc_restorecancel (canc);
195 * Allocates and initializes a vlc object.
197 * @param i_type known object type (all of them are negative integer values),
198 * or object byte size (always positive).
200 * @return the new object, or NULL on error.
202 void * __vlc_object_create( vlc_object_t *p_this, int i_type )
204 const char * psz_type;
209 case VLC_OBJECT_INTF:
210 i_size = sizeof(intf_thread_t);
211 psz_type = "interface";
213 case VLC_OBJECT_DECODER:
214 i_size = sizeof(decoder_t);
215 psz_type = "decoder";
217 case VLC_OBJECT_PACKETIZER:
218 i_size = sizeof(decoder_t);
219 psz_type = "packetizer";
221 case VLC_OBJECT_AOUT:
222 i_size = sizeof(aout_instance_t);
223 psz_type = "audio output";
226 assert( i_type > 0 ); /* unknown type?! */
228 i_type = VLC_OBJECT_GENERIC;
229 psz_type = "generic";
233 return vlc_custom_create( p_this, i_size, i_type, psz_type );
238 ****************************************************************************
239 * Set the destructor of a vlc object
241 * This function sets the destructor of the vlc object. It will be called
242 * when the object is destroyed when the its refcount reaches 0.
243 * (It is called by the internal function vlc_object_destroy())
244 *****************************************************************************/
245 void __vlc_object_set_destructor( vlc_object_t *p_this,
246 vlc_destructor_t pf_destructor )
248 vlc_object_internals_t *p_priv = vlc_internals(p_this );
249 p_priv->pf_destructor = pf_destructor;
253 ****************************************************************************
254 * Destroy a vlc object (Internal)
256 * This function destroys an object that has been previously allocated with
257 * vlc_object_create. The object's refcount must be zero and it must not be
258 * attached to other objects in any way.
260 * This function must be called with cancellation disabled (currently).
261 *****************************************************************************/
262 static void vlc_object_destroy( vlc_object_t *p_this )
264 vlc_object_internals_t *p_priv = vlc_internals( p_this );
266 /* Objects are always detached beforehand */
267 assert( !p_this->p_parent );
269 /* Send a kill to the object's thread if applicable */
270 vlc_object_kill( p_this );
272 /* Call the custom "subclass" destructor */
273 if( p_priv->pf_destructor )
274 p_priv->pf_destructor( p_this );
276 /* Any thread must have been cleaned up at this point. */
277 assert( !p_priv->b_thread );
279 /* Destroy the associated variables, starting from the end so that
280 * no memmove calls have to be done. */
281 while( p_priv->i_vars )
283 var_Destroy( p_this, p_priv->p_vars[p_priv->i_vars - 1].psz_name );
286 free( p_priv->p_vars );
287 vlc_cond_destroy( &p_priv->var_wait );
288 vlc_mutex_destroy( &p_priv->var_lock );
290 free( p_this->psz_header );
292 FREENULL( p_this->psz_object_name );
294 vlc_spin_destroy( &p_priv->ref_spin );
295 vlc_mutex_destroy( &p_priv->lock );
296 if( p_priv->pipes[1] != -1 && p_priv->pipes[1] != p_priv->pipes[0] )
297 close( p_priv->pipes[1] );
298 if( p_priv->pipes[0] != -1 )
299 close( p_priv->pipes[0] );
300 if( VLC_OBJECT(p_this->p_libvlc) == p_this )
301 vlc_mutex_destroy (&(libvlc_priv ((libvlc_int_t *)p_this)->structure_lock));
307 /** Inter-object signaling */
309 void __vlc_object_lock( vlc_object_t *obj )
311 vlc_mutex_lock( &(vlc_internals(obj)->lock) );
314 void __vlc_object_unlock( vlc_object_t *obj )
316 vlc_assert_locked( &(vlc_internals(obj)->lock) );
317 vlc_mutex_unlock( &(vlc_internals(obj)->lock) );
319 void __vlc_object_assert_locked( vlc_object_t *obj )
321 vlc_assert_locked( &(vlc_internals(obj)->lock) );
325 # include <winsock2.h>
326 # include <ws2tcpip.h>
329 * select()-able pipes emulated using Winsock
331 static int pipe (int fd[2])
334 int addrlen = sizeof (addr);
336 SOCKET l = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP), a,
337 c = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
338 if ((l == INVALID_SOCKET) || (c == INVALID_SOCKET))
341 memset (&addr, 0, sizeof (addr));
342 addr.sin_family = AF_INET;
343 addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
344 if (bind (l, (PSOCKADDR)&addr, sizeof (addr))
345 || getsockname (l, (PSOCKADDR)&addr, &addrlen)
347 || connect (c, (PSOCKADDR)&addr, addrlen))
350 a = accept (l, NULL, NULL);
351 if (a == INVALID_SOCKET)
362 if (l != INVALID_SOCKET)
364 if (c != INVALID_SOCKET)
370 #define read( a, b, c ) recv (a, b, c, 0)
372 #define write( a, b, c ) send (a, b, c, 0)
374 #define close( a ) closesocket (a)
378 * Returns the readable end of a pipe that becomes readable once termination
379 * of the object is requested (vlc_object_kill()).
380 * This can be used to wake-up out of a select() or poll() event loop, such
381 * typically when doing network I/O.
383 * Note that the pipe will remain the same for the lifetime of the object.
384 * DO NOT read the pipe nor close it yourself. Ever.
386 * @param obj object that would be "killed"
387 * @return a readable pipe descriptor, or -1 on error.
389 int vlc_object_waitpipe( vlc_object_t *obj )
391 vlc_object_internals_t *internals = vlc_internals( obj );
393 vlc_object_lock (obj);
394 if (internals->pipes[0] == -1)
396 /* This can only ever happen if someone killed us without locking: */
397 assert (internals->pipes[1] == -1);
400 internals->pipes[0] = internals->pipes[1] = eventfd (0, 0);
401 if (internals->pipes[0] == -1)
404 if (pipe (internals->pipes))
405 internals->pipes[0] = internals->pipes[1] = -1;
408 if (internals->pipes[0] != -1 && obj->b_die)
409 { /* Race condition: vlc_object_kill() already invoked! */
410 msg_Dbg (obj, "waitpipe: object already dying");
411 write (internals->pipes[1], &(uint64_t){ 1 }, sizeof (uint64_t));
414 vlc_object_unlock (obj);
415 return internals->pipes[0];
420 * Requests termination of an object, cancels the object thread, and make the
421 * object wait pipe (if it exists) readable. Not a cancellation point.
423 void __vlc_object_kill( vlc_object_t *p_this )
425 vlc_object_internals_t *priv = vlc_internals( p_this );
428 vlc_thread_cancel( p_this );
429 vlc_object_lock( p_this );
433 p_this->b_die = true;
436 /* This also serves as a memory barrier toward vlc_object_alive(): */
437 vlc_object_unlock( p_this );
441 int canc = vlc_savecancel ();
443 /* write _after_ setting b_die, so vlc_object_alive() returns false */
444 write (fd, &(uint64_t){ 1 }, sizeof (uint64_t));
445 msg_Dbg (p_this, "waitpipe: object killed");
446 vlc_restorecancel (canc);
451 /*****************************************************************************
452 * find a typed object and increment its refcount
453 *****************************************************************************
454 * This function recursively looks for a given object type. i_mode can be one
455 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
456 *****************************************************************************/
457 void * __vlc_object_find( vlc_object_t *p_this, int i_type, int i_mode )
459 vlc_object_t *p_found;
461 /* If we are of the requested type ourselves, don't look further */
462 if( !(i_mode & FIND_STRICT)
463 && vlc_internals (p_this)->i_object_type == i_type )
465 vlc_object_hold( p_this );
469 /* Otherwise, recursively look for the object */
470 if ((i_mode & 0x000f) == FIND_ANYWHERE)
471 return vlc_object_find (p_this->p_libvlc, i_type,
472 (i_mode & ~0x000f)|FIND_CHILD);
474 libvlc_lock (p_this->p_libvlc);
475 p_found = FindObject( p_this, i_type, i_mode );
476 libvlc_unlock (p_this->p_libvlc);
480 #undef vlc_object_find_name
482 * Finds a named object and increment its reference count.
483 * Beware that objects found in this manner can be "owned" by another thread,
484 * be of _any_ type, and be attached to any module (if any). With such an
485 * object reference, you can set or get object variables, emit log messages,
486 * and read write-once object parameters (psz_object_type, etc).
487 * You CANNOT cast the object to a more specific object type, and you
488 * definitely cannot invoke object type-specific callbacks with this.
490 * @param p_this object to search from
491 * @param psz_name name of the object to search for
492 * @param i_mode search direction: FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
494 * @return a matching object (must be released by the caller),
497 vlc_object_t *vlc_object_find_name( vlc_object_t *p_this,
498 const char *psz_name, int i_mode )
500 vlc_object_t *p_found;
502 /* If have the requested name ourselves, don't look further */
503 if( !(i_mode & FIND_STRICT)
504 && p_this->psz_object_name
505 && !strcmp( p_this->psz_object_name, psz_name ) )
507 vlc_object_hold( p_this );
511 libvlc_lock (p_this->p_libvlc);
513 /* Otherwise, recursively look for the object */
514 if( (i_mode & 0x000f) == FIND_ANYWHERE )
516 vlc_object_t *p_root = p_this;
519 while( p_root->p_parent != NULL &&
520 p_root != VLC_OBJECT( p_this->p_libvlc ) )
522 p_root = p_root->p_parent;
525 p_found = FindObjectName( p_root, psz_name,
526 (i_mode & ~0x000f)|FIND_CHILD );
527 if( p_found == NULL && p_root != VLC_OBJECT( p_this->p_libvlc ) )
529 p_found = FindObjectName( VLC_OBJECT( p_this->p_libvlc ),
530 psz_name, (i_mode & ~0x000f)|FIND_CHILD );
535 p_found = FindObjectName( p_this, psz_name, i_mode );
538 libvlc_unlock (p_this->p_libvlc);
543 * Increment an object reference counter.
545 void * __vlc_object_hold( vlc_object_t *p_this )
547 vlc_object_internals_t *internals = vlc_internals( p_this );
549 vlc_spin_lock( &internals->ref_spin );
550 /* Avoid obvious freed object uses */
551 assert( internals->i_refcount > 0 );
552 /* Increment the counter */
553 internals->i_refcount++;
554 vlc_spin_unlock( &internals->ref_spin );
558 /*****************************************************************************
559 * Decrement an object refcount
560 * And destroy the object if its refcount reach zero.
561 *****************************************************************************/
562 void __vlc_object_release( vlc_object_t *p_this )
564 vlc_object_internals_t *internals = vlc_internals( p_this );
565 vlc_object_t *parent = NULL;
566 bool b_should_destroy;
568 vlc_spin_lock( &internals->ref_spin );
569 assert( internals->i_refcount > 0 );
571 if( internals->i_refcount > 1 )
574 /* There are still other references to the object */
575 internals->i_refcount--;
576 vlc_spin_unlock( &internals->ref_spin );
579 vlc_spin_unlock( &internals->ref_spin );
582 /* Remember that we cannot hold the spin while waiting on the mutex */
583 libvlc_lock (p_this->p_libvlc);
584 /* Take the spin again. Note that another thread may have held the
585 * object in the (very short) mean time. */
586 vlc_spin_lock( &internals->ref_spin );
587 b_should_destroy = --internals->i_refcount == 0;
588 vlc_spin_unlock( &internals->ref_spin );
590 if( b_should_destroy )
592 /* We have no children */
593 assert (internals->i_children == 0);
594 parent = p_this->p_parent;
597 if( VLC_OBJECT(p_this->p_libvlc) == p_this )
600 vlc_object_t *leaked = internals->next;
601 while( leaked != p_this )
603 /* We are leaking this object */
605 "ERROR: leaking object (%p, type:%s, name:%s)\n",
606 leaked, leaked->psz_object_type,
607 leaked->psz_object_name );
608 /* Dump object to ease debugging */
609 vlc_object_dump( leaked );
611 leaked = vlc_internals (leaked)->next;
614 if( internals->next != p_this )
615 /* Dump libvlc object to ease debugging */
616 vlc_object_dump( p_this );
619 /* Remove the object from object list
620 * so that it cannot be encountered by vlc_object_get() */
621 vlc_internals (internals->next)->prev = internals->prev;
622 vlc_internals (internals->prev)->next = internals->next;
625 /* Detach from parent to protect against FIND_CHILDREN */
626 vlc_object_detach_unlocked (p_this);
628 libvlc_unlock (p_this->p_libvlc);
630 if( b_should_destroy )
634 canc = vlc_savecancel ();
635 vlc_object_destroy( p_this );
636 vlc_restorecancel (canc);
638 vlc_object_release (parent);
643 ****************************************************************************
644 * attach object to a parent object
645 *****************************************************************************
646 * This function sets p_this as a child of p_parent, and p_parent as a parent
647 * of p_this. This link can be undone using vlc_object_detach.
648 *****************************************************************************/
649 void __vlc_object_attach( vlc_object_t *p_this, vlc_object_t *p_parent )
651 if( !p_this ) return;
653 vlc_object_hold (p_parent);
654 libvlc_lock (p_this->p_libvlc);
656 /* Attach the parent to its child */
657 assert (!p_this->p_parent);
658 p_this->p_parent = p_parent;
660 /* Attach the child to its parent */
661 vlc_object_internals_t *priv = vlc_internals( p_parent );
662 INSERT_ELEM( priv->pp_children, priv->i_children, priv->i_children,
664 libvlc_unlock (p_this->p_libvlc);
668 static void vlc_object_detach_unlocked (vlc_object_t *p_this)
670 if (p_this->p_parent == NULL)
673 vlc_object_internals_t *priv = vlc_internals( p_this->p_parent );
677 /* Remove p_this's parent */
678 p_this->p_parent = NULL;
680 /* Remove all of p_parent's children which are p_this */
681 for( i_index = priv->i_children ; i_index-- ; )
683 if( priv->pp_children[i_index] == p_this )
686 for( i = i_index ; i < priv->i_children ; i++ )
687 priv->pp_children[i] = priv->pp_children[i+1];
691 if( priv->i_children )
693 vlc_object_t **pp_children = (vlc_object_t **)
694 realloc( priv->pp_children,
695 priv->i_children * sizeof(vlc_object_t *) );
697 priv->pp_children = pp_children;
701 /* Special case - don't realloc() to zero to avoid leaking */
702 free( priv->pp_children );
703 priv->pp_children = NULL;
709 ****************************************************************************
710 * detach object from its parent
711 *****************************************************************************
712 * This function removes all links between an object and its parent.
713 *****************************************************************************/
714 void __vlc_object_detach( vlc_object_t *p_this )
716 vlc_object_t *p_parent;
717 if( !p_this ) return;
719 libvlc_lock (p_this->p_libvlc);
720 p_parent = p_this->p_parent;
722 vlc_object_detach_unlocked( p_this );
723 libvlc_unlock (p_this->p_libvlc);
726 vlc_object_release (p_parent);
731 ****************************************************************************
732 * find a list typed objects and increment their refcount
733 *****************************************************************************
734 * This function recursively looks for a given object type. i_mode can be one
735 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
736 *****************************************************************************/
737 vlc_list_t * vlc_list_find( vlc_object_t *p_this, int i_type, int i_mode )
742 /* Look for the objects */
743 switch( i_mode & 0x000f )
746 return vlc_list_find (VLC_OBJECT(p_this->p_libvlc), i_type, FIND_CHILD);
749 libvlc_lock (p_this->p_libvlc);
750 i_count = CountChildren( p_this, i_type );
751 p_list = NewList( i_count );
753 /* Check allocation was successful */
754 if( p_list->i_count != i_count )
756 libvlc_unlock (p_this->p_libvlc);
762 ListChildren( p_list, p_this, i_type );
763 libvlc_unlock (p_this->p_libvlc);
767 msg_Err( p_this, "unimplemented!" );
768 p_list = NewList( 0 );
776 * Gets the list of children of an objects, and increment their reference
778 * @return a list (possibly empty) or NULL in case of error.
780 vlc_list_t *__vlc_list_children( vlc_object_t *obj )
783 vlc_object_internals_t *priv = vlc_internals( obj );
785 libvlc_lock (obj->p_libvlc);
786 l = NewList( priv->i_children );
787 for (int i = 0; i < l->i_count; i++)
789 vlc_object_hold( priv->pp_children[i] );
790 l->p_values[i].p_object = priv->pp_children[i];
792 libvlc_unlock (obj->p_libvlc);
796 /*****************************************************************************
797 * DumpCommand: print the current vlc structure
798 *****************************************************************************
799 * This function prints either an ASCII tree showing the connections between
800 * vlc objects, and additional information such as their refcount, thread ID,
801 * etc. (command "tree"), or the same data as a simple list (command "list").
802 *****************************************************************************/
803 static int DumpCommand( vlc_object_t *p_this, char const *psz_cmd,
804 vlc_value_t oldval, vlc_value_t newval, void *p_data )
806 libvlc_int_t *p_libvlc = p_this->p_libvlc;
808 (void)oldval; (void)p_data;
809 if( *psz_cmd == 'l' )
811 vlc_object_t *cur = VLC_OBJECT (p_libvlc);
813 libvlc_lock (p_this->p_libvlc);
816 PrintObject (cur, "");
817 cur = vlc_internals (cur)->next;
819 while (cur != VLC_OBJECT(p_libvlc));
820 libvlc_unlock (p_this->p_libvlc);
824 vlc_object_t *p_object = NULL;
826 if( *newval.psz_string )
828 /* try using the object's name to find it */
829 p_object = vlc_object_find_name( p_this, newval.psz_string,
837 libvlc_lock (p_this->p_libvlc);
838 if( *psz_cmd == 't' )
840 char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
843 p_object = VLC_OBJECT(p_this->p_libvlc);
846 DumpStructure( p_object, 0, psz_foo );
848 else if( *psz_cmd == 'v' )
853 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
855 PrintObject( p_object, "" );
857 if( !vlc_internals( p_object )->i_vars )
858 printf( " `-o No variables\n" );
859 for( i = 0; i < vlc_internals( p_object )->i_vars; i++ )
861 variable_t *p_var = vlc_internals( p_object )->p_vars + i;
863 const char *psz_type = "unknown";
864 switch( p_var->i_type & VLC_VAR_TYPE )
866 #define MYCASE( type, nice ) \
867 case VLC_VAR_ ## type: \
870 MYCASE( VOID, "void" );
871 MYCASE( BOOL, "bool" );
872 MYCASE( INTEGER, "integer" );
873 MYCASE( HOTKEY, "hotkey" );
874 MYCASE( STRING, "string" );
875 MYCASE( MODULE, "module" );
876 MYCASE( FILE, "file" );
877 MYCASE( DIRECTORY, "directory" );
878 MYCASE( VARIABLE, "variable" );
879 MYCASE( FLOAT, "float" );
880 MYCASE( TIME, "time" );
881 MYCASE( ADDRESS, "address" );
882 MYCASE( MUTEX, "mutex" );
883 MYCASE( LIST, "list" );
886 printf( " %c-o \"%s\" (%s",
887 i + 1 == vlc_internals( p_object )->i_vars ? '`' : '|',
888 p_var->psz_name, psz_type );
889 if( p_var->psz_text )
890 printf( ", %s", p_var->psz_text );
892 if( p_var->i_type & VLC_VAR_HASCHOICE )
893 printf( ", has choices" );
894 if( p_var->i_type & VLC_VAR_ISCOMMAND )
895 printf( ", command" );
896 if( p_var->i_entries )
897 printf( ", %d callbacks", p_var->i_entries );
898 switch( p_var->i_type & VLC_VAR_CLASS )
904 printf( ": %s", p_var->val.b_bool ? "true" : "false" );
906 case VLC_VAR_INTEGER:
907 printf( ": %d", p_var->val.i_int );
910 printf( ": \"%s\"", p_var->val.psz_string );
913 printf( ": %f", p_var->val.f_float );
916 printf( ": %"PRIi64, (int64_t)p_var->val.i_time );
918 case VLC_VAR_ADDRESS:
919 printf( ": %p", p_var->val.p_address );
928 libvlc_unlock (p_this->p_libvlc);
930 if( *newval.psz_string )
932 vlc_object_release( p_object );
939 /*****************************************************************************
940 * vlc_list_release: free a list previously allocated by vlc_list_find
941 *****************************************************************************
942 * This function decreases the refcount of all objects in the list and
944 *****************************************************************************/
945 void vlc_list_release( vlc_list_t *p_list )
949 for( i_index = 0; i_index < p_list->i_count; i_index++ )
951 vlc_object_release( p_list->p_values[i_index].p_object );
954 free( p_list->p_values );
958 /*****************************************************************************
959 * dump an object. (Debug function)
960 *****************************************************************************/
962 static void vlc_object_dump( vlc_object_t *p_this )
964 char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
967 DumpStructure( p_this, 0, psz_foo );
971 /* Following functions are local */
973 static vlc_object_t * FindObject( vlc_object_t *p_this, int i_type, int i_mode )
978 switch( i_mode & 0x000f )
981 p_tmp = p_this->p_parent;
984 if( vlc_internals( p_tmp )->i_object_type == i_type )
986 vlc_object_hold( p_tmp );
991 return FindObject( p_tmp, i_type, i_mode );
997 for( i = vlc_internals( p_this )->i_children; i--; )
999 p_tmp = vlc_internals( p_this )->pp_children[i];
1000 if( vlc_internals( p_tmp )->i_object_type == i_type )
1002 vlc_object_hold( p_tmp );
1005 else if( vlc_internals( p_tmp )->i_children )
1007 p_tmp = FindObject( p_tmp, i_type, i_mode );
1017 /* Handled in vlc_object_find */
1024 static vlc_object_t * FindObjectName( vlc_object_t *p_this,
1025 const char *psz_name,
1029 vlc_object_t *p_tmp;
1031 switch( i_mode & 0x000f )
1034 p_tmp = p_this->p_parent;
1037 if( p_tmp->psz_object_name
1038 && !strcmp( p_tmp->psz_object_name, psz_name ) )
1040 vlc_object_hold( p_tmp );
1045 return FindObjectName( p_tmp, psz_name, i_mode );
1051 for( i = vlc_internals( p_this )->i_children; i--; )
1053 p_tmp = vlc_internals( p_this )->pp_children[i];
1054 if( p_tmp->psz_object_name
1055 && !strcmp( p_tmp->psz_object_name, psz_name ) )
1057 vlc_object_hold( p_tmp );
1060 else if( vlc_internals( p_tmp )->i_children )
1062 p_tmp = FindObjectName( p_tmp, psz_name, i_mode );
1072 /* Handled in vlc_object_find */
1080 static void PrintObject( vlc_object_t *p_this, const char *psz_prefix )
1082 char psz_children[20], psz_refcount[20], psz_thread[30], psz_name[50],
1085 int canc = vlc_savecancel ();
1086 memset( &psz_name, 0, sizeof(psz_name) );
1087 if( p_this->psz_object_name )
1089 snprintf( psz_name, 49, " \"%s\"", p_this->psz_object_name );
1091 psz_name[48] = '\"';
1094 psz_children[0] = '\0';
1095 switch( vlc_internals( p_this )->i_children )
1100 strcpy( psz_children, ", 1 child" );
1103 snprintf( psz_children, 19, ", %i children",
1104 vlc_internals( p_this )->i_children );
1108 psz_refcount[0] = '\0';
1109 if( vlc_internals( p_this )->i_refcount > 0 )
1110 snprintf( psz_refcount, 19, ", refcount %u",
1111 vlc_internals( p_this )->i_refcount );
1113 psz_thread[0] = '\0';
1114 if( vlc_internals( p_this )->b_thread )
1115 snprintf( psz_thread, 29, " (thread %lu)",
1116 (unsigned long)vlc_internals( p_this )->thread_id );
1118 psz_parent[0] = '\0';
1119 if( p_this->p_parent )
1120 snprintf( psz_parent, 19, ", parent %p", p_this->p_parent );
1122 printf( " %so %p %s%s%s%s%s%s\n", psz_prefix,
1123 p_this, p_this->psz_object_type,
1124 psz_name, psz_thread, psz_refcount, psz_children,
1126 vlc_restorecancel (canc);
1129 static void DumpStructure( vlc_object_t *p_this, int i_level, char *psz_foo )
1132 char i_back = psz_foo[i_level];
1133 psz_foo[i_level] = '\0';
1135 PrintObject( p_this, psz_foo );
1137 psz_foo[i_level] = i_back;
1139 if( i_level / 2 >= MAX_DUMPSTRUCTURE_DEPTH )
1141 msg_Warn( p_this, "structure tree is too deep" );
1145 for( i = 0 ; i < vlc_internals( p_this )->i_children ; i++ )
1149 psz_foo[i_level-1] = ' ';
1151 if( psz_foo[i_level-2] == '`' )
1153 psz_foo[i_level-2] = ' ';
1157 if( i == vlc_internals( p_this )->i_children - 1 )
1159 psz_foo[i_level] = '`';
1163 psz_foo[i_level] = '|';
1166 psz_foo[i_level+1] = '-';
1167 psz_foo[i_level+2] = '\0';
1169 DumpStructure( vlc_internals( p_this )->pp_children[i], i_level + 2,
1174 static vlc_list_t * NewList( int i_count )
1176 vlc_list_t * p_list = (vlc_list_t *)malloc( sizeof( vlc_list_t ) );
1177 if( p_list == NULL )
1182 p_list->i_count = i_count;
1186 p_list->p_values = NULL;
1190 p_list->p_values = malloc( i_count * sizeof( vlc_value_t ) );
1191 if( p_list->p_values == NULL )
1193 p_list->i_count = 0;
1200 static void ListReplace( vlc_list_t *p_list, vlc_object_t *p_object,
1203 if( p_list == NULL || i_index >= p_list->i_count )
1208 vlc_object_hold( p_object );
1210 p_list->p_values[i_index].p_object = p_object;
1215 /*static void ListAppend( vlc_list_t *p_list, vlc_object_t *p_object )
1217 if( p_list == NULL )
1222 p_list->p_values = realloc( p_list->p_values, (p_list->i_count + 1)
1223 * sizeof( vlc_value_t ) );
1224 if( p_list->p_values == NULL )
1226 p_list->i_count = 0;
1230 vlc_object_hold( p_object );
1232 p_list->p_values[p_list->i_count].p_object = p_object;
1238 static int CountChildren( vlc_object_t *p_this, int i_type )
1240 vlc_object_t *p_tmp;
1243 for( i = 0; i < vlc_internals( p_this )->i_children; i++ )
1245 p_tmp = vlc_internals( p_this )->pp_children[i];
1247 if( vlc_internals( p_tmp )->i_object_type == i_type )
1251 i_count += CountChildren( p_tmp, i_type );
1257 static void ListChildren( vlc_list_t *p_list, vlc_object_t *p_this, int i_type )
1259 vlc_object_t *p_tmp;
1262 for( i = 0; i < vlc_internals( p_this )->i_children; i++ )
1264 p_tmp = vlc_internals( p_this )->pp_children[i];
1266 if( vlc_internals( p_tmp )->i_object_type == i_type )
1267 ListReplace( p_list, p_tmp, p_list->i_count++ );
1269 ListChildren( p_list, p_tmp, i_type );