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 )
54 return v.b_bool ? w.b_bool ? 0 : 1 : w.b_bool ? -1 : 0;
57 static int CmpInt( vlc_value_t v, vlc_value_t w )
59 return v.i_int == w.i_int ? 0 : v.i_int > w.i_int ? 1 : -1;
62 static int CmpTime( vlc_value_t v, vlc_value_t w )
64 return v.i_time == w.i_time ? 0 : v.i_time > w.i_time ? 1 : -1;
67 static int CmpString( vlc_value_t v, vlc_value_t w )
70 return !w.psz_string ? 0 : -1;
72 return !w.psz_string ? 1 : strcmp( v.psz_string, w.psz_string );
74 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; }
75 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; }
77 /*****************************************************************************
78 * Local duplication functions, and local deallocation functions
79 *****************************************************************************/
80 static void DupDummy( vlc_value_t *p_val ) { (void)p_val; /* unused */ }
81 static void DupString( vlc_value_t *p_val )
83 p_val->psz_string = strdup( p_val->psz_string ? p_val->psz_string : "" );
86 static void DupList( vlc_value_t *p_val )
89 vlc_list_t *p_list = malloc( sizeof(vlc_list_t) );
91 p_list->i_count = p_val->p_list->i_count;
92 if( p_val->p_list->i_count )
94 p_list->p_values = malloc( p_list->i_count * sizeof(vlc_value_t) );
95 p_list->pi_types = malloc( p_list->i_count * sizeof(int) );
99 p_list->p_values = NULL;
100 p_list->pi_types = NULL;
103 for( i = 0; i < p_list->i_count; i++ )
105 p_list->p_values[i] = p_val->p_list->p_values[i];
106 p_list->pi_types[i] = p_val->p_list->pi_types[i];
107 switch( p_val->p_list->pi_types[i] & VLC_VAR_CLASS )
111 DupString( &p_list->p_values[i] );
118 p_val->p_list = p_list;
121 static void FreeDummy( vlc_value_t *p_val ) { (void)p_val; /* unused */ }
122 static void FreeString( vlc_value_t *p_val ) { free( p_val->psz_string ); }
123 static void FreeMutex( vlc_value_t *p_val ) { vlc_mutex_destroy( (vlc_mutex_t*)p_val->p_address ); free( p_val->p_address ); }
125 static void FreeList( vlc_value_t *p_val )
128 for( i = 0; i < p_val->p_list->i_count; i++ )
130 switch( p_val->p_list->pi_types[i] & VLC_VAR_CLASS )
133 FreeString( &p_val->p_list->p_values[i] );
136 FreeMutex( &p_val->p_list->p_values[i] );
143 if( p_val->p_list->i_count )
145 free( p_val->p_list->p_values );
146 free( p_val->p_list->pi_types );
148 free( p_val->p_list );
151 static const struct variable_ops_t
152 void_ops = { NULL, DupDummy, FreeDummy, },
153 addr_ops = { CmpAddress, DupDummy, FreeDummy, },
154 bool_ops = { CmpBool, DupDummy, FreeDummy, },
155 float_ops = { CmpFloat, DupDummy, FreeDummy, },
156 int_ops = { CmpInt, DupDummy, FreeDummy, },
157 list_ops = { CmpAddress, DupList, FreeList, },
158 mutex_ops = { CmpAddress, DupDummy, FreeMutex, },
159 string_ops = { CmpString, DupString, FreeString, },
160 time_ops = { CmpTime, DupDummy, FreeDummy, },
161 coords_ops = { NULL, DupDummy, FreeDummy, };
163 /*****************************************************************************
165 *****************************************************************************/
166 static void WaitUnused ( vlc_object_t *, variable_t * );
168 static void CheckValue ( variable_t *, vlc_value_t * );
170 static int TriggerCallback( vlc_object_t *, variable_t *, const char *,
173 static int varcmp( const void *a, const void *b )
175 const variable_t *va = a, *vb = b;
177 /* psz_name must be first */
178 assert( va == (const void *)&va->psz_name );
179 return strcmp( va->psz_name, vb->psz_name );
182 static variable_t *Lookup( vlc_object_t *obj, const char *psz_name )
184 vlc_object_internals_t *priv = vlc_internals( obj );
187 vlc_assert_locked( &priv->var_lock );
188 pp_var = tfind( &psz_name, &priv->var_root, varcmp );
189 return (pp_var != NULL) ? *pp_var : NULL;
192 static void Destroy( variable_t *p_var )
194 p_var->ops->pf_free( &p_var->val );
195 if( p_var->choices.i_count )
197 for( int i = 0 ; i < p_var->choices.i_count ; i++ )
199 p_var->ops->pf_free( &p_var->choices.p_values[i] );
200 free( p_var->choices_text.p_values[i].psz_string );
202 free( p_var->choices.p_values );
203 free( p_var->choices_text.p_values );
205 free( p_var->psz_name );
206 free( p_var->psz_text );
207 free( p_var->p_entries );
213 * Initialize a vlc variable
215 * We hash the given string and insert it into the sorted list. The insertion
216 * may require slow memory copies, but think about what we gain in the log(n)
217 * lookup phase when setting/getting the variable value!
219 * \param p_this The object in which to create the variable
220 * \param psz_name The name of the variable
221 * \param i_type The variables type. Must be one of \ref var_type combined with
222 * zero or more \ref var_flags
224 int var_Create( vlc_object_t *p_this, const char *psz_name, int i_type )
226 static vlc_list_t dummy_null_list = {0, NULL, NULL};
229 variable_t *p_var = calloc( 1, sizeof( *p_var ) );
233 p_var->psz_name = strdup( psz_name );
234 p_var->psz_text = NULL;
236 p_var->i_type = i_type & ~VLC_VAR_DOINHERIT;
240 p_var->i_default = -1;
241 p_var->choices.i_count = 0;
242 p_var->choices.p_values = NULL;
243 p_var->choices_text.i_count = 0;
244 p_var->choices_text.p_values = NULL;
246 p_var->b_incallback = false;
247 p_var->i_entries = 0;
248 p_var->p_entries = NULL;
250 /* Always initialize the variable, even if it is a list variable; this
251 * will lead to errors if the variable is not initialized, but it will
252 * not cause crashes in the variable handling. */
253 switch( i_type & VLC_VAR_CLASS )
256 p_var->ops = &bool_ops;
257 p_var->val.b_bool = false;
259 case VLC_VAR_INTEGER:
260 p_var->ops = &int_ops;
261 p_var->val.i_int = 0;
264 p_var->ops = &string_ops;
265 p_var->val.psz_string = NULL;
268 p_var->ops = &float_ops;
269 p_var->val.f_float = 0.0;
272 p_var->ops = &time_ops;
273 p_var->val.i_time = 0;
276 p_var->ops = &coords_ops;
277 p_var->val.coords.x = p_var->val.coords.y = 0;
279 case VLC_VAR_ADDRESS:
280 p_var->ops = &addr_ops;
281 p_var->val.p_address = NULL;
284 p_var->ops = &mutex_ops;
285 p_var->val.p_address = malloc( sizeof(vlc_mutex_t) );
286 vlc_mutex_init( (vlc_mutex_t*)p_var->val.p_address );
289 p_var->ops = &list_ops;
290 p_var->val.p_list = &dummy_null_list;
293 p_var->ops = &void_ops;
295 if( (i_type & VLC_VAR_CLASS) != VLC_VAR_VOID )
296 msg_Err( p_this, "Creating the variable '%s' without a type",
301 if( i_type & VLC_VAR_DOINHERIT )
303 if( var_Inherit( p_this, psz_name, i_type, &p_var->val ) )
304 msg_Err( p_this, "cannot inherit value for %s", psz_name );
305 else if( i_type & VLC_VAR_HASCHOICE )
307 /* We must add the inherited value to our choice list */
308 p_var->i_default = 0;
310 INSERT_ELEM( p_var->choices.p_values, p_var->choices.i_count,
312 INSERT_ELEM( p_var->choices_text.p_values,
313 p_var->choices_text.i_count, 0, p_var->val );
314 p_var->ops->pf_dup( &p_var->choices.p_values[0] );
315 p_var->choices_text.p_values[0].psz_string = NULL;
319 vlc_object_internals_t *p_priv = vlc_internals( p_this );
320 variable_t **pp_var, *p_oldvar;
321 int ret = VLC_SUCCESS;
323 vlc_mutex_lock( &p_priv->var_lock );
325 pp_var = tsearch( p_var, &p_priv->var_root, varcmp );
326 if( unlikely(pp_var == NULL) )
328 else if( (p_oldvar = *pp_var) == p_var )
330 else if( unlikely((i_type ^ p_oldvar->i_type) & VLC_VAR_CLASS) )
331 { /* If the types differ, variable creation failed. */
332 msg_Err( p_this, "Variable '%s' (0x%04x) already exist "
333 "but with a different type (0x%04x)",
334 psz_name, p_oldvar->i_type, i_type );
340 p_oldvar->i_type |= i_type & (VLC_VAR_ISCOMMAND|VLC_VAR_HASCHOICE);
342 vlc_mutex_unlock( &p_priv->var_lock );
344 /* If we did not need to create a new variable, free everything... */
352 * Destroy a vlc variable
354 * Look for the variable and destroy it if it is found. As in var_Create we
355 * do a call to memmove() but we have performance counterparts elsewhere.
357 * \param p_this The object that holds the variable
358 * \param psz_name The name of the variable
360 int var_Destroy( vlc_object_t *p_this, const char *psz_name )
366 vlc_object_internals_t *p_priv = vlc_internals( p_this );
368 vlc_mutex_lock( &p_priv->var_lock );
370 p_var = Lookup( p_this, psz_name );
373 vlc_mutex_unlock( &p_priv->var_lock );
377 WaitUnused( p_this, p_var );
379 if( --p_var->i_usage == 0 )
380 tdelete( p_var, &p_priv->var_root, varcmp );
383 vlc_mutex_unlock( &p_priv->var_lock );
390 static void CleanupVar( void *var )
395 void var_DestroyAll( vlc_object_t *obj )
397 vlc_object_internals_t *priv = vlc_internals( obj );
399 tdestroy( priv->var_root, CleanupVar );
404 * Perform an action on a variable
406 * \param p_this The object that holds the variable
407 * \param psz_name The name of the variable
408 * \param i_action The action to perform. Must be one of \ref var_action
409 * \param p_val First action parameter
410 * \param p_val2 Second action parameter
412 int var_Change( vlc_object_t *p_this, const char *psz_name,
413 int i_action, vlc_value_t *p_val, vlc_value_t *p_val2 )
422 vlc_object_internals_t *p_priv = vlc_internals( p_this );
424 vlc_mutex_lock( &p_priv->var_lock );
426 p_var = Lookup( p_this, psz_name );
429 vlc_mutex_unlock( &p_priv->var_lock );
436 if( p_var->i_type & VLC_VAR_HASMIN )
438 p_var->ops->pf_free( &p_var->min );
440 p_var->i_type |= VLC_VAR_HASMIN;
442 p_var->ops->pf_dup( &p_var->min );
443 CheckValue( p_var, &p_var->val );
446 if( p_var->i_type & VLC_VAR_HASMIN )
452 if( p_var->i_type & VLC_VAR_HASMAX )
454 p_var->ops->pf_free( &p_var->max );
456 p_var->i_type |= VLC_VAR_HASMAX;
458 p_var->ops->pf_dup( &p_var->max );
459 CheckValue( p_var, &p_var->val );
462 if( p_var->i_type & VLC_VAR_HASMAX )
467 case VLC_VAR_SETSTEP:
468 if( p_var->i_type & VLC_VAR_HASSTEP )
470 p_var->ops->pf_free( &p_var->step );
472 p_var->i_type |= VLC_VAR_HASSTEP;
473 p_var->step = *p_val;
474 p_var->ops->pf_dup( &p_var->step );
475 CheckValue( p_var, &p_var->val );
477 case VLC_VAR_GETSTEP:
478 if( p_var->i_type & VLC_VAR_HASSTEP )
480 *p_val = p_var->step;
483 case VLC_VAR_ADDCHOICE:
484 i = p_var->choices.i_count;
486 INSERT_ELEM( p_var->choices.p_values, p_var->choices.i_count,
488 INSERT_ELEM( p_var->choices_text.p_values,
489 p_var->choices_text.i_count, i, *p_val );
490 p_var->ops->pf_dup( &p_var->choices.p_values[i] );
491 p_var->choices_text.p_values[i].psz_string =
492 ( p_val2 && p_val2->psz_string ) ?
493 strdup( p_val2->psz_string ) : NULL;
495 CheckValue( p_var, &p_var->val );
497 case VLC_VAR_DELCHOICE:
498 for( i = 0 ; i < p_var->choices.i_count ; i++ )
500 if( p_var->ops->pf_cmp( p_var->choices.p_values[i], *p_val ) == 0 )
506 if( i == p_var->choices.i_count )
509 vlc_mutex_unlock( &p_priv->var_lock );
513 if( p_var->i_default > i )
517 else if( p_var->i_default == i )
519 p_var->i_default = -1;
522 p_var->ops->pf_free( &p_var->choices.p_values[i] );
523 free( p_var->choices_text.p_values[i].psz_string );
524 REMOVE_ELEM( p_var->choices.p_values, p_var->choices.i_count, i );
525 REMOVE_ELEM( p_var->choices_text.p_values,
526 p_var->choices_text.i_count, i );
528 CheckValue( p_var, &p_var->val );
530 case VLC_VAR_CHOICESCOUNT:
531 p_val->i_int = p_var->choices.i_count;
533 case VLC_VAR_CLEARCHOICES:
534 for( i = 0 ; i < p_var->choices.i_count ; i++ )
536 p_var->ops->pf_free( &p_var->choices.p_values[i] );
538 for( i = 0 ; i < p_var->choices_text.i_count ; i++ )
539 free( p_var->choices_text.p_values[i].psz_string );
541 if( p_var->choices.i_count ) free( p_var->choices.p_values );
542 if( p_var->choices_text.i_count ) free( p_var->choices_text.p_values );
544 p_var->choices.i_count = 0;
545 p_var->choices.p_values = NULL;
546 p_var->choices_text.i_count = 0;
547 p_var->choices_text.p_values = NULL;
548 p_var->i_default = -1;
550 case VLC_VAR_SETDEFAULT:
551 /* FIXME: the list is sorted, dude. Use something cleverer. */
552 for( i = 0 ; i < p_var->choices.i_count ; i++ )
554 if( p_var->ops->pf_cmp( p_var->choices.p_values[i], *p_val ) == 0 )
560 if( i == p_var->choices.i_count )
566 p_var->i_default = i;
567 CheckValue( p_var, &p_var->val );
569 case VLC_VAR_SETVALUE:
570 /* Duplicate data if needed */
572 p_var->ops->pf_dup( &newval );
573 /* Backup needed stuff */
575 /* Check boundaries and list */
576 CheckValue( p_var, &newval );
577 /* Set the variable */
579 /* Free data if needed */
580 p_var->ops->pf_free( &oldval );
582 case VLC_VAR_GETCHOICES:
583 case VLC_VAR_GETLIST:
584 p_val->p_list = malloc( sizeof(vlc_list_t) );
585 if( p_val2 ) p_val2->p_list = malloc( sizeof(vlc_list_t) );
586 if( p_var->choices.i_count )
588 p_val->p_list->p_values = malloc( p_var->choices.i_count
589 * sizeof(vlc_value_t) );
590 p_val->p_list->pi_types = malloc( p_var->choices.i_count
594 p_val2->p_list->p_values =
595 malloc( p_var->choices.i_count * sizeof(vlc_value_t) );
596 p_val2->p_list->pi_types =
597 malloc( p_var->choices.i_count * sizeof(int) );
600 p_val->p_list->i_count = p_var->choices.i_count;
601 if( p_val2 ) p_val2->p_list->i_count = p_var->choices.i_count;
602 for( i = 0 ; i < p_var->choices.i_count ; i++ )
604 p_val->p_list->p_values[i] = p_var->choices.p_values[i];
605 p_val->p_list->pi_types[i] = p_var->i_type;
606 p_var->ops->pf_dup( &p_val->p_list->p_values[i] );
609 p_val2->p_list->p_values[i].psz_string =
610 p_var->choices_text.p_values[i].psz_string ?
611 strdup(p_var->choices_text.p_values[i].psz_string) : NULL;
612 p_val2->p_list->pi_types[i] = VLC_VAR_STRING;
616 case VLC_VAR_SETTEXT:
617 free( p_var->psz_text );
618 if( p_val && p_val->psz_string )
619 p_var->psz_text = strdup( p_val->psz_string );
621 p_var->psz_text = NULL;
623 case VLC_VAR_GETTEXT:
624 p_val->psz_string = p_var->psz_text ? strdup( p_var->psz_text )
627 case VLC_VAR_SETISCOMMAND:
628 p_var->i_type |= VLC_VAR_ISCOMMAND;
635 vlc_mutex_unlock( &p_priv->var_lock );
642 * Perform a Get and Set on a variable
644 * \param p_this: The object that hold the variable
645 * \param psz_name: the name of the variable
646 * \param i_action: the action to perform
647 * \param p_val: The action parameter
648 * \return vlc error codes
650 int var_GetAndSet( vlc_object_t *p_this, const char *psz_name, int i_action,
660 vlc_object_internals_t *p_priv = vlc_internals( p_this );
662 vlc_mutex_lock( &p_priv->var_lock );
663 p_var = Lookup( p_this, psz_name );
666 vlc_mutex_unlock( &p_priv->var_lock );
670 WaitUnused( p_this, p_var );
672 /* Duplicated data if needed */
673 //p_var->ops->pf_dup( &val );
675 /* Backup needed stuff */
678 /* depending of the action requiered */
681 case VLC_VAR_BOOL_TOGGLE:
682 assert( ( p_var->i_type & VLC_VAR_BOOL ) == VLC_VAR_BOOL );
683 p_var->val.b_bool = !p_var->val.b_bool;
685 case VLC_VAR_INTEGER_ADD:
686 assert( ( p_var->i_type & VLC_VAR_INTEGER ) == VLC_VAR_INTEGER );
687 p_var->val.i_int += p_val->i_int;
689 case VLC_VAR_INTEGER_OR:
690 assert( ( p_var->i_type & VLC_VAR_INTEGER ) == VLC_VAR_INTEGER );
691 p_var->val.i_int |= p_val->i_int;
693 case VLC_VAR_INTEGER_NAND:
694 assert( ( p_var->i_type & VLC_VAR_INTEGER ) == VLC_VAR_INTEGER );
695 p_var->val.i_int &= ~p_val->i_int;
698 vlc_mutex_unlock( &p_priv->var_lock );
702 /* Check boundaries */
703 CheckValue( p_var, &p_var->val );
706 /* Deal with callbacks.*/
707 i_ret = TriggerCallback( p_this, p_var, psz_name, oldval );
709 vlc_mutex_unlock( &p_priv->var_lock );
716 * Request a variable's type
718 * \return The variable type if it exists, or 0 if the
719 * variable could not be found.
722 int var_Type( vlc_object_t *p_this, const char *psz_name )
729 vlc_object_internals_t *p_priv = vlc_internals( p_this );
731 vlc_mutex_lock( &p_priv->var_lock );
733 p_var = Lookup( p_this, psz_name );
735 i_type = p_var->i_type;
737 vlc_mutex_unlock( &p_priv->var_lock );
742 #undef var_SetChecked
743 int var_SetChecked( vlc_object_t *p_this, const char *psz_name,
744 int expected_type, vlc_value_t val )
746 int i_ret = VLC_SUCCESS;
752 vlc_object_internals_t *p_priv = vlc_internals( p_this );
754 vlc_mutex_lock( &p_priv->var_lock );
756 p_var = Lookup( p_this, psz_name );
759 vlc_mutex_unlock( &p_priv->var_lock );
763 assert( expected_type == 0 ||
764 (p_var->i_type & VLC_VAR_CLASS) == expected_type );
766 /* Alert if the type is VLC_VAR_VOID */
767 if( ( p_var->i_type & VLC_VAR_TYPE ) == VLC_VAR_VOID )
768 msg_Warn( p_this, "Calling var_Set on the void variable '%s' (0x%04x)", psz_name, p_var->i_type );
772 WaitUnused( p_this, p_var );
774 /* Duplicate data if needed */
775 p_var->ops->pf_dup( &val );
777 /* Backup needed stuff */
780 /* Check boundaries and list */
781 CheckValue( p_var, &val );
783 /* Set the variable */
786 /* Deal with callbacks */
787 i_ret = TriggerCallback( p_this, p_var, psz_name, oldval );
789 /* Free data if needed */
790 p_var->ops->pf_free( &oldval );
792 vlc_mutex_unlock( &p_priv->var_lock );
799 * Set a variable's value
801 * \param p_this The object that hold the variable
802 * \param psz_name The name of the variable
803 * \param val the value to set
805 int var_Set( vlc_object_t *p_this, const char *psz_name, vlc_value_t val )
807 return var_SetChecked( p_this, psz_name, 0, val );
810 #undef var_GetChecked
811 int var_GetChecked( vlc_object_t *p_this, const char *psz_name,
812 int expected_type, vlc_value_t *p_val )
816 vlc_object_internals_t *p_priv = vlc_internals( p_this );
818 int err = VLC_SUCCESS;
820 vlc_mutex_lock( &p_priv->var_lock );
822 p_var = Lookup( p_this, psz_name );
825 assert( expected_type == 0 ||
826 (p_var->i_type & VLC_VAR_CLASS) == expected_type );
828 /* Really get the variable */
832 /* Alert if the type is VLC_VAR_VOID */
833 if( ( p_var->i_type & VLC_VAR_TYPE ) == VLC_VAR_VOID )
834 msg_Warn( p_this, "Calling var_Get on the void variable '%s' (0x%04x)", psz_name, p_var->i_type );
837 /* Duplicate value if needed */
838 p_var->ops->pf_dup( p_val );
843 vlc_mutex_unlock( &p_priv->var_lock );
849 * Get a variable's value
851 * \param p_this The object that holds the variable
852 * \param psz_name The name of the variable
853 * \param p_val Pointer to a vlc_value_t that will hold the variable's value
854 * after the function is finished
856 int var_Get( vlc_object_t *p_this, const char *psz_name, vlc_value_t *p_val )
858 return var_GetChecked( p_this, psz_name, 0, p_val );
861 #undef var_AddCallback
863 * Register a callback in a variable
865 * We store a function pointer that will be called upon variable
868 * \param p_this The object that holds the variable
869 * \param psz_name The name of the variable
870 * \param pf_callback The function pointer
871 * \param p_data A generic pointer that will be passed as the last
872 * argument to the callback function.
874 * \warning The callback function is run in the thread that calls var_Set on
875 * the variable. Use proper locking. This thread may not have much
876 * time to spare, so keep callback functions short.
878 int var_AddCallback( vlc_object_t *p_this, const char *psz_name,
879 vlc_callback_t pf_callback, void *p_data )
882 callback_entry_t entry;
886 vlc_object_internals_t *p_priv = vlc_internals( p_this );
888 entry.pf_callback = pf_callback;
889 entry.p_data = p_data;
891 vlc_mutex_lock( &p_priv->var_lock );
893 p_var = Lookup( p_this, psz_name );
897 msg_Warn( p_this, "Failed to add a callback to the non-existing "
898 "variable '%s'", psz_name );
900 vlc_mutex_unlock( &p_priv->var_lock );
904 WaitUnused( p_this, p_var );
905 INSERT_ELEM( p_var->p_entries,
910 vlc_mutex_unlock( &p_priv->var_lock );
915 #undef var_DelCallback
917 * Remove a callback from a variable
919 * pf_callback and p_data have to be given again, because different objects
920 * might have registered the same callback function.
922 int var_DelCallback( vlc_object_t *p_this, const char *psz_name,
923 vlc_callback_t pf_callback, void *p_data )
928 bool b_found_similar = false;
933 vlc_object_internals_t *p_priv = vlc_internals( p_this );
935 vlc_mutex_lock( &p_priv->var_lock );
937 p_var = Lookup( p_this, psz_name );
940 vlc_mutex_unlock( &p_priv->var_lock );
944 WaitUnused( p_this, p_var );
946 for( i_entry = p_var->i_entries ; i_entry-- ; )
948 if( p_var->p_entries[i_entry].pf_callback == pf_callback
949 && p_var->p_entries[i_entry].p_data == p_data )
954 else if( p_var->p_entries[i_entry].pf_callback == pf_callback )
955 b_found_similar = true;
962 if( b_found_similar )
963 fprintf( stderr, "Calling var_DelCallback for '%s' with the same "
964 "function but not the same data.", psz_name );
967 vlc_mutex_unlock( &p_priv->var_lock );
971 REMOVE_ELEM( p_var->p_entries, p_var->i_entries, i_entry );
973 vlc_mutex_unlock( &p_priv->var_lock );
978 #undef var_TriggerCallback
980 * Trigger callback on a variable
982 * \param p_this The object that hold the variable
983 * \param psz_name The name of the variable
985 int var_TriggerCallback( vlc_object_t *p_this, const char *psz_name )
992 vlc_object_internals_t *p_priv = vlc_internals( p_this );
994 vlc_mutex_lock( &p_priv->var_lock );
996 p_var = Lookup( p_this, psz_name );
999 vlc_mutex_unlock( &p_priv->var_lock );
1003 WaitUnused( p_this, p_var );
1005 /* Deal with callbacks. Tell we're in a callback, release the lock,
1006 * call stored functions, retake the lock. */
1007 i_ret = TriggerCallback( p_this, p_var, psz_name, p_var->val );
1009 vlc_mutex_unlock( &p_priv->var_lock );
1013 /** Parse a stringified option
1014 * This function parse a string option and create the associated object
1016 * The option must be of the form "[no[-]]foo[=bar]" where foo is the
1017 * option name and bar is the value of the option.
1018 * \param p_obj the object in which the variable must be created
1019 * \param psz_option the option to parse
1020 * \param trusted whether the option is set by a trusted input or not
1023 void var_OptionParse( vlc_object_t *p_obj, const char *psz_option,
1026 char *psz_name, *psz_value;
1028 bool b_isno = false;
1031 val.psz_string = NULL;
1033 /* It's too much of a hassle to remove the ':' when we parse
1034 * the cmd line :) */
1035 if( psz_option[0] == ':' )
1038 if( !psz_option[0] )
1041 psz_name = strdup( psz_option );
1042 if( psz_name == NULL )
1045 psz_value = strchr( psz_name, '=' );
1046 if( psz_value != NULL )
1047 *psz_value++ = '\0';
1049 /* FIXME: :programs should be handled generically */
1050 if( !strcmp( psz_name, "programs" ) )
1051 i_type = VLC_VAR_LIST;
1053 i_type = config_GetType( p_obj, psz_name );
1055 if( !i_type && !psz_value )
1057 /* check for "no-foo" or "nofoo" */
1058 if( !strncmp( psz_name, "no-", 3 ) )
1060 memmove( psz_name, psz_name + 3, strlen(psz_name) + 1 - 3 );
1062 else if( !strncmp( psz_name, "no", 2 ) )
1064 memmove( psz_name, psz_name + 2, strlen(psz_name) + 1 - 2 );
1066 else goto cleanup; /* Option doesn't exist */
1069 i_type = config_GetType( p_obj, psz_name );
1071 if( !i_type ) goto cleanup; /* Option doesn't exist */
1073 if( ( i_type != VLC_VAR_BOOL ) &&
1074 ( !psz_value || !*psz_value ) ) goto cleanup; /* Invalid value */
1076 /* check if option is unsafe */
1079 module_config_t *p_config = config_FindConfig( p_obj, psz_name );
1080 if( !p_config || !p_config->b_safe )
1082 msg_Err( p_obj, "unsafe option \"%s\" has been ignored for "
1083 "security reasons", psz_name );
1089 /* Create the variable in the input object.
1090 * Children of the input object will be able to retreive this value
1091 * thanks to the inheritance property of the object variables. */
1092 var_Create( p_obj, psz_name, i_type );
1097 val.b_bool = !b_isno;
1100 case VLC_VAR_INTEGER:
1101 val.i_int = strtol( psz_value, NULL, 0 );
1105 val.f_float = us_atof( psz_value );
1108 case VLC_VAR_STRING:
1109 case VLC_VAR_MODULE:
1111 case VLC_VAR_DIRECTORY:
1112 val.psz_string = psz_value;
1117 char *psz_orig, *psz_var;
1118 vlc_list_t *p_list = malloc(sizeof(vlc_list_t));
1119 val.p_list = p_list;
1120 p_list->i_count = 0;
1122 psz_var = psz_orig = strdup(psz_value);
1123 while( psz_var && *psz_var )
1125 char *psz_item = psz_var;
1127 while( *psz_var && *psz_var != ',' ) psz_var++;
1128 if( *psz_var == ',' )
1133 val2.i_int = strtol( psz_item, NULL, 0 );
1134 INSERT_ELEM( p_list->p_values, p_list->i_count,
1135 p_list->i_count, val2 );
1136 /* p_list->i_count is incremented twice by INSERT_ELEM */
1138 INSERT_ELEM( p_list->pi_types, p_list->i_count,
1139 p_list->i_count, VLC_VAR_INTEGER );
1149 var_Set( p_obj, psz_name, val );
1151 /* If that's a list, remove all elements allocated */
1152 if( i_type == VLC_VAR_LIST )
1160 /* Following functions are local */
1163 * Waits until the variable is inactive (i.e. not executing a callback)
1165 static void WaitUnused( vlc_object_t *p_this, variable_t *p_var )
1167 vlc_object_internals_t *p_priv = vlc_internals( p_this );
1169 mutex_cleanup_push( &p_priv->var_lock );
1170 while( p_var->b_incallback )
1171 vlc_cond_wait( &p_priv->var_wait, &p_priv->var_lock );
1175 /*****************************************************************************
1176 * CheckValue: check that a value is valid wrt. a variable
1177 *****************************************************************************
1178 * This function checks p_val's value against p_var's limitations such as
1179 * minimal and maximal value, step, in-list position, and modifies p_val if
1181 ****************************************************************************/
1182 static void CheckValue ( variable_t *p_var, vlc_value_t *p_val )
1184 /* Check that our variable is in the list */
1185 if( p_var->i_type & VLC_VAR_HASCHOICE && p_var->choices.i_count )
1189 /* This list is not sorted so go throug it (this is a small list) */
1190 for( i = p_var->choices.i_count ; i-- ; )
1192 if( p_var->ops->pf_cmp( *p_val, p_var->choices.p_values[i] ) == 0 )
1198 /* If not found, change it to anything vaguely valid */
1201 /* Free the old variable, get the new one, dup it */
1202 p_var->ops->pf_free( p_val );
1203 *p_val = p_var->choices.p_values[p_var->i_default >= 0
1204 ? p_var->i_default : 0 ];
1205 p_var->ops->pf_dup( p_val );
1209 /* Check that our variable is within the bounds */
1210 switch( p_var->i_type & VLC_VAR_TYPE )
1212 case VLC_VAR_INTEGER:
1213 if( p_var->i_type & VLC_VAR_HASSTEP && p_var->step.i_int
1214 && (p_val->i_int % p_var->step.i_int) )
1216 p_val->i_int = (p_val->i_int + (p_var->step.i_int / 2))
1217 / p_var->step.i_int * p_var->step.i_int;
1219 if( p_var->i_type & VLC_VAR_HASMIN
1220 && p_val->i_int < p_var->min.i_int )
1222 p_val->i_int = p_var->min.i_int;
1224 if( p_var->i_type & VLC_VAR_HASMAX
1225 && p_val->i_int > p_var->max.i_int )
1227 p_val->i_int = p_var->max.i_int;
1231 if( p_var->i_type & VLC_VAR_HASSTEP && p_var->step.f_float )
1233 float f_round = p_var->step.f_float * (float)(int)( 0.5 +
1234 p_val->f_float / p_var->step.f_float );
1235 if( p_val->f_float != f_round )
1237 p_val->f_float = f_round;
1240 if( p_var->i_type & VLC_VAR_HASMIN
1241 && p_val->f_float < p_var->min.f_float )
1243 p_val->f_float = p_var->min.f_float;
1245 if( p_var->i_type & VLC_VAR_HASMAX
1246 && p_val->f_float > p_var->max.f_float )
1248 p_val->f_float = p_var->max.f_float;
1258 * Finds the value of a variable. If the specified object does not hold a
1259 * variable with the specified name, try the parent object, and iterate until
1260 * the top of the tree. If no match is found, the value is read from the
1263 int var_Inherit( vlc_object_t *p_this, const char *psz_name, int i_type,
1264 vlc_value_t *p_val )
1267 if (p_this != VLC_OBJECT(p_this->p_libvlc)
1268 && unlikely(p_this->p_parent == NULL))
1270 msg_Info (p_this, "%s(%s) on detached object", __func__, psz_name);
1274 i_type &= VLC_VAR_CLASS;
1275 for( vlc_object_t *obj = p_this; obj != NULL; obj = obj->p_parent )
1277 if( var_GetChecked( obj, psz_name, i_type, p_val ) == VLC_SUCCESS )
1280 if (obj != p_this && obj != VLC_OBJECT(p_this->p_libvlc)
1281 && unlikely(obj->p_parent == NULL))
1283 msg_Info (p_this, "%s(%s) on detached tree [%p] %s", __func__,
1284 psz_name, obj, obj->psz_object_type);
1290 /* else take value from config */
1291 switch( i_type & VLC_VAR_CLASS )
1293 case VLC_VAR_STRING:
1294 p_val->psz_string = config_GetPsz( p_this, psz_name );
1295 if( !p_val->psz_string ) p_val->psz_string = strdup("");
1298 p_val->f_float = config_GetFloat( p_this, psz_name );
1300 case VLC_VAR_INTEGER:
1301 p_val->i_int = config_GetInt( p_this, psz_name );
1304 p_val->b_bool = config_GetInt( p_this, psz_name );
1308 char *psz_orig, *psz_var;
1309 vlc_list_t *p_list = malloc(sizeof(vlc_list_t));
1310 p_val->p_list = p_list;
1311 p_list->i_count = 0;
1313 psz_var = psz_orig = config_GetPsz( p_this, psz_name );
1314 while( psz_var && *psz_var )
1316 char *psz_item = psz_var;
1318 while( *psz_var && *psz_var != ',' ) psz_var++;
1319 if( *psz_var == ',' )
1324 val.i_int = strtol( psz_item, NULL, 0 );
1325 INSERT_ELEM( p_list->p_values, p_list->i_count,
1326 p_list->i_count, val );
1327 /* p_list->i_count is incremented twice by INSERT_ELEM */
1329 INSERT_ELEM( p_list->pi_types, p_list->i_count,
1330 p_list->i_count, VLC_VAR_INTEGER );
1336 msg_Warn( p_this, "Could not inherit value for var %s "
1337 "from config. Invalid Type", psz_name );
1340 /*msg_Dbg( p_this, "Inherited value for var %s from config", psz_name );*/
1345 /**********************************************************************
1346 * Trigger the callbacks.
1347 * Tell we're in a callback, release the lock, call stored functions,
1349 **********************************************************************/
1350 static int TriggerCallback( vlc_object_t *p_this, variable_t *p_var,
1351 const char *psz_name, vlc_value_t oldval )
1355 int i_entries = p_var->i_entries;
1356 if( i_entries == 0 )
1359 callback_entry_t *p_entries = p_var->p_entries;
1360 vlc_object_internals_t *p_priv = vlc_internals( p_this );
1362 assert( !p_var->b_incallback );
1363 p_var->b_incallback = true;
1364 vlc_mutex_unlock( &p_priv->var_lock );
1366 /* The real calls */
1367 for( ; i_entries-- ; )
1369 p_entries[i_entries].pf_callback( p_this, psz_name, oldval, p_var->val,
1370 p_entries[i_entries].p_data );
1373 vlc_mutex_lock( &p_priv->var_lock );
1374 p_var->b_incallback = false;
1375 vlc_cond_broadcast( &p_priv->var_wait );
1381 /**********************************************************************
1382 * Execute a var command on an object identified by its name
1383 **********************************************************************/
1384 int var_Command( vlc_object_t *p_this, const char *psz_name,
1385 const char *psz_cmd, const char *psz_arg, char **psz_msg )
1387 vlc_object_t *p_obj = vlc_object_find_name( p_this->p_libvlc,
1388 psz_name, FIND_CHILD );
1394 *psz_msg = strdup( "Unknown destination object." );
1398 i_type = var_Type( p_obj, psz_cmd );
1399 if( !( i_type&VLC_VAR_ISCOMMAND ) )
1401 vlc_object_release( p_obj );
1403 *psz_msg = strdup( "Variable doesn't exist or isn't a command." );
1404 return VLC_EGENERIC;
1407 i_type &= VLC_VAR_CLASS;
1410 case VLC_VAR_INTEGER:
1411 i_ret = var_SetInteger( p_obj, psz_cmd, atoi( psz_arg ) );
1414 i_ret = var_SetFloat( p_obj, psz_cmd, us_atof( psz_arg ) );
1416 case VLC_VAR_STRING:
1417 i_ret = var_SetString( p_obj, psz_cmd, psz_arg );
1420 i_ret = var_SetBool( p_obj, psz_cmd, atoi( psz_arg ) );
1423 i_ret = VLC_EGENERIC;
1427 vlc_object_release( p_obj );
1431 if( asprintf( psz_msg, "%s on object %s returned %i (%s)",
1432 psz_cmd, psz_name, i_ret, vlc_error( i_ret ) ) == -1)
1441 * Free a list and the associated strings
1442 * @param p_val: the list variable
1443 * @param p_val2: the variable associated or NULL
1445 void var_FreeList( vlc_value_t *p_val, vlc_value_t *p_val2 )
1448 if( p_val2 && p_val2->p_list )
1450 for( int i = 0; i < p_val2->p_list->i_count; i++ )
1451 free( p_val2->p_list->p_values[i].psz_string );
1452 if( p_val2->p_list->i_count )
1454 free( p_val2->p_list->p_values );
1455 free( p_val2->p_list->pi_types );
1457 free( p_val2->p_list );