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