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