1 /*****************************************************************************
2 * variables.c: routines for object variables handling
3 *****************************************************************************
4 * Copyright (C) 2002 VideoLAN
5 * $Id: variables.c,v 1.27 2003/07/22 15:59:06 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 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 static int InheritValue( vlc_object_t *, const char *, vlc_value_t *,
138 /*****************************************************************************
139 * var_Create: initialize a vlc variable
140 *****************************************************************************
141 * We hash the given string and insert it into the sorted list. The insertion
142 * may require slow memory copies, but think about what we gain in the log(n)
143 * lookup phase when setting/getting the variable value!
144 *****************************************************************************/
145 int __var_Create( vlc_object_t *p_this, const char *psz_name, int i_type )
149 static vlc_list_t dummy_null_list = {0, NULL, NULL};
151 vlc_mutex_lock( &p_this->var_lock );
153 /* FIXME: if the variable already exists, we don't duplicate it. But we
154 * duplicate the lookups. It's not that serious, but if anyone finds some
155 * time to rework Insert() so that only one lookup has to be done, feel
157 i_new = Lookup( p_this->p_vars, p_this->i_vars, psz_name );
161 /* If the types differ, variable creation failed. */
162 if( i_type != p_this->p_vars[i_new].i_type )
164 vlc_mutex_unlock( &p_this->var_lock );
168 p_this->p_vars[i_new].i_usage++;
169 vlc_mutex_unlock( &p_this->var_lock );
173 i_new = Insert( p_this->p_vars, p_this->i_vars, psz_name );
175 if( (p_this->i_vars & 15) == 15 )
177 p_this->p_vars = realloc( p_this->p_vars,
178 (p_this->i_vars+17) * sizeof(variable_t) );
181 memmove( p_this->p_vars + i_new + 1,
182 p_this->p_vars + i_new,
183 (p_this->i_vars - i_new) * sizeof(variable_t) );
187 p_var = &p_this->p_vars[i_new];
189 p_var->i_hash = HashString( psz_name );
190 p_var->psz_name = strdup( psz_name );
191 p_var->psz_text = NULL;
193 p_var->i_type = i_type;
194 memset( &p_var->val, 0, sizeof(vlc_value_t) );
196 p_var->pf_dup = DupDummy;
197 p_var->pf_free = FreeDummy;
201 p_var->i_default = -1;
202 p_var->choices.i_count = 0;
203 p_var->choices.p_values = NULL;
204 p_var->choices_text.i_count = 0;
205 p_var->choices_text.p_values = NULL;
207 p_var->b_incallback = VLC_FALSE;
208 p_var->i_entries = 0;
209 p_var->p_entries = NULL;
211 /* Always initialize the variable, even if it is a list variable; this
212 * will lead to errors if the variable is not initialized, but it will
213 * not cause crashes in the variable handling. */
214 switch( i_type & VLC_VAR_TYPE )
217 p_var->pf_cmp = CmpBool;
218 p_var->val.b_bool = VLC_FALSE;
220 case VLC_VAR_INTEGER:
221 p_var->pf_cmp = CmpInt;
222 p_var->val.i_int = 0;
227 case VLC_VAR_DIRECTORY:
228 case VLC_VAR_VARIABLE:
229 p_var->pf_cmp = CmpString;
230 p_var->pf_dup = DupString;
231 p_var->pf_free = FreeString;
232 p_var->val.psz_string = "";
235 p_var->pf_cmp = CmpFloat;
236 p_var->val.f_float = 0.0;
239 p_var->pf_cmp = CmpTime;
240 p_var->val.time.i_low = 0;
241 p_var->val.time.i_high = 0;
243 case VLC_VAR_ADDRESS:
244 p_var->pf_cmp = CmpAddress;
245 p_var->val.p_address = NULL;
248 p_var->pf_cmp = CmpAddress;
249 p_var->pf_free = FreeMutex;
250 p_var->val.p_address = malloc( sizeof(vlc_mutex_t) );
251 vlc_mutex_init( p_this, (vlc_mutex_t*)p_var->val.p_address );
254 p_var->pf_cmp = CmpAddress;
255 p_var->pf_dup = DupList;
256 p_var->pf_free = FreeList;
257 p_var->val.p_list = &dummy_null_list;
261 /* Duplicate the default data we stored. */
262 p_var->pf_dup( &p_var->val );
264 vlc_mutex_unlock( &p_this->var_lock );
269 /*****************************************************************************
270 * var_Destroy: destroy a vlc variable
271 *****************************************************************************
272 * Look for the variable and destroy it if it is found. As in var_Create we
273 * do a call to memmove() but we have performance counterparts elsewhere.
274 *****************************************************************************/
275 int __var_Destroy( vlc_object_t *p_this, const char *psz_name )
280 vlc_mutex_lock( &p_this->var_lock );
282 i_var = GetUnused( p_this, psz_name );
285 vlc_mutex_unlock( &p_this->var_lock );
289 p_var = &p_this->p_vars[i_var];
291 if( p_var->i_usage > 1 )
294 vlc_mutex_unlock( &p_this->var_lock );
298 /* Free value if needed */
299 p_var->pf_free( &p_var->val );
301 /* Free choice list if needed */
302 if( p_var->choices.i_count )
304 for( i = 0 ; i < p_var->choices.i_count ; i++ )
306 p_var->pf_free( &p_var->choices.p_values[i] );
307 if( p_var->choices_text.p_values[i].psz_string )
308 free( p_var->choices_text.p_values[i].psz_string );
310 free( p_var->choices.p_values );
311 free( p_var->choices_text.p_values );
314 /* Free callbacks if needed */
315 if( p_var->p_entries )
317 free( p_var->p_entries );
320 free( p_var->psz_name );
321 if( p_var->psz_text ) free( p_var->psz_text );
323 memmove( p_this->p_vars + i_var,
324 p_this->p_vars + i_var + 1,
325 (p_this->i_vars - i_var - 1) * sizeof(variable_t) );
327 if( (p_this->i_vars & 15) == 0 )
329 p_this->p_vars = realloc( p_this->p_vars,
330 (p_this->i_vars) * sizeof( variable_t ) );
335 vlc_mutex_unlock( &p_this->var_lock );
340 /*****************************************************************************
341 * var_Change: perform an action on a variable
342 *****************************************************************************
344 *****************************************************************************/
345 int __var_Change( vlc_object_t *p_this, const char *psz_name,
346 int i_action, vlc_value_t *p_val, vlc_value_t *p_val2 )
352 vlc_mutex_lock( &p_this->var_lock );
354 i_var = Lookup( p_this->p_vars, p_this->i_vars, psz_name );
358 vlc_mutex_unlock( &p_this->var_lock );
362 p_var = &p_this->p_vars[i_var];
367 if( p_var->i_type & VLC_VAR_HASMIN )
369 p_var->pf_free( &p_var->min );
371 p_var->i_type |= VLC_VAR_HASMIN;
373 p_var->pf_dup( &p_var->min );
374 CheckValue( p_var, &p_var->val );
377 if( p_var->i_type & VLC_VAR_HASMAX )
379 p_var->pf_free( &p_var->max );
381 p_var->i_type |= VLC_VAR_HASMAX;
383 p_var->pf_dup( &p_var->max );
384 CheckValue( p_var, &p_var->val );
386 case VLC_VAR_SETSTEP:
387 if( p_var->i_type & VLC_VAR_HASSTEP )
389 p_var->pf_free( &p_var->step );
391 p_var->i_type |= VLC_VAR_HASSTEP;
392 p_var->step = *p_val;
393 p_var->pf_dup( &p_var->step );
394 CheckValue( p_var, &p_var->val );
396 case VLC_VAR_ADDCHOICE:
397 /* FIXME: the list is sorted, dude. Use something cleverer. */
398 for( i = p_var->choices.i_count ; i-- ; )
400 if( p_var->pf_cmp( p_var->choices.p_values[i], *p_val ) < 0 )
406 /* The new place is i+1 */
409 if( p_var->i_default >= i )
414 INSERT_ELEM( p_var->choices.p_values, p_var->choices.i_count,
416 INSERT_ELEM( p_var->choices_text.p_values,
417 p_var->choices_text.i_count, i, *p_val );
418 p_var->pf_dup( &p_var->choices.p_values[i] );
419 p_var->choices_text.p_values[i].psz_string =
420 ( p_val2 && p_val2->psz_string ) ?
421 strdup( p_val2->psz_string ) : NULL;
423 CheckValue( p_var, &p_var->val );
425 case VLC_VAR_DELCHOICE:
426 /* FIXME: the list is sorted, dude. Use something cleverer. */
427 for( i = 0 ; i < p_var->choices.i_count ; i++ )
429 if( p_var->pf_cmp( p_var->choices.p_values[i], *p_val ) == 0 )
435 if( i == p_var->choices.i_count )
438 vlc_mutex_unlock( &p_this->var_lock );
442 if( p_var->i_default > i )
446 else if( p_var->i_default == i )
448 p_var->i_default = -1;
451 p_var->pf_free( &p_var->choices.p_values[i] );
452 if( p_var->choices_text.p_values[i].psz_string )
453 free( p_var->choices_text.p_values[i].psz_string );
454 REMOVE_ELEM( p_var->choices.p_values, p_var->choices.i_count, i );
455 REMOVE_ELEM( p_var->choices_text.p_values,
456 p_var->choices_text.i_count, i );
458 CheckValue( p_var, &p_var->val );
460 case VLC_VAR_CHOICESCOUNT:
461 p_val->i_int = p_var->choices.i_count;
463 case VLC_VAR_CLEARCHOICES:
464 for( i = 0 ; i < p_var->choices.i_count ; i++ )
466 p_var->pf_free( &p_var->choices.p_values[i] );
468 if( p_var->choices.i_count )
469 free( p_var->choices.p_values );
471 p_var->choices.i_count = 0;
472 p_var->choices.p_values = NULL;
473 p_var->i_default = -1;
475 case VLC_VAR_SETDEFAULT:
476 /* FIXME: the list is sorted, dude. Use something cleverer. */
477 for( i = 0 ; i < p_var->choices.i_count ; i++ )
479 if( p_var->pf_cmp( p_var->choices.p_values[i], *p_val ) == 0 )
485 if( i == p_var->choices.i_count )
491 p_var->i_default = i;
492 CheckValue( p_var, &p_var->val );
494 case VLC_VAR_SETVALUE:
495 /* Duplicate data if needed */
496 p_var->pf_dup( p_val );
497 /* Backup needed stuff */
499 /* Check boundaries and list */
500 CheckValue( p_var, p_val );
501 /* Set the variable */
503 /* Free data if needed */
504 p_var->pf_free( &oldval );
506 case VLC_VAR_GETCHOICES:
507 case VLC_VAR_GETLIST:
508 p_val->p_list = malloc( sizeof(vlc_list_t) );
509 if( p_val2 ) p_val2->p_list = malloc( sizeof(vlc_list_t) );
510 if( p_var->choices.i_count )
512 p_val->p_list->p_values = malloc( p_var->choices.i_count
513 * sizeof(vlc_value_t) );
514 p_val->p_list->pi_types = malloc( p_var->choices.i_count
518 p_val2->p_list->p_values =
519 malloc( p_var->choices.i_count * sizeof(vlc_value_t) );
520 p_val2->p_list->pi_types =
521 malloc( p_var->choices.i_count * sizeof(int) );
524 p_val->p_list->i_count = p_var->choices.i_count;
525 if( p_val2 ) p_val2->p_list->i_count = p_var->choices.i_count;
526 for( i = 0 ; i < p_var->choices.i_count ; i++ )
528 p_val->p_list->p_values[i] = p_var->choices.p_values[i];
529 p_val->p_list->pi_types[i] = p_var->i_type;
530 p_var->pf_dup( &p_val->p_list->p_values[i] );
533 p_val2->p_list->p_values[i].psz_string =
534 p_var->choices_text.p_values[i].psz_string ?
535 strdup(p_var->choices_text.p_values[i].psz_string) : NULL;
536 p_val2->p_list->pi_types[i] = VLC_VAR_STRING;
540 case VLC_VAR_FREELIST:
543 case VLC_VAR_SETTEXT:
544 if( p_var->psz_text ) free( p_var->psz_text );
545 if( p_val && p_val->psz_string )
546 p_var->psz_text = strdup( p_val->psz_string );
548 case VLC_VAR_GETTEXT:
549 p_val->psz_string = NULL;
550 if( p_var->psz_text )
552 p_val->psz_string = strdup( p_var->psz_text );
555 case VLC_VAR_INHERITVALUE:
559 if( InheritValue( p_this, psz_name, &val, p_var->i_type )
562 /* Duplicate already done */
564 /* Backup needed stuff */
566 /* Check boundaries and list */
567 CheckValue( p_var, &val );
568 /* Set the variable */
570 /* Free data if needed */
571 p_var->pf_free( &oldval );
577 p_var->pf_dup( p_val );
586 vlc_mutex_unlock( &p_this->var_lock );
591 /*****************************************************************************
592 * var_Type: request a variable's type
593 *****************************************************************************
594 * This function returns the variable type if it exists, or 0 if the
595 * variable could not be found.
596 *****************************************************************************/
597 int __var_Type( vlc_object_t *p_this, const char *psz_name )
601 vlc_mutex_lock( &p_this->var_lock );
603 i_var = Lookup( p_this->p_vars, p_this->i_vars, psz_name );
607 vlc_mutex_unlock( &p_this->var_lock );
611 i_type = p_this->p_vars[i_var].i_type;
613 vlc_mutex_unlock( &p_this->var_lock );
618 /*****************************************************************************
619 * var_Set: set a variable's value
620 *****************************************************************************
622 *****************************************************************************/
623 int __var_Set( vlc_object_t *p_this, const char *psz_name, vlc_value_t val )
629 vlc_mutex_lock( &p_this->var_lock );
631 i_var = GetUnused( p_this, psz_name );
634 vlc_mutex_unlock( &p_this->var_lock );
638 p_var = &p_this->p_vars[i_var];
640 /* Duplicate data if needed */
641 p_var->pf_dup( &val );
643 /* Backup needed stuff */
646 /* Check boundaries and list */
647 CheckValue( p_var, &val );
649 /* Set the variable */
652 /* Deal with callbacks. Tell we're in a callback, release the lock,
653 * call stored functions, retake the lock. */
654 if( p_var->i_entries )
657 int i_entries = p_var->i_entries;
658 callback_entry_t *p_entries = p_var->p_entries;
660 p_var->b_incallback = VLC_TRUE;
661 vlc_mutex_unlock( &p_this->var_lock );
664 for( ; i_entries-- ; )
666 p_entries[i_entries].pf_callback( p_this, psz_name, oldval, val,
667 p_entries[i_entries].p_data );
670 vlc_mutex_lock( &p_this->var_lock );
672 i_var = Lookup( p_this->p_vars, p_this->i_vars, psz_name );
675 msg_Err( p_this, "variable %s has disappeared", psz_name );
676 vlc_mutex_unlock( &p_this->var_lock );
680 p_var = &p_this->p_vars[i_var];
681 p_var->b_incallback = VLC_FALSE;
684 /* Free data if needed */
685 p_var->pf_free( &oldval );
687 vlc_mutex_unlock( &p_this->var_lock );
692 /*****************************************************************************
693 * var_Get: get a variable's value
694 *****************************************************************************
696 *****************************************************************************/
697 int __var_Get( vlc_object_t *p_this, const char *psz_name, vlc_value_t *p_val )
702 vlc_mutex_lock( &p_this->var_lock );
704 i_var = Lookup( p_this->p_vars, p_this->i_vars, psz_name );
708 vlc_mutex_unlock( &p_this->var_lock );
712 p_var = &p_this->p_vars[i_var];
714 /* Really get the variable */
717 /* Duplicate value if needed */
718 p_var->pf_dup( p_val );
720 vlc_mutex_unlock( &p_this->var_lock );
725 /*****************************************************************************
726 * var_AddCallback: register a callback in a variable
727 *****************************************************************************
728 * We store a function pointer pf_callback that will be called upon variable
729 * modification. p_data is a generic pointer that will be passed as additional
730 * argument to the callback function.
731 *****************************************************************************/
732 int __var_AddCallback( vlc_object_t *p_this, const char *psz_name,
733 vlc_callback_t pf_callback, void *p_data )
737 callback_entry_t entry;
739 entry.pf_callback = pf_callback;
740 entry.p_data = p_data;
742 vlc_mutex_lock( &p_this->var_lock );
744 i_var = GetUnused( p_this, psz_name );
747 vlc_mutex_unlock( &p_this->var_lock );
751 p_var = &p_this->p_vars[i_var];
753 INSERT_ELEM( p_var->p_entries,
758 vlc_mutex_unlock( &p_this->var_lock );
763 /*****************************************************************************
764 * var_DelCallback: remove a callback from a variable
765 *****************************************************************************
766 * pf_callback and p_data have to be given again, because different objects
767 * might have registered the same callback function.
768 *****************************************************************************/
769 int __var_DelCallback( vlc_object_t *p_this, const char *psz_name,
770 vlc_callback_t pf_callback, void *p_data )
775 vlc_mutex_lock( &p_this->var_lock );
777 i_var = GetUnused( p_this, psz_name );
780 vlc_mutex_unlock( &p_this->var_lock );
784 p_var = &p_this->p_vars[i_var];
786 for( i_entry = p_var->i_entries ; i_entry-- ; )
788 if( p_var->p_entries[i_entry].pf_callback == pf_callback
789 && p_var->p_entries[i_entry].p_data == p_data )
797 vlc_mutex_unlock( &p_this->var_lock );
801 REMOVE_ELEM( p_var->p_entries, p_var->i_entries, i_entry );
803 vlc_mutex_unlock( &p_this->var_lock );
808 /* Following functions are local */
810 /*****************************************************************************
811 * GetUnused: find an unused variable from its name
812 *****************************************************************************
813 * We do i_tries tries before giving up, just in case the variable is being
814 * modified and called from a callback.
815 *****************************************************************************/
816 static int GetUnused( vlc_object_t *p_this, const char *psz_name )
818 int i_var, i_tries = 0;
822 i_var = Lookup( p_this->p_vars, p_this->i_vars, psz_name );
828 if( ! p_this->p_vars[i_var].b_incallback )
833 if( i_tries++ > 100 )
835 msg_Err( p_this, "caught in a callback deadlock?" );
839 vlc_mutex_unlock( &p_this->var_lock );
840 msleep( THREAD_SLEEP );
841 vlc_mutex_lock( &p_this->var_lock );
845 /*****************************************************************************
846 * HashString: our cool hash function
847 *****************************************************************************
848 * This function is not intended to be crypto-secure, we only want it to be
849 * fast and not suck too much. This one is pretty fast and did 0 collisions
850 * in wenglish's dictionary.
851 *****************************************************************************/
852 static uint32_t HashString( const char *psz_string )
858 i_hash += *psz_string++;
859 i_hash += i_hash << 10;
860 i_hash ^= i_hash >> 8;
866 /*****************************************************************************
867 * Insert: find an empty slot to insert a new variable
868 *****************************************************************************
869 * We use a recursive inner function indexed on the hash. This function does
870 * nothing in the rare cases where a collision may occur, see Lookup()
871 * to see how we handle them.
872 * XXX: does this really need to be written recursively?
873 *****************************************************************************/
874 static int Insert( variable_t *p_vars, int i_count, const char *psz_name )
881 return InsertInner( p_vars, i_count, HashString( psz_name ) );
884 static int InsertInner( variable_t *p_vars, int i_count, uint32_t i_hash )
888 if( i_hash <= p_vars[0].i_hash )
893 if( i_hash >= p_vars[i_count - 1].i_hash )
898 i_middle = i_count / 2;
900 /* We know that 0 < i_middle */
901 if( i_hash < p_vars[i_middle].i_hash )
903 return InsertInner( p_vars, i_middle, i_hash );
906 /* We know that i_middle + 1 < i_count */
907 if( i_hash > p_vars[i_middle + 1].i_hash )
909 return i_middle + 1 + InsertInner( p_vars + i_middle + 1,
910 i_count - i_middle - 1,
917 /*****************************************************************************
918 * Lookup: find an existing variable given its name
919 *****************************************************************************
920 * We use a recursive inner function indexed on the hash. Care is taken of
921 * possible hash collisions.
922 * XXX: does this really need to be written recursively?
923 *****************************************************************************/
924 static int Lookup( variable_t *p_vars, int i_count, const char *psz_name )
934 i_hash = HashString( psz_name );
936 i_pos = LookupInner( p_vars, i_count, i_hash );
939 if( i_hash != p_vars[i_pos].i_hash )
944 /* Hash found, entry found */
945 if( !strcmp( psz_name, p_vars[i_pos].psz_name ) )
950 /* Hash collision! This should be very rare, but we cannot guarantee
951 * it will never happen. Just do an exhaustive search amongst all
952 * entries with the same hash. */
953 for( i = i_pos - 1 ; i > 0 && i_hash == p_vars[i].i_hash ; i-- )
955 if( !strcmp( psz_name, p_vars[i].psz_name ) )
961 for( i = i_pos + 1 ; i < i_count && i_hash == p_vars[i].i_hash ; i++ )
963 if( !strcmp( psz_name, p_vars[i].psz_name ) )
969 /* Hash found, but entry not found */
973 static int LookupInner( variable_t *p_vars, int i_count, uint32_t i_hash )
977 if( i_hash <= p_vars[0].i_hash )
982 if( i_hash >= p_vars[i_count-1].i_hash )
987 i_middle = i_count / 2;
989 /* We know that 0 < i_middle */
990 if( i_hash < p_vars[i_middle].i_hash )
992 return LookupInner( p_vars, i_middle, i_hash );
995 /* We know that i_middle + 1 < i_count */
996 if( i_hash > p_vars[i_middle].i_hash )
998 return i_middle + LookupInner( p_vars + i_middle,
1006 /*****************************************************************************
1007 * CheckValue: check that a value is valid wrt. a variable
1008 *****************************************************************************
1009 * This function checks p_val's value against p_var's limitations such as
1010 * minimal and maximal value, step, in-list position, and modifies p_val if
1012 *****************************************************************************/
1013 static void CheckValue ( variable_t *p_var, vlc_value_t *p_val )
1015 /* Check that our variable is in the list */
1016 if( p_var->i_type & VLC_VAR_HASCHOICE && p_var->choices.i_count )
1020 /* FIXME: the list is sorted, dude. Use something cleverer. */
1021 for( i = p_var->choices.i_count ; i-- ; )
1023 if( p_var->pf_cmp( *p_val, p_var->choices.p_values[i] ) == 0 )
1029 /* If not found, change it to anything vaguely valid */
1032 /* Free the old variable, get the new one, dup it */
1033 p_var->pf_free( p_val );
1034 *p_val = p_var->choices.p_values[p_var->i_default >= 0
1035 ? p_var->i_default : 0 ];
1036 p_var->pf_dup( p_val );
1040 /* Check that our variable is within the bounds */
1041 switch( p_var->i_type & VLC_VAR_TYPE )
1043 case VLC_VAR_INTEGER:
1044 if( p_var->i_type & VLC_VAR_HASSTEP && p_var->step.i_int
1045 && (p_val->i_int % p_var->step.i_int) )
1047 p_val->i_int = (p_val->i_int + (p_var->step.i_int / 2))
1048 / p_var->step.i_int * p_var->step.i_int;
1050 if( p_var->i_type & VLC_VAR_HASMIN
1051 && p_val->i_int < p_var->min.i_int )
1053 p_val->i_int = p_var->min.i_int;
1055 if( p_var->i_type & VLC_VAR_HASMAX
1056 && p_val->i_int > p_var->max.i_int )
1058 p_val->i_int = p_var->max.i_int;
1062 if( p_var->i_type & VLC_VAR_HASSTEP && p_var->step.f_float )
1064 float f_round = p_var->step.f_float * (float)(int)( 0.5 +
1065 p_val->f_float / p_var->step.f_float );
1066 if( p_val->f_float != f_round )
1068 p_val->f_float = f_round;
1071 if( p_var->i_type & VLC_VAR_HASMIN
1072 && p_val->f_float < p_var->min.f_float )
1074 p_val->f_float = p_var->min.f_float;
1076 if( p_var->i_type & VLC_VAR_HASMAX
1077 && p_val->f_float > p_var->max.f_float )
1079 p_val->f_float = p_var->max.f_float;
1088 /*****************************************************************************
1089 * InheritValue: try to inherit the value of this variable from the same one
1090 * in our closest parent.
1091 *****************************************************************************/
1092 static int InheritValue( vlc_object_t *p_this, const char *psz_name,
1093 vlc_value_t *p_val, int i_type )
1098 /* No need to take the structure lock,
1099 * we are only looking for our parents */
1101 if( !p_this->p_parent )
1103 switch( i_type & VLC_VAR_TYPE )
1105 case VLC_VAR_STRING:
1106 p_val->psz_string = config_GetPsz( p_this, psz_name );
1107 if( !p_val->psz_string ) p_val->psz_string = strdup("");
1110 p_val->f_float = config_GetFloat( p_this, psz_name );
1112 case VLC_VAR_INTEGER:
1113 p_val->i_int = config_GetInt( p_this, psz_name );
1116 p_val->b_bool = config_GetInt( p_this, psz_name );
1126 /* Look for the variable */
1127 vlc_mutex_lock( &p_this->p_parent->var_lock );
1129 i_var = Lookup( p_this->p_parent->p_vars, p_this->p_parent->i_vars,
1136 p_var = &p_this->p_parent->p_vars[i_var];
1138 /* Really get the variable */
1139 *p_val = p_var->val;
1141 /* Duplicate value if needed */
1142 p_var->pf_dup( p_val );
1144 vlc_mutex_unlock( &p_this->p_parent->var_lock );
1149 vlc_mutex_unlock( &p_this->p_parent->var_lock );
1151 /* We're still not there */
1153 return InheritValue( p_this->p_parent, psz_name, p_val, i_type );