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