1 /*****************************************************************************
2 * variables.c: routines for object variables handling
3 *****************************************************************************
4 * Copyright (C) 2002 VideoLAN
5 * $Id: variables.c,v 1.14 2002/12/06 10:10:40 sam 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 FreeDummy( vlc_value_t *p_val ) { (void)p_val; /* unused */ }
58 static void FreeString( vlc_value_t *p_val ) { free( p_val->psz_string ); }
60 /*****************************************************************************
62 *****************************************************************************/
63 static int GetUnused ( vlc_object_t *, const char * );
64 static uint32_t HashString ( const char * );
65 static int Insert ( variable_t *, int, const char * );
66 static int InsertInner ( variable_t *, int, uint32_t );
67 static int Lookup ( variable_t *, int, const char * );
68 static int LookupInner ( variable_t *, int, uint32_t );
70 static void CheckValue ( variable_t *, vlc_value_t * );
72 /*****************************************************************************
73 * var_Create: initialize a vlc variable
74 *****************************************************************************
75 * We hash the given string and insert it into the sorted list. The insertion
76 * may require slow memory copies, but think about what we gain in the log(n)
77 * lookup phase when setting/getting the variable value!
78 *****************************************************************************/
79 int __var_Create( vlc_object_t *p_this, const char *psz_name, int i_type )
84 vlc_mutex_lock( &p_this->var_lock );
86 /* FIXME: if the variable already exists, we don't duplicate it. But we
87 * duplicate the lookups. It's not that serious, but if anyone finds some
88 * time to rework Insert() so that only one lookup has to be done, feel
90 i_new = Lookup( p_this->p_vars, p_this->i_vars, psz_name );
94 /* If the types differ, variable creation failed. */
95 if( i_type != p_this->p_vars[i_new].i_type )
97 vlc_mutex_unlock( &p_this->var_lock );
101 p_this->p_vars[i_new].i_usage++;
102 vlc_mutex_unlock( &p_this->var_lock );
106 i_new = Insert( p_this->p_vars, p_this->i_vars, psz_name );
108 if( (p_this->i_vars & 15) == 15 )
110 p_this->p_vars = realloc( p_this->p_vars,
111 (p_this->i_vars+17) * sizeof(variable_t) );
114 memmove( p_this->p_vars + i_new + 1,
115 p_this->p_vars + i_new,
116 (p_this->i_vars - i_new) * sizeof(variable_t) );
120 p_var = &p_this->p_vars[i_new];
122 p_var->i_hash = HashString( psz_name );
123 p_var->psz_name = strdup( psz_name );
125 p_var->i_type = i_type;
126 memset( &p_var->val, 0, sizeof(vlc_value_t) );
128 p_var->pf_dup = DupDummy;
129 p_var->pf_free = FreeDummy;
133 p_var->i_default = -1;
134 p_var->i_choices = 0;
135 p_var->pp_choices = NULL;
137 p_var->b_incallback = VLC_FALSE;
138 p_var->i_entries = 0;
139 p_var->p_entries = NULL;
141 /* Always initialize the variable, even if it is a list variable; this
142 * will lead to errors if the variable is not initialized, but it will
143 * not cause crashes in the variable handling. */
144 switch( i_type & VLC_VAR_TYPE )
147 p_var->pf_cmp = CmpBool;
148 p_var->val.b_bool = VLC_FALSE;
150 case VLC_VAR_INTEGER:
151 p_var->pf_cmp = CmpInt;
152 p_var->val.i_int = 0;
157 p_var->pf_cmp = CmpString;
158 p_var->pf_dup = DupString;
159 p_var->pf_free = FreeString;
160 p_var->val.psz_string = "";
163 p_var->pf_cmp = CmpFloat;
164 p_var->val.f_float = 0.0;
169 case VLC_VAR_ADDRESS:
170 case VLC_VAR_COMMAND:
171 p_var->pf_cmp = CmpAddress;
172 p_var->val.p_address = NULL;
175 p_var->pf_cmp = CmpAddress;
176 p_var->val.p_address = malloc( sizeof(vlc_mutex_t) );
177 vlc_mutex_init( p_this, (vlc_mutex_t*)p_var->val.p_address );
181 /* Duplicate the default data we stored. */
182 p_var->pf_dup( &p_var->val );
184 vlc_mutex_unlock( &p_this->var_lock );
189 /*****************************************************************************
190 * var_Destroy: destroy a vlc variable
191 *****************************************************************************
192 * Look for the variable and destroy it if it is found. As in var_Create we
193 * do a call to memmove() but we have performance counterparts elsewhere.
194 *****************************************************************************/
195 int __var_Destroy( vlc_object_t *p_this, const char *psz_name )
200 vlc_mutex_lock( &p_this->var_lock );
202 i_var = GetUnused( p_this, psz_name );
205 vlc_mutex_unlock( &p_this->var_lock );
209 p_var = &p_this->p_vars[i_var];
211 if( p_var->i_usage > 1 )
214 vlc_mutex_unlock( &p_this->var_lock );
218 /* Free value if needed */
219 p_var->pf_free( &p_var->val );
221 switch( p_var->i_type & VLC_VAR_TYPE )
223 /* XXX: find a way to put this in pf_free */
225 vlc_mutex_destroy( (vlc_mutex_t*)p_var->val.p_address );
226 free( p_var->val.p_address );
230 /* Free choice list if needed */
231 if( p_var->pp_choices )
233 for( i = 0 ; i < p_var->i_choices ; i++ )
235 p_var->pf_free( &p_var->pp_choices[i] );
237 free( p_var->pp_choices );
240 /* Free callbacks if needed */
241 if( p_var->p_entries )
243 free( p_var->p_entries );
246 free( p_var->psz_name );
248 memmove( p_this->p_vars + i_var,
249 p_this->p_vars + i_var + 1,
250 (p_this->i_vars - i_var - 1) * sizeof(variable_t) );
252 if( (p_this->i_vars & 15) == 0 )
254 p_this->p_vars = realloc( p_this->p_vars,
255 (p_this->i_vars) * sizeof( variable_t ) );
260 vlc_mutex_unlock( &p_this->var_lock );
265 /*****************************************************************************
266 * var_Change: perform an action on a variable
267 *****************************************************************************
269 *****************************************************************************/
270 int __var_Change( vlc_object_t *p_this, const char *psz_name,
271 int i_action, vlc_value_t *p_val )
276 vlc_mutex_lock( &p_this->var_lock );
278 i_var = Lookup( p_this->p_vars, p_this->i_vars, psz_name );
282 vlc_mutex_unlock( &p_this->var_lock );
286 p_var = &p_this->p_vars[i_var];
291 if( p_var->i_type & VLC_VAR_HASMIN )
293 p_var->pf_free( &p_var->min );
295 p_var->i_type |= VLC_VAR_HASMIN;
297 p_var->pf_dup( &p_var->min );
298 CheckValue( p_var, &p_var->val );
301 if( p_var->i_type & VLC_VAR_HASMAX )
303 p_var->pf_free( &p_var->max );
305 p_var->i_type |= VLC_VAR_HASMAX;
307 p_var->pf_dup( &p_var->max );
308 CheckValue( p_var, &p_var->val );
310 case VLC_VAR_SETSTEP:
311 if( p_var->i_type & VLC_VAR_HASSTEP )
313 p_var->pf_free( &p_var->step );
315 p_var->i_type |= VLC_VAR_HASSTEP;
316 p_var->step = *p_val;
317 p_var->pf_dup( &p_var->step );
318 CheckValue( p_var, &p_var->val );
321 case VLC_VAR_ADDCHOICE:
322 /* FIXME: the list is sorted, dude. Use something cleverer. */
323 for( i = p_var->i_choices ; i-- ; )
325 if( p_var->pf_cmp( p_var->pp_choices[i], *p_val ) < 0 )
331 /* The new place is i+1 */
334 if( p_var->i_default >= i )
339 INSERT_ELEM( p_var->pp_choices, p_var->i_choices, i, *p_val );
340 p_var->pf_dup( &p_var->pp_choices[i] );
342 CheckValue( p_var, &p_var->val );
344 case VLC_VAR_DELCHOICE:
345 /* FIXME: the list is sorted, dude. Use something cleverer. */
346 for( i = 0 ; i < p_var->i_choices ; i++ )
348 if( p_var->pf_cmp( p_var->pp_choices[i], *p_val ) == 0 )
354 if( i == p_var->i_choices )
357 vlc_mutex_unlock( &p_this->var_lock );
361 if( p_var->i_default > i )
365 else if( p_var->i_default == i )
367 p_var->i_default = -1;
370 p_var->pf_free( &p_var->pp_choices[i] );
371 REMOVE_ELEM( p_var->pp_choices, p_var->i_choices, i );
373 CheckValue( p_var, &p_var->val );
375 case VLC_VAR_SETDEFAULT:
376 /* FIXME: the list is sorted, dude. Use something cleverer. */
377 for( i = 0 ; i < p_var->i_choices ; i++ )
379 if( p_var->pf_cmp( p_var->pp_choices[i], *p_val ) == 0 )
385 if( i == p_var->i_choices )
391 p_var->i_default = i;
392 CheckValue( p_var, &p_var->val );
395 case VLC_VAR_GETLIST:
396 p_val->p_address = malloc( (1 + p_var->i_choices)
397 * sizeof(vlc_value_t) );
398 ((vlc_value_t*)p_val->p_address)[0].i_int = p_var->i_choices;
399 for( i = 0 ; i < p_var->i_choices ; i++ )
401 ((vlc_value_t*)p_val->p_address)[i+1] = p_var->pp_choices[i];
402 p_var->pf_dup( &((vlc_value_t*)p_val->p_address)[i+1] );
405 case VLC_VAR_FREELIST:
406 for( i = ((vlc_value_t*)p_val->p_address)[0].i_int ; i-- ; )
408 p_var->pf_free( &((vlc_value_t*)p_val->p_address)[i+1] );
410 free( p_val->p_address );
417 vlc_mutex_unlock( &p_this->var_lock );
422 /*****************************************************************************
423 * var_Type: request a variable's type
424 *****************************************************************************
425 * This function returns the variable type if it exists, or an error if the
426 * variable could not be found.
427 *****************************************************************************/
428 int __var_Type( vlc_object_t *p_this, const char *psz_name )
432 vlc_mutex_lock( &p_this->var_lock );
434 i_var = Lookup( p_this->p_vars, p_this->i_vars, psz_name );
438 vlc_mutex_unlock( &p_this->var_lock );
442 i_type = p_this->p_vars[i_var].i_type;
444 vlc_mutex_unlock( &p_this->var_lock );
449 /*****************************************************************************
450 * var_Set: set a variable's value
451 *****************************************************************************
453 *****************************************************************************/
454 int __var_Set( vlc_object_t *p_this, const char *psz_name, vlc_value_t val )
460 vlc_mutex_lock( &p_this->var_lock );
462 i_var = GetUnused( p_this, psz_name );
465 vlc_mutex_unlock( &p_this->var_lock );
469 p_var = &p_this->p_vars[i_var];
471 /* Duplicate data if needed */
472 p_var->pf_dup( &val );
474 /* Backup needed stuff */
477 /* Check boundaries and list */
478 CheckValue( p_var, &val );
480 /* Set the variable */
483 /* Deal with callbacks. Tell we're in a callback, release the lock,
484 * call stored functions, retake the lock. */
485 if( p_var->i_entries )
488 int i_entries = p_var->i_entries;
489 callback_entry_t *p_entries = p_var->p_entries;
491 p_var->b_incallback = VLC_TRUE;
492 vlc_mutex_unlock( &p_this->var_lock );
495 for( ; i_entries-- ; )
497 p_entries[i_entries].pf_callback( p_this, psz_name, oldval, val,
498 p_entries[i_entries].p_data );
501 vlc_mutex_lock( &p_this->var_lock );
503 i_var = Lookup( p_this->p_vars, p_this->i_vars, psz_name );
506 msg_Err( p_this, "variable %s has disappeared" );
507 vlc_mutex_unlock( &p_this->var_lock );
511 p_var = &p_this->p_vars[i_var];
512 p_var->b_incallback = VLC_FALSE;
515 /* Free data if needed */
516 p_var->pf_free( &oldval );
518 vlc_mutex_unlock( &p_this->var_lock );
523 /*****************************************************************************
524 * var_Get: get a variable's value
525 *****************************************************************************
527 *****************************************************************************/
528 int __var_Get( vlc_object_t *p_this, const char *psz_name, vlc_value_t *p_val )
533 vlc_mutex_lock( &p_this->var_lock );
535 i_var = Lookup( p_this->p_vars, p_this->i_vars, psz_name );
539 vlc_mutex_unlock( &p_this->var_lock );
543 p_var = &p_this->p_vars[i_var];
545 /* Some variables trigger special behaviour. */
546 switch( p_var->i_type & VLC_VAR_TYPE )
548 case VLC_VAR_COMMAND:
549 if( p_var->val.p_address )
551 /* We need to save data before releasing the lock */
553 int (*pf_command) (vlc_object_t *, char *, char *) =
554 p_var->val.p_address;
555 char *psz_cmd = strdup( p_var->psz_name );
556 char *psz_arg = strdup( p_val->psz_string );
558 vlc_mutex_unlock( &p_this->var_lock );
560 i_ret = pf_command( p_this, psz_cmd, psz_arg );
569 /* Really get the variable */
572 /* Duplicate value if needed */
573 p_var->pf_dup( p_val );
575 vlc_mutex_unlock( &p_this->var_lock );
580 /*****************************************************************************
581 * var_AddCallback: register a callback in a variable
582 *****************************************************************************
583 * We store a function pointer pf_callback that will be called upon variable
584 * modification. p_data is a generic pointer that will be passed as additional
585 * argument to the callback function.
586 *****************************************************************************/
587 int __var_AddCallback( vlc_object_t *p_this, const char *psz_name,
588 vlc_callback_t pf_callback, void *p_data )
592 callback_entry_t entry;
594 entry.pf_callback = pf_callback;
595 entry.p_data = p_data;
597 vlc_mutex_lock( &p_this->var_lock );
599 i_var = GetUnused( p_this, psz_name );
602 vlc_mutex_unlock( &p_this->var_lock );
606 p_var = &p_this->p_vars[i_var];
608 INSERT_ELEM( p_var->p_entries,
613 vlc_mutex_unlock( &p_this->var_lock );
618 /*****************************************************************************
619 * var_DelCallback: remove a callback from a variable
620 *****************************************************************************
621 * pf_callback and p_data have to be given again, because different objects
622 * might have registered the same callback function.
623 *****************************************************************************/
624 int __var_DelCallback( vlc_object_t *p_this, const char *psz_name,
625 vlc_callback_t pf_callback, void *p_data )
630 vlc_mutex_lock( &p_this->var_lock );
632 i_var = GetUnused( p_this, psz_name );
635 vlc_mutex_unlock( &p_this->var_lock );
639 p_var = &p_this->p_vars[i_var];
641 for( i_entry = p_var->i_entries ; i_entry-- ; )
643 if( p_var->p_entries[i_entry].pf_callback == pf_callback
644 || p_var->p_entries[i_entry].p_data == p_data )
652 vlc_mutex_unlock( &p_this->var_lock );
656 REMOVE_ELEM( p_var->p_entries, p_var->i_entries, i_entry );
658 vlc_mutex_unlock( &p_this->var_lock );
663 /* Following functions are local */
665 /*****************************************************************************
666 * GetUnused: find an unused variable from its name
667 *****************************************************************************
668 * We do i_tries tries before giving up, just in case the variable is being
669 * modified and called from a callback.
670 *****************************************************************************/
671 static int GetUnused( vlc_object_t *p_this, const char *psz_name )
673 int i_var, i_tries = 0;
677 i_var = Lookup( p_this->p_vars, p_this->i_vars, psz_name );
683 if( ! p_this->p_vars[i_var].b_incallback )
688 if( i_tries++ > 100 )
690 msg_Err( p_this, "caught in a callback deadlock?" );
694 vlc_mutex_unlock( &p_this->var_lock );
695 msleep( THREAD_SLEEP );
696 vlc_mutex_lock( &p_this->var_lock );
700 /*****************************************************************************
701 * HashString: our cool hash function
702 *****************************************************************************
703 * This function is not intended to be crypto-secure, we only want it to be
704 * fast and not suck too much. This one is pretty fast and did 0 collisions
705 * in wenglish's dictionary.
706 *****************************************************************************/
707 static uint32_t HashString( const char *psz_string )
713 i_hash += *psz_string++;
714 i_hash += i_hash << 10;
715 i_hash ^= i_hash >> 8;
721 /*****************************************************************************
722 * Insert: find an empty slot to insert a new variable
723 *****************************************************************************
724 * We use a recursive inner function indexed on the hash. This function does
725 * nothing in the rare cases where a collision may occur, see Lookup()
726 * to see how we handle them.
727 * XXX: does this really need to be written recursively?
728 *****************************************************************************/
729 static int Insert( variable_t *p_vars, int i_count, const char *psz_name )
736 return InsertInner( p_vars, i_count, HashString( psz_name ) );
739 static int InsertInner( variable_t *p_vars, int i_count, uint32_t i_hash )
743 if( i_hash <= p_vars[0].i_hash )
748 if( i_hash >= p_vars[i_count - 1].i_hash )
753 i_middle = i_count / 2;
755 /* We know that 0 < i_middle */
756 if( i_hash < p_vars[i_middle].i_hash )
758 return InsertInner( p_vars, i_middle, i_hash );
761 /* We know that i_middle + 1 < i_count */
762 if( i_hash > p_vars[i_middle + 1].i_hash )
764 return i_middle + 1 + InsertInner( p_vars + i_middle + 1,
765 i_count - i_middle - 1,
772 /*****************************************************************************
773 * Lookup: find an existing variable given its name
774 *****************************************************************************
775 * We use a recursive inner function indexed on the hash. Care is taken of
776 * possible hash collisions.
777 * XXX: does this really need to be written recursively?
778 *****************************************************************************/
779 static int Lookup( variable_t *p_vars, int i_count, const char *psz_name )
789 i_hash = HashString( psz_name );
791 i_pos = LookupInner( p_vars, i_count, i_hash );
794 if( i_hash != p_vars[i_pos].i_hash )
799 /* Hash found, entry found */
800 if( !strcmp( psz_name, p_vars[i_pos].psz_name ) )
805 /* Hash collision! This should be very rare, but we cannot guarantee
806 * it will never happen. Just do an exhaustive search amongst all
807 * entries with the same hash. */
808 for( i = i_pos - 1 ; i > 0 && i_hash == p_vars[i].i_hash ; i-- )
810 if( !strcmp( psz_name, p_vars[i].psz_name ) )
816 for( i = i_pos + 1 ; i < i_count && i_hash == p_vars[i].i_hash ; i++ )
818 if( !strcmp( psz_name, p_vars[i].psz_name ) )
824 /* Hash found, but entry not found */
828 static int LookupInner( variable_t *p_vars, int i_count, uint32_t i_hash )
832 if( i_hash <= p_vars[0].i_hash )
837 if( i_hash >= p_vars[i_count-1].i_hash )
842 i_middle = i_count / 2;
844 /* We know that 0 < i_middle */
845 if( i_hash < p_vars[i_middle].i_hash )
847 return LookupInner( p_vars, i_middle, i_hash );
850 /* We know that i_middle + 1 < i_count */
851 if( i_hash > p_vars[i_middle].i_hash )
853 return i_middle + LookupInner( p_vars + i_middle,
861 /*****************************************************************************
862 * CheckValue: check that a value is valid wrt. a variable
863 *****************************************************************************
864 * This function checks p_val's value against p_var's limitations such as
865 * minimal and maximal value, step, in-list position, and modifies p_val if
867 *****************************************************************************/
868 static void CheckValue ( variable_t *p_var, vlc_value_t *p_val )
870 /* Check that our variable is in the list */
871 if( p_var->i_type & VLC_VAR_ISLIST && p_var->i_choices )
875 /* FIXME: the list is sorted, dude. Use something cleverer. */
876 for( i = p_var->i_choices ; i-- ; )
878 if( p_var->pf_cmp( *p_val, p_var->pp_choices[i] ) == 0 )
884 /* If not found, change it to anything vaguely valid */
887 /* Free the old variable, get the new one, dup it */
888 p_var->pf_free( p_val );
889 *p_val = p_var->pp_choices[p_var->i_default >= 0
890 ? p_var->i_default : 0 ];
891 p_var->pf_dup( p_val );
895 /* Check that our variable is within the bounds */
896 switch( p_var->i_type & VLC_VAR_TYPE )
898 case VLC_VAR_INTEGER:
899 if( p_var->i_type & VLC_VAR_HASSTEP && p_var->step.i_int
900 && (p_val->i_int % p_var->step.i_int) )
902 p_val->i_int = (p_val->i_int + (p_var->step.i_int / 2))
903 / p_var->step.i_int * p_var->step.i_int;
905 if( p_var->i_type & VLC_VAR_HASMIN
906 && p_val->i_int < p_var->min.i_int )
908 p_val->i_int = p_var->min.i_int;
910 if( p_var->i_type & VLC_VAR_HASMAX
911 && p_val->i_int > p_var->max.i_int )
913 p_val->i_int = p_var->max.i_int;
917 if( p_var->i_type & VLC_VAR_HASSTEP && p_var->step.f_float )
919 float f_round = p_var->step.f_float * (float)(int)( 0.5 +
920 p_val->f_float / p_var->step.f_float );
921 if( p_val->f_float != f_round )
923 p_val->f_float = f_round;
926 if( p_var->i_type & VLC_VAR_HASMIN
927 && p_val->f_float < p_var->min.f_float )
929 p_val->f_float = p_var->min.f_float;
931 if( p_var->i_type & VLC_VAR_HASMAX
932 && p_val->f_float > p_var->max.f_float )
934 p_val->f_float = p_var->max.f_float;