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