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