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