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