1 /*****************************************************************************
2 * variables.c: routines for object variables handling
3 *****************************************************************************
4 * Copyright (C) 2002-2004 VideoLAN
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 p_list->i_count = p_val->p_list->i_count;
67 if( 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) );
74 p_list->p_values = NULL;
75 p_list->pi_types = NULL;
78 for( i = 0; i < p_list->i_count; i++ )
80 p_list->p_values[i] = p_val->p_list->p_values[i];
81 p_list->pi_types[i] = p_val->p_list->pi_types[i];
82 switch( p_val->p_list->pi_types[i] & VLC_VAR_TYPE )
86 DupString( &p_list->p_values[i] );
93 p_val->p_list = p_list;
96 static void FreeDummy( vlc_value_t *p_val ) { (void)p_val; /* unused */ }
97 static void FreeString( vlc_value_t *p_val ) { free( p_val->psz_string ); }
98 static void FreeMutex( vlc_value_t *p_val ) { vlc_mutex_destroy( (vlc_mutex_t*)p_val->p_address ); free( p_val->p_address ); }
100 static void FreeList( vlc_value_t *p_val )
103 for( i = 0; i < p_val->p_list->i_count; i++ )
105 switch( p_val->p_list->pi_types[i] & VLC_VAR_TYPE )
108 FreeString( &p_val->p_list->p_values[i] );
111 FreeMutex( &p_val->p_list->p_values[i] );
118 if( p_val->p_list->i_count )
120 free( p_val->p_list->p_values );
121 free( p_val->p_list->pi_types );
123 free( p_val->p_list );
126 /*****************************************************************************
128 *****************************************************************************/
129 static int GetUnused ( vlc_object_t *, const char * );
130 static uint32_t HashString ( const char * );
131 static int Insert ( variable_t *, int, const char * );
132 static int InsertInner ( variable_t *, int, uint32_t );
133 static int Lookup ( variable_t *, int, const char * );
134 static int LookupInner ( variable_t *, int, uint32_t );
136 static void CheckValue ( variable_t *, vlc_value_t * );
138 static int InheritValue( vlc_object_t *, const char *, vlc_value_t *,
142 * Initialize a vlc variable
144 * We hash the given string and insert it into the sorted list. The insertion
145 * may require slow memory copies, but think about what we gain in the log(n)
146 * lookup phase when setting/getting the variable value!
148 * \param p_this The object in which to create the variable
149 * \param psz_name The name of the variable
150 * \param i_type The variables type. Must be one of \ref var_type combined with
151 * zero or more \ref var_flags
153 int __var_Create( vlc_object_t *p_this, const char *psz_name, int i_type )
157 static vlc_list_t dummy_null_list = {0, NULL, NULL};
159 vlc_mutex_lock( &p_this->var_lock );
161 /* FIXME: if the variable already exists, we don't duplicate it. But we
162 * duplicate the lookups. It's not that serious, but if anyone finds some
163 * time to rework Insert() so that only one lookup has to be done, feel
165 i_new = Lookup( p_this->p_vars, p_this->i_vars, psz_name );
169 /* If the types differ, variable creation failed. */
170 if( (i_type & ~VLC_VAR_DOINHERIT) != p_this->p_vars[i_new].i_type )
172 vlc_mutex_unlock( &p_this->var_lock );
176 p_this->p_vars[i_new].i_usage++;
177 vlc_mutex_unlock( &p_this->var_lock );
181 i_new = Insert( p_this->p_vars, p_this->i_vars, psz_name );
183 if( (p_this->i_vars & 15) == 15 )
185 p_this->p_vars = realloc( p_this->p_vars,
186 (p_this->i_vars+17) * sizeof(variable_t) );
189 memmove( p_this->p_vars + i_new + 1,
190 p_this->p_vars + i_new,
191 (p_this->i_vars - i_new) * sizeof(variable_t) );
195 p_var = &p_this->p_vars[i_new];
197 p_var->i_hash = HashString( psz_name );
198 p_var->psz_name = strdup( psz_name );
199 p_var->psz_text = NULL;
201 p_var->i_type = i_type & ~VLC_VAR_DOINHERIT;
202 memset( &p_var->val, 0, sizeof(vlc_value_t) );
204 p_var->pf_dup = DupDummy;
205 p_var->pf_free = FreeDummy;
209 p_var->i_default = -1;
210 p_var->choices.i_count = 0;
211 p_var->choices.p_values = NULL;
212 p_var->choices_text.i_count = 0;
213 p_var->choices_text.p_values = NULL;
215 p_var->b_incallback = VLC_FALSE;
216 p_var->i_entries = 0;
217 p_var->p_entries = NULL;
219 /* Always initialize the variable, even if it is a list variable; this
220 * will lead to errors if the variable is not initialized, but it will
221 * not cause crashes in the variable handling. */
222 switch( i_type & VLC_VAR_TYPE )
225 p_var->pf_cmp = CmpBool;
226 p_var->val.b_bool = VLC_FALSE;
228 case VLC_VAR_INTEGER:
230 p_var->pf_cmp = CmpInt;
231 p_var->val.i_int = 0;
236 case VLC_VAR_DIRECTORY:
237 case VLC_VAR_VARIABLE:
238 p_var->pf_cmp = CmpString;
239 p_var->pf_dup = DupString;
240 p_var->pf_free = FreeString;
241 p_var->val.psz_string = "";
244 p_var->pf_cmp = CmpFloat;
245 p_var->val.f_float = 0.0;
248 p_var->pf_cmp = CmpTime;
249 p_var->val.i_time = 0;
251 case VLC_VAR_ADDRESS:
252 p_var->pf_cmp = CmpAddress;
253 p_var->val.p_address = NULL;
256 p_var->pf_cmp = CmpAddress;
257 p_var->pf_free = FreeMutex;
258 p_var->val.p_address = malloc( sizeof(vlc_mutex_t) );
259 vlc_mutex_init( p_this, (vlc_mutex_t*)p_var->val.p_address );
262 p_var->pf_cmp = CmpAddress;
263 p_var->pf_dup = DupList;
264 p_var->pf_free = FreeList;
265 p_var->val.p_list = &dummy_null_list;
269 /* Duplicate the default data we stored. */
270 p_var->pf_dup( &p_var->val );
272 if( i_type & VLC_VAR_DOINHERIT )
276 if( InheritValue( p_this, psz_name, &val, p_var->i_type )
279 /* Free data if needed */
280 p_var->pf_free( &p_var->val );
281 /* Set the variable */
284 if( i_type & VLC_VAR_HASCHOICE )
286 /* We must add the inherited value to our choice list */
287 p_var->i_default = 0;
289 INSERT_ELEM( p_var->choices.p_values, p_var->choices.i_count,
291 INSERT_ELEM( p_var->choices_text.p_values,
292 p_var->choices_text.i_count, 0, val );
293 p_var->pf_dup( &p_var->choices.p_values[0] );
294 p_var->choices_text.p_values[0].psz_string = NULL;
299 vlc_mutex_unlock( &p_this->var_lock );
305 * Destroy a vlc variable
307 * Look for the variable and destroy it if it is found. As in var_Create we
308 * do a call to memmove() but we have performance counterparts elsewhere.
310 * \param p_this The object that holds the variable
311 * \param psz_name The name of the variable
313 int __var_Destroy( vlc_object_t *p_this, const char *psz_name )
318 vlc_mutex_lock( &p_this->var_lock );
320 i_var = GetUnused( p_this, psz_name );
323 vlc_mutex_unlock( &p_this->var_lock );
327 p_var = &p_this->p_vars[i_var];
329 if( p_var->i_usage > 1 )
332 vlc_mutex_unlock( &p_this->var_lock );
336 /* Free value if needed */
337 p_var->pf_free( &p_var->val );
339 /* Free choice list if needed */
340 if( p_var->choices.i_count )
342 for( i = 0 ; i < p_var->choices.i_count ; i++ )
344 p_var->pf_free( &p_var->choices.p_values[i] );
345 if( p_var->choices_text.p_values[i].psz_string )
346 free( p_var->choices_text.p_values[i].psz_string );
348 free( p_var->choices.p_values );
349 free( p_var->choices_text.p_values );
352 /* Free callbacks if needed */
353 if( p_var->p_entries )
355 free( p_var->p_entries );
358 free( p_var->psz_name );
359 if( p_var->psz_text ) free( p_var->psz_text );
361 memmove( p_this->p_vars + i_var,
362 p_this->p_vars + i_var + 1,
363 (p_this->i_vars - i_var - 1) * sizeof(variable_t) );
365 if( (p_this->i_vars & 15) == 0 )
367 p_this->p_vars = realloc( p_this->p_vars,
368 (p_this->i_vars) * sizeof( variable_t ) );
373 vlc_mutex_unlock( &p_this->var_lock );
379 * Perform an action on a variable
381 * \param p_this The object that holds the variable
382 * \param psz_name The name of the variable
383 * \param i_action The action to perform. Must be one of \ref var_action
384 * \param p_val First action parameter
385 * \param p_val2 Second action parameter
387 int __var_Change( vlc_object_t *p_this, const char *psz_name,
388 int i_action, vlc_value_t *p_val, vlc_value_t *p_val2 )
394 vlc_mutex_lock( &p_this->var_lock );
396 i_var = Lookup( p_this->p_vars, p_this->i_vars, psz_name );
400 vlc_mutex_unlock( &p_this->var_lock );
404 p_var = &p_this->p_vars[i_var];
409 if( p_var->i_type & VLC_VAR_HASMIN )
411 p_var->pf_free( &p_var->min );
413 p_var->i_type |= VLC_VAR_HASMIN;
415 p_var->pf_dup( &p_var->min );
416 CheckValue( p_var, &p_var->val );
419 if( p_var->i_type & VLC_VAR_HASMAX )
421 p_var->pf_free( &p_var->max );
423 p_var->i_type |= VLC_VAR_HASMAX;
425 p_var->pf_dup( &p_var->max );
426 CheckValue( p_var, &p_var->val );
428 case VLC_VAR_SETSTEP:
429 if( p_var->i_type & VLC_VAR_HASSTEP )
431 p_var->pf_free( &p_var->step );
433 p_var->i_type |= VLC_VAR_HASSTEP;
434 p_var->step = *p_val;
435 p_var->pf_dup( &p_var->step );
436 CheckValue( p_var, &p_var->val );
438 case VLC_VAR_ADDCHOICE:
439 /* FIXME: the list is sorted, dude. Use something cleverer. */
440 for( i = p_var->choices.i_count ; i-- ; )
442 if( p_var->pf_cmp( p_var->choices.p_values[i], *p_val ) < 0 )
448 /* The new place is i+1 */
451 if( p_var->i_default >= i )
456 INSERT_ELEM( p_var->choices.p_values, p_var->choices.i_count,
458 INSERT_ELEM( p_var->choices_text.p_values,
459 p_var->choices_text.i_count, i, *p_val );
460 p_var->pf_dup( &p_var->choices.p_values[i] );
461 p_var->choices_text.p_values[i].psz_string =
462 ( p_val2 && p_val2->psz_string ) ?
463 strdup( p_val2->psz_string ) : NULL;
465 CheckValue( p_var, &p_var->val );
467 case VLC_VAR_DELCHOICE:
468 /* FIXME: the list is sorted, dude. Use something cleverer. */
469 for( i = 0 ; i < p_var->choices.i_count ; i++ )
471 if( p_var->pf_cmp( p_var->choices.p_values[i], *p_val ) == 0 )
477 if( i == p_var->choices.i_count )
480 vlc_mutex_unlock( &p_this->var_lock );
484 if( p_var->i_default > i )
488 else if( p_var->i_default == i )
490 p_var->i_default = -1;
493 p_var->pf_free( &p_var->choices.p_values[i] );
494 if( p_var->choices_text.p_values[i].psz_string )
495 free( p_var->choices_text.p_values[i].psz_string );
496 REMOVE_ELEM( p_var->choices.p_values, p_var->choices.i_count, i );
497 REMOVE_ELEM( p_var->choices_text.p_values,
498 p_var->choices_text.i_count, i );
500 CheckValue( p_var, &p_var->val );
502 case VLC_VAR_CHOICESCOUNT:
503 p_val->i_int = p_var->choices.i_count;
505 case VLC_VAR_CLEARCHOICES:
506 for( i = 0 ; i < p_var->choices.i_count ; i++ )
508 p_var->pf_free( &p_var->choices.p_values[i] );
510 for( i = 0 ; i < p_var->choices_text.i_count ; i++ )
512 if( p_var->choices_text.p_values[i].psz_string )
513 free( p_var->choices_text.p_values[i].psz_string );
515 if( p_var->choices.i_count ) free( p_var->choices.p_values );
516 if( p_var->choices_text.i_count ) free( p_var->choices_text.p_values );
518 p_var->choices.i_count = 0;
519 p_var->choices.p_values = NULL;
520 p_var->choices_text.i_count = 0;
521 p_var->choices_text.p_values = NULL;
522 p_var->i_default = -1;
524 case VLC_VAR_SETDEFAULT:
525 /* FIXME: the list is sorted, dude. Use something cleverer. */
526 for( i = 0 ; i < p_var->choices.i_count ; i++ )
528 if( p_var->pf_cmp( p_var->choices.p_values[i], *p_val ) == 0 )
534 if( i == p_var->choices.i_count )
540 p_var->i_default = i;
541 CheckValue( p_var, &p_var->val );
543 case VLC_VAR_SETVALUE:
544 /* Duplicate data if needed */
545 p_var->pf_dup( p_val );
546 /* Backup needed stuff */
548 /* Check boundaries and list */
549 CheckValue( p_var, p_val );
550 /* Set the variable */
552 /* Free data if needed */
553 p_var->pf_free( &oldval );
555 case VLC_VAR_GETCHOICES:
556 case VLC_VAR_GETLIST:
557 p_val->p_list = malloc( sizeof(vlc_list_t) );
558 if( p_val2 ) p_val2->p_list = malloc( sizeof(vlc_list_t) );
559 if( p_var->choices.i_count )
561 p_val->p_list->p_values = malloc( p_var->choices.i_count
562 * sizeof(vlc_value_t) );
563 p_val->p_list->pi_types = malloc( p_var->choices.i_count
567 p_val2->p_list->p_values =
568 malloc( p_var->choices.i_count * sizeof(vlc_value_t) );
569 p_val2->p_list->pi_types =
570 malloc( p_var->choices.i_count * sizeof(int) );
573 p_val->p_list->i_count = p_var->choices.i_count;
574 if( p_val2 ) p_val2->p_list->i_count = p_var->choices.i_count;
575 for( i = 0 ; i < p_var->choices.i_count ; i++ )
577 p_val->p_list->p_values[i] = p_var->choices.p_values[i];
578 p_val->p_list->pi_types[i] = p_var->i_type;
579 p_var->pf_dup( &p_val->p_list->p_values[i] );
582 p_val2->p_list->p_values[i].psz_string =
583 p_var->choices_text.p_values[i].psz_string ?
584 strdup(p_var->choices_text.p_values[i].psz_string) : NULL;
585 p_val2->p_list->pi_types[i] = VLC_VAR_STRING;
589 case VLC_VAR_FREELIST:
591 if( p_val2 && p_val2->p_list )
593 for( i = 0; i < p_val2->p_list->i_count; i++ )
594 if( p_val2->p_list->p_values[i].psz_string )
595 free( p_val2->p_list->p_values[i].psz_string );
596 if( p_val2->p_list->i_count )
598 free( p_val2->p_list->p_values );
599 free( p_val2->p_list->pi_types );
601 free( p_val2->p_list );
604 case VLC_VAR_SETTEXT:
605 if( p_var->psz_text ) free( p_var->psz_text );
606 if( p_val && p_val->psz_string )
607 p_var->psz_text = strdup( p_val->psz_string );
609 case VLC_VAR_GETTEXT:
610 p_val->psz_string = NULL;
611 if( p_var->psz_text )
613 p_val->psz_string = strdup( p_var->psz_text );
616 case VLC_VAR_INHERITVALUE:
620 if( InheritValue( p_this, psz_name, &val, p_var->i_type )
623 /* Duplicate already done */
625 /* Backup needed stuff */
627 /* Check boundaries and list */
628 CheckValue( p_var, &val );
629 /* Set the variable */
631 /* Free data if needed */
632 p_var->pf_free( &oldval );
638 p_var->pf_dup( p_val );
642 case VLC_VAR_TRIGGER_CALLBACKS:
644 /* Deal with callbacks. Tell we're in a callback, release the lock,
645 * call stored functions, retake the lock. */
646 if( p_var->i_entries )
649 int i_entries = p_var->i_entries;
650 callback_entry_t *p_entries = p_var->p_entries;
652 p_var->b_incallback = VLC_TRUE;
653 vlc_mutex_unlock( &p_this->var_lock );
656 for( ; i_entries-- ; )
658 p_entries[i_entries].pf_callback( p_this, psz_name, p_var->val, p_var->val,
659 p_entries[i_entries].p_data );
662 vlc_mutex_lock( &p_this->var_lock );
664 i_var = Lookup( p_this->p_vars, p_this->i_vars, psz_name );
667 msg_Err( p_this, "variable %s has disappeared", psz_name );
668 vlc_mutex_unlock( &p_this->var_lock );
672 p_var = &p_this->p_vars[i_var];
673 p_var->b_incallback = VLC_FALSE;
682 vlc_mutex_unlock( &p_this->var_lock );
688 * Request a variable's type
690 * \return The variable type if it exists, or 0 if the
691 * variable could not be found.
694 int __var_Type( vlc_object_t *p_this, const char *psz_name )
698 vlc_mutex_lock( &p_this->var_lock );
700 i_var = Lookup( p_this->p_vars, p_this->i_vars, psz_name );
704 vlc_mutex_unlock( &p_this->var_lock );
708 i_type = p_this->p_vars[i_var].i_type;
710 vlc_mutex_unlock( &p_this->var_lock );
716 * Set a variable's value
718 * \param p_this The object that hold the variable
719 * \param psz_name The name of the variable
720 * \param val the value to set
722 int __var_Set( vlc_object_t *p_this, const char *psz_name, vlc_value_t val )
728 vlc_mutex_lock( &p_this->var_lock );
730 i_var = GetUnused( p_this, psz_name );
733 vlc_mutex_unlock( &p_this->var_lock );
737 p_var = &p_this->p_vars[i_var];
739 /* Duplicate data if needed */
740 p_var->pf_dup( &val );
742 /* Backup needed stuff */
745 /* Check boundaries and list */
746 CheckValue( p_var, &val );
748 /* Set the variable */
751 /* Deal with callbacks. Tell we're in a callback, release the lock,
752 * call stored functions, retake the lock. */
753 if( p_var->i_entries )
756 int i_entries = p_var->i_entries;
757 callback_entry_t *p_entries = p_var->p_entries;
759 p_var->b_incallback = VLC_TRUE;
760 vlc_mutex_unlock( &p_this->var_lock );
763 for( ; i_entries-- ; )
765 p_entries[i_entries].pf_callback( p_this, psz_name, oldval, val,
766 p_entries[i_entries].p_data );
769 vlc_mutex_lock( &p_this->var_lock );
771 i_var = Lookup( p_this->p_vars, p_this->i_vars, psz_name );
774 msg_Err( p_this, "variable %s has disappeared", psz_name );
775 vlc_mutex_unlock( &p_this->var_lock );
779 p_var = &p_this->p_vars[i_var];
780 p_var->b_incallback = VLC_FALSE;
783 /* Free data if needed */
784 p_var->pf_free( &oldval );
786 vlc_mutex_unlock( &p_this->var_lock );
792 * Get a variable's value
794 * \param p_this The object that holds the variable
795 * \param psz_name The name of the variable
796 * \param p_val Pointer to a vlc_value_t that will hold the variable's value
797 * after the function is finished
799 int __var_Get( vlc_object_t *p_this, const char *psz_name, vlc_value_t *p_val )
804 vlc_mutex_lock( &p_this->var_lock );
806 i_var = Lookup( p_this->p_vars, p_this->i_vars, psz_name );
810 vlc_mutex_unlock( &p_this->var_lock );
814 p_var = &p_this->p_vars[i_var];
816 /* Really get the variable */
819 /* Duplicate value if needed */
820 p_var->pf_dup( p_val );
822 vlc_mutex_unlock( &p_this->var_lock );
828 * Register a callback in a variable
830 * We store a function pointer that will be called upon variable
833 * \param p_this The object that holds the variable
834 * \param psz_name The name of the variable
835 * \param pf_callback The function pointer
836 * \param p_data A generic pointer that will be passed as the last
837 * argument to the callback function.
839 * \warning The callback function is run in the thread that calls var_Set on
840 * the variable. Use proper locking. This thread may not have much
841 * time to spare, so keep callback functions short.
843 int __var_AddCallback( vlc_object_t *p_this, const char *psz_name,
844 vlc_callback_t pf_callback, void *p_data )
848 callback_entry_t entry;
850 entry.pf_callback = pf_callback;
851 entry.p_data = p_data;
853 vlc_mutex_lock( &p_this->var_lock );
855 i_var = GetUnused( p_this, psz_name );
858 vlc_mutex_unlock( &p_this->var_lock );
862 p_var = &p_this->p_vars[i_var];
864 INSERT_ELEM( p_var->p_entries,
869 vlc_mutex_unlock( &p_this->var_lock );
875 * Remove a callback from a variable
877 * pf_callback and p_data have to be given again, because different objects
878 * might have registered the same callback function.
880 int __var_DelCallback( vlc_object_t *p_this, const char *psz_name,
881 vlc_callback_t pf_callback, void *p_data )
886 vlc_mutex_lock( &p_this->var_lock );
888 i_var = GetUnused( p_this, psz_name );
891 vlc_mutex_unlock( &p_this->var_lock );
895 p_var = &p_this->p_vars[i_var];
897 for( i_entry = p_var->i_entries ; i_entry-- ; )
899 if( p_var->p_entries[i_entry].pf_callback == pf_callback
900 && p_var->p_entries[i_entry].p_data == p_data )
908 vlc_mutex_unlock( &p_this->var_lock );
912 REMOVE_ELEM( p_var->p_entries, p_var->i_entries, i_entry );
914 vlc_mutex_unlock( &p_this->var_lock );
919 /* Following functions are local */
921 /*****************************************************************************
922 * GetUnused: find an unused variable from its name
923 *****************************************************************************
924 * We do i_tries tries before giving up, just in case the variable is being
925 * modified and called from a callback.
926 *****************************************************************************/
927 static int GetUnused( vlc_object_t *p_this, const char *psz_name )
929 int i_var, i_tries = 0;
933 i_var = Lookup( p_this->p_vars, p_this->i_vars, psz_name );
939 if( ! p_this->p_vars[i_var].b_incallback )
944 if( i_tries++ > 100 )
946 msg_Err( p_this, "caught in a callback deadlock?" );
950 vlc_mutex_unlock( &p_this->var_lock );
951 msleep( THREAD_SLEEP );
952 vlc_mutex_lock( &p_this->var_lock );
956 /*****************************************************************************
957 * HashString: our cool hash function
958 *****************************************************************************
959 * This function is not intended to be crypto-secure, we only want it to be
960 * fast and not suck too much. This one is pretty fast and did 0 collisions
961 * in wenglish's dictionary.
962 *****************************************************************************/
963 static uint32_t HashString( const char *psz_string )
969 i_hash += *psz_string++;
970 i_hash += i_hash << 10;
971 i_hash ^= i_hash >> 8;
977 /*****************************************************************************
978 * Insert: find an empty slot to insert a new variable
979 *****************************************************************************
980 * We use a recursive inner function indexed on the hash. This function does
981 * nothing in the rare cases where a collision may occur, see Lookup()
982 * to see how we handle them.
983 * XXX: does this really need to be written recursively?
984 *****************************************************************************/
985 static int Insert( variable_t *p_vars, int i_count, const char *psz_name )
992 return InsertInner( p_vars, i_count, HashString( psz_name ) );
995 static int InsertInner( variable_t *p_vars, int i_count, uint32_t i_hash )
999 if( i_hash <= p_vars[0].i_hash )
1004 if( i_hash >= p_vars[i_count - 1].i_hash )
1009 i_middle = i_count / 2;
1011 /* We know that 0 < i_middle */
1012 if( i_hash < p_vars[i_middle].i_hash )
1014 return InsertInner( p_vars, i_middle, i_hash );
1017 /* We know that i_middle + 1 < i_count */
1018 if( i_hash > p_vars[i_middle + 1].i_hash )
1020 return i_middle + 1 + InsertInner( p_vars + i_middle + 1,
1021 i_count - i_middle - 1,
1025 return i_middle + 1;
1028 /*****************************************************************************
1029 * Lookup: find an existing variable given its name
1030 *****************************************************************************
1031 * We use a recursive inner function indexed on the hash. Care is taken of
1032 * possible hash collisions.
1033 * XXX: does this really need to be written recursively?
1034 *****************************************************************************/
1035 static int Lookup( variable_t *p_vars, int i_count, const char *psz_name )
1045 i_hash = HashString( psz_name );
1047 i_pos = LookupInner( p_vars, i_count, i_hash );
1049 /* Hash not found */
1050 if( i_hash != p_vars[i_pos].i_hash )
1055 /* Hash found, entry found */
1056 if( !strcmp( psz_name, p_vars[i_pos].psz_name ) )
1061 /* Hash collision! This should be very rare, but we cannot guarantee
1062 * it will never happen. Just do an exhaustive search amongst all
1063 * entries with the same hash. */
1064 for( i = i_pos - 1 ; i > 0 && i_hash == p_vars[i].i_hash ; i-- )
1066 if( !strcmp( psz_name, p_vars[i].psz_name ) )
1072 for( i = i_pos + 1 ; i < i_count && i_hash == p_vars[i].i_hash ; i++ )
1074 if( !strcmp( psz_name, p_vars[i].psz_name ) )
1080 /* Hash found, but entry not found */
1084 static int LookupInner( variable_t *p_vars, int i_count, uint32_t i_hash )
1088 if( i_hash <= p_vars[0].i_hash )
1093 if( i_hash >= p_vars[i_count-1].i_hash )
1098 i_middle = i_count / 2;
1100 /* We know that 0 < i_middle */
1101 if( i_hash < p_vars[i_middle].i_hash )
1103 return LookupInner( p_vars, i_middle, i_hash );
1106 /* We know that i_middle + 1 < i_count */
1107 if( i_hash > p_vars[i_middle].i_hash )
1109 return i_middle + LookupInner( p_vars + i_middle,
1117 /*****************************************************************************
1118 * CheckValue: check that a value is valid wrt. a variable
1119 *****************************************************************************
1120 * This function checks p_val's value against p_var's limitations such as
1121 * minimal and maximal value, step, in-list position, and modifies p_val if
1123 *****************************************************************************/
1124 static void CheckValue ( variable_t *p_var, vlc_value_t *p_val )
1126 /* Check that our variable is in the list */
1127 if( p_var->i_type & VLC_VAR_HASCHOICE && p_var->choices.i_count )
1131 /* FIXME: the list is sorted, dude. Use something cleverer. */
1132 for( i = p_var->choices.i_count ; i-- ; )
1134 if( p_var->pf_cmp( *p_val, p_var->choices.p_values[i] ) == 0 )
1140 /* If not found, change it to anything vaguely valid */
1143 /* Free the old variable, get the new one, dup it */
1144 p_var->pf_free( p_val );
1145 *p_val = p_var->choices.p_values[p_var->i_default >= 0
1146 ? p_var->i_default : 0 ];
1147 p_var->pf_dup( p_val );
1151 /* Check that our variable is within the bounds */
1152 switch( p_var->i_type & VLC_VAR_TYPE )
1154 case VLC_VAR_INTEGER:
1155 if( p_var->i_type & VLC_VAR_HASSTEP && p_var->step.i_int
1156 && (p_val->i_int % p_var->step.i_int) )
1158 p_val->i_int = (p_val->i_int + (p_var->step.i_int / 2))
1159 / p_var->step.i_int * p_var->step.i_int;
1161 if( p_var->i_type & VLC_VAR_HASMIN
1162 && p_val->i_int < p_var->min.i_int )
1164 p_val->i_int = p_var->min.i_int;
1166 if( p_var->i_type & VLC_VAR_HASMAX
1167 && p_val->i_int > p_var->max.i_int )
1169 p_val->i_int = p_var->max.i_int;
1173 if( p_var->i_type & VLC_VAR_HASSTEP && p_var->step.f_float )
1175 float f_round = p_var->step.f_float * (float)(int)( 0.5 +
1176 p_val->f_float / p_var->step.f_float );
1177 if( p_val->f_float != f_round )
1179 p_val->f_float = f_round;
1182 if( p_var->i_type & VLC_VAR_HASMIN
1183 && p_val->f_float < p_var->min.f_float )
1185 p_val->f_float = p_var->min.f_float;
1187 if( p_var->i_type & VLC_VAR_HASMAX
1188 && p_val->f_float > p_var->max.f_float )
1190 p_val->f_float = p_var->max.f_float;
1199 /*****************************************************************************
1200 * InheritValue: try to inherit the value of this variable from the same one
1201 * in our closest parent.
1202 *****************************************************************************/
1203 static int InheritValue( vlc_object_t *p_this, const char *psz_name,
1204 vlc_value_t *p_val, int i_type )
1209 /* No need to take the structure lock,
1210 * we are only looking for our parents */
1212 if( !p_this->p_parent )
1214 switch( i_type & VLC_VAR_TYPE )
1217 case VLC_VAR_DIRECTORY:
1218 case VLC_VAR_STRING:
1219 case VLC_VAR_MODULE:
1220 p_val->psz_string = config_GetPsz( p_this, psz_name );
1221 if( !p_val->psz_string ) p_val->psz_string = strdup("");
1224 p_val->f_float = config_GetFloat( p_this, psz_name );
1226 case VLC_VAR_INTEGER:
1227 case VLC_VAR_HOTKEY:
1228 p_val->i_int = config_GetInt( p_this, psz_name );
1231 p_val->b_bool = config_GetInt( p_this, psz_name );
1235 char *psz_orig, *psz_var;
1236 vlc_list_t *p_list = malloc(sizeof(vlc_list_t));
1237 p_val->p_list = p_list;
1238 p_list->i_count = 0;
1240 psz_var = psz_orig = config_GetPsz( p_this, psz_name );
1241 while( psz_var && *psz_var )
1243 char *psz_item = psz_var;
1245 while( *psz_var && *psz_var != ',' ) psz_var++;
1246 if( *psz_var == ',' )
1251 val.i_int = strtol( psz_item, NULL, 0 );
1252 INSERT_ELEM( p_list->p_values, p_list->i_count,
1253 p_list->i_count, val );
1254 /* p_list->i_count is incremented twice by INSERT_ELEM */
1256 INSERT_ELEM( p_list->pi_types, p_list->i_count,
1257 p_list->i_count, VLC_VAR_INTEGER );
1259 if( psz_orig ) free( psz_orig );
1270 /* Look for the variable */
1271 vlc_mutex_lock( &p_this->p_parent->var_lock );
1273 i_var = Lookup( p_this->p_parent->p_vars, p_this->p_parent->i_vars,
1279 p_var = &p_this->p_parent->p_vars[i_var];
1281 /* Really get the variable */
1282 *p_val = p_var->val;
1284 /* Duplicate value if needed */
1285 p_var->pf_dup( p_val );
1287 vlc_mutex_unlock( &p_this->p_parent->var_lock );
1291 vlc_mutex_unlock( &p_this->p_parent->var_lock );
1293 /* We're still not there */
1295 return InheritValue( p_this->p_parent, psz_name, p_val, i_type );