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