]> git.sesse.net Git - vlc/blob - src/misc/variables.c
0a44246a0e937428a798a234dd095e9c6cda757e
[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 = strchr( psz_option, '=' );
1049     int  i_name_len, i_type;
1050     vlc_bool_t b_isno = VLC_FALSE;
1051     vlc_value_t val;
1052     val.psz_string = NULL;
1053
1054     if( psz_value ) i_name_len = psz_value - psz_option;
1055     else i_name_len = strlen( psz_option );
1056
1057     /* It's too much of a hassle to remove the ':' when we parse
1058      * the cmd line :) */
1059     if( i_name_len && *psz_option == ':' )
1060     {
1061         psz_option++;
1062         i_name_len--;
1063     }
1064
1065     if( i_name_len == 0 ) return;
1066
1067     psz_name = strndup( psz_option, i_name_len );
1068     if( psz_value ) psz_value++;
1069
1070     /* FIXME: :programs should be handled generically */
1071     if( !strcmp( psz_name, "programs" ) )
1072         i_type = VLC_VAR_LIST;
1073     else
1074         i_type = config_GetType( p_obj, psz_name );
1075
1076     if( !i_type && !psz_value )
1077     {
1078         /* check for "no-foo" or "nofoo" */
1079         if( !strncmp( psz_name, "no-", 3 ) )
1080         {
1081             memmove( psz_name, psz_name + 3, strlen(psz_name) + 1 - 3 );
1082         }
1083         else if( !strncmp( psz_name, "no", 2 ) )
1084         {
1085             memmove( psz_name, psz_name + 2, strlen(psz_name) + 1 - 2 );
1086         }
1087         else goto cleanup;           /* Option doesn't exist */
1088
1089         b_isno = VLC_TRUE;
1090         i_type = config_GetType( p_obj, psz_name );
1091
1092         if( !i_type ) goto cleanup;  /* Option doesn't exist */
1093     }
1094     else if( !i_type ) goto cleanup; /* Option doesn't exist */
1095
1096     if( ( i_type != VLC_VAR_BOOL ) &&
1097         ( !psz_value || !*psz_value ) ) goto cleanup; /* Invalid value */
1098
1099     /* check if option is unsafe */
1100     {
1101         module_config_t *p_config = config_FindConfig( p_obj, psz_name );
1102         if( p_config->b_unsafe )
1103         {
1104             int policy = config_GetInt( p_obj, "security-policy" );
1105             switch( policy )
1106             {
1107                 case 0: /* block */
1108                     msg_Err( p_obj, "option %s is unsafe and is blocked by security policy", psz_name );
1109                     return;
1110                 case 1: /* allow */
1111                     break;
1112                 case 2: /* prompt */
1113                 {
1114                     char description[256];
1115                     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);
1116                     if( DIALOG_OK_YES != intf_UserYesNo( p_obj, _("WARNING: Unsafe Playlist"), description, _("Yes"), _("No"), NULL) )
1117                     {
1118                         msg_Err( p_obj, "option %s is unsafe and is blocked by security policy", psz_name );
1119                         return;
1120                     }
1121                 }
1122                 default:
1123                     ;
1124             }
1125         }
1126     }
1127
1128     /* Create the variable in the input object.
1129      * Children of the input object will be able to retreive this value
1130      * thanks to the inheritance property of the object variables. */
1131     var_Create( p_obj, psz_name, i_type );
1132
1133     switch( i_type )
1134     {
1135     case VLC_VAR_BOOL:
1136         val.b_bool = !b_isno;
1137         break;
1138
1139     case VLC_VAR_INTEGER:
1140         val.i_int = strtol( psz_value, NULL, 0 );
1141         break;
1142
1143     case VLC_VAR_FLOAT:
1144         val.f_float = atof( psz_value );
1145         break;
1146
1147     case VLC_VAR_STRING:
1148     case VLC_VAR_MODULE:
1149     case VLC_VAR_FILE:
1150     case VLC_VAR_DIRECTORY:
1151         val.psz_string = psz_value;
1152         break;
1153
1154     case VLC_VAR_LIST:
1155     {
1156         char *psz_orig, *psz_var;
1157         vlc_list_t *p_list = malloc(sizeof(vlc_list_t));
1158         val.p_list = p_list;
1159         p_list->i_count = 0;
1160
1161         psz_var = psz_orig = strdup(psz_value);
1162         while( psz_var && *psz_var )
1163         {
1164             char *psz_item = psz_var;
1165             vlc_value_t val2;
1166             while( *psz_var && *psz_var != ',' ) psz_var++;
1167             if( *psz_var == ',' )
1168             {
1169                 *psz_var = '\0';
1170                 psz_var++;
1171             }
1172             val2.i_int = strtol( psz_item, NULL, 0 );
1173             INSERT_ELEM( p_list->p_values, p_list->i_count,
1174                          p_list->i_count, val2 );
1175             /* p_list->i_count is incremented twice by INSERT_ELEM */
1176             p_list->i_count--;
1177             INSERT_ELEM( p_list->pi_types, p_list->i_count,
1178                          p_list->i_count, VLC_VAR_INTEGER );
1179         }
1180         if( psz_orig ) free( psz_orig );
1181         break;
1182     }
1183
1184     default:
1185         goto cleanup;
1186         break;
1187     }
1188
1189     var_Set( p_obj, psz_name, val );
1190
1191   cleanup:
1192     if( psz_name ) free( psz_name );
1193     return;
1194 }
1195
1196
1197 /* Following functions are local */
1198
1199 /*****************************************************************************
1200  * GetUnused: find an unused variable from its name
1201  *****************************************************************************
1202  * We do i_tries tries before giving up, just in case the variable is being
1203  * modified and called from a callback.
1204  *****************************************************************************/
1205 static int GetUnused( vlc_object_t *p_this, const char *psz_name )
1206 {
1207     int i_var, i_tries = 0;
1208     vlc_object_internals_t *p_priv = p_this->p_internals;
1209
1210     while( VLC_TRUE )
1211     {
1212         i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
1213         if( i_var < 0 )
1214         {
1215             return VLC_ENOVAR;
1216         }
1217
1218         if( ! p_priv->p_vars[i_var].b_incallback )
1219         {
1220             return i_var;
1221         }
1222
1223         if( i_tries++ > 100 )
1224         {
1225             msg_Err( p_this, "caught in a callback deadlock?" );
1226             return VLC_ETIMEOUT;
1227         }
1228
1229         vlc_mutex_unlock( &p_priv->var_lock );
1230         msleep( THREAD_SLEEP );
1231         vlc_mutex_lock( &p_priv->var_lock );
1232     }
1233 }
1234
1235 /*****************************************************************************
1236  * HashString: our cool hash function
1237  *****************************************************************************
1238  * This function is not intended to be crypto-secure, we only want it to be
1239  * fast and not suck too much. This one is pretty fast and did 0 collisions
1240  * in wenglish's dictionary.
1241  *****************************************************************************/
1242 static uint32_t HashString( const char *psz_string )
1243 {
1244     uint32_t i_hash = 0;
1245
1246     while( *psz_string )
1247     {
1248         i_hash += *psz_string++;
1249         i_hash += i_hash << 10;
1250         i_hash ^= i_hash >> 8;
1251     }
1252
1253     return i_hash;
1254 }
1255
1256 /*****************************************************************************
1257  * Insert: find an empty slot to insert a new variable
1258  *****************************************************************************
1259  * We use a recursive inner function indexed on the hash. This function does
1260  * nothing in the rare cases where a collision may occur, see Lookup()
1261  * to see how we handle them.
1262  * XXX: does this really need to be written recursively?
1263  *****************************************************************************/
1264 static int Insert( variable_t *p_vars, int i_count, const char *psz_name )
1265 {
1266     if( i_count == 0 )
1267     {
1268         return 0;
1269     }
1270
1271     return InsertInner( p_vars, i_count, HashString( psz_name ) );
1272 }
1273
1274 static int InsertInner( variable_t *p_vars, int i_count, uint32_t i_hash )
1275 {
1276     int i_middle;
1277
1278     if( i_hash <= p_vars[0].i_hash )
1279     {
1280         return 0;
1281     }
1282
1283     if( i_hash >= p_vars[i_count - 1].i_hash )
1284     {
1285         return i_count;
1286     }
1287
1288     i_middle = i_count / 2;
1289
1290     /* We know that 0 < i_middle */
1291     if( i_hash < p_vars[i_middle].i_hash )
1292     {
1293         return InsertInner( p_vars, i_middle, i_hash );
1294     }
1295
1296     /* We know that i_middle + 1 < i_count */
1297     if( i_hash > p_vars[i_middle + 1].i_hash )
1298     {
1299         return i_middle + 1 + InsertInner( p_vars + i_middle + 1,
1300                                            i_count - i_middle - 1,
1301                                            i_hash );
1302     }
1303
1304     return i_middle + 1;
1305 }
1306
1307 /*****************************************************************************
1308  * Lookup: find an existing variable given its name
1309  *****************************************************************************
1310  * We use a recursive inner function indexed on the hash. Care is taken of
1311  * possible hash collisions.
1312  * XXX: does this really need to be written recursively?
1313  *****************************************************************************/
1314 static int Lookup( variable_t *p_vars, int i_count, const char *psz_name )
1315 {
1316     uint32_t i_hash;
1317     int i, i_pos;
1318
1319     if( i_count == 0 )
1320     {
1321         return -1;
1322     }
1323
1324     i_hash = HashString( psz_name );
1325
1326     i_pos = LookupInner( p_vars, i_count, i_hash );
1327
1328     /* Hash not found */
1329     if( i_hash != p_vars[i_pos].i_hash )
1330     {
1331         return -1;
1332     }
1333
1334     /* Hash found, entry found */
1335     if( !strcmp( psz_name, p_vars[i_pos].psz_name ) )
1336     {
1337         return i_pos;
1338     }
1339
1340     /* Hash collision! This should be very rare, but we cannot guarantee
1341      * it will never happen. Just do an exhaustive search amongst all
1342      * entries with the same hash. */
1343     for( i = i_pos - 1 ; i > 0 && i_hash == p_vars[i].i_hash ; i-- )
1344     {
1345         if( !strcmp( psz_name, p_vars[i].psz_name ) )
1346         {
1347             return i;
1348         }
1349     }
1350
1351     for( i = i_pos + 1 ; i < i_count && i_hash == p_vars[i].i_hash ; i++ )
1352     {
1353         if( !strcmp( psz_name, p_vars[i].psz_name ) )
1354         {
1355             return i;
1356         }
1357     }
1358
1359     /* Hash found, but entry not found */
1360     return -1;
1361 }
1362
1363 static int LookupInner( variable_t *p_vars, int i_count, uint32_t i_hash )
1364 {
1365     int i_middle;
1366
1367     if( i_hash <= p_vars[0].i_hash )
1368     {
1369         return 0;
1370     }
1371
1372     if( i_hash >= p_vars[i_count-1].i_hash )
1373     {
1374         return i_count - 1;
1375     }
1376
1377     i_middle = i_count / 2;
1378
1379     /* We know that 0 < i_middle */
1380     if( i_hash < p_vars[i_middle].i_hash )
1381     {
1382         return LookupInner( p_vars, i_middle, i_hash );
1383     }
1384
1385     /* We know that i_middle + 1 < i_count */
1386     if( i_hash > p_vars[i_middle].i_hash )
1387     {
1388         return i_middle + LookupInner( p_vars + i_middle,
1389                                        i_count - i_middle,
1390                                        i_hash );
1391     }
1392
1393     return i_middle;
1394 }
1395
1396 /*****************************************************************************
1397  * CheckValue: check that a value is valid wrt. a variable
1398  *****************************************************************************
1399  * This function checks p_val's value against p_var's limitations such as
1400  * minimal and maximal value, step, in-list position, and modifies p_val if
1401  * necessary.
1402  ****************************************************************************/
1403 static void CheckValue ( variable_t *p_var, vlc_value_t *p_val )
1404 {
1405     /* Check that our variable is in the list */
1406     if( p_var->i_type & VLC_VAR_HASCHOICE && p_var->choices.i_count )
1407     {
1408         int i;
1409
1410         /* FIXME: the list is sorted, dude. Use something cleverer. */
1411         for( i = p_var->choices.i_count ; i-- ; )
1412         {
1413             if( p_var->pf_cmp( *p_val, p_var->choices.p_values[i] ) == 0 )
1414             {
1415                 break;
1416             }
1417         }
1418
1419         /* If not found, change it to anything vaguely valid */
1420         if( i < 0 )
1421         {
1422             /* Free the old variable, get the new one, dup it */
1423             p_var->pf_free( p_val );
1424             *p_val = p_var->choices.p_values[p_var->i_default >= 0
1425                                           ? p_var->i_default : 0 ];
1426             p_var->pf_dup( p_val );
1427         }
1428     }
1429
1430     /* Check that our variable is within the bounds */
1431     switch( p_var->i_type & VLC_VAR_TYPE )
1432     {
1433         case VLC_VAR_INTEGER:
1434             if( p_var->i_type & VLC_VAR_HASSTEP && p_var->step.i_int
1435                  && (p_val->i_int % p_var->step.i_int) )
1436             {
1437                 p_val->i_int = (p_val->i_int + (p_var->step.i_int / 2))
1438                                / p_var->step.i_int * p_var->step.i_int;
1439             }
1440             if( p_var->i_type & VLC_VAR_HASMIN
1441                  && p_val->i_int < p_var->min.i_int )
1442             {
1443                 p_val->i_int = p_var->min.i_int;
1444             }
1445             if( p_var->i_type & VLC_VAR_HASMAX
1446                  && p_val->i_int > p_var->max.i_int )
1447             {
1448                 p_val->i_int = p_var->max.i_int;
1449             }
1450             break;
1451         case VLC_VAR_FLOAT:
1452             if( p_var->i_type & VLC_VAR_HASSTEP && p_var->step.f_float )
1453             {
1454                 float f_round = p_var->step.f_float * (float)(int)( 0.5 +
1455                                         p_val->f_float / p_var->step.f_float );
1456                 if( p_val->f_float != f_round )
1457                 {
1458                     p_val->f_float = f_round;
1459                 }
1460             }
1461             if( p_var->i_type & VLC_VAR_HASMIN
1462                  && p_val->f_float < p_var->min.f_float )
1463             {
1464                 p_val->f_float = p_var->min.f_float;
1465             }
1466             if( p_var->i_type & VLC_VAR_HASMAX
1467                  && p_val->f_float > p_var->max.f_float )
1468             {
1469                 p_val->f_float = p_var->max.f_float;
1470             }
1471             break;
1472         case VLC_VAR_TIME:
1473             /* FIXME: TODO */
1474             break;
1475     }
1476 }
1477
1478 /*****************************************************************************
1479  * InheritValue: try to inherit the value of this variable from the same one
1480  *               in our closest parent.
1481  *****************************************************************************/
1482 static int InheritValue( vlc_object_t *p_this, const char *psz_name,
1483                          vlc_value_t *p_val, int i_type )
1484 {
1485     int i_var;
1486     variable_t *p_var;
1487
1488     /* No need to take the structure lock,
1489      * we are only looking for our parents */
1490
1491     if( !p_this->p_parent )
1492     {
1493         switch( i_type & VLC_VAR_TYPE )
1494         {
1495         case VLC_VAR_FILE:
1496         case VLC_VAR_DIRECTORY:
1497         case VLC_VAR_STRING:
1498         case VLC_VAR_MODULE:
1499             p_val->psz_string = config_GetPsz( p_this, psz_name );
1500             if( !p_val->psz_string ) p_val->psz_string = strdup("");
1501             break;
1502         case VLC_VAR_FLOAT:
1503             p_val->f_float = config_GetFloat( p_this, psz_name );
1504             break;
1505         case VLC_VAR_INTEGER:
1506         case VLC_VAR_HOTKEY:
1507             p_val->i_int = config_GetInt( p_this, psz_name );
1508             break;
1509         case VLC_VAR_BOOL:
1510             p_val->b_bool = config_GetInt( p_this, psz_name );
1511             break;
1512         case VLC_VAR_LIST:
1513         {
1514             char *psz_orig, *psz_var;
1515             vlc_list_t *p_list = malloc(sizeof(vlc_list_t));
1516             p_val->p_list = p_list;
1517             p_list->i_count = 0;
1518
1519             psz_var = psz_orig = config_GetPsz( p_this, psz_name );
1520             while( psz_var && *psz_var )
1521             {
1522                 char *psz_item = psz_var;
1523                 vlc_value_t val;
1524                 while( *psz_var && *psz_var != ',' ) psz_var++;
1525                 if( *psz_var == ',' )
1526                 {
1527                     *psz_var = '\0';
1528                     psz_var++;
1529                 }
1530                 val.i_int = strtol( psz_item, NULL, 0 );
1531                 INSERT_ELEM( p_list->p_values, p_list->i_count,
1532                              p_list->i_count, val );
1533                 /* p_list->i_count is incremented twice by INSERT_ELEM */
1534                 p_list->i_count--;
1535                 INSERT_ELEM( p_list->pi_types, p_list->i_count,
1536                              p_list->i_count, VLC_VAR_INTEGER );
1537             }
1538             if( psz_orig ) free( psz_orig );
1539             break;
1540         }
1541         default:
1542             return VLC_ENOOBJ;
1543             break;
1544         }
1545
1546         return VLC_SUCCESS;
1547     }
1548
1549     vlc_object_internals_t *p_priv = p_this->p_parent->p_internals;
1550
1551     /* Look for the variable */
1552     vlc_mutex_lock( &p_priv->var_lock );
1553
1554     i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
1555
1556     if( i_var >= 0 )
1557     {
1558         /* We found it! */
1559         p_var = &p_priv->p_vars[i_var];
1560
1561         /* Really get the variable */
1562         *p_val = p_var->val;
1563
1564         /* Duplicate value if needed */
1565         p_var->pf_dup( p_val );
1566
1567         vlc_mutex_unlock( &p_priv->var_lock );
1568         return VLC_SUCCESS;
1569     }
1570
1571     vlc_mutex_unlock( &p_priv->var_lock );
1572
1573     /* We're still not there */
1574
1575     return InheritValue( p_this->p_parent, psz_name, p_val, i_type );
1576 }
1577
1578 /**********************************************************************
1579  * Execute a var command on an object identified by its name
1580  **********************************************************************/
1581 int __var_Command( vlc_object_t *p_this, const char *psz_name,
1582                    const char *psz_cmd, const char *psz_arg, char **psz_msg )
1583 {
1584     vlc_object_t *p_obj = vlc_object_find_name( p_this->p_libvlc,
1585                                                 psz_name, FIND_CHILD );
1586     int i_type, i_ret;
1587
1588     if( !p_obj )
1589     {
1590         if( psz_msg )
1591             *psz_msg = strdup( "Unknown destination object." );
1592         return VLC_ENOOBJ;
1593     }
1594
1595     i_type = var_Type( p_obj, psz_cmd );
1596     if( !( i_type&VLC_VAR_ISCOMMAND ) )
1597     {
1598         vlc_object_release( p_obj );
1599         if( psz_msg )
1600             *psz_msg = strdup( "Variable doesn't exist or isn't a command." );
1601         return VLC_EGENERIC;
1602     }
1603
1604     i_type &= 0xf0;
1605     switch( i_type )
1606     {
1607         case VLC_VAR_INTEGER:
1608             i_ret = var_SetInteger( p_obj, psz_cmd, atoi( psz_arg ) );
1609             break;
1610         case VLC_VAR_FLOAT:
1611             i_ret = var_SetFloat( p_obj, psz_cmd, atof( psz_arg ) );
1612             break;
1613         case VLC_VAR_STRING:
1614             i_ret = var_SetString( p_obj, psz_cmd, psz_arg );
1615             break;
1616         case VLC_VAR_BOOL:
1617             i_ret = var_SetBool( p_obj, psz_cmd, atoi( psz_arg ) );
1618             break;
1619         default:
1620             i_ret = VLC_EGENERIC;
1621             break;
1622     }
1623
1624     vlc_object_release( p_obj );
1625
1626     if( psz_msg )
1627     {
1628         *psz_msg = (char*)malloc( 80 );
1629         sprintf( *psz_msg, "%s on object %s returned %i (%s)",
1630                  psz_cmd, psz_name, i_ret, vlc_error( i_ret ) );
1631     }
1632
1633     return i_ret;
1634 }