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