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