1 /*****************************************************************************
2 * variables.c: routines for object variables handling
3 *****************************************************************************
4 * Copyright (C) 2002-2009 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 <vlc_charset.h>
33 #include "variables.h"
40 #include "config/configuration.h"
46 /*****************************************************************************
48 *****************************************************************************/
49 struct callback_entry_t
51 vlc_callback_t pf_callback;
55 /*****************************************************************************
56 * Local comparison functions, returns 0 if v == w, < 0 if v < w, > 0 if v > w
57 *****************************************************************************/
58 static int CmpBool( vlc_value_t v, vlc_value_t w )
60 return v.b_bool ? w.b_bool ? 0 : 1 : w.b_bool ? -1 : 0;
63 static int CmpInt( vlc_value_t v, vlc_value_t w )
65 return v.i_int == w.i_int ? 0 : v.i_int > w.i_int ? 1 : -1;
68 static int CmpTime( vlc_value_t v, vlc_value_t w )
70 return v.i_time == w.i_time ? 0 : v.i_time > w.i_time ? 1 : -1;
73 static int CmpString( vlc_value_t v, vlc_value_t w )
76 return !w.psz_string ? 0 : -1;
78 return !w.psz_string ? 1 : strcmp( v.psz_string, w.psz_string );
80 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; }
81 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; }
83 /*****************************************************************************
84 * Local duplication functions, and local deallocation functions
85 *****************************************************************************/
86 static void DupDummy( vlc_value_t *p_val ) { (void)p_val; /* unused */ }
87 static void DupString( vlc_value_t *p_val )
89 p_val->psz_string = strdup( p_val->psz_string ? p_val->psz_string : "" );
92 static void FreeDummy( vlc_value_t *p_val ) { (void)p_val; /* unused */ }
93 static void FreeString( vlc_value_t *p_val ) { free( p_val->psz_string ); }
95 static void FreeList( vlc_value_t *p_val )
98 for( i = 0; i < p_val->p_list->i_count; i++ )
100 switch( p_val->p_list->pi_types[i] & VLC_VAR_CLASS )
103 FreeString( &p_val->p_list->p_values[i] );
110 if( p_val->p_list->i_count )
112 free( p_val->p_list->p_values );
113 free( p_val->p_list->pi_types );
115 free( p_val->p_list );
118 static const struct variable_ops_t
119 void_ops = { NULL, DupDummy, FreeDummy, },
120 addr_ops = { CmpAddress, DupDummy, FreeDummy, },
121 bool_ops = { CmpBool, DupDummy, FreeDummy, },
122 float_ops = { CmpFloat, DupDummy, FreeDummy, },
123 int_ops = { CmpInt, DupDummy, FreeDummy, },
124 string_ops = { CmpString, DupString, FreeString, },
125 time_ops = { CmpTime, DupDummy, FreeDummy, },
126 coords_ops = { NULL, DupDummy, FreeDummy, };
128 /*****************************************************************************
130 *****************************************************************************/
131 static void WaitUnused ( vlc_object_t *, variable_t * );
133 static void CheckValue ( variable_t *, vlc_value_t * );
135 static int TriggerCallback( vlc_object_t *, variable_t *, const char *,
138 static int varcmp( const void *a, const void *b )
140 const variable_t *va = a, *vb = b;
142 /* psz_name must be first */
143 assert( va == (const void *)&va->psz_name );
144 return strcmp( va->psz_name, vb->psz_name );
147 static variable_t *Lookup( vlc_object_t *obj, const char *psz_name )
149 vlc_object_internals_t *priv = vlc_internals( obj );
152 vlc_assert_locked( &priv->var_lock );
153 pp_var = tfind( &psz_name, &priv->var_root, varcmp );
154 return (pp_var != NULL) ? *pp_var : NULL;
157 static void Destroy( variable_t *p_var )
159 p_var->ops->pf_free( &p_var->val );
160 if( p_var->choices.i_count )
162 for( int i = 0 ; i < p_var->choices.i_count ; i++ )
164 p_var->ops->pf_free( &p_var->choices.p_values[i] );
165 free( p_var->choices_text.p_values[i].psz_string );
167 free( p_var->choices.p_values );
168 free( p_var->choices_text.p_values );
170 free( p_var->psz_name );
171 free( p_var->psz_text );
172 free( p_var->p_entries );
178 * Initialize a vlc variable
180 * We hash the given string and insert it into the sorted list. The insertion
181 * may require slow memory copies, but think about what we gain in the log(n)
182 * lookup phase when setting/getting the variable value!
184 * \param p_this The object in which to create the variable
185 * \param psz_name The name of the variable
186 * \param i_type The variables type. Must be one of \ref var_type combined with
187 * zero or more \ref var_flags
189 int var_Create( vlc_object_t *p_this, const char *psz_name, int i_type )
193 variable_t *p_var = calloc( 1, sizeof( *p_var ) );
197 p_var->psz_name = strdup( psz_name );
198 p_var->psz_text = NULL;
200 p_var->i_type = i_type & ~VLC_VAR_DOINHERIT;
204 p_var->i_default = -1;
205 p_var->choices.i_count = 0;
206 p_var->choices.p_values = NULL;
207 p_var->choices_text.i_count = 0;
208 p_var->choices_text.p_values = NULL;
210 p_var->b_incallback = false;
211 p_var->i_entries = 0;
212 p_var->p_entries = NULL;
214 /* Always initialize the variable, even if it is a list variable; this
215 * will lead to errors if the variable is not initialized, but it will
216 * not cause crashes in the variable handling. */
217 switch( i_type & VLC_VAR_CLASS )
220 p_var->ops = &bool_ops;
221 p_var->val.b_bool = false;
223 case VLC_VAR_INTEGER:
224 p_var->ops = &int_ops;
225 p_var->val.i_int = 0;
228 p_var->ops = &string_ops;
229 p_var->val.psz_string = NULL;
232 p_var->ops = &float_ops;
233 p_var->val.f_float = 0.0;
236 p_var->ops = &time_ops;
237 p_var->val.i_time = 0;
240 p_var->ops = &coords_ops;
241 p_var->val.coords.x = p_var->val.coords.y = 0;
243 case VLC_VAR_ADDRESS:
244 p_var->ops = &addr_ops;
245 p_var->val.p_address = NULL;
248 p_var->ops = &void_ops;
250 if( (i_type & VLC_VAR_CLASS) != VLC_VAR_VOID )
251 msg_Err( p_this, "Creating the variable '%s' without a type",
256 if( i_type & VLC_VAR_DOINHERIT )
258 if( var_Inherit( p_this, psz_name, i_type, &p_var->val ) )
259 msg_Err( p_this, "cannot inherit value for %s", psz_name );
260 else if( i_type & VLC_VAR_HASCHOICE )
262 /* We must add the inherited value to our choice list */
263 p_var->i_default = 0;
265 INSERT_ELEM( p_var->choices.p_values, p_var->choices.i_count,
267 INSERT_ELEM( p_var->choices_text.p_values,
268 p_var->choices_text.i_count, 0, p_var->val );
269 p_var->ops->pf_dup( &p_var->choices.p_values[0] );
270 p_var->choices_text.p_values[0].psz_string = NULL;
274 vlc_object_internals_t *p_priv = vlc_internals( p_this );
275 variable_t **pp_var, *p_oldvar;
276 int ret = VLC_SUCCESS;
278 vlc_mutex_lock( &p_priv->var_lock );
280 pp_var = tsearch( p_var, &p_priv->var_root, varcmp );
281 if( unlikely(pp_var == NULL) )
283 else if( (p_oldvar = *pp_var) == p_var )
285 else if( unlikely((i_type ^ p_oldvar->i_type) & VLC_VAR_CLASS) )
286 { /* If the types differ, variable creation failed. */
287 msg_Err( p_this, "Variable '%s' (0x%04x) already exist "
288 "but with a different type (0x%04x)",
289 psz_name, p_oldvar->i_type, i_type );
295 p_oldvar->i_type |= i_type & (VLC_VAR_ISCOMMAND|VLC_VAR_HASCHOICE);
297 vlc_mutex_unlock( &p_priv->var_lock );
299 /* If we did not need to create a new variable, free everything... */
307 * Destroy a vlc variable
309 * Look for the variable and destroy it if it is found. As in var_Create we
310 * do a call to memmove() but we have performance counterparts elsewhere.
312 * \param p_this The object that holds the variable
313 * \param psz_name The name of the variable
315 int var_Destroy( vlc_object_t *p_this, const char *psz_name )
321 vlc_object_internals_t *p_priv = vlc_internals( p_this );
323 vlc_mutex_lock( &p_priv->var_lock );
325 p_var = Lookup( p_this, psz_name );
328 vlc_mutex_unlock( &p_priv->var_lock );
332 WaitUnused( p_this, p_var );
334 if( --p_var->i_usage == 0 )
335 tdelete( p_var, &p_priv->var_root, varcmp );
338 vlc_mutex_unlock( &p_priv->var_lock );
345 static void CleanupVar( void *var )
350 void var_DestroyAll( vlc_object_t *obj )
352 vlc_object_internals_t *priv = vlc_internals( obj );
354 tdestroy( priv->var_root, CleanupVar );
355 priv->var_root = NULL;
360 * Perform an action on a variable
362 * \param p_this The object that holds the variable
363 * \param psz_name The name of the variable
364 * \param i_action The action to perform. Must be one of \ref var_action
365 * \param p_val First action parameter
366 * \param p_val2 Second action parameter
368 int var_Change( vlc_object_t *p_this, const char *psz_name,
369 int i_action, vlc_value_t *p_val, vlc_value_t *p_val2 )
378 vlc_object_internals_t *p_priv = vlc_internals( p_this );
380 vlc_mutex_lock( &p_priv->var_lock );
382 p_var = Lookup( p_this, psz_name );
385 vlc_mutex_unlock( &p_priv->var_lock );
392 if( p_var->i_type & VLC_VAR_HASMIN )
394 p_var->ops->pf_free( &p_var->min );
396 p_var->i_type |= VLC_VAR_HASMIN;
398 p_var->ops->pf_dup( &p_var->min );
399 CheckValue( p_var, &p_var->val );
402 if( p_var->i_type & VLC_VAR_HASMIN )
408 if( p_var->i_type & VLC_VAR_HASMAX )
410 p_var->ops->pf_free( &p_var->max );
412 p_var->i_type |= VLC_VAR_HASMAX;
414 p_var->ops->pf_dup( &p_var->max );
415 CheckValue( p_var, &p_var->val );
418 if( p_var->i_type & VLC_VAR_HASMAX )
423 case VLC_VAR_SETSTEP:
424 if( p_var->i_type & VLC_VAR_HASSTEP )
426 p_var->ops->pf_free( &p_var->step );
428 p_var->i_type |= VLC_VAR_HASSTEP;
429 p_var->step = *p_val;
430 p_var->ops->pf_dup( &p_var->step );
431 CheckValue( p_var, &p_var->val );
433 case VLC_VAR_GETSTEP:
434 if( p_var->i_type & VLC_VAR_HASSTEP )
436 *p_val = p_var->step;
439 case VLC_VAR_ADDCHOICE:
440 i = p_var->choices.i_count;
442 INSERT_ELEM( p_var->choices.p_values, p_var->choices.i_count,
444 INSERT_ELEM( p_var->choices_text.p_values,
445 p_var->choices_text.i_count, i, *p_val );
446 p_var->ops->pf_dup( &p_var->choices.p_values[i] );
447 p_var->choices_text.p_values[i].psz_string =
448 ( p_val2 && p_val2->psz_string ) ?
449 strdup( p_val2->psz_string ) : NULL;
451 CheckValue( p_var, &p_var->val );
453 case VLC_VAR_DELCHOICE:
454 for( i = 0 ; i < p_var->choices.i_count ; i++ )
456 if( p_var->ops->pf_cmp( p_var->choices.p_values[i], *p_val ) == 0 )
462 if( i == p_var->choices.i_count )
465 vlc_mutex_unlock( &p_priv->var_lock );
469 if( p_var->i_default > i )
473 else if( p_var->i_default == i )
475 p_var->i_default = -1;
478 p_var->ops->pf_free( &p_var->choices.p_values[i] );
479 free( p_var->choices_text.p_values[i].psz_string );
480 REMOVE_ELEM( p_var->choices.p_values, p_var->choices.i_count, i );
481 REMOVE_ELEM( p_var->choices_text.p_values,
482 p_var->choices_text.i_count, i );
484 CheckValue( p_var, &p_var->val );
486 case VLC_VAR_CHOICESCOUNT:
487 p_val->i_int = p_var->choices.i_count;
489 case VLC_VAR_CLEARCHOICES:
490 for( i = 0 ; i < p_var->choices.i_count ; i++ )
492 p_var->ops->pf_free( &p_var->choices.p_values[i] );
494 for( i = 0 ; i < p_var->choices_text.i_count ; i++ )
495 free( p_var->choices_text.p_values[i].psz_string );
497 if( p_var->choices.i_count ) free( p_var->choices.p_values );
498 if( p_var->choices_text.i_count ) free( p_var->choices_text.p_values );
500 p_var->choices.i_count = 0;
501 p_var->choices.p_values = NULL;
502 p_var->choices_text.i_count = 0;
503 p_var->choices_text.p_values = NULL;
504 p_var->i_default = -1;
506 case VLC_VAR_SETDEFAULT:
507 /* FIXME: the list is sorted, dude. Use something cleverer. */
508 for( i = 0 ; i < p_var->choices.i_count ; i++ )
510 if( p_var->ops->pf_cmp( p_var->choices.p_values[i], *p_val ) == 0 )
516 if( i == p_var->choices.i_count )
522 p_var->i_default = i;
523 CheckValue( p_var, &p_var->val );
525 case VLC_VAR_SETVALUE:
526 /* Duplicate data if needed */
528 p_var->ops->pf_dup( &newval );
529 /* Backup needed stuff */
531 /* Check boundaries and list */
532 CheckValue( p_var, &newval );
533 /* Set the variable */
535 /* Free data if needed */
536 p_var->ops->pf_free( &oldval );
538 case VLC_VAR_GETCHOICES:
539 case VLC_VAR_GETLIST:
540 p_val->p_list = malloc( sizeof(vlc_list_t) );
541 if( p_val2 ) p_val2->p_list = malloc( sizeof(vlc_list_t) );
542 if( p_var->choices.i_count )
544 p_val->p_list->p_values = malloc( p_var->choices.i_count
545 * sizeof(vlc_value_t) );
546 p_val->p_list->pi_types = malloc( p_var->choices.i_count
550 p_val2->p_list->p_values =
551 malloc( p_var->choices.i_count * sizeof(vlc_value_t) );
552 p_val2->p_list->pi_types =
553 malloc( p_var->choices.i_count * sizeof(int) );
556 p_val->p_list->i_count = p_var->choices.i_count;
557 if( p_val2 ) p_val2->p_list->i_count = p_var->choices.i_count;
558 for( i = 0 ; i < p_var->choices.i_count ; i++ )
560 p_val->p_list->p_values[i] = p_var->choices.p_values[i];
561 p_val->p_list->pi_types[i] = p_var->i_type;
562 p_var->ops->pf_dup( &p_val->p_list->p_values[i] );
565 p_val2->p_list->p_values[i].psz_string =
566 p_var->choices_text.p_values[i].psz_string ?
567 strdup(p_var->choices_text.p_values[i].psz_string) : NULL;
568 p_val2->p_list->pi_types[i] = VLC_VAR_STRING;
572 case VLC_VAR_SETTEXT:
573 free( p_var->psz_text );
574 if( p_val && p_val->psz_string )
575 p_var->psz_text = strdup( p_val->psz_string );
577 p_var->psz_text = NULL;
579 case VLC_VAR_GETTEXT:
580 p_val->psz_string = p_var->psz_text ? strdup( p_var->psz_text )
583 case VLC_VAR_SETISCOMMAND:
584 p_var->i_type |= VLC_VAR_ISCOMMAND;
591 vlc_mutex_unlock( &p_priv->var_lock );
598 * Perform a Get and Set on a variable
600 * \param p_this: The object that hold the variable
601 * \param psz_name: the name of the variable
602 * \param i_action: the action to perform
603 * \param p_val: The action parameter
604 * \return vlc error codes
606 int var_GetAndSet( vlc_object_t *p_this, const char *psz_name, int i_action,
616 vlc_object_internals_t *p_priv = vlc_internals( p_this );
618 vlc_mutex_lock( &p_priv->var_lock );
619 p_var = Lookup( p_this, psz_name );
622 vlc_mutex_unlock( &p_priv->var_lock );
626 WaitUnused( p_this, p_var );
628 /* Duplicated data if needed */
629 //p_var->ops->pf_dup( &val );
631 /* Backup needed stuff */
634 /* depending of the action requiered */
637 case VLC_VAR_BOOL_TOGGLE:
638 assert( ( p_var->i_type & VLC_VAR_BOOL ) == VLC_VAR_BOOL );
639 p_var->val.b_bool = !p_var->val.b_bool;
641 case VLC_VAR_INTEGER_ADD:
642 assert( ( p_var->i_type & VLC_VAR_INTEGER ) == VLC_VAR_INTEGER );
643 p_var->val.i_int += p_val->i_int;
645 case VLC_VAR_INTEGER_OR:
646 assert( ( p_var->i_type & VLC_VAR_INTEGER ) == VLC_VAR_INTEGER );
647 p_var->val.i_int |= p_val->i_int;
649 case VLC_VAR_INTEGER_NAND:
650 assert( ( p_var->i_type & VLC_VAR_INTEGER ) == VLC_VAR_INTEGER );
651 p_var->val.i_int &= ~p_val->i_int;
654 vlc_mutex_unlock( &p_priv->var_lock );
658 /* Check boundaries */
659 CheckValue( p_var, &p_var->val );
662 /* Deal with callbacks.*/
663 i_ret = TriggerCallback( p_this, p_var, psz_name, oldval );
665 vlc_mutex_unlock( &p_priv->var_lock );
672 * Request a variable's type
674 * \return The variable type if it exists, or 0 if the
675 * variable could not be found.
678 int var_Type( vlc_object_t *p_this, const char *psz_name )
685 vlc_object_internals_t *p_priv = vlc_internals( p_this );
687 vlc_mutex_lock( &p_priv->var_lock );
689 p_var = Lookup( p_this, psz_name );
691 i_type = p_var->i_type;
693 vlc_mutex_unlock( &p_priv->var_lock );
698 #undef var_SetChecked
699 int var_SetChecked( vlc_object_t *p_this, const char *psz_name,
700 int expected_type, vlc_value_t val )
702 int i_ret = VLC_SUCCESS;
708 vlc_object_internals_t *p_priv = vlc_internals( p_this );
710 vlc_mutex_lock( &p_priv->var_lock );
712 p_var = Lookup( p_this, psz_name );
715 vlc_mutex_unlock( &p_priv->var_lock );
719 assert( expected_type == 0 ||
720 (p_var->i_type & VLC_VAR_CLASS) == expected_type );
722 /* Alert if the type is VLC_VAR_VOID */
723 if( ( p_var->i_type & VLC_VAR_TYPE ) == VLC_VAR_VOID )
724 msg_Warn( p_this, "Calling var_Set on the void variable '%s' (0x%04x)", psz_name, p_var->i_type );
728 WaitUnused( p_this, p_var );
730 /* Duplicate data if needed */
731 p_var->ops->pf_dup( &val );
733 /* Backup needed stuff */
736 /* Check boundaries and list */
737 CheckValue( p_var, &val );
739 /* Set the variable */
742 /* Deal with callbacks */
743 i_ret = TriggerCallback( p_this, p_var, psz_name, oldval );
745 /* Free data if needed */
746 p_var->ops->pf_free( &oldval );
748 vlc_mutex_unlock( &p_priv->var_lock );
755 * Set a variable's value
757 * \param p_this The object that hold the variable
758 * \param psz_name The name of the variable
759 * \param val the value to set
761 int var_Set( vlc_object_t *p_this, const char *psz_name, vlc_value_t val )
763 return var_SetChecked( p_this, psz_name, 0, val );
766 #undef var_GetChecked
767 int var_GetChecked( vlc_object_t *p_this, const char *psz_name,
768 int expected_type, vlc_value_t *p_val )
772 vlc_object_internals_t *p_priv = vlc_internals( p_this );
774 int err = VLC_SUCCESS;
776 vlc_mutex_lock( &p_priv->var_lock );
778 p_var = Lookup( p_this, psz_name );
781 assert( expected_type == 0 ||
782 (p_var->i_type & VLC_VAR_CLASS) == expected_type );
784 /* Really get the variable */
788 /* Alert if the type is VLC_VAR_VOID */
789 if( ( p_var->i_type & VLC_VAR_TYPE ) == VLC_VAR_VOID )
790 msg_Warn( p_this, "Calling var_Get on the void variable '%s' (0x%04x)", psz_name, p_var->i_type );
793 /* Duplicate value if needed */
794 p_var->ops->pf_dup( p_val );
799 vlc_mutex_unlock( &p_priv->var_lock );
805 * Get a variable's value
807 * \param p_this The object that holds the variable
808 * \param psz_name The name of the variable
809 * \param p_val Pointer to a vlc_value_t that will hold the variable's value
810 * after the function is finished
812 int var_Get( vlc_object_t *p_this, const char *psz_name, vlc_value_t *p_val )
814 return var_GetChecked( p_this, psz_name, 0, p_val );
817 #undef var_AddCallback
819 * Register a callback in a variable
821 * We store a function pointer that will be called upon variable
824 * \param p_this The object that holds the variable
825 * \param psz_name The name of the variable
826 * \param pf_callback The function pointer
827 * \param p_data A generic pointer that will be passed as the last
828 * argument to the callback function.
830 * \warning The callback function is run in the thread that calls var_Set on
831 * the variable. Use proper locking. This thread may not have much
832 * time to spare, so keep callback functions short.
834 int var_AddCallback( vlc_object_t *p_this, const char *psz_name,
835 vlc_callback_t pf_callback, void *p_data )
838 callback_entry_t entry;
842 vlc_object_internals_t *p_priv = vlc_internals( p_this );
844 entry.pf_callback = pf_callback;
845 entry.p_data = p_data;
847 vlc_mutex_lock( &p_priv->var_lock );
849 p_var = Lookup( p_this, psz_name );
853 msg_Warn( p_this, "Failed to add a callback to the non-existing "
854 "variable '%s'", psz_name );
856 vlc_mutex_unlock( &p_priv->var_lock );
860 WaitUnused( p_this, p_var );
861 INSERT_ELEM( p_var->p_entries,
866 vlc_mutex_unlock( &p_priv->var_lock );
871 #undef var_DelCallback
873 * Remove a callback from a variable
875 * pf_callback and p_data have to be given again, because different objects
876 * might have registered the same callback function.
878 int var_DelCallback( vlc_object_t *p_this, const char *psz_name,
879 vlc_callback_t pf_callback, void *p_data )
884 bool b_found_similar = false;
889 vlc_object_internals_t *p_priv = vlc_internals( p_this );
891 vlc_mutex_lock( &p_priv->var_lock );
893 p_var = Lookup( p_this, psz_name );
896 vlc_mutex_unlock( &p_priv->var_lock );
900 WaitUnused( p_this, p_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 )
910 else if( p_var->p_entries[i_entry].pf_callback == pf_callback )
911 b_found_similar = true;
918 if( b_found_similar )
919 fprintf( stderr, "Calling var_DelCallback for '%s' with the same "
920 "function but not the same data.", psz_name );
923 vlc_mutex_unlock( &p_priv->var_lock );
927 REMOVE_ELEM( p_var->p_entries, p_var->i_entries, i_entry );
929 vlc_mutex_unlock( &p_priv->var_lock );
934 #undef var_TriggerCallback
936 * Trigger callback on a variable
938 * \param p_this The object that hold the variable
939 * \param psz_name The name of the variable
941 int var_TriggerCallback( vlc_object_t *p_this, const char *psz_name )
948 vlc_object_internals_t *p_priv = vlc_internals( p_this );
950 vlc_mutex_lock( &p_priv->var_lock );
952 p_var = Lookup( p_this, psz_name );
955 vlc_mutex_unlock( &p_priv->var_lock );
959 WaitUnused( p_this, p_var );
961 /* Deal with callbacks. Tell we're in a callback, release the lock,
962 * call stored functions, retake the lock. */
963 i_ret = TriggerCallback( p_this, p_var, psz_name, p_var->val );
965 vlc_mutex_unlock( &p_priv->var_lock );
969 /** Parse a stringified option
970 * This function parse a string option and create the associated object
972 * The option must be of the form "[no[-]]foo[=bar]" where foo is the
973 * option name and bar is the value of the option.
974 * \param p_obj the object in which the variable must be created
975 * \param psz_option the option to parse
976 * \param trusted whether the option is set by a trusted input or not
979 void var_OptionParse( vlc_object_t *p_obj, const char *psz_option,
982 char *psz_name, *psz_value;
987 val.psz_string = NULL;
989 /* It's too much of a hassle to remove the ':' when we parse
991 if( psz_option[0] == ':' )
997 psz_name = strdup( psz_option );
998 if( psz_name == NULL )
1001 psz_value = strchr( psz_name, '=' );
1002 if( psz_value != NULL )
1003 *psz_value++ = '\0';
1005 i_type = config_GetType( p_obj, psz_name );
1006 if( !i_type && !psz_value )
1008 /* check for "no-foo" or "nofoo" */
1009 if( !strncmp( psz_name, "no-", 3 ) )
1011 memmove( psz_name, psz_name + 3, strlen(psz_name) + 1 - 3 );
1013 else if( !strncmp( psz_name, "no", 2 ) )
1015 memmove( psz_name, psz_name + 2, strlen(psz_name) + 1 - 2 );
1017 else goto cleanup; /* Option doesn't exist */
1020 i_type = config_GetType( p_obj, psz_name );
1022 if( !i_type ) goto cleanup; /* Option doesn't exist */
1024 if( ( i_type != VLC_VAR_BOOL ) &&
1025 ( !psz_value || !*psz_value ) ) goto cleanup; /* Invalid value */
1027 /* check if option is unsafe */
1028 if( !trusted && !config_IsSafe( psz_name ) )
1030 msg_Err( p_obj, "unsafe option \"%s\" has been ignored for "
1031 "security reasons", psz_name );
1036 /* Create the variable in the input object.
1037 * Children of the input object will be able to retreive this value
1038 * thanks to the inheritance property of the object variables. */
1039 var_Create( p_obj, psz_name, i_type );
1044 val.b_bool = !b_isno;
1047 case VLC_VAR_INTEGER:
1048 val.i_int = strtoll( psz_value, NULL, 0 );
1052 val.f_float = us_atof( psz_value );
1055 case VLC_VAR_STRING:
1056 val.psz_string = psz_value;
1063 var_Set( p_obj, psz_name, val );
1069 #undef var_LocationParse
1071 * Parses a set of colon-separated <variable name>=<value> pairs. Some access
1072 * (or access_demux) plugins uses this scheme in media resource location.
1073 * @note Only trusted/safe variables are allowed. This is intended.
1075 * @warning Only use this for plugins implementing VLC-specific resource
1076 * location schemes. This would not make any sense for standardized ones.
1078 * @param obj VLC object on which to set variables (and emit error messages)
1079 * @param mrl string to parse
1080 * @param pref prefix to prepend to option names in the string
1082 * @return VLC_ENOMEM on error, VLC_SUCCESS on success.
1084 int var_LocationParse (vlc_object_t *obj, const char *mrl, const char *pref)
1086 int ret = VLC_SUCCESS;
1087 size_t preflen = strlen (pref) + 1;
1089 assert(mrl != NULL);
1090 while (*mrl != '\0')
1092 mrl += strspn (mrl, ":"); /* skip leading colon(s) */
1094 size_t len = strcspn (mrl, ":");
1095 char *buf = malloc (preflen + len);
1097 if (likely(buf != NULL))
1099 /* NOTE: this does not support the "no-<varname>" bool syntax. */
1100 /* DO NOT use asprintf() here; it won't work! Think again. */
1101 snprintf (buf, preflen + len, "%s%s", pref, mrl);
1102 var_OptionParse (obj, buf, false);
1114 * Waits until the variable is inactive (i.e. not executing a callback)
1116 static void WaitUnused( vlc_object_t *p_this, variable_t *p_var )
1118 vlc_object_internals_t *p_priv = vlc_internals( p_this );
1120 mutex_cleanup_push( &p_priv->var_lock );
1121 while( p_var->b_incallback )
1122 vlc_cond_wait( &p_priv->var_wait, &p_priv->var_lock );
1126 /*****************************************************************************
1127 * CheckValue: check that a value is valid wrt. a variable
1128 *****************************************************************************
1129 * This function checks p_val's value against p_var's limitations such as
1130 * minimal and maximal value, step, in-list position, and modifies p_val if
1132 ****************************************************************************/
1133 static void CheckValue ( variable_t *p_var, vlc_value_t *p_val )
1135 /* Check that our variable is in the list */
1136 if( p_var->i_type & VLC_VAR_HASCHOICE && p_var->choices.i_count )
1140 /* This list is not sorted so go throug it (this is a small list) */
1141 for( i = p_var->choices.i_count ; i-- ; )
1143 if( p_var->ops->pf_cmp( *p_val, p_var->choices.p_values[i] ) == 0 )
1149 /* If not found, change it to anything vaguely valid */
1152 /* Free the old variable, get the new one, dup it */
1153 p_var->ops->pf_free( p_val );
1154 *p_val = p_var->choices.p_values[p_var->i_default >= 0
1155 ? p_var->i_default : 0 ];
1156 p_var->ops->pf_dup( p_val );
1160 /* Check that our variable is within the bounds */
1161 switch( p_var->i_type & VLC_VAR_TYPE )
1163 case VLC_VAR_INTEGER:
1164 if( p_var->i_type & VLC_VAR_HASSTEP && p_var->step.i_int
1165 && (p_val->i_int % p_var->step.i_int) )
1167 p_val->i_int = (p_val->i_int + (p_var->step.i_int / 2))
1168 / p_var->step.i_int * p_var->step.i_int;
1170 if( p_var->i_type & VLC_VAR_HASMIN
1171 && p_val->i_int < p_var->min.i_int )
1173 p_val->i_int = p_var->min.i_int;
1175 if( p_var->i_type & VLC_VAR_HASMAX
1176 && p_val->i_int > p_var->max.i_int )
1178 p_val->i_int = p_var->max.i_int;
1182 if( p_var->i_type & VLC_VAR_HASSTEP && p_var->step.f_float )
1184 float f_round = p_var->step.f_float * (float)(int)( 0.5 +
1185 p_val->f_float / p_var->step.f_float );
1186 if( p_val->f_float != f_round )
1188 p_val->f_float = f_round;
1191 if( p_var->i_type & VLC_VAR_HASMIN
1192 && p_val->f_float < p_var->min.f_float )
1194 p_val->f_float = p_var->min.f_float;
1196 if( p_var->i_type & VLC_VAR_HASMAX
1197 && p_val->f_float > p_var->max.f_float )
1199 p_val->f_float = p_var->max.f_float;
1209 * Finds the value of a variable. If the specified object does not hold a
1210 * variable with the specified name, try the parent object, and iterate until
1211 * the top of the tree. If no match is found, the value is read from the
1214 int var_Inherit( vlc_object_t *p_this, const char *psz_name, int i_type,
1215 vlc_value_t *p_val )
1218 if (p_this != VLC_OBJECT(p_this->p_libvlc)
1219 && unlikely(p_this->p_parent == NULL))
1221 msg_Info (p_this, "%s(%s) on detached object", __func__, psz_name);
1225 i_type &= VLC_VAR_CLASS;
1226 for( vlc_object_t *obj = p_this; obj != NULL; obj = obj->p_parent )
1228 if( var_GetChecked( obj, psz_name, i_type, p_val ) == VLC_SUCCESS )
1231 if (obj != p_this && obj != VLC_OBJECT(p_this->p_libvlc)
1232 && unlikely(obj->p_parent == NULL))
1234 msg_Info (p_this, "%s(%s) on detached tree [%p] %s", __func__,
1235 psz_name, obj, obj->psz_object_type);
1241 /* else take value from config */
1242 switch( i_type & VLC_VAR_CLASS )
1244 case VLC_VAR_STRING:
1245 p_val->psz_string = config_GetPsz( p_this, psz_name );
1246 if( !p_val->psz_string ) p_val->psz_string = strdup("");
1249 p_val->f_float = config_GetFloat( p_this, psz_name );
1251 case VLC_VAR_INTEGER:
1252 p_val->i_int = config_GetInt( p_this, psz_name );
1255 p_val->b_bool = config_GetInt( p_this, psz_name );
1257 case VLC_VAR_ADDRESS:
1260 msg_Warn( p_this, "Could not inherit value for var %s "
1261 "from config. Invalid Type", psz_name );
1264 /*msg_Dbg( p_this, "Inherited value for var %s from config", psz_name );*/
1270 * It inherits a string as an unsigned rational number (it also accepts basic
1273 * It returns an error if the rational number cannot be parsed (0/0 is valid).
1274 * The rational is already reduced.
1276 int (var_InheritURational)(vlc_object_t *object,
1277 unsigned *num, unsigned *den,
1285 char *tmp = var_InheritString(object, var);
1290 unsigned n = strtol(tmp, &next, 0);
1291 unsigned d = strtol(*next ? &next[1] : "0", NULL, 0);
1294 /* Interpret as a float number */
1295 double r = us_atof(tmp);
1310 vlc_ureduce(num, den, n, d, 0);
1317 return VLC_EGENERIC;
1320 /**********************************************************************
1321 * Trigger the callbacks.
1322 * Tell we're in a callback, release the lock, call stored functions,
1324 **********************************************************************/
1325 static int TriggerCallback( vlc_object_t *p_this, variable_t *p_var,
1326 const char *psz_name, vlc_value_t oldval )
1330 int i_entries = p_var->i_entries;
1331 if( i_entries == 0 )
1334 callback_entry_t *p_entries = p_var->p_entries;
1335 vlc_object_internals_t *p_priv = vlc_internals( p_this );
1337 assert( !p_var->b_incallback );
1338 p_var->b_incallback = true;
1339 vlc_mutex_unlock( &p_priv->var_lock );
1341 /* The real calls */
1342 for( ; i_entries-- ; )
1344 p_entries[i_entries].pf_callback( p_this, psz_name, oldval, p_var->val,
1345 p_entries[i_entries].p_data );
1348 vlc_mutex_lock( &p_priv->var_lock );
1349 p_var->b_incallback = false;
1350 vlc_cond_broadcast( &p_priv->var_wait );
1356 /**********************************************************************
1357 * Execute a var command on an object identified by its name
1358 **********************************************************************/
1359 int var_Command( vlc_object_t *p_this, const char *psz_name,
1360 const char *psz_cmd, const char *psz_arg, char **psz_msg )
1362 vlc_object_t *p_obj = vlc_object_find_name( p_this->p_libvlc,
1369 *psz_msg = strdup( "Unknown destination object." );
1373 i_type = var_Type( p_obj, psz_cmd );
1374 if( !( i_type&VLC_VAR_ISCOMMAND ) )
1376 vlc_object_release( p_obj );
1378 *psz_msg = strdup( "Variable doesn't exist or isn't a command." );
1379 return VLC_EGENERIC;
1382 i_type &= VLC_VAR_CLASS;
1385 case VLC_VAR_INTEGER:
1386 i_ret = var_SetInteger( p_obj, psz_cmd, atoi( psz_arg ) );
1389 i_ret = var_SetFloat( p_obj, psz_cmd, us_atof( psz_arg ) );
1391 case VLC_VAR_STRING:
1392 i_ret = var_SetString( p_obj, psz_cmd, psz_arg );
1395 i_ret = var_SetBool( p_obj, psz_cmd, atoi( psz_arg ) );
1398 i_ret = VLC_EGENERIC;
1402 vlc_object_release( p_obj );
1406 if( asprintf( psz_msg, "%s on object %s returned %i (%s)",
1407 psz_cmd, psz_name, i_ret, vlc_error( i_ret ) ) == -1)
1416 * Free a list and the associated strings
1417 * @param p_val: the list variable
1418 * @param p_val2: the variable associated or NULL
1420 void var_FreeList( vlc_value_t *p_val, vlc_value_t *p_val2 )
1423 if( p_val2 && p_val2->p_list )
1425 for( int i = 0; i < p_val2->p_list->i_count; i++ )
1426 free( p_val2->p_list->p_values[i].psz_string );
1427 if( p_val2->p_list->i_count )
1429 free( p_val2->p_list->p_values );
1430 free( p_val2->p_list->pi_types );
1432 free( p_val2->p_list );