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->var_lock );
167 vlc_cond_init( &p_priv->var_wait );
168 p_priv->pipes[0] = p_priv->pipes[1] = -1;
170 p_priv->next = p_this;
171 libvlc_lock (p_new->p_libvlc);
172 p_priv->prev = vlc_internals (p_this)->prev;
173 vlc_internals (p_this)->prev = p_new;
174 vlc_internals (p_priv->prev)->next = p_new;
175 libvlc_unlock (p_new->p_libvlc);
177 if (p_new == VLC_OBJECT(p_new->p_libvlc))
178 { /* TODO: should be in src/libvlc.c */
179 int canc = vlc_savecancel ();
180 var_Create( p_new, "list", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
181 var_AddCallback( p_new, "list", DumpCommand, NULL );
182 var_Create( p_new, "tree", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
183 var_AddCallback( p_new, "tree", DumpCommand, NULL );
184 var_Create( p_new, "vars", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
185 var_AddCallback( p_new, "vars", DumpCommand, NULL );
186 vlc_restorecancel (canc);
194 * Allocates and initializes a vlc object.
196 * @param i_type known object type (all of them are negative integer values),
197 * or object byte size (always positive).
199 * @return the new object, or NULL on error.
201 void * __vlc_object_create( vlc_object_t *p_this, int i_type )
203 const char * psz_type;
208 case VLC_OBJECT_DECODER:
209 i_size = sizeof(decoder_t);
210 psz_type = "decoder";
212 case VLC_OBJECT_AOUT:
213 i_size = sizeof(aout_instance_t);
214 psz_type = "audio output";
217 assert( i_type > 0 ); /* unknown type?! */
219 i_type = VLC_OBJECT_GENERIC;
220 psz_type = "generic";
224 return vlc_custom_create( p_this, i_size, i_type, psz_type );
229 ****************************************************************************
230 * Set the destructor of a vlc object
232 * This function sets the destructor of the vlc object. It will be called
233 * when the object is destroyed when the its refcount reaches 0.
234 * (It is called by the internal function vlc_object_destroy())
235 *****************************************************************************/
236 void __vlc_object_set_destructor( vlc_object_t *p_this,
237 vlc_destructor_t pf_destructor )
239 vlc_object_internals_t *p_priv = vlc_internals(p_this );
241 vlc_spin_lock( &p_priv->ref_spin );
242 p_priv->pf_destructor = pf_destructor;
243 vlc_spin_unlock( &p_priv->ref_spin );
247 ****************************************************************************
248 * Destroy a vlc object (Internal)
250 * This function destroys an object that has been previously allocated with
251 * vlc_object_create. The object's refcount must be zero and it must not be
252 * attached to other objects in any way.
254 * This function must be called with cancellation disabled (currently).
255 *****************************************************************************/
256 static void vlc_object_destroy( vlc_object_t *p_this )
258 vlc_object_internals_t *p_priv = vlc_internals( p_this );
260 /* Objects are always detached beforehand */
261 assert( !p_this->p_parent );
263 /* Send a kill to the object's thread if applicable */
264 vlc_object_kill( p_this );
266 /* Call the custom "subclass" destructor */
267 if( p_priv->pf_destructor )
268 p_priv->pf_destructor( p_this );
270 /* Any thread must have been cleaned up at this point. */
271 assert( !p_priv->b_thread );
273 /* Destroy the associated variables, starting from the end so that
274 * no memmove calls have to be done. */
275 while( p_priv->i_vars )
277 var_Destroy( p_this, p_priv->p_vars[p_priv->i_vars - 1].psz_name );
280 free( p_priv->p_vars );
281 vlc_cond_destroy( &p_priv->var_wait );
282 vlc_mutex_destroy( &p_priv->var_lock );
284 free( p_this->psz_header );
286 FREENULL( p_this->psz_object_name );
288 vlc_spin_destroy( &p_priv->ref_spin );
289 if( p_priv->pipes[1] != -1 && p_priv->pipes[1] != p_priv->pipes[0] )
290 close( p_priv->pipes[1] );
291 if( p_priv->pipes[0] != -1 )
292 close( p_priv->pipes[0] );
293 if( VLC_OBJECT(p_this->p_libvlc) == p_this )
294 vlc_mutex_destroy (&(libvlc_priv ((libvlc_int_t *)p_this)->structure_lock));
301 # include <winsock2.h>
302 # include <ws2tcpip.h>
305 * select()-able pipes emulated using Winsock
307 static int pipe (int fd[2])
310 int addrlen = sizeof (addr);
312 SOCKET l = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP), a,
313 c = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
314 if ((l == INVALID_SOCKET) || (c == INVALID_SOCKET))
317 memset (&addr, 0, sizeof (addr));
318 addr.sin_family = AF_INET;
319 addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
320 if (bind (l, (PSOCKADDR)&addr, sizeof (addr))
321 || getsockname (l, (PSOCKADDR)&addr, &addrlen)
323 || connect (c, (PSOCKADDR)&addr, addrlen))
326 a = accept (l, NULL, NULL);
327 if (a == INVALID_SOCKET)
338 if (l != INVALID_SOCKET)
340 if (c != INVALID_SOCKET)
346 #define read( a, b, c ) recv (a, b, c, 0)
348 #define write( a, b, c ) send (a, b, c, 0)
350 #define close( a ) closesocket (a)
353 static vlc_mutex_t pipe_lock = VLC_STATIC_MUTEX;
356 * Returns the readable end of a pipe that becomes readable once termination
357 * of the object is requested (vlc_object_kill()).
358 * This can be used to wake-up out of a select() or poll() event loop, such
359 * typically when doing network I/O.
361 * Note that the pipe will remain the same for the lifetime of the object.
362 * DO NOT read the pipe nor close it yourself. Ever.
364 * @param obj object that would be "killed"
365 * @return a readable pipe descriptor, or -1 on error.
367 int vlc_object_waitpipe( vlc_object_t *obj )
369 vlc_object_internals_t *internals = vlc_internals( obj );
371 vlc_mutex_lock (&pipe_lock);
372 if (internals->pipes[0] == -1)
374 /* This can only ever happen if someone killed us without locking: */
375 assert (internals->pipes[1] == -1);
378 internals->pipes[0] = internals->pipes[1] = eventfd (0, 0);
379 if (internals->pipes[0] == -1)
382 if (pipe (internals->pipes))
383 internals->pipes[0] = internals->pipes[1] = -1;
386 if (internals->pipes[0] != -1 && obj->b_die)
387 { /* Race condition: vlc_object_kill() already invoked! */
388 msg_Dbg (obj, "waitpipe: object already dying");
389 write (internals->pipes[1], &(uint64_t){ 1 }, sizeof (uint64_t));
392 vlc_mutex_unlock (&pipe_lock);
393 return internals->pipes[0];
398 * Requests termination of an object, cancels the object thread, and make the
399 * object wait pipe (if it exists) readable. Not a cancellation point.
401 void __vlc_object_kill( vlc_object_t *p_this )
403 vlc_object_internals_t *priv = vlc_internals( p_this );
406 vlc_thread_cancel( p_this );
407 vlc_mutex_lock( &pipe_lock );
411 p_this->b_die = true;
414 /* This also serves as a memory barrier toward vlc_object_alive(): */
415 vlc_mutex_unlock( &pipe_lock );
419 int canc = vlc_savecancel ();
421 /* write _after_ setting b_die, so vlc_object_alive() returns false */
422 write (fd, &(uint64_t){ 1 }, sizeof (uint64_t));
423 msg_Dbg (p_this, "waitpipe: object killed");
424 vlc_restorecancel (canc);
429 /*****************************************************************************
430 * find a typed object and increment its refcount
431 *****************************************************************************
432 * This function recursively looks for a given object type. i_mode can be one
433 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
434 *****************************************************************************/
435 void * __vlc_object_find( vlc_object_t *p_this, int i_type, int i_mode )
437 vlc_object_t *p_found;
439 /* If we are of the requested type ourselves, don't look further */
440 if( !(i_mode & FIND_STRICT)
441 && vlc_internals (p_this)->i_object_type == i_type )
443 vlc_object_hold( p_this );
447 /* Otherwise, recursively look for the object */
448 if ((i_mode & 0x000f) == FIND_ANYWHERE)
449 return vlc_object_find (p_this->p_libvlc, i_type,
450 (i_mode & ~0x000f)|FIND_CHILD);
452 libvlc_lock (p_this->p_libvlc);
453 p_found = FindObject( p_this, i_type, i_mode );
454 libvlc_unlock (p_this->p_libvlc);
458 #undef vlc_object_find_name
460 * Finds a named object and increment its reference count.
461 * Beware that objects found in this manner can be "owned" by another thread,
462 * be of _any_ type, and be attached to any module (if any). With such an
463 * object reference, you can set or get object variables, emit log messages,
464 * and read write-once object parameters (psz_object_type, etc).
465 * You CANNOT cast the object to a more specific object type, and you
466 * definitely cannot invoke object type-specific callbacks with this.
468 * @param p_this object to search from
469 * @param psz_name name of the object to search for
470 * @param i_mode search direction: FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
472 * @return a matching object (must be released by the caller),
475 vlc_object_t *vlc_object_find_name( vlc_object_t *p_this,
476 const char *psz_name, int i_mode )
478 vlc_object_t *p_found;
480 /* Reading psz_object_name from a separate inhibits thread-safety.
481 * Use a libvlc address variable instead for that sort of things! */
482 msg_Warn( p_this, "%s(%s) is not safe!", __func__, psz_name );
483 /* If have the requested name ourselves, don't look further */
484 if( !(i_mode & FIND_STRICT)
485 && p_this->psz_object_name
486 && !strcmp( p_this->psz_object_name, psz_name ) )
488 vlc_object_hold( p_this );
492 libvlc_lock (p_this->p_libvlc);
494 /* Otherwise, recursively look for the object */
495 if( (i_mode & 0x000f) == FIND_ANYWHERE )
497 vlc_object_t *p_root = p_this;
500 while( p_root->p_parent != NULL &&
501 p_root != VLC_OBJECT( p_this->p_libvlc ) )
503 p_root = p_root->p_parent;
506 p_found = FindObjectName( p_root, psz_name,
507 (i_mode & ~0x000f)|FIND_CHILD );
508 if( p_found == NULL && p_root != VLC_OBJECT( p_this->p_libvlc ) )
510 p_found = FindObjectName( VLC_OBJECT( p_this->p_libvlc ),
511 psz_name, (i_mode & ~0x000f)|FIND_CHILD );
516 p_found = FindObjectName( p_this, psz_name, i_mode );
519 libvlc_unlock (p_this->p_libvlc);
524 * Increment an object reference counter.
526 void * __vlc_object_hold( vlc_object_t *p_this )
528 vlc_object_internals_t *internals = vlc_internals( p_this );
530 vlc_spin_lock( &internals->ref_spin );
531 /* Avoid obvious freed object uses */
532 assert( internals->i_refcount > 0 );
533 /* Increment the counter */
534 internals->i_refcount++;
535 vlc_spin_unlock( &internals->ref_spin );
539 /*****************************************************************************
540 * Decrement an object refcount
541 * And destroy the object if its refcount reach zero.
542 *****************************************************************************/
543 void __vlc_object_release( vlc_object_t *p_this )
545 vlc_object_internals_t *internals = vlc_internals( p_this );
546 vlc_object_t *parent = NULL;
547 bool b_should_destroy;
549 vlc_spin_lock( &internals->ref_spin );
550 assert( internals->i_refcount > 0 );
552 if( internals->i_refcount > 1 )
555 /* There are still other references to the object */
556 internals->i_refcount--;
557 vlc_spin_unlock( &internals->ref_spin );
560 vlc_spin_unlock( &internals->ref_spin );
563 /* Remember that we cannot hold the spin while waiting on the mutex */
564 libvlc_lock (p_this->p_libvlc);
565 /* Take the spin again. Note that another thread may have held the
566 * object in the (very short) mean time. */
567 vlc_spin_lock( &internals->ref_spin );
568 b_should_destroy = --internals->i_refcount == 0;
569 vlc_spin_unlock( &internals->ref_spin );
571 if( b_should_destroy )
573 parent = p_this->p_parent;
576 if( VLC_OBJECT(p_this->p_libvlc) == p_this )
579 vlc_object_t *leaked = internals->next;
580 while( leaked != p_this )
582 /* We are leaking this object */
584 "ERROR: leaking object (%p, type:%s, name:%s)\n",
585 leaked, leaked->psz_object_type,
586 leaked->psz_object_name );
587 /* Dump object to ease debugging */
588 vlc_object_dump( leaked );
590 leaked = vlc_internals (leaked)->next;
593 if( internals->next != p_this )
594 /* Dump libvlc object to ease debugging */
595 vlc_object_dump( p_this );
598 /* Remove the object from object list
599 * so that it cannot be encountered by vlc_object_get() */
600 vlc_internals (internals->next)->prev = internals->prev;
601 vlc_internals (internals->prev)->next = internals->next;
604 /* Detach from parent to protect against FIND_CHILDREN */
605 vlc_object_detach_unlocked (p_this);
607 /* We have no children */
608 assert (internals->i_children == 0);
610 libvlc_unlock (p_this->p_libvlc);
612 if( b_should_destroy )
616 canc = vlc_savecancel ();
617 vlc_object_destroy( p_this );
618 vlc_restorecancel (canc);
620 vlc_object_release (parent);
625 ****************************************************************************
626 * attach object to a parent object
627 *****************************************************************************
628 * This function sets p_this as a child of p_parent, and p_parent as a parent
629 * of p_this. This link can be undone using vlc_object_detach.
630 *****************************************************************************/
631 void __vlc_object_attach( vlc_object_t *p_this, vlc_object_t *p_parent )
633 if( !p_this ) return;
635 vlc_object_hold (p_parent);
636 libvlc_lock (p_this->p_libvlc);
638 /* Attach the parent to its child */
639 assert (!p_this->p_parent);
640 p_this->p_parent = p_parent;
642 /* Attach the child to its parent */
643 vlc_object_internals_t *priv = vlc_internals( p_parent );
644 INSERT_ELEM( priv->pp_children, priv->i_children, priv->i_children,
646 libvlc_unlock (p_this->p_libvlc);
650 static void vlc_object_detach_unlocked (vlc_object_t *p_this)
652 if (p_this->p_parent == NULL)
655 vlc_object_internals_t *priv = vlc_internals( p_this->p_parent );
659 /* Remove p_this's parent */
660 p_this->p_parent = NULL;
662 /* Remove all of p_parent's children which are p_this */
663 for( i_index = priv->i_children ; i_index-- ; )
665 if( priv->pp_children[i_index] == p_this )
668 for( i = i_index ; i < priv->i_children ; i++ )
669 priv->pp_children[i] = priv->pp_children[i+1];
673 if( priv->i_children )
675 vlc_object_t **pp_children = (vlc_object_t **)
676 realloc( priv->pp_children,
677 priv->i_children * sizeof(vlc_object_t *) );
679 priv->pp_children = pp_children;
683 /* Special case - don't realloc() to zero to avoid leaking */
684 free( priv->pp_children );
685 priv->pp_children = NULL;
691 ****************************************************************************
692 * detach object from its parent
693 *****************************************************************************
694 * This function removes all links between an object and its parent.
695 *****************************************************************************/
696 void __vlc_object_detach( vlc_object_t *p_this )
698 vlc_object_t *p_parent;
699 if( !p_this ) return;
701 libvlc_lock (p_this->p_libvlc);
702 p_parent = p_this->p_parent;
704 vlc_object_detach_unlocked( p_this );
705 libvlc_unlock (p_this->p_libvlc);
708 vlc_object_release (p_parent);
713 ****************************************************************************
714 * find a list typed objects and increment their refcount
715 *****************************************************************************
716 * This function recursively looks for a given object type. i_mode can be one
717 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
718 *****************************************************************************/
719 vlc_list_t * vlc_list_find( vlc_object_t *p_this, int i_type, int i_mode )
724 /* Look for the objects */
725 switch( i_mode & 0x000f )
728 return vlc_list_find (VLC_OBJECT(p_this->p_libvlc), i_type, FIND_CHILD);
731 libvlc_lock (p_this->p_libvlc);
732 i_count = CountChildren( p_this, i_type );
733 p_list = NewList( i_count );
735 /* Check allocation was successful */
736 if( p_list->i_count != i_count )
738 libvlc_unlock (p_this->p_libvlc);
744 ListChildren( p_list, p_this, i_type );
745 libvlc_unlock (p_this->p_libvlc);
749 msg_Err( p_this, "unimplemented!" );
750 p_list = NewList( 0 );
758 * Gets the list of children of an objects, and increment their reference
760 * @return a list (possibly empty) or NULL in case of error.
762 vlc_list_t *__vlc_list_children( vlc_object_t *obj )
765 vlc_object_internals_t *priv = vlc_internals( obj );
767 libvlc_lock (obj->p_libvlc);
768 l = NewList( priv->i_children );
769 for (int i = 0; i < l->i_count; i++)
771 vlc_object_hold( priv->pp_children[i] );
772 l->p_values[i].p_object = priv->pp_children[i];
774 libvlc_unlock (obj->p_libvlc);
778 /*****************************************************************************
779 * DumpCommand: print the current vlc structure
780 *****************************************************************************
781 * This function prints either an ASCII tree showing the connections between
782 * vlc objects, and additional information such as their refcount, thread ID,
783 * etc. (command "tree"), or the same data as a simple list (command "list").
784 *****************************************************************************/
785 static int DumpCommand( vlc_object_t *p_this, char const *psz_cmd,
786 vlc_value_t oldval, vlc_value_t newval, void *p_data )
788 libvlc_int_t *p_libvlc = p_this->p_libvlc;
790 (void)oldval; (void)p_data;
791 if( *psz_cmd == 'l' )
793 vlc_object_t *cur = VLC_OBJECT (p_libvlc);
795 libvlc_lock (p_this->p_libvlc);
798 PrintObject (cur, "");
799 cur = vlc_internals (cur)->next;
801 while (cur != VLC_OBJECT(p_libvlc));
802 libvlc_unlock (p_this->p_libvlc);
806 vlc_object_t *p_object = NULL;
808 if( *newval.psz_string )
810 /* try using the object's name to find it */
811 p_object = vlc_object_find_name( p_this, newval.psz_string,
819 libvlc_lock (p_this->p_libvlc);
820 if( *psz_cmd == 't' )
822 char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
825 p_object = VLC_OBJECT(p_this->p_libvlc);
828 DumpStructure( p_object, 0, psz_foo );
830 else if( *psz_cmd == 'v' )
835 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
837 PrintObject( p_object, "" );
839 if( !vlc_internals( p_object )->i_vars )
840 printf( " `-o No variables\n" );
841 for( i = 0; i < vlc_internals( p_object )->i_vars; i++ )
843 variable_t *p_var = vlc_internals( p_object )->p_vars + i;
845 const char *psz_type = "unknown";
846 switch( p_var->i_type & VLC_VAR_TYPE )
848 #define MYCASE( type, nice ) \
849 case VLC_VAR_ ## type: \
852 MYCASE( VOID, "void" );
853 MYCASE( BOOL, "bool" );
854 MYCASE( INTEGER, "integer" );
855 MYCASE( HOTKEY, "hotkey" );
856 MYCASE( STRING, "string" );
857 MYCASE( MODULE, "module" );
858 MYCASE( FILE, "file" );
859 MYCASE( DIRECTORY, "directory" );
860 MYCASE( VARIABLE, "variable" );
861 MYCASE( FLOAT, "float" );
862 MYCASE( TIME, "time" );
863 MYCASE( ADDRESS, "address" );
864 MYCASE( MUTEX, "mutex" );
865 MYCASE( LIST, "list" );
868 printf( " %c-o \"%s\" (%s",
869 i + 1 == vlc_internals( p_object )->i_vars ? '`' : '|',
870 p_var->psz_name, psz_type );
871 if( p_var->psz_text )
872 printf( ", %s", p_var->psz_text );
874 if( p_var->i_type & VLC_VAR_HASCHOICE )
875 printf( ", has choices" );
876 if( p_var->i_type & VLC_VAR_ISCOMMAND )
877 printf( ", command" );
878 if( p_var->i_entries )
879 printf( ", %d callbacks", p_var->i_entries );
880 switch( p_var->i_type & VLC_VAR_CLASS )
886 printf( ": %s", p_var->val.b_bool ? "true" : "false" );
888 case VLC_VAR_INTEGER:
889 printf( ": %d", p_var->val.i_int );
892 printf( ": \"%s\"", p_var->val.psz_string );
895 printf( ": %f", p_var->val.f_float );
898 printf( ": %"PRIi64, (int64_t)p_var->val.i_time );
900 case VLC_VAR_ADDRESS:
901 printf( ": %p", p_var->val.p_address );
910 libvlc_unlock (p_this->p_libvlc);
912 if( *newval.psz_string )
914 vlc_object_release( p_object );
921 /*****************************************************************************
922 * vlc_list_release: free a list previously allocated by vlc_list_find
923 *****************************************************************************
924 * This function decreases the refcount of all objects in the list and
926 *****************************************************************************/
927 void vlc_list_release( vlc_list_t *p_list )
931 for( i_index = 0; i_index < p_list->i_count; i_index++ )
933 vlc_object_release( p_list->p_values[i_index].p_object );
936 free( p_list->p_values );
940 /*****************************************************************************
941 * dump an object. (Debug function)
942 *****************************************************************************/
944 static void vlc_object_dump( vlc_object_t *p_this )
946 char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
949 DumpStructure( p_this, 0, psz_foo );
953 /* Following functions are local */
955 static vlc_object_t * FindObject( vlc_object_t *p_this, int i_type, int i_mode )
960 switch( i_mode & 0x000f )
963 p_tmp = p_this->p_parent;
966 if( vlc_internals( p_tmp )->i_object_type == i_type )
968 vlc_object_hold( p_tmp );
973 return FindObject( p_tmp, i_type, i_mode );
979 for( i = vlc_internals( p_this )->i_children; i--; )
981 p_tmp = vlc_internals( p_this )->pp_children[i];
982 if( vlc_internals( p_tmp )->i_object_type == i_type )
984 vlc_object_hold( p_tmp );
987 else if( vlc_internals( p_tmp )->i_children )
989 p_tmp = FindObject( p_tmp, i_type, i_mode );
999 /* Handled in vlc_object_find */
1006 static vlc_object_t * FindObjectName( vlc_object_t *p_this,
1007 const char *psz_name,
1011 vlc_object_t *p_tmp;
1013 switch( i_mode & 0x000f )
1016 p_tmp = p_this->p_parent;
1019 if( p_tmp->psz_object_name
1020 && !strcmp( p_tmp->psz_object_name, psz_name ) )
1022 vlc_object_hold( p_tmp );
1027 return FindObjectName( p_tmp, psz_name, i_mode );
1033 for( i = vlc_internals( p_this )->i_children; i--; )
1035 p_tmp = vlc_internals( p_this )->pp_children[i];
1036 if( p_tmp->psz_object_name
1037 && !strcmp( p_tmp->psz_object_name, psz_name ) )
1039 vlc_object_hold( p_tmp );
1042 else if( vlc_internals( p_tmp )->i_children )
1044 p_tmp = FindObjectName( p_tmp, psz_name, i_mode );
1054 /* Handled in vlc_object_find */
1062 static void PrintObject( vlc_object_t *p_this, const char *psz_prefix )
1064 char psz_children[20], psz_refcount[20], psz_thread[30], psz_name[50],
1067 int canc = vlc_savecancel ();
1068 memset( &psz_name, 0, sizeof(psz_name) );
1069 if( p_this->psz_object_name )
1071 snprintf( psz_name, 49, " \"%s\"", p_this->psz_object_name );
1073 psz_name[48] = '\"';
1076 psz_children[0] = '\0';
1077 switch( vlc_internals( p_this )->i_children )
1082 strcpy( psz_children, ", 1 child" );
1085 snprintf( psz_children, 19, ", %i children",
1086 vlc_internals( p_this )->i_children );
1090 psz_refcount[0] = '\0';
1091 if( vlc_internals( p_this )->i_refcount > 0 )
1092 snprintf( psz_refcount, 19, ", refcount %u",
1093 vlc_internals( p_this )->i_refcount );
1095 psz_thread[0] = '\0';
1096 if( vlc_internals( p_this )->b_thread )
1097 snprintf( psz_thread, 29, " (thread %lu)",
1098 (unsigned long)vlc_internals( p_this )->thread_id );
1100 psz_parent[0] = '\0';
1101 if( p_this->p_parent )
1102 snprintf( psz_parent, 19, ", parent %p", p_this->p_parent );
1104 printf( " %so %p %s%s%s%s%s%s\n", psz_prefix,
1105 p_this, p_this->psz_object_type,
1106 psz_name, psz_thread, psz_refcount, psz_children,
1108 vlc_restorecancel (canc);
1111 static void DumpStructure( vlc_object_t *p_this, int i_level, char *psz_foo )
1114 char i_back = psz_foo[i_level];
1115 psz_foo[i_level] = '\0';
1117 PrintObject( p_this, psz_foo );
1119 psz_foo[i_level] = i_back;
1121 if( i_level / 2 >= MAX_DUMPSTRUCTURE_DEPTH )
1123 msg_Warn( p_this, "structure tree is too deep" );
1127 for( i = 0 ; i < vlc_internals( p_this )->i_children ; i++ )
1131 psz_foo[i_level-1] = ' ';
1133 if( psz_foo[i_level-2] == '`' )
1135 psz_foo[i_level-2] = ' ';
1139 if( i == vlc_internals( p_this )->i_children - 1 )
1141 psz_foo[i_level] = '`';
1145 psz_foo[i_level] = '|';
1148 psz_foo[i_level+1] = '-';
1149 psz_foo[i_level+2] = '\0';
1151 DumpStructure( vlc_internals( p_this )->pp_children[i], i_level + 2,
1156 static vlc_list_t * NewList( int i_count )
1158 vlc_list_t * p_list = (vlc_list_t *)malloc( sizeof( vlc_list_t ) );
1159 if( p_list == NULL )
1164 p_list->i_count = i_count;
1168 p_list->p_values = NULL;
1172 p_list->p_values = malloc( i_count * sizeof( vlc_value_t ) );
1173 if( p_list->p_values == NULL )
1175 p_list->i_count = 0;
1182 static void ListReplace( vlc_list_t *p_list, vlc_object_t *p_object,
1185 if( p_list == NULL || i_index >= p_list->i_count )
1190 vlc_object_hold( p_object );
1192 p_list->p_values[i_index].p_object = p_object;
1197 /*static void ListAppend( vlc_list_t *p_list, vlc_object_t *p_object )
1199 if( p_list == NULL )
1204 p_list->p_values = realloc( p_list->p_values, (p_list->i_count + 1)
1205 * sizeof( vlc_value_t ) );
1206 if( p_list->p_values == NULL )
1208 p_list->i_count = 0;
1212 vlc_object_hold( p_object );
1214 p_list->p_values[p_list->i_count].p_object = p_object;
1220 static int CountChildren( vlc_object_t *p_this, int i_type )
1222 vlc_object_t *p_tmp;
1225 for( i = 0; i < vlc_internals( p_this )->i_children; i++ )
1227 p_tmp = vlc_internals( p_this )->pp_children[i];
1229 if( vlc_internals( p_tmp )->i_object_type == i_type )
1233 i_count += CountChildren( p_tmp, i_type );
1239 static void ListChildren( vlc_list_t *p_list, vlc_object_t *p_this, int i_type )
1241 vlc_object_t *p_tmp;
1244 for( i = 0; i < vlc_internals( p_this )->i_children; i++ )
1246 p_tmp = vlc_internals( p_this )->pp_children[i];
1248 if( vlc_internals( p_tmp )->i_object_type == i_type )
1249 ListReplace( p_list, p_tmp, p_list->i_count++ );
1251 ListChildren( p_list, p_tmp, i_type );