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