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