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