]> git.sesse.net Git - vlc/blob - src/misc/variables.c
* modules/gui/wxwindows/*: don't forget to delete the timer.
[vlc] / src / misc / variables.c
1 /*****************************************************************************
2  * variables.c: routines for object variables handling
3  *****************************************************************************
4  * Copyright (C) 2002 VideoLAN
5  * $Id: variables.c,v 1.31 2003/09/29 15:45:19 sigmunau Exp $
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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <vlc/vlc.h>
28
29 #ifdef HAVE_STDLIB_H
30 #   include <stdlib.h>                                          /* realloc() */
31 #endif
32
33 /*****************************************************************************
34  * Private types
35  *****************************************************************************/
36 struct callback_entry_t
37 {
38     vlc_callback_t pf_callback;
39     void *         p_data;
40 };
41
42 /*****************************************************************************
43  * Local comparison functions, returns 0 if v == w, < 0 if v < w, > 0 if v > w
44  *****************************************************************************/
45 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; }
46 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; }
47 static int CmpTime( vlc_value_t v, vlc_value_t w )
48 {
49     return v.i_time == w.i_time ? 0 : v.i_time > w.i_time ? 1 : -1;
50 }
51 static int CmpString( vlc_value_t v, vlc_value_t w ) { return strcmp( v.psz_string, w.psz_string ); }
52 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; }
53 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; }
54
55 /*****************************************************************************
56  * Local duplication functions, and local deallocation functions
57  *****************************************************************************/
58 static void DupDummy( vlc_value_t *p_val ) { (void)p_val; /* unused */ }
59 static void DupString( vlc_value_t *p_val ) { p_val->psz_string = strdup( p_val->psz_string ); }
60
61 static void DupList( vlc_value_t *p_val )
62 {
63     int i;
64     vlc_list_t *p_list = malloc( sizeof(vlc_list_t) );
65
66     if( p_val->p_list->i_count )
67     {
68         p_list->i_count = p_val->p_list->i_count;
69         p_list->p_values = malloc( p_list->i_count * sizeof(vlc_value_t) );
70         p_list->pi_types = malloc( p_list->i_count * sizeof(int) );
71     }
72
73     for( i = 0; i < p_list->i_count; i++ )
74     {
75         p_list->p_values[i] = p_val->p_list->p_values[i];
76         switch( p_val->p_list->pi_types[i] & VLC_VAR_TYPE )
77         {
78         case VLC_VAR_STRING:
79             
80             DupString( &p_list->p_values[i] );
81             break;
82         default:
83             break;
84         }
85     }
86
87     p_val->p_list = p_list;
88 }
89
90 static void FreeDummy( vlc_value_t *p_val ) { (void)p_val; /* unused */ }
91 static void FreeString( vlc_value_t *p_val ) { free( p_val->psz_string ); }
92 static void FreeMutex( vlc_value_t *p_val ) { vlc_mutex_destroy( (vlc_mutex_t*)p_val->p_address ); free( p_val->p_address ); }
93
94 static void FreeList( vlc_value_t *p_val )
95 {
96     int i;
97     for( i = 0; i < p_val->p_list->i_count; i++ )
98     {
99         switch( p_val->p_list->pi_types[i] & VLC_VAR_TYPE )
100         {
101         case VLC_VAR_STRING:
102             FreeString( &p_val->p_list->p_values[i] );
103             break;
104         case VLC_VAR_MUTEX:
105             FreeMutex( &p_val->p_list->p_values[i] );
106             break;
107         default:
108             break;
109         }
110     }
111
112     if( p_val->p_list->i_count )
113     {
114         free( p_val->p_list->p_values );
115         free( p_val->p_list->pi_types );
116     }
117     free( p_val->p_list );
118 }
119
120 /*****************************************************************************
121  * Local prototypes
122  *****************************************************************************/
123 static int      GetUnused   ( vlc_object_t *, const char * );
124 static uint32_t HashString  ( const char * );
125 static int      Insert      ( variable_t *, int, const char * );
126 static int      InsertInner ( variable_t *, int, uint32_t );
127 static int      Lookup      ( variable_t *, int, const char * );
128 static int      LookupInner ( variable_t *, int, uint32_t );
129
130 static void     CheckValue  ( variable_t *, vlc_value_t * );
131
132 static int      InheritValue( vlc_object_t *, const char *, vlc_value_t *,
133                               int );
134
135 /**
136  * Initialize a vlc variable
137  *
138  * We hash the given string and insert it into the sorted list. The insertion
139  * may require slow memory copies, but think about what we gain in the log(n)
140  * lookup phase when setting/getting the variable value!
141  *
142  * \param p_this The object in which to create the variable
143  * \param psz_name The name of the variable
144  * \param i_type The variables type. Must be one of \ref var_type combined with
145  *               zero or more \ref var_flags
146  */
147 int __var_Create( vlc_object_t *p_this, const char *psz_name, int i_type )
148 {
149     int i_new;
150     variable_t *p_var;
151     static vlc_list_t dummy_null_list = {0, NULL, NULL};
152
153     vlc_mutex_lock( &p_this->var_lock );
154
155     /* FIXME: if the variable already exists, we don't duplicate it. But we
156      * duplicate the lookups. It's not that serious, but if anyone finds some
157      * time to rework Insert() so that only one lookup has to be done, feel
158      * free to do so. */
159     i_new = Lookup( p_this->p_vars, p_this->i_vars, psz_name );
160
161     if( i_new >= 0 )
162     {
163         /* If the types differ, variable creation failed. */
164         if( (i_type & ~VLC_VAR_DOINHERIT) != p_this->p_vars[i_new].i_type )
165         {
166             vlc_mutex_unlock( &p_this->var_lock );
167             return VLC_EBADVAR;
168         }
169
170         p_this->p_vars[i_new].i_usage++;
171         vlc_mutex_unlock( &p_this->var_lock );
172         return VLC_SUCCESS;
173     }
174
175     i_new = Insert( p_this->p_vars, p_this->i_vars, psz_name );
176
177     if( (p_this->i_vars & 15) == 15 )
178     {
179         p_this->p_vars = realloc( p_this->p_vars,
180                                   (p_this->i_vars+17) * sizeof(variable_t) );
181     }
182
183     memmove( p_this->p_vars + i_new + 1,
184              p_this->p_vars + i_new,
185              (p_this->i_vars - i_new) * sizeof(variable_t) );
186
187     p_this->i_vars++;
188
189     p_var = &p_this->p_vars[i_new];
190
191     p_var->i_hash = HashString( psz_name );
192     p_var->psz_name = strdup( psz_name );
193     p_var->psz_text = NULL;
194
195     p_var->i_type = i_type & ~VLC_VAR_DOINHERIT;
196     memset( &p_var->val, 0, sizeof(vlc_value_t) );
197
198     p_var->pf_dup = DupDummy;
199     p_var->pf_free = FreeDummy;
200
201     p_var->i_usage = 1;
202
203     p_var->i_default = -1;
204     p_var->choices.i_count = 0;
205     p_var->choices.p_values = NULL;
206     p_var->choices_text.i_count = 0;
207     p_var->choices_text.p_values = NULL;
208
209     p_var->b_incallback = VLC_FALSE;
210     p_var->i_entries = 0;
211     p_var->p_entries = NULL;
212
213     /* Always initialize the variable, even if it is a list variable; this
214      * will lead to errors if the variable is not initialized, but it will
215      * not cause crashes in the variable handling. */
216     switch( i_type & VLC_VAR_TYPE )
217     {
218         case VLC_VAR_BOOL:
219             p_var->pf_cmp = CmpBool;
220             p_var->val.b_bool = VLC_FALSE;
221             break;
222         case VLC_VAR_INTEGER:
223             p_var->pf_cmp = CmpInt;
224             p_var->val.i_int = 0;
225             break;
226         case VLC_VAR_STRING:
227         case VLC_VAR_MODULE:
228         case VLC_VAR_FILE:
229         case VLC_VAR_DIRECTORY:
230         case VLC_VAR_VARIABLE:
231             p_var->pf_cmp = CmpString;
232             p_var->pf_dup = DupString;
233             p_var->pf_free = FreeString;
234             p_var->val.psz_string = "";
235             break;
236         case VLC_VAR_FLOAT:
237             p_var->pf_cmp = CmpFloat;
238             p_var->val.f_float = 0.0;
239             break;
240         case VLC_VAR_TIME:
241             p_var->pf_cmp = CmpTime;
242             p_var->val.i_time = 0;
243             break;
244         case VLC_VAR_ADDRESS:
245             p_var->pf_cmp = CmpAddress;
246             p_var->val.p_address = NULL;
247             break;
248         case VLC_VAR_MUTEX:
249             p_var->pf_cmp = CmpAddress;
250             p_var->pf_free = FreeMutex;
251             p_var->val.p_address = malloc( sizeof(vlc_mutex_t) );
252             vlc_mutex_init( p_this, (vlc_mutex_t*)p_var->val.p_address );
253             break;
254         case VLC_VAR_LIST:
255             p_var->pf_cmp = CmpAddress;
256             p_var->pf_dup = DupList;
257             p_var->pf_free = FreeList;
258             p_var->val.p_list = &dummy_null_list;
259             break;
260     }
261
262     /* Duplicate the default data we stored. */
263     p_var->pf_dup( &p_var->val );
264
265     if( i_type & VLC_VAR_DOINHERIT )
266     {
267         vlc_value_t val;
268
269         if( InheritValue( p_this, psz_name, &val, p_var->i_type )
270             == VLC_SUCCESS );
271         {
272             /* Free data if needed */
273             p_var->pf_free( &p_var->val );
274             /* Check boundaries and list */
275             CheckValue( p_var, &val );
276             /* Set the variable */
277             p_var->val = val;
278         }
279     }
280
281     vlc_mutex_unlock( &p_this->var_lock );
282
283     return VLC_SUCCESS;
284 }
285
286 /**
287  * Destroy a vlc variable
288  *
289  * Look for the variable and destroy it if it is found. As in var_Create we
290  * do a call to memmove() but we have performance counterparts elsewhere.
291  *
292  * \param p_this The object that holds the variable
293  * \param psz_name The name of the variable
294  */
295 int __var_Destroy( vlc_object_t *p_this, const char *psz_name )
296 {
297     int i_var, i;
298     variable_t *p_var;
299
300     vlc_mutex_lock( &p_this->var_lock );
301
302     i_var = GetUnused( p_this, psz_name );
303     if( i_var < 0 )
304     {
305         vlc_mutex_unlock( &p_this->var_lock );
306         return i_var;
307     }
308
309     p_var = &p_this->p_vars[i_var];
310
311     if( p_var->i_usage > 1 )
312     {
313         p_var->i_usage--;
314         vlc_mutex_unlock( &p_this->var_lock );
315         return VLC_SUCCESS;
316     }
317
318     /* Free value if needed */
319     p_var->pf_free( &p_var->val );
320
321     /* Free choice list if needed */
322     if( p_var->choices.i_count )
323     {
324         for( i = 0 ; i < p_var->choices.i_count ; i++ )
325         {
326             p_var->pf_free( &p_var->choices.p_values[i] );
327             if( p_var->choices_text.p_values[i].psz_string )
328                 free( p_var->choices_text.p_values[i].psz_string );
329         }
330         free( p_var->choices.p_values );
331         free( p_var->choices_text.p_values );
332     }
333
334     /* Free callbacks if needed */
335     if( p_var->p_entries )
336     {
337         free( p_var->p_entries );
338     }
339
340     free( p_var->psz_name );
341     if( p_var->psz_text ) free( p_var->psz_text );
342
343     memmove( p_this->p_vars + i_var,
344              p_this->p_vars + i_var + 1,
345              (p_this->i_vars - i_var - 1) * sizeof(variable_t) );
346
347     if( (p_this->i_vars & 15) == 0 )
348     {
349         p_this->p_vars = realloc( p_this->p_vars,
350                           (p_this->i_vars) * sizeof( variable_t ) );
351     }
352
353     p_this->i_vars--;
354
355     vlc_mutex_unlock( &p_this->var_lock );
356
357     return VLC_SUCCESS;
358 }
359
360 /**
361  * Perform an action on a variable
362  *
363  * \param p_this The object that holds the variable
364  * \param psz_name The name of the variable
365  * \param i_action The action to perform. Must be one of \ref var_action
366  * \param p_val First action parameter
367  * \param p_val2 Second action parameter
368  */
369 int __var_Change( vlc_object_t *p_this, const char *psz_name,
370                   int i_action, vlc_value_t *p_val, vlc_value_t *p_val2 )
371 {
372     int i_var, i;
373     variable_t *p_var;
374     vlc_value_t oldval;
375
376     vlc_mutex_lock( &p_this->var_lock );
377
378     i_var = Lookup( p_this->p_vars, p_this->i_vars, psz_name );
379
380     if( i_var < 0 )
381     {
382         vlc_mutex_unlock( &p_this->var_lock );
383         return VLC_ENOVAR;
384     }
385
386     p_var = &p_this->p_vars[i_var];
387
388     switch( i_action )
389     {
390         case VLC_VAR_SETMIN:
391             if( p_var->i_type & VLC_VAR_HASMIN )
392             {
393                 p_var->pf_free( &p_var->min );
394             }
395             p_var->i_type |= VLC_VAR_HASMIN;
396             p_var->min = *p_val;
397             p_var->pf_dup( &p_var->min );
398             CheckValue( p_var, &p_var->val );
399             break;
400         case VLC_VAR_SETMAX:
401             if( p_var->i_type & VLC_VAR_HASMAX )
402             {
403                 p_var->pf_free( &p_var->max );
404             }
405             p_var->i_type |= VLC_VAR_HASMAX;
406             p_var->max = *p_val;
407             p_var->pf_dup( &p_var->max );
408             CheckValue( p_var, &p_var->val );
409             break;
410         case VLC_VAR_SETSTEP:
411             if( p_var->i_type & VLC_VAR_HASSTEP )
412             {
413                 p_var->pf_free( &p_var->step );
414             }
415             p_var->i_type |= VLC_VAR_HASSTEP;
416             p_var->step = *p_val;
417             p_var->pf_dup( &p_var->step );
418             CheckValue( p_var, &p_var->val );
419             break;
420         case VLC_VAR_ADDCHOICE:
421             /* FIXME: the list is sorted, dude. Use something cleverer. */
422             for( i = p_var->choices.i_count ; i-- ; )
423             {
424                 if( p_var->pf_cmp( p_var->choices.p_values[i], *p_val ) < 0 )
425                 {
426                     break;
427                 }
428             }
429
430             /* The new place is i+1 */
431             i++;
432
433             if( p_var->i_default >= i )
434             {
435                 p_var->i_default++;
436             }
437
438             INSERT_ELEM( p_var->choices.p_values, p_var->choices.i_count,
439                          i, *p_val );
440             INSERT_ELEM( p_var->choices_text.p_values,
441                          p_var->choices_text.i_count, i, *p_val );
442             p_var->pf_dup( &p_var->choices.p_values[i] );
443             p_var->choices_text.p_values[i].psz_string =
444                 ( p_val2 && p_val2->psz_string ) ?
445                 strdup( p_val2->psz_string ) : NULL;
446
447             CheckValue( p_var, &p_var->val );
448             break;
449         case VLC_VAR_DELCHOICE:
450             /* FIXME: the list is sorted, dude. Use something cleverer. */
451             for( i = 0 ; i < p_var->choices.i_count ; i++ )
452             {
453                 if( p_var->pf_cmp( p_var->choices.p_values[i], *p_val ) == 0 )
454                 {
455                     break;
456                 }
457             }
458
459             if( i == p_var->choices.i_count )
460             {
461                 /* Not found */
462                 vlc_mutex_unlock( &p_this->var_lock );
463                 return VLC_EGENERIC;
464             }
465
466             if( p_var->i_default > i )
467             {
468                 p_var->i_default--;
469             }
470             else if( p_var->i_default == i )
471             {
472                 p_var->i_default = -1;
473             }
474
475             p_var->pf_free( &p_var->choices.p_values[i] );
476             if( p_var->choices_text.p_values[i].psz_string )
477                 free( p_var->choices_text.p_values[i].psz_string );
478             REMOVE_ELEM( p_var->choices.p_values, p_var->choices.i_count, i );
479             REMOVE_ELEM( p_var->choices_text.p_values,
480                          p_var->choices_text.i_count, i );
481
482             CheckValue( p_var, &p_var->val );
483             break;
484         case VLC_VAR_CHOICESCOUNT:
485             p_val->i_int = p_var->choices.i_count;
486             break;
487         case VLC_VAR_CLEARCHOICES:
488             for( i = 0 ; i < p_var->choices.i_count ; i++ )
489             {
490                 p_var->pf_free( &p_var->choices.p_values[i] );
491             }
492             if( p_var->choices.i_count )
493                 free( p_var->choices.p_values );
494
495             p_var->choices.i_count = 0;
496             p_var->choices.p_values = NULL;
497             p_var->i_default = -1;
498             break;
499         case VLC_VAR_SETDEFAULT:
500             /* FIXME: the list is sorted, dude. Use something cleverer. */
501             for( i = 0 ; i < p_var->choices.i_count ; i++ )
502             {
503                 if( p_var->pf_cmp( p_var->choices.p_values[i], *p_val ) == 0 )
504                 {
505                     break;
506                 }
507             }
508
509             if( i == p_var->choices.i_count )
510             {
511                 /* Not found */
512                 break;
513             }
514
515             p_var->i_default = i;
516             CheckValue( p_var, &p_var->val );
517             break;
518         case VLC_VAR_SETVALUE:
519             /* Duplicate data if needed */
520             p_var->pf_dup( p_val );
521             /* Backup needed stuff */
522             oldval = p_var->val;
523             /* Check boundaries and list */
524             CheckValue( p_var, p_val );
525             /* Set the variable */
526             p_var->val = *p_val;
527             /* Free data if needed */
528             p_var->pf_free( &oldval );
529             break;
530         case VLC_VAR_GETCHOICES:
531         case VLC_VAR_GETLIST:
532             p_val->p_list = malloc( sizeof(vlc_list_t) );
533             if( p_val2 ) p_val2->p_list = malloc( sizeof(vlc_list_t) );
534             if( p_var->choices.i_count )
535             {
536                 p_val->p_list->p_values = malloc( p_var->choices.i_count
537                                                   * sizeof(vlc_value_t) );
538                 p_val->p_list->pi_types = malloc( p_var->choices.i_count
539                                                   * sizeof(int) );
540                 if( p_val2 )
541                 {
542                     p_val2->p_list->p_values =
543                         malloc( p_var->choices.i_count * sizeof(vlc_value_t) );
544                     p_val2->p_list->pi_types =
545                         malloc( p_var->choices.i_count * sizeof(int) );
546                 }
547             }
548             p_val->p_list->i_count = p_var->choices.i_count;
549             if( p_val2 ) p_val2->p_list->i_count = p_var->choices.i_count;
550             for( i = 0 ; i < p_var->choices.i_count ; i++ )
551             {
552                 p_val->p_list->p_values[i] = p_var->choices.p_values[i];
553                 p_val->p_list->pi_types[i] = p_var->i_type;
554                 p_var->pf_dup( &p_val->p_list->p_values[i] );
555                 if( p_val2 )
556                 {
557                     p_val2->p_list->p_values[i].psz_string =
558                         p_var->choices_text.p_values[i].psz_string ?
559                     strdup(p_var->choices_text.p_values[i].psz_string) : NULL;
560                     p_val2->p_list->pi_types[i] = VLC_VAR_STRING;
561                 }
562             }
563             break;
564         case VLC_VAR_FREELIST:
565             FreeList( p_val );
566             break;
567         case VLC_VAR_SETTEXT:
568             if( p_var->psz_text ) free( p_var->psz_text );
569             if( p_val && p_val->psz_string )
570                 p_var->psz_text = strdup( p_val->psz_string );
571             break;
572         case VLC_VAR_GETTEXT:
573             p_val->psz_string = NULL;
574             if( p_var->psz_text )
575             {
576                 p_val->psz_string = strdup( p_var->psz_text );
577             }
578             break;
579         case VLC_VAR_INHERITVALUE:
580             {
581                 vlc_value_t val;
582
583                 if( InheritValue( p_this, psz_name, &val, p_var->i_type )
584                     == VLC_SUCCESS );
585                 {
586                     /* Duplicate already done */
587
588                     /* Backup needed stuff */
589                     oldval = p_var->val;
590                     /* Check boundaries and list */
591                     CheckValue( p_var, &val );
592                     /* Set the variable */
593                     p_var->val = val;
594                     /* Free data if needed */
595                     p_var->pf_free( &oldval );
596                 }
597
598                 if( p_val )
599                 {
600                     *p_val = p_var->val;
601                     p_var->pf_dup( p_val );
602                 }
603             }
604             break;
605
606         default:
607             break;
608     }
609
610     vlc_mutex_unlock( &p_this->var_lock );
611
612     return VLC_SUCCESS;
613 }
614
615 /**
616  * Request a variable's type
617  *
618  * \return The variable type if it exists, or 0 if the
619  * variable could not be found.
620  * \see \ref var_type
621  */
622 int __var_Type( vlc_object_t *p_this, const char *psz_name )
623 {
624     int i_var, i_type;
625
626     vlc_mutex_lock( &p_this->var_lock );
627
628     i_var = Lookup( p_this->p_vars, p_this->i_vars, psz_name );
629
630     if( i_var < 0 )
631     {
632         vlc_mutex_unlock( &p_this->var_lock );
633         return 0;
634     }
635
636     i_type = p_this->p_vars[i_var].i_type;
637
638     vlc_mutex_unlock( &p_this->var_lock );
639
640     return i_type;
641 }
642
643 /**
644  * Set a variable's value
645  *
646  * \param p_this The object that hold the variable
647  * \param psz_name The name of the variable
648  * \param val the value to set
649  */
650 int __var_Set( vlc_object_t *p_this, const char *psz_name, vlc_value_t val )
651 {
652     int i_var;
653     variable_t *p_var;
654     vlc_value_t oldval;
655
656     vlc_mutex_lock( &p_this->var_lock );
657
658     i_var = GetUnused( p_this, psz_name );
659     if( i_var < 0 )
660     {
661         vlc_mutex_unlock( &p_this->var_lock );
662         return i_var;
663     }
664
665     p_var = &p_this->p_vars[i_var];
666
667     /* Duplicate data if needed */
668     p_var->pf_dup( &val );
669
670     /* Backup needed stuff */
671     oldval = p_var->val;
672
673     /* Check boundaries and list */
674     CheckValue( p_var, &val );
675
676     /* Set the variable */
677     p_var->val = val;
678
679     /* Deal with callbacks. Tell we're in a callback, release the lock,
680      * call stored functions, retake the lock. */
681     if( p_var->i_entries )
682     {
683         int i_var;
684         int i_entries = p_var->i_entries;
685         callback_entry_t *p_entries = p_var->p_entries;
686
687         p_var->b_incallback = VLC_TRUE;
688         vlc_mutex_unlock( &p_this->var_lock );
689
690         /* The real calls */
691         for( ; i_entries-- ; )
692         {
693             p_entries[i_entries].pf_callback( p_this, psz_name, oldval, val,
694                                               p_entries[i_entries].p_data );
695         }
696
697         vlc_mutex_lock( &p_this->var_lock );
698
699         i_var = Lookup( p_this->p_vars, p_this->i_vars, psz_name );
700         if( i_var < 0 )
701         {
702             msg_Err( p_this, "variable %s has disappeared", psz_name );
703             vlc_mutex_unlock( &p_this->var_lock );
704             return VLC_ENOVAR;
705         }
706
707         p_var = &p_this->p_vars[i_var];
708         p_var->b_incallback = VLC_FALSE;
709     }
710
711     /* Free data if needed */
712     p_var->pf_free( &oldval );
713
714     vlc_mutex_unlock( &p_this->var_lock );
715
716     return VLC_SUCCESS;
717 }
718
719 /**
720  * Get a variable's value
721  *
722  * \param p_this The object that holds the variable
723  * \param psz_name The name of the variable
724  * \param p_val Pointer to a vlc_value_t that will hold the variable's value
725  *              after the function is finished
726  */
727 int __var_Get( vlc_object_t *p_this, const char *psz_name, vlc_value_t *p_val )
728 {
729     int i_var;
730     variable_t *p_var;
731
732     vlc_mutex_lock( &p_this->var_lock );
733
734     i_var = Lookup( p_this->p_vars, p_this->i_vars, psz_name );
735
736     if( i_var < 0 )
737     {
738         vlc_mutex_unlock( &p_this->var_lock );
739         return VLC_ENOVAR;
740     }
741
742     p_var = &p_this->p_vars[i_var];
743
744     /* Really get the variable */
745     *p_val = p_var->val;
746
747     /* Duplicate value if needed */
748     p_var->pf_dup( p_val );
749
750     vlc_mutex_unlock( &p_this->var_lock );
751
752     return VLC_SUCCESS;
753 }
754
755 /**
756  * Register a callback in a variable
757  *
758  * We store a function pointer that will be called upon variable
759  * modification.
760  *
761  * \param p_this The object that holds the variable
762  * \param psz_name The name of the variable
763  * \param pf_callback The function pointer
764  * \param p_data A generic pointer that will be passed as the last
765  *               argument to the callback function.
766  *
767  * \warning The callback function is run in the thread that calls var_Set on
768  *          the variable. Use proper locking. This thread may not have much
769  *          time to spare, so keep callback functions short.
770  */
771 int __var_AddCallback( vlc_object_t *p_this, const char *psz_name,
772                        vlc_callback_t pf_callback, void *p_data )
773 {
774     int i_var;
775     variable_t *p_var;
776     callback_entry_t entry;
777
778     entry.pf_callback = pf_callback;
779     entry.p_data = p_data;
780
781     vlc_mutex_lock( &p_this->var_lock );
782
783     i_var = GetUnused( p_this, psz_name );
784     if( i_var < 0 )
785     {
786         vlc_mutex_unlock( &p_this->var_lock );
787         return i_var;
788     }
789
790     p_var = &p_this->p_vars[i_var];
791
792     INSERT_ELEM( p_var->p_entries,
793                  p_var->i_entries,
794                  p_var->i_entries,
795                  entry );
796
797     vlc_mutex_unlock( &p_this->var_lock );
798
799     return VLC_SUCCESS;
800 }
801
802 /**
803  * Remove a callback from a variable
804  *
805  * pf_callback and p_data have to be given again, because different objects
806  * might have registered the same callback function.
807  */
808 int __var_DelCallback( vlc_object_t *p_this, const char *psz_name,
809                        vlc_callback_t pf_callback, void *p_data )
810 {
811     int i_entry, i_var;
812     variable_t *p_var;
813
814     vlc_mutex_lock( &p_this->var_lock );
815
816     i_var = GetUnused( p_this, psz_name );
817     if( i_var < 0 )
818     {
819         vlc_mutex_unlock( &p_this->var_lock );
820         return i_var;
821     }
822
823     p_var = &p_this->p_vars[i_var];
824
825     for( i_entry = p_var->i_entries ; i_entry-- ; )
826     {
827         if( p_var->p_entries[i_entry].pf_callback == pf_callback
828             && p_var->p_entries[i_entry].p_data == p_data )
829         {
830             break;
831         }
832     }
833
834     if( i_entry < 0 )
835     {
836         vlc_mutex_unlock( &p_this->var_lock );
837         return VLC_EGENERIC;
838     }
839
840     REMOVE_ELEM( p_var->p_entries, p_var->i_entries, i_entry );
841
842     vlc_mutex_unlock( &p_this->var_lock );
843
844     return VLC_SUCCESS;
845 }
846
847 /* Following functions are local */
848
849 /*****************************************************************************
850  * GetUnused: find an unused variable from its name
851  *****************************************************************************
852  * We do i_tries tries before giving up, just in case the variable is being
853  * modified and called from a callback.
854  *****************************************************************************/
855 static int GetUnused( vlc_object_t *p_this, const char *psz_name )
856 {
857     int i_var, i_tries = 0;
858
859     while( VLC_TRUE )
860     {
861         i_var = Lookup( p_this->p_vars, p_this->i_vars, psz_name );
862         if( i_var < 0 )
863         {
864             return VLC_ENOVAR;
865         }
866
867         if( ! p_this->p_vars[i_var].b_incallback )
868         {
869             return i_var;
870         }
871
872         if( i_tries++ > 100 )
873         {
874             msg_Err( p_this, "caught in a callback deadlock?" );
875             return VLC_ETIMEOUT;
876         }
877
878         vlc_mutex_unlock( &p_this->var_lock );
879         msleep( THREAD_SLEEP );
880         vlc_mutex_lock( &p_this->var_lock );
881     }
882 }
883
884 /*****************************************************************************
885  * HashString: our cool hash function
886  *****************************************************************************
887  * This function is not intended to be crypto-secure, we only want it to be
888  * fast and not suck too much. This one is pretty fast and did 0 collisions
889  * in wenglish's dictionary.
890  *****************************************************************************/
891 static uint32_t HashString( const char *psz_string )
892 {
893     uint32_t i_hash = 0;
894
895     while( *psz_string )
896     {
897         i_hash += *psz_string++;
898         i_hash += i_hash << 10;
899         i_hash ^= i_hash >> 8;
900     }
901
902     return i_hash;
903 }
904
905 /*****************************************************************************
906  * Insert: find an empty slot to insert a new variable
907  *****************************************************************************
908  * We use a recursive inner function indexed on the hash. This function does
909  * nothing in the rare cases where a collision may occur, see Lookup()
910  * to see how we handle them.
911  * XXX: does this really need to be written recursively?
912  *****************************************************************************/
913 static int Insert( variable_t *p_vars, int i_count, const char *psz_name )
914 {
915     if( i_count == 0 )
916     {
917         return 0;
918     }
919
920     return InsertInner( p_vars, i_count, HashString( psz_name ) );
921 }
922
923 static int InsertInner( variable_t *p_vars, int i_count, uint32_t i_hash )
924 {
925     int i_middle;
926
927     if( i_hash <= p_vars[0].i_hash )
928     {
929         return 0;
930     }
931
932     if( i_hash >= p_vars[i_count - 1].i_hash )
933     {
934         return i_count;
935     }
936
937     i_middle = i_count / 2;
938
939     /* We know that 0 < i_middle */
940     if( i_hash < p_vars[i_middle].i_hash )
941     {
942         return InsertInner( p_vars, i_middle, i_hash );
943     }
944
945     /* We know that i_middle + 1 < i_count */
946     if( i_hash > p_vars[i_middle + 1].i_hash )
947     {
948         return i_middle + 1 + InsertInner( p_vars + i_middle + 1,
949                                            i_count - i_middle - 1,
950                                            i_hash );
951     }
952
953     return i_middle + 1;
954 }
955
956 /*****************************************************************************
957  * Lookup: find an existing variable given its name
958  *****************************************************************************
959  * We use a recursive inner function indexed on the hash. Care is taken of
960  * possible hash collisions.
961  * XXX: does this really need to be written recursively?
962  *****************************************************************************/
963 static int Lookup( variable_t *p_vars, int i_count, const char *psz_name )
964 {
965     uint32_t i_hash;
966     int i, i_pos;
967
968     if( i_count == 0 )
969     {
970         return -1;
971     }
972
973     i_hash = HashString( psz_name );
974
975     i_pos = LookupInner( p_vars, i_count, i_hash );
976
977     /* Hash not found */
978     if( i_hash != p_vars[i_pos].i_hash )
979     {
980         return -1;
981     }
982
983     /* Hash found, entry found */
984     if( !strcmp( psz_name, p_vars[i_pos].psz_name ) )
985     {
986         return i_pos;
987     }
988
989     /* Hash collision! This should be very rare, but we cannot guarantee
990      * it will never happen. Just do an exhaustive search amongst all
991      * entries with the same hash. */
992     for( i = i_pos - 1 ; i > 0 && i_hash == p_vars[i].i_hash ; i-- )
993     {
994         if( !strcmp( psz_name, p_vars[i].psz_name ) )
995         {
996             return i;
997         }
998     }
999
1000     for( i = i_pos + 1 ; i < i_count && i_hash == p_vars[i].i_hash ; i++ )
1001     {
1002         if( !strcmp( psz_name, p_vars[i].psz_name ) )
1003         {
1004             return i;
1005         }
1006     }
1007
1008     /* Hash found, but entry not found */
1009     return -1;
1010 }
1011
1012 static int LookupInner( variable_t *p_vars, int i_count, uint32_t i_hash )
1013 {
1014     int i_middle;
1015
1016     if( i_hash <= p_vars[0].i_hash )
1017     {
1018         return 0;
1019     }
1020
1021     if( i_hash >= p_vars[i_count-1].i_hash )
1022     {
1023         return i_count - 1;
1024     }
1025
1026     i_middle = i_count / 2;
1027
1028     /* We know that 0 < i_middle */
1029     if( i_hash < p_vars[i_middle].i_hash )
1030     {
1031         return LookupInner( p_vars, i_middle, i_hash );
1032     }
1033
1034     /* We know that i_middle + 1 < i_count */
1035     if( i_hash > p_vars[i_middle].i_hash )
1036     {
1037         return i_middle + LookupInner( p_vars + i_middle,
1038                                        i_count - i_middle,
1039                                        i_hash );
1040     }
1041
1042     return i_middle;
1043 }
1044
1045 /*****************************************************************************
1046  * CheckValue: check that a value is valid wrt. a variable
1047  *****************************************************************************
1048  * This function checks p_val's value against p_var's limitations such as
1049  * minimal and maximal value, step, in-list position, and modifies p_val if
1050  * necessary.
1051  *****************************************************************************/
1052 static void CheckValue ( variable_t *p_var, vlc_value_t *p_val )
1053 {
1054     /* Check that our variable is in the list */
1055     if( p_var->i_type & VLC_VAR_HASCHOICE && p_var->choices.i_count )
1056     {
1057         int i;
1058
1059         /* FIXME: the list is sorted, dude. Use something cleverer. */
1060         for( i = p_var->choices.i_count ; i-- ; )
1061         {
1062             if( p_var->pf_cmp( *p_val, p_var->choices.p_values[i] ) == 0 )
1063             {
1064                 break;
1065             }
1066         }
1067
1068         /* If not found, change it to anything vaguely valid */
1069         if( i < 0 )
1070         {
1071             /* Free the old variable, get the new one, dup it */
1072             p_var->pf_free( p_val );
1073             *p_val = p_var->choices.p_values[p_var->i_default >= 0
1074                                           ? p_var->i_default : 0 ];
1075             p_var->pf_dup( p_val );
1076         }
1077     }
1078
1079     /* Check that our variable is within the bounds */
1080     switch( p_var->i_type & VLC_VAR_TYPE )
1081     {
1082         case VLC_VAR_INTEGER:
1083             if( p_var->i_type & VLC_VAR_HASSTEP && p_var->step.i_int
1084                  && (p_val->i_int % p_var->step.i_int) )
1085             {
1086                 p_val->i_int = (p_val->i_int + (p_var->step.i_int / 2))
1087                                / p_var->step.i_int * p_var->step.i_int;
1088             }
1089             if( p_var->i_type & VLC_VAR_HASMIN
1090                  && p_val->i_int < p_var->min.i_int )
1091             {
1092                 p_val->i_int = p_var->min.i_int;
1093             }
1094             if( p_var->i_type & VLC_VAR_HASMAX
1095                  && p_val->i_int > p_var->max.i_int )
1096             {
1097                 p_val->i_int = p_var->max.i_int;
1098             }
1099             break;
1100         case VLC_VAR_FLOAT:
1101             if( p_var->i_type & VLC_VAR_HASSTEP && p_var->step.f_float )
1102             {
1103                 float f_round = p_var->step.f_float * (float)(int)( 0.5 +
1104                                         p_val->f_float / p_var->step.f_float );
1105                 if( p_val->f_float != f_round )
1106                 {
1107                     p_val->f_float = f_round;
1108                 }
1109             }
1110             if( p_var->i_type & VLC_VAR_HASMIN
1111                  && p_val->f_float < p_var->min.f_float )
1112             {
1113                 p_val->f_float = p_var->min.f_float;
1114             }
1115             if( p_var->i_type & VLC_VAR_HASMAX
1116                  && p_val->f_float > p_var->max.f_float )
1117             {
1118                 p_val->f_float = p_var->max.f_float;
1119             }
1120             break;
1121         case VLC_VAR_TIME:
1122             /* FIXME: TODO */
1123             break;
1124     }
1125 }
1126
1127 /*****************************************************************************
1128  * InheritValue: try to inherit the value of this variable from the same one
1129  *               in our closest parent.
1130  *****************************************************************************/
1131 static int InheritValue( vlc_object_t *p_this, const char *psz_name,
1132                          vlc_value_t *p_val, int i_type )
1133 {
1134     int i_var;
1135     variable_t *p_var;
1136
1137     /* No need to take the structure lock,
1138      * we are only looking for our parents */
1139
1140     if( !p_this->p_parent )
1141     {
1142         switch( i_type & VLC_VAR_TYPE )
1143         {
1144         case VLC_VAR_FILE:
1145         case VLC_VAR_DIRECTORY:
1146         case VLC_VAR_STRING:
1147             p_val->psz_string = config_GetPsz( p_this, psz_name );
1148             if( !p_val->psz_string ) p_val->psz_string = strdup("");
1149             break;
1150         case VLC_VAR_FLOAT:
1151             p_val->f_float = config_GetFloat( p_this, psz_name );
1152             break;
1153         case VLC_VAR_INTEGER:
1154             p_val->i_int = config_GetInt( p_this, psz_name );
1155             break;
1156         case VLC_VAR_BOOL:
1157             p_val->b_bool = config_GetInt( p_this, psz_name );
1158             break;
1159         default:
1160             return VLC_ENOOBJ;
1161             break;
1162         }
1163
1164         return VLC_SUCCESS;
1165     }
1166
1167     /* Look for the variable */
1168     vlc_mutex_lock( &p_this->p_parent->var_lock );
1169
1170     i_var = Lookup( p_this->p_parent->p_vars, p_this->p_parent->i_vars,
1171                     psz_name );
1172
1173     if( i_var >= 0 )
1174     {
1175         /* We found it! */
1176
1177         p_var = &p_this->p_parent->p_vars[i_var];
1178
1179         /* Really get the variable */
1180         *p_val = p_var->val;
1181
1182         /* Duplicate value if needed */
1183         p_var->pf_dup( p_val );
1184
1185         vlc_mutex_unlock( &p_this->p_parent->var_lock );
1186         return VLC_SUCCESS;
1187     }
1188
1189     vlc_mutex_unlock( &p_this->p_parent->var_lock );
1190
1191     /* We're still not there */
1192
1193     return InheritValue( p_this->p_parent, psz_name, p_val, i_type );
1194 }