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"
38 /*****************************************************************************
40 *****************************************************************************/
41 struct callback_entry_t
43 vlc_callback_t pf_callback;
47 /*****************************************************************************
48 * Local comparison functions, returns 0 if v == w, < 0 if v < w, > 0 if v > w
49 *****************************************************************************/
50 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; }
51 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; }
52 static int CmpTime( vlc_value_t v, vlc_value_t w )
54 return v.i_time == w.i_time ? 0 : v.i_time > w.i_time ? 1 : -1;
56 static int CmpString( vlc_value_t v, vlc_value_t w )
59 return !w.psz_string ? 0 : -1;
61 return !w.psz_string ? 1 : strcmp( v.psz_string, w.psz_string );
63 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; }
64 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; }
66 /*****************************************************************************
67 * Local duplication functions, and local deallocation functions
68 *****************************************************************************/
69 static void DupDummy( vlc_value_t *p_val ) { (void)p_val; /* unused */ }
70 static void DupString( vlc_value_t *p_val ) { if( p_val->psz_string ) p_val->psz_string = strdup( p_val->psz_string ); }
72 static void DupList( vlc_value_t *p_val )
75 vlc_list_t *p_list = malloc( sizeof(vlc_list_t) );
77 p_list->i_count = p_val->p_list->i_count;
78 if( p_val->p_list->i_count )
80 p_list->p_values = malloc( p_list->i_count * sizeof(vlc_value_t) );
81 p_list->pi_types = malloc( p_list->i_count * sizeof(int) );
85 p_list->p_values = NULL;
86 p_list->pi_types = NULL;
89 for( i = 0; i < p_list->i_count; i++ )
91 p_list->p_values[i] = p_val->p_list->p_values[i];
92 p_list->pi_types[i] = p_val->p_list->pi_types[i];
93 switch( p_val->p_list->pi_types[i] & VLC_VAR_CLASS )
97 DupString( &p_list->p_values[i] );
104 p_val->p_list = p_list;
107 static void FreeDummy( vlc_value_t *p_val ) { (void)p_val; /* unused */ }
108 static void FreeString( vlc_value_t *p_val ) { free( p_val->psz_string ); }
109 static void FreeMutex( vlc_value_t *p_val ) { vlc_mutex_destroy( (vlc_mutex_t*)p_val->p_address ); free( p_val->p_address ); }
111 static void FreeList( vlc_value_t *p_val )
114 for( i = 0; i < p_val->p_list->i_count; i++ )
116 switch( p_val->p_list->pi_types[i] & VLC_VAR_CLASS )
119 FreeString( &p_val->p_list->p_values[i] );
122 FreeMutex( &p_val->p_list->p_values[i] );
129 if( p_val->p_list->i_count )
131 free( p_val->p_list->p_values );
132 free( p_val->p_list->pi_types );
134 free( p_val->p_list );
137 static const struct variable_ops_t
138 void_ops = { NULL, DupDummy, FreeDummy, },
139 addr_ops = { CmpAddress, DupDummy, FreeDummy, },
140 bool_ops = { CmpBool, DupDummy, FreeDummy, },
141 float_ops = { CmpFloat, DupDummy, FreeDummy, },
142 int_ops = { CmpInt, DupDummy, FreeDummy, },
143 list_ops = { CmpAddress, DupList, FreeList, },
144 mutex_ops = { CmpAddress, DupDummy, FreeMutex, },
145 string_ops = { CmpString, DupString, FreeString, },
146 time_ops = { CmpTime, DupDummy, FreeDummy, };
148 /*****************************************************************************
150 *****************************************************************************/
151 static int GetUnused ( vlc_object_t *, const char * );
152 static uint32_t HashString ( const char * );
153 static int Insert ( variable_t *, int, const char * );
154 static int InsertInner ( variable_t *, int, uint32_t );
155 static int Lookup ( variable_t *, int, const char * );
156 static int LookupInner ( variable_t *, int, uint32_t );
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 );
659 case VLC_VAR_TRIGGER_CALLBACKS:
661 /* Deal with callbacks. Tell we're in a callback, release the lock,
662 * call stored functions, retake the lock. */
663 if( p_var->i_entries )
666 int i_entries = p_var->i_entries;
667 callback_entry_t *p_entries = p_var->p_entries;
669 p_var->b_incallback = true;
670 vlc_mutex_unlock( &p_priv->var_lock );
673 for( ; i_entries-- ; )
675 p_entries[i_entries].pf_callback( p_this, psz_name, p_var->val, p_var->val,
676 p_entries[i_entries].p_data );
679 vlc_mutex_lock( &p_priv->var_lock );
681 i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
684 msg_Err( p_this, "variable %s has disappeared", psz_name );
685 vlc_mutex_unlock( &p_priv->var_lock );
689 p_var = &p_priv->p_vars[i_var];
690 p_var->b_incallback = false;
695 case VLC_VAR_SETISCOMMAND:
696 p_var->i_type |= VLC_VAR_ISCOMMAND;
703 vlc_mutex_unlock( &p_priv->var_lock );
709 * Request a variable's type
711 * \return The variable type if it exists, or 0 if the
712 * variable could not be found.
715 int __var_Type( vlc_object_t *p_this, const char *psz_name )
718 vlc_object_internals_t *p_priv = vlc_internals( p_this );
720 vlc_mutex_lock( &p_priv->var_lock );
722 i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
726 vlc_mutex_unlock( &p_priv->var_lock );
730 i_type = p_priv->p_vars[i_var].i_type;
732 vlc_mutex_unlock( &p_priv->var_lock );
738 * Set a variable's value
740 * \param p_this The object that hold the variable
741 * \param psz_name The name of the variable
742 * \param val the value to set
744 int __var_Set( vlc_object_t *p_this, const char *psz_name, vlc_value_t val )
749 vlc_object_internals_t *p_priv = vlc_internals( p_this );
751 vlc_mutex_lock( &p_priv->var_lock );
753 i_var = GetUnused( p_this, psz_name );
756 vlc_mutex_unlock( &p_priv->var_lock );
760 p_var = &p_priv->p_vars[i_var];
762 /* Duplicate data if needed */
763 p_var->ops->pf_dup( &val );
765 /* Backup needed stuff */
768 /* Check boundaries and list */
769 CheckValue( p_var, &val );
771 /* Set the variable */
774 /* Deal with callbacks. Tell we're in a callback, release the lock,
775 * call stored functions, retake the lock. */
776 if( p_var->i_entries )
779 int i_entries = p_var->i_entries;
780 callback_entry_t *p_entries = p_var->p_entries;
782 p_var->b_incallback = true;
783 vlc_mutex_unlock( &p_priv->var_lock );
786 for( ; i_entries-- ; )
788 p_entries[i_entries].pf_callback( p_this, psz_name, oldval, val,
789 p_entries[i_entries].p_data );
792 vlc_mutex_lock( &p_priv->var_lock );
794 i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
797 msg_Err( p_this, "variable %s has disappeared", psz_name );
798 vlc_mutex_unlock( &p_priv->var_lock );
802 p_var = &p_priv->p_vars[i_var];
803 p_var->b_incallback = false;
806 /* Free data if needed */
807 p_var->ops->pf_free( &oldval );
809 vlc_mutex_unlock( &p_priv->var_lock );
815 * Get a variable's value
817 * \param p_this The object that holds the variable
818 * \param psz_name The name of the variable
819 * \param p_val Pointer to a vlc_value_t that will hold the variable's value
820 * after the function is finished
822 int __var_Get( vlc_object_t *p_this, const char *psz_name, vlc_value_t *p_val )
826 vlc_object_internals_t *p_priv = vlc_internals( p_this );
828 vlc_mutex_lock( &p_priv->var_lock );
830 i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
834 vlc_mutex_unlock( &p_priv->var_lock );
838 p_var = &p_priv->p_vars[i_var];
840 /* Really get the variable */
843 /* Duplicate value if needed */
844 p_var->ops->pf_dup( p_val );
846 vlc_mutex_unlock( &p_priv->var_lock );
852 #undef var_AcquireMutex
854 * Finds a process-wide mutex, creates it if needed, and locks it.
855 * Unlock with vlc_mutex_unlock().
857 vlc_mutex_t *var_AcquireMutex( const char *name )
859 libvlc_global_data_t *p_global = vlc_global();
862 if( var_Create( p_global, name, VLC_VAR_MUTEX ) )
865 var_Get( p_global, name, &val );
866 vlc_mutex_lock( val.p_address );
867 return val.p_address;
872 * Register a callback in a variable
874 * We store a function pointer that will be called upon variable
877 * \param p_this The object that holds the variable
878 * \param psz_name The name of the variable
879 * \param pf_callback The function pointer
880 * \param p_data A generic pointer that will be passed as the last
881 * argument to the callback function.
883 * \warning The callback function is run in the thread that calls var_Set on
884 * the variable. Use proper locking. This thread may not have much
885 * time to spare, so keep callback functions short.
887 int __var_AddCallback( vlc_object_t *p_this, const char *psz_name,
888 vlc_callback_t pf_callback, void *p_data )
892 callback_entry_t entry;
893 vlc_object_internals_t *p_priv = vlc_internals( p_this );
895 entry.pf_callback = pf_callback;
896 entry.p_data = p_data;
898 vlc_mutex_lock( &p_priv->var_lock );
900 i_var = GetUnused( p_this, psz_name );
903 vlc_mutex_unlock( &p_priv->var_lock );
907 p_var = &p_priv->p_vars[i_var];
909 INSERT_ELEM( p_var->p_entries,
914 vlc_mutex_unlock( &p_priv->var_lock );
920 * Remove a callback from a variable
922 * pf_callback and p_data have to be given again, because different objects
923 * might have registered the same callback function.
925 int __var_DelCallback( vlc_object_t *p_this, const char *psz_name,
926 vlc_callback_t pf_callback, void *p_data )
930 vlc_object_internals_t *p_priv = vlc_internals( p_this );
932 vlc_mutex_lock( &p_priv->var_lock );
934 i_var = GetUnused( p_this, psz_name );
937 vlc_mutex_unlock( &p_priv->var_lock );
941 p_var = &p_priv->p_vars[i_var];
943 for( i_entry = p_var->i_entries ; i_entry-- ; )
945 if( p_var->p_entries[i_entry].pf_callback == pf_callback
946 && p_var->p_entries[i_entry].p_data == p_data )
954 vlc_mutex_unlock( &p_priv->var_lock );
958 REMOVE_ELEM( p_var->p_entries, p_var->i_entries, i_entry );
960 vlc_mutex_unlock( &p_priv->var_lock );
966 * Trigger callback on a variable
968 * \param p_this The object that hold the variable
969 * \param psz_name The name of the variable
971 int __var_TriggerCallback( vlc_object_t *p_this, const char *psz_name )
976 vlc_object_internals_t *p_priv = vlc_internals( p_this );
978 vlc_mutex_lock( &p_priv->var_lock );
980 i_var = GetUnused( p_this, psz_name );
983 vlc_mutex_unlock( &p_priv->var_lock );
987 p_var = &p_priv->p_vars[i_var];
989 /* Backup needed stuff */
992 /* Deal with callbacks. Tell we're in a callback, release the lock,
993 * call stored functions, retake the lock. */
994 if( p_var->i_entries )
997 int i_entries = p_var->i_entries;
998 callback_entry_t *p_entries = p_var->p_entries;
1000 p_var->b_incallback = true;
1001 vlc_mutex_unlock( &p_priv->var_lock );
1003 /* The real calls */
1004 for( ; i_entries-- ; )
1006 p_entries[i_entries].pf_callback( p_this, psz_name, oldval, oldval,
1007 p_entries[i_entries].p_data );
1010 vlc_mutex_lock( &p_priv->var_lock );
1012 i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
1015 msg_Err( p_this, "variable %s has disappeared", psz_name );
1016 vlc_mutex_unlock( &p_priv->var_lock );
1020 p_var = &p_priv->p_vars[i_var];
1021 p_var->b_incallback = false;
1024 vlc_mutex_unlock( &p_priv->var_lock );
1028 /** Parse a stringified option
1029 * This function parse a string option and create the associated object
1031 * The option must be of the form "[no[-]]foo[=bar]" where foo is the
1032 * option name and bar is the value of the option.
1033 * \param p_obj the object in which the variable must be created
1034 * \param psz_option the option to parse
1035 * \param trusted whether the option is set by a trusted input or not
1038 void var_OptionParse( vlc_object_t *p_obj, const char *psz_option,
1041 char *psz_name, *psz_value;
1043 bool b_isno = false;
1046 val.psz_string = NULL;
1048 /* It's too much of a hassle to remove the ':' when we parse
1049 * the cmd line :) */
1050 if( psz_option[0] == ':' )
1053 if( !psz_option[0] )
1056 psz_name = strdup( psz_option );
1057 if( psz_name == NULL )
1060 psz_value = strchr( psz_name, '=' );
1061 if( psz_value != NULL )
1062 *psz_value++ = '\0';
1064 /* FIXME: :programs should be handled generically */
1065 if( !strcmp( psz_name, "programs" ) )
1066 i_type = VLC_VAR_LIST;
1068 i_type = config_GetType( p_obj, psz_name );
1070 if( !i_type && !psz_value )
1072 /* check for "no-foo" or "nofoo" */
1073 if( !strncmp( psz_name, "no-", 3 ) )
1075 memmove( psz_name, psz_name + 3, strlen(psz_name) + 1 - 3 );
1077 else if( !strncmp( psz_name, "no", 2 ) )
1079 memmove( psz_name, psz_name + 2, strlen(psz_name) + 1 - 2 );
1081 else goto cleanup; /* Option doesn't exist */
1084 i_type = config_GetType( p_obj, psz_name );
1086 if( !i_type ) goto cleanup; /* Option doesn't exist */
1088 if( ( i_type != VLC_VAR_BOOL ) &&
1089 ( !psz_value || !*psz_value ) ) goto cleanup; /* Invalid value */
1091 /* check if option is unsafe */
1094 module_config_t *p_config = config_FindConfig( p_obj, psz_name );
1095 if( !p_config->b_safe )
1097 msg_Err( p_obj, "unsafe option \"%s\" has been ignored for "
1098 "security reasons", psz_name );
1103 /* Create the variable in the input object.
1104 * Children of the input object will be able to retreive this value
1105 * thanks to the inheritance property of the object variables. */
1106 var_Create( p_obj, psz_name, i_type );
1111 val.b_bool = !b_isno;
1114 case VLC_VAR_INTEGER:
1115 val.i_int = strtol( psz_value, NULL, 0 );
1119 val.f_float = atof( psz_value );
1122 case VLC_VAR_STRING:
1123 case VLC_VAR_MODULE:
1125 case VLC_VAR_DIRECTORY:
1126 val.psz_string = psz_value;
1131 char *psz_orig, *psz_var;
1132 vlc_list_t *p_list = malloc(sizeof(vlc_list_t));
1133 val.p_list = p_list;
1134 p_list->i_count = 0;
1136 psz_var = psz_orig = strdup(psz_value);
1137 while( psz_var && *psz_var )
1139 char *psz_item = psz_var;
1141 while( *psz_var && *psz_var != ',' ) psz_var++;
1142 if( *psz_var == ',' )
1147 val2.i_int = strtol( psz_item, NULL, 0 );
1148 INSERT_ELEM( p_list->p_values, p_list->i_count,
1149 p_list->i_count, val2 );
1150 /* p_list->i_count is incremented twice by INSERT_ELEM */
1152 INSERT_ELEM( p_list->pi_types, p_list->i_count,
1153 p_list->i_count, VLC_VAR_INTEGER );
1163 var_Set( p_obj, psz_name, val );
1170 /* Following functions are local */
1172 /*****************************************************************************
1173 * GetUnused: find an unused variable from its name
1174 *****************************************************************************
1175 * We do i_tries tries before giving up, just in case the variable is being
1176 * modified and called from a callback.
1177 *****************************************************************************/
1178 static int GetUnused( vlc_object_t *p_this, const char *psz_name )
1180 int i_var, i_tries = 0;
1181 vlc_object_internals_t *p_priv = vlc_internals( p_this );
1185 i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
1191 if( ! p_priv->p_vars[i_var].b_incallback )
1196 if( i_tries++ > 100 )
1198 msg_Err( p_this, "caught in a callback deadlock? ('%s')", psz_name );
1199 return VLC_ETIMEOUT;
1202 vlc_mutex_unlock( &p_priv->var_lock );
1203 msleep( THREAD_SLEEP );
1204 vlc_mutex_lock( &p_priv->var_lock );
1208 /*****************************************************************************
1209 * HashString: our cool hash function
1210 *****************************************************************************
1211 * This function is not intended to be crypto-secure, we only want it to be
1212 * fast and not suck too much. This one is pretty fast and did 0 collisions
1213 * in wenglish's dictionary.
1214 *****************************************************************************/
1215 static uint32_t HashString( const char *psz_string )
1217 uint32_t i_hash = 0;
1219 while( *psz_string )
1221 i_hash += *psz_string++;
1222 i_hash += i_hash << 10;
1223 i_hash ^= i_hash >> 8;
1229 /*****************************************************************************
1230 * Insert: find an empty slot to insert a new variable
1231 *****************************************************************************
1232 * We use a recursive inner function indexed on the hash. This function does
1233 * nothing in the rare cases where a collision may occur, see Lookup()
1234 * to see how we handle them.
1235 * XXX: does this really need to be written recursively?
1236 *****************************************************************************/
1237 static int Insert( variable_t *p_vars, int i_count, const char *psz_name )
1244 return InsertInner( p_vars, i_count, HashString( psz_name ) );
1247 static int InsertInner( variable_t *p_vars, int i_count, uint32_t i_hash )
1251 if( i_hash <= p_vars[0].i_hash )
1256 if( i_hash >= p_vars[i_count - 1].i_hash )
1261 i_middle = i_count / 2;
1263 /* We know that 0 < i_middle */
1264 if( i_hash < p_vars[i_middle].i_hash )
1266 return InsertInner( p_vars, i_middle, i_hash );
1269 /* We know that i_middle + 1 < i_count */
1270 if( i_hash > p_vars[i_middle + 1].i_hash )
1272 return i_middle + 1 + InsertInner( p_vars + i_middle + 1,
1273 i_count - i_middle - 1,
1277 return i_middle + 1;
1280 /*****************************************************************************
1281 * Lookup: find an existing variable given its name
1282 *****************************************************************************
1283 * We use a recursive inner function indexed on the hash. Care is taken of
1284 * possible hash collisions.
1285 * XXX: does this really need to be written recursively?
1286 *****************************************************************************/
1287 static int Lookup( variable_t *p_vars, int i_count, const char *psz_name )
1297 i_hash = HashString( psz_name );
1299 i_pos = LookupInner( p_vars, i_count, i_hash );
1301 /* Hash not found */
1302 if( i_hash != p_vars[i_pos].i_hash )
1307 /* Hash found, entry found */
1308 if( !strcmp( psz_name, p_vars[i_pos].psz_name ) )
1313 /* Hash collision! This should be very rare, but we cannot guarantee
1314 * it will never happen. Just do an exhaustive search amongst all
1315 * entries with the same hash. */
1316 for( i = i_pos - 1 ; i > 0 && i_hash == p_vars[i].i_hash ; i-- )
1318 if( !strcmp( psz_name, p_vars[i].psz_name ) )
1324 for( i = i_pos + 1 ; i < i_count && i_hash == p_vars[i].i_hash ; i++ )
1326 if( !strcmp( psz_name, p_vars[i].psz_name ) )
1332 /* Hash found, but entry not found */
1336 static int LookupInner( variable_t *p_vars, int i_count, uint32_t i_hash )
1340 if( i_hash <= p_vars[0].i_hash )
1345 if( i_hash >= p_vars[i_count-1].i_hash )
1350 i_middle = i_count / 2;
1352 /* We know that 0 < i_middle */
1353 if( i_hash < p_vars[i_middle].i_hash )
1355 return LookupInner( p_vars, i_middle, i_hash );
1358 /* We know that i_middle + 1 < i_count */
1359 if( i_hash > p_vars[i_middle].i_hash )
1361 return i_middle + LookupInner( p_vars + i_middle,
1369 /*****************************************************************************
1370 * CheckValue: check that a value is valid wrt. a variable
1371 *****************************************************************************
1372 * This function checks p_val's value against p_var's limitations such as
1373 * minimal and maximal value, step, in-list position, and modifies p_val if
1375 ****************************************************************************/
1376 static void CheckValue ( variable_t *p_var, vlc_value_t *p_val )
1378 /* Check that our variable is in the list */
1379 if( p_var->i_type & VLC_VAR_HASCHOICE && p_var->choices.i_count )
1383 /* FIXME: the list is sorted, dude. Use something cleverer. */
1384 for( i = p_var->choices.i_count ; i-- ; )
1386 if( p_var->ops->pf_cmp( *p_val, p_var->choices.p_values[i] ) == 0 )
1392 /* If not found, change it to anything vaguely valid */
1395 /* Free the old variable, get the new one, dup it */
1396 p_var->ops->pf_free( p_val );
1397 *p_val = p_var->choices.p_values[p_var->i_default >= 0
1398 ? p_var->i_default : 0 ];
1399 p_var->ops->pf_dup( p_val );
1403 /* Check that our variable is within the bounds */
1404 switch( p_var->i_type & VLC_VAR_TYPE )
1406 case VLC_VAR_INTEGER:
1407 if( p_var->i_type & VLC_VAR_HASSTEP && p_var->step.i_int
1408 && (p_val->i_int % p_var->step.i_int) )
1410 p_val->i_int = (p_val->i_int + (p_var->step.i_int / 2))
1411 / p_var->step.i_int * p_var->step.i_int;
1413 if( p_var->i_type & VLC_VAR_HASMIN
1414 && p_val->i_int < p_var->min.i_int )
1416 p_val->i_int = p_var->min.i_int;
1418 if( p_var->i_type & VLC_VAR_HASMAX
1419 && p_val->i_int > p_var->max.i_int )
1421 p_val->i_int = p_var->max.i_int;
1425 if( p_var->i_type & VLC_VAR_HASSTEP && p_var->step.f_float )
1427 float f_round = p_var->step.f_float * (float)(int)( 0.5 +
1428 p_val->f_float / p_var->step.f_float );
1429 if( p_val->f_float != f_round )
1431 p_val->f_float = f_round;
1434 if( p_var->i_type & VLC_VAR_HASMIN
1435 && p_val->f_float < p_var->min.f_float )
1437 p_val->f_float = p_var->min.f_float;
1439 if( p_var->i_type & VLC_VAR_HASMAX
1440 && p_val->f_float > p_var->max.f_float )
1442 p_val->f_float = p_var->max.f_float;
1451 /*****************************************************************************
1452 * InheritValue: try to inherit the value of this variable from the same one
1453 * in our closest parent, libvlc or ultimately from the configuration.
1454 * The function should always be entered with the object var_lock locked.
1455 *****************************************************************************/
1456 static int InheritValue( vlc_object_t *p_this, const char *psz_name,
1457 vlc_value_t *p_val, int i_type )
1462 if( p_this->p_parent || p_this->p_libvlc )
1464 vlc_object_internals_t *p_priv;
1466 if( p_this->p_parent )
1467 p_priv = vlc_internals( p_this->p_parent );
1469 p_priv = vlc_internals( p_this->p_libvlc );
1471 i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
1476 p_var = &p_priv->p_vars[i_var];
1478 /* Really get the variable */
1479 *p_val = p_var->val;
1481 /* Duplicate value if needed */
1482 p_var->ops->pf_dup( p_val );
1486 else if ( p_this->p_parent ) /* We are still not there */
1487 return InheritValue( p_this->p_parent, psz_name, p_val, i_type );
1489 /* else take value from config */
1493 switch( i_type & VLC_VAR_CLASS )
1495 case VLC_VAR_STRING:
1496 p_val->psz_string = config_GetPsz( p_this, psz_name );
1497 if( !p_val->psz_string ) p_val->psz_string = strdup("");
1500 p_val->f_float = config_GetFloat( p_this, psz_name );
1502 case VLC_VAR_INTEGER:
1503 p_val->i_int = config_GetInt( p_this, psz_name );
1506 p_val->b_bool = config_GetInt( p_this, psz_name );
1510 char *psz_orig, *psz_var;
1511 vlc_list_t *p_list = malloc(sizeof(vlc_list_t));
1512 p_val->p_list = p_list;
1513 p_list->i_count = 0;
1515 psz_var = psz_orig = config_GetPsz( p_this, psz_name );
1516 while( psz_var && *psz_var )
1518 char *psz_item = psz_var;
1520 while( *psz_var && *psz_var != ',' ) psz_var++;
1521 if( *psz_var == ',' )
1526 val.i_int = strtol( psz_item, NULL, 0 );
1527 INSERT_ELEM( p_list->p_values, p_list->i_count,
1528 p_list->i_count, val );
1529 /* p_list->i_count is incremented twice by INSERT_ELEM */
1531 INSERT_ELEM( p_list->pi_types, p_list->i_count,
1532 p_list->i_count, VLC_VAR_INTEGER );
1544 /**********************************************************************
1545 * Execute a var command on an object identified by its name
1546 **********************************************************************/
1547 int __var_Command( vlc_object_t *p_this, const char *psz_name,
1548 const char *psz_cmd, const char *psz_arg, char **psz_msg )
1550 vlc_object_t *p_obj = vlc_object_find_name( p_this->p_libvlc,
1551 psz_name, FIND_CHILD );
1557 *psz_msg = strdup( "Unknown destination object." );
1561 i_type = var_Type( p_obj, psz_cmd );
1562 if( !( i_type&VLC_VAR_ISCOMMAND ) )
1564 vlc_object_release( p_obj );
1566 *psz_msg = strdup( "Variable doesn't exist or isn't a command." );
1567 return VLC_EGENERIC;
1570 i_type &= VLC_VAR_CLASS;
1573 case VLC_VAR_INTEGER:
1574 i_ret = var_SetInteger( p_obj, psz_cmd, atoi( psz_arg ) );
1577 i_ret = var_SetFloat( p_obj, psz_cmd, atof( psz_arg ) );
1579 case VLC_VAR_STRING:
1580 i_ret = var_SetString( p_obj, psz_cmd, psz_arg );
1583 i_ret = var_SetBool( p_obj, psz_cmd, atoi( psz_arg ) );
1586 i_ret = VLC_EGENERIC;
1590 vlc_object_release( p_obj );
1594 *psz_msg = (char*)malloc( 80 );
1595 sprintf( *psz_msg, "%s on object %s returned %i (%s)",
1596 psz_cmd, psz_name, i_ret, vlc_error( i_ret ) );