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