]> git.sesse.net Git - vlc/blob - src/misc/objects.c
Improvements to preferences
[vlc] / src / misc / objects.c
1 /*****************************************************************************
2  * objects.c: vlc_object_t handling
3  *****************************************************************************
4  * Copyright (C) 2004 VideoLAN
5  * $Id$
6  *
7  * Authors: Samuel Hocevar <sam@zoy.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /**
25  * \file
26  * This file contains the functions to handle the vlc_object_t type
27  */
28
29
30 /*****************************************************************************
31  * Preamble
32  *****************************************************************************/
33 #include <vlc/vlc.h>
34 #include <vlc/input.h>
35
36 #ifdef HAVE_STDLIB_H
37 #   include <stdlib.h>                                          /* realloc() */
38 #endif
39
40 #include "vlc_video.h"
41 #include "video_output.h"
42 #include "vlc_spu.h"
43
44 #include "audio_output.h"
45 #include "aout_internal.h"
46 #include "stream_output.h"
47
48 #include "vlc_playlist.h"
49 #include "vlc_interface.h"
50 #include "vlc_codec.h"
51 #include "vlc_filter.h"
52
53 #include "vlc_httpd.h"
54 #include "vlc_vlm.h"
55 #include "vlc_vod.h"
56 #include "vlc_tls.h"
57 #include "vlc_xml.h"
58
59 /*****************************************************************************
60  * Local prototypes
61  *****************************************************************************/
62 static int  DumpCommand( vlc_object_t *, char const *,
63                          vlc_value_t, vlc_value_t, void * );
64
65 static vlc_object_t * FindObject    ( vlc_object_t *, int, int );
66 static void           DetachObject  ( vlc_object_t * );
67 static void           PrintObject   ( vlc_object_t *, const char * );
68 static void           DumpStructure ( vlc_object_t *, int, char * );
69 static int            FindIndex     ( vlc_object_t *, vlc_object_t **, int );
70 static void           SetAttachment ( vlc_object_t *, vlc_bool_t );
71
72 static vlc_list_t   * NewList       ( int );
73 static void           ListReplace   ( vlc_list_t *, vlc_object_t *, int );
74 static void           ListAppend    ( vlc_list_t *, vlc_object_t * );
75 static int            CountChildren ( vlc_object_t *, int );
76 static void           ListChildren  ( vlc_list_t *, vlc_object_t *, int );
77
78 /*****************************************************************************
79  * Local structure lock
80  *****************************************************************************/
81 static vlc_mutex_t    structure_lock;
82
83 /*****************************************************************************
84  * vlc_object_create: initialize a vlc object
85  *****************************************************************************
86  * This function allocates memory for a vlc object and initializes it. If
87  * i_type is not a known value such as VLC_OBJECT_ROOT, VLC_OBJECT_VOUT and
88  * so on, vlc_object_create will use its value for the object size.
89  *****************************************************************************/
90
91 /**
92  * Initialize a vlc object
93  *
94  * This function allocates memory for a vlc object and initializes it. If
95  * i_type is not a known value such as VLC_OBJECT_ROOT, VLC_OBJECT_VOUT and
96  * so on, vlc_object_create will use its value for the object size.
97  */
98 void * __vlc_object_create( vlc_object_t *p_this, int i_type )
99 {
100     vlc_object_t * p_new;
101     char *         psz_type;
102     size_t         i_size;
103
104     switch( i_type )
105     {
106         case VLC_OBJECT_ROOT:
107             i_size = sizeof(libvlc_t);
108             psz_type = "root";
109             break;
110         case VLC_OBJECT_VLC:
111             i_size = sizeof(vlc_t);
112             psz_type = "vlc";
113             break;
114         case VLC_OBJECT_MODULE:
115             i_size = sizeof(module_t);
116             psz_type = "module";
117             break;
118         case VLC_OBJECT_INTF:
119             i_size = sizeof(intf_thread_t);
120             psz_type = "interface";
121             break;
122         case VLC_OBJECT_DIALOGS:
123             i_size = sizeof(intf_thread_t);
124             psz_type = "dialogs provider";
125             break;
126         case VLC_OBJECT_PLAYLIST:
127             i_size = sizeof(playlist_t);
128             psz_type = "playlist";
129             break;
130         case VLC_OBJECT_SD:
131             i_size = sizeof(services_discovery_t);
132             psz_type = "services discovery";
133             break;
134         case VLC_OBJECT_INPUT:
135             i_size = sizeof(input_thread_t);
136             psz_type = "input";
137             break;
138         case VLC_OBJECT_DEMUX:
139             i_size = sizeof(demux_t);
140             psz_type = "demux";
141             break;
142         case VLC_OBJECT_STREAM:
143             i_size = sizeof(stream_t);
144             psz_type = "stream";
145             break;
146         case VLC_OBJECT_ACCESS:
147             i_size = sizeof(access_t);
148             psz_type = "access";
149             break;
150         case VLC_OBJECT_DECODER:
151             i_size = sizeof(decoder_t);
152             psz_type = "decoder";
153             break;
154         case VLC_OBJECT_PACKETIZER:
155             i_size = sizeof(decoder_t);
156             psz_type = "packetizer";
157             break;
158         case VLC_OBJECT_ENCODER:
159             i_size = sizeof(encoder_t);
160             psz_type = "encoder";
161             break;
162         case VLC_OBJECT_FILTER:
163             i_size = sizeof(filter_t);
164             psz_type = "filter";
165             break;
166         case VLC_OBJECT_VOUT:
167             i_size = sizeof(vout_thread_t);
168             psz_type = "video output";
169             break;
170         case VLC_OBJECT_SPU:
171             i_size = sizeof(spu_t);
172             psz_type = "subpicture unit";
173             break;
174         case VLC_OBJECT_AOUT:
175             i_size = sizeof(aout_instance_t);
176             psz_type = "audio output";
177             break;
178         case VLC_OBJECT_SOUT:
179             i_size = sizeof(sout_instance_t);
180             psz_type = "stream output";
181             break;
182         case VLC_OBJECT_HTTPD:
183             i_size = sizeof( httpd_t );
184             psz_type = "http daemon";
185             break;
186         case VLC_OBJECT_VLM:
187             i_size = sizeof( vlm_t );
188             psz_type = "vlm dameon";
189             break;
190         case VLC_OBJECT_VOD:
191             i_size = sizeof( vod_t );
192             psz_type = "vod server";
193             break;
194         case VLC_OBJECT_TLS:
195             i_size = sizeof( tls_t );
196             psz_type = "tls";
197             break;
198         case VLC_OBJECT_XML:
199             i_size = sizeof( xml_t );
200             psz_type = "xml";
201             break;
202         case VLC_OBJECT_OPENGL:
203             i_size = sizeof( vout_thread_t );
204             psz_type = "opengl provider";
205             break;
206         case VLC_OBJECT_ANNOUNCE:
207             i_size = sizeof( announce_handler_t );
208             psz_type = "announce handler";
209             break;
210         default:
211             i_size = i_type > 0
212                       ? i_type > (int)sizeof(vlc_object_t)
213                          ? i_type : (int)sizeof(vlc_object_t)
214                       : (int)sizeof(vlc_object_t);
215             i_type = VLC_OBJECT_GENERIC;
216             psz_type = "generic";
217             break;
218     }
219
220     if( i_type == VLC_OBJECT_ROOT )
221     {
222         p_new = p_this;
223     }
224     else
225     {
226         p_new = malloc( i_size );
227         if( !p_new ) return NULL;
228         memset( p_new, 0, i_size );
229     }
230
231     p_new->i_object_type = i_type;
232     p_new->psz_object_type = psz_type;
233
234     p_new->psz_object_name = NULL;
235
236     p_new->b_die = VLC_FALSE;
237     p_new->b_error = VLC_FALSE;
238     p_new->b_dead = VLC_FALSE;
239     p_new->b_attached = VLC_FALSE;
240     p_new->b_force = VLC_FALSE;
241
242     p_new->i_vars = 0;
243     p_new->p_vars = (variable_t *)malloc( 16 * sizeof( variable_t ) );
244
245     if( !p_new->p_vars )
246     {
247         free( p_new );
248         return NULL;
249     }
250
251     if( i_type == VLC_OBJECT_ROOT )
252     {
253         /* If i_type is root, then p_new is actually p_libvlc */
254         p_new->p_libvlc = (libvlc_t*)p_new;
255         p_new->p_vlc = NULL;
256
257         p_new->p_libvlc->i_counter = 0;
258         p_new->i_object_id = 0;
259
260         p_new->p_libvlc->i_objects = 1;
261         p_new->p_libvlc->pp_objects = malloc( sizeof(vlc_object_t *) );
262         p_new->p_libvlc->pp_objects[0] = p_new;
263         p_new->b_attached = VLC_TRUE;
264     }
265     else
266     {
267         p_new->p_libvlc = p_this->p_libvlc;
268         p_new->p_vlc = ( i_type == VLC_OBJECT_VLC ) ? (vlc_t*)p_new
269                                                     : p_this->p_vlc;
270
271         vlc_mutex_lock( &structure_lock );
272
273         p_new->p_libvlc->i_counter++;
274         p_new->i_object_id = p_new->p_libvlc->i_counter;
275
276         /* Wooohaa! If *this* fails, we're in serious trouble! Anyway it's
277          * useless to try and recover anything if pp_objects gets smashed. */
278         INSERT_ELEM( p_new->p_libvlc->pp_objects,
279                      p_new->p_libvlc->i_objects,
280                      p_new->p_libvlc->i_objects,
281                      p_new );
282
283         vlc_mutex_unlock( &structure_lock );
284     }
285
286     p_new->i_refcount = 0;
287     p_new->p_parent = NULL;
288     p_new->pp_children = NULL;
289     p_new->i_children = 0;
290
291     p_new->p_private = NULL;
292
293     /* Initialize mutexes and condvars */
294     vlc_mutex_init( p_new, &p_new->object_lock );
295     vlc_cond_init( p_new, &p_new->object_wait );
296     vlc_mutex_init( p_new, &p_new->var_lock );
297
298     if( i_type == VLC_OBJECT_ROOT )
299     {
300         vlc_mutex_init( p_new, &structure_lock );
301
302         var_Create( p_new, "list", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
303         var_AddCallback( p_new, "list", DumpCommand, NULL );
304         var_Create( p_new, "tree", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
305         var_AddCallback( p_new, "tree", DumpCommand, NULL );
306     }
307
308     return p_new;
309 }
310
311 /**
312  ****************************************************************************
313  * Destroy a vlc object
314  *
315  * This function destroys an object that has been previously allocated with
316  * vlc_object_create. The object's refcount must be zero and it must not be
317  * attached to other objects in any way.
318  *****************************************************************************/
319 void __vlc_object_destroy( vlc_object_t *p_this )
320 {
321     int i_delay = 0;
322
323     if( p_this->i_children )
324     {
325         msg_Err( p_this, "cannot delete object (%i, %s) with children" ,
326                  p_this->i_object_id, p_this->psz_object_name );
327         return;
328     }
329
330     if( p_this->p_parent )
331     {
332         msg_Err( p_this, "cannot delete object (%i, %s) with a parent",
333                  p_this->i_object_id, p_this->psz_object_name );
334         return;
335     }
336
337     while( p_this->i_refcount )
338     {
339         i_delay++;
340
341         /* Don't warn immediately ... 100ms seems OK */
342         if( i_delay == 2 )
343         {
344             msg_Warn( p_this, "refcount is %i, delaying before deletion",
345                               p_this->i_refcount );
346         }
347         else if( i_delay == 12 )
348         {
349             msg_Err( p_this, "refcount is %i, I have a bad feeling about this",
350                              p_this->i_refcount );
351         }
352         else if( i_delay == 42 )
353         {
354             msg_Err( p_this, "we waited too long, cancelling destruction" );
355             return;
356         }
357
358         msleep( 100000 );
359     }
360
361     /* Destroy the associated variables, starting from the end so that
362      * no memmove calls have to be done. */
363     while( p_this->i_vars )
364     {
365         var_Destroy( p_this, p_this->p_vars[p_this->i_vars - 1].psz_name );
366     }
367
368     free( p_this->p_vars );
369     vlc_mutex_destroy( &p_this->var_lock );
370
371     if( p_this->i_object_type == VLC_OBJECT_ROOT )
372     {
373         /* We are the root object ... no need to lock. */
374         free( p_this->p_libvlc->pp_objects );
375         p_this->p_libvlc->pp_objects = NULL;
376         p_this->p_libvlc->i_objects--;
377
378         vlc_mutex_destroy( &structure_lock );
379     }
380     else
381     {
382         int i_index;
383
384         vlc_mutex_lock( &structure_lock );
385
386         /* Wooohaa! If *this* fails, we're in serious trouble! Anyway it's
387          * useless to try and recover anything if pp_objects gets smashed. */
388         i_index = FindIndex( p_this, p_this->p_libvlc->pp_objects,
389                              p_this->p_libvlc->i_objects );
390         REMOVE_ELEM( p_this->p_libvlc->pp_objects,
391                      p_this->p_libvlc->i_objects, i_index );
392
393         vlc_mutex_unlock( &structure_lock );
394     }
395
396     vlc_mutex_destroy( &p_this->object_lock );
397     vlc_cond_destroy( &p_this->object_wait );
398
399     free( p_this );
400 }
401
402 /**
403  * find an object given its ID
404  *
405  * This function looks for the object whose i_object_id field is i_id. We
406  * use a dichotomy so that lookups are in log2(n).
407  *****************************************************************************/
408 void * __vlc_object_get( vlc_object_t *p_this, int i_id )
409 {
410     int i_max, i_middle;
411     vlc_object_t **pp_objects;
412
413     vlc_mutex_lock( &structure_lock );
414
415     pp_objects = p_this->p_libvlc->pp_objects;
416
417     /* Perform our dichotomy */
418     for( i_max = p_this->p_libvlc->i_objects - 1 ; ; )
419     {
420         i_middle = i_max / 2;
421
422         if( pp_objects[i_middle]->i_object_id > i_id )
423         {
424             i_max = i_middle;
425         }
426         else if( pp_objects[i_middle]->i_object_id < i_id )
427         {
428             if( i_middle )
429             {
430                 pp_objects += i_middle;
431                 i_max -= i_middle;
432             }
433             else
434             {
435                 /* This happens when there are only two remaining objects */
436                 if( pp_objects[i_middle+1]->i_object_id == i_id )
437                 {
438                     vlc_mutex_unlock( &structure_lock );
439                     pp_objects[i_middle+1]->i_refcount++;
440                     return pp_objects[i_middle+1];
441                 }
442                 break;
443             }
444         }
445         else
446         {
447             vlc_mutex_unlock( &structure_lock );
448             pp_objects[i_middle]->i_refcount++;
449             return pp_objects[i_middle];
450         }
451
452         if( i_max == 0 )
453         {
454             /* this means that i_max == i_middle, and since we have already
455              * tested pp_objects[i_middle]), p_found is properly set. */
456             break;
457         }
458     }
459
460     vlc_mutex_unlock( &structure_lock );
461     return NULL;
462 }
463
464 /**
465  ****************************************************************************
466  * find a typed object and increment its refcount
467  *****************************************************************************
468  * This function recursively looks for a given object type. i_mode can be one
469  * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
470  *****************************************************************************/
471 void * __vlc_object_find( vlc_object_t *p_this, int i_type, int i_mode )
472 {
473     vlc_object_t *p_found;
474
475     vlc_mutex_lock( &structure_lock );
476
477     /* If we are of the requested type ourselves, don't look further */
478     if( !(i_mode & FIND_STRICT) && p_this->i_object_type == i_type )
479     {
480         p_this->i_refcount++;
481         vlc_mutex_unlock( &structure_lock );
482         return p_this;
483     }
484
485     /* Otherwise, recursively look for the object */
486     if( (i_mode & 0x000f) == FIND_ANYWHERE )
487     {
488         vlc_object_t *p_root = p_this;
489
490         /* Find the root */
491         while( p_root->p_parent != NULL &&
492                p_root != VLC_OBJECT( p_this->p_vlc ) )
493         {
494             p_root = p_root->p_parent;
495         }
496
497         p_found = FindObject( p_root, i_type, (i_mode & ~0x000f)|FIND_CHILD );
498         if( p_found == NULL && p_root != VLC_OBJECT( p_this->p_vlc ) )
499         {
500             p_found = FindObject( VLC_OBJECT( p_this->p_vlc ),
501                                   i_type, (i_mode & ~0x000f)|FIND_CHILD );
502         }
503     }
504     else
505     {
506         p_found = FindObject( p_this, i_type, i_mode );
507     }
508
509     vlc_mutex_unlock( &structure_lock );
510
511     return p_found;
512 }
513
514 /**
515  ****************************************************************************
516  * increment an object refcount
517  *****************************************************************************/
518 void __vlc_object_yield( vlc_object_t *p_this )
519 {
520     vlc_mutex_lock( &structure_lock );
521     p_this->i_refcount++;
522     vlc_mutex_unlock( &structure_lock );
523 }
524
525 /**
526  ****************************************************************************
527  * decrement an object refcount
528  *****************************************************************************/
529 void __vlc_object_release( vlc_object_t *p_this )
530 {
531     vlc_mutex_lock( &structure_lock );
532     p_this->i_refcount--;
533     vlc_mutex_unlock( &structure_lock );
534 }
535
536 /**
537  ****************************************************************************
538  * attach object to a parent object
539  *****************************************************************************
540  * This function sets p_this as a child of p_parent, and p_parent as a parent
541  * of p_this. This link can be undone using vlc_object_detach.
542  *****************************************************************************/
543 void __vlc_object_attach( vlc_object_t *p_this, vlc_object_t *p_parent )
544 {
545     vlc_mutex_lock( &structure_lock );
546
547     /* Attach the parent to its child */
548     p_this->p_parent = p_parent;
549
550     /* Attach the child to its parent */
551     INSERT_ELEM( p_parent->pp_children, p_parent->i_children,
552                  p_parent->i_children, p_this );
553
554     /* Climb up the tree to see whether we are connected with the root */
555     if( p_parent->b_attached )
556     {
557         SetAttachment( p_this, VLC_TRUE );
558     }
559
560     vlc_mutex_unlock( &structure_lock );
561 }
562
563 /**
564  ****************************************************************************
565  * detach object from its parent
566  *****************************************************************************
567  * This function removes all links between an object and its parent.
568  *****************************************************************************/
569 void __vlc_object_detach( vlc_object_t *p_this )
570 {
571     vlc_mutex_lock( &structure_lock );
572     if( !p_this->p_parent )
573     {
574         msg_Err( p_this, "object is not attached" );
575         vlc_mutex_unlock( &structure_lock );
576         return;
577     }
578
579     /* Climb up the tree to see whether we are connected with the root */
580     if( p_this->p_parent->b_attached )
581     {
582         SetAttachment( p_this, VLC_FALSE );
583     }
584
585     DetachObject( p_this );
586     vlc_mutex_unlock( &structure_lock );
587 }
588
589 /**
590  ****************************************************************************
591  * find a list typed objects and increment their refcount
592  *****************************************************************************
593  * This function recursively looks for a given object type. i_mode can be one
594  * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
595  *****************************************************************************/
596 vlc_list_t * __vlc_list_find( vlc_object_t *p_this, int i_type, int i_mode )
597 {
598     vlc_list_t *p_list;
599     vlc_object_t **pp_current, **pp_end;
600     int i_count = 0, i_index = 0;
601
602     vlc_mutex_lock( &structure_lock );
603
604     /* Look for the objects */
605     switch( i_mode & 0x000f )
606     {
607     case FIND_ANYWHERE:
608         pp_current = p_this->p_libvlc->pp_objects;
609         pp_end = pp_current + p_this->p_libvlc->i_objects;
610
611         for( ; pp_current < pp_end ; pp_current++ )
612         {
613             if( (*pp_current)->b_attached
614                  && (*pp_current)->i_object_type == i_type )
615             {
616                 i_count++;
617             }
618         }
619
620         p_list = NewList( i_count );
621         pp_current = p_this->p_libvlc->pp_objects;
622
623         for( ; pp_current < pp_end ; pp_current++ )
624         {
625             if( (*pp_current)->b_attached
626                  && (*pp_current)->i_object_type == i_type )
627             {
628                 ListReplace( p_list, *pp_current, i_index );
629                 if( i_index < i_count ) i_index++;
630             }
631         }
632     break;
633
634     case FIND_CHILD:
635         i_count = CountChildren( p_this, i_type );
636         p_list = NewList( i_count );
637
638         /* Check allocation was successful */
639         if( p_list->i_count != i_count )
640         {
641             msg_Err( p_this, "list allocation failed!" );
642             p_list->i_count = 0;
643             break;
644         }
645
646         p_list->i_count = 0;
647         ListChildren( p_list, p_this, i_type );
648         break;
649
650     default:
651         msg_Err( p_this, "unimplemented!" );
652         p_list = NewList( 0 );
653         break;
654     }
655
656     vlc_mutex_unlock( &structure_lock );
657
658     return p_list;
659 }
660
661 /*****************************************************************************
662  * DumpCommand: print the current vlc structure
663  *****************************************************************************
664  * This function prints either an ASCII tree showing the connections between
665  * vlc objects, and additional information such as their refcount, thread ID,
666  * etc. (command "tree"), or the same data as a simple list (command "list").
667  *****************************************************************************/
668 static int DumpCommand( vlc_object_t *p_this, char const *psz_cmd,
669                         vlc_value_t oldval, vlc_value_t newval, void *p_data )
670 {
671     if( *psz_cmd == 't' )
672     {
673         char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
674         vlc_object_t *p_object;
675
676         if( *newval.psz_string )
677         {
678             p_object = vlc_object_get( p_this, atoi(newval.psz_string) );
679
680             if( !p_object )
681             {
682                 return VLC_ENOOBJ;
683             }
684         }
685         else
686         {
687             p_object = p_this->p_vlc ? VLC_OBJECT(p_this->p_vlc) : p_this;
688         }
689
690         vlc_mutex_lock( &structure_lock );
691
692         psz_foo[0] = '|';
693         DumpStructure( p_object, 0, psz_foo );
694
695         vlc_mutex_unlock( &structure_lock );
696
697         if( *newval.psz_string )
698         {
699             vlc_object_release( p_this );
700         }
701     }
702     else if( *psz_cmd == 'l' )
703     {
704         vlc_object_t **pp_current, **pp_end;
705
706         vlc_mutex_lock( &structure_lock );
707
708         pp_current = p_this->p_libvlc->pp_objects;
709         pp_end = pp_current + p_this->p_libvlc->i_objects;
710
711         for( ; pp_current < pp_end ; pp_current++ )
712         {
713             if( (*pp_current)->b_attached )
714             {
715                 PrintObject( *pp_current, "" );
716             }
717             else
718             {
719                 printf( " o %.8i %s (not attached)\n",
720                         (*pp_current)->i_object_id,
721                         (*pp_current)->psz_object_type );
722             }
723         }
724
725         vlc_mutex_unlock( &structure_lock );
726     }
727
728     return VLC_SUCCESS;
729 }
730
731 /*****************************************************************************
732  * vlc_list_release: free a list previously allocated by vlc_list_find
733  *****************************************************************************
734  * This function decreases the refcount of all objects in the list and
735  * frees the list.
736  *****************************************************************************/
737 void vlc_list_release( vlc_list_t *p_list )
738 {
739     int i_index;
740
741     for( i_index = 0; i_index < p_list->i_count; i_index++ )
742     {
743         vlc_mutex_lock( &structure_lock );
744
745         p_list->p_values[i_index].p_object->i_refcount--;
746
747         vlc_mutex_unlock( &structure_lock );
748     }
749
750     free( p_list->p_values );
751     free( p_list );
752 }
753
754 /* Following functions are local */
755
756 /*****************************************************************************
757  * FindIndex: find the index of an object in an array of objects
758  *****************************************************************************
759  * This function assumes that p_this can be found in pp_objects. It will not
760  * crash if p_this cannot be found, but will return a wrong value. It is your
761  * duty to check the return value if you are not certain that the object could
762  * be found for sure.
763  *****************************************************************************/
764 static int FindIndex( vlc_object_t *p_this,
765                       vlc_object_t **pp_objects, int i_count )
766 {
767     int i_middle = i_count / 2;
768
769     if( i_count == 0 )
770     {
771         return 0;
772     }
773
774     if( pp_objects[i_middle] == p_this )
775     {
776         return i_middle;
777     }
778
779     if( i_count == 1 )
780     {
781         return 0;
782     }
783
784     /* We take advantage of the sorted array */
785     if( pp_objects[i_middle]->i_object_id < p_this->i_object_id )
786     {
787         return i_middle + FindIndex( p_this, pp_objects + i_middle,
788                                              i_count - i_middle );
789     }
790     else
791     {
792         return FindIndex( p_this, pp_objects, i_middle );
793     }
794 }
795
796 static vlc_object_t * FindObject( vlc_object_t *p_this, int i_type, int i_mode )
797 {
798     int i;
799     vlc_object_t *p_tmp;
800
801     switch( i_mode & 0x000f )
802     {
803     case FIND_PARENT:
804         p_tmp = p_this->p_parent;
805         if( p_tmp )
806         {
807             if( p_tmp->i_object_type == i_type )
808             {
809                 p_tmp->i_refcount++;
810                 return p_tmp;
811             }
812             else
813             {
814                 return FindObject( p_tmp, i_type, i_mode );
815             }
816         }
817         break;
818
819     case FIND_CHILD:
820         for( i = p_this->i_children; i--; )
821         {
822             p_tmp = p_this->pp_children[i];
823             if( p_tmp->i_object_type == i_type )
824             {
825                 p_tmp->i_refcount++;
826                 return p_tmp;
827             }
828             else if( p_tmp->i_children )
829             {
830                 p_tmp = FindObject( p_tmp, i_type, i_mode );
831                 if( p_tmp )
832                 {
833                     return p_tmp;
834                 }
835             }
836         }
837         break;
838
839     case FIND_ANYWHERE:
840         /* Handled in vlc_object_find */
841         break;
842     }
843
844     return NULL;
845 }
846
847 static void DetachObject( vlc_object_t *p_this )
848 {
849     vlc_object_t *p_parent = p_this->p_parent;
850     int i_index, i;
851
852     /* Remove p_this's parent */
853     p_this->p_parent = NULL;
854
855     /* Remove all of p_parent's children which are p_this */
856     for( i_index = p_parent->i_children ; i_index-- ; )
857     {
858         if( p_parent->pp_children[i_index] == p_this )
859         {
860             p_parent->i_children--;
861             for( i = i_index ; i < p_parent->i_children ; i++ )
862             {
863                 p_parent->pp_children[i] = p_parent->pp_children[i+1];
864             }
865         }
866     }
867
868     if( p_parent->i_children )
869     {
870         p_parent->pp_children = (vlc_object_t **)realloc( p_parent->pp_children,
871                                p_parent->i_children * sizeof(vlc_object_t *) );
872     }
873     else
874     {
875         free( p_parent->pp_children );
876         p_parent->pp_children = NULL;
877     }
878 }
879
880 /*****************************************************************************
881  * SetAttachment: recursively set the b_attached flag of a subtree.
882  *****************************************************************************
883  * This function is used by the attach and detach functions to propagate
884  * the b_attached flag in a subtree.
885  *****************************************************************************/
886 static void SetAttachment( vlc_object_t *p_this, vlc_bool_t b_attached )
887 {
888     int i_index;
889
890     for( i_index = p_this->i_children ; i_index-- ; )
891     {
892         SetAttachment( p_this->pp_children[i_index], b_attached );
893     }
894
895     p_this->b_attached = b_attached;
896 }
897
898 static void PrintObject( vlc_object_t *p_this, const char *psz_prefix )
899 {
900     char psz_children[20], psz_refcount[20], psz_thread[20], psz_name[50];
901
902     psz_name[0] = '\0';
903     if( p_this->psz_object_name )
904     {
905         snprintf( psz_name, 50, " \"%s\"", p_this->psz_object_name );
906         psz_name[48] = '\"';
907         psz_name[49] = '\0';
908     }
909
910     psz_children[0] = '\0';
911     switch( p_this->i_children )
912     {
913         case 0:
914             break;
915         case 1:
916             strcpy( psz_children, ", 1 child" );
917             break;
918         default:
919             snprintf( psz_children, 20,
920                       ", %i children", p_this->i_children );
921             psz_children[19] = '\0';
922             break;
923     }
924
925     psz_refcount[0] = '\0';
926     if( p_this->i_refcount )
927     {
928         snprintf( psz_refcount, 20, ", refcount %i", p_this->i_refcount );
929         psz_refcount[19] = '\0';
930     }
931
932     psz_thread[0] = '\0';
933     if( p_this->b_thread )
934     {
935         snprintf( psz_thread, 20, " (thread %d)", (int)p_this->thread_id );
936         psz_thread[19] = '\0';
937     }
938
939     printf( " %so %.8i %s%s%s%s%s\n", psz_prefix,
940             p_this->i_object_id, p_this->psz_object_type,
941             psz_name, psz_thread, psz_refcount, psz_children );
942 }
943
944 static void DumpStructure( vlc_object_t *p_this, int i_level, char *psz_foo )
945 {
946     int i;
947     char i_back = psz_foo[i_level];
948     psz_foo[i_level] = '\0';
949
950     PrintObject( p_this, psz_foo );
951
952     psz_foo[i_level] = i_back;
953
954     if( i_level / 2 >= MAX_DUMPSTRUCTURE_DEPTH )
955     {
956         msg_Warn( p_this, "structure tree is too deep" );
957         return;
958     }
959
960     for( i = 0 ; i < p_this->i_children ; i++ )
961     {
962         if( i_level )
963         {
964             psz_foo[i_level-1] = ' ';
965
966             if( psz_foo[i_level-2] == '`' )
967             {
968                 psz_foo[i_level-2] = ' ';
969             }
970         }
971
972         if( i == p_this->i_children - 1 )
973         {
974             psz_foo[i_level] = '`';
975         }
976         else
977         {
978             psz_foo[i_level] = '|';
979         }
980
981         psz_foo[i_level+1] = '-';
982         psz_foo[i_level+2] = '\0';
983
984         DumpStructure( p_this->pp_children[i], i_level + 2, psz_foo );
985     }
986 }
987
988 static vlc_list_t * NewList( int i_count )
989 {
990     vlc_list_t * p_list = (vlc_list_t *)malloc( sizeof( vlc_list_t ) );
991     if( p_list == NULL )
992     {
993         return NULL;
994     }
995
996     p_list->i_count = i_count;
997
998     if( i_count == 0 )
999     {
1000         p_list->p_values = NULL;
1001         return p_list;
1002     }
1003
1004     p_list->p_values = malloc( i_count * sizeof( vlc_value_t ) );
1005     if( p_list->p_values == NULL )
1006     {
1007         p_list->i_count = 0;
1008         return p_list;
1009     }
1010
1011     return p_list;
1012 }
1013
1014 static void ListReplace( vlc_list_t *p_list, vlc_object_t *p_object,
1015                          int i_index )
1016 {
1017     if( p_list == NULL || i_index >= p_list->i_count )
1018     {
1019         return;
1020     }
1021
1022     p_object->i_refcount++;
1023
1024     p_list->p_values[i_index].p_object = p_object;
1025
1026     return;
1027 }
1028
1029 static void ListAppend( vlc_list_t *p_list, vlc_object_t *p_object )
1030 {
1031     if( p_list == NULL )
1032     {
1033         return;
1034     }
1035
1036     p_list->p_values = realloc( p_list->p_values, (p_list->i_count + 1)
1037                                 * sizeof( vlc_value_t ) );
1038     if( p_list->p_values == NULL )
1039     {
1040         p_list->i_count = 0;
1041         return;
1042     }
1043
1044     p_object->i_refcount++;
1045
1046     p_list->p_values[p_list->i_count].p_object = p_object;
1047     p_list->i_count++;
1048
1049     return;
1050 }
1051
1052 static int CountChildren( vlc_object_t *p_this, int i_type )
1053 {
1054     vlc_object_t *p_tmp;
1055     int i, i_count = 0;
1056
1057     for( i = 0; i < p_this->i_children; i++ )
1058     {
1059         p_tmp = p_this->pp_children[i];
1060
1061         if( p_tmp->i_object_type == i_type )
1062         {
1063             i_count++;
1064         }
1065
1066         if( p_tmp->i_children )
1067         {
1068             i_count += CountChildren( p_tmp, i_type );
1069         }
1070     }
1071
1072     return i_count;
1073 }
1074
1075 static void ListChildren( vlc_list_t *p_list, vlc_object_t *p_this, int i_type )
1076 {
1077     vlc_object_t *p_tmp;
1078     int i;
1079
1080     for( i = 0; i < p_this->i_children; i++ )
1081     {
1082         p_tmp = p_this->pp_children[i];
1083
1084         if( p_tmp->i_object_type == i_type )
1085         {
1086             ListReplace( p_list, p_tmp, p_list->i_count++ );
1087         }
1088
1089         if( p_tmp->i_children )
1090         {
1091             ListChildren( p_list, p_tmp, i_type );
1092         }
1093     }
1094 }