1 /*****************************************************************************
2 * variables.c: routines for object variables handling
3 *****************************************************************************
4 * Copyright (C) 2002 VideoLAN
5 * $Id: variables.c,v 1.33 2003/10/29 01:33:27 gbazin Exp $
7 * Authors: Samuel Hocevar <sam@zoy.org>
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.
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.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
30 # include <stdlib.h> /* realloc() */
33 /*****************************************************************************
35 *****************************************************************************/
36 struct callback_entry_t
38 vlc_callback_t pf_callback;
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 )
49 return v.i_time == w.i_time ? 0 : v.i_time > w.i_time ? 1 : -1;
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; }
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 ); }
61 static void DupList( vlc_value_t *p_val )
64 vlc_list_t *p_list = malloc( sizeof(vlc_list_t) );
66 if( p_val->p_list->i_count )
68 p_list->i_count = p_val->p_list->i_count;
69 p_list->p_values = malloc( p_list->i_count * sizeof(vlc_value_t) );
70 p_list->pi_types = malloc( p_list->i_count * sizeof(int) );
73 for( i = 0; i < p_list->i_count; i++ )
75 p_list->p_values[i] = p_val->p_list->p_values[i];
76 switch( p_val->p_list->pi_types[i] & VLC_VAR_TYPE )
80 DupString( &p_list->p_values[i] );
87 p_val->p_list = p_list;
90 static void FreeDummy( vlc_value_t *p_val ) { (void)p_val; /* unused */ }
91 static void FreeString( vlc_value_t *p_val ) { free( p_val->psz_string ); }
92 static void FreeMutex( vlc_value_t *p_val ) { vlc_mutex_destroy( (vlc_mutex_t*)p_val->p_address ); free( p_val->p_address ); }
94 static void FreeList( vlc_value_t *p_val )
97 for( i = 0; i < p_val->p_list->i_count; i++ )
99 switch( p_val->p_list->pi_types[i] & VLC_VAR_TYPE )
102 FreeString( &p_val->p_list->p_values[i] );
105 FreeMutex( &p_val->p_list->p_values[i] );
112 if( p_val->p_list->i_count )
114 free( p_val->p_list->p_values );
115 free( p_val->p_list->pi_types );
117 free( p_val->p_list );
120 /*****************************************************************************
122 *****************************************************************************/
123 static int GetUnused ( vlc_object_t *, const char * );
124 static uint32_t HashString ( const char * );
125 static int Insert ( variable_t *, int, const char * );
126 static int InsertInner ( variable_t *, int, uint32_t );
127 static int Lookup ( variable_t *, int, const char * );
128 static int LookupInner ( variable_t *, int, uint32_t );
130 static void CheckValue ( variable_t *, vlc_value_t * );
132 static int InheritValue( vlc_object_t *, const char *, vlc_value_t *,
136 * Initialize a vlc variable
138 * We hash the given string and insert it into the sorted list. The insertion
139 * may require slow memory copies, but think about what we gain in the log(n)
140 * lookup phase when setting/getting the variable value!
142 * \param p_this The object in which to create the variable
143 * \param psz_name The name of the variable
144 * \param i_type The variables type. Must be one of \ref var_type combined with
145 * zero or more \ref var_flags
147 int __var_Create( vlc_object_t *p_this, const char *psz_name, int i_type )
151 static vlc_list_t dummy_null_list = {0, NULL, NULL};
153 vlc_mutex_lock( &p_this->var_lock );
155 /* FIXME: if the variable already exists, we don't duplicate it. But we
156 * duplicate the lookups. It's not that serious, but if anyone finds some
157 * time to rework Insert() so that only one lookup has to be done, feel
159 i_new = Lookup( p_this->p_vars, p_this->i_vars, psz_name );
163 /* If the types differ, variable creation failed. */
164 if( (i_type & ~VLC_VAR_DOINHERIT) != p_this->p_vars[i_new].i_type )
166 vlc_mutex_unlock( &p_this->var_lock );
170 p_this->p_vars[i_new].i_usage++;
171 vlc_mutex_unlock( &p_this->var_lock );
175 i_new = Insert( p_this->p_vars, p_this->i_vars, psz_name );
177 if( (p_this->i_vars & 15) == 15 )
179 p_this->p_vars = realloc( p_this->p_vars,
180 (p_this->i_vars+17) * sizeof(variable_t) );
183 memmove( p_this->p_vars + i_new + 1,
184 p_this->p_vars + i_new,
185 (p_this->i_vars - i_new) * sizeof(variable_t) );
189 p_var = &p_this->p_vars[i_new];
191 p_var->i_hash = HashString( psz_name );
192 p_var->psz_name = strdup( psz_name );
193 p_var->psz_text = NULL;
195 p_var->i_type = i_type & ~VLC_VAR_DOINHERIT;
196 memset( &p_var->val, 0, sizeof(vlc_value_t) );
198 p_var->pf_dup = DupDummy;
199 p_var->pf_free = FreeDummy;
203 p_var->i_default = -1;
204 p_var->choices.i_count = 0;
205 p_var->choices.p_values = NULL;
206 p_var->choices_text.i_count = 0;
207 p_var->choices_text.p_values = NULL;
209 p_var->b_incallback = VLC_FALSE;
210 p_var->i_entries = 0;
211 p_var->p_entries = NULL;
213 /* Always initialize the variable, even if it is a list variable; this
214 * will lead to errors if the variable is not initialized, but it will
215 * not cause crashes in the variable handling. */
216 switch( i_type & VLC_VAR_TYPE )
219 p_var->pf_cmp = CmpBool;
220 p_var->val.b_bool = VLC_FALSE;
222 case VLC_VAR_INTEGER:
224 p_var->pf_cmp = CmpInt;
225 p_var->val.i_int = 0;
230 case VLC_VAR_DIRECTORY:
231 case VLC_VAR_VARIABLE:
232 p_var->pf_cmp = CmpString;
233 p_var->pf_dup = DupString;
234 p_var->pf_free = FreeString;
235 p_var->val.psz_string = "";
238 p_var->pf_cmp = CmpFloat;
239 p_var->val.f_float = 0.0;
242 p_var->pf_cmp = CmpTime;
243 p_var->val.i_time = 0;
245 case VLC_VAR_ADDRESS:
246 p_var->pf_cmp = CmpAddress;
247 p_var->val.p_address = NULL;
250 p_var->pf_cmp = CmpAddress;
251 p_var->pf_free = FreeMutex;
252 p_var->val.p_address = malloc( sizeof(vlc_mutex_t) );
253 vlc_mutex_init( p_this, (vlc_mutex_t*)p_var->val.p_address );
256 p_var->pf_cmp = CmpAddress;
257 p_var->pf_dup = DupList;
258 p_var->pf_free = FreeList;
259 p_var->val.p_list = &dummy_null_list;
263 /* Duplicate the default data we stored. */
264 p_var->pf_dup( &p_var->val );
266 if( i_type & VLC_VAR_DOINHERIT )
270 if( InheritValue( p_this, psz_name, &val, p_var->i_type )
273 /* Free data if needed */
274 p_var->pf_free( &p_var->val );
275 /* Set the variable */
278 if( i_type & VLC_VAR_HASCHOICE )
280 /* We must add the inherited value to our choice list */
281 p_var->i_default = 0;
283 INSERT_ELEM( p_var->choices.p_values, p_var->choices.i_count,
285 INSERT_ELEM( p_var->choices_text.p_values,
286 p_var->choices_text.i_count, 0, val );
287 p_var->pf_dup( &p_var->choices.p_values[0] );
288 p_var->choices_text.p_values[0].psz_string = NULL;
293 vlc_mutex_unlock( &p_this->var_lock );
299 * Destroy a vlc variable
301 * Look for the variable and destroy it if it is found. As in var_Create we
302 * do a call to memmove() but we have performance counterparts elsewhere.
304 * \param p_this The object that holds the variable
305 * \param psz_name The name of the variable
307 int __var_Destroy( vlc_object_t *p_this, const char *psz_name )
312 vlc_mutex_lock( &p_this->var_lock );
314 i_var = GetUnused( p_this, psz_name );
317 vlc_mutex_unlock( &p_this->var_lock );
321 p_var = &p_this->p_vars[i_var];
323 if( p_var->i_usage > 1 )
326 vlc_mutex_unlock( &p_this->var_lock );
330 /* Free value if needed */
331 p_var->pf_free( &p_var->val );
333 /* Free choice list if needed */
334 if( p_var->choices.i_count )
336 for( i = 0 ; i < p_var->choices.i_count ; i++ )
338 p_var->pf_free( &p_var->choices.p_values[i] );
339 if( p_var->choices_text.p_values[i].psz_string )
340 free( p_var->choices_text.p_values[i].psz_string );
342 free( p_var->choices.p_values );
343 free( p_var->choices_text.p_values );
346 /* Free callbacks if needed */
347 if( p_var->p_entries )
349 free( p_var->p_entries );
352 free( p_var->psz_name );
353 if( p_var->psz_text ) free( p_var->psz_text );
355 memmove( p_this->p_vars + i_var,
356 p_this->p_vars + i_var + 1,
357 (p_this->i_vars - i_var - 1) * sizeof(variable_t) );
359 if( (p_this->i_vars & 15) == 0 )
361 p_this->p_vars = realloc( p_this->p_vars,
362 (p_this->i_vars) * sizeof( variable_t ) );
367 vlc_mutex_unlock( &p_this->var_lock );
373 * Perform an action on a variable
375 * \param p_this The object that holds the variable
376 * \param psz_name The name of the variable
377 * \param i_action The action to perform. Must be one of \ref var_action
378 * \param p_val First action parameter
379 * \param p_val2 Second action parameter
381 int __var_Change( vlc_object_t *p_this, const char *psz_name,
382 int i_action, vlc_value_t *p_val, vlc_value_t *p_val2 )
388 vlc_mutex_lock( &p_this->var_lock );
390 i_var = Lookup( p_this->p_vars, p_this->i_vars, psz_name );
394 vlc_mutex_unlock( &p_this->var_lock );
398 p_var = &p_this->p_vars[i_var];
403 if( p_var->i_type & VLC_VAR_HASMIN )
405 p_var->pf_free( &p_var->min );
407 p_var->i_type |= VLC_VAR_HASMIN;
409 p_var->pf_dup( &p_var->min );
410 CheckValue( p_var, &p_var->val );
413 if( p_var->i_type & VLC_VAR_HASMAX )
415 p_var->pf_free( &p_var->max );
417 p_var->i_type |= VLC_VAR_HASMAX;
419 p_var->pf_dup( &p_var->max );
420 CheckValue( p_var, &p_var->val );
422 case VLC_VAR_SETSTEP:
423 if( p_var->i_type & VLC_VAR_HASSTEP )
425 p_var->pf_free( &p_var->step );
427 p_var->i_type |= VLC_VAR_HASSTEP;
428 p_var->step = *p_val;
429 p_var->pf_dup( &p_var->step );
430 CheckValue( p_var, &p_var->val );
432 case VLC_VAR_ADDCHOICE:
433 /* FIXME: the list is sorted, dude. Use something cleverer. */
434 for( i = p_var->choices.i_count ; i-- ; )
436 if( p_var->pf_cmp( p_var->choices.p_values[i], *p_val ) < 0 )
442 /* The new place is i+1 */
445 if( p_var->i_default >= i )
450 INSERT_ELEM( p_var->choices.p_values, p_var->choices.i_count,
452 INSERT_ELEM( p_var->choices_text.p_values,
453 p_var->choices_text.i_count, i, *p_val );
454 p_var->pf_dup( &p_var->choices.p_values[i] );
455 p_var->choices_text.p_values[i].psz_string =
456 ( p_val2 && p_val2->psz_string ) ?
457 strdup( p_val2->psz_string ) : NULL;
459 CheckValue( p_var, &p_var->val );
461 case VLC_VAR_DELCHOICE:
462 /* FIXME: the list is sorted, dude. Use something cleverer. */
463 for( i = 0 ; i < p_var->choices.i_count ; i++ )
465 if( p_var->pf_cmp( p_var->choices.p_values[i], *p_val ) == 0 )
471 if( i == p_var->choices.i_count )
474 vlc_mutex_unlock( &p_this->var_lock );
478 if( p_var->i_default > i )
482 else if( p_var->i_default == i )
484 p_var->i_default = -1;
487 p_var->pf_free( &p_var->choices.p_values[i] );
488 if( p_var->choices_text.p_values[i].psz_string )
489 free( p_var->choices_text.p_values[i].psz_string );
490 REMOVE_ELEM( p_var->choices.p_values, p_var->choices.i_count, i );
491 REMOVE_ELEM( p_var->choices_text.p_values,
492 p_var->choices_text.i_count, i );
494 CheckValue( p_var, &p_var->val );
496 case VLC_VAR_CHOICESCOUNT:
497 p_val->i_int = p_var->choices.i_count;
499 case VLC_VAR_CLEARCHOICES:
500 for( i = 0 ; i < p_var->choices.i_count ; i++ )
502 p_var->pf_free( &p_var->choices.p_values[i] );
504 if( p_var->choices.i_count )
505 free( p_var->choices.p_values );
507 p_var->choices.i_count = 0;
508 p_var->choices.p_values = NULL;
509 p_var->i_default = -1;
511 case VLC_VAR_SETDEFAULT:
512 /* FIXME: the list is sorted, dude. Use something cleverer. */
513 for( i = 0 ; i < p_var->choices.i_count ; i++ )
515 if( p_var->pf_cmp( p_var->choices.p_values[i], *p_val ) == 0 )
521 if( i == p_var->choices.i_count )
527 p_var->i_default = i;
528 CheckValue( p_var, &p_var->val );
530 case VLC_VAR_SETVALUE:
531 /* Duplicate data if needed */
532 p_var->pf_dup( p_val );
533 /* Backup needed stuff */
535 /* Check boundaries and list */
536 CheckValue( p_var, p_val );
537 /* Set the variable */
539 /* Free data if needed */
540 p_var->pf_free( &oldval );
542 case VLC_VAR_GETCHOICES:
543 case VLC_VAR_GETLIST:
544 p_val->p_list = malloc( sizeof(vlc_list_t) );
545 if( p_val2 ) p_val2->p_list = malloc( sizeof(vlc_list_t) );
546 if( p_var->choices.i_count )
548 p_val->p_list->p_values = malloc( p_var->choices.i_count
549 * sizeof(vlc_value_t) );
550 p_val->p_list->pi_types = malloc( p_var->choices.i_count
554 p_val2->p_list->p_values =
555 malloc( p_var->choices.i_count * sizeof(vlc_value_t) );
556 p_val2->p_list->pi_types =
557 malloc( p_var->choices.i_count * sizeof(int) );
560 p_val->p_list->i_count = p_var->choices.i_count;
561 if( p_val2 ) p_val2->p_list->i_count = p_var->choices.i_count;
562 for( i = 0 ; i < p_var->choices.i_count ; i++ )
564 p_val->p_list->p_values[i] = p_var->choices.p_values[i];
565 p_val->p_list->pi_types[i] = p_var->i_type;
566 p_var->pf_dup( &p_val->p_list->p_values[i] );
569 p_val2->p_list->p_values[i].psz_string =
570 p_var->choices_text.p_values[i].psz_string ?
571 strdup(p_var->choices_text.p_values[i].psz_string) : NULL;
572 p_val2->p_list->pi_types[i] = VLC_VAR_STRING;
576 case VLC_VAR_FREELIST:
579 case VLC_VAR_SETTEXT:
580 if( p_var->psz_text ) free( p_var->psz_text );
581 if( p_val && p_val->psz_string )
582 p_var->psz_text = strdup( p_val->psz_string );
584 case VLC_VAR_GETTEXT:
585 p_val->psz_string = NULL;
586 if( p_var->psz_text )
588 p_val->psz_string = strdup( p_var->psz_text );
591 case VLC_VAR_INHERITVALUE:
595 if( InheritValue( p_this, psz_name, &val, p_var->i_type )
598 /* Duplicate already done */
600 /* Backup needed stuff */
602 /* Check boundaries and list */
603 CheckValue( p_var, &val );
604 /* Set the variable */
606 /* Free data if needed */
607 p_var->pf_free( &oldval );
613 p_var->pf_dup( p_val );
622 vlc_mutex_unlock( &p_this->var_lock );
628 * Request a variable's type
630 * \return The variable type if it exists, or 0 if the
631 * variable could not be found.
634 int __var_Type( vlc_object_t *p_this, const char *psz_name )
638 vlc_mutex_lock( &p_this->var_lock );
640 i_var = Lookup( p_this->p_vars, p_this->i_vars, psz_name );
644 vlc_mutex_unlock( &p_this->var_lock );
648 i_type = p_this->p_vars[i_var].i_type;
650 vlc_mutex_unlock( &p_this->var_lock );
656 * Set a variable's value
658 * \param p_this The object that hold the variable
659 * \param psz_name The name of the variable
660 * \param val the value to set
662 int __var_Set( vlc_object_t *p_this, const char *psz_name, vlc_value_t val )
668 vlc_mutex_lock( &p_this->var_lock );
670 i_var = GetUnused( p_this, psz_name );
673 vlc_mutex_unlock( &p_this->var_lock );
677 p_var = &p_this->p_vars[i_var];
679 /* Duplicate data if needed */
680 p_var->pf_dup( &val );
682 /* Backup needed stuff */
685 /* Check boundaries and list */
686 CheckValue( p_var, &val );
688 /* Set the variable */
691 /* Deal with callbacks. Tell we're in a callback, release the lock,
692 * call stored functions, retake the lock. */
693 if( p_var->i_entries )
696 int i_entries = p_var->i_entries;
697 callback_entry_t *p_entries = p_var->p_entries;
699 p_var->b_incallback = VLC_TRUE;
700 vlc_mutex_unlock( &p_this->var_lock );
703 for( ; i_entries-- ; )
705 p_entries[i_entries].pf_callback( p_this, psz_name, oldval, val,
706 p_entries[i_entries].p_data );
709 vlc_mutex_lock( &p_this->var_lock );
711 i_var = Lookup( p_this->p_vars, p_this->i_vars, psz_name );
714 msg_Err( p_this, "variable %s has disappeared", psz_name );
715 vlc_mutex_unlock( &p_this->var_lock );
719 p_var = &p_this->p_vars[i_var];
720 p_var->b_incallback = VLC_FALSE;
723 /* Free data if needed */
724 p_var->pf_free( &oldval );
726 vlc_mutex_unlock( &p_this->var_lock );
732 * Get a variable's value
734 * \param p_this The object that holds the variable
735 * \param psz_name The name of the variable
736 * \param p_val Pointer to a vlc_value_t that will hold the variable's value
737 * after the function is finished
739 int __var_Get( vlc_object_t *p_this, const char *psz_name, vlc_value_t *p_val )
744 vlc_mutex_lock( &p_this->var_lock );
746 i_var = Lookup( p_this->p_vars, p_this->i_vars, psz_name );
750 vlc_mutex_unlock( &p_this->var_lock );
754 p_var = &p_this->p_vars[i_var];
756 /* Really get the variable */
759 /* Duplicate value if needed */
760 p_var->pf_dup( p_val );
762 vlc_mutex_unlock( &p_this->var_lock );
768 * Register a callback in a variable
770 * We store a function pointer that will be called upon variable
773 * \param p_this The object that holds the variable
774 * \param psz_name The name of the variable
775 * \param pf_callback The function pointer
776 * \param p_data A generic pointer that will be passed as the last
777 * argument to the callback function.
779 * \warning The callback function is run in the thread that calls var_Set on
780 * the variable. Use proper locking. This thread may not have much
781 * time to spare, so keep callback functions short.
783 int __var_AddCallback( vlc_object_t *p_this, const char *psz_name,
784 vlc_callback_t pf_callback, void *p_data )
788 callback_entry_t entry;
790 entry.pf_callback = pf_callback;
791 entry.p_data = p_data;
793 vlc_mutex_lock( &p_this->var_lock );
795 i_var = GetUnused( p_this, psz_name );
798 vlc_mutex_unlock( &p_this->var_lock );
802 p_var = &p_this->p_vars[i_var];
804 INSERT_ELEM( p_var->p_entries,
809 vlc_mutex_unlock( &p_this->var_lock );
815 * Remove a callback from a variable
817 * pf_callback and p_data have to be given again, because different objects
818 * might have registered the same callback function.
820 int __var_DelCallback( vlc_object_t *p_this, const char *psz_name,
821 vlc_callback_t pf_callback, void *p_data )
826 vlc_mutex_lock( &p_this->var_lock );
828 i_var = GetUnused( p_this, psz_name );
831 vlc_mutex_unlock( &p_this->var_lock );
835 p_var = &p_this->p_vars[i_var];
837 for( i_entry = p_var->i_entries ; i_entry-- ; )
839 if( p_var->p_entries[i_entry].pf_callback == pf_callback
840 && p_var->p_entries[i_entry].p_data == p_data )
848 vlc_mutex_unlock( &p_this->var_lock );
852 REMOVE_ELEM( p_var->p_entries, p_var->i_entries, i_entry );
854 vlc_mutex_unlock( &p_this->var_lock );
859 /* Following functions are local */
861 /*****************************************************************************
862 * GetUnused: find an unused variable from its name
863 *****************************************************************************
864 * We do i_tries tries before giving up, just in case the variable is being
865 * modified and called from a callback.
866 *****************************************************************************/
867 static int GetUnused( vlc_object_t *p_this, const char *psz_name )
869 int i_var, i_tries = 0;
873 i_var = Lookup( p_this->p_vars, p_this->i_vars, psz_name );
879 if( ! p_this->p_vars[i_var].b_incallback )
884 if( i_tries++ > 100 )
886 msg_Err( p_this, "caught in a callback deadlock?" );
890 vlc_mutex_unlock( &p_this->var_lock );
891 msleep( THREAD_SLEEP );
892 vlc_mutex_lock( &p_this->var_lock );
896 /*****************************************************************************
897 * HashString: our cool hash function
898 *****************************************************************************
899 * This function is not intended to be crypto-secure, we only want it to be
900 * fast and not suck too much. This one is pretty fast and did 0 collisions
901 * in wenglish's dictionary.
902 *****************************************************************************/
903 static uint32_t HashString( const char *psz_string )
909 i_hash += *psz_string++;
910 i_hash += i_hash << 10;
911 i_hash ^= i_hash >> 8;
917 /*****************************************************************************
918 * Insert: find an empty slot to insert a new variable
919 *****************************************************************************
920 * We use a recursive inner function indexed on the hash. This function does
921 * nothing in the rare cases where a collision may occur, see Lookup()
922 * to see how we handle them.
923 * XXX: does this really need to be written recursively?
924 *****************************************************************************/
925 static int Insert( variable_t *p_vars, int i_count, const char *psz_name )
932 return InsertInner( p_vars, i_count, HashString( psz_name ) );
935 static int InsertInner( variable_t *p_vars, int i_count, uint32_t i_hash )
939 if( i_hash <= p_vars[0].i_hash )
944 if( i_hash >= p_vars[i_count - 1].i_hash )
949 i_middle = i_count / 2;
951 /* We know that 0 < i_middle */
952 if( i_hash < p_vars[i_middle].i_hash )
954 return InsertInner( p_vars, i_middle, i_hash );
957 /* We know that i_middle + 1 < i_count */
958 if( i_hash > p_vars[i_middle + 1].i_hash )
960 return i_middle + 1 + InsertInner( p_vars + i_middle + 1,
961 i_count - i_middle - 1,
968 /*****************************************************************************
969 * Lookup: find an existing variable given its name
970 *****************************************************************************
971 * We use a recursive inner function indexed on the hash. Care is taken of
972 * possible hash collisions.
973 * XXX: does this really need to be written recursively?
974 *****************************************************************************/
975 static int Lookup( variable_t *p_vars, int i_count, const char *psz_name )
985 i_hash = HashString( psz_name );
987 i_pos = LookupInner( p_vars, i_count, i_hash );
990 if( i_hash != p_vars[i_pos].i_hash )
995 /* Hash found, entry found */
996 if( !strcmp( psz_name, p_vars[i_pos].psz_name ) )
1001 /* Hash collision! This should be very rare, but we cannot guarantee
1002 * it will never happen. Just do an exhaustive search amongst all
1003 * entries with the same hash. */
1004 for( i = i_pos - 1 ; i > 0 && i_hash == p_vars[i].i_hash ; i-- )
1006 if( !strcmp( psz_name, p_vars[i].psz_name ) )
1012 for( i = i_pos + 1 ; i < i_count && i_hash == p_vars[i].i_hash ; i++ )
1014 if( !strcmp( psz_name, p_vars[i].psz_name ) )
1020 /* Hash found, but entry not found */
1024 static int LookupInner( variable_t *p_vars, int i_count, uint32_t i_hash )
1028 if( i_hash <= p_vars[0].i_hash )
1033 if( i_hash >= p_vars[i_count-1].i_hash )
1038 i_middle = i_count / 2;
1040 /* We know that 0 < i_middle */
1041 if( i_hash < p_vars[i_middle].i_hash )
1043 return LookupInner( p_vars, i_middle, i_hash );
1046 /* We know that i_middle + 1 < i_count */
1047 if( i_hash > p_vars[i_middle].i_hash )
1049 return i_middle + LookupInner( p_vars + i_middle,
1057 /*****************************************************************************
1058 * CheckValue: check that a value is valid wrt. a variable
1059 *****************************************************************************
1060 * This function checks p_val's value against p_var's limitations such as
1061 * minimal and maximal value, step, in-list position, and modifies p_val if
1063 *****************************************************************************/
1064 static void CheckValue ( variable_t *p_var, vlc_value_t *p_val )
1066 /* Check that our variable is in the list */
1067 if( p_var->i_type & VLC_VAR_HASCHOICE && p_var->choices.i_count )
1071 /* FIXME: the list is sorted, dude. Use something cleverer. */
1072 for( i = p_var->choices.i_count ; i-- ; )
1074 if( p_var->pf_cmp( *p_val, p_var->choices.p_values[i] ) == 0 )
1080 /* If not found, change it to anything vaguely valid */
1083 /* Free the old variable, get the new one, dup it */
1084 p_var->pf_free( p_val );
1085 *p_val = p_var->choices.p_values[p_var->i_default >= 0
1086 ? p_var->i_default : 0 ];
1087 p_var->pf_dup( p_val );
1091 /* Check that our variable is within the bounds */
1092 switch( p_var->i_type & VLC_VAR_TYPE )
1094 case VLC_VAR_INTEGER:
1095 if( p_var->i_type & VLC_VAR_HASSTEP && p_var->step.i_int
1096 && (p_val->i_int % p_var->step.i_int) )
1098 p_val->i_int = (p_val->i_int + (p_var->step.i_int / 2))
1099 / p_var->step.i_int * p_var->step.i_int;
1101 if( p_var->i_type & VLC_VAR_HASMIN
1102 && p_val->i_int < p_var->min.i_int )
1104 p_val->i_int = p_var->min.i_int;
1106 if( p_var->i_type & VLC_VAR_HASMAX
1107 && p_val->i_int > p_var->max.i_int )
1109 p_val->i_int = p_var->max.i_int;
1113 if( p_var->i_type & VLC_VAR_HASSTEP && p_var->step.f_float )
1115 float f_round = p_var->step.f_float * (float)(int)( 0.5 +
1116 p_val->f_float / p_var->step.f_float );
1117 if( p_val->f_float != f_round )
1119 p_val->f_float = f_round;
1122 if( p_var->i_type & VLC_VAR_HASMIN
1123 && p_val->f_float < p_var->min.f_float )
1125 p_val->f_float = p_var->min.f_float;
1127 if( p_var->i_type & VLC_VAR_HASMAX
1128 && p_val->f_float > p_var->max.f_float )
1130 p_val->f_float = p_var->max.f_float;
1139 /*****************************************************************************
1140 * InheritValue: try to inherit the value of this variable from the same one
1141 * in our closest parent.
1142 *****************************************************************************/
1143 static int InheritValue( vlc_object_t *p_this, const char *psz_name,
1144 vlc_value_t *p_val, int i_type )
1149 /* No need to take the structure lock,
1150 * we are only looking for our parents */
1152 if( !p_this->p_parent )
1154 switch( i_type & VLC_VAR_TYPE )
1157 case VLC_VAR_DIRECTORY:
1158 case VLC_VAR_STRING:
1159 p_val->psz_string = config_GetPsz( p_this, psz_name );
1160 if( !p_val->psz_string ) p_val->psz_string = strdup("");
1163 p_val->f_float = config_GetFloat( p_this, psz_name );
1165 case VLC_VAR_INTEGER:
1166 case VLC_VAR_HOTKEY:
1167 p_val->i_int = config_GetInt( p_this, psz_name );
1170 p_val->b_bool = config_GetInt( p_this, psz_name );
1180 /* Look for the variable */
1181 vlc_mutex_lock( &p_this->p_parent->var_lock );
1183 i_var = Lookup( p_this->p_parent->p_vars, p_this->p_parent->i_vars,
1189 p_var = &p_this->p_parent->p_vars[i_var];
1191 /* Really get the variable */
1192 *p_val = p_var->val;
1194 /* Duplicate value if needed */
1195 p_var->pf_dup( p_val );
1197 vlc_mutex_unlock( &p_this->p_parent->var_lock );
1201 vlc_mutex_unlock( &p_this->p_parent->var_lock );
1203 /* We're still not there */
1205 return InheritValue( p_this->p_parent, psz_name, p_val, i_type );