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