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