]> git.sesse.net Git - vlc/blob - src/misc/variables.c
Fix parsing of float-typed variables
[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 = realloc( 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         p_priv->p_vars = realloc( p_priv->p_vars,
393                           (p_priv->i_vars) * sizeof( variable_t ) );
394     }
395
396     p_priv->i_vars--;
397
398     vlc_mutex_unlock( &p_priv->var_lock );
399
400     return VLC_SUCCESS;
401 }
402
403 /**
404  * Perform an action on a variable
405  *
406  * \param p_this The object that holds the variable
407  * \param psz_name The name of the variable
408  * \param i_action The action to perform. Must be one of \ref var_action
409  * \param p_val First action parameter
410  * \param p_val2 Second action parameter
411  */
412 int __var_Change( vlc_object_t *p_this, const char *psz_name,
413                   int i_action, vlc_value_t *p_val, vlc_value_t *p_val2 )
414 {
415     int i_var, i;
416     variable_t *p_var;
417     vlc_value_t oldval;
418
419     assert( p_this );
420
421     vlc_object_internals_t *p_priv = vlc_internals( p_this );
422
423     vlc_mutex_lock( &p_priv->var_lock );
424
425     i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
426
427     if( i_var < 0 )
428     {
429         vlc_mutex_unlock( &p_priv->var_lock );
430         return VLC_ENOVAR;
431     }
432
433     p_var = &p_priv->p_vars[i_var];
434
435     switch( i_action )
436     {
437         case VLC_VAR_SETMIN:
438             if( p_var->i_type & VLC_VAR_HASMIN )
439             {
440                 p_var->ops->pf_free( &p_var->min );
441             }
442             p_var->i_type |= VLC_VAR_HASMIN;
443             p_var->min = *p_val;
444             p_var->ops->pf_dup( &p_var->min );
445             CheckValue( p_var, &p_var->val );
446             break;
447         case VLC_VAR_GETMIN:
448             if( p_var->i_type & VLC_VAR_HASMIN )
449             {
450                 *p_val = p_var->min;
451             }
452             break;
453         case VLC_VAR_SETMAX:
454             if( p_var->i_type & VLC_VAR_HASMAX )
455             {
456                 p_var->ops->pf_free( &p_var->max );
457             }
458             p_var->i_type |= VLC_VAR_HASMAX;
459             p_var->max = *p_val;
460             p_var->ops->pf_dup( &p_var->max );
461             CheckValue( p_var, &p_var->val );
462             break;
463         case VLC_VAR_GETMAX:
464             if( p_var->i_type & VLC_VAR_HASMAX )
465             {
466                 *p_val = p_var->max;
467             }
468             break;
469         case VLC_VAR_SETSTEP:
470             if( p_var->i_type & VLC_VAR_HASSTEP )
471             {
472                 p_var->ops->pf_free( &p_var->step );
473             }
474             p_var->i_type |= VLC_VAR_HASSTEP;
475             p_var->step = *p_val;
476             p_var->ops->pf_dup( &p_var->step );
477             CheckValue( p_var, &p_var->val );
478             break;
479         case VLC_VAR_GETSTEP:
480             if( p_var->i_type & VLC_VAR_HASSTEP )
481             {
482                 *p_val = p_var->step;
483             }
484             break;
485         case VLC_VAR_ADDCHOICE:
486             i = p_var->choices.i_count;
487
488             INSERT_ELEM( p_var->choices.p_values, p_var->choices.i_count,
489                          i, *p_val );
490             INSERT_ELEM( p_var->choices_text.p_values,
491                          p_var->choices_text.i_count, i, *p_val );
492             p_var->ops->pf_dup( &p_var->choices.p_values[i] );
493             p_var->choices_text.p_values[i].psz_string =
494                 ( p_val2 && p_val2->psz_string ) ?
495                 strdup( p_val2->psz_string ) : NULL;
496
497             CheckValue( p_var, &p_var->val );
498             break;
499         case VLC_VAR_DELCHOICE:
500             for( i = 0 ; i < p_var->choices.i_count ; i++ )
501             {
502                 if( p_var->ops->pf_cmp( p_var->choices.p_values[i], *p_val ) == 0 )
503                 {
504                     break;
505                 }
506             }
507
508             if( i == p_var->choices.i_count )
509             {
510                 /* Not found */
511                 vlc_mutex_unlock( &p_priv->var_lock );
512                 return VLC_EGENERIC;
513             }
514
515             if( p_var->i_default > i )
516             {
517                 p_var->i_default--;
518             }
519             else if( p_var->i_default == i )
520             {
521                 p_var->i_default = -1;
522             }
523
524             p_var->ops->pf_free( &p_var->choices.p_values[i] );
525             free( p_var->choices_text.p_values[i].psz_string );
526             REMOVE_ELEM( p_var->choices.p_values, p_var->choices.i_count, i );
527             REMOVE_ELEM( p_var->choices_text.p_values,
528                          p_var->choices_text.i_count, i );
529
530             CheckValue( p_var, &p_var->val );
531             break;
532         case VLC_VAR_CHOICESCOUNT:
533             p_val->i_int = p_var->choices.i_count;
534             break;
535         case VLC_VAR_CLEARCHOICES:
536             for( i = 0 ; i < p_var->choices.i_count ; i++ )
537             {
538                 p_var->ops->pf_free( &p_var->choices.p_values[i] );
539             }
540             for( i = 0 ; i < p_var->choices_text.i_count ; i++ )
541                 free( p_var->choices_text.p_values[i].psz_string );
542
543             if( p_var->choices.i_count ) free( p_var->choices.p_values );
544             if( p_var->choices_text.i_count ) free( p_var->choices_text.p_values );
545
546             p_var->choices.i_count = 0;
547             p_var->choices.p_values = NULL;
548             p_var->choices_text.i_count = 0;
549             p_var->choices_text.p_values = NULL;
550             p_var->i_default = -1;
551             break;
552         case VLC_VAR_SETDEFAULT:
553             /* FIXME: the list is sorted, dude. Use something cleverer. */
554             for( i = 0 ; i < p_var->choices.i_count ; i++ )
555             {
556                 if( p_var->ops->pf_cmp( p_var->choices.p_values[i], *p_val ) == 0 )
557                 {
558                     break;
559                 }
560             }
561
562             if( i == p_var->choices.i_count )
563             {
564                 /* Not found */
565                 break;
566             }
567
568             p_var->i_default = i;
569             CheckValue( p_var, &p_var->val );
570             break;
571         case VLC_VAR_SETVALUE:
572             /* Duplicate data if needed */
573             p_var->ops->pf_dup( p_val );
574             /* Backup needed stuff */
575             oldval = p_var->val;
576             /* Check boundaries and list */
577             CheckValue( p_var, p_val );
578             /* Set the variable */
579             p_var->val = *p_val;
580             /* Free data if needed */
581             p_var->ops->pf_free( &oldval );
582             break;
583         case VLC_VAR_GETCHOICES:
584         case VLC_VAR_GETLIST:
585             p_val->p_list = malloc( sizeof(vlc_list_t) );
586             if( p_val2 ) p_val2->p_list = malloc( sizeof(vlc_list_t) );
587             if( p_var->choices.i_count )
588             {
589                 p_val->p_list->p_values = malloc( p_var->choices.i_count
590                                                   * sizeof(vlc_value_t) );
591                 p_val->p_list->pi_types = malloc( p_var->choices.i_count
592                                                   * sizeof(int) );
593                 if( p_val2 )
594                 {
595                     p_val2->p_list->p_values =
596                         malloc( p_var->choices.i_count * sizeof(vlc_value_t) );
597                     p_val2->p_list->pi_types =
598                         malloc( p_var->choices.i_count * sizeof(int) );
599                 }
600             }
601             p_val->p_list->i_count = p_var->choices.i_count;
602             if( p_val2 ) p_val2->p_list->i_count = p_var->choices.i_count;
603             for( i = 0 ; i < p_var->choices.i_count ; i++ )
604             {
605                 p_val->p_list->p_values[i] = p_var->choices.p_values[i];
606                 p_val->p_list->pi_types[i] = p_var->i_type;
607                 p_var->ops->pf_dup( &p_val->p_list->p_values[i] );
608                 if( p_val2 )
609                 {
610                     p_val2->p_list->p_values[i].psz_string =
611                         p_var->choices_text.p_values[i].psz_string ?
612                     strdup(p_var->choices_text.p_values[i].psz_string) : NULL;
613                     p_val2->p_list->pi_types[i] = VLC_VAR_STRING;
614                 }
615             }
616             break;
617         case VLC_VAR_SETTEXT:
618             free( p_var->psz_text );
619             if( p_val && p_val->psz_string )
620                 p_var->psz_text = strdup( p_val->psz_string );
621             else
622                 p_var->psz_text = NULL;
623             break;
624         case VLC_VAR_GETTEXT:
625             p_val->psz_string = p_var->psz_text ? strdup( p_var->psz_text )
626                                                 : NULL;
627             break;
628         case VLC_VAR_INHERITVALUE:
629             {
630                 vlc_value_t val;
631
632                 if( InheritValue( p_this,
633                                   p_val2 ? p_val2->psz_string :  psz_name,
634                                   &val, p_var->i_type )
635                     == VLC_SUCCESS )
636                 {
637                     /* Duplicate already done */
638
639                     /* Backup needed stuff */
640                     oldval = p_var->val;
641                     /* Check boundaries and list */
642                     CheckValue( p_var, &val );
643                     /* Set the variable */
644                     p_var->val = val;
645                     /* Free data if needed */
646                     p_var->ops->pf_free( &oldval );
647                 }
648
649                 if( p_val )
650                 {
651                     *p_val = p_var->val;
652                     p_var->ops->pf_dup( p_val );
653                 }
654             }
655             break;
656
657         case VLC_VAR_SETISCOMMAND:
658             p_var->i_type |= VLC_VAR_ISCOMMAND;
659             break;
660
661         default:
662             break;
663     }
664
665     vlc_mutex_unlock( &p_priv->var_lock );
666
667     return VLC_SUCCESS;
668 }
669
670
671 /**
672  * Perform a Get and Set on a variable
673  *
674  * \param p_this: The object that hold the variable
675  * \param psz_name: the name of the variable
676  * \param i_action: the action to perform
677  * \param p_val: The action parameter
678  * \return vlc error codes
679  */
680 int __var_GetAndSet( vlc_object_t *p_this, const char *psz_name, int i_action,
681                      vlc_value_t val )
682 {
683     int i_var;
684     int i_ret = VLC_SUCCESS;
685     variable_t *p_var;
686     vlc_value_t oldval;
687
688     assert( p_this );
689
690     vlc_object_internals_t *p_priv = vlc_internals( p_this );
691
692     vlc_mutex_lock( &p_priv->var_lock );
693     i_var = GetUnused( p_this, psz_name );
694     if( i_var < 0 )
695     {
696         vlc_mutex_unlock( &p_priv->var_lock );
697         return i_var;
698     }
699
700     p_var = &p_priv->p_vars[i_var];
701
702     /* Duplicated data if needed */
703     //p_var->ops->pf_dup( &val );
704
705     /* Backup needed stuff */
706     oldval = p_var->val;
707
708     /* depending of the action requiered */
709     switch( i_action )
710     {
711     case VLC_VAR_TOGGLE_BOOL:
712         assert( ( p_var->i_type & VLC_VAR_BOOL ) == VLC_VAR_BOOL );
713         p_var->val.b_bool = !p_var->val.b_bool;
714         break;
715     case VLC_VAR_INTEGER_INCDEC:
716         assert( ( p_var->i_type & VLC_VAR_INTEGER ) == VLC_VAR_INTEGER );
717         p_var->val.i_int += val.i_int;
718         break;
719     default:
720         vlc_mutex_unlock( &p_priv->var_lock );
721         return VLC_EGENERIC;
722     }
723
724     /*  Check boundaries */
725     CheckValue( p_var, &p_var->val );
726
727     /* Deal with callbacks.*/
728     if( p_var->i_entries )
729         i_ret = TriggerCallback( p_this, &p_var, psz_name, oldval );
730
731     vlc_mutex_unlock( &p_priv->var_lock );
732
733     return i_ret;
734 }
735
736
737 /**
738  * Request a variable's type
739  *
740  * \return The variable type if it exists, or 0 if the
741  * variable could not be found.
742  * \see \ref var_type
743  */
744 int __var_Type( vlc_object_t *p_this, const char *psz_name )
745 {
746     int i_var, i_type;
747
748     assert( p_this );
749
750     vlc_object_internals_t *p_priv = vlc_internals( p_this );
751
752     vlc_mutex_lock( &p_priv->var_lock );
753
754     i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
755
756     if( i_var < 0 )
757     {
758         vlc_mutex_unlock( &p_priv->var_lock );
759         return 0;
760     }
761
762     i_type = p_priv->p_vars[i_var].i_type;
763
764     vlc_mutex_unlock( &p_priv->var_lock );
765
766     return i_type;
767 }
768
769 int var_SetChecked( vlc_object_t *p_this, const char *psz_name,
770                     int expected_type, vlc_value_t val )
771 {
772     int i_var;
773     int i_ret = VLC_SUCCESS;
774     variable_t *p_var;
775     vlc_value_t oldval;
776
777     assert( p_this );
778
779     vlc_object_internals_t *p_priv = vlc_internals( p_this );
780
781     vlc_mutex_lock( &p_priv->var_lock );
782
783     i_var = GetUnused( p_this, psz_name );
784     if( i_var < 0 )
785     {
786         vlc_mutex_unlock( &p_priv->var_lock );
787         return i_var;
788     }
789
790     p_var = &p_priv->p_vars[i_var];
791     assert( expected_type == 0 ||
792             (p_var->i_type & VLC_VAR_CLASS) == expected_type );
793
794     /* Duplicate data if needed */
795     p_var->ops->pf_dup( &val );
796
797     /* Backup needed stuff */
798     oldval = p_var->val;
799
800     /* Check boundaries and list */
801     CheckValue( p_var, &val );
802
803     /* Set the variable */
804     p_var->val = val;
805
806     /* Deal with callbacks */
807     if( p_var->i_entries )
808         i_ret = TriggerCallback( p_this, &p_var, psz_name, oldval );
809
810     /* Free data if needed */
811     p_var->ops->pf_free( &oldval );
812
813     vlc_mutex_unlock( &p_priv->var_lock );
814
815     return i_ret;
816 }
817
818
819 /**
820  * Set a variable's value
821  *
822  * \param p_this The object that hold the variable
823  * \param psz_name The name of the variable
824  * \param val the value to set
825  */
826 int __var_Set( vlc_object_t *p_this, const char *psz_name, vlc_value_t val )
827 {
828     return var_SetChecked( p_this, psz_name, 0, val );
829 }
830
831 int var_GetChecked( vlc_object_t *p_this, const char *psz_name,
832                     int expected_type, vlc_value_t *p_val )
833 {
834     assert( p_this );
835
836     vlc_object_internals_t *p_priv = vlc_internals( p_this );
837     int i_var, err = VLC_SUCCESS;
838
839     vlc_mutex_lock( &p_priv->var_lock );
840
841     i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
842     if( i_var >= 0 )
843     {
844         variable_t *p_var = &p_priv->p_vars[i_var];
845
846         assert( expected_type == 0 ||
847                 (p_var->i_type & VLC_VAR_CLASS) == expected_type );
848
849         /* Really get the variable */
850         *p_val = p_var->val;
851
852 #ifndef NDEBUG
853         /* Alert if the type is VLC_VAR_VOID */
854         if( ( p_var->i_type & VLC_VAR_TYPE ) == VLC_VAR_VOID )
855             msg_Warn( p_this, "Calling var_GetVoid on the void variable '%s' (0x%04x)", psz_name, p_var->i_type );
856 #endif
857
858         /* Duplicate value if needed */
859         p_var->ops->pf_dup( p_val );
860     }
861     else
862         err = VLC_ENOVAR;
863
864     vlc_mutex_unlock( &p_priv->var_lock );
865     return err;
866 }
867
868 /**
869  * Get a variable's value
870  *
871  * \param p_this The object that holds the variable
872  * \param psz_name The name of the variable
873  * \param p_val Pointer to a vlc_value_t that will hold the variable's value
874  *              after the function is finished
875  */
876 int __var_Get( vlc_object_t *p_this, const char *psz_name, vlc_value_t *p_val )
877 {
878     return var_GetChecked( p_this, psz_name, 0, p_val );
879 }
880
881 /**
882  * Register a callback in a variable
883  *
884  * We store a function pointer that will be called upon variable
885  * modification.
886  *
887  * \param p_this The object that holds the variable
888  * \param psz_name The name of the variable
889  * \param pf_callback The function pointer
890  * \param p_data A generic pointer that will be passed as the last
891  *               argument to the callback function.
892  *
893  * \warning The callback function is run in the thread that calls var_Set on
894  *          the variable. Use proper locking. This thread may not have much
895  *          time to spare, so keep callback functions short.
896  */
897 int __var_AddCallback( vlc_object_t *p_this, const char *psz_name,
898                        vlc_callback_t pf_callback, void *p_data )
899 {
900     int i_var;
901     variable_t *p_var;
902     callback_entry_t entry;
903
904     assert( p_this );
905
906     vlc_object_internals_t *p_priv = vlc_internals( p_this );
907
908     entry.pf_callback = pf_callback;
909     entry.p_data = p_data;
910
911     vlc_mutex_lock( &p_priv->var_lock );
912
913     i_var = GetUnused( p_this, psz_name );
914     if( i_var < 0 )
915     {
916 #ifndef NDEBUG
917         msg_Warn( p_this, "Failed to add a callback to the non-existing "
918                           "variable '%s'", psz_name );
919 #endif
920         vlc_mutex_unlock( &p_priv->var_lock );
921         return i_var;
922     }
923
924     p_var = &p_priv->p_vars[i_var];
925
926     INSERT_ELEM( p_var->p_entries,
927                  p_var->i_entries,
928                  p_var->i_entries,
929                  entry );
930
931     vlc_mutex_unlock( &p_priv->var_lock );
932
933     return VLC_SUCCESS;
934 }
935
936 /**
937  * Remove a callback from a variable
938  *
939  * pf_callback and p_data have to be given again, because different objects
940  * might have registered the same callback function.
941  */
942 int __var_DelCallback( vlc_object_t *p_this, const char *psz_name,
943                        vlc_callback_t pf_callback, void *p_data )
944 {
945     int i_entry, i_var;
946     variable_t *p_var;
947 #ifndef NDEBUG
948     bool b_found_similar = false;
949 #endif
950
951     assert( p_this );
952
953     vlc_object_internals_t *p_priv = vlc_internals( p_this );
954
955     vlc_mutex_lock( &p_priv->var_lock );
956
957     i_var = GetUnused( p_this, psz_name );
958     if( i_var < 0 )
959     {
960         vlc_mutex_unlock( &p_priv->var_lock );
961         return i_var;
962     }
963
964     p_var = &p_priv->p_vars[i_var];
965
966     for( i_entry = p_var->i_entries ; i_entry-- ; )
967     {
968         if( p_var->p_entries[i_entry].pf_callback == pf_callback
969             && p_var->p_entries[i_entry].p_data == p_data )
970         {
971             break;
972         }
973 #ifndef NDEBUG
974         else if( p_var->p_entries[i_entry].pf_callback == pf_callback )
975             b_found_similar = true;
976 #endif
977     }
978
979     if( i_entry < 0 )
980     {
981 #ifndef NDEBUG
982         if( b_found_similar )
983             fprintf( stderr, "Calling var_DelCallback for '%s' with the same "
984                              "function but not the same data.", psz_name );
985         assert( 0 );
986 #endif
987         vlc_mutex_unlock( &p_priv->var_lock );
988         return VLC_EGENERIC;
989     }
990
991     REMOVE_ELEM( p_var->p_entries, p_var->i_entries, i_entry );
992
993     vlc_mutex_unlock( &p_priv->var_lock );
994
995     return VLC_SUCCESS;
996 }
997
998 /**
999  * Trigger callback on a variable
1000  *
1001  * \param p_this The object that hold the variable
1002  * \param psz_name The name of the variable
1003  */
1004 int __var_TriggerCallback( vlc_object_t *p_this, const char *psz_name )
1005 {
1006     int i_var;
1007     int i_ret = VLC_SUCCESS;
1008     variable_t *p_var;
1009
1010     assert( p_this );
1011
1012     vlc_object_internals_t *p_priv = vlc_internals( p_this );
1013
1014     vlc_mutex_lock( &p_priv->var_lock );
1015
1016     i_var = GetUnused( p_this, psz_name );
1017     if( i_var < 0 )
1018     {
1019         vlc_mutex_unlock( &p_priv->var_lock );
1020         return i_var;
1021     }
1022
1023     p_var = &p_priv->p_vars[i_var];
1024
1025     /* Deal with callbacks. Tell we're in a callback, release the lock,
1026      * call stored functions, retake the lock. */
1027     if( p_var->i_entries )
1028         i_ret = TriggerCallback( p_this, &p_var, psz_name, p_var->val );
1029
1030     vlc_mutex_unlock( &p_priv->var_lock );
1031     return i_ret;
1032 }
1033
1034 /** Parse a stringified option
1035  * This function parse a string option and create the associated object
1036  * variable
1037  * The option must be of the form "[no[-]]foo[=bar]" where foo is the
1038  * option name and bar is the value of the option.
1039  * \param p_obj the object in which the variable must be created
1040  * \param psz_option the option to parse
1041  * \param trusted whether the option is set by a trusted input or not
1042  * \return nothing
1043  */
1044 void var_OptionParse( vlc_object_t *p_obj, const char *psz_option,
1045                       bool trusted )
1046 {
1047     char *psz_name, *psz_value;
1048     int  i_type;
1049     bool b_isno = false;
1050     vlc_value_t val;
1051
1052     val.psz_string = NULL;
1053
1054     /* It's too much of a hassle to remove the ':' when we parse
1055      * the cmd line :) */
1056     if( psz_option[0] == ':' )
1057         psz_option++;
1058
1059     if( !psz_option[0] )
1060         return;
1061
1062     psz_name = strdup( psz_option );
1063     if( psz_name == NULL )
1064         return;
1065
1066     psz_value = strchr( psz_name, '=' );
1067     if( psz_value != NULL )
1068         *psz_value++ = '\0';
1069
1070     /* FIXME: :programs should be handled generically */
1071     if( !strcmp( psz_name, "programs" ) )
1072         i_type = VLC_VAR_LIST;
1073     else
1074         i_type = config_GetType( p_obj, psz_name );
1075
1076     if( !i_type && !psz_value )
1077     {
1078         /* check for "no-foo" or "nofoo" */
1079         if( !strncmp( psz_name, "no-", 3 ) )
1080         {
1081             memmove( psz_name, psz_name + 3, strlen(psz_name) + 1 - 3 );
1082         }
1083         else if( !strncmp( psz_name, "no", 2 ) )
1084         {
1085             memmove( psz_name, psz_name + 2, strlen(psz_name) + 1 - 2 );
1086         }
1087         else goto cleanup;           /* Option doesn't exist */
1088
1089         b_isno = true;
1090         i_type = config_GetType( p_obj, psz_name );
1091     }
1092     if( !i_type ) goto cleanup; /* Option doesn't exist */
1093
1094     if( ( i_type != VLC_VAR_BOOL ) &&
1095         ( !psz_value || !*psz_value ) ) goto cleanup; /* Invalid value */
1096
1097     /* check if option is unsafe */
1098     if( !trusted )
1099     {
1100         module_config_t *p_config = config_FindConfig( p_obj, psz_name );
1101         if( !p_config || !p_config->b_safe )
1102         {
1103             msg_Err( p_obj, "unsafe option \"%s\" has been ignored for "
1104                             "security reasons", psz_name );
1105             free( psz_name );
1106             return;
1107         }
1108     }
1109
1110     /* Create the variable in the input object.
1111      * Children of the input object will be able to retreive this value
1112      * thanks to the inheritance property of the object variables. */
1113     __var_Create( p_obj, psz_name, i_type );
1114
1115     switch( i_type )
1116     {
1117     case VLC_VAR_BOOL:
1118         val.b_bool = !b_isno;
1119         break;
1120
1121     case VLC_VAR_INTEGER:
1122         val.i_int = strtol( psz_value, NULL, 0 );
1123         break;
1124
1125     case VLC_VAR_FLOAT:
1126         val.f_float = us_atof( psz_value );
1127         break;
1128
1129     case VLC_VAR_STRING:
1130     case VLC_VAR_MODULE:
1131     case VLC_VAR_FILE:
1132     case VLC_VAR_DIRECTORY:
1133         val.psz_string = psz_value;
1134         break;
1135
1136     case VLC_VAR_LIST:
1137     {
1138         char *psz_orig, *psz_var;
1139         vlc_list_t *p_list = malloc(sizeof(vlc_list_t));
1140         val.p_list = p_list;
1141         p_list->i_count = 0;
1142
1143         psz_var = psz_orig = strdup(psz_value);
1144         while( psz_var && *psz_var )
1145         {
1146             char *psz_item = psz_var;
1147             vlc_value_t val2;
1148             while( *psz_var && *psz_var != ',' ) psz_var++;
1149             if( *psz_var == ',' )
1150             {
1151                 *psz_var = '\0';
1152                 psz_var++;
1153             }
1154             val2.i_int = strtol( psz_item, NULL, 0 );
1155             INSERT_ELEM( p_list->p_values, p_list->i_count,
1156                          p_list->i_count, val2 );
1157             /* p_list->i_count is incremented twice by INSERT_ELEM */
1158             p_list->i_count--;
1159             INSERT_ELEM( p_list->pi_types, p_list->i_count,
1160                          p_list->i_count, VLC_VAR_INTEGER );
1161         }
1162         free( psz_orig );
1163         break;
1164     }
1165
1166     default:
1167         goto cleanup;
1168     }
1169
1170     __var_Set( p_obj, psz_name, val );
1171
1172     /* If that's a list, remove all elements allocated */
1173     if( i_type == VLC_VAR_LIST )
1174         FreeList( &val );
1175
1176 cleanup:
1177     free( psz_name );
1178 }
1179
1180
1181 /* Following functions are local */
1182
1183 /*****************************************************************************
1184  * GetUnused: find an unused (not in callback) variable from its name
1185  *****************************************************************************
1186  * We do i_tries tries before giving up, just in case the variable is being
1187  * modified and called from a callback.
1188  *****************************************************************************/
1189 static int GetUnused( vlc_object_t *p_this, const char *psz_name )
1190 {
1191     assert( p_this );
1192
1193     vlc_object_internals_t *p_priv = vlc_internals( p_this );
1194
1195     while( true )
1196     {
1197         int i_var;
1198
1199         i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
1200         if( i_var < 0 )
1201         {
1202             return VLC_ENOVAR;
1203         }
1204
1205         if( ! p_priv->p_vars[i_var].b_incallback )
1206         {
1207             return i_var;
1208         }
1209
1210         mutex_cleanup_push( &p_priv->var_lock );
1211         vlc_cond_wait( &p_priv->var_wait, &p_priv->var_lock );
1212         vlc_cleanup_pop( );
1213     }
1214 }
1215
1216 /*****************************************************************************
1217  * HashString: our cool hash function
1218  *****************************************************************************
1219  * This function is not intended to be crypto-secure, we only want it to be
1220  * fast and not suck too much. This one is pretty fast and did 0 collisions
1221  * in wenglish's dictionary.
1222  *****************************************************************************/
1223 static uint32_t HashString( const char *psz_string )
1224 {
1225     uint32_t i_hash = 0;
1226
1227     while( *psz_string )
1228     {
1229         i_hash += *psz_string++;
1230         i_hash += i_hash << 10;
1231         i_hash ^= i_hash >> 8;
1232     }
1233
1234     return i_hash;
1235 }
1236
1237 /*****************************************************************************
1238  * Insert: find an empty slot to insert a new variable
1239  *****************************************************************************
1240  * We use a recursive inner function indexed on the hash. This function does
1241  * nothing in the rare cases where a collision may occur, see Lookup()
1242  * to see how we handle them.
1243  * XXX: does this really need to be written recursively?
1244  *****************************************************************************/
1245 static int Insert( variable_t *p_vars, int i_count, const char *psz_name )
1246 {
1247     if( i_count == 0 )
1248     {
1249         return 0;
1250     }
1251
1252     return InsertInner( p_vars, i_count, HashString( psz_name ) );
1253 }
1254
1255 static int InsertInner( variable_t *p_vars, int i_count, uint32_t i_hash )
1256 {
1257     int i_middle;
1258
1259     if( i_hash <= p_vars[0].i_hash )
1260     {
1261         return 0;
1262     }
1263
1264     if( i_hash >= p_vars[i_count - 1].i_hash )
1265     {
1266         return i_count;
1267     }
1268
1269     i_middle = i_count / 2;
1270
1271     /* We know that 0 < i_middle */
1272     if( i_hash < p_vars[i_middle].i_hash )
1273     {
1274         return InsertInner( p_vars, i_middle, i_hash );
1275     }
1276
1277     /* We know that i_middle + 1 < i_count */
1278     if( i_hash > p_vars[i_middle + 1].i_hash )
1279     {
1280         return i_middle + 1 + InsertInner( p_vars + i_middle + 1,
1281                                            i_count - i_middle - 1,
1282                                            i_hash );
1283     }
1284
1285     return i_middle + 1;
1286 }
1287
1288 static int u32cmp( const void *key, const void *data )
1289 {
1290     const variable_t *p_var = data;
1291     uint32_t hash = *(const uint32_t *)key ;
1292
1293     if( hash > p_var->i_hash )
1294         return 1;
1295     if( hash < p_var->i_hash )
1296         return -1;
1297     return 0;
1298 }
1299
1300 /*****************************************************************************
1301  * Lookup: find an existing variable given its name
1302  *****************************************************************************
1303  * We use a recursive inner function indexed on the hash. Care is taken of
1304  * possible hash collisions.
1305  *****************************************************************************/
1306 static int Lookup( variable_t *p_vars, size_t i_count, const char *psz_name )
1307 {
1308     variable_t *p_var;
1309     uint32_t i_hash;
1310
1311     i_hash = HashString( psz_name );
1312     p_var = bsearch( &i_hash, p_vars, i_count, sizeof( *p_var ), u32cmp );
1313
1314     /* Hash not found */
1315     if( p_var == NULL )
1316         return -1;
1317
1318     assert( i_count > 0 );
1319
1320     /* Find the first entry with the right hash */
1321     while( (p_var > p_vars) && (i_hash == p_var[-1].i_hash) )
1322         p_var--;
1323
1324     assert( p_var->i_hash == i_hash );
1325
1326     /* Hash collision should be very unlikely, but we cannot guarantee
1327      * it will never happen. So we do an exhaustive search amongst all
1328      * entries with the same hash. Typically, there is only one anyway. */
1329     for( variable_t *p_end = p_vars + i_count;
1330          (p_var < p_end) && (i_hash == p_var->i_hash);
1331          p_var++ )
1332     {
1333         if( !strcmp( psz_name, p_var->psz_name ) )
1334             return p_var - p_vars;
1335     }
1336
1337     /* Hash found, but entry not found */
1338     return -1;
1339 }
1340
1341 /*****************************************************************************
1342  * CheckValue: check that a value is valid wrt. a variable
1343  *****************************************************************************
1344  * This function checks p_val's value against p_var's limitations such as
1345  * minimal and maximal value, step, in-list position, and modifies p_val if
1346  * necessary.
1347  ****************************************************************************/
1348 static void CheckValue ( variable_t *p_var, vlc_value_t *p_val )
1349 {
1350     /* Check that our variable is in the list */
1351     if( p_var->i_type & VLC_VAR_HASCHOICE && p_var->choices.i_count )
1352     {
1353         int i;
1354
1355         /* This list is not sorted so go throug it (this is a small list) */
1356         for( i = p_var->choices.i_count ; i-- ; )
1357         {
1358             if( p_var->ops->pf_cmp( *p_val, p_var->choices.p_values[i] ) == 0 )
1359             {
1360                 break;
1361             }
1362         }
1363
1364         /* If not found, change it to anything vaguely valid */
1365         if( i < 0 )
1366         {
1367             /* Free the old variable, get the new one, dup it */
1368             p_var->ops->pf_free( p_val );
1369             *p_val = p_var->choices.p_values[p_var->i_default >= 0
1370                                           ? p_var->i_default : 0 ];
1371             p_var->ops->pf_dup( p_val );
1372         }
1373     }
1374
1375     /* Check that our variable is within the bounds */
1376     switch( p_var->i_type & VLC_VAR_TYPE )
1377     {
1378         case VLC_VAR_INTEGER:
1379             if( p_var->i_type & VLC_VAR_HASSTEP && p_var->step.i_int
1380                  && (p_val->i_int % p_var->step.i_int) )
1381             {
1382                 p_val->i_int = (p_val->i_int + (p_var->step.i_int / 2))
1383                                / p_var->step.i_int * p_var->step.i_int;
1384             }
1385             if( p_var->i_type & VLC_VAR_HASMIN
1386                  && p_val->i_int < p_var->min.i_int )
1387             {
1388                 p_val->i_int = p_var->min.i_int;
1389             }
1390             if( p_var->i_type & VLC_VAR_HASMAX
1391                  && p_val->i_int > p_var->max.i_int )
1392             {
1393                 p_val->i_int = p_var->max.i_int;
1394             }
1395             break;
1396         case VLC_VAR_FLOAT:
1397             if( p_var->i_type & VLC_VAR_HASSTEP && p_var->step.f_float )
1398             {
1399                 float f_round = p_var->step.f_float * (float)(int)( 0.5 +
1400                                         p_val->f_float / p_var->step.f_float );
1401                 if( p_val->f_float != f_round )
1402                 {
1403                     p_val->f_float = f_round;
1404                 }
1405             }
1406             if( p_var->i_type & VLC_VAR_HASMIN
1407                  && p_val->f_float < p_var->min.f_float )
1408             {
1409                 p_val->f_float = p_var->min.f_float;
1410             }
1411             if( p_var->i_type & VLC_VAR_HASMAX
1412                  && p_val->f_float > p_var->max.f_float )
1413             {
1414                 p_val->f_float = p_var->max.f_float;
1415             }
1416             break;
1417         case VLC_VAR_TIME:
1418             /* FIXME: TODO */
1419             break;
1420     }
1421 }
1422
1423 /*****************************************************************************
1424  * InheritValue: try to inherit the value of this variable from the same one
1425  * in our closest parent, libvlc or ultimately from the configuration.
1426  * The function should always be entered with the object var_lock locked.
1427  *****************************************************************************/
1428 static int InheritValue( vlc_object_t *p_this, const char *psz_name,
1429                          vlc_value_t *p_val, int i_type )
1430 {
1431     int i_var;
1432     variable_t *p_var;
1433
1434     if( p_this->p_parent || ( p_this->p_libvlc && p_this != (vlc_object_t*) p_this->p_libvlc ) )
1435     {
1436         vlc_object_internals_t *p_priv;
1437
1438         if( p_this->p_parent )
1439             p_priv = vlc_internals( p_this->p_parent );
1440         else
1441             p_priv = vlc_internals( p_this->p_libvlc );
1442
1443         i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
1444
1445         if( i_var >= 0 )
1446         {
1447             /* We found it! */
1448             p_var = &p_priv->p_vars[i_var];
1449
1450             /* Really get the variable */
1451             *p_val = p_var->val;
1452
1453             /* Duplicate value if needed */
1454             p_var->ops->pf_dup( p_val );
1455
1456             /*msg_Dbg( p_this, "Inherited value for var %s from object %s",
1457                      psz_name ? psz_name : "(null)",
1458                      p_this->psz_object_name
1459                          ? p_this->psz_object_name : "(Unknown)" );*/
1460             return VLC_SUCCESS;
1461         }
1462         else if ( p_this->p_parent ) /* We are still not there */
1463             return InheritValue( p_this->p_parent, psz_name, p_val, i_type );
1464
1465         /* else take value from config */
1466     }
1467
1468
1469     switch( i_type & VLC_VAR_CLASS )
1470     {
1471         case VLC_VAR_STRING:
1472             p_val->psz_string = config_GetPsz( p_this, psz_name );
1473             if( !p_val->psz_string ) p_val->psz_string = strdup("");
1474             break;
1475         case VLC_VAR_FLOAT:
1476             p_val->f_float = config_GetFloat( p_this, psz_name );
1477             break;
1478         case VLC_VAR_INTEGER:
1479             p_val->i_int = config_GetInt( p_this, psz_name );
1480             break;
1481         case VLC_VAR_BOOL:
1482             p_val->b_bool = config_GetInt( p_this, psz_name );
1483             break;
1484         case VLC_VAR_LIST:
1485         {
1486             char *psz_orig, *psz_var;
1487             vlc_list_t *p_list = malloc(sizeof(vlc_list_t));
1488             p_val->p_list = p_list;
1489             p_list->i_count = 0;
1490
1491             psz_var = psz_orig = config_GetPsz( p_this, psz_name );
1492             while( psz_var && *psz_var )
1493             {
1494                 char *psz_item = psz_var;
1495                 vlc_value_t val;
1496                 while( *psz_var && *psz_var != ',' ) psz_var++;
1497                 if( *psz_var == ',' )
1498                 {
1499                     *psz_var = '\0';
1500                     psz_var++;
1501                 }
1502                 val.i_int = strtol( psz_item, NULL, 0 );
1503                 INSERT_ELEM( p_list->p_values, p_list->i_count,
1504                              p_list->i_count, val );
1505                 /* p_list->i_count is incremented twice by INSERT_ELEM */
1506                 p_list->i_count--;
1507                 INSERT_ELEM( p_list->pi_types, p_list->i_count,
1508                              p_list->i_count, VLC_VAR_INTEGER );
1509             }
1510             free( psz_orig );
1511             break;
1512         }
1513         default:
1514             msg_Warn( p_this, "Could not inherit value for var %s "
1515                               "from config. Invalid Type", psz_name );
1516             return VLC_ENOOBJ;
1517             break;
1518     }
1519     /*msg_Dbg( p_this, "Inherited value for var %s from config", psz_name );*/
1520     return VLC_SUCCESS;
1521 }
1522
1523
1524 /**********************************************************************
1525  * Trigger the callbacks.
1526  * Tell we're in a callback, release the lock, call stored functions,
1527  * retake the lock.
1528  **********************************************************************/
1529 static int TriggerCallback( vlc_object_t *p_this, variable_t **pp_var,
1530                             const char *psz_name, vlc_value_t oldval )
1531 {
1532     int i_var;
1533     int i_entries = (*pp_var)->i_entries;
1534     callback_entry_t *p_entries = (*pp_var)->p_entries;
1535
1536     assert( p_this );
1537
1538     vlc_object_internals_t *p_priv = vlc_internals( p_this );
1539
1540     (*pp_var)->b_incallback = true;
1541     vlc_mutex_unlock( &p_priv->var_lock );
1542
1543     /* The real calls */
1544     for( ; i_entries-- ; )
1545     {
1546         p_entries[i_entries].pf_callback( p_this, psz_name, oldval, (*pp_var)->val,
1547                                           p_entries[i_entries].p_data );
1548     }
1549
1550     vlc_mutex_lock( &p_priv->var_lock );
1551
1552     i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
1553     if( i_var < 0 )
1554     {
1555         msg_Err( p_this, "variable %s has disappeared", psz_name );
1556         return VLC_ENOVAR;
1557      }
1558
1559      *pp_var = &p_priv->p_vars[i_var];
1560      (*pp_var)->b_incallback = false;
1561      vlc_cond_broadcast( &p_priv->var_wait );
1562
1563      return VLC_SUCCESS;
1564 }
1565
1566
1567 /**********************************************************************
1568  * Execute a var command on an object identified by its name
1569  **********************************************************************/
1570 int __var_Command( vlc_object_t *p_this, const char *psz_name,
1571                    const char *psz_cmd, const char *psz_arg, char **psz_msg )
1572 {
1573     vlc_object_t *p_obj = vlc_object_find_name( p_this->p_libvlc,
1574                                                 psz_name, FIND_CHILD );
1575     int i_type, i_ret;
1576
1577     if( !p_obj )
1578     {
1579         if( psz_msg )
1580             *psz_msg = strdup( "Unknown destination object." );
1581         return VLC_ENOOBJ;
1582     }
1583
1584     i_type = var_Type( p_obj, psz_cmd );
1585     if( !( i_type&VLC_VAR_ISCOMMAND ) )
1586     {
1587         vlc_object_release( p_obj );
1588         if( psz_msg )
1589             *psz_msg = strdup( "Variable doesn't exist or isn't a command." );
1590         return VLC_EGENERIC;
1591     }
1592
1593     i_type &= VLC_VAR_CLASS;
1594     switch( i_type )
1595     {
1596         case VLC_VAR_INTEGER:
1597             i_ret = var_SetInteger( p_obj, psz_cmd, atoi( psz_arg ) );
1598             break;
1599         case VLC_VAR_FLOAT:
1600             i_ret = var_SetFloat( p_obj, psz_cmd, us_atof( psz_arg ) );
1601             break;
1602         case VLC_VAR_STRING:
1603             i_ret = var_SetString( p_obj, psz_cmd, psz_arg );
1604             break;
1605         case VLC_VAR_BOOL:
1606             i_ret = var_SetBool( p_obj, psz_cmd, atoi( psz_arg ) );
1607             break;
1608         default:
1609             i_ret = VLC_EGENERIC;
1610             break;
1611     }
1612
1613     vlc_object_release( p_obj );
1614
1615     if( psz_msg )
1616     {
1617         if( asprintf( psz_msg, "%s on object %s returned %i (%s)",
1618                   psz_cmd, psz_name, i_ret, vlc_error( i_ret ) ) == -1)
1619             *psz_msg = NULL;
1620     }
1621
1622     return i_ret;
1623 }
1624
1625
1626 /**
1627  * Free a list and the associated strings
1628  * @param p_val: the list variable
1629  * @param p_val2: the variable associated or NULL
1630  */
1631 void var_FreeList( vlc_value_t *p_val, vlc_value_t *p_val2 )
1632 {
1633     FreeList( p_val );
1634     if( p_val2 && p_val2->p_list )
1635     {
1636         for( int i = 0; i < p_val2->p_list->i_count; i++ )
1637             free( p_val2->p_list->p_values[i].psz_string );
1638         if( p_val2->p_list->i_count )
1639         {
1640             free( p_val2->p_list->p_values );
1641             free( p_val2->p_list->pi_types );
1642         }
1643         free( p_val2->p_list );
1644     }
1645 }