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