1 /*****************************************************************************
2 * variables.c: routines for object variables handling
3 *****************************************************************************
4 * Copyright (C) 2002-2009 VLC authors and VideoLAN
7 * Authors: Samuel Hocevar <sam@zoy.org>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
31 #include <vlc_common.h>
32 #include <vlc_charset.h>
33 #include "variables.h"
40 #include "config/configuration.h"
49 /*****************************************************************************
51 *****************************************************************************/
52 struct callback_entry_t
54 vlc_callback_t pf_callback;
58 /*****************************************************************************
59 * Local comparison functions, returns 0 if v == w, < 0 if v < w, > 0 if v > w
60 *****************************************************************************/
61 static int CmpBool( vlc_value_t v, vlc_value_t w )
63 return v.b_bool ? w.b_bool ? 0 : 1 : w.b_bool ? -1 : 0;
66 static int CmpInt( vlc_value_t v, vlc_value_t w )
68 return v.i_int == w.i_int ? 0 : v.i_int > w.i_int ? 1 : -1;
71 static int CmpTime( vlc_value_t v, vlc_value_t w )
73 return v.i_time == w.i_time ? 0 : v.i_time > w.i_time ? 1 : -1;
76 static int CmpString( vlc_value_t v, vlc_value_t w )
79 return !w.psz_string ? 0 : -1;
81 return !w.psz_string ? 1 : strcmp( v.psz_string, w.psz_string );
83 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; }
84 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; }
86 /*****************************************************************************
87 * Local duplication functions, and local deallocation functions
88 *****************************************************************************/
89 static void DupDummy( vlc_value_t *p_val ) { (void)p_val; /* unused */ }
90 static void DupString( vlc_value_t *p_val )
92 p_val->psz_string = strdup( p_val->psz_string ? p_val->psz_string : "" );
95 static void FreeDummy( vlc_value_t *p_val ) { (void)p_val; /* unused */ }
96 static void FreeString( vlc_value_t *p_val ) { free( p_val->psz_string ); }
98 static void FreeList( vlc_value_t *p_val )
101 for( i = 0; i < p_val->p_list->i_count; i++ )
103 switch( p_val->p_list->pi_types[i] & VLC_VAR_CLASS )
106 FreeString( &p_val->p_list->p_values[i] );
113 if( p_val->p_list->i_count )
115 free( p_val->p_list->p_values );
116 free( p_val->p_list->pi_types );
118 free( p_val->p_list );
121 static const struct variable_ops_t
122 void_ops = { NULL, DupDummy, FreeDummy, },
123 addr_ops = { CmpAddress, DupDummy, FreeDummy, },
124 bool_ops = { CmpBool, DupDummy, FreeDummy, },
125 float_ops = { CmpFloat, DupDummy, FreeDummy, },
126 int_ops = { CmpInt, DupDummy, FreeDummy, },
127 string_ops = { CmpString, DupString, FreeString, },
128 time_ops = { CmpTime, DupDummy, FreeDummy, },
129 coords_ops = { NULL, DupDummy, FreeDummy, };
131 /*****************************************************************************
133 *****************************************************************************/
134 static void WaitUnused ( vlc_object_t *, variable_t * );
136 static void CheckValue ( variable_t *, vlc_value_t * );
138 static int TriggerCallback( vlc_object_t *, variable_t *, const char *,
141 static int varcmp( const void *a, const void *b )
143 const variable_t *va = a, *vb = b;
145 /* psz_name must be first */
146 assert( va == (const void *)&va->psz_name );
147 return strcmp( va->psz_name, vb->psz_name );
150 static variable_t *Lookup( vlc_object_t *obj, const char *psz_name )
152 vlc_object_internals_t *priv = vlc_internals( obj );
155 vlc_assert_locked( &priv->var_lock );
156 pp_var = tfind( &psz_name, &priv->var_root, varcmp );
157 return (pp_var != NULL) ? *pp_var : NULL;
160 static void Destroy( variable_t *p_var )
162 p_var->ops->pf_free( &p_var->val );
163 if( p_var->choices.i_count )
165 for( int i = 0 ; i < p_var->choices.i_count ; i++ )
167 p_var->ops->pf_free( &p_var->choices.p_values[i] );
168 free( p_var->choices_text.p_values[i].psz_string );
170 free( p_var->choices.p_values );
171 free( p_var->choices_text.p_values );
174 for (int i = 0; i < p_var->i_entries; i++)
176 const char *file = "?", *symbol = "?";
177 const void *addr = p_var->p_entries[i].pf_callback;
181 if (dladdr (addr, &info))
183 if (info.dli_fname) file = info.dli_fname;
184 if (info.dli_sname) symbol = info.dli_sname;
187 fprintf (stderr, "Error: callback on \"%s\" dangling %s(%s)[%p]\n",
188 p_var->psz_name, file, symbol, addr);
192 free( p_var->psz_name );
193 free( p_var->psz_text );
194 free( p_var->p_entries );
200 * Initialize a vlc variable
202 * We hash the given string and insert it into the sorted list. The insertion
203 * may require slow memory copies, but think about what we gain in the log(n)
204 * lookup phase when setting/getting the variable value!
206 * \param p_this The object in which to create the variable
207 * \param psz_name The name of the variable
208 * \param i_type The variables type. Must be one of \ref var_type combined with
209 * zero or more \ref var_flags
211 int var_Create( vlc_object_t *p_this, const char *psz_name, int i_type )
215 variable_t *p_var = calloc( 1, sizeof( *p_var ) );
219 p_var->psz_name = strdup( psz_name );
220 p_var->psz_text = NULL;
222 p_var->i_type = i_type & ~VLC_VAR_DOINHERIT;
226 p_var->i_default = -1;
227 p_var->choices.i_count = 0;
228 p_var->choices.p_values = NULL;
229 p_var->choices_text.i_count = 0;
230 p_var->choices_text.p_values = NULL;
232 p_var->b_incallback = false;
233 p_var->i_entries = 0;
234 p_var->p_entries = NULL;
236 /* Always initialize the variable, even if it is a list variable; this
237 * will lead to errors if the variable is not initialized, but it will
238 * not cause crashes in the variable handling. */
239 switch( i_type & VLC_VAR_CLASS )
242 p_var->ops = &bool_ops;
243 p_var->val.b_bool = false;
245 case VLC_VAR_INTEGER:
246 p_var->ops = &int_ops;
247 p_var->val.i_int = 0;
250 p_var->ops = &string_ops;
251 p_var->val.psz_string = NULL;
254 p_var->ops = &float_ops;
255 p_var->val.f_float = 0.0;
258 p_var->ops = &time_ops;
259 p_var->val.i_time = 0;
262 p_var->ops = &coords_ops;
263 p_var->val.coords.x = p_var->val.coords.y = 0;
265 case VLC_VAR_ADDRESS:
266 p_var->ops = &addr_ops;
267 p_var->val.p_address = NULL;
270 p_var->ops = &void_ops;
276 if( i_type & VLC_VAR_DOINHERIT )
278 if( var_Inherit( p_this, psz_name, i_type, &p_var->val ) )
279 msg_Err( p_this, "cannot inherit value for %s", psz_name );
280 else if( i_type & VLC_VAR_HASCHOICE )
282 /* We must add the inherited value to our choice list */
283 p_var->i_default = 0;
285 INSERT_ELEM( p_var->choices.p_values, p_var->choices.i_count,
287 INSERT_ELEM( p_var->choices_text.p_values,
288 p_var->choices_text.i_count, 0, p_var->val );
289 p_var->ops->pf_dup( &p_var->choices.p_values[0] );
290 p_var->choices_text.p_values[0].psz_string = NULL;
294 vlc_object_internals_t *p_priv = vlc_internals( p_this );
295 variable_t **pp_var, *p_oldvar;
296 int ret = VLC_SUCCESS;
298 vlc_mutex_lock( &p_priv->var_lock );
300 pp_var = tsearch( p_var, &p_priv->var_root, varcmp );
301 if( unlikely(pp_var == NULL) )
303 else if( (p_oldvar = *pp_var) == p_var ) /* Variable create */
304 p_var = NULL; /* Variable created */
305 else /* Variable already exists */
307 assert (((i_type ^ p_oldvar->i_type) & VLC_VAR_CLASS) == 0);
309 p_oldvar->i_type |= i_type & (VLC_VAR_ISCOMMAND|VLC_VAR_HASCHOICE);
311 vlc_mutex_unlock( &p_priv->var_lock );
313 /* If we did not need to create a new variable, free everything... */
321 * Destroy a vlc variable
323 * Look for the variable and destroy it if it is found. As in var_Create we
324 * do a call to memmove() but we have performance counterparts elsewhere.
326 * \param p_this The object that holds the variable
327 * \param psz_name The name of the variable
329 int var_Destroy( vlc_object_t *p_this, const char *psz_name )
335 vlc_object_internals_t *p_priv = vlc_internals( p_this );
337 vlc_mutex_lock( &p_priv->var_lock );
339 p_var = Lookup( p_this, psz_name );
342 vlc_mutex_unlock( &p_priv->var_lock );
346 WaitUnused( p_this, p_var );
348 if( --p_var->i_usage == 0 )
349 tdelete( p_var, &p_priv->var_root, varcmp );
352 vlc_mutex_unlock( &p_priv->var_lock );
359 static void CleanupVar( void *var )
364 void var_DestroyAll( vlc_object_t *obj )
366 vlc_object_internals_t *priv = vlc_internals( obj );
368 tdestroy( priv->var_root, CleanupVar );
369 priv->var_root = NULL;
374 * Perform an action on a variable
376 * \param p_this The object that holds the variable
377 * \param psz_name The name of the variable
378 * \param i_action The action to perform. Must be one of \ref var_action
379 * \param p_val First action parameter
380 * \param p_val2 Second action parameter
382 int var_Change( vlc_object_t *p_this, const char *psz_name,
383 int i_action, vlc_value_t *p_val, vlc_value_t *p_val2 )
385 int ret = VLC_SUCCESS;
392 vlc_object_internals_t *p_priv = vlc_internals( p_this );
394 vlc_mutex_lock( &p_priv->var_lock );
396 p_var = Lookup( p_this, psz_name );
399 vlc_mutex_unlock( &p_priv->var_lock );
406 if( p_var->i_type & VLC_VAR_HASMIN )
408 p_var->ops->pf_free( &p_var->min );
410 p_var->i_type |= VLC_VAR_HASMIN;
412 p_var->ops->pf_dup( &p_var->min );
413 CheckValue( p_var, &p_var->val );
416 if( p_var->i_type & VLC_VAR_HASMIN )
422 if( p_var->i_type & VLC_VAR_HASMAX )
424 p_var->ops->pf_free( &p_var->max );
426 p_var->i_type |= VLC_VAR_HASMAX;
428 p_var->ops->pf_dup( &p_var->max );
429 CheckValue( p_var, &p_var->val );
432 if( p_var->i_type & VLC_VAR_HASMAX )
437 case VLC_VAR_SETSTEP:
438 if( p_var->i_type & VLC_VAR_HASSTEP )
440 p_var->ops->pf_free( &p_var->step );
442 p_var->i_type |= VLC_VAR_HASSTEP;
443 p_var->step = *p_val;
444 p_var->ops->pf_dup( &p_var->step );
445 CheckValue( p_var, &p_var->val );
447 case VLC_VAR_GETSTEP:
448 if( p_var->i_type & VLC_VAR_HASSTEP )
449 *p_val = p_var->step;
453 case VLC_VAR_ADDCHOICE:
455 int i = p_var->choices.i_count;
457 INSERT_ELEM( p_var->choices.p_values, p_var->choices.i_count,
459 INSERT_ELEM( p_var->choices_text.p_values,
460 p_var->choices_text.i_count, i, *p_val );
461 p_var->ops->pf_dup( &p_var->choices.p_values[i] );
462 p_var->choices_text.p_values[i].psz_string =
463 ( p_val2 && p_val2->psz_string ) ?
464 strdup( p_val2->psz_string ) : NULL;
466 CheckValue( p_var, &p_var->val );
469 case VLC_VAR_DELCHOICE:
473 for( i = 0 ; i < p_var->choices.i_count ; i++ )
474 if( p_var->ops->pf_cmp( p_var->choices.p_values[i], *p_val ) == 0 )
477 if( i == p_var->choices.i_count )
480 vlc_mutex_unlock( &p_priv->var_lock );
484 if( p_var->i_default > i )
486 else if( p_var->i_default == i )
487 p_var->i_default = -1;
489 p_var->ops->pf_free( &p_var->choices.p_values[i] );
490 free( p_var->choices_text.p_values[i].psz_string );
491 REMOVE_ELEM( p_var->choices.p_values, p_var->choices.i_count, i );
492 REMOVE_ELEM( p_var->choices_text.p_values,
493 p_var->choices_text.i_count, i );
495 CheckValue( p_var, &p_var->val );
498 case VLC_VAR_CHOICESCOUNT:
499 p_val->i_int = p_var->choices.i_count;
501 case VLC_VAR_CLEARCHOICES:
502 for( int i = 0 ; i < p_var->choices.i_count ; i++ )
503 p_var->ops->pf_free( &p_var->choices.p_values[i] );
504 for( int i = 0 ; i < p_var->choices_text.i_count ; i++ )
505 free( p_var->choices_text.p_values[i].psz_string );
507 if( p_var->choices.i_count ) free( p_var->choices.p_values );
508 if( p_var->choices_text.i_count ) free( p_var->choices_text.p_values );
510 p_var->choices.i_count = 0;
511 p_var->choices.p_values = NULL;
512 p_var->choices_text.i_count = 0;
513 p_var->choices_text.p_values = NULL;
514 p_var->i_default = -1;
516 case VLC_VAR_SETDEFAULT:
519 /* FIXME: the list is sorted, dude. Use something cleverer. */
520 for( i = 0 ; i < p_var->choices.i_count ; i++ )
521 if( p_var->ops->pf_cmp( p_var->choices.p_values[i], *p_val ) == 0 )
524 if( i == p_var->choices.i_count )
528 p_var->i_default = i;
529 CheckValue( p_var, &p_var->val );
532 case VLC_VAR_SETVALUE:
533 /* Duplicate data if needed */
535 p_var->ops->pf_dup( &newval );
536 /* Backup needed stuff */
538 /* Check boundaries and list */
539 CheckValue( p_var, &newval );
540 /* Set the variable */
542 /* Free data if needed */
543 p_var->ops->pf_free( &oldval );
545 case VLC_VAR_GETCHOICES:
546 case VLC_VAR_GETLIST:
547 p_val->p_list = malloc( sizeof(vlc_list_t) );
548 if( p_val2 ) p_val2->p_list = malloc( sizeof(vlc_list_t) );
549 if( p_var->choices.i_count )
551 p_val->p_list->p_values = malloc( p_var->choices.i_count
552 * sizeof(vlc_value_t) );
553 p_val->p_list->pi_types = malloc( p_var->choices.i_count
557 p_val2->p_list->p_values =
558 malloc( p_var->choices.i_count * sizeof(vlc_value_t) );
559 p_val2->p_list->pi_types =
560 malloc( p_var->choices.i_count * sizeof(int) );
563 p_val->p_list->i_count = p_var->choices.i_count;
564 if( p_val2 ) p_val2->p_list->i_count = p_var->choices.i_count;
565 for( int i = 0 ; i < p_var->choices.i_count ; i++ )
567 p_val->p_list->p_values[i] = p_var->choices.p_values[i];
568 p_val->p_list->pi_types[i] = p_var->i_type;
569 p_var->ops->pf_dup( &p_val->p_list->p_values[i] );
572 p_val2->p_list->p_values[i].psz_string =
573 p_var->choices_text.p_values[i].psz_string ?
574 strdup(p_var->choices_text.p_values[i].psz_string) : NULL;
575 p_val2->p_list->pi_types[i] = VLC_VAR_STRING;
579 case VLC_VAR_SETTEXT:
580 free( p_var->psz_text );
581 if( p_val && p_val->psz_string )
582 p_var->psz_text = strdup( p_val->psz_string );
584 p_var->psz_text = NULL;
586 case VLC_VAR_GETTEXT:
587 p_val->psz_string = p_var->psz_text ? strdup( p_var->psz_text )
590 case VLC_VAR_SETISCOMMAND:
591 p_var->i_type |= VLC_VAR_ISCOMMAND;
598 vlc_mutex_unlock( &p_priv->var_lock );
605 * Perform a Get and Set on a variable
607 * \param p_this: The object that hold the variable
608 * \param psz_name: the name of the variable
609 * \param i_action: the action to perform
610 * \param p_val: The action parameter
611 * \return vlc error codes
613 int var_GetAndSet( vlc_object_t *p_this, const char *psz_name, int i_action,
623 vlc_object_internals_t *p_priv = vlc_internals( p_this );
625 vlc_mutex_lock( &p_priv->var_lock );
626 p_var = Lookup( p_this, psz_name );
629 vlc_mutex_unlock( &p_priv->var_lock );
633 WaitUnused( p_this, p_var );
635 /* Duplicated data if needed */
636 //p_var->ops->pf_dup( &val );
638 /* Backup needed stuff */
641 /* depending of the action requiered */
644 case VLC_VAR_BOOL_TOGGLE:
645 assert( ( p_var->i_type & VLC_VAR_BOOL ) == VLC_VAR_BOOL );
646 p_var->val.b_bool = !p_var->val.b_bool;
648 case VLC_VAR_INTEGER_ADD:
649 assert( ( p_var->i_type & VLC_VAR_INTEGER ) == VLC_VAR_INTEGER );
650 p_var->val.i_int += p_val->i_int;
652 case VLC_VAR_INTEGER_OR:
653 assert( ( p_var->i_type & VLC_VAR_INTEGER ) == VLC_VAR_INTEGER );
654 p_var->val.i_int |= p_val->i_int;
656 case VLC_VAR_INTEGER_NAND:
657 assert( ( p_var->i_type & VLC_VAR_INTEGER ) == VLC_VAR_INTEGER );
658 p_var->val.i_int &= ~p_val->i_int;
661 vlc_mutex_unlock( &p_priv->var_lock );
665 /* Check boundaries */
666 CheckValue( p_var, &p_var->val );
669 /* Deal with callbacks.*/
670 i_ret = TriggerCallback( p_this, p_var, psz_name, oldval );
672 vlc_mutex_unlock( &p_priv->var_lock );
679 * Request a variable's type
681 * \return The variable type if it exists, or 0 if the
682 * variable could not be found.
685 int var_Type( vlc_object_t *p_this, const char *psz_name )
692 vlc_object_internals_t *p_priv = vlc_internals( p_this );
694 vlc_mutex_lock( &p_priv->var_lock );
696 p_var = Lookup( p_this, psz_name );
698 i_type = p_var->i_type;
700 vlc_mutex_unlock( &p_priv->var_lock );
705 #undef var_SetChecked
706 int var_SetChecked( vlc_object_t *p_this, const char *psz_name,
707 int expected_type, vlc_value_t val )
709 int i_ret = VLC_SUCCESS;
715 vlc_object_internals_t *p_priv = vlc_internals( p_this );
717 vlc_mutex_lock( &p_priv->var_lock );
719 p_var = Lookup( p_this, psz_name );
722 vlc_mutex_unlock( &p_priv->var_lock );
726 assert( expected_type == 0 ||
727 (p_var->i_type & VLC_VAR_CLASS) == expected_type );
728 assert ((p_var->i_type & VLC_VAR_CLASS) != VLC_VAR_VOID);
730 WaitUnused( p_this, p_var );
732 /* Duplicate data if needed */
733 p_var->ops->pf_dup( &val );
735 /* Backup needed stuff */
738 /* Check boundaries and list */
739 CheckValue( p_var, &val );
741 /* Set the variable */
744 /* Deal with callbacks */
745 i_ret = TriggerCallback( p_this, p_var, psz_name, oldval );
747 /* Free data if needed */
748 p_var->ops->pf_free( &oldval );
750 vlc_mutex_unlock( &p_priv->var_lock );
757 * Set a variable's value
759 * \param p_this The object that hold the variable
760 * \param psz_name The name of the variable
761 * \param val the value to set
763 int var_Set( vlc_object_t *p_this, const char *psz_name, vlc_value_t val )
765 return var_SetChecked( p_this, psz_name, 0, val );
768 #undef var_GetChecked
769 int var_GetChecked( vlc_object_t *p_this, const char *psz_name,
770 int expected_type, vlc_value_t *p_val )
774 vlc_object_internals_t *p_priv = vlc_internals( p_this );
776 int err = VLC_SUCCESS;
778 vlc_mutex_lock( &p_priv->var_lock );
780 p_var = Lookup( p_this, psz_name );
783 assert( expected_type == 0 ||
784 (p_var->i_type & VLC_VAR_CLASS) == expected_type );
785 assert ((p_var->i_type & VLC_VAR_CLASS) != VLC_VAR_VOID);
787 /* Really get the variable */
790 /* Duplicate value if needed */
791 p_var->ops->pf_dup( p_val );
796 vlc_mutex_unlock( &p_priv->var_lock );
802 * Get a variable's value
804 * \param p_this The object that holds the variable
805 * \param psz_name The name of the variable
806 * \param p_val Pointer to a vlc_value_t that will hold the variable's value
807 * after the function is finished
809 int var_Get( vlc_object_t *p_this, const char *psz_name, vlc_value_t *p_val )
811 return var_GetChecked( p_this, psz_name, 0, p_val );
814 #undef var_AddCallback
816 * Register a callback in a variable
818 * We store a function pointer that will be called upon variable
821 * \param p_this The object that holds the variable
822 * \param psz_name The name of the variable
823 * \param pf_callback The function pointer
824 * \param p_data A generic pointer that will be passed as the last
825 * argument to the callback function.
827 * \warning The callback function is run in the thread that calls var_Set on
828 * the variable. Use proper locking. This thread may not have much
829 * time to spare, so keep callback functions short.
831 int var_AddCallback( vlc_object_t *p_this, const char *psz_name,
832 vlc_callback_t pf_callback, void *p_data )
835 callback_entry_t entry;
839 vlc_object_internals_t *p_priv = vlc_internals( p_this );
841 entry.pf_callback = pf_callback;
842 entry.p_data = p_data;
844 vlc_mutex_lock( &p_priv->var_lock );
846 p_var = Lookup( p_this, psz_name );
849 vlc_mutex_unlock( &p_priv->var_lock );
850 msg_Err( p_this, "cannot add callback %p to nonexistent "
851 "variable '%s'", pf_callback, psz_name );
855 WaitUnused( p_this, p_var );
856 INSERT_ELEM( p_var->p_entries,
861 vlc_mutex_unlock( &p_priv->var_lock );
866 #undef var_DelCallback
868 * Remove a callback from a variable
870 * pf_callback and p_data have to be given again, because different objects
871 * might have registered the same callback function.
873 int var_DelCallback( vlc_object_t *p_this, const char *psz_name,
874 vlc_callback_t pf_callback, void *p_data )
879 bool b_found_similar = false;
884 vlc_object_internals_t *p_priv = vlc_internals( p_this );
886 vlc_mutex_lock( &p_priv->var_lock );
888 p_var = Lookup( p_this, psz_name );
891 vlc_mutex_unlock( &p_priv->var_lock );
895 WaitUnused( p_this, p_var );
897 for( i_entry = p_var->i_entries ; i_entry-- ; )
899 if( p_var->p_entries[i_entry].pf_callback == pf_callback
900 && p_var->p_entries[i_entry].p_data == p_data )
905 else if( p_var->p_entries[i_entry].pf_callback == pf_callback )
906 b_found_similar = true;
913 if( b_found_similar )
914 fprintf( stderr, "Calling var_DelCallback for '%s' with the same "
915 "function but not the same data.", psz_name );
918 vlc_mutex_unlock( &p_priv->var_lock );
922 REMOVE_ELEM( p_var->p_entries, p_var->i_entries, i_entry );
924 vlc_mutex_unlock( &p_priv->var_lock );
929 #undef var_TriggerCallback
931 * Trigger callback on a variable
933 * \param p_this The object that hold the variable
934 * \param psz_name The name of the variable
936 int var_TriggerCallback( vlc_object_t *p_this, const char *psz_name )
943 vlc_object_internals_t *p_priv = vlc_internals( p_this );
945 vlc_mutex_lock( &p_priv->var_lock );
947 p_var = Lookup( p_this, psz_name );
950 vlc_mutex_unlock( &p_priv->var_lock );
954 WaitUnused( p_this, p_var );
956 /* Deal with callbacks. Tell we're in a callback, release the lock,
957 * call stored functions, retake the lock. */
958 i_ret = TriggerCallback( p_this, p_var, psz_name, p_var->val );
960 vlc_mutex_unlock( &p_priv->var_lock );
964 /** Parse a stringified option
965 * This function parse a string option and create the associated object
967 * The option must be of the form "[no[-]]foo[=bar]" where foo is the
968 * option name and bar is the value of the option.
969 * \param p_obj the object in which the variable must be created
970 * \param psz_option the option to parse
971 * \param trusted whether the option is set by a trusted input or not
974 void var_OptionParse( vlc_object_t *p_obj, const char *psz_option,
977 char *psz_name, *psz_value;
982 val.psz_string = NULL;
984 /* It's too much of a hassle to remove the ':' when we parse
986 if( psz_option[0] == ':' )
992 psz_name = strdup( psz_option );
993 if( psz_name == NULL )
996 psz_value = strchr( psz_name, '=' );
997 if( psz_value != NULL )
1000 i_type = config_GetType( p_obj, psz_name );
1001 if( !i_type && !psz_value )
1003 /* check for "no-foo" or "nofoo" */
1004 if( !strncmp( psz_name, "no-", 3 ) )
1006 memmove( psz_name, psz_name + 3, strlen(psz_name) + 1 - 3 );
1008 else if( !strncmp( psz_name, "no", 2 ) )
1010 memmove( psz_name, psz_name + 2, strlen(psz_name) + 1 - 2 );
1012 else goto cleanup; /* Option doesn't exist */
1015 i_type = config_GetType( p_obj, psz_name );
1017 if( !i_type ) goto cleanup; /* Option doesn't exist */
1019 if( ( i_type != VLC_VAR_BOOL ) &&
1020 ( !psz_value || !*psz_value ) ) goto cleanup; /* Invalid value */
1022 /* check if option is unsafe */
1023 if( !trusted && !config_IsSafe( psz_name ) )
1025 msg_Err( p_obj, "unsafe option \"%s\" has been ignored for "
1026 "security reasons", psz_name );
1031 /* Create the variable in the input object.
1032 * Children of the input object will be able to retreive this value
1033 * thanks to the inheritance property of the object variables. */
1034 var_Create( p_obj, psz_name, i_type );
1039 val.b_bool = !b_isno;
1042 case VLC_VAR_INTEGER:
1043 val.i_int = strtoll( psz_value, NULL, 0 );
1047 val.f_float = us_atof( psz_value );
1050 case VLC_VAR_STRING:
1051 val.psz_string = psz_value;
1058 var_Set( p_obj, psz_name, val );
1064 #undef var_LocationParse
1066 * Parses a set of colon-separated or semicolon-separated
1067 * <variable name>=<value> pairs.
1068 * Some access (or access_demux) plugins uses this scheme
1069 * in media resource location.
1070 * @note Only trusted/safe variables are allowed. This is intended.
1072 * @warning Only use this for plugins implementing VLC-specific resource
1073 * location schemes. This would not make any sense for standardized ones.
1075 * @param obj VLC object on which to set variables (and emit error messages)
1076 * @param mrl string to parse
1077 * @param pref prefix to prepend to option names in the string
1079 * @return VLC_ENOMEM on error, VLC_SUCCESS on success.
1081 int var_LocationParse (vlc_object_t *obj, const char *mrl, const char *pref)
1083 int ret = VLC_SUCCESS;
1084 size_t preflen = strlen (pref) + 1;
1086 assert(mrl != NULL);
1087 while (*mrl != '\0')
1089 mrl += strspn (mrl, ":;"); /* skip leading colon(s) */
1091 size_t len = strcspn (mrl, ":;");
1092 char *buf = malloc (preflen + len);
1094 if (likely(buf != NULL))
1096 /* NOTE: this does not support the "no-<varname>" bool syntax. */
1097 /* DO NOT use asprintf() here; it won't work! Think again. */
1098 snprintf (buf, preflen + len, "%s%s", pref, mrl);
1099 var_OptionParse (obj, buf, false);
1111 * Waits until the variable is inactive (i.e. not executing a callback)
1113 static void WaitUnused( vlc_object_t *p_this, variable_t *p_var )
1115 vlc_object_internals_t *p_priv = vlc_internals( p_this );
1117 mutex_cleanup_push( &p_priv->var_lock );
1118 while( p_var->b_incallback )
1119 vlc_cond_wait( &p_priv->var_wait, &p_priv->var_lock );
1123 /*****************************************************************************
1124 * CheckValue: check that a value is valid wrt. a variable
1125 *****************************************************************************
1126 * This function checks p_val's value against p_var's limitations such as
1127 * minimal and maximal value, step, in-list position, and modifies p_val if
1129 ****************************************************************************/
1130 static void CheckValue ( variable_t *p_var, vlc_value_t *p_val )
1132 /* Check that our variable is in the list */
1133 if( p_var->i_type & VLC_VAR_HASCHOICE && p_var->choices.i_count )
1137 /* This list is not sorted so go throug it (this is a small list) */
1138 for( i = p_var->choices.i_count ; i-- ; )
1140 if( p_var->ops->pf_cmp( *p_val, p_var->choices.p_values[i] ) == 0 )
1146 /* If not found, change it to anything vaguely valid */
1149 /* Free the old variable, get the new one, dup it */
1150 p_var->ops->pf_free( p_val );
1151 *p_val = p_var->choices.p_values[p_var->i_default >= 0
1152 ? p_var->i_default : 0 ];
1153 p_var->ops->pf_dup( p_val );
1157 /* Check that our variable is within the bounds */
1158 switch( p_var->i_type & VLC_VAR_TYPE )
1160 case VLC_VAR_INTEGER:
1161 if( p_var->i_type & VLC_VAR_HASSTEP && p_var->step.i_int
1162 && (p_val->i_int % p_var->step.i_int) )
1164 p_val->i_int = (p_val->i_int + (p_var->step.i_int / 2))
1165 / p_var->step.i_int * p_var->step.i_int;
1167 if( p_var->i_type & VLC_VAR_HASMIN
1168 && p_val->i_int < p_var->min.i_int )
1170 p_val->i_int = p_var->min.i_int;
1172 if( p_var->i_type & VLC_VAR_HASMAX
1173 && p_val->i_int > p_var->max.i_int )
1175 p_val->i_int = p_var->max.i_int;
1179 if( p_var->i_type & VLC_VAR_HASSTEP && p_var->step.f_float )
1181 float f_round = p_var->step.f_float * (float)(int)( 0.5 +
1182 p_val->f_float / p_var->step.f_float );
1183 if( p_val->f_float != f_round )
1185 p_val->f_float = f_round;
1188 if( p_var->i_type & VLC_VAR_HASMIN
1189 && p_val->f_float < p_var->min.f_float )
1191 p_val->f_float = p_var->min.f_float;
1193 if( p_var->i_type & VLC_VAR_HASMAX
1194 && p_val->f_float > p_var->max.f_float )
1196 p_val->f_float = p_var->max.f_float;
1206 * Finds the value of a variable. If the specified object does not hold a
1207 * variable with the specified name, try the parent object, and iterate until
1208 * the top of the tree. If no match is found, the value is read from the
1211 int var_Inherit( vlc_object_t *p_this, const char *psz_name, int i_type,
1212 vlc_value_t *p_val )
1214 i_type &= VLC_VAR_CLASS;
1215 for( vlc_object_t *obj = p_this; obj != NULL; obj = obj->p_parent )
1217 if( var_GetChecked( obj, psz_name, i_type, p_val ) == VLC_SUCCESS )
1221 /* else take value from config */
1222 switch( i_type & VLC_VAR_CLASS )
1224 case VLC_VAR_STRING:
1225 p_val->psz_string = config_GetPsz( p_this, psz_name );
1226 if( !p_val->psz_string ) p_val->psz_string = strdup("");
1229 p_val->f_float = config_GetFloat( p_this, psz_name );
1231 case VLC_VAR_INTEGER:
1232 p_val->i_int = config_GetInt( p_this, psz_name );
1235 p_val->b_bool = config_GetInt( p_this, psz_name );
1237 case VLC_VAR_ADDRESS:
1240 msg_Warn( p_this, "Could not inherit value for var %s "
1241 "from config. Invalid Type", psz_name );
1244 /*msg_Dbg( p_this, "Inherited value for var %s from config", psz_name );*/
1250 * It inherits a string as an unsigned rational number (it also accepts basic
1253 * It returns an error if the rational number cannot be parsed (0/0 is valid).
1254 * The rational is already reduced.
1256 int (var_InheritURational)(vlc_object_t *object,
1257 unsigned *num, unsigned *den,
1265 char *tmp = var_InheritString(object, var);
1270 unsigned n = strtol(tmp, &next, 0);
1271 unsigned d = strtol(*next ? &next[1] : "0", NULL, 0);
1274 /* Interpret as a float number */
1275 double r = us_atof(tmp);
1290 vlc_ureduce(num, den, n, d, 0);
1297 return VLC_EGENERIC;
1300 /**********************************************************************
1301 * Trigger the callbacks.
1302 * Tell we're in a callback, release the lock, call stored functions,
1304 **********************************************************************/
1305 static int TriggerCallback( vlc_object_t *p_this, variable_t *p_var,
1306 const char *psz_name, vlc_value_t oldval )
1310 int i_entries = p_var->i_entries;
1311 if( i_entries == 0 )
1314 callback_entry_t *p_entries = p_var->p_entries;
1315 vlc_object_internals_t *p_priv = vlc_internals( p_this );
1317 assert( !p_var->b_incallback );
1318 p_var->b_incallback = true;
1319 vlc_mutex_unlock( &p_priv->var_lock );
1321 /* The real calls */
1322 for( ; i_entries-- ; )
1324 p_entries[i_entries].pf_callback( p_this, psz_name, oldval, p_var->val,
1325 p_entries[i_entries].p_data );
1328 vlc_mutex_lock( &p_priv->var_lock );
1329 p_var->b_incallback = false;
1330 vlc_cond_broadcast( &p_priv->var_wait );
1336 /**********************************************************************
1337 * Execute a var command on an object identified by its name
1338 **********************************************************************/
1339 int var_Command( vlc_object_t *p_this, const char *psz_name,
1340 const char *psz_cmd, const char *psz_arg, char **psz_msg )
1342 vlc_object_t *p_obj = vlc_object_find_name( p_this->p_libvlc,
1349 *psz_msg = strdup( "Unknown destination object." );
1353 i_type = var_Type( p_obj, psz_cmd );
1354 if( !( i_type&VLC_VAR_ISCOMMAND ) )
1356 vlc_object_release( p_obj );
1358 *psz_msg = strdup( "Variable doesn't exist or isn't a command." );
1359 return VLC_EGENERIC;
1362 i_type &= VLC_VAR_CLASS;
1365 case VLC_VAR_INTEGER:
1366 i_ret = var_SetInteger( p_obj, psz_cmd, atoi( psz_arg ) );
1369 i_ret = var_SetFloat( p_obj, psz_cmd, us_atof( psz_arg ) );
1371 case VLC_VAR_STRING:
1372 i_ret = var_SetString( p_obj, psz_cmd, psz_arg );
1375 i_ret = var_SetBool( p_obj, psz_cmd, atoi( psz_arg ) );
1378 i_ret = VLC_EGENERIC;
1382 vlc_object_release( p_obj );
1386 if( asprintf( psz_msg, "%s on object %s returned %i (%s)",
1387 psz_cmd, psz_name, i_ret, vlc_error( i_ret ) ) == -1)
1396 * Free a list and the associated strings
1397 * @param p_val: the list variable
1398 * @param p_val2: the variable associated or NULL
1400 void var_FreeList( vlc_value_t *p_val, vlc_value_t *p_val2 )
1403 if( p_val2 && p_val2->p_list )
1405 for( int i = 0; i < p_val2->p_list->i_count; i++ )
1406 free( p_val2->p_list->p_values[i].psz_string );
1407 if( p_val2->p_list->i_count )
1409 free( p_val2->p_list->p_values );
1410 free( p_val2->p_list->pi_types );
1412 free( p_val2->p_list );