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 *****************************************************************************/
42 #include <vlc_common.h>
43 #include <vlc_charset.h>
45 #include "variables.h"
46 #include "config/configuration.h"
48 /*****************************************************************************
50 *****************************************************************************/
51 struct callback_entry_t
55 vlc_callback_t pf_value_callback;
56 vlc_list_callback_t pf_list_callback;
62 /*****************************************************************************
63 * Local comparison functions, returns 0 if v == w, < 0 if v < w, > 0 if v > w
64 *****************************************************************************/
65 static int CmpBool( vlc_value_t v, vlc_value_t w )
67 return v.b_bool ? w.b_bool ? 0 : 1 : w.b_bool ? -1 : 0;
70 static int CmpInt( vlc_value_t v, vlc_value_t w )
72 return v.i_int == w.i_int ? 0 : v.i_int > w.i_int ? 1 : -1;
75 static int CmpTime( vlc_value_t v, vlc_value_t w )
77 return v.i_time == w.i_time ? 0 : v.i_time > w.i_time ? 1 : -1;
80 static int CmpString( vlc_value_t v, vlc_value_t w )
83 return !w.psz_string ? 0 : -1;
85 return !w.psz_string ? 1 : strcmp( v.psz_string, w.psz_string );
87 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; }
88 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; }
90 /*****************************************************************************
91 * Local duplication functions, and local deallocation functions
92 *****************************************************************************/
93 static void DupDummy( vlc_value_t *p_val ) { (void)p_val; /* unused */ }
94 static void DupString( vlc_value_t *p_val )
96 p_val->psz_string = strdup( p_val->psz_string ? p_val->psz_string : "" );
99 static void FreeDummy( vlc_value_t *p_val ) { (void)p_val; /* unused */ }
100 static void FreeString( vlc_value_t *p_val ) { free( p_val->psz_string ); }
102 static const struct variable_ops_t
103 void_ops = { NULL, DupDummy, FreeDummy, },
104 addr_ops = { CmpAddress, DupDummy, FreeDummy, },
105 bool_ops = { CmpBool, DupDummy, FreeDummy, },
106 float_ops = { CmpFloat, DupDummy, FreeDummy, },
107 int_ops = { CmpInt, DupDummy, FreeDummy, },
108 string_ops = { CmpString, DupString, FreeString, },
109 time_ops = { CmpTime, DupDummy, FreeDummy, },
110 coords_ops = { NULL, DupDummy, FreeDummy, };
112 /*****************************************************************************
114 *****************************************************************************/
115 static void WaitUnused ( vlc_object_t *, variable_t * );
117 static void CheckValue ( variable_t *, vlc_value_t * );
119 static int TriggerCallback( vlc_object_t *, variable_t *, const char *,
122 static int TriggerListCallback( vlc_object_t *, variable_t *,
126 static int varcmp( const void *a, const void *b )
128 const variable_t *va = a, *vb = b;
130 /* psz_name must be first */
131 assert( va == (const void *)&va->psz_name );
132 return strcmp( va->psz_name, vb->psz_name );
135 static variable_t *Lookup( vlc_object_t *obj, const char *psz_name )
137 vlc_object_internals_t *priv = vlc_internals( obj );
140 vlc_assert_locked( &priv->var_lock );
141 pp_var = tfind( &psz_name, &priv->var_root, varcmp );
142 return (pp_var != NULL) ? *pp_var : NULL;
145 static void Destroy( variable_t *p_var )
147 p_var->ops->pf_free( &p_var->val );
148 if( p_var->choices.i_count )
150 for( int i = 0 ; i < p_var->choices.i_count ; i++ )
152 p_var->ops->pf_free( &p_var->choices.p_values[i] );
153 free( p_var->choices_text.p_values[i].psz_string );
155 free( p_var->choices.p_values );
156 free( p_var->choices_text.p_values );
159 callback_table_t *p_table = &p_var->value_callbacks;
160 for (int i = 0; i < p_table->i_entries; i++)
162 const char *file = "?", *symbol = "?";
163 const void *addr = p_table->p_entries[i].pf_callback;
167 if (dladdr (addr, &info))
169 if (info.dli_fname) file = info.dli_fname;
170 if (info.dli_sname) symbol = info.dli_sname;
173 fprintf (stderr, "Error: callback on \"%s\" dangling %s(%s)[%p]\n",
174 p_var->psz_name, file, symbol, addr);
178 free( p_var->psz_name );
179 free( p_var->psz_text );
180 free( p_var->value_callbacks.p_entries );
186 * Initialize a vlc variable
188 * We hash the given string and insert it into the sorted list. The insertion
189 * may require slow memory copies, but think about what we gain in the log(n)
190 * lookup phase when setting/getting the variable value!
192 * \param p_this The object in which to create the variable
193 * \param psz_name The name of the variable
194 * \param i_type The variables type. Must be one of \ref var_type combined with
195 * zero or more \ref var_flags
197 int var_Create( vlc_object_t *p_this, const char *psz_name, int i_type )
201 variable_t *p_var = calloc( 1, sizeof( *p_var ) );
205 p_var->psz_name = strdup( psz_name );
206 p_var->psz_text = NULL;
208 p_var->i_type = i_type & ~VLC_VAR_DOINHERIT;
212 p_var->i_default = -1;
213 p_var->choices.i_count = 0;
214 p_var->choices.p_values = NULL;
215 p_var->choices_text.i_count = 0;
216 p_var->choices_text.p_values = NULL;
218 p_var->b_incallback = false;
219 p_var->value_callbacks = (callback_table_t){ 0 };
221 /* Always initialize the variable, even if it is a list variable; this
222 * will lead to errors if the variable is not initialized, but it will
223 * not cause crashes in the variable handling. */
224 switch( i_type & VLC_VAR_CLASS )
227 p_var->ops = &bool_ops;
228 p_var->val.b_bool = false;
230 case VLC_VAR_INTEGER:
231 p_var->ops = &int_ops;
232 p_var->val.i_int = 0;
235 p_var->ops = &string_ops;
236 p_var->val.psz_string = NULL;
239 p_var->ops = &float_ops;
240 p_var->val.f_float = 0.0;
243 p_var->ops = &time_ops;
244 p_var->val.i_time = 0;
247 p_var->ops = &coords_ops;
248 p_var->val.coords.x = p_var->val.coords.y = 0;
250 case VLC_VAR_ADDRESS:
251 p_var->ops = &addr_ops;
252 p_var->val.p_address = NULL;
255 p_var->ops = &void_ops;
261 if( (i_type & VLC_VAR_DOINHERIT)
262 && var_Inherit( p_this, psz_name, i_type, &p_var->val ) == 0 )
264 if( i_type & VLC_VAR_HASCHOICE )
266 /* We must add the inherited value to our choice list */
267 p_var->i_default = 0;
269 INSERT_ELEM( p_var->choices.p_values, p_var->choices.i_count,
271 INSERT_ELEM( p_var->choices_text.p_values,
272 p_var->choices_text.i_count, 0, p_var->val );
273 p_var->ops->pf_dup( &p_var->choices.p_values[0] );
274 p_var->choices_text.p_values[0].psz_string = NULL;
278 vlc_object_internals_t *p_priv = vlc_internals( p_this );
279 variable_t **pp_var, *p_oldvar;
280 int ret = VLC_SUCCESS;
282 vlc_mutex_lock( &p_priv->var_lock );
284 pp_var = tsearch( p_var, &p_priv->var_root, varcmp );
285 if( unlikely(pp_var == NULL) )
287 else if( (p_oldvar = *pp_var) == p_var ) /* Variable create */
288 p_var = NULL; /* Variable created */
289 else /* Variable already exists */
291 assert (((i_type ^ p_oldvar->i_type) & VLC_VAR_CLASS) == 0);
293 p_oldvar->i_type |= i_type & (VLC_VAR_ISCOMMAND|VLC_VAR_HASCHOICE);
295 vlc_mutex_unlock( &p_priv->var_lock );
297 /* If we did not need to create a new variable, free everything... */
305 * Destroy a vlc variable
307 * Look for the variable and destroy it if it is found. As in var_Create we
308 * do a call to memmove() but we have performance counterparts elsewhere.
310 * \param p_this The object that holds the variable
311 * \param psz_name The name of the variable
313 int var_Destroy( vlc_object_t *p_this, const char *psz_name )
319 vlc_object_internals_t *p_priv = vlc_internals( p_this );
321 vlc_mutex_lock( &p_priv->var_lock );
323 p_var = Lookup( p_this, psz_name );
326 vlc_mutex_unlock( &p_priv->var_lock );
330 WaitUnused( p_this, p_var );
332 if( --p_var->i_usage == 0 )
333 tdelete( p_var, &p_priv->var_root, varcmp );
336 vlc_mutex_unlock( &p_priv->var_lock );
343 static void CleanupVar( void *var )
348 void var_DestroyAll( vlc_object_t *obj )
350 vlc_object_internals_t *priv = vlc_internals( obj );
352 tdestroy( priv->var_root, CleanupVar );
353 priv->var_root = NULL;
358 * Perform an action on a variable
360 * \param p_this The object that holds the variable
361 * \param psz_name The name of the variable
362 * \param i_action The action to perform. Must be one of \ref var_action
363 * \param p_val First action parameter
364 * \param p_val2 Second action parameter
366 int var_Change( vlc_object_t *p_this, const char *psz_name,
367 int i_action, vlc_value_t *p_val, vlc_value_t *p_val2 )
369 int ret = VLC_SUCCESS;
376 vlc_object_internals_t *p_priv = vlc_internals( p_this );
378 vlc_mutex_lock( &p_priv->var_lock );
380 p_var = Lookup( p_this, psz_name );
383 vlc_mutex_unlock( &p_priv->var_lock );
390 if( p_var->i_type & VLC_VAR_HASMIN )
392 p_var->ops->pf_free( &p_var->min );
394 p_var->i_type |= VLC_VAR_HASMIN;
396 p_var->ops->pf_dup( &p_var->min );
397 CheckValue( p_var, &p_var->val );
400 if( p_var->i_type & VLC_VAR_HASMIN )
406 if( p_var->i_type & VLC_VAR_HASMAX )
408 p_var->ops->pf_free( &p_var->max );
410 p_var->i_type |= VLC_VAR_HASMAX;
412 p_var->ops->pf_dup( &p_var->max );
413 CheckValue( p_var, &p_var->val );
416 if( p_var->i_type & VLC_VAR_HASMAX )
421 case VLC_VAR_SETSTEP:
422 if( p_var->i_type & VLC_VAR_HASSTEP )
424 p_var->ops->pf_free( &p_var->step );
426 p_var->i_type |= VLC_VAR_HASSTEP;
427 p_var->step = *p_val;
428 p_var->ops->pf_dup( &p_var->step );
429 CheckValue( p_var, &p_var->val );
431 case VLC_VAR_GETSTEP:
432 if( p_var->i_type & VLC_VAR_HASSTEP )
433 *p_val = p_var->step;
437 case VLC_VAR_ADDCHOICE:
439 int i = p_var->choices.i_count;
441 INSERT_ELEM( p_var->choices.p_values, p_var->choices.i_count,
443 INSERT_ELEM( p_var->choices_text.p_values,
444 p_var->choices_text.i_count, i, *p_val );
445 p_var->ops->pf_dup( &p_var->choices.p_values[i] );
446 p_var->choices_text.p_values[i].psz_string =
447 ( p_val2 && p_val2->psz_string ) ?
448 strdup( p_val2->psz_string ) : NULL;
450 CheckValue( p_var, &p_var->val );
452 TriggerListCallback(p_this, p_var, psz_name, VLC_VAR_ADDCHOICE, p_val);
455 case VLC_VAR_DELCHOICE:
459 for( i = 0 ; i < p_var->choices.i_count ; i++ )
460 if( p_var->ops->pf_cmp( p_var->choices.p_values[i], *p_val ) == 0 )
463 if( i == p_var->choices.i_count )
466 vlc_mutex_unlock( &p_priv->var_lock );
470 if( p_var->i_default > i )
472 else if( p_var->i_default == i )
473 p_var->i_default = -1;
475 p_var->ops->pf_free( &p_var->choices.p_values[i] );
476 free( p_var->choices_text.p_values[i].psz_string );
477 REMOVE_ELEM( p_var->choices.p_values, p_var->choices.i_count, i );
478 REMOVE_ELEM( p_var->choices_text.p_values,
479 p_var->choices_text.i_count, i );
481 CheckValue( p_var, &p_var->val );
483 TriggerListCallback(p_this, p_var, psz_name, VLC_VAR_DELCHOICE, p_val);
486 case VLC_VAR_CHOICESCOUNT:
487 p_val->i_int = p_var->choices.i_count;
489 case VLC_VAR_CLEARCHOICES:
490 for( int i = 0 ; i < p_var->choices.i_count ; i++ )
491 p_var->ops->pf_free( &p_var->choices.p_values[i] );
492 for( int i = 0 ; i < p_var->choices_text.i_count ; i++ )
493 free( p_var->choices_text.p_values[i].psz_string );
495 if( p_var->choices.i_count ) free( p_var->choices.p_values );
496 if( p_var->choices_text.i_count ) free( p_var->choices_text.p_values );
498 p_var->choices.i_count = 0;
499 p_var->choices.p_values = NULL;
500 p_var->choices_text.i_count = 0;
501 p_var->choices_text.p_values = NULL;
502 p_var->i_default = -1;
503 TriggerListCallback(p_this, p_var, psz_name, VLC_VAR_CLEARCHOICES, NULL);
505 case VLC_VAR_SETDEFAULT:
508 /* FIXME: the list is sorted, dude. Use something cleverer. */
509 for( i = 0 ; i < p_var->choices.i_count ; i++ )
510 if( p_var->ops->pf_cmp( p_var->choices.p_values[i], *p_val ) == 0 )
513 if( i == p_var->choices.i_count )
517 p_var->i_default = i;
518 CheckValue( p_var, &p_var->val );
521 case VLC_VAR_SETVALUE:
522 /* Duplicate data if needed */
524 p_var->ops->pf_dup( &newval );
525 /* Backup needed stuff */
527 /* Check boundaries and list */
528 CheckValue( p_var, &newval );
529 /* Set the variable */
531 /* Free data if needed */
532 p_var->ops->pf_free( &oldval );
534 case VLC_VAR_GETCHOICES:
535 p_val->p_list = xmalloc( sizeof(vlc_list_t) );
536 p_val->p_list->p_values =
537 xmalloc( p_var->choices.i_count * sizeof(vlc_value_t) );
538 p_val->p_list->i_type = p_var->i_type;
539 p_val->p_list->i_count = p_var->choices.i_count;
542 p_val2->p_list = xmalloc( sizeof(vlc_list_t) );
543 p_val2->p_list->p_values =
544 xmalloc( p_var->choices.i_count * sizeof(vlc_value_t) );
545 p_val2->p_list->i_type = VLC_VAR_STRING;
546 p_val2->p_list->i_count = p_var->choices.i_count;
548 for( int i = 0 ; i < p_var->choices.i_count ; i++ )
550 p_val->p_list->p_values[i] = p_var->choices.p_values[i];
551 p_var->ops->pf_dup( &p_val->p_list->p_values[i] );
554 p_val2->p_list->p_values[i].psz_string =
555 p_var->choices_text.p_values[i].psz_string ?
556 strdup(p_var->choices_text.p_values[i].psz_string) : NULL;
560 case VLC_VAR_SETTEXT:
561 free( p_var->psz_text );
562 if( p_val && p_val->psz_string )
563 p_var->psz_text = strdup( p_val->psz_string );
565 p_var->psz_text = NULL;
567 case VLC_VAR_GETTEXT:
568 p_val->psz_string = p_var->psz_text ? strdup( p_var->psz_text )
575 vlc_mutex_unlock( &p_priv->var_lock );
582 * Perform a Get and Set on a variable
584 * \param p_this: The object that hold the variable
585 * \param psz_name: the name of the variable
586 * \param i_action: the action to perform
587 * \param p_val: The action parameter
588 * \return vlc error codes
590 int var_GetAndSet( vlc_object_t *p_this, const char *psz_name, int i_action,
600 vlc_object_internals_t *p_priv = vlc_internals( p_this );
602 vlc_mutex_lock( &p_priv->var_lock );
603 p_var = Lookup( p_this, psz_name );
606 vlc_mutex_unlock( &p_priv->var_lock );
610 WaitUnused( p_this, p_var );
612 /* Duplicated data if needed */
613 //p_var->ops->pf_dup( &val );
615 /* Backup needed stuff */
618 /* depending of the action requiered */
621 case VLC_VAR_BOOL_TOGGLE:
622 assert( ( p_var->i_type & VLC_VAR_BOOL ) == VLC_VAR_BOOL );
623 p_var->val.b_bool = !p_var->val.b_bool;
625 case VLC_VAR_INTEGER_ADD:
626 assert( ( p_var->i_type & VLC_VAR_INTEGER ) == VLC_VAR_INTEGER );
627 p_var->val.i_int += p_val->i_int;
629 case VLC_VAR_INTEGER_OR:
630 assert( ( p_var->i_type & VLC_VAR_INTEGER ) == VLC_VAR_INTEGER );
631 p_var->val.i_int |= p_val->i_int;
633 case VLC_VAR_INTEGER_NAND:
634 assert( ( p_var->i_type & VLC_VAR_INTEGER ) == VLC_VAR_INTEGER );
635 p_var->val.i_int &= ~p_val->i_int;
638 vlc_mutex_unlock( &p_priv->var_lock );
642 /* Check boundaries */
643 CheckValue( p_var, &p_var->val );
646 /* Deal with callbacks.*/
647 i_ret = TriggerCallback( p_this, p_var, psz_name, oldval );
649 vlc_mutex_unlock( &p_priv->var_lock );
656 * Request a variable's type
658 * \return The variable type if it exists, or 0 if the
659 * variable could not be found.
662 int var_Type( vlc_object_t *p_this, const char *psz_name )
669 vlc_object_internals_t *p_priv = vlc_internals( p_this );
671 vlc_mutex_lock( &p_priv->var_lock );
673 p_var = Lookup( p_this, psz_name );
676 i_type = p_var->i_type;
677 if( p_var->choices.i_count > 0 )
678 i_type |= VLC_VAR_HASCHOICE;
680 vlc_mutex_unlock( &p_priv->var_lock );
685 #undef var_SetChecked
686 int var_SetChecked( vlc_object_t *p_this, const char *psz_name,
687 int expected_type, vlc_value_t val )
689 int i_ret = VLC_SUCCESS;
695 vlc_object_internals_t *p_priv = vlc_internals( p_this );
697 vlc_mutex_lock( &p_priv->var_lock );
699 p_var = Lookup( p_this, psz_name );
702 vlc_mutex_unlock( &p_priv->var_lock );
706 assert( expected_type == 0 ||
707 (p_var->i_type & VLC_VAR_CLASS) == expected_type );
708 assert ((p_var->i_type & VLC_VAR_CLASS) != VLC_VAR_VOID);
710 WaitUnused( p_this, p_var );
712 /* Duplicate data if needed */
713 p_var->ops->pf_dup( &val );
715 /* Backup needed stuff */
718 /* Check boundaries and list */
719 CheckValue( p_var, &val );
721 /* Set the variable */
724 /* Deal with callbacks */
725 i_ret = TriggerCallback( p_this, p_var, psz_name, oldval );
727 /* Free data if needed */
728 p_var->ops->pf_free( &oldval );
730 vlc_mutex_unlock( &p_priv->var_lock );
737 * Set a variable's value
739 * \param p_this The object that hold the variable
740 * \param psz_name The name of the variable
741 * \param val the value to set
743 int var_Set( vlc_object_t *p_this, const char *psz_name, vlc_value_t val )
745 return var_SetChecked( p_this, psz_name, 0, val );
748 #undef var_GetChecked
749 int var_GetChecked( vlc_object_t *p_this, const char *psz_name,
750 int expected_type, vlc_value_t *p_val )
754 vlc_object_internals_t *p_priv = vlc_internals( p_this );
756 int err = VLC_SUCCESS;
758 vlc_mutex_lock( &p_priv->var_lock );
760 p_var = Lookup( p_this, psz_name );
763 assert( expected_type == 0 ||
764 (p_var->i_type & VLC_VAR_CLASS) == expected_type );
765 assert ((p_var->i_type & VLC_VAR_CLASS) != VLC_VAR_VOID);
767 /* Really get the variable */
770 /* Duplicate value if needed */
771 p_var->ops->pf_dup( p_val );
776 vlc_mutex_unlock( &p_priv->var_lock );
782 * Get a variable's value
784 * \param p_this The object that holds the variable
785 * \param psz_name The name of the variable
786 * \param p_val Pointer to a vlc_value_t that will hold the variable's value
787 * after the function is finished
789 int var_Get( vlc_object_t *p_this, const char *psz_name, vlc_value_t *p_val )
791 return var_GetChecked( p_this, psz_name, 0, p_val );
794 static int AddCallback( vlc_object_t *p_this, const char *psz_name,
795 callback_entry_t entry, vlc_callback_type_t i_type )
801 vlc_object_internals_t *p_priv = vlc_internals( p_this );
803 vlc_mutex_lock( &p_priv->var_lock );
805 p_var = Lookup( p_this, psz_name );
808 vlc_mutex_unlock( &p_priv->var_lock );
809 msg_Err( p_this, "cannot add callback %p to nonexistent "
810 "variable '%s'", entry.u.p_callback, psz_name );
814 WaitUnused( p_this, p_var );
816 callback_table_t *p_table;
817 if (i_type == vlc_value_callback)
818 p_table = &p_var->value_callbacks;
820 p_table = &p_var->list_callbacks;
821 INSERT_ELEM( p_table->p_entries,
826 vlc_mutex_unlock( &p_priv->var_lock );
831 #undef var_AddCallback
833 * Register a callback in a variable
835 * We store a function pointer that will be called upon variable
838 * \param p_this The object that holds the variable
839 * \param psz_name The name of the variable
840 * \param pf_callback The function pointer
841 * \param p_data A generic pointer that will be passed as the last
842 * argument to the callback function.
844 * \warning The callback function is run in the thread that calls var_Set on
845 * the variable. Use proper locking. This thread may not have much
846 * time to spare, so keep callback functions short.
848 int var_AddCallback( vlc_object_t *p_this, const char *psz_name,
849 vlc_callback_t pf_callback, void *p_data )
851 callback_entry_t entry;
852 entry.u.pf_value_callback = pf_callback;
853 entry.p_data = p_data;
855 return AddCallback(p_this, psz_name, entry, vlc_value_callback);
858 static int DelCallback( vlc_object_t *p_this, const char *psz_name,
859 callback_entry_t entry, vlc_callback_type_t i_type )
864 bool b_found_similar = false;
869 vlc_object_internals_t *p_priv = vlc_internals( p_this );
871 vlc_mutex_lock( &p_priv->var_lock );
873 p_var = Lookup( p_this, psz_name );
876 vlc_mutex_unlock( &p_priv->var_lock );
880 WaitUnused( p_this, p_var );
882 callback_table_t *p_table;
883 if (i_type == vlc_value_callback)
884 p_table = &p_var->value_callbacks;
886 p_table = &p_var->list_callbacks;
888 for( i_entry = p_table->i_entries ; i_entry-- ; )
890 if( p_table->p_entries[i_entry].u.p_callback == entry.u.p_callback
891 && p_table->p_entries[i_entry].p_data == entry.p_data )
896 else if( p_table->p_entries[i_entry].u.p_callback == entry.u.p_callback )
897 b_found_similar = true;
904 if( b_found_similar )
905 fprintf( stderr, "Calling var_DelCallback for '%s' with the same "
906 "function but not the same data.", psz_name );
909 vlc_mutex_unlock( &p_priv->var_lock );
913 REMOVE_ELEM( p_table->p_entries, p_table->i_entries, i_entry );
915 vlc_mutex_unlock( &p_priv->var_lock );
920 #undef var_DelCallback
922 * Remove a callback from a variable
924 * pf_callback and p_data have to be given again, because different objects
925 * might have registered the same callback function.
927 int var_DelCallback( vlc_object_t *p_this, const char *psz_name,
928 vlc_callback_t pf_callback, void *p_data )
930 callback_entry_t entry;
931 entry.u.pf_value_callback = pf_callback;
932 entry.p_data = p_data;
934 return DelCallback(p_this, psz_name, entry, vlc_value_callback);
937 #undef var_TriggerCallback
939 * Trigger callback on a variable
941 * \param p_this The object that hold the variable
942 * \param psz_name The name of the variable
944 int var_TriggerCallback( vlc_object_t *p_this, const char *psz_name )
951 vlc_object_internals_t *p_priv = vlc_internals( p_this );
953 vlc_mutex_lock( &p_priv->var_lock );
955 p_var = Lookup( p_this, psz_name );
958 vlc_mutex_unlock( &p_priv->var_lock );
962 WaitUnused( p_this, p_var );
964 /* Deal with callbacks. Tell we're in a callback, release the lock,
965 * call stored functions, retake the lock. */
966 i_ret = TriggerCallback( p_this, p_var, psz_name, p_var->val );
968 vlc_mutex_unlock( &p_priv->var_lock );
972 #undef var_AddListCallback
974 * Register a callback for a list variable
976 * The callback is triggered when an element is added/removed from the
977 * list or when the list is cleared.
979 * See var_AddCallback().
981 int var_AddListCallback( vlc_object_t *p_this, const char *psz_name,
982 vlc_list_callback_t pf_callback, void *p_data )
984 callback_entry_t entry;
985 entry.u.pf_list_callback = pf_callback;
986 entry.p_data = p_data;
988 return AddCallback(p_this, psz_name, entry, vlc_list_callback);
991 #undef var_DelListCallback
993 * Remove a callback from a list variable
995 * See var_DelCallback().
997 int var_DelListCallback( vlc_object_t *p_this, const char *psz_name,
998 vlc_list_callback_t pf_callback, void *p_data )
1000 callback_entry_t entry;
1001 entry.u.pf_list_callback = pf_callback;
1002 entry.p_data = p_data;
1004 return DelCallback(p_this, psz_name, entry, vlc_list_callback);
1007 /** Parse a stringified option
1008 * This function parse a string option and create the associated object
1010 * The option must be of the form "[no[-]]foo[=bar]" where foo is the
1011 * option name and bar is the value of the option.
1012 * \param p_obj the object in which the variable must be created
1013 * \param psz_option the option to parse
1014 * \param trusted whether the option is set by a trusted input or not
1017 void var_OptionParse( vlc_object_t *p_obj, const char *psz_option,
1020 char *psz_name, *psz_value;
1022 bool b_isno = false;
1025 val.psz_string = NULL;
1027 /* It's too much of a hassle to remove the ':' when we parse
1028 * the cmd line :) */
1029 if( psz_option[0] == ':' )
1032 if( !psz_option[0] )
1035 psz_name = strdup( psz_option );
1036 if( psz_name == NULL )
1039 psz_value = strchr( psz_name, '=' );
1040 if( psz_value != NULL )
1041 *psz_value++ = '\0';
1043 i_type = config_GetType( p_obj, psz_name );
1044 if( !i_type && !psz_value )
1046 /* check for "no-foo" or "nofoo" */
1047 if( !strncmp( psz_name, "no-", 3 ) )
1049 memmove( psz_name, psz_name + 3, strlen(psz_name) + 1 - 3 );
1051 else if( !strncmp( psz_name, "no", 2 ) )
1053 memmove( psz_name, psz_name + 2, strlen(psz_name) + 1 - 2 );
1055 else goto cleanup; /* Option doesn't exist */
1058 i_type = config_GetType( p_obj, psz_name );
1060 if( !i_type ) goto cleanup; /* Option doesn't exist */
1062 if( ( i_type != VLC_VAR_BOOL ) &&
1063 ( !psz_value || !*psz_value ) ) goto cleanup; /* Invalid value */
1065 /* check if option is unsafe */
1066 if( !trusted && !config_IsSafe( psz_name ) )
1068 msg_Err( p_obj, "unsafe option \"%s\" has been ignored for "
1069 "security reasons", psz_name );
1074 /* Create the variable in the input object.
1075 * Children of the input object will be able to retreive this value
1076 * thanks to the inheritance property of the object variables. */
1077 var_Create( p_obj, psz_name, i_type );
1082 val.b_bool = !b_isno;
1085 case VLC_VAR_INTEGER:
1086 val.i_int = strtoll( psz_value, NULL, 0 );
1090 val.f_float = us_atof( psz_value );
1093 case VLC_VAR_STRING:
1094 val.psz_string = psz_value;
1101 var_Set( p_obj, psz_name, val );
1107 #undef var_LocationParse
1109 * Parses a set of colon-separated or semicolon-separated
1110 * <variable name>=<value> pairs.
1111 * Some access (or access_demux) plugins uses this scheme
1112 * in media resource location.
1113 * @note Only trusted/safe variables are allowed. This is intended.
1115 * @warning Only use this for plugins implementing VLC-specific resource
1116 * location schemes. This would not make any sense for standardized ones.
1118 * @param obj VLC object on which to set variables (and emit error messages)
1119 * @param mrl string to parse
1120 * @param pref prefix to prepend to option names in the string
1122 * @return VLC_ENOMEM on error, VLC_SUCCESS on success.
1124 int var_LocationParse (vlc_object_t *obj, const char *mrl, const char *pref)
1126 int ret = VLC_SUCCESS;
1127 size_t preflen = strlen (pref) + 1;
1129 assert(mrl != NULL);
1130 while (*mrl != '\0')
1132 mrl += strspn (mrl, ":;"); /* skip leading colon(s) */
1134 size_t len = strcspn (mrl, ":;");
1135 char *buf = malloc (preflen + len);
1137 if (likely(buf != NULL))
1139 /* NOTE: this does not support the "no-<varname>" bool syntax. */
1140 /* DO NOT use asprintf() here; it won't work! Think again. */
1141 snprintf (buf, preflen + len, "%s%s", pref, mrl);
1142 var_OptionParse (obj, buf, false);
1154 * Waits until the variable is inactive (i.e. not executing a callback)
1156 static void WaitUnused( vlc_object_t *p_this, variable_t *p_var )
1158 vlc_object_internals_t *p_priv = vlc_internals( p_this );
1160 mutex_cleanup_push( &p_priv->var_lock );
1161 while( p_var->b_incallback )
1162 vlc_cond_wait( &p_priv->var_wait, &p_priv->var_lock );
1166 /*****************************************************************************
1167 * CheckValue: check that a value is valid wrt. a variable
1168 *****************************************************************************
1169 * This function checks p_val's value against p_var's limitations such as
1170 * minimal and maximal value, step, in-list position, and modifies p_val if
1172 ****************************************************************************/
1173 static void CheckValue ( variable_t *p_var, vlc_value_t *p_val )
1175 /* Check that our variable is in the list */
1176 if( p_var->i_type & VLC_VAR_HASCHOICE && p_var->choices.i_count )
1180 /* This list is not sorted so go throug it (this is a small list) */
1181 for( i = p_var->choices.i_count ; i-- ; )
1183 if( p_var->ops->pf_cmp( *p_val, p_var->choices.p_values[i] ) == 0 )
1189 /* If not found, change it to anything vaguely valid */
1192 /* Free the old variable, get the new one, dup it */
1193 p_var->ops->pf_free( p_val );
1194 *p_val = p_var->choices.p_values[p_var->i_default >= 0
1195 ? p_var->i_default : 0 ];
1196 p_var->ops->pf_dup( p_val );
1200 /* Check that our variable is within the bounds */
1201 switch( p_var->i_type & VLC_VAR_TYPE )
1203 case VLC_VAR_INTEGER:
1204 if( p_var->i_type & VLC_VAR_HASSTEP && p_var->step.i_int
1205 && (p_val->i_int % p_var->step.i_int) )
1207 p_val->i_int = (p_val->i_int + (p_var->step.i_int / 2))
1208 / p_var->step.i_int * p_var->step.i_int;
1210 if( p_var->i_type & VLC_VAR_HASMIN
1211 && p_val->i_int < p_var->min.i_int )
1213 p_val->i_int = p_var->min.i_int;
1215 if( p_var->i_type & VLC_VAR_HASMAX
1216 && p_val->i_int > p_var->max.i_int )
1218 p_val->i_int = p_var->max.i_int;
1222 if( p_var->i_type & VLC_VAR_HASSTEP && p_var->step.f_float )
1224 float f_round = p_var->step.f_float
1225 * roundf( p_val->f_float / p_var->step.f_float );
1226 p_val->f_float = f_round;
1228 if( p_var->i_type & VLC_VAR_HASMIN
1229 && p_val->f_float < p_var->min.f_float )
1231 p_val->f_float = p_var->min.f_float;
1233 if( p_var->i_type & VLC_VAR_HASMAX
1234 && p_val->f_float > p_var->max.f_float )
1236 p_val->f_float = p_var->max.f_float;
1246 * Finds the value of a variable. If the specified object does not hold a
1247 * variable with the specified name, try the parent object, and iterate until
1248 * the top of the tree. If no match is found, the value is read from the
1251 int var_Inherit( vlc_object_t *p_this, const char *psz_name, int i_type,
1252 vlc_value_t *p_val )
1254 i_type &= VLC_VAR_CLASS;
1255 for( vlc_object_t *obj = p_this; obj != NULL; obj = obj->p_parent )
1257 if( var_GetChecked( obj, psz_name, i_type, p_val ) == VLC_SUCCESS )
1261 /* else take value from config */
1262 switch( i_type & VLC_VAR_CLASS )
1264 case VLC_VAR_STRING:
1265 p_val->psz_string = config_GetPsz( p_this, psz_name );
1266 if( !p_val->psz_string ) p_val->psz_string = strdup("");
1269 p_val->f_float = config_GetFloat( p_this, psz_name );
1271 case VLC_VAR_INTEGER:
1272 p_val->i_int = config_GetInt( p_this, psz_name );
1275 p_val->b_bool = config_GetInt( p_this, psz_name );
1279 case VLC_VAR_ADDRESS:
1287 * It inherits a string as an unsigned rational number (it also accepts basic
1290 * It returns an error if the rational number cannot be parsed (0/0 is valid).
1291 * The rational is already reduced.
1293 int (var_InheritURational)(vlc_object_t *object,
1294 unsigned *num, unsigned *den,
1302 char *tmp = var_InheritString(object, var);
1307 unsigned n = strtol(tmp, &next, 0);
1308 unsigned d = strtol(*next ? &next[1] : "0", NULL, 0);
1311 /* Interpret as a (finite positive) float number */
1312 const int ubits = CHAR_BIT * sizeof (unsigned);
1314 double f = frexp(us_atof(tmp), &exp);
1316 if (!isgreaterequal(f, 0.))
1317 goto error; /* negative or not a number */
1319 if (exp <= 1 - ubits) {
1320 n = 0; /* too small */
1322 } else if (exp <= 0) {
1323 n = floor(scalbn(f, ubits - 1 + exp));
1324 #if (FLT_RADIX != 2)
1325 # error Floating point configuration not supported.
1327 d = 1u << (ubits - 1);
1328 } else if (exp <= ubits) {
1329 n = floor(scalbn(f, ubits));
1330 d = 1u << (ubits - exp);
1332 goto error; /* too big */
1333 } else if ( *next == '\0' ) {
1334 /* plain integer given */
1340 vlc_ureduce(num, den, n, d, 0);
1347 return VLC_EGENERIC;
1350 /**********************************************************************
1351 * Trigger the callbacks.
1352 * Tell we're in a callback, release the lock, call stored functions,
1354 **********************************************************************/
1355 static int TriggerCallback( vlc_object_t *p_this, variable_t *p_var,
1356 const char *psz_name, vlc_value_t oldval )
1360 callback_table_t *p_table = &p_var->value_callbacks;
1361 int i_entries = p_table->i_entries;
1362 if( i_entries == 0 )
1365 callback_entry_t *p_entries = p_table->p_entries;
1366 vlc_object_internals_t *p_priv = vlc_internals( p_this );
1368 assert( !p_var->b_incallback );
1369 p_var->b_incallback = true;
1370 vlc_mutex_unlock( &p_priv->var_lock );
1372 /* The real calls */
1373 for( ; i_entries-- ; )
1375 p_entries[i_entries].u.pf_value_callback( p_this, psz_name, oldval, p_var->val,
1376 p_entries[i_entries].p_data );
1379 vlc_mutex_lock( &p_priv->var_lock );
1380 p_var->b_incallback = false;
1381 vlc_cond_broadcast( &p_priv->var_wait );
1386 static int TriggerListCallback( vlc_object_t *p_this, variable_t *p_var,
1387 const char *psz_name, int i_action,
1392 callback_table_t *p_table = &p_var->list_callbacks;
1393 int i_entries = p_table->i_entries;
1394 if( i_entries == 0 )
1397 callback_entry_t *p_entries = p_table->p_entries;
1398 vlc_object_internals_t *p_priv = vlc_internals( p_this );
1400 assert( !p_var->b_incallback );
1401 p_var->b_incallback = true;
1402 vlc_mutex_unlock( &p_priv->var_lock );
1404 for( ; i_entries-- ; )
1406 p_entries[i_entries].u.pf_list_callback( p_this, psz_name, i_action, val,
1407 p_entries[i_entries].p_data );
1410 vlc_mutex_lock( &p_priv->var_lock );
1411 p_var->b_incallback = false;
1412 vlc_cond_broadcast( &p_priv->var_wait );
1418 * Free a list and the associated strings
1419 * @param p_val: the list variable
1420 * @param p_val2: the variable associated or NULL
1422 void var_FreeList( vlc_value_t *p_val, vlc_value_t *p_val2 )
1424 switch( p_val->p_list->i_type & VLC_VAR_CLASS )
1426 case VLC_VAR_STRING:
1427 for( int i = 0; i < p_val->p_list->i_count; i++ )
1428 free( p_val->p_list->p_values[i].psz_string );
1432 free( p_val->p_list->p_values );
1433 free( p_val->p_list );
1435 if( p_val2 != NULL )
1437 assert( p_val2->p_list != NULL );
1438 assert( p_val2->p_list->i_type == VLC_VAR_STRING );
1440 for( int i = 0; i < p_val2->p_list->i_count; i++ )
1441 free( p_val2->p_list->p_values[i].psz_string );
1442 free( p_val2->p_list->p_values );
1443 free( p_val2->p_list );