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 ) { p_val->psz_string = strdup( p_val->psz_string ?: ""); }
73 static void DupList( vlc_value_t *p_val )
76 vlc_list_t *p_list = malloc( sizeof(vlc_list_t) );
78 p_list->i_count = p_val->p_list->i_count;
79 if( p_val->p_list->i_count )
81 p_list->p_values = malloc( p_list->i_count * sizeof(vlc_value_t) );
82 p_list->pi_types = malloc( p_list->i_count * sizeof(int) );
86 p_list->p_values = NULL;
87 p_list->pi_types = NULL;
90 for( i = 0; i < p_list->i_count; i++ )
92 p_list->p_values[i] = p_val->p_list->p_values[i];
93 p_list->pi_types[i] = p_val->p_list->pi_types[i];
94 switch( p_val->p_list->pi_types[i] & VLC_VAR_CLASS )
98 DupString( &p_list->p_values[i] );
105 p_val->p_list = p_list;
108 static void FreeDummy( vlc_value_t *p_val ) { (void)p_val; /* unused */ }
109 static void FreeString( vlc_value_t *p_val ) { free( p_val->psz_string ); }
110 static void FreeMutex( vlc_value_t *p_val ) { vlc_mutex_destroy( (vlc_mutex_t*)p_val->p_address ); free( p_val->p_address ); }
112 static void FreeList( vlc_value_t *p_val )
115 for( i = 0; i < p_val->p_list->i_count; i++ )
117 switch( p_val->p_list->pi_types[i] & VLC_VAR_CLASS )
120 FreeString( &p_val->p_list->p_values[i] );
123 FreeMutex( &p_val->p_list->p_values[i] );
130 if( p_val->p_list->i_count )
132 free( p_val->p_list->p_values );
133 free( p_val->p_list->pi_types );
135 free( p_val->p_list );
138 static const struct variable_ops_t
139 void_ops = { NULL, DupDummy, FreeDummy, },
140 addr_ops = { CmpAddress, DupDummy, FreeDummy, },
141 bool_ops = { CmpBool, DupDummy, FreeDummy, },
142 float_ops = { CmpFloat, DupDummy, FreeDummy, },
143 int_ops = { CmpInt, DupDummy, FreeDummy, },
144 list_ops = { CmpAddress, DupList, FreeList, },
145 mutex_ops = { CmpAddress, DupDummy, FreeMutex, },
146 string_ops = { CmpString, DupString, FreeString, },
147 time_ops = { CmpTime, DupDummy, FreeDummy, };
149 /*****************************************************************************
151 *****************************************************************************/
152 static int GetUnused ( vlc_object_t *, const char * );
153 static uint32_t HashString ( const char * );
154 static int Insert ( variable_t *, int, const char * );
155 static int InsertInner ( variable_t *, int, uint32_t );
156 static int Lookup ( variable_t *, size_t, const char * );
158 static void CheckValue ( variable_t *, vlc_value_t * );
160 static int InheritValue( vlc_object_t *, const char *, vlc_value_t *,
164 * Initialize a vlc variable
166 * We hash the given string and insert it into the sorted list. The insertion
167 * may require slow memory copies, but think about what we gain in the log(n)
168 * lookup phase when setting/getting the variable value!
170 * \param p_this The object in which to create the variable
171 * \param psz_name The name of the variable
172 * \param i_type The variables type. Must be one of \ref var_type combined with
173 * zero or more \ref var_flags
175 int __var_Create( vlc_object_t *p_this, const char *psz_name, int i_type )
179 static vlc_list_t dummy_null_list = {0, NULL, NULL};
180 vlc_object_internals_t *p_priv = vlc_internals( p_this );
182 vlc_mutex_lock( &p_priv->var_lock );
184 /* FIXME: if the variable already exists, we don't duplicate it. But we
185 * duplicate the lookups. It's not that serious, but if anyone finds some
186 * time to rework Insert() so that only one lookup has to be done, feel
188 i_new = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
192 /* If the types differ, variable creation failed. */
193 if( (i_type & ~(VLC_VAR_DOINHERIT|VLC_VAR_ISCOMMAND)) != p_priv->p_vars[i_new].i_type )
195 vlc_mutex_unlock( &p_priv->var_lock );
199 p_priv->p_vars[i_new].i_usage++;
200 if( i_type & VLC_VAR_ISCOMMAND )
201 p_priv->p_vars[i_new].i_type |= VLC_VAR_ISCOMMAND;
202 vlc_mutex_unlock( &p_priv->var_lock );
206 i_new = Insert( p_priv->p_vars, p_priv->i_vars, psz_name );
208 if( (p_priv->i_vars & 15) == 15 )
210 p_priv->p_vars = realloc( p_priv->p_vars,
211 (p_priv->i_vars+17) * sizeof(variable_t) );
214 memmove( p_priv->p_vars + i_new + 1,
215 p_priv->p_vars + i_new,
216 (p_priv->i_vars - i_new) * sizeof(variable_t) );
220 p_var = &p_priv->p_vars[i_new];
221 memset( p_var, 0, sizeof(*p_var) );
223 p_var->i_hash = HashString( psz_name );
224 p_var->psz_name = strdup( psz_name );
225 p_var->psz_text = NULL;
227 p_var->i_type = i_type & ~VLC_VAR_DOINHERIT;
228 memset( &p_var->val, 0, sizeof(vlc_value_t) );
232 p_var->i_default = -1;
233 p_var->choices.i_count = 0;
234 p_var->choices.p_values = NULL;
235 p_var->choices_text.i_count = 0;
236 p_var->choices_text.p_values = NULL;
238 p_var->b_incallback = false;
239 p_var->i_entries = 0;
240 p_var->p_entries = NULL;
242 /* Always initialize the variable, even if it is a list variable; this
243 * will lead to errors if the variable is not initialized, but it will
244 * not cause crashes in the variable handling. */
245 switch( i_type & VLC_VAR_CLASS )
248 p_var->ops = &bool_ops;
249 p_var->val.b_bool = false;
251 case VLC_VAR_INTEGER:
252 p_var->ops = &int_ops;
253 p_var->val.i_int = 0;
256 p_var->ops = &string_ops;
257 p_var->val.psz_string = NULL;
260 p_var->ops = &float_ops;
261 p_var->val.f_float = 0.0;
264 p_var->ops = &time_ops;
265 p_var->val.i_time = 0;
267 case VLC_VAR_ADDRESS:
268 p_var->ops = &addr_ops;
269 p_var->val.p_address = NULL;
272 p_var->ops = &mutex_ops;
273 p_var->val.p_address = malloc( sizeof(vlc_mutex_t) );
274 vlc_mutex_init( (vlc_mutex_t*)p_var->val.p_address );
277 p_var->ops = &list_ops;
278 p_var->val.p_list = &dummy_null_list;
281 p_var->ops = &void_ops;
285 /* Duplicate the default data we stored. */
286 p_var->ops->pf_dup( &p_var->val );
288 if( i_type & VLC_VAR_DOINHERIT )
292 if( InheritValue( p_this, psz_name, &val, p_var->i_type )
295 /* Free data if needed */
296 p_var->ops->pf_free( &p_var->val );
297 /* Set the variable */
300 if( i_type & VLC_VAR_HASCHOICE )
302 /* We must add the inherited value to our choice list */
303 p_var->i_default = 0;
305 INSERT_ELEM( p_var->choices.p_values, p_var->choices.i_count,
307 INSERT_ELEM( p_var->choices_text.p_values,
308 p_var->choices_text.i_count, 0, val );
309 p_var->ops->pf_dup( &p_var->choices.p_values[0] );
310 p_var->choices_text.p_values[0].psz_string = NULL;
315 vlc_mutex_unlock( &p_priv->var_lock );
321 * Destroy a vlc variable
323 * Look for the variable and destroy it if it is found. As in var_Create we
324 * do a call to memmove() but we have performance counterparts elsewhere.
326 * \param p_this The object that holds the variable
327 * \param psz_name The name of the variable
329 int __var_Destroy( vlc_object_t *p_this, const char *psz_name )
333 vlc_object_internals_t *p_priv = vlc_internals( p_this );
335 vlc_mutex_lock( &p_priv->var_lock );
337 i_var = GetUnused( p_this, psz_name );
340 vlc_mutex_unlock( &p_priv->var_lock );
344 p_var = &p_priv->p_vars[i_var];
346 if( p_var->i_usage > 1 )
349 vlc_mutex_unlock( &p_priv->var_lock );
353 /* Free value if needed */
354 p_var->ops->pf_free( &p_var->val );
356 /* Free choice list if needed */
357 if( p_var->choices.i_count )
359 for( i = 0 ; i < p_var->choices.i_count ; i++ )
361 p_var->ops->pf_free( &p_var->choices.p_values[i] );
362 free( p_var->choices_text.p_values[i].psz_string );
364 free( p_var->choices.p_values );
365 free( p_var->choices_text.p_values );
368 /* Free callbacks if needed */
369 if( p_var->p_entries )
371 free( p_var->p_entries );
374 free( p_var->psz_name );
375 free( p_var->psz_text );
377 memmove( p_priv->p_vars + i_var,
378 p_priv->p_vars + i_var + 1,
379 (p_priv->i_vars - i_var - 1) * sizeof(variable_t) );
381 if( (p_priv->i_vars & 15) == 0 )
383 p_priv->p_vars = realloc( p_priv->p_vars,
384 (p_priv->i_vars) * sizeof( variable_t ) );
389 vlc_mutex_unlock( &p_priv->var_lock );
395 * Perform an action on a variable
397 * \param p_this The object that holds the variable
398 * \param psz_name The name of the variable
399 * \param i_action The action to perform. Must be one of \ref var_action
400 * \param p_val First action parameter
401 * \param p_val2 Second action parameter
403 int __var_Change( vlc_object_t *p_this, const char *psz_name,
404 int i_action, vlc_value_t *p_val, vlc_value_t *p_val2 )
409 vlc_object_internals_t *p_priv = vlc_internals( p_this );
411 vlc_mutex_lock( &p_priv->var_lock );
413 i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
417 vlc_mutex_unlock( &p_priv->var_lock );
421 p_var = &p_priv->p_vars[i_var];
426 if( p_var->i_type & VLC_VAR_HASMIN )
428 p_var->ops->pf_free( &p_var->min );
430 p_var->i_type |= VLC_VAR_HASMIN;
432 p_var->ops->pf_dup( &p_var->min );
433 CheckValue( p_var, &p_var->val );
436 if( p_var->i_type & VLC_VAR_HASMIN )
442 if( p_var->i_type & VLC_VAR_HASMAX )
444 p_var->ops->pf_free( &p_var->max );
446 p_var->i_type |= VLC_VAR_HASMAX;
448 p_var->ops->pf_dup( &p_var->max );
449 CheckValue( p_var, &p_var->val );
452 if( p_var->i_type & VLC_VAR_HASMAX )
457 case VLC_VAR_SETSTEP:
458 if( p_var->i_type & VLC_VAR_HASSTEP )
460 p_var->ops->pf_free( &p_var->step );
462 p_var->i_type |= VLC_VAR_HASSTEP;
463 p_var->step = *p_val;
464 p_var->ops->pf_dup( &p_var->step );
465 CheckValue( p_var, &p_var->val );
467 case VLC_VAR_GETSTEP:
468 if( p_var->i_type & VLC_VAR_HASSTEP )
470 *p_val = p_var->step;
473 case VLC_VAR_ADDCHOICE:
474 i = p_var->choices.i_count;
476 INSERT_ELEM( p_var->choices.p_values, p_var->choices.i_count,
478 INSERT_ELEM( p_var->choices_text.p_values,
479 p_var->choices_text.i_count, i, *p_val );
480 p_var->ops->pf_dup( &p_var->choices.p_values[i] );
481 p_var->choices_text.p_values[i].psz_string =
482 ( p_val2 && p_val2->psz_string ) ?
483 strdup( p_val2->psz_string ) : NULL;
485 CheckValue( p_var, &p_var->val );
487 case VLC_VAR_DELCHOICE:
488 for( i = 0 ; i < p_var->choices.i_count ; i++ )
490 if( p_var->ops->pf_cmp( p_var->choices.p_values[i], *p_val ) == 0 )
496 if( i == p_var->choices.i_count )
499 vlc_mutex_unlock( &p_priv->var_lock );
503 if( p_var->i_default > i )
507 else if( p_var->i_default == i )
509 p_var->i_default = -1;
512 p_var->ops->pf_free( &p_var->choices.p_values[i] );
513 free( p_var->choices_text.p_values[i].psz_string );
514 REMOVE_ELEM( p_var->choices.p_values, p_var->choices.i_count, i );
515 REMOVE_ELEM( p_var->choices_text.p_values,
516 p_var->choices_text.i_count, i );
518 CheckValue( p_var, &p_var->val );
520 case VLC_VAR_CHOICESCOUNT:
521 p_val->i_int = p_var->choices.i_count;
523 case VLC_VAR_CLEARCHOICES:
524 for( i = 0 ; i < p_var->choices.i_count ; i++ )
526 p_var->ops->pf_free( &p_var->choices.p_values[i] );
528 for( i = 0 ; i < p_var->choices_text.i_count ; i++ )
529 free( p_var->choices_text.p_values[i].psz_string );
531 if( p_var->choices.i_count ) free( p_var->choices.p_values );
532 if( p_var->choices_text.i_count ) free( p_var->choices_text.p_values );
534 p_var->choices.i_count = 0;
535 p_var->choices.p_values = NULL;
536 p_var->choices_text.i_count = 0;
537 p_var->choices_text.p_values = NULL;
538 p_var->i_default = -1;
540 case VLC_VAR_SETDEFAULT:
541 /* FIXME: the list is sorted, dude. Use something cleverer. */
542 for( i = 0 ; i < p_var->choices.i_count ; i++ )
544 if( p_var->ops->pf_cmp( p_var->choices.p_values[i], *p_val ) == 0 )
550 if( i == p_var->choices.i_count )
556 p_var->i_default = i;
557 CheckValue( p_var, &p_var->val );
559 case VLC_VAR_SETVALUE:
560 /* Duplicate data if needed */
561 p_var->ops->pf_dup( p_val );
562 /* Backup needed stuff */
564 /* Check boundaries and list */
565 CheckValue( p_var, p_val );
566 /* Set the variable */
568 /* Free data if needed */
569 p_var->ops->pf_free( &oldval );
571 case VLC_VAR_GETCHOICES:
572 case VLC_VAR_GETLIST:
573 p_val->p_list = malloc( sizeof(vlc_list_t) );
574 if( p_val2 ) p_val2->p_list = malloc( sizeof(vlc_list_t) );
575 if( p_var->choices.i_count )
577 p_val->p_list->p_values = malloc( p_var->choices.i_count
578 * sizeof(vlc_value_t) );
579 p_val->p_list->pi_types = malloc( p_var->choices.i_count
583 p_val2->p_list->p_values =
584 malloc( p_var->choices.i_count * sizeof(vlc_value_t) );
585 p_val2->p_list->pi_types =
586 malloc( p_var->choices.i_count * sizeof(int) );
589 p_val->p_list->i_count = p_var->choices.i_count;
590 if( p_val2 ) p_val2->p_list->i_count = p_var->choices.i_count;
591 for( i = 0 ; i < p_var->choices.i_count ; i++ )
593 p_val->p_list->p_values[i] = p_var->choices.p_values[i];
594 p_val->p_list->pi_types[i] = p_var->i_type;
595 p_var->ops->pf_dup( &p_val->p_list->p_values[i] );
598 p_val2->p_list->p_values[i].psz_string =
599 p_var->choices_text.p_values[i].psz_string ?
600 strdup(p_var->choices_text.p_values[i].psz_string) : NULL;
601 p_val2->p_list->pi_types[i] = VLC_VAR_STRING;
605 case VLC_VAR_FREELIST:
607 if( p_val2 && p_val2->p_list )
609 for( i = 0; i < p_val2->p_list->i_count; i++ )
610 free( p_val2->p_list->p_values[i].psz_string );
611 if( p_val2->p_list->i_count )
613 free( p_val2->p_list->p_values );
614 free( p_val2->p_list->pi_types );
616 free( p_val2->p_list );
619 case VLC_VAR_SETTEXT:
620 free( p_var->psz_text );
621 if( p_val && p_val->psz_string )
622 p_var->psz_text = strdup( p_val->psz_string );
624 case VLC_VAR_GETTEXT:
625 p_val->psz_string = NULL;
626 if( p_var->psz_text )
628 p_val->psz_string = strdup( p_var->psz_text );
631 case VLC_VAR_INHERITVALUE:
635 if( InheritValue( p_this,
636 p_val2 ? p_val2->psz_string : psz_name,
637 &val, p_var->i_type )
640 /* Duplicate already done */
642 /* Backup needed stuff */
644 /* Check boundaries and list */
645 CheckValue( p_var, &val );
646 /* Set the variable */
648 /* Free data if needed */
649 p_var->ops->pf_free( &oldval );
655 p_var->ops->pf_dup( p_val );
660 case VLC_VAR_SETISCOMMAND:
661 p_var->i_type |= VLC_VAR_ISCOMMAND;
668 vlc_mutex_unlock( &p_priv->var_lock );
674 * Request a variable's type
676 * \return The variable type if it exists, or 0 if the
677 * variable could not be found.
680 int __var_Type( vlc_object_t *p_this, const char *psz_name )
683 vlc_object_internals_t *p_priv = vlc_internals( p_this );
685 vlc_mutex_lock( &p_priv->var_lock );
687 i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
691 vlc_mutex_unlock( &p_priv->var_lock );
695 i_type = p_priv->p_vars[i_var].i_type;
697 vlc_mutex_unlock( &p_priv->var_lock );
702 int var_SetChecked( vlc_object_t *p_this, const char *psz_name,
703 int expected_type, vlc_value_t val )
708 vlc_object_internals_t *p_priv = vlc_internals( p_this );
710 vlc_mutex_lock( &p_priv->var_lock );
712 i_var = GetUnused( p_this, psz_name );
715 vlc_mutex_unlock( &p_priv->var_lock );
719 p_var = &p_priv->p_vars[i_var];
720 assert( (p_var->i_type & VLC_VAR_CLASS) == 0 || expected_type == 0 ||
721 (p_var->i_type & VLC_VAR_CLASS) == expected_type );
723 /* Duplicate data if needed */
724 p_var->ops->pf_dup( &val );
726 /* Backup needed stuff */
729 /* Check boundaries and list */
730 CheckValue( p_var, &val );
732 /* Set the variable */
735 /* Deal with callbacks. Tell we're in a callback, release the lock,
736 * call stored functions, retake the lock. */
737 if( p_var->i_entries )
740 int i_entries = p_var->i_entries;
741 callback_entry_t *p_entries = p_var->p_entries;
743 p_var->b_incallback = true;
744 vlc_mutex_unlock( &p_priv->var_lock );
747 for( ; i_entries-- ; )
749 p_entries[i_entries].pf_callback( p_this, psz_name, oldval, val,
750 p_entries[i_entries].p_data );
753 vlc_mutex_lock( &p_priv->var_lock );
755 i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
758 msg_Err( p_this, "variable %s has disappeared", psz_name );
759 vlc_mutex_unlock( &p_priv->var_lock );
763 p_var = &p_priv->p_vars[i_var];
764 p_var->b_incallback = false;
765 vlc_cond_broadcast( &p_priv->var_wait );
768 /* Free data if needed */
769 p_var->ops->pf_free( &oldval );
771 vlc_mutex_unlock( &p_priv->var_lock );
778 * Set a variable's value
780 * \param p_this The object that hold the variable
781 * \param psz_name The name of the variable
782 * \param val the value to set
784 int __var_Set( vlc_object_t *p_this, const char *psz_name, vlc_value_t val )
786 return var_SetChecked( p_this, psz_name, 0, val );
789 int var_GetChecked( vlc_object_t *p_this, const char *psz_name,
790 int expected_type, vlc_value_t *p_val )
792 vlc_object_internals_t *p_priv = vlc_internals( p_this );
793 int i_var, err = VLC_SUCCESS;
795 vlc_mutex_lock( &p_priv->var_lock );
797 i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
800 variable_t *p_var = &p_priv->p_vars[i_var];
802 assert( (p_var->i_type & VLC_VAR_CLASS) == 0 || expected_type == 0 ||
803 (p_var->i_type & VLC_VAR_CLASS) == expected_type );
805 /* Really get the variable */
808 /* Duplicate value if needed */
809 p_var->ops->pf_dup( p_val );
814 vlc_mutex_unlock( &p_priv->var_lock );
819 * Get a variable's value
821 * \param p_this The object that holds the variable
822 * \param psz_name The name of the variable
823 * \param p_val Pointer to a vlc_value_t that will hold the variable's value
824 * after the function is finished
826 int __var_Get( vlc_object_t *p_this, const char *psz_name, vlc_value_t *p_val )
828 return var_GetChecked( p_this, psz_name, 0, p_val );
832 * Register a callback in a variable
834 * We store a function pointer that will be called upon variable
837 * \param p_this The object that holds the variable
838 * \param psz_name The name of the variable
839 * \param pf_callback The function pointer
840 * \param p_data A generic pointer that will be passed as the last
841 * argument to the callback function.
843 * \warning The callback function is run in the thread that calls var_Set on
844 * the variable. Use proper locking. This thread may not have much
845 * time to spare, so keep callback functions short.
847 int __var_AddCallback( vlc_object_t *p_this, const char *psz_name,
848 vlc_callback_t pf_callback, void *p_data )
852 callback_entry_t entry;
853 vlc_object_internals_t *p_priv = vlc_internals( p_this );
855 entry.pf_callback = pf_callback;
856 entry.p_data = p_data;
858 vlc_mutex_lock( &p_priv->var_lock );
860 i_var = GetUnused( p_this, psz_name );
863 vlc_mutex_unlock( &p_priv->var_lock );
867 p_var = &p_priv->p_vars[i_var];
869 INSERT_ELEM( p_var->p_entries,
874 vlc_mutex_unlock( &p_priv->var_lock );
880 * Remove a callback from a variable
882 * pf_callback and p_data have to be given again, because different objects
883 * might have registered the same callback function.
885 int __var_DelCallback( vlc_object_t *p_this, const char *psz_name,
886 vlc_callback_t pf_callback, void *p_data )
890 vlc_object_internals_t *p_priv = vlc_internals( p_this );
892 vlc_mutex_lock( &p_priv->var_lock );
894 i_var = GetUnused( p_this, psz_name );
897 vlc_mutex_unlock( &p_priv->var_lock );
901 p_var = &p_priv->p_vars[i_var];
903 for( i_entry = p_var->i_entries ; i_entry-- ; )
905 if( p_var->p_entries[i_entry].pf_callback == pf_callback
906 && p_var->p_entries[i_entry].p_data == p_data )
914 vlc_mutex_unlock( &p_priv->var_lock );
918 REMOVE_ELEM( p_var->p_entries, p_var->i_entries, i_entry );
920 vlc_mutex_unlock( &p_priv->var_lock );
926 * Trigger callback on a variable
928 * \param p_this The object that hold the variable
929 * \param psz_name The name of the variable
931 int __var_TriggerCallback( vlc_object_t *p_this, const char *psz_name )
936 vlc_object_internals_t *p_priv = vlc_internals( p_this );
938 vlc_mutex_lock( &p_priv->var_lock );
940 i_var = GetUnused( p_this, psz_name );
943 vlc_mutex_unlock( &p_priv->var_lock );
947 p_var = &p_priv->p_vars[i_var];
949 /* Backup needed stuff */
952 /* Deal with callbacks. Tell we're in a callback, release the lock,
953 * call stored functions, retake the lock. */
954 if( p_var->i_entries )
957 int i_entries = p_var->i_entries;
958 callback_entry_t *p_entries = p_var->p_entries;
960 p_var->b_incallback = true;
961 vlc_mutex_unlock( &p_priv->var_lock );
964 for( ; i_entries-- ; )
966 p_entries[i_entries].pf_callback( p_this, psz_name, oldval, oldval,
967 p_entries[i_entries].p_data );
970 vlc_mutex_lock( &p_priv->var_lock );
972 i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
975 msg_Err( p_this, "variable %s has disappeared", psz_name );
976 vlc_mutex_unlock( &p_priv->var_lock );
980 p_var = &p_priv->p_vars[i_var];
981 p_var->b_incallback = false;
982 vlc_cond_broadcast( &p_priv->var_wait );
985 vlc_mutex_unlock( &p_priv->var_lock );
989 /** Parse a stringified option
990 * This function parse a string option and create the associated object
992 * The option must be of the form "[no[-]]foo[=bar]" where foo is the
993 * option name and bar is the value of the option.
994 * \param p_obj the object in which the variable must be created
995 * \param psz_option the option to parse
996 * \param trusted whether the option is set by a trusted input or not
999 void var_OptionParse( vlc_object_t *p_obj, const char *psz_option,
1002 char *psz_name, *psz_value;
1004 bool b_isno = false;
1007 val.psz_string = NULL;
1009 /* It's too much of a hassle to remove the ':' when we parse
1010 * the cmd line :) */
1011 if( psz_option[0] == ':' )
1014 if( !psz_option[0] )
1017 psz_name = strdup( psz_option );
1018 if( psz_name == NULL )
1021 psz_value = strchr( psz_name, '=' );
1022 if( psz_value != NULL )
1023 *psz_value++ = '\0';
1025 /* FIXME: :programs should be handled generically */
1026 if( !strcmp( psz_name, "programs" ) )
1027 i_type = VLC_VAR_LIST;
1029 i_type = config_GetType( p_obj, psz_name );
1031 if( !i_type && !psz_value )
1033 /* check for "no-foo" or "nofoo" */
1034 if( !strncmp( psz_name, "no-", 3 ) )
1036 memmove( psz_name, psz_name + 3, strlen(psz_name) + 1 - 3 );
1038 else if( !strncmp( psz_name, "no", 2 ) )
1040 memmove( psz_name, psz_name + 2, strlen(psz_name) + 1 - 2 );
1042 else goto cleanup; /* Option doesn't exist */
1045 i_type = config_GetType( p_obj, psz_name );
1047 if( !i_type ) goto cleanup; /* Option doesn't exist */
1049 if( ( i_type != VLC_VAR_BOOL ) &&
1050 ( !psz_value || !*psz_value ) ) goto cleanup; /* Invalid value */
1052 /* check if option is unsafe */
1055 module_config_t *p_config = config_FindConfig( p_obj, psz_name );
1056 if( !p_config || !p_config->b_safe )
1058 msg_Err( p_obj, "unsafe option \"%s\" has been ignored for "
1059 "security reasons", psz_name );
1065 /* Create the variable in the input object.
1066 * Children of the input object will be able to retreive this value
1067 * thanks to the inheritance property of the object variables. */
1068 var_Create( p_obj, psz_name, i_type );
1073 val.b_bool = !b_isno;
1076 case VLC_VAR_INTEGER:
1077 val.i_int = strtol( psz_value, NULL, 0 );
1081 val.f_float = atof( psz_value );
1084 case VLC_VAR_STRING:
1085 case VLC_VAR_MODULE:
1087 case VLC_VAR_DIRECTORY:
1088 val.psz_string = psz_value;
1093 char *psz_orig, *psz_var;
1094 vlc_list_t *p_list = malloc(sizeof(vlc_list_t));
1095 val.p_list = p_list;
1096 p_list->i_count = 0;
1098 psz_var = psz_orig = strdup(psz_value);
1099 while( psz_var && *psz_var )
1101 char *psz_item = psz_var;
1103 while( *psz_var && *psz_var != ',' ) psz_var++;
1104 if( *psz_var == ',' )
1109 val2.i_int = strtol( psz_item, NULL, 0 );
1110 INSERT_ELEM( p_list->p_values, p_list->i_count,
1111 p_list->i_count, val2 );
1112 /* p_list->i_count is incremented twice by INSERT_ELEM */
1114 INSERT_ELEM( p_list->pi_types, p_list->i_count,
1115 p_list->i_count, VLC_VAR_INTEGER );
1125 var_Set( p_obj, psz_name, val );
1127 // If that's a list, remove all elements allocated
1128 if( i_type == VLC_VAR_LIST )
1136 /* Following functions are local */
1138 /*****************************************************************************
1139 * GetUnused: find an unused (not in callback) variable from its name
1140 *****************************************************************************
1141 * We do i_tries tries before giving up, just in case the variable is being
1142 * modified and called from a callback.
1143 *****************************************************************************/
1144 static int GetUnused( vlc_object_t *p_this, const char *psz_name )
1146 vlc_object_internals_t *p_priv = vlc_internals( p_this );
1152 i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
1158 if( ! p_priv->p_vars[i_var].b_incallback )
1163 mutex_cleanup_push( &p_priv->var_lock );
1164 vlc_cond_wait( &p_priv->var_wait, &p_priv->var_lock );
1169 /*****************************************************************************
1170 * HashString: our cool hash function
1171 *****************************************************************************
1172 * This function is not intended to be crypto-secure, we only want it to be
1173 * fast and not suck too much. This one is pretty fast and did 0 collisions
1174 * in wenglish's dictionary.
1175 *****************************************************************************/
1176 static uint32_t HashString( const char *psz_string )
1178 uint32_t i_hash = 0;
1180 while( *psz_string )
1182 i_hash += *psz_string++;
1183 i_hash += i_hash << 10;
1184 i_hash ^= i_hash >> 8;
1190 /*****************************************************************************
1191 * Insert: find an empty slot to insert a new variable
1192 *****************************************************************************
1193 * We use a recursive inner function indexed on the hash. This function does
1194 * nothing in the rare cases where a collision may occur, see Lookup()
1195 * to see how we handle them.
1196 * XXX: does this really need to be written recursively?
1197 *****************************************************************************/
1198 static int Insert( variable_t *p_vars, int i_count, const char *psz_name )
1205 return InsertInner( p_vars, i_count, HashString( psz_name ) );
1208 static int InsertInner( variable_t *p_vars, int i_count, uint32_t i_hash )
1212 if( i_hash <= p_vars[0].i_hash )
1217 if( i_hash >= p_vars[i_count - 1].i_hash )
1222 i_middle = i_count / 2;
1224 /* We know that 0 < i_middle */
1225 if( i_hash < p_vars[i_middle].i_hash )
1227 return InsertInner( p_vars, i_middle, i_hash );
1230 /* We know that i_middle + 1 < i_count */
1231 if( i_hash > p_vars[i_middle + 1].i_hash )
1233 return i_middle + 1 + InsertInner( p_vars + i_middle + 1,
1234 i_count - i_middle - 1,
1238 return i_middle + 1;
1241 static int u32cmp( const void *key, const void *data )
1243 const variable_t *p_var = data;
1244 uint32_t hash = *(const uint32_t *)key ;
1246 if( hash > p_var->i_hash )
1248 if( hash < p_var->i_hash )
1253 /*****************************************************************************
1254 * Lookup: find an existing variable given its name
1255 *****************************************************************************
1256 * We use a recursive inner function indexed on the hash. Care is taken of
1257 * possible hash collisions.
1258 *****************************************************************************/
1259 static int Lookup( variable_t *p_vars, size_t i_count, const char *psz_name )
1264 i_hash = HashString( psz_name );
1265 p_var = bsearch( &i_hash, p_vars, i_count, sizeof( *p_var ), u32cmp );
1267 /* Hash not found */
1271 assert( i_count > 0 );
1273 /* Find the first entry with the right hash */
1274 while( (p_var > p_vars) && (i_hash == p_var[-1].i_hash) )
1277 assert( p_var->i_hash == i_hash );
1279 /* Hash collision should be very unlikely, but we cannot guarantee
1280 * it will never happen. So we do an exhaustive search amongst all
1281 * entries with the same hash. Typically, there is only one anyway. */
1282 for( variable_t *p_end = p_vars + i_count;
1283 (p_var < p_end) && (i_hash == p_var->i_hash);
1286 if( !strcmp( psz_name, p_var->psz_name ) )
1287 return p_var - p_vars;
1290 /* Hash found, but entry not found */
1294 /*****************************************************************************
1295 * CheckValue: check that a value is valid wrt. a variable
1296 *****************************************************************************
1297 * This function checks p_val's value against p_var's limitations such as
1298 * minimal and maximal value, step, in-list position, and modifies p_val if
1300 ****************************************************************************/
1301 static void CheckValue ( variable_t *p_var, vlc_value_t *p_val )
1303 /* Check that our variable is in the list */
1304 if( p_var->i_type & VLC_VAR_HASCHOICE && p_var->choices.i_count )
1308 /* FIXME: the list is sorted, dude. Use something cleverer. */
1309 for( i = p_var->choices.i_count ; i-- ; )
1311 if( p_var->ops->pf_cmp( *p_val, p_var->choices.p_values[i] ) == 0 )
1317 /* If not found, change it to anything vaguely valid */
1320 /* Free the old variable, get the new one, dup it */
1321 p_var->ops->pf_free( p_val );
1322 *p_val = p_var->choices.p_values[p_var->i_default >= 0
1323 ? p_var->i_default : 0 ];
1324 p_var->ops->pf_dup( p_val );
1328 /* Check that our variable is within the bounds */
1329 switch( p_var->i_type & VLC_VAR_TYPE )
1331 case VLC_VAR_INTEGER:
1332 if( p_var->i_type & VLC_VAR_HASSTEP && p_var->step.i_int
1333 && (p_val->i_int % p_var->step.i_int) )
1335 p_val->i_int = (p_val->i_int + (p_var->step.i_int / 2))
1336 / p_var->step.i_int * p_var->step.i_int;
1338 if( p_var->i_type & VLC_VAR_HASMIN
1339 && p_val->i_int < p_var->min.i_int )
1341 p_val->i_int = p_var->min.i_int;
1343 if( p_var->i_type & VLC_VAR_HASMAX
1344 && p_val->i_int > p_var->max.i_int )
1346 p_val->i_int = p_var->max.i_int;
1350 if( p_var->i_type & VLC_VAR_HASSTEP && p_var->step.f_float )
1352 float f_round = p_var->step.f_float * (float)(int)( 0.5 +
1353 p_val->f_float / p_var->step.f_float );
1354 if( p_val->f_float != f_round )
1356 p_val->f_float = f_round;
1359 if( p_var->i_type & VLC_VAR_HASMIN
1360 && p_val->f_float < p_var->min.f_float )
1362 p_val->f_float = p_var->min.f_float;
1364 if( p_var->i_type & VLC_VAR_HASMAX
1365 && p_val->f_float > p_var->max.f_float )
1367 p_val->f_float = p_var->max.f_float;
1376 /*****************************************************************************
1377 * InheritValue: try to inherit the value of this variable from the same one
1378 * in our closest parent, libvlc or ultimately from the configuration.
1379 * The function should always be entered with the object var_lock locked.
1380 *****************************************************************************/
1381 static int InheritValue( vlc_object_t *p_this, const char *psz_name,
1382 vlc_value_t *p_val, int i_type )
1387 if( p_this->p_parent || ( p_this->p_libvlc && p_this != (vlc_object_t*) p_this->p_libvlc ) )
1389 vlc_object_internals_t *p_priv;
1391 if( p_this->p_parent )
1392 p_priv = vlc_internals( p_this->p_parent );
1394 p_priv = vlc_internals( p_this->p_libvlc );
1396 i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
1401 p_var = &p_priv->p_vars[i_var];
1403 /* Really get the variable */
1404 *p_val = p_var->val;
1406 /* Duplicate value if needed */
1407 p_var->ops->pf_dup( p_val );
1409 /*msg_Dbg( p_this, "Inherited value for var %s from object %s",
1410 psz_name ? : "(null)",
1411 p_this->psz_object_name ? : "(Unknown)" );*/
1414 else if ( p_this->p_parent ) /* We are still not there */
1415 return InheritValue( p_this->p_parent, psz_name, p_val, i_type );
1417 /* else take value from config */
1421 switch( i_type & VLC_VAR_CLASS )
1423 case VLC_VAR_STRING:
1424 p_val->psz_string = config_GetPsz( p_this, psz_name );
1425 if( !p_val->psz_string ) p_val->psz_string = strdup("");
1428 p_val->f_float = config_GetFloat( p_this, psz_name );
1430 case VLC_VAR_INTEGER:
1431 p_val->i_int = config_GetInt( p_this, psz_name );
1434 p_val->b_bool = config_GetInt( p_this, psz_name );
1438 char *psz_orig, *psz_var;
1439 vlc_list_t *p_list = malloc(sizeof(vlc_list_t));
1440 p_val->p_list = p_list;
1441 p_list->i_count = 0;
1443 psz_var = psz_orig = config_GetPsz( p_this, psz_name );
1444 while( psz_var && *psz_var )
1446 char *psz_item = psz_var;
1448 while( *psz_var && *psz_var != ',' ) psz_var++;
1449 if( *psz_var == ',' )
1454 val.i_int = strtol( psz_item, NULL, 0 );
1455 INSERT_ELEM( p_list->p_values, p_list->i_count,
1456 p_list->i_count, val );
1457 /* p_list->i_count is incremented twice by INSERT_ELEM */
1459 INSERT_ELEM( p_list->pi_types, p_list->i_count,
1460 p_list->i_count, VLC_VAR_INTEGER );
1466 msg_Warn( p_this, "Could not inherit value for var %s "
1467 "from config. Invalid Type", psz_name );
1471 /*msg_Dbg( p_this, "Inherited value for var %s from config", psz_name );*/
1475 /**********************************************************************
1476 * Execute a var command on an object identified by its name
1477 **********************************************************************/
1478 int __var_Command( vlc_object_t *p_this, const char *psz_name,
1479 const char *psz_cmd, const char *psz_arg, char **psz_msg )
1481 vlc_object_t *p_obj = vlc_object_find_name( p_this->p_libvlc,
1482 psz_name, FIND_CHILD );
1488 *psz_msg = strdup( "Unknown destination object." );
1492 i_type = var_Type( p_obj, psz_cmd );
1493 if( !( i_type&VLC_VAR_ISCOMMAND ) )
1495 vlc_object_release( p_obj );
1497 *psz_msg = strdup( "Variable doesn't exist or isn't a command." );
1498 return VLC_EGENERIC;
1501 i_type &= VLC_VAR_CLASS;
1504 case VLC_VAR_INTEGER:
1505 i_ret = var_SetInteger( p_obj, psz_cmd, atoi( psz_arg ) );
1508 i_ret = var_SetFloat( p_obj, psz_cmd, atof( psz_arg ) );
1510 case VLC_VAR_STRING:
1511 i_ret = var_SetString( p_obj, psz_cmd, psz_arg );
1514 i_ret = var_SetBool( p_obj, psz_cmd, atoi( psz_arg ) );
1517 i_ret = VLC_EGENERIC;
1521 vlc_object_release( p_obj );
1525 if( asprintf( psz_msg, "%s on object %s returned %i (%s)",
1526 psz_cmd, psz_name, i_ret, vlc_error( i_ret ) ) == -1)