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