Fix some typos.
[vlc/asuraparaju-public.git] / src / misc / objects.c
blob65802c77c74cb3d89bc5e3e96acfaf90ac5149c1
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
7 * Authors: Samuel Hocevar <sam@zoy.org>
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.
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.
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 *****************************************************************************/
24 /**
25 * \file
26 * This file contains the functions to handle the vlc_object_t type
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.
33 /*****************************************************************************
34 * Preamble
35 *****************************************************************************/
36 #ifdef HAVE_CONFIG_H
37 # include "config.h"
38 #endif
40 #include <vlc_common.h>
42 #include "../libvlc.h"
43 #include <vlc_aout.h>
44 #include "audio_output/aout_internal.h"
46 #include "vlc_interface.h"
47 #include "vlc_codec.h"
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
64 #include <search.h>
65 #include <limits.h>
66 #include <assert.h>
68 #if defined (HAVE_SYS_EVENTFD_H)
69 # include <sys/eventfd.h>
70 # ifndef EFD_CLOEXEC
71 # define EFD_CLOEXEC 0
72 # warning EFD_CLOEXEC missing. Consider updating libc.
73 # endif
74 #endif
77 /*****************************************************************************
78 * Local prototypes
79 *****************************************************************************/
80 static int DumpCommand( vlc_object_t *, char const *,
81 vlc_value_t, vlc_value_t, void * );
83 static vlc_object_t * FindParent ( vlc_object_t *, int );
84 static vlc_object_t * FindChild ( vlc_object_internals_t *, int );
85 static vlc_object_t * FindParentName( vlc_object_t *, const char * );
86 static vlc_object_t * FindChildName ( vlc_object_internals_t *, const char * );
87 static void PrintObject( vlc_object_internals_t *, const char * );
88 static void DumpStructure( vlc_object_internals_t *, unsigned, char * );
90 static vlc_list_t * NewList ( int );
92 static void vlc_object_destroy( vlc_object_t *p_this );
94 /*****************************************************************************
95 * Local structure lock
96 *****************************************************************************/
97 static void libvlc_lock (libvlc_int_t *p_libvlc)
99 vlc_mutex_lock (&(libvlc_priv (p_libvlc)->structure_lock));
102 static void libvlc_unlock (libvlc_int_t *p_libvlc)
104 vlc_mutex_unlock (&(libvlc_priv (p_libvlc)->structure_lock));
107 #undef vlc_custom_create
108 void *vlc_custom_create( vlc_object_t *p_this, size_t i_size,
109 int i_type, const char *psz_type )
111 vlc_object_t *p_new;
112 vlc_object_internals_t *p_priv;
114 /* NOTE:
115 * VLC objects are laid out as follow:
116 * - first the LibVLC-private per-object data,
117 * - then VLC_COMMON members from vlc_object_t,
118 * - finally, the type-specific data (if any).
120 * This function initializes the LibVLC and common data,
121 * and zeroes the rest.
123 p_priv = calloc( 1, sizeof( *p_priv ) + i_size );
124 if( p_priv == NULL )
125 return NULL;
127 assert (i_size >= sizeof (vlc_object_t));
128 p_new = (vlc_object_t *)(p_priv + 1);
130 p_priv->i_object_type = i_type;
131 p_new->psz_object_type = psz_type;
132 p_priv->psz_name = NULL;
134 p_new->b_die = false;
135 p_new->b_force = false;
137 p_new->psz_header = NULL;
139 if (p_this)
140 p_new->i_flags = p_this->i_flags
141 & (OBJECT_FLAGS_NODBG|OBJECT_FLAGS_QUIET|OBJECT_FLAGS_NOINTERACT);
143 p_priv->var_root = NULL;
145 if( p_this == NULL )
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_new;
152 else
153 p_new->p_libvlc = p_this->p_libvlc;
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->first = NULL;
162 /* Initialize mutexes and condvars */
163 vlc_mutex_init( &p_priv->var_lock );
164 vlc_cond_init( &p_priv->var_wait );
165 p_priv->pipes[0] = p_priv->pipes[1] = -1;
167 if (p_new == VLC_OBJECT(p_new->p_libvlc))
168 { /* TODO: should be in src/libvlc.c */
169 int canc = vlc_savecancel ();
170 var_Create( p_new, "tree", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
171 var_AddCallback( p_new, "tree", DumpCommand, NULL );
172 var_Create( p_new, "vars", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
173 var_AddCallback( p_new, "vars", DumpCommand, NULL );
174 vlc_restorecancel (canc);
177 return p_new;
180 #undef vlc_object_create
182 * Allocates and initializes a vlc object.
184 * @param i_size object byte size
186 * @return the new object, or NULL on error.
188 void *vlc_object_create( vlc_object_t *p_this, size_t i_size )
190 return vlc_custom_create( p_this, i_size, VLC_OBJECT_GENERIC, "generic" );
193 #undef vlc_object_set_destructor
195 ****************************************************************************
196 * Set the destructor of a vlc object
198 * This function sets the destructor of the vlc object. It will be called
199 * when the object is destroyed when the its refcount reaches 0.
200 * (It is called by the internal function vlc_object_destroy())
201 *****************************************************************************/
202 void vlc_object_set_destructor( vlc_object_t *p_this,
203 vlc_destructor_t pf_destructor )
205 vlc_object_internals_t *p_priv = vlc_internals(p_this );
207 vlc_spin_lock( &p_priv->ref_spin );
208 p_priv->pf_destructor = pf_destructor;
209 vlc_spin_unlock( &p_priv->ref_spin );
212 static vlc_mutex_t name_lock = VLC_STATIC_MUTEX;
214 #undef vlc_object_set_name
215 int vlc_object_set_name(vlc_object_t *obj, const char *name)
217 vlc_object_internals_t *priv = vlc_internals(obj);
218 char *newname = name ? strdup (name) : NULL;
219 char *oldname;
221 vlc_mutex_lock (&name_lock);
222 oldname = priv->psz_name;
223 priv->psz_name = newname;
224 vlc_mutex_unlock (&name_lock);
226 free (oldname);
227 return (priv->psz_name || !name) ? VLC_SUCCESS : VLC_ENOMEM;
230 #undef vlc_object_get_name
231 char *vlc_object_get_name(const vlc_object_t *obj)
233 vlc_object_internals_t *priv = vlc_internals(obj);
234 char *name;
236 vlc_mutex_lock (&name_lock);
237 name = priv->psz_name ? strdup (priv->psz_name) : NULL;
238 vlc_mutex_unlock (&name_lock);
240 return name;
244 ****************************************************************************
245 * Destroy a vlc object (Internal)
247 * This function destroys an object that has been previously allocated with
248 * vlc_object_create. The object's refcount must be zero and it must not be
249 * attached to other objects in any way.
251 * This function must be called with cancellation disabled (currently).
252 *****************************************************************************/
253 static void vlc_object_destroy( vlc_object_t *p_this )
255 vlc_object_internals_t *p_priv = vlc_internals( p_this );
257 /* Send a kill to the object's thread if applicable */
258 vlc_object_kill( p_this );
260 /* Call the custom "subclass" destructor */
261 if( p_priv->pf_destructor )
262 p_priv->pf_destructor( p_this );
264 /* Any thread must have been cleaned up at this point. */
265 assert( !p_priv->b_thread );
267 /* Destroy the associated variables. */
268 var_DestroyAll( p_this );
270 vlc_cond_destroy( &p_priv->var_wait );
271 vlc_mutex_destroy( &p_priv->var_lock );
273 free( p_this->psz_header );
275 free( p_priv->psz_name );
277 vlc_spin_destroy( &p_priv->ref_spin );
278 if( p_priv->pipes[1] != -1 && p_priv->pipes[1] != p_priv->pipes[0] )
279 close( p_priv->pipes[1] );
280 if( p_priv->pipes[0] != -1 )
281 close( p_priv->pipes[0] );
282 if( VLC_OBJECT(p_this->p_libvlc) == p_this )
283 vlc_mutex_destroy (&(libvlc_priv ((libvlc_int_t *)p_this)->structure_lock));
285 free( p_priv );
289 #ifdef WIN32
291 * select()-able pipes emulated using Winsock
293 static int pipe (int fd[2])
295 SOCKADDR_IN addr;
296 int addrlen = sizeof (addr);
298 SOCKET l = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP), a,
299 c = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
300 if ((l == INVALID_SOCKET) || (c == INVALID_SOCKET))
301 goto error;
303 memset (&addr, 0, sizeof (addr));
304 addr.sin_family = AF_INET;
305 addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
306 if (bind (l, (PSOCKADDR)&addr, sizeof (addr))
307 || getsockname (l, (PSOCKADDR)&addr, &addrlen)
308 || listen (l, 1)
309 || connect (c, (PSOCKADDR)&addr, addrlen))
310 goto error;
312 a = accept (l, NULL, NULL);
313 if (a == INVALID_SOCKET)
314 goto error;
316 closesocket (l);
317 //shutdown (a, 0);
318 //shutdown (c, 1);
319 fd[0] = c;
320 fd[1] = a;
321 return 0;
323 error:
324 if (l != INVALID_SOCKET)
325 closesocket (l);
326 if (c != INVALID_SOCKET)
327 closesocket (c);
328 return -1;
330 #endif /* WIN32 */
332 static vlc_mutex_t pipe_lock = VLC_STATIC_MUTEX;
335 * Returns the readable end of a pipe that becomes readable once termination
336 * of the object is requested (vlc_object_kill()).
337 * This can be used to wake-up out of a select() or poll() event loop, such
338 * typically when doing network I/O.
340 * Note that the pipe will remain the same for the lifetime of the object.
341 * DO NOT read the pipe nor close it yourself. Ever.
343 * @param obj object that would be "killed"
344 * @return a readable pipe descriptor, or -1 on error.
346 int vlc_object_waitpipe( vlc_object_t *obj )
348 vlc_object_internals_t *internals = vlc_internals( obj );
350 vlc_mutex_lock (&pipe_lock);
351 if (internals->pipes[0] == -1)
353 /* This can only ever happen if someone killed us without locking: */
354 assert (internals->pipes[1] == -1);
356 /* pipe() is not a cancellation point, but write() is and eventfd() is
357 * unspecified (not in POSIX). */
358 int canc = vlc_savecancel ();
359 #if defined (HAVE_SYS_EVENTFD_H)
360 internals->pipes[0] = internals->pipes[1] = eventfd (0, EFD_CLOEXEC);
361 if (internals->pipes[0] == -1)
362 #endif
364 if (pipe (internals->pipes))
365 internals->pipes[0] = internals->pipes[1] = -1;
368 if (internals->pipes[0] != -1 && obj->b_die)
369 { /* Race condition: vlc_object_kill() already invoked! */
370 msg_Dbg (obj, "waitpipe: object already dying");
371 write (internals->pipes[1], &(uint64_t){ 1 }, sizeof (uint64_t));
373 vlc_restorecancel (canc);
375 vlc_mutex_unlock (&pipe_lock);
376 return internals->pipes[0];
379 #undef vlc_object_kill
381 * Requests termination of an object, cancels the object thread, and make the
382 * object wait pipe (if it exists) readable. Not a cancellation point.
384 void vlc_object_kill( vlc_object_t *p_this )
386 vlc_object_internals_t *priv = vlc_internals( p_this );
387 int fd = -1;
389 vlc_thread_cancel( p_this );
390 vlc_mutex_lock( &pipe_lock );
391 if( !p_this->b_die )
393 fd = priv->pipes[1];
394 p_this->b_die = true;
397 /* This also serves as a memory barrier toward vlc_object_alive(): */
398 vlc_mutex_unlock( &pipe_lock );
400 if (fd != -1)
402 int canc = vlc_savecancel ();
404 /* write _after_ setting b_die, so vlc_object_alive() returns false */
405 write (fd, &(uint64_t){ 1 }, sizeof (uint64_t));
406 msg_Dbg (p_this, "waitpipe: object killed");
407 vlc_restorecancel (canc);
411 #undef vlc_object_find
412 /*****************************************************************************
413 * find a typed object and increment its refcount
414 *****************************************************************************
415 * This function recursively looks for a given object type. i_mode can be one
416 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
417 *****************************************************************************/
418 void * vlc_object_find( vlc_object_t *p_this, int i_type, int i_mode )
420 vlc_object_t *p_found;
422 /* If we are of the requested type ourselves, don't look further */
423 if( vlc_internals (p_this)->i_object_type == i_type )
425 vlc_object_hold( p_this );
426 return p_this;
429 /* Otherwise, recursively look for the object */
430 if (i_mode == FIND_ANYWHERE)
431 return vlc_object_find (VLC_OBJECT(p_this->p_libvlc), i_type, FIND_CHILD);
433 switch (i_type)
435 case VLC_OBJECT_VOUT:
436 case VLC_OBJECT_AOUT:
437 break;
438 case VLC_OBJECT_INPUT:
439 /* input can only be accessed like this from children,
440 * otherwise we could not promise that it is initialized */
441 if (i_mode != FIND_PARENT)
442 return NULL;
443 break;
444 default:
445 return NULL;
448 libvlc_lock (p_this->p_libvlc);
449 switch (i_mode)
451 case FIND_PARENT:
452 p_found = FindParent (p_this, i_type);
453 break;
454 case FIND_CHILD:
455 p_found = FindChild (vlc_internals (p_this), i_type);
456 break;
457 default:
458 assert (0);
460 libvlc_unlock (p_this->p_libvlc);
461 return p_found;
465 static int objnamecmp(const vlc_object_t *obj, const char *name)
467 char *objname = vlc_object_get_name(obj);
468 if (objname == NULL)
469 return INT_MIN;
471 int ret = strcmp (objname, name);
472 free (objname);
473 return ret;
476 #undef vlc_object_find_name
478 * Finds a named object and increment its reference count.
479 * Beware that objects found in this manner can be "owned" by another thread,
480 * be of _any_ type, and be attached to any module (if any). With such an
481 * object reference, you can set or get object variables, emit log messages,
482 * and read write-once object parameters (psz_object_type, etc).
483 * You CANNOT cast the object to a more specific object type, and you
484 * definitely cannot invoke object type-specific callbacks with this.
486 * @param p_this object to search from
487 * @param psz_name name of the object to search for
488 * @param i_mode search direction: FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
490 * @return a matching object (must be released by the caller),
491 * or NULL on error.
493 vlc_object_t *vlc_object_find_name( vlc_object_t *p_this,
494 const char *psz_name, int i_mode )
496 vlc_object_t *p_found;
498 /* Reading psz_object_name from a separate inhibits thread-safety.
499 * Use a libvlc address variable instead for that sort of things! */
500 msg_Warn( p_this, "%s(%s) is not safe!", __func__, psz_name );
501 /* If have the requested name ourselves, don't look further */
502 if( !objnamecmp(p_this, psz_name) )
504 vlc_object_hold( p_this );
505 return p_this;
508 /* Otherwise, recursively look for the object */
509 if (i_mode == FIND_ANYWHERE)
510 return vlc_object_find_name (VLC_OBJECT(p_this->p_libvlc), psz_name,
511 FIND_CHILD);
513 libvlc_lock (p_this->p_libvlc);
514 vlc_mutex_lock (&name_lock);
515 switch (i_mode)
517 case FIND_PARENT:
518 p_found = FindParentName (p_this, psz_name);
519 break;
520 case FIND_CHILD:
521 p_found = FindChildName (vlc_internals (p_this), psz_name);
522 break;
523 default:
524 assert (0);
526 vlc_mutex_unlock (&name_lock);
527 libvlc_unlock (p_this->p_libvlc);
528 return p_found;
531 #undef vlc_object_hold
533 * Increment an object reference counter.
535 void * vlc_object_hold( vlc_object_t *p_this )
537 vlc_object_internals_t *internals = vlc_internals( p_this );
539 vlc_spin_lock( &internals->ref_spin );
540 /* Avoid obvious freed object uses */
541 assert( internals->i_refcount > 0 );
542 /* Increment the counter */
543 internals->i_refcount++;
544 vlc_spin_unlock( &internals->ref_spin );
545 return p_this;
548 #undef vlc_object_release
549 /*****************************************************************************
550 * Decrement an object refcount
551 * And destroy the object if its refcount reach zero.
552 *****************************************************************************/
553 void vlc_object_release( vlc_object_t *p_this )
555 vlc_object_internals_t *internals = vlc_internals( p_this );
556 vlc_object_t *parent = NULL;
557 bool b_should_destroy;
559 vlc_spin_lock( &internals->ref_spin );
560 assert( internals->i_refcount > 0 );
562 if( internals->i_refcount > 1 )
564 /* Fast path */
565 /* There are still other references to the object */
566 internals->i_refcount--;
567 vlc_spin_unlock( &internals->ref_spin );
568 return;
570 vlc_spin_unlock( &internals->ref_spin );
572 /* Slow path */
573 /* Remember that we cannot hold the spin while waiting on the mutex */
574 libvlc_lock (p_this->p_libvlc);
575 /* Take the spin again. Note that another thread may have held the
576 * object in the (very short) mean time. */
577 vlc_spin_lock( &internals->ref_spin );
578 b_should_destroy = --internals->i_refcount == 0;
579 vlc_spin_unlock( &internals->ref_spin );
581 if( b_should_destroy )
583 /* Detach from parent to protect against FIND_CHILDREN */
584 parent = p_this->p_parent;
585 if (likely(parent))
587 /* Unlink */
588 if (internals->prev != NULL)
589 internals->prev->next = internals->next;
590 else
591 vlc_internals(parent)->first = internals->next;
592 if (internals->next != NULL)
593 internals->next->prev = internals->prev;
596 /* We have no children */
597 assert (internals->first == NULL);
599 libvlc_unlock (p_this->p_libvlc);
601 if( b_should_destroy )
603 int canc;
605 canc = vlc_savecancel ();
606 vlc_object_destroy( p_this );
607 vlc_restorecancel (canc);
608 if (parent)
609 vlc_object_release (parent);
613 #undef vlc_object_attach
615 ****************************************************************************
616 * attach object to a parent object
617 *****************************************************************************
618 * This function sets p_this as a child of p_parent, and p_parent as a parent
619 * of p_this.
620 *****************************************************************************/
621 void vlc_object_attach( vlc_object_t *p_this, vlc_object_t *p_parent )
623 if( !p_this ) return;
625 vlc_object_internals_t *pap = vlc_internals (p_parent);
626 vlc_object_internals_t *priv = vlc_internals (p_this);
628 priv->prev = NULL;
629 vlc_object_hold (p_parent);
630 libvlc_lock (p_this->p_libvlc);
632 /* Attach the parent to its child */
633 assert (p_this->p_parent == NULL);
634 p_this->p_parent = p_parent;
636 /* Attach the child to its parent */
637 priv->next = pap->first;
638 if (priv->next != NULL)
639 priv->next->prev = priv;
640 pap->first = priv;
641 libvlc_unlock (p_this->p_libvlc);
645 #undef vlc_list_children
647 * Gets the list of children of an objects, and increment their reference
648 * count.
649 * @return a list (possibly empty) or NULL in case of error.
651 vlc_list_t *vlc_list_children( vlc_object_t *obj )
653 vlc_list_t *l;
654 vlc_object_internals_t *priv;
655 unsigned count = 0;
657 libvlc_lock (obj->p_libvlc);
658 for (priv = vlc_internals (obj)->first; priv; priv = priv->next)
659 count++;
660 l = NewList (count);
661 if (likely(l != NULL))
663 unsigned i = 0;
665 for (priv = vlc_internals (obj)->first; priv; priv = priv->next)
666 l->p_values[i++].p_object = vlc_object_hold (vlc_externals (priv));
668 libvlc_unlock (obj->p_libvlc);
669 return l;
672 static void DumpVariable (const void *data, const VISIT which, const int depth)
674 if (which != postorder && which != leaf)
675 return;
676 (void) depth;
678 const variable_t *p_var = *(const variable_t **)data;
679 const char *psz_type = "unknown";
681 switch( p_var->i_type & VLC_VAR_TYPE )
683 #define MYCASE( type, nice ) \
684 case VLC_VAR_ ## type: \
685 psz_type = nice; \
686 break;
687 MYCASE( VOID, "void" );
688 MYCASE( BOOL, "bool" );
689 MYCASE( INTEGER, "integer" );
690 MYCASE( HOTKEY, "hotkey" );
691 MYCASE( STRING, "string" );
692 MYCASE( MODULE, "module" );
693 MYCASE( FILE, "file" );
694 MYCASE( DIRECTORY, "directory" );
695 MYCASE( VARIABLE, "variable" );
696 MYCASE( FLOAT, "float" );
697 MYCASE( TIME, "time" );
698 MYCASE( COORDS, "coords" );
699 MYCASE( ADDRESS, "address" );
700 MYCASE( MUTEX, "mutex" );
701 #undef MYCASE
703 printf( " *-o \"%s\" (%s", p_var->psz_name, psz_type );
704 if( p_var->psz_text )
705 printf( ", %s", p_var->psz_text );
706 fputc( ')', stdout );
707 if( p_var->i_type & VLC_VAR_HASCHOICE )
708 fputs( ", has choices", stdout );
709 if( p_var->i_type & VLC_VAR_ISCOMMAND )
710 fputs( ", command", stdout );
711 if( p_var->i_entries )
712 printf( ", %d callbacks", p_var->i_entries );
713 switch( p_var->i_type & VLC_VAR_CLASS )
715 case VLC_VAR_VOID:
716 case VLC_VAR_MUTEX:
717 break;
718 case VLC_VAR_BOOL:
719 printf( ": %s", p_var->val.b_bool ? "true" : "false" );
720 break;
721 case VLC_VAR_INTEGER:
722 printf( ": %"PRId64, p_var->val.i_int );
723 break;
724 case VLC_VAR_STRING:
725 printf( ": \"%s\"", p_var->val.psz_string );
726 break;
727 case VLC_VAR_FLOAT:
728 printf( ": %f", p_var->val.f_float );
729 break;
730 case VLC_VAR_TIME:
731 printf( ": %"PRIi64, (int64_t)p_var->val.i_time );
732 break;
733 case VLC_VAR_COORDS:
734 printf( ": %"PRId32"x%"PRId32,
735 p_var->val.coords.x, p_var->val.coords.y );
736 break;
737 case VLC_VAR_ADDRESS:
738 printf( ": %p", p_var->val.p_address );
739 break;
741 fputc( '\n', stdout );
744 /*****************************************************************************
745 * DumpCommand: print the current vlc structure
746 *****************************************************************************
747 * This function prints either an ASCII tree showing the connections between
748 * vlc objects, and additional information such as their refcount, thread ID,
749 * etc. (command "tree"), or the same data as a simple list (command "list").
750 *****************************************************************************/
751 static int DumpCommand( vlc_object_t *p_this, char const *psz_cmd,
752 vlc_value_t oldval, vlc_value_t newval, void *p_data )
754 (void)oldval; (void)p_data;
755 vlc_object_t *p_object = NULL;
757 if( *newval.psz_string )
759 /* try using the object's name to find it */
760 p_object = vlc_object_find_name( p_this, newval.psz_string,
761 FIND_ANYWHERE );
762 if( !p_object )
764 return VLC_ENOOBJ;
768 libvlc_lock (p_this->p_libvlc);
769 if( *psz_cmd == 't' )
771 char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
773 if( !p_object )
774 p_object = VLC_OBJECT(p_this->p_libvlc);
776 psz_foo[0] = '|';
777 DumpStructure( vlc_internals(p_object), 0, psz_foo );
779 else if( *psz_cmd == 'v' )
781 if( !p_object )
782 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
784 PrintObject( vlc_internals(p_object), "" );
785 vlc_mutex_lock( &vlc_internals( p_object )->var_lock );
786 if( vlc_internals( p_object )->var_root == NULL )
787 puts( " `-o No variables" );
788 else
789 twalk( vlc_internals( p_object )->var_root, DumpVariable );
790 vlc_mutex_unlock( &vlc_internals( p_object )->var_lock );
792 libvlc_unlock (p_this->p_libvlc);
794 if( *newval.psz_string )
796 vlc_object_release( p_object );
798 return VLC_SUCCESS;
801 /*****************************************************************************
802 * vlc_list_release: free a list previously allocated by vlc_list_find
803 *****************************************************************************
804 * This function decreases the refcount of all objects in the list and
805 * frees the list.
806 *****************************************************************************/
807 void vlc_list_release( vlc_list_t *p_list )
809 int i_index;
811 for( i_index = 0; i_index < p_list->i_count; i_index++ )
813 vlc_object_release( p_list->p_values[i_index].p_object );
816 free( p_list->p_values );
817 free( p_list );
820 /* Following functions are local */
822 static vlc_object_t *FindParent (vlc_object_t *p_this, int i_type)
824 for (vlc_object_t *parent = p_this->p_parent;
825 parent != NULL;
826 parent = parent->p_parent)
828 if (vlc_internals (parent)->i_object_type == i_type)
829 return vlc_object_hold (parent);
831 return NULL;
834 static vlc_object_t *FindParentName (vlc_object_t *p_this, const char *name)
836 for (vlc_object_t *parent = p_this->p_parent;
837 parent != NULL;
838 parent = parent->p_parent)
840 const char *objname = vlc_internals (parent)->psz_name;
841 if (objname && !strcmp (objname, name))
842 return vlc_object_hold (parent);
844 return NULL;
847 static vlc_object_t *FindChild (vlc_object_internals_t *priv, int i_type)
849 for (priv = priv->first; priv != NULL; priv = priv->next)
851 if (priv->i_object_type == i_type)
852 return vlc_object_hold (vlc_externals (priv));
854 vlc_object_t *found = FindChild (priv, i_type);
855 if (found != NULL)
856 return found;
858 return NULL;
861 static vlc_object_t *FindChildName (vlc_object_internals_t *priv,
862 const char *name)
864 for (priv = priv->first; priv != NULL; priv = priv->next)
866 if (priv->psz_name && !strcmp (priv->psz_name, name))
867 return vlc_object_hold (vlc_externals (priv));
869 vlc_object_t *found = FindChildName (priv, name);
870 if (found != NULL)
871 return found;
873 return NULL;
876 static void PrintObject( vlc_object_internals_t *priv,
877 const char *psz_prefix )
879 char psz_refcount[20], psz_thread[30], psz_name[50], psz_parent[20];
881 int canc = vlc_savecancel ();
882 memset( &psz_name, 0, sizeof(psz_name) );
884 vlc_mutex_lock (&name_lock);
885 if (priv->psz_name != NULL)
887 snprintf( psz_name, 49, " \"%s\"", priv->psz_name );
888 if( psz_name[48] )
889 psz_name[48] = '\"';
891 vlc_mutex_unlock (&name_lock);
893 psz_refcount[0] = '\0';
894 if( priv->i_refcount > 0 )
895 snprintf( psz_refcount, 19, ", %u refs", priv->i_refcount );
897 psz_thread[0] = '\0';
898 if( priv->b_thread )
899 snprintf( psz_thread, 29, " (thread %lu)",
900 (unsigned long)priv->thread_id );
902 psz_parent[0] = '\0';
903 /* FIXME: need structure lock!!! */
904 if( vlc_externals(priv)->p_parent )
905 snprintf( psz_parent, 19, ", parent %p",
906 vlc_externals(priv)->p_parent );
908 printf( " %so %p %s%s%s%s%s\n", psz_prefix,
909 vlc_externals(priv), vlc_externals(priv)->psz_object_type,
910 psz_name, psz_thread, psz_refcount, psz_parent );
911 vlc_restorecancel (canc);
914 static void DumpStructure (vlc_object_internals_t *priv, unsigned i_level,
915 char *psz_foo)
917 char i_back = psz_foo[i_level];
918 psz_foo[i_level] = '\0';
920 PrintObject (priv, psz_foo);
922 psz_foo[i_level] = i_back;
924 if( i_level / 2 >= MAX_DUMPSTRUCTURE_DEPTH )
926 msg_Warn( vlc_externals(priv), "structure tree is too deep" );
927 return;
930 for (priv = priv->first; priv != NULL; priv = priv->next)
932 if( i_level )
934 psz_foo[i_level-1] = ' ';
936 if( psz_foo[i_level-2] == '`' )
938 psz_foo[i_level-2] = ' ';
942 psz_foo[i_level] = priv->next ? '|' : '`';
943 psz_foo[i_level+1] = '-';
944 psz_foo[i_level+2] = '\0';
946 DumpStructure (priv, i_level + 2, psz_foo);
950 static vlc_list_t * NewList( int i_count )
952 vlc_list_t * p_list = malloc( sizeof( vlc_list_t ) );
953 if( p_list == NULL )
954 return NULL;
956 p_list->i_count = i_count;
958 if( i_count == 0 )
960 p_list->p_values = NULL;
961 return p_list;
964 p_list->p_values = malloc( i_count * sizeof( vlc_value_t ) );
965 if( p_list->p_values == NULL )
967 p_list->i_count = 0;
968 return p_list;
971 return p_list;