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, };
162 /*****************************************************************************
164 *****************************************************************************/
165 static void WaitUnused ( vlc_object_t *, variable_t * );
167 static void CheckValue ( variable_t *, vlc_value_t * );
169 static int TriggerCallback( vlc_object_t *, variable_t *, const char *,
172 static int varcmp( const void *a, const void *b )
174 const variable_t *va = a, *vb = b;
176 /* psz_name must be first */
177 assert( va == (const void *)&va->psz_name );
178 return strcmp( va->psz_name, vb->psz_name );
181 static variable_t *Lookup( vlc_object_t *obj, const char *psz_name )
183 vlc_object_internals_t *priv = vlc_internals( obj );
186 vlc_assert_locked( &priv->var_lock );
187 pp_var = tfind( &psz_name, &priv->var_root, varcmp );
188 return (pp_var != NULL) ? *pp_var : NULL;
191 static void Destroy( variable_t *p_var )
193 p_var->ops->pf_free( &p_var->val );
194 if( p_var->choices.i_count )
196 for( int i = 0 ; i < p_var->choices.i_count ; i++ )
198 p_var->ops->pf_free( &p_var->choices.p_values[i] );
199 free( p_var->choices_text.p_values[i].psz_string );
201 free( p_var->choices.p_values );
202 free( p_var->choices_text.p_values );
204 free( p_var->psz_name );
205 free( p_var->psz_text );
206 free( p_var->p_entries );
212 * Initialize a vlc variable
214 * We hash the given string and insert it into the sorted list. The insertion
215 * may require slow memory copies, but think about what we gain in the log(n)
216 * lookup phase when setting/getting the variable value!
218 * \param p_this The object in which to create the variable
219 * \param psz_name The name of the variable
220 * \param i_type The variables type. Must be one of \ref var_type combined with
221 * zero or more \ref var_flags
223 int var_Create( vlc_object_t *p_this, const char *psz_name, int i_type )
225 static vlc_list_t dummy_null_list = {0, NULL, NULL};
228 variable_t *p_var = calloc( 1, sizeof( *p_var ) );
232 p_var->psz_name = strdup( psz_name );
233 p_var->psz_text = NULL;
235 p_var->i_type = i_type & ~VLC_VAR_DOINHERIT;
239 p_var->i_default = -1;
240 p_var->choices.i_count = 0;
241 p_var->choices.p_values = NULL;
242 p_var->choices_text.i_count = 0;
243 p_var->choices_text.p_values = NULL;
245 p_var->b_incallback = false;
246 p_var->i_entries = 0;
247 p_var->p_entries = NULL;
249 /* Always initialize the variable, even if it is a list variable; this
250 * will lead to errors if the variable is not initialized, but it will
251 * not cause crashes in the variable handling. */
252 switch( i_type & VLC_VAR_CLASS )
255 p_var->ops = &bool_ops;
256 p_var->val.b_bool = false;
258 case VLC_VAR_INTEGER:
259 p_var->ops = &int_ops;
260 p_var->val.i_int = 0;
263 p_var->ops = &string_ops;
264 p_var->val.psz_string = NULL;
267 p_var->ops = &float_ops;
268 p_var->val.f_float = 0.0;
271 p_var->ops = &time_ops;
272 p_var->val.i_time = 0;
274 case VLC_VAR_ADDRESS:
275 p_var->ops = &addr_ops;
276 p_var->val.p_address = NULL;
279 p_var->ops = &mutex_ops;
280 p_var->val.p_address = malloc( sizeof(vlc_mutex_t) );
281 vlc_mutex_init( (vlc_mutex_t*)p_var->val.p_address );
284 p_var->ops = &list_ops;
285 p_var->val.p_list = &dummy_null_list;
288 p_var->ops = &void_ops;
290 if( (i_type & VLC_VAR_CLASS) != VLC_VAR_VOID )
291 msg_Err( p_this, "Creating the variable '%s' without a type",
296 if( i_type & VLC_VAR_DOINHERIT )
298 if( var_Inherit( p_this, psz_name, i_type, &p_var->val ) )
299 msg_Err( p_this, "cannot inherit value for %s", psz_name );
300 else if( i_type & VLC_VAR_HASCHOICE )
302 /* We must add the inherited value to our choice list */
303 p_var->i_default = 0;
305 INSERT_ELEM( p_var->choices.p_values, p_var->choices.i_count,
307 INSERT_ELEM( p_var->choices_text.p_values,
308 p_var->choices_text.i_count, 0, p_var->val );
309 p_var->ops->pf_dup( &p_var->choices.p_values[0] );
310 p_var->choices_text.p_values[0].psz_string = NULL;
314 vlc_object_internals_t *p_priv = vlc_internals( p_this );
315 variable_t **pp_var, *p_oldvar;
316 int ret = VLC_SUCCESS;
318 vlc_mutex_lock( &p_priv->var_lock );
320 pp_var = tsearch( p_var, &p_priv->var_root, varcmp );
321 if( unlikely(pp_var == NULL) )
323 else if( (p_oldvar = *pp_var) == p_var )
325 else if( unlikely((i_type ^ p_oldvar->i_type) & VLC_VAR_CLASS) )
326 { /* If the types differ, variable creation failed. */
327 msg_Err( p_this, "Variable '%s' (0x%04x) already exist "
328 "but with a different type (0x%04x)",
329 psz_name, p_oldvar->i_type, i_type );
335 p_oldvar->i_type |= i_type & (VLC_VAR_ISCOMMAND|VLC_VAR_HASCHOICE);
337 vlc_mutex_unlock( &p_priv->var_lock );
339 /* If we did not need to create a new variable, free everything... */
347 * Destroy a vlc variable
349 * Look for the variable and destroy it if it is found. As in var_Create we
350 * do a call to memmove() but we have performance counterparts elsewhere.
352 * \param p_this The object that holds the variable
353 * \param psz_name The name of the variable
355 int var_Destroy( vlc_object_t *p_this, const char *psz_name )
361 vlc_object_internals_t *p_priv = vlc_internals( p_this );
363 vlc_mutex_lock( &p_priv->var_lock );
365 p_var = Lookup( p_this, psz_name );
368 vlc_mutex_unlock( &p_priv->var_lock );
372 WaitUnused( p_this, p_var );
374 if( --p_var->i_usage == 0 )
375 tdelete( p_var, &p_priv->var_root, varcmp );
378 vlc_mutex_unlock( &p_priv->var_lock );
385 static void CleanupVar( void *var )
390 void var_DestroyAll( vlc_object_t *obj )
392 vlc_object_internals_t *priv = vlc_internals( obj );
394 tdestroy( priv->var_root, CleanupVar );
399 * Perform an action on a variable
401 * \param p_this The object that holds the variable
402 * \param psz_name The name of the variable
403 * \param i_action The action to perform. Must be one of \ref var_action
404 * \param p_val First action parameter
405 * \param p_val2 Second action parameter
407 int var_Change( vlc_object_t *p_this, const char *psz_name,
408 int i_action, vlc_value_t *p_val, vlc_value_t *p_val2 )
417 vlc_object_internals_t *p_priv = vlc_internals( p_this );
419 vlc_mutex_lock( &p_priv->var_lock );
421 p_var = Lookup( p_this, psz_name );
424 vlc_mutex_unlock( &p_priv->var_lock );
431 if( p_var->i_type & VLC_VAR_HASMIN )
433 p_var->ops->pf_free( &p_var->min );
435 p_var->i_type |= VLC_VAR_HASMIN;
437 p_var->ops->pf_dup( &p_var->min );
438 CheckValue( p_var, &p_var->val );
441 if( p_var->i_type & VLC_VAR_HASMIN )
447 if( p_var->i_type & VLC_VAR_HASMAX )
449 p_var->ops->pf_free( &p_var->max );
451 p_var->i_type |= VLC_VAR_HASMAX;
453 p_var->ops->pf_dup( &p_var->max );
454 CheckValue( p_var, &p_var->val );
457 if( p_var->i_type & VLC_VAR_HASMAX )
462 case VLC_VAR_SETSTEP:
463 if( p_var->i_type & VLC_VAR_HASSTEP )
465 p_var->ops->pf_free( &p_var->step );
467 p_var->i_type |= VLC_VAR_HASSTEP;
468 p_var->step = *p_val;
469 p_var->ops->pf_dup( &p_var->step );
470 CheckValue( p_var, &p_var->val );
472 case VLC_VAR_GETSTEP:
473 if( p_var->i_type & VLC_VAR_HASSTEP )
475 *p_val = p_var->step;
478 case VLC_VAR_ADDCHOICE:
479 i = p_var->choices.i_count;
481 INSERT_ELEM( p_var->choices.p_values, p_var->choices.i_count,
483 INSERT_ELEM( p_var->choices_text.p_values,
484 p_var->choices_text.i_count, i, *p_val );
485 p_var->ops->pf_dup( &p_var->choices.p_values[i] );
486 p_var->choices_text.p_values[i].psz_string =
487 ( p_val2 && p_val2->psz_string ) ?
488 strdup( p_val2->psz_string ) : NULL;
490 CheckValue( p_var, &p_var->val );
492 case VLC_VAR_DELCHOICE:
493 for( i = 0 ; i < p_var->choices.i_count ; i++ )
495 if( p_var->ops->pf_cmp( p_var->choices.p_values[i], *p_val ) == 0 )
501 if( i == p_var->choices.i_count )
504 vlc_mutex_unlock( &p_priv->var_lock );
508 if( p_var->i_default > i )
512 else if( p_var->i_default == i )
514 p_var->i_default = -1;
517 p_var->ops->pf_free( &p_var->choices.p_values[i] );
518 free( p_var->choices_text.p_values[i].psz_string );
519 REMOVE_ELEM( p_var->choices.p_values, p_var->choices.i_count, i );
520 REMOVE_ELEM( p_var->choices_text.p_values,
521 p_var->choices_text.i_count, i );
523 CheckValue( p_var, &p_var->val );
525 case VLC_VAR_CHOICESCOUNT:
526 p_val->i_int = p_var->choices.i_count;
528 case VLC_VAR_CLEARCHOICES:
529 for( i = 0 ; i < p_var->choices.i_count ; i++ )
531 p_var->ops->pf_free( &p_var->choices.p_values[i] );
533 for( i = 0 ; i < p_var->choices_text.i_count ; i++ )
534 free( p_var->choices_text.p_values[i].psz_string );
536 if( p_var->choices.i_count ) free( p_var->choices.p_values );
537 if( p_var->choices_text.i_count ) free( p_var->choices_text.p_values );
539 p_var->choices.i_count = 0;
540 p_var->choices.p_values = NULL;
541 p_var->choices_text.i_count = 0;
542 p_var->choices_text.p_values = NULL;
543 p_var->i_default = -1;
545 case VLC_VAR_SETDEFAULT:
546 /* FIXME: the list is sorted, dude. Use something cleverer. */
547 for( i = 0 ; i < p_var->choices.i_count ; i++ )
549 if( p_var->ops->pf_cmp( p_var->choices.p_values[i], *p_val ) == 0 )
555 if( i == p_var->choices.i_count )
561 p_var->i_default = i;
562 CheckValue( p_var, &p_var->val );
564 case VLC_VAR_SETVALUE:
565 /* Duplicate data if needed */
567 p_var->ops->pf_dup( &newval );
568 /* Backup needed stuff */
570 /* Check boundaries and list */
571 CheckValue( p_var, &newval );
572 /* Set the variable */
574 /* Free data if needed */
575 p_var->ops->pf_free( &oldval );
577 case VLC_VAR_GETCHOICES:
578 case VLC_VAR_GETLIST:
579 p_val->p_list = malloc( sizeof(vlc_list_t) );
580 if( p_val2 ) p_val2->p_list = malloc( sizeof(vlc_list_t) );
581 if( p_var->choices.i_count )
583 p_val->p_list->p_values = malloc( p_var->choices.i_count
584 * sizeof(vlc_value_t) );
585 p_val->p_list->pi_types = malloc( p_var->choices.i_count
589 p_val2->p_list->p_values =
590 malloc( p_var->choices.i_count * sizeof(vlc_value_t) );
591 p_val2->p_list->pi_types =
592 malloc( p_var->choices.i_count * sizeof(int) );
595 p_val->p_list->i_count = p_var->choices.i_count;
596 if( p_val2 ) p_val2->p_list->i_count = p_var->choices.i_count;
597 for( i = 0 ; i < p_var->choices.i_count ; i++ )
599 p_val->p_list->p_values[i] = p_var->choices.p_values[i];
600 p_val->p_list->pi_types[i] = p_var->i_type;
601 p_var->ops->pf_dup( &p_val->p_list->p_values[i] );
604 p_val2->p_list->p_values[i].psz_string =
605 p_var->choices_text.p_values[i].psz_string ?
606 strdup(p_var->choices_text.p_values[i].psz_string) : NULL;
607 p_val2->p_list->pi_types[i] = VLC_VAR_STRING;
611 case VLC_VAR_SETTEXT:
612 free( p_var->psz_text );
613 if( p_val && p_val->psz_string )
614 p_var->psz_text = strdup( p_val->psz_string );
616 p_var->psz_text = NULL;
618 case VLC_VAR_GETTEXT:
619 p_val->psz_string = p_var->psz_text ? strdup( p_var->psz_text )
622 case VLC_VAR_SETISCOMMAND:
623 p_var->i_type |= VLC_VAR_ISCOMMAND;
630 vlc_mutex_unlock( &p_priv->var_lock );
637 * Perform a Get and Set on a variable
639 * \param p_this: The object that hold the variable
640 * \param psz_name: the name of the variable
641 * \param i_action: the action to perform
642 * \param p_val: The action parameter
643 * \return vlc error codes
645 int var_GetAndSet( vlc_object_t *p_this, const char *psz_name, int i_action,
655 vlc_object_internals_t *p_priv = vlc_internals( p_this );
657 vlc_mutex_lock( &p_priv->var_lock );
658 p_var = Lookup( p_this, psz_name );
661 vlc_mutex_unlock( &p_priv->var_lock );
665 WaitUnused( p_this, p_var );
667 /* Duplicated data if needed */
668 //p_var->ops->pf_dup( &val );
670 /* Backup needed stuff */
673 /* depending of the action requiered */
676 case VLC_VAR_BOOL_TOGGLE:
677 assert( ( p_var->i_type & VLC_VAR_BOOL ) == VLC_VAR_BOOL );
678 p_var->val.b_bool = !p_var->val.b_bool;
680 case VLC_VAR_INTEGER_ADD:
681 assert( ( p_var->i_type & VLC_VAR_INTEGER ) == VLC_VAR_INTEGER );
682 p_var->val.i_int += p_val->i_int;
684 case VLC_VAR_INTEGER_OR:
685 assert( ( p_var->i_type & VLC_VAR_INTEGER ) == VLC_VAR_INTEGER );
686 p_var->val.i_int |= p_val->i_int;
688 case VLC_VAR_INTEGER_NAND:
689 assert( ( p_var->i_type & VLC_VAR_INTEGER ) == VLC_VAR_INTEGER );
690 p_var->val.i_int &= ~p_val->i_int;
693 vlc_mutex_unlock( &p_priv->var_lock );
697 /* Check boundaries */
698 CheckValue( p_var, &p_var->val );
701 /* Deal with callbacks.*/
702 i_ret = TriggerCallback( p_this, p_var, psz_name, oldval );
704 vlc_mutex_unlock( &p_priv->var_lock );
711 * Request a variable's type
713 * \return The variable type if it exists, or 0 if the
714 * variable could not be found.
717 int var_Type( vlc_object_t *p_this, const char *psz_name )
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 );
730 i_type = p_var->i_type;
732 vlc_mutex_unlock( &p_priv->var_lock );
737 int var_SetChecked( vlc_object_t *p_this, const char *psz_name,
738 int expected_type, vlc_value_t val )
740 int i_ret = VLC_SUCCESS;
746 vlc_object_internals_t *p_priv = vlc_internals( p_this );
748 vlc_mutex_lock( &p_priv->var_lock );
750 p_var = Lookup( p_this, psz_name );
753 vlc_mutex_unlock( &p_priv->var_lock );
757 assert( expected_type == 0 ||
758 (p_var->i_type & VLC_VAR_CLASS) == expected_type );
760 WaitUnused( p_this, p_var );
762 /* Duplicate data if needed */
763 p_var->ops->pf_dup( &val );
765 /* Backup needed stuff */
768 /* Check boundaries and list */
769 CheckValue( p_var, &val );
771 /* Set the variable */
774 /* Deal with callbacks */
775 i_ret = TriggerCallback( p_this, p_var, psz_name, oldval );
777 /* Free data if needed */
778 p_var->ops->pf_free( &oldval );
780 vlc_mutex_unlock( &p_priv->var_lock );
787 * Set a variable's value
789 * \param p_this The object that hold the variable
790 * \param psz_name The name of the variable
791 * \param val the value to set
793 int var_Set( vlc_object_t *p_this, const char *psz_name, vlc_value_t val )
795 return var_SetChecked( p_this, psz_name, 0, val );
798 int var_GetChecked( vlc_object_t *p_this, const char *psz_name,
799 int expected_type, vlc_value_t *p_val )
803 vlc_object_internals_t *p_priv = vlc_internals( p_this );
805 int err = VLC_SUCCESS;
807 vlc_mutex_lock( &p_priv->var_lock );
809 p_var = Lookup( p_this, psz_name );
812 assert( expected_type == 0 ||
813 (p_var->i_type & VLC_VAR_CLASS) == expected_type );
815 /* Really get the variable */
819 /* Alert if the type is VLC_VAR_VOID */
820 if( ( p_var->i_type & VLC_VAR_TYPE ) == VLC_VAR_VOID )
821 msg_Warn( p_this, "Calling var_GetVoid on the void variable '%s' (0x%04x)", psz_name, p_var->i_type );
824 /* Duplicate value if needed */
825 p_var->ops->pf_dup( p_val );
830 vlc_mutex_unlock( &p_priv->var_lock );
836 * Get a variable's value
838 * \param p_this The object that holds the variable
839 * \param psz_name The name of the variable
840 * \param p_val Pointer to a vlc_value_t that will hold the variable's value
841 * after the function is finished
843 int var_Get( vlc_object_t *p_this, const char *psz_name, vlc_value_t *p_val )
845 return var_GetChecked( p_this, psz_name, 0, p_val );
848 #undef var_AddCallback
850 * Register a callback in a variable
852 * We store a function pointer that will be called upon variable
855 * \param p_this The object that holds the variable
856 * \param psz_name The name of the variable
857 * \param pf_callback The function pointer
858 * \param p_data A generic pointer that will be passed as the last
859 * argument to the callback function.
861 * \warning The callback function is run in the thread that calls var_Set on
862 * the variable. Use proper locking. This thread may not have much
863 * time to spare, so keep callback functions short.
865 int var_AddCallback( vlc_object_t *p_this, const char *psz_name,
866 vlc_callback_t pf_callback, void *p_data )
869 callback_entry_t entry;
873 vlc_object_internals_t *p_priv = vlc_internals( p_this );
875 entry.pf_callback = pf_callback;
876 entry.p_data = p_data;
878 vlc_mutex_lock( &p_priv->var_lock );
880 p_var = Lookup( p_this, psz_name );
884 msg_Warn( p_this, "Failed to add a callback to the non-existing "
885 "variable '%s'", psz_name );
887 vlc_mutex_unlock( &p_priv->var_lock );
891 WaitUnused( p_this, p_var );
892 INSERT_ELEM( p_var->p_entries,
897 vlc_mutex_unlock( &p_priv->var_lock );
902 #undef var_DelCallback
904 * Remove a callback from a variable
906 * pf_callback and p_data have to be given again, because different objects
907 * might have registered the same callback function.
909 int var_DelCallback( vlc_object_t *p_this, const char *psz_name,
910 vlc_callback_t pf_callback, void *p_data )
915 bool b_found_similar = false;
920 vlc_object_internals_t *p_priv = vlc_internals( p_this );
922 vlc_mutex_lock( &p_priv->var_lock );
924 p_var = Lookup( p_this, psz_name );
927 vlc_mutex_unlock( &p_priv->var_lock );
931 WaitUnused( p_this, p_var );
933 for( i_entry = p_var->i_entries ; i_entry-- ; )
935 if( p_var->p_entries[i_entry].pf_callback == pf_callback
936 && p_var->p_entries[i_entry].p_data == p_data )
941 else if( p_var->p_entries[i_entry].pf_callback == pf_callback )
942 b_found_similar = true;
949 if( b_found_similar )
950 fprintf( stderr, "Calling var_DelCallback for '%s' with the same "
951 "function but not the same data.", psz_name );
954 vlc_mutex_unlock( &p_priv->var_lock );
958 REMOVE_ELEM( p_var->p_entries, p_var->i_entries, i_entry );
960 vlc_mutex_unlock( &p_priv->var_lock );
965 #undef var_TriggerCallback
967 * Trigger callback on a variable
969 * \param p_this The object that hold the variable
970 * \param psz_name The name of the variable
972 int var_TriggerCallback( vlc_object_t *p_this, const char *psz_name )
979 vlc_object_internals_t *p_priv = vlc_internals( p_this );
981 vlc_mutex_lock( &p_priv->var_lock );
983 p_var = Lookup( p_this, psz_name );
986 vlc_mutex_unlock( &p_priv->var_lock );
990 WaitUnused( p_this, p_var );
992 /* Deal with callbacks. Tell we're in a callback, release the lock,
993 * call stored functions, retake the lock. */
994 i_ret = TriggerCallback( p_this, p_var, psz_name, p_var->val );
996 vlc_mutex_unlock( &p_priv->var_lock );
1000 /** Parse a stringified option
1001 * This function parse a string option and create the associated object
1003 * The option must be of the form "[no[-]]foo[=bar]" where foo is the
1004 * option name and bar is the value of the option.
1005 * \param p_obj the object in which the variable must be created
1006 * \param psz_option the option to parse
1007 * \param trusted whether the option is set by a trusted input or not
1010 void var_OptionParse( vlc_object_t *p_obj, const char *psz_option,
1013 char *psz_name, *psz_value;
1015 bool b_isno = false;
1018 val.psz_string = NULL;
1020 /* It's too much of a hassle to remove the ':' when we parse
1021 * the cmd line :) */
1022 if( psz_option[0] == ':' )
1025 if( !psz_option[0] )
1028 psz_name = strdup( psz_option );
1029 if( psz_name == NULL )
1032 psz_value = strchr( psz_name, '=' );
1033 if( psz_value != NULL )
1034 *psz_value++ = '\0';
1036 /* FIXME: :programs should be handled generically */
1037 if( !strcmp( psz_name, "programs" ) )
1038 i_type = VLC_VAR_LIST;
1040 i_type = config_GetType( p_obj, psz_name );
1042 if( !i_type && !psz_value )
1044 /* check for "no-foo" or "nofoo" */
1045 if( !strncmp( psz_name, "no-", 3 ) )
1047 memmove( psz_name, psz_name + 3, strlen(psz_name) + 1 - 3 );
1049 else if( !strncmp( psz_name, "no", 2 ) )
1051 memmove( psz_name, psz_name + 2, strlen(psz_name) + 1 - 2 );
1053 else goto cleanup; /* Option doesn't exist */
1056 i_type = config_GetType( p_obj, psz_name );
1058 if( !i_type ) goto cleanup; /* Option doesn't exist */
1060 if( ( i_type != VLC_VAR_BOOL ) &&
1061 ( !psz_value || !*psz_value ) ) goto cleanup; /* Invalid value */
1063 /* check if option is unsafe */
1066 module_config_t *p_config = config_FindConfig( p_obj, psz_name );
1067 if( !p_config || !p_config->b_safe )
1069 msg_Err( p_obj, "unsafe option \"%s\" has been ignored for "
1070 "security reasons", psz_name );
1076 /* Create the variable in the input object.
1077 * Children of the input object will be able to retreive this value
1078 * thanks to the inheritance property of the object variables. */
1079 var_Create( p_obj, psz_name, i_type );
1084 val.b_bool = !b_isno;
1087 case VLC_VAR_INTEGER:
1088 val.i_int = strtol( psz_value, NULL, 0 );
1092 val.f_float = us_atof( psz_value );
1095 case VLC_VAR_STRING:
1096 case VLC_VAR_MODULE:
1098 case VLC_VAR_DIRECTORY:
1099 val.psz_string = psz_value;
1104 char *psz_orig, *psz_var;
1105 vlc_list_t *p_list = malloc(sizeof(vlc_list_t));
1106 val.p_list = p_list;
1107 p_list->i_count = 0;
1109 psz_var = psz_orig = strdup(psz_value);
1110 while( psz_var && *psz_var )
1112 char *psz_item = psz_var;
1114 while( *psz_var && *psz_var != ',' ) psz_var++;
1115 if( *psz_var == ',' )
1120 val2.i_int = strtol( psz_item, NULL, 0 );
1121 INSERT_ELEM( p_list->p_values, p_list->i_count,
1122 p_list->i_count, val2 );
1123 /* p_list->i_count is incremented twice by INSERT_ELEM */
1125 INSERT_ELEM( p_list->pi_types, p_list->i_count,
1126 p_list->i_count, VLC_VAR_INTEGER );
1136 var_Set( p_obj, psz_name, val );
1138 /* If that's a list, remove all elements allocated */
1139 if( i_type == VLC_VAR_LIST )
1147 /* Following functions are local */
1150 * Waits until the variable is inactive (i.e. not executing a callback)
1152 static void WaitUnused( vlc_object_t *p_this, variable_t *p_var )
1154 vlc_object_internals_t *p_priv = vlc_internals( p_this );
1156 mutex_cleanup_push( &p_priv->var_lock );
1157 while( p_var->b_incallback )
1158 vlc_cond_wait( &p_priv->var_wait, &p_priv->var_lock );
1162 /*****************************************************************************
1163 * CheckValue: check that a value is valid wrt. a variable
1164 *****************************************************************************
1165 * This function checks p_val's value against p_var's limitations such as
1166 * minimal and maximal value, step, in-list position, and modifies p_val if
1168 ****************************************************************************/
1169 static void CheckValue ( variable_t *p_var, vlc_value_t *p_val )
1171 /* Check that our variable is in the list */
1172 if( p_var->i_type & VLC_VAR_HASCHOICE && p_var->choices.i_count )
1176 /* This list is not sorted so go throug it (this is a small list) */
1177 for( i = p_var->choices.i_count ; i-- ; )
1179 if( p_var->ops->pf_cmp( *p_val, p_var->choices.p_values[i] ) == 0 )
1185 /* If not found, change it to anything vaguely valid */
1188 /* Free the old variable, get the new one, dup it */
1189 p_var->ops->pf_free( p_val );
1190 *p_val = p_var->choices.p_values[p_var->i_default >= 0
1191 ? p_var->i_default : 0 ];
1192 p_var->ops->pf_dup( p_val );
1196 /* Check that our variable is within the bounds */
1197 switch( p_var->i_type & VLC_VAR_TYPE )
1199 case VLC_VAR_INTEGER:
1200 if( p_var->i_type & VLC_VAR_HASSTEP && p_var->step.i_int
1201 && (p_val->i_int % p_var->step.i_int) )
1203 p_val->i_int = (p_val->i_int + (p_var->step.i_int / 2))
1204 / p_var->step.i_int * p_var->step.i_int;
1206 if( p_var->i_type & VLC_VAR_HASMIN
1207 && p_val->i_int < p_var->min.i_int )
1209 p_val->i_int = p_var->min.i_int;
1211 if( p_var->i_type & VLC_VAR_HASMAX
1212 && p_val->i_int > p_var->max.i_int )
1214 p_val->i_int = p_var->max.i_int;
1218 if( p_var->i_type & VLC_VAR_HASSTEP && p_var->step.f_float )
1220 float f_round = p_var->step.f_float * (float)(int)( 0.5 +
1221 p_val->f_float / p_var->step.f_float );
1222 if( p_val->f_float != f_round )
1224 p_val->f_float = f_round;
1227 if( p_var->i_type & VLC_VAR_HASMIN
1228 && p_val->f_float < p_var->min.f_float )
1230 p_val->f_float = p_var->min.f_float;
1232 if( p_var->i_type & VLC_VAR_HASMAX
1233 && p_val->f_float > p_var->max.f_float )
1235 p_val->f_float = p_var->max.f_float;
1245 * Finds the value of a variable. If the specified object does not hold a
1246 * variable with the specified name, try the parent object, and iterate until
1247 * the top of the tree. If no match is found, the value is read from the
1250 int var_Inherit( vlc_object_t *p_this, const char *psz_name, int i_type,
1251 vlc_value_t *p_val )
1254 if (p_this != VLC_OBJECT(p_this->p_libvlc)
1255 && unlikely(p_this->p_parent == NULL))
1257 msg_Info (p_this, "%s(%s) on detached object", __func__, psz_name);
1261 i_type &= VLC_VAR_CLASS;
1262 for( vlc_object_t *obj = p_this; obj != NULL; obj = obj->p_parent )
1264 if( var_GetChecked( obj, psz_name, i_type, p_val ) == VLC_SUCCESS )
1267 if (obj != p_this && obj != VLC_OBJECT(p_this->p_libvlc)
1268 && unlikely(obj->p_parent == NULL))
1270 msg_Info (p_this, "%s(%s) on detached tree [%p] %s", __func__,
1271 psz_name, obj, obj->psz_object_type);
1277 /* else take value from config */
1278 switch( i_type & VLC_VAR_CLASS )
1280 case VLC_VAR_STRING:
1281 p_val->psz_string = config_GetPsz( p_this, psz_name );
1282 if( !p_val->psz_string ) p_val->psz_string = strdup("");
1285 p_val->f_float = config_GetFloat( p_this, psz_name );
1287 case VLC_VAR_INTEGER:
1288 p_val->i_int = config_GetInt( p_this, psz_name );
1291 p_val->b_bool = config_GetInt( p_this, psz_name );
1295 char *psz_orig, *psz_var;
1296 vlc_list_t *p_list = malloc(sizeof(vlc_list_t));
1297 p_val->p_list = p_list;
1298 p_list->i_count = 0;
1300 psz_var = psz_orig = config_GetPsz( p_this, psz_name );
1301 while( psz_var && *psz_var )
1303 char *psz_item = psz_var;
1305 while( *psz_var && *psz_var != ',' ) psz_var++;
1306 if( *psz_var == ',' )
1311 val.i_int = strtol( psz_item, NULL, 0 );
1312 INSERT_ELEM( p_list->p_values, p_list->i_count,
1313 p_list->i_count, val );
1314 /* p_list->i_count is incremented twice by INSERT_ELEM */
1316 INSERT_ELEM( p_list->pi_types, p_list->i_count,
1317 p_list->i_count, VLC_VAR_INTEGER );
1323 msg_Warn( p_this, "Could not inherit value for var %s "
1324 "from config. Invalid Type", psz_name );
1327 /*msg_Dbg( p_this, "Inherited value for var %s from config", psz_name );*/
1332 /**********************************************************************
1333 * Trigger the callbacks.
1334 * Tell we're in a callback, release the lock, call stored functions,
1336 **********************************************************************/
1337 static int TriggerCallback( vlc_object_t *p_this, variable_t *p_var,
1338 const char *psz_name, vlc_value_t oldval )
1342 int i_entries = p_var->i_entries;
1343 if( i_entries == 0 )
1346 callback_entry_t *p_entries = p_var->p_entries;
1347 vlc_object_internals_t *p_priv = vlc_internals( p_this );
1349 assert( !p_var->b_incallback );
1350 p_var->b_incallback = true;
1351 vlc_mutex_unlock( &p_priv->var_lock );
1353 /* The real calls */
1354 for( ; i_entries-- ; )
1356 p_entries[i_entries].pf_callback( p_this, psz_name, oldval, p_var->val,
1357 p_entries[i_entries].p_data );
1360 vlc_mutex_lock( &p_priv->var_lock );
1361 p_var->b_incallback = false;
1362 vlc_cond_broadcast( &p_priv->var_wait );
1368 /**********************************************************************
1369 * Execute a var command on an object identified by its name
1370 **********************************************************************/
1371 int var_Command( vlc_object_t *p_this, const char *psz_name,
1372 const char *psz_cmd, const char *psz_arg, char **psz_msg )
1374 vlc_object_t *p_obj = vlc_object_find_name( p_this->p_libvlc,
1375 psz_name, FIND_CHILD );
1381 *psz_msg = strdup( "Unknown destination object." );
1385 i_type = var_Type( p_obj, psz_cmd );
1386 if( !( i_type&VLC_VAR_ISCOMMAND ) )
1388 vlc_object_release( p_obj );
1390 *psz_msg = strdup( "Variable doesn't exist or isn't a command." );
1391 return VLC_EGENERIC;
1394 i_type &= VLC_VAR_CLASS;
1397 case VLC_VAR_INTEGER:
1398 i_ret = var_SetInteger( p_obj, psz_cmd, atoi( psz_arg ) );
1401 i_ret = var_SetFloat( p_obj, psz_cmd, us_atof( psz_arg ) );
1403 case VLC_VAR_STRING:
1404 i_ret = var_SetString( p_obj, psz_cmd, psz_arg );
1407 i_ret = var_SetBool( p_obj, psz_cmd, atoi( psz_arg ) );
1410 i_ret = VLC_EGENERIC;
1414 vlc_object_release( p_obj );
1418 if( asprintf( psz_msg, "%s on object %s returned %i (%s)",
1419 psz_cmd, psz_name, i_ret, vlc_error( i_ret ) ) == -1)
1428 * Free a list and the associated strings
1429 * @param p_val: the list variable
1430 * @param p_val2: the variable associated or NULL
1432 void var_FreeList( vlc_value_t *p_val, vlc_value_t *p_val2 )
1435 if( p_val2 && p_val2->p_list )
1437 for( int i = 0; i < p_val2->p_list->i_count; i++ )
1438 free( p_val2->p_list->p_values[i].psz_string );
1439 if( p_val2->p_list->i_count )
1441 free( p_val2->p_list->p_values );
1442 free( p_val2->p_list->pi_types );
1444 free( p_val2->p_list );