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