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