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 /*****************************************************************************
42 *****************************************************************************/
43 struct callback_entry_t
45 vlc_callback_t pf_callback;
49 /*****************************************************************************
50 * Local comparison functions, returns 0 if v == w, < 0 if v < w, > 0 if v > w
51 *****************************************************************************/
52 static int CmpBool( vlc_value_t v, vlc_value_t w ) { return v.b_bool ? w.b_bool ? 0 : 1 : w.b_bool ? -1 : 0; }
53 static int CmpInt( vlc_value_t v, vlc_value_t w ) { return v.i_int == w.i_int ? 0 : v.i_int > w.i_int ? 1 : -1; }
54 static int CmpTime( vlc_value_t v, vlc_value_t w )
56 return v.i_time == w.i_time ? 0 : v.i_time > w.i_time ? 1 : -1;
58 static int CmpString( vlc_value_t v, vlc_value_t w )
61 return !w.psz_string ? 0 : -1;
63 return !w.psz_string ? 1 : strcmp( v.psz_string, w.psz_string );
65 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; }
66 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; }
68 /*****************************************************************************
69 * Local duplication functions, and local deallocation functions
70 *****************************************************************************/
71 static void DupDummy( vlc_value_t *p_val ) { (void)p_val; /* unused */ }
72 static void DupString( vlc_value_t *p_val )
74 p_val->psz_string = strdup( p_val->psz_string ? p_val->psz_string : "" );
77 static void DupList( vlc_value_t *p_val )
80 vlc_list_t *p_list = malloc( sizeof(vlc_list_t) );
82 p_list->i_count = p_val->p_list->i_count;
83 if( p_val->p_list->i_count )
85 p_list->p_values = malloc( p_list->i_count * sizeof(vlc_value_t) );
86 p_list->pi_types = malloc( p_list->i_count * sizeof(int) );
90 p_list->p_values = NULL;
91 p_list->pi_types = NULL;
94 for( i = 0; i < p_list->i_count; i++ )
96 p_list->p_values[i] = p_val->p_list->p_values[i];
97 p_list->pi_types[i] = p_val->p_list->pi_types[i];
98 switch( p_val->p_list->pi_types[i] & VLC_VAR_CLASS )
102 DupString( &p_list->p_values[i] );
109 p_val->p_list = p_list;
112 static void FreeDummy( vlc_value_t *p_val ) { (void)p_val; /* unused */ }
113 static void FreeString( vlc_value_t *p_val ) { free( p_val->psz_string ); }
114 static void FreeMutex( vlc_value_t *p_val ) { vlc_mutex_destroy( (vlc_mutex_t*)p_val->p_address ); free( p_val->p_address ); }
116 static void FreeList( vlc_value_t *p_val )
119 for( i = 0; i < p_val->p_list->i_count; i++ )
121 switch( p_val->p_list->pi_types[i] & VLC_VAR_CLASS )
124 FreeString( &p_val->p_list->p_values[i] );
127 FreeMutex( &p_val->p_list->p_values[i] );
134 if( p_val->p_list->i_count )
136 free( p_val->p_list->p_values );
137 free( p_val->p_list->pi_types );
139 free( p_val->p_list );
142 static const struct variable_ops_t
143 void_ops = { NULL, DupDummy, FreeDummy, },
144 addr_ops = { CmpAddress, DupDummy, FreeDummy, },
145 bool_ops = { CmpBool, DupDummy, FreeDummy, },
146 float_ops = { CmpFloat, DupDummy, FreeDummy, },
147 int_ops = { CmpInt, DupDummy, FreeDummy, },
148 list_ops = { CmpAddress, DupList, FreeList, },
149 mutex_ops = { CmpAddress, DupDummy, FreeMutex, },
150 string_ops = { CmpString, DupString, FreeString, },
151 time_ops = { CmpTime, DupDummy, FreeDummy, };
153 /*****************************************************************************
155 *****************************************************************************/
156 static void WaitUnused ( vlc_object_t *, variable_t * );
158 static void CheckValue ( variable_t *, vlc_value_t * );
160 static int TriggerCallback( vlc_object_t *, variable_t *, const char *,
163 static int varcmp( const void *a, const void *b )
165 const variable_t *va = a, *vb = b;
167 /* psz_name must be first */
168 assert( va == (const void *)&va->psz_name );
169 return strcmp( va->psz_name, vb->psz_name );
172 static variable_t *Lookup( vlc_object_t *obj, const char *psz_name )
174 vlc_object_internals_t *priv = vlc_internals( obj );
177 vlc_assert_locked( &priv->var_lock );
178 pp_var = tfind( &psz_name, &priv->var_root, varcmp );
179 return (pp_var != NULL) ? *pp_var : NULL;
182 static void Destroy( variable_t *p_var )
184 p_var->ops->pf_free( &p_var->val );
185 if( p_var->choices.i_count )
187 for( int i = 0 ; i < p_var->choices.i_count ; i++ )
189 p_var->ops->pf_free( &p_var->choices.p_values[i] );
190 free( p_var->choices_text.p_values[i].psz_string );
192 free( p_var->choices.p_values );
193 free( p_var->choices_text.p_values );
195 free( p_var->psz_name );
196 free( p_var->psz_text );
197 free( p_var->p_entries );
202 * Initialize a vlc variable
204 * We hash the given string and insert it into the sorted list. The insertion
205 * may require slow memory copies, but think about what we gain in the log(n)
206 * lookup phase when setting/getting the variable value!
208 * \param p_this The object in which to create the variable
209 * \param psz_name The name of the variable
210 * \param i_type The variables type. Must be one of \ref var_type combined with
211 * zero or more \ref var_flags
213 int __var_Create( vlc_object_t *p_this, const char *psz_name, int i_type )
215 static vlc_list_t dummy_null_list = {0, NULL, NULL};
218 variable_t *p_var = calloc( 1, sizeof( *p_var ) );
222 p_var->psz_name = strdup( psz_name );
223 p_var->psz_text = NULL;
225 p_var->i_type = i_type & ~VLC_VAR_DOINHERIT;
229 p_var->i_default = -1;
230 p_var->choices.i_count = 0;
231 p_var->choices.p_values = NULL;
232 p_var->choices_text.i_count = 0;
233 p_var->choices_text.p_values = NULL;
235 p_var->b_incallback = false;
236 p_var->i_entries = 0;
237 p_var->p_entries = NULL;
239 /* Always initialize the variable, even if it is a list variable; this
240 * will lead to errors if the variable is not initialized, but it will
241 * not cause crashes in the variable handling. */
242 switch( i_type & VLC_VAR_CLASS )
245 p_var->ops = &bool_ops;
246 p_var->val.b_bool = false;
248 case VLC_VAR_INTEGER:
249 p_var->ops = &int_ops;
250 p_var->val.i_int = 0;
253 p_var->ops = &string_ops;
254 p_var->val.psz_string = NULL;
257 p_var->ops = &float_ops;
258 p_var->val.f_float = 0.0;
261 p_var->ops = &time_ops;
262 p_var->val.i_time = 0;
264 case VLC_VAR_ADDRESS:
265 p_var->ops = &addr_ops;
266 p_var->val.p_address = NULL;
269 p_var->ops = &mutex_ops;
270 p_var->val.p_address = malloc( sizeof(vlc_mutex_t) );
271 vlc_mutex_init( (vlc_mutex_t*)p_var->val.p_address );
274 p_var->ops = &list_ops;
275 p_var->val.p_list = &dummy_null_list;
278 p_var->ops = &void_ops;
280 if( i_type & VLC_VAR_CLASS != VLC_VAR_VOID )
281 msg_Err( p_this, "Creating the variable '%s' without a type",
286 if( i_type & VLC_VAR_DOINHERIT )
288 if( var_Inherit( p_this, psz_name, i_type, &p_var->val ) )
289 msg_Err( p_this, "cannot inherit value for %s", psz_name );
290 else if( i_type & VLC_VAR_HASCHOICE )
292 /* We must add the inherited value to our choice list */
293 p_var->i_default = 0;
295 INSERT_ELEM( p_var->choices.p_values, p_var->choices.i_count,
297 INSERT_ELEM( p_var->choices_text.p_values,
298 p_var->choices_text.i_count, 0, p_var->val );
299 p_var->ops->pf_dup( &p_var->choices.p_values[0] );
300 p_var->choices_text.p_values[0].psz_string = NULL;
304 vlc_object_internals_t *p_priv = vlc_internals( p_this );
305 variable_t **pp_var, *p_oldvar;
306 int ret = VLC_SUCCESS;
308 vlc_mutex_lock( &p_priv->var_lock );
310 pp_var = tsearch( p_var, &p_priv->var_root, varcmp );
311 if( unlikely(pp_var == NULL) )
313 else if( (p_oldvar = *pp_var) == p_var )
315 else if( unlikely((i_type ^ p_oldvar->i_type) & VLC_VAR_CLASS) )
316 { /* If the types differ, variable creation failed. */
317 msg_Err( p_this, "Variable '%s' (0x%04x) already exist "
318 "but with a different type (0x%04x)",
319 psz_name, p_oldvar->i_type, i_type );
325 p_oldvar->i_type |= i_type & (VLC_VAR_ISCOMMAND|VLC_VAR_HASCHOICE);
327 vlc_mutex_unlock( &p_priv->var_lock );
329 /* If we did not need to create a new variable, free everything... */
336 * Destroy a vlc variable
338 * Look for the variable and destroy it if it is found. As in var_Create we
339 * do a call to memmove() but we have performance counterparts elsewhere.
341 * \param p_this The object that holds the variable
342 * \param psz_name The name of the variable
344 int __var_Destroy( vlc_object_t *p_this, const char *psz_name )
350 vlc_object_internals_t *p_priv = vlc_internals( p_this );
352 vlc_mutex_lock( &p_priv->var_lock );
354 p_var = Lookup( p_this, psz_name );
357 vlc_mutex_unlock( &p_priv->var_lock );
361 WaitUnused( p_this, p_var );
363 if( --p_var->i_usage == 0 )
364 tdelete( p_var, &p_priv->var_root, varcmp );
367 vlc_mutex_unlock( &p_priv->var_lock );
374 static void CleanupVar( void *var )
379 void var_DestroyAll( vlc_object_t *obj )
381 vlc_object_internals_t *priv = vlc_internals( obj );
383 tdestroy( priv->var_root, CleanupVar );
387 * Perform an action on a variable
389 * \param p_this The object that holds the variable
390 * \param psz_name The name of the variable
391 * \param i_action The action to perform. Must be one of \ref var_action
392 * \param p_val First action parameter
393 * \param p_val2 Second action parameter
395 int __var_Change( vlc_object_t *p_this, const char *psz_name,
396 int i_action, vlc_value_t *p_val, vlc_value_t *p_val2 )
405 vlc_object_internals_t *p_priv = vlc_internals( p_this );
407 vlc_mutex_lock( &p_priv->var_lock );
409 p_var = Lookup( p_this, psz_name );
412 vlc_mutex_unlock( &p_priv->var_lock );
419 if( p_var->i_type & VLC_VAR_HASMIN )
421 p_var->ops->pf_free( &p_var->min );
423 p_var->i_type |= VLC_VAR_HASMIN;
425 p_var->ops->pf_dup( &p_var->min );
426 CheckValue( p_var, &p_var->val );
429 if( p_var->i_type & VLC_VAR_HASMIN )
435 if( p_var->i_type & VLC_VAR_HASMAX )
437 p_var->ops->pf_free( &p_var->max );
439 p_var->i_type |= VLC_VAR_HASMAX;
441 p_var->ops->pf_dup( &p_var->max );
442 CheckValue( p_var, &p_var->val );
445 if( p_var->i_type & VLC_VAR_HASMAX )
450 case VLC_VAR_SETSTEP:
451 if( p_var->i_type & VLC_VAR_HASSTEP )
453 p_var->ops->pf_free( &p_var->step );
455 p_var->i_type |= VLC_VAR_HASSTEP;
456 p_var->step = *p_val;
457 p_var->ops->pf_dup( &p_var->step );
458 CheckValue( p_var, &p_var->val );
460 case VLC_VAR_GETSTEP:
461 if( p_var->i_type & VLC_VAR_HASSTEP )
463 *p_val = p_var->step;
466 case VLC_VAR_ADDCHOICE:
467 i = p_var->choices.i_count;
469 INSERT_ELEM( p_var->choices.p_values, p_var->choices.i_count,
471 INSERT_ELEM( p_var->choices_text.p_values,
472 p_var->choices_text.i_count, i, *p_val );
473 p_var->ops->pf_dup( &p_var->choices.p_values[i] );
474 p_var->choices_text.p_values[i].psz_string =
475 ( p_val2 && p_val2->psz_string ) ?
476 strdup( p_val2->psz_string ) : NULL;
478 CheckValue( p_var, &p_var->val );
480 case VLC_VAR_DELCHOICE:
481 for( i = 0 ; i < p_var->choices.i_count ; i++ )
483 if( p_var->ops->pf_cmp( p_var->choices.p_values[i], *p_val ) == 0 )
489 if( i == p_var->choices.i_count )
492 vlc_mutex_unlock( &p_priv->var_lock );
496 if( p_var->i_default > i )
500 else if( p_var->i_default == i )
502 p_var->i_default = -1;
505 p_var->ops->pf_free( &p_var->choices.p_values[i] );
506 free( p_var->choices_text.p_values[i].psz_string );
507 REMOVE_ELEM( p_var->choices.p_values, p_var->choices.i_count, i );
508 REMOVE_ELEM( p_var->choices_text.p_values,
509 p_var->choices_text.i_count, i );
511 CheckValue( p_var, &p_var->val );
513 case VLC_VAR_CHOICESCOUNT:
514 p_val->i_int = p_var->choices.i_count;
516 case VLC_VAR_CLEARCHOICES:
517 for( i = 0 ; i < p_var->choices.i_count ; i++ )
519 p_var->ops->pf_free( &p_var->choices.p_values[i] );
521 for( i = 0 ; i < p_var->choices_text.i_count ; i++ )
522 free( p_var->choices_text.p_values[i].psz_string );
524 if( p_var->choices.i_count ) free( p_var->choices.p_values );
525 if( p_var->choices_text.i_count ) free( p_var->choices_text.p_values );
527 p_var->choices.i_count = 0;
528 p_var->choices.p_values = NULL;
529 p_var->choices_text.i_count = 0;
530 p_var->choices_text.p_values = NULL;
531 p_var->i_default = -1;
533 case VLC_VAR_SETDEFAULT:
534 /* FIXME: the list is sorted, dude. Use something cleverer. */
535 for( i = 0 ; i < p_var->choices.i_count ; i++ )
537 if( p_var->ops->pf_cmp( p_var->choices.p_values[i], *p_val ) == 0 )
543 if( i == p_var->choices.i_count )
549 p_var->i_default = i;
550 CheckValue( p_var, &p_var->val );
552 case VLC_VAR_SETVALUE:
553 /* Duplicate data if needed */
555 p_var->ops->pf_dup( &newval );
556 /* Backup needed stuff */
558 /* Check boundaries and list */
559 CheckValue( p_var, &newval );
560 /* Set the variable */
562 /* Free data if needed */
563 p_var->ops->pf_free( &oldval );
565 case VLC_VAR_GETCHOICES:
566 case VLC_VAR_GETLIST:
567 p_val->p_list = malloc( sizeof(vlc_list_t) );
568 if( p_val2 ) p_val2->p_list = malloc( sizeof(vlc_list_t) );
569 if( p_var->choices.i_count )
571 p_val->p_list->p_values = malloc( p_var->choices.i_count
572 * sizeof(vlc_value_t) );
573 p_val->p_list->pi_types = malloc( p_var->choices.i_count
577 p_val2->p_list->p_values =
578 malloc( p_var->choices.i_count * sizeof(vlc_value_t) );
579 p_val2->p_list->pi_types =
580 malloc( p_var->choices.i_count * sizeof(int) );
583 p_val->p_list->i_count = p_var->choices.i_count;
584 if( p_val2 ) p_val2->p_list->i_count = p_var->choices.i_count;
585 for( i = 0 ; i < p_var->choices.i_count ; i++ )
587 p_val->p_list->p_values[i] = p_var->choices.p_values[i];
588 p_val->p_list->pi_types[i] = p_var->i_type;
589 p_var->ops->pf_dup( &p_val->p_list->p_values[i] );
592 p_val2->p_list->p_values[i].psz_string =
593 p_var->choices_text.p_values[i].psz_string ?
594 strdup(p_var->choices_text.p_values[i].psz_string) : NULL;
595 p_val2->p_list->pi_types[i] = VLC_VAR_STRING;
599 case VLC_VAR_SETTEXT:
600 free( p_var->psz_text );
601 if( p_val && p_val->psz_string )
602 p_var->psz_text = strdup( p_val->psz_string );
604 p_var->psz_text = NULL;
606 case VLC_VAR_GETTEXT:
607 p_val->psz_string = p_var->psz_text ? strdup( p_var->psz_text )
610 case VLC_VAR_SETISCOMMAND:
611 p_var->i_type |= VLC_VAR_ISCOMMAND;
618 vlc_mutex_unlock( &p_priv->var_lock );
625 * Perform a Get and Set on a variable
627 * \param p_this: The object that hold the variable
628 * \param psz_name: the name of the variable
629 * \param i_action: the action to perform
630 * \param p_val: The action parameter
631 * \return vlc error codes
633 int __var_GetAndSet( vlc_object_t *p_this, const char *psz_name, int i_action,
642 vlc_object_internals_t *p_priv = vlc_internals( p_this );
644 vlc_mutex_lock( &p_priv->var_lock );
645 p_var = Lookup( p_this, psz_name );
648 vlc_mutex_unlock( &p_priv->var_lock );
652 WaitUnused( p_this, p_var );
654 /* Duplicated data if needed */
655 //p_var->ops->pf_dup( &val );
657 /* Backup needed stuff */
660 /* depending of the action requiered */
663 case VLC_VAR_TOGGLE_BOOL:
664 assert( ( p_var->i_type & VLC_VAR_BOOL ) == VLC_VAR_BOOL );
665 p_var->val.b_bool = !p_var->val.b_bool;
667 case VLC_VAR_INTEGER_INCDEC:
668 assert( ( p_var->i_type & VLC_VAR_INTEGER ) == VLC_VAR_INTEGER );
669 p_var->val.i_int += val.i_int;
672 vlc_mutex_unlock( &p_priv->var_lock );
676 /* Check boundaries */
677 CheckValue( p_var, &p_var->val );
679 /* Deal with callbacks.*/
680 i_ret = TriggerCallback( p_this, p_var, psz_name, oldval );
682 vlc_mutex_unlock( &p_priv->var_lock );
689 * Request a variable's type
691 * \return The variable type if it exists, or 0 if the
692 * variable could not be found.
695 int __var_Type( vlc_object_t *p_this, const char *psz_name )
702 vlc_object_internals_t *p_priv = vlc_internals( p_this );
704 vlc_mutex_lock( &p_priv->var_lock );
706 p_var = Lookup( p_this, psz_name );
708 i_type = p_var->i_type;
710 vlc_mutex_unlock( &p_priv->var_lock );
715 int var_SetChecked( vlc_object_t *p_this, const char *psz_name,
716 int expected_type, vlc_value_t val )
718 int i_ret = VLC_SUCCESS;
724 vlc_object_internals_t *p_priv = vlc_internals( p_this );
726 vlc_mutex_lock( &p_priv->var_lock );
728 p_var = Lookup( p_this, psz_name );
731 vlc_mutex_unlock( &p_priv->var_lock );
735 assert( expected_type == 0 ||
736 (p_var->i_type & VLC_VAR_CLASS) == expected_type );
738 WaitUnused( p_this, p_var );
740 /* Duplicate data if needed */
741 p_var->ops->pf_dup( &val );
743 /* Backup needed stuff */
746 /* Check boundaries and list */
747 CheckValue( p_var, &val );
749 /* Set the variable */
752 /* Deal with callbacks */
753 i_ret = TriggerCallback( p_this, p_var, psz_name, oldval );
755 /* Free data if needed */
756 p_var->ops->pf_free( &oldval );
758 vlc_mutex_unlock( &p_priv->var_lock );
765 * Set a variable's value
767 * \param p_this The object that hold the variable
768 * \param psz_name The name of the variable
769 * \param val the value to set
771 int __var_Set( vlc_object_t *p_this, const char *psz_name, vlc_value_t val )
773 return var_SetChecked( p_this, psz_name, 0, val );
776 int var_GetChecked( vlc_object_t *p_this, const char *psz_name,
777 int expected_type, vlc_value_t *p_val )
781 vlc_object_internals_t *p_priv = vlc_internals( p_this );
783 int err = VLC_SUCCESS;
785 vlc_mutex_lock( &p_priv->var_lock );
787 p_var = Lookup( p_this, psz_name );
790 assert( expected_type == 0 ||
791 (p_var->i_type & VLC_VAR_CLASS) == expected_type );
793 /* Really get the variable */
797 /* Alert if the type is VLC_VAR_VOID */
798 if( ( p_var->i_type & VLC_VAR_TYPE ) == VLC_VAR_VOID )
799 msg_Warn( p_this, "Calling var_GetVoid on the void variable '%s' (0x%04x)", psz_name, p_var->i_type );
802 /* Duplicate value if needed */
803 p_var->ops->pf_dup( p_val );
808 vlc_mutex_unlock( &p_priv->var_lock );
813 * Get a variable's value
815 * \param p_this The object that holds the variable
816 * \param psz_name The name of the variable
817 * \param p_val Pointer to a vlc_value_t that will hold the variable's value
818 * after the function is finished
820 int __var_Get( vlc_object_t *p_this, const char *psz_name, vlc_value_t *p_val )
822 return var_GetChecked( p_this, psz_name, 0, p_val );
826 * Register a callback in a variable
828 * We store a function pointer that will be called upon variable
831 * \param p_this The object that holds the variable
832 * \param psz_name The name of the variable
833 * \param pf_callback The function pointer
834 * \param p_data A generic pointer that will be passed as the last
835 * argument to the callback function.
837 * \warning The callback function is run in the thread that calls var_Set on
838 * the variable. Use proper locking. This thread may not have much
839 * time to spare, so keep callback functions short.
841 int __var_AddCallback( vlc_object_t *p_this, const char *psz_name,
842 vlc_callback_t pf_callback, void *p_data )
845 callback_entry_t entry;
849 vlc_object_internals_t *p_priv = vlc_internals( p_this );
851 entry.pf_callback = pf_callback;
852 entry.p_data = p_data;
854 vlc_mutex_lock( &p_priv->var_lock );
856 p_var = Lookup( p_this, psz_name );
860 msg_Warn( p_this, "Failed to add a callback to the non-existing "
861 "variable '%s'", psz_name );
863 vlc_mutex_unlock( &p_priv->var_lock );
867 WaitUnused( p_this, p_var );
868 INSERT_ELEM( p_var->p_entries,
873 vlc_mutex_unlock( &p_priv->var_lock );
879 * Remove a callback from a variable
881 * pf_callback and p_data have to be given again, because different objects
882 * might have registered the same callback function.
884 int __var_DelCallback( vlc_object_t *p_this, const char *psz_name,
885 vlc_callback_t pf_callback, void *p_data )
890 bool b_found_similar = false;
895 vlc_object_internals_t *p_priv = vlc_internals( p_this );
897 vlc_mutex_lock( &p_priv->var_lock );
899 p_var = Lookup( p_this, psz_name );
902 vlc_mutex_unlock( &p_priv->var_lock );
906 WaitUnused( p_this, p_var );
908 for( i_entry = p_var->i_entries ; i_entry-- ; )
910 if( p_var->p_entries[i_entry].pf_callback == pf_callback
911 && p_var->p_entries[i_entry].p_data == p_data )
916 else if( p_var->p_entries[i_entry].pf_callback == pf_callback )
917 b_found_similar = true;
924 if( b_found_similar )
925 fprintf( stderr, "Calling var_DelCallback for '%s' with the same "
926 "function but not the same data.", psz_name );
929 vlc_mutex_unlock( &p_priv->var_lock );
933 REMOVE_ELEM( p_var->p_entries, p_var->i_entries, i_entry );
935 vlc_mutex_unlock( &p_priv->var_lock );
941 * Trigger callback on a variable
943 * \param p_this The object that hold the variable
944 * \param psz_name The name of the variable
946 int __var_TriggerCallback( vlc_object_t *p_this, const char *psz_name )
953 vlc_object_internals_t *p_priv = vlc_internals( p_this );
955 vlc_mutex_lock( &p_priv->var_lock );
957 p_var = Lookup( p_this, psz_name );
960 vlc_mutex_unlock( &p_priv->var_lock );
964 WaitUnused( p_this, p_var );
966 /* Deal with callbacks. Tell we're in a callback, release the lock,
967 * call stored functions, retake the lock. */
968 i_ret = TriggerCallback( p_this, p_var, psz_name, p_var->val );
970 vlc_mutex_unlock( &p_priv->var_lock );
974 /** Parse a stringified option
975 * This function parse a string option and create the associated object
977 * The option must be of the form "[no[-]]foo[=bar]" where foo is the
978 * option name and bar is the value of the option.
979 * \param p_obj the object in which the variable must be created
980 * \param psz_option the option to parse
981 * \param trusted whether the option is set by a trusted input or not
984 void var_OptionParse( vlc_object_t *p_obj, const char *psz_option,
987 char *psz_name, *psz_value;
992 val.psz_string = NULL;
994 /* It's too much of a hassle to remove the ':' when we parse
996 if( psz_option[0] == ':' )
1002 psz_name = strdup( psz_option );
1003 if( psz_name == NULL )
1006 psz_value = strchr( psz_name, '=' );
1007 if( psz_value != NULL )
1008 *psz_value++ = '\0';
1010 /* FIXME: :programs should be handled generically */
1011 if( !strcmp( psz_name, "programs" ) )
1012 i_type = VLC_VAR_LIST;
1014 i_type = config_GetType( p_obj, psz_name );
1016 if( !i_type && !psz_value )
1018 /* check for "no-foo" or "nofoo" */
1019 if( !strncmp( psz_name, "no-", 3 ) )
1021 memmove( psz_name, psz_name + 3, strlen(psz_name) + 1 - 3 );
1023 else if( !strncmp( psz_name, "no", 2 ) )
1025 memmove( psz_name, psz_name + 2, strlen(psz_name) + 1 - 2 );
1027 else goto cleanup; /* Option doesn't exist */
1030 i_type = config_GetType( p_obj, psz_name );
1032 if( !i_type ) goto cleanup; /* Option doesn't exist */
1034 if( ( i_type != VLC_VAR_BOOL ) &&
1035 ( !psz_value || !*psz_value ) ) goto cleanup; /* Invalid value */
1037 /* check if option is unsafe */
1040 module_config_t *p_config = config_FindConfig( p_obj, psz_name );
1041 if( !p_config || !p_config->b_safe )
1043 msg_Err( p_obj, "unsafe option \"%s\" has been ignored for "
1044 "security reasons", psz_name );
1050 /* Create the variable in the input object.
1051 * Children of the input object will be able to retreive this value
1052 * thanks to the inheritance property of the object variables. */
1053 __var_Create( p_obj, psz_name, i_type );
1058 val.b_bool = !b_isno;
1061 case VLC_VAR_INTEGER:
1062 val.i_int = strtol( psz_value, NULL, 0 );
1066 val.f_float = us_atof( psz_value );
1069 case VLC_VAR_STRING:
1070 case VLC_VAR_MODULE:
1072 case VLC_VAR_DIRECTORY:
1073 val.psz_string = psz_value;
1078 char *psz_orig, *psz_var;
1079 vlc_list_t *p_list = malloc(sizeof(vlc_list_t));
1080 val.p_list = p_list;
1081 p_list->i_count = 0;
1083 psz_var = psz_orig = strdup(psz_value);
1084 while( psz_var && *psz_var )
1086 char *psz_item = psz_var;
1088 while( *psz_var && *psz_var != ',' ) psz_var++;
1089 if( *psz_var == ',' )
1094 val2.i_int = strtol( psz_item, NULL, 0 );
1095 INSERT_ELEM( p_list->p_values, p_list->i_count,
1096 p_list->i_count, val2 );
1097 /* p_list->i_count is incremented twice by INSERT_ELEM */
1099 INSERT_ELEM( p_list->pi_types, p_list->i_count,
1100 p_list->i_count, VLC_VAR_INTEGER );
1110 __var_Set( p_obj, psz_name, val );
1112 /* If that's a list, remove all elements allocated */
1113 if( i_type == VLC_VAR_LIST )
1121 /* Following functions are local */
1124 * Waits until the variable is inactive (i.e. not executing a callback)
1126 static void WaitUnused( vlc_object_t *p_this, variable_t *p_var )
1128 vlc_object_internals_t *p_priv = vlc_internals( p_this );
1130 mutex_cleanup_push( &p_priv->var_lock );
1131 while( p_var->b_incallback )
1132 vlc_cond_wait( &p_priv->var_wait, &p_priv->var_lock );
1136 /*****************************************************************************
1137 * CheckValue: check that a value is valid wrt. a variable
1138 *****************************************************************************
1139 * This function checks p_val's value against p_var's limitations such as
1140 * minimal and maximal value, step, in-list position, and modifies p_val if
1142 ****************************************************************************/
1143 static void CheckValue ( variable_t *p_var, vlc_value_t *p_val )
1145 /* Check that our variable is in the list */
1146 if( p_var->i_type & VLC_VAR_HASCHOICE && p_var->choices.i_count )
1150 /* This list is not sorted so go throug it (this is a small list) */
1151 for( i = p_var->choices.i_count ; i-- ; )
1153 if( p_var->ops->pf_cmp( *p_val, p_var->choices.p_values[i] ) == 0 )
1159 /* If not found, change it to anything vaguely valid */
1162 /* Free the old variable, get the new one, dup it */
1163 p_var->ops->pf_free( p_val );
1164 *p_val = p_var->choices.p_values[p_var->i_default >= 0
1165 ? p_var->i_default : 0 ];
1166 p_var->ops->pf_dup( p_val );
1170 /* Check that our variable is within the bounds */
1171 switch( p_var->i_type & VLC_VAR_TYPE )
1173 case VLC_VAR_INTEGER:
1174 if( p_var->i_type & VLC_VAR_HASSTEP && p_var->step.i_int
1175 && (p_val->i_int % p_var->step.i_int) )
1177 p_val->i_int = (p_val->i_int + (p_var->step.i_int / 2))
1178 / p_var->step.i_int * p_var->step.i_int;
1180 if( p_var->i_type & VLC_VAR_HASMIN
1181 && p_val->i_int < p_var->min.i_int )
1183 p_val->i_int = p_var->min.i_int;
1185 if( p_var->i_type & VLC_VAR_HASMAX
1186 && p_val->i_int > p_var->max.i_int )
1188 p_val->i_int = p_var->max.i_int;
1192 if( p_var->i_type & VLC_VAR_HASSTEP && p_var->step.f_float )
1194 float f_round = p_var->step.f_float * (float)(int)( 0.5 +
1195 p_val->f_float / p_var->step.f_float );
1196 if( p_val->f_float != f_round )
1198 p_val->f_float = f_round;
1201 if( p_var->i_type & VLC_VAR_HASMIN
1202 && p_val->f_float < p_var->min.f_float )
1204 p_val->f_float = p_var->min.f_float;
1206 if( p_var->i_type & VLC_VAR_HASMAX
1207 && p_val->f_float > p_var->max.f_float )
1209 p_val->f_float = p_var->max.f_float;
1219 * Finds the value of a variable. If the specified object does not hold a
1220 * variable with the specified name, try the parent object, and iterate until
1221 * the top of the tree. If no match is found, the value is read from the
1224 int var_Inherit( vlc_object_t *p_this, const char *psz_name, int i_type,
1225 vlc_value_t *p_val )
1228 if (p_this != VLC_OBJECT(p_this->p_libvlc)
1229 && unlikely(p_this->p_parent == NULL))
1231 msg_Info (p_this, "%s(%s) on detached object", __func__, psz_name);
1235 i_type &= VLC_VAR_CLASS;
1236 for( vlc_object_t *obj = p_this; obj != NULL; obj = obj->p_parent )
1238 if( var_GetChecked( obj, psz_name, i_type, p_val ) == VLC_SUCCESS )
1241 if (obj != p_this && obj != VLC_OBJECT(p_this->p_libvlc)
1242 && unlikely(obj->p_parent == NULL))
1244 msg_Info (p_this, "%s(%s) on detached tree [%p] %s", __func__,
1245 psz_name, obj, obj->psz_object_type);
1251 /* else take value from config */
1252 switch( i_type & VLC_VAR_CLASS )
1254 case VLC_VAR_STRING:
1255 p_val->psz_string = config_GetPsz( p_this, psz_name );
1256 if( !p_val->psz_string ) p_val->psz_string = strdup("");
1259 p_val->f_float = config_GetFloat( p_this, psz_name );
1261 case VLC_VAR_INTEGER:
1262 p_val->i_int = config_GetInt( p_this, psz_name );
1265 p_val->b_bool = config_GetInt( p_this, psz_name );
1269 char *psz_orig, *psz_var;
1270 vlc_list_t *p_list = malloc(sizeof(vlc_list_t));
1271 p_val->p_list = p_list;
1272 p_list->i_count = 0;
1274 psz_var = psz_orig = config_GetPsz( p_this, psz_name );
1275 while( psz_var && *psz_var )
1277 char *psz_item = psz_var;
1279 while( *psz_var && *psz_var != ',' ) psz_var++;
1280 if( *psz_var == ',' )
1285 val.i_int = strtol( psz_item, NULL, 0 );
1286 INSERT_ELEM( p_list->p_values, p_list->i_count,
1287 p_list->i_count, val );
1288 /* p_list->i_count is incremented twice by INSERT_ELEM */
1290 INSERT_ELEM( p_list->pi_types, p_list->i_count,
1291 p_list->i_count, VLC_VAR_INTEGER );
1297 msg_Warn( p_this, "Could not inherit value for var %s "
1298 "from config. Invalid Type", psz_name );
1301 /*msg_Dbg( p_this, "Inherited value for var %s from config", psz_name );*/
1306 /**********************************************************************
1307 * Trigger the callbacks.
1308 * Tell we're in a callback, release the lock, call stored functions,
1310 **********************************************************************/
1311 static int TriggerCallback( vlc_object_t *p_this, variable_t *p_var,
1312 const char *psz_name, vlc_value_t oldval )
1316 int i_entries = p_var->i_entries;
1317 if( i_entries == 0 )
1320 callback_entry_t *p_entries = p_var->p_entries;
1321 vlc_object_internals_t *p_priv = vlc_internals( p_this );
1323 assert( !p_var->b_incallback );
1324 p_var->b_incallback = true;
1325 vlc_mutex_unlock( &p_priv->var_lock );
1327 /* The real calls */
1328 for( ; i_entries-- ; )
1330 p_entries[i_entries].pf_callback( p_this, psz_name, oldval, p_var->val,
1331 p_entries[i_entries].p_data );
1334 vlc_mutex_lock( &p_priv->var_lock );
1335 p_var->b_incallback = false;
1336 vlc_cond_broadcast( &p_priv->var_wait );
1342 /**********************************************************************
1343 * Execute a var command on an object identified by its name
1344 **********************************************************************/
1345 int __var_Command( vlc_object_t *p_this, const char *psz_name,
1346 const char *psz_cmd, const char *psz_arg, char **psz_msg )
1348 vlc_object_t *p_obj = vlc_object_find_name( p_this->p_libvlc,
1349 psz_name, FIND_CHILD );
1355 *psz_msg = strdup( "Unknown destination object." );
1359 i_type = var_Type( p_obj, psz_cmd );
1360 if( !( i_type&VLC_VAR_ISCOMMAND ) )
1362 vlc_object_release( p_obj );
1364 *psz_msg = strdup( "Variable doesn't exist or isn't a command." );
1365 return VLC_EGENERIC;
1368 i_type &= VLC_VAR_CLASS;
1371 case VLC_VAR_INTEGER:
1372 i_ret = var_SetInteger( p_obj, psz_cmd, atoi( psz_arg ) );
1375 i_ret = var_SetFloat( p_obj, psz_cmd, us_atof( psz_arg ) );
1377 case VLC_VAR_STRING:
1378 i_ret = var_SetString( p_obj, psz_cmd, psz_arg );
1381 i_ret = var_SetBool( p_obj, psz_cmd, atoi( psz_arg ) );
1384 i_ret = VLC_EGENERIC;
1388 vlc_object_release( p_obj );
1392 if( asprintf( psz_msg, "%s on object %s returned %i (%s)",
1393 psz_cmd, psz_name, i_ret, vlc_error( i_ret ) ) == -1)
1402 * Free a list and the associated strings
1403 * @param p_val: the list variable
1404 * @param p_val2: the variable associated or NULL
1406 void var_FreeList( vlc_value_t *p_val, vlc_value_t *p_val2 )
1409 if( p_val2 && p_val2->p_list )
1411 for( int i = 0; i < p_val2->p_list->i_count; i++ )
1412 free( p_val2->p_list->p_values[i].psz_string );
1413 if( p_val2->p_list->i_count )
1415 free( p_val2->p_list->p_values );
1416 free( p_val2->p_list->pi_types );
1418 free( p_val2->p_list );