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 )
394 int ret = VLC_SUCCESS;
401 vlc_object_internals_t *p_priv = vlc_internals( p_this );
403 vlc_mutex_lock( &p_priv->var_lock );
405 p_var = Lookup( p_this, psz_name );
408 vlc_mutex_unlock( &p_priv->var_lock );
415 if( p_var->i_type & VLC_VAR_HASMIN )
417 p_var->ops->pf_free( &p_var->min );
419 p_var->i_type |= VLC_VAR_HASMIN;
421 p_var->ops->pf_dup( &p_var->min );
422 CheckValue( p_var, &p_var->val );
425 if( p_var->i_type & VLC_VAR_HASMIN )
431 if( p_var->i_type & VLC_VAR_HASMAX )
433 p_var->ops->pf_free( &p_var->max );
435 p_var->i_type |= VLC_VAR_HASMAX;
437 p_var->ops->pf_dup( &p_var->max );
438 CheckValue( p_var, &p_var->val );
441 if( p_var->i_type & VLC_VAR_HASMAX )
446 case VLC_VAR_SETSTEP:
447 if( p_var->i_type & VLC_VAR_HASSTEP )
449 p_var->ops->pf_free( &p_var->step );
451 p_var->i_type |= VLC_VAR_HASSTEP;
452 p_var->step = *p_val;
453 p_var->ops->pf_dup( &p_var->step );
454 CheckValue( p_var, &p_var->val );
456 case VLC_VAR_GETSTEP:
457 if( p_var->i_type & VLC_VAR_HASSTEP )
458 *p_val = p_var->step;
462 case VLC_VAR_ADDCHOICE:
463 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 );
476 case VLC_VAR_DELCHOICE:
477 for( i = 0 ; i < p_var->choices.i_count ; i++ )
479 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 )
496 else if( p_var->i_default == i )
498 p_var->i_default = -1;
501 p_var->ops->pf_free( &p_var->choices.p_values[i] );
502 free( p_var->choices_text.p_values[i].psz_string );
503 REMOVE_ELEM( p_var->choices.p_values, p_var->choices.i_count, i );
504 REMOVE_ELEM( p_var->choices_text.p_values,
505 p_var->choices_text.i_count, i );
507 CheckValue( p_var, &p_var->val );
509 case VLC_VAR_CHOICESCOUNT:
510 p_val->i_int = p_var->choices.i_count;
512 case VLC_VAR_CLEARCHOICES:
513 for( i = 0 ; i < p_var->choices.i_count ; i++ )
515 p_var->ops->pf_free( &p_var->choices.p_values[i] );
517 for( i = 0 ; i < p_var->choices_text.i_count ; i++ )
518 free( p_var->choices_text.p_values[i].psz_string );
520 if( p_var->choices.i_count ) free( p_var->choices.p_values );
521 if( p_var->choices_text.i_count ) free( p_var->choices_text.p_values );
523 p_var->choices.i_count = 0;
524 p_var->choices.p_values = NULL;
525 p_var->choices_text.i_count = 0;
526 p_var->choices_text.p_values = NULL;
527 p_var->i_default = -1;
529 case VLC_VAR_SETDEFAULT:
530 /* FIXME: the list is sorted, dude. Use something cleverer. */
531 for( i = 0 ; i < p_var->choices.i_count ; i++ )
533 if( p_var->ops->pf_cmp( p_var->choices.p_values[i], *p_val ) == 0 )
539 if( i == p_var->choices.i_count )
545 p_var->i_default = i;
546 CheckValue( p_var, &p_var->val );
548 case VLC_VAR_SETVALUE:
549 /* Duplicate data if needed */
551 p_var->ops->pf_dup( &newval );
552 /* Backup needed stuff */
554 /* Check boundaries and list */
555 CheckValue( p_var, &newval );
556 /* Set the variable */
558 /* Free data if needed */
559 p_var->ops->pf_free( &oldval );
561 case VLC_VAR_GETCHOICES:
562 case VLC_VAR_GETLIST:
563 p_val->p_list = malloc( sizeof(vlc_list_t) );
564 if( p_val2 ) p_val2->p_list = malloc( sizeof(vlc_list_t) );
565 if( p_var->choices.i_count )
567 p_val->p_list->p_values = malloc( p_var->choices.i_count
568 * sizeof(vlc_value_t) );
569 p_val->p_list->pi_types = malloc( p_var->choices.i_count
573 p_val2->p_list->p_values =
574 malloc( p_var->choices.i_count * sizeof(vlc_value_t) );
575 p_val2->p_list->pi_types =
576 malloc( p_var->choices.i_count * sizeof(int) );
579 p_val->p_list->i_count = p_var->choices.i_count;
580 if( p_val2 ) p_val2->p_list->i_count = p_var->choices.i_count;
581 for( i = 0 ; i < p_var->choices.i_count ; i++ )
583 p_val->p_list->p_values[i] = p_var->choices.p_values[i];
584 p_val->p_list->pi_types[i] = p_var->i_type;
585 p_var->ops->pf_dup( &p_val->p_list->p_values[i] );
588 p_val2->p_list->p_values[i].psz_string =
589 p_var->choices_text.p_values[i].psz_string ?
590 strdup(p_var->choices_text.p_values[i].psz_string) : NULL;
591 p_val2->p_list->pi_types[i] = VLC_VAR_STRING;
595 case VLC_VAR_SETTEXT:
596 free( p_var->psz_text );
597 if( p_val && p_val->psz_string )
598 p_var->psz_text = strdup( p_val->psz_string );
600 p_var->psz_text = NULL;
602 case VLC_VAR_GETTEXT:
603 p_val->psz_string = p_var->psz_text ? strdup( p_var->psz_text )
606 case VLC_VAR_SETISCOMMAND:
607 p_var->i_type |= VLC_VAR_ISCOMMAND;
614 vlc_mutex_unlock( &p_priv->var_lock );
621 * Perform a Get and Set on a variable
623 * \param p_this: The object that hold the variable
624 * \param psz_name: the name of the variable
625 * \param i_action: the action to perform
626 * \param p_val: The action parameter
627 * \return vlc error codes
629 int var_GetAndSet( vlc_object_t *p_this, const char *psz_name, int i_action,
639 vlc_object_internals_t *p_priv = vlc_internals( p_this );
641 vlc_mutex_lock( &p_priv->var_lock );
642 p_var = Lookup( p_this, psz_name );
645 vlc_mutex_unlock( &p_priv->var_lock );
649 WaitUnused( p_this, p_var );
651 /* Duplicated data if needed */
652 //p_var->ops->pf_dup( &val );
654 /* Backup needed stuff */
657 /* depending of the action requiered */
660 case VLC_VAR_BOOL_TOGGLE:
661 assert( ( p_var->i_type & VLC_VAR_BOOL ) == VLC_VAR_BOOL );
662 p_var->val.b_bool = !p_var->val.b_bool;
664 case VLC_VAR_INTEGER_ADD:
665 assert( ( p_var->i_type & VLC_VAR_INTEGER ) == VLC_VAR_INTEGER );
666 p_var->val.i_int += p_val->i_int;
668 case VLC_VAR_INTEGER_OR:
669 assert( ( p_var->i_type & VLC_VAR_INTEGER ) == VLC_VAR_INTEGER );
670 p_var->val.i_int |= p_val->i_int;
672 case VLC_VAR_INTEGER_NAND:
673 assert( ( p_var->i_type & VLC_VAR_INTEGER ) == VLC_VAR_INTEGER );
674 p_var->val.i_int &= ~p_val->i_int;
677 vlc_mutex_unlock( &p_priv->var_lock );
681 /* Check boundaries */
682 CheckValue( p_var, &p_var->val );
685 /* Deal with callbacks.*/
686 i_ret = TriggerCallback( p_this, p_var, psz_name, oldval );
688 vlc_mutex_unlock( &p_priv->var_lock );
695 * Request a variable's type
697 * \return The variable type if it exists, or 0 if the
698 * variable could not be found.
701 int var_Type( vlc_object_t *p_this, const char *psz_name )
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 );
714 i_type = p_var->i_type;
716 vlc_mutex_unlock( &p_priv->var_lock );
721 #undef var_SetChecked
722 int var_SetChecked( vlc_object_t *p_this, const char *psz_name,
723 int expected_type, vlc_value_t val )
725 int i_ret = VLC_SUCCESS;
731 vlc_object_internals_t *p_priv = vlc_internals( p_this );
733 vlc_mutex_lock( &p_priv->var_lock );
735 p_var = Lookup( p_this, psz_name );
738 vlc_mutex_unlock( &p_priv->var_lock );
742 assert( expected_type == 0 ||
743 (p_var->i_type & VLC_VAR_CLASS) == expected_type );
745 /* Alert if the type is VLC_VAR_VOID */
746 if( ( p_var->i_type & VLC_VAR_TYPE ) == VLC_VAR_VOID )
747 msg_Warn( p_this, "Calling var_Set on the void variable '%s' (0x%04x)", psz_name, p_var->i_type );
751 WaitUnused( p_this, p_var );
753 /* Duplicate data if needed */
754 p_var->ops->pf_dup( &val );
756 /* Backup needed stuff */
759 /* Check boundaries and list */
760 CheckValue( p_var, &val );
762 /* Set the variable */
765 /* Deal with callbacks */
766 i_ret = TriggerCallback( p_this, p_var, psz_name, oldval );
768 /* Free data if needed */
769 p_var->ops->pf_free( &oldval );
771 vlc_mutex_unlock( &p_priv->var_lock );
778 * Set a variable's value
780 * \param p_this The object that hold the variable
781 * \param psz_name The name of the variable
782 * \param val the value to set
784 int var_Set( vlc_object_t *p_this, const char *psz_name, vlc_value_t val )
786 return var_SetChecked( p_this, psz_name, 0, val );
789 #undef var_GetChecked
790 int var_GetChecked( vlc_object_t *p_this, const char *psz_name,
791 int expected_type, vlc_value_t *p_val )
795 vlc_object_internals_t *p_priv = vlc_internals( p_this );
797 int err = VLC_SUCCESS;
799 vlc_mutex_lock( &p_priv->var_lock );
801 p_var = Lookup( p_this, psz_name );
804 assert( expected_type == 0 ||
805 (p_var->i_type & VLC_VAR_CLASS) == expected_type );
807 /* Really get the variable */
811 /* Alert if the type is VLC_VAR_VOID */
812 if( ( p_var->i_type & VLC_VAR_TYPE ) == VLC_VAR_VOID )
813 msg_Warn( p_this, "Calling var_Get on the void variable '%s' (0x%04x)", psz_name, p_var->i_type );
816 /* Duplicate value if needed */
817 p_var->ops->pf_dup( p_val );
822 vlc_mutex_unlock( &p_priv->var_lock );
828 * Get a variable's value
830 * \param p_this The object that holds the variable
831 * \param psz_name The name of the variable
832 * \param p_val Pointer to a vlc_value_t that will hold the variable's value
833 * after the function is finished
835 int var_Get( vlc_object_t *p_this, const char *psz_name, vlc_value_t *p_val )
837 return var_GetChecked( p_this, psz_name, 0, p_val );
840 #undef var_AddCallback
842 * Register a callback in a variable
844 * We store a function pointer that will be called upon variable
847 * \param p_this The object that holds the variable
848 * \param psz_name The name of the variable
849 * \param pf_callback The function pointer
850 * \param p_data A generic pointer that will be passed as the last
851 * argument to the callback function.
853 * \warning The callback function is run in the thread that calls var_Set on
854 * the variable. Use proper locking. This thread may not have much
855 * time to spare, so keep callback functions short.
857 int var_AddCallback( vlc_object_t *p_this, const char *psz_name,
858 vlc_callback_t pf_callback, void *p_data )
861 callback_entry_t entry;
865 vlc_object_internals_t *p_priv = vlc_internals( p_this );
867 entry.pf_callback = pf_callback;
868 entry.p_data = p_data;
870 vlc_mutex_lock( &p_priv->var_lock );
872 p_var = Lookup( p_this, psz_name );
876 msg_Warn( p_this, "Failed to add a callback to the non-existing "
877 "variable '%s'", psz_name );
879 vlc_mutex_unlock( &p_priv->var_lock );
883 WaitUnused( p_this, p_var );
884 INSERT_ELEM( p_var->p_entries,
889 vlc_mutex_unlock( &p_priv->var_lock );
894 #undef var_DelCallback
896 * Remove a callback from a variable
898 * pf_callback and p_data have to be given again, because different objects
899 * might have registered the same callback function.
901 int var_DelCallback( vlc_object_t *p_this, const char *psz_name,
902 vlc_callback_t pf_callback, void *p_data )
907 bool b_found_similar = false;
912 vlc_object_internals_t *p_priv = vlc_internals( p_this );
914 vlc_mutex_lock( &p_priv->var_lock );
916 p_var = Lookup( p_this, psz_name );
919 vlc_mutex_unlock( &p_priv->var_lock );
923 WaitUnused( p_this, p_var );
925 for( i_entry = p_var->i_entries ; i_entry-- ; )
927 if( p_var->p_entries[i_entry].pf_callback == pf_callback
928 && p_var->p_entries[i_entry].p_data == p_data )
933 else if( p_var->p_entries[i_entry].pf_callback == pf_callback )
934 b_found_similar = true;
941 if( b_found_similar )
942 fprintf( stderr, "Calling var_DelCallback for '%s' with the same "
943 "function but not the same data.", psz_name );
946 vlc_mutex_unlock( &p_priv->var_lock );
950 REMOVE_ELEM( p_var->p_entries, p_var->i_entries, i_entry );
952 vlc_mutex_unlock( &p_priv->var_lock );
957 #undef var_TriggerCallback
959 * Trigger callback on a variable
961 * \param p_this The object that hold the variable
962 * \param psz_name The name of the variable
964 int var_TriggerCallback( vlc_object_t *p_this, const char *psz_name )
971 vlc_object_internals_t *p_priv = vlc_internals( p_this );
973 vlc_mutex_lock( &p_priv->var_lock );
975 p_var = Lookup( p_this, psz_name );
978 vlc_mutex_unlock( &p_priv->var_lock );
982 WaitUnused( p_this, p_var );
984 /* Deal with callbacks. Tell we're in a callback, release the lock,
985 * call stored functions, retake the lock. */
986 i_ret = TriggerCallback( p_this, p_var, psz_name, p_var->val );
988 vlc_mutex_unlock( &p_priv->var_lock );
992 /** Parse a stringified option
993 * This function parse a string option and create the associated object
995 * The option must be of the form "[no[-]]foo[=bar]" where foo is the
996 * option name and bar is the value of the option.
997 * \param p_obj the object in which the variable must be created
998 * \param psz_option the option to parse
999 * \param trusted whether the option is set by a trusted input or not
1002 void var_OptionParse( vlc_object_t *p_obj, const char *psz_option,
1005 char *psz_name, *psz_value;
1007 bool b_isno = false;
1010 val.psz_string = NULL;
1012 /* It's too much of a hassle to remove the ':' when we parse
1013 * the cmd line :) */
1014 if( psz_option[0] == ':' )
1017 if( !psz_option[0] )
1020 psz_name = strdup( psz_option );
1021 if( psz_name == NULL )
1024 psz_value = strchr( psz_name, '=' );
1025 if( psz_value != NULL )
1026 *psz_value++ = '\0';
1028 i_type = config_GetType( p_obj, psz_name );
1029 if( !i_type && !psz_value )
1031 /* check for "no-foo" or "nofoo" */
1032 if( !strncmp( psz_name, "no-", 3 ) )
1034 memmove( psz_name, psz_name + 3, strlen(psz_name) + 1 - 3 );
1036 else if( !strncmp( psz_name, "no", 2 ) )
1038 memmove( psz_name, psz_name + 2, strlen(psz_name) + 1 - 2 );
1040 else goto cleanup; /* Option doesn't exist */
1043 i_type = config_GetType( p_obj, psz_name );
1045 if( !i_type ) goto cleanup; /* Option doesn't exist */
1047 if( ( i_type != VLC_VAR_BOOL ) &&
1048 ( !psz_value || !*psz_value ) ) goto cleanup; /* Invalid value */
1050 /* check if option is unsafe */
1051 if( !trusted && !config_IsSafe( psz_name ) )
1053 msg_Err( p_obj, "unsafe option \"%s\" has been ignored for "
1054 "security reasons", psz_name );
1059 /* Create the variable in the input object.
1060 * Children of the input object will be able to retreive this value
1061 * thanks to the inheritance property of the object variables. */
1062 var_Create( p_obj, psz_name, i_type );
1067 val.b_bool = !b_isno;
1070 case VLC_VAR_INTEGER:
1071 val.i_int = strtoll( psz_value, NULL, 0 );
1075 val.f_float = us_atof( psz_value );
1078 case VLC_VAR_STRING:
1079 val.psz_string = psz_value;
1086 var_Set( p_obj, psz_name, val );
1092 #undef var_LocationParse
1094 * Parses a set of colon-separated <variable name>=<value> pairs. Some access
1095 * (or access_demux) plugins uses this scheme in media resource location.
1096 * @note Only trusted/safe variables are allowed. This is intended.
1098 * @warning Only use this for plugins implementing VLC-specific resource
1099 * location schemes. This would not make any sense for standardized ones.
1101 * @param obj VLC object on which to set variables (and emit error messages)
1102 * @param mrl string to parse
1103 * @param pref prefix to prepend to option names in the string
1105 * @return VLC_ENOMEM on error, VLC_SUCCESS on success.
1107 int var_LocationParse (vlc_object_t *obj, const char *mrl, const char *pref)
1109 int ret = VLC_SUCCESS;
1110 size_t preflen = strlen (pref) + 1;
1112 assert(mrl != NULL);
1113 while (*mrl != '\0')
1115 mrl += strspn (mrl, ":"); /* skip leading colon(s) */
1117 size_t len = strcspn (mrl, ":");
1118 char *buf = malloc (preflen + len);
1120 if (likely(buf != NULL))
1122 /* NOTE: this does not support the "no-<varname>" bool syntax. */
1123 /* DO NOT use asprintf() here; it won't work! Think again. */
1124 snprintf (buf, preflen + len, "%s%s", pref, mrl);
1125 var_OptionParse (obj, buf, false);
1137 * Waits until the variable is inactive (i.e. not executing a callback)
1139 static void WaitUnused( vlc_object_t *p_this, variable_t *p_var )
1141 vlc_object_internals_t *p_priv = vlc_internals( p_this );
1143 mutex_cleanup_push( &p_priv->var_lock );
1144 while( p_var->b_incallback )
1145 vlc_cond_wait( &p_priv->var_wait, &p_priv->var_lock );
1149 /*****************************************************************************
1150 * CheckValue: check that a value is valid wrt. a variable
1151 *****************************************************************************
1152 * This function checks p_val's value against p_var's limitations such as
1153 * minimal and maximal value, step, in-list position, and modifies p_val if
1155 ****************************************************************************/
1156 static void CheckValue ( variable_t *p_var, vlc_value_t *p_val )
1158 /* Check that our variable is in the list */
1159 if( p_var->i_type & VLC_VAR_HASCHOICE && p_var->choices.i_count )
1163 /* This list is not sorted so go throug it (this is a small list) */
1164 for( i = p_var->choices.i_count ; i-- ; )
1166 if( p_var->ops->pf_cmp( *p_val, p_var->choices.p_values[i] ) == 0 )
1172 /* If not found, change it to anything vaguely valid */
1175 /* Free the old variable, get the new one, dup it */
1176 p_var->ops->pf_free( p_val );
1177 *p_val = p_var->choices.p_values[p_var->i_default >= 0
1178 ? p_var->i_default : 0 ];
1179 p_var->ops->pf_dup( p_val );
1183 /* Check that our variable is within the bounds */
1184 switch( p_var->i_type & VLC_VAR_TYPE )
1186 case VLC_VAR_INTEGER:
1187 if( p_var->i_type & VLC_VAR_HASSTEP && p_var->step.i_int
1188 && (p_val->i_int % p_var->step.i_int) )
1190 p_val->i_int = (p_val->i_int + (p_var->step.i_int / 2))
1191 / p_var->step.i_int * p_var->step.i_int;
1193 if( p_var->i_type & VLC_VAR_HASMIN
1194 && p_val->i_int < p_var->min.i_int )
1196 p_val->i_int = p_var->min.i_int;
1198 if( p_var->i_type & VLC_VAR_HASMAX
1199 && p_val->i_int > p_var->max.i_int )
1201 p_val->i_int = p_var->max.i_int;
1205 if( p_var->i_type & VLC_VAR_HASSTEP && p_var->step.f_float )
1207 float f_round = p_var->step.f_float * (float)(int)( 0.5 +
1208 p_val->f_float / p_var->step.f_float );
1209 if( p_val->f_float != f_round )
1211 p_val->f_float = f_round;
1214 if( p_var->i_type & VLC_VAR_HASMIN
1215 && p_val->f_float < p_var->min.f_float )
1217 p_val->f_float = p_var->min.f_float;
1219 if( p_var->i_type & VLC_VAR_HASMAX
1220 && p_val->f_float > p_var->max.f_float )
1222 p_val->f_float = p_var->max.f_float;
1232 * Finds the value of a variable. If the specified object does not hold a
1233 * variable with the specified name, try the parent object, and iterate until
1234 * the top of the tree. If no match is found, the value is read from the
1237 int var_Inherit( vlc_object_t *p_this, const char *psz_name, int i_type,
1238 vlc_value_t *p_val )
1241 if (p_this != VLC_OBJECT(p_this->p_libvlc)
1242 && unlikely(p_this->p_parent == NULL))
1244 msg_Info (p_this, "%s(%s) on detached object", __func__, psz_name);
1248 i_type &= VLC_VAR_CLASS;
1249 for( vlc_object_t *obj = p_this; obj != NULL; obj = obj->p_parent )
1251 if( var_GetChecked( obj, psz_name, i_type, p_val ) == VLC_SUCCESS )
1254 if (obj != p_this && obj != VLC_OBJECT(p_this->p_libvlc)
1255 && unlikely(obj->p_parent == NULL))
1257 msg_Info (p_this, "%s(%s) on detached tree [%p] %s", __func__,
1258 psz_name, obj, obj->psz_object_type);
1264 /* else take value from config */
1265 switch( i_type & VLC_VAR_CLASS )
1267 case VLC_VAR_STRING:
1268 p_val->psz_string = config_GetPsz( p_this, psz_name );
1269 if( !p_val->psz_string ) p_val->psz_string = strdup("");
1272 p_val->f_float = config_GetFloat( p_this, psz_name );
1274 case VLC_VAR_INTEGER:
1275 p_val->i_int = config_GetInt( p_this, psz_name );
1278 p_val->b_bool = config_GetInt( p_this, psz_name );
1280 case VLC_VAR_ADDRESS:
1283 msg_Warn( p_this, "Could not inherit value for var %s "
1284 "from config. Invalid Type", psz_name );
1287 /*msg_Dbg( p_this, "Inherited value for var %s from config", psz_name );*/
1293 * It inherits a string as an unsigned rational number (it also accepts basic
1296 * It returns an error if the rational number cannot be parsed (0/0 is valid).
1297 * The rational is already reduced.
1299 int (var_InheritURational)(vlc_object_t *object,
1300 unsigned *num, unsigned *den,
1308 char *tmp = var_InheritString(object, var);
1313 unsigned n = strtol(tmp, &next, 0);
1314 unsigned d = strtol(*next ? &next[1] : "0", NULL, 0);
1317 /* Interpret as a float number */
1318 double r = us_atof(tmp);
1333 vlc_ureduce(num, den, n, d, 0);
1340 return VLC_EGENERIC;
1343 /**********************************************************************
1344 * Trigger the callbacks.
1345 * Tell we're in a callback, release the lock, call stored functions,
1347 **********************************************************************/
1348 static int TriggerCallback( vlc_object_t *p_this, variable_t *p_var,
1349 const char *psz_name, vlc_value_t oldval )
1353 int i_entries = p_var->i_entries;
1354 if( i_entries == 0 )
1357 callback_entry_t *p_entries = p_var->p_entries;
1358 vlc_object_internals_t *p_priv = vlc_internals( p_this );
1360 assert( !p_var->b_incallback );
1361 p_var->b_incallback = true;
1362 vlc_mutex_unlock( &p_priv->var_lock );
1364 /* The real calls */
1365 for( ; i_entries-- ; )
1367 p_entries[i_entries].pf_callback( p_this, psz_name, oldval, p_var->val,
1368 p_entries[i_entries].p_data );
1371 vlc_mutex_lock( &p_priv->var_lock );
1372 p_var->b_incallback = false;
1373 vlc_cond_broadcast( &p_priv->var_wait );
1379 /**********************************************************************
1380 * Execute a var command on an object identified by its name
1381 **********************************************************************/
1382 int var_Command( vlc_object_t *p_this, const char *psz_name,
1383 const char *psz_cmd, const char *psz_arg, char **psz_msg )
1385 vlc_object_t *p_obj = vlc_object_find_name( p_this->p_libvlc,
1392 *psz_msg = strdup( "Unknown destination object." );
1396 i_type = var_Type( p_obj, psz_cmd );
1397 if( !( i_type&VLC_VAR_ISCOMMAND ) )
1399 vlc_object_release( p_obj );
1401 *psz_msg = strdup( "Variable doesn't exist or isn't a command." );
1402 return VLC_EGENERIC;
1405 i_type &= VLC_VAR_CLASS;
1408 case VLC_VAR_INTEGER:
1409 i_ret = var_SetInteger( p_obj, psz_cmd, atoi( psz_arg ) );
1412 i_ret = var_SetFloat( p_obj, psz_cmd, us_atof( psz_arg ) );
1414 case VLC_VAR_STRING:
1415 i_ret = var_SetString( p_obj, psz_cmd, psz_arg );
1418 i_ret = var_SetBool( p_obj, psz_cmd, atoi( psz_arg ) );
1421 i_ret = VLC_EGENERIC;
1425 vlc_object_release( p_obj );
1429 if( asprintf( psz_msg, "%s on object %s returned %i (%s)",
1430 psz_cmd, psz_name, i_ret, vlc_error( i_ret ) ) == -1)
1439 * Free a list and the associated strings
1440 * @param p_val: the list variable
1441 * @param p_val2: the variable associated or NULL
1443 void var_FreeList( vlc_value_t *p_val, vlc_value_t *p_val2 )
1446 if( p_val2 && p_val2->p_list )
1448 for( int i = 0; i < p_val2->p_list->i_count; i++ )
1449 free( p_val2->p_list->p_values[i].psz_string );
1450 if( p_val2->p_list->i_count )
1452 free( p_val2->p_list->p_values );
1453 free( p_val2->p_list->pi_types );
1455 free( p_val2->p_list );