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"
36 #include "vlc_interface.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 )
184 static vlc_list_t dummy_null_list = {0, NULL, NULL};
185 vlc_object_internals_t *p_priv = vlc_internals( p_this );
187 vlc_mutex_lock( &p_priv->var_lock );
189 /* FIXME: if the variable already exists, we don't duplicate it. But we
190 * duplicate the lookups. It's not that serious, but if anyone finds some
191 * time to rework Insert() so that only one lookup has to be done, feel
193 i_new = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
197 /* If the types differ, variable creation failed. */
198 if( (i_type & VLC_VAR_TYPE) != (p_priv->p_vars[i_new].i_type & VLC_VAR_TYPE) )
200 msg_Err( p_this, "Variable '%s' (0x%04x) already exist but with a different type (0x%04x)",
201 psz_name, p_priv->p_vars[i_new].i_type, i_type );
202 vlc_mutex_unlock( &p_priv->var_lock );
206 p_priv->p_vars[i_new].i_usage++;
207 p_priv->p_vars[i_new].i_type |= ( i_type & VLC_VAR_ISCOMMAND );
208 p_priv->p_vars[i_new].i_type |= ( i_type & VLC_VAR_HASCHOICE );
209 vlc_mutex_unlock( &p_priv->var_lock );
213 i_new = Insert( p_priv->p_vars, p_priv->i_vars, psz_name );
215 if( (p_priv->i_vars & 15) == 15 )
217 p_priv->p_vars = realloc( p_priv->p_vars,
218 (p_priv->i_vars+17) * sizeof(variable_t) );
221 memmove( p_priv->p_vars + i_new + 1,
222 p_priv->p_vars + i_new,
223 (p_priv->i_vars - i_new) * sizeof(variable_t) );
227 p_var = &p_priv->p_vars[i_new];
228 memset( p_var, 0, sizeof(*p_var) );
230 p_var->i_hash = HashString( psz_name );
231 p_var->psz_name = strdup( psz_name );
232 p_var->psz_text = NULL;
234 p_var->i_type = i_type & ~VLC_VAR_DOINHERIT;
238 p_var->i_default = -1;
239 p_var->choices.i_count = 0;
240 p_var->choices.p_values = NULL;
241 p_var->choices_text.i_count = 0;
242 p_var->choices_text.p_values = NULL;
244 p_var->b_incallback = false;
245 p_var->i_entries = 0;
246 p_var->p_entries = NULL;
248 /* Always initialize the variable, even if it is a list variable; this
249 * will lead to errors if the variable is not initialized, but it will
250 * not cause crashes in the variable handling. */
251 switch( i_type & VLC_VAR_CLASS )
254 p_var->ops = &bool_ops;
255 p_var->val.b_bool = false;
257 case VLC_VAR_INTEGER:
258 p_var->ops = &int_ops;
259 p_var->val.i_int = 0;
262 p_var->ops = &string_ops;
263 p_var->val.psz_string = NULL;
266 p_var->ops = &float_ops;
267 p_var->val.f_float = 0.0;
270 p_var->ops = &time_ops;
271 p_var->val.i_time = 0;
273 case VLC_VAR_ADDRESS:
274 p_var->ops = &addr_ops;
275 p_var->val.p_address = NULL;
278 p_var->ops = &mutex_ops;
279 p_var->val.p_address = malloc( sizeof(vlc_mutex_t) );
280 vlc_mutex_init( (vlc_mutex_t*)p_var->val.p_address );
283 p_var->ops = &list_ops;
284 p_var->val.p_list = &dummy_null_list;
287 p_var->ops = &void_ops;
291 /* Duplicate the default data we stored. */
292 p_var->ops->pf_dup( &p_var->val );
294 if( i_type & VLC_VAR_DOINHERIT )
298 if( InheritValue( p_this, psz_name, &val, p_var->i_type )
301 /* Free data if needed */
302 p_var->ops->pf_free( &p_var->val );
303 /* Set the variable */
306 if( i_type & VLC_VAR_HASCHOICE )
308 /* We must add the inherited value to our choice list */
309 p_var->i_default = 0;
311 INSERT_ELEM( p_var->choices.p_values, p_var->choices.i_count,
313 INSERT_ELEM( p_var->choices_text.p_values,
314 p_var->choices_text.i_count, 0, val );
315 p_var->ops->pf_dup( &p_var->choices.p_values[0] );
316 p_var->choices_text.p_values[0].psz_string = NULL;
321 vlc_mutex_unlock( &p_priv->var_lock );
327 * Destroy a vlc variable
329 * Look for the variable and destroy it if it is found. As in var_Create we
330 * do a call to memmove() but we have performance counterparts elsewhere.
332 * \param p_this The object that holds the variable
333 * \param psz_name The name of the variable
335 int __var_Destroy( vlc_object_t *p_this, const char *psz_name )
339 vlc_object_internals_t *p_priv = vlc_internals( p_this );
341 vlc_mutex_lock( &p_priv->var_lock );
343 i_var = GetUnused( p_this, psz_name );
346 vlc_mutex_unlock( &p_priv->var_lock );
350 p_var = &p_priv->p_vars[i_var];
352 if( p_var->i_usage > 1 )
355 vlc_mutex_unlock( &p_priv->var_lock );
359 /* Free value if needed */
360 p_var->ops->pf_free( &p_var->val );
362 /* Free choice list if needed */
363 if( p_var->choices.i_count )
365 for( i = 0 ; i < p_var->choices.i_count ; i++ )
367 p_var->ops->pf_free( &p_var->choices.p_values[i] );
368 free( p_var->choices_text.p_values[i].psz_string );
370 free( p_var->choices.p_values );
371 free( p_var->choices_text.p_values );
374 /* Free callbacks if needed */
375 if( p_var->p_entries )
377 free( p_var->p_entries );
380 free( p_var->psz_name );
381 free( p_var->psz_text );
383 memmove( p_priv->p_vars + i_var,
384 p_priv->p_vars + i_var + 1,
385 (p_priv->i_vars - i_var - 1) * sizeof(variable_t) );
387 if( (p_priv->i_vars & 15) == 0 )
389 p_priv->p_vars = realloc( p_priv->p_vars,
390 (p_priv->i_vars) * sizeof( variable_t ) );
395 vlc_mutex_unlock( &p_priv->var_lock );
401 * Perform an action on a variable
403 * \param p_this The object that holds the variable
404 * \param psz_name The name of the variable
405 * \param i_action The action to perform. Must be one of \ref var_action
406 * \param p_val First action parameter
407 * \param p_val2 Second action parameter
409 int __var_Change( vlc_object_t *p_this, const char *psz_name,
410 int i_action, vlc_value_t *p_val, vlc_value_t *p_val2 )
415 vlc_object_internals_t *p_priv = vlc_internals( p_this );
417 vlc_mutex_lock( &p_priv->var_lock );
419 i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
423 vlc_mutex_unlock( &p_priv->var_lock );
427 p_var = &p_priv->p_vars[i_var];
432 if( p_var->i_type & VLC_VAR_HASMIN )
434 p_var->ops->pf_free( &p_var->min );
436 p_var->i_type |= VLC_VAR_HASMIN;
438 p_var->ops->pf_dup( &p_var->min );
439 CheckValue( p_var, &p_var->val );
442 if( p_var->i_type & VLC_VAR_HASMIN )
448 if( p_var->i_type & VLC_VAR_HASMAX )
450 p_var->ops->pf_free( &p_var->max );
452 p_var->i_type |= VLC_VAR_HASMAX;
454 p_var->ops->pf_dup( &p_var->max );
455 CheckValue( p_var, &p_var->val );
458 if( p_var->i_type & VLC_VAR_HASMAX )
463 case VLC_VAR_SETSTEP:
464 if( p_var->i_type & VLC_VAR_HASSTEP )
466 p_var->ops->pf_free( &p_var->step );
468 p_var->i_type |= VLC_VAR_HASSTEP;
469 p_var->step = *p_val;
470 p_var->ops->pf_dup( &p_var->step );
471 CheckValue( p_var, &p_var->val );
473 case VLC_VAR_GETSTEP:
474 if( p_var->i_type & VLC_VAR_HASSTEP )
476 *p_val = p_var->step;
479 case VLC_VAR_ADDCHOICE:
480 i = p_var->choices.i_count;
482 INSERT_ELEM( p_var->choices.p_values, p_var->choices.i_count,
484 INSERT_ELEM( p_var->choices_text.p_values,
485 p_var->choices_text.i_count, i, *p_val );
486 p_var->ops->pf_dup( &p_var->choices.p_values[i] );
487 p_var->choices_text.p_values[i].psz_string =
488 ( p_val2 && p_val2->psz_string ) ?
489 strdup( p_val2->psz_string ) : NULL;
491 CheckValue( p_var, &p_var->val );
493 case VLC_VAR_DELCHOICE:
494 for( i = 0 ; i < p_var->choices.i_count ; i++ )
496 if( p_var->ops->pf_cmp( p_var->choices.p_values[i], *p_val ) == 0 )
502 if( i == p_var->choices.i_count )
505 vlc_mutex_unlock( &p_priv->var_lock );
509 if( p_var->i_default > i )
513 else if( p_var->i_default == i )
515 p_var->i_default = -1;
518 p_var->ops->pf_free( &p_var->choices.p_values[i] );
519 free( p_var->choices_text.p_values[i].psz_string );
520 REMOVE_ELEM( p_var->choices.p_values, p_var->choices.i_count, i );
521 REMOVE_ELEM( p_var->choices_text.p_values,
522 p_var->choices_text.i_count, i );
524 CheckValue( p_var, &p_var->val );
526 case VLC_VAR_CHOICESCOUNT:
527 p_val->i_int = p_var->choices.i_count;
529 case VLC_VAR_CLEARCHOICES:
530 for( i = 0 ; i < p_var->choices.i_count ; i++ )
532 p_var->ops->pf_free( &p_var->choices.p_values[i] );
534 for( i = 0 ; i < p_var->choices_text.i_count ; i++ )
535 free( p_var->choices_text.p_values[i].psz_string );
537 if( p_var->choices.i_count ) free( p_var->choices.p_values );
538 if( p_var->choices_text.i_count ) free( p_var->choices_text.p_values );
540 p_var->choices.i_count = 0;
541 p_var->choices.p_values = NULL;
542 p_var->choices_text.i_count = 0;
543 p_var->choices_text.p_values = NULL;
544 p_var->i_default = -1;
546 case VLC_VAR_SETDEFAULT:
547 /* FIXME: the list is sorted, dude. Use something cleverer. */
548 for( i = 0 ; i < p_var->choices.i_count ; i++ )
550 if( p_var->ops->pf_cmp( p_var->choices.p_values[i], *p_val ) == 0 )
556 if( i == p_var->choices.i_count )
562 p_var->i_default = i;
563 CheckValue( p_var, &p_var->val );
565 case VLC_VAR_SETVALUE:
566 /* Duplicate data if needed */
567 p_var->ops->pf_dup( p_val );
568 /* Backup needed stuff */
570 /* Check boundaries and list */
571 CheckValue( p_var, p_val );
572 /* Set the variable */
574 /* Free data if needed */
575 p_var->ops->pf_free( &oldval );
577 case VLC_VAR_GETCHOICES:
578 case VLC_VAR_GETLIST:
579 p_val->p_list = malloc( sizeof(vlc_list_t) );
580 if( p_val2 ) p_val2->p_list = malloc( sizeof(vlc_list_t) );
581 if( p_var->choices.i_count )
583 p_val->p_list->p_values = malloc( p_var->choices.i_count
584 * sizeof(vlc_value_t) );
585 p_val->p_list->pi_types = malloc( p_var->choices.i_count
589 p_val2->p_list->p_values =
590 malloc( p_var->choices.i_count * sizeof(vlc_value_t) );
591 p_val2->p_list->pi_types =
592 malloc( p_var->choices.i_count * sizeof(int) );
595 p_val->p_list->i_count = p_var->choices.i_count;
596 if( p_val2 ) p_val2->p_list->i_count = p_var->choices.i_count;
597 for( i = 0 ; i < p_var->choices.i_count ; i++ )
599 p_val->p_list->p_values[i] = p_var->choices.p_values[i];
600 p_val->p_list->pi_types[i] = p_var->i_type;
601 p_var->ops->pf_dup( &p_val->p_list->p_values[i] );
604 p_val2->p_list->p_values[i].psz_string =
605 p_var->choices_text.p_values[i].psz_string ?
606 strdup(p_var->choices_text.p_values[i].psz_string) : NULL;
607 p_val2->p_list->pi_types[i] = VLC_VAR_STRING;
611 case VLC_VAR_SETTEXT:
612 free( p_var->psz_text );
613 if( p_val && p_val->psz_string )
614 p_var->psz_text = strdup( p_val->psz_string );
616 p_var->psz_text = NULL;
618 case VLC_VAR_GETTEXT:
619 p_val->psz_string = NULL;
620 if( p_var->psz_text )
622 p_val->psz_string = strdup( p_var->psz_text );
625 case VLC_VAR_INHERITVALUE:
629 if( InheritValue( p_this,
630 p_val2 ? p_val2->psz_string : psz_name,
631 &val, p_var->i_type )
634 /* Duplicate already done */
636 /* Backup needed stuff */
638 /* Check boundaries and list */
639 CheckValue( p_var, &val );
640 /* Set the variable */
642 /* Free data if needed */
643 p_var->ops->pf_free( &oldval );
649 p_var->ops->pf_dup( p_val );
654 case VLC_VAR_SETISCOMMAND:
655 p_var->i_type |= VLC_VAR_ISCOMMAND;
662 vlc_mutex_unlock( &p_priv->var_lock );
669 * Perform a Get and Set on a variable
671 * \param p_this: The object that hold the variable
672 * \param psz_name: the name of the variable
673 * \param i_action: the action to perform
674 * \param p_val: The action parameter
675 * \return vlc error codes
677 int __var_GetAndSet( vlc_object_t *p_this, const char *psz_name, int i_action,
681 int i_ret = VLC_SUCCESS;
684 vlc_object_internals_t *p_priv = vlc_internals( p_this );
686 vlc_mutex_lock( &p_priv->var_lock );
687 i_var = GetUnused( p_this, psz_name );
690 vlc_mutex_unlock( &p_priv->var_lock );
694 p_var = &p_priv->p_vars[i_var];
696 /* Duplicated data if needed */
697 //p_var->ops->pf_dup( &val );
699 /* Backup needed stuff */
702 /* depending of the action requiered */
705 case VLC_VAR_TOGGLE_BOOL:
706 assert( ( p_var->i_type & VLC_VAR_BOOL ) == VLC_VAR_BOOL );
707 p_var->val.b_bool = !p_var->val.b_bool;
709 case VLC_VAR_INTEGER_INCDEC:
710 assert( ( p_var->i_type & VLC_VAR_INTEGER ) == VLC_VAR_INTEGER );
711 p_var->val.i_int += val.i_int;
714 vlc_mutex_unlock( &p_priv->var_lock );
718 /* Check boundaries */
719 CheckValue( p_var, &p_var->val );
721 /* Deal with callbacks.*/
722 if( p_var->i_entries )
723 i_ret = TriggerCallback( p_this, &p_var, psz_name, oldval );
725 vlc_mutex_unlock( &p_priv->var_lock );
732 * Request a variable's type
734 * \return The variable type if it exists, or 0 if the
735 * variable could not be found.
738 int __var_Type( vlc_object_t *p_this, const char *psz_name )
741 vlc_object_internals_t *p_priv = vlc_internals( p_this );
743 vlc_mutex_lock( &p_priv->var_lock );
745 i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
749 vlc_mutex_unlock( &p_priv->var_lock );
753 i_type = p_priv->p_vars[i_var].i_type;
755 vlc_mutex_unlock( &p_priv->var_lock );
760 int var_SetChecked( vlc_object_t *p_this, const char *psz_name,
761 int expected_type, vlc_value_t val )
764 int i_ret = VLC_SUCCESS;
767 vlc_object_internals_t *p_priv = vlc_internals( p_this );
769 vlc_mutex_lock( &p_priv->var_lock );
771 i_var = GetUnused( p_this, psz_name );
774 vlc_mutex_unlock( &p_priv->var_lock );
778 p_var = &p_priv->p_vars[i_var];
779 assert( (p_var->i_type & VLC_VAR_CLASS) == 0 || expected_type == 0 ||
780 (p_var->i_type & VLC_VAR_CLASS) == expected_type );
782 /* Duplicate data if needed */
783 p_var->ops->pf_dup( &val );
785 /* Backup needed stuff */
788 /* Check boundaries and list */
789 CheckValue( p_var, &val );
791 /* Set the variable */
794 /* Deal with callbacks */
795 if( p_var->i_entries )
796 i_ret = TriggerCallback( p_this, &p_var, psz_name, oldval );
798 /* Free data if needed */
799 p_var->ops->pf_free( &oldval );
801 vlc_mutex_unlock( &p_priv->var_lock );
808 * Set a variable's value
810 * \param p_this The object that hold the variable
811 * \param psz_name The name of the variable
812 * \param val the value to set
814 int __var_Set( vlc_object_t *p_this, const char *psz_name, vlc_value_t val )
816 return var_SetChecked( p_this, psz_name, 0, val );
819 int var_GetChecked( vlc_object_t *p_this, const char *psz_name,
820 int expected_type, vlc_value_t *p_val )
822 vlc_object_internals_t *p_priv = vlc_internals( p_this );
823 int i_var, err = VLC_SUCCESS;
825 vlc_mutex_lock( &p_priv->var_lock );
827 i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
830 variable_t *p_var = &p_priv->p_vars[i_var];
832 assert( (p_var->i_type & VLC_VAR_CLASS) == 0 || expected_type == 0 ||
833 (p_var->i_type & VLC_VAR_CLASS) == expected_type );
835 /* Really get the variable */
839 /* Alert if the type is VLC_VAR_VOID */
840 if( ( p_var->i_type & VLC_VAR_TYPE ) == VLC_VAR_VOID )
841 msg_Warn( p_this, "Calling var_GetVoid on the void variable '%s' (0x%04x)", psz_name, p_var->i_type );
844 /* Duplicate value if needed */
845 p_var->ops->pf_dup( p_val );
850 vlc_mutex_unlock( &p_priv->var_lock );
855 * Get a variable's value
857 * \param p_this The object that holds the variable
858 * \param psz_name The name of the variable
859 * \param p_val Pointer to a vlc_value_t that will hold the variable's value
860 * after the function is finished
862 int __var_Get( vlc_object_t *p_this, const char *psz_name, vlc_value_t *p_val )
864 return var_GetChecked( p_this, psz_name, 0, p_val );
868 * Register a callback in a variable
870 * We store a function pointer that will be called upon variable
873 * \param p_this The object that holds the variable
874 * \param psz_name The name of the variable
875 * \param pf_callback The function pointer
876 * \param p_data A generic pointer that will be passed as the last
877 * argument to the callback function.
879 * \warning The callback function is run in the thread that calls var_Set on
880 * the variable. Use proper locking. This thread may not have much
881 * time to spare, so keep callback functions short.
883 int __var_AddCallback( vlc_object_t *p_this, const char *psz_name,
884 vlc_callback_t pf_callback, void *p_data )
888 callback_entry_t entry;
889 vlc_object_internals_t *p_priv = vlc_internals( p_this );
891 entry.pf_callback = pf_callback;
892 entry.p_data = p_data;
894 vlc_mutex_lock( &p_priv->var_lock );
896 i_var = GetUnused( p_this, psz_name );
899 vlc_mutex_unlock( &p_priv->var_lock );
903 p_var = &p_priv->p_vars[i_var];
905 INSERT_ELEM( p_var->p_entries,
910 vlc_mutex_unlock( &p_priv->var_lock );
916 * Remove a callback from a variable
918 * pf_callback and p_data have to be given again, because different objects
919 * might have registered the same callback function.
921 int __var_DelCallback( vlc_object_t *p_this, const char *psz_name,
922 vlc_callback_t pf_callback, void *p_data )
926 vlc_object_internals_t *p_priv = vlc_internals( p_this );
928 vlc_mutex_lock( &p_priv->var_lock );
930 i_var = GetUnused( p_this, psz_name );
933 vlc_mutex_unlock( &p_priv->var_lock );
937 p_var = &p_priv->p_vars[i_var];
939 for( i_entry = p_var->i_entries ; i_entry-- ; )
941 if( p_var->p_entries[i_entry].pf_callback == pf_callback
942 && p_var->p_entries[i_entry].p_data == p_data )
947 else if( p_var->p_entries[i_entry].pf_callback == pf_callback )
949 msg_Warn( p_this, "Calling var_DelCallback for '%s' with the same "
950 "function but not the same data.", psz_name );
957 vlc_mutex_unlock( &p_priv->var_lock );
961 REMOVE_ELEM( p_var->p_entries, p_var->i_entries, i_entry );
963 vlc_mutex_unlock( &p_priv->var_lock );
969 * Trigger callback on a variable
971 * \param p_this The object that hold the variable
972 * \param psz_name The name of the variable
974 int __var_TriggerCallback( vlc_object_t *p_this, const char *psz_name )
977 int i_ret = VLC_SUCCESS;
979 vlc_object_internals_t *p_priv = vlc_internals( p_this );
981 vlc_mutex_lock( &p_priv->var_lock );
983 i_var = GetUnused( p_this, psz_name );
986 vlc_mutex_unlock( &p_priv->var_lock );
990 p_var = &p_priv->p_vars[i_var];
992 /* Deal with callbacks. Tell we're in a callback, release the lock,
993 * call stored functions, retake the lock. */
994 if( p_var->i_entries )
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 = 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 */
1150 /*****************************************************************************
1151 * GetUnused: find an unused (not in callback) variable from its name
1152 *****************************************************************************
1153 * We do i_tries tries before giving up, just in case the variable is being
1154 * modified and called from a callback.
1155 *****************************************************************************/
1156 static int GetUnused( vlc_object_t *p_this, const char *psz_name )
1158 vlc_object_internals_t *p_priv = vlc_internals( p_this );
1164 i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
1170 if( ! p_priv->p_vars[i_var].b_incallback )
1175 mutex_cleanup_push( &p_priv->var_lock );
1176 vlc_cond_wait( &p_priv->var_wait, &p_priv->var_lock );
1181 /*****************************************************************************
1182 * HashString: our cool hash function
1183 *****************************************************************************
1184 * This function is not intended to be crypto-secure, we only want it to be
1185 * fast and not suck too much. This one is pretty fast and did 0 collisions
1186 * in wenglish's dictionary.
1187 *****************************************************************************/
1188 static uint32_t HashString( const char *psz_string )
1190 uint32_t i_hash = 0;
1192 while( *psz_string )
1194 i_hash += *psz_string++;
1195 i_hash += i_hash << 10;
1196 i_hash ^= i_hash >> 8;
1202 /*****************************************************************************
1203 * Insert: find an empty slot to insert a new variable
1204 *****************************************************************************
1205 * We use a recursive inner function indexed on the hash. This function does
1206 * nothing in the rare cases where a collision may occur, see Lookup()
1207 * to see how we handle them.
1208 * XXX: does this really need to be written recursively?
1209 *****************************************************************************/
1210 static int Insert( variable_t *p_vars, int i_count, const char *psz_name )
1217 return InsertInner( p_vars, i_count, HashString( psz_name ) );
1220 static int InsertInner( variable_t *p_vars, int i_count, uint32_t i_hash )
1224 if( i_hash <= p_vars[0].i_hash )
1229 if( i_hash >= p_vars[i_count - 1].i_hash )
1234 i_middle = i_count / 2;
1236 /* We know that 0 < i_middle */
1237 if( i_hash < p_vars[i_middle].i_hash )
1239 return InsertInner( p_vars, i_middle, i_hash );
1242 /* We know that i_middle + 1 < i_count */
1243 if( i_hash > p_vars[i_middle + 1].i_hash )
1245 return i_middle + 1 + InsertInner( p_vars + i_middle + 1,
1246 i_count - i_middle - 1,
1250 return i_middle + 1;
1253 static int u32cmp( const void *key, const void *data )
1255 const variable_t *p_var = data;
1256 uint32_t hash = *(const uint32_t *)key ;
1258 if( hash > p_var->i_hash )
1260 if( hash < p_var->i_hash )
1265 /*****************************************************************************
1266 * Lookup: find an existing variable given its name
1267 *****************************************************************************
1268 * We use a recursive inner function indexed on the hash. Care is taken of
1269 * possible hash collisions.
1270 *****************************************************************************/
1271 static int Lookup( variable_t *p_vars, size_t i_count, const char *psz_name )
1276 i_hash = HashString( psz_name );
1277 p_var = bsearch( &i_hash, p_vars, i_count, sizeof( *p_var ), u32cmp );
1279 /* Hash not found */
1283 assert( i_count > 0 );
1285 /* Find the first entry with the right hash */
1286 while( (p_var > p_vars) && (i_hash == p_var[-1].i_hash) )
1289 assert( p_var->i_hash == i_hash );
1291 /* Hash collision should be very unlikely, but we cannot guarantee
1292 * it will never happen. So we do an exhaustive search amongst all
1293 * entries with the same hash. Typically, there is only one anyway. */
1294 for( variable_t *p_end = p_vars + i_count;
1295 (p_var < p_end) && (i_hash == p_var->i_hash);
1298 if( !strcmp( psz_name, p_var->psz_name ) )
1299 return p_var - p_vars;
1302 /* Hash found, but entry not found */
1306 /*****************************************************************************
1307 * CheckValue: check that a value is valid wrt. a variable
1308 *****************************************************************************
1309 * This function checks p_val's value against p_var's limitations such as
1310 * minimal and maximal value, step, in-list position, and modifies p_val if
1312 ****************************************************************************/
1313 static void CheckValue ( variable_t *p_var, vlc_value_t *p_val )
1315 /* Check that our variable is in the list */
1316 if( p_var->i_type & VLC_VAR_HASCHOICE && p_var->choices.i_count )
1320 /* This list is not sorted so go throug it (this is a small list) */
1321 for( i = p_var->choices.i_count ; i-- ; )
1323 if( p_var->ops->pf_cmp( *p_val, p_var->choices.p_values[i] ) == 0 )
1329 /* If not found, change it to anything vaguely valid */
1332 /* Free the old variable, get the new one, dup it */
1333 p_var->ops->pf_free( p_val );
1334 *p_val = p_var->choices.p_values[p_var->i_default >= 0
1335 ? p_var->i_default : 0 ];
1336 p_var->ops->pf_dup( p_val );
1340 /* Check that our variable is within the bounds */
1341 switch( p_var->i_type & VLC_VAR_TYPE )
1343 case VLC_VAR_INTEGER:
1344 if( p_var->i_type & VLC_VAR_HASSTEP && p_var->step.i_int
1345 && (p_val->i_int % p_var->step.i_int) )
1347 p_val->i_int = (p_val->i_int + (p_var->step.i_int / 2))
1348 / p_var->step.i_int * p_var->step.i_int;
1350 if( p_var->i_type & VLC_VAR_HASMIN
1351 && p_val->i_int < p_var->min.i_int )
1353 p_val->i_int = p_var->min.i_int;
1355 if( p_var->i_type & VLC_VAR_HASMAX
1356 && p_val->i_int > p_var->max.i_int )
1358 p_val->i_int = p_var->max.i_int;
1362 if( p_var->i_type & VLC_VAR_HASSTEP && p_var->step.f_float )
1364 float f_round = p_var->step.f_float * (float)(int)( 0.5 +
1365 p_val->f_float / p_var->step.f_float );
1366 if( p_val->f_float != f_round )
1368 p_val->f_float = f_round;
1371 if( p_var->i_type & VLC_VAR_HASMIN
1372 && p_val->f_float < p_var->min.f_float )
1374 p_val->f_float = p_var->min.f_float;
1376 if( p_var->i_type & VLC_VAR_HASMAX
1377 && p_val->f_float > p_var->max.f_float )
1379 p_val->f_float = p_var->max.f_float;
1388 /*****************************************************************************
1389 * InheritValue: try to inherit the value of this variable from the same one
1390 * in our closest parent, libvlc or ultimately from the configuration.
1391 * The function should always be entered with the object var_lock locked.
1392 *****************************************************************************/
1393 static int InheritValue( vlc_object_t *p_this, const char *psz_name,
1394 vlc_value_t *p_val, int i_type )
1399 if( p_this->p_parent || ( p_this->p_libvlc && p_this != (vlc_object_t*) p_this->p_libvlc ) )
1401 vlc_object_internals_t *p_priv;
1403 if( p_this->p_parent )
1404 p_priv = vlc_internals( p_this->p_parent );
1406 p_priv = vlc_internals( p_this->p_libvlc );
1408 i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
1413 p_var = &p_priv->p_vars[i_var];
1415 /* Really get the variable */
1416 *p_val = p_var->val;
1418 /* Duplicate value if needed */
1419 p_var->ops->pf_dup( p_val );
1421 /*msg_Dbg( p_this, "Inherited value for var %s from object %s",
1422 psz_name ? psz_name : "(null)",
1423 p_this->psz_object_name
1424 ? p_this->psz_object_name : "(Unknown)" );*/
1427 else if ( p_this->p_parent ) /* We are still not there */
1428 return InheritValue( p_this->p_parent, psz_name, p_val, i_type );
1430 /* else take value from config */
1434 switch( i_type & VLC_VAR_CLASS )
1436 case VLC_VAR_STRING:
1437 p_val->psz_string = config_GetPsz( p_this, psz_name );
1438 if( !p_val->psz_string ) p_val->psz_string = strdup("");
1441 p_val->f_float = config_GetFloat( p_this, psz_name );
1443 case VLC_VAR_INTEGER:
1444 p_val->i_int = config_GetInt( p_this, psz_name );
1447 p_val->b_bool = config_GetInt( p_this, psz_name );
1451 char *psz_orig, *psz_var;
1452 vlc_list_t *p_list = malloc(sizeof(vlc_list_t));
1453 p_val->p_list = p_list;
1454 p_list->i_count = 0;
1456 psz_var = psz_orig = config_GetPsz( p_this, psz_name );
1457 while( psz_var && *psz_var )
1459 char *psz_item = psz_var;
1461 while( *psz_var && *psz_var != ',' ) psz_var++;
1462 if( *psz_var == ',' )
1467 val.i_int = strtol( psz_item, NULL, 0 );
1468 INSERT_ELEM( p_list->p_values, p_list->i_count,
1469 p_list->i_count, val );
1470 /* p_list->i_count is incremented twice by INSERT_ELEM */
1472 INSERT_ELEM( p_list->pi_types, p_list->i_count,
1473 p_list->i_count, VLC_VAR_INTEGER );
1479 msg_Warn( p_this, "Could not inherit value for var %s "
1480 "from config. Invalid Type", psz_name );
1484 /*msg_Dbg( p_this, "Inherited value for var %s from config", psz_name );*/
1489 /**********************************************************************
1490 * Trigger the callbacks.
1491 * Tell we're in a callback, release the lock, call stored functions,
1493 **********************************************************************/
1494 static int TriggerCallback( vlc_object_t *p_this, variable_t **pp_var,
1495 const char *psz_name, vlc_value_t oldval )
1498 int i_entries = (*pp_var)->i_entries;
1499 callback_entry_t *p_entries = (*pp_var)->p_entries;
1500 vlc_object_internals_t *p_priv = vlc_internals( p_this );
1502 (*pp_var)->b_incallback = true;
1503 vlc_mutex_unlock( &p_priv->var_lock );
1505 /* The real calls */
1506 for( ; i_entries-- ; )
1508 p_entries[i_entries].pf_callback( p_this, psz_name, oldval, (*pp_var)->val,
1509 p_entries[i_entries].p_data );
1512 vlc_mutex_lock( &p_priv->var_lock );
1514 i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
1517 msg_Err( p_this, "variable %s has disappeared", psz_name );
1521 *pp_var = &p_priv->p_vars[i_var];
1522 (*pp_var)->b_incallback = false;
1523 vlc_cond_broadcast( &p_priv->var_wait );
1529 /**********************************************************************
1530 * Execute a var command on an object identified by its name
1531 **********************************************************************/
1532 int __var_Command( vlc_object_t *p_this, const char *psz_name,
1533 const char *psz_cmd, const char *psz_arg, char **psz_msg )
1535 vlc_object_t *p_obj = vlc_object_find_name( p_this->p_libvlc,
1536 psz_name, FIND_CHILD );
1542 *psz_msg = strdup( "Unknown destination object." );
1546 i_type = var_Type( p_obj, psz_cmd );
1547 if( !( i_type&VLC_VAR_ISCOMMAND ) )
1549 vlc_object_release( p_obj );
1551 *psz_msg = strdup( "Variable doesn't exist or isn't a command." );
1552 return VLC_EGENERIC;
1555 i_type &= VLC_VAR_CLASS;
1558 case VLC_VAR_INTEGER:
1559 i_ret = var_SetInteger( p_obj, psz_cmd, atoi( psz_arg ) );
1562 i_ret = var_SetFloat( p_obj, psz_cmd, atof( psz_arg ) );
1564 case VLC_VAR_STRING:
1565 i_ret = var_SetString( p_obj, psz_cmd, psz_arg );
1568 i_ret = var_SetBool( p_obj, psz_cmd, atoi( psz_arg ) );
1571 i_ret = VLC_EGENERIC;
1575 vlc_object_release( p_obj );
1579 if( asprintf( psz_msg, "%s on object %s returned %i (%s)",
1580 psz_cmd, psz_name, i_ret, vlc_error( i_ret ) ) == -1)
1589 * Free a list and the associated strings
1590 * @param p_val: the list variable
1591 * @param p_val2: the variable associated or NULL
1593 void var_FreeList( vlc_value_t *p_val, vlc_value_t *p_val2 )
1596 if( p_val2 && p_val2->p_list )
1598 for( int i = 0; i < p_val2->p_list->i_count; i++ )
1599 free( p_val2->p_list->p_values[i].psz_string );
1600 if( p_val2->p_list->i_count )
1602 free( p_val2->p_list->p_values );
1603 free( p_val2->p_list->pi_types );
1605 free( p_val2->p_list );