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