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