]> git.sesse.net Git - vlc/blob - src/misc/variables.c
Variable inheritance hints
[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         vlc_cond_broadcast( &p_priv->var_wait );
766     }
767
768     /* Free data if needed */
769     p_var->ops->pf_free( &oldval );
770
771     vlc_mutex_unlock( &p_priv->var_lock );
772
773     return VLC_SUCCESS;
774 }
775
776
777 /**
778  * Set a variable's value
779  *
780  * \param p_this The object that hold the variable
781  * \param psz_name The name of the variable
782  * \param val the value to set
783  */
784 int __var_Set( vlc_object_t *p_this, const char *psz_name, vlc_value_t val )
785 {
786     return var_SetChecked( p_this, psz_name, 0, val );
787 }
788
789 int var_GetChecked( vlc_object_t *p_this, const char *psz_name,
790                     int expected_type, vlc_value_t *p_val )
791 {
792     vlc_object_internals_t *p_priv = vlc_internals( p_this );
793     int i_var, err = VLC_SUCCESS;
794
795     vlc_mutex_lock( &p_priv->var_lock );
796
797     i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
798     if( i_var >= 0 )
799     {
800         variable_t *p_var = &p_priv->p_vars[i_var];
801
802         assert( (p_var->i_type & VLC_VAR_CLASS) == 0 || expected_type == 0 ||
803                 (p_var->i_type & VLC_VAR_CLASS) == expected_type );
804
805         /* Really get the variable */
806         *p_val = p_var->val;
807
808         /* Duplicate value if needed */
809         p_var->ops->pf_dup( p_val );
810     }
811     else
812         err = VLC_ENOVAR;
813
814     vlc_mutex_unlock( &p_priv->var_lock );
815     return err;
816 }
817
818 /**
819  * Get a variable's value
820  *
821  * \param p_this The object that holds the variable
822  * \param psz_name The name of the variable
823  * \param p_val Pointer to a vlc_value_t that will hold the variable's value
824  *              after the function is finished
825  */
826 int __var_Get( vlc_object_t *p_this, const char *psz_name, vlc_value_t *p_val )
827 {
828     return var_GetChecked( p_this, psz_name, 0, p_val );
829 }
830
831 /**
832  * Register a callback in a variable
833  *
834  * We store a function pointer that will be called upon variable
835  * modification.
836  *
837  * \param p_this The object that holds the variable
838  * \param psz_name The name of the variable
839  * \param pf_callback The function pointer
840  * \param p_data A generic pointer that will be passed as the last
841  *               argument to the callback function.
842  *
843  * \warning The callback function is run in the thread that calls var_Set on
844  *          the variable. Use proper locking. This thread may not have much
845  *          time to spare, so keep callback functions short.
846  */
847 int __var_AddCallback( vlc_object_t *p_this, const char *psz_name,
848                        vlc_callback_t pf_callback, void *p_data )
849 {
850     int i_var;
851     variable_t *p_var;
852     callback_entry_t entry;
853     vlc_object_internals_t *p_priv = vlc_internals( p_this );
854
855     entry.pf_callback = pf_callback;
856     entry.p_data = p_data;
857
858     vlc_mutex_lock( &p_priv->var_lock );
859
860     i_var = GetUnused( p_this, psz_name );
861     if( i_var < 0 )
862     {
863         vlc_mutex_unlock( &p_priv->var_lock );
864         return i_var;
865     }
866
867     p_var = &p_priv->p_vars[i_var];
868
869     INSERT_ELEM( p_var->p_entries,
870                  p_var->i_entries,
871                  p_var->i_entries,
872                  entry );
873
874     vlc_mutex_unlock( &p_priv->var_lock );
875
876     return VLC_SUCCESS;
877 }
878
879 /**
880  * Remove a callback from a variable
881  *
882  * pf_callback and p_data have to be given again, because different objects
883  * might have registered the same callback function.
884  */
885 int __var_DelCallback( vlc_object_t *p_this, const char *psz_name,
886                        vlc_callback_t pf_callback, void *p_data )
887 {
888     int i_entry, i_var;
889     variable_t *p_var;
890     vlc_object_internals_t *p_priv = vlc_internals( p_this );
891
892     vlc_mutex_lock( &p_priv->var_lock );
893
894     i_var = GetUnused( p_this, psz_name );
895     if( i_var < 0 )
896     {
897         vlc_mutex_unlock( &p_priv->var_lock );
898         return i_var;
899     }
900
901     p_var = &p_priv->p_vars[i_var];
902
903     for( i_entry = p_var->i_entries ; i_entry-- ; )
904     {
905         if( p_var->p_entries[i_entry].pf_callback == pf_callback
906             && p_var->p_entries[i_entry].p_data == p_data )
907         {
908             break;
909         }
910     }
911
912     if( i_entry < 0 )
913     {
914         vlc_mutex_unlock( &p_priv->var_lock );
915         return VLC_EGENERIC;
916     }
917
918     REMOVE_ELEM( p_var->p_entries, p_var->i_entries, i_entry );
919
920     vlc_mutex_unlock( &p_priv->var_lock );
921
922     return VLC_SUCCESS;
923 }
924
925 /**
926  * Trigger callback on a variable
927  *
928  * \param p_this The object that hold the variable
929  * \param psz_name The name of the variable
930  */
931 int __var_TriggerCallback( vlc_object_t *p_this, const char *psz_name )
932 {
933     int i_var;
934     variable_t *p_var;
935     vlc_value_t oldval;
936     vlc_object_internals_t *p_priv = vlc_internals( p_this );
937
938     vlc_mutex_lock( &p_priv->var_lock );
939
940     i_var = GetUnused( p_this, psz_name );
941     if( i_var < 0 )
942     {
943         vlc_mutex_unlock( &p_priv->var_lock );
944         return i_var;
945     }
946
947     p_var = &p_priv->p_vars[i_var];
948
949     /* Backup needed stuff */
950     oldval = p_var->val;
951
952     /* Deal with callbacks. Tell we're in a callback, release the lock,
953      * call stored functions, retake the lock. */
954     if( p_var->i_entries )
955     {
956         int i_var;
957         int i_entries = p_var->i_entries;
958         callback_entry_t *p_entries = p_var->p_entries;
959
960         p_var->b_incallback = true;
961         vlc_mutex_unlock( &p_priv->var_lock );
962
963         /* The real calls */
964         for( ; i_entries-- ; )
965         {
966             p_entries[i_entries].pf_callback( p_this, psz_name, oldval, oldval,
967                                               p_entries[i_entries].p_data );
968         }
969
970         vlc_mutex_lock( &p_priv->var_lock );
971
972         i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
973         if( i_var < 0 )
974         {
975             msg_Err( p_this, "variable %s has disappeared", psz_name );
976             vlc_mutex_unlock( &p_priv->var_lock );
977             return VLC_ENOVAR;
978         }
979
980         p_var = &p_priv->p_vars[i_var];
981         p_var->b_incallback = false;
982         vlc_cond_broadcast( &p_priv->var_wait );
983     }
984
985     vlc_mutex_unlock( &p_priv->var_lock );
986     return VLC_SUCCESS;
987 }
988
989 /** Parse a stringified option
990  * This function parse a string option and create the associated object
991  * variable
992  * The option must be of the form "[no[-]]foo[=bar]" where foo is the
993  * option name and bar is the value of the option.
994  * \param p_obj the object in which the variable must be created
995  * \param psz_option the option to parse
996  * \param trusted whether the option is set by a trusted input or not
997  * \return nothing
998  */
999 void var_OptionParse( vlc_object_t *p_obj, const char *psz_option,
1000                       bool trusted )
1001 {
1002     char *psz_name, *psz_value;
1003     int  i_type;
1004     bool b_isno = false;
1005     vlc_value_t val;
1006
1007     val.psz_string = NULL;
1008
1009     /* It's too much of a hassle to remove the ':' when we parse
1010      * the cmd line :) */
1011     if( psz_option[0] == ':' )
1012         psz_option++;
1013
1014     if( !psz_option[0] )
1015         return;
1016
1017     psz_name = strdup( psz_option );
1018     if( psz_name == NULL )
1019         return;
1020
1021     psz_value = strchr( psz_name, '=' );
1022     if( psz_value != NULL )
1023         *psz_value++ = '\0';
1024
1025     /* FIXME: :programs should be handled generically */
1026     if( !strcmp( psz_name, "programs" ) )
1027         i_type = VLC_VAR_LIST;
1028     else
1029         i_type = config_GetType( p_obj, psz_name );
1030
1031     if( !i_type && !psz_value )
1032     {
1033         /* check for "no-foo" or "nofoo" */
1034         if( !strncmp( psz_name, "no-", 3 ) )
1035         {
1036             memmove( psz_name, psz_name + 3, strlen(psz_name) + 1 - 3 );
1037         }
1038         else if( !strncmp( psz_name, "no", 2 ) )
1039         {
1040             memmove( psz_name, psz_name + 2, strlen(psz_name) + 1 - 2 );
1041         }
1042         else goto cleanup;           /* Option doesn't exist */
1043
1044         b_isno = true;
1045         i_type = config_GetType( p_obj, psz_name );
1046     }
1047     if( !i_type ) goto cleanup; /* Option doesn't exist */
1048
1049     if( ( i_type != VLC_VAR_BOOL ) &&
1050         ( !psz_value || !*psz_value ) ) goto cleanup; /* Invalid value */
1051
1052     /* check if option is unsafe */
1053     if( !trusted )
1054     {
1055         module_config_t *p_config = config_FindConfig( p_obj, psz_name );
1056         if( !p_config || !p_config->b_safe )
1057         {
1058             msg_Err( p_obj, "unsafe option \"%s\" has been ignored for "
1059                             "security reasons", psz_name );
1060             free( psz_name );
1061             return;
1062         }
1063     }
1064
1065     /* Create the variable in the input object.
1066      * Children of the input object will be able to retreive this value
1067      * thanks to the inheritance property of the object variables. */
1068     var_Create( p_obj, psz_name, i_type );
1069
1070     switch( i_type )
1071     {
1072     case VLC_VAR_BOOL:
1073         val.b_bool = !b_isno;
1074         break;
1075
1076     case VLC_VAR_INTEGER:
1077         val.i_int = strtol( psz_value, NULL, 0 );
1078         break;
1079
1080     case VLC_VAR_FLOAT:
1081         val.f_float = atof( psz_value );
1082         break;
1083
1084     case VLC_VAR_STRING:
1085     case VLC_VAR_MODULE:
1086     case VLC_VAR_FILE:
1087     case VLC_VAR_DIRECTORY:
1088         val.psz_string = psz_value;
1089         break;
1090
1091     case VLC_VAR_LIST:
1092     {
1093         char *psz_orig, *psz_var;
1094         vlc_list_t *p_list = malloc(sizeof(vlc_list_t));
1095         val.p_list = p_list;
1096         p_list->i_count = 0;
1097
1098         psz_var = psz_orig = strdup(psz_value);
1099         while( psz_var && *psz_var )
1100         {
1101             char *psz_item = psz_var;
1102             vlc_value_t val2;
1103             while( *psz_var && *psz_var != ',' ) psz_var++;
1104             if( *psz_var == ',' )
1105             {
1106                 *psz_var = '\0';
1107                 psz_var++;
1108             }
1109             val2.i_int = strtol( psz_item, NULL, 0 );
1110             INSERT_ELEM( p_list->p_values, p_list->i_count,
1111                          p_list->i_count, val2 );
1112             /* p_list->i_count is incremented twice by INSERT_ELEM */
1113             p_list->i_count--;
1114             INSERT_ELEM( p_list->pi_types, p_list->i_count,
1115                          p_list->i_count, VLC_VAR_INTEGER );
1116         }
1117         free( psz_orig );
1118         break;
1119     }
1120
1121     default:
1122         goto cleanup;
1123     }
1124
1125     var_Set( p_obj, psz_name, val );
1126
1127     // If that's a list, remove all elements allocated
1128     if( i_type == VLC_VAR_LIST )
1129         FreeList( &val );
1130
1131 cleanup:
1132     free( psz_name );
1133 }
1134
1135
1136 /* Following functions are local */
1137
1138 /*****************************************************************************
1139  * GetUnused: find an unused (not in callback) variable from its name
1140  *****************************************************************************
1141  * We do i_tries tries before giving up, just in case the variable is being
1142  * modified and called from a callback.
1143  *****************************************************************************/
1144 static int GetUnused( vlc_object_t *p_this, const char *psz_name )
1145 {
1146     vlc_object_internals_t *p_priv = vlc_internals( p_this );
1147
1148     while( true )
1149     {
1150         int i_var;
1151
1152         i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
1153         if( i_var < 0 )
1154         {
1155             return VLC_ENOVAR;
1156         }
1157
1158         if( ! p_priv->p_vars[i_var].b_incallback )
1159         {
1160             return i_var;
1161         }
1162
1163         mutex_cleanup_push( &p_priv->var_lock );
1164         vlc_cond_wait( &p_priv->var_wait, &p_priv->var_lock );
1165         vlc_cleanup_pop( );
1166     }
1167 }
1168
1169 /*****************************************************************************
1170  * HashString: our cool hash function
1171  *****************************************************************************
1172  * This function is not intended to be crypto-secure, we only want it to be
1173  * fast and not suck too much. This one is pretty fast and did 0 collisions
1174  * in wenglish's dictionary.
1175  *****************************************************************************/
1176 static uint32_t HashString( const char *psz_string )
1177 {
1178     uint32_t i_hash = 0;
1179
1180     while( *psz_string )
1181     {
1182         i_hash += *psz_string++;
1183         i_hash += i_hash << 10;
1184         i_hash ^= i_hash >> 8;
1185     }
1186
1187     return i_hash;
1188 }
1189
1190 /*****************************************************************************
1191  * Insert: find an empty slot to insert a new variable
1192  *****************************************************************************
1193  * We use a recursive inner function indexed on the hash. This function does
1194  * nothing in the rare cases where a collision may occur, see Lookup()
1195  * to see how we handle them.
1196  * XXX: does this really need to be written recursively?
1197  *****************************************************************************/
1198 static int Insert( variable_t *p_vars, int i_count, const char *psz_name )
1199 {
1200     if( i_count == 0 )
1201     {
1202         return 0;
1203     }
1204
1205     return InsertInner( p_vars, i_count, HashString( psz_name ) );
1206 }
1207
1208 static int InsertInner( variable_t *p_vars, int i_count, uint32_t i_hash )
1209 {
1210     int i_middle;
1211
1212     if( i_hash <= p_vars[0].i_hash )
1213     {
1214         return 0;
1215     }
1216
1217     if( i_hash >= p_vars[i_count - 1].i_hash )
1218     {
1219         return i_count;
1220     }
1221
1222     i_middle = i_count / 2;
1223
1224     /* We know that 0 < i_middle */
1225     if( i_hash < p_vars[i_middle].i_hash )
1226     {
1227         return InsertInner( p_vars, i_middle, i_hash );
1228     }
1229
1230     /* We know that i_middle + 1 < i_count */
1231     if( i_hash > p_vars[i_middle + 1].i_hash )
1232     {
1233         return i_middle + 1 + InsertInner( p_vars + i_middle + 1,
1234                                            i_count - i_middle - 1,
1235                                            i_hash );
1236     }
1237
1238     return i_middle + 1;
1239 }
1240
1241 static int u32cmp( const void *key, const void *data )
1242 {
1243     const variable_t *p_var = data;
1244     uint32_t hash = *(const uint32_t *)key ;
1245
1246     if( hash > p_var->i_hash )
1247         return 1;
1248     if( hash < p_var->i_hash )
1249         return -1;
1250     return 0;
1251 }
1252
1253 /*****************************************************************************
1254  * Lookup: find an existing variable given its name
1255  *****************************************************************************
1256  * We use a recursive inner function indexed on the hash. Care is taken of
1257  * possible hash collisions.
1258  *****************************************************************************/
1259 static int Lookup( variable_t *p_vars, size_t i_count, const char *psz_name )
1260 {
1261     variable_t *p_var;
1262     uint32_t i_hash;
1263
1264     i_hash = HashString( psz_name );
1265     p_var = bsearch( &i_hash, p_vars, i_count, sizeof( *p_var ), u32cmp );
1266
1267     /* Hash not found */
1268     if( p_var == NULL )
1269         return -1;
1270
1271     assert( i_count > 0 );
1272
1273     /* Find the first entry with the right hash */
1274     while( (p_var > p_vars) && (i_hash == p_var[-1].i_hash) )
1275         p_var--;
1276
1277     assert( p_var->i_hash == i_hash );
1278
1279     /* Hash collision should be very unlikely, but we cannot guarantee
1280      * it will never happen. So we do an exhaustive search amongst all
1281      * entries with the same hash. Typically, there is only one anyway. */
1282     for( variable_t *p_end = p_vars + i_count;
1283          (p_var < p_end) && (i_hash == p_var->i_hash);
1284          p_var++ )
1285     {
1286         if( !strcmp( psz_name, p_var->psz_name ) )
1287             return p_var - p_vars;
1288     }
1289
1290     /* Hash found, but entry not found */
1291     return -1;
1292 }
1293
1294 /*****************************************************************************
1295  * CheckValue: check that a value is valid wrt. a variable
1296  *****************************************************************************
1297  * This function checks p_val's value against p_var's limitations such as
1298  * minimal and maximal value, step, in-list position, and modifies p_val if
1299  * necessary.
1300  ****************************************************************************/
1301 static void CheckValue ( variable_t *p_var, vlc_value_t *p_val )
1302 {
1303     /* Check that our variable is in the list */
1304     if( p_var->i_type & VLC_VAR_HASCHOICE && p_var->choices.i_count )
1305     {
1306         int i;
1307
1308         /* FIXME: the list is sorted, dude. Use something cleverer. */
1309         for( i = p_var->choices.i_count ; i-- ; )
1310         {
1311             if( p_var->ops->pf_cmp( *p_val, p_var->choices.p_values[i] ) == 0 )
1312             {
1313                 break;
1314             }
1315         }
1316
1317         /* If not found, change it to anything vaguely valid */
1318         if( i < 0 )
1319         {
1320             /* Free the old variable, get the new one, dup it */
1321             p_var->ops->pf_free( p_val );
1322             *p_val = p_var->choices.p_values[p_var->i_default >= 0
1323                                           ? p_var->i_default : 0 ];
1324             p_var->ops->pf_dup( p_val );
1325         }
1326     }
1327
1328     /* Check that our variable is within the bounds */
1329     switch( p_var->i_type & VLC_VAR_TYPE )
1330     {
1331         case VLC_VAR_INTEGER:
1332             if( p_var->i_type & VLC_VAR_HASSTEP && p_var->step.i_int
1333                  && (p_val->i_int % p_var->step.i_int) )
1334             {
1335                 p_val->i_int = (p_val->i_int + (p_var->step.i_int / 2))
1336                                / p_var->step.i_int * p_var->step.i_int;
1337             }
1338             if( p_var->i_type & VLC_VAR_HASMIN
1339                  && p_val->i_int < p_var->min.i_int )
1340             {
1341                 p_val->i_int = p_var->min.i_int;
1342             }
1343             if( p_var->i_type & VLC_VAR_HASMAX
1344                  && p_val->i_int > p_var->max.i_int )
1345             {
1346                 p_val->i_int = p_var->max.i_int;
1347             }
1348             break;
1349         case VLC_VAR_FLOAT:
1350             if( p_var->i_type & VLC_VAR_HASSTEP && p_var->step.f_float )
1351             {
1352                 float f_round = p_var->step.f_float * (float)(int)( 0.5 +
1353                                         p_val->f_float / p_var->step.f_float );
1354                 if( p_val->f_float != f_round )
1355                 {
1356                     p_val->f_float = f_round;
1357                 }
1358             }
1359             if( p_var->i_type & VLC_VAR_HASMIN
1360                  && p_val->f_float < p_var->min.f_float )
1361             {
1362                 p_val->f_float = p_var->min.f_float;
1363             }
1364             if( p_var->i_type & VLC_VAR_HASMAX
1365                  && p_val->f_float > p_var->max.f_float )
1366             {
1367                 p_val->f_float = p_var->max.f_float;
1368             }
1369             break;
1370         case VLC_VAR_TIME:
1371             /* FIXME: TODO */
1372             break;
1373     }
1374 }
1375
1376 /*****************************************************************************
1377  * InheritValue: try to inherit the value of this variable from the same one
1378  * in our closest parent, libvlc or ultimately from the configuration.
1379  * The function should always be entered with the object var_lock locked.
1380  *****************************************************************************/
1381 static int InheritValue( vlc_object_t *p_this, const char *psz_name,
1382                          vlc_value_t *p_val, int i_type )
1383 {
1384     int i_var;
1385     variable_t *p_var;
1386
1387     if( p_this->p_parent || ( p_this->p_libvlc && p_this != (vlc_object_t*) p_this->p_libvlc ) )
1388     {
1389         vlc_object_internals_t *p_priv;
1390
1391         if( p_this->p_parent )
1392             p_priv = vlc_internals( p_this->p_parent );
1393         else
1394             p_priv = vlc_internals( p_this->p_libvlc );
1395
1396         i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
1397
1398         if( i_var >= 0 )
1399         {
1400             /* We found it! */
1401             p_var = &p_priv->p_vars[i_var];
1402
1403             /* Really get the variable */
1404             *p_val = p_var->val;
1405
1406             /* Duplicate value if needed */
1407             p_var->ops->pf_dup( p_val );
1408
1409             msg_Dbg( p_this, "Inherited value for var %s from object %s", psz_name ? : "(null)",
1410                 p_this->psz_object_name ? : "(Unknown)" ) ;
1411             return VLC_SUCCESS;
1412         }
1413         else if ( p_this->p_parent ) /* We are still not there */
1414             return InheritValue( p_this->p_parent, psz_name, p_val, i_type );
1415
1416         /* else take value from config */
1417     }
1418
1419
1420     switch( i_type & VLC_VAR_CLASS )
1421     {
1422         case VLC_VAR_STRING:
1423             p_val->psz_string = config_GetPsz( p_this, psz_name );
1424             if( !p_val->psz_string ) p_val->psz_string = strdup("");
1425             break;
1426         case VLC_VAR_FLOAT:
1427             p_val->f_float = config_GetFloat( p_this, psz_name );
1428             break;
1429         case VLC_VAR_INTEGER:
1430             p_val->i_int = config_GetInt( p_this, psz_name );
1431             break;
1432         case VLC_VAR_BOOL:
1433             p_val->b_bool = config_GetInt( p_this, psz_name );
1434             break;
1435         case VLC_VAR_LIST:
1436         {
1437             char *psz_orig, *psz_var;
1438             vlc_list_t *p_list = malloc(sizeof(vlc_list_t));
1439             p_val->p_list = p_list;
1440             p_list->i_count = 0;
1441
1442             psz_var = psz_orig = config_GetPsz( p_this, psz_name );
1443             while( psz_var && *psz_var )
1444             {
1445                 char *psz_item = psz_var;
1446                 vlc_value_t val;
1447                 while( *psz_var && *psz_var != ',' ) psz_var++;
1448                 if( *psz_var == ',' )
1449                 {
1450                     *psz_var = '\0';
1451                     psz_var++;
1452                 }
1453                 val.i_int = strtol( psz_item, NULL, 0 );
1454                 INSERT_ELEM( p_list->p_values, p_list->i_count,
1455                              p_list->i_count, val );
1456                 /* p_list->i_count is incremented twice by INSERT_ELEM */
1457                 p_list->i_count--;
1458                 INSERT_ELEM( p_list->pi_types, p_list->i_count,
1459                              p_list->i_count, VLC_VAR_INTEGER );
1460             }
1461             free( psz_orig );
1462             break;
1463         }
1464         default:
1465             msg_Warn( p_this, "Could not inherit value for var %s from config. Invalid Type", psz_name ) ;
1466             return VLC_ENOOBJ;
1467             break;
1468     }
1469     msg_Dbg( p_this, "Inherited value for var %s from config", psz_name ) ;
1470     return VLC_SUCCESS;
1471 }
1472
1473 /**********************************************************************
1474  * Execute a var command on an object identified by its name
1475  **********************************************************************/
1476 int __var_Command( vlc_object_t *p_this, const char *psz_name,
1477                    const char *psz_cmd, const char *psz_arg, char **psz_msg )
1478 {
1479     vlc_object_t *p_obj = vlc_object_find_name( p_this->p_libvlc,
1480                                                 psz_name, FIND_CHILD );
1481     int i_type, i_ret;
1482
1483     if( !p_obj )
1484     {
1485         if( psz_msg )
1486             *psz_msg = strdup( "Unknown destination object." );
1487         return VLC_ENOOBJ;
1488     }
1489
1490     i_type = var_Type( p_obj, psz_cmd );
1491     if( !( i_type&VLC_VAR_ISCOMMAND ) )
1492     {
1493         vlc_object_release( p_obj );
1494         if( psz_msg )
1495             *psz_msg = strdup( "Variable doesn't exist or isn't a command." );
1496         return VLC_EGENERIC;
1497     }
1498
1499     i_type &= VLC_VAR_CLASS;
1500     switch( i_type )
1501     {
1502         case VLC_VAR_INTEGER:
1503             i_ret = var_SetInteger( p_obj, psz_cmd, atoi( psz_arg ) );
1504             break;
1505         case VLC_VAR_FLOAT:
1506             i_ret = var_SetFloat( p_obj, psz_cmd, atof( psz_arg ) );
1507             break;
1508         case VLC_VAR_STRING:
1509             i_ret = var_SetString( p_obj, psz_cmd, psz_arg );
1510             break;
1511         case VLC_VAR_BOOL:
1512             i_ret = var_SetBool( p_obj, psz_cmd, atoi( psz_arg ) );
1513             break;
1514         default:
1515             i_ret = VLC_EGENERIC;
1516             break;
1517     }
1518
1519     vlc_object_release( p_obj );
1520
1521     if( psz_msg )
1522     {
1523         if( asprintf( psz_msg, "%s on object %s returned %i (%s)",
1524                   psz_cmd, psz_name, i_ret, vlc_error( i_ret ) ) == -1)
1525             *psz_msg = NULL;
1526     }
1527
1528     return i_ret;
1529 }