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