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