]> git.sesse.net Git - vlc/blob - src/misc/objects.c
74fa15cc54028d77f72052a63078e110a6ef8172
[vlc] / src / misc / objects.c
1 /*****************************************************************************
2  * objects.c: vlc_object_t handling
3  *****************************************************************************
4  * Copyright (C) 2004-2007 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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, 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 #ifdef HAVE_CONFIG_H
34 # include "config.h"
35 #endif
36
37 #include <vlc/vlc.h>
38
39 #include "../libvlc.h"
40 #include <vlc_vout.h>
41 #include <vlc_aout.h>
42 #include "audio_output/aout_internal.h"
43
44 #include <vlc_access.h>
45 #include <vlc_demux.h>
46 #include <vlc_stream.h>
47
48 #include <vlc_sout.h>
49 #include "stream_output/stream_output.h"
50
51 #include "vlc_playlist.h"
52 #include "vlc_interface.h"
53 #include "vlc_codec.h"
54 #include "vlc_filter.h"
55
56 #include "vlc_httpd.h"
57 #include "vlc_vlm.h"
58 #include "input/vlm_internal.h"
59 #include "vlc_xml.h"
60 #include "vlc_osd.h"
61 #include "vlc_meta.h"
62
63 #include "variables.h"
64 #ifndef WIN32
65 # include <unistd.h>
66 #else
67 # include <io.h>
68 # include <fcntl.h>
69 # include <errno.h> /* ENOSYS */
70 #endif
71 #include <assert.h>
72
73 /*****************************************************************************
74  * Constants
75  *****************************************************************************/
76
77 const vlc_destructor_t kVLCDestructor = NULL;
78
79 /*****************************************************************************
80  * Local prototypes
81  *****************************************************************************/
82 static int  DumpCommand( vlc_object_t *, char const *,
83                          vlc_value_t, vlc_value_t, void * );
84
85 static vlc_object_t * FindObject    ( vlc_object_t *, int, int );
86 static vlc_object_t * FindObjectName( vlc_object_t *, const char *, int );
87 static void           DetachObject  ( vlc_object_t * );
88 static void           PrintObject   ( vlc_object_t *, const char * );
89 static void           DumpStructure ( vlc_object_t *, int, char * );
90 static int            FindIndex     ( vlc_object_t *, vlc_object_t **, int );
91 static void           SetAttachment ( vlc_object_t *, vlc_bool_t );
92
93 static vlc_list_t   * NewList       ( int );
94 static void           ListReplace   ( vlc_list_t *, vlc_object_t *, int );
95 /*static void           ListAppend    ( vlc_list_t *, vlc_object_t * );*/
96 static int            CountChildren ( vlc_object_t *, int );
97 static void           ListChildren  ( vlc_list_t *, vlc_object_t *, int );
98
99 static void vlc_object_destroy( vlc_object_t *p_this );
100 static void vlc_object_yield_locked( vlc_object_t *p_this );
101
102 /*****************************************************************************
103  * Local structure lock
104  *****************************************************************************/
105 static vlc_mutex_t    structure_lock;
106 static vlc_object_internals_t global_internals;
107
108 vlc_object_t *vlc_custom_create( vlc_object_t *p_this, size_t i_size,
109                                  int i_type, const char *psz_type )
110 {
111     vlc_object_t *p_new;
112     vlc_object_internals_t *p_priv;
113
114     if( i_type == VLC_OBJECT_GLOBAL )
115     {
116         p_new = p_this;
117         p_priv = &global_internals;
118         memset( p_priv, 0, sizeof( *p_priv ) );
119     }
120     else
121     {
122         p_priv = calloc( 1, sizeof( *p_priv ) + i_size );
123         if( p_priv == NULL )
124             return NULL;
125
126         p_new = (vlc_object_t *)(p_priv + 1);
127     }
128
129     p_new->p_internals = p_priv;
130     p_new->i_object_type = i_type;
131     p_new->psz_object_type = psz_type;
132
133     p_new->psz_object_name = NULL;
134
135     p_new->b_die = VLC_FALSE;
136     p_new->b_error = VLC_FALSE;
137     p_new->b_dead = VLC_FALSE;
138     p_priv->b_attached = VLC_FALSE;
139     p_new->b_force = VLC_FALSE;
140
141     p_new->psz_header = NULL;
142
143     if( p_this->i_flags & OBJECT_FLAGS_NODBG )
144         p_new->i_flags |= OBJECT_FLAGS_NODBG;
145     if( p_this->i_flags & OBJECT_FLAGS_QUIET )
146         p_new->i_flags |= OBJECT_FLAGS_QUIET;
147     if( p_this->i_flags & OBJECT_FLAGS_NOINTERACT )
148         p_new->i_flags |= OBJECT_FLAGS_NOINTERACT;
149
150     p_priv->p_vars = calloc( sizeof( variable_t ), 16 );
151
152     if( !p_priv->p_vars )
153     {
154         if( i_type != VLC_OBJECT_GLOBAL )
155             free( p_priv );
156         return NULL;
157     }
158
159     if( i_type == VLC_OBJECT_GLOBAL )
160     {
161         /* If i_type is global, then p_new is actually p_libvlc_global */
162         libvlc_global_data_t *p_libvlc_global = (libvlc_global_data_t *)p_new;
163         p_new->p_libvlc = NULL;
164
165         p_libvlc_global->i_counter = 0;
166         p_new->i_object_id = 0;
167
168         p_libvlc_global->i_objects = 1;
169         p_libvlc_global->pp_objects = malloc( sizeof(vlc_object_t *) );
170         p_libvlc_global->pp_objects[0] = p_new;
171         p_priv->b_attached = VLC_TRUE;
172     }
173     else
174     {
175         libvlc_global_data_t *p_libvlc_global = vlc_global();
176         if( i_type == VLC_OBJECT_LIBVLC )
177         {
178             p_new->p_libvlc = (libvlc_int_t*)p_new;
179             p_priv->b_attached = VLC_TRUE;
180         }
181         else
182         {
183             p_new->p_libvlc = p_this->p_libvlc;
184         }
185
186         vlc_mutex_lock( &structure_lock );
187
188         p_libvlc_global->i_counter++;
189         p_new->i_object_id = p_libvlc_global->i_counter;
190
191         /* Wooohaa! If *this* fails, we're in serious trouble! Anyway it's
192          * useless to try and recover anything if pp_objects gets smashed. */
193         TAB_APPEND( p_libvlc_global->i_objects, p_libvlc_global->pp_objects,
194                     p_new );
195
196         vlc_mutex_unlock( &structure_lock );
197     }
198
199     p_priv->i_refcount = 1;
200     p_priv->pf_destructor = kVLCDestructor;
201     p_priv->b_thread = VLC_FALSE;
202     p_new->p_parent = NULL;
203     p_new->pp_children = NULL;
204     p_new->i_children = 0;
205
206     p_new->p_private = NULL;
207
208     /* Initialize mutexes and condvars */
209     vlc_mutex_init( p_new, &p_new->object_lock );
210     vlc_cond_init( p_new, &p_new->object_wait );
211     vlc_mutex_init( p_new, &p_priv->var_lock );
212     vlc_spin_init( &p_priv->spin );
213     p_priv->pipes[0] = p_priv->pipes[1] = -1;
214
215     if( i_type == VLC_OBJECT_GLOBAL )
216     {
217         vlc_mutex_init( p_new, &structure_lock );
218     }
219
220     if( i_type == VLC_OBJECT_LIBVLC )
221     {
222         var_Create( p_new, "list", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
223         var_AddCallback( p_new, "list", DumpCommand, NULL );
224         var_Create( p_new, "tree", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
225         var_AddCallback( p_new, "tree", DumpCommand, NULL );
226         var_Create( p_new, "vars", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
227         var_AddCallback( p_new, "vars", DumpCommand, NULL );
228     }
229
230     return p_new;
231 }
232
233
234 /**
235  * Allocates and initializes a vlc object.
236  *
237  * @param i_type known object type (all of them are negative integer values),
238  *               or object byte size (always positive).
239  *
240  * @return the new object, or NULL on error.
241  */
242 void * __vlc_object_create( vlc_object_t *p_this, int i_type )
243 {
244     const char   * psz_type;
245     size_t         i_size;
246
247     switch( i_type )
248     {
249         case VLC_OBJECT_GLOBAL:
250             i_size = sizeof(libvlc_global_data_t);
251             psz_type = "global";
252             break;
253         case VLC_OBJECT_LIBVLC:
254             i_size = sizeof(libvlc_int_t);
255             psz_type = "libvlc";
256             break;
257         case VLC_OBJECT_INTF:
258             i_size = sizeof(intf_thread_t);
259             psz_type = "interface";
260             break;
261         case VLC_OBJECT_DIALOGS:
262             i_size = sizeof(intf_thread_t);
263             psz_type = "dialogs";
264             break;
265         case VLC_OBJECT_PLAYLIST:
266             i_size = sizeof(playlist_t);
267             psz_type = "playlist";
268             break;
269         case VLC_OBJECT_SD:
270             i_size = sizeof(services_discovery_t);
271             psz_type = "services discovery";
272             break;
273         case VLC_OBJECT_INPUT:
274             i_size = sizeof(input_thread_t);
275             psz_type = "input";
276             break;
277         case VLC_OBJECT_DEMUX:
278             i_size = sizeof(demux_t);
279             psz_type = "demux";
280             break;
281         case VLC_OBJECT_ACCESS:
282             i_size = sizeof(access_t);
283             psz_type = "access";
284             break;
285         case VLC_OBJECT_DECODER:
286             i_size = sizeof(decoder_t);
287             psz_type = "decoder";
288             break;
289         case VLC_OBJECT_PACKETIZER:
290             i_size = sizeof(decoder_t);
291             psz_type = "packetizer";
292             break;
293         case VLC_OBJECT_ENCODER:
294             i_size = sizeof(encoder_t);
295             psz_type = "encoder";
296             break;
297         case VLC_OBJECT_FILTER:
298             i_size = sizeof(filter_t);
299             psz_type = "filter";
300             break;
301         case VLC_OBJECT_VOUT:
302             i_size = sizeof(vout_thread_t);
303             psz_type = "video output";
304             break;
305         case VLC_OBJECT_SPU:
306             i_size = sizeof(spu_t);
307             psz_type = "subpicture";
308             break;
309         case VLC_OBJECT_AOUT:
310             i_size = sizeof(aout_instance_t);
311             psz_type = "audio output";
312             break;
313         case VLC_OBJECT_SOUT:
314             i_size = sizeof(sout_instance_t);
315             psz_type = "stream output";
316             break;
317         case VLC_OBJECT_VLM:
318             i_size = sizeof( vlm_t );
319             psz_type = "vlm dameon";
320             break;
321         case VLC_OBJECT_XML:
322             i_size = sizeof( xml_t );
323             psz_type = "xml";
324             break;
325         case VLC_OBJECT_OPENGL:
326             i_size = sizeof( vout_thread_t );
327             psz_type = "opengl";
328             break;
329         case VLC_OBJECT_ANNOUNCE:
330             i_size = sizeof( announce_handler_t );
331             psz_type = "announce";
332             break;
333         case VLC_OBJECT_META_ENGINE:
334             i_size = sizeof( meta_engine_t );
335             psz_type = "meta engine";
336             break;
337         case VLC_OBJECT_OSDMENU:
338             i_size = sizeof( osd_menu_t );
339             psz_type = "osd menu";
340             break;
341         case VLC_OBJECT_INTERACTION:
342             i_size = sizeof( interaction_t );
343             psz_type = "interaction";
344             break;
345         default:
346             i_size = i_type > (int)sizeof(vlc_object_t)
347                          ? i_type : (int)sizeof(vlc_object_t);
348             i_type = VLC_OBJECT_GENERIC;
349             psz_type = "generic";
350             break;
351     }
352
353     return vlc_custom_create( p_this, i_size, i_type, psz_type );
354 }
355
356
357 /**
358  ****************************************************************************
359  * Set the destructor of a vlc object
360  *
361  * This function sets the destructor of the vlc object. It will be called
362  * when the object is destroyed when the its refcount reaches 0.
363  * (It is called by the internal function vlc_object_destroy())
364  *****************************************************************************/
365 void __vlc_object_set_destructor( vlc_object_t *p_this,
366                                   vlc_destructor_t pf_destructor )
367 {
368     vlc_object_internals_t *p_priv = vlc_internals(p_this );
369
370     vlc_mutex_lock( &structure_lock );
371     p_priv->pf_destructor = pf_destructor;
372     vlc_mutex_unlock( &structure_lock );
373 }
374
375 /**
376  ****************************************************************************
377  * Destroy a vlc object (Internal)
378  *
379  * This function destroys an object that has been previously allocated with
380  * vlc_object_create. The object's refcount must be zero and it must not be
381  * attached to other objects in any way.
382  *****************************************************************************/
383 static void vlc_object_destroy( vlc_object_t *p_this )
384 {
385     vlc_object_internals_t *p_priv = vlc_internals( p_this );
386
387     /* Automatically detach the object from its parents */
388     if( p_this->p_parent ) vlc_object_detach( p_this );
389
390
391     /* Send a kill to the object's thread if applicable */
392     vlc_object_kill( p_this );
393
394     /* If we are running on a thread, wait until it ends */
395     if( p_priv->b_thread )
396         vlc_thread_join( p_this );
397
398     /* Call the custom "subclass" destructor */
399     if( p_priv->pf_destructor )
400         p_priv->pf_destructor( p_this );
401
402     /* Sanity checks */
403     if( p_this->i_children )
404     {
405         int i;
406
407         fprintf( stderr,
408                  "ERROR: cannot delete object (%i, %s) with %d children\n",
409                  p_this->i_object_id, p_this->psz_object_name,
410                  p_this->i_children );
411
412         for( i = 0; i < p_this->i_children; i++ )
413         {
414             fprintf( stderr,
415                      "ERROR: Remaining children object "
416                      "(id:%i, type:%s, name:%s)\n",
417                      p_this->pp_children[i]->i_object_id,
418                      p_this->pp_children[i]->psz_object_type,
419                      p_this->pp_children[i]->psz_object_name );
420         }
421         fflush(stderr);
422         abort();
423     }
424
425     /* Destroy the associated variables, starting from the end so that
426      * no memmove calls have to be done. */
427     while( p_priv->i_vars )
428     {
429         var_Destroy( p_this, p_priv->p_vars[p_priv->i_vars - 1].psz_name );
430     }
431
432     free( p_priv->p_vars );
433     vlc_mutex_destroy( &p_priv->var_lock );
434
435     free( p_this->psz_header );
436
437     if( p_this->i_object_type == VLC_OBJECT_GLOBAL )
438     {
439         libvlc_global_data_t *p_global = (libvlc_global_data_t *)p_this;
440
441         /* Test for leaks */
442         if( p_global->i_objects > 0 )
443         {
444             int i;
445             for( i = 0; i < p_global->i_objects; i++ )
446             {
447                 /* We are leaking this object */
448                 fprintf( stderr,
449                          "ERROR: leaking object (id:%i, type:%s, name:%s)\n",
450                          p_global->pp_objects[i]->i_object_id,
451                          p_global->pp_objects[i]->psz_object_type,
452                          p_global->pp_objects[i]->psz_object_name );
453                 fflush(stderr);
454             }
455             /* Strongly abort, cause we want these to be fixed */
456             abort();
457         }
458
459         /* We are the global object ... no need to lock. */
460         free( p_global->pp_objects );
461         p_global->pp_objects = NULL;
462
463         vlc_mutex_destroy( &structure_lock );
464     }
465
466 #if defined(WIN32) || defined(UNDER_CE)
467     /* if object has an associated thread, close it now */
468     if( p_priv->thread_id.hThread )
469        CloseHandle(p_priv->thread_id.hThread);
470 #endif
471
472     vlc_mutex_destroy( &p_this->object_lock );
473     vlc_cond_destroy( &p_this->object_wait );
474     vlc_spin_destroy( &p_priv->spin );
475     if( p_priv->pipes[1] != -1 )
476         close( p_priv->pipes[1] );
477     if( p_priv->pipes[0] != -1 )
478         close( p_priv->pipes[0] );
479
480     /* global is not dynamically allocated by vlc_object_create */
481     if( p_this->i_object_type != VLC_OBJECT_GLOBAL )
482         free( p_priv );
483 }
484
485
486 /** Inter-object signaling */
487
488 void __vlc_object_lock( vlc_object_t *obj )
489 {
490     vlc_mutex_lock( &obj->object_lock );
491 }
492
493 void __vlc_object_unlock( vlc_object_t *obj )
494 {
495     vlc_assert_locked( &obj->object_lock );
496     vlc_mutex_unlock( &obj->object_lock );
497 }
498
499 #ifdef WIN32
500 # include <winsock2.h>
501 # include <ws2tcpip.h>
502
503 /**
504  * select()-able pipes emulated using Winsock
505  */
506 static int pipe (int fd[2])
507 {
508     SOCKADDR_IN addr;
509     int addrlen = sizeof (addr);
510
511     SOCKET l = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP), a,
512            c = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
513     if ((l == INVALID_SOCKET) || (c == INVALID_SOCKET))
514         goto error;
515
516     memset (&addr, 0, sizeof (addr));
517     addr.sin_family = AF_INET;
518     addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
519     if (bind (l, (PSOCKADDR)&addr, sizeof (addr))
520      || getsockname (l, (PSOCKADDR)&addr, &addrlen)
521      || listen (l, 1)
522      || connect (c, (PSOCKADDR)&addr, addrlen))
523         goto error;
524
525     a = accept (l, NULL, NULL);
526     if (a == INVALID_SOCKET)
527         goto error;
528
529     closesocket (l);
530     //shutdown (a, 0);
531     //shutdown (c, 1);
532     fd[0] = c;
533     fd[1] = a;
534     return 0;
535
536 error:
537     if (l != INVALID_SOCKET)
538         closesocket (l);
539     if (c != INVALID_SOCKET)
540         closesocket (c);
541     return -1;
542 }
543
544 #undef  read
545 #define read( a, b, c )  recv (a, b, c, 0)
546 #undef  write
547 #define write( a, b, c ) send (a, b, c, 0)
548 #undef  close
549 #define close( a )       closesocket (a)
550 #endif /* WIN32 */
551
552 /**
553  * Returns the readable end of a pipe that becomes readable once termination
554  * of the object is requested (vlc_object_kill()).
555  * This can be used to wake-up out of a select() or poll() event loop, such
556  * typically when doing network I/O.
557  *
558  * Note that the pipe will remain the same for the lifetime of the object.
559  * DO NOT read the pipe nor close it yourself. Ever.
560  *
561  * @param obj object that would be "killed"
562  * @return a readable pipe descriptor, or -1 on error.
563  */
564 int __vlc_object_waitpipe( vlc_object_t *obj )
565 {
566     int pfd[2] = { -1, -1 };
567     struct vlc_object_internals_t *internals = obj->p_internals;
568     vlc_bool_t killed = VLC_FALSE;
569
570     vlc_spin_lock (&internals->spin);
571     if (internals->pipes[0] == -1)
572     {
573         /* This can only ever happen if someone killed us without locking: */
574         assert (internals->pipes[1] == -1);
575         vlc_spin_unlock (&internals->spin);
576
577         if (pipe (pfd))
578             return -1;
579
580         vlc_spin_lock (&internals->spin);
581         if (internals->pipes[0] == -1)
582         {
583             internals->pipes[0] = pfd[0];
584             internals->pipes[1] = pfd[1];
585             pfd[0] = pfd[1] = -1;
586         }
587         killed = obj->b_die;
588     }
589     vlc_spin_unlock (&internals->spin);
590
591     if (killed)
592     {
593         /* Race condition: vlc_object_kill() already invoked! */
594         int fd;
595
596         vlc_spin_lock (&internals->spin);
597         fd = internals->pipes[1];
598         internals->pipes[1] = -1;
599         vlc_spin_unlock (&internals->spin);
600
601         msg_Dbg (obj, "waitpipe: object already dying");
602         if (fd != -1)
603             close (fd);
604     }
605
606     /* Race condition: two threads call pipe() - unlikely */
607     if (pfd[0] != -1)
608         close (pfd[0]);
609     if (pfd[1] != -1)
610         close (pfd[1]);
611
612     return internals->pipes[0];
613 }
614
615
616 /**
617  * Waits for the object to be signaled (using vlc_object_signal()).
618  * If the object already has a signal pending, this function will return
619  * immediately. It is asserted that the caller holds the object lock.
620  *
621  * @return true if the object is dying and should terminate.
622  */
623 vlc_bool_t __vlc_object_wait( vlc_object_t *obj )
624 {
625     vlc_assert_locked( &obj->object_lock );
626     vlc_cond_wait( &obj->object_wait, &obj->object_lock );
627     return obj->b_die;
628 }
629
630
631 /**
632  * Waits for the object to be signaled (using vlc_object_signal()), or for
633  * a timer to expire.
634  * If the object already has a signal pending, this function will return
635  * immediately. It is asserted that the caller holds the object lock.
636  *
637  * @return negative if the object is dying and should terminate,
638  * positive if the the object has been signaled but is not dying,
639  * 0 if timeout has been reached.
640  */
641 int __vlc_object_timedwait( vlc_object_t *obj, mtime_t deadline )
642 {
643     int v;
644
645     vlc_assert_locked( &obj->object_lock );
646     v = vlc_cond_timedwait( &obj->object_wait, &obj->object_lock, deadline );
647     if( v == 0 ) /* signaled */
648         return obj->b_die ? -1 : 1;
649     return 0;
650 }
651
652
653 /**
654  * Checks whether an object has been "killed".
655  * The object lock must be held.
656  *
657  * Typical code for an object thread could be:
658  *
659    vlc_object_lock (self);
660    ...initialization...
661    while (vlc_object_alive (self))
662    {
663        ...preprocessing...
664
665        if (vlc_object_wait (self))
666            continue;
667
668        ...postprocessing...
669    }
670    ...deinitialization...
671    vlc_object_unlock (self);
672  *
673  *
674  * @return true iff the object has not been killed yet
675  */
676 vlc_bool_t __vlc_object_alive( vlc_object_t *obj )
677 {
678     vlc_assert_locked( &obj->object_lock );
679     return !obj->b_die;
680 }
681
682
683 /**
684  * Signals an object for which the lock is held.
685  */
686 void __vlc_object_signal_unlocked( vlc_object_t *obj )
687 {
688     vlc_assert_locked (&obj->object_lock);
689     vlc_cond_signal( &obj->object_wait );
690 }
691
692
693 /**
694  * Requests termination of an object.
695  * If the object is LibVLC, also request to terminate all its children.
696  */
697 void __vlc_object_kill( vlc_object_t *p_this )
698 {
699     struct vlc_object_internals_t *internals = p_this->p_internals;
700     int fd;
701
702     vlc_mutex_lock( &p_this->object_lock );
703     p_this->b_die = VLC_TRUE;
704
705     vlc_spin_lock (&internals->spin);
706     fd = internals->pipes[1];
707     internals->pipes[1] = -1;
708     vlc_spin_unlock (&internals->spin);
709
710     if( fd != -1 )
711     {
712         msg_Dbg (p_this, "waitpipe: object killed");
713         close (fd);
714     }
715
716     if( p_this->i_object_type == VLC_OBJECT_LIBVLC )
717         for( int i = 0; i < p_this->i_children ; i++ )
718             vlc_object_kill( p_this->pp_children[i] );
719
720     vlc_object_signal_unlocked( p_this );
721     vlc_mutex_unlock( &p_this->object_lock );
722 }
723
724
725 /**
726  * find an object given its ID
727  *
728  * This function looks for the object whose i_object_id field is i_id. We
729  * use a dichotomy so that lookups are in log2(n).
730  *****************************************************************************/
731 void * vlc_object_get( int i_id )
732 {
733     int i_max, i_middle;
734     vlc_object_t **pp_objects;
735     libvlc_global_data_t *p_libvlc_global = vlc_global();
736
737     vlc_mutex_lock( &structure_lock );
738
739     pp_objects = p_libvlc_global->pp_objects;
740
741     /* Perform our dichotomy */
742     for( i_max = p_libvlc_global->i_objects - 1 ; ; )
743     {
744         i_middle = i_max / 2;
745
746         if( pp_objects[i_middle]->i_object_id > i_id )
747         {
748             i_max = i_middle;
749         }
750         else if( pp_objects[i_middle]->i_object_id < i_id )
751         {
752             if( i_middle )
753             {
754                 pp_objects += i_middle;
755                 i_max -= i_middle;
756             }
757             else
758             {
759                 /* This happens when there are only two remaining objects */
760                 if( pp_objects[i_middle+1]->i_object_id == i_id
761                     && pp_objects[i_middle+1]->p_internals->i_refcount > 0 )
762                 {
763                     vlc_object_yield_locked( pp_objects[i_middle+1] );
764                     vlc_mutex_unlock( &structure_lock );
765                     return pp_objects[i_middle+1];
766                 }
767                 break;
768             }
769         }
770         else if( pp_objects[i_middle]->p_internals->i_refcount > 0 )
771         {
772             vlc_object_yield_locked( pp_objects[i_middle] );
773             vlc_mutex_unlock( &structure_lock );
774             return pp_objects[i_middle];
775         }
776
777         if( i_max == 0 )
778         {
779             /* this means that i_max == i_middle, and since we have already
780              * tested pp_objects[i_middle]), p_found is properly set. */
781             break;
782         }
783     }
784
785     vlc_mutex_unlock( &structure_lock );
786     return NULL;
787 }
788
789 /**
790  ****************************************************************************
791  * find a typed object and increment its refcount
792  *****************************************************************************
793  * This function recursively looks for a given object type. i_mode can be one
794  * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
795  *****************************************************************************/
796 void * __vlc_object_find( vlc_object_t *p_this, int i_type, int i_mode )
797 {
798     vlc_object_t *p_found;
799
800     vlc_mutex_lock( &structure_lock );
801
802     /* If we are of the requested type ourselves, don't look further */
803     if( !(i_mode & FIND_STRICT) && p_this->i_object_type == i_type
804         && p_this->p_internals->i_refcount > 0 )
805     {
806         vlc_object_yield_locked( p_this );
807         vlc_mutex_unlock( &structure_lock );
808         return p_this;
809     }
810
811     /* Otherwise, recursively look for the object */
812     if( (i_mode & 0x000f) == FIND_ANYWHERE )
813     {
814         vlc_object_t *p_root = p_this;
815
816         /* Find the root */
817         while( p_root->p_parent != NULL &&
818                p_root != VLC_OBJECT( p_this->p_libvlc ) )
819         {
820             p_root = p_root->p_parent;
821         }
822
823         p_found = FindObject( p_root, i_type, (i_mode & ~0x000f)|FIND_CHILD );
824         if( p_found == NULL && p_root != VLC_OBJECT( p_this->p_libvlc ) )
825         {
826             p_found = FindObject( VLC_OBJECT( p_this->p_libvlc ),
827                                   i_type, (i_mode & ~0x000f)|FIND_CHILD );
828         }
829     }
830     else
831     {
832         p_found = FindObject( p_this, i_type, i_mode );
833     }
834
835     vlc_mutex_unlock( &structure_lock );
836
837     return p_found;
838 }
839
840 /**
841  ****************************************************************************
842  * find a named object and increment its refcount
843  *****************************************************************************
844  * This function recursively looks for a given object name. i_mode can be one
845  * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
846  *****************************************************************************/
847 void * __vlc_object_find_name( vlc_object_t *p_this, const char *psz_name,
848                                int i_mode )
849 {
850     vlc_object_t *p_found;
851
852     vlc_mutex_lock( &structure_lock );
853
854     /* Avoid obvious freed object uses */
855     assert( p_this->p_internals->i_refcount > 0 );
856
857     /* If have the requested name ourselves, don't look further */
858     if( !(i_mode & FIND_STRICT)
859         && p_this->psz_object_name
860         && !strcmp( p_this->psz_object_name, psz_name )
861         && p_this->p_internals->i_refcount > 0 )
862     {
863         vlc_object_yield_locked( p_this );
864         vlc_mutex_unlock( &structure_lock );
865         return p_this;
866     }
867
868     /* Otherwise, recursively look for the object */
869     if( (i_mode & 0x000f) == FIND_ANYWHERE )
870     {
871         vlc_object_t *p_root = p_this;
872
873         /* Find the root */
874         while( p_root->p_parent != NULL &&
875                p_root != VLC_OBJECT( p_this->p_libvlc ) )
876         {
877             p_root = p_root->p_parent;
878         }
879
880         p_found = FindObjectName( p_root, psz_name,
881                                  (i_mode & ~0x000f)|FIND_CHILD );
882         if( p_found == NULL && p_root != VLC_OBJECT( p_this->p_libvlc ) )
883         {
884             p_found = FindObjectName( VLC_OBJECT( p_this->p_libvlc ),
885                                       psz_name, (i_mode & ~0x000f)|FIND_CHILD );
886         }
887     }
888     else
889     {
890         p_found = FindObjectName( p_this, psz_name, i_mode );
891     }
892
893     vlc_mutex_unlock( &structure_lock );
894
895     return p_found;
896 }
897
898 /**
899  ****************************************************************************
900  * increment an object refcount
901  *****************************************************************************/
902
903 /* When the structure_lock is locked */
904 static void vlc_object_yield_locked( vlc_object_t *p_this )
905 {
906     vlc_assert_locked (&structure_lock);
907
908     /* Avoid obvious freed object uses */
909     assert( p_this->p_internals->i_refcount > 0 );
910
911     /* Increment the counter */
912     p_this->p_internals->i_refcount++;
913 }
914
915 /* Public function */
916 void __vlc_object_yield( vlc_object_t *p_this )
917 {
918     vlc_mutex_lock( &structure_lock );
919     vlc_object_yield_locked( p_this );
920     vlc_mutex_unlock( &structure_lock );
921 }
922
923
924 /*****************************************************************************
925  * decrement an object refcount
926  * And destroy the object if its refcount reach zero.
927  *****************************************************************************/
928 void __vlc_object_release( vlc_object_t *p_this )
929 {
930     vlc_bool_t b_should_destroy;
931
932     vlc_mutex_lock( &structure_lock );
933
934     assert( p_this->p_internals->i_refcount > 0 );
935     p_this->p_internals->i_refcount--;
936     b_should_destroy = (p_this->p_internals->i_refcount == 0);
937
938     if( b_should_destroy )
939     {
940         /* Make sure this object can't be obtained via vlc_find_object now that
941          * it is freed */
942         libvlc_global_data_t *p_libvlc_global = vlc_global();
943         int i_index;
944
945         /* Wooohaa! If *this* fails, we're in serious trouble! Anyway it's
946          * useless to try and recover anything if pp_objects gets smashed. */
947         i_index = FindIndex( p_this, p_libvlc_global->pp_objects,
948                              p_libvlc_global->i_objects );
949         REMOVE_ELEM( p_libvlc_global->pp_objects,
950                      p_libvlc_global->i_objects, i_index );
951     }
952
953     vlc_mutex_unlock( &structure_lock );
954
955     if( b_should_destroy )
956         vlc_object_destroy( p_this );
957 }
958
959 /**
960  ****************************************************************************
961  * attach object to a parent object
962  *****************************************************************************
963  * This function sets p_this as a child of p_parent, and p_parent as a parent
964  * of p_this. This link can be undone using vlc_object_detach.
965  *****************************************************************************/
966 void __vlc_object_attach( vlc_object_t *p_this, vlc_object_t *p_parent )
967 {
968     if( !p_this ) return;
969
970     vlc_mutex_lock( &structure_lock );
971
972     /* Avoid obvious freed object uses */
973     assert( p_this->p_internals->i_refcount > 0 );
974
975     /* Attach the parent to its child */
976     p_this->p_parent = p_parent;
977
978     /* Attach the child to its parent */
979     INSERT_ELEM( p_parent->pp_children, p_parent->i_children,
980                  p_parent->i_children, p_this );
981
982     /* Climb up the tree to see whether we are connected with the root */
983     if( p_parent->p_internals->b_attached )
984     {
985         SetAttachment( p_this, VLC_TRUE );
986     }
987
988     vlc_mutex_unlock( &structure_lock );
989 }
990
991 /**
992  ****************************************************************************
993  * detach object from its parent
994  *****************************************************************************
995  * This function removes all links between an object and its parent.
996  *****************************************************************************/
997 void __vlc_object_detach( vlc_object_t *p_this )
998 {
999     if( !p_this ) return;
1000
1001     vlc_mutex_lock( &structure_lock );
1002
1003     if( !p_this->p_parent )
1004     {
1005         msg_Err( p_this, "object is not attached" );
1006         vlc_mutex_unlock( &structure_lock );
1007         return;
1008     }
1009
1010     /* Climb up the tree to see whether we are connected with the root */
1011     if( p_this->p_parent->p_internals->b_attached )
1012     {
1013         SetAttachment( p_this, VLC_FALSE );
1014     }
1015
1016     DetachObject( p_this );
1017     vlc_mutex_unlock( &structure_lock );
1018     p_this = NULL;
1019 }
1020
1021 /**
1022  ****************************************************************************
1023  * find a list typed objects and increment their refcount
1024  *****************************************************************************
1025  * This function recursively looks for a given object type. i_mode can be one
1026  * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
1027  *****************************************************************************/
1028 vlc_list_t * __vlc_list_find( vlc_object_t *p_this, int i_type, int i_mode )
1029 {
1030     vlc_list_t *p_list;
1031     vlc_object_t **pp_current, **pp_end;
1032     int i_count = 0, i_index = 0;
1033     libvlc_global_data_t *p_libvlc_global = vlc_global();
1034
1035     vlc_mutex_lock( &structure_lock );
1036
1037     /* Look for the objects */
1038     switch( i_mode & 0x000f )
1039     {
1040     case FIND_ANYWHERE:
1041         pp_current = p_libvlc_global->pp_objects;
1042         pp_end = pp_current + p_libvlc_global->i_objects;
1043
1044         for( ; pp_current < pp_end ; pp_current++ )
1045         {
1046             if( (*pp_current)->p_internals->b_attached
1047                  && (*pp_current)->i_object_type == i_type )
1048             {
1049                 i_count++;
1050             }
1051         }
1052
1053         p_list = NewList( i_count );
1054         pp_current = p_libvlc_global->pp_objects;
1055
1056         for( ; pp_current < pp_end ; pp_current++ )
1057         {
1058             if( (*pp_current)->p_internals->b_attached
1059                  && (*pp_current)->i_object_type == i_type )
1060             {
1061                 ListReplace( p_list, *pp_current, i_index );
1062                 if( i_index < i_count ) i_index++;
1063             }
1064         }
1065     break;
1066
1067     case FIND_CHILD:
1068         i_count = CountChildren( p_this, i_type );
1069         p_list = NewList( i_count );
1070
1071         /* Check allocation was successful */
1072         if( p_list->i_count != i_count )
1073         {
1074             msg_Err( p_this, "list allocation failed!" );
1075             p_list->i_count = 0;
1076             break;
1077         }
1078
1079         p_list->i_count = 0;
1080         ListChildren( p_list, p_this, i_type );
1081         break;
1082
1083     default:
1084         msg_Err( p_this, "unimplemented!" );
1085         p_list = NewList( 0 );
1086         break;
1087     }
1088
1089     vlc_mutex_unlock( &structure_lock );
1090
1091     return p_list;
1092 }
1093
1094 /*****************************************************************************
1095  * DumpCommand: print the current vlc structure
1096  *****************************************************************************
1097  * This function prints either an ASCII tree showing the connections between
1098  * vlc objects, and additional information such as their refcount, thread ID,
1099  * etc. (command "tree"), or the same data as a simple list (command "list").
1100  *****************************************************************************/
1101 static int DumpCommand( vlc_object_t *p_this, char const *psz_cmd,
1102                         vlc_value_t oldval, vlc_value_t newval, void *p_data )
1103 {
1104     libvlc_global_data_t *p_libvlc_global = vlc_global();
1105
1106     (void)oldval; (void)p_data;
1107     if( *psz_cmd == 'l' )
1108     {
1109         vlc_mutex_lock( &structure_lock );
1110
1111         vlc_object_t **pp_current, **pp_end;
1112
1113         pp_current = p_libvlc_global->pp_objects;
1114         pp_end = pp_current + p_libvlc_global->i_objects;
1115
1116         for( ; pp_current < pp_end ; pp_current++ )
1117         {
1118             if( (*pp_current)->p_internals->b_attached )
1119             {
1120                 PrintObject( *pp_current, "" );
1121             }
1122             else
1123             {
1124                 printf( " o %.8i %s (not attached)\n",
1125                         (*pp_current)->i_object_id,
1126                         (*pp_current)->psz_object_type );
1127             }
1128         }
1129
1130         vlc_mutex_unlock( &structure_lock );
1131     }
1132     else
1133     {
1134         vlc_object_t *p_object = NULL;
1135
1136         if( *newval.psz_string )
1137         {
1138             char *end;
1139             int i_id = strtol( newval.psz_string, &end, 0 );
1140             if( end != newval.psz_string )
1141                 p_object = vlc_object_get( i_id );
1142             else
1143             {
1144                 /* try using the object's name to find it */
1145                 vlc_object_t *p_libvlc = vlc_object_get( 1 );
1146                 if( p_libvlc )
1147                 {
1148                     /* Look in p_libvlc's children tree */
1149                     p_object = vlc_object_find_name( p_libvlc,
1150                                                      newval.psz_string,
1151                                                      FIND_CHILD );
1152                     vlc_object_release( p_libvlc );
1153                 }
1154                 if( !p_object )
1155                 {
1156                     /* If it's not in libvlc, look in libvlc_global (== p_this) */
1157                     p_object = vlc_object_find_name( p_this,
1158                                                      newval.psz_string,
1159                                                      FIND_CHILD );
1160                 }
1161             }
1162
1163             if( !p_object )
1164             {
1165                 return VLC_ENOOBJ;
1166             }
1167         }
1168
1169         vlc_mutex_lock( &structure_lock );
1170
1171         if( *psz_cmd == 't' )
1172         {
1173             char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
1174
1175             if( !p_object )
1176                 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
1177
1178             psz_foo[0] = '|';
1179             DumpStructure( p_object, 0, psz_foo );
1180         }
1181         else if( *psz_cmd == 'v' )
1182         {
1183             int i;
1184
1185             if( !p_object )
1186                 p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
1187
1188             PrintObject( p_object, "" );
1189
1190             if( !p_object->p_internals->i_vars )
1191                 printf( " `-o No variables\n" );
1192             for( i = 0; i < p_object->p_internals->i_vars; i++ )
1193             {
1194                 variable_t *p_var = p_object->p_internals->p_vars + i;
1195
1196                 const char *psz_type = "unknown";
1197                 switch( p_var->i_type & VLC_VAR_TYPE )
1198                 {
1199 #define MYCASE( type, nice )                \
1200                     case VLC_VAR_ ## type:  \
1201                         psz_type = nice;    \
1202                         break;
1203                     MYCASE( VOID, "void" );
1204                     MYCASE( BOOL, "bool" );
1205                     MYCASE( INTEGER, "integer" );
1206                     MYCASE( HOTKEY, "hotkey" );
1207                     MYCASE( STRING, "string" );
1208                     MYCASE( MODULE, "module" );
1209                     MYCASE( FILE, "file" );
1210                     MYCASE( DIRECTORY, "directory" );
1211                     MYCASE( VARIABLE, "variable" );
1212                     MYCASE( FLOAT, "float" );
1213                     MYCASE( TIME, "time" );
1214                     MYCASE( ADDRESS, "address" );
1215                     MYCASE( MUTEX, "mutex" );
1216                     MYCASE( LIST, "list" );
1217 #undef MYCASE
1218                 }
1219                 printf( " %c-o \"%s\" (%s",
1220                         i + 1 == p_object->p_internals->i_vars ? '`' : '|',
1221                         p_var->psz_name, psz_type );
1222                 if( p_var->psz_text )
1223                     printf( ", %s", p_var->psz_text );
1224                 printf( ")" );
1225                 if( p_var->i_type & VLC_VAR_ISCOMMAND )
1226                     printf( ", command" );
1227                 if( p_var->i_entries )
1228                     printf( ", %d callbacks", p_var->i_entries );
1229                 switch( p_var->i_type & 0x00f0 )
1230                 {
1231                     case VLC_VAR_VOID:
1232                     case VLC_VAR_MUTEX:
1233                         break;
1234                     case VLC_VAR_BOOL:
1235                         printf( ": %s", p_var->val.b_bool ? "true" : "false" );
1236                         break;
1237                     case VLC_VAR_INTEGER:
1238                         printf( ": %d", p_var->val.i_int );
1239                         break;
1240                     case VLC_VAR_STRING:
1241                         printf( ": \"%s\"", p_var->val.psz_string );
1242                         break;
1243                     case VLC_VAR_FLOAT:
1244                         printf( ": %f", p_var->val.f_float );
1245                         break;
1246                     case VLC_VAR_TIME:
1247                         printf( ": " I64Fi, (int64_t)p_var->val.i_time );
1248                         break;
1249                     case VLC_VAR_ADDRESS:
1250                         printf( ": %p", p_var->val.p_address );
1251                         break;
1252                     case VLC_VAR_LIST:
1253                         printf( ": TODO" );
1254                         break;
1255                 }
1256                 printf( "\n" );
1257             }
1258         }
1259
1260         vlc_mutex_unlock( &structure_lock );
1261
1262         if( *newval.psz_string )
1263         {
1264             vlc_object_release( p_object );
1265         }
1266     }
1267
1268     return VLC_SUCCESS;
1269 }
1270
1271 /*****************************************************************************
1272  * vlc_list_release: free a list previously allocated by vlc_list_find
1273  *****************************************************************************
1274  * This function decreases the refcount of all objects in the list and
1275  * frees the list.
1276  *****************************************************************************/
1277 void vlc_list_release( vlc_list_t *p_list )
1278 {
1279     int i_index;
1280
1281     for( i_index = 0; i_index < p_list->i_count; i_index++ )
1282     {
1283         vlc_object_release( p_list->p_values[i_index].p_object );
1284     }
1285
1286     free( p_list->p_values );
1287     free( p_list );
1288 }
1289
1290 /* Following functions are local */
1291
1292 /*****************************************************************************
1293  * FindIndex: find the index of an object in an array of objects
1294  *****************************************************************************
1295  * This function assumes that p_this can be found in pp_objects. It will not
1296  * crash if p_this cannot be found, but will return a wrong value. It is your
1297  * duty to check the return value if you are not certain that the object could
1298  * be found for sure.
1299  *****************************************************************************/
1300 static int FindIndex( vlc_object_t *p_this,
1301                       vlc_object_t **pp_objects, int i_count )
1302 {
1303     int i_middle = i_count / 2;
1304
1305     if( i_count == 0 )
1306     {
1307         return 0;
1308     }
1309
1310     if( pp_objects[i_middle] == p_this )
1311     {
1312         return i_middle;
1313     }
1314
1315     if( i_count == 1 )
1316     {
1317         return 0;
1318     }
1319
1320     /* We take advantage of the sorted array */
1321     if( pp_objects[i_middle]->i_object_id < p_this->i_object_id )
1322     {
1323         return i_middle + FindIndex( p_this, pp_objects + i_middle,
1324                                              i_count - i_middle );
1325     }
1326     else
1327     {
1328         return FindIndex( p_this, pp_objects, i_middle );
1329     }
1330 }
1331
1332 static vlc_object_t * FindObject( vlc_object_t *p_this, int i_type, int i_mode )
1333 {
1334     int i;
1335     vlc_object_t *p_tmp;
1336
1337     switch( i_mode & 0x000f )
1338     {
1339     case FIND_PARENT:
1340         p_tmp = p_this->p_parent;
1341         if( p_tmp )
1342         {
1343             if( p_tmp->i_object_type == i_type
1344                 && p_tmp->p_internals->i_refcount > 0 )
1345             {
1346                 vlc_object_yield_locked( p_tmp );
1347                 return p_tmp;
1348             }
1349             else
1350             {
1351                 return FindObject( p_tmp, i_type, i_mode );
1352             }
1353         }
1354         break;
1355
1356     case FIND_CHILD:
1357         for( i = p_this->i_children; i--; )
1358         {
1359             p_tmp = p_this->pp_children[i];
1360             if( p_tmp->i_object_type == i_type
1361                 && p_tmp->p_internals->i_refcount > 0 )
1362             {
1363                 vlc_object_yield_locked( p_tmp );
1364                 return p_tmp;
1365             }
1366             else if( p_tmp->i_children )
1367             {
1368                 p_tmp = FindObject( p_tmp, i_type, i_mode );
1369                 if( p_tmp )
1370                 {
1371                     return p_tmp;
1372                 }
1373             }
1374         }
1375         break;
1376
1377     case FIND_ANYWHERE:
1378         /* Handled in vlc_object_find */
1379         break;
1380     }
1381
1382     return NULL;
1383 }
1384
1385 static vlc_object_t * FindObjectName( vlc_object_t *p_this,
1386                                       const char *psz_name,
1387                                       int i_mode )
1388 {
1389     int i;
1390     vlc_object_t *p_tmp;
1391
1392     switch( i_mode & 0x000f )
1393     {
1394     case FIND_PARENT:
1395         p_tmp = p_this->p_parent;
1396         if( p_tmp )
1397         {
1398             if( p_tmp->psz_object_name
1399                 && !strcmp( p_tmp->psz_object_name, psz_name )
1400                 && p_tmp->p_internals->i_refcount > 0 )
1401             {
1402                 vlc_object_yield_locked( p_tmp );
1403                 return p_tmp;
1404             }
1405             else
1406             {
1407                 return FindObjectName( p_tmp, psz_name, i_mode );
1408             }
1409         }
1410         break;
1411
1412     case FIND_CHILD:
1413         for( i = p_this->i_children; i--; )
1414         {
1415             p_tmp = p_this->pp_children[i];
1416             if( p_tmp->psz_object_name
1417                 && !strcmp( p_tmp->psz_object_name, psz_name )
1418                 && p_tmp->p_internals->i_refcount > 0 )
1419             {
1420                 vlc_object_yield_locked( p_tmp );
1421                 return p_tmp;
1422             }
1423             else if( p_tmp->i_children )
1424             {
1425                 p_tmp = FindObjectName( p_tmp, psz_name, i_mode );
1426                 if( p_tmp )
1427                 {
1428                     return p_tmp;
1429                 }
1430             }
1431         }
1432         break;
1433
1434     case FIND_ANYWHERE:
1435         /* Handled in vlc_object_find */
1436         break;
1437     }
1438
1439     return NULL;
1440 }
1441
1442 static void DetachObject( vlc_object_t *p_this )
1443 {
1444     vlc_object_t *p_parent = p_this->p_parent;
1445     int i_index, i;
1446
1447     /* Remove p_this's parent */
1448     p_this->p_parent = NULL;
1449
1450     /* Remove all of p_parent's children which are p_this */
1451     for( i_index = p_parent->i_children ; i_index-- ; )
1452     {
1453         if( p_parent->pp_children[i_index] == p_this )
1454         {
1455             p_parent->i_children--;
1456             for( i = i_index ; i < p_parent->i_children ; i++ )
1457             {
1458                 p_parent->pp_children[i] = p_parent->pp_children[i+1];
1459             }
1460         }
1461     }
1462
1463     if( p_parent->i_children )
1464     {
1465         p_parent->pp_children = (vlc_object_t **)realloc( p_parent->pp_children,
1466                                p_parent->i_children * sizeof(vlc_object_t *) );
1467     }
1468     else
1469     {
1470         free( p_parent->pp_children );
1471         p_parent->pp_children = NULL;
1472     }
1473 }
1474
1475 /*****************************************************************************
1476  * SetAttachment: recursively set the b_attached flag of a subtree.
1477  *****************************************************************************
1478  * This function is used by the attach and detach functions to propagate
1479  * the b_attached flag in a subtree.
1480  *****************************************************************************/
1481 static void SetAttachment( vlc_object_t *p_this, vlc_bool_t b_attached )
1482 {
1483     int i_index;
1484
1485     for( i_index = p_this->i_children ; i_index-- ; )
1486     {
1487         SetAttachment( p_this->pp_children[i_index], b_attached );
1488     }
1489
1490     p_this->p_internals->b_attached = b_attached;
1491 }
1492
1493 static void PrintObject( vlc_object_t *p_this, const char *psz_prefix )
1494 {
1495     char psz_children[20], psz_refcount[20], psz_thread[30], psz_name[50],
1496          psz_parent[20];
1497
1498     psz_name[0] = '\0';
1499     if( p_this->psz_object_name )
1500     {
1501         snprintf( psz_name, 49, " \"%s\"", p_this->psz_object_name );
1502         if( psz_name[48] )
1503             psz_name[48] = '\"';
1504     }
1505
1506     psz_children[0] = '\0';
1507     switch( p_this->i_children )
1508     {
1509         case 0:
1510             break;
1511         case 1:
1512             strcpy( psz_children, ", 1 child" );
1513             break;
1514         default:
1515             snprintf( psz_children, 19, ", %i children", p_this->i_children );
1516             break;
1517     }
1518
1519     psz_refcount[0] = '\0';
1520     if( p_this->p_internals->i_refcount > 0 )
1521         snprintf( psz_refcount, 19, ", refcount %u",
1522                   p_this->p_internals->i_refcount );
1523
1524     psz_thread[0] = '\0';
1525     if( p_this->p_internals->b_thread )
1526         snprintf( psz_thread, 29, " (thread %u)",
1527 #if defined(WIN32) || defined(UNDER_CE)
1528                   (unsigned)p_this->p_internals->thread_id.id );
1529 #else
1530                   (unsigned)p_this->p_internals->thread_id );
1531 #endif
1532
1533     psz_parent[0] = '\0';
1534     if( p_this->p_parent )
1535         snprintf( psz_parent, 19, ", parent %i", p_this->p_parent->i_object_id );
1536
1537     printf( " %so %.8i %s%s%s%s%s%s\n", psz_prefix,
1538             p_this->i_object_id, p_this->psz_object_type,
1539             psz_name, psz_thread, psz_refcount, psz_children,
1540             psz_parent );
1541 }
1542
1543 static void DumpStructure( vlc_object_t *p_this, int i_level, char *psz_foo )
1544 {
1545     int i;
1546     char i_back = psz_foo[i_level];
1547     psz_foo[i_level] = '\0';
1548
1549     PrintObject( p_this, psz_foo );
1550
1551     psz_foo[i_level] = i_back;
1552
1553     if( i_level / 2 >= MAX_DUMPSTRUCTURE_DEPTH )
1554     {
1555         msg_Warn( p_this, "structure tree is too deep" );
1556         return;
1557     }
1558
1559     for( i = 0 ; i < p_this->i_children ; i++ )
1560     {
1561         if( i_level )
1562         {
1563             psz_foo[i_level-1] = ' ';
1564
1565             if( psz_foo[i_level-2] == '`' )
1566             {
1567                 psz_foo[i_level-2] = ' ';
1568             }
1569         }
1570
1571         if( i == p_this->i_children - 1 )
1572         {
1573             psz_foo[i_level] = '`';
1574         }
1575         else
1576         {
1577             psz_foo[i_level] = '|';
1578         }
1579
1580         psz_foo[i_level+1] = '-';
1581         psz_foo[i_level+2] = '\0';
1582
1583         DumpStructure( p_this->pp_children[i], i_level + 2, psz_foo );
1584     }
1585 }
1586
1587 static vlc_list_t * NewList( int i_count )
1588 {
1589     vlc_list_t * p_list = (vlc_list_t *)malloc( sizeof( vlc_list_t ) );
1590     if( p_list == NULL )
1591     {
1592         return NULL;
1593     }
1594
1595     p_list->i_count = i_count;
1596
1597     if( i_count == 0 )
1598     {
1599         p_list->p_values = NULL;
1600         return p_list;
1601     }
1602
1603     p_list->p_values = malloc( i_count * sizeof( vlc_value_t ) );
1604     if( p_list->p_values == NULL )
1605     {
1606         p_list->i_count = 0;
1607         return p_list;
1608     }
1609
1610     return p_list;
1611 }
1612
1613 static void ListReplace( vlc_list_t *p_list, vlc_object_t *p_object,
1614                          int i_index )
1615 {
1616     if( p_list == NULL || i_index >= p_list->i_count )
1617     {
1618         return;
1619     }
1620
1621     vlc_object_yield_locked( p_object );
1622
1623     p_list->p_values[i_index].p_object = p_object;
1624
1625     return;
1626 }
1627
1628 /*static void ListAppend( vlc_list_t *p_list, vlc_object_t *p_object )
1629 {
1630     if( p_list == NULL )
1631     {
1632         return;
1633     }
1634
1635     p_list->p_values = realloc( p_list->p_values, (p_list->i_count + 1)
1636                                 * sizeof( vlc_value_t ) );
1637     if( p_list->p_values == NULL )
1638     {
1639         p_list->i_count = 0;
1640         return;
1641     }
1642
1643     vlc_object_yield_locked( p_object );
1644
1645     p_list->p_values[p_list->i_count].p_object = p_object;
1646     p_list->i_count++;
1647
1648     return;
1649 }*/
1650
1651 static int CountChildren( vlc_object_t *p_this, int i_type )
1652 {
1653     vlc_object_t *p_tmp;
1654     int i, i_count = 0;
1655
1656     for( i = 0; i < p_this->i_children; i++ )
1657     {
1658         p_tmp = p_this->pp_children[i];
1659
1660         if( p_tmp->i_object_type == i_type )
1661         {
1662             i_count++;
1663         }
1664
1665         if( p_tmp->i_children )
1666         {
1667             i_count += CountChildren( p_tmp, i_type );
1668         }
1669     }
1670
1671     return i_count;
1672 }
1673
1674 static void ListChildren( vlc_list_t *p_list, vlc_object_t *p_this, int i_type )
1675 {
1676     vlc_object_t *p_tmp;
1677     int i;
1678
1679     for( i = 0; i < p_this->i_children; i++ )
1680     {
1681         p_tmp = p_this->pp_children[i];
1682
1683         if( p_tmp->i_object_type == i_type )
1684         {
1685             ListReplace( p_list, p_tmp, p_list->i_count++ );
1686         }
1687
1688         if( p_tmp->i_children )
1689         {
1690             ListChildren( p_list, p_tmp, i_type );
1691         }
1692     }
1693 }