1 /*****************************************************************************
2 * variables.c: routines for object variables handling
3 *****************************************************************************
4 * Copyright (C) 2002-2009 VLC authors and VideoLAN
7 * Authors: Samuel Hocevar <sam@zoy.org>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * 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"
49 /*****************************************************************************
51 *****************************************************************************/
52 struct callback_entry_t
54 vlc_callback_t pf_callback;
58 /*****************************************************************************
59 * Local comparison functions, returns 0 if v == w, < 0 if v < w, > 0 if v > w
60 *****************************************************************************/
61 static int CmpBool( vlc_value_t v, vlc_value_t w )
63 return v.b_bool ? w.b_bool ? 0 : 1 : w.b_bool ? -1 : 0;
66 static int CmpInt( vlc_value_t v, vlc_value_t w )
68 return v.i_int == w.i_int ? 0 : v.i_int > w.i_int ? 1 : -1;
71 static int CmpTime( vlc_value_t v, vlc_value_t w )
73 return v.i_time == w.i_time ? 0 : v.i_time > w.i_time ? 1 : -1;
76 static int CmpString( vlc_value_t v, vlc_value_t w )
79 return !w.psz_string ? 0 : -1;
81 return !w.psz_string ? 1 : strcmp( v.psz_string, w.psz_string );
83 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; }
84 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; }
86 /*****************************************************************************
87 * Local duplication functions, and local deallocation functions
88 *****************************************************************************/
89 static void DupDummy( vlc_value_t *p_val ) { (void)p_val; /* unused */ }
90 static void DupString( vlc_value_t *p_val )
92 p_val->psz_string = strdup( p_val->psz_string ? p_val->psz_string : "" );
95 static void FreeDummy( vlc_value_t *p_val ) { (void)p_val; /* unused */ }
96 static void FreeString( vlc_value_t *p_val ) { free( p_val->psz_string ); }
98 static void FreeList( vlc_value_t *p_val )
101 for( i = 0; i < p_val->p_list->i_count; i++ )
103 switch( p_val->p_list->pi_types[i] & VLC_VAR_CLASS )
106 FreeString( &p_val->p_list->p_values[i] );
113 if( p_val->p_list->i_count )
115 free( p_val->p_list->p_values );
116 free( p_val->p_list->pi_types );
118 free( p_val->p_list );
121 static const struct variable_ops_t
122 void_ops = { NULL, DupDummy, FreeDummy, },
123 addr_ops = { CmpAddress, DupDummy, FreeDummy, },
124 bool_ops = { CmpBool, DupDummy, FreeDummy, },
125 float_ops = { CmpFloat, DupDummy, FreeDummy, },
126 int_ops = { CmpInt, DupDummy, FreeDummy, },
127 string_ops = { CmpString, DupString, FreeString, },
128 time_ops = { CmpTime, DupDummy, FreeDummy, },
129 coords_ops = { NULL, DupDummy, FreeDummy, };
131 /*****************************************************************************
133 *****************************************************************************/
134 static void WaitUnused ( vlc_object_t *, variable_t * );
136 static void CheckValue ( variable_t *, vlc_value_t * );
138 static int TriggerCallback( vlc_object_t *, variable_t *, const char *,
141 static int varcmp( const void *a, const void *b )
143 const variable_t *va = a, *vb = b;
145 /* psz_name must be first */
146 assert( va == (const void *)&va->psz_name );
147 return strcmp( va->psz_name, vb->psz_name );
150 static variable_t *Lookup( vlc_object_t *obj, const char *psz_name )
152 vlc_object_internals_t *priv = vlc_internals( obj );
155 vlc_assert_locked( &priv->var_lock );
156 pp_var = tfind( &psz_name, &priv->var_root, varcmp );
157 return (pp_var != NULL) ? *pp_var : NULL;
160 static void Destroy( variable_t *p_var )
162 p_var->ops->pf_free( &p_var->val );
163 if( p_var->choices.i_count )
165 for( int i = 0 ; i < p_var->choices.i_count ; i++ )
167 p_var->ops->pf_free( &p_var->choices.p_values[i] );
168 free( p_var->choices_text.p_values[i].psz_string );
170 free( p_var->choices.p_values );
171 free( p_var->choices_text.p_values );
174 for (int i = 0; i < p_var->i_entries; i++)
176 const char *file = "?", *symbol = "?";
177 const void *addr = p_var->p_entries[i].pf_callback;
181 if (dladdr (addr, &info))
183 if (info.dli_fname) file = info.dli_fname;
184 if (info.dli_sname) symbol = info.dli_sname;
187 fprintf (stderr, "Error: callback on \"%s\" dangling %s(%s)[%p]\n",
188 p_var->psz_name, file, symbol, addr);
192 free( p_var->psz_name );
193 free( p_var->psz_text );
194 free( p_var->p_entries );
200 * Initialize a vlc variable
202 * We hash the given string and insert it into the sorted list. The insertion
203 * may require slow memory copies, but think about what we gain in the log(n)
204 * lookup phase when setting/getting the variable value!
206 * \param p_this The object in which to create the variable
207 * \param psz_name The name of the variable
208 * \param i_type The variables type. Must be one of \ref var_type combined with
209 * zero or more \ref var_flags
211 int var_Create( vlc_object_t *p_this, const char *psz_name, int i_type )
215 variable_t *p_var = calloc( 1, sizeof( *p_var ) );
219 p_var->psz_name = strdup( psz_name );
220 p_var->psz_text = NULL;
222 p_var->i_type = i_type & ~VLC_VAR_DOINHERIT;
226 p_var->i_default = -1;
227 p_var->choices.i_count = 0;
228 p_var->choices.p_values = NULL;
229 p_var->choices_text.i_count = 0;
230 p_var->choices_text.p_values = NULL;
232 p_var->b_incallback = false;
233 p_var->i_entries = 0;
234 p_var->p_entries = NULL;
236 /* Always initialize the variable, even if it is a list variable; this
237 * will lead to errors if the variable is not initialized, but it will
238 * not cause crashes in the variable handling. */
239 switch( i_type & VLC_VAR_CLASS )
242 p_var->ops = &bool_ops;
243 p_var->val.b_bool = false;
245 case VLC_VAR_INTEGER:
246 p_var->ops = &int_ops;
247 p_var->val.i_int = 0;
250 p_var->ops = &string_ops;
251 p_var->val.psz_string = NULL;
254 p_var->ops = &float_ops;
255 p_var->val.f_float = 0.0;
258 p_var->ops = &time_ops;
259 p_var->val.i_time = 0;
262 p_var->ops = &coords_ops;
263 p_var->val.coords.x = p_var->val.coords.y = 0;
265 case VLC_VAR_ADDRESS:
266 p_var->ops = &addr_ops;
267 p_var->val.p_address = NULL;
270 p_var->ops = &void_ops;
272 if( (i_type & VLC_VAR_CLASS) != VLC_VAR_VOID )
273 msg_Err( p_this, "Creating the variable '%s' without a type",
278 if( i_type & VLC_VAR_DOINHERIT )
280 if( var_Inherit( p_this, psz_name, i_type, &p_var->val ) )
281 msg_Err( p_this, "cannot inherit value for %s", psz_name );
282 else if( i_type & VLC_VAR_HASCHOICE )
284 /* We must add the inherited value to our choice list */
285 p_var->i_default = 0;
287 INSERT_ELEM( p_var->choices.p_values, p_var->choices.i_count,
289 INSERT_ELEM( p_var->choices_text.p_values,
290 p_var->choices_text.i_count, 0, p_var->val );
291 p_var->ops->pf_dup( &p_var->choices.p_values[0] );
292 p_var->choices_text.p_values[0].psz_string = NULL;
296 vlc_object_internals_t *p_priv = vlc_internals( p_this );
297 variable_t **pp_var, *p_oldvar;
298 int ret = VLC_SUCCESS;
300 vlc_mutex_lock( &p_priv->var_lock );
302 pp_var = tsearch( p_var, &p_priv->var_root, varcmp );
303 if( unlikely(pp_var == NULL) )
305 else if( (p_oldvar = *pp_var) == p_var )
307 else if( unlikely((i_type ^ p_oldvar->i_type) & VLC_VAR_CLASS) )
308 { /* If the types differ, variable creation failed. */
309 msg_Err( p_this, "Variable '%s' (0x%04x) already exist "
310 "but with a different type (0x%04x)",
311 psz_name, p_oldvar->i_type, i_type );
317 p_oldvar->i_type |= i_type & (VLC_VAR_ISCOMMAND|VLC_VAR_HASCHOICE);
319 vlc_mutex_unlock( &p_priv->var_lock );
321 /* If we did not need to create a new variable, free everything... */
329 * Destroy a vlc variable
331 * Look for the variable and destroy it if it is found. As in var_Create we
332 * do a call to memmove() but we have performance counterparts elsewhere.
334 * \param p_this The object that holds the variable
335 * \param psz_name The name of the variable
337 int var_Destroy( vlc_object_t *p_this, const char *psz_name )
343 vlc_object_internals_t *p_priv = vlc_internals( p_this );
345 vlc_mutex_lock( &p_priv->var_lock );
347 p_var = Lookup( p_this, psz_name );
350 vlc_mutex_unlock( &p_priv->var_lock );
354 WaitUnused( p_this, p_var );
356 if( --p_var->i_usage == 0 )
357 tdelete( p_var, &p_priv->var_root, varcmp );
360 vlc_mutex_unlock( &p_priv->var_lock );
367 static void CleanupVar( void *var )
372 void var_DestroyAll( vlc_object_t *obj )
374 vlc_object_internals_t *priv = vlc_internals( obj );
376 tdestroy( priv->var_root, CleanupVar );
377 priv->var_root = NULL;
382 * Perform an action on a variable
384 * \param p_this The object that holds the variable
385 * \param psz_name The name of the variable
386 * \param i_action The action to perform. Must be one of \ref var_action
387 * \param p_val First action parameter
388 * \param p_val2 Second action parameter
390 int var_Change( vlc_object_t *p_this, const char *psz_name,
391 int i_action, vlc_value_t *p_val, vlc_value_t *p_val2 )
393 int ret = VLC_SUCCESS;
400 vlc_object_internals_t *p_priv = vlc_internals( p_this );
402 vlc_mutex_lock( &p_priv->var_lock );
404 p_var = Lookup( p_this, psz_name );
407 vlc_mutex_unlock( &p_priv->var_lock );
414 if( p_var->i_type & VLC_VAR_HASMIN )
416 p_var->ops->pf_free( &p_var->min );
418 p_var->i_type |= VLC_VAR_HASMIN;
420 p_var->ops->pf_dup( &p_var->min );
421 CheckValue( p_var, &p_var->val );
424 if( p_var->i_type & VLC_VAR_HASMIN )
430 if( p_var->i_type & VLC_VAR_HASMAX )
432 p_var->ops->pf_free( &p_var->max );
434 p_var->i_type |= VLC_VAR_HASMAX;
436 p_var->ops->pf_dup( &p_var->max );
437 CheckValue( p_var, &p_var->val );
440 if( p_var->i_type & VLC_VAR_HASMAX )
445 case VLC_VAR_SETSTEP:
446 if( p_var->i_type & VLC_VAR_HASSTEP )
448 p_var->ops->pf_free( &p_var->step );
450 p_var->i_type |= VLC_VAR_HASSTEP;
451 p_var->step = *p_val;
452 p_var->ops->pf_dup( &p_var->step );
453 CheckValue( p_var, &p_var->val );
455 case VLC_VAR_GETSTEP:
456 if( p_var->i_type & VLC_VAR_HASSTEP )
457 *p_val = p_var->step;
461 case VLC_VAR_ADDCHOICE:
463 int i = p_var->choices.i_count;
465 INSERT_ELEM( p_var->choices.p_values, p_var->choices.i_count,
467 INSERT_ELEM( p_var->choices_text.p_values,
468 p_var->choices_text.i_count, i, *p_val );
469 p_var->ops->pf_dup( &p_var->choices.p_values[i] );
470 p_var->choices_text.p_values[i].psz_string =
471 ( p_val2 && p_val2->psz_string ) ?
472 strdup( p_val2->psz_string ) : NULL;
474 CheckValue( p_var, &p_var->val );
477 case VLC_VAR_DELCHOICE:
481 for( i = 0 ; i < p_var->choices.i_count ; i++ )
482 if( p_var->ops->pf_cmp( p_var->choices.p_values[i], *p_val ) == 0 )
485 if( i == p_var->choices.i_count )
488 vlc_mutex_unlock( &p_priv->var_lock );
492 if( p_var->i_default > i )
494 else if( p_var->i_default == i )
495 p_var->i_default = -1;
497 p_var->ops->pf_free( &p_var->choices.p_values[i] );
498 free( p_var->choices_text.p_values[i].psz_string );
499 REMOVE_ELEM( p_var->choices.p_values, p_var->choices.i_count, i );
500 REMOVE_ELEM( p_var->choices_text.p_values,
501 p_var->choices_text.i_count, i );
503 CheckValue( p_var, &p_var->val );
506 case VLC_VAR_CHOICESCOUNT:
507 p_val->i_int = p_var->choices.i_count;
509 case VLC_VAR_CLEARCHOICES:
510 for( int i = 0 ; i < p_var->choices.i_count ; i++ )
511 p_var->ops->pf_free( &p_var->choices.p_values[i] );
512 for( int i = 0 ; i < p_var->choices_text.i_count ; i++ )
513 free( p_var->choices_text.p_values[i].psz_string );
515 if( p_var->choices.i_count ) free( p_var->choices.p_values );
516 if( p_var->choices_text.i_count ) free( p_var->choices_text.p_values );
518 p_var->choices.i_count = 0;
519 p_var->choices.p_values = NULL;
520 p_var->choices_text.i_count = 0;
521 p_var->choices_text.p_values = NULL;
522 p_var->i_default = -1;
524 case VLC_VAR_SETDEFAULT:
527 /* FIXME: the list is sorted, dude. Use something cleverer. */
528 for( i = 0 ; i < p_var->choices.i_count ; i++ )
529 if( p_var->ops->pf_cmp( p_var->choices.p_values[i], *p_val ) == 0 )
532 if( i == p_var->choices.i_count )
536 p_var->i_default = i;
537 CheckValue( p_var, &p_var->val );
540 case VLC_VAR_SETVALUE:
541 /* Duplicate data if needed */
543 p_var->ops->pf_dup( &newval );
544 /* Backup needed stuff */
546 /* Check boundaries and list */
547 CheckValue( p_var, &newval );
548 /* Set the variable */
550 /* Free data if needed */
551 p_var->ops->pf_free( &oldval );
553 case VLC_VAR_GETCHOICES:
554 case VLC_VAR_GETLIST:
555 p_val->p_list = malloc( sizeof(vlc_list_t) );
556 if( p_val2 ) p_val2->p_list = malloc( sizeof(vlc_list_t) );
557 if( p_var->choices.i_count )
559 p_val->p_list->p_values = malloc( p_var->choices.i_count
560 * sizeof(vlc_value_t) );
561 p_val->p_list->pi_types = malloc( p_var->choices.i_count
565 p_val2->p_list->p_values =
566 malloc( p_var->choices.i_count * sizeof(vlc_value_t) );
567 p_val2->p_list->pi_types =
568 malloc( p_var->choices.i_count * sizeof(int) );
571 p_val->p_list->i_count = p_var->choices.i_count;
572 if( p_val2 ) p_val2->p_list->i_count = p_var->choices.i_count;
573 for( int i = 0 ; i < p_var->choices.i_count ; i++ )
575 p_val->p_list->p_values[i] = p_var->choices.p_values[i];
576 p_val->p_list->pi_types[i] = p_var->i_type;
577 p_var->ops->pf_dup( &p_val->p_list->p_values[i] );
580 p_val2->p_list->p_values[i].psz_string =
581 p_var->choices_text.p_values[i].psz_string ?
582 strdup(p_var->choices_text.p_values[i].psz_string) : NULL;
583 p_val2->p_list->pi_types[i] = VLC_VAR_STRING;
587 case VLC_VAR_SETTEXT:
588 free( p_var->psz_text );
589 if( p_val && p_val->psz_string )
590 p_var->psz_text = strdup( p_val->psz_string );
592 p_var->psz_text = NULL;
594 case VLC_VAR_GETTEXT:
595 p_val->psz_string = p_var->psz_text ? strdup( p_var->psz_text )
598 case VLC_VAR_SETISCOMMAND:
599 p_var->i_type |= VLC_VAR_ISCOMMAND;
606 vlc_mutex_unlock( &p_priv->var_lock );
613 * Perform a Get and Set on a variable
615 * \param p_this: The object that hold the variable
616 * \param psz_name: the name of the variable
617 * \param i_action: the action to perform
618 * \param p_val: The action parameter
619 * \return vlc error codes
621 int var_GetAndSet( vlc_object_t *p_this, const char *psz_name, int i_action,
631 vlc_object_internals_t *p_priv = vlc_internals( p_this );
633 vlc_mutex_lock( &p_priv->var_lock );
634 p_var = Lookup( p_this, psz_name );
637 vlc_mutex_unlock( &p_priv->var_lock );
641 WaitUnused( p_this, p_var );
643 /* Duplicated data if needed */
644 //p_var->ops->pf_dup( &val );
646 /* Backup needed stuff */
649 /* depending of the action requiered */
652 case VLC_VAR_BOOL_TOGGLE:
653 assert( ( p_var->i_type & VLC_VAR_BOOL ) == VLC_VAR_BOOL );
654 p_var->val.b_bool = !p_var->val.b_bool;
656 case VLC_VAR_INTEGER_ADD:
657 assert( ( p_var->i_type & VLC_VAR_INTEGER ) == VLC_VAR_INTEGER );
658 p_var->val.i_int += p_val->i_int;
660 case VLC_VAR_INTEGER_OR:
661 assert( ( p_var->i_type & VLC_VAR_INTEGER ) == VLC_VAR_INTEGER );
662 p_var->val.i_int |= p_val->i_int;
664 case VLC_VAR_INTEGER_NAND:
665 assert( ( p_var->i_type & VLC_VAR_INTEGER ) == VLC_VAR_INTEGER );
666 p_var->val.i_int &= ~p_val->i_int;
669 vlc_mutex_unlock( &p_priv->var_lock );
673 /* Check boundaries */
674 CheckValue( p_var, &p_var->val );
677 /* Deal with callbacks.*/
678 i_ret = TriggerCallback( p_this, p_var, psz_name, oldval );
680 vlc_mutex_unlock( &p_priv->var_lock );
687 * Request a variable's type
689 * \return The variable type if it exists, or 0 if the
690 * variable could not be found.
693 int var_Type( vlc_object_t *p_this, const char *psz_name )
700 vlc_object_internals_t *p_priv = vlc_internals( p_this );
702 vlc_mutex_lock( &p_priv->var_lock );
704 p_var = Lookup( p_this, psz_name );
706 i_type = p_var->i_type;
708 vlc_mutex_unlock( &p_priv->var_lock );
713 #undef var_SetChecked
714 int var_SetChecked( vlc_object_t *p_this, const char *psz_name,
715 int expected_type, vlc_value_t val )
717 int i_ret = VLC_SUCCESS;
723 vlc_object_internals_t *p_priv = vlc_internals( p_this );
725 vlc_mutex_lock( &p_priv->var_lock );
727 p_var = Lookup( p_this, psz_name );
730 vlc_mutex_unlock( &p_priv->var_lock );
734 assert( expected_type == 0 ||
735 (p_var->i_type & VLC_VAR_CLASS) == expected_type );
737 /* Alert if the type is VLC_VAR_VOID */
738 if( ( p_var->i_type & VLC_VAR_TYPE ) == VLC_VAR_VOID )
739 msg_Warn( p_this, "Calling var_Set on the void variable '%s' (0x%04x)", psz_name, p_var->i_type );
743 WaitUnused( p_this, p_var );
745 /* Duplicate data if needed */
746 p_var->ops->pf_dup( &val );
748 /* Backup needed stuff */
751 /* Check boundaries and list */
752 CheckValue( p_var, &val );
754 /* Set the variable */
757 /* Deal with callbacks */
758 i_ret = TriggerCallback( p_this, p_var, psz_name, oldval );
760 /* Free data if needed */
761 p_var->ops->pf_free( &oldval );
763 vlc_mutex_unlock( &p_priv->var_lock );
770 * Set a variable's value
772 * \param p_this The object that hold the variable
773 * \param psz_name The name of the variable
774 * \param val the value to set
776 int var_Set( vlc_object_t *p_this, const char *psz_name, vlc_value_t val )
778 return var_SetChecked( p_this, psz_name, 0, val );
781 #undef var_GetChecked
782 int var_GetChecked( vlc_object_t *p_this, const char *psz_name,
783 int expected_type, vlc_value_t *p_val )
787 vlc_object_internals_t *p_priv = vlc_internals( p_this );
789 int err = VLC_SUCCESS;
791 vlc_mutex_lock( &p_priv->var_lock );
793 p_var = Lookup( p_this, psz_name );
796 assert( expected_type == 0 ||
797 (p_var->i_type & VLC_VAR_CLASS) == expected_type );
799 /* Really get the variable */
803 /* Alert if the type is VLC_VAR_VOID */
804 if( ( p_var->i_type & VLC_VAR_TYPE ) == VLC_VAR_VOID )
805 msg_Warn( p_this, "Calling var_Get on the void variable '%s' (0x%04x)", psz_name, p_var->i_type );
808 /* Duplicate value if needed */
809 p_var->ops->pf_dup( p_val );
814 vlc_mutex_unlock( &p_priv->var_lock );
820 * Get a variable's value
822 * \param p_this The object that holds the variable
823 * \param psz_name The name of the variable
824 * \param p_val Pointer to a vlc_value_t that will hold the variable's value
825 * after the function is finished
827 int var_Get( vlc_object_t *p_this, const char *psz_name, vlc_value_t *p_val )
829 return var_GetChecked( p_this, psz_name, 0, p_val );
832 #undef var_AddCallback
834 * Register a callback in a variable
836 * We store a function pointer that will be called upon variable
839 * \param p_this The object that holds the variable
840 * \param psz_name The name of the variable
841 * \param pf_callback The function pointer
842 * \param p_data A generic pointer that will be passed as the last
843 * argument to the callback function.
845 * \warning The callback function is run in the thread that calls var_Set on
846 * the variable. Use proper locking. This thread may not have much
847 * time to spare, so keep callback functions short.
849 int var_AddCallback( vlc_object_t *p_this, const char *psz_name,
850 vlc_callback_t pf_callback, void *p_data )
853 callback_entry_t entry;
857 vlc_object_internals_t *p_priv = vlc_internals( p_this );
859 entry.pf_callback = pf_callback;
860 entry.p_data = p_data;
862 vlc_mutex_lock( &p_priv->var_lock );
864 p_var = Lookup( p_this, psz_name );
868 msg_Warn( p_this, "Failed to add a callback to the non-existing "
869 "variable '%s'", psz_name );
871 vlc_mutex_unlock( &p_priv->var_lock );
875 WaitUnused( p_this, p_var );
876 INSERT_ELEM( p_var->p_entries,
881 vlc_mutex_unlock( &p_priv->var_lock );
886 #undef var_DelCallback
888 * Remove a callback from a variable
890 * pf_callback and p_data have to be given again, because different objects
891 * might have registered the same callback function.
893 int var_DelCallback( vlc_object_t *p_this, const char *psz_name,
894 vlc_callback_t pf_callback, void *p_data )
899 bool b_found_similar = false;
904 vlc_object_internals_t *p_priv = vlc_internals( p_this );
906 vlc_mutex_lock( &p_priv->var_lock );
908 p_var = Lookup( p_this, psz_name );
911 vlc_mutex_unlock( &p_priv->var_lock );
915 WaitUnused( p_this, p_var );
917 for( i_entry = p_var->i_entries ; i_entry-- ; )
919 if( p_var->p_entries[i_entry].pf_callback == pf_callback
920 && p_var->p_entries[i_entry].p_data == p_data )
925 else if( p_var->p_entries[i_entry].pf_callback == pf_callback )
926 b_found_similar = true;
933 if( b_found_similar )
934 fprintf( stderr, "Calling var_DelCallback for '%s' with the same "
935 "function but not the same data.", psz_name );
938 vlc_mutex_unlock( &p_priv->var_lock );
942 REMOVE_ELEM( p_var->p_entries, p_var->i_entries, i_entry );
944 vlc_mutex_unlock( &p_priv->var_lock );
949 #undef var_TriggerCallback
951 * Trigger callback on a variable
953 * \param p_this The object that hold the variable
954 * \param psz_name The name of the variable
956 int var_TriggerCallback( vlc_object_t *p_this, const char *psz_name )
963 vlc_object_internals_t *p_priv = vlc_internals( p_this );
965 vlc_mutex_lock( &p_priv->var_lock );
967 p_var = Lookup( p_this, psz_name );
970 vlc_mutex_unlock( &p_priv->var_lock );
974 WaitUnused( p_this, p_var );
976 /* Deal with callbacks. Tell we're in a callback, release the lock,
977 * call stored functions, retake the lock. */
978 i_ret = TriggerCallback( p_this, p_var, psz_name, p_var->val );
980 vlc_mutex_unlock( &p_priv->var_lock );
984 /** Parse a stringified option
985 * This function parse a string option and create the associated object
987 * The option must be of the form "[no[-]]foo[=bar]" where foo is the
988 * option name and bar is the value of the option.
989 * \param p_obj the object in which the variable must be created
990 * \param psz_option the option to parse
991 * \param trusted whether the option is set by a trusted input or not
994 void var_OptionParse( vlc_object_t *p_obj, const char *psz_option,
997 char *psz_name, *psz_value;
1002 val.psz_string = NULL;
1004 /* It's too much of a hassle to remove the ':' when we parse
1005 * the cmd line :) */
1006 if( psz_option[0] == ':' )
1009 if( !psz_option[0] )
1012 psz_name = strdup( psz_option );
1013 if( psz_name == NULL )
1016 psz_value = strchr( psz_name, '=' );
1017 if( psz_value != NULL )
1018 *psz_value++ = '\0';
1020 i_type = config_GetType( p_obj, psz_name );
1021 if( !i_type && !psz_value )
1023 /* check for "no-foo" or "nofoo" */
1024 if( !strncmp( psz_name, "no-", 3 ) )
1026 memmove( psz_name, psz_name + 3, strlen(psz_name) + 1 - 3 );
1028 else if( !strncmp( psz_name, "no", 2 ) )
1030 memmove( psz_name, psz_name + 2, strlen(psz_name) + 1 - 2 );
1032 else goto cleanup; /* Option doesn't exist */
1035 i_type = config_GetType( p_obj, psz_name );
1037 if( !i_type ) goto cleanup; /* Option doesn't exist */
1039 if( ( i_type != VLC_VAR_BOOL ) &&
1040 ( !psz_value || !*psz_value ) ) goto cleanup; /* Invalid value */
1042 /* check if option is unsafe */
1043 if( !trusted && !config_IsSafe( psz_name ) )
1045 msg_Err( p_obj, "unsafe option \"%s\" has been ignored for "
1046 "security reasons", psz_name );
1051 /* Create the variable in the input object.
1052 * Children of the input object will be able to retreive this value
1053 * thanks to the inheritance property of the object variables. */
1054 var_Create( p_obj, psz_name, i_type );
1059 val.b_bool = !b_isno;
1062 case VLC_VAR_INTEGER:
1063 val.i_int = strtoll( psz_value, NULL, 0 );
1067 val.f_float = us_atof( psz_value );
1070 case VLC_VAR_STRING:
1071 val.psz_string = psz_value;
1078 var_Set( p_obj, psz_name, val );
1084 #undef var_LocationParse
1086 * Parses a set of colon-separated or semicolon-separated
1087 * <variable name>=<value> pairs.
1088 * Some access (or access_demux) plugins uses this scheme
1089 * in media resource location.
1090 * @note Only trusted/safe variables are allowed. This is intended.
1092 * @warning Only use this for plugins implementing VLC-specific resource
1093 * location schemes. This would not make any sense for standardized ones.
1095 * @param obj VLC object on which to set variables (and emit error messages)
1096 * @param mrl string to parse
1097 * @param pref prefix to prepend to option names in the string
1099 * @return VLC_ENOMEM on error, VLC_SUCCESS on success.
1101 int var_LocationParse (vlc_object_t *obj, const char *mrl, const char *pref)
1103 int ret = VLC_SUCCESS;
1104 size_t preflen = strlen (pref) + 1;
1106 assert(mrl != NULL);
1107 while (*mrl != '\0')
1109 mrl += strspn (mrl, ":;"); /* skip leading colon(s) */
1111 size_t len = strcspn (mrl, ":;");
1112 char *buf = malloc (preflen + len);
1114 if (likely(buf != NULL))
1116 /* NOTE: this does not support the "no-<varname>" bool syntax. */
1117 /* DO NOT use asprintf() here; it won't work! Think again. */
1118 snprintf (buf, preflen + len, "%s%s", pref, mrl);
1119 var_OptionParse (obj, buf, false);
1131 * Waits until the variable is inactive (i.e. not executing a callback)
1133 static void WaitUnused( vlc_object_t *p_this, variable_t *p_var )
1135 vlc_object_internals_t *p_priv = vlc_internals( p_this );
1137 mutex_cleanup_push( &p_priv->var_lock );
1138 while( p_var->b_incallback )
1139 vlc_cond_wait( &p_priv->var_wait, &p_priv->var_lock );
1143 /*****************************************************************************
1144 * CheckValue: check that a value is valid wrt. a variable
1145 *****************************************************************************
1146 * This function checks p_val's value against p_var's limitations such as
1147 * minimal and maximal value, step, in-list position, and modifies p_val if
1149 ****************************************************************************/
1150 static void CheckValue ( variable_t *p_var, vlc_value_t *p_val )
1152 /* Check that our variable is in the list */
1153 if( p_var->i_type & VLC_VAR_HASCHOICE && p_var->choices.i_count )
1157 /* This list is not sorted so go throug it (this is a small list) */
1158 for( i = p_var->choices.i_count ; i-- ; )
1160 if( p_var->ops->pf_cmp( *p_val, p_var->choices.p_values[i] ) == 0 )
1166 /* If not found, change it to anything vaguely valid */
1169 /* Free the old variable, get the new one, dup it */
1170 p_var->ops->pf_free( p_val );
1171 *p_val = p_var->choices.p_values[p_var->i_default >= 0
1172 ? p_var->i_default : 0 ];
1173 p_var->ops->pf_dup( p_val );
1177 /* Check that our variable is within the bounds */
1178 switch( p_var->i_type & VLC_VAR_TYPE )
1180 case VLC_VAR_INTEGER:
1181 if( p_var->i_type & VLC_VAR_HASSTEP && p_var->step.i_int
1182 && (p_val->i_int % p_var->step.i_int) )
1184 p_val->i_int = (p_val->i_int + (p_var->step.i_int / 2))
1185 / p_var->step.i_int * p_var->step.i_int;
1187 if( p_var->i_type & VLC_VAR_HASMIN
1188 && p_val->i_int < p_var->min.i_int )
1190 p_val->i_int = p_var->min.i_int;
1192 if( p_var->i_type & VLC_VAR_HASMAX
1193 && p_val->i_int > p_var->max.i_int )
1195 p_val->i_int = p_var->max.i_int;
1199 if( p_var->i_type & VLC_VAR_HASSTEP && p_var->step.f_float )
1201 float f_round = p_var->step.f_float * (float)(int)( 0.5 +
1202 p_val->f_float / p_var->step.f_float );
1203 if( p_val->f_float != f_round )
1205 p_val->f_float = f_round;
1208 if( p_var->i_type & VLC_VAR_HASMIN
1209 && p_val->f_float < p_var->min.f_float )
1211 p_val->f_float = p_var->min.f_float;
1213 if( p_var->i_type & VLC_VAR_HASMAX
1214 && p_val->f_float > p_var->max.f_float )
1216 p_val->f_float = p_var->max.f_float;
1226 * Finds the value of a variable. If the specified object does not hold a
1227 * variable with the specified name, try the parent object, and iterate until
1228 * the top of the tree. If no match is found, the value is read from the
1231 int var_Inherit( vlc_object_t *p_this, const char *psz_name, int i_type,
1232 vlc_value_t *p_val )
1235 if (p_this != VLC_OBJECT(p_this->p_libvlc)
1236 && unlikely(p_this->p_parent == NULL))
1238 msg_Info (p_this, "%s(%s) on detached object", __func__, psz_name);
1242 i_type &= VLC_VAR_CLASS;
1243 for( vlc_object_t *obj = p_this; obj != NULL; obj = obj->p_parent )
1245 if( var_GetChecked( obj, psz_name, i_type, p_val ) == VLC_SUCCESS )
1248 if (obj != p_this && obj != VLC_OBJECT(p_this->p_libvlc)
1249 && unlikely(obj->p_parent == NULL))
1251 msg_Info (p_this, "%s(%s) on detached tree [%p] %s", __func__,
1252 psz_name, obj, obj->psz_object_type);
1258 /* else take value from config */
1259 switch( i_type & VLC_VAR_CLASS )
1261 case VLC_VAR_STRING:
1262 p_val->psz_string = config_GetPsz( p_this, psz_name );
1263 if( !p_val->psz_string ) p_val->psz_string = strdup("");
1266 p_val->f_float = config_GetFloat( p_this, psz_name );
1268 case VLC_VAR_INTEGER:
1269 p_val->i_int = config_GetInt( p_this, psz_name );
1272 p_val->b_bool = config_GetInt( p_this, psz_name );
1274 case VLC_VAR_ADDRESS:
1277 msg_Warn( p_this, "Could not inherit value for var %s "
1278 "from config. Invalid Type", psz_name );
1281 /*msg_Dbg( p_this, "Inherited value for var %s from config", psz_name );*/
1287 * It inherits a string as an unsigned rational number (it also accepts basic
1290 * It returns an error if the rational number cannot be parsed (0/0 is valid).
1291 * The rational is already reduced.
1293 int (var_InheritURational)(vlc_object_t *object,
1294 unsigned *num, unsigned *den,
1302 char *tmp = var_InheritString(object, var);
1307 unsigned n = strtol(tmp, &next, 0);
1308 unsigned d = strtol(*next ? &next[1] : "0", NULL, 0);
1311 /* Interpret as a float number */
1312 double r = us_atof(tmp);
1327 vlc_ureduce(num, den, n, d, 0);
1334 return VLC_EGENERIC;
1337 /**********************************************************************
1338 * Trigger the callbacks.
1339 * Tell we're in a callback, release the lock, call stored functions,
1341 **********************************************************************/
1342 static int TriggerCallback( vlc_object_t *p_this, variable_t *p_var,
1343 const char *psz_name, vlc_value_t oldval )
1347 int i_entries = p_var->i_entries;
1348 if( i_entries == 0 )
1351 callback_entry_t *p_entries = p_var->p_entries;
1352 vlc_object_internals_t *p_priv = vlc_internals( p_this );
1354 assert( !p_var->b_incallback );
1355 p_var->b_incallback = true;
1356 vlc_mutex_unlock( &p_priv->var_lock );
1358 /* The real calls */
1359 for( ; i_entries-- ; )
1361 p_entries[i_entries].pf_callback( p_this, psz_name, oldval, p_var->val,
1362 p_entries[i_entries].p_data );
1365 vlc_mutex_lock( &p_priv->var_lock );
1366 p_var->b_incallback = false;
1367 vlc_cond_broadcast( &p_priv->var_wait );
1373 /**********************************************************************
1374 * Execute a var command on an object identified by its name
1375 **********************************************************************/
1376 int var_Command( vlc_object_t *p_this, const char *psz_name,
1377 const char *psz_cmd, const char *psz_arg, char **psz_msg )
1379 vlc_object_t *p_obj = vlc_object_find_name( p_this->p_libvlc,
1386 *psz_msg = strdup( "Unknown destination object." );
1390 i_type = var_Type( p_obj, psz_cmd );
1391 if( !( i_type&VLC_VAR_ISCOMMAND ) )
1393 vlc_object_release( p_obj );
1395 *psz_msg = strdup( "Variable doesn't exist or isn't a command." );
1396 return VLC_EGENERIC;
1399 i_type &= VLC_VAR_CLASS;
1402 case VLC_VAR_INTEGER:
1403 i_ret = var_SetInteger( p_obj, psz_cmd, atoi( psz_arg ) );
1406 i_ret = var_SetFloat( p_obj, psz_cmd, us_atof( psz_arg ) );
1408 case VLC_VAR_STRING:
1409 i_ret = var_SetString( p_obj, psz_cmd, psz_arg );
1412 i_ret = var_SetBool( p_obj, psz_cmd, atoi( psz_arg ) );
1415 i_ret = VLC_EGENERIC;
1419 vlc_object_release( p_obj );
1423 if( asprintf( psz_msg, "%s on object %s returned %i (%s)",
1424 psz_cmd, psz_name, i_ret, vlc_error( i_ret ) ) == -1)
1433 * Free a list and the associated strings
1434 * @param p_val: the list variable
1435 * @param p_val2: the variable associated or NULL
1437 void var_FreeList( vlc_value_t *p_val, vlc_value_t *p_val2 )
1440 if( p_val2 && p_val2->p_list )
1442 for( int i = 0; i < p_val2->p_list->i_count; i++ )
1443 free( p_val2->p_list->p_values[i].psz_string );
1444 if( p_val2->p_list->i_count )
1446 free( p_val2->p_list->p_values );
1447 free( p_val2->p_list->pi_types );
1449 free( p_val2->p_list );