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