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