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