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