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_FREELIST:
610 if( p_val2 && p_val2->p_list )
612 for( i = 0; i < p_val2->p_list->i_count; i++ )
613 free( p_val2->p_list->p_values[i].psz_string );
614 if( p_val2->p_list->i_count )
616 free( p_val2->p_list->p_values );
617 free( p_val2->p_list->pi_types );
619 free( p_val2->p_list );
622 case VLC_VAR_SETTEXT:
623 free( p_var->psz_text );
624 if( p_val && p_val->psz_string )
625 p_var->psz_text = strdup( p_val->psz_string );
627 p_var->psz_text = NULL;
629 case VLC_VAR_GETTEXT:
630 p_val->psz_string = NULL;
631 if( p_var->psz_text )
633 p_val->psz_string = strdup( p_var->psz_text );
636 case VLC_VAR_INHERITVALUE:
640 if( InheritValue( p_this,
641 p_val2 ? p_val2->psz_string : psz_name,
642 &val, p_var->i_type )
645 /* Duplicate already done */
647 /* Backup needed stuff */
649 /* Check boundaries and list */
650 CheckValue( p_var, &val );
651 /* Set the variable */
653 /* Free data if needed */
654 p_var->ops->pf_free( &oldval );
660 p_var->ops->pf_dup( p_val );
665 case VLC_VAR_SETISCOMMAND:
666 p_var->i_type |= VLC_VAR_ISCOMMAND;
673 vlc_mutex_unlock( &p_priv->var_lock );
679 * Request a variable's type
681 * \return The variable type if it exists, or 0 if the
682 * variable could not be found.
685 int __var_Type( vlc_object_t *p_this, const char *psz_name )
688 vlc_object_internals_t *p_priv = vlc_internals( p_this );
690 vlc_mutex_lock( &p_priv->var_lock );
692 i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
696 vlc_mutex_unlock( &p_priv->var_lock );
700 i_type = p_priv->p_vars[i_var].i_type;
702 vlc_mutex_unlock( &p_priv->var_lock );
707 int var_SetChecked( vlc_object_t *p_this, const char *psz_name,
708 int expected_type, vlc_value_t val )
713 vlc_object_internals_t *p_priv = vlc_internals( p_this );
715 vlc_mutex_lock( &p_priv->var_lock );
717 i_var = GetUnused( p_this, psz_name );
720 vlc_mutex_unlock( &p_priv->var_lock );
724 p_var = &p_priv->p_vars[i_var];
725 assert( (p_var->i_type & VLC_VAR_CLASS) == 0 || expected_type == 0 ||
726 (p_var->i_type & VLC_VAR_CLASS) == expected_type );
728 /* Duplicate data if needed */
729 p_var->ops->pf_dup( &val );
731 /* Backup needed stuff */
734 /* Check boundaries and list */
735 CheckValue( p_var, &val );
737 /* Set the variable */
740 /* Deal with callbacks. Tell we're in a callback, release the lock,
741 * call stored functions, retake the lock. */
742 if( p_var->i_entries )
745 int i_entries = p_var->i_entries;
746 callback_entry_t *p_entries = p_var->p_entries;
748 p_var->b_incallback = true;
749 vlc_mutex_unlock( &p_priv->var_lock );
752 for( ; i_entries-- ; )
754 p_entries[i_entries].pf_callback( p_this, psz_name, oldval, val,
755 p_entries[i_entries].p_data );
758 vlc_mutex_lock( &p_priv->var_lock );
760 i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
763 msg_Err( p_this, "variable %s has disappeared", psz_name );
764 vlc_mutex_unlock( &p_priv->var_lock );
768 p_var = &p_priv->p_vars[i_var];
769 p_var->b_incallback = false;
770 vlc_cond_broadcast( &p_priv->var_wait );
773 /* Free data if needed */
774 p_var->ops->pf_free( &oldval );
776 vlc_mutex_unlock( &p_priv->var_lock );
783 * Set a variable's value
785 * \param p_this The object that hold the variable
786 * \param psz_name The name of the variable
787 * \param val the value to set
789 int __var_Set( vlc_object_t *p_this, const char *psz_name, vlc_value_t val )
791 return var_SetChecked( p_this, psz_name, 0, val );
794 int var_GetChecked( vlc_object_t *p_this, const char *psz_name,
795 int expected_type, vlc_value_t *p_val )
797 vlc_object_internals_t *p_priv = vlc_internals( p_this );
798 int i_var, err = VLC_SUCCESS;
800 vlc_mutex_lock( &p_priv->var_lock );
802 i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
805 variable_t *p_var = &p_priv->p_vars[i_var];
807 assert( (p_var->i_type & VLC_VAR_CLASS) == 0 || expected_type == 0 ||
808 (p_var->i_type & VLC_VAR_CLASS) == expected_type );
810 /* Really get the variable */
813 /* Duplicate value if needed */
814 p_var->ops->pf_dup( p_val );
819 vlc_mutex_unlock( &p_priv->var_lock );
824 * Get a variable's value
826 * \param p_this The object that holds the variable
827 * \param psz_name The name of the variable
828 * \param p_val Pointer to a vlc_value_t that will hold the variable's value
829 * after the function is finished
831 int __var_Get( vlc_object_t *p_this, const char *psz_name, vlc_value_t *p_val )
833 return var_GetChecked( p_this, psz_name, 0, p_val );
837 * Register a callback in a variable
839 * We store a function pointer that will be called upon variable
842 * \param p_this The object that holds the variable
843 * \param psz_name The name of the variable
844 * \param pf_callback The function pointer
845 * \param p_data A generic pointer that will be passed as the last
846 * argument to the callback function.
848 * \warning The callback function is run in the thread that calls var_Set on
849 * the variable. Use proper locking. This thread may not have much
850 * time to spare, so keep callback functions short.
852 int __var_AddCallback( vlc_object_t *p_this, const char *psz_name,
853 vlc_callback_t pf_callback, void *p_data )
857 callback_entry_t entry;
858 vlc_object_internals_t *p_priv = vlc_internals( p_this );
860 entry.pf_callback = pf_callback;
861 entry.p_data = p_data;
863 vlc_mutex_lock( &p_priv->var_lock );
865 i_var = GetUnused( p_this, psz_name );
868 vlc_mutex_unlock( &p_priv->var_lock );
872 p_var = &p_priv->p_vars[i_var];
874 INSERT_ELEM( p_var->p_entries,
879 vlc_mutex_unlock( &p_priv->var_lock );
885 * Remove a callback from a variable
887 * pf_callback and p_data have to be given again, because different objects
888 * might have registered the same callback function.
890 int __var_DelCallback( vlc_object_t *p_this, const char *psz_name,
891 vlc_callback_t pf_callback, void *p_data )
895 vlc_object_internals_t *p_priv = vlc_internals( p_this );
897 vlc_mutex_lock( &p_priv->var_lock );
899 i_var = GetUnused( p_this, psz_name );
902 vlc_mutex_unlock( &p_priv->var_lock );
906 p_var = &p_priv->p_vars[i_var];
908 for( i_entry = p_var->i_entries ; i_entry-- ; )
910 if( p_var->p_entries[i_entry].pf_callback == pf_callback
911 && p_var->p_entries[i_entry].p_data == p_data )
919 vlc_mutex_unlock( &p_priv->var_lock );
923 REMOVE_ELEM( p_var->p_entries, p_var->i_entries, i_entry );
925 vlc_mutex_unlock( &p_priv->var_lock );
931 * Trigger callback on a variable
933 * \param p_this The object that hold the variable
934 * \param psz_name The name of the variable
936 int __var_TriggerCallback( vlc_object_t *p_this, const char *psz_name )
941 vlc_object_internals_t *p_priv = vlc_internals( p_this );
943 vlc_mutex_lock( &p_priv->var_lock );
945 i_var = GetUnused( p_this, psz_name );
948 vlc_mutex_unlock( &p_priv->var_lock );
952 p_var = &p_priv->p_vars[i_var];
954 /* Backup needed stuff */
957 /* Deal with callbacks. Tell we're in a callback, release the lock,
958 * call stored functions, retake the lock. */
959 if( p_var->i_entries )
962 int i_entries = p_var->i_entries;
963 callback_entry_t *p_entries = p_var->p_entries;
965 p_var->b_incallback = true;
966 vlc_mutex_unlock( &p_priv->var_lock );
969 for( ; i_entries-- ; )
971 p_entries[i_entries].pf_callback( p_this, psz_name, oldval, oldval,
972 p_entries[i_entries].p_data );
975 vlc_mutex_lock( &p_priv->var_lock );
977 i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
980 msg_Err( p_this, "variable %s has disappeared", psz_name );
981 vlc_mutex_unlock( &p_priv->var_lock );
985 p_var = &p_priv->p_vars[i_var];
986 p_var->b_incallback = false;
987 vlc_cond_broadcast( &p_priv->var_wait );
990 vlc_mutex_unlock( &p_priv->var_lock );
994 /** Parse a stringified option
995 * This function parse a string option and create the associated object
997 * The option must be of the form "[no[-]]foo[=bar]" where foo is the
998 * option name and bar is the value of the option.
999 * \param p_obj the object in which the variable must be created
1000 * \param psz_option the option to parse
1001 * \param trusted whether the option is set by a trusted input or not
1004 void var_OptionParse( vlc_object_t *p_obj, const char *psz_option,
1007 char *psz_name, *psz_value;
1009 bool b_isno = false;
1012 val.psz_string = NULL;
1014 /* It's too much of a hassle to remove the ':' when we parse
1015 * the cmd line :) */
1016 if( psz_option[0] == ':' )
1019 if( !psz_option[0] )
1022 psz_name = strdup( psz_option );
1023 if( psz_name == NULL )
1026 psz_value = strchr( psz_name, '=' );
1027 if( psz_value != NULL )
1028 *psz_value++ = '\0';
1030 /* FIXME: :programs should be handled generically */
1031 if( !strcmp( psz_name, "programs" ) )
1032 i_type = VLC_VAR_LIST;
1034 i_type = config_GetType( p_obj, psz_name );
1036 if( !i_type && !psz_value )
1038 /* check for "no-foo" or "nofoo" */
1039 if( !strncmp( psz_name, "no-", 3 ) )
1041 memmove( psz_name, psz_name + 3, strlen(psz_name) + 1 - 3 );
1043 else if( !strncmp( psz_name, "no", 2 ) )
1045 memmove( psz_name, psz_name + 2, strlen(psz_name) + 1 - 2 );
1047 else goto cleanup; /* Option doesn't exist */
1050 i_type = config_GetType( p_obj, psz_name );
1052 if( !i_type ) goto cleanup; /* Option doesn't exist */
1054 if( ( i_type != VLC_VAR_BOOL ) &&
1055 ( !psz_value || !*psz_value ) ) goto cleanup; /* Invalid value */
1057 /* check if option is unsafe */
1060 module_config_t *p_config = config_FindConfig( p_obj, psz_name );
1061 if( !p_config || !p_config->b_safe )
1063 msg_Err( p_obj, "unsafe option \"%s\" has been ignored for "
1064 "security reasons", psz_name );
1070 /* Create the variable in the input object.
1071 * Children of the input object will be able to retreive this value
1072 * thanks to the inheritance property of the object variables. */
1073 var_Create( p_obj, psz_name, i_type );
1078 val.b_bool = !b_isno;
1081 case VLC_VAR_INTEGER:
1082 val.i_int = strtol( psz_value, NULL, 0 );
1086 val.f_float = atof( psz_value );
1089 case VLC_VAR_STRING:
1090 case VLC_VAR_MODULE:
1092 case VLC_VAR_DIRECTORY:
1093 val.psz_string = psz_value;
1098 char *psz_orig, *psz_var;
1099 vlc_list_t *p_list = malloc(sizeof(vlc_list_t));
1100 val.p_list = p_list;
1101 p_list->i_count = 0;
1103 psz_var = psz_orig = strdup(psz_value);
1104 while( psz_var && *psz_var )
1106 char *psz_item = psz_var;
1108 while( *psz_var && *psz_var != ',' ) psz_var++;
1109 if( *psz_var == ',' )
1114 val2.i_int = strtol( psz_item, NULL, 0 );
1115 INSERT_ELEM( p_list->p_values, p_list->i_count,
1116 p_list->i_count, val2 );
1117 /* p_list->i_count is incremented twice by INSERT_ELEM */
1119 INSERT_ELEM( p_list->pi_types, p_list->i_count,
1120 p_list->i_count, VLC_VAR_INTEGER );
1130 var_Set( p_obj, psz_name, val );
1132 // If that's a list, remove all elements allocated
1133 if( i_type == VLC_VAR_LIST )
1141 /* Following functions are local */
1143 /*****************************************************************************
1144 * GetUnused: find an unused (not in callback) variable from its name
1145 *****************************************************************************
1146 * We do i_tries tries before giving up, just in case the variable is being
1147 * modified and called from a callback.
1148 *****************************************************************************/
1149 static int GetUnused( vlc_object_t *p_this, const char *psz_name )
1151 vlc_object_internals_t *p_priv = vlc_internals( p_this );
1157 i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
1163 if( ! p_priv->p_vars[i_var].b_incallback )
1168 mutex_cleanup_push( &p_priv->var_lock );
1169 vlc_cond_wait( &p_priv->var_wait, &p_priv->var_lock );
1174 /*****************************************************************************
1175 * HashString: our cool hash function
1176 *****************************************************************************
1177 * This function is not intended to be crypto-secure, we only want it to be
1178 * fast and not suck too much. This one is pretty fast and did 0 collisions
1179 * in wenglish's dictionary.
1180 *****************************************************************************/
1181 static uint32_t HashString( const char *psz_string )
1183 uint32_t i_hash = 0;
1185 while( *psz_string )
1187 i_hash += *psz_string++;
1188 i_hash += i_hash << 10;
1189 i_hash ^= i_hash >> 8;
1195 /*****************************************************************************
1196 * Insert: find an empty slot to insert a new variable
1197 *****************************************************************************
1198 * We use a recursive inner function indexed on the hash. This function does
1199 * nothing in the rare cases where a collision may occur, see Lookup()
1200 * to see how we handle them.
1201 * XXX: does this really need to be written recursively?
1202 *****************************************************************************/
1203 static int Insert( variable_t *p_vars, int i_count, const char *psz_name )
1210 return InsertInner( p_vars, i_count, HashString( psz_name ) );
1213 static int InsertInner( variable_t *p_vars, int i_count, uint32_t i_hash )
1217 if( i_hash <= p_vars[0].i_hash )
1222 if( i_hash >= p_vars[i_count - 1].i_hash )
1227 i_middle = i_count / 2;
1229 /* We know that 0 < i_middle */
1230 if( i_hash < p_vars[i_middle].i_hash )
1232 return InsertInner( p_vars, i_middle, i_hash );
1235 /* We know that i_middle + 1 < i_count */
1236 if( i_hash > p_vars[i_middle + 1].i_hash )
1238 return i_middle + 1 + InsertInner( p_vars + i_middle + 1,
1239 i_count - i_middle - 1,
1243 return i_middle + 1;
1246 static int u32cmp( const void *key, const void *data )
1248 const variable_t *p_var = data;
1249 uint32_t hash = *(const uint32_t *)key ;
1251 if( hash > p_var->i_hash )
1253 if( hash < p_var->i_hash )
1258 /*****************************************************************************
1259 * Lookup: find an existing variable given its name
1260 *****************************************************************************
1261 * We use a recursive inner function indexed on the hash. Care is taken of
1262 * possible hash collisions.
1263 *****************************************************************************/
1264 static int Lookup( variable_t *p_vars, size_t i_count, const char *psz_name )
1269 i_hash = HashString( psz_name );
1270 p_var = bsearch( &i_hash, p_vars, i_count, sizeof( *p_var ), u32cmp );
1272 /* Hash not found */
1276 assert( i_count > 0 );
1278 /* Find the first entry with the right hash */
1279 while( (p_var > p_vars) && (i_hash == p_var[-1].i_hash) )
1282 assert( p_var->i_hash == i_hash );
1284 /* Hash collision should be very unlikely, but we cannot guarantee
1285 * it will never happen. So we do an exhaustive search amongst all
1286 * entries with the same hash. Typically, there is only one anyway. */
1287 for( variable_t *p_end = p_vars + i_count;
1288 (p_var < p_end) && (i_hash == p_var->i_hash);
1291 if( !strcmp( psz_name, p_var->psz_name ) )
1292 return p_var - p_vars;
1295 /* Hash found, but entry not found */
1299 /*****************************************************************************
1300 * CheckValue: check that a value is valid wrt. a variable
1301 *****************************************************************************
1302 * This function checks p_val's value against p_var's limitations such as
1303 * minimal and maximal value, step, in-list position, and modifies p_val if
1305 ****************************************************************************/
1306 static void CheckValue ( variable_t *p_var, vlc_value_t *p_val )
1308 /* Check that our variable is in the list */
1309 if( p_var->i_type & VLC_VAR_HASCHOICE && p_var->choices.i_count )
1313 /* FIXME: the list is sorted, dude. Use something cleverer. */
1314 for( i = p_var->choices.i_count ; i-- ; )
1316 if( p_var->ops->pf_cmp( *p_val, p_var->choices.p_values[i] ) == 0 )
1322 /* If not found, change it to anything vaguely valid */
1325 /* Free the old variable, get the new one, dup it */
1326 p_var->ops->pf_free( p_val );
1327 *p_val = p_var->choices.p_values[p_var->i_default >= 0
1328 ? p_var->i_default : 0 ];
1329 p_var->ops->pf_dup( p_val );
1333 /* Check that our variable is within the bounds */
1334 switch( p_var->i_type & VLC_VAR_TYPE )
1336 case VLC_VAR_INTEGER:
1337 if( p_var->i_type & VLC_VAR_HASSTEP && p_var->step.i_int
1338 && (p_val->i_int % p_var->step.i_int) )
1340 p_val->i_int = (p_val->i_int + (p_var->step.i_int / 2))
1341 / p_var->step.i_int * p_var->step.i_int;
1343 if( p_var->i_type & VLC_VAR_HASMIN
1344 && p_val->i_int < p_var->min.i_int )
1346 p_val->i_int = p_var->min.i_int;
1348 if( p_var->i_type & VLC_VAR_HASMAX
1349 && p_val->i_int > p_var->max.i_int )
1351 p_val->i_int = p_var->max.i_int;
1355 if( p_var->i_type & VLC_VAR_HASSTEP && p_var->step.f_float )
1357 float f_round = p_var->step.f_float * (float)(int)( 0.5 +
1358 p_val->f_float / p_var->step.f_float );
1359 if( p_val->f_float != f_round )
1361 p_val->f_float = f_round;
1364 if( p_var->i_type & VLC_VAR_HASMIN
1365 && p_val->f_float < p_var->min.f_float )
1367 p_val->f_float = p_var->min.f_float;
1369 if( p_var->i_type & VLC_VAR_HASMAX
1370 && p_val->f_float > p_var->max.f_float )
1372 p_val->f_float = p_var->max.f_float;
1381 /*****************************************************************************
1382 * InheritValue: try to inherit the value of this variable from the same one
1383 * in our closest parent, libvlc or ultimately from the configuration.
1384 * The function should always be entered with the object var_lock locked.
1385 *****************************************************************************/
1386 static int InheritValue( vlc_object_t *p_this, const char *psz_name,
1387 vlc_value_t *p_val, int i_type )
1392 if( p_this->p_parent || ( p_this->p_libvlc && p_this != (vlc_object_t*) p_this->p_libvlc ) )
1394 vlc_object_internals_t *p_priv;
1396 if( p_this->p_parent )
1397 p_priv = vlc_internals( p_this->p_parent );
1399 p_priv = vlc_internals( p_this->p_libvlc );
1401 i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
1406 p_var = &p_priv->p_vars[i_var];
1408 /* Really get the variable */
1409 *p_val = p_var->val;
1411 /* Duplicate value if needed */
1412 p_var->ops->pf_dup( p_val );
1414 /*msg_Dbg( p_this, "Inherited value for var %s from object %s",
1415 psz_name ? psz_name : "(null)",
1416 p_this->psz_object_name
1417 ? p_this->psz_object_name : "(Unknown)" );*/
1420 else if ( p_this->p_parent ) /* We are still not there */
1421 return InheritValue( p_this->p_parent, psz_name, p_val, i_type );
1423 /* else take value from config */
1427 switch( i_type & VLC_VAR_CLASS )
1429 case VLC_VAR_STRING:
1430 p_val->psz_string = config_GetPsz( p_this, psz_name );
1431 if( !p_val->psz_string ) p_val->psz_string = strdup("");
1434 p_val->f_float = config_GetFloat( p_this, psz_name );
1436 case VLC_VAR_INTEGER:
1437 p_val->i_int = config_GetInt( p_this, psz_name );
1440 p_val->b_bool = config_GetInt( p_this, psz_name );
1444 char *psz_orig, *psz_var;
1445 vlc_list_t *p_list = malloc(sizeof(vlc_list_t));
1446 p_val->p_list = p_list;
1447 p_list->i_count = 0;
1449 psz_var = psz_orig = config_GetPsz( p_this, psz_name );
1450 while( psz_var && *psz_var )
1452 char *psz_item = psz_var;
1454 while( *psz_var && *psz_var != ',' ) psz_var++;
1455 if( *psz_var == ',' )
1460 val.i_int = strtol( psz_item, NULL, 0 );
1461 INSERT_ELEM( p_list->p_values, p_list->i_count,
1462 p_list->i_count, val );
1463 /* p_list->i_count is incremented twice by INSERT_ELEM */
1465 INSERT_ELEM( p_list->pi_types, p_list->i_count,
1466 p_list->i_count, VLC_VAR_INTEGER );
1472 msg_Warn( p_this, "Could not inherit value for var %s "
1473 "from config. Invalid Type", psz_name );
1477 /*msg_Dbg( p_this, "Inherited value for var %s from config", psz_name );*/
1481 /**********************************************************************
1482 * Execute a var command on an object identified by its name
1483 **********************************************************************/
1484 int __var_Command( vlc_object_t *p_this, const char *psz_name,
1485 const char *psz_cmd, const char *psz_arg, char **psz_msg )
1487 vlc_object_t *p_obj = vlc_object_find_name( p_this->p_libvlc,
1488 psz_name, FIND_CHILD );
1494 *psz_msg = strdup( "Unknown destination object." );
1498 i_type = var_Type( p_obj, psz_cmd );
1499 if( !( i_type&VLC_VAR_ISCOMMAND ) )
1501 vlc_object_release( p_obj );
1503 *psz_msg = strdup( "Variable doesn't exist or isn't a command." );
1504 return VLC_EGENERIC;
1507 i_type &= VLC_VAR_CLASS;
1510 case VLC_VAR_INTEGER:
1511 i_ret = var_SetInteger( p_obj, psz_cmd, atoi( psz_arg ) );
1514 i_ret = var_SetFloat( p_obj, psz_cmd, atof( psz_arg ) );
1516 case VLC_VAR_STRING:
1517 i_ret = var_SetString( p_obj, psz_cmd, psz_arg );
1520 i_ret = var_SetBool( p_obj, psz_cmd, atoi( psz_arg ) );
1523 i_ret = VLC_EGENERIC;
1527 vlc_object_release( p_obj );
1531 if( asprintf( psz_msg, "%s on object %s returned %i (%s)",
1532 psz_cmd, psz_name, i_ret, vlc_error( i_ret ) ) == -1)