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