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 void WaitUnused ( vlc_object_t *, variable_t * );
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 ( vlc_object_t *, const char * );
161 static void CheckValue ( variable_t *, vlc_value_t * );
163 static int TriggerCallback( vlc_object_t *, variable_t *, const char *,
166 static void Destroy( variable_t *p_var )
168 p_var->ops->pf_free( &p_var->val );
169 if( p_var->choices.i_count )
171 for( int i = 0 ; i < p_var->choices.i_count ; i++ )
173 p_var->ops->pf_free( &p_var->choices.p_values[i] );
174 free( p_var->choices_text.p_values[i].psz_string );
176 free( p_var->choices.p_values );
177 free( p_var->choices_text.p_values );
179 free( p_var->psz_name );
180 free( p_var->psz_text );
181 free( p_var->p_entries );
186 * Initialize a vlc variable
188 * We hash the given string and insert it into the sorted list. The insertion
189 * may require slow memory copies, but think about what we gain in the log(n)
190 * lookup phase when setting/getting the variable value!
192 * \param p_this The object in which to create the variable
193 * \param psz_name The name of the variable
194 * \param i_type The variables type. Must be one of \ref var_type combined with
195 * zero or more \ref var_flags
197 int __var_Create( vlc_object_t *p_this, const char *psz_name, int i_type )
199 static vlc_list_t dummy_null_list = {0, NULL, NULL};
202 variable_t *p_var = calloc( 1, sizeof( *p_var ) );
206 p_var->i_hash = HashString( psz_name );
207 p_var->psz_name = strdup( psz_name );
208 p_var->psz_text = NULL;
210 p_var->i_type = i_type & ~VLC_VAR_DOINHERIT;
214 p_var->i_default = -1;
215 p_var->choices.i_count = 0;
216 p_var->choices.p_values = NULL;
217 p_var->choices_text.i_count = 0;
218 p_var->choices_text.p_values = NULL;
220 p_var->b_incallback = false;
221 p_var->i_entries = 0;
222 p_var->p_entries = NULL;
224 /* Always initialize the variable, even if it is a list variable; this
225 * will lead to errors if the variable is not initialized, but it will
226 * not cause crashes in the variable handling. */
227 switch( i_type & VLC_VAR_CLASS )
230 p_var->ops = &bool_ops;
231 p_var->val.b_bool = false;
233 case VLC_VAR_INTEGER:
234 p_var->ops = &int_ops;
235 p_var->val.i_int = 0;
238 p_var->ops = &string_ops;
239 p_var->val.psz_string = NULL;
242 p_var->ops = &float_ops;
243 p_var->val.f_float = 0.0;
246 p_var->ops = &time_ops;
247 p_var->val.i_time = 0;
249 case VLC_VAR_ADDRESS:
250 p_var->ops = &addr_ops;
251 p_var->val.p_address = NULL;
254 p_var->ops = &mutex_ops;
255 p_var->val.p_address = malloc( sizeof(vlc_mutex_t) );
256 vlc_mutex_init( (vlc_mutex_t*)p_var->val.p_address );
259 p_var->ops = &list_ops;
260 p_var->val.p_list = &dummy_null_list;
263 p_var->ops = &void_ops;
267 if( i_type & VLC_VAR_DOINHERIT )
269 if( var_Inherit( p_this, psz_name, i_type, &p_var->val ) )
270 msg_Err( p_this, "cannot inherit value for %s", psz_name );
271 else if( i_type & VLC_VAR_HASCHOICE )
273 /* We must add the inherited value to our choice list */
274 p_var->i_default = 0;
276 INSERT_ELEM( p_var->choices.p_values, p_var->choices.i_count,
278 INSERT_ELEM( p_var->choices_text.p_values,
279 p_var->choices_text.i_count, 0, p_var->val );
280 p_var->ops->pf_dup( &p_var->choices.p_values[0] );
281 p_var->choices_text.p_values[0].psz_string = NULL;
285 vlc_object_internals_t *p_priv = vlc_internals( p_this );
288 vlc_mutex_lock( &p_priv->var_lock );
290 /* FIXME: if the variable already exists, we don't duplicate it. But we
291 * duplicate the lookups. It's not that serious, but if anyone finds some
292 * time to rework Insert() so that only one lookup has to be done, feel
294 i_new = Lookup( p_this, psz_name );
298 /* If the types differ, variable creation failed. */
299 if( (i_type & VLC_VAR_CLASS) != (p_priv->pp_vars[i_new]->i_type & VLC_VAR_CLASS) )
301 msg_Err( p_this, "Variable '%s' (0x%04x) already exist but with a different type (0x%04x)",
302 psz_name, p_priv->pp_vars[i_new]->i_type, i_type );
303 vlc_mutex_unlock( &p_priv->var_lock );
307 p_priv->pp_vars[i_new]->i_usage++;
308 p_priv->pp_vars[i_new]->i_type |= ( i_type & VLC_VAR_ISCOMMAND );
309 p_priv->pp_vars[i_new]->i_type |= ( i_type & VLC_VAR_HASCHOICE );
310 vlc_mutex_unlock( &p_priv->var_lock );
312 /* We did not need to create a new variable, free everything... */
317 i_new = Insert( p_priv->pp_vars, p_priv->i_vars, psz_name );
319 if( (p_priv->i_vars & 15) == 0 )
320 p_priv->pp_vars = xrealloc( p_priv->pp_vars,
321 (p_priv->i_vars+16) * sizeof(variable_t *) );
323 memmove( p_priv->pp_vars + i_new + 1,
324 p_priv->pp_vars + i_new,
325 (p_priv->i_vars - i_new) * sizeof(variable_t *) );
329 p_priv->pp_vars[i_new] = p_var;
330 vlc_mutex_unlock( &p_priv->var_lock );
336 * Destroy a vlc variable
338 * Look for the variable and destroy it if it is found. As in var_Create we
339 * do a call to memmove() but we have performance counterparts elsewhere.
341 * \param p_this The object that holds the variable
342 * \param psz_name The name of the variable
344 int __var_Destroy( vlc_object_t *p_this, const char *psz_name )
351 vlc_object_internals_t *p_priv = vlc_internals( p_this );
353 vlc_mutex_lock( &p_priv->var_lock );
355 i_var = Lookup( p_this, psz_name );
358 vlc_mutex_unlock( &p_priv->var_lock );
362 p_var = p_priv->pp_vars[i_var];
363 WaitUnused( p_this, p_var );
365 if( p_var->i_usage > 1 )
368 vlc_mutex_unlock( &p_priv->var_lock );
373 memmove( p_priv->pp_vars + i_var,
374 p_priv->pp_vars + i_var + 1,
375 (p_priv->i_vars - i_var) * sizeof(variable_t *) );
377 if( p_priv->i_vars == 0 )
379 free( p_priv->pp_vars );
380 p_priv->pp_vars = NULL;
383 if( (p_priv->i_vars & 15) == 0 )
385 variable_t **pp_vars = realloc( p_priv->pp_vars,
386 (p_priv->i_vars) * sizeof(variable_t *) );
387 if( pp_vars != NULL )
388 p_priv->pp_vars = pp_vars;
390 vlc_mutex_unlock( &p_priv->var_lock );
397 * Perform an action on a variable
399 * \param p_this The object that holds the variable
400 * \param psz_name The name of the variable
401 * \param i_action The action to perform. Must be one of \ref var_action
402 * \param p_val First action parameter
403 * \param p_val2 Second action parameter
405 int __var_Change( vlc_object_t *p_this, const char *psz_name,
406 int i_action, vlc_value_t *p_val, vlc_value_t *p_val2 )
414 vlc_object_internals_t *p_priv = vlc_internals( p_this );
416 vlc_mutex_lock( &p_priv->var_lock );
418 i_var = Lookup( p_this, psz_name );
422 vlc_mutex_unlock( &p_priv->var_lock );
426 p_var = p_priv->pp_vars[i_var];
431 if( p_var->i_type & VLC_VAR_HASMIN )
433 p_var->ops->pf_free( &p_var->min );
435 p_var->i_type |= VLC_VAR_HASMIN;
437 p_var->ops->pf_dup( &p_var->min );
438 CheckValue( p_var, &p_var->val );
441 if( p_var->i_type & VLC_VAR_HASMIN )
447 if( p_var->i_type & VLC_VAR_HASMAX )
449 p_var->ops->pf_free( &p_var->max );
451 p_var->i_type |= VLC_VAR_HASMAX;
453 p_var->ops->pf_dup( &p_var->max );
454 CheckValue( p_var, &p_var->val );
457 if( p_var->i_type & VLC_VAR_HASMAX )
462 case VLC_VAR_SETSTEP:
463 if( p_var->i_type & VLC_VAR_HASSTEP )
465 p_var->ops->pf_free( &p_var->step );
467 p_var->i_type |= VLC_VAR_HASSTEP;
468 p_var->step = *p_val;
469 p_var->ops->pf_dup( &p_var->step );
470 CheckValue( p_var, &p_var->val );
472 case VLC_VAR_GETSTEP:
473 if( p_var->i_type & VLC_VAR_HASSTEP )
475 *p_val = p_var->step;
478 case VLC_VAR_ADDCHOICE:
479 i = p_var->choices.i_count;
481 INSERT_ELEM( p_var->choices.p_values, p_var->choices.i_count,
483 INSERT_ELEM( p_var->choices_text.p_values,
484 p_var->choices_text.i_count, i, *p_val );
485 p_var->ops->pf_dup( &p_var->choices.p_values[i] );
486 p_var->choices_text.p_values[i].psz_string =
487 ( p_val2 && p_val2->psz_string ) ?
488 strdup( p_val2->psz_string ) : NULL;
490 CheckValue( p_var, &p_var->val );
492 case VLC_VAR_DELCHOICE:
493 for( i = 0 ; i < p_var->choices.i_count ; i++ )
495 if( p_var->ops->pf_cmp( p_var->choices.p_values[i], *p_val ) == 0 )
501 if( i == p_var->choices.i_count )
504 vlc_mutex_unlock( &p_priv->var_lock );
508 if( p_var->i_default > i )
512 else if( p_var->i_default == i )
514 p_var->i_default = -1;
517 p_var->ops->pf_free( &p_var->choices.p_values[i] );
518 free( p_var->choices_text.p_values[i].psz_string );
519 REMOVE_ELEM( p_var->choices.p_values, p_var->choices.i_count, i );
520 REMOVE_ELEM( p_var->choices_text.p_values,
521 p_var->choices_text.i_count, i );
523 CheckValue( p_var, &p_var->val );
525 case VLC_VAR_CHOICESCOUNT:
526 p_val->i_int = p_var->choices.i_count;
528 case VLC_VAR_CLEARCHOICES:
529 for( i = 0 ; i < p_var->choices.i_count ; i++ )
531 p_var->ops->pf_free( &p_var->choices.p_values[i] );
533 for( i = 0 ; i < p_var->choices_text.i_count ; i++ )
534 free( p_var->choices_text.p_values[i].psz_string );
536 if( p_var->choices.i_count ) free( p_var->choices.p_values );
537 if( p_var->choices_text.i_count ) free( p_var->choices_text.p_values );
539 p_var->choices.i_count = 0;
540 p_var->choices.p_values = NULL;
541 p_var->choices_text.i_count = 0;
542 p_var->choices_text.p_values = NULL;
543 p_var->i_default = -1;
545 case VLC_VAR_SETDEFAULT:
546 /* FIXME: the list is sorted, dude. Use something cleverer. */
547 for( i = 0 ; i < p_var->choices.i_count ; i++ )
549 if( p_var->ops->pf_cmp( p_var->choices.p_values[i], *p_val ) == 0 )
555 if( i == p_var->choices.i_count )
561 p_var->i_default = i;
562 CheckValue( p_var, &p_var->val );
564 case VLC_VAR_SETVALUE:
565 /* Duplicate data if needed */
566 p_var->ops->pf_dup( p_val );
567 /* Backup needed stuff */
569 /* Check boundaries and list */
570 CheckValue( p_var, p_val );
571 /* Set the variable */
573 /* Free data if needed */
574 p_var->ops->pf_free( &oldval );
576 case VLC_VAR_GETCHOICES:
577 case VLC_VAR_GETLIST:
578 p_val->p_list = malloc( sizeof(vlc_list_t) );
579 if( p_val2 ) p_val2->p_list = malloc( sizeof(vlc_list_t) );
580 if( p_var->choices.i_count )
582 p_val->p_list->p_values = malloc( p_var->choices.i_count
583 * sizeof(vlc_value_t) );
584 p_val->p_list->pi_types = malloc( p_var->choices.i_count
588 p_val2->p_list->p_values =
589 malloc( p_var->choices.i_count * sizeof(vlc_value_t) );
590 p_val2->p_list->pi_types =
591 malloc( p_var->choices.i_count * sizeof(int) );
594 p_val->p_list->i_count = p_var->choices.i_count;
595 if( p_val2 ) p_val2->p_list->i_count = p_var->choices.i_count;
596 for( i = 0 ; i < p_var->choices.i_count ; i++ )
598 p_val->p_list->p_values[i] = p_var->choices.p_values[i];
599 p_val->p_list->pi_types[i] = p_var->i_type;
600 p_var->ops->pf_dup( &p_val->p_list->p_values[i] );
603 p_val2->p_list->p_values[i].psz_string =
604 p_var->choices_text.p_values[i].psz_string ?
605 strdup(p_var->choices_text.p_values[i].psz_string) : NULL;
606 p_val2->p_list->pi_types[i] = VLC_VAR_STRING;
610 case VLC_VAR_SETTEXT:
611 free( p_var->psz_text );
612 if( p_val && p_val->psz_string )
613 p_var->psz_text = strdup( p_val->psz_string );
615 p_var->psz_text = NULL;
617 case VLC_VAR_GETTEXT:
618 p_val->psz_string = p_var->psz_text ? strdup( p_var->psz_text )
621 case VLC_VAR_SETISCOMMAND:
622 p_var->i_type |= VLC_VAR_ISCOMMAND;
629 vlc_mutex_unlock( &p_priv->var_lock );
636 * Perform a Get and Set on a variable
638 * \param p_this: The object that hold the variable
639 * \param psz_name: the name of the variable
640 * \param i_action: the action to perform
641 * \param p_val: The action parameter
642 * \return vlc error codes
644 int __var_GetAndSet( vlc_object_t *p_this, const char *psz_name, int i_action,
654 vlc_object_internals_t *p_priv = vlc_internals( p_this );
656 vlc_mutex_lock( &p_priv->var_lock );
657 i_var = Lookup( p_this, psz_name );
660 vlc_mutex_unlock( &p_priv->var_lock );
664 p_var = p_priv->pp_vars[i_var];
665 WaitUnused( p_this, p_var );
667 /* Duplicated data if needed */
668 //p_var->ops->pf_dup( &val );
670 /* Backup needed stuff */
673 /* depending of the action requiered */
676 case VLC_VAR_TOGGLE_BOOL:
677 assert( ( p_var->i_type & VLC_VAR_BOOL ) == VLC_VAR_BOOL );
678 p_var->val.b_bool = !p_var->val.b_bool;
680 case VLC_VAR_INTEGER_INCDEC:
681 assert( ( p_var->i_type & VLC_VAR_INTEGER ) == VLC_VAR_INTEGER );
682 p_var->val.i_int += val.i_int;
685 vlc_mutex_unlock( &p_priv->var_lock );
689 /* Check boundaries */
690 CheckValue( p_var, &p_var->val );
692 /* Deal with callbacks.*/
693 i_ret = TriggerCallback( p_this, p_var, psz_name, oldval );
695 vlc_mutex_unlock( &p_priv->var_lock );
702 * Request a variable's type
704 * \return The variable type if it exists, or 0 if the
705 * variable could not be found.
708 int __var_Type( vlc_object_t *p_this, const char *psz_name )
714 vlc_object_internals_t *p_priv = vlc_internals( p_this );
716 vlc_mutex_lock( &p_priv->var_lock );
718 i_var = Lookup( p_this, psz_name );
722 vlc_mutex_unlock( &p_priv->var_lock );
726 i_type = p_priv->pp_vars[i_var]->i_type;
728 vlc_mutex_unlock( &p_priv->var_lock );
733 int var_SetChecked( vlc_object_t *p_this, const char *psz_name,
734 int expected_type, vlc_value_t val )
737 int i_ret = VLC_SUCCESS;
743 vlc_object_internals_t *p_priv = vlc_internals( p_this );
745 vlc_mutex_lock( &p_priv->var_lock );
747 i_var = Lookup( p_this, psz_name );
750 vlc_mutex_unlock( &p_priv->var_lock );
754 p_var = p_priv->pp_vars[i_var];
755 assert( expected_type == 0 ||
756 (p_var->i_type & VLC_VAR_CLASS) == expected_type );
758 WaitUnused( p_this, p_var );
760 /* Duplicate data if needed */
761 p_var->ops->pf_dup( &val );
763 /* Backup needed stuff */
766 /* Check boundaries and list */
767 CheckValue( p_var, &val );
769 /* Set the variable */
772 /* Deal with callbacks */
773 i_ret = TriggerCallback( p_this, p_var, psz_name, oldval );
775 /* Free data if needed */
776 p_var->ops->pf_free( &oldval );
778 vlc_mutex_unlock( &p_priv->var_lock );
785 * Set a variable's value
787 * \param p_this The object that hold the variable
788 * \param psz_name The name of the variable
789 * \param val the value to set
791 int __var_Set( vlc_object_t *p_this, const char *psz_name, vlc_value_t val )
793 return var_SetChecked( p_this, psz_name, 0, val );
796 int var_GetChecked( vlc_object_t *p_this, const char *psz_name,
797 int expected_type, vlc_value_t *p_val )
801 vlc_object_internals_t *p_priv = vlc_internals( p_this );
802 int i_var, err = VLC_SUCCESS;
804 vlc_mutex_lock( &p_priv->var_lock );
806 i_var = Lookup( p_this, psz_name );
809 variable_t *p_var = p_priv->pp_vars[i_var];
811 assert( expected_type == 0 ||
812 (p_var->i_type & VLC_VAR_CLASS) == expected_type );
814 /* Really get the variable */
818 /* Alert if the type is VLC_VAR_VOID */
819 if( ( p_var->i_type & VLC_VAR_TYPE ) == VLC_VAR_VOID )
820 msg_Warn( p_this, "Calling var_GetVoid on the void variable '%s' (0x%04x)", psz_name, p_var->i_type );
823 /* Duplicate value if needed */
824 p_var->ops->pf_dup( p_val );
829 vlc_mutex_unlock( &p_priv->var_lock );
834 * Get a variable's value
836 * \param p_this The object that holds the variable
837 * \param psz_name The name of the variable
838 * \param p_val Pointer to a vlc_value_t that will hold the variable's value
839 * after the function is finished
841 int __var_Get( vlc_object_t *p_this, const char *psz_name, vlc_value_t *p_val )
843 return var_GetChecked( p_this, psz_name, 0, p_val );
847 * Register a callback in a variable
849 * We store a function pointer that will be called upon variable
852 * \param p_this The object that holds the variable
853 * \param psz_name The name of the variable
854 * \param pf_callback The function pointer
855 * \param p_data A generic pointer that will be passed as the last
856 * argument to the callback function.
858 * \warning The callback function is run in the thread that calls var_Set on
859 * the variable. Use proper locking. This thread may not have much
860 * time to spare, so keep callback functions short.
862 int __var_AddCallback( vlc_object_t *p_this, const char *psz_name,
863 vlc_callback_t pf_callback, void *p_data )
867 callback_entry_t entry;
871 vlc_object_internals_t *p_priv = vlc_internals( p_this );
873 entry.pf_callback = pf_callback;
874 entry.p_data = p_data;
876 vlc_mutex_lock( &p_priv->var_lock );
878 i_var = Lookup( p_this, psz_name );
882 msg_Warn( p_this, "Failed to add a callback to the non-existing "
883 "variable '%s'", psz_name );
885 vlc_mutex_unlock( &p_priv->var_lock );
889 p_var = p_priv->pp_vars[i_var];
891 WaitUnused( p_this, p_var );
892 INSERT_ELEM( p_var->p_entries,
897 vlc_mutex_unlock( &p_priv->var_lock );
903 * Remove a callback from a variable
905 * pf_callback and p_data have to be given again, because different objects
906 * might have registered the same callback function.
908 int __var_DelCallback( vlc_object_t *p_this, const char *psz_name,
909 vlc_callback_t pf_callback, void *p_data )
914 bool b_found_similar = false;
919 vlc_object_internals_t *p_priv = vlc_internals( p_this );
921 vlc_mutex_lock( &p_priv->var_lock );
923 i_var = Lookup( p_this, psz_name );
926 vlc_mutex_unlock( &p_priv->var_lock );
930 p_var = p_priv->pp_vars[i_var];
931 WaitUnused( p_this, p_var );
933 for( i_entry = p_var->i_entries ; i_entry-- ; )
935 if( p_var->p_entries[i_entry].pf_callback == pf_callback
936 && p_var->p_entries[i_entry].p_data == p_data )
941 else if( p_var->p_entries[i_entry].pf_callback == pf_callback )
942 b_found_similar = true;
949 if( b_found_similar )
950 fprintf( stderr, "Calling var_DelCallback for '%s' with the same "
951 "function but not the same data.", psz_name );
954 vlc_mutex_unlock( &p_priv->var_lock );
958 REMOVE_ELEM( p_var->p_entries, p_var->i_entries, i_entry );
960 vlc_mutex_unlock( &p_priv->var_lock );
966 * Trigger callback on a variable
968 * \param p_this The object that hold the variable
969 * \param psz_name The name of the variable
971 int __var_TriggerCallback( vlc_object_t *p_this, const char *psz_name )
979 vlc_object_internals_t *p_priv = vlc_internals( p_this );
981 vlc_mutex_lock( &p_priv->var_lock );
983 i_var = Lookup( p_this, psz_name );
986 vlc_mutex_unlock( &p_priv->var_lock );
990 p_var = p_priv->pp_vars[i_var];
991 WaitUnused( p_this, p_var );
993 /* Deal with callbacks. Tell we're in a callback, release the lock,
994 * call stored functions, retake the lock. */
995 i_ret = TriggerCallback( p_this, p_var, psz_name, p_var->val );
997 vlc_mutex_unlock( &p_priv->var_lock );
1001 /** Parse a stringified option
1002 * This function parse a string option and create the associated object
1004 * The option must be of the form "[no[-]]foo[=bar]" where foo is the
1005 * option name and bar is the value of the option.
1006 * \param p_obj the object in which the variable must be created
1007 * \param psz_option the option to parse
1008 * \param trusted whether the option is set by a trusted input or not
1011 void var_OptionParse( vlc_object_t *p_obj, const char *psz_option,
1014 char *psz_name, *psz_value;
1016 bool b_isno = false;
1019 val.psz_string = NULL;
1021 /* It's too much of a hassle to remove the ':' when we parse
1022 * the cmd line :) */
1023 if( psz_option[0] == ':' )
1026 if( !psz_option[0] )
1029 psz_name = strdup( psz_option );
1030 if( psz_name == NULL )
1033 psz_value = strchr( psz_name, '=' );
1034 if( psz_value != NULL )
1035 *psz_value++ = '\0';
1037 /* FIXME: :programs should be handled generically */
1038 if( !strcmp( psz_name, "programs" ) )
1039 i_type = VLC_VAR_LIST;
1041 i_type = config_GetType( p_obj, psz_name );
1043 if( !i_type && !psz_value )
1045 /* check for "no-foo" or "nofoo" */
1046 if( !strncmp( psz_name, "no-", 3 ) )
1048 memmove( psz_name, psz_name + 3, strlen(psz_name) + 1 - 3 );
1050 else if( !strncmp( psz_name, "no", 2 ) )
1052 memmove( psz_name, psz_name + 2, strlen(psz_name) + 1 - 2 );
1054 else goto cleanup; /* Option doesn't exist */
1057 i_type = config_GetType( p_obj, psz_name );
1059 if( !i_type ) goto cleanup; /* Option doesn't exist */
1061 if( ( i_type != VLC_VAR_BOOL ) &&
1062 ( !psz_value || !*psz_value ) ) goto cleanup; /* Invalid value */
1064 /* check if option is unsafe */
1067 module_config_t *p_config = config_FindConfig( p_obj, psz_name );
1068 if( !p_config || !p_config->b_safe )
1070 msg_Err( p_obj, "unsafe option \"%s\" has been ignored for "
1071 "security reasons", psz_name );
1077 /* Create the variable in the input object.
1078 * Children of the input object will be able to retreive this value
1079 * thanks to the inheritance property of the object variables. */
1080 __var_Create( p_obj, psz_name, i_type );
1085 val.b_bool = !b_isno;
1088 case VLC_VAR_INTEGER:
1089 val.i_int = strtol( psz_value, NULL, 0 );
1093 val.f_float = us_atof( psz_value );
1096 case VLC_VAR_STRING:
1097 case VLC_VAR_MODULE:
1099 case VLC_VAR_DIRECTORY:
1100 val.psz_string = psz_value;
1105 char *psz_orig, *psz_var;
1106 vlc_list_t *p_list = malloc(sizeof(vlc_list_t));
1107 val.p_list = p_list;
1108 p_list->i_count = 0;
1110 psz_var = psz_orig = strdup(psz_value);
1111 while( psz_var && *psz_var )
1113 char *psz_item = psz_var;
1115 while( *psz_var && *psz_var != ',' ) psz_var++;
1116 if( *psz_var == ',' )
1121 val2.i_int = strtol( psz_item, NULL, 0 );
1122 INSERT_ELEM( p_list->p_values, p_list->i_count,
1123 p_list->i_count, val2 );
1124 /* p_list->i_count is incremented twice by INSERT_ELEM */
1126 INSERT_ELEM( p_list->pi_types, p_list->i_count,
1127 p_list->i_count, VLC_VAR_INTEGER );
1137 __var_Set( p_obj, psz_name, val );
1139 /* If that's a list, remove all elements allocated */
1140 if( i_type == VLC_VAR_LIST )
1148 /* Following functions are local */
1151 * Waits until the variable is inactive (i.e. not executing a callback)
1153 static void WaitUnused( vlc_object_t *p_this, variable_t *p_var )
1155 vlc_object_internals_t *p_priv = vlc_internals( p_this );
1157 mutex_cleanup_push( &p_priv->var_lock );
1158 while( p_var->b_incallback )
1159 vlc_cond_wait( &p_priv->var_wait, &p_priv->var_lock );
1163 /*****************************************************************************
1164 * HashString: our cool hash function
1165 *****************************************************************************
1166 * This function is not intended to be crypto-secure, we only want it to be
1167 * fast and not suck too much. This one is pretty fast and did 0 collisions
1168 * in wenglish's dictionary.
1169 *****************************************************************************/
1170 static uint32_t HashString( const char *psz_string )
1172 uint32_t i_hash = 0;
1174 while( *psz_string )
1176 i_hash += *psz_string++;
1177 i_hash += i_hash << 10;
1178 i_hash ^= i_hash >> 8;
1184 /*****************************************************************************
1185 * Insert: find an empty slot to insert a new variable
1186 *****************************************************************************
1187 * We use a recursive inner function indexed on the hash. This function does
1188 * nothing in the rare cases where a collision may occur, see Lookup()
1189 * to see how we handle them.
1190 * XXX: does this really need to be written recursively?
1191 *****************************************************************************/
1192 static int Insert( variable_t **pp_vars, int i_count, const char *psz_name )
1199 return InsertInner( pp_vars, i_count, HashString( psz_name ) );
1202 static int InsertInner( variable_t **pp_vars, int i_count, uint32_t i_hash )
1206 if( i_hash <= pp_vars[0]->i_hash )
1211 if( i_hash >= pp_vars[i_count - 1]->i_hash )
1216 i_middle = i_count / 2;
1218 /* We know that 0 < i_middle */
1219 if( i_hash < pp_vars[i_middle]->i_hash )
1221 return InsertInner( pp_vars, i_middle, i_hash );
1224 /* We know that i_middle + 1 < i_count */
1225 if( i_hash > pp_vars[i_middle + 1]->i_hash )
1227 return i_middle + 1 + InsertInner( pp_vars + i_middle + 1,
1228 i_count - i_middle - 1,
1232 return i_middle + 1;
1235 static int u32cmp( const void *key, const void *data )
1237 const variable_t *const *pp_var = data;
1238 uint32_t hash = *(const uint32_t *)key ;
1240 if( hash > (*pp_var)->i_hash )
1242 if( hash < (*pp_var)->i_hash )
1247 /*****************************************************************************
1248 * Lookup: find an existing variable given its name
1249 *****************************************************************************
1250 * We use a recursive inner function indexed on the hash. Care is taken of
1251 * possible hash collisions.
1252 *****************************************************************************/
1253 static int Lookup( vlc_object_t *obj, const char *psz_name )
1255 vlc_object_internals_t *priv = vlc_internals( obj );
1256 variable_t *const *pp_vars = priv->pp_vars;
1257 size_t i_vars = priv->i_vars;
1258 variable_t *const *pp_var;
1259 uint32_t i_hash = HashString( psz_name );
1261 pp_var = bsearch( &i_hash, pp_vars, i_vars, sizeof( *pp_var ), u32cmp );
1263 /* Hash not found */
1264 if( pp_var == NULL )
1267 assert( i_vars > 0 );
1269 /* Find the first entry with the right hash */
1270 while( (pp_var > pp_vars) && (i_hash == pp_var[-1]->i_hash) )
1273 assert( (*pp_var)->i_hash == i_hash );
1275 /* Hash collision should be very unlikely, but we cannot guarantee
1276 * it will never happen. So we do an exhaustive search amongst all
1277 * entries with the same hash. Typically, there is only one anyway. */
1278 for( variable_t *const *p_end = pp_vars + i_vars;
1279 (pp_var < p_end) && (i_hash == (*pp_var)->i_hash);
1282 if( !strcmp( psz_name, (*pp_var)->psz_name ) )
1283 return pp_var - pp_vars;
1286 /* Hash found, but entry not found */
1290 /*****************************************************************************
1291 * CheckValue: check that a value is valid wrt. a variable
1292 *****************************************************************************
1293 * This function checks p_val's value against p_var's limitations such as
1294 * minimal and maximal value, step, in-list position, and modifies p_val if
1296 ****************************************************************************/
1297 static void CheckValue ( variable_t *p_var, vlc_value_t *p_val )
1299 /* Check that our variable is in the list */
1300 if( p_var->i_type & VLC_VAR_HASCHOICE && p_var->choices.i_count )
1304 /* This list is not sorted so go throug it (this is a small list) */
1305 for( i = p_var->choices.i_count ; i-- ; )
1307 if( p_var->ops->pf_cmp( *p_val, p_var->choices.p_values[i] ) == 0 )
1313 /* If not found, change it to anything vaguely valid */
1316 /* Free the old variable, get the new one, dup it */
1317 p_var->ops->pf_free( p_val );
1318 *p_val = p_var->choices.p_values[p_var->i_default >= 0
1319 ? p_var->i_default : 0 ];
1320 p_var->ops->pf_dup( p_val );
1324 /* Check that our variable is within the bounds */
1325 switch( p_var->i_type & VLC_VAR_TYPE )
1327 case VLC_VAR_INTEGER:
1328 if( p_var->i_type & VLC_VAR_HASSTEP && p_var->step.i_int
1329 && (p_val->i_int % p_var->step.i_int) )
1331 p_val->i_int = (p_val->i_int + (p_var->step.i_int / 2))
1332 / p_var->step.i_int * p_var->step.i_int;
1334 if( p_var->i_type & VLC_VAR_HASMIN
1335 && p_val->i_int < p_var->min.i_int )
1337 p_val->i_int = p_var->min.i_int;
1339 if( p_var->i_type & VLC_VAR_HASMAX
1340 && p_val->i_int > p_var->max.i_int )
1342 p_val->i_int = p_var->max.i_int;
1346 if( p_var->i_type & VLC_VAR_HASSTEP && p_var->step.f_float )
1348 float f_round = p_var->step.f_float * (float)(int)( 0.5 +
1349 p_val->f_float / p_var->step.f_float );
1350 if( p_val->f_float != f_round )
1352 p_val->f_float = f_round;
1355 if( p_var->i_type & VLC_VAR_HASMIN
1356 && p_val->f_float < p_var->min.f_float )
1358 p_val->f_float = p_var->min.f_float;
1360 if( p_var->i_type & VLC_VAR_HASMAX
1361 && p_val->f_float > p_var->max.f_float )
1363 p_val->f_float = p_var->max.f_float;
1373 * Finds the value of a variable. If the specified object does not hold a
1374 * variable with the specified name, try the parent object, and iterate until
1375 * the top of the tree. If no match is found, the value is read from the
1378 int var_Inherit( vlc_object_t *p_this, const char *psz_name, int i_type,
1379 vlc_value_t *p_val )
1381 i_type &= VLC_VAR_CLASS;
1382 for( vlc_object_t *obj = p_this; obj != NULL; obj = obj->p_parent )
1383 if( var_GetChecked( obj, psz_name, i_type, p_val ) == VLC_SUCCESS )
1386 /* else take value from config */
1387 switch( i_type & VLC_VAR_CLASS )
1389 case VLC_VAR_STRING:
1390 p_val->psz_string = config_GetPsz( p_this, psz_name );
1391 if( !p_val->psz_string ) p_val->psz_string = strdup("");
1394 p_val->f_float = config_GetFloat( p_this, psz_name );
1396 case VLC_VAR_INTEGER:
1397 p_val->i_int = config_GetInt( p_this, psz_name );
1400 p_val->b_bool = config_GetInt( p_this, psz_name );
1404 char *psz_orig, *psz_var;
1405 vlc_list_t *p_list = malloc(sizeof(vlc_list_t));
1406 p_val->p_list = p_list;
1407 p_list->i_count = 0;
1409 psz_var = psz_orig = config_GetPsz( p_this, psz_name );
1410 while( psz_var && *psz_var )
1412 char *psz_item = psz_var;
1414 while( *psz_var && *psz_var != ',' ) psz_var++;
1415 if( *psz_var == ',' )
1420 val.i_int = strtol( psz_item, NULL, 0 );
1421 INSERT_ELEM( p_list->p_values, p_list->i_count,
1422 p_list->i_count, val );
1423 /* p_list->i_count is incremented twice by INSERT_ELEM */
1425 INSERT_ELEM( p_list->pi_types, p_list->i_count,
1426 p_list->i_count, VLC_VAR_INTEGER );
1432 msg_Warn( p_this, "Could not inherit value for var %s "
1433 "from config. Invalid Type", psz_name );
1436 /*msg_Dbg( p_this, "Inherited value for var %s from config", psz_name );*/
1441 /**********************************************************************
1442 * Trigger the callbacks.
1443 * Tell we're in a callback, release the lock, call stored functions,
1445 **********************************************************************/
1446 static int TriggerCallback( vlc_object_t *p_this, variable_t *p_var,
1447 const char *psz_name, vlc_value_t oldval )
1451 int i_entries = p_var->i_entries;
1452 if( i_entries == 0 )
1455 callback_entry_t *p_entries = p_var->p_entries;
1456 vlc_object_internals_t *p_priv = vlc_internals( p_this );
1458 assert( !p_var->b_incallback );
1459 p_var->b_incallback = true;
1460 vlc_mutex_unlock( &p_priv->var_lock );
1462 /* The real calls */
1463 for( ; i_entries-- ; )
1465 p_entries[i_entries].pf_callback( p_this, psz_name, oldval, p_var->val,
1466 p_entries[i_entries].p_data );
1469 vlc_mutex_lock( &p_priv->var_lock );
1470 p_var->b_incallback = false;
1471 vlc_cond_broadcast( &p_priv->var_wait );
1477 /**********************************************************************
1478 * Execute a var command on an object identified by its name
1479 **********************************************************************/
1480 int __var_Command( vlc_object_t *p_this, const char *psz_name,
1481 const char *psz_cmd, const char *psz_arg, char **psz_msg )
1483 vlc_object_t *p_obj = vlc_object_find_name( p_this->p_libvlc,
1484 psz_name, FIND_CHILD );
1490 *psz_msg = strdup( "Unknown destination object." );
1494 i_type = var_Type( p_obj, psz_cmd );
1495 if( !( i_type&VLC_VAR_ISCOMMAND ) )
1497 vlc_object_release( p_obj );
1499 *psz_msg = strdup( "Variable doesn't exist or isn't a command." );
1500 return VLC_EGENERIC;
1503 i_type &= VLC_VAR_CLASS;
1506 case VLC_VAR_INTEGER:
1507 i_ret = var_SetInteger( p_obj, psz_cmd, atoi( psz_arg ) );
1510 i_ret = var_SetFloat( p_obj, psz_cmd, us_atof( psz_arg ) );
1512 case VLC_VAR_STRING:
1513 i_ret = var_SetString( p_obj, psz_cmd, psz_arg );
1516 i_ret = var_SetBool( p_obj, psz_cmd, atoi( psz_arg ) );
1519 i_ret = VLC_EGENERIC;
1523 vlc_object_release( p_obj );
1527 if( asprintf( psz_msg, "%s on object %s returned %i (%s)",
1528 psz_cmd, psz_name, i_ret, vlc_error( i_ret ) ) == -1)
1537 * Free a list and the associated strings
1538 * @param p_val: the list variable
1539 * @param p_val2: the variable associated or NULL
1541 void var_FreeList( vlc_value_t *p_val, vlc_value_t *p_val2 )
1544 if( p_val2 && p_val2->p_list )
1546 for( int i = 0; i < p_val2->p_list->i_count; i++ )
1547 free( p_val2->p_list->p_values[i].psz_string );
1548 if( p_val2->p_list->i_count )
1550 free( p_val2->p_list->p_values );
1551 free( p_val2->p_list->pi_types );
1553 free( p_val2->p_list );