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