]> git.sesse.net Git - vlc/blob - src/misc/variables.c
Attempt to fix linking with "included" gettext
[vlc] / src / misc / variables.c
1 /*****************************************************************************
2  * variables.c: routines for object variables handling
3  *****************************************************************************
4  * Copyright (C) 2002-2004 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Samuel Hocevar <sam@zoy.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #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     p_list->i_count = p_val->p_list->i_count;
67     if( p_val->p_list->i_count )
68     {
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     else
73     {
74         p_list->p_values = NULL;
75         p_list->pi_types = NULL;
76     }
77
78     for( i = 0; i < p_list->i_count; i++ )
79     {
80         p_list->p_values[i] = p_val->p_list->p_values[i];
81         p_list->pi_types[i] = p_val->p_list->pi_types[i];
82         switch( p_val->p_list->pi_types[i] & VLC_VAR_TYPE )
83         {
84         case VLC_VAR_STRING:
85
86             DupString( &p_list->p_values[i] );
87             break;
88         default:
89             break;
90         }
91     }
92
93     p_val->p_list = p_list;
94 }
95
96 static void FreeDummy( vlc_value_t *p_val ) { (void)p_val; /* unused */ }
97 static void FreeString( vlc_value_t *p_val ) { free( p_val->psz_string ); }
98 static void FreeMutex( vlc_value_t *p_val ) { vlc_mutex_destroy( (vlc_mutex_t*)p_val->p_address ); free( p_val->p_address ); }
99
100 static void FreeList( vlc_value_t *p_val )
101 {
102     int i;
103     for( i = 0; i < p_val->p_list->i_count; i++ )
104     {
105         switch( p_val->p_list->pi_types[i] & VLC_VAR_TYPE )
106         {
107         case VLC_VAR_STRING:
108             FreeString( &p_val->p_list->p_values[i] );
109             break;
110         case VLC_VAR_MUTEX:
111             FreeMutex( &p_val->p_list->p_values[i] );
112             break;
113         default:
114             break;
115         }
116     }
117
118     if( p_val->p_list->i_count )
119     {
120         free( p_val->p_list->p_values );
121         free( p_val->p_list->pi_types );
122     }
123     free( p_val->p_list );
124 }
125
126 /*****************************************************************************
127  * Local prototypes
128  *****************************************************************************/
129 static int      GetUnused   ( vlc_object_t *, const char * );
130 static uint32_t HashString  ( const char * );
131 static int      Insert      ( variable_t *, int, const char * );
132 static int      InsertInner ( variable_t *, int, uint32_t );
133 static int      Lookup      ( variable_t *, int, const char * );
134 static int      LookupInner ( variable_t *, int, uint32_t );
135
136 static void     CheckValue  ( variable_t *, vlc_value_t * );
137
138 static int      InheritValue( vlc_object_t *, const char *, vlc_value_t *,
139                               int );
140
141 /**
142  * Initialize a vlc variable
143  *
144  * We hash the given string and insert it into the sorted list. The insertion
145  * may require slow memory copies, but think about what we gain in the log(n)
146  * lookup phase when setting/getting the variable value!
147  *
148  * \param p_this The object in which to create the variable
149  * \param psz_name The name of the variable
150  * \param i_type The variables type. Must be one of \ref var_type combined with
151  *               zero or more \ref var_flags
152  */
153 int __var_Create( vlc_object_t *p_this, const char *psz_name, int i_type )
154 {
155     int i_new;
156     variable_t *p_var;
157     static vlc_list_t dummy_null_list = {0, NULL, NULL};
158
159     vlc_mutex_lock( &p_this->var_lock );
160
161     /* FIXME: if the variable already exists, we don't duplicate it. But we
162      * duplicate the lookups. It's not that serious, but if anyone finds some
163      * time to rework Insert() so that only one lookup has to be done, feel
164      * free to do so. */
165     i_new = Lookup( p_this->p_vars, p_this->i_vars, psz_name );
166
167     if( i_new >= 0 )
168     {
169         /* If the types differ, variable creation failed. */
170         if( (i_type & ~VLC_VAR_DOINHERIT) != p_this->p_vars[i_new].i_type )
171         {
172             vlc_mutex_unlock( &p_this->var_lock );
173             return VLC_EBADVAR;
174         }
175
176         p_this->p_vars[i_new].i_usage++;
177         vlc_mutex_unlock( &p_this->var_lock );
178         return VLC_SUCCESS;
179     }
180
181     i_new = Insert( p_this->p_vars, p_this->i_vars, psz_name );
182
183     if( (p_this->i_vars & 15) == 15 )
184     {
185         p_this->p_vars = realloc( p_this->p_vars,
186                                   (p_this->i_vars+17) * sizeof(variable_t) );
187     }
188
189     memmove( p_this->p_vars + i_new + 1,
190              p_this->p_vars + i_new,
191              (p_this->i_vars - i_new) * sizeof(variable_t) );
192
193     p_this->i_vars++;
194
195     p_var = &p_this->p_vars[i_new];
196     memset( p_var, 0, sizeof(*p_var) );
197
198     p_var->i_hash = HashString( psz_name );
199     p_var->psz_name = strdup( psz_name );
200     p_var->psz_text = NULL;
201
202     p_var->i_type = i_type & ~VLC_VAR_DOINHERIT;
203     memset( &p_var->val, 0, sizeof(vlc_value_t) );
204
205     p_var->pf_dup = DupDummy;
206     p_var->pf_free = FreeDummy;
207
208     p_var->i_usage = 1;
209
210     p_var->i_default = -1;
211     p_var->choices.i_count = 0;
212     p_var->choices.p_values = NULL;
213     p_var->choices_text.i_count = 0;
214     p_var->choices_text.p_values = NULL;
215
216     p_var->b_incallback = VLC_FALSE;
217     p_var->i_entries = 0;
218     p_var->p_entries = NULL;
219
220     /* Always initialize the variable, even if it is a list variable; this
221      * will lead to errors if the variable is not initialized, but it will
222      * not cause crashes in the variable handling. */
223     switch( i_type & VLC_VAR_TYPE )
224     {
225         case VLC_VAR_BOOL:
226             p_var->pf_cmp = CmpBool;
227             p_var->val.b_bool = VLC_FALSE;
228             break;
229         case VLC_VAR_INTEGER:
230         case VLC_VAR_HOTKEY:
231             p_var->pf_cmp = CmpInt;
232             p_var->val.i_int = 0;
233             break;
234         case VLC_VAR_STRING:
235         case VLC_VAR_MODULE:
236         case VLC_VAR_FILE:
237         case VLC_VAR_DIRECTORY:
238         case VLC_VAR_VARIABLE:
239             p_var->pf_cmp = CmpString;
240             p_var->pf_dup = DupString;
241             p_var->pf_free = FreeString;
242             p_var->val.psz_string = "";
243             break;
244         case VLC_VAR_FLOAT:
245             p_var->pf_cmp = CmpFloat;
246             p_var->val.f_float = 0.0;
247             break;
248         case VLC_VAR_TIME:
249             p_var->pf_cmp = CmpTime;
250             p_var->val.i_time = 0;
251             break;
252         case VLC_VAR_ADDRESS:
253             p_var->pf_cmp = CmpAddress;
254             p_var->val.p_address = NULL;
255             break;
256         case VLC_VAR_MUTEX:
257             p_var->pf_cmp = CmpAddress;
258             p_var->pf_free = FreeMutex;
259             p_var->val.p_address = malloc( sizeof(vlc_mutex_t) );
260             vlc_mutex_init( p_this, (vlc_mutex_t*)p_var->val.p_address );
261             break;
262         case VLC_VAR_LIST:
263             p_var->pf_cmp = CmpAddress;
264             p_var->pf_dup = DupList;
265             p_var->pf_free = FreeList;
266             p_var->val.p_list = &dummy_null_list;
267             break;
268     }
269
270     /* Duplicate the default data we stored. */
271     p_var->pf_dup( &p_var->val );
272
273     if( i_type & VLC_VAR_DOINHERIT )
274     {
275         vlc_value_t val;
276
277         if( InheritValue( p_this, psz_name, &val, p_var->i_type )
278             == VLC_SUCCESS );
279         {
280             /* Free data if needed */
281             p_var->pf_free( &p_var->val );
282             /* Set the variable */
283             p_var->val = val;
284
285             if( i_type & VLC_VAR_HASCHOICE )
286             {
287                 /* We must add the inherited value to our choice list */
288                 p_var->i_default = 0;
289
290                 INSERT_ELEM( p_var->choices.p_values, p_var->choices.i_count,
291                              0, val );
292                 INSERT_ELEM( p_var->choices_text.p_values,
293                              p_var->choices_text.i_count, 0, val );
294                 p_var->pf_dup( &p_var->choices.p_values[0] );
295                 p_var->choices_text.p_values[0].psz_string = NULL;
296             }
297         }
298     }
299
300     vlc_mutex_unlock( &p_this->var_lock );
301
302     return VLC_SUCCESS;
303 }
304
305 /**
306  * Destroy a vlc variable
307  *
308  * Look for the variable and destroy it if it is found. As in var_Create we
309  * do a call to memmove() but we have performance counterparts elsewhere.
310  *
311  * \param p_this The object that holds the variable
312  * \param psz_name The name of the variable
313  */
314 int __var_Destroy( vlc_object_t *p_this, const char *psz_name )
315 {
316     int i_var, i;
317     variable_t *p_var;
318
319     vlc_mutex_lock( &p_this->var_lock );
320
321     i_var = GetUnused( p_this, psz_name );
322     if( i_var < 0 )
323     {
324         vlc_mutex_unlock( &p_this->var_lock );
325         return i_var;
326     }
327
328     p_var = &p_this->p_vars[i_var];
329
330     if( p_var->i_usage > 1 )
331     {
332         p_var->i_usage--;
333         vlc_mutex_unlock( &p_this->var_lock );
334         return VLC_SUCCESS;
335     }
336
337     /* Free value if needed */
338     p_var->pf_free( &p_var->val );
339
340     /* Free choice list if needed */
341     if( p_var->choices.i_count )
342     {
343         for( i = 0 ; i < p_var->choices.i_count ; i++ )
344         {
345             p_var->pf_free( &p_var->choices.p_values[i] );
346             if( p_var->choices_text.p_values[i].psz_string )
347                 free( p_var->choices_text.p_values[i].psz_string );
348         }
349         free( p_var->choices.p_values );
350         free( p_var->choices_text.p_values );
351     }
352
353     /* Free callbacks if needed */
354     if( p_var->p_entries )
355     {
356         free( p_var->p_entries );
357     }
358
359     free( p_var->psz_name );
360     if( p_var->psz_text ) free( p_var->psz_text );
361
362     memmove( p_this->p_vars + i_var,
363              p_this->p_vars + i_var + 1,
364              (p_this->i_vars - i_var - 1) * sizeof(variable_t) );
365
366     if( (p_this->i_vars & 15) == 0 )
367     {
368         p_this->p_vars = realloc( p_this->p_vars,
369                           (p_this->i_vars) * sizeof( variable_t ) );
370     }
371
372     p_this->i_vars--;
373
374     vlc_mutex_unlock( &p_this->var_lock );
375
376     return VLC_SUCCESS;
377 }
378
379 /**
380  * Perform an action on a variable
381  *
382  * \param p_this The object that holds the variable
383  * \param psz_name The name of the variable
384  * \param i_action The action to perform. Must be one of \ref var_action
385  * \param p_val First action parameter
386  * \param p_val2 Second action parameter
387  */
388 int __var_Change( vlc_object_t *p_this, const char *psz_name,
389                   int i_action, vlc_value_t *p_val, vlc_value_t *p_val2 )
390 {
391     int i_var, i;
392     variable_t *p_var;
393     vlc_value_t oldval;
394
395     vlc_mutex_lock( &p_this->var_lock );
396
397     i_var = Lookup( p_this->p_vars, p_this->i_vars, psz_name );
398
399     if( i_var < 0 )
400     {
401         vlc_mutex_unlock( &p_this->var_lock );
402         return VLC_ENOVAR;
403     }
404
405     p_var = &p_this->p_vars[i_var];
406
407     switch( i_action )
408     {
409         case VLC_VAR_SETMIN:
410             if( p_var->i_type & VLC_VAR_HASMIN )
411             {
412                 p_var->pf_free( &p_var->min );
413             }
414             p_var->i_type |= VLC_VAR_HASMIN;
415             p_var->min = *p_val;
416             p_var->pf_dup( &p_var->min );
417             CheckValue( p_var, &p_var->val );
418             break;
419         case VLC_VAR_SETMAX:
420             if( p_var->i_type & VLC_VAR_HASMAX )
421             {
422                 p_var->pf_free( &p_var->max );
423             }
424             p_var->i_type |= VLC_VAR_HASMAX;
425             p_var->max = *p_val;
426             p_var->pf_dup( &p_var->max );
427             CheckValue( p_var, &p_var->val );
428             break;
429         case VLC_VAR_SETSTEP:
430             if( p_var->i_type & VLC_VAR_HASSTEP )
431             {
432                 p_var->pf_free( &p_var->step );
433             }
434             p_var->i_type |= VLC_VAR_HASSTEP;
435             p_var->step = *p_val;
436             p_var->pf_dup( &p_var->step );
437             CheckValue( p_var, &p_var->val );
438             break;
439         case VLC_VAR_ADDCHOICE:
440             /* FIXME: the list is sorted, dude. Use something cleverer. */
441             for( i = p_var->choices.i_count ; i-- ; )
442             {
443                 if( p_var->pf_cmp( p_var->choices.p_values[i], *p_val ) < 0 )
444                 {
445                     break;
446                 }
447             }
448
449             /* The new place is i+1 */
450             i++;
451
452             if( p_var->i_default >= i )
453             {
454                 p_var->i_default++;
455             }
456
457             INSERT_ELEM( p_var->choices.p_values, p_var->choices.i_count,
458                          i, *p_val );
459             INSERT_ELEM( p_var->choices_text.p_values,
460                          p_var->choices_text.i_count, i, *p_val );
461             p_var->pf_dup( &p_var->choices.p_values[i] );
462             p_var->choices_text.p_values[i].psz_string =
463                 ( p_val2 && p_val2->psz_string ) ?
464                 strdup( p_val2->psz_string ) : NULL;
465
466             CheckValue( p_var, &p_var->val );
467             break;
468         case VLC_VAR_DELCHOICE:
469             /* FIXME: the list is sorted, dude. Use something cleverer. */
470             for( i = 0 ; i < p_var->choices.i_count ; i++ )
471             {
472                 if( p_var->pf_cmp( p_var->choices.p_values[i], *p_val ) == 0 )
473                 {
474                     break;
475                 }
476             }
477
478             if( i == p_var->choices.i_count )
479             {
480                 /* Not found */
481                 vlc_mutex_unlock( &p_this->var_lock );
482                 return VLC_EGENERIC;
483             }
484
485             if( p_var->i_default > i )
486             {
487                 p_var->i_default--;
488             }
489             else if( p_var->i_default == i )
490             {
491                 p_var->i_default = -1;
492             }
493
494             p_var->pf_free( &p_var->choices.p_values[i] );
495             if( p_var->choices_text.p_values[i].psz_string )
496                 free( p_var->choices_text.p_values[i].psz_string );
497             REMOVE_ELEM( p_var->choices.p_values, p_var->choices.i_count, i );
498             REMOVE_ELEM( p_var->choices_text.p_values,
499                          p_var->choices_text.i_count, i );
500
501             CheckValue( p_var, &p_var->val );
502             break;
503         case VLC_VAR_CHOICESCOUNT:
504             p_val->i_int = p_var->choices.i_count;
505             break;
506         case VLC_VAR_CLEARCHOICES:
507             for( i = 0 ; i < p_var->choices.i_count ; i++ )
508             {
509                 p_var->pf_free( &p_var->choices.p_values[i] );
510             }
511             for( i = 0 ; i < p_var->choices_text.i_count ; i++ )
512             {
513                 if( p_var->choices_text.p_values[i].psz_string )
514                     free( p_var->choices_text.p_values[i].psz_string );
515             }
516             if( p_var->choices.i_count ) free( p_var->choices.p_values );
517             if( p_var->choices_text.i_count ) free( p_var->choices_text.p_values );
518
519             p_var->choices.i_count = 0;
520             p_var->choices.p_values = NULL;
521             p_var->choices_text.i_count = 0;
522             p_var->choices_text.p_values = NULL;
523             p_var->i_default = -1;
524             break;
525         case VLC_VAR_SETDEFAULT:
526             /* FIXME: the list is sorted, dude. Use something cleverer. */
527             for( i = 0 ; i < p_var->choices.i_count ; i++ )
528             {
529                 if( p_var->pf_cmp( p_var->choices.p_values[i], *p_val ) == 0 )
530                 {
531                     break;
532                 }
533             }
534
535             if( i == p_var->choices.i_count )
536             {
537                 /* Not found */
538                 break;
539             }
540
541             p_var->i_default = i;
542             CheckValue( p_var, &p_var->val );
543             break;
544         case VLC_VAR_SETVALUE:
545             /* Duplicate data if needed */
546             p_var->pf_dup( p_val );
547             /* Backup needed stuff */
548             oldval = p_var->val;
549             /* Check boundaries and list */
550             CheckValue( p_var, p_val );
551             /* Set the variable */
552             p_var->val = *p_val;
553             /* Free data if needed */
554             p_var->pf_free( &oldval );
555             break;
556         case VLC_VAR_GETCHOICES:
557         case VLC_VAR_GETLIST:
558             p_val->p_list = malloc( sizeof(vlc_list_t) );
559             if( p_val2 ) p_val2->p_list = malloc( sizeof(vlc_list_t) );
560             if( p_var->choices.i_count )
561             {
562                 p_val->p_list->p_values = malloc( p_var->choices.i_count
563                                                   * sizeof(vlc_value_t) );
564                 p_val->p_list->pi_types = malloc( p_var->choices.i_count
565                                                   * sizeof(int) );
566                 if( p_val2 )
567                 {
568                     p_val2->p_list->p_values =
569                         malloc( p_var->choices.i_count * sizeof(vlc_value_t) );
570                     p_val2->p_list->pi_types =
571                         malloc( p_var->choices.i_count * sizeof(int) );
572                 }
573             }
574             p_val->p_list->i_count = p_var->choices.i_count;
575             if( p_val2 ) p_val2->p_list->i_count = p_var->choices.i_count;
576             for( i = 0 ; i < p_var->choices.i_count ; i++ )
577             {
578                 p_val->p_list->p_values[i] = p_var->choices.p_values[i];
579                 p_val->p_list->pi_types[i] = p_var->i_type;
580                 p_var->pf_dup( &p_val->p_list->p_values[i] );
581                 if( p_val2 )
582                 {
583                     p_val2->p_list->p_values[i].psz_string =
584                         p_var->choices_text.p_values[i].psz_string ?
585                     strdup(p_var->choices_text.p_values[i].psz_string) : NULL;
586                     p_val2->p_list->pi_types[i] = VLC_VAR_STRING;
587                 }
588             }
589             break;
590         case VLC_VAR_FREELIST:
591             FreeList( p_val );
592             if( p_val2 && p_val2->p_list )
593             {
594                 for( i = 0; i < p_val2->p_list->i_count; i++ )
595                     if( p_val2->p_list->p_values[i].psz_string )
596                         free( p_val2->p_list->p_values[i].psz_string );
597                 if( p_val2->p_list->i_count )
598                 {
599                     free( p_val2->p_list->p_values );
600                     free( p_val2->p_list->pi_types );
601                 }
602                 free( p_val2->p_list );
603             }
604             break;
605         case VLC_VAR_SETTEXT:
606             if( p_var->psz_text ) free( p_var->psz_text );
607             if( p_val && p_val->psz_string )
608                 p_var->psz_text = strdup( p_val->psz_string );
609             break;
610         case VLC_VAR_GETTEXT:
611             p_val->psz_string = NULL;
612             if( p_var->psz_text )
613             {
614                 p_val->psz_string = strdup( p_var->psz_text );
615             }
616             break;
617         case VLC_VAR_INHERITVALUE:
618             {
619                 vlc_value_t val;
620
621                 if( InheritValue( p_this, psz_name, &val, p_var->i_type )
622                     == VLC_SUCCESS );
623                 {
624                     /* Duplicate already done */
625
626                     /* Backup needed stuff */
627                     oldval = p_var->val;
628                     /* Check boundaries and list */
629                     CheckValue( p_var, &val );
630                     /* Set the variable */
631                     p_var->val = val;
632                     /* Free data if needed */
633                     p_var->pf_free( &oldval );
634                 }
635
636                 if( p_val )
637                 {
638                     *p_val = p_var->val;
639                     p_var->pf_dup( p_val );
640                 }
641             }
642             break;
643         case VLC_VAR_TRIGGER_CALLBACKS:
644             {
645                 /* Deal with callbacks. Tell we're in a callback, release the lock,
646                  * call stored functions, retake the lock. */
647                 if( p_var->i_entries )
648                 {
649                     int i_var;
650                     int i_entries = p_var->i_entries;
651                     callback_entry_t *p_entries = p_var->p_entries;
652
653                     p_var->b_incallback = VLC_TRUE;
654                     vlc_mutex_unlock( &p_this->var_lock );
655
656                     /* The real calls */
657                     for( ; i_entries-- ; )
658                     {
659                         p_entries[i_entries].pf_callback( p_this, psz_name, p_var->val, p_var->val,
660                                                           p_entries[i_entries].p_data );
661                     }
662
663                     vlc_mutex_lock( &p_this->var_lock );
664
665                     i_var = Lookup( p_this->p_vars, p_this->i_vars, psz_name );
666                     if( i_var < 0 )
667                     {
668                         msg_Err( p_this, "variable %s has disappeared", psz_name );
669                         vlc_mutex_unlock( &p_this->var_lock );
670                         return VLC_ENOVAR;
671                     }
672
673                     p_var = &p_this->p_vars[i_var];
674                     p_var->b_incallback = VLC_FALSE;
675                 }
676             }
677             break;
678
679         default:
680             break;
681     }
682
683     vlc_mutex_unlock( &p_this->var_lock );
684
685     return VLC_SUCCESS;
686 }
687
688 /**
689  * Request a variable's type
690  *
691  * \return The variable type if it exists, or 0 if the
692  * variable could not be found.
693  * \see \ref var_type
694  */
695 int __var_Type( vlc_object_t *p_this, const char *psz_name )
696 {
697     int i_var, i_type;
698
699     vlc_mutex_lock( &p_this->var_lock );
700
701     i_var = Lookup( p_this->p_vars, p_this->i_vars, psz_name );
702
703     if( i_var < 0 )
704     {
705         vlc_mutex_unlock( &p_this->var_lock );
706         return 0;
707     }
708
709     i_type = p_this->p_vars[i_var].i_type;
710
711     vlc_mutex_unlock( &p_this->var_lock );
712
713     return i_type;
714 }
715
716 /**
717  * Set a variable's value
718  *
719  * \param p_this The object that hold the variable
720  * \param psz_name The name of the variable
721  * \param val the value to set
722  */
723 int __var_Set( vlc_object_t *p_this, const char *psz_name, vlc_value_t val )
724 {
725     int i_var;
726     variable_t *p_var;
727     vlc_value_t oldval;
728
729     vlc_mutex_lock( &p_this->var_lock );
730
731     i_var = GetUnused( p_this, psz_name );
732     if( i_var < 0 )
733     {
734         vlc_mutex_unlock( &p_this->var_lock );
735         return i_var;
736     }
737
738     p_var = &p_this->p_vars[i_var];
739
740     /* Duplicate data if needed */
741     p_var->pf_dup( &val );
742
743     /* Backup needed stuff */
744     oldval = p_var->val;
745
746     /* Check boundaries and list */
747     CheckValue( p_var, &val );
748
749     /* Set the variable */
750     p_var->val = val;
751
752     /* Deal with callbacks. Tell we're in a callback, release the lock,
753      * call stored functions, retake the lock. */
754     if( p_var->i_entries )
755     {
756         int i_var;
757         int i_entries = p_var->i_entries;
758         callback_entry_t *p_entries = p_var->p_entries;
759
760         p_var->b_incallback = VLC_TRUE;
761         vlc_mutex_unlock( &p_this->var_lock );
762
763         /* The real calls */
764         for( ; i_entries-- ; )
765         {
766             p_entries[i_entries].pf_callback( p_this, psz_name, oldval, val,
767                                               p_entries[i_entries].p_data );
768         }
769
770         vlc_mutex_lock( &p_this->var_lock );
771
772         i_var = Lookup( p_this->p_vars, p_this->i_vars, psz_name );
773         if( i_var < 0 )
774         {
775             msg_Err( p_this, "variable %s has disappeared", psz_name );
776             vlc_mutex_unlock( &p_this->var_lock );
777             return VLC_ENOVAR;
778         }
779
780         p_var = &p_this->p_vars[i_var];
781         p_var->b_incallback = VLC_FALSE;
782     }
783
784     /* Free data if needed */
785     p_var->pf_free( &oldval );
786
787     vlc_mutex_unlock( &p_this->var_lock );
788
789     return VLC_SUCCESS;
790 }
791
792 /**
793  * Get a variable's value
794  *
795  * \param p_this The object that holds the variable
796  * \param psz_name The name of the variable
797  * \param p_val Pointer to a vlc_value_t that will hold the variable's value
798  *              after the function is finished
799  */
800 int __var_Get( vlc_object_t *p_this, const char *psz_name, vlc_value_t *p_val )
801 {
802     int i_var;
803     variable_t *p_var;
804
805     vlc_mutex_lock( &p_this->var_lock );
806
807     i_var = Lookup( p_this->p_vars, p_this->i_vars, psz_name );
808
809     if( i_var < 0 )
810     {
811         vlc_mutex_unlock( &p_this->var_lock );
812         return VLC_ENOVAR;
813     }
814
815     p_var = &p_this->p_vars[i_var];
816
817     /* Really get the variable */
818     *p_val = p_var->val;
819
820     /* Duplicate value if needed */
821     p_var->pf_dup( p_val );
822
823     vlc_mutex_unlock( &p_this->var_lock );
824
825     return VLC_SUCCESS;
826 }
827
828 /**
829  * Register a callback in a variable
830  *
831  * We store a function pointer that will be called upon variable
832  * modification.
833  *
834  * \param p_this The object that holds the variable
835  * \param psz_name The name of the variable
836  * \param pf_callback The function pointer
837  * \param p_data A generic pointer that will be passed as the last
838  *               argument to the callback function.
839  *
840  * \warning The callback function is run in the thread that calls var_Set on
841  *          the variable. Use proper locking. This thread may not have much
842  *          time to spare, so keep callback functions short.
843  */
844 int __var_AddCallback( vlc_object_t *p_this, const char *psz_name,
845                        vlc_callback_t pf_callback, void *p_data )
846 {
847     int i_var;
848     variable_t *p_var;
849     callback_entry_t entry;
850
851     entry.pf_callback = pf_callback;
852     entry.p_data = p_data;
853
854     vlc_mutex_lock( &p_this->var_lock );
855
856     i_var = GetUnused( p_this, psz_name );
857     if( i_var < 0 )
858     {
859         vlc_mutex_unlock( &p_this->var_lock );
860         return i_var;
861     }
862
863     p_var = &p_this->p_vars[i_var];
864
865     INSERT_ELEM( p_var->p_entries,
866                  p_var->i_entries,
867                  p_var->i_entries,
868                  entry );
869
870     vlc_mutex_unlock( &p_this->var_lock );
871
872     return VLC_SUCCESS;
873 }
874
875 /**
876  * Remove a callback from a variable
877  *
878  * pf_callback and p_data have to be given again, because different objects
879  * might have registered the same callback function.
880  */
881 int __var_DelCallback( vlc_object_t *p_this, const char *psz_name,
882                        vlc_callback_t pf_callback, void *p_data )
883 {
884     int i_entry, i_var;
885     variable_t *p_var;
886
887     vlc_mutex_lock( &p_this->var_lock );
888
889     i_var = GetUnused( p_this, psz_name );
890     if( i_var < 0 )
891     {
892         vlc_mutex_unlock( &p_this->var_lock );
893         return i_var;
894     }
895
896     p_var = &p_this->p_vars[i_var];
897
898     for( i_entry = p_var->i_entries ; i_entry-- ; )
899     {
900         if( p_var->p_entries[i_entry].pf_callback == pf_callback
901             && p_var->p_entries[i_entry].p_data == p_data )
902         {
903             break;
904         }
905     }
906
907     if( i_entry < 0 )
908     {
909         vlc_mutex_unlock( &p_this->var_lock );
910         return VLC_EGENERIC;
911     }
912
913     REMOVE_ELEM( p_var->p_entries, p_var->i_entries, i_entry );
914
915     vlc_mutex_unlock( &p_this->var_lock );
916
917     return VLC_SUCCESS;
918 }
919
920 /** Parse a stringified option
921  * This function parse a string option and create the associated object
922  * variable
923  * The option must be of the form "[no[-]]foo[=bar]" where foo is the
924  * option name and bar is the value of the option.
925  * \param p_obj the object in which the variable must be created
926  * \param psz_option the option to parse
927  * \return nothing
928  */
929 void __var_OptionParse( vlc_object_t *p_obj,const char *psz_option )
930 {
931     char *psz_name = (char *)psz_option;
932     char *psz_value = strchr( psz_option, '=' );
933     int  i_name_len, i_type;
934     vlc_bool_t b_isno = VLC_FALSE;
935     vlc_value_t val;
936
937     if( psz_value ) i_name_len = psz_value - psz_option;
938     else i_name_len = strlen( psz_option );
939
940     /* It's too much of an hassle to remove the ':' when we parse
941      * the cmd line :) */
942     if( i_name_len && *psz_name == ':' )
943     {
944         psz_name++;
945         i_name_len--;
946     }
947
948     if( i_name_len == 0 ) return;
949
950     psz_name = strndup( psz_name, i_name_len );
951     if( psz_value ) psz_value++;
952
953     /* FIXME: :programs should be handled generically */
954     if( !strcmp( psz_name, "programs" ) )
955         i_type = VLC_VAR_LIST;
956     else
957         i_type = config_GetType( p_obj, psz_name );
958
959     if( !i_type && !psz_value )
960     {
961         /* check for "no-foo" or "nofoo" */
962         if( !strncmp( psz_name, "no-", 3 ) )
963         {
964             memmove( psz_name, psz_name + 3, strlen(psz_name) + 1 - 3 );
965         }
966         else if( !strncmp( psz_name, "no", 2 ) )
967         {
968             memmove( psz_name, psz_name + 2, strlen(psz_name) + 1 - 2 );
969         }
970         else goto cleanup;           /* Option doesn't exist */
971
972         b_isno = VLC_TRUE;
973         i_type = config_GetType( p_obj, psz_name );
974
975         if( !i_type ) goto cleanup;  /* Option doesn't exist */
976     }
977     else if( !i_type ) goto cleanup; /* Option doesn't exist */
978
979     if( ( i_type != VLC_VAR_BOOL ) &&
980         ( !psz_value || !*psz_value ) ) goto cleanup; /* Invalid value */
981
982     /* Create the variable in the input object.
983      * Children of the input object will be able to retreive this value
984      * thanks to the inheritance property of the object variables. */
985     var_Create( p_obj, psz_name, i_type );
986
987     switch( i_type )
988     {
989     case VLC_VAR_BOOL:
990         val.b_bool = !b_isno;
991         break;
992
993     case VLC_VAR_INTEGER:
994         val.i_int = strtol( psz_value, NULL, 0 );
995         break;
996
997     case VLC_VAR_FLOAT:
998         val.f_float = atof( psz_value );
999         break;
1000
1001     case VLC_VAR_STRING:
1002     case VLC_VAR_MODULE:
1003     case VLC_VAR_FILE:
1004     case VLC_VAR_DIRECTORY:
1005         val.psz_string = psz_value;
1006         break;
1007
1008     case VLC_VAR_LIST:
1009     {
1010         char *psz_orig, *psz_var;
1011         vlc_list_t *p_list = malloc(sizeof(vlc_list_t));
1012         val.p_list = p_list;
1013         p_list->i_count = 0;
1014
1015         psz_var = psz_orig = strdup(psz_value);
1016         while( psz_var && *psz_var )
1017         {
1018             char *psz_item = psz_var;
1019             vlc_value_t val2;
1020             while( *psz_var && *psz_var != ',' ) psz_var++;
1021             if( *psz_var == ',' )
1022             {
1023                 *psz_var = '\0';
1024                 psz_var++;
1025             }
1026             val2.i_int = strtol( psz_item, NULL, 0 );
1027             INSERT_ELEM( p_list->p_values, p_list->i_count,
1028                          p_list->i_count, val2 );
1029             /* p_list->i_count is incremented twice by INSERT_ELEM */
1030             p_list->i_count--;
1031             INSERT_ELEM( p_list->pi_types, p_list->i_count,
1032                          p_list->i_count, VLC_VAR_INTEGER );
1033         }
1034         if( psz_orig ) free( psz_orig );
1035         break;
1036     }
1037
1038     default:
1039         goto cleanup;
1040         break;
1041     }
1042
1043     var_Set( p_obj, psz_name, val );
1044
1045   cleanup:
1046     if( psz_name ) free( psz_name );
1047     return;
1048 }
1049
1050
1051 /* Following functions are local */
1052
1053 /*****************************************************************************
1054  * GetUnused: find an unused variable from its name
1055  *****************************************************************************
1056  * We do i_tries tries before giving up, just in case the variable is being
1057  * modified and called from a callback.
1058  *****************************************************************************/
1059 static int GetUnused( vlc_object_t *p_this, const char *psz_name )
1060 {
1061     int i_var, i_tries = 0;
1062
1063     while( VLC_TRUE )
1064     {
1065         i_var = Lookup( p_this->p_vars, p_this->i_vars, psz_name );
1066         if( i_var < 0 )
1067         {
1068             return VLC_ENOVAR;
1069         }
1070
1071         if( ! p_this->p_vars[i_var].b_incallback )
1072         {
1073             return i_var;
1074         }
1075
1076         if( i_tries++ > 100 )
1077         {
1078             msg_Err( p_this, "caught in a callback deadlock?" );
1079             return VLC_ETIMEOUT;
1080         }
1081
1082         vlc_mutex_unlock( &p_this->var_lock );
1083         msleep( THREAD_SLEEP );
1084         vlc_mutex_lock( &p_this->var_lock );
1085     }
1086 }
1087
1088 /*****************************************************************************
1089  * HashString: our cool hash function
1090  *****************************************************************************
1091  * This function is not intended to be crypto-secure, we only want it to be
1092  * fast and not suck too much. This one is pretty fast and did 0 collisions
1093  * in wenglish's dictionary.
1094  *****************************************************************************/
1095 static uint32_t HashString( const char *psz_string )
1096 {
1097     uint32_t i_hash = 0;
1098
1099     while( *psz_string )
1100     {
1101         i_hash += *psz_string++;
1102         i_hash += i_hash << 10;
1103         i_hash ^= i_hash >> 8;
1104     }
1105
1106     return i_hash;
1107 }
1108
1109 /*****************************************************************************
1110  * Insert: find an empty slot to insert a new variable
1111  *****************************************************************************
1112  * We use a recursive inner function indexed on the hash. This function does
1113  * nothing in the rare cases where a collision may occur, see Lookup()
1114  * to see how we handle them.
1115  * XXX: does this really need to be written recursively?
1116  *****************************************************************************/
1117 static int Insert( variable_t *p_vars, int i_count, const char *psz_name )
1118 {
1119     if( i_count == 0 )
1120     {
1121         return 0;
1122     }
1123
1124     return InsertInner( p_vars, i_count, HashString( psz_name ) );
1125 }
1126
1127 static int InsertInner( variable_t *p_vars, int i_count, uint32_t i_hash )
1128 {
1129     int i_middle;
1130
1131     if( i_hash <= p_vars[0].i_hash )
1132     {
1133         return 0;
1134     }
1135
1136     if( i_hash >= p_vars[i_count - 1].i_hash )
1137     {
1138         return i_count;
1139     }
1140
1141     i_middle = i_count / 2;
1142
1143     /* We know that 0 < i_middle */
1144     if( i_hash < p_vars[i_middle].i_hash )
1145     {
1146         return InsertInner( p_vars, i_middle, i_hash );
1147     }
1148
1149     /* We know that i_middle + 1 < i_count */
1150     if( i_hash > p_vars[i_middle + 1].i_hash )
1151     {
1152         return i_middle + 1 + InsertInner( p_vars + i_middle + 1,
1153                                            i_count - i_middle - 1,
1154                                            i_hash );
1155     }
1156
1157     return i_middle + 1;
1158 }
1159
1160 /*****************************************************************************
1161  * Lookup: find an existing variable given its name
1162  *****************************************************************************
1163  * We use a recursive inner function indexed on the hash. Care is taken of
1164  * possible hash collisions.
1165  * XXX: does this really need to be written recursively?
1166  *****************************************************************************/
1167 static int Lookup( variable_t *p_vars, int i_count, const char *psz_name )
1168 {
1169     uint32_t i_hash;
1170     int i, i_pos;
1171
1172     if( i_count == 0 )
1173     {
1174         return -1;
1175     }
1176
1177     i_hash = HashString( psz_name );
1178
1179     i_pos = LookupInner( p_vars, i_count, i_hash );
1180
1181     /* Hash not found */
1182     if( i_hash != p_vars[i_pos].i_hash )
1183     {
1184         return -1;
1185     }
1186
1187     /* Hash found, entry found */
1188     if( !strcmp( psz_name, p_vars[i_pos].psz_name ) )
1189     {
1190         return i_pos;
1191     }
1192
1193     /* Hash collision! This should be very rare, but we cannot guarantee
1194      * it will never happen. Just do an exhaustive search amongst all
1195      * entries with the same hash. */
1196     for( i = i_pos - 1 ; i > 0 && i_hash == p_vars[i].i_hash ; i-- )
1197     {
1198         if( !strcmp( psz_name, p_vars[i].psz_name ) )
1199         {
1200             return i;
1201         }
1202     }
1203
1204     for( i = i_pos + 1 ; i < i_count && i_hash == p_vars[i].i_hash ; i++ )
1205     {
1206         if( !strcmp( psz_name, p_vars[i].psz_name ) )
1207         {
1208             return i;
1209         }
1210     }
1211
1212     /* Hash found, but entry not found */
1213     return -1;
1214 }
1215
1216 static int LookupInner( variable_t *p_vars, int i_count, uint32_t i_hash )
1217 {
1218     int i_middle;
1219
1220     if( i_hash <= p_vars[0].i_hash )
1221     {
1222         return 0;
1223     }
1224
1225     if( i_hash >= p_vars[i_count-1].i_hash )
1226     {
1227         return i_count - 1;
1228     }
1229
1230     i_middle = i_count / 2;
1231
1232     /* We know that 0 < i_middle */
1233     if( i_hash < p_vars[i_middle].i_hash )
1234     {
1235         return LookupInner( p_vars, i_middle, i_hash );
1236     }
1237
1238     /* We know that i_middle + 1 < i_count */
1239     if( i_hash > p_vars[i_middle].i_hash )
1240     {
1241         return i_middle + LookupInner( p_vars + i_middle,
1242                                        i_count - i_middle,
1243                                        i_hash );
1244     }
1245
1246     return i_middle;
1247 }
1248
1249 /*****************************************************************************
1250  * CheckValue: check that a value is valid wrt. a variable
1251  *****************************************************************************
1252  * This function checks p_val's value against p_var's limitations such as
1253  * minimal and maximal value, step, in-list position, and modifies p_val if
1254  * necessary.
1255  *****************************************************************************/
1256 static void CheckValue ( variable_t *p_var, vlc_value_t *p_val )
1257 {
1258     /* Check that our variable is in the list */
1259     if( p_var->i_type & VLC_VAR_HASCHOICE && p_var->choices.i_count )
1260     {
1261         int i;
1262
1263         /* FIXME: the list is sorted, dude. Use something cleverer. */
1264         for( i = p_var->choices.i_count ; i-- ; )
1265         {
1266             if( p_var->pf_cmp( *p_val, p_var->choices.p_values[i] ) == 0 )
1267             {
1268                 break;
1269             }
1270         }
1271
1272         /* If not found, change it to anything vaguely valid */
1273         if( i < 0 )
1274         {
1275             /* Free the old variable, get the new one, dup it */
1276             p_var->pf_free( p_val );
1277             *p_val = p_var->choices.p_values[p_var->i_default >= 0
1278                                           ? p_var->i_default : 0 ];
1279             p_var->pf_dup( p_val );
1280         }
1281     }
1282
1283     /* Check that our variable is within the bounds */
1284     switch( p_var->i_type & VLC_VAR_TYPE )
1285     {
1286         case VLC_VAR_INTEGER:
1287             if( p_var->i_type & VLC_VAR_HASSTEP && p_var->step.i_int
1288                  && (p_val->i_int % p_var->step.i_int) )
1289             {
1290                 p_val->i_int = (p_val->i_int + (p_var->step.i_int / 2))
1291                                / p_var->step.i_int * p_var->step.i_int;
1292             }
1293             if( p_var->i_type & VLC_VAR_HASMIN
1294                  && p_val->i_int < p_var->min.i_int )
1295             {
1296                 p_val->i_int = p_var->min.i_int;
1297             }
1298             if( p_var->i_type & VLC_VAR_HASMAX
1299                  && p_val->i_int > p_var->max.i_int )
1300             {
1301                 p_val->i_int = p_var->max.i_int;
1302             }
1303             break;
1304         case VLC_VAR_FLOAT:
1305             if( p_var->i_type & VLC_VAR_HASSTEP && p_var->step.f_float )
1306             {
1307                 float f_round = p_var->step.f_float * (float)(int)( 0.5 +
1308                                         p_val->f_float / p_var->step.f_float );
1309                 if( p_val->f_float != f_round )
1310                 {
1311                     p_val->f_float = f_round;
1312                 }
1313             }
1314             if( p_var->i_type & VLC_VAR_HASMIN
1315                  && p_val->f_float < p_var->min.f_float )
1316             {
1317                 p_val->f_float = p_var->min.f_float;
1318             }
1319             if( p_var->i_type & VLC_VAR_HASMAX
1320                  && p_val->f_float > p_var->max.f_float )
1321             {
1322                 p_val->f_float = p_var->max.f_float;
1323             }
1324             break;
1325         case VLC_VAR_TIME:
1326             /* FIXME: TODO */
1327             break;
1328     }
1329 }
1330
1331 /*****************************************************************************
1332  * InheritValue: try to inherit the value of this variable from the same one
1333  *               in our closest parent.
1334  *****************************************************************************/
1335 static int InheritValue( vlc_object_t *p_this, const char *psz_name,
1336                          vlc_value_t *p_val, int i_type )
1337 {
1338     int i_var;
1339     variable_t *p_var;
1340
1341     /* No need to take the structure lock,
1342      * we are only looking for our parents */
1343
1344     if( !p_this->p_parent )
1345     {
1346         switch( i_type & VLC_VAR_TYPE )
1347         {
1348         case VLC_VAR_FILE:
1349         case VLC_VAR_DIRECTORY:
1350         case VLC_VAR_STRING:
1351         case VLC_VAR_MODULE:
1352             p_val->psz_string = config_GetPsz( p_this, psz_name );
1353             if( !p_val->psz_string ) p_val->psz_string = strdup("");
1354             break;
1355         case VLC_VAR_FLOAT:
1356             p_val->f_float = config_GetFloat( p_this, psz_name );
1357             break;
1358         case VLC_VAR_INTEGER:
1359         case VLC_VAR_HOTKEY:
1360             p_val->i_int = config_GetInt( p_this, psz_name );
1361             break;
1362         case VLC_VAR_BOOL:
1363             p_val->b_bool = config_GetInt( p_this, psz_name );
1364             break;
1365         case VLC_VAR_LIST:
1366         {
1367             char *psz_orig, *psz_var;
1368             vlc_list_t *p_list = malloc(sizeof(vlc_list_t));
1369             p_val->p_list = p_list;
1370             p_list->i_count = 0;
1371
1372             psz_var = psz_orig = config_GetPsz( p_this, psz_name );
1373             while( psz_var && *psz_var )
1374             {
1375                 char *psz_item = psz_var;
1376                 vlc_value_t val;
1377                 while( *psz_var && *psz_var != ',' ) psz_var++;
1378                 if( *psz_var == ',' )
1379                 {
1380                     *psz_var = '\0';
1381                     psz_var++;
1382                 }
1383                 val.i_int = strtol( psz_item, NULL, 0 );
1384                 INSERT_ELEM( p_list->p_values, p_list->i_count,
1385                              p_list->i_count, val );
1386                 /* p_list->i_count is incremented twice by INSERT_ELEM */
1387                 p_list->i_count--;
1388                 INSERT_ELEM( p_list->pi_types, p_list->i_count,
1389                              p_list->i_count, VLC_VAR_INTEGER );
1390             }
1391             if( psz_orig ) free( psz_orig );
1392             break;
1393         }
1394         default:
1395             return VLC_ENOOBJ;
1396             break;
1397         }
1398
1399         return VLC_SUCCESS;
1400     }
1401
1402     /* Look for the variable */
1403     vlc_mutex_lock( &p_this->p_parent->var_lock );
1404
1405     i_var = Lookup( p_this->p_parent->p_vars, p_this->p_parent->i_vars,
1406                     psz_name );
1407
1408     if( i_var >= 0 )
1409     {
1410         /* We found it! */
1411         p_var = &p_this->p_parent->p_vars[i_var];
1412
1413         /* Really get the variable */
1414         *p_val = p_var->val;
1415
1416         /* Duplicate value if needed */
1417         p_var->pf_dup( p_val );
1418
1419         vlc_mutex_unlock( &p_this->p_parent->var_lock );
1420         return VLC_SUCCESS;
1421     }
1422
1423     vlc_mutex_unlock( &p_this->p_parent->var_lock );
1424
1425     /* We're still not there */
1426
1427     return InheritValue( p_this->p_parent, psz_name, p_val, i_type );
1428 }