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