]> git.sesse.net Git - vlc/blob - src/misc/variables.c
Remove redumdant parameter to vlc_global
[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 #ifdef HAVE_STDLIB_H
31 #   include <stdlib.h>                                          /* realloc() */
32 #endif
33 #include "libvlc.h"
34
35 /*****************************************************************************
36  * Private types
37  *****************************************************************************/
38 struct callback_entry_t
39 {
40     vlc_callback_t pf_callback;
41     void *         p_data;
42 };
43
44 /*****************************************************************************
45  * Local comparison functions, returns 0 if v == w, < 0 if v < w, > 0 if v > w
46  *****************************************************************************/
47 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; }
48 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; }
49 static int CmpTime( vlc_value_t v, vlc_value_t w )
50 {
51     return v.i_time == w.i_time ? 0 : v.i_time > w.i_time ? 1 : -1;
52 }
53 static int CmpString( vlc_value_t v, vlc_value_t w ) { return strcmp( v.psz_string, w.psz_string ); }
54 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; }
55 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; }
56
57 /*****************************************************************************
58  * Local duplication functions, and local deallocation functions
59  *****************************************************************************/
60 static void DupDummy( vlc_value_t *p_val ) { (void)p_val; /* unused */ }
61 static void DupString( vlc_value_t *p_val ) { p_val->psz_string = strdup( p_val->psz_string ); }
62
63 static void DupList( vlc_value_t *p_val )
64 {
65     int i;
66     vlc_list_t *p_list = malloc( sizeof(vlc_list_t) );
67
68     p_list->i_count = p_val->p_list->i_count;
69     if( p_val->p_list->i_count )
70     {
71         p_list->p_values = malloc( p_list->i_count * sizeof(vlc_value_t) );
72         p_list->pi_types = malloc( p_list->i_count * sizeof(int) );
73     }
74     else
75     {
76         p_list->p_values = NULL;
77         p_list->pi_types = NULL;
78     }
79
80     for( i = 0; i < p_list->i_count; i++ )
81     {
82         p_list->p_values[i] = p_val->p_list->p_values[i];
83         p_list->pi_types[i] = p_val->p_list->pi_types[i];
84         switch( p_val->p_list->pi_types[i] & VLC_VAR_TYPE )
85         {
86         case VLC_VAR_STRING:
87
88             DupString( &p_list->p_values[i] );
89             break;
90         default:
91             break;
92         }
93     }
94
95     p_val->p_list = p_list;
96 }
97
98 static void FreeDummy( vlc_value_t *p_val ) { (void)p_val; /* unused */ }
99 static void FreeString( vlc_value_t *p_val ) { free( p_val->psz_string ); }
100 static void FreeMutex( vlc_value_t *p_val ) { vlc_mutex_destroy( (vlc_mutex_t*)p_val->p_address ); free( p_val->p_address ); }
101
102 static void FreeList( vlc_value_t *p_val )
103 {
104     int i;
105     for( i = 0; i < p_val->p_list->i_count; i++ )
106     {
107         switch( p_val->p_list->pi_types[i] & VLC_VAR_TYPE )
108         {
109         case VLC_VAR_STRING:
110             FreeString( &p_val->p_list->p_values[i] );
111             break;
112         case VLC_VAR_MUTEX:
113             FreeMutex( &p_val->p_list->p_values[i] );
114             break;
115         default:
116             break;
117         }
118     }
119
120     if( p_val->p_list->i_count )
121     {
122         free( p_val->p_list->p_values );
123         free( p_val->p_list->pi_types );
124     }
125     free( p_val->p_list );
126 }
127
128 /*****************************************************************************
129  * Local prototypes
130  *****************************************************************************/
131 static int      GetUnused   ( vlc_object_t *, const char * );
132 static uint32_t HashString  ( const char * );
133 static int      Insert      ( variable_t *, int, const char * );
134 static int      InsertInner ( variable_t *, int, uint32_t );
135 static int      Lookup      ( variable_t *, int, const char * );
136 static int      LookupInner ( variable_t *, int, uint32_t );
137
138 static void     CheckValue  ( variable_t *, vlc_value_t * );
139
140 static int      InheritValue( vlc_object_t *, const char *, vlc_value_t *,
141                               int );
142
143 /**
144  * Initialize a vlc variable
145  *
146  * We hash the given string and insert it into the sorted list. The insertion
147  * may require slow memory copies, but think about what we gain in the log(n)
148  * lookup phase when setting/getting the variable value!
149  *
150  * \param p_this The object in which to create the variable
151  * \param psz_name The name of the variable
152  * \param i_type The variables type. Must be one of \ref var_type combined with
153  *               zero or more \ref var_flags
154  */
155 int __var_Create( vlc_object_t *p_this, const char *psz_name, int i_type )
156 {
157     int i_new;
158     variable_t *p_var;
159     static vlc_list_t dummy_null_list = {0, NULL, NULL};
160
161     vlc_mutex_lock( &p_this->var_lock );
162
163     /* FIXME: if the variable already exists, we don't duplicate it. But we
164      * duplicate the lookups. It's not that serious, but if anyone finds some
165      * time to rework Insert() so that only one lookup has to be done, feel
166      * free to do so. */
167     i_new = Lookup( p_this->p_vars, p_this->i_vars, psz_name );
168
169     if( i_new >= 0 )
170     {
171         /* If the types differ, variable creation failed. */
172         if( (i_type & ~(VLC_VAR_DOINHERIT|VLC_VAR_ISCOMMAND)) != p_this->p_vars[i_new].i_type )
173         {
174             vlc_mutex_unlock( &p_this->var_lock );
175             return VLC_EBADVAR;
176         }
177
178         p_this->p_vars[i_new].i_usage++;
179         if( i_type & VLC_VAR_ISCOMMAND )
180             p_this->p_vars[i_new].i_type |= VLC_VAR_ISCOMMAND;
181         vlc_mutex_unlock( &p_this->var_lock );
182         return VLC_SUCCESS;
183     }
184
185     i_new = Insert( p_this->p_vars, p_this->i_vars, psz_name );
186
187     if( (p_this->i_vars & 15) == 15 )
188     {
189         p_this->p_vars = realloc( p_this->p_vars,
190                                   (p_this->i_vars+17) * sizeof(variable_t) );
191     }
192
193     memmove( p_this->p_vars + i_new + 1,
194              p_this->p_vars + i_new,
195              (p_this->i_vars - i_new) * sizeof(variable_t) );
196
197     p_this->i_vars++;
198
199     p_var = &p_this->p_vars[i_new];
200     memset( p_var, 0, sizeof(*p_var) );
201
202     p_var->i_hash = HashString( psz_name );
203     p_var->psz_name = strdup( psz_name );
204     p_var->psz_text = NULL;
205
206     p_var->i_type = i_type & ~VLC_VAR_DOINHERIT;
207     memset( &p_var->val, 0, sizeof(vlc_value_t) );
208
209     p_var->pf_dup = DupDummy;
210     p_var->pf_free = FreeDummy;
211
212     p_var->i_usage = 1;
213
214     p_var->i_default = -1;
215     p_var->choices.i_count = 0;
216     p_var->choices.p_values = NULL;
217     p_var->choices_text.i_count = 0;
218     p_var->choices_text.p_values = NULL;
219
220     p_var->b_incallback = VLC_FALSE;
221     p_var->i_entries = 0;
222     p_var->p_entries = NULL;
223
224     /* Always initialize the variable, even if it is a list variable; this
225      * will lead to errors if the variable is not initialized, but it will
226      * not cause crashes in the variable handling. */
227     switch( i_type & VLC_VAR_TYPE )
228     {
229         case VLC_VAR_BOOL:
230             p_var->pf_cmp = CmpBool;
231             p_var->val.b_bool = VLC_FALSE;
232             break;
233         case VLC_VAR_INTEGER:
234         case VLC_VAR_HOTKEY:
235             p_var->pf_cmp = CmpInt;
236             p_var->val.i_int = 0;
237             break;
238         case VLC_VAR_STRING:
239         case VLC_VAR_MODULE:
240         case VLC_VAR_FILE:
241         case VLC_VAR_DIRECTORY:
242         case VLC_VAR_VARIABLE:
243             p_var->pf_cmp = CmpString;
244             p_var->pf_dup = DupString;
245             p_var->pf_free = FreeString;
246             p_var->val.psz_string = "";
247             break;
248         case VLC_VAR_FLOAT:
249             p_var->pf_cmp = CmpFloat;
250             p_var->val.f_float = 0.0;
251             break;
252         case VLC_VAR_TIME:
253             p_var->pf_cmp = CmpTime;
254             p_var->val.i_time = 0;
255             break;
256         case VLC_VAR_ADDRESS:
257             p_var->pf_cmp = CmpAddress;
258             p_var->val.p_address = NULL;
259             break;
260         case VLC_VAR_MUTEX:
261             p_var->pf_cmp = CmpAddress;
262             p_var->pf_free = FreeMutex;
263             p_var->val.p_address = malloc( sizeof(vlc_mutex_t) );
264             vlc_mutex_init( p_this, (vlc_mutex_t*)p_var->val.p_address );
265             break;
266         case VLC_VAR_LIST:
267             p_var->pf_cmp = CmpAddress;
268             p_var->pf_dup = DupList;
269             p_var->pf_free = FreeList;
270             p_var->val.p_list = &dummy_null_list;
271             break;
272     }
273
274     /* Duplicate the default data we stored. */
275     p_var->pf_dup( &p_var->val );
276
277     if( i_type & VLC_VAR_DOINHERIT )
278     {
279         vlc_value_t val;
280
281         if( InheritValue( p_this, psz_name, &val, p_var->i_type )
282             == VLC_SUCCESS )
283         {
284             /* Free data if needed */
285             p_var->pf_free( &p_var->val );
286             /* Set the variable */
287             p_var->val = val;
288
289             if( i_type & VLC_VAR_HASCHOICE )
290             {
291                 /* We must add the inherited value to our choice list */
292                 p_var->i_default = 0;
293
294                 INSERT_ELEM( p_var->choices.p_values, p_var->choices.i_count,
295                              0, val );
296                 INSERT_ELEM( p_var->choices_text.p_values,
297                              p_var->choices_text.i_count, 0, val );
298                 p_var->pf_dup( &p_var->choices.p_values[0] );
299                 p_var->choices_text.p_values[0].psz_string = NULL;
300             }
301         }
302     }
303
304     vlc_mutex_unlock( &p_this->var_lock );
305
306     return VLC_SUCCESS;
307 }
308
309 /**
310  * Destroy a vlc variable
311  *
312  * Look for the variable and destroy it if it is found. As in var_Create we
313  * do a call to memmove() but we have performance counterparts elsewhere.
314  *
315  * \param p_this The object that holds the variable
316  * \param psz_name The name of the variable
317  */
318 int __var_Destroy( vlc_object_t *p_this, const char *psz_name )
319 {
320     int i_var, i;
321     variable_t *p_var;
322
323     vlc_mutex_lock( &p_this->var_lock );
324
325     i_var = GetUnused( p_this, psz_name );
326     if( i_var < 0 )
327     {
328         vlc_mutex_unlock( &p_this->var_lock );
329         return i_var;
330     }
331
332     p_var = &p_this->p_vars[i_var];
333
334     if( p_var->i_usage > 1 )
335     {
336         p_var->i_usage--;
337         vlc_mutex_unlock( &p_this->var_lock );
338         return VLC_SUCCESS;
339     }
340
341     /* Free value if needed */
342     p_var->pf_free( &p_var->val );
343
344     /* Free choice list if needed */
345     if( p_var->choices.i_count )
346     {
347         for( i = 0 ; i < p_var->choices.i_count ; i++ )
348         {
349             p_var->pf_free( &p_var->choices.p_values[i] );
350             if( p_var->choices_text.p_values[i].psz_string )
351                 free( p_var->choices_text.p_values[i].psz_string );
352         }
353         free( p_var->choices.p_values );
354         free( p_var->choices_text.p_values );
355     }
356
357     /* Free callbacks if needed */
358     if( p_var->p_entries )
359     {
360         free( p_var->p_entries );
361     }
362
363     free( p_var->psz_name );
364     if( p_var->psz_text ) free( p_var->psz_text );
365
366     memmove( p_this->p_vars + i_var,
367              p_this->p_vars + i_var + 1,
368              (p_this->i_vars - i_var - 1) * sizeof(variable_t) );
369
370     if( (p_this->i_vars & 15) == 0 )
371     {
372         p_this->p_vars = realloc( p_this->p_vars,
373                           (p_this->i_vars) * sizeof( variable_t ) );
374     }
375
376     p_this->i_vars--;
377
378     vlc_mutex_unlock( &p_this->var_lock );
379
380     return VLC_SUCCESS;
381 }
382
383 /**
384  * Perform an action on a variable
385  *
386  * \param p_this The object that holds the variable
387  * \param psz_name The name of the variable
388  * \param i_action The action to perform. Must be one of \ref var_action
389  * \param p_val First action parameter
390  * \param p_val2 Second action parameter
391  */
392 int __var_Change( vlc_object_t *p_this, const char *psz_name,
393                   int i_action, vlc_value_t *p_val, vlc_value_t *p_val2 )
394 {
395     int i_var, i;
396     variable_t *p_var;
397     vlc_value_t oldval;
398
399     vlc_mutex_lock( &p_this->var_lock );
400
401     i_var = Lookup( p_this->p_vars, p_this->i_vars, psz_name );
402
403     if( i_var < 0 )
404     {
405         vlc_mutex_unlock( &p_this->var_lock );
406         return VLC_ENOVAR;
407     }
408
409     p_var = &p_this->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_this->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_this->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_this->var_lock );
668
669                     i_var = Lookup( p_this->p_vars, p_this->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_this->var_lock );
674                         return VLC_ENOVAR;
675                     }
676
677                     p_var = &p_this->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_this->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
703     vlc_mutex_lock( &p_this->var_lock );
704
705     i_var = Lookup( p_this->p_vars, p_this->i_vars, psz_name );
706
707     if( i_var < 0 )
708     {
709         vlc_mutex_unlock( &p_this->var_lock );
710         return 0;
711     }
712
713     i_type = p_this->p_vars[i_var].i_type;
714
715     vlc_mutex_unlock( &p_this->var_lock );
716
717     return i_type;
718 }
719
720 /**
721  * Set a variable's value
722  *
723  * \param p_this The object that hold the variable
724  * \param psz_name The name of the variable
725  * \param val the value to set
726  */
727 int __var_Set( vlc_object_t *p_this, const char *psz_name, vlc_value_t val )
728 {
729     int i_var;
730     variable_t *p_var;
731     vlc_value_t oldval;
732
733     vlc_mutex_lock( &p_this->var_lock );
734
735     i_var = GetUnused( p_this, psz_name );
736     if( i_var < 0 )
737     {
738         vlc_mutex_unlock( &p_this->var_lock );
739         return i_var;
740     }
741
742     p_var = &p_this->p_vars[i_var];
743
744     /* Duplicate data if needed */
745     p_var->pf_dup( &val );
746
747     /* Backup needed stuff */
748     oldval = p_var->val;
749
750     /* Check boundaries and list */
751     CheckValue( p_var, &val );
752
753     /* Set the variable */
754     p_var->val = val;
755
756     /* Deal with callbacks. Tell we're in a callback, release the lock,
757      * call stored functions, retake the lock. */
758     if( p_var->i_entries )
759     {
760         int i_var;
761         int i_entries = p_var->i_entries;
762         callback_entry_t *p_entries = p_var->p_entries;
763
764         p_var->b_incallback = VLC_TRUE;
765         vlc_mutex_unlock( &p_this->var_lock );
766
767         /* The real calls */
768         for( ; i_entries-- ; )
769         {
770             p_entries[i_entries].pf_callback( p_this, psz_name, oldval, val,
771                                               p_entries[i_entries].p_data );
772         }
773
774         vlc_mutex_lock( &p_this->var_lock );
775
776         i_var = Lookup( p_this->p_vars, p_this->i_vars, psz_name );
777         if( i_var < 0 )
778         {
779             msg_Err( p_this, "variable %s has disappeared", psz_name );
780             vlc_mutex_unlock( &p_this->var_lock );
781             return VLC_ENOVAR;
782         }
783
784         p_var = &p_this->p_vars[i_var];
785         p_var->b_incallback = VLC_FALSE;
786     }
787
788     /* Free data if needed */
789     p_var->pf_free( &oldval );
790
791     vlc_mutex_unlock( &p_this->var_lock );
792
793     return VLC_SUCCESS;
794 }
795
796 /**
797  * Get a variable's value
798  *
799  * \param p_this The object that holds the variable
800  * \param psz_name The name of the variable
801  * \param p_val Pointer to a vlc_value_t that will hold the variable's value
802  *              after the function is finished
803  */
804 int __var_Get( vlc_object_t *p_this, const char *psz_name, vlc_value_t *p_val )
805 {
806     int i_var;
807     variable_t *p_var;
808
809     vlc_mutex_lock( &p_this->var_lock );
810
811     i_var = Lookup( p_this->p_vars, p_this->i_vars, psz_name );
812
813     if( i_var < 0 )
814     {
815         vlc_mutex_unlock( &p_this->var_lock );
816         return VLC_ENOVAR;
817     }
818
819     p_var = &p_this->p_vars[i_var];
820
821     /* Really get the variable */
822     *p_val = p_var->val;
823
824     /* Duplicate value if needed */
825     p_var->pf_dup( p_val );
826
827     vlc_mutex_unlock( &p_this->var_lock );
828
829     return VLC_SUCCESS;
830 }
831
832
833 /**
834  * Gets a process-wide mutex, creates it if needed.
835  */
836 vlc_mutex_t *var_GetGlobalMutex( const char *name )
837 {
838     libvlc_global_data_t *p_global = vlc_global();
839     vlc_value_t val;
840
841     if( var_Create( p_global, name, VLC_VAR_MUTEX ) )
842         return NULL;
843
844     var_Get( p_global, name, &val );
845     return val.p_address;
846 }
847
848
849 /**
850  * Register a callback in a variable
851  *
852  * We store a function pointer that will be called upon variable
853  * modification.
854  *
855  * \param p_this The object that holds the variable
856  * \param psz_name The name of the variable
857  * \param pf_callback The function pointer
858  * \param p_data A generic pointer that will be passed as the last
859  *               argument to the callback function.
860  *
861  * \warning The callback function is run in the thread that calls var_Set on
862  *          the variable. Use proper locking. This thread may not have much
863  *          time to spare, so keep callback functions short.
864  */
865 int __var_AddCallback( vlc_object_t *p_this, const char *psz_name,
866                        vlc_callback_t pf_callback, void *p_data )
867 {
868     int i_var;
869     variable_t *p_var;
870     callback_entry_t entry;
871
872     entry.pf_callback = pf_callback;
873     entry.p_data = p_data;
874
875     vlc_mutex_lock( &p_this->var_lock );
876
877     i_var = GetUnused( p_this, psz_name );
878     if( i_var < 0 )
879     {
880         vlc_mutex_unlock( &p_this->var_lock );
881         return i_var;
882     }
883
884     p_var = &p_this->p_vars[i_var];
885
886     INSERT_ELEM( p_var->p_entries,
887                  p_var->i_entries,
888                  p_var->i_entries,
889                  entry );
890
891     vlc_mutex_unlock( &p_this->var_lock );
892
893     return VLC_SUCCESS;
894 }
895
896 /**
897  * Remove a callback from a variable
898  *
899  * pf_callback and p_data have to be given again, because different objects
900  * might have registered the same callback function.
901  */
902 int __var_DelCallback( vlc_object_t *p_this, const char *psz_name,
903                        vlc_callback_t pf_callback, void *p_data )
904 {
905     int i_entry, i_var;
906     variable_t *p_var;
907
908     vlc_mutex_lock( &p_this->var_lock );
909
910     i_var = GetUnused( p_this, psz_name );
911     if( i_var < 0 )
912     {
913         vlc_mutex_unlock( &p_this->var_lock );
914         return i_var;
915     }
916
917     p_var = &p_this->p_vars[i_var];
918
919     for( i_entry = p_var->i_entries ; i_entry-- ; )
920     {
921         if( p_var->p_entries[i_entry].pf_callback == pf_callback
922             && p_var->p_entries[i_entry].p_data == p_data )
923         {
924             break;
925         }
926     }
927
928     if( i_entry < 0 )
929     {
930         vlc_mutex_unlock( &p_this->var_lock );
931         return VLC_EGENERIC;
932     }
933
934     REMOVE_ELEM( p_var->p_entries, p_var->i_entries, i_entry );
935
936     vlc_mutex_unlock( &p_this->var_lock );
937
938     return VLC_SUCCESS;
939 }
940
941 /**
942  * Trigger callback on a variable
943  *
944  * \param p_this The object that hold the variable
945  * \param psz_name The name of the variable
946  */
947 int __var_TriggerCallback( vlc_object_t *p_this, const char *psz_name )
948 {
949     int i_var;
950     variable_t *p_var;
951     vlc_value_t oldval;
952
953     vlc_mutex_lock( &p_this->var_lock );
954
955     i_var = GetUnused( p_this, psz_name );
956     if( i_var < 0 )
957     {
958         vlc_mutex_unlock( &p_this->var_lock );
959         return i_var;
960     }
961
962     p_var = &p_this->p_vars[i_var];
963
964     /* Backup needed stuff */
965     oldval = p_var->val;
966
967     /* Deal with callbacks. Tell we're in a callback, release the lock,
968      * call stored functions, retake the lock. */
969     if( p_var->i_entries )
970     {
971         int i_var;
972         int i_entries = p_var->i_entries;
973         callback_entry_t *p_entries = p_var->p_entries;
974
975         p_var->b_incallback = VLC_TRUE;
976         vlc_mutex_unlock( &p_this->var_lock );
977
978         /* The real calls */
979         for( ; i_entries-- ; )
980         {
981             p_entries[i_entries].pf_callback( p_this, psz_name, oldval, oldval,
982                                               p_entries[i_entries].p_data );
983         }
984
985         vlc_mutex_lock( &p_this->var_lock );
986
987         i_var = Lookup( p_this->p_vars, p_this->i_vars, psz_name );
988         if( i_var < 0 )
989         {
990             msg_Err( p_this, "variable %s has disappeared", psz_name );
991             vlc_mutex_unlock( &p_this->var_lock );
992             return VLC_ENOVAR;
993         }
994
995         p_var = &p_this->p_vars[i_var];
996         p_var->b_incallback = VLC_FALSE;
997     }
998
999     vlc_mutex_unlock( &p_this->var_lock );
1000     return VLC_SUCCESS;
1001 }
1002
1003 /** Parse a stringified option
1004  * This function parse a string option and create the associated object
1005  * variable
1006  * The option must be of the form "[no[-]]foo[=bar]" where foo is the
1007  * option name and bar is the value of the option.
1008  * \param p_obj the object in which the variable must be created
1009  * \param psz_option the option to parse
1010  * \return nothing
1011  */
1012 void __var_OptionParse( vlc_object_t *p_obj, const char *psz_option )
1013 {
1014     char *psz_name, *psz_value = strchr( psz_option, '=' );
1015     int  i_name_len, i_type;
1016     vlc_bool_t b_isno = VLC_FALSE;
1017     vlc_value_t val;
1018
1019     if( psz_value ) i_name_len = psz_value - psz_option;
1020     else i_name_len = strlen( psz_option );
1021
1022     /* It's too much of a hassle to remove the ':' when we parse
1023      * the cmd line :) */
1024     if( i_name_len && *psz_option == ':' )
1025     {
1026         psz_option++;
1027         i_name_len--;
1028     }
1029
1030     if( i_name_len == 0 ) return;
1031
1032     psz_name = strndup( psz_option, i_name_len );
1033     if( psz_value ) psz_value++;
1034
1035     /* FIXME: :programs should be handled generically */
1036     if( !strcmp( psz_name, "programs" ) )
1037         i_type = VLC_VAR_LIST;
1038     else
1039         i_type = config_GetType( p_obj, psz_name );
1040
1041     if( !i_type && !psz_value )
1042     {
1043         /* check for "no-foo" or "nofoo" */
1044         if( !strncmp( psz_name, "no-", 3 ) )
1045         {
1046             memmove( psz_name, psz_name + 3, strlen(psz_name) + 1 - 3 );
1047         }
1048         else if( !strncmp( psz_name, "no", 2 ) )
1049         {
1050             memmove( psz_name, psz_name + 2, strlen(psz_name) + 1 - 2 );
1051         }
1052         else goto cleanup;           /* Option doesn't exist */
1053
1054         b_isno = VLC_TRUE;
1055         i_type = config_GetType( p_obj, psz_name );
1056
1057         if( !i_type ) goto cleanup;  /* Option doesn't exist */
1058     }
1059     else if( !i_type ) goto cleanup; /* Option doesn't exist */
1060
1061     if( ( i_type != VLC_VAR_BOOL ) &&
1062         ( !psz_value || !*psz_value ) ) goto cleanup; /* Invalid value */
1063
1064     /* Create the variable in the input object.
1065      * Children of the input object will be able to retreive this value
1066      * thanks to the inheritance property of the object variables. */
1067     var_Create( p_obj, psz_name, i_type );
1068
1069     switch( i_type )
1070     {
1071     case VLC_VAR_BOOL:
1072         val.b_bool = !b_isno;
1073         break;
1074
1075     case VLC_VAR_INTEGER:
1076         val.i_int = strtol( psz_value, NULL, 0 );
1077         break;
1078
1079     case VLC_VAR_FLOAT:
1080         val.f_float = atof( psz_value );
1081         break;
1082
1083     case VLC_VAR_STRING:
1084     case VLC_VAR_MODULE:
1085     case VLC_VAR_FILE:
1086     case VLC_VAR_DIRECTORY:
1087         val.psz_string = psz_value;
1088         break;
1089
1090     case VLC_VAR_LIST:
1091     {
1092         char *psz_orig, *psz_var;
1093         vlc_list_t *p_list = malloc(sizeof(vlc_list_t));
1094         val.p_list = p_list;
1095         p_list->i_count = 0;
1096
1097         psz_var = psz_orig = strdup(psz_value);
1098         while( psz_var && *psz_var )
1099         {
1100             char *psz_item = psz_var;
1101             vlc_value_t val2;
1102             while( *psz_var && *psz_var != ',' ) psz_var++;
1103             if( *psz_var == ',' )
1104             {
1105                 *psz_var = '\0';
1106                 psz_var++;
1107             }
1108             val2.i_int = strtol( psz_item, NULL, 0 );
1109             INSERT_ELEM( p_list->p_values, p_list->i_count,
1110                          p_list->i_count, val2 );
1111             /* p_list->i_count is incremented twice by INSERT_ELEM */
1112             p_list->i_count--;
1113             INSERT_ELEM( p_list->pi_types, p_list->i_count,
1114                          p_list->i_count, VLC_VAR_INTEGER );
1115         }
1116         if( psz_orig ) free( psz_orig );
1117         break;
1118     }
1119
1120     default:
1121         goto cleanup;
1122         break;
1123     }
1124
1125     var_Set( p_obj, psz_name, val );
1126
1127   cleanup:
1128     if( psz_name ) free( psz_name );
1129     return;
1130 }
1131
1132
1133 /* Following functions are local */
1134
1135 /*****************************************************************************
1136  * GetUnused: find an unused variable from its name
1137  *****************************************************************************
1138  * We do i_tries tries before giving up, just in case the variable is being
1139  * modified and called from a callback.
1140  *****************************************************************************/
1141 static int GetUnused( vlc_object_t *p_this, const char *psz_name )
1142 {
1143     int i_var, i_tries = 0;
1144
1145     while( VLC_TRUE )
1146     {
1147         i_var = Lookup( p_this->p_vars, p_this->i_vars, psz_name );
1148         if( i_var < 0 )
1149         {
1150             return VLC_ENOVAR;
1151         }
1152
1153         if( ! p_this->p_vars[i_var].b_incallback )
1154         {
1155             return i_var;
1156         }
1157
1158         if( i_tries++ > 100 )
1159         {
1160             msg_Err( p_this, "caught in a callback deadlock?" );
1161             return VLC_ETIMEOUT;
1162         }
1163
1164         vlc_mutex_unlock( &p_this->var_lock );
1165         msleep( THREAD_SLEEP );
1166         vlc_mutex_lock( &p_this->var_lock );
1167     }
1168 }
1169
1170 /*****************************************************************************
1171  * HashString: our cool hash function
1172  *****************************************************************************
1173  * This function is not intended to be crypto-secure, we only want it to be
1174  * fast and not suck too much. This one is pretty fast and did 0 collisions
1175  * in wenglish's dictionary.
1176  *****************************************************************************/
1177 static uint32_t HashString( const char *psz_string )
1178 {
1179     uint32_t i_hash = 0;
1180
1181     while( *psz_string )
1182     {
1183         i_hash += *psz_string++;
1184         i_hash += i_hash << 10;
1185         i_hash ^= i_hash >> 8;
1186     }
1187
1188     return i_hash;
1189 }
1190
1191 /*****************************************************************************
1192  * Insert: find an empty slot to insert a new variable
1193  *****************************************************************************
1194  * We use a recursive inner function indexed on the hash. This function does
1195  * nothing in the rare cases where a collision may occur, see Lookup()
1196  * to see how we handle them.
1197  * XXX: does this really need to be written recursively?
1198  *****************************************************************************/
1199 static int Insert( variable_t *p_vars, int i_count, const char *psz_name )
1200 {
1201     if( i_count == 0 )
1202     {
1203         return 0;
1204     }
1205
1206     return InsertInner( p_vars, i_count, HashString( psz_name ) );
1207 }
1208
1209 static int InsertInner( variable_t *p_vars, int i_count, uint32_t i_hash )
1210 {
1211     int i_middle;
1212
1213     if( i_hash <= p_vars[0].i_hash )
1214     {
1215         return 0;
1216     }
1217
1218     if( i_hash >= p_vars[i_count - 1].i_hash )
1219     {
1220         return i_count;
1221     }
1222
1223     i_middle = i_count / 2;
1224
1225     /* We know that 0 < i_middle */
1226     if( i_hash < p_vars[i_middle].i_hash )
1227     {
1228         return InsertInner( p_vars, i_middle, i_hash );
1229     }
1230
1231     /* We know that i_middle + 1 < i_count */
1232     if( i_hash > p_vars[i_middle + 1].i_hash )
1233     {
1234         return i_middle + 1 + InsertInner( p_vars + i_middle + 1,
1235                                            i_count - i_middle - 1,
1236                                            i_hash );
1237     }
1238
1239     return i_middle + 1;
1240 }
1241
1242 /*****************************************************************************
1243  * Lookup: find an existing variable given its name
1244  *****************************************************************************
1245  * We use a recursive inner function indexed on the hash. Care is taken of
1246  * possible hash collisions.
1247  * XXX: does this really need to be written recursively?
1248  *****************************************************************************/
1249 static int Lookup( variable_t *p_vars, int i_count, const char *psz_name )
1250 {
1251     uint32_t i_hash;
1252     int i, i_pos;
1253
1254     if( i_count == 0 )
1255     {
1256         return -1;
1257     }
1258
1259     i_hash = HashString( psz_name );
1260
1261     i_pos = LookupInner( p_vars, i_count, i_hash );
1262
1263     /* Hash not found */
1264     if( i_hash != p_vars[i_pos].i_hash )
1265     {
1266         return -1;
1267     }
1268
1269     /* Hash found, entry found */
1270     if( !strcmp( psz_name, p_vars[i_pos].psz_name ) )
1271     {
1272         return i_pos;
1273     }
1274
1275     /* Hash collision! This should be very rare, but we cannot guarantee
1276      * it will never happen. Just do an exhaustive search amongst all
1277      * entries with the same hash. */
1278     for( i = i_pos - 1 ; i > 0 && i_hash == p_vars[i].i_hash ; i-- )
1279     {
1280         if( !strcmp( psz_name, p_vars[i].psz_name ) )
1281         {
1282             return i;
1283         }
1284     }
1285
1286     for( i = i_pos + 1 ; i < i_count && i_hash == p_vars[i].i_hash ; i++ )
1287     {
1288         if( !strcmp( psz_name, p_vars[i].psz_name ) )
1289         {
1290             return i;
1291         }
1292     }
1293
1294     /* Hash found, but entry not found */
1295     return -1;
1296 }
1297
1298 static int LookupInner( variable_t *p_vars, int i_count, uint32_t i_hash )
1299 {
1300     int i_middle;
1301
1302     if( i_hash <= p_vars[0].i_hash )
1303     {
1304         return 0;
1305     }
1306
1307     if( i_hash >= p_vars[i_count-1].i_hash )
1308     {
1309         return i_count - 1;
1310     }
1311
1312     i_middle = i_count / 2;
1313
1314     /* We know that 0 < i_middle */
1315     if( i_hash < p_vars[i_middle].i_hash )
1316     {
1317         return LookupInner( p_vars, i_middle, i_hash );
1318     }
1319
1320     /* We know that i_middle + 1 < i_count */
1321     if( i_hash > p_vars[i_middle].i_hash )
1322     {
1323         return i_middle + LookupInner( p_vars + i_middle,
1324                                        i_count - i_middle,
1325                                        i_hash );
1326     }
1327
1328     return i_middle;
1329 }
1330
1331 /*****************************************************************************
1332  * CheckValue: check that a value is valid wrt. a variable
1333  *****************************************************************************
1334  * This function checks p_val's value against p_var's limitations such as
1335  * minimal and maximal value, step, in-list position, and modifies p_val if
1336  * necessary.
1337  ****************************************************************************/
1338 static void CheckValue ( variable_t *p_var, vlc_value_t *p_val )
1339 {
1340     /* Check that our variable is in the list */
1341     if( p_var->i_type & VLC_VAR_HASCHOICE && p_var->choices.i_count )
1342     {
1343         int i;
1344
1345         /* FIXME: the list is sorted, dude. Use something cleverer. */
1346         for( i = p_var->choices.i_count ; i-- ; )
1347         {
1348             if( p_var->pf_cmp( *p_val, p_var->choices.p_values[i] ) == 0 )
1349             {
1350                 break;
1351             }
1352         }
1353
1354         /* If not found, change it to anything vaguely valid */
1355         if( i < 0 )
1356         {
1357             /* Free the old variable, get the new one, dup it */
1358             p_var->pf_free( p_val );
1359             *p_val = p_var->choices.p_values[p_var->i_default >= 0
1360                                           ? p_var->i_default : 0 ];
1361             p_var->pf_dup( p_val );
1362         }
1363     }
1364
1365     /* Check that our variable is within the bounds */
1366     switch( p_var->i_type & VLC_VAR_TYPE )
1367     {
1368         case VLC_VAR_INTEGER:
1369             if( p_var->i_type & VLC_VAR_HASSTEP && p_var->step.i_int
1370                  && (p_val->i_int % p_var->step.i_int) )
1371             {
1372                 p_val->i_int = (p_val->i_int + (p_var->step.i_int / 2))
1373                                / p_var->step.i_int * p_var->step.i_int;
1374             }
1375             if( p_var->i_type & VLC_VAR_HASMIN
1376                  && p_val->i_int < p_var->min.i_int )
1377             {
1378                 p_val->i_int = p_var->min.i_int;
1379             }
1380             if( p_var->i_type & VLC_VAR_HASMAX
1381                  && p_val->i_int > p_var->max.i_int )
1382             {
1383                 p_val->i_int = p_var->max.i_int;
1384             }
1385             break;
1386         case VLC_VAR_FLOAT:
1387             if( p_var->i_type & VLC_VAR_HASSTEP && p_var->step.f_float )
1388             {
1389                 float f_round = p_var->step.f_float * (float)(int)( 0.5 +
1390                                         p_val->f_float / p_var->step.f_float );
1391                 if( p_val->f_float != f_round )
1392                 {
1393                     p_val->f_float = f_round;
1394                 }
1395             }
1396             if( p_var->i_type & VLC_VAR_HASMIN
1397                  && p_val->f_float < p_var->min.f_float )
1398             {
1399                 p_val->f_float = p_var->min.f_float;
1400             }
1401             if( p_var->i_type & VLC_VAR_HASMAX
1402                  && p_val->f_float > p_var->max.f_float )
1403             {
1404                 p_val->f_float = p_var->max.f_float;
1405             }
1406             break;
1407         case VLC_VAR_TIME:
1408             /* FIXME: TODO */
1409             break;
1410     }
1411 }
1412
1413 /*****************************************************************************
1414  * InheritValue: try to inherit the value of this variable from the same one
1415  *               in our closest parent.
1416  *****************************************************************************/
1417 static int InheritValue( vlc_object_t *p_this, const char *psz_name,
1418                          vlc_value_t *p_val, int i_type )
1419 {
1420     int i_var;
1421     variable_t *p_var;
1422
1423     /* No need to take the structure lock,
1424      * we are only looking for our parents */
1425
1426     if( !p_this->p_parent )
1427     {
1428         switch( i_type & VLC_VAR_TYPE )
1429         {
1430         case VLC_VAR_FILE:
1431         case VLC_VAR_DIRECTORY:
1432         case VLC_VAR_STRING:
1433         case VLC_VAR_MODULE:
1434             p_val->psz_string = config_GetPsz( p_this, psz_name );
1435             if( !p_val->psz_string ) p_val->psz_string = strdup("");
1436             break;
1437         case VLC_VAR_FLOAT:
1438             p_val->f_float = config_GetFloat( p_this, psz_name );
1439             break;
1440         case VLC_VAR_INTEGER:
1441         case VLC_VAR_HOTKEY:
1442             p_val->i_int = config_GetInt( p_this, psz_name );
1443             break;
1444         case VLC_VAR_BOOL:
1445             p_val->b_bool = config_GetInt( p_this, psz_name );
1446             break;
1447         case VLC_VAR_LIST:
1448         {
1449             char *psz_orig, *psz_var;
1450             vlc_list_t *p_list = malloc(sizeof(vlc_list_t));
1451             p_val->p_list = p_list;
1452             p_list->i_count = 0;
1453
1454             psz_var = psz_orig = config_GetPsz( p_this, psz_name );
1455             while( psz_var && *psz_var )
1456             {
1457                 char *psz_item = psz_var;
1458                 vlc_value_t val;
1459                 while( *psz_var && *psz_var != ',' ) psz_var++;
1460                 if( *psz_var == ',' )
1461                 {
1462                     *psz_var = '\0';
1463                     psz_var++;
1464                 }
1465                 val.i_int = strtol( psz_item, NULL, 0 );
1466                 INSERT_ELEM( p_list->p_values, p_list->i_count,
1467                              p_list->i_count, val );
1468                 /* p_list->i_count is incremented twice by INSERT_ELEM */
1469                 p_list->i_count--;
1470                 INSERT_ELEM( p_list->pi_types, p_list->i_count,
1471                              p_list->i_count, VLC_VAR_INTEGER );
1472             }
1473             if( psz_orig ) free( psz_orig );
1474             break;
1475         }
1476         default:
1477             return VLC_ENOOBJ;
1478             break;
1479         }
1480
1481         return VLC_SUCCESS;
1482     }
1483
1484     /* Look for the variable */
1485     vlc_mutex_lock( &p_this->p_parent->var_lock );
1486
1487     i_var = Lookup( p_this->p_parent->p_vars, p_this->p_parent->i_vars,
1488                     psz_name );
1489
1490     if( i_var >= 0 )
1491     {
1492         /* We found it! */
1493         p_var = &p_this->p_parent->p_vars[i_var];
1494
1495         /* Really get the variable */
1496         *p_val = p_var->val;
1497
1498         /* Duplicate value if needed */
1499         p_var->pf_dup( p_val );
1500
1501         vlc_mutex_unlock( &p_this->p_parent->var_lock );
1502         return VLC_SUCCESS;
1503     }
1504
1505     vlc_mutex_unlock( &p_this->p_parent->var_lock );
1506
1507     /* We're still not there */
1508
1509     return InheritValue( p_this->p_parent, psz_name, p_val, i_type );
1510 }
1511
1512 /**********************************************************************
1513  * Execute a var command on an object identified by its name
1514  **********************************************************************/
1515 int __var_Command( vlc_object_t *p_this, const char *psz_name,
1516                    const char *psz_cmd, const char *psz_arg, char **psz_msg )
1517 {
1518     vlc_object_t *p_obj = vlc_object_find_name( p_this->p_libvlc,
1519                                                 psz_name, FIND_CHILD );
1520     int i_type, i_ret;
1521
1522     if( !p_obj )
1523     {
1524         if( psz_msg )
1525             *psz_msg = strdup( "Unknown destination object." );
1526         return VLC_ENOOBJ;
1527     }
1528
1529     i_type = var_Type( p_obj, psz_cmd );
1530     if( !( i_type&VLC_VAR_ISCOMMAND ) )
1531     {
1532         vlc_object_release( p_obj );
1533         if( psz_msg )
1534             *psz_msg = strdup( "Variable doesn't exist or isn't a command." );
1535         return VLC_EGENERIC;
1536     }
1537
1538     i_type &= 0xf0;
1539     switch( i_type )
1540     {
1541         case VLC_VAR_INTEGER:
1542             i_ret = var_SetInteger( p_obj, psz_cmd, atoi( psz_arg ) );
1543             break;
1544         case VLC_VAR_FLOAT:
1545             i_ret = var_SetFloat( p_obj, psz_cmd, atof( psz_arg ) );
1546             break;
1547         case VLC_VAR_STRING:
1548             i_ret = var_SetString( p_obj, psz_cmd, psz_arg );
1549             break;
1550         case VLC_VAR_BOOL:
1551             i_ret = var_SetBool( p_obj, psz_cmd, atoi( psz_arg ) );
1552             break;
1553         default:
1554             i_ret = VLC_EGENERIC;
1555             break;
1556     }
1557
1558     vlc_object_release( p_obj );
1559
1560     if( psz_msg )
1561     {
1562         *psz_msg = (char*)malloc( 80 );
1563         sprintf( *psz_msg, "%s on object %s returned %i (%s)",
1564                  psz_cmd, psz_name, i_ret, vlc_error( i_ret ) );
1565     }
1566
1567     return i_ret;
1568 }