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"
44 #include "audio_output/aout_internal.h"
46 #include <vlc_access.h>
47 #include <vlc_demux.h>
48 #include <vlc_stream.h>
51 #include "stream_output/stream_output.h"
53 #include "vlc_interface.h"
54 #include "vlc_codec.h"
55 #include "vlc_filter.h"
57 #include "variables.h"
63 # include <errno.h> /* ENOSYS */
65 #ifdef HAVE_SYS_EVENTFD_H
66 # include <sys/eventfd.h>
70 /*****************************************************************************
72 *****************************************************************************/
73 static int DumpCommand( vlc_object_t *, char const *,
74 vlc_value_t, vlc_value_t, void * );
76 static vlc_object_t * FindObject ( vlc_object_t *, int, int );
77 static vlc_object_t * FindObjectName( vlc_object_t *, const char *, int );
78 static void PrintObject ( vlc_object_t *, const char * );
79 static void DumpStructure ( vlc_object_t *, int, char * );
81 static vlc_list_t * NewList ( int );
82 static void ListReplace ( vlc_list_t *, vlc_object_t *, int );
83 /*static void ListAppend ( vlc_list_t *, vlc_object_t * );*/
84 static int CountChildren ( vlc_object_t *, int );
85 static void ListChildren ( vlc_list_t *, vlc_object_t *, int );
87 static void vlc_object_destroy( vlc_object_t *p_this );
88 static void vlc_object_detach_unlocked (vlc_object_t *p_this);
90 static void vlc_object_dump( vlc_object_t *p_this );
93 /*****************************************************************************
94 * Local structure lock
95 *****************************************************************************/
96 static void libvlc_lock (libvlc_int_t *p_libvlc)
98 vlc_mutex_lock (&(libvlc_priv (p_libvlc)->structure_lock));
101 static void libvlc_unlock (libvlc_int_t *p_libvlc)
103 vlc_mutex_unlock (&(libvlc_priv (p_libvlc)->structure_lock));
106 void *__vlc_custom_create( vlc_object_t *p_this, size_t i_size,
107 int i_type, const char *psz_type )
110 vlc_object_internals_t *p_priv;
113 * VLC objects are laid out as follow:
114 * - first the LibVLC-private per-object data,
115 * - then VLC_COMMON members from vlc_object_t,
116 * - finally, the type-specific data (if any).
118 * This function initializes the LibVLC and common data,
119 * and zeroes the rest.
121 p_priv = calloc( 1, sizeof( *p_priv ) + i_size );
125 assert (i_size >= sizeof (vlc_object_t));
126 p_new = (vlc_object_t *)(p_priv + 1);
128 p_priv->i_object_type = i_type;
129 p_new->psz_object_type = psz_type;
130 p_new->psz_object_name = NULL;
132 p_new->b_die = false;
133 p_new->b_error = false;
134 p_new->b_force = false;
136 p_new->psz_header = NULL;
139 p_new->i_flags = p_this->i_flags
140 & (OBJECT_FLAGS_NODBG|OBJECT_FLAGS_QUIET|OBJECT_FLAGS_NOINTERACT);
142 p_priv->p_vars = calloc( 16, sizeof( variable_t ) );
144 if( !p_priv->p_vars )
152 libvlc_int_t *self = (libvlc_int_t*)p_new;
153 p_new->p_libvlc = self;
154 vlc_mutex_init (&(libvlc_priv (self)->structure_lock));
155 p_this = p_priv->next = p_priv->prev = p_new;
158 p_new->p_libvlc = p_this->p_libvlc;
160 vlc_spin_init( &p_priv->ref_spin );
161 p_priv->i_refcount = 1;
162 p_priv->pf_destructor = NULL;
163 p_priv->b_thread = false;
164 p_new->p_parent = NULL;
165 p_priv->pp_children = NULL;
166 p_priv->i_children = 0;
168 p_new->p_private = NULL;
170 /* Initialize mutexes and condvars */
171 vlc_mutex_init( &p_priv->lock );
172 vlc_mutex_init( &p_priv->var_lock );
173 vlc_cond_init( &p_priv->var_wait );
174 p_priv->pipes[0] = p_priv->pipes[1] = -1;
176 p_priv->next = p_this;
177 libvlc_lock (p_new->p_libvlc);
178 p_priv->prev = vlc_internals (p_this)->prev;
179 vlc_internals (p_this)->prev = p_new;
180 vlc_internals (p_priv->prev)->next = p_new;
181 libvlc_unlock (p_new->p_libvlc);
183 if (p_new == VLC_OBJECT(p_new->p_libvlc))
184 { /* TODO: should be in src/libvlc.c */
185 int canc = vlc_savecancel ();
186 var_Create( p_new, "list", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
187 var_AddCallback( p_new, "list", DumpCommand, NULL );
188 var_Create( p_new, "tree", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
189 var_AddCallback( p_new, "tree", DumpCommand, NULL );
190 var_Create( p_new, "vars", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
191 var_AddCallback( p_new, "vars", DumpCommand, NULL );
192 vlc_restorecancel (canc);
200 * Allocates and initializes a vlc object.
202 * @param i_type known object type (all of them are negative integer values),
203 * or object byte size (always positive).
205 * @return the new object, or NULL on error.
207 void * __vlc_object_create( vlc_object_t *p_this, int i_type )
209 const char * psz_type;
214 case VLC_OBJECT_INTF:
215 i_size = sizeof(intf_thread_t);
216 psz_type = "interface";
218 case VLC_OBJECT_DECODER:
219 i_size = sizeof(decoder_t);
220 psz_type = "decoder";
222 case VLC_OBJECT_PACKETIZER:
223 i_size = sizeof(decoder_t);
224 psz_type = "packetizer";
226 case VLC_OBJECT_AOUT:
227 i_size = sizeof(aout_instance_t);
228 psz_type = "audio output";
231 assert( i_type > 0 ); /* unknown type?! */
233 i_type = VLC_OBJECT_GENERIC;
234 psz_type = "generic";
238 return vlc_custom_create( p_this, i_size, i_type, psz_type );
243 ****************************************************************************
244 * Set the destructor of a vlc object
246 * This function sets the destructor of the vlc object. It will be called
247 * when the object is destroyed when the its refcount reaches 0.
248 * (It is called by the internal function vlc_object_destroy())
249 *****************************************************************************/
250 void __vlc_object_set_destructor( vlc_object_t *p_this,
251 vlc_destructor_t pf_destructor )
253 vlc_object_internals_t *p_priv = vlc_internals(p_this );
254 p_priv->pf_destructor = pf_destructor;
258 ****************************************************************************
259 * Destroy a vlc object (Internal)
261 * This function destroys an object that has been previously allocated with
262 * vlc_object_create. The object's refcount must be zero and it must not be
263 * attached to other objects in any way.
265 * This function must be called with cancellation disabled (currently).
266 *****************************************************************************/
267 static void vlc_object_destroy( vlc_object_t *p_this )
269 vlc_object_internals_t *p_priv = vlc_internals( p_this );
271 /* Objects are always detached beforehand */
272 assert( !p_this->p_parent );
274 /* Send a kill to the object's thread if applicable */
275 vlc_object_kill( p_this );
277 /* Call the custom "subclass" destructor */
278 if( p_priv->pf_destructor )
279 p_priv->pf_destructor( p_this );
281 /* Any thread must have been cleaned up at this point. */
282 assert( !p_priv->b_thread );
284 /* Destroy the associated variables, starting from the end so that
285 * no memmove calls have to be done. */
286 while( p_priv->i_vars )
288 var_Destroy( p_this, p_priv->p_vars[p_priv->i_vars - 1].psz_name );
291 free( p_priv->p_vars );
292 vlc_cond_destroy( &p_priv->var_wait );
293 vlc_mutex_destroy( &p_priv->var_lock );
295 free( p_this->psz_header );
297 FREENULL( p_this->psz_object_name );
299 vlc_spin_destroy( &p_priv->ref_spin );
300 vlc_mutex_destroy( &p_priv->lock );
301 if( p_priv->pipes[1] != -1 && p_priv->pipes[1] != p_priv->pipes[0] )
302 close( p_priv->pipes[1] );
303 if( p_priv->pipes[0] != -1 )
304 close( p_priv->pipes[0] );
305 if( VLC_OBJECT(p_this->p_libvlc) == p_this )
306 vlc_mutex_destroy (&(libvlc_priv ((libvlc_int_t *)p_this)->structure_lock));
312 /** Inter-object signaling */
314 void __vlc_object_lock( vlc_object_t *obj )
316 vlc_mutex_lock( &(vlc_internals(obj)->lock) );
319 void __vlc_object_unlock( vlc_object_t *obj )
321 vlc_assert_locked( &(vlc_internals(obj)->lock) );
322 vlc_mutex_unlock( &(vlc_internals(obj)->lock) );
324 void __vlc_object_assert_locked( vlc_object_t *obj )
326 vlc_assert_locked( &(vlc_internals(obj)->lock) );
330 # include <winsock2.h>
331 # include <ws2tcpip.h>
334 * select()-able pipes emulated using Winsock
336 static int pipe (int fd[2])
339 int addrlen = sizeof (addr);
341 SOCKET l = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP), a,
342 c = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
343 if ((l == INVALID_SOCKET) || (c == INVALID_SOCKET))
346 memset (&addr, 0, sizeof (addr));
347 addr.sin_family = AF_INET;
348 addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
349 if (bind (l, (PSOCKADDR)&addr, sizeof (addr))
350 || getsockname (l, (PSOCKADDR)&addr, &addrlen)
352 || connect (c, (PSOCKADDR)&addr, addrlen))
355 a = accept (l, NULL, NULL);
356 if (a == INVALID_SOCKET)
367 if (l != INVALID_SOCKET)
369 if (c != INVALID_SOCKET)
375 #define read( a, b, c ) recv (a, b, c, 0)
377 #define write( a, b, c ) send (a, b, c, 0)
379 #define close( a ) closesocket (a)
383 * Returns the readable end of a pipe that becomes readable once termination
384 * of the object is requested (vlc_object_kill()).
385 * This can be used to wake-up out of a select() or poll() event loop, such
386 * typically when doing network I/O.
388 * Note that the pipe will remain the same for the lifetime of the object.
389 * DO NOT read the pipe nor close it yourself. Ever.
391 * @param obj object that would be "killed"
392 * @return a readable pipe descriptor, or -1 on error.
394 int vlc_object_waitpipe( vlc_object_t *obj )
396 vlc_object_internals_t *internals = vlc_internals( obj );
398 vlc_object_lock (obj);
399 if (internals->pipes[0] == -1)
401 /* This can only ever happen if someone killed us without locking: */
402 assert (internals->pipes[1] == -1);
404 #ifdef HAVE_SYS_EVENTFD_H
405 internals->pipes[0] = internals->pipes[1] = eventfd (0, 0);
406 if (internals->pipes[0] == -1)
409 if (pipe (internals->pipes))
410 internals->pipes[0] = internals->pipes[1] = -1;
413 if (internals->pipes[0] != -1 && obj->b_die)
414 { /* Race condition: vlc_object_kill() already invoked! */
415 msg_Dbg (obj, "waitpipe: object already dying");
416 write (internals->pipes[1], &(uint64_t){ 1 }, sizeof (uint64_t));
419 vlc_object_unlock (obj);
420 return internals->pipes[0];
425 * Requests termination of an object, cancels the object thread, and make the
426 * object wait pipe (if it exists) readable. Not a cancellation point.
428 void __vlc_object_kill( vlc_object_t *p_this )
430 vlc_object_internals_t *priv = vlc_internals( p_this );
433 vlc_thread_cancel( p_this );
434 vlc_object_lock( p_this );
438 p_this->b_die = true;
441 /* This also serves as a memory barrier toward vlc_object_alive(): */
442 vlc_object_unlock( p_this );
446 int canc = vlc_savecancel ();
448 /* write _after_ setting b_die, so vlc_object_alive() returns false */
449 write (fd, &(uint64_t){ 1 }, sizeof (uint64_t));
450 msg_Dbg (p_this, "waitpipe: object killed");
451 vlc_restorecancel (canc);
456 /*****************************************************************************
457 * find a typed object and increment its refcount
458 *****************************************************************************
459 * This function recursively looks for a given object type. i_mode can be one
460 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
461 *****************************************************************************/
462 void * __vlc_object_find( vlc_object_t *p_this, int i_type, int i_mode )
464 vlc_object_t *p_found;
466 /* If we are of the requested type ourselves, don't look further */
467 if( !(i_mode & FIND_STRICT)
468 && vlc_internals (p_this)->i_object_type == i_type )
470 vlc_object_hold( p_this );
474 /* Otherwise, recursively look for the object */
475 if ((i_mode & 0x000f) == FIND_ANYWHERE)
476 return vlc_object_find (p_this->p_libvlc, i_type,
477 (i_mode & ~0x000f)|FIND_CHILD);
479 libvlc_lock (p_this->p_libvlc);
480 p_found = FindObject( p_this, i_type, i_mode );
481 libvlc_unlock (p_this->p_libvlc);
485 #undef vlc_object_find_name
487 * Finds a named object and increment its reference count.
488 * Beware that objects found in this manner can be "owned" by another thread,
489 * be of _any_ type, and be attached to any module (if any). With such an
490 * object reference, you can set or get object variables, emit log messages,
491 * and read write-once object parameters (psz_object_type, etc).
492 * You CANNOT cast the object to a more specific object type, and you
493 * definitely cannot invoke object type-specific callbacks with this.
495 * @param p_this object to search from
496 * @param psz_name name of the object to search for
497 * @param i_mode search direction: FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
499 * @return a matching object (must be released by the caller),
502 vlc_object_t *vlc_object_find_name( vlc_object_t *p_this,
503 const char *psz_name, int i_mode )
505 vlc_object_t *p_found;
507 /* If have the requested name ourselves, don't look further */
508 if( !(i_mode & FIND_STRICT)
509 && p_this->psz_object_name
510 && !strcmp( p_this->psz_object_name, psz_name ) )
512 vlc_object_hold( p_this );
516 libvlc_lock (p_this->p_libvlc);
518 /* Otherwise, recursively look for the object */
519 if( (i_mode & 0x000f) == FIND_ANYWHERE )
521 vlc_object_t *p_root = p_this;
524 while( p_root->p_parent != NULL &&
525 p_root != VLC_OBJECT( p_this->p_libvlc ) )
527 p_root = p_root->p_parent;
530 p_found = FindObjectName( p_root, psz_name,
531 (i_mode & ~0x000f)|FIND_CHILD );
532 if( p_found == NULL && p_root != VLC_OBJECT( p_this->p_libvlc ) )
534 p_found = FindObjectName( VLC_OBJECT( p_this->p_libvlc ),
535 psz_name, (i_mode & ~0x000f)|FIND_CHILD );
540 p_found = FindObjectName( p_this, psz_name, i_mode );
543 libvlc_unlock (p_this->p_libvlc);
548 * Increment an object reference counter.
550 void * __vlc_object_hold( vlc_object_t *p_this )
552 vlc_object_internals_t *internals = vlc_internals( p_this );
554 vlc_spin_lock( &internals->ref_spin );
555 /* Avoid obvious freed object uses */
556 assert( internals->i_refcount > 0 );
557 /* Increment the counter */
558 internals->i_refcount++;
559 vlc_spin_unlock( &internals->ref_spin );
563 /*****************************************************************************
564 * Decrement an object refcount
565 * And destroy the object if its refcount reach zero.
566 *****************************************************************************/
567 void __vlc_object_release( vlc_object_t *p_this )
569 vlc_object_internals_t *internals = vlc_internals( p_this );
570 vlc_object_t *parent = NULL;
571 bool b_should_destroy;
573 vlc_spin_lock( &internals->ref_spin );
574 assert( internals->i_refcount > 0 );
576 if( internals->i_refcount > 1 )
579 /* There are still other references to the object */
580 internals->i_refcount--;
581 vlc_spin_unlock( &internals->ref_spin );
584 vlc_spin_unlock( &internals->ref_spin );
587 /* Remember that we cannot hold the spin while waiting on the mutex */
588 libvlc_lock (p_this->p_libvlc);
589 /* Take the spin again. Note that another thread may have held the
590 * object in the (very short) mean time. */
591 vlc_spin_lock( &internals->ref_spin );
592 b_should_destroy = --internals->i_refcount == 0;
593 vlc_spin_unlock( &internals->ref_spin );
595 if( b_should_destroy )
597 /* We have no children */
598 assert (internals->i_children == 0);
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 leaked->psz_object_name );
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 libvlc_unlock (p_this->p_libvlc);
635 if( b_should_destroy )
639 canc = vlc_savecancel ();
640 vlc_object_destroy( p_this );
641 vlc_restorecancel (canc);
643 vlc_object_release (parent);
648 ****************************************************************************
649 * attach object to a parent object
650 *****************************************************************************
651 * This function sets p_this as a child of p_parent, and p_parent as a parent
652 * of p_this. This link can be undone using vlc_object_detach.
653 *****************************************************************************/
654 void __vlc_object_attach( vlc_object_t *p_this, vlc_object_t *p_parent )
656 if( !p_this ) return;
658 vlc_object_hold (p_parent);
659 libvlc_lock (p_this->p_libvlc);
661 /* Attach the parent to its child */
662 assert (!p_this->p_parent);
663 p_this->p_parent = p_parent;
665 /* Attach the child to its parent */
666 vlc_object_internals_t *priv = vlc_internals( p_parent );
667 INSERT_ELEM( priv->pp_children, priv->i_children, priv->i_children,
669 libvlc_unlock (p_this->p_libvlc);
673 static void vlc_object_detach_unlocked (vlc_object_t *p_this)
675 if (p_this->p_parent == NULL)
678 vlc_object_internals_t *priv = vlc_internals( p_this->p_parent );
682 /* Remove p_this's parent */
683 p_this->p_parent = NULL;
685 /* Remove all of p_parent's children which are p_this */
686 for( i_index = priv->i_children ; i_index-- ; )
688 if( priv->pp_children[i_index] == p_this )
691 for( i = i_index ; i < priv->i_children ; i++ )
692 priv->pp_children[i] = priv->pp_children[i+1];
696 if( priv->i_children )
698 vlc_object_t **pp_children = (vlc_object_t **)
699 realloc( priv->pp_children,
700 priv->i_children * sizeof(vlc_object_t *) );
702 priv->pp_children = pp_children;
706 /* Special case - don't realloc() to zero to avoid leaking */
707 free( priv->pp_children );
708 priv->pp_children = NULL;
714 ****************************************************************************
715 * detach object from its parent
716 *****************************************************************************
717 * This function removes all links between an object and its parent.
718 *****************************************************************************/
719 void __vlc_object_detach( vlc_object_t *p_this )
721 vlc_object_t *p_parent;
722 if( !p_this ) return;
724 libvlc_lock (p_this->p_libvlc);
725 p_parent = p_this->p_parent;
727 vlc_object_detach_unlocked( p_this );
728 libvlc_unlock (p_this->p_libvlc);
731 vlc_object_release (p_parent);
736 ****************************************************************************
737 * find a list typed objects and increment their refcount
738 *****************************************************************************
739 * This function recursively looks for a given object type. i_mode can be one
740 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
741 *****************************************************************************/
742 vlc_list_t * vlc_list_find( vlc_object_t *p_this, int i_type, int i_mode )
747 /* Look for the objects */
748 switch( i_mode & 0x000f )
751 return vlc_list_find (VLC_OBJECT(p_this->p_libvlc), i_type, FIND_CHILD);
754 libvlc_lock (p_this->p_libvlc);
755 i_count = CountChildren( p_this, i_type );
756 p_list = NewList( i_count );
758 /* Check allocation was successful */
759 if( p_list->i_count != i_count )
761 libvlc_unlock (p_this->p_libvlc);
767 ListChildren( p_list, p_this, i_type );
768 libvlc_unlock (p_this->p_libvlc);
772 msg_Err( p_this, "unimplemented!" );
773 p_list = NewList( 0 );
781 * Gets the list of children of an objects, and increment their reference
783 * @return a list (possibly empty) or NULL in case of error.
785 vlc_list_t *__vlc_list_children( vlc_object_t *obj )
788 vlc_object_internals_t *priv = vlc_internals( obj );
790 libvlc_lock (obj->p_libvlc);
791 l = NewList( priv->i_children );
792 for (int i = 0; i < l->i_count; i++)
794 vlc_object_hold( priv->pp_children[i] );
795 l->p_values[i].p_object = priv->pp_children[i];
797 libvlc_unlock (obj->p_libvlc);
801 /*****************************************************************************
802 * DumpCommand: print the current vlc structure
803 *****************************************************************************
804 * This function prints either an ASCII tree showing the connections between
805 * vlc objects, and additional information such as their refcount, thread ID,
806 * etc. (command "tree"), or the same data as a simple list (command "list").
807 *****************************************************************************/
808 static int DumpCommand( vlc_object_t *p_this, char const *psz_cmd,
809 vlc_value_t oldval, vlc_value_t newval, void *p_data )
811 libvlc_int_t *p_libvlc = p_this->p_libvlc;
813 (void)oldval; (void)p_data;
814 if( *psz_cmd == 'l' )
816 vlc_object_t *cur = VLC_OBJECT (p_libvlc);
818 libvlc_lock (p_this->p_libvlc);
821 PrintObject (cur, "");
822 cur = vlc_internals (cur)->next;
824 while (cur != VLC_OBJECT(p_libvlc));
825 libvlc_unlock (p_this->p_libvlc);
829 vlc_object_t *p_object = NULL;
831 if( *newval.psz_string )
833 /* try using the object's name to find it */
834 p_object = vlc_object_find_name( p_this, newval.psz_string,
842 libvlc_lock (p_this->p_libvlc);
843 if( *psz_cmd == 't' )
845 char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
848 p_object = VLC_OBJECT(p_this->p_libvlc);
851 DumpStructure( p_object, 0, psz_foo );
853 else if( *psz_cmd == 'v' )
858 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
860 PrintObject( p_object, "" );
862 if( !vlc_internals( p_object )->i_vars )
863 printf( " `-o No variables\n" );
864 for( i = 0; i < vlc_internals( p_object )->i_vars; i++ )
866 variable_t *p_var = vlc_internals( p_object )->p_vars + i;
868 const char *psz_type = "unknown";
869 switch( p_var->i_type & VLC_VAR_TYPE )
871 #define MYCASE( type, nice ) \
872 case VLC_VAR_ ## type: \
875 MYCASE( VOID, "void" );
876 MYCASE( BOOL, "bool" );
877 MYCASE( INTEGER, "integer" );
878 MYCASE( HOTKEY, "hotkey" );
879 MYCASE( STRING, "string" );
880 MYCASE( MODULE, "module" );
881 MYCASE( FILE, "file" );
882 MYCASE( DIRECTORY, "directory" );
883 MYCASE( VARIABLE, "variable" );
884 MYCASE( FLOAT, "float" );
885 MYCASE( TIME, "time" );
886 MYCASE( ADDRESS, "address" );
887 MYCASE( MUTEX, "mutex" );
888 MYCASE( LIST, "list" );
891 printf( " %c-o \"%s\" (%s",
892 i + 1 == vlc_internals( p_object )->i_vars ? '`' : '|',
893 p_var->psz_name, psz_type );
894 if( p_var->psz_text )
895 printf( ", %s", p_var->psz_text );
897 if( p_var->i_type & VLC_VAR_HASCHOICE )
898 printf( ", has choices" );
899 if( p_var->i_type & VLC_VAR_ISCOMMAND )
900 printf( ", command" );
901 if( p_var->i_entries )
902 printf( ", %d callbacks", p_var->i_entries );
903 switch( p_var->i_type & VLC_VAR_CLASS )
909 printf( ": %s", p_var->val.b_bool ? "true" : "false" );
911 case VLC_VAR_INTEGER:
912 printf( ": %d", p_var->val.i_int );
915 printf( ": \"%s\"", p_var->val.psz_string );
918 printf( ": %f", p_var->val.f_float );
921 printf( ": %"PRIi64, (int64_t)p_var->val.i_time );
923 case VLC_VAR_ADDRESS:
924 printf( ": %p", p_var->val.p_address );
933 libvlc_unlock (p_this->p_libvlc);
935 if( *newval.psz_string )
937 vlc_object_release( p_object );
944 /*****************************************************************************
945 * vlc_list_release: free a list previously allocated by vlc_list_find
946 *****************************************************************************
947 * This function decreases the refcount of all objects in the list and
949 *****************************************************************************/
950 void vlc_list_release( vlc_list_t *p_list )
954 for( i_index = 0; i_index < p_list->i_count; i_index++ )
956 vlc_object_release( p_list->p_values[i_index].p_object );
959 free( p_list->p_values );
963 /*****************************************************************************
964 * dump an object. (Debug function)
965 *****************************************************************************/
967 static void vlc_object_dump( vlc_object_t *p_this )
969 char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
972 DumpStructure( p_this, 0, psz_foo );
976 /* Following functions are local */
978 static vlc_object_t * FindObject( vlc_object_t *p_this, int i_type, int i_mode )
983 switch( i_mode & 0x000f )
986 p_tmp = p_this->p_parent;
989 if( vlc_internals( p_tmp )->i_object_type == i_type )
991 vlc_object_hold( p_tmp );
996 return FindObject( p_tmp, i_type, i_mode );
1002 for( i = vlc_internals( p_this )->i_children; i--; )
1004 p_tmp = vlc_internals( p_this )->pp_children[i];
1005 if( vlc_internals( p_tmp )->i_object_type == i_type )
1007 vlc_object_hold( p_tmp );
1010 else if( vlc_internals( p_tmp )->i_children )
1012 p_tmp = FindObject( p_tmp, i_type, i_mode );
1022 /* Handled in vlc_object_find */
1029 static vlc_object_t * FindObjectName( vlc_object_t *p_this,
1030 const char *psz_name,
1034 vlc_object_t *p_tmp;
1036 switch( i_mode & 0x000f )
1039 p_tmp = p_this->p_parent;
1042 if( p_tmp->psz_object_name
1043 && !strcmp( p_tmp->psz_object_name, psz_name ) )
1045 vlc_object_hold( p_tmp );
1050 return FindObjectName( p_tmp, psz_name, i_mode );
1056 for( i = vlc_internals( p_this )->i_children; i--; )
1058 p_tmp = vlc_internals( p_this )->pp_children[i];
1059 if( p_tmp->psz_object_name
1060 && !strcmp( p_tmp->psz_object_name, psz_name ) )
1062 vlc_object_hold( p_tmp );
1065 else if( vlc_internals( p_tmp )->i_children )
1067 p_tmp = FindObjectName( p_tmp, psz_name, i_mode );
1077 /* Handled in vlc_object_find */
1085 static void PrintObject( vlc_object_t *p_this, const char *psz_prefix )
1087 char psz_children[20], psz_refcount[20], psz_thread[30], psz_name[50],
1090 int canc = vlc_savecancel ();
1091 memset( &psz_name, 0, sizeof(psz_name) );
1092 if( p_this->psz_object_name )
1094 snprintf( psz_name, 49, " \"%s\"", p_this->psz_object_name );
1096 psz_name[48] = '\"';
1099 psz_children[0] = '\0';
1100 switch( vlc_internals( p_this )->i_children )
1105 strcpy( psz_children, ", 1 child" );
1108 snprintf( psz_children, 19, ", %i children",
1109 vlc_internals( p_this )->i_children );
1113 psz_refcount[0] = '\0';
1114 if( vlc_internals( p_this )->i_refcount > 0 )
1115 snprintf( psz_refcount, 19, ", refcount %u",
1116 vlc_internals( p_this )->i_refcount );
1118 psz_thread[0] = '\0';
1119 if( vlc_internals( p_this )->b_thread )
1120 snprintf( psz_thread, 29, " (thread %lu)",
1121 (unsigned long)vlc_internals( p_this )->thread_id );
1123 psz_parent[0] = '\0';
1124 if( p_this->p_parent )
1125 snprintf( psz_parent, 19, ", parent %p", p_this->p_parent );
1127 printf( " %so %p %s%s%s%s%s%s\n", psz_prefix,
1128 p_this, p_this->psz_object_type,
1129 psz_name, psz_thread, psz_refcount, psz_children,
1131 vlc_restorecancel (canc);
1134 static void DumpStructure( vlc_object_t *p_this, int i_level, char *psz_foo )
1137 char i_back = psz_foo[i_level];
1138 psz_foo[i_level] = '\0';
1140 PrintObject( p_this, psz_foo );
1142 psz_foo[i_level] = i_back;
1144 if( i_level / 2 >= MAX_DUMPSTRUCTURE_DEPTH )
1146 msg_Warn( p_this, "structure tree is too deep" );
1150 for( i = 0 ; i < vlc_internals( p_this )->i_children ; i++ )
1154 psz_foo[i_level-1] = ' ';
1156 if( psz_foo[i_level-2] == '`' )
1158 psz_foo[i_level-2] = ' ';
1162 if( i == vlc_internals( p_this )->i_children - 1 )
1164 psz_foo[i_level] = '`';
1168 psz_foo[i_level] = '|';
1171 psz_foo[i_level+1] = '-';
1172 psz_foo[i_level+2] = '\0';
1174 DumpStructure( vlc_internals( p_this )->pp_children[i], i_level + 2,
1179 static vlc_list_t * NewList( int i_count )
1181 vlc_list_t * p_list = (vlc_list_t *)malloc( sizeof( vlc_list_t ) );
1182 if( p_list == NULL )
1187 p_list->i_count = i_count;
1191 p_list->p_values = NULL;
1195 p_list->p_values = malloc( i_count * sizeof( vlc_value_t ) );
1196 if( p_list->p_values == NULL )
1198 p_list->i_count = 0;
1205 static void ListReplace( vlc_list_t *p_list, vlc_object_t *p_object,
1208 if( p_list == NULL || i_index >= p_list->i_count )
1213 vlc_object_hold( p_object );
1215 p_list->p_values[i_index].p_object = p_object;
1220 /*static void ListAppend( vlc_list_t *p_list, vlc_object_t *p_object )
1222 if( p_list == NULL )
1227 p_list->p_values = realloc( p_list->p_values, (p_list->i_count + 1)
1228 * sizeof( vlc_value_t ) );
1229 if( p_list->p_values == NULL )
1231 p_list->i_count = 0;
1235 vlc_object_hold( p_object );
1237 p_list->p_values[p_list->i_count].p_object = p_object;
1243 static int CountChildren( vlc_object_t *p_this, int i_type )
1245 vlc_object_t *p_tmp;
1248 for( i = 0; i < vlc_internals( p_this )->i_children; i++ )
1250 p_tmp = vlc_internals( p_this )->pp_children[i];
1252 if( vlc_internals( p_tmp )->i_object_type == i_type )
1256 i_count += CountChildren( p_tmp, i_type );
1262 static void ListChildren( vlc_list_t *p_list, vlc_object_t *p_this, int i_type )
1264 vlc_object_t *p_tmp;
1267 for( i = 0; i < vlc_internals( p_this )->i_children; i++ )
1269 p_tmp = vlc_internals( p_this )->pp_children[i];
1271 if( vlc_internals( p_tmp )->i_object_type == i_type )
1272 ListReplace( p_list, p_tmp, p_list->i_count++ );
1274 ListChildren( p_list, p_tmp, i_type );