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 WaitUnused( p_this, p_var );
768 /* Duplicate data if needed */
769 p_var->ops->pf_dup( &val );
771 /* Backup needed stuff */
774 /* Check boundaries and list */
775 CheckValue( p_var, &val );
777 /* Set the variable */
780 /* Deal with callbacks */
781 i_ret = TriggerCallback( p_this, p_var, psz_name, oldval );
783 /* Free data if needed */
784 p_var->ops->pf_free( &oldval );
786 vlc_mutex_unlock( &p_priv->var_lock );
793 * Set a variable's value
795 * \param p_this The object that hold the variable
796 * \param psz_name The name of the variable
797 * \param val the value to set
799 int var_Set( vlc_object_t *p_this, const char *psz_name, vlc_value_t val )
801 return var_SetChecked( p_this, psz_name, 0, val );
804 #undef var_GetChecked
805 int var_GetChecked( vlc_object_t *p_this, const char *psz_name,
806 int expected_type, vlc_value_t *p_val )
810 vlc_object_internals_t *p_priv = vlc_internals( p_this );
812 int err = VLC_SUCCESS;
814 vlc_mutex_lock( &p_priv->var_lock );
816 p_var = Lookup( p_this, psz_name );
819 assert( expected_type == 0 ||
820 (p_var->i_type & VLC_VAR_CLASS) == expected_type );
822 /* Really get the variable */
826 /* Alert if the type is VLC_VAR_VOID */
827 if( ( p_var->i_type & VLC_VAR_TYPE ) == VLC_VAR_VOID )
828 msg_Warn( p_this, "Calling var_GetVoid on the void variable '%s' (0x%04x)", psz_name, p_var->i_type );
831 /* Duplicate value if needed */
832 p_var->ops->pf_dup( p_val );
837 vlc_mutex_unlock( &p_priv->var_lock );
843 * Get a variable's value
845 * \param p_this The object that holds the variable
846 * \param psz_name The name of the variable
847 * \param p_val Pointer to a vlc_value_t that will hold the variable's value
848 * after the function is finished
850 int var_Get( vlc_object_t *p_this, const char *psz_name, vlc_value_t *p_val )
852 return var_GetChecked( p_this, psz_name, 0, p_val );
855 #undef var_AddCallback
857 * Register a callback in a variable
859 * We store a function pointer that will be called upon variable
862 * \param p_this The object that holds the variable
863 * \param psz_name The name of the variable
864 * \param pf_callback The function pointer
865 * \param p_data A generic pointer that will be passed as the last
866 * argument to the callback function.
868 * \warning The callback function is run in the thread that calls var_Set on
869 * the variable. Use proper locking. This thread may not have much
870 * time to spare, so keep callback functions short.
872 int var_AddCallback( vlc_object_t *p_this, const char *psz_name,
873 vlc_callback_t pf_callback, void *p_data )
876 callback_entry_t entry;
880 vlc_object_internals_t *p_priv = vlc_internals( p_this );
882 entry.pf_callback = pf_callback;
883 entry.p_data = p_data;
885 vlc_mutex_lock( &p_priv->var_lock );
887 p_var = Lookup( p_this, psz_name );
891 msg_Warn( p_this, "Failed to add a callback to the non-existing "
892 "variable '%s'", psz_name );
894 vlc_mutex_unlock( &p_priv->var_lock );
898 WaitUnused( p_this, p_var );
899 INSERT_ELEM( p_var->p_entries,
904 vlc_mutex_unlock( &p_priv->var_lock );
909 #undef var_DelCallback
911 * Remove a callback from a variable
913 * pf_callback and p_data have to be given again, because different objects
914 * might have registered the same callback function.
916 int var_DelCallback( vlc_object_t *p_this, const char *psz_name,
917 vlc_callback_t pf_callback, void *p_data )
922 bool b_found_similar = false;
927 vlc_object_internals_t *p_priv = vlc_internals( p_this );
929 vlc_mutex_lock( &p_priv->var_lock );
931 p_var = Lookup( p_this, psz_name );
934 vlc_mutex_unlock( &p_priv->var_lock );
938 WaitUnused( p_this, p_var );
940 for( i_entry = p_var->i_entries ; i_entry-- ; )
942 if( p_var->p_entries[i_entry].pf_callback == pf_callback
943 && p_var->p_entries[i_entry].p_data == p_data )
948 else if( p_var->p_entries[i_entry].pf_callback == pf_callback )
949 b_found_similar = true;
956 if( b_found_similar )
957 fprintf( stderr, "Calling var_DelCallback for '%s' with the same "
958 "function but not the same data.", psz_name );
961 vlc_mutex_unlock( &p_priv->var_lock );
965 REMOVE_ELEM( p_var->p_entries, p_var->i_entries, i_entry );
967 vlc_mutex_unlock( &p_priv->var_lock );
972 #undef var_TriggerCallback
974 * Trigger callback on a variable
976 * \param p_this The object that hold the variable
977 * \param psz_name The name of the variable
979 int var_TriggerCallback( vlc_object_t *p_this, const char *psz_name )
986 vlc_object_internals_t *p_priv = vlc_internals( p_this );
988 vlc_mutex_lock( &p_priv->var_lock );
990 p_var = Lookup( p_this, psz_name );
993 vlc_mutex_unlock( &p_priv->var_lock );
997 WaitUnused( p_this, p_var );
999 /* Deal with callbacks. Tell we're in a callback, release the lock,
1000 * call stored functions, retake the lock. */
1001 i_ret = TriggerCallback( p_this, p_var, psz_name, p_var->val );
1003 vlc_mutex_unlock( &p_priv->var_lock );
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 /* FIXME: :programs should be handled generically */
1044 if( !strcmp( psz_name, "programs" ) )
1045 i_type = VLC_VAR_LIST;
1047 i_type = config_GetType( p_obj, psz_name );
1049 if( !i_type && !psz_value )
1051 /* check for "no-foo" or "nofoo" */
1052 if( !strncmp( psz_name, "no-", 3 ) )
1054 memmove( psz_name, psz_name + 3, strlen(psz_name) + 1 - 3 );
1056 else if( !strncmp( psz_name, "no", 2 ) )
1058 memmove( psz_name, psz_name + 2, strlen(psz_name) + 1 - 2 );
1060 else goto cleanup; /* Option doesn't exist */
1063 i_type = config_GetType( p_obj, psz_name );
1065 if( !i_type ) goto cleanup; /* Option doesn't exist */
1067 if( ( i_type != VLC_VAR_BOOL ) &&
1068 ( !psz_value || !*psz_value ) ) goto cleanup; /* Invalid value */
1070 /* check if option is unsafe */
1073 module_config_t *p_config = config_FindConfig( p_obj, psz_name );
1074 if( !p_config || !p_config->b_safe )
1076 msg_Err( p_obj, "unsafe option \"%s\" has been ignored for "
1077 "security reasons", psz_name );
1083 /* Create the variable in the input object.
1084 * Children of the input object will be able to retreive this value
1085 * thanks to the inheritance property of the object variables. */
1086 var_Create( p_obj, psz_name, i_type );
1091 val.b_bool = !b_isno;
1094 case VLC_VAR_INTEGER:
1095 val.i_int = strtol( psz_value, NULL, 0 );
1099 val.f_float = us_atof( psz_value );
1102 case VLC_VAR_STRING:
1103 case VLC_VAR_MODULE:
1105 case VLC_VAR_DIRECTORY:
1106 val.psz_string = psz_value;
1111 char *psz_orig, *psz_var;
1112 vlc_list_t *p_list = malloc(sizeof(vlc_list_t));
1113 val.p_list = p_list;
1114 p_list->i_count = 0;
1116 psz_var = psz_orig = strdup(psz_value);
1117 while( psz_var && *psz_var )
1119 char *psz_item = psz_var;
1121 while( *psz_var && *psz_var != ',' ) psz_var++;
1122 if( *psz_var == ',' )
1127 val2.i_int = strtol( psz_item, NULL, 0 );
1128 INSERT_ELEM( p_list->p_values, p_list->i_count,
1129 p_list->i_count, val2 );
1130 /* p_list->i_count is incremented twice by INSERT_ELEM */
1132 INSERT_ELEM( p_list->pi_types, p_list->i_count,
1133 p_list->i_count, VLC_VAR_INTEGER );
1143 var_Set( p_obj, psz_name, val );
1145 /* If that's a list, remove all elements allocated */
1146 if( i_type == VLC_VAR_LIST )
1154 /* Following functions are local */
1157 * Waits until the variable is inactive (i.e. not executing a callback)
1159 static void WaitUnused( vlc_object_t *p_this, variable_t *p_var )
1161 vlc_object_internals_t *p_priv = vlc_internals( p_this );
1163 mutex_cleanup_push( &p_priv->var_lock );
1164 while( p_var->b_incallback )
1165 vlc_cond_wait( &p_priv->var_wait, &p_priv->var_lock );
1169 /*****************************************************************************
1170 * CheckValue: check that a value is valid wrt. a variable
1171 *****************************************************************************
1172 * This function checks p_val's value against p_var's limitations such as
1173 * minimal and maximal value, step, in-list position, and modifies p_val if
1175 ****************************************************************************/
1176 static void CheckValue ( variable_t *p_var, vlc_value_t *p_val )
1178 /* Check that our variable is in the list */
1179 if( p_var->i_type & VLC_VAR_HASCHOICE && p_var->choices.i_count )
1183 /* This list is not sorted so go throug it (this is a small list) */
1184 for( i = p_var->choices.i_count ; i-- ; )
1186 if( p_var->ops->pf_cmp( *p_val, p_var->choices.p_values[i] ) == 0 )
1192 /* If not found, change it to anything vaguely valid */
1195 /* Free the old variable, get the new one, dup it */
1196 p_var->ops->pf_free( p_val );
1197 *p_val = p_var->choices.p_values[p_var->i_default >= 0
1198 ? p_var->i_default : 0 ];
1199 p_var->ops->pf_dup( p_val );
1203 /* Check that our variable is within the bounds */
1204 switch( p_var->i_type & VLC_VAR_TYPE )
1206 case VLC_VAR_INTEGER:
1207 if( p_var->i_type & VLC_VAR_HASSTEP && p_var->step.i_int
1208 && (p_val->i_int % p_var->step.i_int) )
1210 p_val->i_int = (p_val->i_int + (p_var->step.i_int / 2))
1211 / p_var->step.i_int * p_var->step.i_int;
1213 if( p_var->i_type & VLC_VAR_HASMIN
1214 && p_val->i_int < p_var->min.i_int )
1216 p_val->i_int = p_var->min.i_int;
1218 if( p_var->i_type & VLC_VAR_HASMAX
1219 && p_val->i_int > p_var->max.i_int )
1221 p_val->i_int = p_var->max.i_int;
1225 if( p_var->i_type & VLC_VAR_HASSTEP && p_var->step.f_float )
1227 float f_round = p_var->step.f_float * (float)(int)( 0.5 +
1228 p_val->f_float / p_var->step.f_float );
1229 if( p_val->f_float != f_round )
1231 p_val->f_float = f_round;
1234 if( p_var->i_type & VLC_VAR_HASMIN
1235 && p_val->f_float < p_var->min.f_float )
1237 p_val->f_float = p_var->min.f_float;
1239 if( p_var->i_type & VLC_VAR_HASMAX
1240 && p_val->f_float > p_var->max.f_float )
1242 p_val->f_float = p_var->max.f_float;
1252 * Finds the value of a variable. If the specified object does not hold a
1253 * variable with the specified name, try the parent object, and iterate until
1254 * the top of the tree. If no match is found, the value is read from the
1257 int var_Inherit( vlc_object_t *p_this, const char *psz_name, int i_type,
1258 vlc_value_t *p_val )
1261 if (p_this != VLC_OBJECT(p_this->p_libvlc)
1262 && unlikely(p_this->p_parent == NULL))
1264 msg_Info (p_this, "%s(%s) on detached object", __func__, psz_name);
1268 i_type &= VLC_VAR_CLASS;
1269 for( vlc_object_t *obj = p_this; obj != NULL; obj = obj->p_parent )
1271 if( var_GetChecked( obj, psz_name, i_type, p_val ) == VLC_SUCCESS )
1274 if (obj != p_this && obj != VLC_OBJECT(p_this->p_libvlc)
1275 && unlikely(obj->p_parent == NULL))
1277 msg_Info (p_this, "%s(%s) on detached tree [%p] %s", __func__,
1278 psz_name, obj, obj->psz_object_type);
1284 /* else take value from config */
1285 switch( i_type & VLC_VAR_CLASS )
1287 case VLC_VAR_STRING:
1288 p_val->psz_string = config_GetPsz( p_this, psz_name );
1289 if( !p_val->psz_string ) p_val->psz_string = strdup("");
1292 p_val->f_float = config_GetFloat( p_this, psz_name );
1294 case VLC_VAR_INTEGER:
1295 p_val->i_int = config_GetInt( p_this, psz_name );
1298 p_val->b_bool = config_GetInt( p_this, psz_name );
1302 char *psz_orig, *psz_var;
1303 vlc_list_t *p_list = malloc(sizeof(vlc_list_t));
1304 p_val->p_list = p_list;
1305 p_list->i_count = 0;
1307 psz_var = psz_orig = config_GetPsz( p_this, psz_name );
1308 while( psz_var && *psz_var )
1310 char *psz_item = psz_var;
1312 while( *psz_var && *psz_var != ',' ) psz_var++;
1313 if( *psz_var == ',' )
1318 val.i_int = strtol( psz_item, NULL, 0 );
1319 INSERT_ELEM( p_list->p_values, p_list->i_count,
1320 p_list->i_count, val );
1321 /* p_list->i_count is incremented twice by INSERT_ELEM */
1323 INSERT_ELEM( p_list->pi_types, p_list->i_count,
1324 p_list->i_count, VLC_VAR_INTEGER );
1330 msg_Warn( p_this, "Could not inherit value for var %s "
1331 "from config. Invalid Type", psz_name );
1334 /*msg_Dbg( p_this, "Inherited value for var %s from config", psz_name );*/
1339 /**********************************************************************
1340 * Trigger the callbacks.
1341 * Tell we're in a callback, release the lock, call stored functions,
1343 **********************************************************************/
1344 static int TriggerCallback( vlc_object_t *p_this, variable_t *p_var,
1345 const char *psz_name, vlc_value_t oldval )
1349 int i_entries = p_var->i_entries;
1350 if( i_entries == 0 )
1353 callback_entry_t *p_entries = p_var->p_entries;
1354 vlc_object_internals_t *p_priv = vlc_internals( p_this );
1356 assert( !p_var->b_incallback );
1357 p_var->b_incallback = true;
1358 vlc_mutex_unlock( &p_priv->var_lock );
1360 /* The real calls */
1361 for( ; i_entries-- ; )
1363 p_entries[i_entries].pf_callback( p_this, psz_name, oldval, p_var->val,
1364 p_entries[i_entries].p_data );
1367 vlc_mutex_lock( &p_priv->var_lock );
1368 p_var->b_incallback = false;
1369 vlc_cond_broadcast( &p_priv->var_wait );
1375 /**********************************************************************
1376 * Execute a var command on an object identified by its name
1377 **********************************************************************/
1378 int var_Command( vlc_object_t *p_this, const char *psz_name,
1379 const char *psz_cmd, const char *psz_arg, char **psz_msg )
1381 vlc_object_t *p_obj = vlc_object_find_name( p_this->p_libvlc,
1382 psz_name, FIND_CHILD );
1388 *psz_msg = strdup( "Unknown destination object." );
1392 i_type = var_Type( p_obj, psz_cmd );
1393 if( !( i_type&VLC_VAR_ISCOMMAND ) )
1395 vlc_object_release( p_obj );
1397 *psz_msg = strdup( "Variable doesn't exist or isn't a command." );
1398 return VLC_EGENERIC;
1401 i_type &= VLC_VAR_CLASS;
1404 case VLC_VAR_INTEGER:
1405 i_ret = var_SetInteger( p_obj, psz_cmd, atoi( psz_arg ) );
1408 i_ret = var_SetFloat( p_obj, psz_cmd, us_atof( psz_arg ) );
1410 case VLC_VAR_STRING:
1411 i_ret = var_SetString( p_obj, psz_cmd, psz_arg );
1414 i_ret = var_SetBool( p_obj, psz_cmd, atoi( psz_arg ) );
1417 i_ret = VLC_EGENERIC;
1421 vlc_object_release( p_obj );
1425 if( asprintf( psz_msg, "%s on object %s returned %i (%s)",
1426 psz_cmd, psz_name, i_ret, vlc_error( i_ret ) ) == -1)
1435 * Free a list and the associated strings
1436 * @param p_val: the list variable
1437 * @param p_val2: the variable associated or NULL
1439 void var_FreeList( vlc_value_t *p_val, vlc_value_t *p_val2 )
1442 if( p_val2 && p_val2->p_list )
1444 for( int i = 0; i < p_val2->p_list->i_count; i++ )
1445 free( p_val2->p_list->p_values[i].psz_string );
1446 if( p_val2->p_list->i_count )
1448 free( p_val2->p_list->p_values );
1449 free( p_val2->p_list->pi_types );
1451 free( p_val2->p_list );