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