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