1 /*****************************************************************************
2 * objects.c: vlc_object_t handling
3 *****************************************************************************
4 * Copyright (C) 2004-2007 the VideoLAN team
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 *****************************************************************************/
26 * This file contains the functions to handle the vlc_object_t type
30 /*****************************************************************************
32 *****************************************************************************/
39 #include "../libvlc.h"
42 #include "audio_output/aout_internal.h"
44 #include <vlc_access.h>
45 #include <vlc_demux.h>
46 #include <vlc_stream.h>
49 #include "stream_output/stream_output.h"
51 #include "vlc_playlist.h"
52 #include "vlc_interface.h"
53 #include "vlc_codec.h"
54 #include "vlc_filter.h"
56 #include "vlc_httpd.h"
58 #include "input/vlm_internal.h"
65 #include "variables.h"
71 # include <errno.h> /* ENOSYS */
74 /*****************************************************************************
76 *****************************************************************************/
77 static int DumpCommand( vlc_object_t *, char const *,
78 vlc_value_t, vlc_value_t, void * );
80 static vlc_object_t * FindObject ( vlc_object_t *, int, int );
81 static vlc_object_t * FindObjectName( vlc_object_t *, const char *, int );
82 static void DetachObject ( vlc_object_t * );
83 static void PrintObject ( vlc_object_t *, const char * );
84 static void DumpStructure ( vlc_object_t *, int, char * );
85 static int FindIndex ( vlc_object_t *, vlc_object_t **, int );
86 static void SetAttachment ( vlc_object_t *, vlc_bool_t );
88 static vlc_list_t * NewList ( int );
89 static void ListReplace ( vlc_list_t *, vlc_object_t *, int );
90 /*static void ListAppend ( vlc_list_t *, vlc_object_t * );*/
91 static int CountChildren ( vlc_object_t *, int );
92 static void ListChildren ( vlc_list_t *, vlc_object_t *, int );
94 /*****************************************************************************
95 * Local structure lock
96 *****************************************************************************/
97 static vlc_mutex_t structure_lock;
98 static vlc_object_internals_t global_internals;
100 vlc_object_t *vlc_custom_create( vlc_object_t *p_this, size_t i_size,
101 int i_type, const char *psz_type )
104 vlc_object_internals_t *p_priv;
106 if( i_type == VLC_OBJECT_GLOBAL )
109 p_priv = &global_internals;
110 memset( p_priv, 0, sizeof( *p_priv ) );
114 p_priv = calloc( 1, sizeof( *p_priv ) + i_size );
118 p_new = (vlc_object_t *)(p_priv + 1);
121 p_new->p_internals = p_priv;
122 p_new->i_object_type = i_type;
123 p_new->psz_object_type = psz_type;
125 p_new->psz_object_name = NULL;
127 p_new->b_die = VLC_FALSE;
128 p_new->b_error = VLC_FALSE;
129 p_new->b_dead = VLC_FALSE;
130 p_priv->b_attached = VLC_FALSE;
131 p_new->b_force = VLC_FALSE;
133 p_new->psz_header = NULL;
135 if( p_this->i_flags & OBJECT_FLAGS_NODBG )
136 p_new->i_flags |= OBJECT_FLAGS_NODBG;
137 if( p_this->i_flags & OBJECT_FLAGS_QUIET )
138 p_new->i_flags |= OBJECT_FLAGS_QUIET;
139 if( p_this->i_flags & OBJECT_FLAGS_NOINTERACT )
140 p_new->i_flags |= OBJECT_FLAGS_NOINTERACT;
142 p_priv->p_vars = calloc( sizeof( variable_t ), 16 );
144 if( !p_priv->p_vars )
146 if( i_type != VLC_OBJECT_GLOBAL )
151 if( i_type == VLC_OBJECT_GLOBAL )
153 /* If i_type is global, then p_new is actually p_libvlc_global */
154 libvlc_global_data_t *p_libvlc_global = (libvlc_global_data_t *)p_new;
155 p_new->p_libvlc = NULL;
157 p_libvlc_global->i_counter = 0;
158 p_new->i_object_id = 0;
160 p_libvlc_global->i_objects = 1;
161 p_libvlc_global->pp_objects = malloc( sizeof(vlc_object_t *) );
162 p_libvlc_global->pp_objects[0] = p_new;
163 p_priv->b_attached = VLC_TRUE;
167 libvlc_global_data_t *p_libvlc_global = vlc_global();
168 if( i_type == VLC_OBJECT_LIBVLC )
170 p_new->p_libvlc = (libvlc_int_t*)p_new;
171 p_priv->b_attached = VLC_TRUE;
175 p_new->p_libvlc = p_this->p_libvlc;
178 vlc_mutex_lock( &structure_lock );
180 p_libvlc_global->i_counter++;
181 p_new->i_object_id = p_libvlc_global->i_counter;
183 /* Wooohaa! If *this* fails, we're in serious trouble! Anyway it's
184 * useless to try and recover anything if pp_objects gets smashed. */
185 TAB_APPEND( p_libvlc_global->i_objects, p_libvlc_global->pp_objects,
188 vlc_mutex_unlock( &structure_lock );
191 p_priv->i_refcount = 0;
192 p_new->p_parent = NULL;
193 p_new->pp_children = NULL;
194 p_new->i_children = 0;
196 p_new->p_private = NULL;
198 /* Initialize mutexes and condvars */
199 vlc_mutex_init( p_new, &p_new->object_lock );
200 vlc_cond_init( p_new, &p_new->object_wait );
201 vlc_mutex_init( p_new, &p_priv->var_lock );
202 vlc_spin_init( &p_priv->spin );
203 p_priv->pipes[0] = p_priv->pipes[1] = -1;
205 if( i_type == VLC_OBJECT_GLOBAL )
207 vlc_mutex_init( p_new, &structure_lock );
210 if( i_type == VLC_OBJECT_LIBVLC )
212 var_Create( p_new, "list", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
213 var_AddCallback( p_new, "list", DumpCommand, NULL );
214 var_Create( p_new, "tree", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
215 var_AddCallback( p_new, "tree", DumpCommand, NULL );
216 var_Create( p_new, "vars", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
217 var_AddCallback( p_new, "vars", DumpCommand, NULL );
225 * Allocates and initializes a vlc object.
227 * @param i_type known object type (all of them are negative integer values),
228 * or object byte size (always positive).
230 * @return the new object, or NULL on error.
232 void * __vlc_object_create( vlc_object_t *p_this, int i_type )
234 const char * psz_type;
239 case VLC_OBJECT_GLOBAL:
240 i_size = sizeof(libvlc_global_data_t);
243 case VLC_OBJECT_LIBVLC:
244 i_size = sizeof(libvlc_int_t);
247 case VLC_OBJECT_INTF:
248 i_size = sizeof(intf_thread_t);
249 psz_type = "interface";
251 case VLC_OBJECT_DIALOGS:
252 i_size = sizeof(intf_thread_t);
253 psz_type = "dialogs";
255 case VLC_OBJECT_PLAYLIST:
256 i_size = sizeof(playlist_t);
257 psz_type = "playlist";
260 i_size = sizeof(services_discovery_t);
261 psz_type = "services discovery";
263 case VLC_OBJECT_INPUT:
264 i_size = sizeof(input_thread_t);
267 case VLC_OBJECT_DEMUX:
268 i_size = sizeof(demux_t);
271 case VLC_OBJECT_ACCESS:
272 i_size = sizeof(access_t);
275 case VLC_OBJECT_DECODER:
276 i_size = sizeof(decoder_t);
277 psz_type = "decoder";
279 case VLC_OBJECT_PACKETIZER:
280 i_size = sizeof(decoder_t);
281 psz_type = "packetizer";
283 case VLC_OBJECT_ENCODER:
284 i_size = sizeof(encoder_t);
285 psz_type = "encoder";
287 case VLC_OBJECT_FILTER:
288 i_size = sizeof(filter_t);
291 case VLC_OBJECT_VOUT:
292 i_size = sizeof(vout_thread_t);
293 psz_type = "video output";
296 i_size = sizeof(spu_t);
297 psz_type = "subpicture";
299 case VLC_OBJECT_AOUT:
300 i_size = sizeof(aout_instance_t);
301 psz_type = "audio output";
303 case VLC_OBJECT_SOUT:
304 i_size = sizeof(sout_instance_t);
305 psz_type = "stream output";
308 i_size = sizeof( vlm_t );
309 psz_type = "vlm dameon";
312 i_size = sizeof( vod_t );
313 psz_type = "vod server";
316 i_size = sizeof( xml_t );
319 case VLC_OBJECT_OPENGL:
320 i_size = sizeof( vout_thread_t );
323 case VLC_OBJECT_ANNOUNCE:
324 i_size = sizeof( announce_handler_t );
325 psz_type = "announce";
327 case VLC_OBJECT_META_ENGINE:
328 i_size = sizeof( meta_engine_t );
329 psz_type = "meta engine";
331 case VLC_OBJECT_OSDMENU:
332 i_size = sizeof( osd_menu_t );
333 psz_type = "osd menu";
336 i_size = i_type > (int)sizeof(vlc_object_t)
337 ? i_type : (int)sizeof(vlc_object_t);
338 i_type = VLC_OBJECT_GENERIC;
339 psz_type = "generic";
343 return vlc_custom_create( p_this, i_size, i_type, psz_type );
348 ****************************************************************************
349 * Destroy a vlc object
351 * This function destroys an object that has been previously allocated with
352 * vlc_object_create. The object's refcount must be zero and it must not be
353 * attached to other objects in any way.
354 *****************************************************************************/
355 void __vlc_object_destroy( vlc_object_t *p_this )
357 vlc_object_internals_t *p_priv = vlc_internals( p_this );
360 if( p_this->i_children )
362 msg_Err( p_this, "cannot delete object (%i, %s) with children" ,
363 p_this->i_object_id, p_this->psz_object_name );
367 if( p_this->p_parent )
369 msg_Err( p_this, "cannot delete object (%i, %s) with a parent",
370 p_this->i_object_id, p_this->psz_object_name );
374 while( p_priv->i_refcount > 0 )
378 /* Don't warn immediately ... 100ms seems OK */
382 "refcount is %u, delaying before deletion (id=%d,type=%d)",
383 p_priv->i_refcount, p_this->i_object_id,
384 p_this->i_object_type );
386 else if( i_delay == 10 )
389 "refcount is %u, delaying again (id=%d,type=%d)",
390 p_priv->i_refcount, p_this->i_object_id,
391 p_this->i_object_type );
393 else if( i_delay == 20 )
396 "waited too long, cancelling destruction (id=%d,type=%d)",
397 p_this->i_object_id, p_this->i_object_type );
404 /* Destroy the associated variables, starting from the end so that
405 * no memmove calls have to be done. */
406 while( p_priv->i_vars )
408 var_Destroy( p_this, p_priv->p_vars[p_priv->i_vars - 1].psz_name );
411 free( p_priv->p_vars );
412 vlc_mutex_destroy( &p_priv->var_lock );
414 if( p_this->psz_header ) free( p_this->psz_header );
416 if( p_this->i_object_type == VLC_OBJECT_GLOBAL )
418 libvlc_global_data_t *p_global = (libvlc_global_data_t *)p_this;
419 /* We are the global object ... no need to lock. */
420 free( p_global->pp_objects );
421 p_global->pp_objects = NULL;
422 p_global->i_objects--;
424 vlc_mutex_destroy( &structure_lock );
428 libvlc_global_data_t *p_libvlc_global = vlc_global();
431 vlc_mutex_lock( &structure_lock );
433 /* Wooohaa! If *this* fails, we're in serious trouble! Anyway it's
434 * useless to try and recover anything if pp_objects gets smashed. */
435 i_index = FindIndex( p_this, p_libvlc_global->pp_objects,
436 p_libvlc_global->i_objects );
437 REMOVE_ELEM( p_libvlc_global->pp_objects,
438 p_libvlc_global->i_objects, i_index );
440 vlc_mutex_unlock( &structure_lock );
443 #if defined(WIN32) || defined(UNDER_CE)
444 /* if object has an associated thread, close it now */
445 if( p_priv->thread_id.hThread )
446 CloseHandle(p_priv->thread_id.hThread);
449 vlc_mutex_destroy( &p_this->object_lock );
450 vlc_cond_destroy( &p_this->object_wait );
451 vlc_spin_destroy( &p_priv->spin );
452 if( p_priv->pipes[0] != -1 )
453 close( p_priv->pipes[0] );
454 if( p_priv->pipes[1] != -1 )
455 close( p_priv->pipes[1] );
457 /* global is not dynamically allocated by vlc_object_create */
458 if( p_this->i_object_type != VLC_OBJECT_GLOBAL )
463 /** Inter-object signaling */
465 void __vlc_object_lock( vlc_object_t *obj )
467 vlc_mutex_lock( &obj->object_lock );
470 void __vlc_object_unlock( vlc_object_t *obj )
472 vlc_assert_locked( &obj->object_lock );
473 vlc_mutex_unlock( &obj->object_lock );
477 # include <winsock2.h>
478 # include <ws2tcpip.h>
481 * select()-able pipes emulated using Winsock
483 static int pipe (int fd[2])
486 int addrlen = sizeof (addr);
488 SOCKET l = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP), a,
489 c = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
490 if ((l == INVALID_SOCKET) || (c == INVALID_SOCKET))
493 memset (&addr, 0, sizeof (addr));
494 addr.sin_family = AF_INET;
495 addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
496 if (bind (l, (PSOCKADDR)&addr, sizeof (addr))
497 || getsockname (l, (PSOCKADDR)&addr, &addrlen)
499 || connect (c, (PSOCKADDR)&addr, addrlen))
502 a = accept (l, NULL, NULL);
503 if (a == INVALID_SOCKET)
514 if (l != INVALID_SOCKET)
516 if (c != INVALID_SOCKET)
522 #define read( a, b, c ) recv (a, b, c, 0)
524 #define write( a, b, c ) send (a, b, c, 0)
526 #define close( a ) closesocket (a)
530 * Returns the readable end of a pipe that becomes readable whenever
531 * an object is signaled. This can be used to wait for VLC object events
532 * inside select(), poll() loops or frameworks providing an event loop.
534 * Note that the pipe will remain the same for the lifetime of the object.
535 * DO NOT close it yourself. Ever.
537 * DO NOT try to read from the pipe either: call vlc_object_wait() instead.
538 * Assuming the pipe is readable, vlc_object_wait() will not block.
539 * Also note that, as with vlc_object_wait(), there may be spurious wakeups.
541 * @param obj object that would be signaled
542 * @return a readable pipe descriptor, or -1 on error.
544 int __vlc_object_waitpipe( vlc_object_t *obj )
546 int pfd[2] = { -1, -1 };
547 struct vlc_object_internals_t *internals = obj->p_internals;
548 vlc_bool_t race = VLC_FALSE, signaled = VLC_FALSE;
550 vlc_spin_lock (&internals->spin);
551 if (internals->pipes[0] == -1)
553 /* This can only ever happen if someone killed us without locking: */
554 assert (internals->pipes[1] == -1);
555 vlc_spin_unlock (&internals->spin);
560 vlc_spin_lock (&internals->spin);
561 signaled = internals->b_signaled;
562 if (internals->pipes[0] == -1)
564 internals->pipes[0] = pfd[0];
565 internals->pipes[1] = pfd[1];
570 vlc_spin_unlock (&internals->spin);
573 { /* Race condition: two threads call pipe() - unlikely */
579 /* Race condition: lc_object_signal() already invoked! */
580 while (write (internals->pipes[1], &(char){ 0 }, 1) < 0);
582 return internals->pipes[0];
587 * Waits for the object to be signaled (using vlc_object_signal()).
588 * If the object already has a signal pending, this function will return
589 * immediately. It is asserted that the caller holds the object lock.
591 * @return true if the object is dying and should terminate.
593 vlc_bool_t __vlc_object_wait( vlc_object_t *obj )
597 vlc_assert_locked( &obj->object_lock );
599 fd = obj->p_internals->pipes[0];
602 while (read (fd, &(char){ 0 }, 1) < 0);
606 vlc_cond_wait( &obj->object_wait, &obj->object_lock );
607 obj->p_internals->b_signaled = VLC_FALSE;
613 * Waits for the object to be signaled (using vlc_object_signal()), or for
615 * If the object already has a signal pending, this function will return
616 * immediately. It is asserted that the caller holds the object lock.
618 * @return negative if the object is dying and should terminate,
619 * positive if the the object has been signaled but is not dying,
620 * 0 if timeout has been reached.
622 int __vlc_object_timedwait( vlc_object_t *obj, mtime_t deadline )
626 vlc_assert_locked( &obj->object_lock );
627 v = vlc_cond_timedwait( &obj->object_wait, &obj->object_lock, deadline );
628 if( v == 0 ) /* signaled */
629 return obj->b_die ? -1 : 1;
635 * Checks whether an object has been "killed".
636 * The object lock must be held.
638 * Typical code for an object thread could be:
640 vlc_object_lock (self);
642 while (vlc_object_alive (self))
646 if (vlc_object_wait (self))
651 ...deinitialization...
652 vlc_object_unlock (self);
655 * @return true iff the object has not been killed yet
657 vlc_bool_t __vlc_object_alive( vlc_object_t *obj )
659 vlc_assert_locked( &obj->object_lock );
665 * Signals an object for which the lock is held.
667 void __vlc_object_signal_unlocked( vlc_object_t *obj )
669 struct vlc_object_internals_t *internals = obj->p_internals;
672 vlc_assert_locked (&obj->object_lock);
674 vlc_spin_lock (&internals->spin);
675 fd = internals->pipes[1];
676 internals->b_signaled = VLC_TRUE;
677 vlc_spin_unlock (&internals->spin);
680 while( write( fd, &(char){ 0 }, 1 ) < 0 );
682 vlc_cond_signal( &obj->object_wait );
687 * Requests termination of an object.
688 * If the object is LibVLC, also request to terminate all its children.
690 void __vlc_object_kill( vlc_object_t *p_this )
692 vlc_mutex_lock( &p_this->object_lock );
693 p_this->b_die = VLC_TRUE;
695 if( p_this->i_object_type == VLC_OBJECT_LIBVLC )
696 for( int i = 0; i < p_this->i_children ; i++ )
697 vlc_object_kill( p_this->pp_children[i] );
699 vlc_object_signal_unlocked( p_this );
700 vlc_mutex_unlock( &p_this->object_lock );
705 * find an object given its ID
707 * This function looks for the object whose i_object_id field is i_id. We
708 * use a dichotomy so that lookups are in log2(n).
709 *****************************************************************************/
710 void * __vlc_object_get( vlc_object_t *p_this, int i_id )
713 vlc_object_t **pp_objects;
714 libvlc_global_data_t *p_libvlc_global = vlc_global();
716 vlc_mutex_lock( &structure_lock );
718 pp_objects = p_libvlc_global->pp_objects;
720 /* Perform our dichotomy */
721 for( i_max = p_libvlc_global->i_objects - 1 ; ; )
723 i_middle = i_max / 2;
725 if( pp_objects[i_middle]->i_object_id > i_id )
729 else if( pp_objects[i_middle]->i_object_id < i_id )
733 pp_objects += i_middle;
738 /* This happens when there are only two remaining objects */
739 if( pp_objects[i_middle+1]->i_object_id == i_id )
741 vlc_mutex_unlock( &structure_lock );
742 pp_objects[i_middle+1]->p_internals->i_refcount++;
743 return pp_objects[i_middle+1];
750 vlc_mutex_unlock( &structure_lock );
751 pp_objects[i_middle]->p_internals->i_refcount++;
752 return pp_objects[i_middle];
757 /* this means that i_max == i_middle, and since we have already
758 * tested pp_objects[i_middle]), p_found is properly set. */
763 vlc_mutex_unlock( &structure_lock );
768 ****************************************************************************
769 * find a typed object and increment its refcount
770 *****************************************************************************
771 * This function recursively looks for a given object type. i_mode can be one
772 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
773 *****************************************************************************/
774 void * __vlc_object_find( vlc_object_t *p_this, int i_type, int i_mode )
776 vlc_object_t *p_found;
778 vlc_mutex_lock( &structure_lock );
780 /* If we are of the requested type ourselves, don't look further */
781 if( !(i_mode & FIND_STRICT) && p_this->i_object_type == i_type )
783 p_this->p_internals->i_refcount++;
784 vlc_mutex_unlock( &structure_lock );
788 /* Otherwise, recursively look for the object */
789 if( (i_mode & 0x000f) == FIND_ANYWHERE )
791 vlc_object_t *p_root = p_this;
794 while( p_root->p_parent != NULL &&
795 p_root != VLC_OBJECT( p_this->p_libvlc ) )
797 p_root = p_root->p_parent;
800 p_found = FindObject( p_root, i_type, (i_mode & ~0x000f)|FIND_CHILD );
801 if( p_found == NULL && p_root != VLC_OBJECT( p_this->p_libvlc ) )
803 p_found = FindObject( VLC_OBJECT( p_this->p_libvlc ),
804 i_type, (i_mode & ~0x000f)|FIND_CHILD );
809 p_found = FindObject( p_this, i_type, i_mode );
812 vlc_mutex_unlock( &structure_lock );
818 ****************************************************************************
819 * find a named object and increment its refcount
820 *****************************************************************************
821 * This function recursively looks for a given object name. i_mode can be one
822 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
823 *****************************************************************************/
824 void * __vlc_object_find_name( vlc_object_t *p_this, const char *psz_name,
827 vlc_object_t *p_found;
829 vlc_mutex_lock( &structure_lock );
831 /* If have the requested name ourselves, don't look further */
832 if( !(i_mode & FIND_STRICT)
833 && p_this->psz_object_name
834 && !strcmp( p_this->psz_object_name, psz_name ) )
836 p_this->p_internals->i_refcount++;
837 vlc_mutex_unlock( &structure_lock );
841 /* Otherwise, recursively look for the object */
842 if( (i_mode & 0x000f) == FIND_ANYWHERE )
844 vlc_object_t *p_root = p_this;
847 while( p_root->p_parent != NULL &&
848 p_root != VLC_OBJECT( p_this->p_libvlc ) )
850 p_root = p_root->p_parent;
853 p_found = FindObjectName( p_root, psz_name,
854 (i_mode & ~0x000f)|FIND_CHILD );
855 if( p_found == NULL && p_root != VLC_OBJECT( p_this->p_libvlc ) )
857 p_found = FindObjectName( VLC_OBJECT( p_this->p_libvlc ),
858 psz_name, (i_mode & ~0x000f)|FIND_CHILD );
863 p_found = FindObjectName( p_this, psz_name, i_mode );
866 vlc_mutex_unlock( &structure_lock );
872 ****************************************************************************
873 * increment an object refcount
874 *****************************************************************************/
875 void __vlc_object_yield( vlc_object_t *p_this )
877 vlc_mutex_lock( &structure_lock );
878 p_this->p_internals->i_refcount++;
879 vlc_mutex_unlock( &structure_lock );
882 static inline void Release( vlc_object_t *obj )
884 assert( obj->p_internals->i_refcount > 0 );
885 obj->p_internals->i_refcount--;
888 /*****************************************************************************
889 * decrement an object refcount
890 *****************************************************************************/
891 void __vlc_object_release( vlc_object_t *p_this )
893 vlc_mutex_lock( &structure_lock );
895 vlc_mutex_unlock( &structure_lock );
899 ****************************************************************************
900 * attach object to a parent object
901 *****************************************************************************
902 * This function sets p_this as a child of p_parent, and p_parent as a parent
903 * of p_this. This link can be undone using vlc_object_detach.
904 *****************************************************************************/
905 void __vlc_object_attach( vlc_object_t *p_this, vlc_object_t *p_parent )
907 if( !p_this ) return;
909 vlc_mutex_lock( &structure_lock );
911 /* Attach the parent to its child */
912 p_this->p_parent = p_parent;
914 /* Attach the child to its parent */
915 INSERT_ELEM( p_parent->pp_children, p_parent->i_children,
916 p_parent->i_children, p_this );
918 /* Climb up the tree to see whether we are connected with the root */
919 if( p_parent->p_internals->b_attached )
921 SetAttachment( p_this, VLC_TRUE );
924 vlc_mutex_unlock( &structure_lock );
928 ****************************************************************************
929 * detach object from its parent
930 *****************************************************************************
931 * This function removes all links between an object and its parent.
932 *****************************************************************************/
933 void __vlc_object_detach( vlc_object_t *p_this )
935 if( !p_this ) return;
937 vlc_mutex_lock( &structure_lock );
938 if( !p_this->p_parent )
940 msg_Err( p_this, "object is not attached" );
941 vlc_mutex_unlock( &structure_lock );
945 /* Climb up the tree to see whether we are connected with the root */
946 if( p_this->p_parent->p_internals->b_attached )
948 SetAttachment( p_this, VLC_FALSE );
951 DetachObject( p_this );
952 vlc_mutex_unlock( &structure_lock );
957 ****************************************************************************
958 * find a list typed objects and increment their refcount
959 *****************************************************************************
960 * This function recursively looks for a given object type. i_mode can be one
961 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
962 *****************************************************************************/
963 vlc_list_t * __vlc_list_find( vlc_object_t *p_this, int i_type, int i_mode )
966 vlc_object_t **pp_current, **pp_end;
967 int i_count = 0, i_index = 0;
968 libvlc_global_data_t *p_libvlc_global = vlc_global();
970 vlc_mutex_lock( &structure_lock );
972 /* Look for the objects */
973 switch( i_mode & 0x000f )
976 pp_current = p_libvlc_global->pp_objects;
977 pp_end = pp_current + p_libvlc_global->i_objects;
979 for( ; pp_current < pp_end ; pp_current++ )
981 if( (*pp_current)->p_internals->b_attached
982 && (*pp_current)->i_object_type == i_type )
988 p_list = NewList( i_count );
989 pp_current = p_libvlc_global->pp_objects;
991 for( ; pp_current < pp_end ; pp_current++ )
993 if( (*pp_current)->p_internals->b_attached
994 && (*pp_current)->i_object_type == i_type )
996 ListReplace( p_list, *pp_current, i_index );
997 if( i_index < i_count ) i_index++;
1003 i_count = CountChildren( p_this, i_type );
1004 p_list = NewList( i_count );
1006 /* Check allocation was successful */
1007 if( p_list->i_count != i_count )
1009 msg_Err( p_this, "list allocation failed!" );
1010 p_list->i_count = 0;
1014 p_list->i_count = 0;
1015 ListChildren( p_list, p_this, i_type );
1019 msg_Err( p_this, "unimplemented!" );
1020 p_list = NewList( 0 );
1024 vlc_mutex_unlock( &structure_lock );
1029 /*****************************************************************************
1030 * DumpCommand: print the current vlc structure
1031 *****************************************************************************
1032 * This function prints either an ASCII tree showing the connections between
1033 * vlc objects, and additional information such as their refcount, thread ID,
1034 * etc. (command "tree"), or the same data as a simple list (command "list").
1035 *****************************************************************************/
1036 static int DumpCommand( vlc_object_t *p_this, char const *psz_cmd,
1037 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1039 libvlc_global_data_t *p_libvlc_global = vlc_global();
1041 (void)oldval; (void)p_data;
1042 if( *psz_cmd == 'l' )
1044 vlc_mutex_lock( &structure_lock );
1046 vlc_object_t **pp_current, **pp_end;
1048 pp_current = p_libvlc_global->pp_objects;
1049 pp_end = pp_current + p_libvlc_global->i_objects;
1051 for( ; pp_current < pp_end ; pp_current++ )
1053 if( (*pp_current)->p_internals->b_attached )
1055 PrintObject( *pp_current, "" );
1059 printf( " o %.8i %s (not attached)\n",
1060 (*pp_current)->i_object_id,
1061 (*pp_current)->psz_object_type );
1065 vlc_mutex_unlock( &structure_lock );
1069 vlc_object_t *p_object = NULL;
1071 if( *newval.psz_string )
1074 int i_id = strtol( newval.psz_string, &end, 0 );
1075 if( end != newval.psz_string )
1076 p_object = vlc_object_get( p_this, i_id );
1079 /* try using the object's name to find it */
1080 vlc_object_t *p_libvlc = vlc_object_get( p_this, 1 );
1083 /* Look in p_libvlc's children tree */
1084 p_object = vlc_object_find_name( p_libvlc,
1087 vlc_object_release( p_libvlc );
1091 /* If it's not in libvlc, look in libvlc_global (== p_this) */
1092 p_object = vlc_object_find_name( p_this,
1104 vlc_mutex_lock( &structure_lock );
1106 if( *psz_cmd == 't' )
1108 char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
1111 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
1114 DumpStructure( p_object, 0, psz_foo );
1116 else if( *psz_cmd == 'v' )
1121 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
1123 PrintObject( p_object, "" );
1125 if( !p_object->p_internals->i_vars )
1126 printf( " `-o No variables\n" );
1127 for( i = 0; i < p_object->p_internals->i_vars; i++ )
1129 variable_t *p_var = p_object->p_internals->p_vars + i;
1131 const char *psz_type = "unknown";
1132 switch( p_var->i_type & VLC_VAR_TYPE )
1134 #define MYCASE( type, nice ) \
1135 case VLC_VAR_ ## type: \
1138 MYCASE( VOID, "void" );
1139 MYCASE( BOOL, "bool" );
1140 MYCASE( INTEGER, "integer" );
1141 MYCASE( HOTKEY, "hotkey" );
1142 MYCASE( STRING, "string" );
1143 MYCASE( MODULE, "module" );
1144 MYCASE( FILE, "file" );
1145 MYCASE( DIRECTORY, "directory" );
1146 MYCASE( VARIABLE, "variable" );
1147 MYCASE( FLOAT, "float" );
1148 MYCASE( TIME, "time" );
1149 MYCASE( ADDRESS, "address" );
1150 MYCASE( MUTEX, "mutex" );
1151 MYCASE( LIST, "list" );
1154 printf( " %c-o \"%s\" (%s",
1155 i + 1 == p_object->p_internals->i_vars ? '`' : '|',
1156 p_var->psz_name, psz_type );
1157 if( p_var->psz_text )
1158 printf( ", %s", p_var->psz_text );
1160 if( p_var->i_type & VLC_VAR_ISCOMMAND )
1161 printf( ", command" );
1162 if( p_var->i_entries )
1163 printf( ", %d callbacks", p_var->i_entries );
1164 switch( p_var->i_type & 0x00f0 )
1170 printf( ": %s", p_var->val.b_bool ? "true" : "false" );
1172 case VLC_VAR_INTEGER:
1173 printf( ": %d", p_var->val.i_int );
1175 case VLC_VAR_STRING:
1176 printf( ": \"%s\"", p_var->val.psz_string );
1179 printf( ": %f", p_var->val.f_float );
1182 printf( ": " I64Fi, (int64_t)p_var->val.i_time );
1184 case VLC_VAR_ADDRESS:
1185 printf( ": %p", p_var->val.p_address );
1195 vlc_mutex_unlock( &structure_lock );
1197 if( *newval.psz_string )
1199 vlc_object_release( p_object );
1206 /*****************************************************************************
1207 * vlc_list_release: free a list previously allocated by vlc_list_find
1208 *****************************************************************************
1209 * This function decreases the refcount of all objects in the list and
1211 *****************************************************************************/
1212 void vlc_list_release( vlc_list_t *p_list )
1216 vlc_mutex_lock( &structure_lock );
1217 for( i_index = 0; i_index < p_list->i_count; i_index++ )
1219 Release( p_list->p_values[i_index].p_object );
1221 vlc_mutex_unlock( &structure_lock );
1223 free( p_list->p_values );
1227 /* Following functions are local */
1229 /*****************************************************************************
1230 * FindIndex: find the index of an object in an array of objects
1231 *****************************************************************************
1232 * This function assumes that p_this can be found in pp_objects. It will not
1233 * crash if p_this cannot be found, but will return a wrong value. It is your
1234 * duty to check the return value if you are not certain that the object could
1235 * be found for sure.
1236 *****************************************************************************/
1237 static int FindIndex( vlc_object_t *p_this,
1238 vlc_object_t **pp_objects, int i_count )
1240 int i_middle = i_count / 2;
1247 if( pp_objects[i_middle] == p_this )
1257 /* We take advantage of the sorted array */
1258 if( pp_objects[i_middle]->i_object_id < p_this->i_object_id )
1260 return i_middle + FindIndex( p_this, pp_objects + i_middle,
1261 i_count - i_middle );
1265 return FindIndex( p_this, pp_objects, i_middle );
1269 static vlc_object_t * FindObject( vlc_object_t *p_this, int i_type, int i_mode )
1272 vlc_object_t *p_tmp;
1274 switch( i_mode & 0x000f )
1277 p_tmp = p_this->p_parent;
1280 if( p_tmp->i_object_type == i_type )
1282 p_tmp->p_internals->i_refcount++;
1287 return FindObject( p_tmp, i_type, i_mode );
1293 for( i = p_this->i_children; i--; )
1295 p_tmp = p_this->pp_children[i];
1296 if( p_tmp->i_object_type == i_type )
1298 p_tmp->p_internals->i_refcount++;
1301 else if( p_tmp->i_children )
1303 p_tmp = FindObject( p_tmp, i_type, i_mode );
1313 /* Handled in vlc_object_find */
1320 static vlc_object_t * FindObjectName( vlc_object_t *p_this,
1321 const char *psz_name,
1325 vlc_object_t *p_tmp;
1327 switch( i_mode & 0x000f )
1330 p_tmp = p_this->p_parent;
1333 if( p_tmp->psz_object_name
1334 && !strcmp( p_tmp->psz_object_name, psz_name ) )
1336 p_tmp->p_internals->i_refcount++;
1341 return FindObjectName( p_tmp, psz_name, i_mode );
1347 for( i = p_this->i_children; i--; )
1349 p_tmp = p_this->pp_children[i];
1350 if( p_tmp->psz_object_name
1351 && !strcmp( p_tmp->psz_object_name, psz_name ) )
1353 p_tmp->p_internals->i_refcount++;
1356 else if( p_tmp->i_children )
1358 p_tmp = FindObjectName( p_tmp, psz_name, i_mode );
1368 /* Handled in vlc_object_find */
1375 static void DetachObject( vlc_object_t *p_this )
1377 vlc_object_t *p_parent = p_this->p_parent;
1380 /* Remove p_this's parent */
1381 p_this->p_parent = NULL;
1383 /* Remove all of p_parent's children which are p_this */
1384 for( i_index = p_parent->i_children ; i_index-- ; )
1386 if( p_parent->pp_children[i_index] == p_this )
1388 p_parent->i_children--;
1389 for( i = i_index ; i < p_parent->i_children ; i++ )
1391 p_parent->pp_children[i] = p_parent->pp_children[i+1];
1396 if( p_parent->i_children )
1398 p_parent->pp_children = (vlc_object_t **)realloc( p_parent->pp_children,
1399 p_parent->i_children * sizeof(vlc_object_t *) );
1403 free( p_parent->pp_children );
1404 p_parent->pp_children = NULL;
1408 /*****************************************************************************
1409 * SetAttachment: recursively set the b_attached flag of a subtree.
1410 *****************************************************************************
1411 * This function is used by the attach and detach functions to propagate
1412 * the b_attached flag in a subtree.
1413 *****************************************************************************/
1414 static void SetAttachment( vlc_object_t *p_this, vlc_bool_t b_attached )
1418 for( i_index = p_this->i_children ; i_index-- ; )
1420 SetAttachment( p_this->pp_children[i_index], b_attached );
1423 p_this->p_internals->b_attached = b_attached;
1426 static void PrintObject( vlc_object_t *p_this, const char *psz_prefix )
1428 char psz_children[20], psz_refcount[20], psz_thread[30], psz_name[50],
1432 if( p_this->psz_object_name )
1434 snprintf( psz_name, 49, " \"%s\"", p_this->psz_object_name );
1436 psz_name[48] = '\"';
1439 psz_children[0] = '\0';
1440 switch( p_this->i_children )
1445 strcpy( psz_children, ", 1 child" );
1448 snprintf( psz_children, 19, ", %i children", p_this->i_children );
1452 psz_refcount[0] = '\0';
1453 if( p_this->p_internals->i_refcount > 0 )
1454 snprintf( psz_refcount, 19, ", refcount %u",
1455 p_this->p_internals->i_refcount );
1457 psz_thread[0] = '\0';
1458 if( p_this->p_internals->b_thread )
1459 snprintf( psz_thread, 29, " (thread %u)",
1460 #if defined(WIN32) || defined(UNDER_CE)
1461 (unsigned)p_this->p_internals->thread_id.id );
1463 (unsigned)p_this->p_internals->thread_id );
1466 psz_parent[0] = '\0';
1467 if( p_this->p_parent )
1468 snprintf( psz_parent, 19, ", parent %i", p_this->p_parent->i_object_id );
1470 printf( " %so %.8i %s%s%s%s%s%s\n", psz_prefix,
1471 p_this->i_object_id, p_this->psz_object_type,
1472 psz_name, psz_thread, psz_refcount, psz_children,
1476 static void DumpStructure( vlc_object_t *p_this, int i_level, char *psz_foo )
1479 char i_back = psz_foo[i_level];
1480 psz_foo[i_level] = '\0';
1482 PrintObject( p_this, psz_foo );
1484 psz_foo[i_level] = i_back;
1486 if( i_level / 2 >= MAX_DUMPSTRUCTURE_DEPTH )
1488 msg_Warn( p_this, "structure tree is too deep" );
1492 for( i = 0 ; i < p_this->i_children ; i++ )
1496 psz_foo[i_level-1] = ' ';
1498 if( psz_foo[i_level-2] == '`' )
1500 psz_foo[i_level-2] = ' ';
1504 if( i == p_this->i_children - 1 )
1506 psz_foo[i_level] = '`';
1510 psz_foo[i_level] = '|';
1513 psz_foo[i_level+1] = '-';
1514 psz_foo[i_level+2] = '\0';
1516 DumpStructure( p_this->pp_children[i], i_level + 2, psz_foo );
1520 static vlc_list_t * NewList( int i_count )
1522 vlc_list_t * p_list = (vlc_list_t *)malloc( sizeof( vlc_list_t ) );
1523 if( p_list == NULL )
1528 p_list->i_count = i_count;
1532 p_list->p_values = NULL;
1536 p_list->p_values = malloc( i_count * sizeof( vlc_value_t ) );
1537 if( p_list->p_values == NULL )
1539 p_list->i_count = 0;
1546 static void ListReplace( vlc_list_t *p_list, vlc_object_t *p_object,
1549 if( p_list == NULL || i_index >= p_list->i_count )
1554 p_object->p_internals->i_refcount++;
1556 p_list->p_values[i_index].p_object = p_object;
1561 /*static void ListAppend( vlc_list_t *p_list, vlc_object_t *p_object )
1563 if( p_list == NULL )
1568 p_list->p_values = realloc( p_list->p_values, (p_list->i_count + 1)
1569 * sizeof( vlc_value_t ) );
1570 if( p_list->p_values == NULL )
1572 p_list->i_count = 0;
1576 p_object->p_internals->i_refcount++;
1578 p_list->p_values[p_list->i_count].p_object = p_object;
1584 static int CountChildren( vlc_object_t *p_this, int i_type )
1586 vlc_object_t *p_tmp;
1589 for( i = 0; i < p_this->i_children; i++ )
1591 p_tmp = p_this->pp_children[i];
1593 if( p_tmp->i_object_type == i_type )
1598 if( p_tmp->i_children )
1600 i_count += CountChildren( p_tmp, i_type );
1607 static void ListChildren( vlc_list_t *p_list, vlc_object_t *p_this, int i_type )
1609 vlc_object_t *p_tmp;
1612 for( i = 0; i < p_this->i_children; i++ )
1614 p_tmp = p_this->pp_children[i];
1616 if( p_tmp->i_object_type == i_type )
1618 ListReplace( p_list, p_tmp, p_list->i_count++ );
1621 if( p_tmp->i_children )
1623 ListChildren( p_list, p_tmp, i_type );