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