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 "variables.h"
38 /*****************************************************************************
40 *****************************************************************************/
41 struct callback_entry_t
43 vlc_callback_t pf_callback;
47 /*****************************************************************************
48 * Local comparison functions, returns 0 if v == w, < 0 if v < w, > 0 if v > w
49 *****************************************************************************/
50 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; }
51 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; }
52 static int CmpTime( vlc_value_t v, vlc_value_t w )
54 return v.i_time == w.i_time ? 0 : v.i_time > w.i_time ? 1 : -1;
56 static int CmpString( vlc_value_t v, vlc_value_t w )
59 return !w.psz_string ? 0 : -1;
61 return !w.psz_string ? 1 : strcmp( v.psz_string, w.psz_string );
63 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; }
64 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; }
66 /*****************************************************************************
67 * Local duplication functions, and local deallocation functions
68 *****************************************************************************/
69 static void DupDummy( vlc_value_t *p_val ) { (void)p_val; /* unused */ }
70 static void DupString( vlc_value_t *p_val )
72 p_val->psz_string = strdup( p_val->psz_string ? p_val->psz_string : "" );
75 static void DupList( vlc_value_t *p_val )
78 vlc_list_t *p_list = malloc( sizeof(vlc_list_t) );
80 p_list->i_count = p_val->p_list->i_count;
81 if( p_val->p_list->i_count )
83 p_list->p_values = malloc( p_list->i_count * sizeof(vlc_value_t) );
84 p_list->pi_types = malloc( p_list->i_count * sizeof(int) );
88 p_list->p_values = NULL;
89 p_list->pi_types = NULL;
92 for( i = 0; i < p_list->i_count; i++ )
94 p_list->p_values[i] = p_val->p_list->p_values[i];
95 p_list->pi_types[i] = p_val->p_list->pi_types[i];
96 switch( p_val->p_list->pi_types[i] & VLC_VAR_CLASS )
100 DupString( &p_list->p_values[i] );
107 p_val->p_list = p_list;
110 static void FreeDummy( vlc_value_t *p_val ) { (void)p_val; /* unused */ }
111 static void FreeString( vlc_value_t *p_val ) { free( p_val->psz_string ); }
112 static void FreeMutex( vlc_value_t *p_val ) { vlc_mutex_destroy( (vlc_mutex_t*)p_val->p_address ); free( p_val->p_address ); }
114 static void FreeList( vlc_value_t *p_val )
117 for( i = 0; i < p_val->p_list->i_count; i++ )
119 switch( p_val->p_list->pi_types[i] & VLC_VAR_CLASS )
122 FreeString( &p_val->p_list->p_values[i] );
125 FreeMutex( &p_val->p_list->p_values[i] );
132 if( p_val->p_list->i_count )
134 free( p_val->p_list->p_values );
135 free( p_val->p_list->pi_types );
137 free( p_val->p_list );
140 static const struct variable_ops_t
141 void_ops = { NULL, DupDummy, FreeDummy, },
142 addr_ops = { CmpAddress, DupDummy, FreeDummy, },
143 bool_ops = { CmpBool, DupDummy, FreeDummy, },
144 float_ops = { CmpFloat, DupDummy, FreeDummy, },
145 int_ops = { CmpInt, DupDummy, FreeDummy, },
146 list_ops = { CmpAddress, DupList, FreeList, },
147 mutex_ops = { CmpAddress, DupDummy, FreeMutex, },
148 string_ops = { CmpString, DupString, FreeString, },
149 time_ops = { CmpTime, DupDummy, FreeDummy, };
151 /*****************************************************************************
153 *****************************************************************************/
154 static int GetUnused ( vlc_object_t *, const char * );
155 static uint32_t HashString ( const char * );
156 static int Insert ( variable_t *, int, const char * );
157 static int InsertInner ( variable_t *, int, uint32_t );
158 static int Lookup ( variable_t *, size_t, const char * );
160 static void CheckValue ( variable_t *, vlc_value_t * );
162 static int InheritValue( vlc_object_t *, const char *, vlc_value_t *,
164 static int TriggerCallback( vlc_object_t *, variable_t **, const char *,
168 * Initialize a vlc variable
170 * We hash the given string and insert it into the sorted list. The insertion
171 * may require slow memory copies, but think about what we gain in the log(n)
172 * lookup phase when setting/getting the variable value!
174 * \param p_this The object in which to create the variable
175 * \param psz_name The name of the variable
176 * \param i_type The variables type. Must be one of \ref var_type combined with
177 * zero or more \ref var_flags
179 int __var_Create( vlc_object_t *p_this, const char *psz_name, int i_type )
183 static vlc_list_t dummy_null_list = {0, NULL, NULL};
187 vlc_object_internals_t *p_priv = vlc_internals( p_this );
189 vlc_mutex_lock( &p_priv->var_lock );
191 /* FIXME: if the variable already exists, we don't duplicate it. But we
192 * duplicate the lookups. It's not that serious, but if anyone finds some
193 * time to rework Insert() so that only one lookup has to be done, feel
195 i_new = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
199 /* If the types differ, variable creation failed. */
200 if( (i_type & VLC_VAR_CLASS) != (p_priv->p_vars[i_new].i_type & VLC_VAR_CLASS) )
202 msg_Err( p_this, "Variable '%s' (0x%04x) already exist but with a different type (0x%04x)",
203 psz_name, p_priv->p_vars[i_new].i_type, i_type );
204 vlc_mutex_unlock( &p_priv->var_lock );
208 p_priv->p_vars[i_new].i_usage++;
209 p_priv->p_vars[i_new].i_type |= ( i_type & VLC_VAR_ISCOMMAND );
210 p_priv->p_vars[i_new].i_type |= ( i_type & VLC_VAR_HASCHOICE );
211 vlc_mutex_unlock( &p_priv->var_lock );
215 i_new = Insert( p_priv->p_vars, p_priv->i_vars, psz_name );
217 if( (p_priv->i_vars & 15) == 15 )
219 p_priv->p_vars = realloc( p_priv->p_vars,
220 (p_priv->i_vars+17) * sizeof(variable_t) );
223 memmove( p_priv->p_vars + i_new + 1,
224 p_priv->p_vars + i_new,
225 (p_priv->i_vars - i_new) * sizeof(variable_t) );
229 p_var = &p_priv->p_vars[i_new];
230 memset( p_var, 0, sizeof(*p_var) );
232 p_var->i_hash = HashString( psz_name );
233 p_var->psz_name = strdup( psz_name );
234 p_var->psz_text = NULL;
236 p_var->i_type = i_type & ~VLC_VAR_DOINHERIT;
240 p_var->i_default = -1;
241 p_var->choices.i_count = 0;
242 p_var->choices.p_values = NULL;
243 p_var->choices_text.i_count = 0;
244 p_var->choices_text.p_values = NULL;
246 p_var->b_incallback = false;
247 p_var->i_entries = 0;
248 p_var->p_entries = NULL;
250 /* Always initialize the variable, even if it is a list variable; this
251 * will lead to errors if the variable is not initialized, but it will
252 * not cause crashes in the variable handling. */
253 switch( i_type & VLC_VAR_CLASS )
256 p_var->ops = &bool_ops;
257 p_var->val.b_bool = false;
259 case VLC_VAR_INTEGER:
260 p_var->ops = &int_ops;
261 p_var->val.i_int = 0;
264 p_var->ops = &string_ops;
265 p_var->val.psz_string = NULL;
268 p_var->ops = &float_ops;
269 p_var->val.f_float = 0.0;
272 p_var->ops = &time_ops;
273 p_var->val.i_time = 0;
275 case VLC_VAR_ADDRESS:
276 p_var->ops = &addr_ops;
277 p_var->val.p_address = NULL;
280 p_var->ops = &mutex_ops;
281 p_var->val.p_address = malloc( sizeof(vlc_mutex_t) );
282 vlc_mutex_init( (vlc_mutex_t*)p_var->val.p_address );
285 p_var->ops = &list_ops;
286 p_var->val.p_list = &dummy_null_list;
289 p_var->ops = &void_ops;
293 /* Duplicate the default data we stored. */
294 p_var->ops->pf_dup( &p_var->val );
296 if( i_type & VLC_VAR_DOINHERIT )
300 if( InheritValue( p_this, psz_name, &val, p_var->i_type )
303 /* Free data if needed */
304 p_var->ops->pf_free( &p_var->val );
305 /* Set the variable */
308 if( i_type & VLC_VAR_HASCHOICE )
310 /* We must add the inherited value to our choice list */
311 p_var->i_default = 0;
313 INSERT_ELEM( p_var->choices.p_values, p_var->choices.i_count,
315 INSERT_ELEM( p_var->choices_text.p_values,
316 p_var->choices_text.i_count, 0, val );
317 p_var->ops->pf_dup( &p_var->choices.p_values[0] );
318 p_var->choices_text.p_values[0].psz_string = NULL;
323 vlc_mutex_unlock( &p_priv->var_lock );
329 * Destroy a vlc variable
331 * Look for the variable and destroy it if it is found. As in var_Create we
332 * do a call to memmove() but we have performance counterparts elsewhere.
334 * \param p_this The object that holds the variable
335 * \param psz_name The name of the variable
337 int __var_Destroy( vlc_object_t *p_this, const char *psz_name )
344 vlc_object_internals_t *p_priv = vlc_internals( p_this );
346 vlc_mutex_lock( &p_priv->var_lock );
348 i_var = GetUnused( p_this, psz_name );
351 vlc_mutex_unlock( &p_priv->var_lock );
355 p_var = &p_priv->p_vars[i_var];
357 if( p_var->i_usage > 1 )
360 vlc_mutex_unlock( &p_priv->var_lock );
364 /* Free value if needed */
365 p_var->ops->pf_free( &p_var->val );
367 /* Free choice list if needed */
368 if( p_var->choices.i_count )
370 for( i = 0 ; i < p_var->choices.i_count ; i++ )
372 p_var->ops->pf_free( &p_var->choices.p_values[i] );
373 free( p_var->choices_text.p_values[i].psz_string );
375 free( p_var->choices.p_values );
376 free( p_var->choices_text.p_values );
379 /* Free callbacks if needed */
380 free( p_var->p_entries );
382 free( p_var->psz_name );
383 free( p_var->psz_text );
385 memmove( p_priv->p_vars + i_var,
386 p_priv->p_vars + i_var + 1,
387 (p_priv->i_vars - i_var - 1) * sizeof(variable_t) );
389 if( (p_priv->i_vars & 15) == 0 )
391 p_priv->p_vars = realloc( p_priv->p_vars,
392 (p_priv->i_vars) * sizeof( variable_t ) );
397 vlc_mutex_unlock( &p_priv->var_lock );
403 * Perform an action on a variable
405 * \param p_this The object that holds the variable
406 * \param psz_name The name of the variable
407 * \param i_action The action to perform. Must be one of \ref var_action
408 * \param p_val First action parameter
409 * \param p_val2 Second action parameter
411 int __var_Change( vlc_object_t *p_this, const char *psz_name,
412 int i_action, vlc_value_t *p_val, vlc_value_t *p_val2 )
420 vlc_object_internals_t *p_priv = vlc_internals( p_this );
422 vlc_mutex_lock( &p_priv->var_lock );
424 i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
428 vlc_mutex_unlock( &p_priv->var_lock );
432 p_var = &p_priv->p_vars[i_var];
437 if( p_var->i_type & VLC_VAR_HASMIN )
439 p_var->ops->pf_free( &p_var->min );
441 p_var->i_type |= VLC_VAR_HASMIN;
443 p_var->ops->pf_dup( &p_var->min );
444 CheckValue( p_var, &p_var->val );
447 if( p_var->i_type & VLC_VAR_HASMIN )
453 if( p_var->i_type & VLC_VAR_HASMAX )
455 p_var->ops->pf_free( &p_var->max );
457 p_var->i_type |= VLC_VAR_HASMAX;
459 p_var->ops->pf_dup( &p_var->max );
460 CheckValue( p_var, &p_var->val );
463 if( p_var->i_type & VLC_VAR_HASMAX )
468 case VLC_VAR_SETSTEP:
469 if( p_var->i_type & VLC_VAR_HASSTEP )
471 p_var->ops->pf_free( &p_var->step );
473 p_var->i_type |= VLC_VAR_HASSTEP;
474 p_var->step = *p_val;
475 p_var->ops->pf_dup( &p_var->step );
476 CheckValue( p_var, &p_var->val );
478 case VLC_VAR_GETSTEP:
479 if( p_var->i_type & VLC_VAR_HASSTEP )
481 *p_val = p_var->step;
484 case VLC_VAR_ADDCHOICE:
485 i = p_var->choices.i_count;
487 INSERT_ELEM( p_var->choices.p_values, p_var->choices.i_count,
489 INSERT_ELEM( p_var->choices_text.p_values,
490 p_var->choices_text.i_count, i, *p_val );
491 p_var->ops->pf_dup( &p_var->choices.p_values[i] );
492 p_var->choices_text.p_values[i].psz_string =
493 ( p_val2 && p_val2->psz_string ) ?
494 strdup( p_val2->psz_string ) : NULL;
496 CheckValue( p_var, &p_var->val );
498 case VLC_VAR_DELCHOICE:
499 for( i = 0 ; i < p_var->choices.i_count ; i++ )
501 if( p_var->ops->pf_cmp( p_var->choices.p_values[i], *p_val ) == 0 )
507 if( i == p_var->choices.i_count )
510 vlc_mutex_unlock( &p_priv->var_lock );
514 if( p_var->i_default > i )
518 else if( p_var->i_default == i )
520 p_var->i_default = -1;
523 p_var->ops->pf_free( &p_var->choices.p_values[i] );
524 free( p_var->choices_text.p_values[i].psz_string );
525 REMOVE_ELEM( p_var->choices.p_values, p_var->choices.i_count, i );
526 REMOVE_ELEM( p_var->choices_text.p_values,
527 p_var->choices_text.i_count, i );
529 CheckValue( p_var, &p_var->val );
531 case VLC_VAR_CHOICESCOUNT:
532 p_val->i_int = p_var->choices.i_count;
534 case VLC_VAR_CLEARCHOICES:
535 for( i = 0 ; i < p_var->choices.i_count ; i++ )
537 p_var->ops->pf_free( &p_var->choices.p_values[i] );
539 for( i = 0 ; i < p_var->choices_text.i_count ; i++ )
540 free( p_var->choices_text.p_values[i].psz_string );
542 if( p_var->choices.i_count ) free( p_var->choices.p_values );
543 if( p_var->choices_text.i_count ) free( p_var->choices_text.p_values );
545 p_var->choices.i_count = 0;
546 p_var->choices.p_values = NULL;
547 p_var->choices_text.i_count = 0;
548 p_var->choices_text.p_values = NULL;
549 p_var->i_default = -1;
551 case VLC_VAR_SETDEFAULT:
552 /* FIXME: the list is sorted, dude. Use something cleverer. */
553 for( i = 0 ; i < p_var->choices.i_count ; i++ )
555 if( p_var->ops->pf_cmp( p_var->choices.p_values[i], *p_val ) == 0 )
561 if( i == p_var->choices.i_count )
567 p_var->i_default = i;
568 CheckValue( p_var, &p_var->val );
570 case VLC_VAR_SETVALUE:
571 /* Duplicate data if needed */
572 p_var->ops->pf_dup( p_val );
573 /* Backup needed stuff */
575 /* Check boundaries and list */
576 CheckValue( p_var, p_val );
577 /* Set the variable */
579 /* Free data if needed */
580 p_var->ops->pf_free( &oldval );
582 case VLC_VAR_GETCHOICES:
583 case VLC_VAR_GETLIST:
584 p_val->p_list = malloc( sizeof(vlc_list_t) );
585 if( p_val2 ) p_val2->p_list = malloc( sizeof(vlc_list_t) );
586 if( p_var->choices.i_count )
588 p_val->p_list->p_values = malloc( p_var->choices.i_count
589 * sizeof(vlc_value_t) );
590 p_val->p_list->pi_types = malloc( p_var->choices.i_count
594 p_val2->p_list->p_values =
595 malloc( p_var->choices.i_count * sizeof(vlc_value_t) );
596 p_val2->p_list->pi_types =
597 malloc( p_var->choices.i_count * sizeof(int) );
600 p_val->p_list->i_count = p_var->choices.i_count;
601 if( p_val2 ) p_val2->p_list->i_count = p_var->choices.i_count;
602 for( i = 0 ; i < p_var->choices.i_count ; i++ )
604 p_val->p_list->p_values[i] = p_var->choices.p_values[i];
605 p_val->p_list->pi_types[i] = p_var->i_type;
606 p_var->ops->pf_dup( &p_val->p_list->p_values[i] );
609 p_val2->p_list->p_values[i].psz_string =
610 p_var->choices_text.p_values[i].psz_string ?
611 strdup(p_var->choices_text.p_values[i].psz_string) : NULL;
612 p_val2->p_list->pi_types[i] = VLC_VAR_STRING;
616 case VLC_VAR_SETTEXT:
617 free( p_var->psz_text );
618 if( p_val && p_val->psz_string )
619 p_var->psz_text = strdup( p_val->psz_string );
621 p_var->psz_text = NULL;
623 case VLC_VAR_GETTEXT:
624 p_val->psz_string = NULL;
625 if( p_var->psz_text )
627 p_val->psz_string = strdup( p_var->psz_text );
630 case VLC_VAR_INHERITVALUE:
634 if( InheritValue( p_this,
635 p_val2 ? p_val2->psz_string : psz_name,
636 &val, p_var->i_type )
639 /* Duplicate already done */
641 /* Backup needed stuff */
643 /* Check boundaries and list */
644 CheckValue( p_var, &val );
645 /* Set the variable */
647 /* Free data if needed */
648 p_var->ops->pf_free( &oldval );
654 p_var->ops->pf_dup( p_val );
659 case VLC_VAR_SETISCOMMAND:
660 p_var->i_type |= VLC_VAR_ISCOMMAND;
667 vlc_mutex_unlock( &p_priv->var_lock );
674 * Perform a Get and Set on a variable
676 * \param p_this: The object that hold the variable
677 * \param psz_name: the name of the variable
678 * \param i_action: the action to perform
679 * \param p_val: The action parameter
680 * \return vlc error codes
682 int __var_GetAndSet( vlc_object_t *p_this, const char *psz_name, int i_action,
686 int i_ret = VLC_SUCCESS;
692 vlc_object_internals_t *p_priv = vlc_internals( p_this );
694 vlc_mutex_lock( &p_priv->var_lock );
695 i_var = GetUnused( p_this, psz_name );
698 vlc_mutex_unlock( &p_priv->var_lock );
702 p_var = &p_priv->p_vars[i_var];
704 /* Duplicated data if needed */
705 //p_var->ops->pf_dup( &val );
707 /* Backup needed stuff */
710 /* depending of the action requiered */
713 case VLC_VAR_TOGGLE_BOOL:
714 assert( ( p_var->i_type & VLC_VAR_BOOL ) == VLC_VAR_BOOL );
715 p_var->val.b_bool = !p_var->val.b_bool;
717 case VLC_VAR_INTEGER_INCDEC:
718 assert( ( p_var->i_type & VLC_VAR_INTEGER ) == VLC_VAR_INTEGER );
719 p_var->val.i_int += val.i_int;
722 vlc_mutex_unlock( &p_priv->var_lock );
726 /* Check boundaries */
727 CheckValue( p_var, &p_var->val );
729 /* Deal with callbacks.*/
730 if( p_var->i_entries )
731 i_ret = TriggerCallback( p_this, &p_var, psz_name, oldval );
733 vlc_mutex_unlock( &p_priv->var_lock );
740 * Request a variable's type
742 * \return The variable type if it exists, or 0 if the
743 * variable could not be found.
746 int __var_Type( vlc_object_t *p_this, const char *psz_name )
752 vlc_object_internals_t *p_priv = vlc_internals( p_this );
754 vlc_mutex_lock( &p_priv->var_lock );
756 i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
760 vlc_mutex_unlock( &p_priv->var_lock );
764 i_type = p_priv->p_vars[i_var].i_type;
766 vlc_mutex_unlock( &p_priv->var_lock );
771 int var_SetChecked( vlc_object_t *p_this, const char *psz_name,
772 int expected_type, vlc_value_t val )
775 int i_ret = VLC_SUCCESS;
781 vlc_object_internals_t *p_priv = vlc_internals( p_this );
783 vlc_mutex_lock( &p_priv->var_lock );
785 i_var = GetUnused( p_this, psz_name );
788 vlc_mutex_unlock( &p_priv->var_lock );
792 p_var = &p_priv->p_vars[i_var];
793 assert( expected_type == 0 ||
794 (p_var->i_type & VLC_VAR_CLASS) == expected_type );
796 /* Duplicate data if needed */
797 p_var->ops->pf_dup( &val );
799 /* Backup needed stuff */
802 /* Check boundaries and list */
803 CheckValue( p_var, &val );
805 /* Set the variable */
808 /* Deal with callbacks */
809 if( p_var->i_entries )
810 i_ret = TriggerCallback( p_this, &p_var, psz_name, oldval );
812 /* Free data if needed */
813 p_var->ops->pf_free( &oldval );
815 vlc_mutex_unlock( &p_priv->var_lock );
822 * Set a variable's value
824 * \param p_this The object that hold the variable
825 * \param psz_name The name of the variable
826 * \param val the value to set
828 int __var_Set( vlc_object_t *p_this, const char *psz_name, vlc_value_t val )
830 return var_SetChecked( p_this, psz_name, 0, val );
833 int var_GetChecked( vlc_object_t *p_this, const char *psz_name,
834 int expected_type, vlc_value_t *p_val )
838 vlc_object_internals_t *p_priv = vlc_internals( p_this );
839 int i_var, err = VLC_SUCCESS;
841 vlc_mutex_lock( &p_priv->var_lock );
843 i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
846 variable_t *p_var = &p_priv->p_vars[i_var];
848 assert( expected_type == 0 ||
849 (p_var->i_type & VLC_VAR_CLASS) == expected_type );
851 /* Really get the variable */
855 /* Alert if the type is VLC_VAR_VOID */
856 if( ( p_var->i_type & VLC_VAR_TYPE ) == VLC_VAR_VOID )
857 msg_Warn( p_this, "Calling var_GetVoid on the void variable '%s' (0x%04x)", psz_name, p_var->i_type );
860 /* Duplicate value if needed */
861 p_var->ops->pf_dup( p_val );
866 vlc_mutex_unlock( &p_priv->var_lock );
871 * Get a variable's value
873 * \param p_this The object that holds the variable
874 * \param psz_name The name of the variable
875 * \param p_val Pointer to a vlc_value_t that will hold the variable's value
876 * after the function is finished
878 int __var_Get( vlc_object_t *p_this, const char *psz_name, vlc_value_t *p_val )
880 return var_GetChecked( p_this, psz_name, 0, p_val );
884 * Register a callback in a variable
886 * We store a function pointer that will be called upon variable
889 * \param p_this The object that holds the variable
890 * \param psz_name The name of the variable
891 * \param pf_callback The function pointer
892 * \param p_data A generic pointer that will be passed as the last
893 * argument to the callback function.
895 * \warning The callback function is run in the thread that calls var_Set on
896 * the variable. Use proper locking. This thread may not have much
897 * time to spare, so keep callback functions short.
899 int __var_AddCallback( vlc_object_t *p_this, const char *psz_name,
900 vlc_callback_t pf_callback, void *p_data )
904 callback_entry_t entry;
908 vlc_object_internals_t *p_priv = vlc_internals( p_this );
910 entry.pf_callback = pf_callback;
911 entry.p_data = p_data;
913 vlc_mutex_lock( &p_priv->var_lock );
915 i_var = GetUnused( p_this, psz_name );
918 vlc_mutex_unlock( &p_priv->var_lock );
922 p_var = &p_priv->p_vars[i_var];
924 INSERT_ELEM( p_var->p_entries,
929 vlc_mutex_unlock( &p_priv->var_lock );
935 * Remove a callback from a variable
937 * pf_callback and p_data have to be given again, because different objects
938 * might have registered the same callback function.
940 int __var_DelCallback( vlc_object_t *p_this, const char *psz_name,
941 vlc_callback_t pf_callback, void *p_data )
946 bool b_found_similar = false;
951 vlc_object_internals_t *p_priv = vlc_internals( p_this );
953 vlc_mutex_lock( &p_priv->var_lock );
955 i_var = GetUnused( p_this, psz_name );
958 vlc_mutex_unlock( &p_priv->var_lock );
962 p_var = &p_priv->p_vars[i_var];
964 for( i_entry = p_var->i_entries ; i_entry-- ; )
966 if( p_var->p_entries[i_entry].pf_callback == pf_callback
967 && p_var->p_entries[i_entry].p_data == p_data )
972 else if( p_var->p_entries[i_entry].pf_callback == pf_callback )
973 b_found_similar = true;
980 if( b_found_similar )
981 fprintf( stderr, "Calling var_DelCallback for '%s' with the same "
982 "function but not the same data.", psz_name );
985 vlc_mutex_unlock( &p_priv->var_lock );
989 REMOVE_ELEM( p_var->p_entries, p_var->i_entries, i_entry );
991 vlc_mutex_unlock( &p_priv->var_lock );
997 * Trigger callback on a variable
999 * \param p_this The object that hold the variable
1000 * \param psz_name The name of the variable
1002 int __var_TriggerCallback( vlc_object_t *p_this, const char *psz_name )
1005 int i_ret = VLC_SUCCESS;
1010 vlc_object_internals_t *p_priv = vlc_internals( p_this );
1012 vlc_mutex_lock( &p_priv->var_lock );
1014 i_var = GetUnused( p_this, psz_name );
1017 vlc_mutex_unlock( &p_priv->var_lock );
1021 p_var = &p_priv->p_vars[i_var];
1023 /* Deal with callbacks. Tell we're in a callback, release the lock,
1024 * call stored functions, retake the lock. */
1025 if( p_var->i_entries )
1026 i_ret = TriggerCallback( p_this, &p_var, psz_name, p_var->val );
1028 vlc_mutex_unlock( &p_priv->var_lock );
1032 /** Parse a stringified option
1033 * This function parse a string option and create the associated object
1035 * The option must be of the form "[no[-]]foo[=bar]" where foo is the
1036 * option name and bar is the value of the option.
1037 * \param p_obj the object in which the variable must be created
1038 * \param psz_option the option to parse
1039 * \param trusted whether the option is set by a trusted input or not
1042 void var_OptionParse( vlc_object_t *p_obj, const char *psz_option,
1045 char *psz_name, *psz_value;
1047 bool b_isno = false;
1050 val.psz_string = NULL;
1052 /* It's too much of a hassle to remove the ':' when we parse
1053 * the cmd line :) */
1054 if( psz_option[0] == ':' )
1057 if( !psz_option[0] )
1060 psz_name = strdup( psz_option );
1061 if( psz_name == NULL )
1064 psz_value = strchr( psz_name, '=' );
1065 if( psz_value != NULL )
1066 *psz_value++ = '\0';
1068 /* FIXME: :programs should be handled generically */
1069 if( !strcmp( psz_name, "programs" ) )
1070 i_type = VLC_VAR_LIST;
1072 i_type = config_GetType( p_obj, psz_name );
1074 if( !i_type && !psz_value )
1076 /* check for "no-foo" or "nofoo" */
1077 if( !strncmp( psz_name, "no-", 3 ) )
1079 memmove( psz_name, psz_name + 3, strlen(psz_name) + 1 - 3 );
1081 else if( !strncmp( psz_name, "no", 2 ) )
1083 memmove( psz_name, psz_name + 2, strlen(psz_name) + 1 - 2 );
1085 else goto cleanup; /* Option doesn't exist */
1088 i_type = config_GetType( p_obj, psz_name );
1090 if( !i_type ) goto cleanup; /* Option doesn't exist */
1092 if( ( i_type != VLC_VAR_BOOL ) &&
1093 ( !psz_value || !*psz_value ) ) goto cleanup; /* Invalid value */
1095 /* check if option is unsafe */
1098 module_config_t *p_config = config_FindConfig( p_obj, psz_name );
1099 if( !p_config || !p_config->b_safe )
1101 msg_Err( p_obj, "unsafe option \"%s\" has been ignored for "
1102 "security reasons", psz_name );
1108 /* Create the variable in the input object.
1109 * Children of the input object will be able to retreive this value
1110 * thanks to the inheritance property of the object variables. */
1111 __var_Create( p_obj, psz_name, i_type );
1116 val.b_bool = !b_isno;
1119 case VLC_VAR_INTEGER:
1120 val.i_int = strtol( psz_value, NULL, 0 );
1124 val.f_float = atof( psz_value );
1127 case VLC_VAR_STRING:
1128 case VLC_VAR_MODULE:
1130 case VLC_VAR_DIRECTORY:
1131 val.psz_string = psz_value;
1136 char *psz_orig, *psz_var;
1137 vlc_list_t *p_list = malloc(sizeof(vlc_list_t));
1138 val.p_list = p_list;
1139 p_list->i_count = 0;
1141 psz_var = psz_orig = strdup(psz_value);
1142 while( psz_var && *psz_var )
1144 char *psz_item = psz_var;
1146 while( *psz_var && *psz_var != ',' ) psz_var++;
1147 if( *psz_var == ',' )
1152 val2.i_int = strtol( psz_item, NULL, 0 );
1153 INSERT_ELEM( p_list->p_values, p_list->i_count,
1154 p_list->i_count, val2 );
1155 /* p_list->i_count is incremented twice by INSERT_ELEM */
1157 INSERT_ELEM( p_list->pi_types, p_list->i_count,
1158 p_list->i_count, VLC_VAR_INTEGER );
1168 __var_Set( p_obj, psz_name, val );
1170 /* If that's a list, remove all elements allocated */
1171 if( i_type == VLC_VAR_LIST )
1179 /* Following functions are local */
1181 /*****************************************************************************
1182 * GetUnused: find an unused (not in callback) variable from its name
1183 *****************************************************************************
1184 * We do i_tries tries before giving up, just in case the variable is being
1185 * modified and called from a callback.
1186 *****************************************************************************/
1187 static int GetUnused( vlc_object_t *p_this, const char *psz_name )
1191 vlc_object_internals_t *p_priv = vlc_internals( p_this );
1197 i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
1203 if( ! p_priv->p_vars[i_var].b_incallback )
1208 mutex_cleanup_push( &p_priv->var_lock );
1209 vlc_cond_wait( &p_priv->var_wait, &p_priv->var_lock );
1214 /*****************************************************************************
1215 * HashString: our cool hash function
1216 *****************************************************************************
1217 * This function is not intended to be crypto-secure, we only want it to be
1218 * fast and not suck too much. This one is pretty fast and did 0 collisions
1219 * in wenglish's dictionary.
1220 *****************************************************************************/
1221 static uint32_t HashString( const char *psz_string )
1223 uint32_t i_hash = 0;
1225 while( *psz_string )
1227 i_hash += *psz_string++;
1228 i_hash += i_hash << 10;
1229 i_hash ^= i_hash >> 8;
1235 /*****************************************************************************
1236 * Insert: find an empty slot to insert a new variable
1237 *****************************************************************************
1238 * We use a recursive inner function indexed on the hash. This function does
1239 * nothing in the rare cases where a collision may occur, see Lookup()
1240 * to see how we handle them.
1241 * XXX: does this really need to be written recursively?
1242 *****************************************************************************/
1243 static int Insert( variable_t *p_vars, int i_count, const char *psz_name )
1250 return InsertInner( p_vars, i_count, HashString( psz_name ) );
1253 static int InsertInner( variable_t *p_vars, int i_count, uint32_t i_hash )
1257 if( i_hash <= p_vars[0].i_hash )
1262 if( i_hash >= p_vars[i_count - 1].i_hash )
1267 i_middle = i_count / 2;
1269 /* We know that 0 < i_middle */
1270 if( i_hash < p_vars[i_middle].i_hash )
1272 return InsertInner( p_vars, i_middle, i_hash );
1275 /* We know that i_middle + 1 < i_count */
1276 if( i_hash > p_vars[i_middle + 1].i_hash )
1278 return i_middle + 1 + InsertInner( p_vars + i_middle + 1,
1279 i_count - i_middle - 1,
1283 return i_middle + 1;
1286 static int u32cmp( const void *key, const void *data )
1288 const variable_t *p_var = data;
1289 uint32_t hash = *(const uint32_t *)key ;
1291 if( hash > p_var->i_hash )
1293 if( hash < p_var->i_hash )
1298 /*****************************************************************************
1299 * Lookup: find an existing variable given its name
1300 *****************************************************************************
1301 * We use a recursive inner function indexed on the hash. Care is taken of
1302 * possible hash collisions.
1303 *****************************************************************************/
1304 static int Lookup( variable_t *p_vars, size_t i_count, const char *psz_name )
1309 i_hash = HashString( psz_name );
1310 p_var = bsearch( &i_hash, p_vars, i_count, sizeof( *p_var ), u32cmp );
1312 /* Hash not found */
1316 assert( i_count > 0 );
1318 /* Find the first entry with the right hash */
1319 while( (p_var > p_vars) && (i_hash == p_var[-1].i_hash) )
1322 assert( p_var->i_hash == i_hash );
1324 /* Hash collision should be very unlikely, but we cannot guarantee
1325 * it will never happen. So we do an exhaustive search amongst all
1326 * entries with the same hash. Typically, there is only one anyway. */
1327 for( variable_t *p_end = p_vars + i_count;
1328 (p_var < p_end) && (i_hash == p_var->i_hash);
1331 if( !strcmp( psz_name, p_var->psz_name ) )
1332 return p_var - p_vars;
1335 /* Hash found, but entry not found */
1339 /*****************************************************************************
1340 * CheckValue: check that a value is valid wrt. a variable
1341 *****************************************************************************
1342 * This function checks p_val's value against p_var's limitations such as
1343 * minimal and maximal value, step, in-list position, and modifies p_val if
1345 ****************************************************************************/
1346 static void CheckValue ( variable_t *p_var, vlc_value_t *p_val )
1348 /* Check that our variable is in the list */
1349 if( p_var->i_type & VLC_VAR_HASCHOICE && p_var->choices.i_count )
1353 /* This list is not sorted so go throug it (this is a small list) */
1354 for( i = p_var->choices.i_count ; i-- ; )
1356 if( p_var->ops->pf_cmp( *p_val, p_var->choices.p_values[i] ) == 0 )
1362 /* If not found, change it to anything vaguely valid */
1365 /* Free the old variable, get the new one, dup it */
1366 p_var->ops->pf_free( p_val );
1367 *p_val = p_var->choices.p_values[p_var->i_default >= 0
1368 ? p_var->i_default : 0 ];
1369 p_var->ops->pf_dup( p_val );
1373 /* Check that our variable is within the bounds */
1374 switch( p_var->i_type & VLC_VAR_TYPE )
1376 case VLC_VAR_INTEGER:
1377 if( p_var->i_type & VLC_VAR_HASSTEP && p_var->step.i_int
1378 && (p_val->i_int % p_var->step.i_int) )
1380 p_val->i_int = (p_val->i_int + (p_var->step.i_int / 2))
1381 / p_var->step.i_int * p_var->step.i_int;
1383 if( p_var->i_type & VLC_VAR_HASMIN
1384 && p_val->i_int < p_var->min.i_int )
1386 p_val->i_int = p_var->min.i_int;
1388 if( p_var->i_type & VLC_VAR_HASMAX
1389 && p_val->i_int > p_var->max.i_int )
1391 p_val->i_int = p_var->max.i_int;
1395 if( p_var->i_type & VLC_VAR_HASSTEP && p_var->step.f_float )
1397 float f_round = p_var->step.f_float * (float)(int)( 0.5 +
1398 p_val->f_float / p_var->step.f_float );
1399 if( p_val->f_float != f_round )
1401 p_val->f_float = f_round;
1404 if( p_var->i_type & VLC_VAR_HASMIN
1405 && p_val->f_float < p_var->min.f_float )
1407 p_val->f_float = p_var->min.f_float;
1409 if( p_var->i_type & VLC_VAR_HASMAX
1410 && p_val->f_float > p_var->max.f_float )
1412 p_val->f_float = p_var->max.f_float;
1421 /*****************************************************************************
1422 * InheritValue: try to inherit the value of this variable from the same one
1423 * in our closest parent, libvlc or ultimately from the configuration.
1424 * The function should always be entered with the object var_lock locked.
1425 *****************************************************************************/
1426 static int InheritValue( vlc_object_t *p_this, const char *psz_name,
1427 vlc_value_t *p_val, int i_type )
1432 if( p_this->p_parent || ( p_this->p_libvlc && p_this != (vlc_object_t*) p_this->p_libvlc ) )
1434 vlc_object_internals_t *p_priv;
1436 if( p_this->p_parent )
1437 p_priv = vlc_internals( p_this->p_parent );
1439 p_priv = vlc_internals( p_this->p_libvlc );
1441 i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
1446 p_var = &p_priv->p_vars[i_var];
1448 /* Really get the variable */
1449 *p_val = p_var->val;
1451 /* Duplicate value if needed */
1452 p_var->ops->pf_dup( p_val );
1454 /*msg_Dbg( p_this, "Inherited value for var %s from object %s",
1455 psz_name ? psz_name : "(null)",
1456 p_this->psz_object_name
1457 ? p_this->psz_object_name : "(Unknown)" );*/
1460 else if ( p_this->p_parent ) /* We are still not there */
1461 return InheritValue( p_this->p_parent, psz_name, p_val, i_type );
1463 /* else take value from config */
1467 switch( i_type & VLC_VAR_CLASS )
1469 case VLC_VAR_STRING:
1470 p_val->psz_string = config_GetPsz( p_this, psz_name );
1471 if( !p_val->psz_string ) p_val->psz_string = strdup("");
1474 p_val->f_float = config_GetFloat( p_this, psz_name );
1476 case VLC_VAR_INTEGER:
1477 p_val->i_int = config_GetInt( p_this, psz_name );
1480 p_val->b_bool = config_GetInt( p_this, psz_name );
1484 char *psz_orig, *psz_var;
1485 vlc_list_t *p_list = malloc(sizeof(vlc_list_t));
1486 p_val->p_list = p_list;
1487 p_list->i_count = 0;
1489 psz_var = psz_orig = config_GetPsz( p_this, psz_name );
1490 while( psz_var && *psz_var )
1492 char *psz_item = psz_var;
1494 while( *psz_var && *psz_var != ',' ) psz_var++;
1495 if( *psz_var == ',' )
1500 val.i_int = strtol( psz_item, NULL, 0 );
1501 INSERT_ELEM( p_list->p_values, p_list->i_count,
1502 p_list->i_count, val );
1503 /* p_list->i_count is incremented twice by INSERT_ELEM */
1505 INSERT_ELEM( p_list->pi_types, p_list->i_count,
1506 p_list->i_count, VLC_VAR_INTEGER );
1512 msg_Warn( p_this, "Could not inherit value for var %s "
1513 "from config. Invalid Type", psz_name );
1517 /*msg_Dbg( p_this, "Inherited value for var %s from config", psz_name );*/
1522 /**********************************************************************
1523 * Trigger the callbacks.
1524 * Tell we're in a callback, release the lock, call stored functions,
1526 **********************************************************************/
1527 static int TriggerCallback( vlc_object_t *p_this, variable_t **pp_var,
1528 const char *psz_name, vlc_value_t oldval )
1531 int i_entries = (*pp_var)->i_entries;
1532 callback_entry_t *p_entries = (*pp_var)->p_entries;
1536 vlc_object_internals_t *p_priv = vlc_internals( p_this );
1538 (*pp_var)->b_incallback = true;
1539 vlc_mutex_unlock( &p_priv->var_lock );
1541 /* The real calls */
1542 for( ; i_entries-- ; )
1544 p_entries[i_entries].pf_callback( p_this, psz_name, oldval, (*pp_var)->val,
1545 p_entries[i_entries].p_data );
1548 vlc_mutex_lock( &p_priv->var_lock );
1550 i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
1553 msg_Err( p_this, "variable %s has disappeared", psz_name );
1557 *pp_var = &p_priv->p_vars[i_var];
1558 (*pp_var)->b_incallback = false;
1559 vlc_cond_broadcast( &p_priv->var_wait );
1565 /**********************************************************************
1566 * Execute a var command on an object identified by its name
1567 **********************************************************************/
1568 int __var_Command( vlc_object_t *p_this, const char *psz_name,
1569 const char *psz_cmd, const char *psz_arg, char **psz_msg )
1571 vlc_object_t *p_obj = vlc_object_find_name( p_this->p_libvlc,
1572 psz_name, FIND_CHILD );
1578 *psz_msg = strdup( "Unknown destination object." );
1582 i_type = var_Type( p_obj, psz_cmd );
1583 if( !( i_type&VLC_VAR_ISCOMMAND ) )
1585 vlc_object_release( p_obj );
1587 *psz_msg = strdup( "Variable doesn't exist or isn't a command." );
1588 return VLC_EGENERIC;
1591 i_type &= VLC_VAR_CLASS;
1594 case VLC_VAR_INTEGER:
1595 i_ret = var_SetInteger( p_obj, psz_cmd, atoi( psz_arg ) );
1598 i_ret = var_SetFloat( p_obj, psz_cmd, atof( psz_arg ) );
1600 case VLC_VAR_STRING:
1601 i_ret = var_SetString( p_obj, psz_cmd, psz_arg );
1604 i_ret = var_SetBool( p_obj, psz_cmd, atoi( psz_arg ) );
1607 i_ret = VLC_EGENERIC;
1611 vlc_object_release( p_obj );
1615 if( asprintf( psz_msg, "%s on object %s returned %i (%s)",
1616 psz_cmd, psz_name, i_ret, vlc_error( i_ret ) ) == -1)
1625 * Free a list and the associated strings
1626 * @param p_val: the list variable
1627 * @param p_val2: the variable associated or NULL
1629 void var_FreeList( vlc_value_t *p_val, vlc_value_t *p_val2 )
1632 if( p_val2 && p_val2->p_list )
1634 for( int i = 0; i < p_val2->p_list->i_count; i++ )
1635 free( p_val2->p_list->p_values[i].psz_string );
1636 if( p_val2->p_list->i_count )
1638 free( p_val2->p_list->p_values );
1639 free( p_val2->p_list->pi_types );
1641 free( p_val2->p_list );