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