1 /*****************************************************************************
2 * variables.c: routines for object variables handling
3 *****************************************************************************
4 * Copyright (C) 2002-2009 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 *****************************************************************************/
31 #include <vlc_common.h>
32 #include <vlc_charset.h>
33 #include "variables.h"
39 /*****************************************************************************
41 *****************************************************************************/
42 struct callback_entry_t
44 vlc_callback_t pf_callback;
48 /*****************************************************************************
49 * Local comparison functions, returns 0 if v == w, < 0 if v < w, > 0 if v > w
50 *****************************************************************************/
51 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; }
52 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; }
53 static int CmpTime( vlc_value_t v, vlc_value_t w )
55 return v.i_time == w.i_time ? 0 : v.i_time > w.i_time ? 1 : -1;
57 static int CmpString( vlc_value_t v, vlc_value_t w )
60 return !w.psz_string ? 0 : -1;
62 return !w.psz_string ? 1 : strcmp( v.psz_string, w.psz_string );
64 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; }
65 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; }
67 /*****************************************************************************
68 * Local duplication functions, and local deallocation functions
69 *****************************************************************************/
70 static void DupDummy( vlc_value_t *p_val ) { (void)p_val; /* unused */ }
71 static void DupString( vlc_value_t *p_val )
73 p_val->psz_string = strdup( p_val->psz_string ? p_val->psz_string : "" );
76 static void DupList( vlc_value_t *p_val )
79 vlc_list_t *p_list = malloc( sizeof(vlc_list_t) );
81 p_list->i_count = p_val->p_list->i_count;
82 if( p_val->p_list->i_count )
84 p_list->p_values = malloc( p_list->i_count * sizeof(vlc_value_t) );
85 p_list->pi_types = malloc( p_list->i_count * sizeof(int) );
89 p_list->p_values = NULL;
90 p_list->pi_types = NULL;
93 for( i = 0; i < p_list->i_count; i++ )
95 p_list->p_values[i] = p_val->p_list->p_values[i];
96 p_list->pi_types[i] = p_val->p_list->pi_types[i];
97 switch( p_val->p_list->pi_types[i] & VLC_VAR_CLASS )
101 DupString( &p_list->p_values[i] );
108 p_val->p_list = p_list;
111 static void FreeDummy( vlc_value_t *p_val ) { (void)p_val; /* unused */ }
112 static void FreeString( vlc_value_t *p_val ) { free( p_val->psz_string ); }
113 static void FreeMutex( vlc_value_t *p_val ) { vlc_mutex_destroy( (vlc_mutex_t*)p_val->p_address ); free( p_val->p_address ); }
115 static void FreeList( vlc_value_t *p_val )
118 for( i = 0; i < p_val->p_list->i_count; i++ )
120 switch( p_val->p_list->pi_types[i] & VLC_VAR_CLASS )
123 FreeString( &p_val->p_list->p_values[i] );
126 FreeMutex( &p_val->p_list->p_values[i] );
133 if( p_val->p_list->i_count )
135 free( p_val->p_list->p_values );
136 free( p_val->p_list->pi_types );
138 free( p_val->p_list );
141 static const struct variable_ops_t
142 void_ops = { NULL, DupDummy, FreeDummy, },
143 addr_ops = { CmpAddress, DupDummy, FreeDummy, },
144 bool_ops = { CmpBool, DupDummy, FreeDummy, },
145 float_ops = { CmpFloat, DupDummy, FreeDummy, },
146 int_ops = { CmpInt, DupDummy, FreeDummy, },
147 list_ops = { CmpAddress, DupList, FreeList, },
148 mutex_ops = { CmpAddress, DupDummy, FreeMutex, },
149 string_ops = { CmpString, DupString, FreeString, },
150 time_ops = { CmpTime, DupDummy, FreeDummy, };
152 /*****************************************************************************
154 *****************************************************************************/
155 static int GetUnused ( vlc_object_t *, const char * );
156 static uint32_t HashString ( const char * );
157 static int Insert ( variable_t **, int, const char * );
158 static int InsertInner ( variable_t **, int, uint32_t );
159 static int Lookup ( variable_t **, size_t, const char * );
161 static void CheckValue ( variable_t *, vlc_value_t * );
163 static int InheritValue( vlc_object_t *, const char *, vlc_value_t *,
165 static int TriggerCallback( vlc_object_t *, variable_t *, const char *,
169 * Initialize a vlc variable
171 * We hash the given string and insert it into the sorted list. The insertion
172 * may require slow memory copies, but think about what we gain in the log(n)
173 * lookup phase when setting/getting the variable value!
175 * \param p_this The object in which to create the variable
176 * \param psz_name The name of the variable
177 * \param i_type The variables type. Must be one of \ref var_type combined with
178 * zero or more \ref var_flags
180 int __var_Create( vlc_object_t *p_this, const char *psz_name, int i_type )
182 static vlc_list_t dummy_null_list = {0, NULL, NULL};
185 variable_t *p_var = calloc( 1, sizeof( *p_var ) );
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 & ~VLC_VAR_DOINHERIT;
197 p_var->i_default = -1;
198 p_var->choices.i_count = 0;
199 p_var->choices.p_values = NULL;
200 p_var->choices_text.i_count = 0;
201 p_var->choices_text.p_values = NULL;
203 p_var->b_incallback = false;
204 p_var->i_entries = 0;
205 p_var->p_entries = NULL;
207 /* Always initialize the variable, even if it is a list variable; this
208 * will lead to errors if the variable is not initialized, but it will
209 * not cause crashes in the variable handling. */
210 switch( i_type & VLC_VAR_CLASS )
213 p_var->ops = &bool_ops;
214 p_var->val.b_bool = false;
216 case VLC_VAR_INTEGER:
217 p_var->ops = &int_ops;
218 p_var->val.i_int = 0;
221 p_var->ops = &string_ops;
222 p_var->val.psz_string = NULL;
225 p_var->ops = &float_ops;
226 p_var->val.f_float = 0.0;
229 p_var->ops = &time_ops;
230 p_var->val.i_time = 0;
232 case VLC_VAR_ADDRESS:
233 p_var->ops = &addr_ops;
234 p_var->val.p_address = NULL;
237 p_var->ops = &mutex_ops;
238 p_var->val.p_address = malloc( sizeof(vlc_mutex_t) );
239 vlc_mutex_init( (vlc_mutex_t*)p_var->val.p_address );
242 p_var->ops = &list_ops;
243 p_var->val.p_list = &dummy_null_list;
246 p_var->ops = &void_ops;
250 if( i_type & VLC_VAR_DOINHERIT )
252 if( InheritValue( p_this, psz_name, &p_var->val, p_var->i_type ) )
253 msg_Err( p_this, "cannot inherit value for %s", psz_name );
254 else if( i_type & VLC_VAR_HASCHOICE )
256 /* We must add the inherited value to our choice list */
257 p_var->i_default = 0;
259 INSERT_ELEM( p_var->choices.p_values, p_var->choices.i_count,
261 INSERT_ELEM( p_var->choices_text.p_values,
262 p_var->choices_text.i_count, 0, p_var->val );
263 p_var->ops->pf_dup( &p_var->choices.p_values[0] );
264 p_var->choices_text.p_values[0].psz_string = NULL;
268 vlc_object_internals_t *p_priv = vlc_internals( p_this );
271 vlc_mutex_lock( &p_priv->var_lock );
273 /* FIXME: if the variable already exists, we don't duplicate it. But we
274 * duplicate the lookups. It's not that serious, but if anyone finds some
275 * time to rework Insert() so that only one lookup has to be done, feel
277 i_new = Lookup( p_priv->pp_vars, p_priv->i_vars, psz_name );
281 /* If the types differ, variable creation failed. */
282 if( (i_type & VLC_VAR_CLASS) != (p_priv->pp_vars[i_new]->i_type & VLC_VAR_CLASS) )
284 msg_Err( p_this, "Variable '%s' (0x%04x) already exist but with a different type (0x%04x)",
285 psz_name, p_priv->pp_vars[i_new]->i_type, i_type );
286 vlc_mutex_unlock( &p_priv->var_lock );
290 p_priv->pp_vars[i_new]->i_usage++;
291 p_priv->pp_vars[i_new]->i_type |= ( i_type & VLC_VAR_ISCOMMAND );
292 p_priv->pp_vars[i_new]->i_type |= ( i_type & VLC_VAR_HASCHOICE );
293 vlc_mutex_unlock( &p_priv->var_lock );
295 /* We did not need to create a new variable, free everything... */
296 p_var->ops->pf_free( &p_var->val );
297 free( p_var->psz_name );
298 if( p_var->choices.i_count )
300 for( int i = 0 ; i < p_var->choices.i_count ; i++ )
302 p_var->ops->pf_free( &p_var->choices.p_values[i] );
303 free( p_var->choices_text.p_values[i].psz_string );
305 free( p_var->choices.p_values );
306 free( p_var->choices_text.p_values );
312 i_new = Insert( p_priv->pp_vars, p_priv->i_vars, psz_name );
314 if( (p_priv->i_vars & 15) == 0 )
315 p_priv->pp_vars = xrealloc( p_priv->pp_vars,
316 (p_priv->i_vars+16) * sizeof(variable_t *) );
318 memmove( p_priv->pp_vars + i_new + 1,
319 p_priv->pp_vars + i_new,
320 (p_priv->i_vars - i_new) * sizeof(variable_t *) );
324 p_priv->pp_vars[i_new] = p_var;
325 vlc_mutex_unlock( &p_priv->var_lock );
331 * Destroy a vlc variable
333 * Look for the variable and destroy it if it is found. As in var_Create we
334 * do a call to memmove() but we have performance counterparts elsewhere.
336 * \param p_this The object that holds the variable
337 * \param psz_name The name of the variable
339 int __var_Destroy( vlc_object_t *p_this, const char *psz_name )
346 vlc_object_internals_t *p_priv = vlc_internals( p_this );
348 vlc_mutex_lock( &p_priv->var_lock );
350 i_var = GetUnused( p_this, psz_name );
353 vlc_mutex_unlock( &p_priv->var_lock );
357 p_var = p_priv->pp_vars[i_var];
359 if( p_var->i_usage > 1 )
362 vlc_mutex_unlock( &p_priv->var_lock );
366 /* Free value if needed */
367 p_var->ops->pf_free( &p_var->val );
369 /* Free choice list if needed */
370 if( p_var->choices.i_count )
372 for( i = 0 ; i < p_var->choices.i_count ; i++ )
374 p_var->ops->pf_free( &p_var->choices.p_values[i] );
375 free( p_var->choices_text.p_values[i].psz_string );
377 free( p_var->choices.p_values );
378 free( p_var->choices_text.p_values );
381 /* Free callbacks if needed */
382 free( p_var->p_entries );
384 free( p_var->psz_name );
385 free( p_var->psz_text );
388 memmove( p_priv->pp_vars + i_var,
389 p_priv->pp_vars + i_var + 1,
390 (p_priv->i_vars - i_var) * sizeof(variable_t *) );
392 if( p_priv->i_vars == 0 )
394 free( p_priv->pp_vars );
395 p_priv->pp_vars = NULL;
398 if( (p_priv->i_vars & 15) == 0 )
400 variable_t **pp_vars = realloc( p_priv->pp_vars,
401 (p_priv->i_vars) * sizeof(variable_t *) );
402 if( pp_vars != NULL )
403 p_priv->pp_vars = pp_vars;
406 vlc_mutex_unlock( &p_priv->var_lock );
413 * Perform an action on a variable
415 * \param p_this The object that holds the variable
416 * \param psz_name The name of the variable
417 * \param i_action The action to perform. Must be one of \ref var_action
418 * \param p_val First action parameter
419 * \param p_val2 Second action parameter
421 int __var_Change( vlc_object_t *p_this, const char *psz_name,
422 int i_action, vlc_value_t *p_val, vlc_value_t *p_val2 )
430 vlc_object_internals_t *p_priv = vlc_internals( p_this );
432 vlc_mutex_lock( &p_priv->var_lock );
434 i_var = Lookup( p_priv->pp_vars, p_priv->i_vars, psz_name );
438 vlc_mutex_unlock( &p_priv->var_lock );
442 p_var = p_priv->pp_vars[i_var];
447 if( p_var->i_type & VLC_VAR_HASMIN )
449 p_var->ops->pf_free( &p_var->min );
451 p_var->i_type |= VLC_VAR_HASMIN;
453 p_var->ops->pf_dup( &p_var->min );
454 CheckValue( p_var, &p_var->val );
457 if( p_var->i_type & VLC_VAR_HASMIN )
463 if( p_var->i_type & VLC_VAR_HASMAX )
465 p_var->ops->pf_free( &p_var->max );
467 p_var->i_type |= VLC_VAR_HASMAX;
469 p_var->ops->pf_dup( &p_var->max );
470 CheckValue( p_var, &p_var->val );
473 if( p_var->i_type & VLC_VAR_HASMAX )
478 case VLC_VAR_SETSTEP:
479 if( p_var->i_type & VLC_VAR_HASSTEP )
481 p_var->ops->pf_free( &p_var->step );
483 p_var->i_type |= VLC_VAR_HASSTEP;
484 p_var->step = *p_val;
485 p_var->ops->pf_dup( &p_var->step );
486 CheckValue( p_var, &p_var->val );
488 case VLC_VAR_GETSTEP:
489 if( p_var->i_type & VLC_VAR_HASSTEP )
491 *p_val = p_var->step;
494 case VLC_VAR_ADDCHOICE:
495 i = p_var->choices.i_count;
497 INSERT_ELEM( p_var->choices.p_values, p_var->choices.i_count,
499 INSERT_ELEM( p_var->choices_text.p_values,
500 p_var->choices_text.i_count, i, *p_val );
501 p_var->ops->pf_dup( &p_var->choices.p_values[i] );
502 p_var->choices_text.p_values[i].psz_string =
503 ( p_val2 && p_val2->psz_string ) ?
504 strdup( p_val2->psz_string ) : NULL;
506 CheckValue( p_var, &p_var->val );
508 case VLC_VAR_DELCHOICE:
509 for( i = 0 ; i < p_var->choices.i_count ; i++ )
511 if( p_var->ops->pf_cmp( p_var->choices.p_values[i], *p_val ) == 0 )
517 if( i == p_var->choices.i_count )
520 vlc_mutex_unlock( &p_priv->var_lock );
524 if( p_var->i_default > i )
528 else if( p_var->i_default == i )
530 p_var->i_default = -1;
533 p_var->ops->pf_free( &p_var->choices.p_values[i] );
534 free( p_var->choices_text.p_values[i].psz_string );
535 REMOVE_ELEM( p_var->choices.p_values, p_var->choices.i_count, i );
536 REMOVE_ELEM( p_var->choices_text.p_values,
537 p_var->choices_text.i_count, i );
539 CheckValue( p_var, &p_var->val );
541 case VLC_VAR_CHOICESCOUNT:
542 p_val->i_int = p_var->choices.i_count;
544 case VLC_VAR_CLEARCHOICES:
545 for( i = 0 ; i < p_var->choices.i_count ; i++ )
547 p_var->ops->pf_free( &p_var->choices.p_values[i] );
549 for( i = 0 ; i < p_var->choices_text.i_count ; i++ )
550 free( p_var->choices_text.p_values[i].psz_string );
552 if( p_var->choices.i_count ) free( p_var->choices.p_values );
553 if( p_var->choices_text.i_count ) free( p_var->choices_text.p_values );
555 p_var->choices.i_count = 0;
556 p_var->choices.p_values = NULL;
557 p_var->choices_text.i_count = 0;
558 p_var->choices_text.p_values = NULL;
559 p_var->i_default = -1;
561 case VLC_VAR_SETDEFAULT:
562 /* FIXME: the list is sorted, dude. Use something cleverer. */
563 for( i = 0 ; i < p_var->choices.i_count ; i++ )
565 if( p_var->ops->pf_cmp( p_var->choices.p_values[i], *p_val ) == 0 )
571 if( i == p_var->choices.i_count )
577 p_var->i_default = i;
578 CheckValue( p_var, &p_var->val );
580 case VLC_VAR_SETVALUE:
581 /* Duplicate data if needed */
582 p_var->ops->pf_dup( p_val );
583 /* Backup needed stuff */
585 /* Check boundaries and list */
586 CheckValue( p_var, p_val );
587 /* Set the variable */
589 /* Free data if needed */
590 p_var->ops->pf_free( &oldval );
592 case VLC_VAR_GETCHOICES:
593 case VLC_VAR_GETLIST:
594 p_val->p_list = malloc( sizeof(vlc_list_t) );
595 if( p_val2 ) p_val2->p_list = malloc( sizeof(vlc_list_t) );
596 if( p_var->choices.i_count )
598 p_val->p_list->p_values = malloc( p_var->choices.i_count
599 * sizeof(vlc_value_t) );
600 p_val->p_list->pi_types = malloc( p_var->choices.i_count
604 p_val2->p_list->p_values =
605 malloc( p_var->choices.i_count * sizeof(vlc_value_t) );
606 p_val2->p_list->pi_types =
607 malloc( p_var->choices.i_count * sizeof(int) );
610 p_val->p_list->i_count = p_var->choices.i_count;
611 if( p_val2 ) p_val2->p_list->i_count = p_var->choices.i_count;
612 for( i = 0 ; i < p_var->choices.i_count ; i++ )
614 p_val->p_list->p_values[i] = p_var->choices.p_values[i];
615 p_val->p_list->pi_types[i] = p_var->i_type;
616 p_var->ops->pf_dup( &p_val->p_list->p_values[i] );
619 p_val2->p_list->p_values[i].psz_string =
620 p_var->choices_text.p_values[i].psz_string ?
621 strdup(p_var->choices_text.p_values[i].psz_string) : NULL;
622 p_val2->p_list->pi_types[i] = VLC_VAR_STRING;
626 case VLC_VAR_SETTEXT:
627 free( p_var->psz_text );
628 if( p_val && p_val->psz_string )
629 p_var->psz_text = strdup( p_val->psz_string );
631 p_var->psz_text = NULL;
633 case VLC_VAR_GETTEXT:
634 p_val->psz_string = p_var->psz_text ? strdup( p_var->psz_text )
637 case VLC_VAR_SETISCOMMAND:
638 p_var->i_type |= VLC_VAR_ISCOMMAND;
645 vlc_mutex_unlock( &p_priv->var_lock );
652 * Perform a Get and Set on a variable
654 * \param p_this: The object that hold the variable
655 * \param psz_name: the name of the variable
656 * \param i_action: the action to perform
657 * \param p_val: The action parameter
658 * \return vlc error codes
660 int __var_GetAndSet( vlc_object_t *p_this, const char *psz_name, int i_action,
664 int i_ret = VLC_SUCCESS;
670 vlc_object_internals_t *p_priv = vlc_internals( p_this );
672 vlc_mutex_lock( &p_priv->var_lock );
673 i_var = GetUnused( p_this, psz_name );
676 vlc_mutex_unlock( &p_priv->var_lock );
680 p_var = p_priv->pp_vars[i_var];
682 /* Duplicated data if needed */
683 //p_var->ops->pf_dup( &val );
685 /* Backup needed stuff */
688 /* depending of the action requiered */
691 case VLC_VAR_TOGGLE_BOOL:
692 assert( ( p_var->i_type & VLC_VAR_BOOL ) == VLC_VAR_BOOL );
693 p_var->val.b_bool = !p_var->val.b_bool;
695 case VLC_VAR_INTEGER_INCDEC:
696 assert( ( p_var->i_type & VLC_VAR_INTEGER ) == VLC_VAR_INTEGER );
697 p_var->val.i_int += val.i_int;
700 vlc_mutex_unlock( &p_priv->var_lock );
704 /* Check boundaries */
705 CheckValue( p_var, &p_var->val );
707 /* Deal with callbacks.*/
708 if( p_var->i_entries )
709 i_ret = TriggerCallback( p_this, p_var, psz_name, oldval );
711 vlc_mutex_unlock( &p_priv->var_lock );
718 * Request a variable's type
720 * \return The variable type if it exists, or 0 if the
721 * variable could not be found.
724 int __var_Type( vlc_object_t *p_this, const char *psz_name )
730 vlc_object_internals_t *p_priv = vlc_internals( p_this );
732 vlc_mutex_lock( &p_priv->var_lock );
734 i_var = Lookup( p_priv->pp_vars, p_priv->i_vars, psz_name );
738 vlc_mutex_unlock( &p_priv->var_lock );
742 i_type = p_priv->pp_vars[i_var]->i_type;
744 vlc_mutex_unlock( &p_priv->var_lock );
749 int var_SetChecked( vlc_object_t *p_this, const char *psz_name,
750 int expected_type, vlc_value_t val )
753 int i_ret = VLC_SUCCESS;
759 vlc_object_internals_t *p_priv = vlc_internals( p_this );
761 vlc_mutex_lock( &p_priv->var_lock );
763 i_var = GetUnused( p_this, psz_name );
766 vlc_mutex_unlock( &p_priv->var_lock );
770 p_var = p_priv->pp_vars[i_var];
771 assert( expected_type == 0 ||
772 (p_var->i_type & VLC_VAR_CLASS) == expected_type );
774 /* Duplicate data if needed */
775 p_var->ops->pf_dup( &val );
777 /* Backup needed stuff */
780 /* Check boundaries and list */
781 CheckValue( p_var, &val );
783 /* Set the variable */
786 /* Deal with callbacks */
787 if( p_var->i_entries )
788 i_ret = TriggerCallback( p_this, p_var, psz_name, oldval );
790 /* Free data if needed */
791 p_var->ops->pf_free( &oldval );
793 vlc_mutex_unlock( &p_priv->var_lock );
800 * Set a variable's value
802 * \param p_this The object that hold the variable
803 * \param psz_name The name of the variable
804 * \param val the value to set
806 int __var_Set( vlc_object_t *p_this, const char *psz_name, vlc_value_t val )
808 return var_SetChecked( p_this, psz_name, 0, val );
811 int var_GetChecked( vlc_object_t *p_this, const char *psz_name,
812 int expected_type, vlc_value_t *p_val )
816 vlc_object_internals_t *p_priv = vlc_internals( p_this );
817 int i_var, err = VLC_SUCCESS;
819 vlc_mutex_lock( &p_priv->var_lock );
821 i_var = Lookup( p_priv->pp_vars, p_priv->i_vars, psz_name );
824 variable_t *p_var = p_priv->pp_vars[i_var];
826 assert( expected_type == 0 ||
827 (p_var->i_type & VLC_VAR_CLASS) == expected_type );
829 /* Really get the variable */
833 /* Alert if the type is VLC_VAR_VOID */
834 if( ( p_var->i_type & VLC_VAR_TYPE ) == VLC_VAR_VOID )
835 msg_Warn( p_this, "Calling var_GetVoid on the void variable '%s' (0x%04x)", psz_name, p_var->i_type );
838 /* Duplicate value if needed */
839 p_var->ops->pf_dup( p_val );
844 vlc_mutex_unlock( &p_priv->var_lock );
849 * Get a variable's value
851 * \param p_this The object that holds the variable
852 * \param psz_name The name of the variable
853 * \param p_val Pointer to a vlc_value_t that will hold the variable's value
854 * after the function is finished
856 int __var_Get( vlc_object_t *p_this, const char *psz_name, vlc_value_t *p_val )
858 return var_GetChecked( p_this, psz_name, 0, p_val );
862 * Register a callback in a variable
864 * We store a function pointer that will be called upon variable
867 * \param p_this The object that holds the variable
868 * \param psz_name The name of the variable
869 * \param pf_callback The function pointer
870 * \param p_data A generic pointer that will be passed as the last
871 * argument to the callback function.
873 * \warning The callback function is run in the thread that calls var_Set on
874 * the variable. Use proper locking. This thread may not have much
875 * time to spare, so keep callback functions short.
877 int __var_AddCallback( vlc_object_t *p_this, const char *psz_name,
878 vlc_callback_t pf_callback, void *p_data )
882 callback_entry_t entry;
886 vlc_object_internals_t *p_priv = vlc_internals( p_this );
888 entry.pf_callback = pf_callback;
889 entry.p_data = p_data;
891 vlc_mutex_lock( &p_priv->var_lock );
893 i_var = GetUnused( p_this, psz_name );
897 msg_Warn( p_this, "Failed to add a callback to the non-existing "
898 "variable '%s'", psz_name );
900 vlc_mutex_unlock( &p_priv->var_lock );
904 p_var = p_priv->pp_vars[i_var];
906 INSERT_ELEM( p_var->p_entries,
911 vlc_mutex_unlock( &p_priv->var_lock );
917 * Remove a callback from a variable
919 * pf_callback and p_data have to be given again, because different objects
920 * might have registered the same callback function.
922 int __var_DelCallback( vlc_object_t *p_this, const char *psz_name,
923 vlc_callback_t pf_callback, void *p_data )
928 bool b_found_similar = false;
933 vlc_object_internals_t *p_priv = vlc_internals( p_this );
935 vlc_mutex_lock( &p_priv->var_lock );
937 i_var = GetUnused( p_this, psz_name );
940 vlc_mutex_unlock( &p_priv->var_lock );
944 p_var = p_priv->pp_vars[i_var];
946 for( i_entry = p_var->i_entries ; i_entry-- ; )
948 if( p_var->p_entries[i_entry].pf_callback == pf_callback
949 && p_var->p_entries[i_entry].p_data == p_data )
954 else if( p_var->p_entries[i_entry].pf_callback == pf_callback )
955 b_found_similar = true;
962 if( b_found_similar )
963 fprintf( stderr, "Calling var_DelCallback for '%s' with the same "
964 "function but not the same data.", psz_name );
967 vlc_mutex_unlock( &p_priv->var_lock );
971 REMOVE_ELEM( p_var->p_entries, p_var->i_entries, i_entry );
973 vlc_mutex_unlock( &p_priv->var_lock );
979 * Trigger callback on a variable
981 * \param p_this The object that hold the variable
982 * \param psz_name The name of the variable
984 int __var_TriggerCallback( vlc_object_t *p_this, const char *psz_name )
987 int i_ret = VLC_SUCCESS;
992 vlc_object_internals_t *p_priv = vlc_internals( p_this );
994 vlc_mutex_lock( &p_priv->var_lock );
996 i_var = GetUnused( p_this, psz_name );
999 vlc_mutex_unlock( &p_priv->var_lock );
1003 p_var = p_priv->pp_vars[i_var];
1005 /* Deal with callbacks. Tell we're in a callback, release the lock,
1006 * call stored functions, retake the lock. */
1007 if( p_var->i_entries )
1008 i_ret = TriggerCallback( p_this, p_var, psz_name, p_var->val );
1010 vlc_mutex_unlock( &p_priv->var_lock );
1014 /** Parse a stringified option
1015 * This function parse a string option and create the associated object
1017 * The option must be of the form "[no[-]]foo[=bar]" where foo is the
1018 * option name and bar is the value of the option.
1019 * \param p_obj the object in which the variable must be created
1020 * \param psz_option the option to parse
1021 * \param trusted whether the option is set by a trusted input or not
1024 void var_OptionParse( vlc_object_t *p_obj, const char *psz_option,
1027 char *psz_name, *psz_value;
1029 bool b_isno = false;
1032 val.psz_string = NULL;
1034 /* It's too much of a hassle to remove the ':' when we parse
1035 * the cmd line :) */
1036 if( psz_option[0] == ':' )
1039 if( !psz_option[0] )
1042 psz_name = strdup( psz_option );
1043 if( psz_name == NULL )
1046 psz_value = strchr( psz_name, '=' );
1047 if( psz_value != NULL )
1048 *psz_value++ = '\0';
1050 /* FIXME: :programs should be handled generically */
1051 if( !strcmp( psz_name, "programs" ) )
1052 i_type = VLC_VAR_LIST;
1054 i_type = config_GetType( p_obj, psz_name );
1056 if( !i_type && !psz_value )
1058 /* check for "no-foo" or "nofoo" */
1059 if( !strncmp( psz_name, "no-", 3 ) )
1061 memmove( psz_name, psz_name + 3, strlen(psz_name) + 1 - 3 );
1063 else if( !strncmp( psz_name, "no", 2 ) )
1065 memmove( psz_name, psz_name + 2, strlen(psz_name) + 1 - 2 );
1067 else goto cleanup; /* Option doesn't exist */
1070 i_type = config_GetType( p_obj, psz_name );
1072 if( !i_type ) goto cleanup; /* Option doesn't exist */
1074 if( ( i_type != VLC_VAR_BOOL ) &&
1075 ( !psz_value || !*psz_value ) ) goto cleanup; /* Invalid value */
1077 /* check if option is unsafe */
1080 module_config_t *p_config = config_FindConfig( p_obj, psz_name );
1081 if( !p_config || !p_config->b_safe )
1083 msg_Err( p_obj, "unsafe option \"%s\" has been ignored for "
1084 "security reasons", psz_name );
1090 /* Create the variable in the input object.
1091 * Children of the input object will be able to retreive this value
1092 * thanks to the inheritance property of the object variables. */
1093 __var_Create( p_obj, psz_name, i_type );
1098 val.b_bool = !b_isno;
1101 case VLC_VAR_INTEGER:
1102 val.i_int = strtol( psz_value, NULL, 0 );
1106 val.f_float = us_atof( psz_value );
1109 case VLC_VAR_STRING:
1110 case VLC_VAR_MODULE:
1112 case VLC_VAR_DIRECTORY:
1113 val.psz_string = psz_value;
1118 char *psz_orig, *psz_var;
1119 vlc_list_t *p_list = malloc(sizeof(vlc_list_t));
1120 val.p_list = p_list;
1121 p_list->i_count = 0;
1123 psz_var = psz_orig = strdup(psz_value);
1124 while( psz_var && *psz_var )
1126 char *psz_item = psz_var;
1128 while( *psz_var && *psz_var != ',' ) psz_var++;
1129 if( *psz_var == ',' )
1134 val2.i_int = strtol( psz_item, NULL, 0 );
1135 INSERT_ELEM( p_list->p_values, p_list->i_count,
1136 p_list->i_count, val2 );
1137 /* p_list->i_count is incremented twice by INSERT_ELEM */
1139 INSERT_ELEM( p_list->pi_types, p_list->i_count,
1140 p_list->i_count, VLC_VAR_INTEGER );
1150 __var_Set( p_obj, psz_name, val );
1152 /* If that's a list, remove all elements allocated */
1153 if( i_type == VLC_VAR_LIST )
1161 /* Following functions are local */
1163 /*****************************************************************************
1164 * GetUnused: find an unused (not in callback) variable from its name
1165 *****************************************************************************
1166 * We do i_tries tries before giving up, just in case the variable is being
1167 * modified and called from a callback.
1168 *****************************************************************************/
1169 static int GetUnused( vlc_object_t *p_this, const char *psz_name )
1173 vlc_object_internals_t *p_priv = vlc_internals( p_this );
1179 i_var = Lookup( p_priv->pp_vars, p_priv->i_vars, psz_name );
1185 if( ! p_priv->pp_vars[i_var]->b_incallback )
1190 mutex_cleanup_push( &p_priv->var_lock );
1191 vlc_cond_wait( &p_priv->var_wait, &p_priv->var_lock );
1196 /*****************************************************************************
1197 * HashString: our cool hash function
1198 *****************************************************************************
1199 * This function is not intended to be crypto-secure, we only want it to be
1200 * fast and not suck too much. This one is pretty fast and did 0 collisions
1201 * in wenglish's dictionary.
1202 *****************************************************************************/
1203 static uint32_t HashString( const char *psz_string )
1205 uint32_t i_hash = 0;
1207 while( *psz_string )
1209 i_hash += *psz_string++;
1210 i_hash += i_hash << 10;
1211 i_hash ^= i_hash >> 8;
1217 /*****************************************************************************
1218 * Insert: find an empty slot to insert a new variable
1219 *****************************************************************************
1220 * We use a recursive inner function indexed on the hash. This function does
1221 * nothing in the rare cases where a collision may occur, see Lookup()
1222 * to see how we handle them.
1223 * XXX: does this really need to be written recursively?
1224 *****************************************************************************/
1225 static int Insert( variable_t **pp_vars, int i_count, const char *psz_name )
1232 return InsertInner( pp_vars, i_count, HashString( psz_name ) );
1235 static int InsertInner( variable_t **pp_vars, int i_count, uint32_t i_hash )
1239 if( i_hash <= pp_vars[0]->i_hash )
1244 if( i_hash >= pp_vars[i_count - 1]->i_hash )
1249 i_middle = i_count / 2;
1251 /* We know that 0 < i_middle */
1252 if( i_hash < pp_vars[i_middle]->i_hash )
1254 return InsertInner( pp_vars, i_middle, i_hash );
1257 /* We know that i_middle + 1 < i_count */
1258 if( i_hash > pp_vars[i_middle + 1]->i_hash )
1260 return i_middle + 1 + InsertInner( pp_vars + i_middle + 1,
1261 i_count - i_middle - 1,
1265 return i_middle + 1;
1268 static int u32cmp( const void *key, const void *data )
1270 const variable_t *const *pp_var = data;
1271 uint32_t hash = *(const uint32_t *)key ;
1273 if( hash > (*pp_var)->i_hash )
1275 if( hash < (*pp_var)->i_hash )
1280 /*****************************************************************************
1281 * Lookup: find an existing variable given its name
1282 *****************************************************************************
1283 * We use a recursive inner function indexed on the hash. Care is taken of
1284 * possible hash collisions.
1285 *****************************************************************************/
1286 static int Lookup( variable_t **pp_vars, size_t i_count, const char *psz_name )
1288 variable_t **pp_var;
1291 i_hash = HashString( psz_name );
1292 pp_var = bsearch( &i_hash, pp_vars, i_count, sizeof( *pp_var ), u32cmp );
1294 /* Hash not found */
1295 if( pp_var == NULL )
1298 assert( i_count > 0 );
1300 /* Find the first entry with the right hash */
1301 while( (pp_var > pp_vars) && (i_hash == pp_var[-1]->i_hash) )
1304 assert( (*pp_var)->i_hash == i_hash );
1306 /* Hash collision should be very unlikely, but we cannot guarantee
1307 * it will never happen. So we do an exhaustive search amongst all
1308 * entries with the same hash. Typically, there is only one anyway. */
1309 for( variable_t **p_end = pp_vars + i_count;
1310 (pp_var < p_end) && (i_hash == (*pp_var)->i_hash);
1313 if( !strcmp( psz_name, (*pp_var)->psz_name ) )
1314 return pp_var - pp_vars;
1317 /* Hash found, but entry not found */
1321 /*****************************************************************************
1322 * CheckValue: check that a value is valid wrt. a variable
1323 *****************************************************************************
1324 * This function checks p_val's value against p_var's limitations such as
1325 * minimal and maximal value, step, in-list position, and modifies p_val if
1327 ****************************************************************************/
1328 static void CheckValue ( variable_t *p_var, vlc_value_t *p_val )
1330 /* Check that our variable is in the list */
1331 if( p_var->i_type & VLC_VAR_HASCHOICE && p_var->choices.i_count )
1335 /* This list is not sorted so go throug it (this is a small list) */
1336 for( i = p_var->choices.i_count ; i-- ; )
1338 if( p_var->ops->pf_cmp( *p_val, p_var->choices.p_values[i] ) == 0 )
1344 /* If not found, change it to anything vaguely valid */
1347 /* Free the old variable, get the new one, dup it */
1348 p_var->ops->pf_free( p_val );
1349 *p_val = p_var->choices.p_values[p_var->i_default >= 0
1350 ? p_var->i_default : 0 ];
1351 p_var->ops->pf_dup( p_val );
1355 /* Check that our variable is within the bounds */
1356 switch( p_var->i_type & VLC_VAR_TYPE )
1358 case VLC_VAR_INTEGER:
1359 if( p_var->i_type & VLC_VAR_HASSTEP && p_var->step.i_int
1360 && (p_val->i_int % p_var->step.i_int) )
1362 p_val->i_int = (p_val->i_int + (p_var->step.i_int / 2))
1363 / p_var->step.i_int * p_var->step.i_int;
1365 if( p_var->i_type & VLC_VAR_HASMIN
1366 && p_val->i_int < p_var->min.i_int )
1368 p_val->i_int = p_var->min.i_int;
1370 if( p_var->i_type & VLC_VAR_HASMAX
1371 && p_val->i_int > p_var->max.i_int )
1373 p_val->i_int = p_var->max.i_int;
1377 if( p_var->i_type & VLC_VAR_HASSTEP && p_var->step.f_float )
1379 float f_round = p_var->step.f_float * (float)(int)( 0.5 +
1380 p_val->f_float / p_var->step.f_float );
1381 if( p_val->f_float != f_round )
1383 p_val->f_float = f_round;
1386 if( p_var->i_type & VLC_VAR_HASMIN
1387 && p_val->f_float < p_var->min.f_float )
1389 p_val->f_float = p_var->min.f_float;
1391 if( p_var->i_type & VLC_VAR_HASMAX
1392 && p_val->f_float > p_var->max.f_float )
1394 p_val->f_float = p_var->max.f_float;
1403 /*****************************************************************************
1404 * InheritValue: try to inherit the value of this variable from the closest
1405 * ancestor objects or ultimately from the configuration.
1406 * The function should always be entered with the object var_lock locked.
1407 *****************************************************************************/
1408 static int InheritValue( vlc_object_t *p_this, const char *psz_name,
1409 vlc_value_t *p_val, int i_type )
1411 i_type &= VLC_VAR_CLASS;
1412 for( vlc_object_t *obj = p_this->p_parent; obj != NULL; obj = obj->p_parent )
1413 if( var_GetChecked( obj, psz_name, i_type, p_val ) == VLC_SUCCESS )
1416 /* else take value from config */
1417 switch( i_type & VLC_VAR_CLASS )
1419 case VLC_VAR_STRING:
1420 p_val->psz_string = config_GetPsz( p_this, psz_name );
1421 if( !p_val->psz_string ) p_val->psz_string = strdup("");
1424 p_val->f_float = config_GetFloat( p_this, psz_name );
1426 case VLC_VAR_INTEGER:
1427 p_val->i_int = config_GetInt( p_this, psz_name );
1430 p_val->b_bool = config_GetInt( p_this, psz_name );
1434 char *psz_orig, *psz_var;
1435 vlc_list_t *p_list = malloc(sizeof(vlc_list_t));
1436 p_val->p_list = p_list;
1437 p_list->i_count = 0;
1439 psz_var = psz_orig = config_GetPsz( p_this, psz_name );
1440 while( psz_var && *psz_var )
1442 char *psz_item = psz_var;
1444 while( *psz_var && *psz_var != ',' ) psz_var++;
1445 if( *psz_var == ',' )
1450 val.i_int = strtol( psz_item, NULL, 0 );
1451 INSERT_ELEM( p_list->p_values, p_list->i_count,
1452 p_list->i_count, val );
1453 /* p_list->i_count is incremented twice by INSERT_ELEM */
1455 INSERT_ELEM( p_list->pi_types, p_list->i_count,
1456 p_list->i_count, VLC_VAR_INTEGER );
1462 msg_Warn( p_this, "Could not inherit value for var %s "
1463 "from config. Invalid Type", psz_name );
1467 /*msg_Dbg( p_this, "Inherited value for var %s from config", psz_name );*/
1472 /**********************************************************************
1473 * Trigger the callbacks.
1474 * Tell we're in a callback, release the lock, call stored functions,
1476 **********************************************************************/
1477 static int TriggerCallback( vlc_object_t *p_this, variable_t *p_var,
1478 const char *psz_name, vlc_value_t oldval )
1480 int i_entries = p_var->i_entries;
1481 callback_entry_t *p_entries = p_var->p_entries;
1485 vlc_object_internals_t *p_priv = vlc_internals( p_this );
1487 p_var->b_incallback = true;
1488 vlc_mutex_unlock( &p_priv->var_lock );
1490 /* The real calls */
1491 for( ; i_entries-- ; )
1493 p_entries[i_entries].pf_callback( p_this, psz_name, oldval, p_var->val,
1494 p_entries[i_entries].p_data );
1497 vlc_mutex_lock( &p_priv->var_lock );
1498 p_var->b_incallback = false;
1499 vlc_cond_broadcast( &p_priv->var_wait );
1505 /**********************************************************************
1506 * Execute a var command on an object identified by its name
1507 **********************************************************************/
1508 int __var_Command( vlc_object_t *p_this, const char *psz_name,
1509 const char *psz_cmd, const char *psz_arg, char **psz_msg )
1511 vlc_object_t *p_obj = vlc_object_find_name( p_this->p_libvlc,
1512 psz_name, FIND_CHILD );
1518 *psz_msg = strdup( "Unknown destination object." );
1522 i_type = var_Type( p_obj, psz_cmd );
1523 if( !( i_type&VLC_VAR_ISCOMMAND ) )
1525 vlc_object_release( p_obj );
1527 *psz_msg = strdup( "Variable doesn't exist or isn't a command." );
1528 return VLC_EGENERIC;
1531 i_type &= VLC_VAR_CLASS;
1534 case VLC_VAR_INTEGER:
1535 i_ret = var_SetInteger( p_obj, psz_cmd, atoi( psz_arg ) );
1538 i_ret = var_SetFloat( p_obj, psz_cmd, us_atof( psz_arg ) );
1540 case VLC_VAR_STRING:
1541 i_ret = var_SetString( p_obj, psz_cmd, psz_arg );
1544 i_ret = var_SetBool( p_obj, psz_cmd, atoi( psz_arg ) );
1547 i_ret = VLC_EGENERIC;
1551 vlc_object_release( p_obj );
1555 if( asprintf( psz_msg, "%s on object %s returned %i (%s)",
1556 psz_cmd, psz_name, i_ret, vlc_error( i_ret ) ) == -1)
1565 * Free a list and the associated strings
1566 * @param p_val: the list variable
1567 * @param p_val2: the variable associated or NULL
1569 void var_FreeList( vlc_value_t *p_val, vlc_value_t *p_val2 )
1572 if( p_val2 && p_val2->p_list )
1574 for( int i = 0; i < p_val2->p_list->i_count; i++ )
1575 free( p_val2->p_list->p_values[i].psz_string );
1576 if( p_val2->p_list->i_count )
1578 free( p_val2->p_list->p_values );
1579 free( p_val2->p_list->pi_types );
1581 free( p_val2->p_list );