1 /*****************************************************************************
2 * variables.c: routines for object variables handling
3 *****************************************************************************
4 * Copyright (C) 2002-2006 the VideoLAN team
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, 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];
196 memset( p_var, 0, sizeof(*p_var) );
198 p_var->i_hash = HashString( psz_name );
199 p_var->psz_name = strdup( psz_name );
200 p_var->psz_text = NULL;
202 p_var->i_type = i_type & ~VLC_VAR_DOINHERIT;
203 memset( &p_var->val, 0, sizeof(vlc_value_t) );
205 p_var->pf_dup = DupDummy;
206 p_var->pf_free = FreeDummy;
210 p_var->i_default = -1;
211 p_var->choices.i_count = 0;
212 p_var->choices.p_values = NULL;
213 p_var->choices_text.i_count = 0;
214 p_var->choices_text.p_values = NULL;
216 p_var->b_incallback = VLC_FALSE;
217 p_var->i_entries = 0;
218 p_var->p_entries = NULL;
220 /* Always initialize the variable, even if it is a list variable; this
221 * will lead to errors if the variable is not initialized, but it will
222 * not cause crashes in the variable handling. */
223 switch( i_type & VLC_VAR_TYPE )
226 p_var->pf_cmp = CmpBool;
227 p_var->val.b_bool = VLC_FALSE;
229 case VLC_VAR_INTEGER:
231 p_var->pf_cmp = CmpInt;
232 p_var->val.i_int = 0;
237 case VLC_VAR_DIRECTORY:
238 case VLC_VAR_VARIABLE:
239 p_var->pf_cmp = CmpString;
240 p_var->pf_dup = DupString;
241 p_var->pf_free = FreeString;
242 p_var->val.psz_string = "";
245 p_var->pf_cmp = CmpFloat;
246 p_var->val.f_float = 0.0;
249 p_var->pf_cmp = CmpTime;
250 p_var->val.i_time = 0;
252 case VLC_VAR_ADDRESS:
253 p_var->pf_cmp = CmpAddress;
254 p_var->val.p_address = NULL;
257 p_var->pf_cmp = CmpAddress;
258 p_var->pf_free = FreeMutex;
259 p_var->val.p_address = malloc( sizeof(vlc_mutex_t) );
260 vlc_mutex_init( p_this, (vlc_mutex_t*)p_var->val.p_address );
263 p_var->pf_cmp = CmpAddress;
264 p_var->pf_dup = DupList;
265 p_var->pf_free = FreeList;
266 p_var->val.p_list = &dummy_null_list;
270 /* Duplicate the default data we stored. */
271 p_var->pf_dup( &p_var->val );
273 if( i_type & VLC_VAR_DOINHERIT )
277 if( InheritValue( p_this, psz_name, &val, p_var->i_type )
280 /* Free data if needed */
281 p_var->pf_free( &p_var->val );
282 /* Set the variable */
285 if( i_type & VLC_VAR_HASCHOICE )
287 /* We must add the inherited value to our choice list */
288 p_var->i_default = 0;
290 INSERT_ELEM( p_var->choices.p_values, p_var->choices.i_count,
292 INSERT_ELEM( p_var->choices_text.p_values,
293 p_var->choices_text.i_count, 0, val );
294 p_var->pf_dup( &p_var->choices.p_values[0] );
295 p_var->choices_text.p_values[0].psz_string = NULL;
300 vlc_mutex_unlock( &p_this->var_lock );
306 * Destroy a vlc variable
308 * Look for the variable and destroy it if it is found. As in var_Create we
309 * do a call to memmove() but we have performance counterparts elsewhere.
311 * \param p_this The object that holds the variable
312 * \param psz_name The name of the variable
314 int __var_Destroy( vlc_object_t *p_this, const char *psz_name )
319 vlc_mutex_lock( &p_this->var_lock );
321 i_var = GetUnused( p_this, psz_name );
324 vlc_mutex_unlock( &p_this->var_lock );
328 p_var = &p_this->p_vars[i_var];
330 if( p_var->i_usage > 1 )
333 vlc_mutex_unlock( &p_this->var_lock );
337 /* Free value if needed */
338 p_var->pf_free( &p_var->val );
340 /* Free choice list if needed */
341 if( p_var->choices.i_count )
343 for( i = 0 ; i < p_var->choices.i_count ; i++ )
345 p_var->pf_free( &p_var->choices.p_values[i] );
346 if( p_var->choices_text.p_values[i].psz_string )
347 free( p_var->choices_text.p_values[i].psz_string );
349 free( p_var->choices.p_values );
350 free( p_var->choices_text.p_values );
353 /* Free callbacks if needed */
354 if( p_var->p_entries )
356 free( p_var->p_entries );
359 free( p_var->psz_name );
360 if( p_var->psz_text ) free( p_var->psz_text );
362 memmove( p_this->p_vars + i_var,
363 p_this->p_vars + i_var + 1,
364 (p_this->i_vars - i_var - 1) * sizeof(variable_t) );
366 if( (p_this->i_vars & 15) == 0 )
368 p_this->p_vars = realloc( p_this->p_vars,
369 (p_this->i_vars) * sizeof( variable_t ) );
374 vlc_mutex_unlock( &p_this->var_lock );
380 * Perform an action on a variable
382 * \param p_this The object that holds the variable
383 * \param psz_name The name of the variable
384 * \param i_action The action to perform. Must be one of \ref var_action
385 * \param p_val First action parameter
386 * \param p_val2 Second action parameter
388 int __var_Change( vlc_object_t *p_this, const char *psz_name,
389 int i_action, vlc_value_t *p_val, vlc_value_t *p_val2 )
395 vlc_mutex_lock( &p_this->var_lock );
397 i_var = Lookup( p_this->p_vars, p_this->i_vars, psz_name );
401 vlc_mutex_unlock( &p_this->var_lock );
405 p_var = &p_this->p_vars[i_var];
410 if( p_var->i_type & VLC_VAR_HASMIN )
412 p_var->pf_free( &p_var->min );
414 p_var->i_type |= VLC_VAR_HASMIN;
416 p_var->pf_dup( &p_var->min );
417 CheckValue( p_var, &p_var->val );
420 if( p_var->i_type & VLC_VAR_HASMAX )
422 p_var->pf_free( &p_var->max );
424 p_var->i_type |= VLC_VAR_HASMAX;
426 p_var->pf_dup( &p_var->max );
427 CheckValue( p_var, &p_var->val );
429 case VLC_VAR_SETSTEP:
430 if( p_var->i_type & VLC_VAR_HASSTEP )
432 p_var->pf_free( &p_var->step );
434 p_var->i_type |= VLC_VAR_HASSTEP;
435 p_var->step = *p_val;
436 p_var->pf_dup( &p_var->step );
437 CheckValue( p_var, &p_var->val );
439 case VLC_VAR_ADDCHOICE:
440 /* FIXME: the list is sorted, dude. Use something cleverer. */
441 for( i = p_var->choices.i_count ; i-- ; )
443 if( p_var->pf_cmp( p_var->choices.p_values[i], *p_val ) < 0 )
449 /* The new place is i+1 */
452 if( p_var->i_default >= i )
457 INSERT_ELEM( p_var->choices.p_values, p_var->choices.i_count,
459 INSERT_ELEM( p_var->choices_text.p_values,
460 p_var->choices_text.i_count, i, *p_val );
461 p_var->pf_dup( &p_var->choices.p_values[i] );
462 p_var->choices_text.p_values[i].psz_string =
463 ( p_val2 && p_val2->psz_string ) ?
464 strdup( p_val2->psz_string ) : NULL;
466 CheckValue( p_var, &p_var->val );
468 case VLC_VAR_DELCHOICE:
469 /* FIXME: the list is sorted, dude. Use something cleverer. */
470 for( i = 0 ; i < p_var->choices.i_count ; i++ )
472 if( p_var->pf_cmp( p_var->choices.p_values[i], *p_val ) == 0 )
478 if( i == p_var->choices.i_count )
481 vlc_mutex_unlock( &p_this->var_lock );
485 if( p_var->i_default > i )
489 else if( p_var->i_default == i )
491 p_var->i_default = -1;
494 p_var->pf_free( &p_var->choices.p_values[i] );
495 if( p_var->choices_text.p_values[i].psz_string )
496 free( p_var->choices_text.p_values[i].psz_string );
497 REMOVE_ELEM( p_var->choices.p_values, p_var->choices.i_count, i );
498 REMOVE_ELEM( p_var->choices_text.p_values,
499 p_var->choices_text.i_count, i );
501 CheckValue( p_var, &p_var->val );
503 case VLC_VAR_CHOICESCOUNT:
504 p_val->i_int = p_var->choices.i_count;
506 case VLC_VAR_CLEARCHOICES:
507 for( i = 0 ; i < p_var->choices.i_count ; i++ )
509 p_var->pf_free( &p_var->choices.p_values[i] );
511 for( i = 0 ; i < p_var->choices_text.i_count ; i++ )
513 if( p_var->choices_text.p_values[i].psz_string )
514 free( p_var->choices_text.p_values[i].psz_string );
516 if( p_var->choices.i_count ) free( p_var->choices.p_values );
517 if( p_var->choices_text.i_count ) free( p_var->choices_text.p_values );
519 p_var->choices.i_count = 0;
520 p_var->choices.p_values = NULL;
521 p_var->choices_text.i_count = 0;
522 p_var->choices_text.p_values = NULL;
523 p_var->i_default = -1;
525 case VLC_VAR_SETDEFAULT:
526 /* FIXME: the list is sorted, dude. Use something cleverer. */
527 for( i = 0 ; i < p_var->choices.i_count ; i++ )
529 if( p_var->pf_cmp( p_var->choices.p_values[i], *p_val ) == 0 )
535 if( i == p_var->choices.i_count )
541 p_var->i_default = i;
542 CheckValue( p_var, &p_var->val );
544 case VLC_VAR_SETVALUE:
545 /* Duplicate data if needed */
546 p_var->pf_dup( p_val );
547 /* Backup needed stuff */
549 /* Check boundaries and list */
550 CheckValue( p_var, p_val );
551 /* Set the variable */
553 /* Free data if needed */
554 p_var->pf_free( &oldval );
556 case VLC_VAR_GETCHOICES:
557 case VLC_VAR_GETLIST:
558 p_val->p_list = malloc( sizeof(vlc_list_t) );
559 if( p_val2 ) p_val2->p_list = malloc( sizeof(vlc_list_t) );
560 if( p_var->choices.i_count )
562 p_val->p_list->p_values = malloc( p_var->choices.i_count
563 * sizeof(vlc_value_t) );
564 p_val->p_list->pi_types = malloc( p_var->choices.i_count
568 p_val2->p_list->p_values =
569 malloc( p_var->choices.i_count * sizeof(vlc_value_t) );
570 p_val2->p_list->pi_types =
571 malloc( p_var->choices.i_count * sizeof(int) );
574 p_val->p_list->i_count = p_var->choices.i_count;
575 if( p_val2 ) p_val2->p_list->i_count = p_var->choices.i_count;
576 for( i = 0 ; i < p_var->choices.i_count ; i++ )
578 p_val->p_list->p_values[i] = p_var->choices.p_values[i];
579 p_val->p_list->pi_types[i] = p_var->i_type;
580 p_var->pf_dup( &p_val->p_list->p_values[i] );
583 p_val2->p_list->p_values[i].psz_string =
584 p_var->choices_text.p_values[i].psz_string ?
585 strdup(p_var->choices_text.p_values[i].psz_string) : NULL;
586 p_val2->p_list->pi_types[i] = VLC_VAR_STRING;
590 case VLC_VAR_FREELIST:
592 if( p_val2 && p_val2->p_list )
594 for( i = 0; i < p_val2->p_list->i_count; i++ )
595 if( p_val2->p_list->p_values[i].psz_string )
596 free( p_val2->p_list->p_values[i].psz_string );
597 if( p_val2->p_list->i_count )
599 free( p_val2->p_list->p_values );
600 free( p_val2->p_list->pi_types );
602 free( p_val2->p_list );
605 case VLC_VAR_SETTEXT:
606 if( p_var->psz_text ) free( p_var->psz_text );
607 if( p_val && p_val->psz_string )
608 p_var->psz_text = strdup( p_val->psz_string );
610 case VLC_VAR_GETTEXT:
611 p_val->psz_string = NULL;
612 if( p_var->psz_text )
614 p_val->psz_string = strdup( p_var->psz_text );
617 case VLC_VAR_INHERITVALUE:
621 if( InheritValue( p_this, psz_name, &val, p_var->i_type )
624 /* Duplicate already done */
626 /* Backup needed stuff */
628 /* Check boundaries and list */
629 CheckValue( p_var, &val );
630 /* Set the variable */
632 /* Free data if needed */
633 p_var->pf_free( &oldval );
639 p_var->pf_dup( p_val );
643 case VLC_VAR_TRIGGER_CALLBACKS:
645 /* Deal with callbacks. Tell we're in a callback, release the lock,
646 * call stored functions, retake the lock. */
647 if( p_var->i_entries )
650 int i_entries = p_var->i_entries;
651 callback_entry_t *p_entries = p_var->p_entries;
653 p_var->b_incallback = VLC_TRUE;
654 vlc_mutex_unlock( &p_this->var_lock );
657 for( ; i_entries-- ; )
659 p_entries[i_entries].pf_callback( p_this, psz_name, p_var->val, p_var->val,
660 p_entries[i_entries].p_data );
663 vlc_mutex_lock( &p_this->var_lock );
665 i_var = Lookup( p_this->p_vars, p_this->i_vars, psz_name );
668 msg_Err( p_this, "variable %s has disappeared", psz_name );
669 vlc_mutex_unlock( &p_this->var_lock );
673 p_var = &p_this->p_vars[i_var];
674 p_var->b_incallback = VLC_FALSE;
683 vlc_mutex_unlock( &p_this->var_lock );
689 * Request a variable's type
691 * \return The variable type if it exists, or 0 if the
692 * variable could not be found.
695 int __var_Type( vlc_object_t *p_this, const char *psz_name )
699 vlc_mutex_lock( &p_this->var_lock );
701 i_var = Lookup( p_this->p_vars, p_this->i_vars, psz_name );
705 vlc_mutex_unlock( &p_this->var_lock );
709 i_type = p_this->p_vars[i_var].i_type;
711 vlc_mutex_unlock( &p_this->var_lock );
717 * Set a variable's value
719 * \param p_this The object that hold the variable
720 * \param psz_name The name of the variable
721 * \param val the value to set
723 int __var_Set( vlc_object_t *p_this, const char *psz_name, vlc_value_t val )
729 vlc_mutex_lock( &p_this->var_lock );
731 i_var = GetUnused( p_this, psz_name );
734 vlc_mutex_unlock( &p_this->var_lock );
738 p_var = &p_this->p_vars[i_var];
740 /* Duplicate data if needed */
741 p_var->pf_dup( &val );
743 /* Backup needed stuff */
746 /* Check boundaries and list */
747 CheckValue( p_var, &val );
749 /* Set the variable */
752 /* Deal with callbacks. Tell we're in a callback, release the lock,
753 * call stored functions, retake the lock. */
754 if( p_var->i_entries )
757 int i_entries = p_var->i_entries;
758 callback_entry_t *p_entries = p_var->p_entries;
760 p_var->b_incallback = VLC_TRUE;
761 vlc_mutex_unlock( &p_this->var_lock );
764 for( ; i_entries-- ; )
766 p_entries[i_entries].pf_callback( p_this, psz_name, oldval, val,
767 p_entries[i_entries].p_data );
770 vlc_mutex_lock( &p_this->var_lock );
772 i_var = Lookup( p_this->p_vars, p_this->i_vars, psz_name );
775 msg_Err( p_this, "variable %s has disappeared", psz_name );
776 vlc_mutex_unlock( &p_this->var_lock );
780 p_var = &p_this->p_vars[i_var];
781 p_var->b_incallback = VLC_FALSE;
784 /* Free data if needed */
785 p_var->pf_free( &oldval );
787 vlc_mutex_unlock( &p_this->var_lock );
793 * Get a variable's value
795 * \param p_this The object that holds the variable
796 * \param psz_name The name of the variable
797 * \param p_val Pointer to a vlc_value_t that will hold the variable's value
798 * after the function is finished
800 int __var_Get( vlc_object_t *p_this, const char *psz_name, vlc_value_t *p_val )
805 vlc_mutex_lock( &p_this->var_lock );
807 i_var = Lookup( p_this->p_vars, p_this->i_vars, psz_name );
811 vlc_mutex_unlock( &p_this->var_lock );
815 p_var = &p_this->p_vars[i_var];
817 /* Really get the variable */
820 /* Duplicate value if needed */
821 p_var->pf_dup( p_val );
823 vlc_mutex_unlock( &p_this->var_lock );
829 * Register a callback in a variable
831 * We store a function pointer that will be called upon variable
834 * \param p_this The object that holds the variable
835 * \param psz_name The name of the variable
836 * \param pf_callback The function pointer
837 * \param p_data A generic pointer that will be passed as the last
838 * argument to the callback function.
840 * \warning The callback function is run in the thread that calls var_Set on
841 * the variable. Use proper locking. This thread may not have much
842 * time to spare, so keep callback functions short.
844 int __var_AddCallback( vlc_object_t *p_this, const char *psz_name,
845 vlc_callback_t pf_callback, void *p_data )
849 callback_entry_t entry;
851 entry.pf_callback = pf_callback;
852 entry.p_data = p_data;
854 vlc_mutex_lock( &p_this->var_lock );
856 i_var = GetUnused( p_this, psz_name );
859 vlc_mutex_unlock( &p_this->var_lock );
863 p_var = &p_this->p_vars[i_var];
865 INSERT_ELEM( p_var->p_entries,
870 vlc_mutex_unlock( &p_this->var_lock );
876 * Remove a callback from a variable
878 * pf_callback and p_data have to be given again, because different objects
879 * might have registered the same callback function.
881 int __var_DelCallback( vlc_object_t *p_this, const char *psz_name,
882 vlc_callback_t pf_callback, void *p_data )
887 vlc_mutex_lock( &p_this->var_lock );
889 i_var = GetUnused( p_this, psz_name );
892 vlc_mutex_unlock( &p_this->var_lock );
896 p_var = &p_this->p_vars[i_var];
898 for( i_entry = p_var->i_entries ; i_entry-- ; )
900 if( p_var->p_entries[i_entry].pf_callback == pf_callback
901 && p_var->p_entries[i_entry].p_data == p_data )
909 vlc_mutex_unlock( &p_this->var_lock );
913 REMOVE_ELEM( p_var->p_entries, p_var->i_entries, i_entry );
915 vlc_mutex_unlock( &p_this->var_lock );
920 /** Parse a stringified option
921 * This function parse a string option and create the associated object
923 * The option must be of the form "[no[-]]foo[=bar]" where foo is the
924 * option name and bar is the value of the option.
925 * \param p_obj the object in which the variable must be created
926 * \param psz_option the option to parse
929 void __var_OptionParse( vlc_object_t *p_obj, const char *psz_option )
931 char *psz_name, *psz_value = strchr( psz_option, '=' );
932 int i_name_len, i_type;
933 vlc_bool_t b_isno = VLC_FALSE;
936 if( psz_value ) i_name_len = psz_value - psz_option;
937 else i_name_len = strlen( psz_option );
939 /* It's too much of an hassle to remove the ':' when we parse
941 if( i_name_len && *psz_option == ':' )
947 if( i_name_len == 0 ) return;
949 psz_name = strndup( psz_option, i_name_len );
950 if( psz_value ) psz_value++;
952 /* FIXME: :programs should be handled generically */
953 if( !strcmp( psz_name, "programs" ) )
954 i_type = VLC_VAR_LIST;
956 i_type = config_GetType( p_obj, psz_name );
958 if( !i_type && !psz_value )
960 /* check for "no-foo" or "nofoo" */
961 if( !strncmp( psz_name, "no-", 3 ) )
963 memmove( psz_name, psz_name + 3, strlen(psz_name) + 1 - 3 );
965 else if( !strncmp( psz_name, "no", 2 ) )
967 memmove( psz_name, psz_name + 2, strlen(psz_name) + 1 - 2 );
969 else goto cleanup; /* Option doesn't exist */
972 i_type = config_GetType( p_obj, psz_name );
974 if( !i_type ) goto cleanup; /* Option doesn't exist */
976 else if( !i_type ) goto cleanup; /* Option doesn't exist */
978 if( ( i_type != VLC_VAR_BOOL ) &&
979 ( !psz_value || !*psz_value ) ) goto cleanup; /* Invalid value */
981 /* Create the variable in the input object.
982 * Children of the input object will be able to retreive this value
983 * thanks to the inheritance property of the object variables. */
984 var_Create( p_obj, psz_name, i_type );
989 val.b_bool = !b_isno;
992 case VLC_VAR_INTEGER:
993 val.i_int = strtol( psz_value, NULL, 0 );
997 val.f_float = atof( psz_value );
1000 case VLC_VAR_STRING:
1001 case VLC_VAR_MODULE:
1003 case VLC_VAR_DIRECTORY:
1004 val.psz_string = psz_value;
1009 char *psz_orig, *psz_var;
1010 vlc_list_t *p_list = malloc(sizeof(vlc_list_t));
1011 val.p_list = p_list;
1012 p_list->i_count = 0;
1014 psz_var = psz_orig = strdup(psz_value);
1015 while( psz_var && *psz_var )
1017 char *psz_item = psz_var;
1019 while( *psz_var && *psz_var != ',' ) psz_var++;
1020 if( *psz_var == ',' )
1025 val2.i_int = strtol( psz_item, NULL, 0 );
1026 INSERT_ELEM( p_list->p_values, p_list->i_count,
1027 p_list->i_count, val2 );
1028 /* p_list->i_count is incremented twice by INSERT_ELEM */
1030 INSERT_ELEM( p_list->pi_types, p_list->i_count,
1031 p_list->i_count, VLC_VAR_INTEGER );
1033 if( psz_orig ) free( psz_orig );
1042 var_Set( p_obj, psz_name, val );
1045 if( psz_name ) free( psz_name );
1050 /* Following functions are local */
1052 /*****************************************************************************
1053 * GetUnused: find an unused variable from its name
1054 *****************************************************************************
1055 * We do i_tries tries before giving up, just in case the variable is being
1056 * modified and called from a callback.
1057 *****************************************************************************/
1058 static int GetUnused( vlc_object_t *p_this, const char *psz_name )
1060 int i_var, i_tries = 0;
1064 i_var = Lookup( p_this->p_vars, p_this->i_vars, psz_name );
1070 if( ! p_this->p_vars[i_var].b_incallback )
1075 if( i_tries++ > 100 )
1077 msg_Err( p_this, "caught in a callback deadlock?" );
1078 return VLC_ETIMEOUT;
1081 vlc_mutex_unlock( &p_this->var_lock );
1082 msleep( THREAD_SLEEP );
1083 vlc_mutex_lock( &p_this->var_lock );
1087 /*****************************************************************************
1088 * HashString: our cool hash function
1089 *****************************************************************************
1090 * This function is not intended to be crypto-secure, we only want it to be
1091 * fast and not suck too much. This one is pretty fast and did 0 collisions
1092 * in wenglish's dictionary.
1093 *****************************************************************************/
1094 static uint32_t HashString( const char *psz_string )
1096 uint32_t i_hash = 0;
1098 while( *psz_string )
1100 i_hash += *psz_string++;
1101 i_hash += i_hash << 10;
1102 i_hash ^= i_hash >> 8;
1108 /*****************************************************************************
1109 * Insert: find an empty slot to insert a new variable
1110 *****************************************************************************
1111 * We use a recursive inner function indexed on the hash. This function does
1112 * nothing in the rare cases where a collision may occur, see Lookup()
1113 * to see how we handle them.
1114 * XXX: does this really need to be written recursively?
1115 *****************************************************************************/
1116 static int Insert( variable_t *p_vars, int i_count, const char *psz_name )
1123 return InsertInner( p_vars, i_count, HashString( psz_name ) );
1126 static int InsertInner( variable_t *p_vars, int i_count, uint32_t i_hash )
1130 if( i_hash <= p_vars[0].i_hash )
1135 if( i_hash >= p_vars[i_count - 1].i_hash )
1140 i_middle = i_count / 2;
1142 /* We know that 0 < i_middle */
1143 if( i_hash < p_vars[i_middle].i_hash )
1145 return InsertInner( p_vars, i_middle, i_hash );
1148 /* We know that i_middle + 1 < i_count */
1149 if( i_hash > p_vars[i_middle + 1].i_hash )
1151 return i_middle + 1 + InsertInner( p_vars + i_middle + 1,
1152 i_count - i_middle - 1,
1156 return i_middle + 1;
1159 /*****************************************************************************
1160 * Lookup: find an existing variable given its name
1161 *****************************************************************************
1162 * We use a recursive inner function indexed on the hash. Care is taken of
1163 * possible hash collisions.
1164 * XXX: does this really need to be written recursively?
1165 *****************************************************************************/
1166 static int Lookup( variable_t *p_vars, int i_count, const char *psz_name )
1176 i_hash = HashString( psz_name );
1178 i_pos = LookupInner( p_vars, i_count, i_hash );
1180 /* Hash not found */
1181 if( i_hash != p_vars[i_pos].i_hash )
1186 /* Hash found, entry found */
1187 if( !strcmp( psz_name, p_vars[i_pos].psz_name ) )
1192 /* Hash collision! This should be very rare, but we cannot guarantee
1193 * it will never happen. Just do an exhaustive search amongst all
1194 * entries with the same hash. */
1195 for( i = i_pos - 1 ; i > 0 && i_hash == p_vars[i].i_hash ; i-- )
1197 if( !strcmp( psz_name, p_vars[i].psz_name ) )
1203 for( i = i_pos + 1 ; i < i_count && i_hash == p_vars[i].i_hash ; i++ )
1205 if( !strcmp( psz_name, p_vars[i].psz_name ) )
1211 /* Hash found, but entry not found */
1215 static int LookupInner( variable_t *p_vars, int i_count, uint32_t i_hash )
1219 if( i_hash <= p_vars[0].i_hash )
1224 if( i_hash >= p_vars[i_count-1].i_hash )
1229 i_middle = i_count / 2;
1231 /* We know that 0 < i_middle */
1232 if( i_hash < p_vars[i_middle].i_hash )
1234 return LookupInner( p_vars, i_middle, i_hash );
1237 /* We know that i_middle + 1 < i_count */
1238 if( i_hash > p_vars[i_middle].i_hash )
1240 return i_middle + LookupInner( p_vars + i_middle,
1248 /*****************************************************************************
1249 * CheckValue: check that a value is valid wrt. a variable
1250 *****************************************************************************
1251 * This function checks p_val's value against p_var's limitations such as
1252 * minimal and maximal value, step, in-list position, and modifies p_val if
1254 ****************************************************************************/
1255 static void CheckValue ( variable_t *p_var, vlc_value_t *p_val )
1257 /* Check that our variable is in the list */
1258 if( p_var->i_type & VLC_VAR_HASCHOICE && p_var->choices.i_count )
1262 /* FIXME: the list is sorted, dude. Use something cleverer. */
1263 for( i = p_var->choices.i_count ; i-- ; )
1265 if( p_var->pf_cmp( *p_val, p_var->choices.p_values[i] ) == 0 )
1271 /* If not found, change it to anything vaguely valid */
1274 /* Free the old variable, get the new one, dup it */
1275 p_var->pf_free( p_val );
1276 *p_val = p_var->choices.p_values[p_var->i_default >= 0
1277 ? p_var->i_default : 0 ];
1278 p_var->pf_dup( p_val );
1282 /* Check that our variable is within the bounds */
1283 switch( p_var->i_type & VLC_VAR_TYPE )
1285 case VLC_VAR_INTEGER:
1286 if( p_var->i_type & VLC_VAR_HASSTEP && p_var->step.i_int
1287 && (p_val->i_int % p_var->step.i_int) )
1289 p_val->i_int = (p_val->i_int + (p_var->step.i_int / 2))
1290 / p_var->step.i_int * p_var->step.i_int;
1292 if( p_var->i_type & VLC_VAR_HASMIN
1293 && p_val->i_int < p_var->min.i_int )
1295 p_val->i_int = p_var->min.i_int;
1297 if( p_var->i_type & VLC_VAR_HASMAX
1298 && p_val->i_int > p_var->max.i_int )
1300 p_val->i_int = p_var->max.i_int;
1304 if( p_var->i_type & VLC_VAR_HASSTEP && p_var->step.f_float )
1306 float f_round = p_var->step.f_float * (float)(int)( 0.5 +
1307 p_val->f_float / p_var->step.f_float );
1308 if( p_val->f_float != f_round )
1310 p_val->f_float = f_round;
1313 if( p_var->i_type & VLC_VAR_HASMIN
1314 && p_val->f_float < p_var->min.f_float )
1316 p_val->f_float = p_var->min.f_float;
1318 if( p_var->i_type & VLC_VAR_HASMAX
1319 && p_val->f_float > p_var->max.f_float )
1321 p_val->f_float = p_var->max.f_float;
1330 /*****************************************************************************
1331 * InheritValue: try to inherit the value of this variable from the same one
1332 * in our closest parent.
1333 *****************************************************************************/
1334 static int InheritValue( vlc_object_t *p_this, const char *psz_name,
1335 vlc_value_t *p_val, int i_type )
1340 /* No need to take the structure lock,
1341 * we are only looking for our parents */
1343 if( !p_this->p_parent )
1345 switch( i_type & VLC_VAR_TYPE )
1348 case VLC_VAR_DIRECTORY:
1349 case VLC_VAR_STRING:
1350 case VLC_VAR_MODULE:
1351 p_val->psz_string = config_GetPsz( p_this, psz_name );
1352 if( !p_val->psz_string ) p_val->psz_string = strdup("");
1355 p_val->f_float = config_GetFloat( p_this, psz_name );
1357 case VLC_VAR_INTEGER:
1358 case VLC_VAR_HOTKEY:
1359 p_val->i_int = config_GetInt( p_this, psz_name );
1362 p_val->b_bool = config_GetInt( p_this, psz_name );
1366 char *psz_orig, *psz_var;
1367 vlc_list_t *p_list = malloc(sizeof(vlc_list_t));
1368 p_val->p_list = p_list;
1369 p_list->i_count = 0;
1371 psz_var = psz_orig = config_GetPsz( p_this, psz_name );
1372 while( psz_var && *psz_var )
1374 char *psz_item = psz_var;
1376 while( *psz_var && *psz_var != ',' ) psz_var++;
1377 if( *psz_var == ',' )
1382 val.i_int = strtol( psz_item, NULL, 0 );
1383 INSERT_ELEM( p_list->p_values, p_list->i_count,
1384 p_list->i_count, val );
1385 /* p_list->i_count is incremented twice by INSERT_ELEM */
1387 INSERT_ELEM( p_list->pi_types, p_list->i_count,
1388 p_list->i_count, VLC_VAR_INTEGER );
1390 if( psz_orig ) free( psz_orig );
1401 /* Look for the variable */
1402 vlc_mutex_lock( &p_this->p_parent->var_lock );
1404 i_var = Lookup( p_this->p_parent->p_vars, p_this->p_parent->i_vars,
1410 p_var = &p_this->p_parent->p_vars[i_var];
1412 /* Really get the variable */
1413 *p_val = p_var->val;
1415 /* Duplicate value if needed */
1416 p_var->pf_dup( p_val );
1418 vlc_mutex_unlock( &p_this->p_parent->var_lock );
1422 vlc_mutex_unlock( &p_this->p_parent->var_lock );
1424 /* We're still not there */
1426 return InheritValue( p_this->p_parent, psz_name, p_val, i_type );