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 *****************************************************************************/
41 #include <vlc_common.h>
42 #include <vlc_charset.h>
44 #include "variables.h"
45 #include "config/configuration.h"
47 /*****************************************************************************
49 *****************************************************************************/
50 struct callback_entry_t
52 vlc_callback_t pf_callback;
56 /*****************************************************************************
57 * Local comparison functions, returns 0 if v == w, < 0 if v < w, > 0 if v > w
58 *****************************************************************************/
59 static int CmpBool( vlc_value_t v, vlc_value_t w )
61 return v.b_bool ? w.b_bool ? 0 : 1 : w.b_bool ? -1 : 0;
64 static int CmpInt( vlc_value_t v, vlc_value_t w )
66 return v.i_int == w.i_int ? 0 : v.i_int > w.i_int ? 1 : -1;
69 static int CmpTime( vlc_value_t v, vlc_value_t w )
71 return v.i_time == w.i_time ? 0 : v.i_time > w.i_time ? 1 : -1;
74 static int CmpString( vlc_value_t v, vlc_value_t w )
77 return !w.psz_string ? 0 : -1;
79 return !w.psz_string ? 1 : strcmp( v.psz_string, w.psz_string );
81 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; }
82 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; }
84 /*****************************************************************************
85 * Local duplication functions, and local deallocation functions
86 *****************************************************************************/
87 static void DupDummy( vlc_value_t *p_val ) { (void)p_val; /* unused */ }
88 static void DupString( vlc_value_t *p_val )
90 p_val->psz_string = strdup( p_val->psz_string ? p_val->psz_string : "" );
93 static void FreeDummy( vlc_value_t *p_val ) { (void)p_val; /* unused */ }
94 static void FreeString( vlc_value_t *p_val ) { free( p_val->psz_string ); }
96 static void FreeList( vlc_value_t *p_val )
99 for( i = 0; i < p_val->p_list->i_count; i++ )
101 switch( p_val->p_list->pi_types[i] & VLC_VAR_CLASS )
104 FreeString( &p_val->p_list->p_values[i] );
111 if( p_val->p_list->i_count )
113 free( p_val->p_list->p_values );
114 free( p_val->p_list->pi_types );
116 free( p_val->p_list );
119 static const struct variable_ops_t
120 void_ops = { NULL, DupDummy, FreeDummy, },
121 addr_ops = { CmpAddress, DupDummy, FreeDummy, },
122 bool_ops = { CmpBool, DupDummy, FreeDummy, },
123 float_ops = { CmpFloat, DupDummy, FreeDummy, },
124 int_ops = { CmpInt, DupDummy, FreeDummy, },
125 string_ops = { CmpString, DupString, FreeString, },
126 time_ops = { CmpTime, DupDummy, FreeDummy, },
127 coords_ops = { NULL, DupDummy, FreeDummy, };
129 /*****************************************************************************
131 *****************************************************************************/
132 static void WaitUnused ( vlc_object_t *, variable_t * );
134 static void CheckValue ( variable_t *, vlc_value_t * );
136 static int TriggerCallback( vlc_object_t *, variable_t *, const char *,
139 static int varcmp( const void *a, const void *b )
141 const variable_t *va = a, *vb = b;
143 /* psz_name must be first */
144 assert( va == (const void *)&va->psz_name );
145 return strcmp( va->psz_name, vb->psz_name );
148 static variable_t *Lookup( vlc_object_t *obj, const char *psz_name )
150 vlc_object_internals_t *priv = vlc_internals( obj );
153 vlc_assert_locked( &priv->var_lock );
154 pp_var = tfind( &psz_name, &priv->var_root, varcmp );
155 return (pp_var != NULL) ? *pp_var : NULL;
158 static void Destroy( variable_t *p_var )
160 p_var->ops->pf_free( &p_var->val );
161 if( p_var->choices.i_count )
163 for( int i = 0 ; i < p_var->choices.i_count ; i++ )
165 p_var->ops->pf_free( &p_var->choices.p_values[i] );
166 free( p_var->choices_text.p_values[i].psz_string );
168 free( p_var->choices.p_values );
169 free( p_var->choices_text.p_values );
172 for (int i = 0; i < p_var->i_entries; i++)
174 const char *file = "?", *symbol = "?";
175 const void *addr = p_var->p_entries[i].pf_callback;
179 if (dladdr (addr, &info))
181 if (info.dli_fname) file = info.dli_fname;
182 if (info.dli_sname) symbol = info.dli_sname;
185 fprintf (stderr, "Error: callback on \"%s\" dangling %s(%s)[%p]\n",
186 p_var->psz_name, file, symbol, addr);
190 free( p_var->psz_name );
191 free( p_var->psz_text );
192 free( p_var->p_entries );
198 * Initialize a vlc variable
200 * We hash the given string and insert it into the sorted list. The insertion
201 * may require slow memory copies, but think about what we gain in the log(n)
202 * lookup phase when setting/getting the variable value!
204 * \param p_this The object in which to create the variable
205 * \param psz_name The name of the variable
206 * \param i_type The variables type. Must be one of \ref var_type combined with
207 * zero or more \ref var_flags
209 int var_Create( vlc_object_t *p_this, const char *psz_name, int i_type )
213 variable_t *p_var = calloc( 1, sizeof( *p_var ) );
217 p_var->psz_name = strdup( psz_name );
218 p_var->psz_text = NULL;
220 p_var->i_type = i_type & ~VLC_VAR_DOINHERIT;
224 p_var->i_default = -1;
225 p_var->choices.i_count = 0;
226 p_var->choices.p_values = NULL;
227 p_var->choices_text.i_count = 0;
228 p_var->choices_text.p_values = NULL;
230 p_var->b_incallback = false;
231 p_var->i_entries = 0;
232 p_var->p_entries = NULL;
234 /* Always initialize the variable, even if it is a list variable; this
235 * will lead to errors if the variable is not initialized, but it will
236 * not cause crashes in the variable handling. */
237 switch( i_type & VLC_VAR_CLASS )
240 p_var->ops = &bool_ops;
241 p_var->val.b_bool = false;
243 case VLC_VAR_INTEGER:
244 p_var->ops = &int_ops;
245 p_var->val.i_int = 0;
248 p_var->ops = &string_ops;
249 p_var->val.psz_string = NULL;
252 p_var->ops = &float_ops;
253 p_var->val.f_float = 0.0;
256 p_var->ops = &time_ops;
257 p_var->val.i_time = 0;
260 p_var->ops = &coords_ops;
261 p_var->val.coords.x = p_var->val.coords.y = 0;
263 case VLC_VAR_ADDRESS:
264 p_var->ops = &addr_ops;
265 p_var->val.p_address = NULL;
268 p_var->ops = &void_ops;
274 if( (i_type & VLC_VAR_DOINHERIT)
275 && var_Inherit( p_this, psz_name, i_type, &p_var->val ) == 0 )
277 if( i_type & VLC_VAR_HASCHOICE )
279 /* We must add the inherited value to our choice list */
280 p_var->i_default = 0;
282 INSERT_ELEM( p_var->choices.p_values, p_var->choices.i_count,
284 INSERT_ELEM( p_var->choices_text.p_values,
285 p_var->choices_text.i_count, 0, p_var->val );
286 p_var->ops->pf_dup( &p_var->choices.p_values[0] );
287 p_var->choices_text.p_values[0].psz_string = NULL;
291 vlc_object_internals_t *p_priv = vlc_internals( p_this );
292 variable_t **pp_var, *p_oldvar;
293 int ret = VLC_SUCCESS;
295 vlc_mutex_lock( &p_priv->var_lock );
297 pp_var = tsearch( p_var, &p_priv->var_root, varcmp );
298 if( unlikely(pp_var == NULL) )
300 else if( (p_oldvar = *pp_var) == p_var ) /* Variable create */
301 p_var = NULL; /* Variable created */
302 else /* Variable already exists */
304 assert (((i_type ^ p_oldvar->i_type) & VLC_VAR_CLASS) == 0);
306 p_oldvar->i_type |= i_type & (VLC_VAR_ISCOMMAND|VLC_VAR_HASCHOICE);
308 vlc_mutex_unlock( &p_priv->var_lock );
310 /* If we did not need to create a new variable, free everything... */
318 * Destroy a vlc variable
320 * Look for the variable and destroy it if it is found. As in var_Create we
321 * do a call to memmove() but we have performance counterparts elsewhere.
323 * \param p_this The object that holds the variable
324 * \param psz_name The name of the variable
326 int var_Destroy( vlc_object_t *p_this, const char *psz_name )
332 vlc_object_internals_t *p_priv = vlc_internals( p_this );
334 vlc_mutex_lock( &p_priv->var_lock );
336 p_var = Lookup( p_this, psz_name );
339 vlc_mutex_unlock( &p_priv->var_lock );
343 WaitUnused( p_this, p_var );
345 if( --p_var->i_usage == 0 )
346 tdelete( p_var, &p_priv->var_root, varcmp );
349 vlc_mutex_unlock( &p_priv->var_lock );
356 static void CleanupVar( void *var )
361 void var_DestroyAll( vlc_object_t *obj )
363 vlc_object_internals_t *priv = vlc_internals( obj );
365 tdestroy( priv->var_root, CleanupVar );
366 priv->var_root = NULL;
371 * Perform an action on a variable
373 * \param p_this The object that holds the variable
374 * \param psz_name The name of the variable
375 * \param i_action The action to perform. Must be one of \ref var_action
376 * \param p_val First action parameter
377 * \param p_val2 Second action parameter
379 int var_Change( vlc_object_t *p_this, const char *psz_name,
380 int i_action, vlc_value_t *p_val, vlc_value_t *p_val2 )
382 int ret = VLC_SUCCESS;
389 vlc_object_internals_t *p_priv = vlc_internals( p_this );
391 vlc_mutex_lock( &p_priv->var_lock );
393 p_var = Lookup( p_this, psz_name );
396 vlc_mutex_unlock( &p_priv->var_lock );
403 if( p_var->i_type & VLC_VAR_HASMIN )
405 p_var->ops->pf_free( &p_var->min );
407 p_var->i_type |= VLC_VAR_HASMIN;
409 p_var->ops->pf_dup( &p_var->min );
410 CheckValue( p_var, &p_var->val );
413 if( p_var->i_type & VLC_VAR_HASMIN )
419 if( p_var->i_type & VLC_VAR_HASMAX )
421 p_var->ops->pf_free( &p_var->max );
423 p_var->i_type |= VLC_VAR_HASMAX;
425 p_var->ops->pf_dup( &p_var->max );
426 CheckValue( p_var, &p_var->val );
429 if( p_var->i_type & VLC_VAR_HASMAX )
434 case VLC_VAR_SETSTEP:
435 if( p_var->i_type & VLC_VAR_HASSTEP )
437 p_var->ops->pf_free( &p_var->step );
439 p_var->i_type |= VLC_VAR_HASSTEP;
440 p_var->step = *p_val;
441 p_var->ops->pf_dup( &p_var->step );
442 CheckValue( p_var, &p_var->val );
444 case VLC_VAR_GETSTEP:
445 if( p_var->i_type & VLC_VAR_HASSTEP )
446 *p_val = p_var->step;
450 case VLC_VAR_ADDCHOICE:
452 int i = p_var->choices.i_count;
454 INSERT_ELEM( p_var->choices.p_values, p_var->choices.i_count,
456 INSERT_ELEM( p_var->choices_text.p_values,
457 p_var->choices_text.i_count, i, *p_val );
458 p_var->ops->pf_dup( &p_var->choices.p_values[i] );
459 p_var->choices_text.p_values[i].psz_string =
460 ( p_val2 && p_val2->psz_string ) ?
461 strdup( p_val2->psz_string ) : NULL;
463 CheckValue( p_var, &p_var->val );
466 case VLC_VAR_DELCHOICE:
470 for( i = 0 ; i < p_var->choices.i_count ; i++ )
471 if( p_var->ops->pf_cmp( p_var->choices.p_values[i], *p_val ) == 0 )
474 if( i == p_var->choices.i_count )
477 vlc_mutex_unlock( &p_priv->var_lock );
481 if( p_var->i_default > i )
483 else if( p_var->i_default == i )
484 p_var->i_default = -1;
486 p_var->ops->pf_free( &p_var->choices.p_values[i] );
487 free( p_var->choices_text.p_values[i].psz_string );
488 REMOVE_ELEM( p_var->choices.p_values, p_var->choices.i_count, i );
489 REMOVE_ELEM( p_var->choices_text.p_values,
490 p_var->choices_text.i_count, i );
492 CheckValue( p_var, &p_var->val );
495 case VLC_VAR_CHOICESCOUNT:
496 p_val->i_int = p_var->choices.i_count;
498 case VLC_VAR_CLEARCHOICES:
499 for( int i = 0 ; i < p_var->choices.i_count ; i++ )
500 p_var->ops->pf_free( &p_var->choices.p_values[i] );
501 for( int i = 0 ; i < p_var->choices_text.i_count ; i++ )
502 free( p_var->choices_text.p_values[i].psz_string );
504 if( p_var->choices.i_count ) free( p_var->choices.p_values );
505 if( p_var->choices_text.i_count ) free( p_var->choices_text.p_values );
507 p_var->choices.i_count = 0;
508 p_var->choices.p_values = NULL;
509 p_var->choices_text.i_count = 0;
510 p_var->choices_text.p_values = NULL;
511 p_var->i_default = -1;
513 case VLC_VAR_SETDEFAULT:
516 /* FIXME: the list is sorted, dude. Use something cleverer. */
517 for( i = 0 ; i < p_var->choices.i_count ; i++ )
518 if( p_var->ops->pf_cmp( p_var->choices.p_values[i], *p_val ) == 0 )
521 if( i == p_var->choices.i_count )
525 p_var->i_default = i;
526 CheckValue( p_var, &p_var->val );
529 case VLC_VAR_SETVALUE:
530 /* Duplicate data if needed */
532 p_var->ops->pf_dup( &newval );
533 /* Backup needed stuff */
535 /* Check boundaries and list */
536 CheckValue( p_var, &newval );
537 /* Set the variable */
539 /* Free data if needed */
540 p_var->ops->pf_free( &oldval );
542 case VLC_VAR_GETCHOICES:
543 case VLC_VAR_GETLIST:
544 p_val->p_list = malloc( sizeof(vlc_list_t) );
545 if( p_val2 ) p_val2->p_list = malloc( sizeof(vlc_list_t) );
546 if( p_var->choices.i_count )
548 p_val->p_list->p_values = malloc( p_var->choices.i_count
549 * sizeof(vlc_value_t) );
550 p_val->p_list->pi_types = malloc( p_var->choices.i_count
554 p_val2->p_list->p_values =
555 malloc( p_var->choices.i_count * sizeof(vlc_value_t) );
556 p_val2->p_list->pi_types =
557 malloc( p_var->choices.i_count * sizeof(int) );
560 p_val->p_list->i_count = p_var->choices.i_count;
561 if( p_val2 ) p_val2->p_list->i_count = p_var->choices.i_count;
562 for( int i = 0 ; i < p_var->choices.i_count ; i++ )
564 p_val->p_list->p_values[i] = p_var->choices.p_values[i];
565 p_val->p_list->pi_types[i] = p_var->i_type;
566 p_var->ops->pf_dup( &p_val->p_list->p_values[i] );
569 p_val2->p_list->p_values[i].psz_string =
570 p_var->choices_text.p_values[i].psz_string ?
571 strdup(p_var->choices_text.p_values[i].psz_string) : NULL;
572 p_val2->p_list->pi_types[i] = VLC_VAR_STRING;
576 case VLC_VAR_SETTEXT:
577 free( p_var->psz_text );
578 if( p_val && p_val->psz_string )
579 p_var->psz_text = strdup( p_val->psz_string );
581 p_var->psz_text = NULL;
583 case VLC_VAR_GETTEXT:
584 p_val->psz_string = p_var->psz_text ? strdup( p_var->psz_text )
587 case VLC_VAR_SETISCOMMAND:
588 p_var->i_type |= VLC_VAR_ISCOMMAND;
595 vlc_mutex_unlock( &p_priv->var_lock );
602 * Perform a Get and Set on a variable
604 * \param p_this: The object that hold the variable
605 * \param psz_name: the name of the variable
606 * \param i_action: the action to perform
607 * \param p_val: The action parameter
608 * \return vlc error codes
610 int var_GetAndSet( vlc_object_t *p_this, const char *psz_name, int i_action,
620 vlc_object_internals_t *p_priv = vlc_internals( p_this );
622 vlc_mutex_lock( &p_priv->var_lock );
623 p_var = Lookup( p_this, psz_name );
626 vlc_mutex_unlock( &p_priv->var_lock );
630 WaitUnused( p_this, p_var );
632 /* Duplicated data if needed */
633 //p_var->ops->pf_dup( &val );
635 /* Backup needed stuff */
638 /* depending of the action requiered */
641 case VLC_VAR_BOOL_TOGGLE:
642 assert( ( p_var->i_type & VLC_VAR_BOOL ) == VLC_VAR_BOOL );
643 p_var->val.b_bool = !p_var->val.b_bool;
645 case VLC_VAR_INTEGER_ADD:
646 assert( ( p_var->i_type & VLC_VAR_INTEGER ) == VLC_VAR_INTEGER );
647 p_var->val.i_int += p_val->i_int;
649 case VLC_VAR_INTEGER_OR:
650 assert( ( p_var->i_type & VLC_VAR_INTEGER ) == VLC_VAR_INTEGER );
651 p_var->val.i_int |= p_val->i_int;
653 case VLC_VAR_INTEGER_NAND:
654 assert( ( p_var->i_type & VLC_VAR_INTEGER ) == VLC_VAR_INTEGER );
655 p_var->val.i_int &= ~p_val->i_int;
658 vlc_mutex_unlock( &p_priv->var_lock );
662 /* Check boundaries */
663 CheckValue( p_var, &p_var->val );
666 /* Deal with callbacks.*/
667 i_ret = TriggerCallback( p_this, p_var, psz_name, oldval );
669 vlc_mutex_unlock( &p_priv->var_lock );
676 * Request a variable's type
678 * \return The variable type if it exists, or 0 if the
679 * variable could not be found.
682 int var_Type( vlc_object_t *p_this, const char *psz_name )
689 vlc_object_internals_t *p_priv = vlc_internals( p_this );
691 vlc_mutex_lock( &p_priv->var_lock );
693 p_var = Lookup( p_this, psz_name );
695 i_type = p_var->i_type;
697 vlc_mutex_unlock( &p_priv->var_lock );
702 #undef var_SetChecked
703 int var_SetChecked( vlc_object_t *p_this, const char *psz_name,
704 int expected_type, vlc_value_t val )
706 int i_ret = VLC_SUCCESS;
712 vlc_object_internals_t *p_priv = vlc_internals( p_this );
714 vlc_mutex_lock( &p_priv->var_lock );
716 p_var = Lookup( p_this, psz_name );
719 vlc_mutex_unlock( &p_priv->var_lock );
723 assert( expected_type == 0 ||
724 (p_var->i_type & VLC_VAR_CLASS) == expected_type );
725 assert ((p_var->i_type & VLC_VAR_CLASS) != VLC_VAR_VOID);
727 WaitUnused( p_this, p_var );
729 /* Duplicate data if needed */
730 p_var->ops->pf_dup( &val );
732 /* Backup needed stuff */
735 /* Check boundaries and list */
736 CheckValue( p_var, &val );
738 /* Set the variable */
741 /* Deal with callbacks */
742 i_ret = TriggerCallback( p_this, p_var, psz_name, oldval );
744 /* Free data if needed */
745 p_var->ops->pf_free( &oldval );
747 vlc_mutex_unlock( &p_priv->var_lock );
754 * Set a variable's value
756 * \param p_this The object that hold the variable
757 * \param psz_name The name of the variable
758 * \param val the value to set
760 int var_Set( vlc_object_t *p_this, const char *psz_name, vlc_value_t val )
762 return var_SetChecked( p_this, psz_name, 0, val );
765 #undef var_GetChecked
766 int var_GetChecked( vlc_object_t *p_this, const char *psz_name,
767 int expected_type, vlc_value_t *p_val )
771 vlc_object_internals_t *p_priv = vlc_internals( p_this );
773 int err = VLC_SUCCESS;
775 vlc_mutex_lock( &p_priv->var_lock );
777 p_var = Lookup( p_this, psz_name );
780 assert( expected_type == 0 ||
781 (p_var->i_type & VLC_VAR_CLASS) == expected_type );
782 assert ((p_var->i_type & VLC_VAR_CLASS) != VLC_VAR_VOID);
784 /* Really get the variable */
787 /* Duplicate value if needed */
788 p_var->ops->pf_dup( p_val );
793 vlc_mutex_unlock( &p_priv->var_lock );
799 * Get a variable's value
801 * \param p_this The object that holds the variable
802 * \param psz_name The name of the variable
803 * \param p_val Pointer to a vlc_value_t that will hold the variable's value
804 * after the function is finished
806 int var_Get( vlc_object_t *p_this, const char *psz_name, vlc_value_t *p_val )
808 return var_GetChecked( p_this, psz_name, 0, p_val );
811 #undef var_AddCallback
813 * Register a callback in a variable
815 * We store a function pointer that will be called upon variable
818 * \param p_this The object that holds the variable
819 * \param psz_name The name of the variable
820 * \param pf_callback The function pointer
821 * \param p_data A generic pointer that will be passed as the last
822 * argument to the callback function.
824 * \warning The callback function is run in the thread that calls var_Set on
825 * the variable. Use proper locking. This thread may not have much
826 * time to spare, so keep callback functions short.
828 int var_AddCallback( vlc_object_t *p_this, const char *psz_name,
829 vlc_callback_t pf_callback, void *p_data )
832 callback_entry_t entry;
836 vlc_object_internals_t *p_priv = vlc_internals( p_this );
838 entry.pf_callback = pf_callback;
839 entry.p_data = p_data;
841 vlc_mutex_lock( &p_priv->var_lock );
843 p_var = Lookup( p_this, psz_name );
846 vlc_mutex_unlock( &p_priv->var_lock );
847 msg_Err( p_this, "cannot add callback %p to nonexistent "
848 "variable '%s'", pf_callback, psz_name );
852 WaitUnused( p_this, p_var );
853 INSERT_ELEM( p_var->p_entries,
858 vlc_mutex_unlock( &p_priv->var_lock );
863 #undef var_DelCallback
865 * Remove a callback from a variable
867 * pf_callback and p_data have to be given again, because different objects
868 * might have registered the same callback function.
870 int var_DelCallback( vlc_object_t *p_this, const char *psz_name,
871 vlc_callback_t pf_callback, void *p_data )
876 bool b_found_similar = false;
881 vlc_object_internals_t *p_priv = vlc_internals( p_this );
883 vlc_mutex_lock( &p_priv->var_lock );
885 p_var = Lookup( p_this, psz_name );
888 vlc_mutex_unlock( &p_priv->var_lock );
892 WaitUnused( p_this, p_var );
894 for( i_entry = p_var->i_entries ; i_entry-- ; )
896 if( p_var->p_entries[i_entry].pf_callback == pf_callback
897 && p_var->p_entries[i_entry].p_data == p_data )
902 else if( p_var->p_entries[i_entry].pf_callback == pf_callback )
903 b_found_similar = true;
910 if( b_found_similar )
911 fprintf( stderr, "Calling var_DelCallback for '%s' with the same "
912 "function but not the same data.", psz_name );
915 vlc_mutex_unlock( &p_priv->var_lock );
919 REMOVE_ELEM( p_var->p_entries, p_var->i_entries, i_entry );
921 vlc_mutex_unlock( &p_priv->var_lock );
926 #undef var_TriggerCallback
928 * Trigger callback on a variable
930 * \param p_this The object that hold the variable
931 * \param psz_name The name of the variable
933 int var_TriggerCallback( vlc_object_t *p_this, const char *psz_name )
940 vlc_object_internals_t *p_priv = vlc_internals( p_this );
942 vlc_mutex_lock( &p_priv->var_lock );
944 p_var = Lookup( p_this, psz_name );
947 vlc_mutex_unlock( &p_priv->var_lock );
951 WaitUnused( p_this, p_var );
953 /* Deal with callbacks. Tell we're in a callback, release the lock,
954 * call stored functions, retake the lock. */
955 i_ret = TriggerCallback( p_this, p_var, psz_name, p_var->val );
957 vlc_mutex_unlock( &p_priv->var_lock );
961 /** Parse a stringified option
962 * This function parse a string option and create the associated object
964 * The option must be of the form "[no[-]]foo[=bar]" where foo is the
965 * option name and bar is the value of the option.
966 * \param p_obj the object in which the variable must be created
967 * \param psz_option the option to parse
968 * \param trusted whether the option is set by a trusted input or not
971 void var_OptionParse( vlc_object_t *p_obj, const char *psz_option,
974 char *psz_name, *psz_value;
979 val.psz_string = NULL;
981 /* It's too much of a hassle to remove the ':' when we parse
983 if( psz_option[0] == ':' )
989 psz_name = strdup( psz_option );
990 if( psz_name == NULL )
993 psz_value = strchr( psz_name, '=' );
994 if( psz_value != NULL )
997 i_type = config_GetType( p_obj, psz_name );
998 if( !i_type && !psz_value )
1000 /* check for "no-foo" or "nofoo" */
1001 if( !strncmp( psz_name, "no-", 3 ) )
1003 memmove( psz_name, psz_name + 3, strlen(psz_name) + 1 - 3 );
1005 else if( !strncmp( psz_name, "no", 2 ) )
1007 memmove( psz_name, psz_name + 2, strlen(psz_name) + 1 - 2 );
1009 else goto cleanup; /* Option doesn't exist */
1012 i_type = config_GetType( p_obj, psz_name );
1014 if( !i_type ) goto cleanup; /* Option doesn't exist */
1016 if( ( i_type != VLC_VAR_BOOL ) &&
1017 ( !psz_value || !*psz_value ) ) goto cleanup; /* Invalid value */
1019 /* check if option is unsafe */
1020 if( !trusted && !config_IsSafe( psz_name ) )
1022 msg_Err( p_obj, "unsafe option \"%s\" has been ignored for "
1023 "security reasons", psz_name );
1028 /* Create the variable in the input object.
1029 * Children of the input object will be able to retreive this value
1030 * thanks to the inheritance property of the object variables. */
1031 var_Create( p_obj, psz_name, i_type );
1036 val.b_bool = !b_isno;
1039 case VLC_VAR_INTEGER:
1040 val.i_int = strtoll( psz_value, NULL, 0 );
1044 val.f_float = us_atof( psz_value );
1047 case VLC_VAR_STRING:
1048 val.psz_string = psz_value;
1055 var_Set( p_obj, psz_name, val );
1061 #undef var_LocationParse
1063 * Parses a set of colon-separated or semicolon-separated
1064 * <variable name>=<value> pairs.
1065 * Some access (or access_demux) plugins uses this scheme
1066 * in media resource location.
1067 * @note Only trusted/safe variables are allowed. This is intended.
1069 * @warning Only use this for plugins implementing VLC-specific resource
1070 * location schemes. This would not make any sense for standardized ones.
1072 * @param obj VLC object on which to set variables (and emit error messages)
1073 * @param mrl string to parse
1074 * @param pref prefix to prepend to option names in the string
1076 * @return VLC_ENOMEM on error, VLC_SUCCESS on success.
1078 int var_LocationParse (vlc_object_t *obj, const char *mrl, const char *pref)
1080 int ret = VLC_SUCCESS;
1081 size_t preflen = strlen (pref) + 1;
1083 assert(mrl != NULL);
1084 while (*mrl != '\0')
1086 mrl += strspn (mrl, ":;"); /* skip leading colon(s) */
1088 size_t len = strcspn (mrl, ":;");
1089 char *buf = malloc (preflen + len);
1091 if (likely(buf != NULL))
1093 /* NOTE: this does not support the "no-<varname>" bool syntax. */
1094 /* DO NOT use asprintf() here; it won't work! Think again. */
1095 snprintf (buf, preflen + len, "%s%s", pref, mrl);
1096 var_OptionParse (obj, buf, false);
1108 * Waits until the variable is inactive (i.e. not executing a callback)
1110 static void WaitUnused( vlc_object_t *p_this, variable_t *p_var )
1112 vlc_object_internals_t *p_priv = vlc_internals( p_this );
1114 mutex_cleanup_push( &p_priv->var_lock );
1115 while( p_var->b_incallback )
1116 vlc_cond_wait( &p_priv->var_wait, &p_priv->var_lock );
1120 /*****************************************************************************
1121 * CheckValue: check that a value is valid wrt. a variable
1122 *****************************************************************************
1123 * This function checks p_val's value against p_var's limitations such as
1124 * minimal and maximal value, step, in-list position, and modifies p_val if
1126 ****************************************************************************/
1127 static void CheckValue ( variable_t *p_var, vlc_value_t *p_val )
1129 /* Check that our variable is in the list */
1130 if( p_var->i_type & VLC_VAR_HASCHOICE && p_var->choices.i_count )
1134 /* This list is not sorted so go throug it (this is a small list) */
1135 for( i = p_var->choices.i_count ; i-- ; )
1137 if( p_var->ops->pf_cmp( *p_val, p_var->choices.p_values[i] ) == 0 )
1143 /* If not found, change it to anything vaguely valid */
1146 /* Free the old variable, get the new one, dup it */
1147 p_var->ops->pf_free( p_val );
1148 *p_val = p_var->choices.p_values[p_var->i_default >= 0
1149 ? p_var->i_default : 0 ];
1150 p_var->ops->pf_dup( p_val );
1154 /* Check that our variable is within the bounds */
1155 switch( p_var->i_type & VLC_VAR_TYPE )
1157 case VLC_VAR_INTEGER:
1158 if( p_var->i_type & VLC_VAR_HASSTEP && p_var->step.i_int
1159 && (p_val->i_int % p_var->step.i_int) )
1161 p_val->i_int = (p_val->i_int + (p_var->step.i_int / 2))
1162 / p_var->step.i_int * p_var->step.i_int;
1164 if( p_var->i_type & VLC_VAR_HASMIN
1165 && p_val->i_int < p_var->min.i_int )
1167 p_val->i_int = p_var->min.i_int;
1169 if( p_var->i_type & VLC_VAR_HASMAX
1170 && p_val->i_int > p_var->max.i_int )
1172 p_val->i_int = p_var->max.i_int;
1176 if( p_var->i_type & VLC_VAR_HASSTEP && p_var->step.f_float )
1178 float f_round = p_var->step.f_float * (float)(int)( 0.5 +
1179 p_val->f_float / p_var->step.f_float );
1180 if( p_val->f_float != f_round )
1182 p_val->f_float = f_round;
1185 if( p_var->i_type & VLC_VAR_HASMIN
1186 && p_val->f_float < p_var->min.f_float )
1188 p_val->f_float = p_var->min.f_float;
1190 if( p_var->i_type & VLC_VAR_HASMAX
1191 && p_val->f_float > p_var->max.f_float )
1193 p_val->f_float = p_var->max.f_float;
1203 * Finds the value of a variable. If the specified object does not hold a
1204 * variable with the specified name, try the parent object, and iterate until
1205 * the top of the tree. If no match is found, the value is read from the
1208 int var_Inherit( vlc_object_t *p_this, const char *psz_name, int i_type,
1209 vlc_value_t *p_val )
1211 i_type &= VLC_VAR_CLASS;
1212 for( vlc_object_t *obj = p_this; obj != NULL; obj = obj->p_parent )
1214 if( var_GetChecked( obj, psz_name, i_type, p_val ) == VLC_SUCCESS )
1218 /* else take value from config */
1219 switch( i_type & VLC_VAR_CLASS )
1221 case VLC_VAR_STRING:
1222 p_val->psz_string = config_GetPsz( p_this, psz_name );
1223 if( !p_val->psz_string ) p_val->psz_string = strdup("");
1226 p_val->f_float = config_GetFloat( p_this, psz_name );
1228 case VLC_VAR_INTEGER:
1229 p_val->i_int = config_GetInt( p_this, psz_name );
1232 p_val->b_bool = config_GetInt( p_this, psz_name );
1236 case VLC_VAR_ADDRESS:
1244 * It inherits a string as an unsigned rational number (it also accepts basic
1247 * It returns an error if the rational number cannot be parsed (0/0 is valid).
1248 * The rational is already reduced.
1250 int (var_InheritURational)(vlc_object_t *object,
1251 unsigned *num, unsigned *den,
1259 char *tmp = var_InheritString(object, var);
1264 unsigned n = strtol(tmp, &next, 0);
1265 unsigned d = strtol(*next ? &next[1] : "0", NULL, 0);
1268 /* Interpret as a float number */
1269 double r = us_atof(tmp);
1284 vlc_ureduce(num, den, n, d, 0);
1291 return VLC_EGENERIC;
1294 /**********************************************************************
1295 * Trigger the callbacks.
1296 * Tell we're in a callback, release the lock, call stored functions,
1298 **********************************************************************/
1299 static int TriggerCallback( vlc_object_t *p_this, variable_t *p_var,
1300 const char *psz_name, vlc_value_t oldval )
1304 int i_entries = p_var->i_entries;
1305 if( i_entries == 0 )
1308 callback_entry_t *p_entries = p_var->p_entries;
1309 vlc_object_internals_t *p_priv = vlc_internals( p_this );
1311 assert( !p_var->b_incallback );
1312 p_var->b_incallback = true;
1313 vlc_mutex_unlock( &p_priv->var_lock );
1315 /* The real calls */
1316 for( ; i_entries-- ; )
1318 p_entries[i_entries].pf_callback( p_this, psz_name, oldval, p_var->val,
1319 p_entries[i_entries].p_data );
1322 vlc_mutex_lock( &p_priv->var_lock );
1323 p_var->b_incallback = false;
1324 vlc_cond_broadcast( &p_priv->var_wait );
1330 * Free a list and the associated strings
1331 * @param p_val: the list variable
1332 * @param p_val2: the variable associated or NULL
1334 void var_FreeList( vlc_value_t *p_val, vlc_value_t *p_val2 )
1337 if( p_val2 && p_val2->p_list )
1339 for( int i = 0; i < p_val2->p_list->i_count; i++ )
1340 free( p_val2->p_list->p_values[i].psz_string );
1341 if( p_val2->p_list->i_count )
1343 free( p_val2->p_list->p_values );
1344 free( p_val2->p_list->pi_types );
1346 free( p_val2->p_list );