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