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