]> git.sesse.net Git - vlc/blob - src/misc/variables.c
Avoid ?: GCC-ism
[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_FREELIST:
609             FreeList( p_val );
610             if( p_val2 && p_val2->p_list )
611             {
612                 for( i = 0; i < p_val2->p_list->i_count; i++ )
613                     free( p_val2->p_list->p_values[i].psz_string );
614                 if( p_val2->p_list->i_count )
615                 {
616                     free( p_val2->p_list->p_values );
617                     free( p_val2->p_list->pi_types );
618                 }
619                 free( p_val2->p_list );
620             }
621             break;
622         case VLC_VAR_SETTEXT:
623             free( p_var->psz_text );
624             if( p_val && p_val->psz_string )
625                 p_var->psz_text = strdup( p_val->psz_string );
626             break;
627         case VLC_VAR_GETTEXT:
628             p_val->psz_string = NULL;
629             if( p_var->psz_text )
630             {
631                 p_val->psz_string = strdup( p_var->psz_text );
632             }
633             break;
634         case VLC_VAR_INHERITVALUE:
635             {
636                 vlc_value_t val;
637
638                 if( InheritValue( p_this,
639                                   p_val2 ? p_val2->psz_string :  psz_name,
640                                   &val, p_var->i_type )
641                     == VLC_SUCCESS )
642                 {
643                     /* Duplicate already done */
644
645                     /* Backup needed stuff */
646                     oldval = p_var->val;
647                     /* Check boundaries and list */
648                     CheckValue( p_var, &val );
649                     /* Set the variable */
650                     p_var->val = val;
651                     /* Free data if needed */
652                     p_var->ops->pf_free( &oldval );
653                 }
654
655                 if( p_val )
656                 {
657                     *p_val = p_var->val;
658                     p_var->ops->pf_dup( p_val );
659                 }
660             }
661             break;
662
663         case VLC_VAR_SETISCOMMAND:
664             p_var->i_type |= VLC_VAR_ISCOMMAND;
665             break;
666
667         default:
668             break;
669     }
670
671     vlc_mutex_unlock( &p_priv->var_lock );
672
673     return VLC_SUCCESS;
674 }
675
676 /**
677  * Request a variable's type
678  *
679  * \return The variable type if it exists, or 0 if the
680  * variable could not be found.
681  * \see \ref var_type
682  */
683 int __var_Type( vlc_object_t *p_this, const char *psz_name )
684 {
685     int i_var, i_type;
686     vlc_object_internals_t *p_priv = vlc_internals( p_this );
687
688     vlc_mutex_lock( &p_priv->var_lock );
689
690     i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
691
692     if( i_var < 0 )
693     {
694         vlc_mutex_unlock( &p_priv->var_lock );
695         return 0;
696     }
697
698     i_type = p_priv->p_vars[i_var].i_type;
699
700     vlc_mutex_unlock( &p_priv->var_lock );
701
702     return i_type;
703 }
704
705 int var_SetChecked( vlc_object_t *p_this, const char *psz_name,
706                     int expected_type, vlc_value_t val )
707 {
708     int i_var;
709     variable_t *p_var;
710     vlc_value_t oldval;
711     vlc_object_internals_t *p_priv = vlc_internals( p_this );
712
713     vlc_mutex_lock( &p_priv->var_lock );
714
715     i_var = GetUnused( p_this, psz_name );
716     if( i_var < 0 )
717     {
718         vlc_mutex_unlock( &p_priv->var_lock );
719         return i_var;
720     }
721
722     p_var = &p_priv->p_vars[i_var];
723     assert( (p_var->i_type & VLC_VAR_CLASS) == 0 || expected_type == 0 ||
724             (p_var->i_type & VLC_VAR_CLASS) == expected_type );
725
726     /* Duplicate data if needed */
727     p_var->ops->pf_dup( &val );
728
729     /* Backup needed stuff */
730     oldval = p_var->val;
731
732     /* Check boundaries and list */
733     CheckValue( p_var, &val );
734
735     /* Set the variable */
736     p_var->val = val;
737
738     /* Deal with callbacks. Tell we're in a callback, release the lock,
739      * call stored functions, retake the lock. */
740     if( p_var->i_entries )
741     {
742         int i_var;
743         int i_entries = p_var->i_entries;
744         callback_entry_t *p_entries = p_var->p_entries;
745
746         p_var->b_incallback = true;
747         vlc_mutex_unlock( &p_priv->var_lock );
748
749         /* The real calls */
750         for( ; i_entries-- ; )
751         {
752             p_entries[i_entries].pf_callback( p_this, psz_name, oldval, val,
753                                               p_entries[i_entries].p_data );
754         }
755
756         vlc_mutex_lock( &p_priv->var_lock );
757
758         i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
759         if( i_var < 0 )
760         {
761             msg_Err( p_this, "variable %s has disappeared", psz_name );
762             vlc_mutex_unlock( &p_priv->var_lock );
763             return VLC_ENOVAR;
764         }
765
766         p_var = &p_priv->p_vars[i_var];
767         p_var->b_incallback = false;
768         vlc_cond_broadcast( &p_priv->var_wait );
769     }
770
771     /* Free data if needed */
772     p_var->ops->pf_free( &oldval );
773
774     vlc_mutex_unlock( &p_priv->var_lock );
775
776     return VLC_SUCCESS;
777 }
778
779
780 /**
781  * Set a variable's value
782  *
783  * \param p_this The object that hold the variable
784  * \param psz_name The name of the variable
785  * \param val the value to set
786  */
787 int __var_Set( vlc_object_t *p_this, const char *psz_name, vlc_value_t val )
788 {
789     return var_SetChecked( p_this, psz_name, 0, val );
790 }
791
792 int var_GetChecked( vlc_object_t *p_this, const char *psz_name,
793                     int expected_type, vlc_value_t *p_val )
794 {
795     vlc_object_internals_t *p_priv = vlc_internals( p_this );
796     int i_var, err = VLC_SUCCESS;
797
798     vlc_mutex_lock( &p_priv->var_lock );
799
800     i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
801     if( i_var >= 0 )
802     {
803         variable_t *p_var = &p_priv->p_vars[i_var];
804
805         assert( (p_var->i_type & VLC_VAR_CLASS) == 0 || expected_type == 0 ||
806                 (p_var->i_type & VLC_VAR_CLASS) == expected_type );
807
808         /* Really get the variable */
809         *p_val = p_var->val;
810
811         /* Duplicate value if needed */
812         p_var->ops->pf_dup( p_val );
813     }
814     else
815         err = VLC_ENOVAR;
816
817     vlc_mutex_unlock( &p_priv->var_lock );
818     return err;
819 }
820
821 /**
822  * Get a variable's value
823  *
824  * \param p_this The object that holds the variable
825  * \param psz_name The name of the variable
826  * \param p_val Pointer to a vlc_value_t that will hold the variable's value
827  *              after the function is finished
828  */
829 int __var_Get( vlc_object_t *p_this, const char *psz_name, vlc_value_t *p_val )
830 {
831     return var_GetChecked( p_this, psz_name, 0, p_val );
832 }
833
834 /**
835  * Register a callback in a variable
836  *
837  * We store a function pointer that will be called upon variable
838  * modification.
839  *
840  * \param p_this The object that holds the variable
841  * \param psz_name The name of the variable
842  * \param pf_callback The function pointer
843  * \param p_data A generic pointer that will be passed as the last
844  *               argument to the callback function.
845  *
846  * \warning The callback function is run in the thread that calls var_Set on
847  *          the variable. Use proper locking. This thread may not have much
848  *          time to spare, so keep callback functions short.
849  */
850 int __var_AddCallback( vlc_object_t *p_this, const char *psz_name,
851                        vlc_callback_t pf_callback, void *p_data )
852 {
853     int i_var;
854     variable_t *p_var;
855     callback_entry_t entry;
856     vlc_object_internals_t *p_priv = vlc_internals( p_this );
857
858     entry.pf_callback = pf_callback;
859     entry.p_data = p_data;
860
861     vlc_mutex_lock( &p_priv->var_lock );
862
863     i_var = GetUnused( p_this, psz_name );
864     if( i_var < 0 )
865     {
866         vlc_mutex_unlock( &p_priv->var_lock );
867         return i_var;
868     }
869
870     p_var = &p_priv->p_vars[i_var];
871
872     INSERT_ELEM( p_var->p_entries,
873                  p_var->i_entries,
874                  p_var->i_entries,
875                  entry );
876
877     vlc_mutex_unlock( &p_priv->var_lock );
878
879     return VLC_SUCCESS;
880 }
881
882 /**
883  * Remove a callback from a variable
884  *
885  * pf_callback and p_data have to be given again, because different objects
886  * might have registered the same callback function.
887  */
888 int __var_DelCallback( vlc_object_t *p_this, const char *psz_name,
889                        vlc_callback_t pf_callback, void *p_data )
890 {
891     int i_entry, i_var;
892     variable_t *p_var;
893     vlc_object_internals_t *p_priv = vlc_internals( p_this );
894
895     vlc_mutex_lock( &p_priv->var_lock );
896
897     i_var = GetUnused( p_this, psz_name );
898     if( i_var < 0 )
899     {
900         vlc_mutex_unlock( &p_priv->var_lock );
901         return i_var;
902     }
903
904     p_var = &p_priv->p_vars[i_var];
905
906     for( i_entry = p_var->i_entries ; i_entry-- ; )
907     {
908         if( p_var->p_entries[i_entry].pf_callback == pf_callback
909             && p_var->p_entries[i_entry].p_data == p_data )
910         {
911             break;
912         }
913     }
914
915     if( i_entry < 0 )
916     {
917         vlc_mutex_unlock( &p_priv->var_lock );
918         return VLC_EGENERIC;
919     }
920
921     REMOVE_ELEM( p_var->p_entries, p_var->i_entries, i_entry );
922
923     vlc_mutex_unlock( &p_priv->var_lock );
924
925     return VLC_SUCCESS;
926 }
927
928 /**
929  * Trigger callback on a variable
930  *
931  * \param p_this The object that hold the variable
932  * \param psz_name The name of the variable
933  */
934 int __var_TriggerCallback( vlc_object_t *p_this, const char *psz_name )
935 {
936     int i_var;
937     variable_t *p_var;
938     vlc_value_t oldval;
939     vlc_object_internals_t *p_priv = vlc_internals( p_this );
940
941     vlc_mutex_lock( &p_priv->var_lock );
942
943     i_var = GetUnused( p_this, psz_name );
944     if( i_var < 0 )
945     {
946         vlc_mutex_unlock( &p_priv->var_lock );
947         return i_var;
948     }
949
950     p_var = &p_priv->p_vars[i_var];
951
952     /* Backup needed stuff */
953     oldval = p_var->val;
954
955     /* Deal with callbacks. Tell we're in a callback, release the lock,
956      * call stored functions, retake the lock. */
957     if( p_var->i_entries )
958     {
959         int i_var;
960         int i_entries = p_var->i_entries;
961         callback_entry_t *p_entries = p_var->p_entries;
962
963         p_var->b_incallback = true;
964         vlc_mutex_unlock( &p_priv->var_lock );
965
966         /* The real calls */
967         for( ; i_entries-- ; )
968         {
969             p_entries[i_entries].pf_callback( p_this, psz_name, oldval, oldval,
970                                               p_entries[i_entries].p_data );
971         }
972
973         vlc_mutex_lock( &p_priv->var_lock );
974
975         i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
976         if( i_var < 0 )
977         {
978             msg_Err( p_this, "variable %s has disappeared", psz_name );
979             vlc_mutex_unlock( &p_priv->var_lock );
980             return VLC_ENOVAR;
981         }
982
983         p_var = &p_priv->p_vars[i_var];
984         p_var->b_incallback = false;
985         vlc_cond_broadcast( &p_priv->var_wait );
986     }
987
988     vlc_mutex_unlock( &p_priv->var_lock );
989     return VLC_SUCCESS;
990 }
991
992 /** Parse a stringified option
993  * This function parse a string option and create the associated object
994  * variable
995  * The option must be of the form "[no[-]]foo[=bar]" where foo is the
996  * option name and bar is the value of the option.
997  * \param p_obj the object in which the variable must be created
998  * \param psz_option the option to parse
999  * \param trusted whether the option is set by a trusted input or not
1000  * \return nothing
1001  */
1002 void var_OptionParse( vlc_object_t *p_obj, const char *psz_option,
1003                       bool trusted )
1004 {
1005     char *psz_name, *psz_value;
1006     int  i_type;
1007     bool b_isno = false;
1008     vlc_value_t val;
1009
1010     val.psz_string = NULL;
1011
1012     /* It's too much of a hassle to remove the ':' when we parse
1013      * the cmd line :) */
1014     if( psz_option[0] == ':' )
1015         psz_option++;
1016
1017     if( !psz_option[0] )
1018         return;
1019
1020     psz_name = strdup( psz_option );
1021     if( psz_name == NULL )
1022         return;
1023
1024     psz_value = strchr( psz_name, '=' );
1025     if( psz_value != NULL )
1026         *psz_value++ = '\0';
1027
1028     /* FIXME: :programs should be handled generically */
1029     if( !strcmp( psz_name, "programs" ) )
1030         i_type = VLC_VAR_LIST;
1031     else
1032         i_type = config_GetType( p_obj, psz_name );
1033
1034     if( !i_type && !psz_value )
1035     {
1036         /* check for "no-foo" or "nofoo" */
1037         if( !strncmp( psz_name, "no-", 3 ) )
1038         {
1039             memmove( psz_name, psz_name + 3, strlen(psz_name) + 1 - 3 );
1040         }
1041         else if( !strncmp( psz_name, "no", 2 ) )
1042         {
1043             memmove( psz_name, psz_name + 2, strlen(psz_name) + 1 - 2 );
1044         }
1045         else goto cleanup;           /* Option doesn't exist */
1046
1047         b_isno = true;
1048         i_type = config_GetType( p_obj, psz_name );
1049     }
1050     if( !i_type ) goto cleanup; /* Option doesn't exist */
1051
1052     if( ( i_type != VLC_VAR_BOOL ) &&
1053         ( !psz_value || !*psz_value ) ) goto cleanup; /* Invalid value */
1054
1055     /* check if option is unsafe */
1056     if( !trusted )
1057     {
1058         module_config_t *p_config = config_FindConfig( p_obj, psz_name );
1059         if( !p_config || !p_config->b_safe )
1060         {
1061             msg_Err( p_obj, "unsafe option \"%s\" has been ignored for "
1062                             "security reasons", psz_name );
1063             free( psz_name );
1064             return;
1065         }
1066     }
1067
1068     /* Create the variable in the input object.
1069      * Children of the input object will be able to retreive this value
1070      * thanks to the inheritance property of the object variables. */
1071     var_Create( p_obj, psz_name, i_type );
1072
1073     switch( i_type )
1074     {
1075     case VLC_VAR_BOOL:
1076         val.b_bool = !b_isno;
1077         break;
1078
1079     case VLC_VAR_INTEGER:
1080         val.i_int = strtol( psz_value, NULL, 0 );
1081         break;
1082
1083     case VLC_VAR_FLOAT:
1084         val.f_float = atof( psz_value );
1085         break;
1086
1087     case VLC_VAR_STRING:
1088     case VLC_VAR_MODULE:
1089     case VLC_VAR_FILE:
1090     case VLC_VAR_DIRECTORY:
1091         val.psz_string = psz_value;
1092         break;
1093
1094     case VLC_VAR_LIST:
1095     {
1096         char *psz_orig, *psz_var;
1097         vlc_list_t *p_list = malloc(sizeof(vlc_list_t));
1098         val.p_list = p_list;
1099         p_list->i_count = 0;
1100
1101         psz_var = psz_orig = strdup(psz_value);
1102         while( psz_var && *psz_var )
1103         {
1104             char *psz_item = psz_var;
1105             vlc_value_t val2;
1106             while( *psz_var && *psz_var != ',' ) psz_var++;
1107             if( *psz_var == ',' )
1108             {
1109                 *psz_var = '\0';
1110                 psz_var++;
1111             }
1112             val2.i_int = strtol( psz_item, NULL, 0 );
1113             INSERT_ELEM( p_list->p_values, p_list->i_count,
1114                          p_list->i_count, val2 );
1115             /* p_list->i_count is incremented twice by INSERT_ELEM */
1116             p_list->i_count--;
1117             INSERT_ELEM( p_list->pi_types, p_list->i_count,
1118                          p_list->i_count, VLC_VAR_INTEGER );
1119         }
1120         free( psz_orig );
1121         break;
1122     }
1123
1124     default:
1125         goto cleanup;
1126     }
1127
1128     var_Set( p_obj, psz_name, val );
1129
1130     // If that's a list, remove all elements allocated
1131     if( i_type == VLC_VAR_LIST )
1132         FreeList( &val );
1133
1134 cleanup:
1135     free( psz_name );
1136 }
1137
1138
1139 /* Following functions are local */
1140
1141 /*****************************************************************************
1142  * GetUnused: find an unused (not in callback) variable from its name
1143  *****************************************************************************
1144  * We do i_tries tries before giving up, just in case the variable is being
1145  * modified and called from a callback.
1146  *****************************************************************************/
1147 static int GetUnused( vlc_object_t *p_this, const char *psz_name )
1148 {
1149     vlc_object_internals_t *p_priv = vlc_internals( p_this );
1150
1151     while( true )
1152     {
1153         int i_var;
1154
1155         i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
1156         if( i_var < 0 )
1157         {
1158             return VLC_ENOVAR;
1159         }
1160
1161         if( ! p_priv->p_vars[i_var].b_incallback )
1162         {
1163             return i_var;
1164         }
1165
1166         mutex_cleanup_push( &p_priv->var_lock );
1167         vlc_cond_wait( &p_priv->var_wait, &p_priv->var_lock );
1168         vlc_cleanup_pop( );
1169     }
1170 }
1171
1172 /*****************************************************************************
1173  * HashString: our cool hash function
1174  *****************************************************************************
1175  * This function is not intended to be crypto-secure, we only want it to be
1176  * fast and not suck too much. This one is pretty fast and did 0 collisions
1177  * in wenglish's dictionary.
1178  *****************************************************************************/
1179 static uint32_t HashString( const char *psz_string )
1180 {
1181     uint32_t i_hash = 0;
1182
1183     while( *psz_string )
1184     {
1185         i_hash += *psz_string++;
1186         i_hash += i_hash << 10;
1187         i_hash ^= i_hash >> 8;
1188     }
1189
1190     return i_hash;
1191 }
1192
1193 /*****************************************************************************
1194  * Insert: find an empty slot to insert a new variable
1195  *****************************************************************************
1196  * We use a recursive inner function indexed on the hash. This function does
1197  * nothing in the rare cases where a collision may occur, see Lookup()
1198  * to see how we handle them.
1199  * XXX: does this really need to be written recursively?
1200  *****************************************************************************/
1201 static int Insert( variable_t *p_vars, int i_count, const char *psz_name )
1202 {
1203     if( i_count == 0 )
1204     {
1205         return 0;
1206     }
1207
1208     return InsertInner( p_vars, i_count, HashString( psz_name ) );
1209 }
1210
1211 static int InsertInner( variable_t *p_vars, int i_count, uint32_t i_hash )
1212 {
1213     int i_middle;
1214
1215     if( i_hash <= p_vars[0].i_hash )
1216     {
1217         return 0;
1218     }
1219
1220     if( i_hash >= p_vars[i_count - 1].i_hash )
1221     {
1222         return i_count;
1223     }
1224
1225     i_middle = i_count / 2;
1226
1227     /* We know that 0 < i_middle */
1228     if( i_hash < p_vars[i_middle].i_hash )
1229     {
1230         return InsertInner( p_vars, i_middle, i_hash );
1231     }
1232
1233     /* We know that i_middle + 1 < i_count */
1234     if( i_hash > p_vars[i_middle + 1].i_hash )
1235     {
1236         return i_middle + 1 + InsertInner( p_vars + i_middle + 1,
1237                                            i_count - i_middle - 1,
1238                                            i_hash );
1239     }
1240
1241     return i_middle + 1;
1242 }
1243
1244 static int u32cmp( const void *key, const void *data )
1245 {
1246     const variable_t *p_var = data;
1247     uint32_t hash = *(const uint32_t *)key ;
1248
1249     if( hash > p_var->i_hash )
1250         return 1;
1251     if( hash < p_var->i_hash )
1252         return -1;
1253     return 0;
1254 }
1255
1256 /*****************************************************************************
1257  * Lookup: find an existing variable given its name
1258  *****************************************************************************
1259  * We use a recursive inner function indexed on the hash. Care is taken of
1260  * possible hash collisions.
1261  *****************************************************************************/
1262 static int Lookup( variable_t *p_vars, size_t i_count, const char *psz_name )
1263 {
1264     variable_t *p_var;
1265     uint32_t i_hash;
1266
1267     i_hash = HashString( psz_name );
1268     p_var = bsearch( &i_hash, p_vars, i_count, sizeof( *p_var ), u32cmp );
1269
1270     /* Hash not found */
1271     if( p_var == NULL )
1272         return -1;
1273
1274     assert( i_count > 0 );
1275
1276     /* Find the first entry with the right hash */
1277     while( (p_var > p_vars) && (i_hash == p_var[-1].i_hash) )
1278         p_var--;
1279
1280     assert( p_var->i_hash == i_hash );
1281
1282     /* Hash collision should be very unlikely, but we cannot guarantee
1283      * it will never happen. So we do an exhaustive search amongst all
1284      * entries with the same hash. Typically, there is only one anyway. */
1285     for( variable_t *p_end = p_vars + i_count;
1286          (p_var < p_end) && (i_hash == p_var->i_hash);
1287          p_var++ )
1288     {
1289         if( !strcmp( psz_name, p_var->psz_name ) )
1290             return p_var - p_vars;
1291     }
1292
1293     /* Hash found, but entry not found */
1294     return -1;
1295 }
1296
1297 /*****************************************************************************
1298  * CheckValue: check that a value is valid wrt. a variable
1299  *****************************************************************************
1300  * This function checks p_val's value against p_var's limitations such as
1301  * minimal and maximal value, step, in-list position, and modifies p_val if
1302  * necessary.
1303  ****************************************************************************/
1304 static void CheckValue ( variable_t *p_var, vlc_value_t *p_val )
1305 {
1306     /* Check that our variable is in the list */
1307     if( p_var->i_type & VLC_VAR_HASCHOICE && p_var->choices.i_count )
1308     {
1309         int i;
1310
1311         /* FIXME: the list is sorted, dude. Use something cleverer. */
1312         for( i = p_var->choices.i_count ; i-- ; )
1313         {
1314             if( p_var->ops->pf_cmp( *p_val, p_var->choices.p_values[i] ) == 0 )
1315             {
1316                 break;
1317             }
1318         }
1319
1320         /* If not found, change it to anything vaguely valid */
1321         if( i < 0 )
1322         {
1323             /* Free the old variable, get the new one, dup it */
1324             p_var->ops->pf_free( p_val );
1325             *p_val = p_var->choices.p_values[p_var->i_default >= 0
1326                                           ? p_var->i_default : 0 ];
1327             p_var->ops->pf_dup( p_val );
1328         }
1329     }
1330
1331     /* Check that our variable is within the bounds */
1332     switch( p_var->i_type & VLC_VAR_TYPE )
1333     {
1334         case VLC_VAR_INTEGER:
1335             if( p_var->i_type & VLC_VAR_HASSTEP && p_var->step.i_int
1336                  && (p_val->i_int % p_var->step.i_int) )
1337             {
1338                 p_val->i_int = (p_val->i_int + (p_var->step.i_int / 2))
1339                                / p_var->step.i_int * p_var->step.i_int;
1340             }
1341             if( p_var->i_type & VLC_VAR_HASMIN
1342                  && p_val->i_int < p_var->min.i_int )
1343             {
1344                 p_val->i_int = p_var->min.i_int;
1345             }
1346             if( p_var->i_type & VLC_VAR_HASMAX
1347                  && p_val->i_int > p_var->max.i_int )
1348             {
1349                 p_val->i_int = p_var->max.i_int;
1350             }
1351             break;
1352         case VLC_VAR_FLOAT:
1353             if( p_var->i_type & VLC_VAR_HASSTEP && p_var->step.f_float )
1354             {
1355                 float f_round = p_var->step.f_float * (float)(int)( 0.5 +
1356                                         p_val->f_float / p_var->step.f_float );
1357                 if( p_val->f_float != f_round )
1358                 {
1359                     p_val->f_float = f_round;
1360                 }
1361             }
1362             if( p_var->i_type & VLC_VAR_HASMIN
1363                  && p_val->f_float < p_var->min.f_float )
1364             {
1365                 p_val->f_float = p_var->min.f_float;
1366             }
1367             if( p_var->i_type & VLC_VAR_HASMAX
1368                  && p_val->f_float > p_var->max.f_float )
1369             {
1370                 p_val->f_float = p_var->max.f_float;
1371             }
1372             break;
1373         case VLC_VAR_TIME:
1374             /* FIXME: TODO */
1375             break;
1376     }
1377 }
1378
1379 /*****************************************************************************
1380  * InheritValue: try to inherit the value of this variable from the same one
1381  * in our closest parent, libvlc or ultimately from the configuration.
1382  * The function should always be entered with the object var_lock locked.
1383  *****************************************************************************/
1384 static int InheritValue( vlc_object_t *p_this, const char *psz_name,
1385                          vlc_value_t *p_val, int i_type )
1386 {
1387     int i_var;
1388     variable_t *p_var;
1389
1390     if( p_this->p_parent || ( p_this->p_libvlc && p_this != (vlc_object_t*) p_this->p_libvlc ) )
1391     {
1392         vlc_object_internals_t *p_priv;
1393
1394         if( p_this->p_parent )
1395             p_priv = vlc_internals( p_this->p_parent );
1396         else
1397             p_priv = vlc_internals( p_this->p_libvlc );
1398
1399         i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
1400
1401         if( i_var >= 0 )
1402         {
1403             /* We found it! */
1404             p_var = &p_priv->p_vars[i_var];
1405
1406             /* Really get the variable */
1407             *p_val = p_var->val;
1408
1409             /* Duplicate value if needed */
1410             p_var->ops->pf_dup( p_val );
1411
1412             /*msg_Dbg( p_this, "Inherited value for var %s from object %s",
1413                      psz_name ? psz_name : "(null)",
1414                      p_this->psz_object_name
1415                          ? p_this->psz_object_name : "(Unknown)" );*/
1416             return VLC_SUCCESS;
1417         }
1418         else if ( p_this->p_parent ) /* We are still not there */
1419             return InheritValue( p_this->p_parent, psz_name, p_val, i_type );
1420
1421         /* else take value from config */
1422     }
1423
1424
1425     switch( i_type & VLC_VAR_CLASS )
1426     {
1427         case VLC_VAR_STRING:
1428             p_val->psz_string = config_GetPsz( p_this, psz_name );
1429             if( !p_val->psz_string ) p_val->psz_string = strdup("");
1430             break;
1431         case VLC_VAR_FLOAT:
1432             p_val->f_float = config_GetFloat( p_this, psz_name );
1433             break;
1434         case VLC_VAR_INTEGER:
1435             p_val->i_int = config_GetInt( p_this, psz_name );
1436             break;
1437         case VLC_VAR_BOOL:
1438             p_val->b_bool = config_GetInt( p_this, psz_name );
1439             break;
1440         case VLC_VAR_LIST:
1441         {
1442             char *psz_orig, *psz_var;
1443             vlc_list_t *p_list = malloc(sizeof(vlc_list_t));
1444             p_val->p_list = p_list;
1445             p_list->i_count = 0;
1446
1447             psz_var = psz_orig = config_GetPsz( p_this, psz_name );
1448             while( psz_var && *psz_var )
1449             {
1450                 char *psz_item = psz_var;
1451                 vlc_value_t val;
1452                 while( *psz_var && *psz_var != ',' ) psz_var++;
1453                 if( *psz_var == ',' )
1454                 {
1455                     *psz_var = '\0';
1456                     psz_var++;
1457                 }
1458                 val.i_int = strtol( psz_item, NULL, 0 );
1459                 INSERT_ELEM( p_list->p_values, p_list->i_count,
1460                              p_list->i_count, val );
1461                 /* p_list->i_count is incremented twice by INSERT_ELEM */
1462                 p_list->i_count--;
1463                 INSERT_ELEM( p_list->pi_types, p_list->i_count,
1464                              p_list->i_count, VLC_VAR_INTEGER );
1465             }
1466             free( psz_orig );
1467             break;
1468         }
1469         default:
1470             msg_Warn( p_this, "Could not inherit value for var %s "
1471                               "from config. Invalid Type", psz_name );
1472             return VLC_ENOOBJ;
1473             break;
1474     }
1475     /*msg_Dbg( p_this, "Inherited value for var %s from config", psz_name );*/
1476     return VLC_SUCCESS;
1477 }
1478
1479 /**********************************************************************
1480  * Execute a var command on an object identified by its name
1481  **********************************************************************/
1482 int __var_Command( vlc_object_t *p_this, const char *psz_name,
1483                    const char *psz_cmd, const char *psz_arg, char **psz_msg )
1484 {
1485     vlc_object_t *p_obj = vlc_object_find_name( p_this->p_libvlc,
1486                                                 psz_name, FIND_CHILD );
1487     int i_type, i_ret;
1488
1489     if( !p_obj )
1490     {
1491         if( psz_msg )
1492             *psz_msg = strdup( "Unknown destination object." );
1493         return VLC_ENOOBJ;
1494     }
1495
1496     i_type = var_Type( p_obj, psz_cmd );
1497     if( !( i_type&VLC_VAR_ISCOMMAND ) )
1498     {
1499         vlc_object_release( p_obj );
1500         if( psz_msg )
1501             *psz_msg = strdup( "Variable doesn't exist or isn't a command." );
1502         return VLC_EGENERIC;
1503     }
1504
1505     i_type &= VLC_VAR_CLASS;
1506     switch( i_type )
1507     {
1508         case VLC_VAR_INTEGER:
1509             i_ret = var_SetInteger( p_obj, psz_cmd, atoi( psz_arg ) );
1510             break;
1511         case VLC_VAR_FLOAT:
1512             i_ret = var_SetFloat( p_obj, psz_cmd, atof( psz_arg ) );
1513             break;
1514         case VLC_VAR_STRING:
1515             i_ret = var_SetString( p_obj, psz_cmd, psz_arg );
1516             break;
1517         case VLC_VAR_BOOL:
1518             i_ret = var_SetBool( p_obj, psz_cmd, atoi( psz_arg ) );
1519             break;
1520         default:
1521             i_ret = VLC_EGENERIC;
1522             break;
1523     }
1524
1525     vlc_object_release( p_obj );
1526
1527     if( psz_msg )
1528     {
1529         if( asprintf( psz_msg, "%s on object %s returned %i (%s)",
1530                   psz_cmd, psz_name, i_ret, vlc_error( i_ret ) ) == -1)
1531             *psz_msg = NULL;
1532     }
1533
1534     return i_ret;
1535 }