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