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