1 /*****************************************************************************
2 * variables.c: routines for object variables handling
3 *****************************************************************************
4 * Copyright (C) 2002-2006 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 "variables.h"
36 #include "vlc_interface.h"
38 /*****************************************************************************
40 *****************************************************************************/
41 struct callback_entry_t
43 vlc_callback_t pf_callback;
47 /*****************************************************************************
48 * Local comparison functions, returns 0 if v == w, < 0 if v < w, > 0 if v > w
49 *****************************************************************************/
50 static int CmpBool( vlc_value_t v, vlc_value_t w ) { return v.b_bool ? w.b_bool ? 0 : 1 : w.b_bool ? -1 : 0; }
51 static int CmpInt( vlc_value_t v, vlc_value_t w ) { return v.i_int == w.i_int ? 0 : v.i_int > w.i_int ? 1 : -1; }
52 static int CmpTime( vlc_value_t v, vlc_value_t w )
54 return v.i_time == w.i_time ? 0 : v.i_time > w.i_time ? 1 : -1;
56 static int CmpString( vlc_value_t v, vlc_value_t w )
59 return !w.psz_string ? 0 : -1;
61 return !w.psz_string ? 1 : strcmp( v.psz_string, w.psz_string );
63 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; }
64 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; }
66 /*****************************************************************************
67 * Local duplication functions, and local deallocation functions
68 *****************************************************************************/
69 static void DupDummy( vlc_value_t *p_val ) { (void)p_val; /* unused */ }
70 static void DupString( vlc_value_t *p_val ) { if( p_val->psz_string ) p_val->psz_string = strdup( p_val->psz_string ); }
72 static void DupList( vlc_value_t *p_val )
75 vlc_list_t *p_list = malloc( sizeof(vlc_list_t) );
77 p_list->i_count = p_val->p_list->i_count;
78 if( p_val->p_list->i_count )
80 p_list->p_values = malloc( p_list->i_count * sizeof(vlc_value_t) );
81 p_list->pi_types = malloc( p_list->i_count * sizeof(int) );
85 p_list->p_values = NULL;
86 p_list->pi_types = NULL;
89 for( i = 0; i < p_list->i_count; i++ )
91 p_list->p_values[i] = p_val->p_list->p_values[i];
92 p_list->pi_types[i] = p_val->p_list->pi_types[i];
93 switch( p_val->p_list->pi_types[i] & VLC_VAR_CLASS )
97 DupString( &p_list->p_values[i] );
104 p_val->p_list = p_list;
107 static void FreeDummy( vlc_value_t *p_val ) { (void)p_val; /* unused */ }
108 static void FreeString( vlc_value_t *p_val ) { free( p_val->psz_string ); }
109 static void FreeMutex( vlc_value_t *p_val ) { vlc_mutex_destroy( (vlc_mutex_t*)p_val->p_address ); free( p_val->p_address ); }
111 static void FreeList( vlc_value_t *p_val )
114 for( i = 0; i < p_val->p_list->i_count; i++ )
116 switch( p_val->p_list->pi_types[i] & VLC_VAR_CLASS )
119 FreeString( &p_val->p_list->p_values[i] );
122 FreeMutex( &p_val->p_list->p_values[i] );
129 if( p_val->p_list->i_count )
131 free( p_val->p_list->p_values );
132 free( p_val->p_list->pi_types );
134 free( p_val->p_list );
137 static const struct variable_ops_t
138 addr_ops = { CmpAddress, DupDummy, FreeDummy, },
139 bool_ops = { CmpBool, DupDummy, FreeDummy, },
140 float_ops = { CmpFloat, DupDummy, FreeDummy, },
141 int_ops = { CmpInt, DupDummy, FreeDummy, },
142 list_ops = { CmpAddress, DupList, FreeList, },
143 mutex_ops = { CmpAddress, DupDummy, FreeMutex, },
144 string_ops = { CmpString, DupString, FreeString, },
145 time_ops = { CmpTime, DupDummy, FreeDummy, };
147 /*****************************************************************************
149 *****************************************************************************/
150 static int GetUnused ( vlc_object_t *, const char * );
151 static uint32_t HashString ( const char * );
152 static int Insert ( variable_t *, int, const char * );
153 static int InsertInner ( variable_t *, int, uint32_t );
154 static int Lookup ( variable_t *, int, const char * );
155 static int LookupInner ( variable_t *, int, uint32_t );
157 static void CheckValue ( variable_t *, vlc_value_t * );
159 static int InheritValue( vlc_object_t *, const char *, vlc_value_t *,
163 * Initialize a vlc variable
165 * We hash the given string and insert it into the sorted list. The insertion
166 * may require slow memory copies, but think about what we gain in the log(n)
167 * lookup phase when setting/getting the variable value!
169 * \param p_this The object in which to create the variable
170 * \param psz_name The name of the variable
171 * \param i_type The variables type. Must be one of \ref var_type combined with
172 * zero or more \ref var_flags
174 int __var_Create( vlc_object_t *p_this, const char *psz_name, int i_type )
178 static vlc_list_t dummy_null_list = {0, NULL, NULL};
179 vlc_object_internals_t *p_priv = vlc_internals( p_this );
181 vlc_mutex_lock( &p_priv->var_lock );
183 /* FIXME: if the variable already exists, we don't duplicate it. But we
184 * duplicate the lookups. It's not that serious, but if anyone finds some
185 * time to rework Insert() so that only one lookup has to be done, feel
187 i_new = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
191 /* If the types differ, variable creation failed. */
192 if( (i_type & ~(VLC_VAR_DOINHERIT|VLC_VAR_ISCOMMAND)) != p_priv->p_vars[i_new].i_type )
194 vlc_mutex_unlock( &p_priv->var_lock );
198 p_priv->p_vars[i_new].i_usage++;
199 if( i_type & VLC_VAR_ISCOMMAND )
200 p_priv->p_vars[i_new].i_type |= VLC_VAR_ISCOMMAND;
201 vlc_mutex_unlock( &p_priv->var_lock );
205 i_new = Insert( p_priv->p_vars, p_priv->i_vars, psz_name );
207 if( (p_priv->i_vars & 15) == 15 )
209 p_priv->p_vars = realloc( p_priv->p_vars,
210 (p_priv->i_vars+17) * sizeof(variable_t) );
213 memmove( p_priv->p_vars + i_new + 1,
214 p_priv->p_vars + i_new,
215 (p_priv->i_vars - i_new) * sizeof(variable_t) );
219 p_var = &p_priv->p_vars[i_new];
220 memset( p_var, 0, sizeof(*p_var) );
222 p_var->i_hash = HashString( psz_name );
223 p_var->psz_name = strdup( psz_name );
224 p_var->psz_text = NULL;
226 p_var->i_type = i_type & ~VLC_VAR_DOINHERIT;
227 memset( &p_var->val, 0, sizeof(vlc_value_t) );
231 p_var->i_default = -1;
232 p_var->choices.i_count = 0;
233 p_var->choices.p_values = NULL;
234 p_var->choices_text.i_count = 0;
235 p_var->choices_text.p_values = NULL;
237 p_var->b_incallback = false;
238 p_var->i_entries = 0;
239 p_var->p_entries = NULL;
241 /* Always initialize the variable, even if it is a list variable; this
242 * will lead to errors if the variable is not initialized, but it will
243 * not cause crashes in the variable handling. */
244 switch( i_type & VLC_VAR_CLASS )
247 p_var->ops = &bool_ops;
248 p_var->val.b_bool = false;
250 case VLC_VAR_INTEGER:
251 p_var->ops = &int_ops;
252 p_var->val.i_int = 0;
255 p_var->ops = &string_ops;
256 p_var->val.psz_string = NULL;
259 p_var->ops = &float_ops;
260 p_var->val.f_float = 0.0;
263 p_var->ops = &time_ops;
264 p_var->val.i_time = 0;
266 case VLC_VAR_ADDRESS:
267 p_var->ops = &addr_ops;
268 p_var->val.p_address = NULL;
271 p_var->ops = &mutex_ops;
272 p_var->val.p_address = malloc( sizeof(vlc_mutex_t) );
273 vlc_mutex_init( (vlc_mutex_t*)p_var->val.p_address );
276 p_var->ops = &list_ops;
277 p_var->val.p_list = &dummy_null_list;
281 /* Duplicate the default data we stored. */
282 p_var->ops->pf_dup( &p_var->val );
284 if( i_type & VLC_VAR_DOINHERIT )
288 if( InheritValue( p_this, psz_name, &val, p_var->i_type )
291 /* Free data if needed */
292 p_var->ops->pf_free( &p_var->val );
293 /* Set the variable */
296 if( i_type & VLC_VAR_HASCHOICE )
298 /* We must add the inherited value to our choice list */
299 p_var->i_default = 0;
301 INSERT_ELEM( p_var->choices.p_values, p_var->choices.i_count,
303 INSERT_ELEM( p_var->choices_text.p_values,
304 p_var->choices_text.i_count, 0, val );
305 p_var->ops->pf_dup( &p_var->choices.p_values[0] );
306 p_var->choices_text.p_values[0].psz_string = NULL;
311 vlc_mutex_unlock( &p_priv->var_lock );
317 * Destroy a vlc variable
319 * Look for the variable and destroy it if it is found. As in var_Create we
320 * do a call to memmove() but we have performance counterparts elsewhere.
322 * \param p_this The object that holds the variable
323 * \param psz_name The name of the variable
325 int __var_Destroy( vlc_object_t *p_this, const char *psz_name )
329 vlc_object_internals_t *p_priv = vlc_internals( p_this );
331 vlc_mutex_lock( &p_priv->var_lock );
333 i_var = GetUnused( p_this, psz_name );
336 vlc_mutex_unlock( &p_priv->var_lock );
340 p_var = &p_priv->p_vars[i_var];
342 if( p_var->i_usage > 1 )
345 vlc_mutex_unlock( &p_priv->var_lock );
349 /* Free value if needed */
350 p_var->ops->pf_free( &p_var->val );
352 /* Free choice list if needed */
353 if( p_var->choices.i_count )
355 for( i = 0 ; i < p_var->choices.i_count ; i++ )
357 p_var->ops->pf_free( &p_var->choices.p_values[i] );
358 free( p_var->choices_text.p_values[i].psz_string );
360 free( p_var->choices.p_values );
361 free( p_var->choices_text.p_values );
364 /* Free callbacks if needed */
365 if( p_var->p_entries )
367 free( p_var->p_entries );
370 free( p_var->psz_name );
371 free( p_var->psz_text );
373 memmove( p_priv->p_vars + i_var,
374 p_priv->p_vars + i_var + 1,
375 (p_priv->i_vars - i_var - 1) * sizeof(variable_t) );
377 if( (p_priv->i_vars & 15) == 0 )
379 p_priv->p_vars = realloc( p_priv->p_vars,
380 (p_priv->i_vars) * sizeof( variable_t ) );
385 vlc_mutex_unlock( &p_priv->var_lock );
391 * Perform an action on a variable
393 * \param p_this The object that holds the variable
394 * \param psz_name The name of the variable
395 * \param i_action The action to perform. Must be one of \ref var_action
396 * \param p_val First action parameter
397 * \param p_val2 Second action parameter
399 int __var_Change( vlc_object_t *p_this, const char *psz_name,
400 int i_action, vlc_value_t *p_val, vlc_value_t *p_val2 )
405 vlc_object_internals_t *p_priv = vlc_internals( p_this );
407 vlc_mutex_lock( &p_priv->var_lock );
409 i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
413 vlc_mutex_unlock( &p_priv->var_lock );
417 p_var = &p_priv->p_vars[i_var];
422 if( p_var->i_type & VLC_VAR_HASMIN )
424 p_var->ops->pf_free( &p_var->min );
426 p_var->i_type |= VLC_VAR_HASMIN;
428 p_var->ops->pf_dup( &p_var->min );
429 CheckValue( p_var, &p_var->val );
432 if( p_var->i_type & VLC_VAR_HASMIN )
438 if( p_var->i_type & VLC_VAR_HASMAX )
440 p_var->ops->pf_free( &p_var->max );
442 p_var->i_type |= VLC_VAR_HASMAX;
444 p_var->ops->pf_dup( &p_var->max );
445 CheckValue( p_var, &p_var->val );
448 if( p_var->i_type & VLC_VAR_HASMAX )
453 case VLC_VAR_SETSTEP:
454 if( p_var->i_type & VLC_VAR_HASSTEP )
456 p_var->ops->pf_free( &p_var->step );
458 p_var->i_type |= VLC_VAR_HASSTEP;
459 p_var->step = *p_val;
460 p_var->ops->pf_dup( &p_var->step );
461 CheckValue( p_var, &p_var->val );
463 case VLC_VAR_GETSTEP:
464 if( p_var->i_type & VLC_VAR_HASSTEP )
466 *p_val = p_var->step;
469 case VLC_VAR_ADDCHOICE:
470 i = p_var->choices.i_count;
472 INSERT_ELEM( p_var->choices.p_values, p_var->choices.i_count,
474 INSERT_ELEM( p_var->choices_text.p_values,
475 p_var->choices_text.i_count, i, *p_val );
476 p_var->ops->pf_dup( &p_var->choices.p_values[i] );
477 p_var->choices_text.p_values[i].psz_string =
478 ( p_val2 && p_val2->psz_string ) ?
479 strdup( p_val2->psz_string ) : NULL;
481 CheckValue( p_var, &p_var->val );
483 case VLC_VAR_DELCHOICE:
484 for( i = 0 ; i < p_var->choices.i_count ; i++ )
486 if( p_var->ops->pf_cmp( p_var->choices.p_values[i], *p_val ) == 0 )
492 if( i == p_var->choices.i_count )
495 vlc_mutex_unlock( &p_priv->var_lock );
499 if( p_var->i_default > i )
503 else if( p_var->i_default == i )
505 p_var->i_default = -1;
508 p_var->ops->pf_free( &p_var->choices.p_values[i] );
509 free( p_var->choices_text.p_values[i].psz_string );
510 REMOVE_ELEM( p_var->choices.p_values, p_var->choices.i_count, i );
511 REMOVE_ELEM( p_var->choices_text.p_values,
512 p_var->choices_text.i_count, i );
514 CheckValue( p_var, &p_var->val );
516 case VLC_VAR_CHOICESCOUNT:
517 p_val->i_int = p_var->choices.i_count;
519 case VLC_VAR_CLEARCHOICES:
520 for( i = 0 ; i < p_var->choices.i_count ; i++ )
522 p_var->ops->pf_free( &p_var->choices.p_values[i] );
524 for( i = 0 ; i < p_var->choices_text.i_count ; i++ )
525 free( p_var->choices_text.p_values[i].psz_string );
527 if( p_var->choices.i_count ) free( p_var->choices.p_values );
528 if( p_var->choices_text.i_count ) free( p_var->choices_text.p_values );
530 p_var->choices.i_count = 0;
531 p_var->choices.p_values = NULL;
532 p_var->choices_text.i_count = 0;
533 p_var->choices_text.p_values = NULL;
534 p_var->i_default = -1;
536 case VLC_VAR_SETDEFAULT:
537 /* FIXME: the list is sorted, dude. Use something cleverer. */
538 for( i = 0 ; i < p_var->choices.i_count ; i++ )
540 if( p_var->ops->pf_cmp( p_var->choices.p_values[i], *p_val ) == 0 )
546 if( i == p_var->choices.i_count )
552 p_var->i_default = i;
553 CheckValue( p_var, &p_var->val );
555 case VLC_VAR_SETVALUE:
556 /* Duplicate data if needed */
557 p_var->ops->pf_dup( p_val );
558 /* Backup needed stuff */
560 /* Check boundaries and list */
561 CheckValue( p_var, p_val );
562 /* Set the variable */
564 /* Free data if needed */
565 p_var->ops->pf_free( &oldval );
567 case VLC_VAR_GETCHOICES:
568 case VLC_VAR_GETLIST:
569 p_val->p_list = malloc( sizeof(vlc_list_t) );
570 if( p_val2 ) p_val2->p_list = malloc( sizeof(vlc_list_t) );
571 if( p_var->choices.i_count )
573 p_val->p_list->p_values = malloc( p_var->choices.i_count
574 * sizeof(vlc_value_t) );
575 p_val->p_list->pi_types = malloc( p_var->choices.i_count
579 p_val2->p_list->p_values =
580 malloc( p_var->choices.i_count * sizeof(vlc_value_t) );
581 p_val2->p_list->pi_types =
582 malloc( p_var->choices.i_count * sizeof(int) );
585 p_val->p_list->i_count = p_var->choices.i_count;
586 if( p_val2 ) p_val2->p_list->i_count = p_var->choices.i_count;
587 for( i = 0 ; i < p_var->choices.i_count ; i++ )
589 p_val->p_list->p_values[i] = p_var->choices.p_values[i];
590 p_val->p_list->pi_types[i] = p_var->i_type;
591 p_var->ops->pf_dup( &p_val->p_list->p_values[i] );
594 p_val2->p_list->p_values[i].psz_string =
595 p_var->choices_text.p_values[i].psz_string ?
596 strdup(p_var->choices_text.p_values[i].psz_string) : NULL;
597 p_val2->p_list->pi_types[i] = VLC_VAR_STRING;
601 case VLC_VAR_FREELIST:
603 if( p_val2 && p_val2->p_list )
605 for( i = 0; i < p_val2->p_list->i_count; i++ )
606 free( p_val2->p_list->p_values[i].psz_string );
607 if( p_val2->p_list->i_count )
609 free( p_val2->p_list->p_values );
610 free( p_val2->p_list->pi_types );
612 free( p_val2->p_list );
615 case VLC_VAR_SETTEXT:
616 free( p_var->psz_text );
617 if( p_val && p_val->psz_string )
618 p_var->psz_text = strdup( p_val->psz_string );
620 case VLC_VAR_GETTEXT:
621 p_val->psz_string = NULL;
622 if( p_var->psz_text )
624 p_val->psz_string = strdup( p_var->psz_text );
627 case VLC_VAR_INHERITVALUE:
631 if( InheritValue( p_this,
632 p_val2 ? p_val2->psz_string : psz_name,
633 &val, p_var->i_type )
636 /* Duplicate already done */
638 /* Backup needed stuff */
640 /* Check boundaries and list */
641 CheckValue( p_var, &val );
642 /* Set the variable */
644 /* Free data if needed */
645 p_var->ops->pf_free( &oldval );
651 p_var->ops->pf_dup( p_val );
655 case VLC_VAR_TRIGGER_CALLBACKS:
657 /* Deal with callbacks. Tell we're in a callback, release the lock,
658 * call stored functions, retake the lock. */
659 if( p_var->i_entries )
662 int i_entries = p_var->i_entries;
663 callback_entry_t *p_entries = p_var->p_entries;
665 p_var->b_incallback = true;
666 vlc_mutex_unlock( &p_priv->var_lock );
669 for( ; i_entries-- ; )
671 p_entries[i_entries].pf_callback( p_this, psz_name, p_var->val, p_var->val,
672 p_entries[i_entries].p_data );
675 vlc_mutex_lock( &p_priv->var_lock );
677 i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
680 msg_Err( p_this, "variable %s has disappeared", psz_name );
681 vlc_mutex_unlock( &p_priv->var_lock );
685 p_var = &p_priv->p_vars[i_var];
686 p_var->b_incallback = false;
691 case VLC_VAR_SETISCOMMAND:
692 p_var->i_type |= VLC_VAR_ISCOMMAND;
699 vlc_mutex_unlock( &p_priv->var_lock );
705 * Request a variable's type
707 * \return The variable type if it exists, or 0 if the
708 * variable could not be found.
711 int __var_Type( vlc_object_t *p_this, const char *psz_name )
714 vlc_object_internals_t *p_priv = vlc_internals( p_this );
716 vlc_mutex_lock( &p_priv->var_lock );
718 i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
722 vlc_mutex_unlock( &p_priv->var_lock );
726 i_type = p_priv->p_vars[i_var].i_type;
728 vlc_mutex_unlock( &p_priv->var_lock );
734 * Set a variable's value
736 * \param p_this The object that hold the variable
737 * \param psz_name The name of the variable
738 * \param val the value to set
740 int __var_Set( vlc_object_t *p_this, const char *psz_name, vlc_value_t val )
745 vlc_object_internals_t *p_priv = vlc_internals( p_this );
747 vlc_mutex_lock( &p_priv->var_lock );
749 i_var = GetUnused( p_this, psz_name );
752 vlc_mutex_unlock( &p_priv->var_lock );
756 p_var = &p_priv->p_vars[i_var];
758 /* Duplicate data if needed */
759 p_var->ops->pf_dup( &val );
761 /* Backup needed stuff */
764 /* Check boundaries and list */
765 CheckValue( p_var, &val );
767 /* Set the variable */
770 /* Deal with callbacks. Tell we're in a callback, release the lock,
771 * call stored functions, retake the lock. */
772 if( p_var->i_entries )
775 int i_entries = p_var->i_entries;
776 callback_entry_t *p_entries = p_var->p_entries;
778 p_var->b_incallback = true;
779 vlc_mutex_unlock( &p_priv->var_lock );
782 for( ; i_entries-- ; )
784 p_entries[i_entries].pf_callback( p_this, psz_name, oldval, val,
785 p_entries[i_entries].p_data );
788 vlc_mutex_lock( &p_priv->var_lock );
790 i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
793 msg_Err( p_this, "variable %s has disappeared", psz_name );
794 vlc_mutex_unlock( &p_priv->var_lock );
798 p_var = &p_priv->p_vars[i_var];
799 p_var->b_incallback = false;
802 /* Free data if needed */
803 p_var->ops->pf_free( &oldval );
805 vlc_mutex_unlock( &p_priv->var_lock );
811 * Get a variable's value
813 * \param p_this The object that holds the variable
814 * \param psz_name The name of the variable
815 * \param p_val Pointer to a vlc_value_t that will hold the variable's value
816 * after the function is finished
818 int __var_Get( vlc_object_t *p_this, const char *psz_name, vlc_value_t *p_val )
822 vlc_object_internals_t *p_priv = vlc_internals( p_this );
824 vlc_mutex_lock( &p_priv->var_lock );
826 i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
830 vlc_mutex_unlock( &p_priv->var_lock );
834 p_var = &p_priv->p_vars[i_var];
836 /* Really get the variable */
839 /* Duplicate value if needed */
840 p_var->ops->pf_dup( p_val );
842 vlc_mutex_unlock( &p_priv->var_lock );
848 #undef var_AcquireMutex
850 * Finds a process-wide mutex, creates it if needed, and locks it.
851 * Unlock with vlc_mutex_unlock().
853 vlc_mutex_t *var_AcquireMutex( const char *name )
855 libvlc_global_data_t *p_global = vlc_global();
858 if( var_Create( p_global, name, VLC_VAR_MUTEX ) )
861 var_Get( p_global, name, &val );
862 vlc_mutex_lock( val.p_address );
863 return val.p_address;
868 * Register a callback in a variable
870 * We store a function pointer that will be called upon variable
873 * \param p_this The object that holds the variable
874 * \param psz_name The name of the variable
875 * \param pf_callback The function pointer
876 * \param p_data A generic pointer that will be passed as the last
877 * argument to the callback function.
879 * \warning The callback function is run in the thread that calls var_Set on
880 * the variable. Use proper locking. This thread may not have much
881 * time to spare, so keep callback functions short.
883 int __var_AddCallback( vlc_object_t *p_this, const char *psz_name,
884 vlc_callback_t pf_callback, void *p_data )
888 callback_entry_t entry;
889 vlc_object_internals_t *p_priv = vlc_internals( p_this );
891 entry.pf_callback = pf_callback;
892 entry.p_data = p_data;
894 vlc_mutex_lock( &p_priv->var_lock );
896 i_var = GetUnused( p_this, psz_name );
899 vlc_mutex_unlock( &p_priv->var_lock );
903 p_var = &p_priv->p_vars[i_var];
905 INSERT_ELEM( p_var->p_entries,
910 vlc_mutex_unlock( &p_priv->var_lock );
916 * Remove a callback from a variable
918 * pf_callback and p_data have to be given again, because different objects
919 * might have registered the same callback function.
921 int __var_DelCallback( vlc_object_t *p_this, const char *psz_name,
922 vlc_callback_t pf_callback, void *p_data )
926 vlc_object_internals_t *p_priv = vlc_internals( p_this );
928 vlc_mutex_lock( &p_priv->var_lock );
930 i_var = GetUnused( p_this, psz_name );
933 vlc_mutex_unlock( &p_priv->var_lock );
937 p_var = &p_priv->p_vars[i_var];
939 for( i_entry = p_var->i_entries ; i_entry-- ; )
941 if( p_var->p_entries[i_entry].pf_callback == pf_callback
942 && p_var->p_entries[i_entry].p_data == p_data )
950 vlc_mutex_unlock( &p_priv->var_lock );
954 REMOVE_ELEM( p_var->p_entries, p_var->i_entries, i_entry );
956 vlc_mutex_unlock( &p_priv->var_lock );
962 * Trigger callback on a variable
964 * \param p_this The object that hold the variable
965 * \param psz_name The name of the variable
967 int __var_TriggerCallback( vlc_object_t *p_this, const char *psz_name )
972 vlc_object_internals_t *p_priv = vlc_internals( p_this );
974 vlc_mutex_lock( &p_priv->var_lock );
976 i_var = GetUnused( p_this, psz_name );
979 vlc_mutex_unlock( &p_priv->var_lock );
983 p_var = &p_priv->p_vars[i_var];
985 /* Backup needed stuff */
988 /* Deal with callbacks. Tell we're in a callback, release the lock,
989 * call stored functions, retake the lock. */
990 if( p_var->i_entries )
993 int i_entries = p_var->i_entries;
994 callback_entry_t *p_entries = p_var->p_entries;
996 p_var->b_incallback = true;
997 vlc_mutex_unlock( &p_priv->var_lock );
1000 for( ; i_entries-- ; )
1002 p_entries[i_entries].pf_callback( p_this, psz_name, oldval, oldval,
1003 p_entries[i_entries].p_data );
1006 vlc_mutex_lock( &p_priv->var_lock );
1008 i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
1011 msg_Err( p_this, "variable %s has disappeared", psz_name );
1012 vlc_mutex_unlock( &p_priv->var_lock );
1016 p_var = &p_priv->p_vars[i_var];
1017 p_var->b_incallback = false;
1020 vlc_mutex_unlock( &p_priv->var_lock );
1024 /** Parse a stringified option
1025 * This function parse a string option and create the associated object
1027 * The option must be of the form "[no[-]]foo[=bar]" where foo is the
1028 * option name and bar is the value of the option.
1029 * \param p_obj the object in which the variable must be created
1030 * \param psz_option the option to parse
1031 * \param trusted whether the option is set by a trusted input or not
1034 void var_OptionParse( vlc_object_t *p_obj, const char *psz_option,
1037 char *psz_name, *psz_value;
1039 bool b_isno = false;
1042 val.psz_string = NULL;
1044 /* It's too much of a hassle to remove the ':' when we parse
1045 * the cmd line :) */
1046 if( psz_option[0] == ':' )
1049 if( !psz_option[0] )
1052 psz_name = strdup( psz_option );
1053 if( psz_name == NULL )
1056 psz_value = strchr( psz_name, '=' );
1057 if( psz_value != NULL )
1058 *psz_value++ = '\0';
1060 /* FIXME: :programs should be handled generically */
1061 if( !strcmp( psz_name, "programs" ) )
1062 i_type = VLC_VAR_LIST;
1064 i_type = config_GetType( p_obj, psz_name );
1066 if( !i_type && !psz_value )
1068 /* check for "no-foo" or "nofoo" */
1069 if( !strncmp( psz_name, "no-", 3 ) )
1071 memmove( psz_name, psz_name + 3, strlen(psz_name) + 1 - 3 );
1073 else if( !strncmp( psz_name, "no", 2 ) )
1075 memmove( psz_name, psz_name + 2, strlen(psz_name) + 1 - 2 );
1077 else goto cleanup; /* Option doesn't exist */
1080 i_type = config_GetType( p_obj, psz_name );
1082 if( !i_type ) goto cleanup; /* Option doesn't exist */
1084 if( ( i_type != VLC_VAR_BOOL ) &&
1085 ( !psz_value || !*psz_value ) ) goto cleanup; /* Invalid value */
1087 /* check if option is unsafe */
1090 module_config_t *p_config = config_FindConfig( p_obj, psz_name );
1091 if( !p_config->b_safe )
1093 msg_Err( p_obj, "unsafe option \"%s\" has been ignored for "
1094 "security reasons", psz_name );
1099 /* Create the variable in the input object.
1100 * Children of the input object will be able to retreive this value
1101 * thanks to the inheritance property of the object variables. */
1102 var_Create( p_obj, psz_name, i_type );
1107 val.b_bool = !b_isno;
1110 case VLC_VAR_INTEGER:
1111 val.i_int = strtol( psz_value, NULL, 0 );
1115 val.f_float = atof( psz_value );
1118 case VLC_VAR_STRING:
1119 case VLC_VAR_MODULE:
1121 case VLC_VAR_DIRECTORY:
1122 val.psz_string = psz_value;
1127 char *psz_orig, *psz_var;
1128 vlc_list_t *p_list = malloc(sizeof(vlc_list_t));
1129 val.p_list = p_list;
1130 p_list->i_count = 0;
1132 psz_var = psz_orig = strdup(psz_value);
1133 while( psz_var && *psz_var )
1135 char *psz_item = psz_var;
1137 while( *psz_var && *psz_var != ',' ) psz_var++;
1138 if( *psz_var == ',' )
1143 val2.i_int = strtol( psz_item, NULL, 0 );
1144 INSERT_ELEM( p_list->p_values, p_list->i_count,
1145 p_list->i_count, val2 );
1146 /* p_list->i_count is incremented twice by INSERT_ELEM */
1148 INSERT_ELEM( p_list->pi_types, p_list->i_count,
1149 p_list->i_count, VLC_VAR_INTEGER );
1159 var_Set( p_obj, psz_name, val );
1166 /* Following functions are local */
1168 /*****************************************************************************
1169 * GetUnused: find an unused variable from its name
1170 *****************************************************************************
1171 * We do i_tries tries before giving up, just in case the variable is being
1172 * modified and called from a callback.
1173 *****************************************************************************/
1174 static int GetUnused( vlc_object_t *p_this, const char *psz_name )
1176 int i_var, i_tries = 0;
1177 vlc_object_internals_t *p_priv = vlc_internals( p_this );
1181 i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
1187 if( ! p_priv->p_vars[i_var].b_incallback )
1192 if( i_tries++ > 100 )
1194 msg_Err( p_this, "caught in a callback deadlock? ('%s')", psz_name );
1195 return VLC_ETIMEOUT;
1198 vlc_mutex_unlock( &p_priv->var_lock );
1199 msleep( THREAD_SLEEP );
1200 vlc_mutex_lock( &p_priv->var_lock );
1204 /*****************************************************************************
1205 * HashString: our cool hash function
1206 *****************************************************************************
1207 * This function is not intended to be crypto-secure, we only want it to be
1208 * fast and not suck too much. This one is pretty fast and did 0 collisions
1209 * in wenglish's dictionary.
1210 *****************************************************************************/
1211 static uint32_t HashString( const char *psz_string )
1213 uint32_t i_hash = 0;
1215 while( *psz_string )
1217 i_hash += *psz_string++;
1218 i_hash += i_hash << 10;
1219 i_hash ^= i_hash >> 8;
1225 /*****************************************************************************
1226 * Insert: find an empty slot to insert a new variable
1227 *****************************************************************************
1228 * We use a recursive inner function indexed on the hash. This function does
1229 * nothing in the rare cases where a collision may occur, see Lookup()
1230 * to see how we handle them.
1231 * XXX: does this really need to be written recursively?
1232 *****************************************************************************/
1233 static int Insert( variable_t *p_vars, int i_count, const char *psz_name )
1240 return InsertInner( p_vars, i_count, HashString( psz_name ) );
1243 static int InsertInner( variable_t *p_vars, int i_count, uint32_t i_hash )
1247 if( i_hash <= p_vars[0].i_hash )
1252 if( i_hash >= p_vars[i_count - 1].i_hash )
1257 i_middle = i_count / 2;
1259 /* We know that 0 < i_middle */
1260 if( i_hash < p_vars[i_middle].i_hash )
1262 return InsertInner( p_vars, i_middle, i_hash );
1265 /* We know that i_middle + 1 < i_count */
1266 if( i_hash > p_vars[i_middle + 1].i_hash )
1268 return i_middle + 1 + InsertInner( p_vars + i_middle + 1,
1269 i_count - i_middle - 1,
1273 return i_middle + 1;
1276 /*****************************************************************************
1277 * Lookup: find an existing variable given its name
1278 *****************************************************************************
1279 * We use a recursive inner function indexed on the hash. Care is taken of
1280 * possible hash collisions.
1281 * XXX: does this really need to be written recursively?
1282 *****************************************************************************/
1283 static int Lookup( variable_t *p_vars, int i_count, const char *psz_name )
1293 i_hash = HashString( psz_name );
1295 i_pos = LookupInner( p_vars, i_count, i_hash );
1297 /* Hash not found */
1298 if( i_hash != p_vars[i_pos].i_hash )
1303 /* Hash found, entry found */
1304 if( !strcmp( psz_name, p_vars[i_pos].psz_name ) )
1309 /* Hash collision! This should be very rare, but we cannot guarantee
1310 * it will never happen. Just do an exhaustive search amongst all
1311 * entries with the same hash. */
1312 for( i = i_pos - 1 ; i > 0 && i_hash == p_vars[i].i_hash ; i-- )
1314 if( !strcmp( psz_name, p_vars[i].psz_name ) )
1320 for( i = i_pos + 1 ; i < i_count && i_hash == p_vars[i].i_hash ; i++ )
1322 if( !strcmp( psz_name, p_vars[i].psz_name ) )
1328 /* Hash found, but entry not found */
1332 static int LookupInner( variable_t *p_vars, int i_count, uint32_t i_hash )
1336 if( i_hash <= p_vars[0].i_hash )
1341 if( i_hash >= p_vars[i_count-1].i_hash )
1346 i_middle = i_count / 2;
1348 /* We know that 0 < i_middle */
1349 if( i_hash < p_vars[i_middle].i_hash )
1351 return LookupInner( p_vars, i_middle, i_hash );
1354 /* We know that i_middle + 1 < i_count */
1355 if( i_hash > p_vars[i_middle].i_hash )
1357 return i_middle + LookupInner( p_vars + i_middle,
1365 /*****************************************************************************
1366 * CheckValue: check that a value is valid wrt. a variable
1367 *****************************************************************************
1368 * This function checks p_val's value against p_var's limitations such as
1369 * minimal and maximal value, step, in-list position, and modifies p_val if
1371 ****************************************************************************/
1372 static void CheckValue ( variable_t *p_var, vlc_value_t *p_val )
1374 /* Check that our variable is in the list */
1375 if( p_var->i_type & VLC_VAR_HASCHOICE && p_var->choices.i_count )
1379 /* FIXME: the list is sorted, dude. Use something cleverer. */
1380 for( i = p_var->choices.i_count ; i-- ; )
1382 if( p_var->ops->pf_cmp( *p_val, p_var->choices.p_values[i] ) == 0 )
1388 /* If not found, change it to anything vaguely valid */
1391 /* Free the old variable, get the new one, dup it */
1392 p_var->ops->pf_free( p_val );
1393 *p_val = p_var->choices.p_values[p_var->i_default >= 0
1394 ? p_var->i_default : 0 ];
1395 p_var->ops->pf_dup( p_val );
1399 /* Check that our variable is within the bounds */
1400 switch( p_var->i_type & VLC_VAR_TYPE )
1402 case VLC_VAR_INTEGER:
1403 if( p_var->i_type & VLC_VAR_HASSTEP && p_var->step.i_int
1404 && (p_val->i_int % p_var->step.i_int) )
1406 p_val->i_int = (p_val->i_int + (p_var->step.i_int / 2))
1407 / p_var->step.i_int * p_var->step.i_int;
1409 if( p_var->i_type & VLC_VAR_HASMIN
1410 && p_val->i_int < p_var->min.i_int )
1412 p_val->i_int = p_var->min.i_int;
1414 if( p_var->i_type & VLC_VAR_HASMAX
1415 && p_val->i_int > p_var->max.i_int )
1417 p_val->i_int = p_var->max.i_int;
1421 if( p_var->i_type & VLC_VAR_HASSTEP && p_var->step.f_float )
1423 float f_round = p_var->step.f_float * (float)(int)( 0.5 +
1424 p_val->f_float / p_var->step.f_float );
1425 if( p_val->f_float != f_round )
1427 p_val->f_float = f_round;
1430 if( p_var->i_type & VLC_VAR_HASMIN
1431 && p_val->f_float < p_var->min.f_float )
1433 p_val->f_float = p_var->min.f_float;
1435 if( p_var->i_type & VLC_VAR_HASMAX
1436 && p_val->f_float > p_var->max.f_float )
1438 p_val->f_float = p_var->max.f_float;
1447 /*****************************************************************************
1448 * InheritValue: try to inherit the value of this variable from the same one
1449 * in our closest parent.
1450 *****************************************************************************/
1451 static int InheritValue( vlc_object_t *p_this, const char *psz_name,
1452 vlc_value_t *p_val, int i_type )
1456 vlc_object_internals_t *p_priv;
1458 /* No need to take the structure lock,
1459 * we are only looking for our parents */
1461 if( !p_this->p_parent && !p_this->p_libvlc )
1463 switch( i_type & VLC_VAR_CLASS )
1465 case VLC_VAR_STRING:
1466 p_val->psz_string = config_GetPsz( p_this, psz_name );
1467 if( !p_val->psz_string ) p_val->psz_string = strdup("");
1470 p_val->f_float = config_GetFloat( p_this, psz_name );
1472 case VLC_VAR_INTEGER:
1473 p_val->i_int = config_GetInt( p_this, psz_name );
1476 p_val->b_bool = config_GetInt( p_this, psz_name );
1480 char *psz_orig, *psz_var;
1481 vlc_list_t *p_list = malloc(sizeof(vlc_list_t));
1482 p_val->p_list = p_list;
1483 p_list->i_count = 0;
1485 psz_var = psz_orig = config_GetPsz( p_this, psz_name );
1486 while( psz_var && *psz_var )
1488 char *psz_item = psz_var;
1490 while( *psz_var && *psz_var != ',' ) psz_var++;
1491 if( *psz_var == ',' )
1496 val.i_int = strtol( psz_item, NULL, 0 );
1497 INSERT_ELEM( p_list->p_values, p_list->i_count,
1498 p_list->i_count, val );
1499 /* p_list->i_count is incremented twice by INSERT_ELEM */
1501 INSERT_ELEM( p_list->pi_types, p_list->i_count,
1502 p_list->i_count, VLC_VAR_INTEGER );
1515 if( !p_this->p_parent )
1516 p_priv = vlc_internals( p_this->p_libvlc );
1518 p_priv = vlc_internals( p_this->p_parent );
1520 /* Look for the variable */
1521 vlc_mutex_lock( &p_priv->var_lock );
1523 i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
1528 p_var = &p_priv->p_vars[i_var];
1530 /* Really get the variable */
1531 *p_val = p_var->val;
1533 /* Duplicate value if needed */
1534 p_var->ops->pf_dup( p_val );
1536 vlc_mutex_unlock( &p_priv->var_lock );
1540 vlc_mutex_unlock( &p_priv->var_lock );
1542 /* We're still not there */
1544 return InheritValue( p_this->p_parent, psz_name, p_val, i_type );
1547 /**********************************************************************
1548 * Execute a var command on an object identified by its name
1549 **********************************************************************/
1550 int __var_Command( vlc_object_t *p_this, const char *psz_name,
1551 const char *psz_cmd, const char *psz_arg, char **psz_msg )
1553 vlc_object_t *p_obj = vlc_object_find_name( p_this->p_libvlc,
1554 psz_name, FIND_CHILD );
1560 *psz_msg = strdup( "Unknown destination object." );
1564 i_type = var_Type( p_obj, psz_cmd );
1565 if( !( i_type&VLC_VAR_ISCOMMAND ) )
1567 vlc_object_release( p_obj );
1569 *psz_msg = strdup( "Variable doesn't exist or isn't a command." );
1570 return VLC_EGENERIC;
1573 i_type &= VLC_VAR_CLASS;
1576 case VLC_VAR_INTEGER:
1577 i_ret = var_SetInteger( p_obj, psz_cmd, atoi( psz_arg ) );
1580 i_ret = var_SetFloat( p_obj, psz_cmd, atof( psz_arg ) );
1582 case VLC_VAR_STRING:
1583 i_ret = var_SetString( p_obj, psz_cmd, psz_arg );
1586 i_ret = var_SetBool( p_obj, psz_cmd, atoi( psz_arg ) );
1589 i_ret = VLC_EGENERIC;
1593 vlc_object_release( p_obj );
1597 *psz_msg = (char*)malloc( 80 );
1598 sprintf( *psz_msg, "%s on object %s returned %i (%s)",
1599 psz_cmd, psz_name, i_ret, vlc_error( i_ret ) );