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