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_priv->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 /* Initialize mutexes and condvars */
164 vlc_mutex_init( &p_priv->var_lock );
165 vlc_cond_init( &p_priv->var_wait );
166 p_priv->pipes[0] = p_priv->pipes[1] = -1;
168 p_priv->next = p_this;
169 libvlc_lock (p_new->p_libvlc);
170 p_priv->prev = vlc_internals (p_this)->prev;
171 vlc_internals (p_this)->prev = p_new;
172 vlc_internals (p_priv->prev)->next = p_new;
173 libvlc_unlock (p_new->p_libvlc);
175 if (p_new == VLC_OBJECT(p_new->p_libvlc))
176 { /* TODO: should be in src/libvlc.c */
177 int canc = vlc_savecancel ();
178 var_Create( p_new, "list", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
179 var_AddCallback( p_new, "list", DumpCommand, NULL );
180 var_Create( p_new, "tree", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
181 var_AddCallback( p_new, "tree", DumpCommand, NULL );
182 var_Create( p_new, "vars", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
183 var_AddCallback( p_new, "vars", DumpCommand, NULL );
184 vlc_restorecancel (canc);
192 * Allocates and initializes a vlc object.
194 * @param i_type known object type (all of them are negative integer values),
195 * or object byte size (always positive).
197 * @return the new object, or NULL on error.
199 void * __vlc_object_create( vlc_object_t *p_this, int i_type )
201 const char * psz_type;
206 case VLC_OBJECT_DECODER:
207 i_size = sizeof(decoder_t);
208 psz_type = "decoder";
210 case VLC_OBJECT_AOUT:
211 i_size = sizeof(aout_instance_t);
212 psz_type = "audio output";
215 assert( i_type > 0 ); /* unknown type?! */
217 i_type = VLC_OBJECT_GENERIC;
218 psz_type = "generic";
222 return vlc_custom_create( p_this, i_size, i_type, psz_type );
227 ****************************************************************************
228 * Set the destructor of a vlc object
230 * This function sets the destructor of the vlc object. It will be called
231 * when the object is destroyed when the its refcount reaches 0.
232 * (It is called by the internal function vlc_object_destroy())
233 *****************************************************************************/
234 void __vlc_object_set_destructor( vlc_object_t *p_this,
235 vlc_destructor_t pf_destructor )
237 vlc_object_internals_t *p_priv = vlc_internals(p_this );
239 vlc_spin_lock( &p_priv->ref_spin );
240 p_priv->pf_destructor = pf_destructor;
241 vlc_spin_unlock( &p_priv->ref_spin );
245 ****************************************************************************
246 * Destroy a vlc object (Internal)
248 * This function destroys an object that has been previously allocated with
249 * vlc_object_create. The object's refcount must be zero and it must not be
250 * attached to other objects in any way.
252 * This function must be called with cancellation disabled (currently).
253 *****************************************************************************/
254 static void vlc_object_destroy( vlc_object_t *p_this )
256 vlc_object_internals_t *p_priv = vlc_internals( p_this );
258 /* Objects are always detached beforehand */
259 assert( !p_this->p_parent );
261 /* Send a kill to the object's thread if applicable */
262 vlc_object_kill( p_this );
264 /* Call the custom "subclass" destructor */
265 if( p_priv->pf_destructor )
266 p_priv->pf_destructor( p_this );
268 /* Any thread must have been cleaned up at this point. */
269 assert( !p_priv->b_thread );
271 /* Destroy the associated variables, starting from the end so that
272 * no memmove calls have to be done. */
273 while( p_priv->i_vars )
275 var_Destroy( p_this, p_priv->p_vars[p_priv->i_vars - 1].psz_name );
278 free( p_priv->p_vars );
279 vlc_cond_destroy( &p_priv->var_wait );
280 vlc_mutex_destroy( &p_priv->var_lock );
282 free( p_this->psz_header );
284 free( p_priv->psz_object_name );
286 vlc_spin_destroy( &p_priv->ref_spin );
287 if( p_priv->pipes[1] != -1 && p_priv->pipes[1] != p_priv->pipes[0] )
288 close( p_priv->pipes[1] );
289 if( p_priv->pipes[0] != -1 )
290 close( p_priv->pipes[0] );
291 if( VLC_OBJECT(p_this->p_libvlc) == p_this )
292 vlc_mutex_destroy (&(libvlc_priv ((libvlc_int_t *)p_this)->structure_lock));
299 # include <winsock2.h>
300 # include <ws2tcpip.h>
303 * select()-able pipes emulated using Winsock
305 static int pipe (int fd[2])
308 int addrlen = sizeof (addr);
310 SOCKET l = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP), a,
311 c = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
312 if ((l == INVALID_SOCKET) || (c == INVALID_SOCKET))
315 memset (&addr, 0, sizeof (addr));
316 addr.sin_family = AF_INET;
317 addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
318 if (bind (l, (PSOCKADDR)&addr, sizeof (addr))
319 || getsockname (l, (PSOCKADDR)&addr, &addrlen)
321 || connect (c, (PSOCKADDR)&addr, addrlen))
324 a = accept (l, NULL, NULL);
325 if (a == INVALID_SOCKET)
336 if (l != INVALID_SOCKET)
338 if (c != INVALID_SOCKET)
344 #define read( a, b, c ) recv (a, b, c, 0)
346 #define write( a, b, c ) send (a, b, c, 0)
348 #define close( a ) closesocket (a)
351 static vlc_mutex_t pipe_lock = VLC_STATIC_MUTEX;
354 * Returns the readable end of a pipe that becomes readable once termination
355 * of the object is requested (vlc_object_kill()).
356 * This can be used to wake-up out of a select() or poll() event loop, such
357 * typically when doing network I/O.
359 * Note that the pipe will remain the same for the lifetime of the object.
360 * DO NOT read the pipe nor close it yourself. Ever.
362 * @param obj object that would be "killed"
363 * @return a readable pipe descriptor, or -1 on error.
365 int vlc_object_waitpipe( vlc_object_t *obj )
367 vlc_object_internals_t *internals = vlc_internals( obj );
369 vlc_mutex_lock (&pipe_lock);
370 if (internals->pipes[0] == -1)
372 /* This can only ever happen if someone killed us without locking: */
373 assert (internals->pipes[1] == -1);
376 internals->pipes[0] = internals->pipes[1] = eventfd (0, 0);
377 if (internals->pipes[0] == -1)
380 if (pipe (internals->pipes))
381 internals->pipes[0] = internals->pipes[1] = -1;
384 if (internals->pipes[0] != -1 && obj->b_die)
385 { /* Race condition: vlc_object_kill() already invoked! */
386 msg_Dbg (obj, "waitpipe: object already dying");
387 write (internals->pipes[1], &(uint64_t){ 1 }, sizeof (uint64_t));
390 vlc_mutex_unlock (&pipe_lock);
391 return internals->pipes[0];
396 * Requests termination of an object, cancels the object thread, and make the
397 * object wait pipe (if it exists) readable. Not a cancellation point.
399 void __vlc_object_kill( vlc_object_t *p_this )
401 vlc_object_internals_t *priv = vlc_internals( p_this );
404 vlc_thread_cancel( p_this );
405 vlc_mutex_lock( &pipe_lock );
409 p_this->b_die = true;
412 /* This also serves as a memory barrier toward vlc_object_alive(): */
413 vlc_mutex_unlock( &pipe_lock );
417 int canc = vlc_savecancel ();
419 /* write _after_ setting b_die, so vlc_object_alive() returns false */
420 write (fd, &(uint64_t){ 1 }, sizeof (uint64_t));
421 msg_Dbg (p_this, "waitpipe: object killed");
422 vlc_restorecancel (canc);
427 /*****************************************************************************
428 * find a typed object and increment its refcount
429 *****************************************************************************
430 * This function recursively looks for a given object type. i_mode can be one
431 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
432 *****************************************************************************/
433 void * __vlc_object_find( vlc_object_t *p_this, int i_type, int i_mode )
435 vlc_object_t *p_found;
437 /* If we are of the requested type ourselves, don't look further */
438 if( !(i_mode & FIND_STRICT)
439 && vlc_internals (p_this)->i_object_type == i_type )
441 vlc_object_hold( p_this );
445 /* Otherwise, recursively look for the object */
446 if ((i_mode & 0x000f) == FIND_ANYWHERE)
447 return vlc_object_find (p_this->p_libvlc, i_type,
448 (i_mode & ~0x000f)|FIND_CHILD);
450 libvlc_lock (p_this->p_libvlc);
451 p_found = FindObject( p_this, i_type, i_mode );
452 libvlc_unlock (p_this->p_libvlc);
456 #undef vlc_object_find_name
458 * Finds a named object and increment its reference count.
459 * Beware that objects found in this manner can be "owned" by another thread,
460 * be of _any_ type, and be attached to any module (if any). With such an
461 * object reference, you can set or get object variables, emit log messages,
462 * and read write-once object parameters (psz_object_type, etc).
463 * You CANNOT cast the object to a more specific object type, and you
464 * definitely cannot invoke object type-specific callbacks with this.
466 * @param p_this object to search from
467 * @param psz_name name of the object to search for
468 * @param i_mode search direction: FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
470 * @return a matching object (must be released by the caller),
473 vlc_object_t *vlc_object_find_name( vlc_object_t *p_this,
474 const char *psz_name, int i_mode )
476 vlc_object_t *p_found;
478 /* Reading psz_object_name from a separate inhibits thread-safety.
479 * Use a libvlc address variable instead for that sort of things! */
480 msg_Warn( p_this, "%s(%s) is not safe!", __func__, psz_name );
481 /* If have the requested name ourselves, don't look further */
482 if( !(i_mode & FIND_STRICT)
483 && vlc_internals(p_this)->psz_object_name
484 && !strcmp( vlc_internals(p_this)->psz_object_name, psz_name ) )
486 vlc_object_hold( p_this );
490 libvlc_lock (p_this->p_libvlc);
492 /* Otherwise, recursively look for the object */
493 if( (i_mode & 0x000f) == FIND_ANYWHERE )
495 vlc_object_t *p_root = p_this;
498 while( p_root->p_parent != NULL &&
499 p_root != VLC_OBJECT( p_this->p_libvlc ) )
501 p_root = p_root->p_parent;
504 p_found = FindObjectName( p_root, psz_name,
505 (i_mode & ~0x000f)|FIND_CHILD );
506 if( p_found == NULL && p_root != VLC_OBJECT( p_this->p_libvlc ) )
508 p_found = FindObjectName( VLC_OBJECT( p_this->p_libvlc ),
509 psz_name, (i_mode & ~0x000f)|FIND_CHILD );
514 p_found = FindObjectName( p_this, psz_name, i_mode );
517 libvlc_unlock (p_this->p_libvlc);
522 * Increment an object reference counter.
524 void * __vlc_object_hold( vlc_object_t *p_this )
526 vlc_object_internals_t *internals = vlc_internals( p_this );
528 vlc_spin_lock( &internals->ref_spin );
529 /* Avoid obvious freed object uses */
530 assert( internals->i_refcount > 0 );
531 /* Increment the counter */
532 internals->i_refcount++;
533 vlc_spin_unlock( &internals->ref_spin );
537 /*****************************************************************************
538 * Decrement an object refcount
539 * And destroy the object if its refcount reach zero.
540 *****************************************************************************/
541 void __vlc_object_release( vlc_object_t *p_this )
543 vlc_object_internals_t *internals = vlc_internals( p_this );
544 vlc_object_t *parent = NULL;
545 bool b_should_destroy;
547 vlc_spin_lock( &internals->ref_spin );
548 assert( internals->i_refcount > 0 );
550 if( internals->i_refcount > 1 )
553 /* There are still other references to the object */
554 internals->i_refcount--;
555 vlc_spin_unlock( &internals->ref_spin );
558 vlc_spin_unlock( &internals->ref_spin );
561 /* Remember that we cannot hold the spin while waiting on the mutex */
562 libvlc_lock (p_this->p_libvlc);
563 /* Take the spin again. Note that another thread may have held the
564 * object in the (very short) mean time. */
565 vlc_spin_lock( &internals->ref_spin );
566 b_should_destroy = --internals->i_refcount == 0;
567 vlc_spin_unlock( &internals->ref_spin );
569 if( b_should_destroy )
571 parent = p_this->p_parent;
574 if( VLC_OBJECT(p_this->p_libvlc) == p_this )
577 vlc_object_t *leaked = internals->next;
578 while( leaked != p_this )
580 /* We are leaking this object */
582 "ERROR: leaking object (%p, type:%s, name:%s)\n",
583 leaked, leaked->psz_object_type,
584 vlc_internals(leaked)->psz_object_name );
585 /* Dump object to ease debugging */
586 vlc_object_dump( leaked );
588 leaked = vlc_internals (leaked)->next;
591 if( internals->next != p_this )
592 /* Dump libvlc object to ease debugging */
593 vlc_object_dump( p_this );
596 /* Remove the object from object list
597 * so that it cannot be encountered by vlc_object_get() */
598 vlc_internals (internals->next)->prev = internals->prev;
599 vlc_internals (internals->prev)->next = internals->next;
602 /* Detach from parent to protect against FIND_CHILDREN */
603 vlc_object_detach_unlocked (p_this);
605 /* We have no children */
606 assert (internals->i_children == 0);
608 libvlc_unlock (p_this->p_libvlc);
610 if( b_should_destroy )
614 canc = vlc_savecancel ();
615 vlc_object_destroy( p_this );
616 vlc_restorecancel (canc);
618 vlc_object_release (parent);
623 ****************************************************************************
624 * attach object to a parent object
625 *****************************************************************************
626 * This function sets p_this as a child of p_parent, and p_parent as a parent
627 * of p_this. This link can be undone using vlc_object_detach.
628 *****************************************************************************/
629 void __vlc_object_attach( vlc_object_t *p_this, vlc_object_t *p_parent )
631 if( !p_this ) return;
633 vlc_object_hold (p_parent);
634 libvlc_lock (p_this->p_libvlc);
636 /* Attach the parent to its child */
637 assert (!p_this->p_parent);
638 p_this->p_parent = p_parent;
640 /* Attach the child to its parent */
641 vlc_object_internals_t *priv = vlc_internals( p_parent );
642 INSERT_ELEM( priv->pp_children, priv->i_children, priv->i_children,
644 libvlc_unlock (p_this->p_libvlc);
648 static void vlc_object_detach_unlocked (vlc_object_t *p_this)
650 if (p_this->p_parent == NULL)
653 vlc_object_internals_t *priv = vlc_internals( p_this->p_parent );
657 /* Remove p_this's parent */
658 p_this->p_parent = NULL;
660 /* Remove all of p_parent's children which are p_this */
661 for( i_index = priv->i_children ; i_index-- ; )
663 if( priv->pp_children[i_index] == p_this )
666 for( i = i_index ; i < priv->i_children ; i++ )
667 priv->pp_children[i] = priv->pp_children[i+1];
671 if( priv->i_children )
673 vlc_object_t **pp_children = (vlc_object_t **)
674 realloc( priv->pp_children,
675 priv->i_children * sizeof(vlc_object_t *) );
677 priv->pp_children = pp_children;
681 /* Special case - don't realloc() to zero to avoid leaking */
682 free( priv->pp_children );
683 priv->pp_children = NULL;
689 ****************************************************************************
690 * detach object from its parent
691 *****************************************************************************
692 * This function removes all links between an object and its parent.
693 *****************************************************************************/
694 void __vlc_object_detach( vlc_object_t *p_this )
696 vlc_object_t *p_parent;
697 if( !p_this ) return;
699 libvlc_lock (p_this->p_libvlc);
700 p_parent = p_this->p_parent;
702 vlc_object_detach_unlocked( p_this );
703 libvlc_unlock (p_this->p_libvlc);
706 vlc_object_release (p_parent);
711 ****************************************************************************
712 * find a list typed objects and increment their refcount
713 *****************************************************************************
714 * This function recursively looks for a given object type. i_mode can be one
715 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
716 *****************************************************************************/
717 vlc_list_t * vlc_list_find( vlc_object_t *p_this, int i_type, int i_mode )
722 /* Look for the objects */
723 switch( i_mode & 0x000f )
726 return vlc_list_find (VLC_OBJECT(p_this->p_libvlc), i_type, FIND_CHILD);
729 libvlc_lock (p_this->p_libvlc);
730 i_count = CountChildren( p_this, i_type );
731 p_list = NewList( i_count );
733 /* Check allocation was successful */
734 if( p_list->i_count != i_count )
736 libvlc_unlock (p_this->p_libvlc);
742 ListChildren( p_list, p_this, i_type );
743 libvlc_unlock (p_this->p_libvlc);
747 msg_Err( p_this, "unimplemented!" );
748 p_list = NewList( 0 );
756 * Gets the list of children of an objects, and increment their reference
758 * @return a list (possibly empty) or NULL in case of error.
760 vlc_list_t *__vlc_list_children( vlc_object_t *obj )
763 vlc_object_internals_t *priv = vlc_internals( obj );
765 libvlc_lock (obj->p_libvlc);
766 l = NewList( priv->i_children );
767 for (int i = 0; i < l->i_count; i++)
769 vlc_object_hold( priv->pp_children[i] );
770 l->p_values[i].p_object = priv->pp_children[i];
772 libvlc_unlock (obj->p_libvlc);
776 /*****************************************************************************
777 * DumpCommand: print the current vlc structure
778 *****************************************************************************
779 * This function prints either an ASCII tree showing the connections between
780 * vlc objects, and additional information such as their refcount, thread ID,
781 * etc. (command "tree"), or the same data as a simple list (command "list").
782 *****************************************************************************/
783 static int DumpCommand( vlc_object_t *p_this, char const *psz_cmd,
784 vlc_value_t oldval, vlc_value_t newval, void *p_data )
786 libvlc_int_t *p_libvlc = p_this->p_libvlc;
788 (void)oldval; (void)p_data;
789 if( *psz_cmd == 'l' )
791 vlc_object_t *cur = VLC_OBJECT (p_libvlc);
793 libvlc_lock (p_this->p_libvlc);
796 PrintObject (cur, "");
797 cur = vlc_internals (cur)->next;
799 while (cur != VLC_OBJECT(p_libvlc));
800 libvlc_unlock (p_this->p_libvlc);
804 vlc_object_t *p_object = NULL;
806 if( *newval.psz_string )
808 /* try using the object's name to find it */
809 p_object = vlc_object_find_name( p_this, newval.psz_string,
817 libvlc_lock (p_this->p_libvlc);
818 if( *psz_cmd == 't' )
820 char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
823 p_object = VLC_OBJECT(p_this->p_libvlc);
826 DumpStructure( p_object, 0, psz_foo );
828 else if( *psz_cmd == 'v' )
833 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
835 PrintObject( p_object, "" );
837 if( !vlc_internals( p_object )->i_vars )
838 printf( " `-o No variables\n" );
839 for( i = 0; i < vlc_internals( p_object )->i_vars; i++ )
841 variable_t *p_var = vlc_internals( p_object )->p_vars + i;
843 const char *psz_type = "unknown";
844 switch( p_var->i_type & VLC_VAR_TYPE )
846 #define MYCASE( type, nice ) \
847 case VLC_VAR_ ## type: \
850 MYCASE( VOID, "void" );
851 MYCASE( BOOL, "bool" );
852 MYCASE( INTEGER, "integer" );
853 MYCASE( HOTKEY, "hotkey" );
854 MYCASE( STRING, "string" );
855 MYCASE( MODULE, "module" );
856 MYCASE( FILE, "file" );
857 MYCASE( DIRECTORY, "directory" );
858 MYCASE( VARIABLE, "variable" );
859 MYCASE( FLOAT, "float" );
860 MYCASE( TIME, "time" );
861 MYCASE( ADDRESS, "address" );
862 MYCASE( MUTEX, "mutex" );
863 MYCASE( LIST, "list" );
866 printf( " %c-o \"%s\" (%s",
867 i + 1 == vlc_internals( p_object )->i_vars ? '`' : '|',
868 p_var->psz_name, psz_type );
869 if( p_var->psz_text )
870 printf( ", %s", p_var->psz_text );
872 if( p_var->i_type & VLC_VAR_HASCHOICE )
873 printf( ", has choices" );
874 if( p_var->i_type & VLC_VAR_ISCOMMAND )
875 printf( ", command" );
876 if( p_var->i_entries )
877 printf( ", %d callbacks", p_var->i_entries );
878 switch( p_var->i_type & VLC_VAR_CLASS )
884 printf( ": %s", p_var->val.b_bool ? "true" : "false" );
886 case VLC_VAR_INTEGER:
887 printf( ": %d", p_var->val.i_int );
890 printf( ": \"%s\"", p_var->val.psz_string );
893 printf( ": %f", p_var->val.f_float );
896 printf( ": %"PRIi64, (int64_t)p_var->val.i_time );
898 case VLC_VAR_ADDRESS:
899 printf( ": %p", p_var->val.p_address );
908 libvlc_unlock (p_this->p_libvlc);
910 if( *newval.psz_string )
912 vlc_object_release( p_object );
919 /*****************************************************************************
920 * vlc_list_release: free a list previously allocated by vlc_list_find
921 *****************************************************************************
922 * This function decreases the refcount of all objects in the list and
924 *****************************************************************************/
925 void vlc_list_release( vlc_list_t *p_list )
929 for( i_index = 0; i_index < p_list->i_count; i_index++ )
931 vlc_object_release( p_list->p_values[i_index].p_object );
934 free( p_list->p_values );
938 /*****************************************************************************
939 * dump an object. (Debug function)
940 *****************************************************************************/
942 static void vlc_object_dump( vlc_object_t *p_this )
944 char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
947 DumpStructure( p_this, 0, psz_foo );
951 /* Following functions are local */
953 static vlc_object_t * FindObject( vlc_object_t *p_this, int i_type, int i_mode )
958 switch( i_mode & 0x000f )
961 p_tmp = p_this->p_parent;
964 if( vlc_internals( p_tmp )->i_object_type == i_type )
966 vlc_object_hold( p_tmp );
971 return FindObject( p_tmp, i_type, i_mode );
977 for( i = vlc_internals( p_this )->i_children; i--; )
979 p_tmp = vlc_internals( p_this )->pp_children[i];
980 if( vlc_internals( p_tmp )->i_object_type == i_type )
982 vlc_object_hold( p_tmp );
985 else if( vlc_internals( p_tmp )->i_children )
987 p_tmp = FindObject( p_tmp, i_type, i_mode );
997 /* Handled in vlc_object_find */
1004 static vlc_object_t * FindObjectName( vlc_object_t *p_this,
1005 const char *psz_name,
1009 vlc_object_t *p_tmp;
1011 switch( i_mode & 0x000f )
1014 p_tmp = p_this->p_parent;
1017 if( vlc_internals(p_tmp)->psz_object_name
1018 && !strcmp( vlc_internals(p_tmp)->psz_object_name, psz_name ) )
1020 vlc_object_hold( p_tmp );
1025 return FindObjectName( p_tmp, psz_name, i_mode );
1031 for( i = vlc_internals( p_this )->i_children; i--; )
1033 p_tmp = vlc_internals( p_this )->pp_children[i];
1034 if( vlc_internals(p_tmp)->psz_object_name
1035 && !strcmp( vlc_internals(p_tmp)->psz_object_name, psz_name ) )
1037 vlc_object_hold( p_tmp );
1040 else if( vlc_internals( p_tmp )->i_children )
1042 p_tmp = FindObjectName( p_tmp, psz_name, i_mode );
1052 /* Handled in vlc_object_find */
1060 static void PrintObject( vlc_object_t *p_this, const char *psz_prefix )
1062 char psz_children[20], psz_refcount[20], psz_thread[30], psz_name[50],
1065 int canc = vlc_savecancel ();
1066 memset( &psz_name, 0, sizeof(psz_name) );
1067 if( vlc_internals(p_this)->psz_object_name )
1069 snprintf( psz_name, 49, " \"%s\"",
1070 vlc_internals(p_this)->psz_object_name );
1072 psz_name[48] = '\"';
1075 psz_children[0] = '\0';
1076 switch( vlc_internals( p_this )->i_children )
1081 strcpy( psz_children, ", 1 child" );
1084 snprintf( psz_children, 19, ", %i children",
1085 vlc_internals( p_this )->i_children );
1089 psz_refcount[0] = '\0';
1090 if( vlc_internals( p_this )->i_refcount > 0 )
1091 snprintf( psz_refcount, 19, ", refcount %u",
1092 vlc_internals( p_this )->i_refcount );
1094 psz_thread[0] = '\0';
1095 if( vlc_internals( p_this )->b_thread )
1096 snprintf( psz_thread, 29, " (thread %lu)",
1097 (unsigned long)vlc_internals( p_this )->thread_id );
1099 psz_parent[0] = '\0';
1100 if( p_this->p_parent )
1101 snprintf( psz_parent, 19, ", parent %p", p_this->p_parent );
1103 printf( " %so %p %s%s%s%s%s%s\n", psz_prefix,
1104 p_this, p_this->psz_object_type,
1105 psz_name, psz_thread, psz_refcount, psz_children,
1107 vlc_restorecancel (canc);
1110 static void DumpStructure( vlc_object_t *p_this, int i_level, char *psz_foo )
1113 char i_back = psz_foo[i_level];
1114 psz_foo[i_level] = '\0';
1116 PrintObject( p_this, psz_foo );
1118 psz_foo[i_level] = i_back;
1120 if( i_level / 2 >= MAX_DUMPSTRUCTURE_DEPTH )
1122 msg_Warn( p_this, "structure tree is too deep" );
1126 for( i = 0 ; i < vlc_internals( p_this )->i_children ; i++ )
1130 psz_foo[i_level-1] = ' ';
1132 if( psz_foo[i_level-2] == '`' )
1134 psz_foo[i_level-2] = ' ';
1138 if( i == vlc_internals( p_this )->i_children - 1 )
1140 psz_foo[i_level] = '`';
1144 psz_foo[i_level] = '|';
1147 psz_foo[i_level+1] = '-';
1148 psz_foo[i_level+2] = '\0';
1150 DumpStructure( vlc_internals( p_this )->pp_children[i], i_level + 2,
1155 static vlc_list_t * NewList( int i_count )
1157 vlc_list_t * p_list = (vlc_list_t *)malloc( sizeof( vlc_list_t ) );
1158 if( p_list == NULL )
1163 p_list->i_count = i_count;
1167 p_list->p_values = NULL;
1171 p_list->p_values = malloc( i_count * sizeof( vlc_value_t ) );
1172 if( p_list->p_values == NULL )
1174 p_list->i_count = 0;
1181 static void ListReplace( vlc_list_t *p_list, vlc_object_t *p_object,
1184 if( p_list == NULL || i_index >= p_list->i_count )
1189 vlc_object_hold( p_object );
1191 p_list->p_values[i_index].p_object = p_object;
1196 /*static void ListAppend( vlc_list_t *p_list, vlc_object_t *p_object )
1198 if( p_list == NULL )
1203 p_list->p_values = realloc( p_list->p_values, (p_list->i_count + 1)
1204 * sizeof( vlc_value_t ) );
1205 if( p_list->p_values == NULL )
1207 p_list->i_count = 0;
1211 vlc_object_hold( p_object );
1213 p_list->p_values[p_list->i_count].p_object = p_object;
1219 static int CountChildren( vlc_object_t *p_this, int i_type )
1221 vlc_object_t *p_tmp;
1224 for( i = 0; i < vlc_internals( p_this )->i_children; i++ )
1226 p_tmp = vlc_internals( p_this )->pp_children[i];
1228 if( vlc_internals( p_tmp )->i_object_type == i_type )
1232 i_count += CountChildren( p_tmp, i_type );
1238 static void ListChildren( vlc_list_t *p_list, 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 )
1248 ListReplace( p_list, p_tmp, p_list->i_count++ );
1250 ListChildren( p_list, p_tmp, i_type );