1 /*****************************************************************************
2 * variables.c: routines for object variables handling
3 *****************************************************************************
4 * Copyright (C) 2002 VideoLAN
5 * $Id: variables.c,v 1.23 2003/05/05 15:21:27 sigmunau Exp $
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 mtime_t v_time,w_time;
50 v_time = ( (mtime_t)v.time.i_high << 32 ) + v.time.i_low;
51 w_time = ( (mtime_t)w.time.i_high << 32 ) + w.time.i_low;
52 return v_time == w_time ? 0 : v_time > w_time ? 1 : -1;
54 static int CmpString( vlc_value_t v, vlc_value_t w ) { return strcmp( v.psz_string, w.psz_string ); }
55 static int CmpFloat( vlc_value_t v, vlc_value_t w ) { return v.f_float == w.f_float ? 0 : v.f_float > w.f_float ? 1 : -1; }
56 static int CmpAddress( vlc_value_t v, vlc_value_t w ) { return v.p_address == w.p_address ? 0 : v.p_address > w.p_address ? 1 : -1; }
58 /*****************************************************************************
59 * Local duplication functions, and local deallocation functions
60 *****************************************************************************/
61 static void DupDummy( vlc_value_t *p_val ) { (void)p_val; /* unused */ }
62 static void DupString( vlc_value_t *p_val ) { p_val->psz_string = strdup( p_val->psz_string ); }
64 static void DupList( vlc_value_t *p_val )
67 vlc_list_t *p_list = malloc( sizeof(vlc_list_t) );
69 if( p_val->p_list->i_count )
71 p_list->i_count = p_val->p_list->i_count;
72 p_list->p_values = malloc( p_list->i_count * sizeof(vlc_value_t) );
73 p_list->pi_types = malloc( p_list->i_count * sizeof(int) );
76 for( i = 0; i < p_list->i_count; i++ )
78 p_list->p_values[i] = p_val->p_list->p_values[i];
79 switch( p_val->p_list->pi_types[i] & VLC_VAR_TYPE )
83 DupString( &p_list->p_values[i] );
90 p_val->p_list = p_list;
93 static void FreeDummy( vlc_value_t *p_val ) { (void)p_val; /* unused */ }
94 static void FreeString( vlc_value_t *p_val ) { free( p_val->psz_string ); }
95 static void FreeMutex( vlc_value_t *p_val ) { vlc_mutex_destroy( (vlc_mutex_t*)p_val->p_address ); free( p_val->p_address ); }
97 static void FreeList( vlc_value_t *p_val )
100 for( i = 0; i < p_val->p_list->i_count; i++ )
102 switch( p_val->p_list->pi_types[i] & VLC_VAR_TYPE )
105 FreeString( &p_val->p_list->p_values[i] );
108 FreeMutex( &p_val->p_list->p_values[i] );
115 if( p_val->p_list->i_count )
117 free( p_val->p_list->p_values );
118 free( p_val->p_list->pi_types );
120 free( p_val->p_list );
123 /*****************************************************************************
125 *****************************************************************************/
126 static int GetUnused ( vlc_object_t *, const char * );
127 static uint32_t HashString ( const char * );
128 static int Insert ( variable_t *, int, const char * );
129 static int InsertInner ( variable_t *, int, uint32_t );
130 static int Lookup ( variable_t *, int, const char * );
131 static int LookupInner ( variable_t *, int, uint32_t );
133 static void CheckValue ( variable_t *, vlc_value_t * );
135 /*****************************************************************************
136 * var_Create: initialize a vlc variable
137 *****************************************************************************
138 * We hash the given string and insert it into the sorted list. The insertion
139 * may require slow memory copies, but think about what we gain in the log(n)
140 * lookup phase when setting/getting the variable value!
141 *****************************************************************************/
142 int __var_Create( vlc_object_t *p_this, const char *psz_name, int i_type )
146 static vlc_list_t dummy_null_list = {0, NULL, NULL};
148 vlc_mutex_lock( &p_this->var_lock );
150 /* FIXME: if the variable already exists, we don't duplicate it. But we
151 * duplicate the lookups. It's not that serious, but if anyone finds some
152 * time to rework Insert() so that only one lookup has to be done, feel
154 i_new = Lookup( p_this->p_vars, p_this->i_vars, psz_name );
158 /* If the types differ, variable creation failed. */
159 if( i_type != p_this->p_vars[i_new].i_type )
161 vlc_mutex_unlock( &p_this->var_lock );
165 p_this->p_vars[i_new].i_usage++;
166 vlc_mutex_unlock( &p_this->var_lock );
170 i_new = Insert( p_this->p_vars, p_this->i_vars, psz_name );
172 if( (p_this->i_vars & 15) == 15 )
174 p_this->p_vars = realloc( p_this->p_vars,
175 (p_this->i_vars+17) * sizeof(variable_t) );
178 memmove( p_this->p_vars + i_new + 1,
179 p_this->p_vars + i_new,
180 (p_this->i_vars - i_new) * sizeof(variable_t) );
184 p_var = &p_this->p_vars[i_new];
186 p_var->i_hash = HashString( psz_name );
187 p_var->psz_name = strdup( psz_name );
188 p_var->psz_text = NULL;
190 p_var->i_type = i_type;
191 memset( &p_var->val, 0, sizeof(vlc_value_t) );
193 p_var->pf_dup = DupDummy;
194 p_var->pf_free = FreeDummy;
198 p_var->i_default = -1;
199 p_var->choices.i_count = 0;
200 p_var->choices.p_values = NULL;
201 p_var->choices_text.i_count = 0;
202 p_var->choices_text.p_values = NULL;
204 p_var->b_incallback = VLC_FALSE;
205 p_var->i_entries = 0;
206 p_var->p_entries = NULL;
208 /* Always initialize the variable, even if it is a list variable; this
209 * will lead to errors if the variable is not initialized, but it will
210 * not cause crashes in the variable handling. */
211 switch( i_type & VLC_VAR_TYPE )
214 p_var->pf_cmp = CmpBool;
215 p_var->val.b_bool = VLC_FALSE;
217 case VLC_VAR_INTEGER:
218 p_var->pf_cmp = CmpInt;
219 p_var->val.i_int = 0;
224 case VLC_VAR_DIRECTORY:
225 case VLC_VAR_VARIABLE:
226 p_var->pf_cmp = CmpString;
227 p_var->pf_dup = DupString;
228 p_var->pf_free = FreeString;
229 p_var->val.psz_string = "";
232 p_var->pf_cmp = CmpFloat;
233 p_var->val.f_float = 0.0;
236 p_var->pf_cmp = CmpTime;
237 p_var->val.time.i_low = 0;
238 p_var->val.time.i_high = 0;
240 case VLC_VAR_ADDRESS:
241 p_var->pf_cmp = CmpAddress;
242 p_var->val.p_address = NULL;
245 p_var->pf_cmp = CmpAddress;
246 p_var->pf_free = FreeMutex;
247 p_var->val.p_address = malloc( sizeof(vlc_mutex_t) );
248 vlc_mutex_init( p_this, (vlc_mutex_t*)p_var->val.p_address );
251 p_var->pf_cmp = CmpAddress;
252 p_var->pf_dup = DupList;
253 p_var->pf_free = FreeList;
254 p_var->val.p_list = &dummy_null_list;
258 /* Duplicate the default data we stored. */
259 p_var->pf_dup( &p_var->val );
261 vlc_mutex_unlock( &p_this->var_lock );
266 /*****************************************************************************
267 * var_Destroy: destroy a vlc variable
268 *****************************************************************************
269 * Look for the variable and destroy it if it is found. As in var_Create we
270 * do a call to memmove() but we have performance counterparts elsewhere.
271 *****************************************************************************/
272 int __var_Destroy( vlc_object_t *p_this, const char *psz_name )
277 vlc_mutex_lock( &p_this->var_lock );
279 i_var = GetUnused( p_this, psz_name );
282 vlc_mutex_unlock( &p_this->var_lock );
286 p_var = &p_this->p_vars[i_var];
288 if( p_var->i_usage > 1 )
291 vlc_mutex_unlock( &p_this->var_lock );
295 /* Free value if needed */
296 p_var->pf_free( &p_var->val );
298 /* Free choice list if needed */
299 if( p_var->choices.i_count )
301 for( i = 0 ; i < p_var->choices.i_count ; i++ )
303 p_var->pf_free( &p_var->choices.p_values[i] );
305 free( p_var->choices.p_values );
308 /* Free callbacks if needed */
309 if( p_var->p_entries )
311 free( p_var->p_entries );
314 free( p_var->psz_name );
315 if( p_var->psz_text ) free( p_var->psz_text );
317 memmove( p_this->p_vars + i_var,
318 p_this->p_vars + i_var + 1,
319 (p_this->i_vars - i_var - 1) * sizeof(variable_t) );
321 if( (p_this->i_vars & 15) == 0 )
323 p_this->p_vars = realloc( p_this->p_vars,
324 (p_this->i_vars) * sizeof( variable_t ) );
329 vlc_mutex_unlock( &p_this->var_lock );
334 /*****************************************************************************
335 * var_Change: perform an action on a variable
336 *****************************************************************************
338 *****************************************************************************/
339 int __var_Change( vlc_object_t *p_this, const char *psz_name,
340 int i_action, vlc_value_t *p_val, vlc_value_t *p_val2 )
346 vlc_mutex_lock( &p_this->var_lock );
348 i_var = Lookup( p_this->p_vars, p_this->i_vars, psz_name );
352 vlc_mutex_unlock( &p_this->var_lock );
356 p_var = &p_this->p_vars[i_var];
361 if( p_var->i_type & VLC_VAR_HASMIN )
363 p_var->pf_free( &p_var->min );
365 p_var->i_type |= VLC_VAR_HASMIN;
367 p_var->pf_dup( &p_var->min );
368 CheckValue( p_var, &p_var->val );
371 if( p_var->i_type & VLC_VAR_HASMAX )
373 p_var->pf_free( &p_var->max );
375 p_var->i_type |= VLC_VAR_HASMAX;
377 p_var->pf_dup( &p_var->max );
378 CheckValue( p_var, &p_var->val );
380 case VLC_VAR_SETSTEP:
381 if( p_var->i_type & VLC_VAR_HASSTEP )
383 p_var->pf_free( &p_var->step );
385 p_var->i_type |= VLC_VAR_HASSTEP;
386 p_var->step = *p_val;
387 p_var->pf_dup( &p_var->step );
388 CheckValue( p_var, &p_var->val );
390 case VLC_VAR_ADDCHOICE:
391 /* FIXME: the list is sorted, dude. Use something cleverer. */
392 for( i = p_var->choices.i_count ; i-- ; )
394 if( p_var->pf_cmp( p_var->choices.p_values[i], *p_val ) < 0 )
400 /* The new place is i+1 */
403 if( p_var->i_default >= i )
408 INSERT_ELEM( p_var->choices.p_values, p_var->choices.i_count,
410 INSERT_ELEM( p_var->choices_text.p_values,
411 p_var->choices_text.i_count, i, (vlc_value_t)0 );
412 p_var->pf_dup( &p_var->choices.p_values[i] );
413 p_var->choices_text.p_values[i].psz_string =
414 ( p_val2 && p_val2->psz_string ) ?
415 strdup( p_val2->psz_string ) : NULL;
417 CheckValue( p_var, &p_var->val );
419 case VLC_VAR_DELCHOICE:
420 /* FIXME: the list is sorted, dude. Use something cleverer. */
421 for( i = 0 ; i < p_var->choices.i_count ; i++ )
423 if( p_var->pf_cmp( p_var->choices.p_values[i], *p_val ) == 0 )
429 if( i == p_var->choices.i_count )
432 vlc_mutex_unlock( &p_this->var_lock );
436 if( p_var->i_default > i )
440 else if( p_var->i_default == i )
442 p_var->i_default = -1;
445 p_var->pf_free( &p_var->choices.p_values[i] );
446 if( p_var->choices_text.p_values[i].psz_string )
447 free( p_var->choices_text.p_values[i].psz_string );
448 REMOVE_ELEM( p_var->choices.p_values, p_var->choices.i_count, i );
449 REMOVE_ELEM( p_var->choices_text.p_values,
450 p_var->choices_text.i_count, i );
452 CheckValue( p_var, &p_var->val );
454 case VLC_VAR_CHOICESCOUNT:
455 p_val->i_int = p_var->choices.i_count;
457 case VLC_VAR_CLEARCHOICES:
458 for( i = 0 ; i < p_var->choices.i_count ; i++ )
460 p_var->pf_free( &p_var->choices.p_values[i] );
462 if( p_var->choices.i_count )
463 free( p_var->choices.p_values );
465 p_var->choices.i_count = 0;
466 p_var->choices.p_values = NULL;
467 p_var->i_default = -1;
469 case VLC_VAR_SETDEFAULT:
470 /* FIXME: the list is sorted, dude. Use something cleverer. */
471 for( i = 0 ; i < p_var->choices.i_count ; i++ )
473 if( p_var->pf_cmp( p_var->choices.p_values[i], *p_val ) == 0 )
479 if( i == p_var->choices.i_count )
485 p_var->i_default = i;
486 CheckValue( p_var, &p_var->val );
488 case VLC_VAR_SETVALUE:
489 /* Duplicate data if needed */
490 p_var->pf_dup( p_val );
491 /* Backup needed stuff */
493 /* Check boundaries and list */
494 CheckValue( p_var, p_val );
495 /* Set the variable */
497 /* Free data if needed */
498 p_var->pf_free( &oldval );
500 case VLC_VAR_GETCHOICES:
501 case VLC_VAR_GETLIST:
502 p_val->p_list = malloc( sizeof(vlc_list_t) );
503 if( p_val2 ) p_val2->p_list = malloc( sizeof(vlc_list_t) );
504 if( p_var->choices.i_count )
506 p_val->p_list->p_values = malloc( p_var->choices.i_count
507 * sizeof(vlc_value_t) );
508 p_val->p_list->pi_types = malloc( p_var->choices.i_count
512 p_val2->p_list->p_values =
513 malloc( p_var->choices.i_count * sizeof(vlc_value_t) );
514 p_val2->p_list->pi_types =
515 malloc( p_var->choices.i_count * sizeof(int) );
518 p_val->p_list->i_count = p_var->choices.i_count;
519 if( p_val2 ) p_val2->p_list->i_count = p_var->choices.i_count;
520 for( i = 0 ; i < p_var->choices.i_count ; i++ )
522 p_val->p_list->p_values[i] = p_var->choices.p_values[i];
523 p_val->p_list->pi_types[i] = p_var->i_type;
524 p_var->pf_dup( &p_val->p_list->p_values[i] );
527 p_val2->p_list->p_values[i].psz_string =
528 p_var->choices_text.p_values[i].psz_string ?
529 strdup(p_var->choices_text.p_values[i].psz_string) : NULL;
530 p_val2->p_list->pi_types[i] = VLC_VAR_STRING;
534 case VLC_VAR_FREELIST:
537 case VLC_VAR_SETTEXT:
538 if( p_var->psz_text ) free( p_var->psz_text );
539 if( p_val && p_val->psz_string )
540 p_var->psz_text = strdup( p_val->psz_string );
542 case VLC_VAR_GETTEXT:
543 p_val->psz_string = NULL;
544 if( p_var->psz_text )
546 p_val->psz_string = strdup( p_var->psz_text );
554 vlc_mutex_unlock( &p_this->var_lock );
559 /*****************************************************************************
560 * var_Type: request a variable's type
561 *****************************************************************************
562 * This function returns the variable type if it exists, or 0 if the
563 * variable could not be found.
564 *****************************************************************************/
565 int __var_Type( vlc_object_t *p_this, const char *psz_name )
569 vlc_mutex_lock( &p_this->var_lock );
571 i_var = Lookup( p_this->p_vars, p_this->i_vars, psz_name );
575 vlc_mutex_unlock( &p_this->var_lock );
579 i_type = p_this->p_vars[i_var].i_type;
581 vlc_mutex_unlock( &p_this->var_lock );
586 /*****************************************************************************
587 * var_Set: set a variable's value
588 *****************************************************************************
590 *****************************************************************************/
591 int __var_Set( vlc_object_t *p_this, const char *psz_name, vlc_value_t val )
597 vlc_mutex_lock( &p_this->var_lock );
599 i_var = GetUnused( p_this, psz_name );
602 vlc_mutex_unlock( &p_this->var_lock );
606 p_var = &p_this->p_vars[i_var];
608 /* Duplicate data if needed */
609 p_var->pf_dup( &val );
611 /* Backup needed stuff */
614 /* Check boundaries and list */
615 CheckValue( p_var, &val );
617 /* Set the variable */
620 /* Deal with callbacks. Tell we're in a callback, release the lock,
621 * call stored functions, retake the lock. */
622 if( p_var->i_entries )
625 int i_entries = p_var->i_entries;
626 callback_entry_t *p_entries = p_var->p_entries;
628 p_var->b_incallback = VLC_TRUE;
629 vlc_mutex_unlock( &p_this->var_lock );
632 for( ; i_entries-- ; )
634 p_entries[i_entries].pf_callback( p_this, psz_name, oldval, val,
635 p_entries[i_entries].p_data );
638 vlc_mutex_lock( &p_this->var_lock );
640 i_var = Lookup( p_this->p_vars, p_this->i_vars, psz_name );
643 msg_Err( p_this, "variable %s has disappeared", psz_name );
644 vlc_mutex_unlock( &p_this->var_lock );
648 p_var = &p_this->p_vars[i_var];
649 p_var->b_incallback = VLC_FALSE;
652 /* Free data if needed */
653 p_var->pf_free( &oldval );
655 vlc_mutex_unlock( &p_this->var_lock );
660 /*****************************************************************************
661 * var_Get: get a variable's value
662 *****************************************************************************
664 *****************************************************************************/
665 int __var_Get( vlc_object_t *p_this, const char *psz_name, vlc_value_t *p_val )
670 vlc_mutex_lock( &p_this->var_lock );
672 i_var = Lookup( p_this->p_vars, p_this->i_vars, psz_name );
676 vlc_mutex_unlock( &p_this->var_lock );
680 p_var = &p_this->p_vars[i_var];
682 /* Really get the variable */
685 /* Duplicate value if needed */
686 p_var->pf_dup( p_val );
688 vlc_mutex_unlock( &p_this->var_lock );
693 /*****************************************************************************
694 * var_AddCallback: register a callback in a variable
695 *****************************************************************************
696 * We store a function pointer pf_callback that will be called upon variable
697 * modification. p_data is a generic pointer that will be passed as additional
698 * argument to the callback function.
699 *****************************************************************************/
700 int __var_AddCallback( vlc_object_t *p_this, const char *psz_name,
701 vlc_callback_t pf_callback, void *p_data )
705 callback_entry_t entry;
707 entry.pf_callback = pf_callback;
708 entry.p_data = p_data;
710 vlc_mutex_lock( &p_this->var_lock );
712 i_var = GetUnused( p_this, psz_name );
715 vlc_mutex_unlock( &p_this->var_lock );
719 p_var = &p_this->p_vars[i_var];
721 INSERT_ELEM( p_var->p_entries,
726 vlc_mutex_unlock( &p_this->var_lock );
731 /*****************************************************************************
732 * var_DelCallback: remove a callback from a variable
733 *****************************************************************************
734 * pf_callback and p_data have to be given again, because different objects
735 * might have registered the same callback function.
736 *****************************************************************************/
737 int __var_DelCallback( vlc_object_t *p_this, const char *psz_name,
738 vlc_callback_t pf_callback, void *p_data )
743 vlc_mutex_lock( &p_this->var_lock );
745 i_var = GetUnused( p_this, psz_name );
748 vlc_mutex_unlock( &p_this->var_lock );
752 p_var = &p_this->p_vars[i_var];
754 for( i_entry = p_var->i_entries ; i_entry-- ; )
756 if( p_var->p_entries[i_entry].pf_callback == pf_callback
757 && p_var->p_entries[i_entry].p_data == p_data )
765 vlc_mutex_unlock( &p_this->var_lock );
769 REMOVE_ELEM( p_var->p_entries, p_var->i_entries, i_entry );
771 vlc_mutex_unlock( &p_this->var_lock );
776 /* Following functions are local */
778 /*****************************************************************************
779 * GetUnused: find an unused variable from its name
780 *****************************************************************************
781 * We do i_tries tries before giving up, just in case the variable is being
782 * modified and called from a callback.
783 *****************************************************************************/
784 static int GetUnused( vlc_object_t *p_this, const char *psz_name )
786 int i_var, i_tries = 0;
790 i_var = Lookup( p_this->p_vars, p_this->i_vars, psz_name );
796 if( ! p_this->p_vars[i_var].b_incallback )
801 if( i_tries++ > 100 )
803 msg_Err( p_this, "caught in a callback deadlock?" );
807 vlc_mutex_unlock( &p_this->var_lock );
808 msleep( THREAD_SLEEP );
809 vlc_mutex_lock( &p_this->var_lock );
813 /*****************************************************************************
814 * HashString: our cool hash function
815 *****************************************************************************
816 * This function is not intended to be crypto-secure, we only want it to be
817 * fast and not suck too much. This one is pretty fast and did 0 collisions
818 * in wenglish's dictionary.
819 *****************************************************************************/
820 static uint32_t HashString( const char *psz_string )
826 i_hash += *psz_string++;
827 i_hash += i_hash << 10;
828 i_hash ^= i_hash >> 8;
834 /*****************************************************************************
835 * Insert: find an empty slot to insert a new variable
836 *****************************************************************************
837 * We use a recursive inner function indexed on the hash. This function does
838 * nothing in the rare cases where a collision may occur, see Lookup()
839 * to see how we handle them.
840 * XXX: does this really need to be written recursively?
841 *****************************************************************************/
842 static int Insert( variable_t *p_vars, int i_count, const char *psz_name )
849 return InsertInner( p_vars, i_count, HashString( psz_name ) );
852 static int InsertInner( variable_t *p_vars, int i_count, uint32_t i_hash )
856 if( i_hash <= p_vars[0].i_hash )
861 if( i_hash >= p_vars[i_count - 1].i_hash )
866 i_middle = i_count / 2;
868 /* We know that 0 < i_middle */
869 if( i_hash < p_vars[i_middle].i_hash )
871 return InsertInner( p_vars, i_middle, i_hash );
874 /* We know that i_middle + 1 < i_count */
875 if( i_hash > p_vars[i_middle + 1].i_hash )
877 return i_middle + 1 + InsertInner( p_vars + i_middle + 1,
878 i_count - i_middle - 1,
885 /*****************************************************************************
886 * Lookup: find an existing variable given its name
887 *****************************************************************************
888 * We use a recursive inner function indexed on the hash. Care is taken of
889 * possible hash collisions.
890 * XXX: does this really need to be written recursively?
891 *****************************************************************************/
892 static int Lookup( variable_t *p_vars, int i_count, const char *psz_name )
902 i_hash = HashString( psz_name );
904 i_pos = LookupInner( p_vars, i_count, i_hash );
907 if( i_hash != p_vars[i_pos].i_hash )
912 /* Hash found, entry found */
913 if( !strcmp( psz_name, p_vars[i_pos].psz_name ) )
918 /* Hash collision! This should be very rare, but we cannot guarantee
919 * it will never happen. Just do an exhaustive search amongst all
920 * entries with the same hash. */
921 for( i = i_pos - 1 ; i > 0 && i_hash == p_vars[i].i_hash ; i-- )
923 if( !strcmp( psz_name, p_vars[i].psz_name ) )
929 for( i = i_pos + 1 ; i < i_count && i_hash == p_vars[i].i_hash ; i++ )
931 if( !strcmp( psz_name, p_vars[i].psz_name ) )
937 /* Hash found, but entry not found */
941 static int LookupInner( variable_t *p_vars, int i_count, uint32_t i_hash )
945 if( i_hash <= p_vars[0].i_hash )
950 if( i_hash >= p_vars[i_count-1].i_hash )
955 i_middle = i_count / 2;
957 /* We know that 0 < i_middle */
958 if( i_hash < p_vars[i_middle].i_hash )
960 return LookupInner( p_vars, i_middle, i_hash );
963 /* We know that i_middle + 1 < i_count */
964 if( i_hash > p_vars[i_middle].i_hash )
966 return i_middle + LookupInner( p_vars + i_middle,
974 /*****************************************************************************
975 * CheckValue: check that a value is valid wrt. a variable
976 *****************************************************************************
977 * This function checks p_val's value against p_var's limitations such as
978 * minimal and maximal value, step, in-list position, and modifies p_val if
980 *****************************************************************************/
981 static void CheckValue ( variable_t *p_var, vlc_value_t *p_val )
983 /* Check that our variable is in the list */
984 if( p_var->i_type & VLC_VAR_HASCHOICE && p_var->choices.i_count )
988 /* FIXME: the list is sorted, dude. Use something cleverer. */
989 for( i = p_var->choices.i_count ; i-- ; )
991 if( p_var->pf_cmp( *p_val, p_var->choices.p_values[i] ) == 0 )
997 /* If not found, change it to anything vaguely valid */
1000 /* Free the old variable, get the new one, dup it */
1001 p_var->pf_free( p_val );
1002 *p_val = p_var->choices.p_values[p_var->i_default >= 0
1003 ? p_var->i_default : 0 ];
1004 p_var->pf_dup( p_val );
1008 /* Check that our variable is within the bounds */
1009 switch( p_var->i_type & VLC_VAR_TYPE )
1011 case VLC_VAR_INTEGER:
1012 if( p_var->i_type & VLC_VAR_HASSTEP && p_var->step.i_int
1013 && (p_val->i_int % p_var->step.i_int) )
1015 p_val->i_int = (p_val->i_int + (p_var->step.i_int / 2))
1016 / p_var->step.i_int * p_var->step.i_int;
1018 if( p_var->i_type & VLC_VAR_HASMIN
1019 && p_val->i_int < p_var->min.i_int )
1021 p_val->i_int = p_var->min.i_int;
1023 if( p_var->i_type & VLC_VAR_HASMAX
1024 && p_val->i_int > p_var->max.i_int )
1026 p_val->i_int = p_var->max.i_int;
1030 if( p_var->i_type & VLC_VAR_HASSTEP && p_var->step.f_float )
1032 float f_round = p_var->step.f_float * (float)(int)( 0.5 +
1033 p_val->f_float / p_var->step.f_float );
1034 if( p_val->f_float != f_round )
1036 p_val->f_float = f_round;
1039 if( p_var->i_type & VLC_VAR_HASMIN
1040 && p_val->f_float < p_var->min.f_float )
1042 p_val->f_float = p_var->min.f_float;
1044 if( p_var->i_type & VLC_VAR_HASMAX
1045 && p_val->f_float > p_var->max.f_float )
1047 p_val->f_float = p_var->max.f_float;