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 *****************************************************************************/
35 #include "../libvlc.h"
38 #include "audio_output/aout_internal.h"
40 #include <vlc_access.h>
41 #include <vlc_demux.h>
42 #include <vlc_stream.h>
45 #include "stream_output/stream_output.h"
47 #include "vlc_playlist.h"
48 #include "vlc_interface.h"
49 #include "vlc_codec.h"
50 #include "vlc_filter.h"
52 #include "vlc_httpd.h"
54 #include "input/vlm_internal.h"
61 #include "variables.h"
67 # include <errno.h> /* ENOSYS */
70 /*****************************************************************************
72 *****************************************************************************/
73 static int DumpCommand( vlc_object_t *, char const *,
74 vlc_value_t, vlc_value_t, void * );
76 static vlc_object_t * FindObject ( vlc_object_t *, int, int );
77 static vlc_object_t * FindObjectName( vlc_object_t *, const char *, int );
78 static void DetachObject ( vlc_object_t * );
79 static void PrintObject ( vlc_object_t *, const char * );
80 static void DumpStructure ( vlc_object_t *, int, char * );
81 static int FindIndex ( vlc_object_t *, vlc_object_t **, int );
82 static void SetAttachment ( vlc_object_t *, vlc_bool_t );
84 static vlc_list_t * NewList ( int );
85 static void ListReplace ( vlc_list_t *, vlc_object_t *, int );
86 /*static void ListAppend ( vlc_list_t *, vlc_object_t * );*/
87 static int CountChildren ( vlc_object_t *, int );
88 static void ListChildren ( vlc_list_t *, vlc_object_t *, int );
90 /*****************************************************************************
91 * Local structure lock
92 *****************************************************************************/
93 static vlc_mutex_t structure_lock;
94 static vlc_object_internals_t global_internals;
96 vlc_object_t *vlc_custom_create( vlc_object_t *p_this, size_t i_size,
97 int i_type, const char *psz_type )
100 vlc_object_internals_t *p_priv;
102 if( i_type == VLC_OBJECT_GLOBAL )
105 p_priv = &global_internals;
106 memset( p_priv, 0, sizeof( *p_priv ) );
110 p_priv = calloc( 1, sizeof( *p_priv ) + i_size );
114 p_new = (vlc_object_t *)(p_priv + 1);
117 p_new->p_internals = p_priv;
118 p_new->i_object_type = i_type;
119 p_new->psz_object_type = psz_type;
121 p_new->psz_object_name = NULL;
123 p_new->b_die = VLC_FALSE;
124 p_new->b_error = VLC_FALSE;
125 p_new->b_dead = VLC_FALSE;
126 p_priv->b_attached = VLC_FALSE;
127 p_new->b_force = VLC_FALSE;
129 p_new->psz_header = NULL;
131 if( p_this->i_flags & OBJECT_FLAGS_NODBG )
132 p_new->i_flags |= OBJECT_FLAGS_NODBG;
133 if( p_this->i_flags & OBJECT_FLAGS_QUIET )
134 p_new->i_flags |= OBJECT_FLAGS_QUIET;
135 if( p_this->i_flags & OBJECT_FLAGS_NOINTERACT )
136 p_new->i_flags |= OBJECT_FLAGS_NOINTERACT;
138 p_priv->p_vars = calloc( sizeof( variable_t ), 16 );
140 if( !p_priv->p_vars )
142 if( i_type != VLC_OBJECT_GLOBAL )
147 if( i_type == VLC_OBJECT_GLOBAL )
149 /* If i_type is global, then p_new is actually p_libvlc_global */
150 libvlc_global_data_t *p_libvlc_global = (libvlc_global_data_t *)p_new;
151 p_new->p_libvlc = NULL;
153 p_libvlc_global->i_counter = 0;
154 p_new->i_object_id = 0;
156 p_libvlc_global->i_objects = 1;
157 p_libvlc_global->pp_objects = malloc( sizeof(vlc_object_t *) );
158 p_libvlc_global->pp_objects[0] = p_new;
159 p_priv->b_attached = VLC_TRUE;
163 libvlc_global_data_t *p_libvlc_global = vlc_global();
164 if( i_type == VLC_OBJECT_LIBVLC )
166 p_new->p_libvlc = (libvlc_int_t*)p_new;
167 p_priv->b_attached = VLC_TRUE;
171 p_new->p_libvlc = p_this->p_libvlc;
174 vlc_mutex_lock( &structure_lock );
176 p_libvlc_global->i_counter++;
177 p_new->i_object_id = p_libvlc_global->i_counter;
179 /* Wooohaa! If *this* fails, we're in serious trouble! Anyway it's
180 * useless to try and recover anything if pp_objects gets smashed. */
181 TAB_APPEND( p_libvlc_global->i_objects, p_libvlc_global->pp_objects,
184 vlc_mutex_unlock( &structure_lock );
187 p_priv->i_refcount = 0;
188 p_new->p_parent = NULL;
189 p_new->pp_children = NULL;
190 p_new->i_children = 0;
192 p_new->p_private = NULL;
194 /* Initialize mutexes and condvars */
195 vlc_mutex_init( p_new, &p_new->object_lock );
196 vlc_cond_init( p_new, &p_new->object_wait );
197 vlc_mutex_init( p_new, &p_priv->var_lock );
198 p_priv->pipes[0] = p_priv->pipes[1] = -1;
200 if( i_type == VLC_OBJECT_GLOBAL )
202 vlc_mutex_init( p_new, &structure_lock );
205 if( i_type == VLC_OBJECT_LIBVLC )
207 var_Create( p_new, "list", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
208 var_AddCallback( p_new, "list", DumpCommand, NULL );
209 var_Create( p_new, "tree", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
210 var_AddCallback( p_new, "tree", DumpCommand, NULL );
211 var_Create( p_new, "vars", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
212 var_AddCallback( p_new, "vars", DumpCommand, NULL );
220 * Allocates and initializes a vlc object.
222 * @param i_type known object type (all of them are negative integer values),
223 * or object byte size (always positive).
225 * @return the new object, or NULL on error.
227 void * __vlc_object_create( vlc_object_t *p_this, int i_type )
229 const char * psz_type;
234 case VLC_OBJECT_GLOBAL:
235 i_size = sizeof(libvlc_global_data_t);
238 case VLC_OBJECT_LIBVLC:
239 i_size = sizeof(libvlc_int_t);
242 case VLC_OBJECT_INTF:
243 i_size = sizeof(intf_thread_t);
244 psz_type = "interface";
246 case VLC_OBJECT_DIALOGS:
247 i_size = sizeof(intf_thread_t);
248 psz_type = "dialogs";
250 case VLC_OBJECT_PLAYLIST:
251 i_size = sizeof(playlist_t);
252 psz_type = "playlist";
255 i_size = sizeof(services_discovery_t);
256 psz_type = "services discovery";
258 case VLC_OBJECT_INPUT:
259 i_size = sizeof(input_thread_t);
262 case VLC_OBJECT_DEMUX:
263 i_size = sizeof(demux_t);
266 case VLC_OBJECT_ACCESS:
267 i_size = sizeof(access_t);
270 case VLC_OBJECT_DECODER:
271 i_size = sizeof(decoder_t);
272 psz_type = "decoder";
274 case VLC_OBJECT_PACKETIZER:
275 i_size = sizeof(decoder_t);
276 psz_type = "packetizer";
278 case VLC_OBJECT_ENCODER:
279 i_size = sizeof(encoder_t);
280 psz_type = "encoder";
282 case VLC_OBJECT_FILTER:
283 i_size = sizeof(filter_t);
286 case VLC_OBJECT_VOUT:
287 i_size = sizeof(vout_thread_t);
288 psz_type = "video output";
291 i_size = sizeof(spu_t);
292 psz_type = "subpicture";
294 case VLC_OBJECT_AOUT:
295 i_size = sizeof(aout_instance_t);
296 psz_type = "audio output";
298 case VLC_OBJECT_SOUT:
299 i_size = sizeof(sout_instance_t);
300 psz_type = "stream output";
303 i_size = sizeof( vlm_t );
304 psz_type = "vlm dameon";
307 i_size = sizeof( vod_t );
308 psz_type = "vod server";
311 i_size = sizeof( xml_t );
314 case VLC_OBJECT_OPENGL:
315 i_size = sizeof( vout_thread_t );
318 case VLC_OBJECT_ANNOUNCE:
319 i_size = sizeof( announce_handler_t );
320 psz_type = "announce";
322 case VLC_OBJECT_META_ENGINE:
323 i_size = sizeof( meta_engine_t );
324 psz_type = "meta engine";
326 case VLC_OBJECT_OSDMENU:
327 i_size = sizeof( osd_menu_t );
328 psz_type = "osd menu";
331 i_size = i_type > (int)sizeof(vlc_object_t)
332 ? i_type : (int)sizeof(vlc_object_t);
333 i_type = VLC_OBJECT_GENERIC;
334 psz_type = "generic";
338 return vlc_custom_create( p_this, i_size, i_type, psz_type );
343 ****************************************************************************
344 * Destroy a vlc object
346 * This function destroys an object that has been previously allocated with
347 * vlc_object_create. The object's refcount must be zero and it must not be
348 * attached to other objects in any way.
349 *****************************************************************************/
350 void __vlc_object_destroy( vlc_object_t *p_this )
352 vlc_object_internals_t *p_priv = vlc_internals( p_this );
355 if( p_this->i_children )
357 msg_Err( p_this, "cannot delete object (%i, %s) with children" ,
358 p_this->i_object_id, p_this->psz_object_name );
362 if( p_this->p_parent )
364 msg_Err( p_this, "cannot delete object (%i, %s) with a parent",
365 p_this->i_object_id, p_this->psz_object_name );
369 while( p_priv->i_refcount > 0 )
373 /* Don't warn immediately ... 100ms seems OK */
377 "refcount is %u, delaying before deletion (id=%d,type=%d)",
378 p_priv->i_refcount, p_this->i_object_id,
379 p_this->i_object_type );
381 else if( i_delay == 10 )
384 "refcount is %u, delaying again (id=%d,type=%d)",
385 p_priv->i_refcount, p_this->i_object_id,
386 p_this->i_object_type );
388 else if( i_delay == 20 )
391 "waited too long, cancelling destruction (id=%d,type=%d)",
392 p_this->i_object_id, p_this->i_object_type );
399 /* Destroy the associated variables, starting from the end so that
400 * no memmove calls have to be done. */
401 while( p_priv->i_vars )
403 var_Destroy( p_this, p_priv->p_vars[p_priv->i_vars - 1].psz_name );
406 free( p_priv->p_vars );
407 vlc_mutex_destroy( &p_priv->var_lock );
409 if( p_this->psz_header ) free( p_this->psz_header );
411 if( p_this->i_object_type == VLC_OBJECT_GLOBAL )
413 libvlc_global_data_t *p_global = (libvlc_global_data_t *)p_this;
414 /* We are the global object ... no need to lock. */
415 free( p_global->pp_objects );
416 p_global->pp_objects = NULL;
417 p_global->i_objects--;
419 vlc_mutex_destroy( &structure_lock );
423 libvlc_global_data_t *p_libvlc_global = vlc_global();
426 vlc_mutex_lock( &structure_lock );
428 /* Wooohaa! If *this* fails, we're in serious trouble! Anyway it's
429 * useless to try and recover anything if pp_objects gets smashed. */
430 i_index = FindIndex( p_this, p_libvlc_global->pp_objects,
431 p_libvlc_global->i_objects );
432 REMOVE_ELEM( p_libvlc_global->pp_objects,
433 p_libvlc_global->i_objects, i_index );
435 vlc_mutex_unlock( &structure_lock );
438 #if defined(WIN32) || defined(UNDER_CE)
439 /* if object has an associated thread, close it now */
440 if( p_priv->thread_id.hThread )
441 CloseHandle(p_priv->thread_id.hThread);
444 vlc_mutex_destroy( &p_this->object_lock );
445 vlc_cond_destroy( &p_this->object_wait );
446 if( p_priv->pipes[0] != -1 )
447 close( p_priv->pipes[0] );
448 if( p_priv->pipes[1] != -1 )
449 close( p_priv->pipes[1] );
451 /* global is not dynamically allocated by vlc_object_create */
452 if( p_this->i_object_type != VLC_OBJECT_GLOBAL )
457 /** Inter-object signaling */
459 void __vlc_object_lock( vlc_object_t *obj )
461 vlc_mutex_lock( &obj->object_lock );
464 void __vlc_object_unlock( vlc_object_t *obj )
466 vlc_assert_locked( &obj->object_lock );
467 vlc_mutex_unlock( &obj->object_lock );
471 # include <winsock2.h>
472 # include <ws2tcpip.h>
475 * select()-able pipes emulated using Winsock
477 static int pipe (int fd[2])
480 int addrlen = sizeof (addr);
482 SOCKET l = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP), a,
483 c = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
484 if ((l == INVALID_SOCKET) || (c == INVALID_SOCKET))
487 memset (&addr, 0, sizeof (addr));
488 addr.sin_family = AF_INET;
489 addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
490 if (bind (l, (PSOCKADDR)&addr, sizeof (addr))
491 || getsockname (l, (PSOCKADDR)&addr, &addrlen)
493 || connect (c, (PSOCKADDR)&addr, addrlen))
496 a = accept (l, NULL, NULL);
497 if (a == INVALID_SOCKET)
508 if (l != INVALID_SOCKET)
510 if (c != INVALID_SOCKET)
516 #define read( a, b, c ) recv (a, b, c, 0)
518 #define write( a, b, c ) send (a, b, c, 0)
520 #define close( a ) closesocket (a)
524 * Returns the readable end of a pipe that becomes readable whenever
525 * an object is signaled. This can be used to wait for VLC object events
526 * inside select(), poll() loops or frameworks providing an event loop.
528 * Note that the pipe will remain the same for the lifetime of the object.
529 * DO NOT close it yourself. Ever.
531 * DO NOT try to read from the pipe either: call vlc_object_wait() instead.
532 * Assuming the pipe is readable, vlc_object_wait() will not block.
533 * Also note that, as with vlc_object_wait(), there may be spurious wakeups.
535 * @param obj object that would be signaled (object lock MUST hold)
536 * @return a readable pipe descriptor, or -1 on error.
538 int __vlc_object_waitpipe( vlc_object_t *obj )
540 int *pipes = obj->p_internals->pipes;
541 vlc_assert_locked( &obj->object_lock );
545 /* This can only ever happen if someone killed us without locking */
546 assert( pipes[0] == -1 );
557 * Waits for the object to be signaled (using vlc_object_signal()).
558 * If the object already has a signal pending, this function will return
559 * immediately. It is asserted that the caller holds the object lock.
561 * @return true if the object is dying and should terminate.
563 vlc_bool_t __vlc_object_wait( vlc_object_t *obj )
565 vlc_assert_locked( &obj->object_lock );
567 int fd = obj->p_internals->pipes[0];
570 while (read (fd, &(char){ 0 }, 1 < 0));
574 vlc_cond_wait( &obj->object_wait, &obj->object_lock );
580 * Waits for the object to be signaled (using vlc_object_signal()), or for
582 * If the object already has a signal pending, this function will return
583 * immediately. It is asserted that the caller holds the object lock.
585 * @return negative if the object is dying and should terminate,
586 * positive if the the object has been signaled but is not dying,
587 * 0 if timeout has been reached.
589 int __vlc_object_timedwait( vlc_object_t *obj, mtime_t deadline )
593 vlc_assert_locked( &obj->object_lock );
594 v = vlc_cond_timedwait( &obj->object_wait, &obj->object_lock, deadline );
595 if( v == 0 ) /* signaled */
596 return obj->b_die ? -1 : 1;
602 * Checks whether an object has been "killed".
603 * The object lock must be held.
605 * Typical code for an object thread could be:
607 vlc_object_lock (self);
609 while (vlc_object_alive (self))
613 if (vlc_object_wait (self))
618 ...deinitialization...
619 vlc_object_unlock (self);
622 * @return true iff the object has not been killed yet
624 vlc_bool_t __vlc_object_alive( vlc_object_t *obj )
626 vlc_assert_locked( &obj->object_lock );
632 * Signals an object for which the lock is held.
634 void __vlc_object_signal_unlocked( vlc_object_t *obj )
636 vlc_assert_locked( &obj->object_lock );
638 int fd = obj->p_internals->pipes[1];
640 while( write( fd, &(char){ 0 }, 1 ) < 0 );
642 vlc_cond_signal( &obj->object_wait );
647 * Requests termination of an object.
648 * If the object is LibVLC, also request to terminate all its children.
650 void __vlc_object_kill( vlc_object_t *p_this )
652 vlc_mutex_lock( &p_this->object_lock );
653 p_this->b_die = VLC_TRUE;
655 if( p_this->i_object_type == VLC_OBJECT_LIBVLC )
656 for( int i = 0; i < p_this->i_children ; i++ )
657 vlc_object_kill( p_this->pp_children[i] );
659 vlc_object_signal_unlocked( p_this );
660 vlc_mutex_unlock( &p_this->object_lock );
665 * find an object given its ID
667 * This function looks for the object whose i_object_id field is i_id. We
668 * use a dichotomy so that lookups are in log2(n).
669 *****************************************************************************/
670 void * __vlc_object_get( vlc_object_t *p_this, int i_id )
673 vlc_object_t **pp_objects;
674 libvlc_global_data_t *p_libvlc_global = vlc_global();
676 vlc_mutex_lock( &structure_lock );
678 pp_objects = p_libvlc_global->pp_objects;
680 /* Perform our dichotomy */
681 for( i_max = p_libvlc_global->i_objects - 1 ; ; )
683 i_middle = i_max / 2;
685 if( pp_objects[i_middle]->i_object_id > i_id )
689 else if( pp_objects[i_middle]->i_object_id < i_id )
693 pp_objects += i_middle;
698 /* This happens when there are only two remaining objects */
699 if( pp_objects[i_middle+1]->i_object_id == i_id )
701 vlc_mutex_unlock( &structure_lock );
702 pp_objects[i_middle+1]->p_internals->i_refcount++;
703 return pp_objects[i_middle+1];
710 vlc_mutex_unlock( &structure_lock );
711 pp_objects[i_middle]->p_internals->i_refcount++;
712 return pp_objects[i_middle];
717 /* this means that i_max == i_middle, and since we have already
718 * tested pp_objects[i_middle]), p_found is properly set. */
723 vlc_mutex_unlock( &structure_lock );
728 ****************************************************************************
729 * find a typed object and increment its refcount
730 *****************************************************************************
731 * This function recursively looks for a given object type. i_mode can be one
732 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
733 *****************************************************************************/
734 void * __vlc_object_find( vlc_object_t *p_this, int i_type, int i_mode )
736 vlc_object_t *p_found;
738 vlc_mutex_lock( &structure_lock );
740 /* If we are of the requested type ourselves, don't look further */
741 if( !(i_mode & FIND_STRICT) && p_this->i_object_type == i_type )
743 p_this->p_internals->i_refcount++;
744 vlc_mutex_unlock( &structure_lock );
748 /* Otherwise, recursively look for the object */
749 if( (i_mode & 0x000f) == FIND_ANYWHERE )
751 vlc_object_t *p_root = p_this;
754 while( p_root->p_parent != NULL &&
755 p_root != VLC_OBJECT( p_this->p_libvlc ) )
757 p_root = p_root->p_parent;
760 p_found = FindObject( p_root, i_type, (i_mode & ~0x000f)|FIND_CHILD );
761 if( p_found == NULL && p_root != VLC_OBJECT( p_this->p_libvlc ) )
763 p_found = FindObject( VLC_OBJECT( p_this->p_libvlc ),
764 i_type, (i_mode & ~0x000f)|FIND_CHILD );
769 p_found = FindObject( p_this, i_type, i_mode );
772 vlc_mutex_unlock( &structure_lock );
778 ****************************************************************************
779 * find a named object and increment its refcount
780 *****************************************************************************
781 * This function recursively looks for a given object name. i_mode can be one
782 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
783 *****************************************************************************/
784 void * __vlc_object_find_name( vlc_object_t *p_this, const char *psz_name,
787 vlc_object_t *p_found;
789 vlc_mutex_lock( &structure_lock );
791 /* If have the requested name ourselves, don't look further */
792 if( !(i_mode & FIND_STRICT)
793 && p_this->psz_object_name
794 && !strcmp( p_this->psz_object_name, psz_name ) )
796 p_this->p_internals->i_refcount++;
797 vlc_mutex_unlock( &structure_lock );
801 /* Otherwise, recursively look for the object */
802 if( (i_mode & 0x000f) == FIND_ANYWHERE )
804 vlc_object_t *p_root = p_this;
807 while( p_root->p_parent != NULL &&
808 p_root != VLC_OBJECT( p_this->p_libvlc ) )
810 p_root = p_root->p_parent;
813 p_found = FindObjectName( p_root, psz_name,
814 (i_mode & ~0x000f)|FIND_CHILD );
815 if( p_found == NULL && p_root != VLC_OBJECT( p_this->p_libvlc ) )
817 p_found = FindObjectName( VLC_OBJECT( p_this->p_libvlc ),
818 psz_name, (i_mode & ~0x000f)|FIND_CHILD );
823 p_found = FindObjectName( p_this, psz_name, i_mode );
826 vlc_mutex_unlock( &structure_lock );
832 ****************************************************************************
833 * increment an object refcount
834 *****************************************************************************/
835 void __vlc_object_yield( vlc_object_t *p_this )
837 vlc_mutex_lock( &structure_lock );
838 p_this->p_internals->i_refcount++;
839 vlc_mutex_unlock( &structure_lock );
842 static inline void Release( vlc_object_t *obj )
844 assert( obj->p_internals->i_refcount > 0 );
845 obj->p_internals->i_refcount--;
848 /*****************************************************************************
849 * decrement an object refcount
850 *****************************************************************************/
851 void __vlc_object_release( vlc_object_t *p_this )
853 vlc_mutex_lock( &structure_lock );
855 vlc_mutex_unlock( &structure_lock );
859 ****************************************************************************
860 * attach object to a parent object
861 *****************************************************************************
862 * This function sets p_this as a child of p_parent, and p_parent as a parent
863 * of p_this. This link can be undone using vlc_object_detach.
864 *****************************************************************************/
865 void __vlc_object_attach( vlc_object_t *p_this, vlc_object_t *p_parent )
867 if( !p_this ) return;
869 vlc_mutex_lock( &structure_lock );
871 /* Attach the parent to its child */
872 p_this->p_parent = p_parent;
874 /* Attach the child to its parent */
875 INSERT_ELEM( p_parent->pp_children, p_parent->i_children,
876 p_parent->i_children, p_this );
878 /* Climb up the tree to see whether we are connected with the root */
879 if( p_parent->p_internals->b_attached )
881 SetAttachment( p_this, VLC_TRUE );
884 vlc_mutex_unlock( &structure_lock );
888 ****************************************************************************
889 * detach object from its parent
890 *****************************************************************************
891 * This function removes all links between an object and its parent.
892 *****************************************************************************/
893 void __vlc_object_detach( vlc_object_t *p_this )
895 if( !p_this ) return;
897 vlc_mutex_lock( &structure_lock );
898 if( !p_this->p_parent )
900 msg_Err( p_this, "object is not attached" );
901 vlc_mutex_unlock( &structure_lock );
905 /* Climb up the tree to see whether we are connected with the root */
906 if( p_this->p_parent->p_internals->b_attached )
908 SetAttachment( p_this, VLC_FALSE );
911 DetachObject( p_this );
912 vlc_mutex_unlock( &structure_lock );
917 ****************************************************************************
918 * find a list typed objects and increment their refcount
919 *****************************************************************************
920 * This function recursively looks for a given object type. i_mode can be one
921 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
922 *****************************************************************************/
923 vlc_list_t * __vlc_list_find( vlc_object_t *p_this, int i_type, int i_mode )
926 vlc_object_t **pp_current, **pp_end;
927 int i_count = 0, i_index = 0;
928 libvlc_global_data_t *p_libvlc_global = vlc_global();
930 vlc_mutex_lock( &structure_lock );
932 /* Look for the objects */
933 switch( i_mode & 0x000f )
936 pp_current = p_libvlc_global->pp_objects;
937 pp_end = pp_current + p_libvlc_global->i_objects;
939 for( ; pp_current < pp_end ; pp_current++ )
941 if( (*pp_current)->p_internals->b_attached
942 && (*pp_current)->i_object_type == i_type )
948 p_list = NewList( i_count );
949 pp_current = p_libvlc_global->pp_objects;
951 for( ; pp_current < pp_end ; pp_current++ )
953 if( (*pp_current)->p_internals->b_attached
954 && (*pp_current)->i_object_type == i_type )
956 ListReplace( p_list, *pp_current, i_index );
957 if( i_index < i_count ) i_index++;
963 i_count = CountChildren( p_this, i_type );
964 p_list = NewList( i_count );
966 /* Check allocation was successful */
967 if( p_list->i_count != i_count )
969 msg_Err( p_this, "list allocation failed!" );
975 ListChildren( p_list, p_this, i_type );
979 msg_Err( p_this, "unimplemented!" );
980 p_list = NewList( 0 );
984 vlc_mutex_unlock( &structure_lock );
989 /*****************************************************************************
990 * DumpCommand: print the current vlc structure
991 *****************************************************************************
992 * This function prints either an ASCII tree showing the connections between
993 * vlc objects, and additional information such as their refcount, thread ID,
994 * etc. (command "tree"), or the same data as a simple list (command "list").
995 *****************************************************************************/
996 static int DumpCommand( vlc_object_t *p_this, char const *psz_cmd,
997 vlc_value_t oldval, vlc_value_t newval, void *p_data )
999 libvlc_global_data_t *p_libvlc_global = vlc_global();
1001 (void)oldval; (void)p_data;
1002 if( *psz_cmd == 'l' )
1004 vlc_mutex_lock( &structure_lock );
1006 vlc_object_t **pp_current, **pp_end;
1008 pp_current = p_libvlc_global->pp_objects;
1009 pp_end = pp_current + p_libvlc_global->i_objects;
1011 for( ; pp_current < pp_end ; pp_current++ )
1013 if( (*pp_current)->p_internals->b_attached )
1015 PrintObject( *pp_current, "" );
1019 printf( " o %.8i %s (not attached)\n",
1020 (*pp_current)->i_object_id,
1021 (*pp_current)->psz_object_type );
1025 vlc_mutex_unlock( &structure_lock );
1029 vlc_object_t *p_object = NULL;
1031 if( *newval.psz_string )
1034 int i_id = strtol( newval.psz_string, &end, 0 );
1035 if( end != newval.psz_string )
1036 p_object = vlc_object_get( p_this, i_id );
1039 /* try using the object's name to find it */
1040 vlc_object_t *p_libvlc = vlc_object_get( p_this, 1 );
1043 /* Look in p_libvlc's children tree */
1044 p_object = vlc_object_find_name( p_libvlc,
1047 vlc_object_release( p_libvlc );
1051 /* If it's not in libvlc, look in libvlc_global (== p_this) */
1052 p_object = vlc_object_find_name( p_this,
1064 vlc_mutex_lock( &structure_lock );
1066 if( *psz_cmd == 't' )
1068 char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
1071 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
1074 DumpStructure( p_object, 0, psz_foo );
1076 else if( *psz_cmd == 'v' )
1081 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
1083 PrintObject( p_object, "" );
1085 if( !p_object->p_internals->i_vars )
1086 printf( " `-o No variables\n" );
1087 for( i = 0; i < p_object->p_internals->i_vars; i++ )
1089 variable_t *p_var = p_object->p_internals->p_vars + i;
1091 const char *psz_type = "unknown";
1092 switch( p_var->i_type & VLC_VAR_TYPE )
1094 #define MYCASE( type, nice ) \
1095 case VLC_VAR_ ## type: \
1098 MYCASE( VOID, "void" );
1099 MYCASE( BOOL, "bool" );
1100 MYCASE( INTEGER, "integer" );
1101 MYCASE( HOTKEY, "hotkey" );
1102 MYCASE( STRING, "string" );
1103 MYCASE( MODULE, "module" );
1104 MYCASE( FILE, "file" );
1105 MYCASE( DIRECTORY, "directory" );
1106 MYCASE( VARIABLE, "variable" );
1107 MYCASE( FLOAT, "float" );
1108 MYCASE( TIME, "time" );
1109 MYCASE( ADDRESS, "address" );
1110 MYCASE( MUTEX, "mutex" );
1111 MYCASE( LIST, "list" );
1114 printf( " %c-o \"%s\" (%s",
1115 i + 1 == p_object->p_internals->i_vars ? '`' : '|',
1116 p_var->psz_name, psz_type );
1117 if( p_var->psz_text )
1118 printf( ", %s", p_var->psz_text );
1120 if( p_var->i_type & VLC_VAR_ISCOMMAND )
1121 printf( ", command" );
1122 if( p_var->i_entries )
1123 printf( ", %d callbacks", p_var->i_entries );
1124 switch( p_var->i_type & 0x00f0 )
1130 printf( ": %s", p_var->val.b_bool ? "true" : "false" );
1132 case VLC_VAR_INTEGER:
1133 printf( ": %d", p_var->val.i_int );
1135 case VLC_VAR_STRING:
1136 printf( ": \"%s\"", p_var->val.psz_string );
1139 printf( ": %f", p_var->val.f_float );
1142 printf( ": " I64Fi, (int64_t)p_var->val.i_time );
1144 case VLC_VAR_ADDRESS:
1145 printf( ": %p", p_var->val.p_address );
1155 vlc_mutex_unlock( &structure_lock );
1157 if( *newval.psz_string )
1159 vlc_object_release( p_object );
1166 /*****************************************************************************
1167 * vlc_list_release: free a list previously allocated by vlc_list_find
1168 *****************************************************************************
1169 * This function decreases the refcount of all objects in the list and
1171 *****************************************************************************/
1172 void vlc_list_release( vlc_list_t *p_list )
1176 vlc_mutex_lock( &structure_lock );
1177 for( i_index = 0; i_index < p_list->i_count; i_index++ )
1179 Release( p_list->p_values[i_index].p_object );
1181 vlc_mutex_unlock( &structure_lock );
1183 free( p_list->p_values );
1187 /* Following functions are local */
1189 /*****************************************************************************
1190 * FindIndex: find the index of an object in an array of objects
1191 *****************************************************************************
1192 * This function assumes that p_this can be found in pp_objects. It will not
1193 * crash if p_this cannot be found, but will return a wrong value. It is your
1194 * duty to check the return value if you are not certain that the object could
1195 * be found for sure.
1196 *****************************************************************************/
1197 static int FindIndex( vlc_object_t *p_this,
1198 vlc_object_t **pp_objects, int i_count )
1200 int i_middle = i_count / 2;
1207 if( pp_objects[i_middle] == p_this )
1217 /* We take advantage of the sorted array */
1218 if( pp_objects[i_middle]->i_object_id < p_this->i_object_id )
1220 return i_middle + FindIndex( p_this, pp_objects + i_middle,
1221 i_count - i_middle );
1225 return FindIndex( p_this, pp_objects, i_middle );
1229 static vlc_object_t * FindObject( vlc_object_t *p_this, int i_type, int i_mode )
1232 vlc_object_t *p_tmp;
1234 switch( i_mode & 0x000f )
1237 p_tmp = p_this->p_parent;
1240 if( p_tmp->i_object_type == i_type )
1242 p_tmp->p_internals->i_refcount++;
1247 return FindObject( p_tmp, i_type, i_mode );
1253 for( i = p_this->i_children; i--; )
1255 p_tmp = p_this->pp_children[i];
1256 if( p_tmp->i_object_type == i_type )
1258 p_tmp->p_internals->i_refcount++;
1261 else if( p_tmp->i_children )
1263 p_tmp = FindObject( p_tmp, i_type, i_mode );
1273 /* Handled in vlc_object_find */
1280 static vlc_object_t * FindObjectName( vlc_object_t *p_this,
1281 const char *psz_name,
1285 vlc_object_t *p_tmp;
1287 switch( i_mode & 0x000f )
1290 p_tmp = p_this->p_parent;
1293 if( p_tmp->psz_object_name
1294 && !strcmp( p_tmp->psz_object_name, psz_name ) )
1296 p_tmp->p_internals->i_refcount++;
1301 return FindObjectName( p_tmp, psz_name, i_mode );
1307 for( i = p_this->i_children; i--; )
1309 p_tmp = p_this->pp_children[i];
1310 if( p_tmp->psz_object_name
1311 && !strcmp( p_tmp->psz_object_name, psz_name ) )
1313 p_tmp->p_internals->i_refcount++;
1316 else if( p_tmp->i_children )
1318 p_tmp = FindObjectName( p_tmp, psz_name, i_mode );
1328 /* Handled in vlc_object_find */
1335 static void DetachObject( vlc_object_t *p_this )
1337 vlc_object_t *p_parent = p_this->p_parent;
1340 /* Remove p_this's parent */
1341 p_this->p_parent = NULL;
1343 /* Remove all of p_parent's children which are p_this */
1344 for( i_index = p_parent->i_children ; i_index-- ; )
1346 if( p_parent->pp_children[i_index] == p_this )
1348 p_parent->i_children--;
1349 for( i = i_index ; i < p_parent->i_children ; i++ )
1351 p_parent->pp_children[i] = p_parent->pp_children[i+1];
1356 if( p_parent->i_children )
1358 p_parent->pp_children = (vlc_object_t **)realloc( p_parent->pp_children,
1359 p_parent->i_children * sizeof(vlc_object_t *) );
1363 free( p_parent->pp_children );
1364 p_parent->pp_children = NULL;
1368 /*****************************************************************************
1369 * SetAttachment: recursively set the b_attached flag of a subtree.
1370 *****************************************************************************
1371 * This function is used by the attach and detach functions to propagate
1372 * the b_attached flag in a subtree.
1373 *****************************************************************************/
1374 static void SetAttachment( vlc_object_t *p_this, vlc_bool_t b_attached )
1378 for( i_index = p_this->i_children ; i_index-- ; )
1380 SetAttachment( p_this->pp_children[i_index], b_attached );
1383 p_this->p_internals->b_attached = b_attached;
1386 static void PrintObject( vlc_object_t *p_this, const char *psz_prefix )
1388 char psz_children[20], psz_refcount[20], psz_thread[30], psz_name[50],
1392 if( p_this->psz_object_name )
1394 snprintf( psz_name, 49, " \"%s\"", p_this->psz_object_name );
1396 psz_name[48] = '\"';
1399 psz_children[0] = '\0';
1400 switch( p_this->i_children )
1405 strcpy( psz_children, ", 1 child" );
1408 snprintf( psz_children, 19, ", %i children", p_this->i_children );
1412 psz_refcount[0] = '\0';
1413 if( p_this->p_internals->i_refcount > 0 )
1414 snprintf( psz_refcount, 19, ", refcount %u",
1415 p_this->p_internals->i_refcount );
1417 psz_thread[0] = '\0';
1418 if( p_this->p_internals->b_thread )
1419 snprintf( psz_thread, 29, " (thread %u)",
1420 #if defined(WIN32) || defined(UNDER_CE)
1421 (unsigned)p_this->p_internals->thread_id.id );
1423 (unsigned)p_this->p_internals->thread_id );
1426 psz_parent[0] = '\0';
1427 if( p_this->p_parent )
1428 snprintf( psz_parent, 19, ", parent %i", p_this->p_parent->i_object_id );
1430 printf( " %so %.8i %s%s%s%s%s%s\n", psz_prefix,
1431 p_this->i_object_id, p_this->psz_object_type,
1432 psz_name, psz_thread, psz_refcount, psz_children,
1436 static void DumpStructure( vlc_object_t *p_this, int i_level, char *psz_foo )
1439 char i_back = psz_foo[i_level];
1440 psz_foo[i_level] = '\0';
1442 PrintObject( p_this, psz_foo );
1444 psz_foo[i_level] = i_back;
1446 if( i_level / 2 >= MAX_DUMPSTRUCTURE_DEPTH )
1448 msg_Warn( p_this, "structure tree is too deep" );
1452 for( i = 0 ; i < p_this->i_children ; i++ )
1456 psz_foo[i_level-1] = ' ';
1458 if( psz_foo[i_level-2] == '`' )
1460 psz_foo[i_level-2] = ' ';
1464 if( i == p_this->i_children - 1 )
1466 psz_foo[i_level] = '`';
1470 psz_foo[i_level] = '|';
1473 psz_foo[i_level+1] = '-';
1474 psz_foo[i_level+2] = '\0';
1476 DumpStructure( p_this->pp_children[i], i_level + 2, psz_foo );
1480 static vlc_list_t * NewList( int i_count )
1482 vlc_list_t * p_list = (vlc_list_t *)malloc( sizeof( vlc_list_t ) );
1483 if( p_list == NULL )
1488 p_list->i_count = i_count;
1492 p_list->p_values = NULL;
1496 p_list->p_values = malloc( i_count * sizeof( vlc_value_t ) );
1497 if( p_list->p_values == NULL )
1499 p_list->i_count = 0;
1506 static void ListReplace( vlc_list_t *p_list, vlc_object_t *p_object,
1509 if( p_list == NULL || i_index >= p_list->i_count )
1514 p_object->p_internals->i_refcount++;
1516 p_list->p_values[i_index].p_object = p_object;
1521 /*static void ListAppend( vlc_list_t *p_list, vlc_object_t *p_object )
1523 if( p_list == NULL )
1528 p_list->p_values = realloc( p_list->p_values, (p_list->i_count + 1)
1529 * sizeof( vlc_value_t ) );
1530 if( p_list->p_values == NULL )
1532 p_list->i_count = 0;
1536 p_object->p_internals->i_refcount++;
1538 p_list->p_values[p_list->i_count].p_object = p_object;
1544 static int CountChildren( vlc_object_t *p_this, int i_type )
1546 vlc_object_t *p_tmp;
1549 for( i = 0; i < p_this->i_children; i++ )
1551 p_tmp = p_this->pp_children[i];
1553 if( p_tmp->i_object_type == i_type )
1558 if( p_tmp->i_children )
1560 i_count += CountChildren( p_tmp, i_type );
1567 static void ListChildren( vlc_list_t *p_list, vlc_object_t *p_this, int i_type )
1569 vlc_object_t *p_tmp;
1572 for( i = 0; i < p_this->i_children; i++ )
1574 p_tmp = p_this->pp_children[i];
1576 if( p_tmp->i_object_type == i_type )
1578 ListReplace( p_list, p_tmp, p_list->i_count++ );
1581 if( p_tmp->i_children )
1583 ListChildren( p_list, p_tmp, i_type );