]> git.sesse.net Git - vlc/blob - src/misc/variables.c
Simplify and fix WinCE build
[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     variable_t *p_var;
395     vlc_value_t oldval;
396     vlc_value_t newval;
397
398     assert( p_this );
399
400     vlc_object_internals_t *p_priv = vlc_internals( p_this );
401
402     vlc_mutex_lock( &p_priv->var_lock );
403
404     p_var = Lookup( p_this, psz_name );
405     if( p_var == NULL )
406     {
407         vlc_mutex_unlock( &p_priv->var_lock );
408         return VLC_ENOVAR;
409     }
410
411     switch( i_action )
412     {
413         case VLC_VAR_SETMIN:
414             if( p_var->i_type & VLC_VAR_HASMIN )
415             {
416                 p_var->ops->pf_free( &p_var->min );
417             }
418             p_var->i_type |= VLC_VAR_HASMIN;
419             p_var->min = *p_val;
420             p_var->ops->pf_dup( &p_var->min );
421             CheckValue( p_var, &p_var->val );
422             break;
423         case VLC_VAR_GETMIN:
424             if( p_var->i_type & VLC_VAR_HASMIN )
425             {
426                 *p_val = p_var->min;
427             }
428             break;
429         case VLC_VAR_SETMAX:
430             if( p_var->i_type & VLC_VAR_HASMAX )
431             {
432                 p_var->ops->pf_free( &p_var->max );
433             }
434             p_var->i_type |= VLC_VAR_HASMAX;
435             p_var->max = *p_val;
436             p_var->ops->pf_dup( &p_var->max );
437             CheckValue( p_var, &p_var->val );
438             break;
439         case VLC_VAR_GETMAX:
440             if( p_var->i_type & VLC_VAR_HASMAX )
441             {
442                 *p_val = p_var->max;
443             }
444             break;
445         case VLC_VAR_SETSTEP:
446             if( p_var->i_type & VLC_VAR_HASSTEP )
447             {
448                 p_var->ops->pf_free( &p_var->step );
449             }
450             p_var->i_type |= VLC_VAR_HASSTEP;
451             p_var->step = *p_val;
452             p_var->ops->pf_dup( &p_var->step );
453             CheckValue( p_var, &p_var->val );
454             break;
455         case VLC_VAR_GETSTEP:
456             if( p_var->i_type & VLC_VAR_HASSTEP )
457             {
458                 *p_val = p_var->step;
459             }
460             break;
461         case VLC_VAR_ADDCHOICE:
462             i = p_var->choices.i_count;
463
464             INSERT_ELEM( p_var->choices.p_values, p_var->choices.i_count,
465                          i, *p_val );
466             INSERT_ELEM( p_var->choices_text.p_values,
467                          p_var->choices_text.i_count, i, *p_val );
468             p_var->ops->pf_dup( &p_var->choices.p_values[i] );
469             p_var->choices_text.p_values[i].psz_string =
470                 ( p_val2 && p_val2->psz_string ) ?
471                 strdup( p_val2->psz_string ) : NULL;
472
473             CheckValue( p_var, &p_var->val );
474             break;
475         case VLC_VAR_DELCHOICE:
476             for( i = 0 ; i < p_var->choices.i_count ; i++ )
477             {
478                 if( p_var->ops->pf_cmp( p_var->choices.p_values[i], *p_val ) == 0 )
479                 {
480                     break;
481                 }
482             }
483
484             if( i == p_var->choices.i_count )
485             {
486                 /* Not found */
487                 vlc_mutex_unlock( &p_priv->var_lock );
488                 return VLC_EGENERIC;
489             }
490
491             if( p_var->i_default > i )
492             {
493                 p_var->i_default--;
494             }
495             else if( p_var->i_default == i )
496             {
497                 p_var->i_default = -1;
498             }
499
500             p_var->ops->pf_free( &p_var->choices.p_values[i] );
501             free( p_var->choices_text.p_values[i].psz_string );
502             REMOVE_ELEM( p_var->choices.p_values, p_var->choices.i_count, i );
503             REMOVE_ELEM( p_var->choices_text.p_values,
504                          p_var->choices_text.i_count, i );
505
506             CheckValue( p_var, &p_var->val );
507             break;
508         case VLC_VAR_CHOICESCOUNT:
509             p_val->i_int = p_var->choices.i_count;
510             break;
511         case VLC_VAR_CLEARCHOICES:
512             for( i = 0 ; i < p_var->choices.i_count ; i++ )
513             {
514                 p_var->ops->pf_free( &p_var->choices.p_values[i] );
515             }
516             for( i = 0 ; i < p_var->choices_text.i_count ; i++ )
517                 free( p_var->choices_text.p_values[i].psz_string );
518
519             if( p_var->choices.i_count ) free( p_var->choices.p_values );
520             if( p_var->choices_text.i_count ) free( p_var->choices_text.p_values );
521
522             p_var->choices.i_count = 0;
523             p_var->choices.p_values = NULL;
524             p_var->choices_text.i_count = 0;
525             p_var->choices_text.p_values = NULL;
526             p_var->i_default = -1;
527             break;
528         case VLC_VAR_SETDEFAULT:
529             /* FIXME: the list is sorted, dude. Use something cleverer. */
530             for( i = 0 ; i < p_var->choices.i_count ; i++ )
531             {
532                 if( p_var->ops->pf_cmp( p_var->choices.p_values[i], *p_val ) == 0 )
533                 {
534                     break;
535                 }
536             }
537
538             if( i == p_var->choices.i_count )
539             {
540                 /* Not found */
541                 break;
542             }
543
544             p_var->i_default = i;
545             CheckValue( p_var, &p_var->val );
546             break;
547         case VLC_VAR_SETVALUE:
548             /* Duplicate data if needed */
549             newval = *p_val;
550             p_var->ops->pf_dup( &newval );
551             /* Backup needed stuff */
552             oldval = p_var->val;
553             /* Check boundaries and list */
554             CheckValue( p_var, &newval );
555             /* Set the variable */
556             p_var->val = newval;
557             /* Free data if needed */
558             p_var->ops->pf_free( &oldval );
559             break;
560         case VLC_VAR_GETCHOICES:
561         case VLC_VAR_GETLIST:
562             p_val->p_list = malloc( sizeof(vlc_list_t) );
563             if( p_val2 ) p_val2->p_list = malloc( sizeof(vlc_list_t) );
564             if( p_var->choices.i_count )
565             {
566                 p_val->p_list->p_values = malloc( p_var->choices.i_count
567                                                   * sizeof(vlc_value_t) );
568                 p_val->p_list->pi_types = malloc( p_var->choices.i_count
569                                                   * sizeof(int) );
570                 if( p_val2 )
571                 {
572                     p_val2->p_list->p_values =
573                         malloc( p_var->choices.i_count * sizeof(vlc_value_t) );
574                     p_val2->p_list->pi_types =
575                         malloc( p_var->choices.i_count * sizeof(int) );
576                 }
577             }
578             p_val->p_list->i_count = p_var->choices.i_count;
579             if( p_val2 ) p_val2->p_list->i_count = p_var->choices.i_count;
580             for( i = 0 ; i < p_var->choices.i_count ; i++ )
581             {
582                 p_val->p_list->p_values[i] = p_var->choices.p_values[i];
583                 p_val->p_list->pi_types[i] = p_var->i_type;
584                 p_var->ops->pf_dup( &p_val->p_list->p_values[i] );
585                 if( p_val2 )
586                 {
587                     p_val2->p_list->p_values[i].psz_string =
588                         p_var->choices_text.p_values[i].psz_string ?
589                     strdup(p_var->choices_text.p_values[i].psz_string) : NULL;
590                     p_val2->p_list->pi_types[i] = VLC_VAR_STRING;
591                 }
592             }
593             break;
594         case VLC_VAR_SETTEXT:
595             free( p_var->psz_text );
596             if( p_val && p_val->psz_string )
597                 p_var->psz_text = strdup( p_val->psz_string );
598             else
599                 p_var->psz_text = NULL;
600             break;
601         case VLC_VAR_GETTEXT:
602             p_val->psz_string = p_var->psz_text ? strdup( p_var->psz_text )
603                                                 : NULL;
604             break;
605         case VLC_VAR_SETISCOMMAND:
606             p_var->i_type |= VLC_VAR_ISCOMMAND;
607             break;
608
609         default:
610             break;
611     }
612
613     vlc_mutex_unlock( &p_priv->var_lock );
614
615     return VLC_SUCCESS;
616 }
617
618 #undef var_GetAndSet
619 /**
620  * Perform a Get and Set on a variable
621  *
622  * \param p_this: The object that hold the variable
623  * \param psz_name: the name of the variable
624  * \param i_action: the action to perform
625  * \param p_val: The action parameter
626  * \return vlc error codes
627  */
628 int var_GetAndSet( vlc_object_t *p_this, const char *psz_name, int i_action,
629                    vlc_value_t *p_val )
630 {
631     int i_ret;
632     variable_t *p_var;
633     vlc_value_t oldval;
634
635     assert( p_this );
636     assert( p_val );
637
638     vlc_object_internals_t *p_priv = vlc_internals( p_this );
639
640     vlc_mutex_lock( &p_priv->var_lock );
641     p_var = Lookup( p_this, psz_name );
642     if( p_var == NULL )
643     {
644         vlc_mutex_unlock( &p_priv->var_lock );
645         return VLC_ENOVAR;
646     }
647
648     WaitUnused( p_this, p_var );
649
650     /* Duplicated data if needed */
651     //p_var->ops->pf_dup( &val );
652
653     /* Backup needed stuff */
654     oldval = p_var->val;
655
656     /* depending of the action requiered */
657     switch( i_action )
658     {
659     case VLC_VAR_BOOL_TOGGLE:
660         assert( ( p_var->i_type & VLC_VAR_BOOL ) == VLC_VAR_BOOL );
661         p_var->val.b_bool = !p_var->val.b_bool;
662         break;
663     case VLC_VAR_INTEGER_ADD:
664         assert( ( p_var->i_type & VLC_VAR_INTEGER ) == VLC_VAR_INTEGER );
665         p_var->val.i_int += p_val->i_int;
666         break;
667     case VLC_VAR_INTEGER_OR:
668         assert( ( p_var->i_type & VLC_VAR_INTEGER ) == VLC_VAR_INTEGER );
669         p_var->val.i_int |= p_val->i_int;
670         break;
671     case VLC_VAR_INTEGER_NAND:
672         assert( ( p_var->i_type & VLC_VAR_INTEGER ) == VLC_VAR_INTEGER );
673         p_var->val.i_int &= ~p_val->i_int;
674         break;
675     default:
676         vlc_mutex_unlock( &p_priv->var_lock );
677         return VLC_EGENERIC;
678     }
679
680     /*  Check boundaries */
681     CheckValue( p_var, &p_var->val );
682     *p_val = p_var->val;
683
684     /* Deal with callbacks.*/
685     i_ret = TriggerCallback( p_this, p_var, psz_name, oldval );
686
687     vlc_mutex_unlock( &p_priv->var_lock );
688
689     return i_ret;
690 }
691
692 #undef var_Type
693 /**
694  * Request a variable's type
695  *
696  * \return The variable type if it exists, or 0 if the
697  * variable could not be found.
698  * \see \ref var_type
699  */
700 int var_Type( vlc_object_t *p_this, const char *psz_name )
701 {
702     variable_t *p_var;
703     int i_type = 0;
704
705     assert( p_this );
706
707     vlc_object_internals_t *p_priv = vlc_internals( p_this );
708
709     vlc_mutex_lock( &p_priv->var_lock );
710
711     p_var = Lookup( p_this, psz_name );
712     if( p_var != NULL )
713         i_type = p_var->i_type;
714
715     vlc_mutex_unlock( &p_priv->var_lock );
716
717     return i_type;
718 }
719
720 #undef var_SetChecked
721 int var_SetChecked( vlc_object_t *p_this, const char *psz_name,
722                     int expected_type, vlc_value_t val )
723 {
724     int i_ret = VLC_SUCCESS;
725     variable_t *p_var;
726     vlc_value_t oldval;
727
728     assert( p_this );
729
730     vlc_object_internals_t *p_priv = vlc_internals( p_this );
731
732     vlc_mutex_lock( &p_priv->var_lock );
733
734     p_var = Lookup( p_this, psz_name );
735     if( p_var == NULL )
736     {
737         vlc_mutex_unlock( &p_priv->var_lock );
738         return VLC_ENOVAR;
739     }
740
741     assert( expected_type == 0 ||
742             (p_var->i_type & VLC_VAR_CLASS) == expected_type );
743 #ifndef NDEBUG
744         /* Alert if the type is VLC_VAR_VOID */
745         if( ( p_var->i_type & VLC_VAR_TYPE ) == VLC_VAR_VOID )
746             msg_Warn( p_this, "Calling var_Set on the void variable '%s' (0x%04x)", psz_name, p_var->i_type );
747 #endif
748
749
750     WaitUnused( p_this, p_var );
751
752     /* Duplicate data if needed */
753     p_var->ops->pf_dup( &val );
754
755     /* Backup needed stuff */
756     oldval = p_var->val;
757
758     /* Check boundaries and list */
759     CheckValue( p_var, &val );
760
761     /* Set the variable */
762     p_var->val = val;
763
764     /* Deal with callbacks */
765     i_ret = TriggerCallback( p_this, p_var, psz_name, oldval );
766
767     /* Free data if needed */
768     p_var->ops->pf_free( &oldval );
769
770     vlc_mutex_unlock( &p_priv->var_lock );
771
772     return i_ret;
773 }
774
775 #undef var_Set
776 /**
777  * Set a variable's value
778  *
779  * \param p_this The object that hold the variable
780  * \param psz_name The name of the variable
781  * \param val the value to set
782  */
783 int var_Set( vlc_object_t *p_this, const char *psz_name, vlc_value_t val )
784 {
785     return var_SetChecked( p_this, psz_name, 0, val );
786 }
787
788 #undef var_GetChecked
789 int var_GetChecked( vlc_object_t *p_this, const char *psz_name,
790                     int expected_type, vlc_value_t *p_val )
791 {
792     assert( p_this );
793
794     vlc_object_internals_t *p_priv = vlc_internals( p_this );
795     variable_t *p_var;
796     int err = VLC_SUCCESS;
797
798     vlc_mutex_lock( &p_priv->var_lock );
799
800     p_var = Lookup( p_this, psz_name );
801     if( p_var != NULL )
802     {
803         assert( expected_type == 0 ||
804                 (p_var->i_type & VLC_VAR_CLASS) == expected_type );
805
806         /* Really get the variable */
807         *p_val = p_var->val;
808
809 #ifndef NDEBUG
810         /* Alert if the type is VLC_VAR_VOID */
811         if( ( p_var->i_type & VLC_VAR_TYPE ) == VLC_VAR_VOID )
812             msg_Warn( p_this, "Calling var_Get on the void variable '%s' (0x%04x)", psz_name, p_var->i_type );
813 #endif
814
815         /* Duplicate value if needed */
816         p_var->ops->pf_dup( p_val );
817     }
818     else
819         err = VLC_ENOVAR;
820
821     vlc_mutex_unlock( &p_priv->var_lock );
822     return err;
823 }
824
825 #undef var_Get
826 /**
827  * Get a variable's value
828  *
829  * \param p_this The object that holds the variable
830  * \param psz_name The name of the variable
831  * \param p_val Pointer to a vlc_value_t that will hold the variable's value
832  *              after the function is finished
833  */
834 int var_Get( vlc_object_t *p_this, const char *psz_name, vlc_value_t *p_val )
835 {
836     return var_GetChecked( p_this, psz_name, 0, p_val );
837 }
838
839 #undef var_AddCallback
840 /**
841  * Register a callback in a variable
842  *
843  * We store a function pointer that will be called upon variable
844  * modification.
845  *
846  * \param p_this The object that holds the variable
847  * \param psz_name The name of the variable
848  * \param pf_callback The function pointer
849  * \param p_data A generic pointer that will be passed as the last
850  *               argument to the callback function.
851  *
852  * \warning The callback function is run in the thread that calls var_Set on
853  *          the variable. Use proper locking. This thread may not have much
854  *          time to spare, so keep callback functions short.
855  */
856 int var_AddCallback( vlc_object_t *p_this, const char *psz_name,
857                      vlc_callback_t pf_callback, void *p_data )
858 {
859     variable_t *p_var;
860     callback_entry_t entry;
861
862     assert( p_this );
863
864     vlc_object_internals_t *p_priv = vlc_internals( p_this );
865
866     entry.pf_callback = pf_callback;
867     entry.p_data = p_data;
868
869     vlc_mutex_lock( &p_priv->var_lock );
870
871     p_var = Lookup( p_this, psz_name );
872     if( p_var == NULL )
873     {
874 #ifndef NDEBUG
875         msg_Warn( p_this, "Failed to add a callback to the non-existing "
876                           "variable '%s'", psz_name );
877 #endif
878         vlc_mutex_unlock( &p_priv->var_lock );
879         return VLC_ENOVAR;
880     }
881
882     WaitUnused( p_this, p_var );
883     INSERT_ELEM( p_var->p_entries,
884                  p_var->i_entries,
885                  p_var->i_entries,
886                  entry );
887
888     vlc_mutex_unlock( &p_priv->var_lock );
889
890     return VLC_SUCCESS;
891 }
892
893 #undef var_DelCallback
894 /**
895  * Remove a callback from a variable
896  *
897  * pf_callback and p_data have to be given again, because different objects
898  * might have registered the same callback function.
899  */
900 int var_DelCallback( vlc_object_t *p_this, const char *psz_name,
901                      vlc_callback_t pf_callback, void *p_data )
902 {
903     int i_entry;
904     variable_t *p_var;
905 #ifndef NDEBUG
906     bool b_found_similar = false;
907 #endif
908
909     assert( p_this );
910
911     vlc_object_internals_t *p_priv = vlc_internals( p_this );
912
913     vlc_mutex_lock( &p_priv->var_lock );
914
915     p_var = Lookup( p_this, psz_name );
916     if( p_var == NULL )
917     {
918         vlc_mutex_unlock( &p_priv->var_lock );
919         return VLC_ENOVAR;
920     }
921
922     WaitUnused( p_this, p_var );
923
924     for( i_entry = p_var->i_entries ; i_entry-- ; )
925     {
926         if( p_var->p_entries[i_entry].pf_callback == pf_callback
927             && p_var->p_entries[i_entry].p_data == p_data )
928         {
929             break;
930         }
931 #ifndef NDEBUG
932         else if( p_var->p_entries[i_entry].pf_callback == pf_callback )
933             b_found_similar = true;
934 #endif
935     }
936
937     if( i_entry < 0 )
938     {
939 #ifndef NDEBUG
940         if( b_found_similar )
941             fprintf( stderr, "Calling var_DelCallback for '%s' with the same "
942                              "function but not the same data.", psz_name );
943         assert( 0 );
944 #endif
945         vlc_mutex_unlock( &p_priv->var_lock );
946         return VLC_EGENERIC;
947     }
948
949     REMOVE_ELEM( p_var->p_entries, p_var->i_entries, i_entry );
950
951     vlc_mutex_unlock( &p_priv->var_lock );
952
953     return VLC_SUCCESS;
954 }
955
956 #undef var_TriggerCallback
957 /**
958  * Trigger callback on a variable
959  *
960  * \param p_this The object that hold the variable
961  * \param psz_name The name of the variable
962  */
963 int var_TriggerCallback( vlc_object_t *p_this, const char *psz_name )
964 {
965     int i_ret;
966     variable_t *p_var;
967
968     assert( p_this );
969
970     vlc_object_internals_t *p_priv = vlc_internals( p_this );
971
972     vlc_mutex_lock( &p_priv->var_lock );
973
974     p_var = Lookup( p_this, psz_name );
975     if( p_var == NULL )
976     {
977         vlc_mutex_unlock( &p_priv->var_lock );
978         return VLC_ENOVAR;
979     }
980
981     WaitUnused( p_this, p_var );
982
983     /* Deal with callbacks. Tell we're in a callback, release the lock,
984      * call stored functions, retake the lock. */
985     i_ret = TriggerCallback( p_this, p_var, psz_name, p_var->val );
986
987     vlc_mutex_unlock( &p_priv->var_lock );
988     return i_ret;
989 }
990
991 /** Parse a stringified option
992  * This function parse a string option and create the associated object
993  * variable
994  * The option must be of the form "[no[-]]foo[=bar]" where foo is the
995  * option name and bar is the value of the option.
996  * \param p_obj the object in which the variable must be created
997  * \param psz_option the option to parse
998  * \param trusted whether the option is set by a trusted input or not
999  * \return nothing
1000  */
1001 void var_OptionParse( vlc_object_t *p_obj, const char *psz_option,
1002                       bool trusted )
1003 {
1004     char *psz_name, *psz_value;
1005     int  i_type;
1006     bool b_isno = false;
1007     vlc_value_t val;
1008
1009     val.psz_string = NULL;
1010
1011     /* It's too much of a hassle to remove the ':' when we parse
1012      * the cmd line :) */
1013     if( psz_option[0] == ':' )
1014         psz_option++;
1015
1016     if( !psz_option[0] )
1017         return;
1018
1019     psz_name = strdup( psz_option );
1020     if( psz_name == NULL )
1021         return;
1022
1023     psz_value = strchr( psz_name, '=' );
1024     if( psz_value != NULL )
1025         *psz_value++ = '\0';
1026
1027     i_type = config_GetType( p_obj, psz_name );
1028     if( !i_type && !psz_value )
1029     {
1030         /* check for "no-foo" or "nofoo" */
1031         if( !strncmp( psz_name, "no-", 3 ) )
1032         {
1033             memmove( psz_name, psz_name + 3, strlen(psz_name) + 1 - 3 );
1034         }
1035         else if( !strncmp( psz_name, "no", 2 ) )
1036         {
1037             memmove( psz_name, psz_name + 2, strlen(psz_name) + 1 - 2 );
1038         }
1039         else goto cleanup;           /* Option doesn't exist */
1040
1041         b_isno = true;
1042         i_type = config_GetType( p_obj, psz_name );
1043     }
1044     if( !i_type ) goto cleanup; /* Option doesn't exist */
1045
1046     if( ( i_type != VLC_VAR_BOOL ) &&
1047         ( !psz_value || !*psz_value ) ) goto cleanup; /* Invalid value */
1048
1049     /* check if option is unsafe */
1050     if( !trusted && !config_IsSafe( psz_name ) )
1051     {
1052         msg_Err( p_obj, "unsafe option \"%s\" has been ignored for "
1053                         "security reasons", psz_name );
1054         free( psz_name );
1055         return;
1056     }
1057
1058     /* Create the variable in the input object.
1059      * Children of the input object will be able to retreive this value
1060      * thanks to the inheritance property of the object variables. */
1061     var_Create( p_obj, psz_name, i_type );
1062
1063     switch( i_type )
1064     {
1065     case VLC_VAR_BOOL:
1066         val.b_bool = !b_isno;
1067         break;
1068
1069     case VLC_VAR_INTEGER:
1070         val.i_int = strtoll( psz_value, NULL, 0 );
1071         break;
1072
1073     case VLC_VAR_FLOAT:
1074         val.f_float = us_atof( psz_value );
1075         break;
1076
1077     case VLC_VAR_STRING:
1078         val.psz_string = psz_value;
1079         break;
1080
1081     default:
1082         goto cleanup;
1083     }
1084
1085     var_Set( p_obj, psz_name, val );
1086
1087 cleanup:
1088     free( psz_name );
1089 }
1090
1091 #undef var_LocationParse
1092 /**
1093  * Parses a set of colon-separated <variable name>=<value> pairs. Some access
1094  * (or access_demux) plugins uses this scheme in media resource location.
1095  * @note Only trusted/safe variables are allowed. This is intended.
1096  *
1097  * @warning Only use this for plugins implementing VLC-specific resource
1098  * location schemes. This would not make any sense for standardized ones.
1099  *
1100  * @param obj VLC object on which to set variables (and emit error messages)
1101  * @param mrl string to parse
1102  * @param pref prefix to prepend to option names in the string
1103  *
1104  * @return VLC_ENOMEM on error, VLC_SUCCESS on success.
1105  */
1106 int var_LocationParse (vlc_object_t *obj, const char *mrl, const char *pref)
1107 {
1108     int ret = VLC_SUCCESS;
1109     size_t preflen = strlen (pref) + 1;
1110
1111     assert(mrl != NULL);
1112     while (*mrl != '\0')
1113     {
1114         mrl += strspn (mrl, ":"); /* skip leading colon(s) */
1115
1116         size_t len = strcspn (mrl, ":");
1117         char *buf = malloc (preflen + len);
1118
1119         if (likely(buf != NULL))
1120         {
1121             /* NOTE: this does not support the "no-<varname>" bool syntax. */
1122             /* DO NOT use asprintf() here; it won't work! Think again. */
1123             snprintf (buf, preflen + len, "%s%s", pref, mrl);
1124             var_OptionParse (obj, buf, false);
1125             free (buf);
1126         }
1127         else
1128             ret = VLC_ENOMEM;
1129         mrl += len;
1130     }
1131
1132     return ret;
1133 }
1134
1135 /**
1136  * Waits until the variable is inactive (i.e. not executing a callback)
1137  */
1138 static void WaitUnused( vlc_object_t *p_this, variable_t *p_var )
1139 {
1140     vlc_object_internals_t *p_priv = vlc_internals( p_this );
1141
1142     mutex_cleanup_push( &p_priv->var_lock );
1143     while( p_var->b_incallback )
1144         vlc_cond_wait( &p_priv->var_wait, &p_priv->var_lock );
1145     vlc_cleanup_pop( );
1146 }
1147
1148 /*****************************************************************************
1149  * CheckValue: check that a value is valid wrt. a variable
1150  *****************************************************************************
1151  * This function checks p_val's value against p_var's limitations such as
1152  * minimal and maximal value, step, in-list position, and modifies p_val if
1153  * necessary.
1154  ****************************************************************************/
1155 static void CheckValue ( variable_t *p_var, vlc_value_t *p_val )
1156 {
1157     /* Check that our variable is in the list */
1158     if( p_var->i_type & VLC_VAR_HASCHOICE && p_var->choices.i_count )
1159     {
1160         int i;
1161
1162         /* This list is not sorted so go throug it (this is a small list) */
1163         for( i = p_var->choices.i_count ; i-- ; )
1164         {
1165             if( p_var->ops->pf_cmp( *p_val, p_var->choices.p_values[i] ) == 0 )
1166             {
1167                 break;
1168             }
1169         }
1170
1171         /* If not found, change it to anything vaguely valid */
1172         if( i < 0 )
1173         {
1174             /* Free the old variable, get the new one, dup it */
1175             p_var->ops->pf_free( p_val );
1176             *p_val = p_var->choices.p_values[p_var->i_default >= 0
1177                                           ? p_var->i_default : 0 ];
1178             p_var->ops->pf_dup( p_val );
1179         }
1180     }
1181
1182     /* Check that our variable is within the bounds */
1183     switch( p_var->i_type & VLC_VAR_TYPE )
1184     {
1185         case VLC_VAR_INTEGER:
1186             if( p_var->i_type & VLC_VAR_HASSTEP && p_var->step.i_int
1187                  && (p_val->i_int % p_var->step.i_int) )
1188             {
1189                 p_val->i_int = (p_val->i_int + (p_var->step.i_int / 2))
1190                                / p_var->step.i_int * p_var->step.i_int;
1191             }
1192             if( p_var->i_type & VLC_VAR_HASMIN
1193                  && p_val->i_int < p_var->min.i_int )
1194             {
1195                 p_val->i_int = p_var->min.i_int;
1196             }
1197             if( p_var->i_type & VLC_VAR_HASMAX
1198                  && p_val->i_int > p_var->max.i_int )
1199             {
1200                 p_val->i_int = p_var->max.i_int;
1201             }
1202             break;
1203         case VLC_VAR_FLOAT:
1204             if( p_var->i_type & VLC_VAR_HASSTEP && p_var->step.f_float )
1205             {
1206                 float f_round = p_var->step.f_float * (float)(int)( 0.5 +
1207                                         p_val->f_float / p_var->step.f_float );
1208                 if( p_val->f_float != f_round )
1209                 {
1210                     p_val->f_float = f_round;
1211                 }
1212             }
1213             if( p_var->i_type & VLC_VAR_HASMIN
1214                  && p_val->f_float < p_var->min.f_float )
1215             {
1216                 p_val->f_float = p_var->min.f_float;
1217             }
1218             if( p_var->i_type & VLC_VAR_HASMAX
1219                  && p_val->f_float > p_var->max.f_float )
1220             {
1221                 p_val->f_float = p_var->max.f_float;
1222             }
1223             break;
1224         case VLC_VAR_TIME:
1225             /* FIXME: TODO */
1226             break;
1227     }
1228 }
1229
1230 /**
1231  * Finds the value of a variable. If the specified object does not hold a
1232  * variable with the specified name, try the parent object, and iterate until
1233  * the top of the tree. If no match is found, the value is read from the
1234  * configuration.
1235  */
1236 int var_Inherit( vlc_object_t *p_this, const char *psz_name, int i_type,
1237                  vlc_value_t *p_val )
1238 {
1239 #ifndef NDEBUG
1240     if (p_this != VLC_OBJECT(p_this->p_libvlc)
1241      && unlikely(p_this->p_parent == NULL))
1242     {
1243         msg_Info (p_this, "%s(%s) on detached object", __func__, psz_name);
1244         //vlc_backtrace ();
1245     }
1246 #endif
1247     i_type &= VLC_VAR_CLASS;
1248     for( vlc_object_t *obj = p_this; obj != NULL; obj = obj->p_parent )
1249     {
1250         if( var_GetChecked( obj, psz_name, i_type, p_val ) == VLC_SUCCESS )
1251             return VLC_SUCCESS;
1252 #ifndef NDEBUG
1253         if (obj != p_this && obj != VLC_OBJECT(p_this->p_libvlc)
1254          && unlikely(obj->p_parent == NULL))
1255         {
1256             msg_Info (p_this, "%s(%s) on detached tree [%p] %s", __func__,
1257                       psz_name, obj, obj->psz_object_type);
1258             //vlc_backtrace ();
1259         }
1260 #endif
1261     }
1262
1263     /* else take value from config */
1264     switch( i_type & VLC_VAR_CLASS )
1265     {
1266         case VLC_VAR_STRING:
1267             p_val->psz_string = config_GetPsz( p_this, psz_name );
1268             if( !p_val->psz_string ) p_val->psz_string = strdup("");
1269             break;
1270         case VLC_VAR_FLOAT:
1271             p_val->f_float = config_GetFloat( p_this, psz_name );
1272             break;
1273         case VLC_VAR_INTEGER:
1274             p_val->i_int = config_GetInt( p_this, psz_name );
1275             break;
1276         case VLC_VAR_BOOL:
1277             p_val->b_bool = config_GetInt( p_this, psz_name );
1278             break;
1279         case VLC_VAR_ADDRESS:
1280             return VLC_ENOOBJ;
1281         default:
1282             msg_Warn( p_this, "Could not inherit value for var %s "
1283                               "from config. Invalid Type", psz_name );
1284             return VLC_ENOOBJ;
1285     }
1286     /*msg_Dbg( p_this, "Inherited value for var %s from config", psz_name );*/
1287     return VLC_SUCCESS;
1288 }
1289
1290
1291 /**
1292  * It inherits a string as an unsigned rational number (it also accepts basic
1293  * float number).
1294  *
1295  * It returns an error if the rational number cannot be parsed (0/0 is valid).
1296  * The rational is already reduced.
1297  */
1298 int (var_InheritURational)(vlc_object_t *object,
1299                            unsigned *num, unsigned *den,
1300                            const char *var)
1301 {
1302     /* */
1303     *num = 0;
1304     *den = 0;
1305
1306     /* */
1307     char *tmp = var_InheritString(object, var);
1308     if (!tmp)
1309         goto error;
1310
1311     char *next;
1312     unsigned n = strtol(tmp,  &next, 0);
1313     unsigned d = strtol(*next ? &next[1] : "0", NULL, 0);
1314
1315     if (*next == '.') {
1316         /* Interpret as a float number */
1317         double r = us_atof(tmp);
1318         double c = ceil(r);
1319         if (c >= UINT_MAX)
1320             goto error;
1321         unsigned m = c;
1322         if (m > 0) {
1323             d = UINT_MAX / m;
1324             n = r * d;
1325         } else {
1326             n = 0;
1327             d = 0;
1328         }
1329     }
1330
1331     if (n > 0 && d > 0)
1332         vlc_ureduce(num, den, n, d, 0);
1333
1334     free(tmp);
1335     return VLC_SUCCESS;
1336
1337 error:
1338     free(tmp);
1339     return VLC_EGENERIC;
1340 }
1341
1342 /**********************************************************************
1343  * Trigger the callbacks.
1344  * Tell we're in a callback, release the lock, call stored functions,
1345  * retake the lock.
1346  **********************************************************************/
1347 static int TriggerCallback( vlc_object_t *p_this, variable_t *p_var,
1348                             const char *psz_name, vlc_value_t oldval )
1349 {
1350     assert( p_this );
1351
1352     int i_entries = p_var->i_entries;
1353     if( i_entries == 0 )
1354         return VLC_SUCCESS;
1355
1356     callback_entry_t *p_entries = p_var->p_entries;
1357     vlc_object_internals_t *p_priv = vlc_internals( p_this );
1358
1359     assert( !p_var->b_incallback );
1360     p_var->b_incallback = true;
1361     vlc_mutex_unlock( &p_priv->var_lock );
1362
1363     /* The real calls */
1364     for( ; i_entries-- ; )
1365     {
1366         p_entries[i_entries].pf_callback( p_this, psz_name, oldval, p_var->val,
1367                                           p_entries[i_entries].p_data );
1368     }
1369
1370     vlc_mutex_lock( &p_priv->var_lock );
1371     p_var->b_incallback = false;
1372     vlc_cond_broadcast( &p_priv->var_wait );
1373
1374     return VLC_SUCCESS;
1375 }
1376
1377 #undef var_Command
1378 /**********************************************************************
1379  * Execute a var command on an object identified by its name
1380  **********************************************************************/
1381 int var_Command( vlc_object_t *p_this, const char *psz_name,
1382                  const char *psz_cmd, const char *psz_arg, char **psz_msg )
1383 {
1384     vlc_object_t *p_obj = vlc_object_find_name( p_this->p_libvlc,
1385                                                 psz_name );
1386     int i_type, i_ret;
1387
1388     if( !p_obj )
1389     {
1390         if( psz_msg )
1391             *psz_msg = strdup( "Unknown destination object." );
1392         return VLC_ENOOBJ;
1393     }
1394
1395     i_type = var_Type( p_obj, psz_cmd );
1396     if( !( i_type&VLC_VAR_ISCOMMAND ) )
1397     {
1398         vlc_object_release( p_obj );
1399         if( psz_msg )
1400             *psz_msg = strdup( "Variable doesn't exist or isn't a command." );
1401         return VLC_EGENERIC;
1402     }
1403
1404     i_type &= VLC_VAR_CLASS;
1405     switch( i_type )
1406     {
1407         case VLC_VAR_INTEGER:
1408             i_ret = var_SetInteger( p_obj, psz_cmd, atoi( psz_arg ) );
1409             break;
1410         case VLC_VAR_FLOAT:
1411             i_ret = var_SetFloat( p_obj, psz_cmd, us_atof( psz_arg ) );
1412             break;
1413         case VLC_VAR_STRING:
1414             i_ret = var_SetString( p_obj, psz_cmd, psz_arg );
1415             break;
1416         case VLC_VAR_BOOL:
1417             i_ret = var_SetBool( p_obj, psz_cmd, atoi( psz_arg ) );
1418             break;
1419         default:
1420             i_ret = VLC_EGENERIC;
1421             break;
1422     }
1423
1424     vlc_object_release( p_obj );
1425
1426     if( psz_msg )
1427     {
1428         if( asprintf( psz_msg, "%s on object %s returned %i (%s)",
1429                   psz_cmd, psz_name, i_ret, vlc_error( i_ret ) ) == -1)
1430             *psz_msg = NULL;
1431     }
1432
1433     return i_ret;
1434 }
1435
1436
1437 /**
1438  * Free a list and the associated strings
1439  * @param p_val: the list variable
1440  * @param p_val2: the variable associated or NULL
1441  */
1442 void var_FreeList( vlc_value_t *p_val, vlc_value_t *p_val2 )
1443 {
1444     FreeList( p_val );
1445     if( p_val2 && p_val2->p_list )
1446     {
1447         for( int i = 0; i < p_val2->p_list->i_count; i++ )
1448             free( p_val2->p_list->p_values[i].psz_string );
1449         if( p_val2->p_list->i_count )
1450         {
1451             free( p_val2->p_list->p_values );
1452             free( p_val2->p_list->pi_types );
1453         }
1454         free( p_val2->p_list );
1455     }
1456 }