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