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