1 /*****************************************************************************
2 * variables.c: routines for object variables handling
3 *****************************************************************************
4 * Copyright (C) 2002 VideoLAN
5 * $Id: variables.c,v 1.31 2003/09/29 15:45:19 sigmunau 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:
223 p_var->pf_cmp = CmpInt;
224 p_var->val.i_int = 0;
229 case VLC_VAR_DIRECTORY:
230 case VLC_VAR_VARIABLE:
231 p_var->pf_cmp = CmpString;
232 p_var->pf_dup = DupString;
233 p_var->pf_free = FreeString;
234 p_var->val.psz_string = "";
237 p_var->pf_cmp = CmpFloat;
238 p_var->val.f_float = 0.0;
241 p_var->pf_cmp = CmpTime;
242 p_var->val.i_time = 0;
244 case VLC_VAR_ADDRESS:
245 p_var->pf_cmp = CmpAddress;
246 p_var->val.p_address = NULL;
249 p_var->pf_cmp = CmpAddress;
250 p_var->pf_free = FreeMutex;
251 p_var->val.p_address = malloc( sizeof(vlc_mutex_t) );
252 vlc_mutex_init( p_this, (vlc_mutex_t*)p_var->val.p_address );
255 p_var->pf_cmp = CmpAddress;
256 p_var->pf_dup = DupList;
257 p_var->pf_free = FreeList;
258 p_var->val.p_list = &dummy_null_list;
262 /* Duplicate the default data we stored. */
263 p_var->pf_dup( &p_var->val );
265 if( i_type & VLC_VAR_DOINHERIT )
269 if( InheritValue( p_this, psz_name, &val, p_var->i_type )
272 /* Free data if needed */
273 p_var->pf_free( &p_var->val );
274 /* Check boundaries and list */
275 CheckValue( p_var, &val );
276 /* Set the variable */
281 vlc_mutex_unlock( &p_this->var_lock );
287 * Destroy a vlc variable
289 * Look for the variable and destroy it if it is found. As in var_Create we
290 * do a call to memmove() but we have performance counterparts elsewhere.
292 * \param p_this The object that holds the variable
293 * \param psz_name The name of the variable
295 int __var_Destroy( vlc_object_t *p_this, const char *psz_name )
300 vlc_mutex_lock( &p_this->var_lock );
302 i_var = GetUnused( p_this, psz_name );
305 vlc_mutex_unlock( &p_this->var_lock );
309 p_var = &p_this->p_vars[i_var];
311 if( p_var->i_usage > 1 )
314 vlc_mutex_unlock( &p_this->var_lock );
318 /* Free value if needed */
319 p_var->pf_free( &p_var->val );
321 /* Free choice list if needed */
322 if( p_var->choices.i_count )
324 for( i = 0 ; i < p_var->choices.i_count ; i++ )
326 p_var->pf_free( &p_var->choices.p_values[i] );
327 if( p_var->choices_text.p_values[i].psz_string )
328 free( p_var->choices_text.p_values[i].psz_string );
330 free( p_var->choices.p_values );
331 free( p_var->choices_text.p_values );
334 /* Free callbacks if needed */
335 if( p_var->p_entries )
337 free( p_var->p_entries );
340 free( p_var->psz_name );
341 if( p_var->psz_text ) free( p_var->psz_text );
343 memmove( p_this->p_vars + i_var,
344 p_this->p_vars + i_var + 1,
345 (p_this->i_vars - i_var - 1) * sizeof(variable_t) );
347 if( (p_this->i_vars & 15) == 0 )
349 p_this->p_vars = realloc( p_this->p_vars,
350 (p_this->i_vars) * sizeof( variable_t ) );
355 vlc_mutex_unlock( &p_this->var_lock );
361 * Perform an action on a variable
363 * \param p_this The object that holds the variable
364 * \param psz_name The name of the variable
365 * \param i_action The action to perform. Must be one of \ref var_action
366 * \param p_val First action parameter
367 * \param p_val2 Second action parameter
369 int __var_Change( vlc_object_t *p_this, const char *psz_name,
370 int i_action, vlc_value_t *p_val, vlc_value_t *p_val2 )
376 vlc_mutex_lock( &p_this->var_lock );
378 i_var = Lookup( p_this->p_vars, p_this->i_vars, psz_name );
382 vlc_mutex_unlock( &p_this->var_lock );
386 p_var = &p_this->p_vars[i_var];
391 if( p_var->i_type & VLC_VAR_HASMIN )
393 p_var->pf_free( &p_var->min );
395 p_var->i_type |= VLC_VAR_HASMIN;
397 p_var->pf_dup( &p_var->min );
398 CheckValue( p_var, &p_var->val );
401 if( p_var->i_type & VLC_VAR_HASMAX )
403 p_var->pf_free( &p_var->max );
405 p_var->i_type |= VLC_VAR_HASMAX;
407 p_var->pf_dup( &p_var->max );
408 CheckValue( p_var, &p_var->val );
410 case VLC_VAR_SETSTEP:
411 if( p_var->i_type & VLC_VAR_HASSTEP )
413 p_var->pf_free( &p_var->step );
415 p_var->i_type |= VLC_VAR_HASSTEP;
416 p_var->step = *p_val;
417 p_var->pf_dup( &p_var->step );
418 CheckValue( p_var, &p_var->val );
420 case VLC_VAR_ADDCHOICE:
421 /* FIXME: the list is sorted, dude. Use something cleverer. */
422 for( i = p_var->choices.i_count ; i-- ; )
424 if( p_var->pf_cmp( p_var->choices.p_values[i], *p_val ) < 0 )
430 /* The new place is i+1 */
433 if( p_var->i_default >= i )
438 INSERT_ELEM( p_var->choices.p_values, p_var->choices.i_count,
440 INSERT_ELEM( p_var->choices_text.p_values,
441 p_var->choices_text.i_count, i, *p_val );
442 p_var->pf_dup( &p_var->choices.p_values[i] );
443 p_var->choices_text.p_values[i].psz_string =
444 ( p_val2 && p_val2->psz_string ) ?
445 strdup( p_val2->psz_string ) : NULL;
447 CheckValue( p_var, &p_var->val );
449 case VLC_VAR_DELCHOICE:
450 /* FIXME: the list is sorted, dude. Use something cleverer. */
451 for( i = 0 ; i < p_var->choices.i_count ; i++ )
453 if( p_var->pf_cmp( p_var->choices.p_values[i], *p_val ) == 0 )
459 if( i == p_var->choices.i_count )
462 vlc_mutex_unlock( &p_this->var_lock );
466 if( p_var->i_default > i )
470 else if( p_var->i_default == i )
472 p_var->i_default = -1;
475 p_var->pf_free( &p_var->choices.p_values[i] );
476 if( p_var->choices_text.p_values[i].psz_string )
477 free( p_var->choices_text.p_values[i].psz_string );
478 REMOVE_ELEM( p_var->choices.p_values, p_var->choices.i_count, i );
479 REMOVE_ELEM( p_var->choices_text.p_values,
480 p_var->choices_text.i_count, i );
482 CheckValue( p_var, &p_var->val );
484 case VLC_VAR_CHOICESCOUNT:
485 p_val->i_int = p_var->choices.i_count;
487 case VLC_VAR_CLEARCHOICES:
488 for( i = 0 ; i < p_var->choices.i_count ; i++ )
490 p_var->pf_free( &p_var->choices.p_values[i] );
492 if( p_var->choices.i_count )
493 free( p_var->choices.p_values );
495 p_var->choices.i_count = 0;
496 p_var->choices.p_values = NULL;
497 p_var->i_default = -1;
499 case VLC_VAR_SETDEFAULT:
500 /* FIXME: the list is sorted, dude. Use something cleverer. */
501 for( i = 0 ; i < p_var->choices.i_count ; i++ )
503 if( p_var->pf_cmp( p_var->choices.p_values[i], *p_val ) == 0 )
509 if( i == p_var->choices.i_count )
515 p_var->i_default = i;
516 CheckValue( p_var, &p_var->val );
518 case VLC_VAR_SETVALUE:
519 /* Duplicate data if needed */
520 p_var->pf_dup( p_val );
521 /* Backup needed stuff */
523 /* Check boundaries and list */
524 CheckValue( p_var, p_val );
525 /* Set the variable */
527 /* Free data if needed */
528 p_var->pf_free( &oldval );
530 case VLC_VAR_GETCHOICES:
531 case VLC_VAR_GETLIST:
532 p_val->p_list = malloc( sizeof(vlc_list_t) );
533 if( p_val2 ) p_val2->p_list = malloc( sizeof(vlc_list_t) );
534 if( p_var->choices.i_count )
536 p_val->p_list->p_values = malloc( p_var->choices.i_count
537 * sizeof(vlc_value_t) );
538 p_val->p_list->pi_types = malloc( p_var->choices.i_count
542 p_val2->p_list->p_values =
543 malloc( p_var->choices.i_count * sizeof(vlc_value_t) );
544 p_val2->p_list->pi_types =
545 malloc( p_var->choices.i_count * sizeof(int) );
548 p_val->p_list->i_count = p_var->choices.i_count;
549 if( p_val2 ) p_val2->p_list->i_count = p_var->choices.i_count;
550 for( i = 0 ; i < p_var->choices.i_count ; i++ )
552 p_val->p_list->p_values[i] = p_var->choices.p_values[i];
553 p_val->p_list->pi_types[i] = p_var->i_type;
554 p_var->pf_dup( &p_val->p_list->p_values[i] );
557 p_val2->p_list->p_values[i].psz_string =
558 p_var->choices_text.p_values[i].psz_string ?
559 strdup(p_var->choices_text.p_values[i].psz_string) : NULL;
560 p_val2->p_list->pi_types[i] = VLC_VAR_STRING;
564 case VLC_VAR_FREELIST:
567 case VLC_VAR_SETTEXT:
568 if( p_var->psz_text ) free( p_var->psz_text );
569 if( p_val && p_val->psz_string )
570 p_var->psz_text = strdup( p_val->psz_string );
572 case VLC_VAR_GETTEXT:
573 p_val->psz_string = NULL;
574 if( p_var->psz_text )
576 p_val->psz_string = strdup( p_var->psz_text );
579 case VLC_VAR_INHERITVALUE:
583 if( InheritValue( p_this, psz_name, &val, p_var->i_type )
586 /* Duplicate already done */
588 /* Backup needed stuff */
590 /* Check boundaries and list */
591 CheckValue( p_var, &val );
592 /* Set the variable */
594 /* Free data if needed */
595 p_var->pf_free( &oldval );
601 p_var->pf_dup( p_val );
610 vlc_mutex_unlock( &p_this->var_lock );
616 * Request a variable's type
618 * \return The variable type if it exists, or 0 if the
619 * variable could not be found.
622 int __var_Type( vlc_object_t *p_this, const char *psz_name )
626 vlc_mutex_lock( &p_this->var_lock );
628 i_var = Lookup( p_this->p_vars, p_this->i_vars, psz_name );
632 vlc_mutex_unlock( &p_this->var_lock );
636 i_type = p_this->p_vars[i_var].i_type;
638 vlc_mutex_unlock( &p_this->var_lock );
644 * Set a variable's value
646 * \param p_this The object that hold the variable
647 * \param psz_name The name of the variable
648 * \param val the value to set
650 int __var_Set( vlc_object_t *p_this, const char *psz_name, vlc_value_t val )
656 vlc_mutex_lock( &p_this->var_lock );
658 i_var = GetUnused( p_this, psz_name );
661 vlc_mutex_unlock( &p_this->var_lock );
665 p_var = &p_this->p_vars[i_var];
667 /* Duplicate data if needed */
668 p_var->pf_dup( &val );
670 /* Backup needed stuff */
673 /* Check boundaries and list */
674 CheckValue( p_var, &val );
676 /* Set the variable */
679 /* Deal with callbacks. Tell we're in a callback, release the lock,
680 * call stored functions, retake the lock. */
681 if( p_var->i_entries )
684 int i_entries = p_var->i_entries;
685 callback_entry_t *p_entries = p_var->p_entries;
687 p_var->b_incallback = VLC_TRUE;
688 vlc_mutex_unlock( &p_this->var_lock );
691 for( ; i_entries-- ; )
693 p_entries[i_entries].pf_callback( p_this, psz_name, oldval, val,
694 p_entries[i_entries].p_data );
697 vlc_mutex_lock( &p_this->var_lock );
699 i_var = Lookup( p_this->p_vars, p_this->i_vars, psz_name );
702 msg_Err( p_this, "variable %s has disappeared", psz_name );
703 vlc_mutex_unlock( &p_this->var_lock );
707 p_var = &p_this->p_vars[i_var];
708 p_var->b_incallback = VLC_FALSE;
711 /* Free data if needed */
712 p_var->pf_free( &oldval );
714 vlc_mutex_unlock( &p_this->var_lock );
720 * Get a variable's value
722 * \param p_this The object that holds the variable
723 * \param psz_name The name of the variable
724 * \param p_val Pointer to a vlc_value_t that will hold the variable's value
725 * after the function is finished
727 int __var_Get( vlc_object_t *p_this, const char *psz_name, vlc_value_t *p_val )
732 vlc_mutex_lock( &p_this->var_lock );
734 i_var = Lookup( p_this->p_vars, p_this->i_vars, psz_name );
738 vlc_mutex_unlock( &p_this->var_lock );
742 p_var = &p_this->p_vars[i_var];
744 /* Really get the variable */
747 /* Duplicate value if needed */
748 p_var->pf_dup( p_val );
750 vlc_mutex_unlock( &p_this->var_lock );
756 * Register a callback in a variable
758 * We store a function pointer that will be called upon variable
761 * \param p_this The object that holds the variable
762 * \param psz_name The name of the variable
763 * \param pf_callback The function pointer
764 * \param p_data A generic pointer that will be passed as the last
765 * argument to the callback function.
767 * \warning The callback function is run in the thread that calls var_Set on
768 * the variable. Use proper locking. This thread may not have much
769 * time to spare, so keep callback functions short.
771 int __var_AddCallback( vlc_object_t *p_this, const char *psz_name,
772 vlc_callback_t pf_callback, void *p_data )
776 callback_entry_t entry;
778 entry.pf_callback = pf_callback;
779 entry.p_data = p_data;
781 vlc_mutex_lock( &p_this->var_lock );
783 i_var = GetUnused( p_this, psz_name );
786 vlc_mutex_unlock( &p_this->var_lock );
790 p_var = &p_this->p_vars[i_var];
792 INSERT_ELEM( p_var->p_entries,
797 vlc_mutex_unlock( &p_this->var_lock );
803 * Remove a callback from a variable
805 * pf_callback and p_data have to be given again, because different objects
806 * might have registered the same callback function.
808 int __var_DelCallback( vlc_object_t *p_this, const char *psz_name,
809 vlc_callback_t pf_callback, void *p_data )
814 vlc_mutex_lock( &p_this->var_lock );
816 i_var = GetUnused( p_this, psz_name );
819 vlc_mutex_unlock( &p_this->var_lock );
823 p_var = &p_this->p_vars[i_var];
825 for( i_entry = p_var->i_entries ; i_entry-- ; )
827 if( p_var->p_entries[i_entry].pf_callback == pf_callback
828 && p_var->p_entries[i_entry].p_data == p_data )
836 vlc_mutex_unlock( &p_this->var_lock );
840 REMOVE_ELEM( p_var->p_entries, p_var->i_entries, i_entry );
842 vlc_mutex_unlock( &p_this->var_lock );
847 /* Following functions are local */
849 /*****************************************************************************
850 * GetUnused: find an unused variable from its name
851 *****************************************************************************
852 * We do i_tries tries before giving up, just in case the variable is being
853 * modified and called from a callback.
854 *****************************************************************************/
855 static int GetUnused( vlc_object_t *p_this, const char *psz_name )
857 int i_var, i_tries = 0;
861 i_var = Lookup( p_this->p_vars, p_this->i_vars, psz_name );
867 if( ! p_this->p_vars[i_var].b_incallback )
872 if( i_tries++ > 100 )
874 msg_Err( p_this, "caught in a callback deadlock?" );
878 vlc_mutex_unlock( &p_this->var_lock );
879 msleep( THREAD_SLEEP );
880 vlc_mutex_lock( &p_this->var_lock );
884 /*****************************************************************************
885 * HashString: our cool hash function
886 *****************************************************************************
887 * This function is not intended to be crypto-secure, we only want it to be
888 * fast and not suck too much. This one is pretty fast and did 0 collisions
889 * in wenglish's dictionary.
890 *****************************************************************************/
891 static uint32_t HashString( const char *psz_string )
897 i_hash += *psz_string++;
898 i_hash += i_hash << 10;
899 i_hash ^= i_hash >> 8;
905 /*****************************************************************************
906 * Insert: find an empty slot to insert a new variable
907 *****************************************************************************
908 * We use a recursive inner function indexed on the hash. This function does
909 * nothing in the rare cases where a collision may occur, see Lookup()
910 * to see how we handle them.
911 * XXX: does this really need to be written recursively?
912 *****************************************************************************/
913 static int Insert( variable_t *p_vars, int i_count, const char *psz_name )
920 return InsertInner( p_vars, i_count, HashString( psz_name ) );
923 static int InsertInner( variable_t *p_vars, int i_count, uint32_t i_hash )
927 if( i_hash <= p_vars[0].i_hash )
932 if( i_hash >= p_vars[i_count - 1].i_hash )
937 i_middle = i_count / 2;
939 /* We know that 0 < i_middle */
940 if( i_hash < p_vars[i_middle].i_hash )
942 return InsertInner( p_vars, i_middle, i_hash );
945 /* We know that i_middle + 1 < i_count */
946 if( i_hash > p_vars[i_middle + 1].i_hash )
948 return i_middle + 1 + InsertInner( p_vars + i_middle + 1,
949 i_count - i_middle - 1,
956 /*****************************************************************************
957 * Lookup: find an existing variable given its name
958 *****************************************************************************
959 * We use a recursive inner function indexed on the hash. Care is taken of
960 * possible hash collisions.
961 * XXX: does this really need to be written recursively?
962 *****************************************************************************/
963 static int Lookup( variable_t *p_vars, int i_count, const char *psz_name )
973 i_hash = HashString( psz_name );
975 i_pos = LookupInner( p_vars, i_count, i_hash );
978 if( i_hash != p_vars[i_pos].i_hash )
983 /* Hash found, entry found */
984 if( !strcmp( psz_name, p_vars[i_pos].psz_name ) )
989 /* Hash collision! This should be very rare, but we cannot guarantee
990 * it will never happen. Just do an exhaustive search amongst all
991 * entries with the same hash. */
992 for( i = i_pos - 1 ; i > 0 && i_hash == p_vars[i].i_hash ; i-- )
994 if( !strcmp( psz_name, p_vars[i].psz_name ) )
1000 for( i = i_pos + 1 ; i < i_count && i_hash == p_vars[i].i_hash ; i++ )
1002 if( !strcmp( psz_name, p_vars[i].psz_name ) )
1008 /* Hash found, but entry not found */
1012 static int LookupInner( variable_t *p_vars, int i_count, uint32_t i_hash )
1016 if( i_hash <= p_vars[0].i_hash )
1021 if( i_hash >= p_vars[i_count-1].i_hash )
1026 i_middle = i_count / 2;
1028 /* We know that 0 < i_middle */
1029 if( i_hash < p_vars[i_middle].i_hash )
1031 return LookupInner( p_vars, i_middle, i_hash );
1034 /* We know that i_middle + 1 < i_count */
1035 if( i_hash > p_vars[i_middle].i_hash )
1037 return i_middle + LookupInner( p_vars + i_middle,
1045 /*****************************************************************************
1046 * CheckValue: check that a value is valid wrt. a variable
1047 *****************************************************************************
1048 * This function checks p_val's value against p_var's limitations such as
1049 * minimal and maximal value, step, in-list position, and modifies p_val if
1051 *****************************************************************************/
1052 static void CheckValue ( variable_t *p_var, vlc_value_t *p_val )
1054 /* Check that our variable is in the list */
1055 if( p_var->i_type & VLC_VAR_HASCHOICE && p_var->choices.i_count )
1059 /* FIXME: the list is sorted, dude. Use something cleverer. */
1060 for( i = p_var->choices.i_count ; i-- ; )
1062 if( p_var->pf_cmp( *p_val, p_var->choices.p_values[i] ) == 0 )
1068 /* If not found, change it to anything vaguely valid */
1071 /* Free the old variable, get the new one, dup it */
1072 p_var->pf_free( p_val );
1073 *p_val = p_var->choices.p_values[p_var->i_default >= 0
1074 ? p_var->i_default : 0 ];
1075 p_var->pf_dup( p_val );
1079 /* Check that our variable is within the bounds */
1080 switch( p_var->i_type & VLC_VAR_TYPE )
1082 case VLC_VAR_INTEGER:
1083 if( p_var->i_type & VLC_VAR_HASSTEP && p_var->step.i_int
1084 && (p_val->i_int % p_var->step.i_int) )
1086 p_val->i_int = (p_val->i_int + (p_var->step.i_int / 2))
1087 / p_var->step.i_int * p_var->step.i_int;
1089 if( p_var->i_type & VLC_VAR_HASMIN
1090 && p_val->i_int < p_var->min.i_int )
1092 p_val->i_int = p_var->min.i_int;
1094 if( p_var->i_type & VLC_VAR_HASMAX
1095 && p_val->i_int > p_var->max.i_int )
1097 p_val->i_int = p_var->max.i_int;
1101 if( p_var->i_type & VLC_VAR_HASSTEP && p_var->step.f_float )
1103 float f_round = p_var->step.f_float * (float)(int)( 0.5 +
1104 p_val->f_float / p_var->step.f_float );
1105 if( p_val->f_float != f_round )
1107 p_val->f_float = f_round;
1110 if( p_var->i_type & VLC_VAR_HASMIN
1111 && p_val->f_float < p_var->min.f_float )
1113 p_val->f_float = p_var->min.f_float;
1115 if( p_var->i_type & VLC_VAR_HASMAX
1116 && p_val->f_float > p_var->max.f_float )
1118 p_val->f_float = p_var->max.f_float;
1127 /*****************************************************************************
1128 * InheritValue: try to inherit the value of this variable from the same one
1129 * in our closest parent.
1130 *****************************************************************************/
1131 static int InheritValue( vlc_object_t *p_this, const char *psz_name,
1132 vlc_value_t *p_val, int i_type )
1137 /* No need to take the structure lock,
1138 * we are only looking for our parents */
1140 if( !p_this->p_parent )
1142 switch( i_type & VLC_VAR_TYPE )
1145 case VLC_VAR_DIRECTORY:
1146 case VLC_VAR_STRING:
1147 p_val->psz_string = config_GetPsz( p_this, psz_name );
1148 if( !p_val->psz_string ) p_val->psz_string = strdup("");
1151 p_val->f_float = config_GetFloat( p_this, psz_name );
1153 case VLC_VAR_INTEGER:
1154 p_val->i_int = config_GetInt( p_this, psz_name );
1157 p_val->b_bool = config_GetInt( p_this, psz_name );
1167 /* Look for the variable */
1168 vlc_mutex_lock( &p_this->p_parent->var_lock );
1170 i_var = Lookup( p_this->p_parent->p_vars, p_this->p_parent->i_vars,
1177 p_var = &p_this->p_parent->p_vars[i_var];
1179 /* Really get the variable */
1180 *p_val = p_var->val;
1182 /* Duplicate value if needed */
1183 p_var->pf_dup( p_val );
1185 vlc_mutex_unlock( &p_this->p_parent->var_lock );
1189 vlc_mutex_unlock( &p_this->p_parent->var_lock );
1191 /* We're still not there */
1193 return InheritValue( p_this->p_parent, psz_name, p_val, i_type );