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