1 /*****************************************************************************
2 * variables.c: routines for object variables handling
3 *****************************************************************************
4 * Copyright (C) 2002 VideoLAN
5 * $Id: variables.c,v 1.22 2003/05/04 22:42:18 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 CmpString( vlc_value_t v, vlc_value_t w ) { return strcmp( v.psz_string, w.psz_string ); }
48 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; }
49 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; }
51 /*****************************************************************************
52 * Local duplication functions, and local deallocation functions
53 *****************************************************************************/
54 static void DupDummy( vlc_value_t *p_val ) { (void)p_val; /* unused */ }
55 static void DupString( vlc_value_t *p_val ) { p_val->psz_string = strdup( p_val->psz_string ); }
57 static void DupList( vlc_value_t *p_val )
60 vlc_list_t *p_list = malloc( sizeof(vlc_list_t) );
62 if( p_val->p_list->i_count )
64 p_list->i_count = p_val->p_list->i_count;
65 p_list->p_values = malloc( p_list->i_count * sizeof(vlc_value_t) );
66 p_list->pi_types = malloc( p_list->i_count * sizeof(int) );
69 for( i = 0; i < p_list->i_count; i++ )
71 p_list->p_values[i] = p_val->p_list->p_values[i];
72 switch( p_val->p_list->pi_types[i] & VLC_VAR_TYPE )
76 DupString( &p_list->p_values[i] );
83 p_val->p_list = p_list;
86 static void FreeDummy( vlc_value_t *p_val ) { (void)p_val; /* unused */ }
87 static void FreeString( vlc_value_t *p_val ) { free( p_val->psz_string ); }
88 static void FreeMutex( vlc_value_t *p_val ) { vlc_mutex_destroy( (vlc_mutex_t*)p_val->p_address ); free( p_val->p_address ); }
90 static void FreeList( vlc_value_t *p_val )
93 for( i = 0; i < p_val->p_list->i_count; i++ )
95 switch( p_val->p_list->pi_types[i] & VLC_VAR_TYPE )
98 FreeString( &p_val->p_list->p_values[i] );
101 FreeMutex( &p_val->p_list->p_values[i] );
108 if( p_val->p_list->i_count )
110 free( p_val->p_list->p_values );
111 free( p_val->p_list->pi_types );
113 free( p_val->p_list );
116 /*****************************************************************************
118 *****************************************************************************/
119 static int GetUnused ( vlc_object_t *, const char * );
120 static uint32_t HashString ( const char * );
121 static int Insert ( variable_t *, int, const char * );
122 static int InsertInner ( variable_t *, int, uint32_t );
123 static int Lookup ( variable_t *, int, const char * );
124 static int LookupInner ( variable_t *, int, uint32_t );
126 static void CheckValue ( variable_t *, vlc_value_t * );
128 /*****************************************************************************
129 * var_Create: initialize a vlc variable
130 *****************************************************************************
131 * We hash the given string and insert it into the sorted list. The insertion
132 * may require slow memory copies, but think about what we gain in the log(n)
133 * lookup phase when setting/getting the variable value!
134 *****************************************************************************/
135 int __var_Create( vlc_object_t *p_this, const char *psz_name, int i_type )
139 static vlc_list_t dummy_null_list = {0, NULL, NULL};
141 vlc_mutex_lock( &p_this->var_lock );
143 /* FIXME: if the variable already exists, we don't duplicate it. But we
144 * duplicate the lookups. It's not that serious, but if anyone finds some
145 * time to rework Insert() so that only one lookup has to be done, feel
147 i_new = Lookup( p_this->p_vars, p_this->i_vars, psz_name );
151 /* If the types differ, variable creation failed. */
152 if( i_type != p_this->p_vars[i_new].i_type )
154 vlc_mutex_unlock( &p_this->var_lock );
158 p_this->p_vars[i_new].i_usage++;
159 vlc_mutex_unlock( &p_this->var_lock );
163 i_new = Insert( p_this->p_vars, p_this->i_vars, psz_name );
165 if( (p_this->i_vars & 15) == 15 )
167 p_this->p_vars = realloc( p_this->p_vars,
168 (p_this->i_vars+17) * sizeof(variable_t) );
171 memmove( p_this->p_vars + i_new + 1,
172 p_this->p_vars + i_new,
173 (p_this->i_vars - i_new) * sizeof(variable_t) );
177 p_var = &p_this->p_vars[i_new];
179 p_var->i_hash = HashString( psz_name );
180 p_var->psz_name = strdup( psz_name );
181 p_var->psz_text = NULL;
183 p_var->i_type = i_type;
184 memset( &p_var->val, 0, sizeof(vlc_value_t) );
186 p_var->pf_dup = DupDummy;
187 p_var->pf_free = FreeDummy;
191 p_var->i_default = -1;
192 p_var->choices.i_count = 0;
193 p_var->choices.p_values = NULL;
194 p_var->choices_text.i_count = 0;
195 p_var->choices_text.p_values = NULL;
197 p_var->b_incallback = VLC_FALSE;
198 p_var->i_entries = 0;
199 p_var->p_entries = NULL;
201 /* Always initialize the variable, even if it is a list variable; this
202 * will lead to errors if the variable is not initialized, but it will
203 * not cause crashes in the variable handling. */
204 switch( i_type & VLC_VAR_TYPE )
207 p_var->pf_cmp = CmpBool;
208 p_var->val.b_bool = VLC_FALSE;
210 case VLC_VAR_INTEGER:
211 p_var->pf_cmp = CmpInt;
212 p_var->val.i_int = 0;
217 case VLC_VAR_DIRECTORY:
218 case VLC_VAR_VARIABLE:
219 p_var->pf_cmp = CmpString;
220 p_var->pf_dup = DupString;
221 p_var->pf_free = FreeString;
222 p_var->val.psz_string = "";
225 p_var->pf_cmp = CmpFloat;
226 p_var->val.f_float = 0.0;
231 case VLC_VAR_ADDRESS:
232 p_var->pf_cmp = CmpAddress;
233 p_var->val.p_address = NULL;
236 p_var->pf_cmp = CmpAddress;
237 p_var->pf_free = FreeMutex;
238 p_var->val.p_address = malloc( sizeof(vlc_mutex_t) );
239 vlc_mutex_init( p_this, (vlc_mutex_t*)p_var->val.p_address );
242 p_var->pf_cmp = CmpAddress;
243 p_var->pf_dup = DupList;
244 p_var->pf_free = FreeList;
245 p_var->val.p_list = &dummy_null_list;
249 /* Duplicate the default data we stored. */
250 p_var->pf_dup( &p_var->val );
252 vlc_mutex_unlock( &p_this->var_lock );
257 /*****************************************************************************
258 * var_Destroy: destroy a vlc variable
259 *****************************************************************************
260 * Look for the variable and destroy it if it is found. As in var_Create we
261 * do a call to memmove() but we have performance counterparts elsewhere.
262 *****************************************************************************/
263 int __var_Destroy( vlc_object_t *p_this, const char *psz_name )
268 vlc_mutex_lock( &p_this->var_lock );
270 i_var = GetUnused( p_this, psz_name );
273 vlc_mutex_unlock( &p_this->var_lock );
277 p_var = &p_this->p_vars[i_var];
279 if( p_var->i_usage > 1 )
282 vlc_mutex_unlock( &p_this->var_lock );
286 /* Free value if needed */
287 p_var->pf_free( &p_var->val );
289 /* Free choice list if needed */
290 if( p_var->choices.i_count )
292 for( i = 0 ; i < p_var->choices.i_count ; i++ )
294 p_var->pf_free( &p_var->choices.p_values[i] );
296 free( p_var->choices.p_values );
299 /* Free callbacks if needed */
300 if( p_var->p_entries )
302 free( p_var->p_entries );
305 free( p_var->psz_name );
306 if( p_var->psz_text ) free( p_var->psz_text );
308 memmove( p_this->p_vars + i_var,
309 p_this->p_vars + i_var + 1,
310 (p_this->i_vars - i_var - 1) * sizeof(variable_t) );
312 if( (p_this->i_vars & 15) == 0 )
314 p_this->p_vars = realloc( p_this->p_vars,
315 (p_this->i_vars) * sizeof( variable_t ) );
320 vlc_mutex_unlock( &p_this->var_lock );
325 /*****************************************************************************
326 * var_Change: perform an action on a variable
327 *****************************************************************************
329 *****************************************************************************/
330 int __var_Change( vlc_object_t *p_this, const char *psz_name,
331 int i_action, vlc_value_t *p_val, vlc_value_t *p_val2 )
337 vlc_mutex_lock( &p_this->var_lock );
339 i_var = Lookup( p_this->p_vars, p_this->i_vars, psz_name );
343 vlc_mutex_unlock( &p_this->var_lock );
347 p_var = &p_this->p_vars[i_var];
352 if( p_var->i_type & VLC_VAR_HASMIN )
354 p_var->pf_free( &p_var->min );
356 p_var->i_type |= VLC_VAR_HASMIN;
358 p_var->pf_dup( &p_var->min );
359 CheckValue( p_var, &p_var->val );
362 if( p_var->i_type & VLC_VAR_HASMAX )
364 p_var->pf_free( &p_var->max );
366 p_var->i_type |= VLC_VAR_HASMAX;
368 p_var->pf_dup( &p_var->max );
369 CheckValue( p_var, &p_var->val );
371 case VLC_VAR_SETSTEP:
372 if( p_var->i_type & VLC_VAR_HASSTEP )
374 p_var->pf_free( &p_var->step );
376 p_var->i_type |= VLC_VAR_HASSTEP;
377 p_var->step = *p_val;
378 p_var->pf_dup( &p_var->step );
379 CheckValue( p_var, &p_var->val );
381 case VLC_VAR_ADDCHOICE:
382 /* FIXME: the list is sorted, dude. Use something cleverer. */
383 for( i = p_var->choices.i_count ; i-- ; )
385 if( p_var->pf_cmp( p_var->choices.p_values[i], *p_val ) < 0 )
391 /* The new place is i+1 */
394 if( p_var->i_default >= i )
399 INSERT_ELEM( p_var->choices.p_values, p_var->choices.i_count,
401 INSERT_ELEM( p_var->choices_text.p_values,
402 p_var->choices_text.i_count, i, (vlc_value_t)0 );
403 p_var->pf_dup( &p_var->choices.p_values[i] );
404 p_var->choices_text.p_values[i].psz_string =
405 ( p_val2 && p_val2->psz_string ) ?
406 strdup( p_val2->psz_string ) : NULL;
408 CheckValue( p_var, &p_var->val );
410 case VLC_VAR_DELCHOICE:
411 /* FIXME: the list is sorted, dude. Use something cleverer. */
412 for( i = 0 ; i < p_var->choices.i_count ; i++ )
414 if( p_var->pf_cmp( p_var->choices.p_values[i], *p_val ) == 0 )
420 if( i == p_var->choices.i_count )
423 vlc_mutex_unlock( &p_this->var_lock );
427 if( p_var->i_default > i )
431 else if( p_var->i_default == i )
433 p_var->i_default = -1;
436 p_var->pf_free( &p_var->choices.p_values[i] );
437 if( p_var->choices_text.p_values[i].psz_string )
438 free( p_var->choices_text.p_values[i].psz_string );
439 REMOVE_ELEM( p_var->choices.p_values, p_var->choices.i_count, i );
440 REMOVE_ELEM( p_var->choices_text.p_values,
441 p_var->choices_text.i_count, i );
443 CheckValue( p_var, &p_var->val );
445 case VLC_VAR_CHOICESCOUNT:
446 p_val->i_int = p_var->choices.i_count;
448 case VLC_VAR_CLEARCHOICES:
449 for( i = 0 ; i < p_var->choices.i_count ; i++ )
451 p_var->pf_free( &p_var->choices.p_values[i] );
453 if( p_var->choices.i_count )
454 free( p_var->choices.p_values );
456 p_var->choices.i_count = 0;
457 p_var->choices.p_values = NULL;
458 p_var->i_default = -1;
460 case VLC_VAR_SETDEFAULT:
461 /* FIXME: the list is sorted, dude. Use something cleverer. */
462 for( i = 0 ; i < p_var->choices.i_count ; i++ )
464 if( p_var->pf_cmp( p_var->choices.p_values[i], *p_val ) == 0 )
470 if( i == p_var->choices.i_count )
476 p_var->i_default = i;
477 CheckValue( p_var, &p_var->val );
479 case VLC_VAR_SETVALUE:
480 /* Duplicate data if needed */
481 p_var->pf_dup( p_val );
482 /* Backup needed stuff */
484 /* Check boundaries and list */
485 CheckValue( p_var, p_val );
486 /* Set the variable */
488 /* Free data if needed */
489 p_var->pf_free( &oldval );
491 case VLC_VAR_GETCHOICES:
492 case VLC_VAR_GETLIST:
493 p_val->p_list = malloc( sizeof(vlc_list_t) );
494 if( p_val2 ) p_val2->p_list = malloc( sizeof(vlc_list_t) );
495 if( p_var->choices.i_count )
497 p_val->p_list->p_values = malloc( p_var->choices.i_count
498 * sizeof(vlc_value_t) );
499 p_val->p_list->pi_types = malloc( p_var->choices.i_count
503 p_val2->p_list->p_values =
504 malloc( p_var->choices.i_count * sizeof(vlc_value_t) );
505 p_val2->p_list->pi_types =
506 malloc( p_var->choices.i_count * sizeof(int) );
509 p_val->p_list->i_count = p_var->choices.i_count;
510 if( p_val2 ) p_val2->p_list->i_count = p_var->choices.i_count;
511 for( i = 0 ; i < p_var->choices.i_count ; i++ )
513 p_val->p_list->p_values[i] = p_var->choices.p_values[i];
514 p_val->p_list->pi_types[i] = p_var->i_type;
515 p_var->pf_dup( &p_val->p_list->p_values[i] );
518 p_val2->p_list->p_values[i].psz_string =
519 p_var->choices_text.p_values[i].psz_string ?
520 strdup(p_var->choices_text.p_values[i].psz_string) : NULL;
521 p_val2->p_list->pi_types[i] = VLC_VAR_STRING;
525 case VLC_VAR_FREELIST:
528 case VLC_VAR_SETTEXT:
529 if( p_var->psz_text ) free( p_var->psz_text );
530 if( p_val && p_val->psz_string )
531 p_var->psz_text = strdup( p_val->psz_string );
533 case VLC_VAR_GETTEXT:
534 p_val->psz_string = NULL;
535 if( p_var->psz_text )
537 p_val->psz_string = strdup( p_var->psz_text );
545 vlc_mutex_unlock( &p_this->var_lock );
550 /*****************************************************************************
551 * var_Type: request a variable's type
552 *****************************************************************************
553 * This function returns the variable type if it exists, or 0 if the
554 * variable could not be found.
555 *****************************************************************************/
556 int __var_Type( vlc_object_t *p_this, const char *psz_name )
560 vlc_mutex_lock( &p_this->var_lock );
562 i_var = Lookup( p_this->p_vars, p_this->i_vars, psz_name );
566 vlc_mutex_unlock( &p_this->var_lock );
570 i_type = p_this->p_vars[i_var].i_type;
572 vlc_mutex_unlock( &p_this->var_lock );
577 /*****************************************************************************
578 * var_Set: set a variable's value
579 *****************************************************************************
581 *****************************************************************************/
582 int __var_Set( vlc_object_t *p_this, const char *psz_name, vlc_value_t val )
588 vlc_mutex_lock( &p_this->var_lock );
590 i_var = GetUnused( p_this, psz_name );
593 vlc_mutex_unlock( &p_this->var_lock );
597 p_var = &p_this->p_vars[i_var];
599 /* Duplicate data if needed */
600 p_var->pf_dup( &val );
602 /* Backup needed stuff */
605 /* Check boundaries and list */
606 CheckValue( p_var, &val );
608 /* Set the variable */
611 /* Deal with callbacks. Tell we're in a callback, release the lock,
612 * call stored functions, retake the lock. */
613 if( p_var->i_entries )
616 int i_entries = p_var->i_entries;
617 callback_entry_t *p_entries = p_var->p_entries;
619 p_var->b_incallback = VLC_TRUE;
620 vlc_mutex_unlock( &p_this->var_lock );
623 for( ; i_entries-- ; )
625 p_entries[i_entries].pf_callback( p_this, psz_name, oldval, val,
626 p_entries[i_entries].p_data );
629 vlc_mutex_lock( &p_this->var_lock );
631 i_var = Lookup( p_this->p_vars, p_this->i_vars, psz_name );
634 msg_Err( p_this, "variable %s has disappeared", psz_name );
635 vlc_mutex_unlock( &p_this->var_lock );
639 p_var = &p_this->p_vars[i_var];
640 p_var->b_incallback = VLC_FALSE;
643 /* Free data if needed */
644 p_var->pf_free( &oldval );
646 vlc_mutex_unlock( &p_this->var_lock );
651 /*****************************************************************************
652 * var_Get: get a variable's value
653 *****************************************************************************
655 *****************************************************************************/
656 int __var_Get( vlc_object_t *p_this, const char *psz_name, vlc_value_t *p_val )
661 vlc_mutex_lock( &p_this->var_lock );
663 i_var = Lookup( p_this->p_vars, p_this->i_vars, psz_name );
667 vlc_mutex_unlock( &p_this->var_lock );
671 p_var = &p_this->p_vars[i_var];
673 /* Really get the variable */
676 /* Duplicate value if needed */
677 p_var->pf_dup( p_val );
679 vlc_mutex_unlock( &p_this->var_lock );
684 /*****************************************************************************
685 * var_AddCallback: register a callback in a variable
686 *****************************************************************************
687 * We store a function pointer pf_callback that will be called upon variable
688 * modification. p_data is a generic pointer that will be passed as additional
689 * argument to the callback function.
690 *****************************************************************************/
691 int __var_AddCallback( vlc_object_t *p_this, const char *psz_name,
692 vlc_callback_t pf_callback, void *p_data )
696 callback_entry_t entry;
698 entry.pf_callback = pf_callback;
699 entry.p_data = p_data;
701 vlc_mutex_lock( &p_this->var_lock );
703 i_var = GetUnused( p_this, psz_name );
706 vlc_mutex_unlock( &p_this->var_lock );
710 p_var = &p_this->p_vars[i_var];
712 INSERT_ELEM( p_var->p_entries,
717 vlc_mutex_unlock( &p_this->var_lock );
722 /*****************************************************************************
723 * var_DelCallback: remove a callback from a variable
724 *****************************************************************************
725 * pf_callback and p_data have to be given again, because different objects
726 * might have registered the same callback function.
727 *****************************************************************************/
728 int __var_DelCallback( vlc_object_t *p_this, const char *psz_name,
729 vlc_callback_t pf_callback, void *p_data )
734 vlc_mutex_lock( &p_this->var_lock );
736 i_var = GetUnused( p_this, psz_name );
739 vlc_mutex_unlock( &p_this->var_lock );
743 p_var = &p_this->p_vars[i_var];
745 for( i_entry = p_var->i_entries ; i_entry-- ; )
747 if( p_var->p_entries[i_entry].pf_callback == pf_callback
748 && p_var->p_entries[i_entry].p_data == p_data )
756 vlc_mutex_unlock( &p_this->var_lock );
760 REMOVE_ELEM( p_var->p_entries, p_var->i_entries, i_entry );
762 vlc_mutex_unlock( &p_this->var_lock );
767 /* Following functions are local */
769 /*****************************************************************************
770 * GetUnused: find an unused variable from its name
771 *****************************************************************************
772 * We do i_tries tries before giving up, just in case the variable is being
773 * modified and called from a callback.
774 *****************************************************************************/
775 static int GetUnused( vlc_object_t *p_this, const char *psz_name )
777 int i_var, i_tries = 0;
781 i_var = Lookup( p_this->p_vars, p_this->i_vars, psz_name );
787 if( ! p_this->p_vars[i_var].b_incallback )
792 if( i_tries++ > 100 )
794 msg_Err( p_this, "caught in a callback deadlock?" );
798 vlc_mutex_unlock( &p_this->var_lock );
799 msleep( THREAD_SLEEP );
800 vlc_mutex_lock( &p_this->var_lock );
804 /*****************************************************************************
805 * HashString: our cool hash function
806 *****************************************************************************
807 * This function is not intended to be crypto-secure, we only want it to be
808 * fast and not suck too much. This one is pretty fast and did 0 collisions
809 * in wenglish's dictionary.
810 *****************************************************************************/
811 static uint32_t HashString( const char *psz_string )
817 i_hash += *psz_string++;
818 i_hash += i_hash << 10;
819 i_hash ^= i_hash >> 8;
825 /*****************************************************************************
826 * Insert: find an empty slot to insert a new variable
827 *****************************************************************************
828 * We use a recursive inner function indexed on the hash. This function does
829 * nothing in the rare cases where a collision may occur, see Lookup()
830 * to see how we handle them.
831 * XXX: does this really need to be written recursively?
832 *****************************************************************************/
833 static int Insert( variable_t *p_vars, int i_count, const char *psz_name )
840 return InsertInner( p_vars, i_count, HashString( psz_name ) );
843 static int InsertInner( variable_t *p_vars, int i_count, uint32_t i_hash )
847 if( i_hash <= p_vars[0].i_hash )
852 if( i_hash >= p_vars[i_count - 1].i_hash )
857 i_middle = i_count / 2;
859 /* We know that 0 < i_middle */
860 if( i_hash < p_vars[i_middle].i_hash )
862 return InsertInner( p_vars, i_middle, i_hash );
865 /* We know that i_middle + 1 < i_count */
866 if( i_hash > p_vars[i_middle + 1].i_hash )
868 return i_middle + 1 + InsertInner( p_vars + i_middle + 1,
869 i_count - i_middle - 1,
876 /*****************************************************************************
877 * Lookup: find an existing variable given its name
878 *****************************************************************************
879 * We use a recursive inner function indexed on the hash. Care is taken of
880 * possible hash collisions.
881 * XXX: does this really need to be written recursively?
882 *****************************************************************************/
883 static int Lookup( variable_t *p_vars, int i_count, const char *psz_name )
893 i_hash = HashString( psz_name );
895 i_pos = LookupInner( p_vars, i_count, i_hash );
898 if( i_hash != p_vars[i_pos].i_hash )
903 /* Hash found, entry found */
904 if( !strcmp( psz_name, p_vars[i_pos].psz_name ) )
909 /* Hash collision! This should be very rare, but we cannot guarantee
910 * it will never happen. Just do an exhaustive search amongst all
911 * entries with the same hash. */
912 for( i = i_pos - 1 ; i > 0 && i_hash == p_vars[i].i_hash ; i-- )
914 if( !strcmp( psz_name, p_vars[i].psz_name ) )
920 for( i = i_pos + 1 ; i < i_count && i_hash == p_vars[i].i_hash ; i++ )
922 if( !strcmp( psz_name, p_vars[i].psz_name ) )
928 /* Hash found, but entry not found */
932 static int LookupInner( variable_t *p_vars, int i_count, uint32_t i_hash )
936 if( i_hash <= p_vars[0].i_hash )
941 if( i_hash >= p_vars[i_count-1].i_hash )
946 i_middle = i_count / 2;
948 /* We know that 0 < i_middle */
949 if( i_hash < p_vars[i_middle].i_hash )
951 return LookupInner( p_vars, i_middle, i_hash );
954 /* We know that i_middle + 1 < i_count */
955 if( i_hash > p_vars[i_middle].i_hash )
957 return i_middle + LookupInner( p_vars + i_middle,
965 /*****************************************************************************
966 * CheckValue: check that a value is valid wrt. a variable
967 *****************************************************************************
968 * This function checks p_val's value against p_var's limitations such as
969 * minimal and maximal value, step, in-list position, and modifies p_val if
971 *****************************************************************************/
972 static void CheckValue ( variable_t *p_var, vlc_value_t *p_val )
974 /* Check that our variable is in the list */
975 if( p_var->i_type & VLC_VAR_HASCHOICE && p_var->choices.i_count )
979 /* FIXME: the list is sorted, dude. Use something cleverer. */
980 for( i = p_var->choices.i_count ; i-- ; )
982 if( p_var->pf_cmp( *p_val, p_var->choices.p_values[i] ) == 0 )
988 /* If not found, change it to anything vaguely valid */
991 /* Free the old variable, get the new one, dup it */
992 p_var->pf_free( p_val );
993 *p_val = p_var->choices.p_values[p_var->i_default >= 0
994 ? p_var->i_default : 0 ];
995 p_var->pf_dup( p_val );
999 /* Check that our variable is within the bounds */
1000 switch( p_var->i_type & VLC_VAR_TYPE )
1002 case VLC_VAR_INTEGER:
1003 if( p_var->i_type & VLC_VAR_HASSTEP && p_var->step.i_int
1004 && (p_val->i_int % p_var->step.i_int) )
1006 p_val->i_int = (p_val->i_int + (p_var->step.i_int / 2))
1007 / p_var->step.i_int * p_var->step.i_int;
1009 if( p_var->i_type & VLC_VAR_HASMIN
1010 && p_val->i_int < p_var->min.i_int )
1012 p_val->i_int = p_var->min.i_int;
1014 if( p_var->i_type & VLC_VAR_HASMAX
1015 && p_val->i_int > p_var->max.i_int )
1017 p_val->i_int = p_var->max.i_int;
1021 if( p_var->i_type & VLC_VAR_HASSTEP && p_var->step.f_float )
1023 float f_round = p_var->step.f_float * (float)(int)( 0.5 +
1024 p_val->f_float / p_var->step.f_float );
1025 if( p_val->f_float != f_round )
1027 p_val->f_float = f_round;
1030 if( p_var->i_type & VLC_VAR_HASMIN
1031 && p_val->f_float < p_var->min.f_float )
1033 p_val->f_float = p_var->min.f_float;
1035 if( p_var->i_type & VLC_VAR_HASMAX
1036 && p_val->f_float > p_var->max.f_float )
1038 p_val->f_float = p_var->max.f_float;