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