1 /*****************************************************************************
2 * variables.c: routines for object variables handling
3 *****************************************************************************
4 * Copyright (C) 2002-2009 the VideoLAN team
7 * Authors: Samuel Hocevar <sam@zoy.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
31 #include <vlc_common.h>
32 #include <vlc_charset.h>
33 #include "variables.h"
40 #include "config/configuration.h"
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 )
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 )
458 *p_val = p_var->step;
461 case VLC_VAR_ADDCHOICE:
462 i = p_var->choices.i_count;
464 INSERT_ELEM( p_var->choices.p_values, p_var->choices.i_count,
466 INSERT_ELEM( p_var->choices_text.p_values,
467 p_var->choices_text.i_count, i, *p_val );
468 p_var->ops->pf_dup( &p_var->choices.p_values[i] );
469 p_var->choices_text.p_values[i].psz_string =
470 ( p_val2 && p_val2->psz_string ) ?
471 strdup( p_val2->psz_string ) : NULL;
473 CheckValue( p_var, &p_var->val );
475 case VLC_VAR_DELCHOICE:
476 for( i = 0 ; i < p_var->choices.i_count ; i++ )
478 if( p_var->ops->pf_cmp( p_var->choices.p_values[i], *p_val ) == 0 )
484 if( i == p_var->choices.i_count )
487 vlc_mutex_unlock( &p_priv->var_lock );
491 if( p_var->i_default > i )
495 else if( p_var->i_default == i )
497 p_var->i_default = -1;
500 p_var->ops->pf_free( &p_var->choices.p_values[i] );
501 free( p_var->choices_text.p_values[i].psz_string );
502 REMOVE_ELEM( p_var->choices.p_values, p_var->choices.i_count, i );
503 REMOVE_ELEM( p_var->choices_text.p_values,
504 p_var->choices_text.i_count, i );
506 CheckValue( p_var, &p_var->val );
508 case VLC_VAR_CHOICESCOUNT:
509 p_val->i_int = p_var->choices.i_count;
511 case VLC_VAR_CLEARCHOICES:
512 for( i = 0 ; i < p_var->choices.i_count ; i++ )
514 p_var->ops->pf_free( &p_var->choices.p_values[i] );
516 for( i = 0 ; i < p_var->choices_text.i_count ; i++ )
517 free( p_var->choices_text.p_values[i].psz_string );
519 if( p_var->choices.i_count ) free( p_var->choices.p_values );
520 if( p_var->choices_text.i_count ) free( p_var->choices_text.p_values );
522 p_var->choices.i_count = 0;
523 p_var->choices.p_values = NULL;
524 p_var->choices_text.i_count = 0;
525 p_var->choices_text.p_values = NULL;
526 p_var->i_default = -1;
528 case VLC_VAR_SETDEFAULT:
529 /* FIXME: the list is sorted, dude. Use something cleverer. */
530 for( i = 0 ; i < p_var->choices.i_count ; i++ )
532 if( p_var->ops->pf_cmp( p_var->choices.p_values[i], *p_val ) == 0 )
538 if( i == p_var->choices.i_count )
544 p_var->i_default = i;
545 CheckValue( p_var, &p_var->val );
547 case VLC_VAR_SETVALUE:
548 /* Duplicate data if needed */
550 p_var->ops->pf_dup( &newval );
551 /* Backup needed stuff */
553 /* Check boundaries and list */
554 CheckValue( p_var, &newval );
555 /* Set the variable */
557 /* Free data if needed */
558 p_var->ops->pf_free( &oldval );
560 case VLC_VAR_GETCHOICES:
561 case VLC_VAR_GETLIST:
562 p_val->p_list = malloc( sizeof(vlc_list_t) );
563 if( p_val2 ) p_val2->p_list = malloc( sizeof(vlc_list_t) );
564 if( p_var->choices.i_count )
566 p_val->p_list->p_values = malloc( p_var->choices.i_count
567 * sizeof(vlc_value_t) );
568 p_val->p_list->pi_types = malloc( p_var->choices.i_count
572 p_val2->p_list->p_values =
573 malloc( p_var->choices.i_count * sizeof(vlc_value_t) );
574 p_val2->p_list->pi_types =
575 malloc( p_var->choices.i_count * sizeof(int) );
578 p_val->p_list->i_count = p_var->choices.i_count;
579 if( p_val2 ) p_val2->p_list->i_count = p_var->choices.i_count;
580 for( i = 0 ; i < p_var->choices.i_count ; i++ )
582 p_val->p_list->p_values[i] = p_var->choices.p_values[i];
583 p_val->p_list->pi_types[i] = p_var->i_type;
584 p_var->ops->pf_dup( &p_val->p_list->p_values[i] );
587 p_val2->p_list->p_values[i].psz_string =
588 p_var->choices_text.p_values[i].psz_string ?
589 strdup(p_var->choices_text.p_values[i].psz_string) : NULL;
590 p_val2->p_list->pi_types[i] = VLC_VAR_STRING;
594 case VLC_VAR_SETTEXT:
595 free( p_var->psz_text );
596 if( p_val && p_val->psz_string )
597 p_var->psz_text = strdup( p_val->psz_string );
599 p_var->psz_text = NULL;
601 case VLC_VAR_GETTEXT:
602 p_val->psz_string = p_var->psz_text ? strdup( p_var->psz_text )
605 case VLC_VAR_SETISCOMMAND:
606 p_var->i_type |= VLC_VAR_ISCOMMAND;
613 vlc_mutex_unlock( &p_priv->var_lock );
620 * Perform a Get and Set on a variable
622 * \param p_this: The object that hold the variable
623 * \param psz_name: the name of the variable
624 * \param i_action: the action to perform
625 * \param p_val: The action parameter
626 * \return vlc error codes
628 int var_GetAndSet( vlc_object_t *p_this, const char *psz_name, int i_action,
638 vlc_object_internals_t *p_priv = vlc_internals( p_this );
640 vlc_mutex_lock( &p_priv->var_lock );
641 p_var = Lookup( p_this, psz_name );
644 vlc_mutex_unlock( &p_priv->var_lock );
648 WaitUnused( p_this, p_var );
650 /* Duplicated data if needed */
651 //p_var->ops->pf_dup( &val );
653 /* Backup needed stuff */
656 /* depending of the action requiered */
659 case VLC_VAR_BOOL_TOGGLE:
660 assert( ( p_var->i_type & VLC_VAR_BOOL ) == VLC_VAR_BOOL );
661 p_var->val.b_bool = !p_var->val.b_bool;
663 case VLC_VAR_INTEGER_ADD:
664 assert( ( p_var->i_type & VLC_VAR_INTEGER ) == VLC_VAR_INTEGER );
665 p_var->val.i_int += p_val->i_int;
667 case VLC_VAR_INTEGER_OR:
668 assert( ( p_var->i_type & VLC_VAR_INTEGER ) == VLC_VAR_INTEGER );
669 p_var->val.i_int |= p_val->i_int;
671 case VLC_VAR_INTEGER_NAND:
672 assert( ( p_var->i_type & VLC_VAR_INTEGER ) == VLC_VAR_INTEGER );
673 p_var->val.i_int &= ~p_val->i_int;
676 vlc_mutex_unlock( &p_priv->var_lock );
680 /* Check boundaries */
681 CheckValue( p_var, &p_var->val );
684 /* Deal with callbacks.*/
685 i_ret = TriggerCallback( p_this, p_var, psz_name, oldval );
687 vlc_mutex_unlock( &p_priv->var_lock );
694 * Request a variable's type
696 * \return The variable type if it exists, or 0 if the
697 * variable could not be found.
700 int var_Type( vlc_object_t *p_this, const char *psz_name )
707 vlc_object_internals_t *p_priv = vlc_internals( p_this );
709 vlc_mutex_lock( &p_priv->var_lock );
711 p_var = Lookup( p_this, psz_name );
713 i_type = p_var->i_type;
715 vlc_mutex_unlock( &p_priv->var_lock );
720 #undef var_SetChecked
721 int var_SetChecked( vlc_object_t *p_this, const char *psz_name,
722 int expected_type, vlc_value_t val )
724 int i_ret = VLC_SUCCESS;
730 vlc_object_internals_t *p_priv = vlc_internals( p_this );
732 vlc_mutex_lock( &p_priv->var_lock );
734 p_var = Lookup( p_this, psz_name );
737 vlc_mutex_unlock( &p_priv->var_lock );
741 assert( expected_type == 0 ||
742 (p_var->i_type & VLC_VAR_CLASS) == expected_type );
744 /* Alert if the type is VLC_VAR_VOID */
745 if( ( p_var->i_type & VLC_VAR_TYPE ) == VLC_VAR_VOID )
746 msg_Warn( p_this, "Calling var_Set on the void variable '%s' (0x%04x)", psz_name, p_var->i_type );
750 WaitUnused( p_this, p_var );
752 /* Duplicate data if needed */
753 p_var->ops->pf_dup( &val );
755 /* Backup needed stuff */
758 /* Check boundaries and list */
759 CheckValue( p_var, &val );
761 /* Set the variable */
764 /* Deal with callbacks */
765 i_ret = TriggerCallback( p_this, p_var, psz_name, oldval );
767 /* Free data if needed */
768 p_var->ops->pf_free( &oldval );
770 vlc_mutex_unlock( &p_priv->var_lock );
777 * Set a variable's value
779 * \param p_this The object that hold the variable
780 * \param psz_name The name of the variable
781 * \param val the value to set
783 int var_Set( vlc_object_t *p_this, const char *psz_name, vlc_value_t val )
785 return var_SetChecked( p_this, psz_name, 0, val );
788 #undef var_GetChecked
789 int var_GetChecked( vlc_object_t *p_this, const char *psz_name,
790 int expected_type, vlc_value_t *p_val )
794 vlc_object_internals_t *p_priv = vlc_internals( p_this );
796 int err = VLC_SUCCESS;
798 vlc_mutex_lock( &p_priv->var_lock );
800 p_var = Lookup( p_this, psz_name );
803 assert( expected_type == 0 ||
804 (p_var->i_type & VLC_VAR_CLASS) == expected_type );
806 /* Really get the variable */
810 /* Alert if the type is VLC_VAR_VOID */
811 if( ( p_var->i_type & VLC_VAR_TYPE ) == VLC_VAR_VOID )
812 msg_Warn( p_this, "Calling var_Get on the void variable '%s' (0x%04x)", psz_name, p_var->i_type );
815 /* Duplicate value if needed */
816 p_var->ops->pf_dup( p_val );
821 vlc_mutex_unlock( &p_priv->var_lock );
827 * Get a variable's value
829 * \param p_this The object that holds the variable
830 * \param psz_name The name of the variable
831 * \param p_val Pointer to a vlc_value_t that will hold the variable's value
832 * after the function is finished
834 int var_Get( vlc_object_t *p_this, const char *psz_name, vlc_value_t *p_val )
836 return var_GetChecked( p_this, psz_name, 0, p_val );
839 #undef var_AddCallback
841 * Register a callback in a variable
843 * We store a function pointer that will be called upon variable
846 * \param p_this The object that holds the variable
847 * \param psz_name The name of the variable
848 * \param pf_callback The function pointer
849 * \param p_data A generic pointer that will be passed as the last
850 * argument to the callback function.
852 * \warning The callback function is run in the thread that calls var_Set on
853 * the variable. Use proper locking. This thread may not have much
854 * time to spare, so keep callback functions short.
856 int var_AddCallback( vlc_object_t *p_this, const char *psz_name,
857 vlc_callback_t pf_callback, void *p_data )
860 callback_entry_t entry;
864 vlc_object_internals_t *p_priv = vlc_internals( p_this );
866 entry.pf_callback = pf_callback;
867 entry.p_data = p_data;
869 vlc_mutex_lock( &p_priv->var_lock );
871 p_var = Lookup( p_this, psz_name );
875 msg_Warn( p_this, "Failed to add a callback to the non-existing "
876 "variable '%s'", psz_name );
878 vlc_mutex_unlock( &p_priv->var_lock );
882 WaitUnused( p_this, p_var );
883 INSERT_ELEM( p_var->p_entries,
888 vlc_mutex_unlock( &p_priv->var_lock );
893 #undef var_DelCallback
895 * Remove a callback from a variable
897 * pf_callback and p_data have to be given again, because different objects
898 * might have registered the same callback function.
900 int var_DelCallback( vlc_object_t *p_this, const char *psz_name,
901 vlc_callback_t pf_callback, void *p_data )
906 bool b_found_similar = false;
911 vlc_object_internals_t *p_priv = vlc_internals( p_this );
913 vlc_mutex_lock( &p_priv->var_lock );
915 p_var = Lookup( p_this, psz_name );
918 vlc_mutex_unlock( &p_priv->var_lock );
922 WaitUnused( p_this, p_var );
924 for( i_entry = p_var->i_entries ; i_entry-- ; )
926 if( p_var->p_entries[i_entry].pf_callback == pf_callback
927 && p_var->p_entries[i_entry].p_data == p_data )
932 else if( p_var->p_entries[i_entry].pf_callback == pf_callback )
933 b_found_similar = true;
940 if( b_found_similar )
941 fprintf( stderr, "Calling var_DelCallback for '%s' with the same "
942 "function but not the same data.", psz_name );
945 vlc_mutex_unlock( &p_priv->var_lock );
949 REMOVE_ELEM( p_var->p_entries, p_var->i_entries, i_entry );
951 vlc_mutex_unlock( &p_priv->var_lock );
956 #undef var_TriggerCallback
958 * Trigger callback on a variable
960 * \param p_this The object that hold the variable
961 * \param psz_name The name of the variable
963 int var_TriggerCallback( vlc_object_t *p_this, const char *psz_name )
970 vlc_object_internals_t *p_priv = vlc_internals( p_this );
972 vlc_mutex_lock( &p_priv->var_lock );
974 p_var = Lookup( p_this, psz_name );
977 vlc_mutex_unlock( &p_priv->var_lock );
981 WaitUnused( p_this, p_var );
983 /* Deal with callbacks. Tell we're in a callback, release the lock,
984 * call stored functions, retake the lock. */
985 i_ret = TriggerCallback( p_this, p_var, psz_name, p_var->val );
987 vlc_mutex_unlock( &p_priv->var_lock );
991 /** Parse a stringified option
992 * This function parse a string option and create the associated object
994 * The option must be of the form "[no[-]]foo[=bar]" where foo is the
995 * option name and bar is the value of the option.
996 * \param p_obj the object in which the variable must be created
997 * \param psz_option the option to parse
998 * \param trusted whether the option is set by a trusted input or not
1001 void var_OptionParse( vlc_object_t *p_obj, const char *psz_option,
1004 char *psz_name, *psz_value;
1006 bool b_isno = false;
1009 val.psz_string = NULL;
1011 /* It's too much of a hassle to remove the ':' when we parse
1012 * the cmd line :) */
1013 if( psz_option[0] == ':' )
1016 if( !psz_option[0] )
1019 psz_name = strdup( psz_option );
1020 if( psz_name == NULL )
1023 psz_value = strchr( psz_name, '=' );
1024 if( psz_value != NULL )
1025 *psz_value++ = '\0';
1027 i_type = config_GetType( p_obj, psz_name );
1028 if( !i_type && !psz_value )
1030 /* check for "no-foo" or "nofoo" */
1031 if( !strncmp( psz_name, "no-", 3 ) )
1033 memmove( psz_name, psz_name + 3, strlen(psz_name) + 1 - 3 );
1035 else if( !strncmp( psz_name, "no", 2 ) )
1037 memmove( psz_name, psz_name + 2, strlen(psz_name) + 1 - 2 );
1039 else goto cleanup; /* Option doesn't exist */
1042 i_type = config_GetType( p_obj, psz_name );
1044 if( !i_type ) goto cleanup; /* Option doesn't exist */
1046 if( ( i_type != VLC_VAR_BOOL ) &&
1047 ( !psz_value || !*psz_value ) ) goto cleanup; /* Invalid value */
1049 /* check if option is unsafe */
1050 if( !trusted && !config_IsSafe( psz_name ) )
1052 msg_Err( p_obj, "unsafe option \"%s\" has been ignored for "
1053 "security reasons", psz_name );
1058 /* Create the variable in the input object.
1059 * Children of the input object will be able to retreive this value
1060 * thanks to the inheritance property of the object variables. */
1061 var_Create( p_obj, psz_name, i_type );
1066 val.b_bool = !b_isno;
1069 case VLC_VAR_INTEGER:
1070 val.i_int = strtoll( psz_value, NULL, 0 );
1074 val.f_float = us_atof( psz_value );
1077 case VLC_VAR_STRING:
1078 val.psz_string = psz_value;
1085 var_Set( p_obj, psz_name, val );
1091 #undef var_LocationParse
1093 * Parses a set of colon-separated <variable name>=<value> pairs. Some access
1094 * (or access_demux) plugins uses this scheme in media resource location.
1095 * @note Only trusted/safe variables are allowed. This is intended.
1097 * @warning Only use this for plugins implementing VLC-specific resource
1098 * location schemes. This would not make any sense for standardized ones.
1100 * @param obj VLC object on which to set variables (and emit error messages)
1101 * @param mrl string to parse
1102 * @param pref prefix to prepend to option names in the string
1104 * @return VLC_ENOMEM on error, VLC_SUCCESS on success.
1106 int var_LocationParse (vlc_object_t *obj, const char *mrl, const char *pref)
1108 int ret = VLC_SUCCESS;
1109 size_t preflen = strlen (pref) + 1;
1111 assert(mrl != NULL);
1112 while (*mrl != '\0')
1114 mrl += strspn (mrl, ":"); /* skip leading colon(s) */
1116 size_t len = strcspn (mrl, ":");
1117 char *buf = malloc (preflen + len);
1119 if (likely(buf != NULL))
1121 /* NOTE: this does not support the "no-<varname>" bool syntax. */
1122 /* DO NOT use asprintf() here; it won't work! Think again. */
1123 snprintf (buf, preflen + len, "%s%s", pref, mrl);
1124 var_OptionParse (obj, buf, false);
1136 * Waits until the variable is inactive (i.e. not executing a callback)
1138 static void WaitUnused( vlc_object_t *p_this, variable_t *p_var )
1140 vlc_object_internals_t *p_priv = vlc_internals( p_this );
1142 mutex_cleanup_push( &p_priv->var_lock );
1143 while( p_var->b_incallback )
1144 vlc_cond_wait( &p_priv->var_wait, &p_priv->var_lock );
1148 /*****************************************************************************
1149 * CheckValue: check that a value is valid wrt. a variable
1150 *****************************************************************************
1151 * This function checks p_val's value against p_var's limitations such as
1152 * minimal and maximal value, step, in-list position, and modifies p_val if
1154 ****************************************************************************/
1155 static void CheckValue ( variable_t *p_var, vlc_value_t *p_val )
1157 /* Check that our variable is in the list */
1158 if( p_var->i_type & VLC_VAR_HASCHOICE && p_var->choices.i_count )
1162 /* This list is not sorted so go throug it (this is a small list) */
1163 for( i = p_var->choices.i_count ; i-- ; )
1165 if( p_var->ops->pf_cmp( *p_val, p_var->choices.p_values[i] ) == 0 )
1171 /* If not found, change it to anything vaguely valid */
1174 /* Free the old variable, get the new one, dup it */
1175 p_var->ops->pf_free( p_val );
1176 *p_val = p_var->choices.p_values[p_var->i_default >= 0
1177 ? p_var->i_default : 0 ];
1178 p_var->ops->pf_dup( p_val );
1182 /* Check that our variable is within the bounds */
1183 switch( p_var->i_type & VLC_VAR_TYPE )
1185 case VLC_VAR_INTEGER:
1186 if( p_var->i_type & VLC_VAR_HASSTEP && p_var->step.i_int
1187 && (p_val->i_int % p_var->step.i_int) )
1189 p_val->i_int = (p_val->i_int + (p_var->step.i_int / 2))
1190 / p_var->step.i_int * p_var->step.i_int;
1192 if( p_var->i_type & VLC_VAR_HASMIN
1193 && p_val->i_int < p_var->min.i_int )
1195 p_val->i_int = p_var->min.i_int;
1197 if( p_var->i_type & VLC_VAR_HASMAX
1198 && p_val->i_int > p_var->max.i_int )
1200 p_val->i_int = p_var->max.i_int;
1204 if( p_var->i_type & VLC_VAR_HASSTEP && p_var->step.f_float )
1206 float f_round = p_var->step.f_float * (float)(int)( 0.5 +
1207 p_val->f_float / p_var->step.f_float );
1208 if( p_val->f_float != f_round )
1210 p_val->f_float = f_round;
1213 if( p_var->i_type & VLC_VAR_HASMIN
1214 && p_val->f_float < p_var->min.f_float )
1216 p_val->f_float = p_var->min.f_float;
1218 if( p_var->i_type & VLC_VAR_HASMAX
1219 && p_val->f_float > p_var->max.f_float )
1221 p_val->f_float = p_var->max.f_float;
1231 * Finds the value of a variable. If the specified object does not hold a
1232 * variable with the specified name, try the parent object, and iterate until
1233 * the top of the tree. If no match is found, the value is read from the
1236 int var_Inherit( vlc_object_t *p_this, const char *psz_name, int i_type,
1237 vlc_value_t *p_val )
1240 if (p_this != VLC_OBJECT(p_this->p_libvlc)
1241 && unlikely(p_this->p_parent == NULL))
1243 msg_Info (p_this, "%s(%s) on detached object", __func__, psz_name);
1247 i_type &= VLC_VAR_CLASS;
1248 for( vlc_object_t *obj = p_this; obj != NULL; obj = obj->p_parent )
1250 if( var_GetChecked( obj, psz_name, i_type, p_val ) == VLC_SUCCESS )
1253 if (obj != p_this && obj != VLC_OBJECT(p_this->p_libvlc)
1254 && unlikely(obj->p_parent == NULL))
1256 msg_Info (p_this, "%s(%s) on detached tree [%p] %s", __func__,
1257 psz_name, obj, obj->psz_object_type);
1263 /* else take value from config */
1264 switch( i_type & VLC_VAR_CLASS )
1266 case VLC_VAR_STRING:
1267 p_val->psz_string = config_GetPsz( p_this, psz_name );
1268 if( !p_val->psz_string ) p_val->psz_string = strdup("");
1271 p_val->f_float = config_GetFloat( p_this, psz_name );
1273 case VLC_VAR_INTEGER:
1274 p_val->i_int = config_GetInt( p_this, psz_name );
1277 p_val->b_bool = config_GetInt( p_this, psz_name );
1279 case VLC_VAR_ADDRESS:
1282 msg_Warn( p_this, "Could not inherit value for var %s "
1283 "from config. Invalid Type", psz_name );
1286 /*msg_Dbg( p_this, "Inherited value for var %s from config", psz_name );*/
1292 * It inherits a string as an unsigned rational number (it also accepts basic
1295 * It returns an error if the rational number cannot be parsed (0/0 is valid).
1296 * The rational is already reduced.
1298 int (var_InheritURational)(vlc_object_t *object,
1299 unsigned *num, unsigned *den,
1307 char *tmp = var_InheritString(object, var);
1312 unsigned n = strtol(tmp, &next, 0);
1313 unsigned d = strtol(*next ? &next[1] : "0", NULL, 0);
1316 /* Interpret as a float number */
1317 double r = us_atof(tmp);
1332 vlc_ureduce(num, den, n, d, 0);
1339 return VLC_EGENERIC;
1342 /**********************************************************************
1343 * Trigger the callbacks.
1344 * Tell we're in a callback, release the lock, call stored functions,
1346 **********************************************************************/
1347 static int TriggerCallback( vlc_object_t *p_this, variable_t *p_var,
1348 const char *psz_name, vlc_value_t oldval )
1352 int i_entries = p_var->i_entries;
1353 if( i_entries == 0 )
1356 callback_entry_t *p_entries = p_var->p_entries;
1357 vlc_object_internals_t *p_priv = vlc_internals( p_this );
1359 assert( !p_var->b_incallback );
1360 p_var->b_incallback = true;
1361 vlc_mutex_unlock( &p_priv->var_lock );
1363 /* The real calls */
1364 for( ; i_entries-- ; )
1366 p_entries[i_entries].pf_callback( p_this, psz_name, oldval, p_var->val,
1367 p_entries[i_entries].p_data );
1370 vlc_mutex_lock( &p_priv->var_lock );
1371 p_var->b_incallback = false;
1372 vlc_cond_broadcast( &p_priv->var_wait );
1378 /**********************************************************************
1379 * Execute a var command on an object identified by its name
1380 **********************************************************************/
1381 int var_Command( vlc_object_t *p_this, const char *psz_name,
1382 const char *psz_cmd, const char *psz_arg, char **psz_msg )
1384 vlc_object_t *p_obj = vlc_object_find_name( p_this->p_libvlc,
1391 *psz_msg = strdup( "Unknown destination object." );
1395 i_type = var_Type( p_obj, psz_cmd );
1396 if( !( i_type&VLC_VAR_ISCOMMAND ) )
1398 vlc_object_release( p_obj );
1400 *psz_msg = strdup( "Variable doesn't exist or isn't a command." );
1401 return VLC_EGENERIC;
1404 i_type &= VLC_VAR_CLASS;
1407 case VLC_VAR_INTEGER:
1408 i_ret = var_SetInteger( p_obj, psz_cmd, atoi( psz_arg ) );
1411 i_ret = var_SetFloat( p_obj, psz_cmd, us_atof( psz_arg ) );
1413 case VLC_VAR_STRING:
1414 i_ret = var_SetString( p_obj, psz_cmd, psz_arg );
1417 i_ret = var_SetBool( p_obj, psz_cmd, atoi( psz_arg ) );
1420 i_ret = VLC_EGENERIC;
1424 vlc_object_release( p_obj );
1428 if( asprintf( psz_msg, "%s on object %s returned %i (%s)",
1429 psz_cmd, psz_name, i_ret, vlc_error( i_ret ) ) == -1)
1438 * Free a list and the associated strings
1439 * @param p_val: the list variable
1440 * @param p_val2: the variable associated or NULL
1442 void var_FreeList( vlc_value_t *p_val, vlc_value_t *p_val2 )
1445 if( p_val2 && p_val2->p_list )
1447 for( int i = 0; i < p_val2->p_list->i_count; i++ )
1448 free( p_val2->p_list->p_values[i].psz_string );
1449 if( p_val2->p_list->i_count )
1451 free( p_val2->p_list->p_values );
1452 free( p_val2->p_list->pi_types );
1454 free( p_val2->p_list );