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