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