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