]> git.sesse.net Git - vlc/blob - src/misc/variables.c
* Split download function in find/download
[vlc] / src / misc / variables.c
1 /*****************************************************************************
2  * variables.c: routines for object variables handling
3  *****************************************************************************
4  * Copyright (C) 2002-2006 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, *psz_value = strchr( psz_option, '=' );
932     int  i_name_len, i_type;
933     vlc_bool_t b_isno = VLC_FALSE;
934     vlc_value_t val;
935
936     if( psz_value ) i_name_len = psz_value - psz_option;
937     else i_name_len = strlen( psz_option );
938
939     /* It's too much of an hassle to remove the ':' when we parse
940      * the cmd line :) */
941     if( i_name_len && *psz_option == ':' )
942     {
943         psz_option++;
944         i_name_len--;
945     }
946
947     if( i_name_len == 0 ) return;
948
949     psz_name = strndup( psz_option, i_name_len );
950     if( psz_value ) psz_value++;
951
952     /* FIXME: :programs should be handled generically */
953     if( !strcmp( psz_name, "programs" ) )
954         i_type = VLC_VAR_LIST;
955     else
956         i_type = config_GetType( p_obj, psz_name );
957
958     if( !i_type && !psz_value )
959     {
960         /* check for "no-foo" or "nofoo" */
961         if( !strncmp( psz_name, "no-", 3 ) )
962         {
963             memmove( psz_name, psz_name + 3, strlen(psz_name) + 1 - 3 );
964         }
965         else if( !strncmp( psz_name, "no", 2 ) )
966         {
967             memmove( psz_name, psz_name + 2, strlen(psz_name) + 1 - 2 );
968         }
969         else goto cleanup;           /* Option doesn't exist */
970
971         b_isno = VLC_TRUE;
972         i_type = config_GetType( p_obj, psz_name );
973
974         if( !i_type ) goto cleanup;  /* Option doesn't exist */
975     }
976     else if( !i_type ) goto cleanup; /* Option doesn't exist */
977
978     if( ( i_type != VLC_VAR_BOOL ) &&
979         ( !psz_value || !*psz_value ) ) goto cleanup; /* Invalid value */
980
981     /* Create the variable in the input object.
982      * Children of the input object will be able to retreive this value
983      * thanks to the inheritance property of the object variables. */
984     var_Create( p_obj, psz_name, i_type );
985
986     switch( i_type )
987     {
988     case VLC_VAR_BOOL:
989         val.b_bool = !b_isno;
990         break;
991
992     case VLC_VAR_INTEGER:
993         val.i_int = strtol( psz_value, NULL, 0 );
994         break;
995
996     case VLC_VAR_FLOAT:
997         val.f_float = atof( psz_value );
998         break;
999
1000     case VLC_VAR_STRING:
1001     case VLC_VAR_MODULE:
1002     case VLC_VAR_FILE:
1003     case VLC_VAR_DIRECTORY:
1004         val.psz_string = psz_value;
1005         break;
1006
1007     case VLC_VAR_LIST:
1008     {
1009         char *psz_orig, *psz_var;
1010         vlc_list_t *p_list = malloc(sizeof(vlc_list_t));
1011         val.p_list = p_list;
1012         p_list->i_count = 0;
1013
1014         psz_var = psz_orig = strdup(psz_value);
1015         while( psz_var && *psz_var )
1016         {
1017             char *psz_item = psz_var;
1018             vlc_value_t val2;
1019             while( *psz_var && *psz_var != ',' ) psz_var++;
1020             if( *psz_var == ',' )
1021             {
1022                 *psz_var = '\0';
1023                 psz_var++;
1024             }
1025             val2.i_int = strtol( psz_item, NULL, 0 );
1026             INSERT_ELEM( p_list->p_values, p_list->i_count,
1027                          p_list->i_count, val2 );
1028             /* p_list->i_count is incremented twice by INSERT_ELEM */
1029             p_list->i_count--;
1030             INSERT_ELEM( p_list->pi_types, p_list->i_count,
1031                          p_list->i_count, VLC_VAR_INTEGER );
1032         }
1033         if( psz_orig ) free( psz_orig );
1034         break;
1035     }
1036
1037     default:
1038         goto cleanup;
1039         break;
1040     }
1041
1042     var_Set( p_obj, psz_name, val );
1043
1044   cleanup:
1045     if( psz_name ) free( psz_name );
1046     return;
1047 }
1048
1049
1050 /* Following functions are local */
1051
1052 /*****************************************************************************
1053  * GetUnused: find an unused variable from its name
1054  *****************************************************************************
1055  * We do i_tries tries before giving up, just in case the variable is being
1056  * modified and called from a callback.
1057  *****************************************************************************/
1058 static int GetUnused( vlc_object_t *p_this, const char *psz_name )
1059 {
1060     int i_var, i_tries = 0;
1061
1062     while( VLC_TRUE )
1063     {
1064         i_var = Lookup( p_this->p_vars, p_this->i_vars, psz_name );
1065         if( i_var < 0 )
1066         {
1067             return VLC_ENOVAR;
1068         }
1069
1070         if( ! p_this->p_vars[i_var].b_incallback )
1071         {
1072             return i_var;
1073         }
1074
1075         if( i_tries++ > 100 )
1076         {
1077             msg_Err( p_this, "caught in a callback deadlock?" );
1078             return VLC_ETIMEOUT;
1079         }
1080
1081         vlc_mutex_unlock( &p_this->var_lock );
1082         msleep( THREAD_SLEEP );
1083         vlc_mutex_lock( &p_this->var_lock );
1084     }
1085 }
1086
1087 /*****************************************************************************
1088  * HashString: our cool hash function
1089  *****************************************************************************
1090  * This function is not intended to be crypto-secure, we only want it to be
1091  * fast and not suck too much. This one is pretty fast and did 0 collisions
1092  * in wenglish's dictionary.
1093  *****************************************************************************/
1094 static uint32_t HashString( const char *psz_string )
1095 {
1096     uint32_t i_hash = 0;
1097
1098     while( *psz_string )
1099     {
1100         i_hash += *psz_string++;
1101         i_hash += i_hash << 10;
1102         i_hash ^= i_hash >> 8;
1103     }
1104
1105     return i_hash;
1106 }
1107
1108 /*****************************************************************************
1109  * Insert: find an empty slot to insert a new variable
1110  *****************************************************************************
1111  * We use a recursive inner function indexed on the hash. This function does
1112  * nothing in the rare cases where a collision may occur, see Lookup()
1113  * to see how we handle them.
1114  * XXX: does this really need to be written recursively?
1115  *****************************************************************************/
1116 static int Insert( variable_t *p_vars, int i_count, const char *psz_name )
1117 {
1118     if( i_count == 0 )
1119     {
1120         return 0;
1121     }
1122
1123     return InsertInner( p_vars, i_count, HashString( psz_name ) );
1124 }
1125
1126 static int InsertInner( variable_t *p_vars, int i_count, uint32_t i_hash )
1127 {
1128     int i_middle;
1129
1130     if( i_hash <= p_vars[0].i_hash )
1131     {
1132         return 0;
1133     }
1134
1135     if( i_hash >= p_vars[i_count - 1].i_hash )
1136     {
1137         return i_count;
1138     }
1139
1140     i_middle = i_count / 2;
1141
1142     /* We know that 0 < i_middle */
1143     if( i_hash < p_vars[i_middle].i_hash )
1144     {
1145         return InsertInner( p_vars, i_middle, i_hash );
1146     }
1147
1148     /* We know that i_middle + 1 < i_count */
1149     if( i_hash > p_vars[i_middle + 1].i_hash )
1150     {
1151         return i_middle + 1 + InsertInner( p_vars + i_middle + 1,
1152                                            i_count - i_middle - 1,
1153                                            i_hash );
1154     }
1155
1156     return i_middle + 1;
1157 }
1158
1159 /*****************************************************************************
1160  * Lookup: find an existing variable given its name
1161  *****************************************************************************
1162  * We use a recursive inner function indexed on the hash. Care is taken of
1163  * possible hash collisions.
1164  * XXX: does this really need to be written recursively?
1165  *****************************************************************************/
1166 static int Lookup( variable_t *p_vars, int i_count, const char *psz_name )
1167 {
1168     uint32_t i_hash;
1169     int i, i_pos;
1170
1171     if( i_count == 0 )
1172     {
1173         return -1;
1174     }
1175
1176     i_hash = HashString( psz_name );
1177
1178     i_pos = LookupInner( p_vars, i_count, i_hash );
1179
1180     /* Hash not found */
1181     if( i_hash != p_vars[i_pos].i_hash )
1182     {
1183         return -1;
1184     }
1185
1186     /* Hash found, entry found */
1187     if( !strcmp( psz_name, p_vars[i_pos].psz_name ) )
1188     {
1189         return i_pos;
1190     }
1191
1192     /* Hash collision! This should be very rare, but we cannot guarantee
1193      * it will never happen. Just do an exhaustive search amongst all
1194      * entries with the same hash. */
1195     for( i = i_pos - 1 ; i > 0 && i_hash == p_vars[i].i_hash ; i-- )
1196     {
1197         if( !strcmp( psz_name, p_vars[i].psz_name ) )
1198         {
1199             return i;
1200         }
1201     }
1202
1203     for( i = i_pos + 1 ; i < i_count && i_hash == p_vars[i].i_hash ; i++ )
1204     {
1205         if( !strcmp( psz_name, p_vars[i].psz_name ) )
1206         {
1207             return i;
1208         }
1209     }
1210
1211     /* Hash found, but entry not found */
1212     return -1;
1213 }
1214
1215 static int LookupInner( variable_t *p_vars, int i_count, uint32_t i_hash )
1216 {
1217     int i_middle;
1218
1219     if( i_hash <= p_vars[0].i_hash )
1220     {
1221         return 0;
1222     }
1223
1224     if( i_hash >= p_vars[i_count-1].i_hash )
1225     {
1226         return i_count - 1;
1227     }
1228
1229     i_middle = i_count / 2;
1230
1231     /* We know that 0 < i_middle */
1232     if( i_hash < p_vars[i_middle].i_hash )
1233     {
1234         return LookupInner( p_vars, i_middle, i_hash );
1235     }
1236
1237     /* We know that i_middle + 1 < i_count */
1238     if( i_hash > p_vars[i_middle].i_hash )
1239     {
1240         return i_middle + LookupInner( p_vars + i_middle,
1241                                        i_count - i_middle,
1242                                        i_hash );
1243     }
1244
1245     return i_middle;
1246 }
1247
1248 /*****************************************************************************
1249  * CheckValue: check that a value is valid wrt. a variable
1250  *****************************************************************************
1251  * This function checks p_val's value against p_var's limitations such as
1252  * minimal and maximal value, step, in-list position, and modifies p_val if
1253  * necessary.
1254  ****************************************************************************/
1255 static void CheckValue ( variable_t *p_var, vlc_value_t *p_val )
1256 {
1257     /* Check that our variable is in the list */
1258     if( p_var->i_type & VLC_VAR_HASCHOICE && p_var->choices.i_count )
1259     {
1260         int i;
1261
1262         /* FIXME: the list is sorted, dude. Use something cleverer. */
1263         for( i = p_var->choices.i_count ; i-- ; )
1264         {
1265             if( p_var->pf_cmp( *p_val, p_var->choices.p_values[i] ) == 0 )
1266             {
1267                 break;
1268             }
1269         }
1270
1271         /* If not found, change it to anything vaguely valid */
1272         if( i < 0 )
1273         {
1274             /* Free the old variable, get the new one, dup it */
1275             p_var->pf_free( p_val );
1276             *p_val = p_var->choices.p_values[p_var->i_default >= 0
1277                                           ? p_var->i_default : 0 ];
1278             p_var->pf_dup( p_val );
1279         }
1280     }
1281
1282     /* Check that our variable is within the bounds */
1283     switch( p_var->i_type & VLC_VAR_TYPE )
1284     {
1285         case VLC_VAR_INTEGER:
1286             if( p_var->i_type & VLC_VAR_HASSTEP && p_var->step.i_int
1287                  && (p_val->i_int % p_var->step.i_int) )
1288             {
1289                 p_val->i_int = (p_val->i_int + (p_var->step.i_int / 2))
1290                                / p_var->step.i_int * p_var->step.i_int;
1291             }
1292             if( p_var->i_type & VLC_VAR_HASMIN
1293                  && p_val->i_int < p_var->min.i_int )
1294             {
1295                 p_val->i_int = p_var->min.i_int;
1296             }
1297             if( p_var->i_type & VLC_VAR_HASMAX
1298                  && p_val->i_int > p_var->max.i_int )
1299             {
1300                 p_val->i_int = p_var->max.i_int;
1301             }
1302             break;
1303         case VLC_VAR_FLOAT:
1304             if( p_var->i_type & VLC_VAR_HASSTEP && p_var->step.f_float )
1305             {
1306                 float f_round = p_var->step.f_float * (float)(int)( 0.5 +
1307                                         p_val->f_float / p_var->step.f_float );
1308                 if( p_val->f_float != f_round )
1309                 {
1310                     p_val->f_float = f_round;
1311                 }
1312             }
1313             if( p_var->i_type & VLC_VAR_HASMIN
1314                  && p_val->f_float < p_var->min.f_float )
1315             {
1316                 p_val->f_float = p_var->min.f_float;
1317             }
1318             if( p_var->i_type & VLC_VAR_HASMAX
1319                  && p_val->f_float > p_var->max.f_float )
1320             {
1321                 p_val->f_float = p_var->max.f_float;
1322             }
1323             break;
1324         case VLC_VAR_TIME:
1325             /* FIXME: TODO */
1326             break;
1327     }
1328 }
1329
1330 /*****************************************************************************
1331  * InheritValue: try to inherit the value of this variable from the same one
1332  *               in our closest parent.
1333  *****************************************************************************/
1334 static int InheritValue( vlc_object_t *p_this, const char *psz_name,
1335                          vlc_value_t *p_val, int i_type )
1336 {
1337     int i_var;
1338     variable_t *p_var;
1339
1340     /* No need to take the structure lock,
1341      * we are only looking for our parents */
1342
1343     if( !p_this->p_parent )
1344     {
1345         switch( i_type & VLC_VAR_TYPE )
1346         {
1347         case VLC_VAR_FILE:
1348         case VLC_VAR_DIRECTORY:
1349         case VLC_VAR_STRING:
1350         case VLC_VAR_MODULE:
1351             p_val->psz_string = config_GetPsz( p_this, psz_name );
1352             if( !p_val->psz_string ) p_val->psz_string = strdup("");
1353             break;
1354         case VLC_VAR_FLOAT:
1355             p_val->f_float = config_GetFloat( p_this, psz_name );
1356             break;
1357         case VLC_VAR_INTEGER:
1358         case VLC_VAR_HOTKEY:
1359             p_val->i_int = config_GetInt( p_this, psz_name );
1360             break;
1361         case VLC_VAR_BOOL:
1362             p_val->b_bool = config_GetInt( p_this, psz_name );
1363             break;
1364         case VLC_VAR_LIST:
1365         {
1366             char *psz_orig, *psz_var;
1367             vlc_list_t *p_list = malloc(sizeof(vlc_list_t));
1368             p_val->p_list = p_list;
1369             p_list->i_count = 0;
1370
1371             psz_var = psz_orig = config_GetPsz( p_this, psz_name );
1372             while( psz_var && *psz_var )
1373             {
1374                 char *psz_item = psz_var;
1375                 vlc_value_t val;
1376                 while( *psz_var && *psz_var != ',' ) psz_var++;
1377                 if( *psz_var == ',' )
1378                 {
1379                     *psz_var = '\0';
1380                     psz_var++;
1381                 }
1382                 val.i_int = strtol( psz_item, NULL, 0 );
1383                 INSERT_ELEM( p_list->p_values, p_list->i_count,
1384                              p_list->i_count, val );
1385                 /* p_list->i_count is incremented twice by INSERT_ELEM */
1386                 p_list->i_count--;
1387                 INSERT_ELEM( p_list->pi_types, p_list->i_count,
1388                              p_list->i_count, VLC_VAR_INTEGER );
1389             }
1390             if( psz_orig ) free( psz_orig );
1391             break;
1392         }
1393         default:
1394             return VLC_ENOOBJ;
1395             break;
1396         }
1397
1398         return VLC_SUCCESS;
1399     }
1400
1401     /* Look for the variable */
1402     vlc_mutex_lock( &p_this->p_parent->var_lock );
1403
1404     i_var = Lookup( p_this->p_parent->p_vars, p_this->p_parent->i_vars,
1405                     psz_name );
1406
1407     if( i_var >= 0 )
1408     {
1409         /* We found it! */
1410         p_var = &p_this->p_parent->p_vars[i_var];
1411
1412         /* Really get the variable */
1413         *p_val = p_var->val;
1414
1415         /* Duplicate value if needed */
1416         p_var->pf_dup( p_val );
1417
1418         vlc_mutex_unlock( &p_this->p_parent->var_lock );
1419         return VLC_SUCCESS;
1420     }
1421
1422     vlc_mutex_unlock( &p_this->p_parent->var_lock );
1423
1424     /* We're still not there */
1425
1426     return InheritValue( p_this->p_parent, psz_name, p_val, i_type );
1427 }