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 variable_t var, *p_var = &var;
183 static vlc_list_t dummy_null_list = {0, NULL, NULL};
187 memset( p_var, 0, 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->p_vars, p_priv->i_vars, psz_name );
281 /* If the types differ, variable creation failed. */
282 if( (i_type & VLC_VAR_CLASS) != (p_priv->p_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->p_vars[i_new].i_type, i_type );
286 vlc_mutex_unlock( &p_priv->var_lock );
290 p_priv->p_vars[i_new].i_usage++;
291 p_priv->p_vars[i_new].i_type |= ( i_type & VLC_VAR_ISCOMMAND );
292 p_priv->p_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 );
311 i_new = Insert( p_priv->p_vars, p_priv->i_vars, psz_name );
313 if( (p_priv->i_vars & 15) == 15 )
315 p_priv->p_vars = xrealloc( p_priv->p_vars,
316 (p_priv->i_vars+17) * sizeof(variable_t) );
319 memmove( p_priv->p_vars + i_new + 1,
320 p_priv->p_vars + i_new,
321 (p_priv->i_vars - i_new) * sizeof(variable_t) );
325 p_priv->p_vars[i_new] = var;
326 vlc_mutex_unlock( &p_priv->var_lock );
332 * Destroy a vlc variable
334 * Look for the variable and destroy it if it is found. As in var_Create we
335 * do a call to memmove() but we have performance counterparts elsewhere.
337 * \param p_this The object that holds the variable
338 * \param psz_name The name of the variable
340 int __var_Destroy( vlc_object_t *p_this, const char *psz_name )
347 vlc_object_internals_t *p_priv = vlc_internals( p_this );
349 vlc_mutex_lock( &p_priv->var_lock );
351 i_var = GetUnused( p_this, psz_name );
354 vlc_mutex_unlock( &p_priv->var_lock );
358 p_var = &p_priv->p_vars[i_var];
360 if( p_var->i_usage > 1 )
363 vlc_mutex_unlock( &p_priv->var_lock );
367 /* Free value if needed */
368 p_var->ops->pf_free( &p_var->val );
370 /* Free choice list if needed */
371 if( p_var->choices.i_count )
373 for( i = 0 ; i < p_var->choices.i_count ; i++ )
375 p_var->ops->pf_free( &p_var->choices.p_values[i] );
376 free( p_var->choices_text.p_values[i].psz_string );
378 free( p_var->choices.p_values );
379 free( p_var->choices_text.p_values );
382 /* Free callbacks if needed */
383 free( p_var->p_entries );
385 free( p_var->psz_name );
386 free( p_var->psz_text );
388 memmove( p_priv->p_vars + i_var,
389 p_priv->p_vars + i_var + 1,
390 (p_priv->i_vars - i_var - 1) * sizeof(variable_t) );
392 if( (p_priv->i_vars & 15) == 0 )
394 variable_t *p_vars = realloc( p_priv->p_vars,
395 (p_priv->i_vars) * sizeof( variable_t ) );
397 p_priv->p_vars = p_vars;
402 vlc_mutex_unlock( &p_priv->var_lock );
408 * Perform an action on a variable
410 * \param p_this The object that holds the variable
411 * \param psz_name The name of the variable
412 * \param i_action The action to perform. Must be one of \ref var_action
413 * \param p_val First action parameter
414 * \param p_val2 Second action parameter
416 int __var_Change( vlc_object_t *p_this, const char *psz_name,
417 int i_action, vlc_value_t *p_val, vlc_value_t *p_val2 )
425 vlc_object_internals_t *p_priv = vlc_internals( p_this );
427 vlc_mutex_lock( &p_priv->var_lock );
429 i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
433 vlc_mutex_unlock( &p_priv->var_lock );
437 p_var = &p_priv->p_vars[i_var];
442 if( p_var->i_type & VLC_VAR_HASMIN )
444 p_var->ops->pf_free( &p_var->min );
446 p_var->i_type |= VLC_VAR_HASMIN;
448 p_var->ops->pf_dup( &p_var->min );
449 CheckValue( p_var, &p_var->val );
452 if( p_var->i_type & VLC_VAR_HASMIN )
458 if( p_var->i_type & VLC_VAR_HASMAX )
460 p_var->ops->pf_free( &p_var->max );
462 p_var->i_type |= VLC_VAR_HASMAX;
464 p_var->ops->pf_dup( &p_var->max );
465 CheckValue( p_var, &p_var->val );
468 if( p_var->i_type & VLC_VAR_HASMAX )
473 case VLC_VAR_SETSTEP:
474 if( p_var->i_type & VLC_VAR_HASSTEP )
476 p_var->ops->pf_free( &p_var->step );
478 p_var->i_type |= VLC_VAR_HASSTEP;
479 p_var->step = *p_val;
480 p_var->ops->pf_dup( &p_var->step );
481 CheckValue( p_var, &p_var->val );
483 case VLC_VAR_GETSTEP:
484 if( p_var->i_type & VLC_VAR_HASSTEP )
486 *p_val = p_var->step;
489 case VLC_VAR_ADDCHOICE:
490 i = p_var->choices.i_count;
492 INSERT_ELEM( p_var->choices.p_values, p_var->choices.i_count,
494 INSERT_ELEM( p_var->choices_text.p_values,
495 p_var->choices_text.i_count, i, *p_val );
496 p_var->ops->pf_dup( &p_var->choices.p_values[i] );
497 p_var->choices_text.p_values[i].psz_string =
498 ( p_val2 && p_val2->psz_string ) ?
499 strdup( p_val2->psz_string ) : NULL;
501 CheckValue( p_var, &p_var->val );
503 case VLC_VAR_DELCHOICE:
504 for( i = 0 ; i < p_var->choices.i_count ; i++ )
506 if( p_var->ops->pf_cmp( p_var->choices.p_values[i], *p_val ) == 0 )
512 if( i == p_var->choices.i_count )
515 vlc_mutex_unlock( &p_priv->var_lock );
519 if( p_var->i_default > i )
523 else if( p_var->i_default == i )
525 p_var->i_default = -1;
528 p_var->ops->pf_free( &p_var->choices.p_values[i] );
529 free( p_var->choices_text.p_values[i].psz_string );
530 REMOVE_ELEM( p_var->choices.p_values, p_var->choices.i_count, i );
531 REMOVE_ELEM( p_var->choices_text.p_values,
532 p_var->choices_text.i_count, i );
534 CheckValue( p_var, &p_var->val );
536 case VLC_VAR_CHOICESCOUNT:
537 p_val->i_int = p_var->choices.i_count;
539 case VLC_VAR_CLEARCHOICES:
540 for( i = 0 ; i < p_var->choices.i_count ; i++ )
542 p_var->ops->pf_free( &p_var->choices.p_values[i] );
544 for( i = 0 ; i < p_var->choices_text.i_count ; i++ )
545 free( p_var->choices_text.p_values[i].psz_string );
547 if( p_var->choices.i_count ) free( p_var->choices.p_values );
548 if( p_var->choices_text.i_count ) free( p_var->choices_text.p_values );
550 p_var->choices.i_count = 0;
551 p_var->choices.p_values = NULL;
552 p_var->choices_text.i_count = 0;
553 p_var->choices_text.p_values = NULL;
554 p_var->i_default = -1;
556 case VLC_VAR_SETDEFAULT:
557 /* FIXME: the list is sorted, dude. Use something cleverer. */
558 for( i = 0 ; i < p_var->choices.i_count ; i++ )
560 if( p_var->ops->pf_cmp( p_var->choices.p_values[i], *p_val ) == 0 )
566 if( i == p_var->choices.i_count )
572 p_var->i_default = i;
573 CheckValue( p_var, &p_var->val );
575 case VLC_VAR_SETVALUE:
576 /* Duplicate data if needed */
577 p_var->ops->pf_dup( p_val );
578 /* Backup needed stuff */
580 /* Check boundaries and list */
581 CheckValue( p_var, p_val );
582 /* Set the variable */
584 /* Free data if needed */
585 p_var->ops->pf_free( &oldval );
587 case VLC_VAR_GETCHOICES:
588 case VLC_VAR_GETLIST:
589 p_val->p_list = malloc( sizeof(vlc_list_t) );
590 if( p_val2 ) p_val2->p_list = malloc( sizeof(vlc_list_t) );
591 if( p_var->choices.i_count )
593 p_val->p_list->p_values = malloc( p_var->choices.i_count
594 * sizeof(vlc_value_t) );
595 p_val->p_list->pi_types = malloc( p_var->choices.i_count
599 p_val2->p_list->p_values =
600 malloc( p_var->choices.i_count * sizeof(vlc_value_t) );
601 p_val2->p_list->pi_types =
602 malloc( p_var->choices.i_count * sizeof(int) );
605 p_val->p_list->i_count = p_var->choices.i_count;
606 if( p_val2 ) p_val2->p_list->i_count = p_var->choices.i_count;
607 for( i = 0 ; i < p_var->choices.i_count ; i++ )
609 p_val->p_list->p_values[i] = p_var->choices.p_values[i];
610 p_val->p_list->pi_types[i] = p_var->i_type;
611 p_var->ops->pf_dup( &p_val->p_list->p_values[i] );
614 p_val2->p_list->p_values[i].psz_string =
615 p_var->choices_text.p_values[i].psz_string ?
616 strdup(p_var->choices_text.p_values[i].psz_string) : NULL;
617 p_val2->p_list->pi_types[i] = VLC_VAR_STRING;
621 case VLC_VAR_SETTEXT:
622 free( p_var->psz_text );
623 if( p_val && p_val->psz_string )
624 p_var->psz_text = strdup( p_val->psz_string );
626 p_var->psz_text = NULL;
628 case VLC_VAR_GETTEXT:
629 p_val->psz_string = p_var->psz_text ? strdup( p_var->psz_text )
632 case VLC_VAR_SETISCOMMAND:
633 p_var->i_type |= VLC_VAR_ISCOMMAND;
640 vlc_mutex_unlock( &p_priv->var_lock );
647 * Perform a Get and Set on a variable
649 * \param p_this: The object that hold the variable
650 * \param psz_name: the name of the variable
651 * \param i_action: the action to perform
652 * \param p_val: The action parameter
653 * \return vlc error codes
655 int __var_GetAndSet( vlc_object_t *p_this, const char *psz_name, int i_action,
659 int i_ret = VLC_SUCCESS;
665 vlc_object_internals_t *p_priv = vlc_internals( p_this );
667 vlc_mutex_lock( &p_priv->var_lock );
668 i_var = GetUnused( p_this, psz_name );
671 vlc_mutex_unlock( &p_priv->var_lock );
675 p_var = &p_priv->p_vars[i_var];
677 /* Duplicated data if needed */
678 //p_var->ops->pf_dup( &val );
680 /* Backup needed stuff */
683 /* depending of the action requiered */
686 case VLC_VAR_TOGGLE_BOOL:
687 assert( ( p_var->i_type & VLC_VAR_BOOL ) == VLC_VAR_BOOL );
688 p_var->val.b_bool = !p_var->val.b_bool;
690 case VLC_VAR_INTEGER_INCDEC:
691 assert( ( p_var->i_type & VLC_VAR_INTEGER ) == VLC_VAR_INTEGER );
692 p_var->val.i_int += val.i_int;
695 vlc_mutex_unlock( &p_priv->var_lock );
699 /* Check boundaries */
700 CheckValue( p_var, &p_var->val );
702 /* Deal with callbacks.*/
703 if( p_var->i_entries )
704 i_ret = TriggerCallback( p_this, &p_var, psz_name, oldval );
706 vlc_mutex_unlock( &p_priv->var_lock );
713 * Request a variable's type
715 * \return The variable type if it exists, or 0 if the
716 * variable could not be found.
719 int __var_Type( vlc_object_t *p_this, const char *psz_name )
725 vlc_object_internals_t *p_priv = vlc_internals( p_this );
727 vlc_mutex_lock( &p_priv->var_lock );
729 i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
733 vlc_mutex_unlock( &p_priv->var_lock );
737 i_type = p_priv->p_vars[i_var].i_type;
739 vlc_mutex_unlock( &p_priv->var_lock );
744 int var_SetChecked( vlc_object_t *p_this, const char *psz_name,
745 int expected_type, vlc_value_t val )
748 int i_ret = VLC_SUCCESS;
754 vlc_object_internals_t *p_priv = vlc_internals( p_this );
756 vlc_mutex_lock( &p_priv->var_lock );
758 i_var = GetUnused( p_this, psz_name );
761 vlc_mutex_unlock( &p_priv->var_lock );
765 p_var = &p_priv->p_vars[i_var];
766 assert( expected_type == 0 ||
767 (p_var->i_type & VLC_VAR_CLASS) == expected_type );
769 /* Duplicate data if needed */
770 p_var->ops->pf_dup( &val );
772 /* Backup needed stuff */
775 /* Check boundaries and list */
776 CheckValue( p_var, &val );
778 /* Set the variable */
781 /* Deal with callbacks */
782 if( p_var->i_entries )
783 i_ret = TriggerCallback( p_this, &p_var, psz_name, oldval );
785 /* Free data if needed */
786 p_var->ops->pf_free( &oldval );
788 vlc_mutex_unlock( &p_priv->var_lock );
795 * Set a variable's value
797 * \param p_this The object that hold the variable
798 * \param psz_name The name of the variable
799 * \param val the value to set
801 int __var_Set( vlc_object_t *p_this, const char *psz_name, vlc_value_t val )
803 return var_SetChecked( p_this, psz_name, 0, val );
806 int var_GetChecked( vlc_object_t *p_this, const char *psz_name,
807 int expected_type, vlc_value_t *p_val )
811 vlc_object_internals_t *p_priv = vlc_internals( p_this );
812 int i_var, err = VLC_SUCCESS;
814 vlc_mutex_lock( &p_priv->var_lock );
816 i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
819 variable_t *p_var = &p_priv->p_vars[i_var];
821 assert( expected_type == 0 ||
822 (p_var->i_type & VLC_VAR_CLASS) == expected_type );
824 /* Really get the variable */
828 /* Alert if the type is VLC_VAR_VOID */
829 if( ( p_var->i_type & VLC_VAR_TYPE ) == VLC_VAR_VOID )
830 msg_Warn( p_this, "Calling var_GetVoid on the void variable '%s' (0x%04x)", psz_name, p_var->i_type );
833 /* Duplicate value if needed */
834 p_var->ops->pf_dup( p_val );
839 vlc_mutex_unlock( &p_priv->var_lock );
844 * Get a variable's value
846 * \param p_this The object that holds the variable
847 * \param psz_name The name of the variable
848 * \param p_val Pointer to a vlc_value_t that will hold the variable's value
849 * after the function is finished
851 int __var_Get( vlc_object_t *p_this, const char *psz_name, vlc_value_t *p_val )
853 return var_GetChecked( p_this, psz_name, 0, p_val );
857 * Register a callback in a variable
859 * We store a function pointer that will be called upon variable
862 * \param p_this The object that holds the variable
863 * \param psz_name The name of the variable
864 * \param pf_callback The function pointer
865 * \param p_data A generic pointer that will be passed as the last
866 * argument to the callback function.
868 * \warning The callback function is run in the thread that calls var_Set on
869 * the variable. Use proper locking. This thread may not have much
870 * time to spare, so keep callback functions short.
872 int __var_AddCallback( vlc_object_t *p_this, const char *psz_name,
873 vlc_callback_t pf_callback, void *p_data )
877 callback_entry_t entry;
881 vlc_object_internals_t *p_priv = vlc_internals( p_this );
883 entry.pf_callback = pf_callback;
884 entry.p_data = p_data;
886 vlc_mutex_lock( &p_priv->var_lock );
888 i_var = GetUnused( p_this, psz_name );
892 msg_Warn( p_this, "Failed to add a callback to the non-existing "
893 "variable '%s'", psz_name );
895 vlc_mutex_unlock( &p_priv->var_lock );
899 p_var = &p_priv->p_vars[i_var];
901 INSERT_ELEM( p_var->p_entries,
906 vlc_mutex_unlock( &p_priv->var_lock );
912 * Remove a callback from a variable
914 * pf_callback and p_data have to be given again, because different objects
915 * might have registered the same callback function.
917 int __var_DelCallback( vlc_object_t *p_this, const char *psz_name,
918 vlc_callback_t pf_callback, void *p_data )
923 bool b_found_similar = false;
928 vlc_object_internals_t *p_priv = vlc_internals( p_this );
930 vlc_mutex_lock( &p_priv->var_lock );
932 i_var = GetUnused( p_this, psz_name );
935 vlc_mutex_unlock( &p_priv->var_lock );
939 p_var = &p_priv->p_vars[i_var];
941 for( i_entry = p_var->i_entries ; i_entry-- ; )
943 if( p_var->p_entries[i_entry].pf_callback == pf_callback
944 && p_var->p_entries[i_entry].p_data == p_data )
949 else if( p_var->p_entries[i_entry].pf_callback == pf_callback )
950 b_found_similar = true;
957 if( b_found_similar )
958 fprintf( stderr, "Calling var_DelCallback for '%s' with the same "
959 "function but not the same data.", psz_name );
962 vlc_mutex_unlock( &p_priv->var_lock );
966 REMOVE_ELEM( p_var->p_entries, p_var->i_entries, i_entry );
968 vlc_mutex_unlock( &p_priv->var_lock );
974 * Trigger callback on a variable
976 * \param p_this The object that hold the variable
977 * \param psz_name The name of the variable
979 int __var_TriggerCallback( vlc_object_t *p_this, const char *psz_name )
982 int i_ret = VLC_SUCCESS;
987 vlc_object_internals_t *p_priv = vlc_internals( p_this );
989 vlc_mutex_lock( &p_priv->var_lock );
991 i_var = GetUnused( p_this, psz_name );
994 vlc_mutex_unlock( &p_priv->var_lock );
998 p_var = &p_priv->p_vars[i_var];
1000 /* Deal with callbacks. Tell we're in a callback, release the lock,
1001 * call stored functions, retake the lock. */
1002 if( p_var->i_entries )
1003 i_ret = TriggerCallback( p_this, &p_var, psz_name, p_var->val );
1005 vlc_mutex_unlock( &p_priv->var_lock );
1009 /** Parse a stringified option
1010 * This function parse a string option and create the associated object
1012 * The option must be of the form "[no[-]]foo[=bar]" where foo is the
1013 * option name and bar is the value of the option.
1014 * \param p_obj the object in which the variable must be created
1015 * \param psz_option the option to parse
1016 * \param trusted whether the option is set by a trusted input or not
1019 void var_OptionParse( vlc_object_t *p_obj, const char *psz_option,
1022 char *psz_name, *psz_value;
1024 bool b_isno = false;
1027 val.psz_string = NULL;
1029 /* It's too much of a hassle to remove the ':' when we parse
1030 * the cmd line :) */
1031 if( psz_option[0] == ':' )
1034 if( !psz_option[0] )
1037 psz_name = strdup( psz_option );
1038 if( psz_name == NULL )
1041 psz_value = strchr( psz_name, '=' );
1042 if( psz_value != NULL )
1043 *psz_value++ = '\0';
1045 /* FIXME: :programs should be handled generically */
1046 if( !strcmp( psz_name, "programs" ) )
1047 i_type = VLC_VAR_LIST;
1049 i_type = config_GetType( p_obj, psz_name );
1051 if( !i_type && !psz_value )
1053 /* check for "no-foo" or "nofoo" */
1054 if( !strncmp( psz_name, "no-", 3 ) )
1056 memmove( psz_name, psz_name + 3, strlen(psz_name) + 1 - 3 );
1058 else if( !strncmp( psz_name, "no", 2 ) )
1060 memmove( psz_name, psz_name + 2, strlen(psz_name) + 1 - 2 );
1062 else goto cleanup; /* Option doesn't exist */
1065 i_type = config_GetType( p_obj, psz_name );
1067 if( !i_type ) goto cleanup; /* Option doesn't exist */
1069 if( ( i_type != VLC_VAR_BOOL ) &&
1070 ( !psz_value || !*psz_value ) ) goto cleanup; /* Invalid value */
1072 /* check if option is unsafe */
1075 module_config_t *p_config = config_FindConfig( p_obj, psz_name );
1076 if( !p_config || !p_config->b_safe )
1078 msg_Err( p_obj, "unsafe option \"%s\" has been ignored for "
1079 "security reasons", psz_name );
1085 /* Create the variable in the input object.
1086 * Children of the input object will be able to retreive this value
1087 * thanks to the inheritance property of the object variables. */
1088 __var_Create( p_obj, psz_name, i_type );
1093 val.b_bool = !b_isno;
1096 case VLC_VAR_INTEGER:
1097 val.i_int = strtol( psz_value, NULL, 0 );
1101 val.f_float = us_atof( psz_value );
1104 case VLC_VAR_STRING:
1105 case VLC_VAR_MODULE:
1107 case VLC_VAR_DIRECTORY:
1108 val.psz_string = psz_value;
1113 char *psz_orig, *psz_var;
1114 vlc_list_t *p_list = malloc(sizeof(vlc_list_t));
1115 val.p_list = p_list;
1116 p_list->i_count = 0;
1118 psz_var = psz_orig = strdup(psz_value);
1119 while( psz_var && *psz_var )
1121 char *psz_item = psz_var;
1123 while( *psz_var && *psz_var != ',' ) psz_var++;
1124 if( *psz_var == ',' )
1129 val2.i_int = strtol( psz_item, NULL, 0 );
1130 INSERT_ELEM( p_list->p_values, p_list->i_count,
1131 p_list->i_count, val2 );
1132 /* p_list->i_count is incremented twice by INSERT_ELEM */
1134 INSERT_ELEM( p_list->pi_types, p_list->i_count,
1135 p_list->i_count, VLC_VAR_INTEGER );
1145 __var_Set( p_obj, psz_name, val );
1147 /* If that's a list, remove all elements allocated */
1148 if( i_type == VLC_VAR_LIST )
1156 /* Following functions are local */
1158 /*****************************************************************************
1159 * GetUnused: find an unused (not in callback) variable from its name
1160 *****************************************************************************
1161 * We do i_tries tries before giving up, just in case the variable is being
1162 * modified and called from a callback.
1163 *****************************************************************************/
1164 static int GetUnused( vlc_object_t *p_this, const char *psz_name )
1168 vlc_object_internals_t *p_priv = vlc_internals( p_this );
1174 i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
1180 if( ! p_priv->p_vars[i_var].b_incallback )
1185 mutex_cleanup_push( &p_priv->var_lock );
1186 vlc_cond_wait( &p_priv->var_wait, &p_priv->var_lock );
1191 /*****************************************************************************
1192 * HashString: our cool hash function
1193 *****************************************************************************
1194 * This function is not intended to be crypto-secure, we only want it to be
1195 * fast and not suck too much. This one is pretty fast and did 0 collisions
1196 * in wenglish's dictionary.
1197 *****************************************************************************/
1198 static uint32_t HashString( const char *psz_string )
1200 uint32_t i_hash = 0;
1202 while( *psz_string )
1204 i_hash += *psz_string++;
1205 i_hash += i_hash << 10;
1206 i_hash ^= i_hash >> 8;
1212 /*****************************************************************************
1213 * Insert: find an empty slot to insert a new variable
1214 *****************************************************************************
1215 * We use a recursive inner function indexed on the hash. This function does
1216 * nothing in the rare cases where a collision may occur, see Lookup()
1217 * to see how we handle them.
1218 * XXX: does this really need to be written recursively?
1219 *****************************************************************************/
1220 static int Insert( variable_t *p_vars, int i_count, const char *psz_name )
1227 return InsertInner( p_vars, i_count, HashString( psz_name ) );
1230 static int InsertInner( variable_t *p_vars, int i_count, uint32_t i_hash )
1234 if( i_hash <= p_vars[0].i_hash )
1239 if( i_hash >= p_vars[i_count - 1].i_hash )
1244 i_middle = i_count / 2;
1246 /* We know that 0 < i_middle */
1247 if( i_hash < p_vars[i_middle].i_hash )
1249 return InsertInner( p_vars, i_middle, i_hash );
1252 /* We know that i_middle + 1 < i_count */
1253 if( i_hash > p_vars[i_middle + 1].i_hash )
1255 return i_middle + 1 + InsertInner( p_vars + i_middle + 1,
1256 i_count - i_middle - 1,
1260 return i_middle + 1;
1263 static int u32cmp( const void *key, const void *data )
1265 const variable_t *p_var = data;
1266 uint32_t hash = *(const uint32_t *)key ;
1268 if( hash > p_var->i_hash )
1270 if( hash < p_var->i_hash )
1275 /*****************************************************************************
1276 * Lookup: find an existing variable given its name
1277 *****************************************************************************
1278 * We use a recursive inner function indexed on the hash. Care is taken of
1279 * possible hash collisions.
1280 *****************************************************************************/
1281 static int Lookup( variable_t *p_vars, size_t i_count, const char *psz_name )
1286 i_hash = HashString( psz_name );
1287 p_var = bsearch( &i_hash, p_vars, i_count, sizeof( *p_var ), u32cmp );
1289 /* Hash not found */
1293 assert( i_count > 0 );
1295 /* Find the first entry with the right hash */
1296 while( (p_var > p_vars) && (i_hash == p_var[-1].i_hash) )
1299 assert( p_var->i_hash == i_hash );
1301 /* Hash collision should be very unlikely, but we cannot guarantee
1302 * it will never happen. So we do an exhaustive search amongst all
1303 * entries with the same hash. Typically, there is only one anyway. */
1304 for( variable_t *p_end = p_vars + i_count;
1305 (p_var < p_end) && (i_hash == p_var->i_hash);
1308 if( !strcmp( psz_name, p_var->psz_name ) )
1309 return p_var - p_vars;
1312 /* Hash found, but entry not found */
1316 /*****************************************************************************
1317 * CheckValue: check that a value is valid wrt. a variable
1318 *****************************************************************************
1319 * This function checks p_val's value against p_var's limitations such as
1320 * minimal and maximal value, step, in-list position, and modifies p_val if
1322 ****************************************************************************/
1323 static void CheckValue ( variable_t *p_var, vlc_value_t *p_val )
1325 /* Check that our variable is in the list */
1326 if( p_var->i_type & VLC_VAR_HASCHOICE && p_var->choices.i_count )
1330 /* This list is not sorted so go throug it (this is a small list) */
1331 for( i = p_var->choices.i_count ; i-- ; )
1333 if( p_var->ops->pf_cmp( *p_val, p_var->choices.p_values[i] ) == 0 )
1339 /* If not found, change it to anything vaguely valid */
1342 /* Free the old variable, get the new one, dup it */
1343 p_var->ops->pf_free( p_val );
1344 *p_val = p_var->choices.p_values[p_var->i_default >= 0
1345 ? p_var->i_default : 0 ];
1346 p_var->ops->pf_dup( p_val );
1350 /* Check that our variable is within the bounds */
1351 switch( p_var->i_type & VLC_VAR_TYPE )
1353 case VLC_VAR_INTEGER:
1354 if( p_var->i_type & VLC_VAR_HASSTEP && p_var->step.i_int
1355 && (p_val->i_int % p_var->step.i_int) )
1357 p_val->i_int = (p_val->i_int + (p_var->step.i_int / 2))
1358 / p_var->step.i_int * p_var->step.i_int;
1360 if( p_var->i_type & VLC_VAR_HASMIN
1361 && p_val->i_int < p_var->min.i_int )
1363 p_val->i_int = p_var->min.i_int;
1365 if( p_var->i_type & VLC_VAR_HASMAX
1366 && p_val->i_int > p_var->max.i_int )
1368 p_val->i_int = p_var->max.i_int;
1372 if( p_var->i_type & VLC_VAR_HASSTEP && p_var->step.f_float )
1374 float f_round = p_var->step.f_float * (float)(int)( 0.5 +
1375 p_val->f_float / p_var->step.f_float );
1376 if( p_val->f_float != f_round )
1378 p_val->f_float = f_round;
1381 if( p_var->i_type & VLC_VAR_HASMIN
1382 && p_val->f_float < p_var->min.f_float )
1384 p_val->f_float = p_var->min.f_float;
1386 if( p_var->i_type & VLC_VAR_HASMAX
1387 && p_val->f_float > p_var->max.f_float )
1389 p_val->f_float = p_var->max.f_float;
1398 /*****************************************************************************
1399 * InheritValue: try to inherit the value of this variable from the closest
1400 * ancestor objects or ultimately from the configuration.
1401 * The function should always be entered with the object var_lock locked.
1402 *****************************************************************************/
1403 static int InheritValue( vlc_object_t *p_this, const char *psz_name,
1404 vlc_value_t *p_val, int i_type )
1406 i_type &= VLC_VAR_CLASS;
1407 for( vlc_object_t *obj = p_this->p_parent; obj != NULL; obj = obj->p_parent )
1408 if( var_GetChecked( obj, psz_name, i_type, p_val ) == VLC_SUCCESS )
1411 /* else take value from config */
1412 switch( i_type & VLC_VAR_CLASS )
1414 case VLC_VAR_STRING:
1415 p_val->psz_string = config_GetPsz( p_this, psz_name );
1416 if( !p_val->psz_string ) p_val->psz_string = strdup("");
1419 p_val->f_float = config_GetFloat( p_this, psz_name );
1421 case VLC_VAR_INTEGER:
1422 p_val->i_int = config_GetInt( p_this, psz_name );
1425 p_val->b_bool = config_GetInt( p_this, psz_name );
1429 char *psz_orig, *psz_var;
1430 vlc_list_t *p_list = malloc(sizeof(vlc_list_t));
1431 p_val->p_list = p_list;
1432 p_list->i_count = 0;
1434 psz_var = psz_orig = config_GetPsz( p_this, psz_name );
1435 while( psz_var && *psz_var )
1437 char *psz_item = psz_var;
1439 while( *psz_var && *psz_var != ',' ) psz_var++;
1440 if( *psz_var == ',' )
1445 val.i_int = strtol( psz_item, NULL, 0 );
1446 INSERT_ELEM( p_list->p_values, p_list->i_count,
1447 p_list->i_count, val );
1448 /* p_list->i_count is incremented twice by INSERT_ELEM */
1450 INSERT_ELEM( p_list->pi_types, p_list->i_count,
1451 p_list->i_count, VLC_VAR_INTEGER );
1457 msg_Warn( p_this, "Could not inherit value for var %s "
1458 "from config. Invalid Type", psz_name );
1462 /*msg_Dbg( p_this, "Inherited value for var %s from config", psz_name );*/
1467 /**********************************************************************
1468 * Trigger the callbacks.
1469 * Tell we're in a callback, release the lock, call stored functions,
1471 **********************************************************************/
1472 static int TriggerCallback( vlc_object_t *p_this, variable_t **pp_var,
1473 const char *psz_name, vlc_value_t oldval )
1476 int i_entries = (*pp_var)->i_entries;
1477 callback_entry_t *p_entries = (*pp_var)->p_entries;
1481 vlc_object_internals_t *p_priv = vlc_internals( p_this );
1483 (*pp_var)->b_incallback = true;
1484 vlc_mutex_unlock( &p_priv->var_lock );
1486 /* The real calls */
1487 for( ; i_entries-- ; )
1489 p_entries[i_entries].pf_callback( p_this, psz_name, oldval, (*pp_var)->val,
1490 p_entries[i_entries].p_data );
1493 vlc_mutex_lock( &p_priv->var_lock );
1495 i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
1498 msg_Err( p_this, "variable %s has disappeared", psz_name );
1502 *pp_var = &p_priv->p_vars[i_var];
1503 (*pp_var)->b_incallback = false;
1504 vlc_cond_broadcast( &p_priv->var_wait );
1510 /**********************************************************************
1511 * Execute a var command on an object identified by its name
1512 **********************************************************************/
1513 int __var_Command( vlc_object_t *p_this, const char *psz_name,
1514 const char *psz_cmd, const char *psz_arg, char **psz_msg )
1516 vlc_object_t *p_obj = vlc_object_find_name( p_this->p_libvlc,
1517 psz_name, FIND_CHILD );
1523 *psz_msg = strdup( "Unknown destination object." );
1527 i_type = var_Type( p_obj, psz_cmd );
1528 if( !( i_type&VLC_VAR_ISCOMMAND ) )
1530 vlc_object_release( p_obj );
1532 *psz_msg = strdup( "Variable doesn't exist or isn't a command." );
1533 return VLC_EGENERIC;
1536 i_type &= VLC_VAR_CLASS;
1539 case VLC_VAR_INTEGER:
1540 i_ret = var_SetInteger( p_obj, psz_cmd, atoi( psz_arg ) );
1543 i_ret = var_SetFloat( p_obj, psz_cmd, us_atof( psz_arg ) );
1545 case VLC_VAR_STRING:
1546 i_ret = var_SetString( p_obj, psz_cmd, psz_arg );
1549 i_ret = var_SetBool( p_obj, psz_cmd, atoi( psz_arg ) );
1552 i_ret = VLC_EGENERIC;
1556 vlc_object_release( p_obj );
1560 if( asprintf( psz_msg, "%s on object %s returned %i (%s)",
1561 psz_cmd, psz_name, i_ret, vlc_error( i_ret ) ) == -1)
1570 * Free a list and the associated strings
1571 * @param p_val: the list variable
1572 * @param p_val2: the variable associated or NULL
1574 void var_FreeList( vlc_value_t *p_val, vlc_value_t *p_val2 )
1577 if( p_val2 && p_val2->p_list )
1579 for( int i = 0; i < p_val2->p_list->i_count; i++ )
1580 free( p_val2->p_list->p_values[i].psz_string );
1581 if( p_val2->p_list->i_count )
1583 free( p_val2->p_list->p_values );
1584 free( p_val2->p_list->pi_types );
1586 free( p_val2->p_list );