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 vlc_mutex_unlock( &p_priv->var_lock );
204 p_priv->p_vars[i_new].i_usage++;
205 p_priv->p_vars[i_new].i_type |= ( i_type & VLC_VAR_ISCOMMAND );
206 p_priv->p_vars[i_new].i_type |= ( i_type & VLC_VAR_HASCHOICE );
207 vlc_mutex_unlock( &p_priv->var_lock );
211 i_new = Insert( p_priv->p_vars, p_priv->i_vars, psz_name );
213 if( (p_priv->i_vars & 15) == 15 )
215 p_priv->p_vars = realloc( p_priv->p_vars,
216 (p_priv->i_vars+17) * sizeof(variable_t) );
219 memmove( p_priv->p_vars + i_new + 1,
220 p_priv->p_vars + i_new,
221 (p_priv->i_vars - i_new) * sizeof(variable_t) );
225 p_var = &p_priv->p_vars[i_new];
226 memset( p_var, 0, sizeof(*p_var) );
228 p_var->i_hash = HashString( psz_name );
229 p_var->psz_name = strdup( psz_name );
230 p_var->psz_text = NULL;
232 p_var->i_type = i_type & ~VLC_VAR_DOINHERIT;
236 p_var->i_default = -1;
237 p_var->choices.i_count = 0;
238 p_var->choices.p_values = NULL;
239 p_var->choices_text.i_count = 0;
240 p_var->choices_text.p_values = NULL;
242 p_var->b_incallback = false;
243 p_var->i_entries = 0;
244 p_var->p_entries = NULL;
246 /* Always initialize the variable, even if it is a list variable; this
247 * will lead to errors if the variable is not initialized, but it will
248 * not cause crashes in the variable handling. */
249 switch( i_type & VLC_VAR_CLASS )
252 p_var->ops = &bool_ops;
253 p_var->val.b_bool = false;
255 case VLC_VAR_INTEGER:
256 p_var->ops = &int_ops;
257 p_var->val.i_int = 0;
260 p_var->ops = &string_ops;
261 p_var->val.psz_string = NULL;
264 p_var->ops = &float_ops;
265 p_var->val.f_float = 0.0;
268 p_var->ops = &time_ops;
269 p_var->val.i_time = 0;
271 case VLC_VAR_ADDRESS:
272 p_var->ops = &addr_ops;
273 p_var->val.p_address = NULL;
276 p_var->ops = &mutex_ops;
277 p_var->val.p_address = malloc( sizeof(vlc_mutex_t) );
278 vlc_mutex_init( (vlc_mutex_t*)p_var->val.p_address );
281 p_var->ops = &list_ops;
282 p_var->val.p_list = &dummy_null_list;
285 p_var->ops = &void_ops;
289 /* Duplicate the default data we stored. */
290 p_var->ops->pf_dup( &p_var->val );
292 if( i_type & VLC_VAR_DOINHERIT )
296 if( InheritValue( p_this, psz_name, &val, p_var->i_type )
299 /* Free data if needed */
300 p_var->ops->pf_free( &p_var->val );
301 /* Set the variable */
304 if( i_type & VLC_VAR_HASCHOICE )
306 /* We must add the inherited value to our choice list */
307 p_var->i_default = 0;
309 INSERT_ELEM( p_var->choices.p_values, p_var->choices.i_count,
311 INSERT_ELEM( p_var->choices_text.p_values,
312 p_var->choices_text.i_count, 0, val );
313 p_var->ops->pf_dup( &p_var->choices.p_values[0] );
314 p_var->choices_text.p_values[0].psz_string = NULL;
319 vlc_mutex_unlock( &p_priv->var_lock );
325 * Destroy a vlc variable
327 * Look for the variable and destroy it if it is found. As in var_Create we
328 * do a call to memmove() but we have performance counterparts elsewhere.
330 * \param p_this The object that holds the variable
331 * \param psz_name The name of the variable
333 int __var_Destroy( vlc_object_t *p_this, const char *psz_name )
337 vlc_object_internals_t *p_priv = vlc_internals( p_this );
339 vlc_mutex_lock( &p_priv->var_lock );
341 i_var = GetUnused( p_this, psz_name );
344 vlc_mutex_unlock( &p_priv->var_lock );
348 p_var = &p_priv->p_vars[i_var];
350 if( p_var->i_usage > 1 )
353 vlc_mutex_unlock( &p_priv->var_lock );
357 /* Free value if needed */
358 p_var->ops->pf_free( &p_var->val );
360 /* Free choice list if needed */
361 if( p_var->choices.i_count )
363 for( i = 0 ; i < p_var->choices.i_count ; i++ )
365 p_var->ops->pf_free( &p_var->choices.p_values[i] );
366 free( p_var->choices_text.p_values[i].psz_string );
368 free( p_var->choices.p_values );
369 free( p_var->choices_text.p_values );
372 /* Free callbacks if needed */
373 if( p_var->p_entries )
375 free( p_var->p_entries );
378 free( p_var->psz_name );
379 free( p_var->psz_text );
381 memmove( p_priv->p_vars + i_var,
382 p_priv->p_vars + i_var + 1,
383 (p_priv->i_vars - i_var - 1) * sizeof(variable_t) );
385 if( (p_priv->i_vars & 15) == 0 )
387 p_priv->p_vars = realloc( p_priv->p_vars,
388 (p_priv->i_vars) * sizeof( variable_t ) );
393 vlc_mutex_unlock( &p_priv->var_lock );
399 * Perform an action on a variable
401 * \param p_this The object that holds the variable
402 * \param psz_name The name of the variable
403 * \param i_action The action to perform. Must be one of \ref var_action
404 * \param p_val First action parameter
405 * \param p_val2 Second action parameter
407 int __var_Change( vlc_object_t *p_this, const char *psz_name,
408 int i_action, vlc_value_t *p_val, vlc_value_t *p_val2 )
413 vlc_object_internals_t *p_priv = vlc_internals( p_this );
415 vlc_mutex_lock( &p_priv->var_lock );
417 i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
421 vlc_mutex_unlock( &p_priv->var_lock );
425 p_var = &p_priv->p_vars[i_var];
430 if( p_var->i_type & VLC_VAR_HASMIN )
432 p_var->ops->pf_free( &p_var->min );
434 p_var->i_type |= VLC_VAR_HASMIN;
436 p_var->ops->pf_dup( &p_var->min );
437 CheckValue( p_var, &p_var->val );
440 if( p_var->i_type & VLC_VAR_HASMIN )
446 if( p_var->i_type & VLC_VAR_HASMAX )
448 p_var->ops->pf_free( &p_var->max );
450 p_var->i_type |= VLC_VAR_HASMAX;
452 p_var->ops->pf_dup( &p_var->max );
453 CheckValue( p_var, &p_var->val );
456 if( p_var->i_type & VLC_VAR_HASMAX )
461 case VLC_VAR_SETSTEP:
462 if( p_var->i_type & VLC_VAR_HASSTEP )
464 p_var->ops->pf_free( &p_var->step );
466 p_var->i_type |= VLC_VAR_HASSTEP;
467 p_var->step = *p_val;
468 p_var->ops->pf_dup( &p_var->step );
469 CheckValue( p_var, &p_var->val );
471 case VLC_VAR_GETSTEP:
472 if( p_var->i_type & VLC_VAR_HASSTEP )
474 *p_val = p_var->step;
477 case VLC_VAR_ADDCHOICE:
478 i = p_var->choices.i_count;
480 INSERT_ELEM( p_var->choices.p_values, p_var->choices.i_count,
482 INSERT_ELEM( p_var->choices_text.p_values,
483 p_var->choices_text.i_count, i, *p_val );
484 p_var->ops->pf_dup( &p_var->choices.p_values[i] );
485 p_var->choices_text.p_values[i].psz_string =
486 ( p_val2 && p_val2->psz_string ) ?
487 strdup( p_val2->psz_string ) : NULL;
489 CheckValue( p_var, &p_var->val );
491 case VLC_VAR_DELCHOICE:
492 for( i = 0 ; i < p_var->choices.i_count ; i++ )
494 if( p_var->ops->pf_cmp( p_var->choices.p_values[i], *p_val ) == 0 )
500 if( i == p_var->choices.i_count )
503 vlc_mutex_unlock( &p_priv->var_lock );
507 if( p_var->i_default > i )
511 else if( p_var->i_default == i )
513 p_var->i_default = -1;
516 p_var->ops->pf_free( &p_var->choices.p_values[i] );
517 free( p_var->choices_text.p_values[i].psz_string );
518 REMOVE_ELEM( p_var->choices.p_values, p_var->choices.i_count, i );
519 REMOVE_ELEM( p_var->choices_text.p_values,
520 p_var->choices_text.i_count, i );
522 CheckValue( p_var, &p_var->val );
524 case VLC_VAR_CHOICESCOUNT:
525 p_val->i_int = p_var->choices.i_count;
527 case VLC_VAR_CLEARCHOICES:
528 for( i = 0 ; i < p_var->choices.i_count ; i++ )
530 p_var->ops->pf_free( &p_var->choices.p_values[i] );
532 for( i = 0 ; i < p_var->choices_text.i_count ; i++ )
533 free( p_var->choices_text.p_values[i].psz_string );
535 if( p_var->choices.i_count ) free( p_var->choices.p_values );
536 if( p_var->choices_text.i_count ) free( p_var->choices_text.p_values );
538 p_var->choices.i_count = 0;
539 p_var->choices.p_values = NULL;
540 p_var->choices_text.i_count = 0;
541 p_var->choices_text.p_values = NULL;
542 p_var->i_default = -1;
544 case VLC_VAR_SETDEFAULT:
545 /* FIXME: the list is sorted, dude. Use something cleverer. */
546 for( i = 0 ; i < p_var->choices.i_count ; i++ )
548 if( p_var->ops->pf_cmp( p_var->choices.p_values[i], *p_val ) == 0 )
554 if( i == p_var->choices.i_count )
560 p_var->i_default = i;
561 CheckValue( p_var, &p_var->val );
563 case VLC_VAR_SETVALUE:
564 /* Duplicate data if needed */
565 p_var->ops->pf_dup( p_val );
566 /* Backup needed stuff */
568 /* Check boundaries and list */
569 CheckValue( p_var, p_val );
570 /* Set the variable */
572 /* Free data if needed */
573 p_var->ops->pf_free( &oldval );
575 case VLC_VAR_GETCHOICES:
576 case VLC_VAR_GETLIST:
577 p_val->p_list = malloc( sizeof(vlc_list_t) );
578 if( p_val2 ) p_val2->p_list = malloc( sizeof(vlc_list_t) );
579 if( p_var->choices.i_count )
581 p_val->p_list->p_values = malloc( p_var->choices.i_count
582 * sizeof(vlc_value_t) );
583 p_val->p_list->pi_types = malloc( p_var->choices.i_count
587 p_val2->p_list->p_values =
588 malloc( p_var->choices.i_count * sizeof(vlc_value_t) );
589 p_val2->p_list->pi_types =
590 malloc( p_var->choices.i_count * sizeof(int) );
593 p_val->p_list->i_count = p_var->choices.i_count;
594 if( p_val2 ) p_val2->p_list->i_count = p_var->choices.i_count;
595 for( i = 0 ; i < p_var->choices.i_count ; i++ )
597 p_val->p_list->p_values[i] = p_var->choices.p_values[i];
598 p_val->p_list->pi_types[i] = p_var->i_type;
599 p_var->ops->pf_dup( &p_val->p_list->p_values[i] );
602 p_val2->p_list->p_values[i].psz_string =
603 p_var->choices_text.p_values[i].psz_string ?
604 strdup(p_var->choices_text.p_values[i].psz_string) : NULL;
605 p_val2->p_list->pi_types[i] = VLC_VAR_STRING;
609 case VLC_VAR_SETTEXT:
610 free( p_var->psz_text );
611 if( p_val && p_val->psz_string )
612 p_var->psz_text = strdup( p_val->psz_string );
614 p_var->psz_text = NULL;
616 case VLC_VAR_GETTEXT:
617 p_val->psz_string = NULL;
618 if( p_var->psz_text )
620 p_val->psz_string = strdup( p_var->psz_text );
623 case VLC_VAR_INHERITVALUE:
627 if( InheritValue( p_this,
628 p_val2 ? p_val2->psz_string : psz_name,
629 &val, p_var->i_type )
632 /* Duplicate already done */
634 /* Backup needed stuff */
636 /* Check boundaries and list */
637 CheckValue( p_var, &val );
638 /* Set the variable */
640 /* Free data if needed */
641 p_var->ops->pf_free( &oldval );
647 p_var->ops->pf_dup( p_val );
652 case VLC_VAR_SETISCOMMAND:
653 p_var->i_type |= VLC_VAR_ISCOMMAND;
660 vlc_mutex_unlock( &p_priv->var_lock );
667 * Perform a Get and Set on a variable
669 * \param p_this: The object that hold the variable
670 * \param psz_name: the name of the variable
671 * \param i_action: the action to perform
672 * \param p_val: The action parameter
673 * \return vlc error codes
675 int __var_GetAndSet( vlc_object_t *p_this, const char *psz_name, int i_action,
679 int i_ret = VLC_SUCCESS;
682 vlc_object_internals_t *p_priv = vlc_internals( p_this );
684 vlc_mutex_lock( &p_priv->var_lock );
685 i_var = GetUnused( p_this, psz_name );
688 vlc_mutex_unlock( &p_priv->var_lock );
692 p_var = &p_priv->p_vars[i_var];
694 /* Duplicated data if needed */
695 //p_var->ops->pf_dup( &val );
697 /* Backup needed stuff */
700 /* depending of the action requiered */
703 case VLC_VAR_TOGGLE_BOOL:
704 assert( ( p_var->i_type & VLC_VAR_BOOL ) == VLC_VAR_BOOL );
705 p_var->val.b_bool = !p_var->val.b_bool;
708 vlc_mutex_unlock( &p_priv->var_lock );
712 /* Check boundaries */
713 CheckValue( p_var, &p_var->val );
715 /* Deal with callbacks.*/
716 if( p_var->i_entries )
717 i_ret = TriggerCallback( p_this, p_var, psz_name, oldval );
719 vlc_mutex_unlock( &p_priv->var_lock );
726 * Request a variable's type
728 * \return The variable type if it exists, or 0 if the
729 * variable could not be found.
732 int __var_Type( vlc_object_t *p_this, const char *psz_name )
735 vlc_object_internals_t *p_priv = vlc_internals( p_this );
737 vlc_mutex_lock( &p_priv->var_lock );
739 i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
743 vlc_mutex_unlock( &p_priv->var_lock );
747 i_type = p_priv->p_vars[i_var].i_type;
749 vlc_mutex_unlock( &p_priv->var_lock );
754 int var_SetChecked( vlc_object_t *p_this, const char *psz_name,
755 int expected_type, vlc_value_t val )
758 int i_ret = VLC_SUCCESS;
761 vlc_object_internals_t *p_priv = vlc_internals( p_this );
763 vlc_mutex_lock( &p_priv->var_lock );
765 i_var = GetUnused( p_this, psz_name );
768 vlc_mutex_unlock( &p_priv->var_lock );
772 p_var = &p_priv->p_vars[i_var];
773 assert( (p_var->i_type & VLC_VAR_CLASS) == 0 || expected_type == 0 ||
774 (p_var->i_type & VLC_VAR_CLASS) == expected_type );
776 /* Duplicate data if needed */
777 p_var->ops->pf_dup( &val );
779 /* Backup needed stuff */
782 /* Check boundaries and list */
783 CheckValue( p_var, &val );
785 /* Set the variable */
788 /* Deal with callbacks */
789 if( p_var->i_entries )
790 i_ret = TriggerCallback( p_this, p_var, psz_name, oldval );
792 /* Free data if needed */
793 p_var->ops->pf_free( &oldval );
795 vlc_mutex_unlock( &p_priv->var_lock );
802 * Set a variable's value
804 * \param p_this The object that hold the variable
805 * \param psz_name The name of the variable
806 * \param val the value to set
808 int __var_Set( vlc_object_t *p_this, const char *psz_name, vlc_value_t val )
810 return var_SetChecked( p_this, psz_name, 0, val );
813 int var_GetChecked( vlc_object_t *p_this, const char *psz_name,
814 int expected_type, vlc_value_t *p_val )
816 vlc_object_internals_t *p_priv = vlc_internals( p_this );
817 int i_var, err = VLC_SUCCESS;
819 vlc_mutex_lock( &p_priv->var_lock );
821 i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
824 variable_t *p_var = &p_priv->p_vars[i_var];
826 assert( (p_var->i_type & VLC_VAR_CLASS) == 0 || expected_type == 0 ||
827 (p_var->i_type & VLC_VAR_CLASS) == expected_type );
829 /* Really get the variable */
832 /* Duplicate value if needed */
833 p_var->ops->pf_dup( p_val );
838 vlc_mutex_unlock( &p_priv->var_lock );
843 * Get a variable's value
845 * \param p_this The object that holds the variable
846 * \param psz_name The name of the variable
847 * \param p_val Pointer to a vlc_value_t that will hold the variable's value
848 * after the function is finished
850 int __var_Get( vlc_object_t *p_this, const char *psz_name, vlc_value_t *p_val )
852 return var_GetChecked( p_this, psz_name, 0, p_val );
856 * Register a callback in a variable
858 * We store a function pointer that will be called upon variable
861 * \param p_this The object that holds the variable
862 * \param psz_name The name of the variable
863 * \param pf_callback The function pointer
864 * \param p_data A generic pointer that will be passed as the last
865 * argument to the callback function.
867 * \warning The callback function is run in the thread that calls var_Set on
868 * the variable. Use proper locking. This thread may not have much
869 * time to spare, so keep callback functions short.
871 int __var_AddCallback( vlc_object_t *p_this, const char *psz_name,
872 vlc_callback_t pf_callback, void *p_data )
876 callback_entry_t entry;
877 vlc_object_internals_t *p_priv = vlc_internals( p_this );
879 entry.pf_callback = pf_callback;
880 entry.p_data = p_data;
882 vlc_mutex_lock( &p_priv->var_lock );
884 i_var = GetUnused( p_this, psz_name );
887 vlc_mutex_unlock( &p_priv->var_lock );
891 p_var = &p_priv->p_vars[i_var];
893 INSERT_ELEM( p_var->p_entries,
898 vlc_mutex_unlock( &p_priv->var_lock );
904 * Remove a callback from a variable
906 * pf_callback and p_data have to be given again, because different objects
907 * might have registered the same callback function.
909 int __var_DelCallback( vlc_object_t *p_this, const char *psz_name,
910 vlc_callback_t pf_callback, void *p_data )
914 vlc_object_internals_t *p_priv = vlc_internals( p_this );
916 vlc_mutex_lock( &p_priv->var_lock );
918 i_var = GetUnused( p_this, psz_name );
921 vlc_mutex_unlock( &p_priv->var_lock );
925 p_var = &p_priv->p_vars[i_var];
927 for( i_entry = p_var->i_entries ; i_entry-- ; )
929 if( p_var->p_entries[i_entry].pf_callback == pf_callback
930 && p_var->p_entries[i_entry].p_data == p_data )
938 vlc_mutex_unlock( &p_priv->var_lock );
942 REMOVE_ELEM( p_var->p_entries, p_var->i_entries, i_entry );
944 vlc_mutex_unlock( &p_priv->var_lock );
950 * Trigger callback on a variable
952 * \param p_this The object that hold the variable
953 * \param psz_name The name of the variable
955 int __var_TriggerCallback( vlc_object_t *p_this, const char *psz_name )
958 int i_ret = VLC_SUCCESS;
960 vlc_object_internals_t *p_priv = vlc_internals( p_this );
962 vlc_mutex_lock( &p_priv->var_lock );
964 i_var = GetUnused( p_this, psz_name );
967 vlc_mutex_unlock( &p_priv->var_lock );
971 p_var = &p_priv->p_vars[i_var];
973 /* Deal with callbacks. Tell we're in a callback, release the lock,
974 * call stored functions, retake the lock. */
975 if( p_var->i_entries )
976 i_ret = TriggerCallback( p_this, p_var, psz_name, p_var->val );
978 vlc_mutex_unlock( &p_priv->var_lock );
982 /** Parse a stringified option
983 * This function parse a string option and create the associated object
985 * The option must be of the form "[no[-]]foo[=bar]" where foo is the
986 * option name and bar is the value of the option.
987 * \param p_obj the object in which the variable must be created
988 * \param psz_option the option to parse
989 * \param trusted whether the option is set by a trusted input or not
992 void var_OptionParse( vlc_object_t *p_obj, const char *psz_option,
995 char *psz_name, *psz_value;
1000 val.psz_string = NULL;
1002 /* It's too much of a hassle to remove the ':' when we parse
1003 * the cmd line :) */
1004 if( psz_option[0] == ':' )
1007 if( !psz_option[0] )
1010 psz_name = strdup( psz_option );
1011 if( psz_name == NULL )
1014 psz_value = strchr( psz_name, '=' );
1015 if( psz_value != NULL )
1016 *psz_value++ = '\0';
1018 /* FIXME: :programs should be handled generically */
1019 if( !strcmp( psz_name, "programs" ) )
1020 i_type = VLC_VAR_LIST;
1022 i_type = config_GetType( p_obj, psz_name );
1024 if( !i_type && !psz_value )
1026 /* check for "no-foo" or "nofoo" */
1027 if( !strncmp( psz_name, "no-", 3 ) )
1029 memmove( psz_name, psz_name + 3, strlen(psz_name) + 1 - 3 );
1031 else if( !strncmp( psz_name, "no", 2 ) )
1033 memmove( psz_name, psz_name + 2, strlen(psz_name) + 1 - 2 );
1035 else goto cleanup; /* Option doesn't exist */
1038 i_type = config_GetType( p_obj, psz_name );
1040 if( !i_type ) goto cleanup; /* Option doesn't exist */
1042 if( ( i_type != VLC_VAR_BOOL ) &&
1043 ( !psz_value || !*psz_value ) ) goto cleanup; /* Invalid value */
1045 /* check if option is unsafe */
1048 module_config_t *p_config = config_FindConfig( p_obj, psz_name );
1049 if( !p_config || !p_config->b_safe )
1051 msg_Err( p_obj, "unsafe option \"%s\" has been ignored for "
1052 "security reasons", psz_name );
1058 /* Create the variable in the input object.
1059 * Children of the input object will be able to retreive this value
1060 * thanks to the inheritance property of the object variables. */
1061 var_Create( p_obj, psz_name, i_type );
1066 val.b_bool = !b_isno;
1069 case VLC_VAR_INTEGER:
1070 val.i_int = strtol( psz_value, NULL, 0 );
1074 val.f_float = atof( psz_value );
1077 case VLC_VAR_STRING:
1078 case VLC_VAR_MODULE:
1080 case VLC_VAR_DIRECTORY:
1081 val.psz_string = psz_value;
1086 char *psz_orig, *psz_var;
1087 vlc_list_t *p_list = malloc(sizeof(vlc_list_t));
1088 val.p_list = p_list;
1089 p_list->i_count = 0;
1091 psz_var = psz_orig = strdup(psz_value);
1092 while( psz_var && *psz_var )
1094 char *psz_item = psz_var;
1096 while( *psz_var && *psz_var != ',' ) psz_var++;
1097 if( *psz_var == ',' )
1102 val2.i_int = strtol( psz_item, NULL, 0 );
1103 INSERT_ELEM( p_list->p_values, p_list->i_count,
1104 p_list->i_count, val2 );
1105 /* p_list->i_count is incremented twice by INSERT_ELEM */
1107 INSERT_ELEM( p_list->pi_types, p_list->i_count,
1108 p_list->i_count, VLC_VAR_INTEGER );
1118 var_Set( p_obj, psz_name, val );
1120 /* If that's a list, remove all elements allocated */
1121 if( i_type == VLC_VAR_LIST )
1129 /* Following functions are local */
1131 /*****************************************************************************
1132 * GetUnused: find an unused (not in callback) variable from its name
1133 *****************************************************************************
1134 * We do i_tries tries before giving up, just in case the variable is being
1135 * modified and called from a callback.
1136 *****************************************************************************/
1137 static int GetUnused( vlc_object_t *p_this, const char *psz_name )
1139 vlc_object_internals_t *p_priv = vlc_internals( p_this );
1145 i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
1151 if( ! p_priv->p_vars[i_var].b_incallback )
1156 mutex_cleanup_push( &p_priv->var_lock );
1157 vlc_cond_wait( &p_priv->var_wait, &p_priv->var_lock );
1162 /*****************************************************************************
1163 * HashString: our cool hash function
1164 *****************************************************************************
1165 * This function is not intended to be crypto-secure, we only want it to be
1166 * fast and not suck too much. This one is pretty fast and did 0 collisions
1167 * in wenglish's dictionary.
1168 *****************************************************************************/
1169 static uint32_t HashString( const char *psz_string )
1171 uint32_t i_hash = 0;
1173 while( *psz_string )
1175 i_hash += *psz_string++;
1176 i_hash += i_hash << 10;
1177 i_hash ^= i_hash >> 8;
1183 /*****************************************************************************
1184 * Insert: find an empty slot to insert a new variable
1185 *****************************************************************************
1186 * We use a recursive inner function indexed on the hash. This function does
1187 * nothing in the rare cases where a collision may occur, see Lookup()
1188 * to see how we handle them.
1189 * XXX: does this really need to be written recursively?
1190 *****************************************************************************/
1191 static int Insert( variable_t *p_vars, int i_count, const char *psz_name )
1198 return InsertInner( p_vars, i_count, HashString( psz_name ) );
1201 static int InsertInner( variable_t *p_vars, int i_count, uint32_t i_hash )
1205 if( i_hash <= p_vars[0].i_hash )
1210 if( i_hash >= p_vars[i_count - 1].i_hash )
1215 i_middle = i_count / 2;
1217 /* We know that 0 < i_middle */
1218 if( i_hash < p_vars[i_middle].i_hash )
1220 return InsertInner( p_vars, i_middle, i_hash );
1223 /* We know that i_middle + 1 < i_count */
1224 if( i_hash > p_vars[i_middle + 1].i_hash )
1226 return i_middle + 1 + InsertInner( p_vars + i_middle + 1,
1227 i_count - i_middle - 1,
1231 return i_middle + 1;
1234 static int u32cmp( const void *key, const void *data )
1236 const variable_t *p_var = data;
1237 uint32_t hash = *(const uint32_t *)key ;
1239 if( hash > p_var->i_hash )
1241 if( hash < p_var->i_hash )
1246 /*****************************************************************************
1247 * Lookup: find an existing variable given its name
1248 *****************************************************************************
1249 * We use a recursive inner function indexed on the hash. Care is taken of
1250 * possible hash collisions.
1251 *****************************************************************************/
1252 static int Lookup( variable_t *p_vars, size_t i_count, const char *psz_name )
1257 i_hash = HashString( psz_name );
1258 p_var = bsearch( &i_hash, p_vars, i_count, sizeof( *p_var ), u32cmp );
1260 /* Hash not found */
1264 assert( i_count > 0 );
1266 /* Find the first entry with the right hash */
1267 while( (p_var > p_vars) && (i_hash == p_var[-1].i_hash) )
1270 assert( p_var->i_hash == i_hash );
1272 /* Hash collision should be very unlikely, but we cannot guarantee
1273 * it will never happen. So we do an exhaustive search amongst all
1274 * entries with the same hash. Typically, there is only one anyway. */
1275 for( variable_t *p_end = p_vars + i_count;
1276 (p_var < p_end) && (i_hash == p_var->i_hash);
1279 if( !strcmp( psz_name, p_var->psz_name ) )
1280 return p_var - p_vars;
1283 /* Hash found, but entry not found */
1287 /*****************************************************************************
1288 * CheckValue: check that a value is valid wrt. a variable
1289 *****************************************************************************
1290 * This function checks p_val's value against p_var's limitations such as
1291 * minimal and maximal value, step, in-list position, and modifies p_val if
1293 ****************************************************************************/
1294 static void CheckValue ( variable_t *p_var, vlc_value_t *p_val )
1296 /* Check that our variable is in the list */
1297 if( p_var->i_type & VLC_VAR_HASCHOICE && p_var->choices.i_count )
1301 /* FIXME: the list is sorted, dude. Use something cleverer. */
1302 for( i = p_var->choices.i_count ; i-- ; )
1304 if( p_var->ops->pf_cmp( *p_val, p_var->choices.p_values[i] ) == 0 )
1310 /* If not found, change it to anything vaguely valid */
1313 /* Free the old variable, get the new one, dup it */
1314 p_var->ops->pf_free( p_val );
1315 *p_val = p_var->choices.p_values[p_var->i_default >= 0
1316 ? p_var->i_default : 0 ];
1317 p_var->ops->pf_dup( p_val );
1321 /* Check that our variable is within the bounds */
1322 switch( p_var->i_type & VLC_VAR_TYPE )
1324 case VLC_VAR_INTEGER:
1325 if( p_var->i_type & VLC_VAR_HASSTEP && p_var->step.i_int
1326 && (p_val->i_int % p_var->step.i_int) )
1328 p_val->i_int = (p_val->i_int + (p_var->step.i_int / 2))
1329 / p_var->step.i_int * p_var->step.i_int;
1331 if( p_var->i_type & VLC_VAR_HASMIN
1332 && p_val->i_int < p_var->min.i_int )
1334 p_val->i_int = p_var->min.i_int;
1336 if( p_var->i_type & VLC_VAR_HASMAX
1337 && p_val->i_int > p_var->max.i_int )
1339 p_val->i_int = p_var->max.i_int;
1343 if( p_var->i_type & VLC_VAR_HASSTEP && p_var->step.f_float )
1345 float f_round = p_var->step.f_float * (float)(int)( 0.5 +
1346 p_val->f_float / p_var->step.f_float );
1347 if( p_val->f_float != f_round )
1349 p_val->f_float = f_round;
1352 if( p_var->i_type & VLC_VAR_HASMIN
1353 && p_val->f_float < p_var->min.f_float )
1355 p_val->f_float = p_var->min.f_float;
1357 if( p_var->i_type & VLC_VAR_HASMAX
1358 && p_val->f_float > p_var->max.f_float )
1360 p_val->f_float = p_var->max.f_float;
1369 /*****************************************************************************
1370 * InheritValue: try to inherit the value of this variable from the same one
1371 * in our closest parent, libvlc or ultimately from the configuration.
1372 * The function should always be entered with the object var_lock locked.
1373 *****************************************************************************/
1374 static int InheritValue( vlc_object_t *p_this, const char *psz_name,
1375 vlc_value_t *p_val, int i_type )
1380 if( p_this->p_parent || ( p_this->p_libvlc && p_this != (vlc_object_t*) p_this->p_libvlc ) )
1382 vlc_object_internals_t *p_priv;
1384 if( p_this->p_parent )
1385 p_priv = vlc_internals( p_this->p_parent );
1387 p_priv = vlc_internals( p_this->p_libvlc );
1389 i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
1394 p_var = &p_priv->p_vars[i_var];
1396 /* Really get the variable */
1397 *p_val = p_var->val;
1399 /* Duplicate value if needed */
1400 p_var->ops->pf_dup( p_val );
1402 /*msg_Dbg( p_this, "Inherited value for var %s from object %s",
1403 psz_name ? psz_name : "(null)",
1404 p_this->psz_object_name
1405 ? p_this->psz_object_name : "(Unknown)" );*/
1408 else if ( p_this->p_parent ) /* We are still not there */
1409 return InheritValue( p_this->p_parent, psz_name, p_val, i_type );
1411 /* else take value from config */
1415 switch( i_type & VLC_VAR_CLASS )
1417 case VLC_VAR_STRING:
1418 p_val->psz_string = config_GetPsz( p_this, psz_name );
1419 if( !p_val->psz_string ) p_val->psz_string = strdup("");
1422 p_val->f_float = config_GetFloat( p_this, psz_name );
1424 case VLC_VAR_INTEGER:
1425 p_val->i_int = config_GetInt( p_this, psz_name );
1428 p_val->b_bool = config_GetInt( p_this, psz_name );
1432 char *psz_orig, *psz_var;
1433 vlc_list_t *p_list = malloc(sizeof(vlc_list_t));
1434 p_val->p_list = p_list;
1435 p_list->i_count = 0;
1437 psz_var = psz_orig = config_GetPsz( p_this, psz_name );
1438 while( psz_var && *psz_var )
1440 char *psz_item = psz_var;
1442 while( *psz_var && *psz_var != ',' ) psz_var++;
1443 if( *psz_var == ',' )
1448 val.i_int = strtol( psz_item, NULL, 0 );
1449 INSERT_ELEM( p_list->p_values, p_list->i_count,
1450 p_list->i_count, val );
1451 /* p_list->i_count is incremented twice by INSERT_ELEM */
1453 INSERT_ELEM( p_list->pi_types, p_list->i_count,
1454 p_list->i_count, VLC_VAR_INTEGER );
1460 msg_Warn( p_this, "Could not inherit value for var %s "
1461 "from config. Invalid Type", psz_name );
1465 /*msg_Dbg( p_this, "Inherited value for var %s from config", psz_name );*/
1470 /**********************************************************************
1471 * Trigger the callbacks.
1472 * Tell we're in a callback, release the lock, call stored functions,
1474 **********************************************************************/
1475 static int TriggerCallback( vlc_object_t *p_this, variable_t *p_var,
1476 const char *psz_name, vlc_value_t oldval )
1479 int i_entries = p_var->i_entries;
1480 callback_entry_t *p_entries = p_var->p_entries;
1481 vlc_object_internals_t *p_priv = vlc_internals( p_this );
1483 p_var->b_incallback = true;
1484 vlc_mutex_unlock( &p_priv->var_lock );
1486 /* The real calls */
1487 for( ; i_entries-- ; )
1489 p_entries[i_entries].pf_callback( p_this, psz_name, oldval, p_var->val,
1490 p_entries[i_entries].p_data );
1493 vlc_mutex_lock( &p_priv->var_lock );
1495 i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
1498 msg_Err( p_this, "variable %s has disappeared", psz_name );
1502 p_var = &p_priv->p_vars[i_var];
1503 p_var->b_incallback = false;
1504 vlc_cond_broadcast( &p_priv->var_wait );
1510 /**********************************************************************
1511 * Execute a var command on an object identified by its name
1512 **********************************************************************/
1513 int __var_Command( vlc_object_t *p_this, const char *psz_name,
1514 const char *psz_cmd, const char *psz_arg, char **psz_msg )
1516 vlc_object_t *p_obj = vlc_object_find_name( p_this->p_libvlc,
1517 psz_name, FIND_CHILD );
1523 *psz_msg = strdup( "Unknown destination object." );
1527 i_type = var_Type( p_obj, psz_cmd );
1528 if( !( i_type&VLC_VAR_ISCOMMAND ) )
1530 vlc_object_release( p_obj );
1532 *psz_msg = strdup( "Variable doesn't exist or isn't a command." );
1533 return VLC_EGENERIC;
1536 i_type &= VLC_VAR_CLASS;
1539 case VLC_VAR_INTEGER:
1540 i_ret = var_SetInteger( p_obj, psz_cmd, atoi( psz_arg ) );
1543 i_ret = var_SetFloat( p_obj, psz_cmd, atof( psz_arg ) );
1545 case VLC_VAR_STRING:
1546 i_ret = var_SetString( p_obj, psz_cmd, psz_arg );
1549 i_ret = var_SetBool( p_obj, psz_cmd, atoi( psz_arg ) );
1552 i_ret = VLC_EGENERIC;
1556 vlc_object_release( p_obj );
1560 if( asprintf( psz_msg, "%s on object %s returned %i (%s)",
1561 psz_cmd, psz_name, i_ret, vlc_error( i_ret ) ) == -1)
1570 * Free a list and the associated strings
1571 * @param p_val: the list variable
1572 * @param p_val2: the variable associated or NULL
1574 void var_FreeList( vlc_value_t *p_val, vlc_value_t *p_val2 )
1577 if( p_val2 && p_val2->p_list )
1579 for( int i = 0; i < p_val2->p_list->i_count; i++ )
1580 free( p_val2->p_list->p_values[i].psz_string );
1581 if( p_val2->p_list->i_count )
1583 free( p_val2->p_list->p_values );
1584 free( p_val2->p_list->pi_types );
1586 free( p_val2->p_list );