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 */
56 #ifdef HAVE_SYS_EVENTFD_H
57 # include <sys/eventfd.h>
61 /*****************************************************************************
63 *****************************************************************************/
64 static int DumpCommand( vlc_object_t *, char const *,
65 vlc_value_t, vlc_value_t, void * );
67 static vlc_object_t * FindObject ( vlc_object_t *, int, int );
68 static vlc_object_t * FindObjectName( vlc_object_t *, const char *, int );
69 static void PrintObject ( vlc_object_t *, const char * );
70 static void DumpStructure ( vlc_object_t *, int, char * );
72 static vlc_list_t * NewList ( int );
73 static void ListReplace ( vlc_list_t *, vlc_object_t *, int );
74 /*static void ListAppend ( vlc_list_t *, vlc_object_t * );*/
75 static int CountChildren ( vlc_object_t *, int );
76 static void ListChildren ( vlc_list_t *, vlc_object_t *, int );
78 static void vlc_object_destroy( vlc_object_t *p_this );
79 static void vlc_object_detach_unlocked (vlc_object_t *p_this);
81 static void vlc_object_dump( vlc_object_t *p_this );
84 /*****************************************************************************
85 * Local structure lock
86 *****************************************************************************/
87 static void libvlc_lock (libvlc_int_t *p_libvlc)
89 vlc_mutex_lock (&(libvlc_priv (p_libvlc)->structure_lock));
92 static void libvlc_unlock (libvlc_int_t *p_libvlc)
94 vlc_mutex_unlock (&(libvlc_priv (p_libvlc)->structure_lock));
97 void *__vlc_custom_create( vlc_object_t *p_this, size_t i_size,
98 int i_type, const char *psz_type )
101 vlc_object_internals_t *p_priv;
104 * VLC objects are laid out as follow:
105 * - first the LibVLC-private per-object data,
106 * - then VLC_COMMON members from vlc_object_t,
107 * - finally, the type-specific data (if any).
109 * This function initializes the LibVLC and common data,
110 * and zeroes the rest.
112 p_priv = calloc( 1, sizeof( *p_priv ) + i_size );
116 assert (i_size >= sizeof (vlc_object_t));
117 p_new = (vlc_object_t *)(p_priv + 1);
119 p_priv->i_object_type = i_type;
120 p_new->psz_object_type = psz_type;
121 p_new->psz_object_name = NULL;
123 p_new->b_die = false;
124 p_new->b_error = false;
125 p_new->b_force = false;
127 p_new->psz_header = NULL;
130 p_new->i_flags = p_this->i_flags
131 & (OBJECT_FLAGS_NODBG|OBJECT_FLAGS_QUIET|OBJECT_FLAGS_NOINTERACT);
133 p_priv->p_vars = calloc( 16, sizeof( variable_t ) );
135 if( !p_priv->p_vars )
143 libvlc_int_t *self = (libvlc_int_t*)p_new;
144 p_new->p_libvlc = self;
145 vlc_mutex_init (&(libvlc_priv (self)->structure_lock));
146 p_this = p_priv->next = p_priv->prev = p_new;
149 p_new->p_libvlc = p_this->p_libvlc;
151 vlc_spin_init( &p_priv->ref_spin );
152 p_priv->i_refcount = 1;
153 p_priv->pf_destructor = NULL;
154 p_priv->b_thread = false;
155 p_new->p_parent = NULL;
156 p_priv->pp_children = NULL;
157 p_priv->i_children = 0;
159 p_new->p_private = NULL;
161 /* Initialize mutexes and condvars */
162 vlc_mutex_init( &p_priv->lock );
163 vlc_mutex_init( &p_priv->var_lock );
164 vlc_cond_init( &p_priv->var_wait );
165 p_priv->pipes[0] = p_priv->pipes[1] = -1;
167 p_priv->next = p_this;
168 libvlc_lock (p_new->p_libvlc);
169 p_priv->prev = vlc_internals (p_this)->prev;
170 vlc_internals (p_this)->prev = p_new;
171 vlc_internals (p_priv->prev)->next = p_new;
172 libvlc_unlock (p_new->p_libvlc);
174 if (p_new == VLC_OBJECT(p_new->p_libvlc))
175 { /* TODO: should be in src/libvlc.c */
176 int canc = vlc_savecancel ();
177 var_Create( p_new, "list", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
178 var_AddCallback( p_new, "list", DumpCommand, NULL );
179 var_Create( p_new, "tree", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
180 var_AddCallback( p_new, "tree", DumpCommand, NULL );
181 var_Create( p_new, "vars", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
182 var_AddCallback( p_new, "vars", DumpCommand, NULL );
183 vlc_restorecancel (canc);
191 * Allocates and initializes a vlc object.
193 * @param i_type known object type (all of them are negative integer values),
194 * or object byte size (always positive).
196 * @return the new object, or NULL on error.
198 void * __vlc_object_create( vlc_object_t *p_this, int i_type )
200 const char * psz_type;
205 case VLC_OBJECT_INTF:
206 i_size = sizeof(intf_thread_t);
207 psz_type = "interface";
209 case VLC_OBJECT_DECODER:
210 i_size = sizeof(decoder_t);
211 psz_type = "decoder";
213 case VLC_OBJECT_PACKETIZER:
214 i_size = sizeof(decoder_t);
215 psz_type = "packetizer";
217 case VLC_OBJECT_AOUT:
218 i_size = sizeof(aout_instance_t);
219 psz_type = "audio output";
222 assert( i_type > 0 ); /* unknown type?! */
224 i_type = VLC_OBJECT_GENERIC;
225 psz_type = "generic";
229 return vlc_custom_create( p_this, i_size, i_type, psz_type );
234 ****************************************************************************
235 * Set the destructor of a vlc object
237 * This function sets the destructor of the vlc object. It will be called
238 * when the object is destroyed when the its refcount reaches 0.
239 * (It is called by the internal function vlc_object_destroy())
240 *****************************************************************************/
241 void __vlc_object_set_destructor( vlc_object_t *p_this,
242 vlc_destructor_t pf_destructor )
244 vlc_object_internals_t *p_priv = vlc_internals(p_this );
245 p_priv->pf_destructor = pf_destructor;
249 ****************************************************************************
250 * Destroy a vlc object (Internal)
252 * This function destroys an object that has been previously allocated with
253 * vlc_object_create. The object's refcount must be zero and it must not be
254 * attached to other objects in any way.
256 * This function must be called with cancellation disabled (currently).
257 *****************************************************************************/
258 static void vlc_object_destroy( vlc_object_t *p_this )
260 vlc_object_internals_t *p_priv = vlc_internals( p_this );
262 /* Objects are always detached beforehand */
263 assert( !p_this->p_parent );
265 /* Send a kill to the object's thread if applicable */
266 vlc_object_kill( p_this );
268 /* Call the custom "subclass" destructor */
269 if( p_priv->pf_destructor )
270 p_priv->pf_destructor( p_this );
272 /* Any thread must have been cleaned up at this point. */
273 assert( !p_priv->b_thread );
275 /* Destroy the associated variables, starting from the end so that
276 * no memmove calls have to be done. */
277 while( p_priv->i_vars )
279 var_Destroy( p_this, p_priv->p_vars[p_priv->i_vars - 1].psz_name );
282 free( p_priv->p_vars );
283 vlc_cond_destroy( &p_priv->var_wait );
284 vlc_mutex_destroy( &p_priv->var_lock );
286 free( p_this->psz_header );
288 FREENULL( p_this->psz_object_name );
290 vlc_spin_destroy( &p_priv->ref_spin );
291 vlc_mutex_destroy( &p_priv->lock );
292 if( p_priv->pipes[1] != -1 && p_priv->pipes[1] != p_priv->pipes[0] )
293 close( p_priv->pipes[1] );
294 if( p_priv->pipes[0] != -1 )
295 close( p_priv->pipes[0] );
296 if( VLC_OBJECT(p_this->p_libvlc) == p_this )
297 vlc_mutex_destroy (&(libvlc_priv ((libvlc_int_t *)p_this)->structure_lock));
303 /** Inter-object signaling */
305 void __vlc_object_lock( vlc_object_t *obj )
307 vlc_mutex_lock( &(vlc_internals(obj)->lock) );
310 void __vlc_object_unlock( vlc_object_t *obj )
312 vlc_assert_locked( &(vlc_internals(obj)->lock) );
313 vlc_mutex_unlock( &(vlc_internals(obj)->lock) );
315 void __vlc_object_assert_locked( vlc_object_t *obj )
317 vlc_assert_locked( &(vlc_internals(obj)->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)
374 * Returns the readable end of a pipe that becomes readable once termination
375 * of the object is requested (vlc_object_kill()).
376 * This can be used to wake-up out of a select() or poll() event loop, such
377 * typically when doing network I/O.
379 * Note that the pipe will remain the same for the lifetime of the object.
380 * DO NOT read the pipe nor close it yourself. Ever.
382 * @param obj object that would be "killed"
383 * @return a readable pipe descriptor, or -1 on error.
385 int vlc_object_waitpipe( vlc_object_t *obj )
387 vlc_object_internals_t *internals = vlc_internals( obj );
389 vlc_object_lock (obj);
390 if (internals->pipes[0] == -1)
392 /* This can only ever happen if someone killed us without locking: */
393 assert (internals->pipes[1] == -1);
395 #ifdef HAVE_SYS_EVENTFD_H
396 internals->pipes[0] = internals->pipes[1] = eventfd (0, 0);
397 if (internals->pipes[0] == -1)
400 if (pipe (internals->pipes))
401 internals->pipes[0] = internals->pipes[1] = -1;
404 if (internals->pipes[0] != -1 && obj->b_die)
405 { /* Race condition: vlc_object_kill() already invoked! */
406 msg_Dbg (obj, "waitpipe: object already dying");
407 write (internals->pipes[1], &(uint64_t){ 1 }, sizeof (uint64_t));
410 vlc_object_unlock (obj);
411 return internals->pipes[0];
416 * Requests termination of an object, cancels the object thread, and make the
417 * object wait pipe (if it exists) readable. Not a cancellation point.
419 void __vlc_object_kill( vlc_object_t *p_this )
421 vlc_object_internals_t *priv = vlc_internals( p_this );
424 vlc_thread_cancel( p_this );
425 vlc_object_lock( p_this );
429 p_this->b_die = true;
432 /* This also serves as a memory barrier toward vlc_object_alive(): */
433 vlc_object_unlock( p_this );
437 int canc = vlc_savecancel ();
439 /* write _after_ setting b_die, so vlc_object_alive() returns false */
440 write (fd, &(uint64_t){ 1 }, sizeof (uint64_t));
441 msg_Dbg (p_this, "waitpipe: object killed");
442 vlc_restorecancel (canc);
447 /*****************************************************************************
448 * find a typed object and increment its refcount
449 *****************************************************************************
450 * This function recursively looks for a given object type. i_mode can be one
451 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
452 *****************************************************************************/
453 void * __vlc_object_find( vlc_object_t *p_this, int i_type, int i_mode )
455 vlc_object_t *p_found;
457 /* If we are of the requested type ourselves, don't look further */
458 if( !(i_mode & FIND_STRICT)
459 && vlc_internals (p_this)->i_object_type == i_type )
461 vlc_object_hold( p_this );
465 /* Otherwise, recursively look for the object */
466 if ((i_mode & 0x000f) == FIND_ANYWHERE)
467 return vlc_object_find (p_this->p_libvlc, i_type,
468 (i_mode & ~0x000f)|FIND_CHILD);
470 libvlc_lock (p_this->p_libvlc);
471 p_found = FindObject( p_this, i_type, i_mode );
472 libvlc_unlock (p_this->p_libvlc);
476 #undef vlc_object_find_name
478 * Finds a named object and increment its reference count.
479 * Beware that objects found in this manner can be "owned" by another thread,
480 * be of _any_ type, and be attached to any module (if any). With such an
481 * object reference, you can set or get object variables, emit log messages,
482 * and read write-once object parameters (psz_object_type, etc).
483 * You CANNOT cast the object to a more specific object type, and you
484 * definitely cannot invoke object type-specific callbacks with this.
486 * @param p_this object to search from
487 * @param psz_name name of the object to search for
488 * @param i_mode search direction: FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
490 * @return a matching object (must be released by the caller),
493 vlc_object_t *vlc_object_find_name( vlc_object_t *p_this,
494 const char *psz_name, int i_mode )
496 vlc_object_t *p_found;
498 /* If have the requested name ourselves, don't look further */
499 if( !(i_mode & FIND_STRICT)
500 && p_this->psz_object_name
501 && !strcmp( p_this->psz_object_name, psz_name ) )
503 vlc_object_hold( p_this );
507 libvlc_lock (p_this->p_libvlc);
509 /* Otherwise, recursively look for the object */
510 if( (i_mode & 0x000f) == FIND_ANYWHERE )
512 vlc_object_t *p_root = p_this;
515 while( p_root->p_parent != NULL &&
516 p_root != VLC_OBJECT( p_this->p_libvlc ) )
518 p_root = p_root->p_parent;
521 p_found = FindObjectName( p_root, psz_name,
522 (i_mode & ~0x000f)|FIND_CHILD );
523 if( p_found == NULL && p_root != VLC_OBJECT( p_this->p_libvlc ) )
525 p_found = FindObjectName( VLC_OBJECT( p_this->p_libvlc ),
526 psz_name, (i_mode & ~0x000f)|FIND_CHILD );
531 p_found = FindObjectName( p_this, psz_name, i_mode );
534 libvlc_unlock (p_this->p_libvlc);
539 * Increment an object reference counter.
541 void * __vlc_object_hold( vlc_object_t *p_this )
543 vlc_object_internals_t *internals = vlc_internals( p_this );
545 vlc_spin_lock( &internals->ref_spin );
546 /* Avoid obvious freed object uses */
547 assert( internals->i_refcount > 0 );
548 /* Increment the counter */
549 internals->i_refcount++;
550 vlc_spin_unlock( &internals->ref_spin );
554 /*****************************************************************************
555 * Decrement an object refcount
556 * And destroy the object if its refcount reach zero.
557 *****************************************************************************/
558 void __vlc_object_release( vlc_object_t *p_this )
560 vlc_object_internals_t *internals = vlc_internals( p_this );
561 vlc_object_t *parent = NULL;
562 bool b_should_destroy;
564 vlc_spin_lock( &internals->ref_spin );
565 assert( internals->i_refcount > 0 );
567 if( internals->i_refcount > 1 )
570 /* There are still other references to the object */
571 internals->i_refcount--;
572 vlc_spin_unlock( &internals->ref_spin );
575 vlc_spin_unlock( &internals->ref_spin );
578 /* Remember that we cannot hold the spin while waiting on the mutex */
579 libvlc_lock (p_this->p_libvlc);
580 /* Take the spin again. Note that another thread may have held the
581 * object in the (very short) mean time. */
582 vlc_spin_lock( &internals->ref_spin );
583 b_should_destroy = --internals->i_refcount == 0;
584 vlc_spin_unlock( &internals->ref_spin );
586 if( b_should_destroy )
588 /* We have no children */
589 assert (internals->i_children == 0);
590 parent = p_this->p_parent;
593 if( VLC_OBJECT(p_this->p_libvlc) == p_this )
596 vlc_object_t *leaked = internals->next;
597 while( leaked != p_this )
599 /* We are leaking this object */
601 "ERROR: leaking object (%p, type:%s, name:%s)\n",
602 leaked, leaked->psz_object_type,
603 leaked->psz_object_name );
604 /* Dump object to ease debugging */
605 vlc_object_dump( leaked );
607 leaked = vlc_internals (leaked)->next;
610 if( internals->next != p_this )
611 /* Dump libvlc object to ease debugging */
612 vlc_object_dump( p_this );
615 /* Remove the object from object list
616 * so that it cannot be encountered by vlc_object_get() */
617 vlc_internals (internals->next)->prev = internals->prev;
618 vlc_internals (internals->prev)->next = internals->next;
621 /* Detach from parent to protect against FIND_CHILDREN */
622 vlc_object_detach_unlocked (p_this);
624 libvlc_unlock (p_this->p_libvlc);
626 if( b_should_destroy )
630 canc = vlc_savecancel ();
631 vlc_object_destroy( p_this );
632 vlc_restorecancel (canc);
634 vlc_object_release (parent);
639 ****************************************************************************
640 * attach object to a parent object
641 *****************************************************************************
642 * This function sets p_this as a child of p_parent, and p_parent as a parent
643 * of p_this. This link can be undone using vlc_object_detach.
644 *****************************************************************************/
645 void __vlc_object_attach( vlc_object_t *p_this, vlc_object_t *p_parent )
647 if( !p_this ) return;
649 vlc_object_hold (p_parent);
650 libvlc_lock (p_this->p_libvlc);
652 /* Attach the parent to its child */
653 assert (!p_this->p_parent);
654 p_this->p_parent = p_parent;
656 /* Attach the child to its parent */
657 vlc_object_internals_t *priv = vlc_internals( p_parent );
658 INSERT_ELEM( priv->pp_children, priv->i_children, priv->i_children,
660 libvlc_unlock (p_this->p_libvlc);
664 static void vlc_object_detach_unlocked (vlc_object_t *p_this)
666 if (p_this->p_parent == NULL)
669 vlc_object_internals_t *priv = vlc_internals( p_this->p_parent );
673 /* Remove p_this's parent */
674 p_this->p_parent = NULL;
676 /* Remove all of p_parent's children which are p_this */
677 for( i_index = priv->i_children ; i_index-- ; )
679 if( priv->pp_children[i_index] == p_this )
682 for( i = i_index ; i < priv->i_children ; i++ )
683 priv->pp_children[i] = priv->pp_children[i+1];
687 if( priv->i_children )
689 vlc_object_t **pp_children = (vlc_object_t **)
690 realloc( priv->pp_children,
691 priv->i_children * sizeof(vlc_object_t *) );
693 priv->pp_children = pp_children;
697 /* Special case - don't realloc() to zero to avoid leaking */
698 free( priv->pp_children );
699 priv->pp_children = NULL;
705 ****************************************************************************
706 * detach object from its parent
707 *****************************************************************************
708 * This function removes all links between an object and its parent.
709 *****************************************************************************/
710 void __vlc_object_detach( vlc_object_t *p_this )
712 vlc_object_t *p_parent;
713 if( !p_this ) return;
715 libvlc_lock (p_this->p_libvlc);
716 p_parent = p_this->p_parent;
718 vlc_object_detach_unlocked( p_this );
719 libvlc_unlock (p_this->p_libvlc);
722 vlc_object_release (p_parent);
727 ****************************************************************************
728 * find a list typed objects and increment their refcount
729 *****************************************************************************
730 * This function recursively looks for a given object type. i_mode can be one
731 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
732 *****************************************************************************/
733 vlc_list_t * vlc_list_find( vlc_object_t *p_this, int i_type, int i_mode )
738 /* Look for the objects */
739 switch( i_mode & 0x000f )
742 return vlc_list_find (VLC_OBJECT(p_this->p_libvlc), i_type, FIND_CHILD);
745 libvlc_lock (p_this->p_libvlc);
746 i_count = CountChildren( p_this, i_type );
747 p_list = NewList( i_count );
749 /* Check allocation was successful */
750 if( p_list->i_count != i_count )
752 libvlc_unlock (p_this->p_libvlc);
758 ListChildren( p_list, p_this, i_type );
759 libvlc_unlock (p_this->p_libvlc);
763 msg_Err( p_this, "unimplemented!" );
764 p_list = NewList( 0 );
772 * Gets the list of children of an objects, and increment their reference
774 * @return a list (possibly empty) or NULL in case of error.
776 vlc_list_t *__vlc_list_children( vlc_object_t *obj )
779 vlc_object_internals_t *priv = vlc_internals( obj );
781 libvlc_lock (obj->p_libvlc);
782 l = NewList( priv->i_children );
783 for (int i = 0; i < l->i_count; i++)
785 vlc_object_hold( priv->pp_children[i] );
786 l->p_values[i].p_object = priv->pp_children[i];
788 libvlc_unlock (obj->p_libvlc);
792 /*****************************************************************************
793 * DumpCommand: print the current vlc structure
794 *****************************************************************************
795 * This function prints either an ASCII tree showing the connections between
796 * vlc objects, and additional information such as their refcount, thread ID,
797 * etc. (command "tree"), or the same data as a simple list (command "list").
798 *****************************************************************************/
799 static int DumpCommand( vlc_object_t *p_this, char const *psz_cmd,
800 vlc_value_t oldval, vlc_value_t newval, void *p_data )
802 libvlc_int_t *p_libvlc = p_this->p_libvlc;
804 (void)oldval; (void)p_data;
805 if( *psz_cmd == 'l' )
807 vlc_object_t *cur = VLC_OBJECT (p_libvlc);
809 libvlc_lock (p_this->p_libvlc);
812 PrintObject (cur, "");
813 cur = vlc_internals (cur)->next;
815 while (cur != VLC_OBJECT(p_libvlc));
816 libvlc_unlock (p_this->p_libvlc);
820 vlc_object_t *p_object = NULL;
822 if( *newval.psz_string )
824 /* try using the object's name to find it */
825 p_object = vlc_object_find_name( p_this, newval.psz_string,
833 libvlc_lock (p_this->p_libvlc);
834 if( *psz_cmd == 't' )
836 char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
839 p_object = VLC_OBJECT(p_this->p_libvlc);
842 DumpStructure( p_object, 0, psz_foo );
844 else if( *psz_cmd == 'v' )
849 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
851 PrintObject( p_object, "" );
853 if( !vlc_internals( p_object )->i_vars )
854 printf( " `-o No variables\n" );
855 for( i = 0; i < vlc_internals( p_object )->i_vars; i++ )
857 variable_t *p_var = vlc_internals( p_object )->p_vars + i;
859 const char *psz_type = "unknown";
860 switch( p_var->i_type & VLC_VAR_TYPE )
862 #define MYCASE( type, nice ) \
863 case VLC_VAR_ ## type: \
866 MYCASE( VOID, "void" );
867 MYCASE( BOOL, "bool" );
868 MYCASE( INTEGER, "integer" );
869 MYCASE( HOTKEY, "hotkey" );
870 MYCASE( STRING, "string" );
871 MYCASE( MODULE, "module" );
872 MYCASE( FILE, "file" );
873 MYCASE( DIRECTORY, "directory" );
874 MYCASE( VARIABLE, "variable" );
875 MYCASE( FLOAT, "float" );
876 MYCASE( TIME, "time" );
877 MYCASE( ADDRESS, "address" );
878 MYCASE( MUTEX, "mutex" );
879 MYCASE( LIST, "list" );
882 printf( " %c-o \"%s\" (%s",
883 i + 1 == vlc_internals( p_object )->i_vars ? '`' : '|',
884 p_var->psz_name, psz_type );
885 if( p_var->psz_text )
886 printf( ", %s", p_var->psz_text );
888 if( p_var->i_type & VLC_VAR_HASCHOICE )
889 printf( ", has choices" );
890 if( p_var->i_type & VLC_VAR_ISCOMMAND )
891 printf( ", command" );
892 if( p_var->i_entries )
893 printf( ", %d callbacks", p_var->i_entries );
894 switch( p_var->i_type & VLC_VAR_CLASS )
900 printf( ": %s", p_var->val.b_bool ? "true" : "false" );
902 case VLC_VAR_INTEGER:
903 printf( ": %d", p_var->val.i_int );
906 printf( ": \"%s\"", p_var->val.psz_string );
909 printf( ": %f", p_var->val.f_float );
912 printf( ": %"PRIi64, (int64_t)p_var->val.i_time );
914 case VLC_VAR_ADDRESS:
915 printf( ": %p", p_var->val.p_address );
924 libvlc_unlock (p_this->p_libvlc);
926 if( *newval.psz_string )
928 vlc_object_release( p_object );
935 /*****************************************************************************
936 * vlc_list_release: free a list previously allocated by vlc_list_find
937 *****************************************************************************
938 * This function decreases the refcount of all objects in the list and
940 *****************************************************************************/
941 void vlc_list_release( vlc_list_t *p_list )
945 for( i_index = 0; i_index < p_list->i_count; i_index++ )
947 vlc_object_release( p_list->p_values[i_index].p_object );
950 free( p_list->p_values );
954 /*****************************************************************************
955 * dump an object. (Debug function)
956 *****************************************************************************/
958 static void vlc_object_dump( vlc_object_t *p_this )
960 char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
963 DumpStructure( p_this, 0, psz_foo );
967 /* Following functions are local */
969 static vlc_object_t * FindObject( vlc_object_t *p_this, int i_type, int i_mode )
974 switch( i_mode & 0x000f )
977 p_tmp = p_this->p_parent;
980 if( vlc_internals( p_tmp )->i_object_type == i_type )
982 vlc_object_hold( p_tmp );
987 return FindObject( p_tmp, i_type, i_mode );
993 for( i = vlc_internals( p_this )->i_children; i--; )
995 p_tmp = vlc_internals( p_this )->pp_children[i];
996 if( vlc_internals( p_tmp )->i_object_type == i_type )
998 vlc_object_hold( p_tmp );
1001 else if( vlc_internals( p_tmp )->i_children )
1003 p_tmp = FindObject( p_tmp, i_type, i_mode );
1013 /* Handled in vlc_object_find */
1020 static vlc_object_t * FindObjectName( vlc_object_t *p_this,
1021 const char *psz_name,
1025 vlc_object_t *p_tmp;
1027 switch( i_mode & 0x000f )
1030 p_tmp = p_this->p_parent;
1033 if( p_tmp->psz_object_name
1034 && !strcmp( p_tmp->psz_object_name, psz_name ) )
1036 vlc_object_hold( p_tmp );
1041 return FindObjectName( p_tmp, psz_name, i_mode );
1047 for( i = vlc_internals( p_this )->i_children; i--; )
1049 p_tmp = vlc_internals( p_this )->pp_children[i];
1050 if( p_tmp->psz_object_name
1051 && !strcmp( p_tmp->psz_object_name, psz_name ) )
1053 vlc_object_hold( p_tmp );
1056 else if( vlc_internals( p_tmp )->i_children )
1058 p_tmp = FindObjectName( p_tmp, psz_name, i_mode );
1068 /* Handled in vlc_object_find */
1076 static void PrintObject( vlc_object_t *p_this, const char *psz_prefix )
1078 char psz_children[20], psz_refcount[20], psz_thread[30], psz_name[50],
1081 int canc = vlc_savecancel ();
1082 memset( &psz_name, 0, sizeof(psz_name) );
1083 if( p_this->psz_object_name )
1085 snprintf( psz_name, 49, " \"%s\"", p_this->psz_object_name );
1087 psz_name[48] = '\"';
1090 psz_children[0] = '\0';
1091 switch( vlc_internals( p_this )->i_children )
1096 strcpy( psz_children, ", 1 child" );
1099 snprintf( psz_children, 19, ", %i children",
1100 vlc_internals( p_this )->i_children );
1104 psz_refcount[0] = '\0';
1105 if( vlc_internals( p_this )->i_refcount > 0 )
1106 snprintf( psz_refcount, 19, ", refcount %u",
1107 vlc_internals( p_this )->i_refcount );
1109 psz_thread[0] = '\0';
1110 if( vlc_internals( p_this )->b_thread )
1111 snprintf( psz_thread, 29, " (thread %lu)",
1112 (unsigned long)vlc_internals( p_this )->thread_id );
1114 psz_parent[0] = '\0';
1115 if( p_this->p_parent )
1116 snprintf( psz_parent, 19, ", parent %p", p_this->p_parent );
1118 printf( " %so %p %s%s%s%s%s%s\n", psz_prefix,
1119 p_this, p_this->psz_object_type,
1120 psz_name, psz_thread, psz_refcount, psz_children,
1122 vlc_restorecancel (canc);
1125 static void DumpStructure( vlc_object_t *p_this, int i_level, char *psz_foo )
1128 char i_back = psz_foo[i_level];
1129 psz_foo[i_level] = '\0';
1131 PrintObject( p_this, psz_foo );
1133 psz_foo[i_level] = i_back;
1135 if( i_level / 2 >= MAX_DUMPSTRUCTURE_DEPTH )
1137 msg_Warn( p_this, "structure tree is too deep" );
1141 for( i = 0 ; i < vlc_internals( p_this )->i_children ; i++ )
1145 psz_foo[i_level-1] = ' ';
1147 if( psz_foo[i_level-2] == '`' )
1149 psz_foo[i_level-2] = ' ';
1153 if( i == vlc_internals( p_this )->i_children - 1 )
1155 psz_foo[i_level] = '`';
1159 psz_foo[i_level] = '|';
1162 psz_foo[i_level+1] = '-';
1163 psz_foo[i_level+2] = '\0';
1165 DumpStructure( vlc_internals( p_this )->pp_children[i], i_level + 2,
1170 static vlc_list_t * NewList( int i_count )
1172 vlc_list_t * p_list = (vlc_list_t *)malloc( sizeof( vlc_list_t ) );
1173 if( p_list == NULL )
1178 p_list->i_count = i_count;
1182 p_list->p_values = NULL;
1186 p_list->p_values = malloc( i_count * sizeof( vlc_value_t ) );
1187 if( p_list->p_values == NULL )
1189 p_list->i_count = 0;
1196 static void ListReplace( vlc_list_t *p_list, vlc_object_t *p_object,
1199 if( p_list == NULL || i_index >= p_list->i_count )
1204 vlc_object_hold( p_object );
1206 p_list->p_values[i_index].p_object = p_object;
1211 /*static void ListAppend( vlc_list_t *p_list, vlc_object_t *p_object )
1213 if( p_list == NULL )
1218 p_list->p_values = realloc( p_list->p_values, (p_list->i_count + 1)
1219 * sizeof( vlc_value_t ) );
1220 if( p_list->p_values == NULL )
1222 p_list->i_count = 0;
1226 vlc_object_hold( p_object );
1228 p_list->p_values[p_list->i_count].p_object = p_object;
1234 static int CountChildren( vlc_object_t *p_this, int i_type )
1236 vlc_object_t *p_tmp;
1239 for( i = 0; i < vlc_internals( p_this )->i_children; i++ )
1241 p_tmp = vlc_internals( p_this )->pp_children[i];
1243 if( vlc_internals( p_tmp )->i_object_type == i_type )
1247 i_count += CountChildren( p_tmp, i_type );
1253 static void ListChildren( vlc_list_t *p_list, vlc_object_t *p_this, int i_type )
1255 vlc_object_t *p_tmp;
1258 for( i = 0; i < vlc_internals( p_this )->i_children; i++ )
1260 p_tmp = vlc_internals( p_this )->pp_children[i];
1262 if( vlc_internals( p_tmp )->i_object_type == i_type )
1263 ListReplace( p_list, p_tmp, p_list->i_count++ );
1265 ListChildren( p_list, p_tmp, i_type );