]> git.sesse.net Git - vlc/blob - src/misc/variables.c
Remove the broken reference checker
[vlc] / src / misc / variables.c
1 /*****************************************************************************
2  * variables.c: routines for object variables handling
3  *****************************************************************************
4  * Copyright (C) 2002-2006 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  * Preamble
26  *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <vlc_common.h>
32 #include "variables.h"
33
34 #include "libvlc.h"
35
36 #include "vlc_interface.h"
37
38 /*****************************************************************************
39  * Private types
40  *****************************************************************************/
41 struct callback_entry_t
42 {
43     vlc_callback_t pf_callback;
44     void *         p_data;
45 };
46
47 /*****************************************************************************
48  * Local comparison functions, returns 0 if v == w, < 0 if v < w, > 0 if v > w
49  *****************************************************************************/
50 static int CmpBool( vlc_value_t v, vlc_value_t w ) { return v.b_bool ? w.b_bool ? 0 : 1 : w.b_bool ? -1 : 0; }
51 static int CmpInt( vlc_value_t v, vlc_value_t w ) { return v.i_int == w.i_int ? 0 : v.i_int > w.i_int ? 1 : -1; }
52 static int CmpTime( vlc_value_t v, vlc_value_t w )
53 {
54     return v.i_time == w.i_time ? 0 : v.i_time > w.i_time ? 1 : -1;
55 }
56 static int CmpString( vlc_value_t v, vlc_value_t w )
57 {
58     if( !v.psz_string )
59         return !w.psz_string ? 0 : -1;
60     else
61         return !w.psz_string ? 1 : strcmp( v.psz_string, w.psz_string );
62 }
63 static int CmpFloat( vlc_value_t v, vlc_value_t w ) { return v.f_float == w.f_float ? 0 : v.f_float > w.f_float ? 1 : -1; }
64 static int CmpAddress( vlc_value_t v, vlc_value_t w ) { return v.p_address == w.p_address ? 0 : v.p_address > w.p_address ? 1 : -1; }
65
66 /*****************************************************************************
67  * Local duplication functions, and local deallocation functions
68  *****************************************************************************/
69 static void DupDummy( vlc_value_t *p_val ) { (void)p_val; /* unused */ }
70 static void DupString( vlc_value_t *p_val ) { if( p_val->psz_string ) p_val->psz_string = strdup( p_val->psz_string ); }
71
72 static void DupList( vlc_value_t *p_val )
73 {
74     int i;
75     vlc_list_t *p_list = malloc( sizeof(vlc_list_t) );
76
77     p_list->i_count = p_val->p_list->i_count;
78     if( p_val->p_list->i_count )
79     {
80         p_list->p_values = malloc( p_list->i_count * sizeof(vlc_value_t) );
81         p_list->pi_types = malloc( p_list->i_count * sizeof(int) );
82     }
83     else
84     {
85         p_list->p_values = NULL;
86         p_list->pi_types = NULL;
87     }
88
89     for( i = 0; i < p_list->i_count; i++ )
90     {
91         p_list->p_values[i] = p_val->p_list->p_values[i];
92         p_list->pi_types[i] = p_val->p_list->pi_types[i];
93         switch( p_val->p_list->pi_types[i] & VLC_VAR_TYPE )
94         {
95         case VLC_VAR_STRING:
96
97             DupString( &p_list->p_values[i] );
98             break;
99         default:
100             break;
101         }
102     }
103
104     p_val->p_list = p_list;
105 }
106
107 static void FreeDummy( vlc_value_t *p_val ) { (void)p_val; /* unused */ }
108 static void FreeString( vlc_value_t *p_val ) { free( p_val->psz_string ); }
109 static void FreeMutex( vlc_value_t *p_val ) { vlc_mutex_destroy( (vlc_mutex_t*)p_val->p_address ); free( p_val->p_address ); }
110
111 static void FreeList( vlc_value_t *p_val )
112 {
113     int i;
114     for( i = 0; i < p_val->p_list->i_count; i++ )
115     {
116         switch( p_val->p_list->pi_types[i] & VLC_VAR_TYPE )
117         {
118         case VLC_VAR_STRING:
119             FreeString( &p_val->p_list->p_values[i] );
120             break;
121         case VLC_VAR_MUTEX:
122             FreeMutex( &p_val->p_list->p_values[i] );
123             break;
124         default:
125             break;
126         }
127     }
128
129     if( p_val->p_list->i_count )
130     {
131         free( p_val->p_list->p_values );
132         free( p_val->p_list->pi_types );
133     }
134     free( p_val->p_list );
135 }
136
137 /*****************************************************************************
138  * Local prototypes
139  *****************************************************************************/
140 static int      GetUnused   ( vlc_object_t *, const char * );
141 static uint32_t HashString  ( const char * );
142 static int      Insert      ( variable_t *, int, const char * );
143 static int      InsertInner ( variable_t *, int, uint32_t );
144 static int      Lookup      ( variable_t *, int, const char * );
145 static int      LookupInner ( variable_t *, int, uint32_t );
146
147 static void     CheckValue  ( variable_t *, vlc_value_t * );
148
149 static int      InheritValue( vlc_object_t *, const char *, vlc_value_t *,
150                               int );
151
152 /**
153  * Initialize a vlc variable
154  *
155  * We hash the given string and insert it into the sorted list. The insertion
156  * may require slow memory copies, but think about what we gain in the log(n)
157  * lookup phase when setting/getting the variable value!
158  *
159  * \param p_this The object in which to create the variable
160  * \param psz_name The name of the variable
161  * \param i_type The variables type. Must be one of \ref var_type combined with
162  *               zero or more \ref var_flags
163  */
164 int __var_Create( vlc_object_t *p_this, const char *psz_name, int i_type )
165 {
166     int i_new;
167     variable_t *p_var;
168     static vlc_list_t dummy_null_list = {0, NULL, NULL};
169     vlc_object_internals_t *p_priv = vlc_internals( p_this );
170
171     vlc_mutex_lock( &p_priv->var_lock );
172
173     /* FIXME: if the variable already exists, we don't duplicate it. But we
174      * duplicate the lookups. It's not that serious, but if anyone finds some
175      * time to rework Insert() so that only one lookup has to be done, feel
176      * free to do so. */
177     i_new = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
178
179     if( i_new >= 0 )
180     {
181         /* If the types differ, variable creation failed. */
182         if( (i_type & ~(VLC_VAR_DOINHERIT|VLC_VAR_ISCOMMAND)) != p_priv->p_vars[i_new].i_type )
183         {
184             vlc_mutex_unlock( &p_priv->var_lock );
185             return VLC_EBADVAR;
186         }
187
188         p_priv->p_vars[i_new].i_usage++;
189         if( i_type & VLC_VAR_ISCOMMAND )
190             p_priv->p_vars[i_new].i_type |= VLC_VAR_ISCOMMAND;
191         vlc_mutex_unlock( &p_priv->var_lock );
192         return VLC_SUCCESS;
193     }
194
195     i_new = Insert( p_priv->p_vars, p_priv->i_vars, psz_name );
196
197     if( (p_priv->i_vars & 15) == 15 )
198     {
199         p_priv->p_vars = realloc( p_priv->p_vars,
200                                   (p_priv->i_vars+17) * sizeof(variable_t) );
201     }
202
203     memmove( p_priv->p_vars + i_new + 1,
204              p_priv->p_vars + i_new,
205              (p_priv->i_vars - i_new) * sizeof(variable_t) );
206
207     p_priv->i_vars++;
208
209     p_var = &p_priv->p_vars[i_new];
210     memset( p_var, 0, sizeof(*p_var) );
211
212     p_var->i_hash = HashString( psz_name );
213     p_var->psz_name = strdup( psz_name );
214     p_var->psz_text = NULL;
215
216     p_var->i_type = i_type & ~VLC_VAR_DOINHERIT;
217     memset( &p_var->val, 0, sizeof(vlc_value_t) );
218
219     p_var->pf_dup = DupDummy;
220     p_var->pf_free = FreeDummy;
221
222     p_var->i_usage = 1;
223
224     p_var->i_default = -1;
225     p_var->choices.i_count = 0;
226     p_var->choices.p_values = NULL;
227     p_var->choices_text.i_count = 0;
228     p_var->choices_text.p_values = NULL;
229
230     p_var->b_incallback = false;
231     p_var->i_entries = 0;
232     p_var->p_entries = NULL;
233
234     /* Always initialize the variable, even if it is a list variable; this
235      * will lead to errors if the variable is not initialized, but it will
236      * not cause crashes in the variable handling. */
237     switch( i_type & VLC_VAR_TYPE )
238     {
239         case VLC_VAR_BOOL:
240             p_var->pf_cmp = CmpBool;
241             p_var->val.b_bool = false;
242             break;
243         case VLC_VAR_INTEGER:
244         case VLC_VAR_HOTKEY:
245             p_var->pf_cmp = CmpInt;
246             p_var->val.i_int = 0;
247             break;
248         case VLC_VAR_STRING:
249         case VLC_VAR_MODULE:
250         case VLC_VAR_FILE:
251         case VLC_VAR_DIRECTORY:
252         case VLC_VAR_VARIABLE:
253             p_var->pf_cmp = CmpString;
254             p_var->pf_dup = DupString;
255             p_var->pf_free = FreeString;
256             p_var->val.psz_string = NULL;
257             break;
258         case VLC_VAR_FLOAT:
259             p_var->pf_cmp = CmpFloat;
260             p_var->val.f_float = 0.0;
261             break;
262         case VLC_VAR_TIME:
263             p_var->pf_cmp = CmpTime;
264             p_var->val.i_time = 0;
265             break;
266         case VLC_VAR_ADDRESS:
267             p_var->pf_cmp = CmpAddress;
268             p_var->val.p_address = NULL;
269             break;
270         case VLC_VAR_MUTEX:
271             p_var->pf_cmp = CmpAddress;
272             p_var->pf_free = FreeMutex;
273             p_var->val.p_address = malloc( sizeof(vlc_mutex_t) );
274             vlc_mutex_init( (vlc_mutex_t*)p_var->val.p_address );
275             break;
276         case VLC_VAR_LIST:
277             p_var->pf_cmp = CmpAddress;
278             p_var->pf_dup = DupList;
279             p_var->pf_free = FreeList;
280             p_var->val.p_list = &dummy_null_list;
281             break;
282     }
283
284     /* Duplicate the default data we stored. */
285     p_var->pf_dup( &p_var->val );
286
287     if( i_type & VLC_VAR_DOINHERIT )
288     {
289         vlc_value_t val;
290
291         if( InheritValue( p_this, psz_name, &val, p_var->i_type )
292             == VLC_SUCCESS )
293         {
294             /* Free data if needed */
295             p_var->pf_free( &p_var->val );
296             /* Set the variable */
297             p_var->val = val;
298
299             if( i_type & VLC_VAR_HASCHOICE )
300             {
301                 /* We must add the inherited value to our choice list */
302                 p_var->i_default = 0;
303
304                 INSERT_ELEM( p_var->choices.p_values, p_var->choices.i_count,
305                              0, val );
306                 INSERT_ELEM( p_var->choices_text.p_values,
307                              p_var->choices_text.i_count, 0, val );
308                 p_var->pf_dup( &p_var->choices.p_values[0] );
309                 p_var->choices_text.p_values[0].psz_string = NULL;
310             }
311         }
312     }
313
314     vlc_mutex_unlock( &p_priv->var_lock );
315
316     return VLC_SUCCESS;
317 }
318
319 /**
320  * Destroy a vlc variable
321  *
322  * Look for the variable and destroy it if it is found. As in var_Create we
323  * do a call to memmove() but we have performance counterparts elsewhere.
324  *
325  * \param p_this The object that holds the variable
326  * \param psz_name The name of the variable
327  */
328 int __var_Destroy( vlc_object_t *p_this, const char *psz_name )
329 {
330     int i_var, i;
331     variable_t *p_var;
332     vlc_object_internals_t *p_priv = vlc_internals( p_this );
333
334     vlc_mutex_lock( &p_priv->var_lock );
335
336     i_var = GetUnused( p_this, psz_name );
337     if( i_var < 0 )
338     {
339         vlc_mutex_unlock( &p_priv->var_lock );
340         return i_var;
341     }
342
343     p_var = &p_priv->p_vars[i_var];
344
345     if( p_var->i_usage > 1 )
346     {
347         p_var->i_usage--;
348         vlc_mutex_unlock( &p_priv->var_lock );
349         return VLC_SUCCESS;
350     }
351
352     /* Free value if needed */
353     p_var->pf_free( &p_var->val );
354
355     /* Free choice list if needed */
356     if( p_var->choices.i_count )
357     {
358         for( i = 0 ; i < p_var->choices.i_count ; i++ )
359         {
360             p_var->pf_free( &p_var->choices.p_values[i] );
361             free( p_var->choices_text.p_values[i].psz_string );
362         }
363         free( p_var->choices.p_values );
364         free( p_var->choices_text.p_values );
365     }
366
367     /* Free callbacks if needed */
368     if( p_var->p_entries )
369     {
370         free( p_var->p_entries );
371     }
372
373     free( p_var->psz_name );
374     free( p_var->psz_text );
375
376     memmove( p_priv->p_vars + i_var,
377              p_priv->p_vars + i_var + 1,
378              (p_priv->i_vars - i_var - 1) * sizeof(variable_t) );
379
380     if( (p_priv->i_vars & 15) == 0 )
381     {
382         p_priv->p_vars = realloc( p_priv->p_vars,
383                           (p_priv->i_vars) * sizeof( variable_t ) );
384     }
385
386     p_priv->i_vars--;
387
388     vlc_mutex_unlock( &p_priv->var_lock );
389
390     return VLC_SUCCESS;
391 }
392
393 /**
394  * Perform an action on a variable
395  *
396  * \param p_this The object that holds the variable
397  * \param psz_name The name of the variable
398  * \param i_action The action to perform. Must be one of \ref var_action
399  * \param p_val First action parameter
400  * \param p_val2 Second action parameter
401  */
402 int __var_Change( vlc_object_t *p_this, const char *psz_name,
403                   int i_action, vlc_value_t *p_val, vlc_value_t *p_val2 )
404 {
405     int i_var, i;
406     variable_t *p_var;
407     vlc_value_t oldval;
408     vlc_object_internals_t *p_priv = vlc_internals( p_this );
409
410     vlc_mutex_lock( &p_priv->var_lock );
411
412     i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
413
414     if( i_var < 0 )
415     {
416         vlc_mutex_unlock( &p_priv->var_lock );
417         return VLC_ENOVAR;
418     }
419
420     p_var = &p_priv->p_vars[i_var];
421
422     switch( i_action )
423     {
424         case VLC_VAR_SETMIN:
425             if( p_var->i_type & VLC_VAR_HASMIN )
426             {
427                 p_var->pf_free( &p_var->min );
428             }
429             p_var->i_type |= VLC_VAR_HASMIN;
430             p_var->min = *p_val;
431             p_var->pf_dup( &p_var->min );
432             CheckValue( p_var, &p_var->val );
433             break;
434         case VLC_VAR_GETMIN:
435             if( p_var->i_type & VLC_VAR_HASMIN )
436             {
437                 *p_val = p_var->min;
438             }
439             break;
440         case VLC_VAR_SETMAX:
441             if( p_var->i_type & VLC_VAR_HASMAX )
442             {
443                 p_var->pf_free( &p_var->max );
444             }
445             p_var->i_type |= VLC_VAR_HASMAX;
446             p_var->max = *p_val;
447             p_var->pf_dup( &p_var->max );
448             CheckValue( p_var, &p_var->val );
449             break;
450         case VLC_VAR_GETMAX:
451             if( p_var->i_type & VLC_VAR_HASMAX )
452             {
453                 *p_val = p_var->max;
454             }
455             break;
456         case VLC_VAR_SETSTEP:
457             if( p_var->i_type & VLC_VAR_HASSTEP )
458             {
459                 p_var->pf_free( &p_var->step );
460             }
461             p_var->i_type |= VLC_VAR_HASSTEP;
462             p_var->step = *p_val;
463             p_var->pf_dup( &p_var->step );
464             CheckValue( p_var, &p_var->val );
465             break;
466         case VLC_VAR_GETSTEP:
467             if( p_var->i_type & VLC_VAR_HASSTEP )
468             {
469                 *p_val = p_var->step;
470             }
471             break;
472         case VLC_VAR_ADDCHOICE:
473             i = p_var->choices.i_count;
474
475             INSERT_ELEM( p_var->choices.p_values, p_var->choices.i_count,
476                          i, *p_val );
477             INSERT_ELEM( p_var->choices_text.p_values,
478                          p_var->choices_text.i_count, i, *p_val );
479             p_var->pf_dup( &p_var->choices.p_values[i] );
480             p_var->choices_text.p_values[i].psz_string =
481                 ( p_val2 && p_val2->psz_string ) ?
482                 strdup( p_val2->psz_string ) : NULL;
483
484             CheckValue( p_var, &p_var->val );
485             break;
486         case VLC_VAR_DELCHOICE:
487             for( i = 0 ; i < p_var->choices.i_count ; i++ )
488             {
489                 if( p_var->pf_cmp( p_var->choices.p_values[i], *p_val ) == 0 )
490                 {
491                     break;
492                 }
493             }
494
495             if( i == p_var->choices.i_count )
496             {
497                 /* Not found */
498                 vlc_mutex_unlock( &p_priv->var_lock );
499                 return VLC_EGENERIC;
500             }
501
502             if( p_var->i_default > i )
503             {
504                 p_var->i_default--;
505             }
506             else if( p_var->i_default == i )
507             {
508                 p_var->i_default = -1;
509             }
510
511             p_var->pf_free( &p_var->choices.p_values[i] );
512             free( p_var->choices_text.p_values[i].psz_string );
513             REMOVE_ELEM( p_var->choices.p_values, p_var->choices.i_count, i );
514             REMOVE_ELEM( p_var->choices_text.p_values,
515                          p_var->choices_text.i_count, i );
516
517             CheckValue( p_var, &p_var->val );
518             break;
519         case VLC_VAR_CHOICESCOUNT:
520             p_val->i_int = p_var->choices.i_count;
521             break;
522         case VLC_VAR_CLEARCHOICES:
523             for( i = 0 ; i < p_var->choices.i_count ; i++ )
524             {
525                 p_var->pf_free( &p_var->choices.p_values[i] );
526             }
527             for( i = 0 ; i < p_var->choices_text.i_count ; i++ )
528                 free( p_var->choices_text.p_values[i].psz_string );
529
530             if( p_var->choices.i_count ) free( p_var->choices.p_values );
531             if( p_var->choices_text.i_count ) free( p_var->choices_text.p_values );
532
533             p_var->choices.i_count = 0;
534             p_var->choices.p_values = NULL;
535             p_var->choices_text.i_count = 0;
536             p_var->choices_text.p_values = NULL;
537             p_var->i_default = -1;
538             break;
539         case VLC_VAR_SETDEFAULT:
540             /* FIXME: the list is sorted, dude. Use something cleverer. */
541             for( i = 0 ; i < p_var->choices.i_count ; i++ )
542             {
543                 if( p_var->pf_cmp( p_var->choices.p_values[i], *p_val ) == 0 )
544                 {
545                     break;
546                 }
547             }
548
549             if( i == p_var->choices.i_count )
550             {
551                 /* Not found */
552                 break;
553             }
554
555             p_var->i_default = i;
556             CheckValue( p_var, &p_var->val );
557             break;
558         case VLC_VAR_SETVALUE:
559             /* Duplicate data if needed */
560             p_var->pf_dup( p_val );
561             /* Backup needed stuff */
562             oldval = p_var->val;
563             /* Check boundaries and list */
564             CheckValue( p_var, p_val );
565             /* Set the variable */
566             p_var->val = *p_val;
567             /* Free data if needed */
568             p_var->pf_free( &oldval );
569             break;
570         case VLC_VAR_GETCHOICES:
571         case VLC_VAR_GETLIST:
572             p_val->p_list = malloc( sizeof(vlc_list_t) );
573             if( p_val2 ) p_val2->p_list = malloc( sizeof(vlc_list_t) );
574             if( p_var->choices.i_count )
575             {
576                 p_val->p_list->p_values = malloc( p_var->choices.i_count
577                                                   * sizeof(vlc_value_t) );
578                 p_val->p_list->pi_types = malloc( p_var->choices.i_count
579                                                   * sizeof(int) );
580                 if( p_val2 )
581                 {
582                     p_val2->p_list->p_values =
583                         malloc( p_var->choices.i_count * sizeof(vlc_value_t) );
584                     p_val2->p_list->pi_types =
585                         malloc( p_var->choices.i_count * sizeof(int) );
586                 }
587             }
588             p_val->p_list->i_count = p_var->choices.i_count;
589             if( p_val2 ) p_val2->p_list->i_count = p_var->choices.i_count;
590             for( i = 0 ; i < p_var->choices.i_count ; i++ )
591             {
592                 p_val->p_list->p_values[i] = p_var->choices.p_values[i];
593                 p_val->p_list->pi_types[i] = p_var->i_type;
594                 p_var->pf_dup( &p_val->p_list->p_values[i] );
595                 if( p_val2 )
596                 {
597                     p_val2->p_list->p_values[i].psz_string =
598                         p_var->choices_text.p_values[i].psz_string ?
599                     strdup(p_var->choices_text.p_values[i].psz_string) : NULL;
600                     p_val2->p_list->pi_types[i] = VLC_VAR_STRING;
601                 }
602             }
603             break;
604         case VLC_VAR_FREELIST:
605             FreeList( p_val );
606             if( p_val2 && p_val2->p_list )
607             {
608                 for( i = 0; i < p_val2->p_list->i_count; i++ )
609                     free( p_val2->p_list->p_values[i].psz_string );
610                 if( p_val2->p_list->i_count )
611                 {
612                     free( p_val2->p_list->p_values );
613                     free( p_val2->p_list->pi_types );
614                 }
615                 free( p_val2->p_list );
616             }
617             break;
618         case VLC_VAR_SETTEXT:
619             free( p_var->psz_text );
620             if( p_val && p_val->psz_string )
621                 p_var->psz_text = strdup( p_val->psz_string );
622             break;
623         case VLC_VAR_GETTEXT:
624             p_val->psz_string = NULL;
625             if( p_var->psz_text )
626             {
627                 p_val->psz_string = strdup( p_var->psz_text );
628             }
629             break;
630         case VLC_VAR_INHERITVALUE:
631             {
632                 vlc_value_t val;
633
634                 if( InheritValue( p_this,
635                                   p_val2 ? p_val2->psz_string :  psz_name,
636                                   &val, p_var->i_type )
637                     == VLC_SUCCESS )
638                 {
639                     /* Duplicate already done */
640
641                     /* Backup needed stuff */
642                     oldval = p_var->val;
643                     /* Check boundaries and list */
644                     CheckValue( p_var, &val );
645                     /* Set the variable */
646                     p_var->val = val;
647                     /* Free data if needed */
648                     p_var->pf_free( &oldval );
649                 }
650
651                 if( p_val )
652                 {
653                     *p_val = p_var->val;
654                     p_var->pf_dup( p_val );
655                 }
656             }
657             break;
658         case VLC_VAR_TRIGGER_CALLBACKS:
659             {
660                 /* Deal with callbacks. Tell we're in a callback, release the lock,
661                  * call stored functions, retake the lock. */
662                 if( p_var->i_entries )
663                 {
664                     int i_var;
665                     int i_entries = p_var->i_entries;
666                     callback_entry_t *p_entries = p_var->p_entries;
667
668                     p_var->b_incallback = true;
669                     vlc_mutex_unlock( &p_priv->var_lock );
670
671                     /* The real calls */
672                     for( ; i_entries-- ; )
673                     {
674                         p_entries[i_entries].pf_callback( p_this, psz_name, p_var->val, p_var->val,
675                                                           p_entries[i_entries].p_data );
676                     }
677
678                     vlc_mutex_lock( &p_priv->var_lock );
679
680                     i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
681                     if( i_var < 0 )
682                     {
683                         msg_Err( p_this, "variable %s has disappeared", psz_name );
684                         vlc_mutex_unlock( &p_priv->var_lock );
685                         return VLC_ENOVAR;
686                     }
687
688                     p_var = &p_priv->p_vars[i_var];
689                     p_var->b_incallback = false;
690                 }
691             }
692             break;
693
694         case VLC_VAR_SETISCOMMAND:
695             p_var->i_type |= VLC_VAR_ISCOMMAND;
696             break;
697
698         default:
699             break;
700     }
701
702     vlc_mutex_unlock( &p_priv->var_lock );
703
704     return VLC_SUCCESS;
705 }
706
707 /**
708  * Request a variable's type
709  *
710  * \return The variable type if it exists, or 0 if the
711  * variable could not be found.
712  * \see \ref var_type
713  */
714 int __var_Type( vlc_object_t *p_this, const char *psz_name )
715 {
716     int i_var, i_type;
717     vlc_object_internals_t *p_priv = vlc_internals( p_this );
718
719     vlc_mutex_lock( &p_priv->var_lock );
720
721     i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
722
723     if( i_var < 0 )
724     {
725         vlc_mutex_unlock( &p_priv->var_lock );
726         return 0;
727     }
728
729     i_type = p_priv->p_vars[i_var].i_type;
730
731     vlc_mutex_unlock( &p_priv->var_lock );
732
733     return i_type;
734 }
735
736 /**
737  * Set a variable's value
738  *
739  * \param p_this The object that hold the variable
740  * \param psz_name The name of the variable
741  * \param val the value to set
742  */
743 int __var_Set( vlc_object_t *p_this, const char *psz_name, vlc_value_t val )
744 {
745     int i_var;
746     variable_t *p_var;
747     vlc_value_t oldval;
748     vlc_object_internals_t *p_priv = vlc_internals( p_this );
749
750     vlc_mutex_lock( &p_priv->var_lock );
751
752     i_var = GetUnused( p_this, psz_name );
753     if( i_var < 0 )
754     {
755         vlc_mutex_unlock( &p_priv->var_lock );
756         return i_var;
757     }
758
759     p_var = &p_priv->p_vars[i_var];
760
761     /* Duplicate data if needed */
762     p_var->pf_dup( &val );
763
764     /* Backup needed stuff */
765     oldval = p_var->val;
766
767     /* Check boundaries and list */
768     CheckValue( p_var, &val );
769
770     /* Set the variable */
771     p_var->val = val;
772
773     /* Deal with callbacks. Tell we're in a callback, release the lock,
774      * call stored functions, retake the lock. */
775     if( p_var->i_entries )
776     {
777         int i_var;
778         int i_entries = p_var->i_entries;
779         callback_entry_t *p_entries = p_var->p_entries;
780
781         p_var->b_incallback = true;
782         vlc_mutex_unlock( &p_priv->var_lock );
783
784         /* The real calls */
785         for( ; i_entries-- ; )
786         {
787             p_entries[i_entries].pf_callback( p_this, psz_name, oldval, val,
788                                               p_entries[i_entries].p_data );
789         }
790
791         vlc_mutex_lock( &p_priv->var_lock );
792
793         i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
794         if( i_var < 0 )
795         {
796             msg_Err( p_this, "variable %s has disappeared", psz_name );
797             vlc_mutex_unlock( &p_priv->var_lock );
798             return VLC_ENOVAR;
799         }
800
801         p_var = &p_priv->p_vars[i_var];
802         p_var->b_incallback = false;
803     }
804
805     /* Free data if needed */
806     p_var->pf_free( &oldval );
807
808     vlc_mutex_unlock( &p_priv->var_lock );
809
810     return VLC_SUCCESS;
811 }
812
813 /**
814  * Get a variable's value
815  *
816  * \param p_this The object that holds the variable
817  * \param psz_name The name of the variable
818  * \param p_val Pointer to a vlc_value_t that will hold the variable's value
819  *              after the function is finished
820  */
821 int __var_Get( vlc_object_t *p_this, const char *psz_name, vlc_value_t *p_val )
822 {
823     int i_var;
824     variable_t *p_var;
825     vlc_object_internals_t *p_priv = vlc_internals( p_this );
826
827     vlc_mutex_lock( &p_priv->var_lock );
828
829     i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
830
831     if( i_var < 0 )
832     {
833         vlc_mutex_unlock( &p_priv->var_lock );
834         return VLC_ENOVAR;
835     }
836
837     p_var = &p_priv->p_vars[i_var];
838
839     /* Really get the variable */
840     *p_val = p_var->val;
841
842     /* Duplicate value if needed */
843     p_var->pf_dup( p_val );
844
845     vlc_mutex_unlock( &p_priv->var_lock );
846
847     return VLC_SUCCESS;
848 }
849
850
851 /**
852  * Finds a process-wide mutex, creates it if needed, and locks it.
853  * Unlock with vlc_mutex_unlock().
854  */
855 vlc_mutex_t *var_AcquireMutex( const char *name )
856 {
857     libvlc_global_data_t *p_global = vlc_global();
858     vlc_value_t val;
859
860     if( var_Create( p_global, name, VLC_VAR_MUTEX ) )
861         return NULL;
862
863     var_Get( p_global, name, &val );
864     vlc_mutex_lock( val.p_address );
865     return val.p_address;
866 }
867
868
869 /**
870  * Register a callback in a variable
871  *
872  * We store a function pointer that will be called upon variable
873  * modification.
874  *
875  * \param p_this The object that holds the variable
876  * \param psz_name The name of the variable
877  * \param pf_callback The function pointer
878  * \param p_data A generic pointer that will be passed as the last
879  *               argument to the callback function.
880  *
881  * \warning The callback function is run in the thread that calls var_Set on
882  *          the variable. Use proper locking. This thread may not have much
883  *          time to spare, so keep callback functions short.
884  */
885 int __var_AddCallback( vlc_object_t *p_this, const char *psz_name,
886                        vlc_callback_t pf_callback, void *p_data )
887 {
888     int i_var;
889     variable_t *p_var;
890     callback_entry_t entry;
891     vlc_object_internals_t *p_priv = vlc_internals( p_this );
892
893     entry.pf_callback = pf_callback;
894     entry.p_data = p_data;
895
896     vlc_mutex_lock( &p_priv->var_lock );
897
898     i_var = GetUnused( p_this, psz_name );
899     if( i_var < 0 )
900     {
901         vlc_mutex_unlock( &p_priv->var_lock );
902         return i_var;
903     }
904
905     p_var = &p_priv->p_vars[i_var];
906
907     INSERT_ELEM( p_var->p_entries,
908                  p_var->i_entries,
909                  p_var->i_entries,
910                  entry );
911
912     vlc_mutex_unlock( &p_priv->var_lock );
913
914     return VLC_SUCCESS;
915 }
916
917 /**
918  * Remove a callback from a variable
919  *
920  * pf_callback and p_data have to be given again, because different objects
921  * might have registered the same callback function.
922  */
923 int __var_DelCallback( vlc_object_t *p_this, const char *psz_name,
924                        vlc_callback_t pf_callback, void *p_data )
925 {
926     int i_entry, i_var;
927     variable_t *p_var;
928     vlc_object_internals_t *p_priv = vlc_internals( p_this );
929
930     vlc_mutex_lock( &p_priv->var_lock );
931
932     i_var = GetUnused( p_this, psz_name );
933     if( i_var < 0 )
934     {
935         vlc_mutex_unlock( &p_priv->var_lock );
936         return i_var;
937     }
938
939     p_var = &p_priv->p_vars[i_var];
940
941     for( i_entry = p_var->i_entries ; i_entry-- ; )
942     {
943         if( p_var->p_entries[i_entry].pf_callback == pf_callback
944             && p_var->p_entries[i_entry].p_data == p_data )
945         {
946             break;
947         }
948     }
949
950     if( i_entry < 0 )
951     {
952         vlc_mutex_unlock( &p_priv->var_lock );
953         return VLC_EGENERIC;
954     }
955
956     REMOVE_ELEM( p_var->p_entries, p_var->i_entries, i_entry );
957
958     vlc_mutex_unlock( &p_priv->var_lock );
959
960     return VLC_SUCCESS;
961 }
962
963 /**
964  * Trigger callback on a variable
965  *
966  * \param p_this The object that hold the variable
967  * \param psz_name The name of the variable
968  */
969 int __var_TriggerCallback( vlc_object_t *p_this, const char *psz_name )
970 {
971     int i_var;
972     variable_t *p_var;
973     vlc_value_t oldval;
974     vlc_object_internals_t *p_priv = vlc_internals( p_this );
975
976     vlc_mutex_lock( &p_priv->var_lock );
977
978     i_var = GetUnused( p_this, psz_name );
979     if( i_var < 0 )
980     {
981         vlc_mutex_unlock( &p_priv->var_lock );
982         return i_var;
983     }
984
985     p_var = &p_priv->p_vars[i_var];
986
987     /* Backup needed stuff */
988     oldval = p_var->val;
989
990     /* Deal with callbacks. Tell we're in a callback, release the lock,
991      * call stored functions, retake the lock. */
992     if( p_var->i_entries )
993     {
994         int i_var;
995         int i_entries = p_var->i_entries;
996         callback_entry_t *p_entries = p_var->p_entries;
997
998         p_var->b_incallback = true;
999         vlc_mutex_unlock( &p_priv->var_lock );
1000
1001         /* The real calls */
1002         for( ; i_entries-- ; )
1003         {
1004             p_entries[i_entries].pf_callback( p_this, psz_name, oldval, oldval,
1005                                               p_entries[i_entries].p_data );
1006         }
1007
1008         vlc_mutex_lock( &p_priv->var_lock );
1009
1010         i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
1011         if( i_var < 0 )
1012         {
1013             msg_Err( p_this, "variable %s has disappeared", psz_name );
1014             vlc_mutex_unlock( &p_priv->var_lock );
1015             return VLC_ENOVAR;
1016         }
1017
1018         p_var = &p_priv->p_vars[i_var];
1019         p_var->b_incallback = false;
1020     }
1021
1022     vlc_mutex_unlock( &p_priv->var_lock );
1023     return VLC_SUCCESS;
1024 }
1025
1026 /** Parse a stringified option
1027  * This function parse a string option and create the associated object
1028  * variable
1029  * The option must be of the form "[no[-]]foo[=bar]" where foo is the
1030  * option name and bar is the value of the option.
1031  * \param p_obj the object in which the variable must be created
1032  * \param psz_option the option to parse
1033  * \param trusted whether the option is set by a trusted input or not
1034  * \return nothing
1035  */
1036 void var_OptionParse( vlc_object_t *p_obj, const char *psz_option,
1037                       bool trusted )
1038 {
1039     char *psz_name, *psz_value;
1040     int  i_type;
1041     bool b_isno = false;
1042     vlc_value_t val;
1043
1044     val.psz_string = NULL;
1045
1046     /* It's too much of a hassle to remove the ':' when we parse
1047      * the cmd line :) */
1048     if( psz_option[0] == ':' )
1049         psz_option++;
1050
1051     if( !psz_option[0] )
1052         return;
1053
1054     psz_name = strdup( psz_option );
1055     if( psz_name == NULL )
1056         return;
1057
1058     psz_value = strchr( psz_name, '=' );
1059     if( psz_value != NULL )
1060         *psz_value++ = '\0';
1061
1062     /* FIXME: :programs should be handled generically */
1063     if( !strcmp( psz_name, "programs" ) )
1064         i_type = VLC_VAR_LIST;
1065     else
1066         i_type = config_GetType( p_obj, psz_name );
1067
1068     if( !i_type && !psz_value )
1069     {
1070         /* check for "no-foo" or "nofoo" */
1071         if( !strncmp( psz_name, "no-", 3 ) )
1072         {
1073             memmove( psz_name, psz_name + 3, strlen(psz_name) + 1 - 3 );
1074         }
1075         else if( !strncmp( psz_name, "no", 2 ) )
1076         {
1077             memmove( psz_name, psz_name + 2, strlen(psz_name) + 1 - 2 );
1078         }
1079         else goto cleanup;           /* Option doesn't exist */
1080
1081         b_isno = true;
1082         i_type = config_GetType( p_obj, psz_name );
1083     }
1084     if( !i_type ) goto cleanup; /* Option doesn't exist */
1085
1086     if( ( i_type != VLC_VAR_BOOL ) &&
1087         ( !psz_value || !*psz_value ) ) goto cleanup; /* Invalid value */
1088
1089     /* check if option is unsafe */
1090     if( !trusted )
1091     {
1092         module_config_t *p_config = config_FindConfig( p_obj, psz_name );
1093         if( !p_config->b_safe )
1094         {
1095             msg_Err( p_obj, "unsafe option \"%s\" has been ignored for "
1096                             "security reasons", psz_name );
1097             return;
1098         }
1099     }
1100
1101     /* Create the variable in the input object.
1102      * Children of the input object will be able to retreive this value
1103      * thanks to the inheritance property of the object variables. */
1104     var_Create( p_obj, psz_name, i_type );
1105
1106     switch( i_type )
1107     {
1108     case VLC_VAR_BOOL:
1109         val.b_bool = !b_isno;
1110         break;
1111
1112     case VLC_VAR_INTEGER:
1113         val.i_int = strtol( psz_value, NULL, 0 );
1114         break;
1115
1116     case VLC_VAR_FLOAT:
1117         val.f_float = atof( psz_value );
1118         break;
1119
1120     case VLC_VAR_STRING:
1121     case VLC_VAR_MODULE:
1122     case VLC_VAR_FILE:
1123     case VLC_VAR_DIRECTORY:
1124         val.psz_string = psz_value;
1125         break;
1126
1127     case VLC_VAR_LIST:
1128     {
1129         char *psz_orig, *psz_var;
1130         vlc_list_t *p_list = malloc(sizeof(vlc_list_t));
1131         val.p_list = p_list;
1132         p_list->i_count = 0;
1133
1134         psz_var = psz_orig = strdup(psz_value);
1135         while( psz_var && *psz_var )
1136         {
1137             char *psz_item = psz_var;
1138             vlc_value_t val2;
1139             while( *psz_var && *psz_var != ',' ) psz_var++;
1140             if( *psz_var == ',' )
1141             {
1142                 *psz_var = '\0';
1143                 psz_var++;
1144             }
1145             val2.i_int = strtol( psz_item, NULL, 0 );
1146             INSERT_ELEM( p_list->p_values, p_list->i_count,
1147                          p_list->i_count, val2 );
1148             /* p_list->i_count is incremented twice by INSERT_ELEM */
1149             p_list->i_count--;
1150             INSERT_ELEM( p_list->pi_types, p_list->i_count,
1151                          p_list->i_count, VLC_VAR_INTEGER );
1152         }
1153         free( psz_orig );
1154         break;
1155     }
1156
1157     default:
1158         goto cleanup;
1159     }
1160
1161     var_Set( p_obj, psz_name, val );
1162
1163 cleanup:
1164     free( psz_name );
1165 }
1166
1167
1168 /* Following functions are local */
1169
1170 /*****************************************************************************
1171  * GetUnused: find an unused variable from its name
1172  *****************************************************************************
1173  * We do i_tries tries before giving up, just in case the variable is being
1174  * modified and called from a callback.
1175  *****************************************************************************/
1176 static int GetUnused( vlc_object_t *p_this, const char *psz_name )
1177 {
1178     int i_var, i_tries = 0;
1179     vlc_object_internals_t *p_priv = vlc_internals( p_this );
1180
1181     while( true )
1182     {
1183         i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
1184         if( i_var < 0 )
1185         {
1186             return VLC_ENOVAR;
1187         }
1188
1189         if( ! p_priv->p_vars[i_var].b_incallback )
1190         {
1191             return i_var;
1192         }
1193
1194         if( i_tries++ > 100 )
1195         {
1196             msg_Err( p_this, "caught in a callback deadlock? ('%s')", psz_name );
1197             return VLC_ETIMEOUT;
1198         }
1199
1200         vlc_mutex_unlock( &p_priv->var_lock );
1201         msleep( THREAD_SLEEP );
1202         vlc_mutex_lock( &p_priv->var_lock );
1203     }
1204 }
1205
1206 /*****************************************************************************
1207  * HashString: our cool hash function
1208  *****************************************************************************
1209  * This function is not intended to be crypto-secure, we only want it to be
1210  * fast and not suck too much. This one is pretty fast and did 0 collisions
1211  * in wenglish's dictionary.
1212  *****************************************************************************/
1213 static uint32_t HashString( const char *psz_string )
1214 {
1215     uint32_t i_hash = 0;
1216
1217     while( *psz_string )
1218     {
1219         i_hash += *psz_string++;
1220         i_hash += i_hash << 10;
1221         i_hash ^= i_hash >> 8;
1222     }
1223
1224     return i_hash;
1225 }
1226
1227 /*****************************************************************************
1228  * Insert: find an empty slot to insert a new variable
1229  *****************************************************************************
1230  * We use a recursive inner function indexed on the hash. This function does
1231  * nothing in the rare cases where a collision may occur, see Lookup()
1232  * to see how we handle them.
1233  * XXX: does this really need to be written recursively?
1234  *****************************************************************************/
1235 static int Insert( variable_t *p_vars, int i_count, const char *psz_name )
1236 {
1237     if( i_count == 0 )
1238     {
1239         return 0;
1240     }
1241
1242     return InsertInner( p_vars, i_count, HashString( psz_name ) );
1243 }
1244
1245 static int InsertInner( variable_t *p_vars, int i_count, uint32_t i_hash )
1246 {
1247     int i_middle;
1248
1249     if( i_hash <= p_vars[0].i_hash )
1250     {
1251         return 0;
1252     }
1253
1254     if( i_hash >= p_vars[i_count - 1].i_hash )
1255     {
1256         return i_count;
1257     }
1258
1259     i_middle = i_count / 2;
1260
1261     /* We know that 0 < i_middle */
1262     if( i_hash < p_vars[i_middle].i_hash )
1263     {
1264         return InsertInner( p_vars, i_middle, i_hash );
1265     }
1266
1267     /* We know that i_middle + 1 < i_count */
1268     if( i_hash > p_vars[i_middle + 1].i_hash )
1269     {
1270         return i_middle + 1 + InsertInner( p_vars + i_middle + 1,
1271                                            i_count - i_middle - 1,
1272                                            i_hash );
1273     }
1274
1275     return i_middle + 1;
1276 }
1277
1278 /*****************************************************************************
1279  * Lookup: find an existing variable given its name
1280  *****************************************************************************
1281  * We use a recursive inner function indexed on the hash. Care is taken of
1282  * possible hash collisions.
1283  * XXX: does this really need to be written recursively?
1284  *****************************************************************************/
1285 static int Lookup( variable_t *p_vars, int i_count, const char *psz_name )
1286 {
1287     uint32_t i_hash;
1288     int i, i_pos;
1289
1290     if( i_count == 0 )
1291     {
1292         return -1;
1293     }
1294
1295     i_hash = HashString( psz_name );
1296
1297     i_pos = LookupInner( p_vars, i_count, i_hash );
1298
1299     /* Hash not found */
1300     if( i_hash != p_vars[i_pos].i_hash )
1301     {
1302         return -1;
1303     }
1304
1305     /* Hash found, entry found */
1306     if( !strcmp( psz_name, p_vars[i_pos].psz_name ) )
1307     {
1308         return i_pos;
1309     }
1310
1311     /* Hash collision! This should be very rare, but we cannot guarantee
1312      * it will never happen. Just do an exhaustive search amongst all
1313      * entries with the same hash. */
1314     for( i = i_pos - 1 ; i > 0 && i_hash == p_vars[i].i_hash ; i-- )
1315     {
1316         if( !strcmp( psz_name, p_vars[i].psz_name ) )
1317         {
1318             return i;
1319         }
1320     }
1321
1322     for( i = i_pos + 1 ; i < i_count && i_hash == p_vars[i].i_hash ; i++ )
1323     {
1324         if( !strcmp( psz_name, p_vars[i].psz_name ) )
1325         {
1326             return i;
1327         }
1328     }
1329
1330     /* Hash found, but entry not found */
1331     return -1;
1332 }
1333
1334 static int LookupInner( variable_t *p_vars, int i_count, uint32_t i_hash )
1335 {
1336     int i_middle;
1337
1338     if( i_hash <= p_vars[0].i_hash )
1339     {
1340         return 0;
1341     }
1342
1343     if( i_hash >= p_vars[i_count-1].i_hash )
1344     {
1345         return i_count - 1;
1346     }
1347
1348     i_middle = i_count / 2;
1349
1350     /* We know that 0 < i_middle */
1351     if( i_hash < p_vars[i_middle].i_hash )
1352     {
1353         return LookupInner( p_vars, i_middle, i_hash );
1354     }
1355
1356     /* We know that i_middle + 1 < i_count */
1357     if( i_hash > p_vars[i_middle].i_hash )
1358     {
1359         return i_middle + LookupInner( p_vars + i_middle,
1360                                        i_count - i_middle,
1361                                        i_hash );
1362     }
1363
1364     return i_middle;
1365 }
1366
1367 /*****************************************************************************
1368  * CheckValue: check that a value is valid wrt. a variable
1369  *****************************************************************************
1370  * This function checks p_val's value against p_var's limitations such as
1371  * minimal and maximal value, step, in-list position, and modifies p_val if
1372  * necessary.
1373  ****************************************************************************/
1374 static void CheckValue ( variable_t *p_var, vlc_value_t *p_val )
1375 {
1376     /* Check that our variable is in the list */
1377     if( p_var->i_type & VLC_VAR_HASCHOICE && p_var->choices.i_count )
1378     {
1379         int i;
1380
1381         /* FIXME: the list is sorted, dude. Use something cleverer. */
1382         for( i = p_var->choices.i_count ; i-- ; )
1383         {
1384             if( p_var->pf_cmp( *p_val, p_var->choices.p_values[i] ) == 0 )
1385             {
1386                 break;
1387             }
1388         }
1389
1390         /* If not found, change it to anything vaguely valid */
1391         if( i < 0 )
1392         {
1393             /* Free the old variable, get the new one, dup it */
1394             p_var->pf_free( p_val );
1395             *p_val = p_var->choices.p_values[p_var->i_default >= 0
1396                                           ? p_var->i_default : 0 ];
1397             p_var->pf_dup( p_val );
1398         }
1399     }
1400
1401     /* Check that our variable is within the bounds */
1402     switch( p_var->i_type & VLC_VAR_TYPE )
1403     {
1404         case VLC_VAR_INTEGER:
1405             if( p_var->i_type & VLC_VAR_HASSTEP && p_var->step.i_int
1406                  && (p_val->i_int % p_var->step.i_int) )
1407             {
1408                 p_val->i_int = (p_val->i_int + (p_var->step.i_int / 2))
1409                                / p_var->step.i_int * p_var->step.i_int;
1410             }
1411             if( p_var->i_type & VLC_VAR_HASMIN
1412                  && p_val->i_int < p_var->min.i_int )
1413             {
1414                 p_val->i_int = p_var->min.i_int;
1415             }
1416             if( p_var->i_type & VLC_VAR_HASMAX
1417                  && p_val->i_int > p_var->max.i_int )
1418             {
1419                 p_val->i_int = p_var->max.i_int;
1420             }
1421             break;
1422         case VLC_VAR_FLOAT:
1423             if( p_var->i_type & VLC_VAR_HASSTEP && p_var->step.f_float )
1424             {
1425                 float f_round = p_var->step.f_float * (float)(int)( 0.5 +
1426                                         p_val->f_float / p_var->step.f_float );
1427                 if( p_val->f_float != f_round )
1428                 {
1429                     p_val->f_float = f_round;
1430                 }
1431             }
1432             if( p_var->i_type & VLC_VAR_HASMIN
1433                  && p_val->f_float < p_var->min.f_float )
1434             {
1435                 p_val->f_float = p_var->min.f_float;
1436             }
1437             if( p_var->i_type & VLC_VAR_HASMAX
1438                  && p_val->f_float > p_var->max.f_float )
1439             {
1440                 p_val->f_float = p_var->max.f_float;
1441             }
1442             break;
1443         case VLC_VAR_TIME:
1444             /* FIXME: TODO */
1445             break;
1446     }
1447 }
1448
1449 /*****************************************************************************
1450  * InheritValue: try to inherit the value of this variable from the same one
1451  *               in our closest parent.
1452  *****************************************************************************/
1453 static int InheritValue( vlc_object_t *p_this, const char *psz_name,
1454                          vlc_value_t *p_val, int i_type )
1455 {
1456     int i_var;
1457     variable_t *p_var;
1458
1459     /* No need to take the structure lock,
1460      * we are only looking for our parents */
1461
1462     if( !p_this->p_parent )
1463     {
1464         switch( i_type & VLC_VAR_TYPE )
1465         {
1466         case VLC_VAR_FILE:
1467         case VLC_VAR_DIRECTORY:
1468         case VLC_VAR_STRING:
1469         case VLC_VAR_MODULE:
1470             p_val->psz_string = config_GetPsz( p_this, psz_name );
1471             if( !p_val->psz_string ) p_val->psz_string = strdup("");
1472             break;
1473         case VLC_VAR_FLOAT:
1474             p_val->f_float = config_GetFloat( p_this, psz_name );
1475             break;
1476         case VLC_VAR_INTEGER:
1477         case VLC_VAR_HOTKEY:
1478             p_val->i_int = config_GetInt( p_this, psz_name );
1479             break;
1480         case VLC_VAR_BOOL:
1481             p_val->b_bool = config_GetInt( p_this, psz_name );
1482             break;
1483         case VLC_VAR_LIST:
1484         {
1485             char *psz_orig, *psz_var;
1486             vlc_list_t *p_list = malloc(sizeof(vlc_list_t));
1487             p_val->p_list = p_list;
1488             p_list->i_count = 0;
1489
1490             psz_var = psz_orig = config_GetPsz( p_this, psz_name );
1491             while( psz_var && *psz_var )
1492             {
1493                 char *psz_item = psz_var;
1494                 vlc_value_t val;
1495                 while( *psz_var && *psz_var != ',' ) psz_var++;
1496                 if( *psz_var == ',' )
1497                 {
1498                     *psz_var = '\0';
1499                     psz_var++;
1500                 }
1501                 val.i_int = strtol( psz_item, NULL, 0 );
1502                 INSERT_ELEM( p_list->p_values, p_list->i_count,
1503                              p_list->i_count, val );
1504                 /* p_list->i_count is incremented twice by INSERT_ELEM */
1505                 p_list->i_count--;
1506                 INSERT_ELEM( p_list->pi_types, p_list->i_count,
1507                              p_list->i_count, VLC_VAR_INTEGER );
1508             }
1509             free( psz_orig );
1510             break;
1511         }
1512         default:
1513             return VLC_ENOOBJ;
1514             break;
1515         }
1516
1517         return VLC_SUCCESS;
1518     }
1519
1520     vlc_object_internals_t *p_priv = vlc_internals( p_this->p_parent );
1521
1522     /* Look for the variable */
1523     vlc_mutex_lock( &p_priv->var_lock );
1524
1525     i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
1526
1527     if( i_var >= 0 )
1528     {
1529         /* We found it! */
1530         p_var = &p_priv->p_vars[i_var];
1531
1532         /* Really get the variable */
1533         *p_val = p_var->val;
1534
1535         /* Duplicate value if needed */
1536         p_var->pf_dup( p_val );
1537
1538         vlc_mutex_unlock( &p_priv->var_lock );
1539         return VLC_SUCCESS;
1540     }
1541
1542     vlc_mutex_unlock( &p_priv->var_lock );
1543
1544     /* We're still not there */
1545
1546     return InheritValue( p_this->p_parent, psz_name, p_val, i_type );
1547 }
1548
1549 /**********************************************************************
1550  * Execute a var command on an object identified by its name
1551  **********************************************************************/
1552 int __var_Command( vlc_object_t *p_this, const char *psz_name,
1553                    const char *psz_cmd, const char *psz_arg, char **psz_msg )
1554 {
1555     vlc_object_t *p_obj = vlc_object_find_name( p_this->p_libvlc,
1556                                                 psz_name, FIND_CHILD );
1557     int i_type, i_ret;
1558
1559     if( !p_obj )
1560     {
1561         if( psz_msg )
1562             *psz_msg = strdup( "Unknown destination object." );
1563         return VLC_ENOOBJ;
1564     }
1565
1566     i_type = var_Type( p_obj, psz_cmd );
1567     if( !( i_type&VLC_VAR_ISCOMMAND ) )
1568     {
1569         vlc_object_release( p_obj );
1570         if( psz_msg )
1571             *psz_msg = strdup( "Variable doesn't exist or isn't a command." );
1572         return VLC_EGENERIC;
1573     }
1574
1575     i_type &= 0xf0;
1576     switch( i_type )
1577     {
1578         case VLC_VAR_INTEGER:
1579             i_ret = var_SetInteger( p_obj, psz_cmd, atoi( psz_arg ) );
1580             break;
1581         case VLC_VAR_FLOAT:
1582             i_ret = var_SetFloat( p_obj, psz_cmd, atof( psz_arg ) );
1583             break;
1584         case VLC_VAR_STRING:
1585             i_ret = var_SetString( p_obj, psz_cmd, psz_arg );
1586             break;
1587         case VLC_VAR_BOOL:
1588             i_ret = var_SetBool( p_obj, psz_cmd, atoi( psz_arg ) );
1589             break;
1590         default:
1591             i_ret = VLC_EGENERIC;
1592             break;
1593     }
1594
1595     vlc_object_release( p_obj );
1596
1597     if( psz_msg )
1598     {
1599         *psz_msg = (char*)malloc( 80 );
1600         sprintf( *psz_msg, "%s on object %s returned %i (%s)",
1601                  psz_cmd, psz_name, i_ret, vlc_error( i_ret ) );
1602     }
1603
1604     return i_ret;
1605 }