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