1 /*****************************************************************************
2 * variables.c: routines for object variables handling
3 *****************************************************************************
4 * Copyright (C) 2002-2006 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 *,
167 * Initialize a vlc variable
169 * We hash the given string and insert it into the sorted list. The insertion
170 * may require slow memory copies, but think about what we gain in the log(n)
171 * lookup phase when setting/getting the variable value!
173 * \param p_this The object in which to create the variable
174 * \param psz_name The name of the variable
175 * \param i_type The variables type. Must be one of \ref var_type combined with
176 * zero or more \ref var_flags
178 int __var_Create( vlc_object_t *p_this, const char *psz_name, int i_type )
182 static vlc_list_t dummy_null_list = {0, NULL, NULL};
183 vlc_object_internals_t *p_priv = vlc_internals( p_this );
185 vlc_mutex_lock( &p_priv->var_lock );
187 /* FIXME: if the variable already exists, we don't duplicate it. But we
188 * duplicate the lookups. It's not that serious, but if anyone finds some
189 * time to rework Insert() so that only one lookup has to be done, feel
191 i_new = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
195 /* If the types differ, variable creation failed. */
196 if( (i_type & ~(VLC_VAR_DOINHERIT|VLC_VAR_ISCOMMAND)) != p_priv->p_vars[i_new].i_type )
198 vlc_mutex_unlock( &p_priv->var_lock );
202 p_priv->p_vars[i_new].i_usage++;
203 if( i_type & VLC_VAR_ISCOMMAND )
204 p_priv->p_vars[i_new].i_type |= VLC_VAR_ISCOMMAND;
205 vlc_mutex_unlock( &p_priv->var_lock );
209 i_new = Insert( p_priv->p_vars, p_priv->i_vars, psz_name );
211 if( (p_priv->i_vars & 15) == 15 )
213 p_priv->p_vars = realloc( p_priv->p_vars,
214 (p_priv->i_vars+17) * sizeof(variable_t) );
217 memmove( p_priv->p_vars + i_new + 1,
218 p_priv->p_vars + i_new,
219 (p_priv->i_vars - i_new) * sizeof(variable_t) );
223 p_var = &p_priv->p_vars[i_new];
224 memset( p_var, 0, sizeof(*p_var) );
226 p_var->i_hash = HashString( psz_name );
227 p_var->psz_name = strdup( psz_name );
228 p_var->psz_text = NULL;
230 p_var->i_type = i_type & ~VLC_VAR_DOINHERIT;
231 memset( &p_var->val, 0, sizeof(vlc_value_t) );
235 p_var->i_default = -1;
236 p_var->choices.i_count = 0;
237 p_var->choices.p_values = NULL;
238 p_var->choices_text.i_count = 0;
239 p_var->choices_text.p_values = NULL;
241 p_var->b_incallback = false;
242 p_var->i_entries = 0;
243 p_var->p_entries = NULL;
245 /* Always initialize the variable, even if it is a list variable; this
246 * will lead to errors if the variable is not initialized, but it will
247 * not cause crashes in the variable handling. */
248 switch( i_type & VLC_VAR_CLASS )
251 p_var->ops = &bool_ops;
252 p_var->val.b_bool = false;
254 case VLC_VAR_INTEGER:
255 p_var->ops = &int_ops;
256 p_var->val.i_int = 0;
259 p_var->ops = &string_ops;
260 p_var->val.psz_string = NULL;
263 p_var->ops = &float_ops;
264 p_var->val.f_float = 0.0;
267 p_var->ops = &time_ops;
268 p_var->val.i_time = 0;
270 case VLC_VAR_ADDRESS:
271 p_var->ops = &addr_ops;
272 p_var->val.p_address = NULL;
275 p_var->ops = &mutex_ops;
276 p_var->val.p_address = malloc( sizeof(vlc_mutex_t) );
277 vlc_mutex_init( (vlc_mutex_t*)p_var->val.p_address );
280 p_var->ops = &list_ops;
281 p_var->val.p_list = &dummy_null_list;
284 p_var->ops = &void_ops;
288 /* Duplicate the default data we stored. */
289 p_var->ops->pf_dup( &p_var->val );
291 if( i_type & VLC_VAR_DOINHERIT )
295 if( InheritValue( p_this, psz_name, &val, p_var->i_type )
298 /* Free data if needed */
299 p_var->ops->pf_free( &p_var->val );
300 /* Set the variable */
303 if( i_type & VLC_VAR_HASCHOICE )
305 /* We must add the inherited value to our choice list */
306 p_var->i_default = 0;
308 INSERT_ELEM( p_var->choices.p_values, p_var->choices.i_count,
310 INSERT_ELEM( p_var->choices_text.p_values,
311 p_var->choices_text.i_count, 0, val );
312 p_var->ops->pf_dup( &p_var->choices.p_values[0] );
313 p_var->choices_text.p_values[0].psz_string = NULL;
318 vlc_mutex_unlock( &p_priv->var_lock );
324 * Destroy a vlc variable
326 * Look for the variable and destroy it if it is found. As in var_Create we
327 * do a call to memmove() but we have performance counterparts elsewhere.
329 * \param p_this The object that holds the variable
330 * \param psz_name The name of the variable
332 int __var_Destroy( vlc_object_t *p_this, const char *psz_name )
336 vlc_object_internals_t *p_priv = vlc_internals( p_this );
338 vlc_mutex_lock( &p_priv->var_lock );
340 i_var = GetUnused( p_this, psz_name );
343 vlc_mutex_unlock( &p_priv->var_lock );
347 p_var = &p_priv->p_vars[i_var];
349 if( p_var->i_usage > 1 )
352 vlc_mutex_unlock( &p_priv->var_lock );
356 /* Free value if needed */
357 p_var->ops->pf_free( &p_var->val );
359 /* Free choice list if needed */
360 if( p_var->choices.i_count )
362 for( i = 0 ; i < p_var->choices.i_count ; i++ )
364 p_var->ops->pf_free( &p_var->choices.p_values[i] );
365 free( p_var->choices_text.p_values[i].psz_string );
367 free( p_var->choices.p_values );
368 free( p_var->choices_text.p_values );
371 /* Free callbacks if needed */
372 if( p_var->p_entries )
374 free( p_var->p_entries );
377 free( p_var->psz_name );
378 free( p_var->psz_text );
380 memmove( p_priv->p_vars + i_var,
381 p_priv->p_vars + i_var + 1,
382 (p_priv->i_vars - i_var - 1) * sizeof(variable_t) );
384 if( (p_priv->i_vars & 15) == 0 )
386 p_priv->p_vars = realloc( p_priv->p_vars,
387 (p_priv->i_vars) * sizeof( variable_t ) );
392 vlc_mutex_unlock( &p_priv->var_lock );
398 * Perform an action on a variable
400 * \param p_this The object that holds the variable
401 * \param psz_name The name of the variable
402 * \param i_action The action to perform. Must be one of \ref var_action
403 * \param p_val First action parameter
404 * \param p_val2 Second action parameter
406 int __var_Change( vlc_object_t *p_this, const char *psz_name,
407 int i_action, vlc_value_t *p_val, vlc_value_t *p_val2 )
412 vlc_object_internals_t *p_priv = vlc_internals( p_this );
414 vlc_mutex_lock( &p_priv->var_lock );
416 i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
420 vlc_mutex_unlock( &p_priv->var_lock );
424 p_var = &p_priv->p_vars[i_var];
429 if( p_var->i_type & VLC_VAR_HASMIN )
431 p_var->ops->pf_free( &p_var->min );
433 p_var->i_type |= VLC_VAR_HASMIN;
435 p_var->ops->pf_dup( &p_var->min );
436 CheckValue( p_var, &p_var->val );
439 if( p_var->i_type & VLC_VAR_HASMIN )
445 if( p_var->i_type & VLC_VAR_HASMAX )
447 p_var->ops->pf_free( &p_var->max );
449 p_var->i_type |= VLC_VAR_HASMAX;
451 p_var->ops->pf_dup( &p_var->max );
452 CheckValue( p_var, &p_var->val );
455 if( p_var->i_type & VLC_VAR_HASMAX )
460 case VLC_VAR_SETSTEP:
461 if( p_var->i_type & VLC_VAR_HASSTEP )
463 p_var->ops->pf_free( &p_var->step );
465 p_var->i_type |= VLC_VAR_HASSTEP;
466 p_var->step = *p_val;
467 p_var->ops->pf_dup( &p_var->step );
468 CheckValue( p_var, &p_var->val );
470 case VLC_VAR_GETSTEP:
471 if( p_var->i_type & VLC_VAR_HASSTEP )
473 *p_val = p_var->step;
476 case VLC_VAR_ADDCHOICE:
477 i = p_var->choices.i_count;
479 INSERT_ELEM( p_var->choices.p_values, p_var->choices.i_count,
481 INSERT_ELEM( p_var->choices_text.p_values,
482 p_var->choices_text.i_count, i, *p_val );
483 p_var->ops->pf_dup( &p_var->choices.p_values[i] );
484 p_var->choices_text.p_values[i].psz_string =
485 ( p_val2 && p_val2->psz_string ) ?
486 strdup( p_val2->psz_string ) : NULL;
488 CheckValue( p_var, &p_var->val );
490 case VLC_VAR_DELCHOICE:
491 for( i = 0 ; i < p_var->choices.i_count ; i++ )
493 if( p_var->ops->pf_cmp( p_var->choices.p_values[i], *p_val ) == 0 )
499 if( i == p_var->choices.i_count )
502 vlc_mutex_unlock( &p_priv->var_lock );
506 if( p_var->i_default > i )
510 else if( p_var->i_default == i )
512 p_var->i_default = -1;
515 p_var->ops->pf_free( &p_var->choices.p_values[i] );
516 free( p_var->choices_text.p_values[i].psz_string );
517 REMOVE_ELEM( p_var->choices.p_values, p_var->choices.i_count, i );
518 REMOVE_ELEM( p_var->choices_text.p_values,
519 p_var->choices_text.i_count, i );
521 CheckValue( p_var, &p_var->val );
523 case VLC_VAR_CHOICESCOUNT:
524 p_val->i_int = p_var->choices.i_count;
526 case VLC_VAR_CLEARCHOICES:
527 for( i = 0 ; i < p_var->choices.i_count ; i++ )
529 p_var->ops->pf_free( &p_var->choices.p_values[i] );
531 for( i = 0 ; i < p_var->choices_text.i_count ; i++ )
532 free( p_var->choices_text.p_values[i].psz_string );
534 if( p_var->choices.i_count ) free( p_var->choices.p_values );
535 if( p_var->choices_text.i_count ) free( p_var->choices_text.p_values );
537 p_var->choices.i_count = 0;
538 p_var->choices.p_values = NULL;
539 p_var->choices_text.i_count = 0;
540 p_var->choices_text.p_values = NULL;
541 p_var->i_default = -1;
543 case VLC_VAR_SETDEFAULT:
544 /* FIXME: the list is sorted, dude. Use something cleverer. */
545 for( i = 0 ; i < p_var->choices.i_count ; i++ )
547 if( p_var->ops->pf_cmp( p_var->choices.p_values[i], *p_val ) == 0 )
553 if( i == p_var->choices.i_count )
559 p_var->i_default = i;
560 CheckValue( p_var, &p_var->val );
562 case VLC_VAR_SETVALUE:
563 /* Duplicate data if needed */
564 p_var->ops->pf_dup( p_val );
565 /* Backup needed stuff */
567 /* Check boundaries and list */
568 CheckValue( p_var, p_val );
569 /* Set the variable */
571 /* Free data if needed */
572 p_var->ops->pf_free( &oldval );
574 case VLC_VAR_GETCHOICES:
575 case VLC_VAR_GETLIST:
576 p_val->p_list = malloc( sizeof(vlc_list_t) );
577 if( p_val2 ) p_val2->p_list = malloc( sizeof(vlc_list_t) );
578 if( p_var->choices.i_count )
580 p_val->p_list->p_values = malloc( p_var->choices.i_count
581 * sizeof(vlc_value_t) );
582 p_val->p_list->pi_types = malloc( p_var->choices.i_count
586 p_val2->p_list->p_values =
587 malloc( p_var->choices.i_count * sizeof(vlc_value_t) );
588 p_val2->p_list->pi_types =
589 malloc( p_var->choices.i_count * sizeof(int) );
592 p_val->p_list->i_count = p_var->choices.i_count;
593 if( p_val2 ) p_val2->p_list->i_count = p_var->choices.i_count;
594 for( i = 0 ; i < p_var->choices.i_count ; i++ )
596 p_val->p_list->p_values[i] = p_var->choices.p_values[i];
597 p_val->p_list->pi_types[i] = p_var->i_type;
598 p_var->ops->pf_dup( &p_val->p_list->p_values[i] );
601 p_val2->p_list->p_values[i].psz_string =
602 p_var->choices_text.p_values[i].psz_string ?
603 strdup(p_var->choices_text.p_values[i].psz_string) : NULL;
604 p_val2->p_list->pi_types[i] = VLC_VAR_STRING;
608 case VLC_VAR_SETTEXT:
609 free( p_var->psz_text );
610 if( p_val && p_val->psz_string )
611 p_var->psz_text = strdup( p_val->psz_string );
613 p_var->psz_text = NULL;
615 case VLC_VAR_GETTEXT:
616 p_val->psz_string = NULL;
617 if( p_var->psz_text )
619 p_val->psz_string = strdup( p_var->psz_text );
622 case VLC_VAR_INHERITVALUE:
626 if( InheritValue( p_this,
627 p_val2 ? p_val2->psz_string : psz_name,
628 &val, p_var->i_type )
631 /* Duplicate already done */
633 /* Backup needed stuff */
635 /* Check boundaries and list */
636 CheckValue( p_var, &val );
637 /* Set the variable */
639 /* Free data if needed */
640 p_var->ops->pf_free( &oldval );
646 p_var->ops->pf_dup( p_val );
651 case VLC_VAR_SETISCOMMAND:
652 p_var->i_type |= VLC_VAR_ISCOMMAND;
659 vlc_mutex_unlock( &p_priv->var_lock );
665 * Request a variable's type
667 * \return The variable type if it exists, or 0 if the
668 * variable could not be found.
671 int __var_Type( vlc_object_t *p_this, const char *psz_name )
674 vlc_object_internals_t *p_priv = vlc_internals( p_this );
676 vlc_mutex_lock( &p_priv->var_lock );
678 i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
682 vlc_mutex_unlock( &p_priv->var_lock );
686 i_type = p_priv->p_vars[i_var].i_type;
688 vlc_mutex_unlock( &p_priv->var_lock );
693 int var_SetChecked( vlc_object_t *p_this, const char *psz_name,
694 int expected_type, vlc_value_t val )
699 vlc_object_internals_t *p_priv = vlc_internals( p_this );
701 vlc_mutex_lock( &p_priv->var_lock );
703 i_var = GetUnused( p_this, psz_name );
706 vlc_mutex_unlock( &p_priv->var_lock );
710 p_var = &p_priv->p_vars[i_var];
711 assert( (p_var->i_type & VLC_VAR_CLASS) == 0 || expected_type == 0 ||
712 (p_var->i_type & VLC_VAR_CLASS) == expected_type );
714 /* Duplicate data if needed */
715 p_var->ops->pf_dup( &val );
717 /* Backup needed stuff */
720 /* Check boundaries and list */
721 CheckValue( p_var, &val );
723 /* Set the variable */
726 /* Deal with callbacks. Tell we're in a callback, release the lock,
727 * call stored functions, retake the lock. */
728 if( p_var->i_entries )
731 int i_entries = p_var->i_entries;
732 callback_entry_t *p_entries = p_var->p_entries;
734 p_var->b_incallback = true;
735 vlc_mutex_unlock( &p_priv->var_lock );
738 for( ; i_entries-- ; )
740 p_entries[i_entries].pf_callback( p_this, psz_name, oldval, val,
741 p_entries[i_entries].p_data );
744 vlc_mutex_lock( &p_priv->var_lock );
746 i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
749 msg_Err( p_this, "variable %s has disappeared", psz_name );
750 vlc_mutex_unlock( &p_priv->var_lock );
754 p_var = &p_priv->p_vars[i_var];
755 p_var->b_incallback = false;
756 vlc_cond_broadcast( &p_priv->var_wait );
759 /* Free data if needed */
760 p_var->ops->pf_free( &oldval );
762 vlc_mutex_unlock( &p_priv->var_lock );
769 * Set a variable's value
771 * \param p_this The object that hold the variable
772 * \param psz_name The name of the variable
773 * \param val the value to set
775 int __var_Set( vlc_object_t *p_this, const char *psz_name, vlc_value_t val )
777 return var_SetChecked( p_this, psz_name, 0, val );
780 int var_GetChecked( vlc_object_t *p_this, const char *psz_name,
781 int expected_type, vlc_value_t *p_val )
783 vlc_object_internals_t *p_priv = vlc_internals( p_this );
784 int i_var, err = VLC_SUCCESS;
786 vlc_mutex_lock( &p_priv->var_lock );
788 i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
791 variable_t *p_var = &p_priv->p_vars[i_var];
793 assert( (p_var->i_type & VLC_VAR_CLASS) == 0 || expected_type == 0 ||
794 (p_var->i_type & VLC_VAR_CLASS) == expected_type );
796 /* Really get the variable */
799 /* Duplicate value if needed */
800 p_var->ops->pf_dup( p_val );
805 vlc_mutex_unlock( &p_priv->var_lock );
810 * Get a variable's value
812 * \param p_this The object that holds the variable
813 * \param psz_name The name of the variable
814 * \param p_val Pointer to a vlc_value_t that will hold the variable's value
815 * after the function is finished
817 int __var_Get( vlc_object_t *p_this, const char *psz_name, vlc_value_t *p_val )
819 return var_GetChecked( p_this, psz_name, 0, p_val );
823 * Register a callback in a variable
825 * We store a function pointer that will be called upon variable
828 * \param p_this The object that holds the variable
829 * \param psz_name The name of the variable
830 * \param pf_callback The function pointer
831 * \param p_data A generic pointer that will be passed as the last
832 * argument to the callback function.
834 * \warning The callback function is run in the thread that calls var_Set on
835 * the variable. Use proper locking. This thread may not have much
836 * time to spare, so keep callback functions short.
838 int __var_AddCallback( vlc_object_t *p_this, const char *psz_name,
839 vlc_callback_t pf_callback, void *p_data )
843 callback_entry_t entry;
844 vlc_object_internals_t *p_priv = vlc_internals( p_this );
846 entry.pf_callback = pf_callback;
847 entry.p_data = p_data;
849 vlc_mutex_lock( &p_priv->var_lock );
851 i_var = GetUnused( p_this, psz_name );
854 vlc_mutex_unlock( &p_priv->var_lock );
858 p_var = &p_priv->p_vars[i_var];
860 INSERT_ELEM( p_var->p_entries,
865 vlc_mutex_unlock( &p_priv->var_lock );
871 * Remove a callback from a variable
873 * pf_callback and p_data have to be given again, because different objects
874 * might have registered the same callback function.
876 int __var_DelCallback( vlc_object_t *p_this, const char *psz_name,
877 vlc_callback_t pf_callback, void *p_data )
881 vlc_object_internals_t *p_priv = vlc_internals( p_this );
883 vlc_mutex_lock( &p_priv->var_lock );
885 i_var = GetUnused( p_this, psz_name );
888 vlc_mutex_unlock( &p_priv->var_lock );
892 p_var = &p_priv->p_vars[i_var];
894 for( i_entry = p_var->i_entries ; i_entry-- ; )
896 if( p_var->p_entries[i_entry].pf_callback == pf_callback
897 && p_var->p_entries[i_entry].p_data == p_data )
905 vlc_mutex_unlock( &p_priv->var_lock );
909 REMOVE_ELEM( p_var->p_entries, p_var->i_entries, i_entry );
911 vlc_mutex_unlock( &p_priv->var_lock );
917 * Trigger callback on a variable
919 * \param p_this The object that hold the variable
920 * \param psz_name The name of the variable
922 int __var_TriggerCallback( vlc_object_t *p_this, const char *psz_name )
927 vlc_object_internals_t *p_priv = vlc_internals( p_this );
929 vlc_mutex_lock( &p_priv->var_lock );
931 i_var = GetUnused( p_this, psz_name );
934 vlc_mutex_unlock( &p_priv->var_lock );
938 p_var = &p_priv->p_vars[i_var];
940 /* Backup needed stuff */
943 /* Deal with callbacks. Tell we're in a callback, release the lock,
944 * call stored functions, retake the lock. */
945 if( p_var->i_entries )
948 int i_entries = p_var->i_entries;
949 callback_entry_t *p_entries = p_var->p_entries;
951 p_var->b_incallback = true;
952 vlc_mutex_unlock( &p_priv->var_lock );
955 for( ; i_entries-- ; )
957 p_entries[i_entries].pf_callback( p_this, psz_name, oldval, oldval,
958 p_entries[i_entries].p_data );
961 vlc_mutex_lock( &p_priv->var_lock );
963 i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
966 msg_Err( p_this, "variable %s has disappeared", psz_name );
967 vlc_mutex_unlock( &p_priv->var_lock );
971 p_var = &p_priv->p_vars[i_var];
972 p_var->b_incallback = false;
973 vlc_cond_broadcast( &p_priv->var_wait );
976 vlc_mutex_unlock( &p_priv->var_lock );
980 /** Parse a stringified option
981 * This function parse a string option and create the associated object
983 * The option must be of the form "[no[-]]foo[=bar]" where foo is the
984 * option name and bar is the value of the option.
985 * \param p_obj the object in which the variable must be created
986 * \param psz_option the option to parse
987 * \param trusted whether the option is set by a trusted input or not
990 void var_OptionParse( vlc_object_t *p_obj, const char *psz_option,
993 char *psz_name, *psz_value;
998 val.psz_string = NULL;
1000 /* It's too much of a hassle to remove the ':' when we parse
1001 * the cmd line :) */
1002 if( psz_option[0] == ':' )
1005 if( !psz_option[0] )
1008 psz_name = strdup( psz_option );
1009 if( psz_name == NULL )
1012 psz_value = strchr( psz_name, '=' );
1013 if( psz_value != NULL )
1014 *psz_value++ = '\0';
1016 /* FIXME: :programs should be handled generically */
1017 if( !strcmp( psz_name, "programs" ) )
1018 i_type = VLC_VAR_LIST;
1020 i_type = config_GetType( p_obj, psz_name );
1022 if( !i_type && !psz_value )
1024 /* check for "no-foo" or "nofoo" */
1025 if( !strncmp( psz_name, "no-", 3 ) )
1027 memmove( psz_name, psz_name + 3, strlen(psz_name) + 1 - 3 );
1029 else if( !strncmp( psz_name, "no", 2 ) )
1031 memmove( psz_name, psz_name + 2, strlen(psz_name) + 1 - 2 );
1033 else goto cleanup; /* Option doesn't exist */
1036 i_type = config_GetType( p_obj, psz_name );
1038 if( !i_type ) goto cleanup; /* Option doesn't exist */
1040 if( ( i_type != VLC_VAR_BOOL ) &&
1041 ( !psz_value || !*psz_value ) ) goto cleanup; /* Invalid value */
1043 /* check if option is unsafe */
1046 module_config_t *p_config = config_FindConfig( p_obj, psz_name );
1047 if( !p_config || !p_config->b_safe )
1049 msg_Err( p_obj, "unsafe option \"%s\" has been ignored for "
1050 "security reasons", psz_name );
1056 /* Create the variable in the input object.
1057 * Children of the input object will be able to retreive this value
1058 * thanks to the inheritance property of the object variables. */
1059 var_Create( p_obj, psz_name, i_type );
1064 val.b_bool = !b_isno;
1067 case VLC_VAR_INTEGER:
1068 val.i_int = strtol( psz_value, NULL, 0 );
1072 val.f_float = atof( psz_value );
1075 case VLC_VAR_STRING:
1076 case VLC_VAR_MODULE:
1078 case VLC_VAR_DIRECTORY:
1079 val.psz_string = psz_value;
1084 char *psz_orig, *psz_var;
1085 vlc_list_t *p_list = malloc(sizeof(vlc_list_t));
1086 val.p_list = p_list;
1087 p_list->i_count = 0;
1089 psz_var = psz_orig = strdup(psz_value);
1090 while( psz_var && *psz_var )
1092 char *psz_item = psz_var;
1094 while( *psz_var && *psz_var != ',' ) psz_var++;
1095 if( *psz_var == ',' )
1100 val2.i_int = strtol( psz_item, NULL, 0 );
1101 INSERT_ELEM( p_list->p_values, p_list->i_count,
1102 p_list->i_count, val2 );
1103 /* p_list->i_count is incremented twice by INSERT_ELEM */
1105 INSERT_ELEM( p_list->pi_types, p_list->i_count,
1106 p_list->i_count, VLC_VAR_INTEGER );
1116 var_Set( p_obj, psz_name, val );
1118 // If that's a list, remove all elements allocated
1119 if( i_type == VLC_VAR_LIST )
1127 /* Following functions are local */
1129 /*****************************************************************************
1130 * GetUnused: find an unused (not in callback) variable from its name
1131 *****************************************************************************
1132 * We do i_tries tries before giving up, just in case the variable is being
1133 * modified and called from a callback.
1134 *****************************************************************************/
1135 static int GetUnused( vlc_object_t *p_this, const char *psz_name )
1137 vlc_object_internals_t *p_priv = vlc_internals( p_this );
1143 i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
1149 if( ! p_priv->p_vars[i_var].b_incallback )
1154 mutex_cleanup_push( &p_priv->var_lock );
1155 vlc_cond_wait( &p_priv->var_wait, &p_priv->var_lock );
1160 /*****************************************************************************
1161 * HashString: our cool hash function
1162 *****************************************************************************
1163 * This function is not intended to be crypto-secure, we only want it to be
1164 * fast and not suck too much. This one is pretty fast and did 0 collisions
1165 * in wenglish's dictionary.
1166 *****************************************************************************/
1167 static uint32_t HashString( const char *psz_string )
1169 uint32_t i_hash = 0;
1171 while( *psz_string )
1173 i_hash += *psz_string++;
1174 i_hash += i_hash << 10;
1175 i_hash ^= i_hash >> 8;
1181 /*****************************************************************************
1182 * Insert: find an empty slot to insert a new variable
1183 *****************************************************************************
1184 * We use a recursive inner function indexed on the hash. This function does
1185 * nothing in the rare cases where a collision may occur, see Lookup()
1186 * to see how we handle them.
1187 * XXX: does this really need to be written recursively?
1188 *****************************************************************************/
1189 static int Insert( variable_t *p_vars, int i_count, const char *psz_name )
1196 return InsertInner( p_vars, i_count, HashString( psz_name ) );
1199 static int InsertInner( variable_t *p_vars, int i_count, uint32_t i_hash )
1203 if( i_hash <= p_vars[0].i_hash )
1208 if( i_hash >= p_vars[i_count - 1].i_hash )
1213 i_middle = i_count / 2;
1215 /* We know that 0 < i_middle */
1216 if( i_hash < p_vars[i_middle].i_hash )
1218 return InsertInner( p_vars, i_middle, i_hash );
1221 /* We know that i_middle + 1 < i_count */
1222 if( i_hash > p_vars[i_middle + 1].i_hash )
1224 return i_middle + 1 + InsertInner( p_vars + i_middle + 1,
1225 i_count - i_middle - 1,
1229 return i_middle + 1;
1232 static int u32cmp( const void *key, const void *data )
1234 const variable_t *p_var = data;
1235 uint32_t hash = *(const uint32_t *)key ;
1237 if( hash > p_var->i_hash )
1239 if( hash < p_var->i_hash )
1244 /*****************************************************************************
1245 * Lookup: find an existing variable given its name
1246 *****************************************************************************
1247 * We use a recursive inner function indexed on the hash. Care is taken of
1248 * possible hash collisions.
1249 *****************************************************************************/
1250 static int Lookup( variable_t *p_vars, size_t i_count, const char *psz_name )
1255 i_hash = HashString( psz_name );
1256 p_var = bsearch( &i_hash, p_vars, i_count, sizeof( *p_var ), u32cmp );
1258 /* Hash not found */
1262 assert( i_count > 0 );
1264 /* Find the first entry with the right hash */
1265 while( (p_var > p_vars) && (i_hash == p_var[-1].i_hash) )
1268 assert( p_var->i_hash == i_hash );
1270 /* Hash collision should be very unlikely, but we cannot guarantee
1271 * it will never happen. So we do an exhaustive search amongst all
1272 * entries with the same hash. Typically, there is only one anyway. */
1273 for( variable_t *p_end = p_vars + i_count;
1274 (p_var < p_end) && (i_hash == p_var->i_hash);
1277 if( !strcmp( psz_name, p_var->psz_name ) )
1278 return p_var - p_vars;
1281 /* Hash found, but entry not found */
1285 /*****************************************************************************
1286 * CheckValue: check that a value is valid wrt. a variable
1287 *****************************************************************************
1288 * This function checks p_val's value against p_var's limitations such as
1289 * minimal and maximal value, step, in-list position, and modifies p_val if
1291 ****************************************************************************/
1292 static void CheckValue ( variable_t *p_var, vlc_value_t *p_val )
1294 /* Check that our variable is in the list */
1295 if( p_var->i_type & VLC_VAR_HASCHOICE && p_var->choices.i_count )
1299 /* FIXME: the list is sorted, dude. Use something cleverer. */
1300 for( i = p_var->choices.i_count ; i-- ; )
1302 if( p_var->ops->pf_cmp( *p_val, p_var->choices.p_values[i] ) == 0 )
1308 /* If not found, change it to anything vaguely valid */
1311 /* Free the old variable, get the new one, dup it */
1312 p_var->ops->pf_free( p_val );
1313 *p_val = p_var->choices.p_values[p_var->i_default >= 0
1314 ? p_var->i_default : 0 ];
1315 p_var->ops->pf_dup( p_val );
1319 /* Check that our variable is within the bounds */
1320 switch( p_var->i_type & VLC_VAR_TYPE )
1322 case VLC_VAR_INTEGER:
1323 if( p_var->i_type & VLC_VAR_HASSTEP && p_var->step.i_int
1324 && (p_val->i_int % p_var->step.i_int) )
1326 p_val->i_int = (p_val->i_int + (p_var->step.i_int / 2))
1327 / p_var->step.i_int * p_var->step.i_int;
1329 if( p_var->i_type & VLC_VAR_HASMIN
1330 && p_val->i_int < p_var->min.i_int )
1332 p_val->i_int = p_var->min.i_int;
1334 if( p_var->i_type & VLC_VAR_HASMAX
1335 && p_val->i_int > p_var->max.i_int )
1337 p_val->i_int = p_var->max.i_int;
1341 if( p_var->i_type & VLC_VAR_HASSTEP && p_var->step.f_float )
1343 float f_round = p_var->step.f_float * (float)(int)( 0.5 +
1344 p_val->f_float / p_var->step.f_float );
1345 if( p_val->f_float != f_round )
1347 p_val->f_float = f_round;
1350 if( p_var->i_type & VLC_VAR_HASMIN
1351 && p_val->f_float < p_var->min.f_float )
1353 p_val->f_float = p_var->min.f_float;
1355 if( p_var->i_type & VLC_VAR_HASMAX
1356 && p_val->f_float > p_var->max.f_float )
1358 p_val->f_float = p_var->max.f_float;
1367 /*****************************************************************************
1368 * InheritValue: try to inherit the value of this variable from the same one
1369 * in our closest parent, libvlc or ultimately from the configuration.
1370 * The function should always be entered with the object var_lock locked.
1371 *****************************************************************************/
1372 static int InheritValue( vlc_object_t *p_this, const char *psz_name,
1373 vlc_value_t *p_val, int i_type )
1378 if( p_this->p_parent || ( p_this->p_libvlc && p_this != (vlc_object_t*) p_this->p_libvlc ) )
1380 vlc_object_internals_t *p_priv;
1382 if( p_this->p_parent )
1383 p_priv = vlc_internals( p_this->p_parent );
1385 p_priv = vlc_internals( p_this->p_libvlc );
1387 i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
1392 p_var = &p_priv->p_vars[i_var];
1394 /* Really get the variable */
1395 *p_val = p_var->val;
1397 /* Duplicate value if needed */
1398 p_var->ops->pf_dup( p_val );
1400 /*msg_Dbg( p_this, "Inherited value for var %s from object %s",
1401 psz_name ? psz_name : "(null)",
1402 p_this->psz_object_name
1403 ? p_this->psz_object_name : "(Unknown)" );*/
1406 else if ( p_this->p_parent ) /* We are still not there */
1407 return InheritValue( p_this->p_parent, psz_name, p_val, i_type );
1409 /* else take value from config */
1413 switch( i_type & VLC_VAR_CLASS )
1415 case VLC_VAR_STRING:
1416 p_val->psz_string = config_GetPsz( p_this, psz_name );
1417 if( !p_val->psz_string ) p_val->psz_string = strdup("");
1420 p_val->f_float = config_GetFloat( p_this, psz_name );
1422 case VLC_VAR_INTEGER:
1423 p_val->i_int = config_GetInt( p_this, psz_name );
1426 p_val->b_bool = config_GetInt( p_this, psz_name );
1430 char *psz_orig, *psz_var;
1431 vlc_list_t *p_list = malloc(sizeof(vlc_list_t));
1432 p_val->p_list = p_list;
1433 p_list->i_count = 0;
1435 psz_var = psz_orig = config_GetPsz( p_this, psz_name );
1436 while( psz_var && *psz_var )
1438 char *psz_item = psz_var;
1440 while( *psz_var && *psz_var != ',' ) psz_var++;
1441 if( *psz_var == ',' )
1446 val.i_int = strtol( psz_item, NULL, 0 );
1447 INSERT_ELEM( p_list->p_values, p_list->i_count,
1448 p_list->i_count, val );
1449 /* p_list->i_count is incremented twice by INSERT_ELEM */
1451 INSERT_ELEM( p_list->pi_types, p_list->i_count,
1452 p_list->i_count, VLC_VAR_INTEGER );
1458 msg_Warn( p_this, "Could not inherit value for var %s "
1459 "from config. Invalid Type", psz_name );
1463 /*msg_Dbg( p_this, "Inherited value for var %s from config", psz_name );*/
1467 /**********************************************************************
1468 * Execute a var command on an object identified by its name
1469 **********************************************************************/
1470 int __var_Command( vlc_object_t *p_this, const char *psz_name,
1471 const char *psz_cmd, const char *psz_arg, char **psz_msg )
1473 vlc_object_t *p_obj = vlc_object_find_name( p_this->p_libvlc,
1474 psz_name, FIND_CHILD );
1480 *psz_msg = strdup( "Unknown destination object." );
1484 i_type = var_Type( p_obj, psz_cmd );
1485 if( !( i_type&VLC_VAR_ISCOMMAND ) )
1487 vlc_object_release( p_obj );
1489 *psz_msg = strdup( "Variable doesn't exist or isn't a command." );
1490 return VLC_EGENERIC;
1493 i_type &= VLC_VAR_CLASS;
1496 case VLC_VAR_INTEGER:
1497 i_ret = var_SetInteger( p_obj, psz_cmd, atoi( psz_arg ) );
1500 i_ret = var_SetFloat( p_obj, psz_cmd, atof( psz_arg ) );
1502 case VLC_VAR_STRING:
1503 i_ret = var_SetString( p_obj, psz_cmd, psz_arg );
1506 i_ret = var_SetBool( p_obj, psz_cmd, atoi( psz_arg ) );
1509 i_ret = VLC_EGENERIC;
1513 vlc_object_release( p_obj );
1517 if( asprintf( psz_msg, "%s on object %s returned %i (%s)",
1518 psz_cmd, psz_name, i_ret, vlc_error( i_ret ) ) == -1)
1527 * Free a list and the associated strings
1528 * @param p_val: the list variable
1529 * @param p_val2: the variable associated or NULL
1531 void var_FreeList( vlc_value_t *p_val, vlc_value_t *p_val2 )
1534 if( p_val2 && p_val2->p_list )
1536 for( int i = 0; i < p_val2->p_list->i_count; i++ )
1537 free( p_val2->p_list->p_values[i].psz_string );
1538 if( p_val2->p_list->i_count )
1540 free( p_val2->p_list->p_values );
1541 free( p_val2->p_list->pi_types );
1543 free( p_val2->p_list );