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