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