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