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"
42 /*****************************************************************************
44 *****************************************************************************/
45 struct callback_entry_t
47 vlc_callback_t pf_callback;
51 /*****************************************************************************
52 * Local comparison functions, returns 0 if v == w, < 0 if v < w, > 0 if v > w
53 *****************************************************************************/
54 static int CmpBool( vlc_value_t v, vlc_value_t w )
56 return v.b_bool ? w.b_bool ? 0 : 1 : w.b_bool ? -1 : 0;
59 static int CmpInt( vlc_value_t v, vlc_value_t w )
61 return v.i_int == w.i_int ? 0 : v.i_int > w.i_int ? 1 : -1;
64 static int CmpTime( vlc_value_t v, vlc_value_t w )
66 return v.i_time == w.i_time ? 0 : v.i_time > w.i_time ? 1 : -1;
69 static int CmpString( vlc_value_t v, vlc_value_t w )
72 return !w.psz_string ? 0 : -1;
74 return !w.psz_string ? 1 : strcmp( v.psz_string, w.psz_string );
76 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; }
77 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; }
79 /*****************************************************************************
80 * Local duplication functions, and local deallocation functions
81 *****************************************************************************/
82 static void DupDummy( vlc_value_t *p_val ) { (void)p_val; /* unused */ }
83 static void DupString( vlc_value_t *p_val )
85 p_val->psz_string = strdup( p_val->psz_string ? p_val->psz_string : "" );
88 static void FreeDummy( vlc_value_t *p_val ) { (void)p_val; /* unused */ }
89 static void FreeString( vlc_value_t *p_val ) { free( p_val->psz_string ); }
90 static void FreeMutex( vlc_value_t *p_val ) { vlc_mutex_destroy( (vlc_mutex_t*)p_val->p_address ); free( p_val->p_address ); }
92 static void FreeList( vlc_value_t *p_val )
95 for( i = 0; i < p_val->p_list->i_count; i++ )
97 switch( p_val->p_list->pi_types[i] & VLC_VAR_CLASS )
100 FreeString( &p_val->p_list->p_values[i] );
103 FreeMutex( &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 mutex_ops = { CmpAddress, DupDummy, FreeMutex, },
125 string_ops = { CmpString, DupString, FreeString, },
126 time_ops = { CmpTime, DupDummy, FreeDummy, },
127 coords_ops = { NULL, DupDummy, FreeDummy, };
129 /*****************************************************************************
131 *****************************************************************************/
132 static void WaitUnused ( vlc_object_t *, variable_t * );
134 static void CheckValue ( variable_t *, vlc_value_t * );
136 static int TriggerCallback( vlc_object_t *, variable_t *, const char *,
139 static int varcmp( const void *a, const void *b )
141 const variable_t *va = a, *vb = b;
143 /* psz_name must be first */
144 assert( va == (const void *)&va->psz_name );
145 return strcmp( va->psz_name, vb->psz_name );
148 static variable_t *Lookup( vlc_object_t *obj, const char *psz_name )
150 vlc_object_internals_t *priv = vlc_internals( obj );
153 vlc_assert_locked( &priv->var_lock );
154 pp_var = tfind( &psz_name, &priv->var_root, varcmp );
155 return (pp_var != NULL) ? *pp_var : NULL;
158 static void Destroy( variable_t *p_var )
160 p_var->ops->pf_free( &p_var->val );
161 if( p_var->choices.i_count )
163 for( int i = 0 ; i < p_var->choices.i_count ; i++ )
165 p_var->ops->pf_free( &p_var->choices.p_values[i] );
166 free( p_var->choices_text.p_values[i].psz_string );
168 free( p_var->choices.p_values );
169 free( p_var->choices_text.p_values );
171 free( p_var->psz_name );
172 free( p_var->psz_text );
173 free( p_var->p_entries );
179 * Initialize a vlc variable
181 * We hash the given string and insert it into the sorted list. The insertion
182 * may require slow memory copies, but think about what we gain in the log(n)
183 * lookup phase when setting/getting the variable value!
185 * \param p_this The object in which to create the variable
186 * \param psz_name The name of the variable
187 * \param i_type The variables type. Must be one of \ref var_type combined with
188 * zero or more \ref var_flags
190 int var_Create( vlc_object_t *p_this, const char *psz_name, int i_type )
194 variable_t *p_var = calloc( 1, sizeof( *p_var ) );
198 p_var->psz_name = strdup( psz_name );
199 p_var->psz_text = NULL;
201 p_var->i_type = i_type & ~VLC_VAR_DOINHERIT;
205 p_var->i_default = -1;
206 p_var->choices.i_count = 0;
207 p_var->choices.p_values = NULL;
208 p_var->choices_text.i_count = 0;
209 p_var->choices_text.p_values = NULL;
211 p_var->b_incallback = false;
212 p_var->i_entries = 0;
213 p_var->p_entries = NULL;
215 /* Always initialize the variable, even if it is a list variable; this
216 * will lead to errors if the variable is not initialized, but it will
217 * not cause crashes in the variable handling. */
218 switch( i_type & VLC_VAR_CLASS )
221 p_var->ops = &bool_ops;
222 p_var->val.b_bool = false;
224 case VLC_VAR_INTEGER:
225 p_var->ops = &int_ops;
226 p_var->val.i_int = 0;
229 p_var->ops = &string_ops;
230 p_var->val.psz_string = NULL;
233 p_var->ops = &float_ops;
234 p_var->val.f_float = 0.0;
237 p_var->ops = &time_ops;
238 p_var->val.i_time = 0;
241 p_var->ops = &coords_ops;
242 p_var->val.coords.x = p_var->val.coords.y = 0;
244 case VLC_VAR_ADDRESS:
245 p_var->ops = &addr_ops;
246 p_var->val.p_address = NULL;
249 p_var->ops = &mutex_ops;
250 p_var->val.p_address = malloc( sizeof(vlc_mutex_t) );
251 vlc_mutex_init( (vlc_mutex_t*)p_var->val.p_address );
254 p_var->ops = &void_ops;
256 if( (i_type & VLC_VAR_CLASS) != VLC_VAR_VOID )
257 msg_Err( p_this, "Creating the variable '%s' without a type",
262 if( i_type & VLC_VAR_DOINHERIT )
264 if( var_Inherit( p_this, psz_name, i_type, &p_var->val ) )
265 msg_Err( p_this, "cannot inherit value for %s", psz_name );
266 else if( i_type & VLC_VAR_HASCHOICE )
268 /* We must add the inherited value to our choice list */
269 p_var->i_default = 0;
271 INSERT_ELEM( p_var->choices.p_values, p_var->choices.i_count,
273 INSERT_ELEM( p_var->choices_text.p_values,
274 p_var->choices_text.i_count, 0, p_var->val );
275 p_var->ops->pf_dup( &p_var->choices.p_values[0] );
276 p_var->choices_text.p_values[0].psz_string = NULL;
280 vlc_object_internals_t *p_priv = vlc_internals( p_this );
281 variable_t **pp_var, *p_oldvar;
282 int ret = VLC_SUCCESS;
284 vlc_mutex_lock( &p_priv->var_lock );
286 pp_var = tsearch( p_var, &p_priv->var_root, varcmp );
287 if( unlikely(pp_var == NULL) )
289 else if( (p_oldvar = *pp_var) == p_var )
291 else if( unlikely((i_type ^ p_oldvar->i_type) & VLC_VAR_CLASS) )
292 { /* If the types differ, variable creation failed. */
293 msg_Err( p_this, "Variable '%s' (0x%04x) already exist "
294 "but with a different type (0x%04x)",
295 psz_name, p_oldvar->i_type, i_type );
301 p_oldvar->i_type |= i_type & (VLC_VAR_ISCOMMAND|VLC_VAR_HASCHOICE);
303 vlc_mutex_unlock( &p_priv->var_lock );
305 /* If we did not need to create a new variable, free everything... */
313 * Destroy a vlc variable
315 * Look for the variable and destroy it if it is found. As in var_Create we
316 * do a call to memmove() but we have performance counterparts elsewhere.
318 * \param p_this The object that holds the variable
319 * \param psz_name The name of the variable
321 int var_Destroy( vlc_object_t *p_this, const char *psz_name )
327 vlc_object_internals_t *p_priv = vlc_internals( p_this );
329 vlc_mutex_lock( &p_priv->var_lock );
331 p_var = Lookup( p_this, psz_name );
334 vlc_mutex_unlock( &p_priv->var_lock );
338 WaitUnused( p_this, p_var );
340 if( --p_var->i_usage == 0 )
341 tdelete( p_var, &p_priv->var_root, varcmp );
344 vlc_mutex_unlock( &p_priv->var_lock );
351 static void CleanupVar( void *var )
356 void var_DestroyAll( vlc_object_t *obj )
358 vlc_object_internals_t *priv = vlc_internals( obj );
360 tdestroy( priv->var_root, CleanupVar );
361 priv->var_root = NULL;
366 * Perform an action on a variable
368 * \param p_this The object that holds the variable
369 * \param psz_name The name of the variable
370 * \param i_action The action to perform. Must be one of \ref var_action
371 * \param p_val First action parameter
372 * \param p_val2 Second action parameter
374 int var_Change( vlc_object_t *p_this, const char *psz_name,
375 int i_action, vlc_value_t *p_val, vlc_value_t *p_val2 )
384 vlc_object_internals_t *p_priv = vlc_internals( p_this );
386 vlc_mutex_lock( &p_priv->var_lock );
388 p_var = Lookup( p_this, psz_name );
391 vlc_mutex_unlock( &p_priv->var_lock );
398 if( p_var->i_type & VLC_VAR_HASMIN )
400 p_var->ops->pf_free( &p_var->min );
402 p_var->i_type |= VLC_VAR_HASMIN;
404 p_var->ops->pf_dup( &p_var->min );
405 CheckValue( p_var, &p_var->val );
408 if( p_var->i_type & VLC_VAR_HASMIN )
414 if( p_var->i_type & VLC_VAR_HASMAX )
416 p_var->ops->pf_free( &p_var->max );
418 p_var->i_type |= VLC_VAR_HASMAX;
420 p_var->ops->pf_dup( &p_var->max );
421 CheckValue( p_var, &p_var->val );
424 if( p_var->i_type & VLC_VAR_HASMAX )
429 case VLC_VAR_SETSTEP:
430 if( p_var->i_type & VLC_VAR_HASSTEP )
432 p_var->ops->pf_free( &p_var->step );
434 p_var->i_type |= VLC_VAR_HASSTEP;
435 p_var->step = *p_val;
436 p_var->ops->pf_dup( &p_var->step );
437 CheckValue( p_var, &p_var->val );
439 case VLC_VAR_GETSTEP:
440 if( p_var->i_type & VLC_VAR_HASSTEP )
442 *p_val = p_var->step;
445 case VLC_VAR_ADDCHOICE:
446 i = p_var->choices.i_count;
448 INSERT_ELEM( p_var->choices.p_values, p_var->choices.i_count,
450 INSERT_ELEM( p_var->choices_text.p_values,
451 p_var->choices_text.i_count, i, *p_val );
452 p_var->ops->pf_dup( &p_var->choices.p_values[i] );
453 p_var->choices_text.p_values[i].psz_string =
454 ( p_val2 && p_val2->psz_string ) ?
455 strdup( p_val2->psz_string ) : NULL;
457 CheckValue( p_var, &p_var->val );
459 case VLC_VAR_DELCHOICE:
460 for( i = 0 ; i < p_var->choices.i_count ; i++ )
462 if( p_var->ops->pf_cmp( p_var->choices.p_values[i], *p_val ) == 0 )
468 if( i == p_var->choices.i_count )
471 vlc_mutex_unlock( &p_priv->var_lock );
475 if( p_var->i_default > i )
479 else if( p_var->i_default == i )
481 p_var->i_default = -1;
484 p_var->ops->pf_free( &p_var->choices.p_values[i] );
485 free( p_var->choices_text.p_values[i].psz_string );
486 REMOVE_ELEM( p_var->choices.p_values, p_var->choices.i_count, i );
487 REMOVE_ELEM( p_var->choices_text.p_values,
488 p_var->choices_text.i_count, i );
490 CheckValue( p_var, &p_var->val );
492 case VLC_VAR_CHOICESCOUNT:
493 p_val->i_int = p_var->choices.i_count;
495 case VLC_VAR_CLEARCHOICES:
496 for( i = 0 ; i < p_var->choices.i_count ; i++ )
498 p_var->ops->pf_free( &p_var->choices.p_values[i] );
500 for( i = 0 ; i < p_var->choices_text.i_count ; i++ )
501 free( p_var->choices_text.p_values[i].psz_string );
503 if( p_var->choices.i_count ) free( p_var->choices.p_values );
504 if( p_var->choices_text.i_count ) free( p_var->choices_text.p_values );
506 p_var->choices.i_count = 0;
507 p_var->choices.p_values = NULL;
508 p_var->choices_text.i_count = 0;
509 p_var->choices_text.p_values = NULL;
510 p_var->i_default = -1;
512 case VLC_VAR_SETDEFAULT:
513 /* FIXME: the list is sorted, dude. Use something cleverer. */
514 for( i = 0 ; i < p_var->choices.i_count ; i++ )
516 if( p_var->ops->pf_cmp( p_var->choices.p_values[i], *p_val ) == 0 )
522 if( i == p_var->choices.i_count )
528 p_var->i_default = i;
529 CheckValue( p_var, &p_var->val );
531 case VLC_VAR_SETVALUE:
532 /* Duplicate data if needed */
534 p_var->ops->pf_dup( &newval );
535 /* Backup needed stuff */
537 /* Check boundaries and list */
538 CheckValue( p_var, &newval );
539 /* Set the variable */
541 /* Free data if needed */
542 p_var->ops->pf_free( &oldval );
544 case VLC_VAR_GETCHOICES:
545 case VLC_VAR_GETLIST:
546 p_val->p_list = malloc( sizeof(vlc_list_t) );
547 if( p_val2 ) p_val2->p_list = malloc( sizeof(vlc_list_t) );
548 if( p_var->choices.i_count )
550 p_val->p_list->p_values = malloc( p_var->choices.i_count
551 * sizeof(vlc_value_t) );
552 p_val->p_list->pi_types = malloc( p_var->choices.i_count
556 p_val2->p_list->p_values =
557 malloc( p_var->choices.i_count * sizeof(vlc_value_t) );
558 p_val2->p_list->pi_types =
559 malloc( p_var->choices.i_count * sizeof(int) );
562 p_val->p_list->i_count = p_var->choices.i_count;
563 if( p_val2 ) p_val2->p_list->i_count = p_var->choices.i_count;
564 for( i = 0 ; i < p_var->choices.i_count ; i++ )
566 p_val->p_list->p_values[i] = p_var->choices.p_values[i];
567 p_val->p_list->pi_types[i] = p_var->i_type;
568 p_var->ops->pf_dup( &p_val->p_list->p_values[i] );
571 p_val2->p_list->p_values[i].psz_string =
572 p_var->choices_text.p_values[i].psz_string ?
573 strdup(p_var->choices_text.p_values[i].psz_string) : NULL;
574 p_val2->p_list->pi_types[i] = VLC_VAR_STRING;
578 case VLC_VAR_SETTEXT:
579 free( p_var->psz_text );
580 if( p_val && p_val->psz_string )
581 p_var->psz_text = strdup( p_val->psz_string );
583 p_var->psz_text = NULL;
585 case VLC_VAR_GETTEXT:
586 p_val->psz_string = p_var->psz_text ? strdup( p_var->psz_text )
589 case VLC_VAR_SETISCOMMAND:
590 p_var->i_type |= VLC_VAR_ISCOMMAND;
597 vlc_mutex_unlock( &p_priv->var_lock );
604 * Perform a Get and Set on a variable
606 * \param p_this: The object that hold the variable
607 * \param psz_name: the name of the variable
608 * \param i_action: the action to perform
609 * \param p_val: The action parameter
610 * \return vlc error codes
612 int var_GetAndSet( vlc_object_t *p_this, const char *psz_name, int i_action,
622 vlc_object_internals_t *p_priv = vlc_internals( p_this );
624 vlc_mutex_lock( &p_priv->var_lock );
625 p_var = Lookup( p_this, psz_name );
628 vlc_mutex_unlock( &p_priv->var_lock );
632 WaitUnused( p_this, p_var );
634 /* Duplicated data if needed */
635 //p_var->ops->pf_dup( &val );
637 /* Backup needed stuff */
640 /* depending of the action requiered */
643 case VLC_VAR_BOOL_TOGGLE:
644 assert( ( p_var->i_type & VLC_VAR_BOOL ) == VLC_VAR_BOOL );
645 p_var->val.b_bool = !p_var->val.b_bool;
647 case VLC_VAR_INTEGER_ADD:
648 assert( ( p_var->i_type & VLC_VAR_INTEGER ) == VLC_VAR_INTEGER );
649 p_var->val.i_int += p_val->i_int;
651 case VLC_VAR_INTEGER_OR:
652 assert( ( p_var->i_type & VLC_VAR_INTEGER ) == VLC_VAR_INTEGER );
653 p_var->val.i_int |= p_val->i_int;
655 case VLC_VAR_INTEGER_NAND:
656 assert( ( p_var->i_type & VLC_VAR_INTEGER ) == VLC_VAR_INTEGER );
657 p_var->val.i_int &= ~p_val->i_int;
660 vlc_mutex_unlock( &p_priv->var_lock );
664 /* Check boundaries */
665 CheckValue( p_var, &p_var->val );
668 /* Deal with callbacks.*/
669 i_ret = TriggerCallback( p_this, p_var, psz_name, oldval );
671 vlc_mutex_unlock( &p_priv->var_lock );
678 * Request a variable's type
680 * \return The variable type if it exists, or 0 if the
681 * variable could not be found.
684 int var_Type( vlc_object_t *p_this, const char *psz_name )
691 vlc_object_internals_t *p_priv = vlc_internals( p_this );
693 vlc_mutex_lock( &p_priv->var_lock );
695 p_var = Lookup( p_this, psz_name );
697 i_type = p_var->i_type;
699 vlc_mutex_unlock( &p_priv->var_lock );
704 #undef var_SetChecked
705 int var_SetChecked( vlc_object_t *p_this, const char *psz_name,
706 int expected_type, vlc_value_t val )
708 int i_ret = VLC_SUCCESS;
714 vlc_object_internals_t *p_priv = vlc_internals( p_this );
716 vlc_mutex_lock( &p_priv->var_lock );
718 p_var = Lookup( p_this, psz_name );
721 vlc_mutex_unlock( &p_priv->var_lock );
725 assert( expected_type == 0 ||
726 (p_var->i_type & VLC_VAR_CLASS) == expected_type );
728 /* Alert if the type is VLC_VAR_VOID */
729 if( ( p_var->i_type & VLC_VAR_TYPE ) == VLC_VAR_VOID )
730 msg_Warn( p_this, "Calling var_Set on the void variable '%s' (0x%04x)", psz_name, p_var->i_type );
734 WaitUnused( p_this, p_var );
736 /* Duplicate data if needed */
737 p_var->ops->pf_dup( &val );
739 /* Backup needed stuff */
742 /* Check boundaries and list */
743 CheckValue( p_var, &val );
745 /* Set the variable */
748 /* Deal with callbacks */
749 i_ret = TriggerCallback( p_this, p_var, psz_name, oldval );
751 /* Free data if needed */
752 p_var->ops->pf_free( &oldval );
754 vlc_mutex_unlock( &p_priv->var_lock );
761 * Set a variable's value
763 * \param p_this The object that hold the variable
764 * \param psz_name The name of the variable
765 * \param val the value to set
767 int var_Set( vlc_object_t *p_this, const char *psz_name, vlc_value_t val )
769 return var_SetChecked( p_this, psz_name, 0, val );
772 #undef var_GetChecked
773 int var_GetChecked( vlc_object_t *p_this, const char *psz_name,
774 int expected_type, vlc_value_t *p_val )
778 vlc_object_internals_t *p_priv = vlc_internals( p_this );
780 int err = VLC_SUCCESS;
782 vlc_mutex_lock( &p_priv->var_lock );
784 p_var = Lookup( p_this, psz_name );
787 assert( expected_type == 0 ||
788 (p_var->i_type & VLC_VAR_CLASS) == expected_type );
790 /* Really get the variable */
794 /* Alert if the type is VLC_VAR_VOID */
795 if( ( p_var->i_type & VLC_VAR_TYPE ) == VLC_VAR_VOID )
796 msg_Warn( p_this, "Calling var_Get on the void variable '%s' (0x%04x)", psz_name, p_var->i_type );
799 /* Duplicate value if needed */
800 p_var->ops->pf_dup( p_val );
805 vlc_mutex_unlock( &p_priv->var_lock );
811 * Get a variable's value
813 * \param p_this The object that holds the variable
814 * \param psz_name The name of the variable
815 * \param p_val Pointer to a vlc_value_t that will hold the variable's value
816 * after the function is finished
818 int var_Get( vlc_object_t *p_this, const char *psz_name, vlc_value_t *p_val )
820 return var_GetChecked( p_this, psz_name, 0, p_val );
823 #undef var_AddCallback
825 * Register a callback in a variable
827 * We store a function pointer that will be called upon variable
830 * \param p_this The object that holds the variable
831 * \param psz_name The name of the variable
832 * \param pf_callback The function pointer
833 * \param p_data A generic pointer that will be passed as the last
834 * argument to the callback function.
836 * \warning The callback function is run in the thread that calls var_Set on
837 * the variable. Use proper locking. This thread may not have much
838 * time to spare, so keep callback functions short.
840 int var_AddCallback( vlc_object_t *p_this, const char *psz_name,
841 vlc_callback_t pf_callback, void *p_data )
844 callback_entry_t entry;
848 vlc_object_internals_t *p_priv = vlc_internals( p_this );
850 entry.pf_callback = pf_callback;
851 entry.p_data = p_data;
853 vlc_mutex_lock( &p_priv->var_lock );
855 p_var = Lookup( p_this, psz_name );
859 msg_Warn( p_this, "Failed to add a callback to the non-existing "
860 "variable '%s'", psz_name );
862 vlc_mutex_unlock( &p_priv->var_lock );
866 WaitUnused( p_this, p_var );
867 INSERT_ELEM( p_var->p_entries,
872 vlc_mutex_unlock( &p_priv->var_lock );
877 #undef var_DelCallback
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 )
890 bool b_found_similar = false;
895 vlc_object_internals_t *p_priv = vlc_internals( p_this );
897 vlc_mutex_lock( &p_priv->var_lock );
899 p_var = Lookup( p_this, psz_name );
902 vlc_mutex_unlock( &p_priv->var_lock );
906 WaitUnused( p_this, p_var );
908 for( i_entry = p_var->i_entries ; i_entry-- ; )
910 if( p_var->p_entries[i_entry].pf_callback == pf_callback
911 && p_var->p_entries[i_entry].p_data == p_data )
916 else if( p_var->p_entries[i_entry].pf_callback == pf_callback )
917 b_found_similar = true;
924 if( b_found_similar )
925 fprintf( stderr, "Calling var_DelCallback for '%s' with the same "
926 "function but not the same data.", psz_name );
929 vlc_mutex_unlock( &p_priv->var_lock );
933 REMOVE_ELEM( p_var->p_entries, p_var->i_entries, i_entry );
935 vlc_mutex_unlock( &p_priv->var_lock );
940 #undef var_TriggerCallback
942 * Trigger callback on a variable
944 * \param p_this The object that hold the variable
945 * \param psz_name The name of the variable
947 int var_TriggerCallback( vlc_object_t *p_this, const char *psz_name )
954 vlc_object_internals_t *p_priv = vlc_internals( p_this );
956 vlc_mutex_lock( &p_priv->var_lock );
958 p_var = Lookup( p_this, psz_name );
961 vlc_mutex_unlock( &p_priv->var_lock );
965 WaitUnused( p_this, p_var );
967 /* Deal with callbacks. Tell we're in a callback, release the lock,
968 * call stored functions, retake the lock. */
969 i_ret = TriggerCallback( p_this, p_var, psz_name, p_var->val );
971 vlc_mutex_unlock( &p_priv->var_lock );
975 /** Parse a stringified option
976 * This function parse a string option and create the associated object
978 * The option must be of the form "[no[-]]foo[=bar]" where foo is the
979 * option name and bar is the value of the option.
980 * \param p_obj the object in which the variable must be created
981 * \param psz_option the option to parse
982 * \param trusted whether the option is set by a trusted input or not
985 void var_OptionParse( vlc_object_t *p_obj, const char *psz_option,
988 char *psz_name, *psz_value;
993 val.psz_string = NULL;
995 /* It's too much of a hassle to remove the ':' when we parse
997 if( psz_option[0] == ':' )
1000 if( !psz_option[0] )
1003 psz_name = strdup( psz_option );
1004 if( psz_name == NULL )
1007 psz_value = strchr( psz_name, '=' );
1008 if( psz_value != NULL )
1009 *psz_value++ = '\0';
1011 i_type = config_GetType( p_obj, psz_name );
1012 if( !i_type && !psz_value )
1014 /* check for "no-foo" or "nofoo" */
1015 if( !strncmp( psz_name, "no-", 3 ) )
1017 memmove( psz_name, psz_name + 3, strlen(psz_name) + 1 - 3 );
1019 else if( !strncmp( psz_name, "no", 2 ) )
1021 memmove( psz_name, psz_name + 2, strlen(psz_name) + 1 - 2 );
1023 else goto cleanup; /* Option doesn't exist */
1026 i_type = config_GetType( p_obj, psz_name );
1028 if( !i_type ) goto cleanup; /* Option doesn't exist */
1030 if( ( i_type != VLC_VAR_BOOL ) &&
1031 ( !psz_value || !*psz_value ) ) goto cleanup; /* Invalid value */
1033 /* check if option is unsafe */
1036 module_config_t *p_config = config_FindConfig( p_obj, psz_name );
1037 if( !p_config || !p_config->b_safe )
1039 msg_Err( p_obj, "unsafe option \"%s\" has been ignored for "
1040 "security reasons", psz_name );
1046 /* Create the variable in the input object.
1047 * Children of the input object will be able to retreive this value
1048 * thanks to the inheritance property of the object variables. */
1049 var_Create( p_obj, psz_name, i_type );
1054 val.b_bool = !b_isno;
1057 case VLC_VAR_INTEGER:
1058 val.i_int = strtoll( psz_value, NULL, 0 );
1062 val.f_float = us_atof( psz_value );
1065 case VLC_VAR_STRING:
1066 val.psz_string = psz_value;
1073 var_Set( p_obj, psz_name, val );
1080 /* Following functions are local */
1083 * Waits until the variable is inactive (i.e. not executing a callback)
1085 static void WaitUnused( vlc_object_t *p_this, variable_t *p_var )
1087 vlc_object_internals_t *p_priv = vlc_internals( p_this );
1089 mutex_cleanup_push( &p_priv->var_lock );
1090 while( p_var->b_incallback )
1091 vlc_cond_wait( &p_priv->var_wait, &p_priv->var_lock );
1095 /*****************************************************************************
1096 * CheckValue: check that a value is valid wrt. a variable
1097 *****************************************************************************
1098 * This function checks p_val's value against p_var's limitations such as
1099 * minimal and maximal value, step, in-list position, and modifies p_val if
1101 ****************************************************************************/
1102 static void CheckValue ( variable_t *p_var, vlc_value_t *p_val )
1104 /* Check that our variable is in the list */
1105 if( p_var->i_type & VLC_VAR_HASCHOICE && p_var->choices.i_count )
1109 /* This list is not sorted so go throug it (this is a small list) */
1110 for( i = p_var->choices.i_count ; i-- ; )
1112 if( p_var->ops->pf_cmp( *p_val, p_var->choices.p_values[i] ) == 0 )
1118 /* If not found, change it to anything vaguely valid */
1121 /* Free the old variable, get the new one, dup it */
1122 p_var->ops->pf_free( p_val );
1123 *p_val = p_var->choices.p_values[p_var->i_default >= 0
1124 ? p_var->i_default : 0 ];
1125 p_var->ops->pf_dup( p_val );
1129 /* Check that our variable is within the bounds */
1130 switch( p_var->i_type & VLC_VAR_TYPE )
1132 case VLC_VAR_INTEGER:
1133 if( p_var->i_type & VLC_VAR_HASSTEP && p_var->step.i_int
1134 && (p_val->i_int % p_var->step.i_int) )
1136 p_val->i_int = (p_val->i_int + (p_var->step.i_int / 2))
1137 / p_var->step.i_int * p_var->step.i_int;
1139 if( p_var->i_type & VLC_VAR_HASMIN
1140 && p_val->i_int < p_var->min.i_int )
1142 p_val->i_int = p_var->min.i_int;
1144 if( p_var->i_type & VLC_VAR_HASMAX
1145 && p_val->i_int > p_var->max.i_int )
1147 p_val->i_int = p_var->max.i_int;
1151 if( p_var->i_type & VLC_VAR_HASSTEP && p_var->step.f_float )
1153 float f_round = p_var->step.f_float * (float)(int)( 0.5 +
1154 p_val->f_float / p_var->step.f_float );
1155 if( p_val->f_float != f_round )
1157 p_val->f_float = f_round;
1160 if( p_var->i_type & VLC_VAR_HASMIN
1161 && p_val->f_float < p_var->min.f_float )
1163 p_val->f_float = p_var->min.f_float;
1165 if( p_var->i_type & VLC_VAR_HASMAX
1166 && p_val->f_float > p_var->max.f_float )
1168 p_val->f_float = p_var->max.f_float;
1178 * Finds the value of a variable. If the specified object does not hold a
1179 * variable with the specified name, try the parent object, and iterate until
1180 * the top of the tree. If no match is found, the value is read from the
1183 int var_Inherit( vlc_object_t *p_this, const char *psz_name, int i_type,
1184 vlc_value_t *p_val )
1187 if (p_this != VLC_OBJECT(p_this->p_libvlc)
1188 && unlikely(p_this->p_parent == NULL))
1190 msg_Info (p_this, "%s(%s) on detached object", __func__, psz_name);
1194 i_type &= VLC_VAR_CLASS;
1195 for( vlc_object_t *obj = p_this; obj != NULL; obj = obj->p_parent )
1197 if( var_GetChecked( obj, psz_name, i_type, p_val ) == VLC_SUCCESS )
1200 if (obj != p_this && obj != VLC_OBJECT(p_this->p_libvlc)
1201 && unlikely(obj->p_parent == NULL))
1203 msg_Info (p_this, "%s(%s) on detached tree [%p] %s", __func__,
1204 psz_name, obj, obj->psz_object_type);
1210 /* else take value from config */
1211 switch( i_type & VLC_VAR_CLASS )
1213 case VLC_VAR_STRING:
1214 p_val->psz_string = config_GetPsz( p_this, psz_name );
1215 if( !p_val->psz_string ) p_val->psz_string = strdup("");
1218 p_val->f_float = config_GetFloat( p_this, psz_name );
1220 case VLC_VAR_INTEGER:
1221 p_val->i_int = config_GetInt( p_this, psz_name );
1224 p_val->b_bool = config_GetInt( p_this, psz_name );
1226 case VLC_VAR_ADDRESS:
1229 msg_Warn( p_this, "Could not inherit value for var %s "
1230 "from config. Invalid Type", psz_name );
1233 /*msg_Dbg( p_this, "Inherited value for var %s from config", psz_name );*/
1239 * It inherits a string as an unsigned rational number (it also accepts basic
1242 * It returns an error if the rational number cannot be parsed (0/0 is valid).
1243 * The rational is already reduced.
1245 int (var_InheritURational)(vlc_object_t *object,
1246 unsigned *num, unsigned *den,
1254 char *tmp = var_InheritString(object, var);
1259 unsigned n = strtol(tmp, &next, 0);
1260 unsigned d = strtol(*next ? &next[1] : "0", NULL, 0);
1263 /* Interpret as a float number */
1264 double r = us_atof(tmp);
1279 vlc_ureduce(num, den, n, d, 0);
1286 return VLC_EGENERIC;
1289 /**********************************************************************
1290 * Trigger the callbacks.
1291 * Tell we're in a callback, release the lock, call stored functions,
1293 **********************************************************************/
1294 static int TriggerCallback( vlc_object_t *p_this, variable_t *p_var,
1295 const char *psz_name, vlc_value_t oldval )
1299 int i_entries = p_var->i_entries;
1300 if( i_entries == 0 )
1303 callback_entry_t *p_entries = p_var->p_entries;
1304 vlc_object_internals_t *p_priv = vlc_internals( p_this );
1306 assert( !p_var->b_incallback );
1307 p_var->b_incallback = true;
1308 vlc_mutex_unlock( &p_priv->var_lock );
1310 /* The real calls */
1311 for( ; i_entries-- ; )
1313 p_entries[i_entries].pf_callback( p_this, psz_name, oldval, p_var->val,
1314 p_entries[i_entries].p_data );
1317 vlc_mutex_lock( &p_priv->var_lock );
1318 p_var->b_incallback = false;
1319 vlc_cond_broadcast( &p_priv->var_wait );
1325 /**********************************************************************
1326 * Execute a var command on an object identified by its name
1327 **********************************************************************/
1328 int var_Command( vlc_object_t *p_this, const char *psz_name,
1329 const char *psz_cmd, const char *psz_arg, char **psz_msg )
1331 vlc_object_t *p_obj = vlc_object_find_name( p_this->p_libvlc,
1332 psz_name, FIND_CHILD );
1338 *psz_msg = strdup( "Unknown destination object." );
1342 i_type = var_Type( p_obj, psz_cmd );
1343 if( !( i_type&VLC_VAR_ISCOMMAND ) )
1345 vlc_object_release( p_obj );
1347 *psz_msg = strdup( "Variable doesn't exist or isn't a command." );
1348 return VLC_EGENERIC;
1351 i_type &= VLC_VAR_CLASS;
1354 case VLC_VAR_INTEGER:
1355 i_ret = var_SetInteger( p_obj, psz_cmd, atoi( psz_arg ) );
1358 i_ret = var_SetFloat( p_obj, psz_cmd, us_atof( psz_arg ) );
1360 case VLC_VAR_STRING:
1361 i_ret = var_SetString( p_obj, psz_cmd, psz_arg );
1364 i_ret = var_SetBool( p_obj, psz_cmd, atoi( psz_arg ) );
1367 i_ret = VLC_EGENERIC;
1371 vlc_object_release( p_obj );
1375 if( asprintf( psz_msg, "%s on object %s returned %i (%s)",
1376 psz_cmd, psz_name, i_ret, vlc_error( i_ret ) ) == -1)
1385 * Free a list and the associated strings
1386 * @param p_val: the list variable
1387 * @param p_val2: the variable associated or NULL
1389 void var_FreeList( vlc_value_t *p_val, vlc_value_t *p_val2 )
1392 if( p_val2 && p_val2->p_list )
1394 for( int i = 0; i < p_val2->p_list->i_count; i++ )
1395 free( p_val2->p_list->p_values[i].psz_string );
1396 if( p_val2->p_list->i_count )
1398 free( p_val2->p_list->p_values );
1399 free( p_val2->p_list->pi_types );
1401 free( p_val2->p_list );