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