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