]> git.sesse.net Git - vlc/blob - src/misc/variables.c
Use asprintf and check for the return value. (better than malloc+sprintf)
[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
38 /*****************************************************************************
39  * Private types
40  *****************************************************************************/
41 struct callback_entry_t
42 {
43     vlc_callback_t pf_callback;
44     void *         p_data;
45 };
46
47 /*****************************************************************************
48  * Local comparison functions, returns 0 if v == w, < 0 if v < w, > 0 if v > w
49  *****************************************************************************/
50 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; }
51 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; }
52 static int CmpTime( vlc_value_t v, vlc_value_t w )
53 {
54     return v.i_time == w.i_time ? 0 : v.i_time > w.i_time ? 1 : -1;
55 }
56 static int CmpString( vlc_value_t v, vlc_value_t w )
57 {
58     if( !v.psz_string )
59         return !w.psz_string ? 0 : -1;
60     else
61         return !w.psz_string ? 1 : strcmp( v.psz_string, w.psz_string );
62 }
63 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; }
64 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; }
65
66 /*****************************************************************************
67  * Local duplication functions, and local deallocation functions
68  *****************************************************************************/
69 static void DupDummy( vlc_value_t *p_val ) { (void)p_val; /* unused */ }
70 static void DupString( vlc_value_t *p_val ) { if( p_val->psz_string ) p_val->psz_string = strdup( p_val->psz_string ); }
71
72 static void DupList( vlc_value_t *p_val )
73 {
74     int i;
75     vlc_list_t *p_list = malloc( sizeof(vlc_list_t) );
76
77     p_list->i_count = p_val->p_list->i_count;
78     if( p_val->p_list->i_count )
79     {
80         p_list->p_values = malloc( p_list->i_count * sizeof(vlc_value_t) );
81         p_list->pi_types = malloc( p_list->i_count * sizeof(int) );
82     }
83     else
84     {
85         p_list->p_values = NULL;
86         p_list->pi_types = NULL;
87     }
88
89     for( i = 0; i < p_list->i_count; i++ )
90     {
91         p_list->p_values[i] = p_val->p_list->p_values[i];
92         p_list->pi_types[i] = p_val->p_list->pi_types[i];
93         switch( p_val->p_list->pi_types[i] & VLC_VAR_CLASS )
94         {
95         case VLC_VAR_STRING:
96
97             DupString( &p_list->p_values[i] );
98             break;
99         default:
100             break;
101         }
102     }
103
104     p_val->p_list = p_list;
105 }
106
107 static void FreeDummy( vlc_value_t *p_val ) { (void)p_val; /* unused */ }
108 static void FreeString( vlc_value_t *p_val ) { free( p_val->psz_string ); }
109 static void FreeMutex( vlc_value_t *p_val ) { vlc_mutex_destroy( (vlc_mutex_t*)p_val->p_address ); free( p_val->p_address ); }
110
111 static void FreeList( vlc_value_t *p_val )
112 {
113     int i;
114     for( i = 0; i < p_val->p_list->i_count; i++ )
115     {
116         switch( p_val->p_list->pi_types[i] & VLC_VAR_CLASS )
117         {
118         case VLC_VAR_STRING:
119             FreeString( &p_val->p_list->p_values[i] );
120             break;
121         case VLC_VAR_MUTEX:
122             FreeMutex( &p_val->p_list->p_values[i] );
123             break;
124         default:
125             break;
126         }
127     }
128
129     if( p_val->p_list->i_count )
130     {
131         free( p_val->p_list->p_values );
132         free( p_val->p_list->pi_types );
133     }
134     free( p_val->p_list );
135 }
136
137 static const struct variable_ops_t
138 void_ops   = { NULL,       DupDummy,  FreeDummy,  },
139 addr_ops   = { CmpAddress, DupDummy,  FreeDummy,  },
140 bool_ops   = { CmpBool,    DupDummy,  FreeDummy,  },
141 float_ops  = { CmpFloat,   DupDummy,  FreeDummy,  },
142 int_ops    = { CmpInt,     DupDummy,  FreeDummy,  },
143 list_ops   = { CmpAddress, DupList,   FreeList,   },
144 mutex_ops  = { CmpAddress, DupDummy,  FreeMutex,  },
145 string_ops = { CmpString,  DupString, FreeString, },
146 time_ops   = { CmpTime,    DupDummy,  FreeDummy,  };
147
148 /*****************************************************************************
149  * Local prototypes
150  *****************************************************************************/
151 static int      GetUnused   ( vlc_object_t *, const char * );
152 static uint32_t HashString  ( const char * );
153 static int      Insert      ( variable_t *, int, const char * );
154 static int      InsertInner ( variable_t *, int, uint32_t );
155 static int      Lookup      ( variable_t *, int, const char * );
156 static int      LookupInner ( variable_t *, int, uint32_t );
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         case VLC_VAR_TRIGGER_CALLBACKS:
660             {
661                 /* Deal with callbacks. Tell we're in a callback, release the lock,
662                  * call stored functions, retake the lock. */
663                 if( p_var->i_entries )
664                 {
665                     int i_var;
666                     int i_entries = p_var->i_entries;
667                     callback_entry_t *p_entries = p_var->p_entries;
668
669                     p_var->b_incallback = true;
670                     vlc_mutex_unlock( &p_priv->var_lock );
671
672                     /* The real calls */
673                     for( ; i_entries-- ; )
674                     {
675                         p_entries[i_entries].pf_callback( p_this, psz_name, p_var->val, p_var->val,
676                                                           p_entries[i_entries].p_data );
677                     }
678
679                     vlc_mutex_lock( &p_priv->var_lock );
680
681                     i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
682                     if( i_var < 0 )
683                     {
684                         msg_Err( p_this, "variable %s has disappeared", psz_name );
685                         vlc_mutex_unlock( &p_priv->var_lock );
686                         return VLC_ENOVAR;
687                     }
688
689                     p_var = &p_priv->p_vars[i_var];
690                     p_var->b_incallback = false;
691                 }
692             }
693             break;
694
695         case VLC_VAR_SETISCOMMAND:
696             p_var->i_type |= VLC_VAR_ISCOMMAND;
697             break;
698
699         default:
700             break;
701     }
702
703     vlc_mutex_unlock( &p_priv->var_lock );
704
705     return VLC_SUCCESS;
706 }
707
708 /**
709  * Request a variable's type
710  *
711  * \return The variable type if it exists, or 0 if the
712  * variable could not be found.
713  * \see \ref var_type
714  */
715 int __var_Type( vlc_object_t *p_this, const char *psz_name )
716 {
717     int i_var, i_type;
718     vlc_object_internals_t *p_priv = vlc_internals( p_this );
719
720     vlc_mutex_lock( &p_priv->var_lock );
721
722     i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
723
724     if( i_var < 0 )
725     {
726         vlc_mutex_unlock( &p_priv->var_lock );
727         return 0;
728     }
729
730     i_type = p_priv->p_vars[i_var].i_type;
731
732     vlc_mutex_unlock( &p_priv->var_lock );
733
734     return i_type;
735 }
736
737 /**
738  * Set a variable's value
739  *
740  * \param p_this The object that hold the variable
741  * \param psz_name The name of the variable
742  * \param val the value to set
743  */
744 int __var_Set( vlc_object_t *p_this, const char *psz_name, vlc_value_t val )
745 {
746     int i_var;
747     variable_t *p_var;
748     vlc_value_t oldval;
749     vlc_object_internals_t *p_priv = vlc_internals( p_this );
750
751     vlc_mutex_lock( &p_priv->var_lock );
752
753     i_var = GetUnused( p_this, psz_name );
754     if( i_var < 0 )
755     {
756         vlc_mutex_unlock( &p_priv->var_lock );
757         return i_var;
758     }
759
760     p_var = &p_priv->p_vars[i_var];
761
762     /* Duplicate data if needed */
763     p_var->ops->pf_dup( &val );
764
765     /* Backup needed stuff */
766     oldval = p_var->val;
767
768     /* Check boundaries and list */
769     CheckValue( p_var, &val );
770
771     /* Set the variable */
772     p_var->val = val;
773
774     /* Deal with callbacks. Tell we're in a callback, release the lock,
775      * call stored functions, retake the lock. */
776     if( p_var->i_entries )
777     {
778         int i_var;
779         int i_entries = p_var->i_entries;
780         callback_entry_t *p_entries = p_var->p_entries;
781
782         p_var->b_incallback = true;
783         vlc_mutex_unlock( &p_priv->var_lock );
784
785         /* The real calls */
786         for( ; i_entries-- ; )
787         {
788             p_entries[i_entries].pf_callback( p_this, psz_name, oldval, val,
789                                               p_entries[i_entries].p_data );
790         }
791
792         vlc_mutex_lock( &p_priv->var_lock );
793
794         i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
795         if( i_var < 0 )
796         {
797             msg_Err( p_this, "variable %s has disappeared", psz_name );
798             vlc_mutex_unlock( &p_priv->var_lock );
799             return VLC_ENOVAR;
800         }
801
802         p_var = &p_priv->p_vars[i_var];
803         p_var->b_incallback = false;
804     }
805
806     /* Free data if needed */
807     p_var->ops->pf_free( &oldval );
808
809     vlc_mutex_unlock( &p_priv->var_lock );
810
811     return VLC_SUCCESS;
812 }
813
814 /**
815  * Get a variable's value
816  *
817  * \param p_this The object that holds the variable
818  * \param psz_name The name of the variable
819  * \param p_val Pointer to a vlc_value_t that will hold the variable's value
820  *              after the function is finished
821  */
822 int __var_Get( vlc_object_t *p_this, const char *psz_name, vlc_value_t *p_val )
823 {
824     int i_var;
825     variable_t *p_var;
826     vlc_object_internals_t *p_priv = vlc_internals( p_this );
827
828     vlc_mutex_lock( &p_priv->var_lock );
829
830     i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
831
832     if( i_var < 0 )
833     {
834         vlc_mutex_unlock( &p_priv->var_lock );
835         return VLC_ENOVAR;
836     }
837
838     p_var = &p_priv->p_vars[i_var];
839
840     /* Really get the variable */
841     *p_val = p_var->val;
842
843     /* Duplicate value if needed */
844     p_var->ops->pf_dup( p_val );
845
846     vlc_mutex_unlock( &p_priv->var_lock );
847
848     return VLC_SUCCESS;
849 }
850
851
852 /**
853  * Register a callback in a variable
854  *
855  * We store a function pointer that will be called upon variable
856  * modification.
857  *
858  * \param p_this The object that holds the variable
859  * \param psz_name The name of the variable
860  * \param pf_callback The function pointer
861  * \param p_data A generic pointer that will be passed as the last
862  *               argument to the callback function.
863  *
864  * \warning The callback function is run in the thread that calls var_Set on
865  *          the variable. Use proper locking. This thread may not have much
866  *          time to spare, so keep callback functions short.
867  */
868 int __var_AddCallback( vlc_object_t *p_this, const char *psz_name,
869                        vlc_callback_t pf_callback, void *p_data )
870 {
871     int i_var;
872     variable_t *p_var;
873     callback_entry_t entry;
874     vlc_object_internals_t *p_priv = vlc_internals( p_this );
875
876     entry.pf_callback = pf_callback;
877     entry.p_data = p_data;
878
879     vlc_mutex_lock( &p_priv->var_lock );
880
881     i_var = GetUnused( p_this, psz_name );
882     if( i_var < 0 )
883     {
884         vlc_mutex_unlock( &p_priv->var_lock );
885         return i_var;
886     }
887
888     p_var = &p_priv->p_vars[i_var];
889
890     INSERT_ELEM( p_var->p_entries,
891                  p_var->i_entries,
892                  p_var->i_entries,
893                  entry );
894
895     vlc_mutex_unlock( &p_priv->var_lock );
896
897     return VLC_SUCCESS;
898 }
899
900 /**
901  * Remove a callback from a variable
902  *
903  * pf_callback and p_data have to be given again, because different objects
904  * might have registered the same callback function.
905  */
906 int __var_DelCallback( vlc_object_t *p_this, const char *psz_name,
907                        vlc_callback_t pf_callback, void *p_data )
908 {
909     int i_entry, i_var;
910     variable_t *p_var;
911     vlc_object_internals_t *p_priv = vlc_internals( p_this );
912
913     vlc_mutex_lock( &p_priv->var_lock );
914
915     i_var = GetUnused( p_this, psz_name );
916     if( i_var < 0 )
917     {
918         vlc_mutex_unlock( &p_priv->var_lock );
919         return i_var;
920     }
921
922     p_var = &p_priv->p_vars[i_var];
923
924     for( i_entry = p_var->i_entries ; i_entry-- ; )
925     {
926         if( p_var->p_entries[i_entry].pf_callback == pf_callback
927             && p_var->p_entries[i_entry].p_data == p_data )
928         {
929             break;
930         }
931     }
932
933     if( i_entry < 0 )
934     {
935         vlc_mutex_unlock( &p_priv->var_lock );
936         return VLC_EGENERIC;
937     }
938
939     REMOVE_ELEM( p_var->p_entries, p_var->i_entries, i_entry );
940
941     vlc_mutex_unlock( &p_priv->var_lock );
942
943     return VLC_SUCCESS;
944 }
945
946 /**
947  * Trigger callback on a variable
948  *
949  * \param p_this The object that hold the variable
950  * \param psz_name The name of the variable
951  */
952 int __var_TriggerCallback( vlc_object_t *p_this, const char *psz_name )
953 {
954     int i_var;
955     variable_t *p_var;
956     vlc_value_t oldval;
957     vlc_object_internals_t *p_priv = vlc_internals( p_this );
958
959     vlc_mutex_lock( &p_priv->var_lock );
960
961     i_var = GetUnused( p_this, psz_name );
962     if( i_var < 0 )
963     {
964         vlc_mutex_unlock( &p_priv->var_lock );
965         return i_var;
966     }
967
968     p_var = &p_priv->p_vars[i_var];
969
970     /* Backup needed stuff */
971     oldval = p_var->val;
972
973     /* Deal with callbacks. Tell we're in a callback, release the lock,
974      * call stored functions, retake the lock. */
975     if( p_var->i_entries )
976     {
977         int i_var;
978         int i_entries = p_var->i_entries;
979         callback_entry_t *p_entries = p_var->p_entries;
980
981         p_var->b_incallback = true;
982         vlc_mutex_unlock( &p_priv->var_lock );
983
984         /* The real calls */
985         for( ; i_entries-- ; )
986         {
987             p_entries[i_entries].pf_callback( p_this, psz_name, oldval, oldval,
988                                               p_entries[i_entries].p_data );
989         }
990
991         vlc_mutex_lock( &p_priv->var_lock );
992
993         i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
994         if( i_var < 0 )
995         {
996             msg_Err( p_this, "variable %s has disappeared", psz_name );
997             vlc_mutex_unlock( &p_priv->var_lock );
998             return VLC_ENOVAR;
999         }
1000
1001         p_var = &p_priv->p_vars[i_var];
1002         p_var->b_incallback = false;
1003     }
1004
1005     vlc_mutex_unlock( &p_priv->var_lock );
1006     return VLC_SUCCESS;
1007 }
1008
1009 /** Parse a stringified option
1010  * This function parse a string option and create the associated object
1011  * variable
1012  * The option must be of the form "[no[-]]foo[=bar]" where foo is the
1013  * option name and bar is the value of the option.
1014  * \param p_obj the object in which the variable must be created
1015  * \param psz_option the option to parse
1016  * \param trusted whether the option is set by a trusted input or not
1017  * \return nothing
1018  */
1019 void var_OptionParse( vlc_object_t *p_obj, const char *psz_option,
1020                       bool trusted )
1021 {
1022     char *psz_name, *psz_value;
1023     int  i_type;
1024     bool b_isno = false;
1025     vlc_value_t val;
1026
1027     val.psz_string = NULL;
1028
1029     /* It's too much of a hassle to remove the ':' when we parse
1030      * the cmd line :) */
1031     if( psz_option[0] == ':' )
1032         psz_option++;
1033
1034     if( !psz_option[0] )
1035         return;
1036
1037     psz_name = strdup( psz_option );
1038     if( psz_name == NULL )
1039         return;
1040
1041     psz_value = strchr( psz_name, '=' );
1042     if( psz_value != NULL )
1043         *psz_value++ = '\0';
1044
1045     /* FIXME: :programs should be handled generically */
1046     if( !strcmp( psz_name, "programs" ) )
1047         i_type = VLC_VAR_LIST;
1048     else
1049         i_type = config_GetType( p_obj, psz_name );
1050
1051     if( !i_type && !psz_value )
1052     {
1053         /* check for "no-foo" or "nofoo" */
1054         if( !strncmp( psz_name, "no-", 3 ) )
1055         {
1056             memmove( psz_name, psz_name + 3, strlen(psz_name) + 1 - 3 );
1057         }
1058         else if( !strncmp( psz_name, "no", 2 ) )
1059         {
1060             memmove( psz_name, psz_name + 2, strlen(psz_name) + 1 - 2 );
1061         }
1062         else goto cleanup;           /* Option doesn't exist */
1063
1064         b_isno = true;
1065         i_type = config_GetType( p_obj, psz_name );
1066     }
1067     if( !i_type ) goto cleanup; /* Option doesn't exist */
1068
1069     if( ( i_type != VLC_VAR_BOOL ) &&
1070         ( !psz_value || !*psz_value ) ) goto cleanup; /* Invalid value */
1071
1072     /* check if option is unsafe */
1073     if( !trusted )
1074     {
1075         module_config_t *p_config = config_FindConfig( p_obj, psz_name );
1076         if( !p_config || !p_config->b_safe )
1077         {
1078             msg_Err( p_obj, "unsafe option \"%s\" has been ignored for "
1079                             "security reasons", psz_name );
1080             free( psz_name );
1081             return;
1082         }
1083     }
1084
1085     /* Create the variable in the input object.
1086      * Children of the input object will be able to retreive this value
1087      * thanks to the inheritance property of the object variables. */
1088     var_Create( p_obj, psz_name, i_type );
1089
1090     switch( i_type )
1091     {
1092     case VLC_VAR_BOOL:
1093         val.b_bool = !b_isno;
1094         break;
1095
1096     case VLC_VAR_INTEGER:
1097         val.i_int = strtol( psz_value, NULL, 0 );
1098         break;
1099
1100     case VLC_VAR_FLOAT:
1101         val.f_float = atof( psz_value );
1102         break;
1103
1104     case VLC_VAR_STRING:
1105     case VLC_VAR_MODULE:
1106     case VLC_VAR_FILE:
1107     case VLC_VAR_DIRECTORY:
1108         val.psz_string = psz_value;
1109         break;
1110
1111     case VLC_VAR_LIST:
1112     {
1113         char *psz_orig, *psz_var;
1114         vlc_list_t *p_list = malloc(sizeof(vlc_list_t));
1115         val.p_list = p_list;
1116         p_list->i_count = 0;
1117
1118         psz_var = psz_orig = strdup(psz_value);
1119         while( psz_var && *psz_var )
1120         {
1121             char *psz_item = psz_var;
1122             vlc_value_t val2;
1123             while( *psz_var && *psz_var != ',' ) psz_var++;
1124             if( *psz_var == ',' )
1125             {
1126                 *psz_var = '\0';
1127                 psz_var++;
1128             }
1129             val2.i_int = strtol( psz_item, NULL, 0 );
1130             INSERT_ELEM( p_list->p_values, p_list->i_count,
1131                          p_list->i_count, val2 );
1132             /* p_list->i_count is incremented twice by INSERT_ELEM */
1133             p_list->i_count--;
1134             INSERT_ELEM( p_list->pi_types, p_list->i_count,
1135                          p_list->i_count, VLC_VAR_INTEGER );
1136         }
1137         free( psz_orig );
1138         break;
1139     }
1140
1141     default:
1142         goto cleanup;
1143     }
1144
1145     var_Set( p_obj, psz_name, val );
1146
1147     // If that's a list, remove all elements allocated
1148     if( i_type == VLC_VAR_LIST )
1149         FreeList( &val );
1150
1151 cleanup:
1152     free( psz_name );
1153 }
1154
1155
1156 /* Following functions are local */
1157
1158 /*****************************************************************************
1159  * GetUnused: find an unused variable from its name
1160  *****************************************************************************
1161  * We do i_tries tries before giving up, just in case the variable is being
1162  * modified and called from a callback.
1163  *****************************************************************************/
1164 static int GetUnused( vlc_object_t *p_this, const char *psz_name )
1165 {
1166     int i_var, i_tries = 0;
1167     vlc_object_internals_t *p_priv = vlc_internals( p_this );
1168
1169     while( true )
1170     {
1171         i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
1172         if( i_var < 0 )
1173         {
1174             return VLC_ENOVAR;
1175         }
1176
1177         if( ! p_priv->p_vars[i_var].b_incallback )
1178         {
1179             return i_var;
1180         }
1181
1182         if( i_tries++ > 100 )
1183         {
1184             msg_Err( p_this, "caught in a callback deadlock? ('%s')", psz_name );
1185             return VLC_ETIMEOUT;
1186         }
1187
1188         vlc_mutex_unlock( &p_priv->var_lock );
1189         msleep( THREAD_SLEEP );
1190         vlc_mutex_lock( &p_priv->var_lock );
1191     }
1192 }
1193
1194 /*****************************************************************************
1195  * HashString: our cool hash function
1196  *****************************************************************************
1197  * This function is not intended to be crypto-secure, we only want it to be
1198  * fast and not suck too much. This one is pretty fast and did 0 collisions
1199  * in wenglish's dictionary.
1200  *****************************************************************************/
1201 static uint32_t HashString( const char *psz_string )
1202 {
1203     uint32_t i_hash = 0;
1204
1205     while( *psz_string )
1206     {
1207         i_hash += *psz_string++;
1208         i_hash += i_hash << 10;
1209         i_hash ^= i_hash >> 8;
1210     }
1211
1212     return i_hash;
1213 }
1214
1215 /*****************************************************************************
1216  * Insert: find an empty slot to insert a new variable
1217  *****************************************************************************
1218  * We use a recursive inner function indexed on the hash. This function does
1219  * nothing in the rare cases where a collision may occur, see Lookup()
1220  * to see how we handle them.
1221  * XXX: does this really need to be written recursively?
1222  *****************************************************************************/
1223 static int Insert( variable_t *p_vars, int i_count, const char *psz_name )
1224 {
1225     if( i_count == 0 )
1226     {
1227         return 0;
1228     }
1229
1230     return InsertInner( p_vars, i_count, HashString( psz_name ) );
1231 }
1232
1233 static int InsertInner( variable_t *p_vars, int i_count, uint32_t i_hash )
1234 {
1235     int i_middle;
1236
1237     if( i_hash <= p_vars[0].i_hash )
1238     {
1239         return 0;
1240     }
1241
1242     if( i_hash >= p_vars[i_count - 1].i_hash )
1243     {
1244         return i_count;
1245     }
1246
1247     i_middle = i_count / 2;
1248
1249     /* We know that 0 < i_middle */
1250     if( i_hash < p_vars[i_middle].i_hash )
1251     {
1252         return InsertInner( p_vars, i_middle, i_hash );
1253     }
1254
1255     /* We know that i_middle + 1 < i_count */
1256     if( i_hash > p_vars[i_middle + 1].i_hash )
1257     {
1258         return i_middle + 1 + InsertInner( p_vars + i_middle + 1,
1259                                            i_count - i_middle - 1,
1260                                            i_hash );
1261     }
1262
1263     return i_middle + 1;
1264 }
1265
1266 /*****************************************************************************
1267  * Lookup: find an existing variable given its name
1268  *****************************************************************************
1269  * We use a recursive inner function indexed on the hash. Care is taken of
1270  * possible hash collisions.
1271  * XXX: does this really need to be written recursively?
1272  *****************************************************************************/
1273 static int Lookup( variable_t *p_vars, int i_count, const char *psz_name )
1274 {
1275     uint32_t i_hash;
1276     int i, i_pos;
1277
1278     if( i_count == 0 )
1279     {
1280         return -1;
1281     }
1282
1283     i_hash = HashString( psz_name );
1284
1285     i_pos = LookupInner( p_vars, i_count, i_hash );
1286
1287     /* Hash not found */
1288     if( i_hash != p_vars[i_pos].i_hash )
1289     {
1290         return -1;
1291     }
1292
1293     /* Hash found, entry found */
1294     if( !strcmp( psz_name, p_vars[i_pos].psz_name ) )
1295     {
1296         return i_pos;
1297     }
1298
1299     /* Hash collision! This should be very rare, but we cannot guarantee
1300      * it will never happen. Just do an exhaustive search amongst all
1301      * entries with the same hash. */
1302     for( i = i_pos - 1 ; i > 0 && i_hash == p_vars[i].i_hash ; i-- )
1303     {
1304         if( !strcmp( psz_name, p_vars[i].psz_name ) )
1305         {
1306             return i;
1307         }
1308     }
1309
1310     for( i = i_pos + 1 ; i < i_count && i_hash == p_vars[i].i_hash ; i++ )
1311     {
1312         if( !strcmp( psz_name, p_vars[i].psz_name ) )
1313         {
1314             return i;
1315         }
1316     }
1317
1318     /* Hash found, but entry not found */
1319     return -1;
1320 }
1321
1322 static int LookupInner( variable_t *p_vars, int i_count, uint32_t i_hash )
1323 {
1324     int i_middle;
1325
1326     if( i_hash <= p_vars[0].i_hash )
1327     {
1328         return 0;
1329     }
1330
1331     if( i_hash >= p_vars[i_count-1].i_hash )
1332     {
1333         return i_count - 1;
1334     }
1335
1336     i_middle = i_count / 2;
1337
1338     /* We know that 0 < i_middle */
1339     if( i_hash < p_vars[i_middle].i_hash )
1340     {
1341         return LookupInner( p_vars, i_middle, i_hash );
1342     }
1343
1344     /* We know that i_middle + 1 < i_count */
1345     if( i_hash > p_vars[i_middle].i_hash )
1346     {
1347         return i_middle + LookupInner( p_vars + i_middle,
1348                                        i_count - i_middle,
1349                                        i_hash );
1350     }
1351
1352     return i_middle;
1353 }
1354
1355 /*****************************************************************************
1356  * CheckValue: check that a value is valid wrt. a variable
1357  *****************************************************************************
1358  * This function checks p_val's value against p_var's limitations such as
1359  * minimal and maximal value, step, in-list position, and modifies p_val if
1360  * necessary.
1361  ****************************************************************************/
1362 static void CheckValue ( variable_t *p_var, vlc_value_t *p_val )
1363 {
1364     /* Check that our variable is in the list */
1365     if( p_var->i_type & VLC_VAR_HASCHOICE && p_var->choices.i_count )
1366     {
1367         int i;
1368
1369         /* FIXME: the list is sorted, dude. Use something cleverer. */
1370         for( i = p_var->choices.i_count ; i-- ; )
1371         {
1372             if( p_var->ops->pf_cmp( *p_val, p_var->choices.p_values[i] ) == 0 )
1373             {
1374                 break;
1375             }
1376         }
1377
1378         /* If not found, change it to anything vaguely valid */
1379         if( i < 0 )
1380         {
1381             /* Free the old variable, get the new one, dup it */
1382             p_var->ops->pf_free( p_val );
1383             *p_val = p_var->choices.p_values[p_var->i_default >= 0
1384                                           ? p_var->i_default : 0 ];
1385             p_var->ops->pf_dup( p_val );
1386         }
1387     }
1388
1389     /* Check that our variable is within the bounds */
1390     switch( p_var->i_type & VLC_VAR_TYPE )
1391     {
1392         case VLC_VAR_INTEGER:
1393             if( p_var->i_type & VLC_VAR_HASSTEP && p_var->step.i_int
1394                  && (p_val->i_int % p_var->step.i_int) )
1395             {
1396                 p_val->i_int = (p_val->i_int + (p_var->step.i_int / 2))
1397                                / p_var->step.i_int * p_var->step.i_int;
1398             }
1399             if( p_var->i_type & VLC_VAR_HASMIN
1400                  && p_val->i_int < p_var->min.i_int )
1401             {
1402                 p_val->i_int = p_var->min.i_int;
1403             }
1404             if( p_var->i_type & VLC_VAR_HASMAX
1405                  && p_val->i_int > p_var->max.i_int )
1406             {
1407                 p_val->i_int = p_var->max.i_int;
1408             }
1409             break;
1410         case VLC_VAR_FLOAT:
1411             if( p_var->i_type & VLC_VAR_HASSTEP && p_var->step.f_float )
1412             {
1413                 float f_round = p_var->step.f_float * (float)(int)( 0.5 +
1414                                         p_val->f_float / p_var->step.f_float );
1415                 if( p_val->f_float != f_round )
1416                 {
1417                     p_val->f_float = f_round;
1418                 }
1419             }
1420             if( p_var->i_type & VLC_VAR_HASMIN
1421                  && p_val->f_float < p_var->min.f_float )
1422             {
1423                 p_val->f_float = p_var->min.f_float;
1424             }
1425             if( p_var->i_type & VLC_VAR_HASMAX
1426                  && p_val->f_float > p_var->max.f_float )
1427             {
1428                 p_val->f_float = p_var->max.f_float;
1429             }
1430             break;
1431         case VLC_VAR_TIME:
1432             /* FIXME: TODO */
1433             break;
1434     }
1435 }
1436
1437 /*****************************************************************************
1438  * InheritValue: try to inherit the value of this variable from the same one
1439  * in our closest parent, libvlc or ultimately from the configuration.
1440  * The function should always be entered with the object var_lock locked.
1441  *****************************************************************************/
1442 static int InheritValue( vlc_object_t *p_this, const char *psz_name,
1443                          vlc_value_t *p_val, int i_type )
1444 {
1445     int i_var;
1446     variable_t *p_var;
1447
1448     if( p_this->p_parent || ( p_this->p_libvlc && p_this != p_this->p_libvlc ) )
1449     {
1450         vlc_object_internals_t *p_priv;
1451
1452         if( p_this->p_parent )
1453             p_priv = vlc_internals( p_this->p_parent );
1454         else
1455             p_priv = vlc_internals( p_this->p_libvlc );
1456
1457         i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
1458
1459         if( i_var >= 0 )
1460         {
1461             /* We found it! */
1462             p_var = &p_priv->p_vars[i_var];
1463
1464             /* Really get the variable */
1465             *p_val = p_var->val;
1466
1467             /* Duplicate value if needed */
1468             p_var->ops->pf_dup( p_val );
1469
1470             return VLC_SUCCESS;
1471         }
1472         else if ( p_this->p_parent ) /* We are still not there */
1473             return InheritValue( p_this->p_parent, psz_name, p_val, i_type );
1474
1475         /* else take value from config */
1476     }
1477
1478
1479     switch( i_type & VLC_VAR_CLASS )
1480     {
1481         case VLC_VAR_STRING:
1482             p_val->psz_string = config_GetPsz( p_this, psz_name );
1483             if( !p_val->psz_string ) p_val->psz_string = strdup("");
1484             break;
1485         case VLC_VAR_FLOAT:
1486             p_val->f_float = config_GetFloat( p_this, psz_name );
1487             break;
1488         case VLC_VAR_INTEGER:
1489             p_val->i_int = config_GetInt( p_this, psz_name );
1490             break;
1491         case VLC_VAR_BOOL:
1492             p_val->b_bool = config_GetInt( p_this, psz_name );
1493             break;
1494         case VLC_VAR_LIST:
1495         {
1496             char *psz_orig, *psz_var;
1497             vlc_list_t *p_list = malloc(sizeof(vlc_list_t));
1498             p_val->p_list = p_list;
1499             p_list->i_count = 0;
1500
1501             psz_var = psz_orig = config_GetPsz( p_this, psz_name );
1502             while( psz_var && *psz_var )
1503             {
1504                 char *psz_item = psz_var;
1505                 vlc_value_t val;
1506                 while( *psz_var && *psz_var != ',' ) psz_var++;
1507                 if( *psz_var == ',' )
1508                 {
1509                     *psz_var = '\0';
1510                     psz_var++;
1511                 }
1512                 val.i_int = strtol( psz_item, NULL, 0 );
1513                 INSERT_ELEM( p_list->p_values, p_list->i_count,
1514                              p_list->i_count, val );
1515                 /* p_list->i_count is incremented twice by INSERT_ELEM */
1516                 p_list->i_count--;
1517                 INSERT_ELEM( p_list->pi_types, p_list->i_count,
1518                              p_list->i_count, VLC_VAR_INTEGER );
1519             }
1520             free( psz_orig );
1521             break;
1522         }
1523         default:
1524             return VLC_ENOOBJ;
1525             break;
1526     }
1527     return VLC_SUCCESS;
1528 }
1529
1530 /**********************************************************************
1531  * Execute a var command on an object identified by its name
1532  **********************************************************************/
1533 int __var_Command( vlc_object_t *p_this, const char *psz_name,
1534                    const char *psz_cmd, const char *psz_arg, char **psz_msg )
1535 {
1536     vlc_object_t *p_obj = vlc_object_find_name( p_this->p_libvlc,
1537                                                 psz_name, FIND_CHILD );
1538     int i_type, i_ret;
1539
1540     if( !p_obj )
1541     {
1542         if( psz_msg )
1543             *psz_msg = strdup( "Unknown destination object." );
1544         return VLC_ENOOBJ;
1545     }
1546
1547     i_type = var_Type( p_obj, psz_cmd );
1548     if( !( i_type&VLC_VAR_ISCOMMAND ) )
1549     {
1550         vlc_object_release( p_obj );
1551         if( psz_msg )
1552             *psz_msg = strdup( "Variable doesn't exist or isn't a command." );
1553         return VLC_EGENERIC;
1554     }
1555
1556     i_type &= VLC_VAR_CLASS;
1557     switch( i_type )
1558     {
1559         case VLC_VAR_INTEGER:
1560             i_ret = var_SetInteger( p_obj, psz_cmd, atoi( psz_arg ) );
1561             break;
1562         case VLC_VAR_FLOAT:
1563             i_ret = var_SetFloat( p_obj, psz_cmd, atof( psz_arg ) );
1564             break;
1565         case VLC_VAR_STRING:
1566             i_ret = var_SetString( p_obj, psz_cmd, psz_arg );
1567             break;
1568         case VLC_VAR_BOOL:
1569             i_ret = var_SetBool( p_obj, psz_cmd, atoi( psz_arg ) );
1570             break;
1571         default:
1572             i_ret = VLC_EGENERIC;
1573             break;
1574     }
1575
1576     vlc_object_release( p_obj );
1577
1578     if( psz_msg )
1579     {
1580         if( asprintf( psz_msg, "%s on object %s returned %i (%s)",
1581                   psz_cmd, psz_name, i_ret, vlc_error( i_ret ) ) == -1)
1582             *psz_msg = NULL;
1583     }
1584
1585     return i_ret;
1586 }