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