]> git.sesse.net Git - vlc/blob - src/misc/objects.c
Use var_Inherit* instead of var_CreateGet*.
[vlc] / src / misc / objects.c
1 /*****************************************************************************
2  * objects.c: vlc_object_t handling
3  *****************************************************************************
4  * Copyright (C) 2004-2008 the VideoLAN team
5  * Copyright (C) 2006-2010 RĂ©mi Denis-Courmont
6  *
7  * Authors: Samuel Hocevar <sam@zoy.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /**
25  * \file
26  * This file contains the functions to handle the vlc_object_t type
27  *
28  * Unless otherwise stated, functions in this file are not cancellation point.
29  * All functions in this file are safe w.r.t. deferred cancellation.
30  */
31
32
33 /*****************************************************************************
34  * Preamble
35  *****************************************************************************/
36 #ifdef HAVE_CONFIG_H
37 # include "config.h"
38 #endif
39
40 #include <vlc_common.h>
41
42 #include "../libvlc.h"
43 #include <vlc_aout.h>
44 #include "audio_output/aout_internal.h"
45
46 #include "vlc_interface.h"
47 #include "vlc_codec.h"
48
49 #include "variables.h"
50 #ifndef WIN32
51 # include <unistd.h>
52 #else
53 # include <io.h>
54 # include <winsock2.h>
55 # include <ws2tcpip.h>
56 # undef  read
57 # define read( a, b, c )  recv (a, b, c, 0)
58 # undef  write
59 # define write( a, b, c ) send (a, b, c, 0)
60 # undef  close
61 # define close( a )       closesocket (a)
62 #endif
63
64 #include <search.h>
65 #include <limits.h>
66 #include <assert.h>
67
68 #if defined (HAVE_SYS_EVENTFD_H)
69 # include <sys/eventfd.h>
70 # ifndef EFD_CLOEXEC
71 #  define EFD_CLOEXEC 0
72 #  warning EFD_CLOEXEC missing. Consider updating libc.
73 # endif
74 #endif
75
76
77 /*****************************************************************************
78  * Local prototypes
79  *****************************************************************************/
80 static int  DumpCommand( vlc_object_t *, char const *,
81                          vlc_value_t, vlc_value_t, void * );
82
83 static vlc_object_t * FindParent    ( vlc_object_t *, int );
84 static vlc_object_t * FindChild     ( vlc_object_internals_t *, int );
85 static vlc_object_t * FindParentName( vlc_object_t *, const char * );
86 static vlc_object_t * FindChildName ( vlc_object_internals_t *, const char * );
87 static void PrintObject( vlc_object_internals_t *, const char * );
88 static void DumpStructure( vlc_object_internals_t *, unsigned, char * );
89
90 static vlc_list_t   * NewList       ( int );
91
92 static void vlc_object_destroy( vlc_object_t *p_this );
93
94 /*****************************************************************************
95  * Local structure lock
96  *****************************************************************************/
97 static void libvlc_lock (libvlc_int_t *p_libvlc)
98 {
99     vlc_mutex_lock (&(libvlc_priv (p_libvlc)->structure_lock));
100 }
101
102 static void libvlc_unlock (libvlc_int_t *p_libvlc)
103 {
104     vlc_mutex_unlock (&(libvlc_priv (p_libvlc)->structure_lock));
105 }
106
107 #undef vlc_custom_create
108 void *vlc_custom_create( vlc_object_t *p_this, size_t i_size,
109                          int i_type, const char *psz_type )
110 {
111     vlc_object_t *p_new;
112     vlc_object_internals_t *p_priv;
113
114     /* NOTE:
115      * VLC objects are laid out as follow:
116      * - first the LibVLC-private per-object data,
117      * - then VLC_COMMON members from vlc_object_t,
118      * - finally, the type-specific data (if any).
119      *
120      * This function initializes the LibVLC and common data,
121      * and zeroes the rest.
122      */
123     p_priv = calloc( 1, sizeof( *p_priv ) + i_size );
124     if( p_priv == NULL )
125         return NULL;
126
127     assert (i_size >= sizeof (vlc_object_t));
128     p_new = (vlc_object_t *)(p_priv + 1);
129
130     p_priv->i_object_type = i_type;
131     p_new->psz_object_type = psz_type;
132     p_priv->psz_name = NULL;
133
134     p_new->b_die = false;
135     p_new->b_force = false;
136
137     p_new->psz_header = NULL;
138
139     if (p_this)
140         p_new->i_flags = p_this->i_flags
141             & (OBJECT_FLAGS_NODBG|OBJECT_FLAGS_QUIET|OBJECT_FLAGS_NOINTERACT);
142
143     p_priv->var_root = NULL;
144
145     if( p_this == NULL )
146     {
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_new;
151     }
152     else
153         p_new->p_libvlc = p_this->p_libvlc;
154
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->first = NULL;
161
162     /* Initialize mutexes and condvars */
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;
166
167     if (p_new == VLC_OBJECT(p_new->p_libvlc))
168     {   /* TODO: should be in src/libvlc.c */
169         int canc = vlc_savecancel ();
170         var_Create( p_new, "tree", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
171         var_AddCallback( p_new, "tree", DumpCommand, NULL );
172         var_Create( p_new, "vars", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
173         var_AddCallback( p_new, "vars", DumpCommand, NULL );
174         vlc_restorecancel (canc);
175     }
176
177     return p_new;
178 }
179
180 #undef vlc_object_create
181 /**
182  * Allocates and initializes a vlc object.
183  *
184  * @param i_size object byte size
185  *
186  * @return the new object, or NULL on error.
187  */
188 void *vlc_object_create( vlc_object_t *p_this, size_t i_size )
189 {
190     return vlc_custom_create( p_this, i_size, VLC_OBJECT_GENERIC, "generic" );
191 }
192
193 #undef vlc_object_set_destructor
194 /**
195  ****************************************************************************
196  * Set the destructor of a vlc object
197  *
198  * This function sets the destructor of the vlc object. It will be called
199  * when the object is destroyed when the its refcount reaches 0.
200  * (It is called by the internal function vlc_object_destroy())
201  *****************************************************************************/
202 void vlc_object_set_destructor( vlc_object_t *p_this,
203                                 vlc_destructor_t pf_destructor )
204 {
205     vlc_object_internals_t *p_priv = vlc_internals(p_this );
206
207     vlc_spin_lock( &p_priv->ref_spin );
208     p_priv->pf_destructor = pf_destructor;
209     vlc_spin_unlock( &p_priv->ref_spin );
210 }
211
212 static vlc_mutex_t name_lock = VLC_STATIC_MUTEX;
213
214 #undef vlc_object_set_name
215 int vlc_object_set_name(vlc_object_t *obj, const char *name)
216 {
217     vlc_object_internals_t *priv = vlc_internals(obj);
218     char *newname = name ? strdup (name) : NULL;
219     char *oldname;
220
221     vlc_mutex_lock (&name_lock);
222     oldname = priv->psz_name;
223     priv->psz_name = newname;
224     vlc_mutex_unlock (&name_lock);
225
226     free (oldname);
227     return (priv->psz_name || !name) ? VLC_SUCCESS : VLC_ENOMEM;
228 }
229
230 #undef vlc_object_get_name
231 char *vlc_object_get_name(const vlc_object_t *obj)
232 {
233     vlc_object_internals_t *priv = vlc_internals(obj);
234     char *name;
235
236     vlc_mutex_lock (&name_lock);
237     name = priv->psz_name ? strdup (priv->psz_name) : NULL;
238     vlc_mutex_unlock (&name_lock);
239
240     return name;
241 }
242
243 /**
244  ****************************************************************************
245  * Destroy a vlc object (Internal)
246  *
247  * This function destroys an object that has been previously allocated with
248  * vlc_object_create. The object's refcount must be zero and it must not be
249  * attached to other objects in any way.
250  *
251  * This function must be called with cancellation disabled (currently).
252  *****************************************************************************/
253 static void vlc_object_destroy( vlc_object_t *p_this )
254 {
255     vlc_object_internals_t *p_priv = vlc_internals( p_this );
256
257     /* Send a kill to the object's thread if applicable */
258     vlc_object_kill( p_this );
259
260     /* Call the custom "subclass" destructor */
261     if( p_priv->pf_destructor )
262         p_priv->pf_destructor( p_this );
263
264     /* Any thread must have been cleaned up at this point. */
265     assert( !p_priv->b_thread );
266
267     /* Destroy the associated variables. */
268     var_DestroyAll( p_this );
269
270     vlc_cond_destroy( &p_priv->var_wait );
271     vlc_mutex_destroy( &p_priv->var_lock );
272
273     free( p_this->psz_header );
274
275     free( p_priv->psz_name );
276
277     vlc_spin_destroy( &p_priv->ref_spin );
278     if( p_priv->pipes[1] != -1 && p_priv->pipes[1] != p_priv->pipes[0] )
279         close( p_priv->pipes[1] );
280     if( p_priv->pipes[0] != -1 )
281         close( p_priv->pipes[0] );
282     if( VLC_OBJECT(p_this->p_libvlc) == p_this )
283         vlc_mutex_destroy (&(libvlc_priv ((libvlc_int_t *)p_this)->structure_lock));
284
285     free( p_priv );
286 }
287
288
289 #ifdef WIN32
290 /**
291  * select()-able pipes emulated using Winsock
292  */
293 static int pipe (int fd[2])
294 {
295     SOCKADDR_IN addr;
296     int addrlen = sizeof (addr);
297
298     SOCKET l = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP), a,
299            c = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
300     if ((l == INVALID_SOCKET) || (c == INVALID_SOCKET))
301         goto error;
302
303     memset (&addr, 0, sizeof (addr));
304     addr.sin_family = AF_INET;
305     addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
306     if (bind (l, (PSOCKADDR)&addr, sizeof (addr))
307      || getsockname (l, (PSOCKADDR)&addr, &addrlen)
308      || listen (l, 1)
309      || connect (c, (PSOCKADDR)&addr, addrlen))
310         goto error;
311
312     a = accept (l, NULL, NULL);
313     if (a == INVALID_SOCKET)
314         goto error;
315
316     closesocket (l);
317     //shutdown (a, 0);
318     //shutdown (c, 1);
319     fd[0] = c;
320     fd[1] = a;
321     return 0;
322
323 error:
324     if (l != INVALID_SOCKET)
325         closesocket (l);
326     if (c != INVALID_SOCKET)
327         closesocket (c);
328     return -1;
329 }
330 #endif /* WIN32 */
331
332 static vlc_mutex_t pipe_lock = VLC_STATIC_MUTEX;
333
334 /**
335  * Returns the readable end of a pipe that becomes readable once termination
336  * of the object is requested (vlc_object_kill()).
337  * This can be used to wake-up out of a select() or poll() event loop, such
338  * typically when doing network I/O.
339  *
340  * Note that the pipe will remain the same for the lifetime of the object.
341  * DO NOT read the pipe nor close it yourself. Ever.
342  *
343  * @param obj object that would be "killed"
344  * @return a readable pipe descriptor, or -1 on error.
345  */
346 int vlc_object_waitpipe( vlc_object_t *obj )
347 {
348     vlc_object_internals_t *internals = vlc_internals( obj );
349
350     vlc_mutex_lock (&pipe_lock);
351     if (internals->pipes[0] == -1)
352     {
353         /* This can only ever happen if someone killed us without locking: */
354         assert (internals->pipes[1] == -1);
355
356         /* pipe() is not a cancellation point, but write() is and eventfd() is
357          * unspecified (not in POSIX). */
358         int canc = vlc_savecancel ();
359 #if defined (HAVE_SYS_EVENTFD_H)
360         internals->pipes[0] = internals->pipes[1] = eventfd (0, EFD_CLOEXEC);
361         if (internals->pipes[0] == -1)
362 #endif
363         {
364             if (pipe (internals->pipes))
365                 internals->pipes[0] = internals->pipes[1] = -1;
366         }
367
368         if (internals->pipes[0] != -1 && obj->b_die)
369         {   /* Race condition: vlc_object_kill() already invoked! */
370             msg_Dbg (obj, "waitpipe: object already dying");
371             write (internals->pipes[1], &(uint64_t){ 1 }, sizeof (uint64_t));
372         }
373         vlc_restorecancel (canc);
374     }
375     vlc_mutex_unlock (&pipe_lock);
376     return internals->pipes[0];
377 }
378
379 #undef vlc_object_kill
380 /**
381  * Requests termination of an object, cancels the object thread, and make the
382  * object wait pipe (if it exists) readable. Not a cancellation point.
383  */
384 void vlc_object_kill( vlc_object_t *p_this )
385 {
386     vlc_object_internals_t *priv = vlc_internals( p_this );
387     int fd = -1;
388
389     vlc_thread_cancel( p_this );
390     vlc_mutex_lock( &pipe_lock );
391     if( !p_this->b_die )
392     {
393         fd = priv->pipes[1];
394         p_this->b_die = true;
395     }
396
397     /* This also serves as a memory barrier toward vlc_object_alive(): */
398     vlc_mutex_unlock( &pipe_lock );
399
400     if (fd != -1)
401     {
402         int canc = vlc_savecancel ();
403
404         /* write _after_ setting b_die, so vlc_object_alive() returns false */
405         write (fd, &(uint64_t){ 1 }, sizeof (uint64_t));
406         msg_Dbg (p_this, "waitpipe: object killed");
407         vlc_restorecancel (canc);
408     }
409 }
410
411 #undef vlc_object_find
412 /*****************************************************************************
413  * find a typed object and increment its refcount
414  *****************************************************************************
415  * This function recursively looks for a given object type. i_mode can be one
416  * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
417  *****************************************************************************/
418 void * vlc_object_find( vlc_object_t *p_this, int i_type, int i_mode )
419 {
420     vlc_object_t *p_found;
421
422     /* If we are of the requested type ourselves, don't look further */
423     if( vlc_internals (p_this)->i_object_type == i_type )
424     {
425         vlc_object_hold( p_this );
426         return p_this;
427     }
428
429     /* Otherwise, recursively look for the object */
430     if (i_mode == FIND_ANYWHERE)
431         return vlc_object_find (VLC_OBJECT(p_this->p_libvlc), i_type, FIND_CHILD);
432
433     switch (i_type)
434     {
435         case VLC_OBJECT_VOUT:
436         case VLC_OBJECT_AOUT:
437             break;
438         case VLC_OBJECT_INPUT:
439             /* input can only be accessed like this from children,
440              * otherwise we could not promise that it is initialized */
441             if (i_mode != FIND_PARENT)
442                 return NULL;
443             break;
444         default:
445             return NULL;
446     }
447
448     libvlc_lock (p_this->p_libvlc);
449     switch (i_mode)
450     {
451         case FIND_PARENT:
452             p_found = FindParent (p_this, i_type);
453             break;
454         case FIND_CHILD:
455             p_found = FindChild (vlc_internals (p_this), i_type);
456             break;
457         default:
458             assert (0);
459     }
460     libvlc_unlock (p_this->p_libvlc);
461     return p_found;
462 }
463
464
465 static int objnamecmp(const vlc_object_t *obj, const char *name)
466 {
467     char *objname = vlc_object_get_name(obj);
468     if (objname == NULL)
469         return INT_MIN;
470
471     int ret = strcmp (objname, name);
472     free (objname);
473     return ret;
474 }
475
476 #undef vlc_object_find_name
477 /**
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.
485  *
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.
489  *
490  * @return a matching object (must be released by the caller),
491  * or NULL on error.
492  */
493 vlc_object_t *vlc_object_find_name( vlc_object_t *p_this,
494                                     const char *psz_name, int i_mode )
495 {
496     vlc_object_t *p_found;
497
498     /* Reading psz_object_name from a separate inhibits thread-safety.
499      * Use a libvlc address variable instead for that sort of things! */
500     msg_Warn( p_this, "%s(%s) is not safe!", __func__, psz_name );
501     /* If have the requested name ourselves, don't look further */
502     if( !objnamecmp(p_this, psz_name) )
503     {
504         vlc_object_hold( p_this );
505         return p_this;
506     }
507
508     /* Otherwise, recursively look for the object */
509     if (i_mode == FIND_ANYWHERE)
510         return vlc_object_find_name (VLC_OBJECT(p_this->p_libvlc), psz_name,
511                                      FIND_CHILD);
512
513     libvlc_lock (p_this->p_libvlc);
514     vlc_mutex_lock (&name_lock);
515     switch (i_mode)
516     {
517         case FIND_PARENT:
518             p_found = FindParentName (p_this, psz_name);
519             break;
520         case FIND_CHILD:
521             p_found = FindChildName (vlc_internals (p_this), psz_name);
522             break;
523         default:
524             assert (0);
525     }
526     vlc_mutex_unlock (&name_lock);
527     libvlc_unlock (p_this->p_libvlc);
528     return p_found;
529 }
530
531 #undef vlc_object_hold
532 /**
533  * Increment an object reference counter.
534  */
535 void * vlc_object_hold( vlc_object_t *p_this )
536 {
537     vlc_object_internals_t *internals = vlc_internals( p_this );
538
539     vlc_spin_lock( &internals->ref_spin );
540     /* Avoid obvious freed object uses */
541     assert( internals->i_refcount > 0 );
542     /* Increment the counter */
543     internals->i_refcount++;
544     vlc_spin_unlock( &internals->ref_spin );
545     return p_this;
546 }
547
548 #undef vlc_object_release
549 /*****************************************************************************
550  * Decrement an object refcount
551  * And destroy the object if its refcount reach zero.
552  *****************************************************************************/
553 void vlc_object_release( vlc_object_t *p_this )
554 {
555     vlc_object_internals_t *internals = vlc_internals( p_this );
556     vlc_object_t *parent = NULL;
557     bool b_should_destroy;
558
559     vlc_spin_lock( &internals->ref_spin );
560     assert( internals->i_refcount > 0 );
561
562     if( internals->i_refcount > 1 )
563     {
564         /* Fast path */
565         /* There are still other references to the object */
566         internals->i_refcount--;
567         vlc_spin_unlock( &internals->ref_spin );
568         return;
569     }
570     vlc_spin_unlock( &internals->ref_spin );
571
572     /* Slow path */
573     /* Remember that we cannot hold the spin while waiting on the mutex */
574     libvlc_lock (p_this->p_libvlc);
575     /* Take the spin again. Note that another thread may have held the
576      * object in the (very short) mean time. */
577     vlc_spin_lock( &internals->ref_spin );
578     b_should_destroy = --internals->i_refcount == 0;
579     vlc_spin_unlock( &internals->ref_spin );
580
581     if( b_should_destroy )
582     {
583         /* Detach from parent to protect against FIND_CHILDREN */
584         parent = p_this->p_parent;
585         if (likely(parent))
586         {
587            /* Unlink */
588            if (internals->prev != NULL)
589                internals->prev->next = internals->next;
590            else
591                vlc_internals(parent)->first = internals->next;
592            if (internals->next != NULL)
593                internals->next->prev = internals->prev;
594         }
595
596         /* We have no children */
597         assert (internals->first == NULL);
598     }
599     libvlc_unlock (p_this->p_libvlc);
600
601     if( b_should_destroy )
602     {
603         int canc;
604
605         canc = vlc_savecancel ();
606         vlc_object_destroy( p_this );
607         vlc_restorecancel (canc);
608         if (parent)
609             vlc_object_release (parent);
610     }
611 }
612
613 #undef vlc_object_attach
614 /**
615  ****************************************************************************
616  * attach object to a parent object
617  *****************************************************************************
618  * This function sets p_this as a child of p_parent, and p_parent as a parent
619  * of p_this.
620  *****************************************************************************/
621 void vlc_object_attach( vlc_object_t *p_this, vlc_object_t *p_parent )
622 {
623     if( !p_this ) return;
624
625     vlc_object_internals_t *pap = vlc_internals (p_parent);
626     vlc_object_internals_t *priv = vlc_internals (p_this);
627
628     priv->prev = NULL;
629     vlc_object_hold (p_parent);
630     libvlc_lock (p_this->p_libvlc);
631
632     /* Attach the parent to its child */
633     assert (p_this->p_parent == NULL);
634     p_this->p_parent = p_parent;
635
636     /* Attach the child to its parent */
637     priv->next = pap->first;
638     if (priv->next != NULL)
639         priv->next->prev = priv;
640     pap->first = priv;
641     libvlc_unlock (p_this->p_libvlc);
642 }
643
644
645 #undef vlc_list_children
646 /**
647  * Gets the list of children of an objects, and increment their reference
648  * count.
649  * @return a list (possibly empty) or NULL in case of error.
650  */
651 vlc_list_t *vlc_list_children( vlc_object_t *obj )
652 {
653     vlc_list_t *l;
654     vlc_object_internals_t *priv;
655     unsigned count = 0;
656
657     libvlc_lock (obj->p_libvlc);
658     for (priv = vlc_internals (obj)->first; priv; priv = priv->next)
659          count++;
660     l = NewList (count);
661     if (likely(l != NULL))
662     {
663         unsigned i = 0;
664
665         for (priv = vlc_internals (obj)->first; priv; priv = priv->next)
666             l->p_values[i++].p_object = vlc_object_hold (vlc_externals (priv));
667     }
668     libvlc_unlock (obj->p_libvlc);
669     return l;
670 }
671
672 static void DumpVariable (const void *data, const VISIT which, const int depth)
673 {
674     if (which != postorder && which != leaf)
675         return;
676     (void) depth;
677
678     const variable_t *p_var = *(const variable_t **)data;
679     const char *psz_type = "unknown";
680
681     switch( p_var->i_type & VLC_VAR_TYPE )
682     {
683 #define MYCASE( type, nice )    \
684         case VLC_VAR_ ## type:  \
685             psz_type = nice;    \
686             break;
687         MYCASE( VOID, "void" );
688         MYCASE( BOOL, "bool" );
689         MYCASE( INTEGER, "integer" );
690         MYCASE( HOTKEY, "hotkey" );
691         MYCASE( STRING, "string" );
692         MYCASE( MODULE, "module" );
693         MYCASE( FILE, "file" );
694         MYCASE( DIRECTORY, "directory" );
695         MYCASE( VARIABLE, "variable" );
696         MYCASE( FLOAT, "float" );
697         MYCASE( TIME, "time" );
698         MYCASE( COORDS, "coords" );
699         MYCASE( ADDRESS, "address" );
700         MYCASE( MUTEX, "mutex" );
701 #undef MYCASE
702     }
703     printf( " *-o \"%s\" (%s", p_var->psz_name, psz_type );
704     if( p_var->psz_text )
705         printf( ", %s", p_var->psz_text );
706     fputc( ')', stdout );
707     if( p_var->i_type & VLC_VAR_HASCHOICE )
708         fputs( ", has choices", stdout );
709     if( p_var->i_type & VLC_VAR_ISCOMMAND )
710         fputs( ", command", stdout );
711     if( p_var->i_entries )
712         printf( ", %d callbacks", p_var->i_entries );
713     switch( p_var->i_type & VLC_VAR_CLASS )
714     {
715         case VLC_VAR_VOID:
716         case VLC_VAR_MUTEX:
717             break;
718         case VLC_VAR_BOOL:
719             printf( ": %s", p_var->val.b_bool ? "true" : "false" );
720             break;
721         case VLC_VAR_INTEGER:
722             printf( ": %"PRId64, p_var->val.i_int );
723             break;
724         case VLC_VAR_STRING:
725             printf( ": \"%s\"", p_var->val.psz_string );
726             break;
727         case VLC_VAR_FLOAT:
728             printf( ": %f", p_var->val.f_float );
729             break;
730         case VLC_VAR_TIME:
731             printf( ": %"PRIi64, (int64_t)p_var->val.i_time );
732             break;
733         case VLC_VAR_COORDS:
734             printf( ": %"PRId32"x%"PRId32,
735                     p_var->val.coords.x, p_var->val.coords.y );
736             break;
737         case VLC_VAR_ADDRESS:
738             printf( ": %p", p_var->val.p_address );
739             break;
740     }
741     fputc( '\n', stdout );
742 }
743
744 /*****************************************************************************
745  * DumpCommand: print the current vlc structure
746  *****************************************************************************
747  * This function prints either an ASCII tree showing the connections between
748  * vlc objects, and additional information such as their refcount, thread ID,
749  * etc. (command "tree"), or the same data as a simple list (command "list").
750  *****************************************************************************/
751 static int DumpCommand( vlc_object_t *p_this, char const *psz_cmd,
752                         vlc_value_t oldval, vlc_value_t newval, void *p_data )
753 {
754     (void)oldval; (void)p_data;
755     vlc_object_t *p_object = NULL;
756
757     if( *newval.psz_string )
758     {
759         /* try using the object's name to find it */
760         p_object = vlc_object_find_name( p_this, newval.psz_string,
761                                          FIND_ANYWHERE );
762         if( !p_object )
763         {
764             return VLC_ENOOBJ;
765         }
766     }
767
768     libvlc_lock (p_this->p_libvlc);
769     if( *psz_cmd == 't' )
770     {
771         char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
772
773         if( !p_object )
774             p_object = VLC_OBJECT(p_this->p_libvlc);
775
776         psz_foo[0] = '|';
777         DumpStructure( vlc_internals(p_object), 0, psz_foo );
778     }
779     else if( *psz_cmd == 'v' )
780     {
781         if( !p_object )
782             p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
783
784         PrintObject( vlc_internals(p_object), "" );
785         vlc_mutex_lock( &vlc_internals( p_object )->var_lock );
786         if( vlc_internals( p_object )->var_root == NULL )
787             puts( " `-o No variables" );
788         else
789             twalk( vlc_internals( p_object )->var_root, DumpVariable );
790         vlc_mutex_unlock( &vlc_internals( p_object )->var_lock );
791     }
792     libvlc_unlock (p_this->p_libvlc);
793
794     if( *newval.psz_string )
795     {
796         vlc_object_release( p_object );
797     }
798     return VLC_SUCCESS;
799 }
800
801 /*****************************************************************************
802  * vlc_list_release: free a list previously allocated by vlc_list_find
803  *****************************************************************************
804  * This function decreases the refcount of all objects in the list and
805  * frees the list.
806  *****************************************************************************/
807 void vlc_list_release( vlc_list_t *p_list )
808 {
809     int i_index;
810
811     for( i_index = 0; i_index < p_list->i_count; i_index++ )
812     {
813         vlc_object_release( p_list->p_values[i_index].p_object );
814     }
815
816     free( p_list->p_values );
817     free( p_list );
818 }
819
820 /* Following functions are local */
821
822 static vlc_object_t *FindParent (vlc_object_t *p_this, int i_type)
823 {
824     for (vlc_object_t *parent = p_this->p_parent;
825          parent != NULL;
826          parent = parent->p_parent)
827     {
828         if (vlc_internals (parent)->i_object_type == i_type)
829             return vlc_object_hold (parent);
830     }
831     return NULL;
832 }
833
834 static vlc_object_t *FindParentName (vlc_object_t *p_this, const char *name)
835 {
836     for (vlc_object_t *parent = p_this->p_parent;
837          parent != NULL;
838          parent = parent->p_parent)
839     {
840         const char *objname = vlc_internals (parent)->psz_name;
841         if (objname && !strcmp (objname, name))
842             return vlc_object_hold (parent);
843     }
844     return NULL;
845 }
846
847 static vlc_object_t *FindChild (vlc_object_internals_t *priv, int i_type)
848 {
849     for (priv = priv->first; priv != NULL; priv = priv->next)
850     {
851         if (priv->i_object_type == i_type)
852             return vlc_object_hold (vlc_externals (priv));
853
854         vlc_object_t *found = FindChild (priv, i_type);
855         if (found != NULL)
856             return found;
857     }
858     return NULL;
859 }
860
861 static vlc_object_t *FindChildName (vlc_object_internals_t *priv,
862                                     const char *name)
863 {
864     for (priv = priv->first; priv != NULL; priv = priv->next)
865     {
866         if (priv->psz_name && !strcmp (priv->psz_name, name))
867             return vlc_object_hold (vlc_externals (priv));
868
869         vlc_object_t *found = FindChildName (priv, name);
870         if (found != NULL)
871             return found;
872     }
873     return NULL;
874 }
875
876 static void PrintObject( vlc_object_internals_t *priv,
877                          const char *psz_prefix )
878 {
879     char psz_refcount[20], psz_thread[30], psz_name[50], psz_parent[20];
880
881     int canc = vlc_savecancel ();
882     memset( &psz_name, 0, sizeof(psz_name) );
883
884     vlc_mutex_lock (&name_lock);
885     if (priv->psz_name != NULL)
886     {
887         snprintf( psz_name, 49, " \"%s\"", priv->psz_name );
888         if( psz_name[48] )
889             psz_name[48] = '\"';
890     }
891     vlc_mutex_unlock (&name_lock);
892
893     psz_refcount[0] = '\0';
894     if( priv->i_refcount > 0 )
895         snprintf( psz_refcount, 19, ", %u refs", priv->i_refcount );
896
897     psz_thread[0] = '\0';
898     if( priv->b_thread )
899         snprintf( psz_thread, 29, " (thread %lu)",
900                   (unsigned long)priv->thread_id );
901
902     psz_parent[0] = '\0';
903     /* FIXME: need structure lock!!! */
904     if( vlc_externals(priv)->p_parent )
905         snprintf( psz_parent, 19, ", parent %p",
906                   vlc_externals(priv)->p_parent );
907
908     printf( " %so %p %s%s%s%s%s\n", psz_prefix,
909             vlc_externals(priv), vlc_externals(priv)->psz_object_type,
910             psz_name, psz_thread, psz_refcount, psz_parent );
911     vlc_restorecancel (canc);
912 }
913
914 static void DumpStructure (vlc_object_internals_t *priv, unsigned i_level,
915                            char *psz_foo)
916 {
917     char i_back = psz_foo[i_level];
918     psz_foo[i_level] = '\0';
919
920     PrintObject (priv, psz_foo);
921
922     psz_foo[i_level] = i_back;
923
924     if( i_level / 2 >= MAX_DUMPSTRUCTURE_DEPTH )
925     {
926         msg_Warn( vlc_externals(priv), "structure tree is too deep" );
927         return;
928     }
929
930     for (priv = priv->first; priv != NULL; priv = priv->next)
931     {
932         if( i_level )
933         {
934             psz_foo[i_level-1] = ' ';
935
936             if( psz_foo[i_level-2] == '`' )
937             {
938                 psz_foo[i_level-2] = ' ';
939             }
940         }
941
942         psz_foo[i_level] = priv->next ? '|' : '`';
943         psz_foo[i_level+1] = '-';
944         psz_foo[i_level+2] = '\0';
945
946         DumpStructure (priv, i_level + 2, psz_foo);
947     }
948 }
949
950 static vlc_list_t * NewList( int i_count )
951 {
952     vlc_list_t * p_list = malloc( sizeof( vlc_list_t ) );
953     if( p_list == NULL )
954         return NULL;
955
956     p_list->i_count = i_count;
957
958     if( i_count == 0 )
959     {
960         p_list->p_values = NULL;
961         return p_list;
962     }
963
964     p_list->p_values = malloc( i_count * sizeof( vlc_value_t ) );
965     if( p_list->p_values == NULL )
966     {
967         p_list->i_count = 0;
968         return p_list;
969     }
970
971     return p_list;
972 }