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 */
75 /*****************************************************************************
77 *****************************************************************************/
78 static int DumpCommand( vlc_object_t *, char const *,
79 vlc_value_t, vlc_value_t, void * );
81 static vlc_object_t * FindObject ( vlc_object_t *, int, int );
82 static vlc_object_t * FindObjectName( vlc_object_t *, const char *, int );
83 static void DetachObject ( vlc_object_t * );
84 static void PrintObject ( vlc_object_t *, const char * );
85 static void DumpStructure ( vlc_object_t *, int, char * );
86 static int FindIndex ( vlc_object_t *, vlc_object_t **, int );
87 static void SetAttachment ( vlc_object_t *, vlc_bool_t );
89 static vlc_list_t * NewList ( int );
90 static void ListReplace ( vlc_list_t *, vlc_object_t *, int );
91 /*static void ListAppend ( vlc_list_t *, vlc_object_t * );*/
92 static int CountChildren ( vlc_object_t *, int );
93 static void ListChildren ( vlc_list_t *, vlc_object_t *, int );
95 /*****************************************************************************
96 * Local structure lock
97 *****************************************************************************/
98 static vlc_mutex_t structure_lock;
99 static vlc_object_internals_t global_internals;
101 vlc_object_t *vlc_custom_create( vlc_object_t *p_this, size_t i_size,
102 int i_type, const char *psz_type )
105 vlc_object_internals_t *p_priv;
107 if( i_type == VLC_OBJECT_GLOBAL )
110 p_priv = &global_internals;
111 memset( p_priv, 0, sizeof( *p_priv ) );
115 p_priv = calloc( 1, sizeof( *p_priv ) + i_size );
119 p_new = (vlc_object_t *)(p_priv + 1);
122 p_new->p_internals = p_priv;
123 p_new->i_object_type = i_type;
124 p_new->psz_object_type = psz_type;
126 p_new->psz_object_name = NULL;
128 p_new->b_die = VLC_FALSE;
129 p_new->b_error = VLC_FALSE;
130 p_new->b_dead = VLC_FALSE;
131 p_priv->b_attached = VLC_FALSE;
132 p_new->b_force = VLC_FALSE;
134 p_new->psz_header = NULL;
136 if( p_this->i_flags & OBJECT_FLAGS_NODBG )
137 p_new->i_flags |= OBJECT_FLAGS_NODBG;
138 if( p_this->i_flags & OBJECT_FLAGS_QUIET )
139 p_new->i_flags |= OBJECT_FLAGS_QUIET;
140 if( p_this->i_flags & OBJECT_FLAGS_NOINTERACT )
141 p_new->i_flags |= OBJECT_FLAGS_NOINTERACT;
143 p_priv->p_vars = calloc( sizeof( variable_t ), 16 );
145 if( !p_priv->p_vars )
147 if( i_type != VLC_OBJECT_GLOBAL )
152 if( i_type == VLC_OBJECT_GLOBAL )
154 /* If i_type is global, then p_new is actually p_libvlc_global */
155 libvlc_global_data_t *p_libvlc_global = (libvlc_global_data_t *)p_new;
156 p_new->p_libvlc = NULL;
158 p_libvlc_global->i_counter = 0;
159 p_new->i_object_id = 0;
161 p_libvlc_global->i_objects = 1;
162 p_libvlc_global->pp_objects = malloc( sizeof(vlc_object_t *) );
163 p_libvlc_global->pp_objects[0] = p_new;
164 p_priv->b_attached = VLC_TRUE;
168 libvlc_global_data_t *p_libvlc_global = vlc_global();
169 if( i_type == VLC_OBJECT_LIBVLC )
171 p_new->p_libvlc = (libvlc_int_t*)p_new;
172 p_priv->b_attached = VLC_TRUE;
176 p_new->p_libvlc = p_this->p_libvlc;
179 vlc_mutex_lock( &structure_lock );
181 p_libvlc_global->i_counter++;
182 p_new->i_object_id = p_libvlc_global->i_counter;
184 /* Wooohaa! If *this* fails, we're in serious trouble! Anyway it's
185 * useless to try and recover anything if pp_objects gets smashed. */
186 TAB_APPEND( p_libvlc_global->i_objects, p_libvlc_global->pp_objects,
189 vlc_mutex_unlock( &structure_lock );
192 p_priv->i_refcount = 0;
193 p_new->p_parent = NULL;
194 p_new->pp_children = NULL;
195 p_new->i_children = 0;
197 p_new->p_private = NULL;
199 /* Initialize mutexes and condvars */
200 vlc_mutex_init( p_new, &p_new->object_lock );
201 vlc_cond_init( p_new, &p_new->object_wait );
202 vlc_mutex_init( p_new, &p_priv->var_lock );
203 vlc_spin_init( &p_priv->spin );
204 p_priv->pipes[0] = p_priv->pipes[1] = -1;
206 if( i_type == VLC_OBJECT_GLOBAL )
208 vlc_mutex_init( p_new, &structure_lock );
211 if( i_type == VLC_OBJECT_LIBVLC )
213 var_Create( p_new, "list", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
214 var_AddCallback( p_new, "list", DumpCommand, NULL );
215 var_Create( p_new, "tree", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
216 var_AddCallback( p_new, "tree", DumpCommand, NULL );
217 var_Create( p_new, "vars", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
218 var_AddCallback( p_new, "vars", DumpCommand, NULL );
226 * Allocates and initializes a vlc object.
228 * @param i_type known object type (all of them are negative integer values),
229 * or object byte size (always positive).
231 * @return the new object, or NULL on error.
233 void * __vlc_object_create( vlc_object_t *p_this, int i_type )
235 const char * psz_type;
240 case VLC_OBJECT_GLOBAL:
241 i_size = sizeof(libvlc_global_data_t);
244 case VLC_OBJECT_LIBVLC:
245 i_size = sizeof(libvlc_int_t);
248 case VLC_OBJECT_INTF:
249 i_size = sizeof(intf_thread_t);
250 psz_type = "interface";
252 case VLC_OBJECT_DIALOGS:
253 i_size = sizeof(intf_thread_t);
254 psz_type = "dialogs";
256 case VLC_OBJECT_PLAYLIST:
257 i_size = sizeof(playlist_t);
258 psz_type = "playlist";
261 i_size = sizeof(services_discovery_t);
262 psz_type = "services discovery";
264 case VLC_OBJECT_INPUT:
265 i_size = sizeof(input_thread_t);
268 case VLC_OBJECT_DEMUX:
269 i_size = sizeof(demux_t);
272 case VLC_OBJECT_ACCESS:
273 i_size = sizeof(access_t);
276 case VLC_OBJECT_DECODER:
277 i_size = sizeof(decoder_t);
278 psz_type = "decoder";
280 case VLC_OBJECT_PACKETIZER:
281 i_size = sizeof(decoder_t);
282 psz_type = "packetizer";
284 case VLC_OBJECT_ENCODER:
285 i_size = sizeof(encoder_t);
286 psz_type = "encoder";
288 case VLC_OBJECT_FILTER:
289 i_size = sizeof(filter_t);
292 case VLC_OBJECT_VOUT:
293 i_size = sizeof(vout_thread_t);
294 psz_type = "video output";
297 i_size = sizeof(spu_t);
298 psz_type = "subpicture";
300 case VLC_OBJECT_AOUT:
301 i_size = sizeof(aout_instance_t);
302 psz_type = "audio output";
304 case VLC_OBJECT_SOUT:
305 i_size = sizeof(sout_instance_t);
306 psz_type = "stream output";
309 i_size = sizeof( vlm_t );
310 psz_type = "vlm dameon";
313 i_size = sizeof( vod_t );
314 psz_type = "vod server";
317 i_size = sizeof( xml_t );
320 case VLC_OBJECT_OPENGL:
321 i_size = sizeof( vout_thread_t );
324 case VLC_OBJECT_ANNOUNCE:
325 i_size = sizeof( announce_handler_t );
326 psz_type = "announce";
328 case VLC_OBJECT_META_ENGINE:
329 i_size = sizeof( meta_engine_t );
330 psz_type = "meta engine";
332 case VLC_OBJECT_OSDMENU:
333 i_size = sizeof( osd_menu_t );
334 psz_type = "osd menu";
337 i_size = i_type > (int)sizeof(vlc_object_t)
338 ? i_type : (int)sizeof(vlc_object_t);
339 i_type = VLC_OBJECT_GENERIC;
340 psz_type = "generic";
344 return vlc_custom_create( p_this, i_size, i_type, psz_type );
349 ****************************************************************************
350 * Destroy a vlc object
352 * This function destroys an object that has been previously allocated with
353 * vlc_object_create. The object's refcount must be zero and it must not be
354 * attached to other objects in any way.
355 *****************************************************************************/
356 void __vlc_object_destroy( vlc_object_t *p_this )
358 vlc_object_internals_t *p_priv = vlc_internals( p_this );
361 if( p_this->i_children )
363 msg_Err( p_this, "cannot delete object (%i, %s) with children" ,
364 p_this->i_object_id, p_this->psz_object_name );
368 if( p_this->p_parent )
370 msg_Err( p_this, "cannot delete object (%i, %s) with a parent",
371 p_this->i_object_id, p_this->psz_object_name );
375 while( p_priv->i_refcount > 0 )
379 /* Don't warn immediately ... 100ms seems OK */
383 "refcount is %u, delaying before deletion (id=%d,type=%d)",
384 p_priv->i_refcount, p_this->i_object_id,
385 p_this->i_object_type );
387 else if( i_delay == 10 )
390 "refcount is %u, delaying again (id=%d,type=%d)",
391 p_priv->i_refcount, p_this->i_object_id,
392 p_this->i_object_type );
394 else if( i_delay == 20 )
397 "waited too long, cancelling destruction (id=%d,type=%d)",
398 p_this->i_object_id, p_this->i_object_type );
405 /* Destroy the associated variables, starting from the end so that
406 * no memmove calls have to be done. */
407 while( p_priv->i_vars )
409 var_Destroy( p_this, p_priv->p_vars[p_priv->i_vars - 1].psz_name );
412 free( p_priv->p_vars );
413 vlc_mutex_destroy( &p_priv->var_lock );
415 if( p_this->psz_header ) free( p_this->psz_header );
417 if( p_this->i_object_type == VLC_OBJECT_GLOBAL )
419 libvlc_global_data_t *p_global = (libvlc_global_data_t *)p_this;
420 /* We are the global object ... no need to lock. */
421 free( p_global->pp_objects );
422 p_global->pp_objects = NULL;
423 p_global->i_objects--;
425 vlc_mutex_destroy( &structure_lock );
429 libvlc_global_data_t *p_libvlc_global = vlc_global();
432 vlc_mutex_lock( &structure_lock );
434 /* Wooohaa! If *this* fails, we're in serious trouble! Anyway it's
435 * useless to try and recover anything if pp_objects gets smashed. */
436 i_index = FindIndex( p_this, p_libvlc_global->pp_objects,
437 p_libvlc_global->i_objects );
438 REMOVE_ELEM( p_libvlc_global->pp_objects,
439 p_libvlc_global->i_objects, i_index );
441 vlc_mutex_unlock( &structure_lock );
444 #if defined(WIN32) || defined(UNDER_CE)
445 /* if object has an associated thread, close it now */
446 if( p_priv->thread_id.hThread )
447 CloseHandle(p_priv->thread_id.hThread);
450 vlc_mutex_destroy( &p_this->object_lock );
451 vlc_cond_destroy( &p_this->object_wait );
452 vlc_spin_destroy( &p_priv->spin );
453 if( p_priv->pipes[1] != -1 )
454 close( p_priv->pipes[1] );
455 if( p_priv->pipes[0] != -1 )
456 close( p_priv->pipes[0] );
458 /* global is not dynamically allocated by vlc_object_create */
459 if( p_this->i_object_type != VLC_OBJECT_GLOBAL )
464 /** Inter-object signaling */
466 void __vlc_object_lock( vlc_object_t *obj )
468 vlc_mutex_lock( &obj->object_lock );
471 void __vlc_object_unlock( vlc_object_t *obj )
473 vlc_assert_locked( &obj->object_lock );
474 vlc_mutex_unlock( &obj->object_lock );
478 # include <winsock2.h>
479 # include <ws2tcpip.h>
482 * select()-able pipes emulated using Winsock
484 static int pipe (int fd[2])
487 int addrlen = sizeof (addr);
489 SOCKET l = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP), a,
490 c = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
491 if ((l == INVALID_SOCKET) || (c == INVALID_SOCKET))
494 memset (&addr, 0, sizeof (addr));
495 addr.sin_family = AF_INET;
496 addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
497 if (bind (l, (PSOCKADDR)&addr, sizeof (addr))
498 || getsockname (l, (PSOCKADDR)&addr, &addrlen)
500 || connect (c, (PSOCKADDR)&addr, addrlen))
503 a = accept (l, NULL, NULL);
504 if (a == INVALID_SOCKET)
515 if (l != INVALID_SOCKET)
517 if (c != INVALID_SOCKET)
523 #define read( a, b, c ) recv (a, b, c, 0)
525 #define write( a, b, c ) send (a, b, c, 0)
527 #define close( a ) closesocket (a)
531 * Returns the readable end of a pipe that becomes readable once termination
532 * of the object is requested (vlc_object_kill()).
533 * This can be used to wake-up out of a select() or poll() event loop, such
534 * typically when doing network I/O.
536 * Note that the pipe will remain the same for the lifetime of the object.
537 * DO NOT read the pipe nor close it yourself. Ever.
539 * @param obj object that would be "killed"
540 * @return a readable pipe descriptor, or -1 on error.
542 int __vlc_object_waitpipe( vlc_object_t *obj )
544 int pfd[2] = { -1, -1 };
545 struct vlc_object_internals_t *internals = obj->p_internals;
546 vlc_bool_t killed = VLC_FALSE;
548 vlc_spin_lock (&internals->spin);
549 if (internals->pipes[0] == -1)
551 /* This can only ever happen if someone killed us without locking: */
552 assert (internals->pipes[1] == -1);
553 vlc_spin_unlock (&internals->spin);
558 vlc_spin_lock (&internals->spin);
559 if (internals->pipes[0] == -1)
561 internals->pipes[0] = pfd[0];
562 internals->pipes[1] = pfd[1];
563 pfd[0] = pfd[1] = -1;
567 vlc_spin_unlock (&internals->spin);
571 /* Race condition: vlc_object_kill() already invoked! */
574 vlc_spin_lock (&internals->spin);
575 fd = internals->pipes[1];
576 internals->pipes[1] = -1;
577 vlc_spin_unlock (&internals->spin);
579 msg_Dbg (obj, "waitpipe: object already dying");
584 /* Race condition: two threads call pipe() - unlikely */
590 return internals->pipes[0];
595 * Waits for the object to be signaled (using vlc_object_signal()).
596 * If the object already has a signal pending, this function will return
597 * immediately. It is asserted that the caller holds the object lock.
599 * @return true if the object is dying and should terminate.
601 vlc_bool_t __vlc_object_wait( vlc_object_t *obj )
603 vlc_assert_locked( &obj->object_lock );
604 vlc_cond_wait( &obj->object_wait, &obj->object_lock );
610 * Waits for the object to be signaled (using vlc_object_signal()), or for
612 * If the object already has a signal pending, this function will return
613 * immediately. It is asserted that the caller holds the object lock.
615 * @return negative if the object is dying and should terminate,
616 * positive if the the object has been signaled but is not dying,
617 * 0 if timeout has been reached.
619 int __vlc_object_timedwait( vlc_object_t *obj, mtime_t deadline )
623 vlc_assert_locked( &obj->object_lock );
624 v = vlc_cond_timedwait( &obj->object_wait, &obj->object_lock, deadline );
625 if( v == 0 ) /* signaled */
626 return obj->b_die ? -1 : 1;
632 * Checks whether an object has been "killed".
633 * The object lock must be held.
635 * Typical code for an object thread could be:
637 vlc_object_lock (self);
639 while (vlc_object_alive (self))
643 if (vlc_object_wait (self))
648 ...deinitialization...
649 vlc_object_unlock (self);
652 * @return true iff the object has not been killed yet
654 vlc_bool_t __vlc_object_alive( vlc_object_t *obj )
656 vlc_assert_locked( &obj->object_lock );
662 * Signals an object for which the lock is held.
664 void __vlc_object_signal_unlocked( vlc_object_t *obj )
666 vlc_assert_locked (&obj->object_lock);
667 vlc_cond_signal( &obj->object_wait );
672 * Requests termination of an object.
673 * If the object is LibVLC, also request to terminate all its children.
675 void __vlc_object_kill( vlc_object_t *p_this )
677 struct vlc_object_internals_t *internals = p_this->p_internals;
680 vlc_mutex_lock( &p_this->object_lock );
681 p_this->b_die = VLC_TRUE;
683 vlc_spin_lock (&internals->spin);
684 fd = internals->pipes[1];
685 internals->pipes[1] = -1;
686 vlc_spin_unlock (&internals->spin);
690 msg_Dbg (p_this, "waitpipe: object killed");
694 if( p_this->i_object_type == VLC_OBJECT_LIBVLC )
695 for( int i = 0; i < p_this->i_children ; i++ )
696 vlc_object_kill( p_this->pp_children[i] );
698 vlc_object_signal_unlocked( p_this );
699 vlc_mutex_unlock( &p_this->object_lock );
704 * find an object given its ID
706 * This function looks for the object whose i_object_id field is i_id. We
707 * use a dichotomy so that lookups are in log2(n).
708 *****************************************************************************/
709 void * vlc_object_get( int i_id )
712 vlc_object_t **pp_objects;
713 libvlc_global_data_t *p_libvlc_global = vlc_global();
715 vlc_mutex_lock( &structure_lock );
717 pp_objects = p_libvlc_global->pp_objects;
719 /* Perform our dichotomy */
720 for( i_max = p_libvlc_global->i_objects - 1 ; ; )
722 i_middle = i_max / 2;
724 if( pp_objects[i_middle]->i_object_id > i_id )
728 else if( pp_objects[i_middle]->i_object_id < i_id )
732 pp_objects += i_middle;
737 /* This happens when there are only two remaining objects */
738 if( pp_objects[i_middle+1]->i_object_id == i_id )
740 vlc_mutex_unlock( &structure_lock );
741 pp_objects[i_middle+1]->p_internals->i_refcount++;
742 return pp_objects[i_middle+1];
749 vlc_mutex_unlock( &structure_lock );
750 pp_objects[i_middle]->p_internals->i_refcount++;
751 return pp_objects[i_middle];
756 /* this means that i_max == i_middle, and since we have already
757 * tested pp_objects[i_middle]), p_found is properly set. */
762 vlc_mutex_unlock( &structure_lock );
767 ****************************************************************************
768 * find a typed object and increment its refcount
769 *****************************************************************************
770 * This function recursively looks for a given object type. i_mode can be one
771 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
772 *****************************************************************************/
773 void * __vlc_object_find( vlc_object_t *p_this, int i_type, int i_mode )
775 vlc_object_t *p_found;
777 vlc_mutex_lock( &structure_lock );
779 /* If we are of the requested type ourselves, don't look further */
780 if( !(i_mode & FIND_STRICT) && p_this->i_object_type == i_type )
782 p_this->p_internals->i_refcount++;
783 vlc_mutex_unlock( &structure_lock );
787 /* Otherwise, recursively look for the object */
788 if( (i_mode & 0x000f) == FIND_ANYWHERE )
790 vlc_object_t *p_root = p_this;
793 while( p_root->p_parent != NULL &&
794 p_root != VLC_OBJECT( p_this->p_libvlc ) )
796 p_root = p_root->p_parent;
799 p_found = FindObject( p_root, i_type, (i_mode & ~0x000f)|FIND_CHILD );
800 if( p_found == NULL && p_root != VLC_OBJECT( p_this->p_libvlc ) )
802 p_found = FindObject( VLC_OBJECT( p_this->p_libvlc ),
803 i_type, (i_mode & ~0x000f)|FIND_CHILD );
808 p_found = FindObject( p_this, i_type, i_mode );
811 vlc_mutex_unlock( &structure_lock );
817 ****************************************************************************
818 * find a named object and increment its refcount
819 *****************************************************************************
820 * This function recursively looks for a given object name. i_mode can be one
821 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
822 *****************************************************************************/
823 void * __vlc_object_find_name( vlc_object_t *p_this, const char *psz_name,
826 vlc_object_t *p_found;
828 vlc_mutex_lock( &structure_lock );
830 /* If have the requested name ourselves, don't look further */
831 if( !(i_mode & FIND_STRICT)
832 && p_this->psz_object_name
833 && !strcmp( p_this->psz_object_name, psz_name ) )
835 p_this->p_internals->i_refcount++;
836 vlc_mutex_unlock( &structure_lock );
840 /* Otherwise, recursively look for the object */
841 if( (i_mode & 0x000f) == FIND_ANYWHERE )
843 vlc_object_t *p_root = p_this;
846 while( p_root->p_parent != NULL &&
847 p_root != VLC_OBJECT( p_this->p_libvlc ) )
849 p_root = p_root->p_parent;
852 p_found = FindObjectName( p_root, psz_name,
853 (i_mode & ~0x000f)|FIND_CHILD );
854 if( p_found == NULL && p_root != VLC_OBJECT( p_this->p_libvlc ) )
856 p_found = FindObjectName( VLC_OBJECT( p_this->p_libvlc ),
857 psz_name, (i_mode & ~0x000f)|FIND_CHILD );
862 p_found = FindObjectName( p_this, psz_name, i_mode );
865 vlc_mutex_unlock( &structure_lock );
871 ****************************************************************************
872 * increment an object refcount
873 *****************************************************************************/
874 void __vlc_object_yield( vlc_object_t *p_this )
876 vlc_mutex_lock( &structure_lock );
877 p_this->p_internals->i_refcount++;
878 vlc_mutex_unlock( &structure_lock );
881 static inline void Release( vlc_object_t *obj )
883 assert( obj->p_internals->i_refcount > 0 );
884 obj->p_internals->i_refcount--;
887 /*****************************************************************************
888 * decrement an object refcount
889 *****************************************************************************/
890 void __vlc_object_release( vlc_object_t *p_this )
892 vlc_mutex_lock( &structure_lock );
894 vlc_mutex_unlock( &structure_lock );
898 ****************************************************************************
899 * attach object to a parent object
900 *****************************************************************************
901 * This function sets p_this as a child of p_parent, and p_parent as a parent
902 * of p_this. This link can be undone using vlc_object_detach.
903 *****************************************************************************/
904 void __vlc_object_attach( vlc_object_t *p_this, vlc_object_t *p_parent )
906 if( !p_this ) return;
908 vlc_mutex_lock( &structure_lock );
910 /* Attach the parent to its child */
911 p_this->p_parent = p_parent;
913 /* Attach the child to its parent */
914 INSERT_ELEM( p_parent->pp_children, p_parent->i_children,
915 p_parent->i_children, p_this );
917 /* Climb up the tree to see whether we are connected with the root */
918 if( p_parent->p_internals->b_attached )
920 SetAttachment( p_this, VLC_TRUE );
923 vlc_mutex_unlock( &structure_lock );
927 ****************************************************************************
928 * detach object from its parent
929 *****************************************************************************
930 * This function removes all links between an object and its parent.
931 *****************************************************************************/
932 void __vlc_object_detach( vlc_object_t *p_this )
934 if( !p_this ) return;
936 vlc_mutex_lock( &structure_lock );
937 if( !p_this->p_parent )
939 msg_Err( p_this, "object is not attached" );
940 vlc_mutex_unlock( &structure_lock );
944 /* Climb up the tree to see whether we are connected with the root */
945 if( p_this->p_parent->p_internals->b_attached )
947 SetAttachment( p_this, VLC_FALSE );
950 DetachObject( p_this );
951 vlc_mutex_unlock( &structure_lock );
956 ****************************************************************************
957 * find a list typed objects and increment their refcount
958 *****************************************************************************
959 * This function recursively looks for a given object type. i_mode can be one
960 * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
961 *****************************************************************************/
962 vlc_list_t * __vlc_list_find( vlc_object_t *p_this, int i_type, int i_mode )
965 vlc_object_t **pp_current, **pp_end;
966 int i_count = 0, i_index = 0;
967 libvlc_global_data_t *p_libvlc_global = vlc_global();
969 vlc_mutex_lock( &structure_lock );
971 /* Look for the objects */
972 switch( i_mode & 0x000f )
975 pp_current = p_libvlc_global->pp_objects;
976 pp_end = pp_current + p_libvlc_global->i_objects;
978 for( ; pp_current < pp_end ; pp_current++ )
980 if( (*pp_current)->p_internals->b_attached
981 && (*pp_current)->i_object_type == i_type )
987 p_list = NewList( i_count );
988 pp_current = p_libvlc_global->pp_objects;
990 for( ; pp_current < pp_end ; pp_current++ )
992 if( (*pp_current)->p_internals->b_attached
993 && (*pp_current)->i_object_type == i_type )
995 ListReplace( p_list, *pp_current, i_index );
996 if( i_index < i_count ) i_index++;
1002 i_count = CountChildren( p_this, i_type );
1003 p_list = NewList( i_count );
1005 /* Check allocation was successful */
1006 if( p_list->i_count != i_count )
1008 msg_Err( p_this, "list allocation failed!" );
1009 p_list->i_count = 0;
1013 p_list->i_count = 0;
1014 ListChildren( p_list, p_this, i_type );
1018 msg_Err( p_this, "unimplemented!" );
1019 p_list = NewList( 0 );
1023 vlc_mutex_unlock( &structure_lock );
1028 /*****************************************************************************
1029 * DumpCommand: print the current vlc structure
1030 *****************************************************************************
1031 * This function prints either an ASCII tree showing the connections between
1032 * vlc objects, and additional information such as their refcount, thread ID,
1033 * etc. (command "tree"), or the same data as a simple list (command "list").
1034 *****************************************************************************/
1035 static int DumpCommand( vlc_object_t *p_this, char const *psz_cmd,
1036 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1038 libvlc_global_data_t *p_libvlc_global = vlc_global();
1040 (void)oldval; (void)p_data;
1041 if( *psz_cmd == 'l' )
1043 vlc_mutex_lock( &structure_lock );
1045 vlc_object_t **pp_current, **pp_end;
1047 pp_current = p_libvlc_global->pp_objects;
1048 pp_end = pp_current + p_libvlc_global->i_objects;
1050 for( ; pp_current < pp_end ; pp_current++ )
1052 if( (*pp_current)->p_internals->b_attached )
1054 PrintObject( *pp_current, "" );
1058 printf( " o %.8i %s (not attached)\n",
1059 (*pp_current)->i_object_id,
1060 (*pp_current)->psz_object_type );
1064 vlc_mutex_unlock( &structure_lock );
1068 vlc_object_t *p_object = NULL;
1070 if( *newval.psz_string )
1073 int i_id = strtol( newval.psz_string, &end, 0 );
1074 if( end != newval.psz_string )
1075 p_object = vlc_object_get( i_id );
1078 /* try using the object's name to find it */
1079 vlc_object_t *p_libvlc = vlc_object_get( 1 );
1082 /* Look in p_libvlc's children tree */
1083 p_object = vlc_object_find_name( p_libvlc,
1086 vlc_object_release( p_libvlc );
1090 /* If it's not in libvlc, look in libvlc_global (== p_this) */
1091 p_object = vlc_object_find_name( p_this,
1103 vlc_mutex_lock( &structure_lock );
1105 if( *psz_cmd == 't' )
1107 char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
1110 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
1113 DumpStructure( p_object, 0, psz_foo );
1115 else if( *psz_cmd == 'v' )
1120 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
1122 PrintObject( p_object, "" );
1124 if( !p_object->p_internals->i_vars )
1125 printf( " `-o No variables\n" );
1126 for( i = 0; i < p_object->p_internals->i_vars; i++ )
1128 variable_t *p_var = p_object->p_internals->p_vars + i;
1130 const char *psz_type = "unknown";
1131 switch( p_var->i_type & VLC_VAR_TYPE )
1133 #define MYCASE( type, nice ) \
1134 case VLC_VAR_ ## type: \
1137 MYCASE( VOID, "void" );
1138 MYCASE( BOOL, "bool" );
1139 MYCASE( INTEGER, "integer" );
1140 MYCASE( HOTKEY, "hotkey" );
1141 MYCASE( STRING, "string" );
1142 MYCASE( MODULE, "module" );
1143 MYCASE( FILE, "file" );
1144 MYCASE( DIRECTORY, "directory" );
1145 MYCASE( VARIABLE, "variable" );
1146 MYCASE( FLOAT, "float" );
1147 MYCASE( TIME, "time" );
1148 MYCASE( ADDRESS, "address" );
1149 MYCASE( MUTEX, "mutex" );
1150 MYCASE( LIST, "list" );
1153 printf( " %c-o \"%s\" (%s",
1154 i + 1 == p_object->p_internals->i_vars ? '`' : '|',
1155 p_var->psz_name, psz_type );
1156 if( p_var->psz_text )
1157 printf( ", %s", p_var->psz_text );
1159 if( p_var->i_type & VLC_VAR_ISCOMMAND )
1160 printf( ", command" );
1161 if( p_var->i_entries )
1162 printf( ", %d callbacks", p_var->i_entries );
1163 switch( p_var->i_type & 0x00f0 )
1169 printf( ": %s", p_var->val.b_bool ? "true" : "false" );
1171 case VLC_VAR_INTEGER:
1172 printf( ": %d", p_var->val.i_int );
1174 case VLC_VAR_STRING:
1175 printf( ": \"%s\"", p_var->val.psz_string );
1178 printf( ": %f", p_var->val.f_float );
1181 printf( ": " I64Fi, (int64_t)p_var->val.i_time );
1183 case VLC_VAR_ADDRESS:
1184 printf( ": %p", p_var->val.p_address );
1194 vlc_mutex_unlock( &structure_lock );
1196 if( *newval.psz_string )
1198 vlc_object_release( p_object );
1205 /*****************************************************************************
1206 * vlc_list_release: free a list previously allocated by vlc_list_find
1207 *****************************************************************************
1208 * This function decreases the refcount of all objects in the list and
1210 *****************************************************************************/
1211 void vlc_list_release( vlc_list_t *p_list )
1215 vlc_mutex_lock( &structure_lock );
1216 for( i_index = 0; i_index < p_list->i_count; i_index++ )
1218 Release( p_list->p_values[i_index].p_object );
1220 vlc_mutex_unlock( &structure_lock );
1222 free( p_list->p_values );
1226 /* Following functions are local */
1228 /*****************************************************************************
1229 * FindIndex: find the index of an object in an array of objects
1230 *****************************************************************************
1231 * This function assumes that p_this can be found in pp_objects. It will not
1232 * crash if p_this cannot be found, but will return a wrong value. It is your
1233 * duty to check the return value if you are not certain that the object could
1234 * be found for sure.
1235 *****************************************************************************/
1236 static int FindIndex( vlc_object_t *p_this,
1237 vlc_object_t **pp_objects, int i_count )
1239 int i_middle = i_count / 2;
1246 if( pp_objects[i_middle] == p_this )
1256 /* We take advantage of the sorted array */
1257 if( pp_objects[i_middle]->i_object_id < p_this->i_object_id )
1259 return i_middle + FindIndex( p_this, pp_objects + i_middle,
1260 i_count - i_middle );
1264 return FindIndex( p_this, pp_objects, i_middle );
1268 static vlc_object_t * FindObject( vlc_object_t *p_this, int i_type, int i_mode )
1271 vlc_object_t *p_tmp;
1273 switch( i_mode & 0x000f )
1276 p_tmp = p_this->p_parent;
1279 if( p_tmp->i_object_type == i_type )
1281 p_tmp->p_internals->i_refcount++;
1286 return FindObject( p_tmp, i_type, i_mode );
1292 for( i = p_this->i_children; i--; )
1294 p_tmp = p_this->pp_children[i];
1295 if( p_tmp->i_object_type == i_type )
1297 p_tmp->p_internals->i_refcount++;
1300 else if( p_tmp->i_children )
1302 p_tmp = FindObject( p_tmp, i_type, i_mode );
1312 /* Handled in vlc_object_find */
1319 static vlc_object_t * FindObjectName( vlc_object_t *p_this,
1320 const char *psz_name,
1324 vlc_object_t *p_tmp;
1326 switch( i_mode & 0x000f )
1329 p_tmp = p_this->p_parent;
1332 if( p_tmp->psz_object_name
1333 && !strcmp( p_tmp->psz_object_name, psz_name ) )
1335 p_tmp->p_internals->i_refcount++;
1340 return FindObjectName( p_tmp, psz_name, i_mode );
1346 for( i = p_this->i_children; i--; )
1348 p_tmp = p_this->pp_children[i];
1349 if( p_tmp->psz_object_name
1350 && !strcmp( p_tmp->psz_object_name, psz_name ) )
1352 p_tmp->p_internals->i_refcount++;
1355 else if( p_tmp->i_children )
1357 p_tmp = FindObjectName( p_tmp, psz_name, i_mode );
1367 /* Handled in vlc_object_find */
1374 static void DetachObject( vlc_object_t *p_this )
1376 vlc_object_t *p_parent = p_this->p_parent;
1379 /* Remove p_this's parent */
1380 p_this->p_parent = NULL;
1382 /* Remove all of p_parent's children which are p_this */
1383 for( i_index = p_parent->i_children ; i_index-- ; )
1385 if( p_parent->pp_children[i_index] == p_this )
1387 p_parent->i_children--;
1388 for( i = i_index ; i < p_parent->i_children ; i++ )
1390 p_parent->pp_children[i] = p_parent->pp_children[i+1];
1395 if( p_parent->i_children )
1397 p_parent->pp_children = (vlc_object_t **)realloc( p_parent->pp_children,
1398 p_parent->i_children * sizeof(vlc_object_t *) );
1402 free( p_parent->pp_children );
1403 p_parent->pp_children = NULL;
1407 /*****************************************************************************
1408 * SetAttachment: recursively set the b_attached flag of a subtree.
1409 *****************************************************************************
1410 * This function is used by the attach and detach functions to propagate
1411 * the b_attached flag in a subtree.
1412 *****************************************************************************/
1413 static void SetAttachment( vlc_object_t *p_this, vlc_bool_t b_attached )
1417 for( i_index = p_this->i_children ; i_index-- ; )
1419 SetAttachment( p_this->pp_children[i_index], b_attached );
1422 p_this->p_internals->b_attached = b_attached;
1425 static void PrintObject( vlc_object_t *p_this, const char *psz_prefix )
1427 char psz_children[20], psz_refcount[20], psz_thread[30], psz_name[50],
1431 if( p_this->psz_object_name )
1433 snprintf( psz_name, 49, " \"%s\"", p_this->psz_object_name );
1435 psz_name[48] = '\"';
1438 psz_children[0] = '\0';
1439 switch( p_this->i_children )
1444 strcpy( psz_children, ", 1 child" );
1447 snprintf( psz_children, 19, ", %i children", p_this->i_children );
1451 psz_refcount[0] = '\0';
1452 if( p_this->p_internals->i_refcount > 0 )
1453 snprintf( psz_refcount, 19, ", refcount %u",
1454 p_this->p_internals->i_refcount );
1456 psz_thread[0] = '\0';
1457 if( p_this->p_internals->b_thread )
1458 snprintf( psz_thread, 29, " (thread %u)",
1459 #if defined(WIN32) || defined(UNDER_CE)
1460 (unsigned)p_this->p_internals->thread_id.id );
1462 (unsigned)p_this->p_internals->thread_id );
1465 psz_parent[0] = '\0';
1466 if( p_this->p_parent )
1467 snprintf( psz_parent, 19, ", parent %i", p_this->p_parent->i_object_id );
1469 printf( " %so %.8i %s%s%s%s%s%s\n", psz_prefix,
1470 p_this->i_object_id, p_this->psz_object_type,
1471 psz_name, psz_thread, psz_refcount, psz_children,
1475 static void DumpStructure( vlc_object_t *p_this, int i_level, char *psz_foo )
1478 char i_back = psz_foo[i_level];
1479 psz_foo[i_level] = '\0';
1481 PrintObject( p_this, psz_foo );
1483 psz_foo[i_level] = i_back;
1485 if( i_level / 2 >= MAX_DUMPSTRUCTURE_DEPTH )
1487 msg_Warn( p_this, "structure tree is too deep" );
1491 for( i = 0 ; i < p_this->i_children ; i++ )
1495 psz_foo[i_level-1] = ' ';
1497 if( psz_foo[i_level-2] == '`' )
1499 psz_foo[i_level-2] = ' ';
1503 if( i == p_this->i_children - 1 )
1505 psz_foo[i_level] = '`';
1509 psz_foo[i_level] = '|';
1512 psz_foo[i_level+1] = '-';
1513 psz_foo[i_level+2] = '\0';
1515 DumpStructure( p_this->pp_children[i], i_level + 2, psz_foo );
1519 static vlc_list_t * NewList( int i_count )
1521 vlc_list_t * p_list = (vlc_list_t *)malloc( sizeof( vlc_list_t ) );
1522 if( p_list == NULL )
1527 p_list->i_count = i_count;
1531 p_list->p_values = NULL;
1535 p_list->p_values = malloc( i_count * sizeof( vlc_value_t ) );
1536 if( p_list->p_values == NULL )
1538 p_list->i_count = 0;
1545 static void ListReplace( vlc_list_t *p_list, vlc_object_t *p_object,
1548 if( p_list == NULL || i_index >= p_list->i_count )
1553 p_object->p_internals->i_refcount++;
1555 p_list->p_values[i_index].p_object = p_object;
1560 /*static void ListAppend( vlc_list_t *p_list, vlc_object_t *p_object )
1562 if( p_list == NULL )
1567 p_list->p_values = realloc( p_list->p_values, (p_list->i_count + 1)
1568 * sizeof( vlc_value_t ) );
1569 if( p_list->p_values == NULL )
1571 p_list->i_count = 0;
1575 p_object->p_internals->i_refcount++;
1577 p_list->p_values[p_list->i_count].p_object = p_object;
1583 static int CountChildren( vlc_object_t *p_this, int i_type )
1585 vlc_object_t *p_tmp;
1588 for( i = 0; i < p_this->i_children; i++ )
1590 p_tmp = p_this->pp_children[i];
1592 if( p_tmp->i_object_type == i_type )
1597 if( p_tmp->i_children )
1599 i_count += CountChildren( p_tmp, i_type );
1606 static void ListChildren( vlc_list_t *p_list, vlc_object_t *p_this, int i_type )
1608 vlc_object_t *p_tmp;
1611 for( i = 0; i < p_this->i_children; i++ )
1613 p_tmp = p_this->pp_children[i];
1615 if( p_tmp->i_object_type == i_type )
1617 ListReplace( p_list, p_tmp, p_list->i_count++ );
1620 if( p_tmp->i_children )
1622 ListChildren( p_list, p_tmp, i_type );