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