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