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 ) { if( p_val->psz_string ) 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;
767 /* Free data if needed */
768 p_var->ops->pf_free( &oldval );
770 vlc_mutex_unlock( &p_priv->var_lock );
777 * Set a variable's value
779 * \param p_this The object that hold the variable
780 * \param psz_name The name of the variable
781 * \param val the value to set
783 int __var_Set( vlc_object_t *p_this, const char *psz_name, vlc_value_t val )
785 return var_SetChecked( p_this, psz_name, 0, val );
788 int var_GetChecked( vlc_object_t *p_this, const char *psz_name,
789 int expected_type, vlc_value_t *p_val )
791 vlc_object_internals_t *p_priv = vlc_internals( p_this );
792 int i_var, err = VLC_SUCCESS;
794 vlc_mutex_lock( &p_priv->var_lock );
796 i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
799 variable_t *p_var = &p_priv->p_vars[i_var];
801 assert( (p_var->i_type & VLC_VAR_CLASS) == 0 || expected_type == 0 ||
802 (p_var->i_type & VLC_VAR_CLASS) == expected_type );
804 /* Really get the variable */
807 /* Duplicate value if needed */
808 p_var->ops->pf_dup( p_val );
813 vlc_mutex_unlock( &p_priv->var_lock );
818 * Get a variable's value
820 * \param p_this The object that holds the variable
821 * \param psz_name The name of the variable
822 * \param p_val Pointer to a vlc_value_t that will hold the variable's value
823 * after the function is finished
825 int __var_Get( vlc_object_t *p_this, const char *psz_name, vlc_value_t *p_val )
827 return var_GetChecked( p_this, psz_name, 0, p_val );
831 * Register a callback in a variable
833 * We store a function pointer that will be called upon variable
836 * \param p_this The object that holds the variable
837 * \param psz_name The name of the variable
838 * \param pf_callback The function pointer
839 * \param p_data A generic pointer that will be passed as the last
840 * argument to the callback function.
842 * \warning The callback function is run in the thread that calls var_Set on
843 * the variable. Use proper locking. This thread may not have much
844 * time to spare, so keep callback functions short.
846 int __var_AddCallback( vlc_object_t *p_this, const char *psz_name,
847 vlc_callback_t pf_callback, void *p_data )
851 callback_entry_t entry;
852 vlc_object_internals_t *p_priv = vlc_internals( p_this );
854 entry.pf_callback = pf_callback;
855 entry.p_data = p_data;
857 vlc_mutex_lock( &p_priv->var_lock );
859 i_var = GetUnused( p_this, psz_name );
862 vlc_mutex_unlock( &p_priv->var_lock );
866 p_var = &p_priv->p_vars[i_var];
868 INSERT_ELEM( p_var->p_entries,
873 vlc_mutex_unlock( &p_priv->var_lock );
879 * Remove a callback from a variable
881 * pf_callback and p_data have to be given again, because different objects
882 * might have registered the same callback function.
884 int __var_DelCallback( vlc_object_t *p_this, const char *psz_name,
885 vlc_callback_t pf_callback, void *p_data )
889 vlc_object_internals_t *p_priv = vlc_internals( p_this );
891 vlc_mutex_lock( &p_priv->var_lock );
893 i_var = GetUnused( p_this, psz_name );
896 vlc_mutex_unlock( &p_priv->var_lock );
900 p_var = &p_priv->p_vars[i_var];
902 for( i_entry = p_var->i_entries ; i_entry-- ; )
904 if( p_var->p_entries[i_entry].pf_callback == pf_callback
905 && p_var->p_entries[i_entry].p_data == p_data )
913 vlc_mutex_unlock( &p_priv->var_lock );
917 REMOVE_ELEM( p_var->p_entries, p_var->i_entries, i_entry );
919 vlc_mutex_unlock( &p_priv->var_lock );
925 * Trigger callback on a variable
927 * \param p_this The object that hold the variable
928 * \param psz_name The name of the variable
930 int __var_TriggerCallback( vlc_object_t *p_this, const char *psz_name )
935 vlc_object_internals_t *p_priv = vlc_internals( p_this );
937 vlc_mutex_lock( &p_priv->var_lock );
939 i_var = GetUnused( p_this, psz_name );
942 vlc_mutex_unlock( &p_priv->var_lock );
946 p_var = &p_priv->p_vars[i_var];
948 /* Backup needed stuff */
951 /* Deal with callbacks. Tell we're in a callback, release the lock,
952 * call stored functions, retake the lock. */
953 if( p_var->i_entries )
956 int i_entries = p_var->i_entries;
957 callback_entry_t *p_entries = p_var->p_entries;
959 p_var->b_incallback = true;
960 vlc_mutex_unlock( &p_priv->var_lock );
963 for( ; i_entries-- ; )
965 p_entries[i_entries].pf_callback( p_this, psz_name, oldval, oldval,
966 p_entries[i_entries].p_data );
969 vlc_mutex_lock( &p_priv->var_lock );
971 i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
974 msg_Err( p_this, "variable %s has disappeared", psz_name );
975 vlc_mutex_unlock( &p_priv->var_lock );
979 p_var = &p_priv->p_vars[i_var];
980 p_var->b_incallback = false;
983 vlc_mutex_unlock( &p_priv->var_lock );
987 /** Parse a stringified option
988 * This function parse a string option and create the associated object
990 * The option must be of the form "[no[-]]foo[=bar]" where foo is the
991 * option name and bar is the value of the option.
992 * \param p_obj the object in which the variable must be created
993 * \param psz_option the option to parse
994 * \param trusted whether the option is set by a trusted input or not
997 void var_OptionParse( vlc_object_t *p_obj, const char *psz_option,
1000 char *psz_name, *psz_value;
1002 bool b_isno = false;
1005 val.psz_string = NULL;
1007 /* It's too much of a hassle to remove the ':' when we parse
1008 * the cmd line :) */
1009 if( psz_option[0] == ':' )
1012 if( !psz_option[0] )
1015 psz_name = strdup( psz_option );
1016 if( psz_name == NULL )
1019 psz_value = strchr( psz_name, '=' );
1020 if( psz_value != NULL )
1021 *psz_value++ = '\0';
1023 /* FIXME: :programs should be handled generically */
1024 if( !strcmp( psz_name, "programs" ) )
1025 i_type = VLC_VAR_LIST;
1027 i_type = config_GetType( p_obj, psz_name );
1029 if( !i_type && !psz_value )
1031 /* check for "no-foo" or "nofoo" */
1032 if( !strncmp( psz_name, "no-", 3 ) )
1034 memmove( psz_name, psz_name + 3, strlen(psz_name) + 1 - 3 );
1036 else if( !strncmp( psz_name, "no", 2 ) )
1038 memmove( psz_name, psz_name + 2, strlen(psz_name) + 1 - 2 );
1040 else goto cleanup; /* Option doesn't exist */
1043 i_type = config_GetType( p_obj, psz_name );
1045 if( !i_type ) goto cleanup; /* Option doesn't exist */
1047 if( ( i_type != VLC_VAR_BOOL ) &&
1048 ( !psz_value || !*psz_value ) ) goto cleanup; /* Invalid value */
1050 /* check if option is unsafe */
1053 module_config_t *p_config = config_FindConfig( p_obj, psz_name );
1054 if( !p_config || !p_config->b_safe )
1056 msg_Err( p_obj, "unsafe option \"%s\" has been ignored for "
1057 "security reasons", psz_name );
1063 /* Create the variable in the input object.
1064 * Children of the input object will be able to retreive this value
1065 * thanks to the inheritance property of the object variables. */
1066 var_Create( p_obj, psz_name, i_type );
1071 val.b_bool = !b_isno;
1074 case VLC_VAR_INTEGER:
1075 val.i_int = strtol( psz_value, NULL, 0 );
1079 val.f_float = atof( psz_value );
1082 case VLC_VAR_STRING:
1083 case VLC_VAR_MODULE:
1085 case VLC_VAR_DIRECTORY:
1086 val.psz_string = psz_value;
1091 char *psz_orig, *psz_var;
1092 vlc_list_t *p_list = malloc(sizeof(vlc_list_t));
1093 val.p_list = p_list;
1094 p_list->i_count = 0;
1096 psz_var = psz_orig = strdup(psz_value);
1097 while( psz_var && *psz_var )
1099 char *psz_item = psz_var;
1101 while( *psz_var && *psz_var != ',' ) psz_var++;
1102 if( *psz_var == ',' )
1107 val2.i_int = strtol( psz_item, NULL, 0 );
1108 INSERT_ELEM( p_list->p_values, p_list->i_count,
1109 p_list->i_count, val2 );
1110 /* p_list->i_count is incremented twice by INSERT_ELEM */
1112 INSERT_ELEM( p_list->pi_types, p_list->i_count,
1113 p_list->i_count, VLC_VAR_INTEGER );
1123 var_Set( p_obj, psz_name, val );
1125 // If that's a list, remove all elements allocated
1126 if( i_type == VLC_VAR_LIST )
1134 /* Following functions are local */
1136 /*****************************************************************************
1137 * GetUnused: find an unused variable from its name
1138 *****************************************************************************
1139 * We do i_tries tries before giving up, just in case the variable is being
1140 * modified and called from a callback.
1141 *****************************************************************************/
1142 static int GetUnused( vlc_object_t *p_this, const char *psz_name )
1144 int i_var, i_tries = 0;
1145 vlc_object_internals_t *p_priv = vlc_internals( p_this );
1149 i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
1155 if( ! p_priv->p_vars[i_var].b_incallback )
1160 if( i_tries++ > 100 )
1162 msg_Err( p_this, "caught in a callback deadlock? ('%s')", psz_name );
1163 return VLC_ETIMEOUT;
1166 vlc_mutex_unlock( &p_priv->var_lock );
1167 msleep( THREAD_SLEEP );
1168 vlc_mutex_lock( &p_priv->var_lock );
1172 /*****************************************************************************
1173 * HashString: our cool hash function
1174 *****************************************************************************
1175 * This function is not intended to be crypto-secure, we only want it to be
1176 * fast and not suck too much. This one is pretty fast and did 0 collisions
1177 * in wenglish's dictionary.
1178 *****************************************************************************/
1179 static uint32_t HashString( const char *psz_string )
1181 uint32_t i_hash = 0;
1183 while( *psz_string )
1185 i_hash += *psz_string++;
1186 i_hash += i_hash << 10;
1187 i_hash ^= i_hash >> 8;
1193 /*****************************************************************************
1194 * Insert: find an empty slot to insert a new variable
1195 *****************************************************************************
1196 * We use a recursive inner function indexed on the hash. This function does
1197 * nothing in the rare cases where a collision may occur, see Lookup()
1198 * to see how we handle them.
1199 * XXX: does this really need to be written recursively?
1200 *****************************************************************************/
1201 static int Insert( variable_t *p_vars, int i_count, const char *psz_name )
1208 return InsertInner( p_vars, i_count, HashString( psz_name ) );
1211 static int InsertInner( variable_t *p_vars, int i_count, uint32_t i_hash )
1215 if( i_hash <= p_vars[0].i_hash )
1220 if( i_hash >= p_vars[i_count - 1].i_hash )
1225 i_middle = i_count / 2;
1227 /* We know that 0 < i_middle */
1228 if( i_hash < p_vars[i_middle].i_hash )
1230 return InsertInner( p_vars, i_middle, i_hash );
1233 /* We know that i_middle + 1 < i_count */
1234 if( i_hash > p_vars[i_middle + 1].i_hash )
1236 return i_middle + 1 + InsertInner( p_vars + i_middle + 1,
1237 i_count - i_middle - 1,
1241 return i_middle + 1;
1244 static int u32cmp( const void *key, const void *data )
1246 const variable_t *p_var = data;
1247 uint32_t hash = *(const uint32_t *)key ;
1249 if( hash > p_var->i_hash )
1251 if( hash < p_var->i_hash )
1256 /*****************************************************************************
1257 * Lookup: find an existing variable given its name
1258 *****************************************************************************
1259 * We use a recursive inner function indexed on the hash. Care is taken of
1260 * possible hash collisions.
1261 *****************************************************************************/
1262 static int Lookup( variable_t *p_vars, size_t i_count, const char *psz_name )
1267 i_hash = HashString( psz_name );
1268 p_var = bsearch( &i_hash, p_vars, i_count, sizeof( *p_var ), u32cmp );
1270 /* Hash not found */
1274 assert( i_count > 0 );
1276 /* Find the first entry with the right hash */
1277 while( (p_var > p_vars) && (i_hash == p_var[-1].i_hash) )
1280 assert( p_var->i_hash == i_hash );
1282 /* Hash collision should be very unlikely, but we cannot guarantee
1283 * it will never happen. So we do an exhaustive search amongst all
1284 * entries with the same hash. Typically, there is only one anyway. */
1285 for( variable_t *p_end = p_vars + i_count;
1286 (p_var < p_end) && (i_hash == p_var->i_hash);
1289 if( !strcmp( psz_name, p_var->psz_name ) )
1290 return p_var - p_vars;
1293 /* Hash found, but entry not found */
1297 /*****************************************************************************
1298 * CheckValue: check that a value is valid wrt. a variable
1299 *****************************************************************************
1300 * This function checks p_val's value against p_var's limitations such as
1301 * minimal and maximal value, step, in-list position, and modifies p_val if
1303 ****************************************************************************/
1304 static void CheckValue ( variable_t *p_var, vlc_value_t *p_val )
1306 /* Check that our variable is in the list */
1307 if( p_var->i_type & VLC_VAR_HASCHOICE && p_var->choices.i_count )
1311 /* FIXME: the list is sorted, dude. Use something cleverer. */
1312 for( i = p_var->choices.i_count ; i-- ; )
1314 if( p_var->ops->pf_cmp( *p_val, p_var->choices.p_values[i] ) == 0 )
1320 /* If not found, change it to anything vaguely valid */
1323 /* Free the old variable, get the new one, dup it */
1324 p_var->ops->pf_free( p_val );
1325 *p_val = p_var->choices.p_values[p_var->i_default >= 0
1326 ? p_var->i_default : 0 ];
1327 p_var->ops->pf_dup( p_val );
1331 /* Check that our variable is within the bounds */
1332 switch( p_var->i_type & VLC_VAR_TYPE )
1334 case VLC_VAR_INTEGER:
1335 if( p_var->i_type & VLC_VAR_HASSTEP && p_var->step.i_int
1336 && (p_val->i_int % p_var->step.i_int) )
1338 p_val->i_int = (p_val->i_int + (p_var->step.i_int / 2))
1339 / p_var->step.i_int * p_var->step.i_int;
1341 if( p_var->i_type & VLC_VAR_HASMIN
1342 && p_val->i_int < p_var->min.i_int )
1344 p_val->i_int = p_var->min.i_int;
1346 if( p_var->i_type & VLC_VAR_HASMAX
1347 && p_val->i_int > p_var->max.i_int )
1349 p_val->i_int = p_var->max.i_int;
1353 if( p_var->i_type & VLC_VAR_HASSTEP && p_var->step.f_float )
1355 float f_round = p_var->step.f_float * (float)(int)( 0.5 +
1356 p_val->f_float / p_var->step.f_float );
1357 if( p_val->f_float != f_round )
1359 p_val->f_float = f_round;
1362 if( p_var->i_type & VLC_VAR_HASMIN
1363 && p_val->f_float < p_var->min.f_float )
1365 p_val->f_float = p_var->min.f_float;
1367 if( p_var->i_type & VLC_VAR_HASMAX
1368 && p_val->f_float > p_var->max.f_float )
1370 p_val->f_float = p_var->max.f_float;
1379 /*****************************************************************************
1380 * InheritValue: try to inherit the value of this variable from the same one
1381 * in our closest parent, libvlc or ultimately from the configuration.
1382 * The function should always be entered with the object var_lock locked.
1383 *****************************************************************************/
1384 static int InheritValue( vlc_object_t *p_this, const char *psz_name,
1385 vlc_value_t *p_val, int i_type )
1390 if( p_this->p_parent || ( p_this->p_libvlc && p_this != p_this->p_libvlc ) )
1392 vlc_object_internals_t *p_priv;
1394 if( p_this->p_parent )
1395 p_priv = vlc_internals( p_this->p_parent );
1397 p_priv = vlc_internals( p_this->p_libvlc );
1399 i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
1404 p_var = &p_priv->p_vars[i_var];
1406 /* Really get the variable */
1407 *p_val = p_var->val;
1409 /* Duplicate value if needed */
1410 p_var->ops->pf_dup( p_val );
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 );
1472 /**********************************************************************
1473 * Execute a var command on an object identified by its name
1474 **********************************************************************/
1475 int __var_Command( vlc_object_t *p_this, const char *psz_name,
1476 const char *psz_cmd, const char *psz_arg, char **psz_msg )
1478 vlc_object_t *p_obj = vlc_object_find_name( p_this->p_libvlc,
1479 psz_name, FIND_CHILD );
1485 *psz_msg = strdup( "Unknown destination object." );
1489 i_type = var_Type( p_obj, psz_cmd );
1490 if( !( i_type&VLC_VAR_ISCOMMAND ) )
1492 vlc_object_release( p_obj );
1494 *psz_msg = strdup( "Variable doesn't exist or isn't a command." );
1495 return VLC_EGENERIC;
1498 i_type &= VLC_VAR_CLASS;
1501 case VLC_VAR_INTEGER:
1502 i_ret = var_SetInteger( p_obj, psz_cmd, atoi( psz_arg ) );
1505 i_ret = var_SetFloat( p_obj, psz_cmd, atof( psz_arg ) );
1507 case VLC_VAR_STRING:
1508 i_ret = var_SetString( p_obj, psz_cmd, psz_arg );
1511 i_ret = var_SetBool( p_obj, psz_cmd, atoi( psz_arg ) );
1514 i_ret = VLC_EGENERIC;
1518 vlc_object_release( p_obj );
1522 if( asprintf( psz_msg, "%s on object %s returned %i (%s)",
1523 psz_cmd, psz_name, i_ret, vlc_error( i_ret ) ) == -1)