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