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 *****************************************************************************/
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_TYPE )
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_TYPE )
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 /*****************************************************************************
139 *****************************************************************************/
140 static int GetUnused ( vlc_object_t *, const char * );
141 static uint32_t HashString ( const char * );
142 static int Insert ( variable_t *, int, const char * );
143 static int InsertInner ( variable_t *, int, uint32_t );
144 static int Lookup ( variable_t *, int, const char * );
145 static int LookupInner ( variable_t *, int, uint32_t );
147 static void CheckValue ( variable_t *, vlc_value_t * );
149 static int InheritValue( vlc_object_t *, const char *, vlc_value_t *,
153 * Initialize a vlc variable
155 * We hash the given string and insert it into the sorted list. The insertion
156 * may require slow memory copies, but think about what we gain in the log(n)
157 * lookup phase when setting/getting the variable value!
159 * \param p_this The object in which to create the variable
160 * \param psz_name The name of the variable
161 * \param i_type The variables type. Must be one of \ref var_type combined with
162 * zero or more \ref var_flags
164 int __var_Create( vlc_object_t *p_this, const char *psz_name, int i_type )
168 static vlc_list_t dummy_null_list = {0, NULL, NULL};
169 vlc_object_internals_t *p_priv = vlc_internals( p_this );
171 vlc_mutex_lock( &p_priv->var_lock );
173 /* FIXME: if the variable already exists, we don't duplicate it. But we
174 * duplicate the lookups. It's not that serious, but if anyone finds some
175 * time to rework Insert() so that only one lookup has to be done, feel
177 i_new = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
181 /* If the types differ, variable creation failed. */
182 if( (i_type & ~(VLC_VAR_DOINHERIT|VLC_VAR_ISCOMMAND)) != p_priv->p_vars[i_new].i_type )
184 vlc_mutex_unlock( &p_priv->var_lock );
188 p_priv->p_vars[i_new].i_usage++;
189 if( i_type & VLC_VAR_ISCOMMAND )
190 p_priv->p_vars[i_new].i_type |= VLC_VAR_ISCOMMAND;
191 vlc_mutex_unlock( &p_priv->var_lock );
195 i_new = Insert( p_priv->p_vars, p_priv->i_vars, psz_name );
197 if( (p_priv->i_vars & 15) == 15 )
199 p_priv->p_vars = realloc( p_priv->p_vars,
200 (p_priv->i_vars+17) * sizeof(variable_t) );
203 memmove( p_priv->p_vars + i_new + 1,
204 p_priv->p_vars + i_new,
205 (p_priv->i_vars - i_new) * sizeof(variable_t) );
209 p_var = &p_priv->p_vars[i_new];
210 memset( p_var, 0, sizeof(*p_var) );
212 p_var->i_hash = HashString( psz_name );
213 p_var->psz_name = strdup( psz_name );
214 p_var->psz_text = NULL;
216 p_var->i_type = i_type & ~VLC_VAR_DOINHERIT;
217 memset( &p_var->val, 0, sizeof(vlc_value_t) );
219 p_var->pf_dup = DupDummy;
220 p_var->pf_free = FreeDummy;
224 p_var->i_default = -1;
225 p_var->choices.i_count = 0;
226 p_var->choices.p_values = NULL;
227 p_var->choices_text.i_count = 0;
228 p_var->choices_text.p_values = NULL;
230 p_var->b_incallback = false;
231 p_var->i_entries = 0;
232 p_var->p_entries = NULL;
234 /* Always initialize the variable, even if it is a list variable; this
235 * will lead to errors if the variable is not initialized, but it will
236 * not cause crashes in the variable handling. */
237 switch( i_type & VLC_VAR_TYPE )
240 p_var->pf_cmp = CmpBool;
241 p_var->val.b_bool = false;
243 case VLC_VAR_INTEGER:
245 p_var->pf_cmp = CmpInt;
246 p_var->val.i_int = 0;
251 case VLC_VAR_DIRECTORY:
252 case VLC_VAR_VARIABLE:
253 p_var->pf_cmp = CmpString;
254 p_var->pf_dup = DupString;
255 p_var->pf_free = FreeString;
256 p_var->val.psz_string = NULL;
259 p_var->pf_cmp = CmpFloat;
260 p_var->val.f_float = 0.0;
263 p_var->pf_cmp = CmpTime;
264 p_var->val.i_time = 0;
266 case VLC_VAR_ADDRESS:
267 p_var->pf_cmp = CmpAddress;
268 p_var->val.p_address = NULL;
271 p_var->pf_cmp = CmpAddress;
272 p_var->pf_free = FreeMutex;
273 p_var->val.p_address = malloc( sizeof(vlc_mutex_t) );
274 vlc_mutex_init( (vlc_mutex_t*)p_var->val.p_address );
277 p_var->pf_cmp = CmpAddress;
278 p_var->pf_dup = DupList;
279 p_var->pf_free = FreeList;
280 p_var->val.p_list = &dummy_null_list;
284 /* Duplicate the default data we stored. */
285 p_var->pf_dup( &p_var->val );
287 if( i_type & VLC_VAR_DOINHERIT )
291 if( InheritValue( p_this, psz_name, &val, p_var->i_type )
294 /* Free data if needed */
295 p_var->pf_free( &p_var->val );
296 /* Set the variable */
299 if( i_type & VLC_VAR_HASCHOICE )
301 /* We must add the inherited value to our choice list */
302 p_var->i_default = 0;
304 INSERT_ELEM( p_var->choices.p_values, p_var->choices.i_count,
306 INSERT_ELEM( p_var->choices_text.p_values,
307 p_var->choices_text.i_count, 0, val );
308 p_var->pf_dup( &p_var->choices.p_values[0] );
309 p_var->choices_text.p_values[0].psz_string = NULL;
314 vlc_mutex_unlock( &p_priv->var_lock );
320 * Destroy a vlc variable
322 * Look for the variable and destroy it if it is found. As in var_Create we
323 * do a call to memmove() but we have performance counterparts elsewhere.
325 * \param p_this The object that holds the variable
326 * \param psz_name The name of the variable
328 int __var_Destroy( vlc_object_t *p_this, const char *psz_name )
332 vlc_object_internals_t *p_priv = vlc_internals( p_this );
334 vlc_mutex_lock( &p_priv->var_lock );
336 i_var = GetUnused( p_this, psz_name );
339 vlc_mutex_unlock( &p_priv->var_lock );
343 p_var = &p_priv->p_vars[i_var];
345 if( p_var->i_usage > 1 )
348 vlc_mutex_unlock( &p_priv->var_lock );
352 /* Free value if needed */
353 p_var->pf_free( &p_var->val );
355 /* Free choice list if needed */
356 if( p_var->choices.i_count )
358 for( i = 0 ; i < p_var->choices.i_count ; i++ )
360 p_var->pf_free( &p_var->choices.p_values[i] );
361 free( p_var->choices_text.p_values[i].psz_string );
363 free( p_var->choices.p_values );
364 free( p_var->choices_text.p_values );
367 /* Free callbacks if needed */
368 if( p_var->p_entries )
370 free( p_var->p_entries );
373 free( p_var->psz_name );
374 free( p_var->psz_text );
376 memmove( p_priv->p_vars + i_var,
377 p_priv->p_vars + i_var + 1,
378 (p_priv->i_vars - i_var - 1) * sizeof(variable_t) );
380 if( (p_priv->i_vars & 15) == 0 )
382 p_priv->p_vars = realloc( p_priv->p_vars,
383 (p_priv->i_vars) * sizeof( variable_t ) );
388 vlc_mutex_unlock( &p_priv->var_lock );
394 * Perform an action on a variable
396 * \param p_this The object that holds the variable
397 * \param psz_name The name of the variable
398 * \param i_action The action to perform. Must be one of \ref var_action
399 * \param p_val First action parameter
400 * \param p_val2 Second action parameter
402 int __var_Change( vlc_object_t *p_this, const char *psz_name,
403 int i_action, vlc_value_t *p_val, vlc_value_t *p_val2 )
408 vlc_object_internals_t *p_priv = vlc_internals( p_this );
410 vlc_mutex_lock( &p_priv->var_lock );
412 i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
416 vlc_mutex_unlock( &p_priv->var_lock );
420 p_var = &p_priv->p_vars[i_var];
425 if( p_var->i_type & VLC_VAR_HASMIN )
427 p_var->pf_free( &p_var->min );
429 p_var->i_type |= VLC_VAR_HASMIN;
431 p_var->pf_dup( &p_var->min );
432 CheckValue( p_var, &p_var->val );
435 if( p_var->i_type & VLC_VAR_HASMIN )
441 if( p_var->i_type & VLC_VAR_HASMAX )
443 p_var->pf_free( &p_var->max );
445 p_var->i_type |= VLC_VAR_HASMAX;
447 p_var->pf_dup( &p_var->max );
448 CheckValue( p_var, &p_var->val );
451 if( p_var->i_type & VLC_VAR_HASMAX )
456 case VLC_VAR_SETSTEP:
457 if( p_var->i_type & VLC_VAR_HASSTEP )
459 p_var->pf_free( &p_var->step );
461 p_var->i_type |= VLC_VAR_HASSTEP;
462 p_var->step = *p_val;
463 p_var->pf_dup( &p_var->step );
464 CheckValue( p_var, &p_var->val );
466 case VLC_VAR_GETSTEP:
467 if( p_var->i_type & VLC_VAR_HASSTEP )
469 *p_val = p_var->step;
472 case VLC_VAR_ADDCHOICE:
473 /* FIXME: the list is sorted, dude. Use something cleverer. */
474 for( i = p_var->choices.i_count ; i-- ; )
476 if( p_var->pf_cmp( p_var->choices.p_values[i], *p_val ) < 0 )
482 /* The new place is i+1 */
485 if( p_var->i_default >= i )
490 INSERT_ELEM( p_var->choices.p_values, p_var->choices.i_count,
492 INSERT_ELEM( p_var->choices_text.p_values,
493 p_var->choices_text.i_count, i, *p_val );
494 p_var->pf_dup( &p_var->choices.p_values[i] );
495 p_var->choices_text.p_values[i].psz_string =
496 ( p_val2 && p_val2->psz_string ) ?
497 strdup( p_val2->psz_string ) : NULL;
499 CheckValue( p_var, &p_var->val );
501 case VLC_VAR_DELCHOICE:
502 /* FIXME: the list is sorted, dude. Use something cleverer. */
503 for( i = 0 ; i < p_var->choices.i_count ; i++ )
505 if( p_var->pf_cmp( p_var->choices.p_values[i], *p_val ) == 0 )
511 if( i == p_var->choices.i_count )
514 vlc_mutex_unlock( &p_priv->var_lock );
518 if( p_var->i_default > i )
522 else if( p_var->i_default == i )
524 p_var->i_default = -1;
527 p_var->pf_free( &p_var->choices.p_values[i] );
528 free( p_var->choices_text.p_values[i].psz_string );
529 REMOVE_ELEM( p_var->choices.p_values, p_var->choices.i_count, i );
530 REMOVE_ELEM( p_var->choices_text.p_values,
531 p_var->choices_text.i_count, i );
533 CheckValue( p_var, &p_var->val );
535 case VLC_VAR_CHOICESCOUNT:
536 p_val->i_int = p_var->choices.i_count;
538 case VLC_VAR_CLEARCHOICES:
539 for( i = 0 ; i < p_var->choices.i_count ; i++ )
541 p_var->pf_free( &p_var->choices.p_values[i] );
543 for( i = 0 ; i < p_var->choices_text.i_count ; i++ )
544 free( p_var->choices_text.p_values[i].psz_string );
546 if( p_var->choices.i_count ) free( p_var->choices.p_values );
547 if( p_var->choices_text.i_count ) free( p_var->choices_text.p_values );
549 p_var->choices.i_count = 0;
550 p_var->choices.p_values = NULL;
551 p_var->choices_text.i_count = 0;
552 p_var->choices_text.p_values = NULL;
553 p_var->i_default = -1;
555 case VLC_VAR_SETDEFAULT:
556 /* FIXME: the list is sorted, dude. Use something cleverer. */
557 for( i = 0 ; i < p_var->choices.i_count ; i++ )
559 if( p_var->pf_cmp( p_var->choices.p_values[i], *p_val ) == 0 )
565 if( i == p_var->choices.i_count )
571 p_var->i_default = i;
572 CheckValue( p_var, &p_var->val );
574 case VLC_VAR_SETVALUE:
575 /* Duplicate data if needed */
576 p_var->pf_dup( p_val );
577 /* Backup needed stuff */
579 /* Check boundaries and list */
580 CheckValue( p_var, p_val );
581 /* Set the variable */
583 /* Free data if needed */
584 p_var->pf_free( &oldval );
586 case VLC_VAR_GETCHOICES:
587 case VLC_VAR_GETLIST:
588 p_val->p_list = malloc( sizeof(vlc_list_t) );
589 if( p_val2 ) p_val2->p_list = malloc( sizeof(vlc_list_t) );
590 if( p_var->choices.i_count )
592 p_val->p_list->p_values = malloc( p_var->choices.i_count
593 * sizeof(vlc_value_t) );
594 p_val->p_list->pi_types = malloc( p_var->choices.i_count
598 p_val2->p_list->p_values =
599 malloc( p_var->choices.i_count * sizeof(vlc_value_t) );
600 p_val2->p_list->pi_types =
601 malloc( p_var->choices.i_count * sizeof(int) );
604 p_val->p_list->i_count = p_var->choices.i_count;
605 if( p_val2 ) p_val2->p_list->i_count = p_var->choices.i_count;
606 for( i = 0 ; i < p_var->choices.i_count ; i++ )
608 p_val->p_list->p_values[i] = p_var->choices.p_values[i];
609 p_val->p_list->pi_types[i] = p_var->i_type;
610 p_var->pf_dup( &p_val->p_list->p_values[i] );
613 p_val2->p_list->p_values[i].psz_string =
614 p_var->choices_text.p_values[i].psz_string ?
615 strdup(p_var->choices_text.p_values[i].psz_string) : NULL;
616 p_val2->p_list->pi_types[i] = VLC_VAR_STRING;
620 case VLC_VAR_FREELIST:
622 if( p_val2 && p_val2->p_list )
624 for( i = 0; i < p_val2->p_list->i_count; i++ )
625 free( p_val2->p_list->p_values[i].psz_string );
626 if( p_val2->p_list->i_count )
628 free( p_val2->p_list->p_values );
629 free( p_val2->p_list->pi_types );
631 free( p_val2->p_list );
634 case VLC_VAR_SETTEXT:
635 free( p_var->psz_text );
636 if( p_val && p_val->psz_string )
637 p_var->psz_text = strdup( p_val->psz_string );
639 case VLC_VAR_GETTEXT:
640 p_val->psz_string = NULL;
641 if( p_var->psz_text )
643 p_val->psz_string = strdup( p_var->psz_text );
646 case VLC_VAR_INHERITVALUE:
650 if( InheritValue( p_this,
651 p_val2 ? p_val2->psz_string : psz_name,
652 &val, p_var->i_type )
655 /* Duplicate already done */
657 /* Backup needed stuff */
659 /* Check boundaries and list */
660 CheckValue( p_var, &val );
661 /* Set the variable */
663 /* Free data if needed */
664 p_var->pf_free( &oldval );
670 p_var->pf_dup( p_val );
674 case VLC_VAR_TRIGGER_CALLBACKS:
676 /* Deal with callbacks. Tell we're in a callback, release the lock,
677 * call stored functions, retake the lock. */
678 if( p_var->i_entries )
681 int i_entries = p_var->i_entries;
682 callback_entry_t *p_entries = p_var->p_entries;
684 p_var->b_incallback = true;
685 vlc_mutex_unlock( &p_priv->var_lock );
688 for( ; i_entries-- ; )
690 p_entries[i_entries].pf_callback( p_this, psz_name, p_var->val, p_var->val,
691 p_entries[i_entries].p_data );
694 vlc_mutex_lock( &p_priv->var_lock );
696 i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
699 msg_Err( p_this, "variable %s has disappeared", psz_name );
700 vlc_mutex_unlock( &p_priv->var_lock );
704 p_var = &p_priv->p_vars[i_var];
705 p_var->b_incallback = false;
714 vlc_mutex_unlock( &p_priv->var_lock );
720 * Request a variable's type
722 * \return The variable type if it exists, or 0 if the
723 * variable could not be found.
726 int __var_Type( vlc_object_t *p_this, const char *psz_name )
729 vlc_object_internals_t *p_priv = vlc_internals( p_this );
731 vlc_mutex_lock( &p_priv->var_lock );
733 i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
737 vlc_mutex_unlock( &p_priv->var_lock );
741 i_type = p_priv->p_vars[i_var].i_type;
743 vlc_mutex_unlock( &p_priv->var_lock );
749 * Set a variable's value
751 * \param p_this The object that hold the variable
752 * \param psz_name The name of the variable
753 * \param val the value to set
755 int __var_Set( vlc_object_t *p_this, const char *psz_name, vlc_value_t val )
760 vlc_object_internals_t *p_priv = vlc_internals( p_this );
762 vlc_mutex_lock( &p_priv->var_lock );
764 i_var = GetUnused( p_this, psz_name );
767 vlc_mutex_unlock( &p_priv->var_lock );
771 p_var = &p_priv->p_vars[i_var];
773 /* Duplicate data if needed */
774 p_var->pf_dup( &val );
776 /* Backup needed stuff */
779 /* Check boundaries and list */
780 CheckValue( p_var, &val );
782 /* Set the variable */
785 /* Deal with callbacks. Tell we're in a callback, release the lock,
786 * call stored functions, retake the lock. */
787 if( p_var->i_entries )
790 int i_entries = p_var->i_entries;
791 callback_entry_t *p_entries = p_var->p_entries;
793 p_var->b_incallback = true;
794 vlc_mutex_unlock( &p_priv->var_lock );
797 for( ; i_entries-- ; )
799 p_entries[i_entries].pf_callback( p_this, psz_name, oldval, val,
800 p_entries[i_entries].p_data );
803 vlc_mutex_lock( &p_priv->var_lock );
805 i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
808 msg_Err( p_this, "variable %s has disappeared", psz_name );
809 vlc_mutex_unlock( &p_priv->var_lock );
813 p_var = &p_priv->p_vars[i_var];
814 p_var->b_incallback = false;
817 /* Free data if needed */
818 p_var->pf_free( &oldval );
820 vlc_mutex_unlock( &p_priv->var_lock );
826 * Get a variable's value
828 * \param p_this The object that holds the variable
829 * \param psz_name The name of the variable
830 * \param p_val Pointer to a vlc_value_t that will hold the variable's value
831 * after the function is finished
833 int __var_Get( vlc_object_t *p_this, const char *psz_name, vlc_value_t *p_val )
837 vlc_object_internals_t *p_priv = vlc_internals( p_this );
839 vlc_mutex_lock( &p_priv->var_lock );
841 i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
845 vlc_mutex_unlock( &p_priv->var_lock );
849 p_var = &p_priv->p_vars[i_var];
851 /* Really get the variable */
854 /* Duplicate value if needed */
855 p_var->pf_dup( p_val );
857 vlc_mutex_unlock( &p_priv->var_lock );
864 * Finds a process-wide mutex, creates it if needed, and locks it.
865 * Unlock with vlc_mutex_unlock().
867 vlc_mutex_t *var_AcquireMutex( const char *name )
869 libvlc_global_data_t *p_global = vlc_global();
872 if( var_Create( p_global, name, VLC_VAR_MUTEX ) )
875 var_Get( p_global, name, &val );
876 vlc_mutex_lock( val.p_address );
877 return val.p_address;
882 * Register a callback in a variable
884 * We store a function pointer that will be called upon variable
887 * \param p_this The object that holds the variable
888 * \param psz_name The name of the variable
889 * \param pf_callback The function pointer
890 * \param p_data A generic pointer that will be passed as the last
891 * argument to the callback function.
893 * \warning The callback function is run in the thread that calls var_Set on
894 * the variable. Use proper locking. This thread may not have much
895 * time to spare, so keep callback functions short.
897 int __var_AddCallback( vlc_object_t *p_this, const char *psz_name,
898 vlc_callback_t pf_callback, void *p_data )
902 callback_entry_t entry;
903 vlc_object_internals_t *p_priv = vlc_internals( p_this );
905 entry.pf_callback = pf_callback;
906 entry.p_data = p_data;
908 vlc_mutex_lock( &p_priv->var_lock );
910 i_var = GetUnused( p_this, psz_name );
913 vlc_mutex_unlock( &p_priv->var_lock );
917 p_var = &p_priv->p_vars[i_var];
919 INSERT_ELEM( p_var->p_entries,
924 vlc_mutex_unlock( &p_priv->var_lock );
930 * Remove a callback from a variable
932 * pf_callback and p_data have to be given again, because different objects
933 * might have registered the same callback function.
935 int __var_DelCallback( vlc_object_t *p_this, const char *psz_name,
936 vlc_callback_t pf_callback, void *p_data )
940 vlc_object_internals_t *p_priv = vlc_internals( p_this );
942 vlc_mutex_lock( &p_priv->var_lock );
944 i_var = GetUnused( p_this, psz_name );
947 vlc_mutex_unlock( &p_priv->var_lock );
951 p_var = &p_priv->p_vars[i_var];
953 for( i_entry = p_var->i_entries ; i_entry-- ; )
955 if( p_var->p_entries[i_entry].pf_callback == pf_callback
956 && p_var->p_entries[i_entry].p_data == p_data )
964 vlc_mutex_unlock( &p_priv->var_lock );
968 REMOVE_ELEM( p_var->p_entries, p_var->i_entries, i_entry );
970 vlc_mutex_unlock( &p_priv->var_lock );
976 * Trigger callback on a variable
978 * \param p_this The object that hold the variable
979 * \param psz_name The name of the variable
981 int __var_TriggerCallback( vlc_object_t *p_this, const char *psz_name )
986 vlc_object_internals_t *p_priv = vlc_internals( p_this );
988 vlc_mutex_lock( &p_priv->var_lock );
990 i_var = GetUnused( p_this, psz_name );
993 vlc_mutex_unlock( &p_priv->var_lock );
997 p_var = &p_priv->p_vars[i_var];
999 /* Backup needed stuff */
1000 oldval = p_var->val;
1002 /* Deal with callbacks. Tell we're in a callback, release the lock,
1003 * call stored functions, retake the lock. */
1004 if( p_var->i_entries )
1007 int i_entries = p_var->i_entries;
1008 callback_entry_t *p_entries = p_var->p_entries;
1010 p_var->b_incallback = true;
1011 vlc_mutex_unlock( &p_priv->var_lock );
1013 /* The real calls */
1014 for( ; i_entries-- ; )
1016 p_entries[i_entries].pf_callback( p_this, psz_name, oldval, oldval,
1017 p_entries[i_entries].p_data );
1020 vlc_mutex_lock( &p_priv->var_lock );
1022 i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
1025 msg_Err( p_this, "variable %s has disappeared", psz_name );
1026 vlc_mutex_unlock( &p_priv->var_lock );
1030 p_var = &p_priv->p_vars[i_var];
1031 p_var->b_incallback = false;
1034 vlc_mutex_unlock( &p_priv->var_lock );
1038 /** Parse a stringified option
1039 * This function parse a string option and create the associated object
1041 * The option must be of the form "[no[-]]foo[=bar]" where foo is the
1042 * option name and bar is the value of the option.
1043 * \param p_obj the object in which the variable must be created
1044 * \param psz_option the option to parse
1045 * \param trusted whether the option is set by a trusted input or not
1048 void var_OptionParse( vlc_object_t *p_obj, const char *psz_option,
1051 char *psz_name, *psz_value;
1053 bool b_isno = false;
1056 val.psz_string = NULL;
1058 /* It's too much of a hassle to remove the ':' when we parse
1059 * the cmd line :) */
1060 if( psz_option[0] == ':' )
1063 if( !psz_option[0] )
1066 psz_name = strdup( psz_option );
1067 if( psz_name == NULL )
1070 psz_value = strchr( psz_name, '=' );
1071 if( psz_value != NULL )
1072 *psz_value++ = '\0';
1074 /* FIXME: :programs should be handled generically */
1075 if( !strcmp( psz_name, "programs" ) )
1076 i_type = VLC_VAR_LIST;
1078 i_type = config_GetType( p_obj, psz_name );
1080 if( !i_type && !psz_value )
1082 /* check for "no-foo" or "nofoo" */
1083 if( !strncmp( psz_name, "no-", 3 ) )
1085 memmove( psz_name, psz_name + 3, strlen(psz_name) + 1 - 3 );
1087 else if( !strncmp( psz_name, "no", 2 ) )
1089 memmove( psz_name, psz_name + 2, strlen(psz_name) + 1 - 2 );
1091 else goto cleanup; /* Option doesn't exist */
1094 i_type = config_GetType( p_obj, psz_name );
1096 if( !i_type ) goto cleanup; /* Option doesn't exist */
1098 if( ( i_type != VLC_VAR_BOOL ) &&
1099 ( !psz_value || !*psz_value ) ) goto cleanup; /* Invalid value */
1101 /* check if option is unsafe */
1104 module_config_t *p_config = config_FindConfig( p_obj, psz_name );
1105 if( !p_config->b_safe )
1107 msg_Err( p_obj, "unsafe option \"%s\" has been ignored for "
1108 "security reasons", psz_name );
1113 /* Create the variable in the input object.
1114 * Children of the input object will be able to retreive this value
1115 * thanks to the inheritance property of the object variables. */
1116 var_Create( p_obj, psz_name, i_type );
1121 val.b_bool = !b_isno;
1124 case VLC_VAR_INTEGER:
1125 val.i_int = strtol( psz_value, NULL, 0 );
1129 val.f_float = atof( psz_value );
1132 case VLC_VAR_STRING:
1133 case VLC_VAR_MODULE:
1135 case VLC_VAR_DIRECTORY:
1136 val.psz_string = psz_value;
1141 char *psz_orig, *psz_var;
1142 vlc_list_t *p_list = malloc(sizeof(vlc_list_t));
1143 val.p_list = p_list;
1144 p_list->i_count = 0;
1146 psz_var = psz_orig = strdup(psz_value);
1147 while( psz_var && *psz_var )
1149 char *psz_item = psz_var;
1151 while( *psz_var && *psz_var != ',' ) psz_var++;
1152 if( *psz_var == ',' )
1157 val2.i_int = strtol( psz_item, NULL, 0 );
1158 INSERT_ELEM( p_list->p_values, p_list->i_count,
1159 p_list->i_count, val2 );
1160 /* p_list->i_count is incremented twice by INSERT_ELEM */
1162 INSERT_ELEM( p_list->pi_types, p_list->i_count,
1163 p_list->i_count, VLC_VAR_INTEGER );
1173 var_Set( p_obj, psz_name, val );
1180 /* Following functions are local */
1182 /*****************************************************************************
1183 * GetUnused: find an unused variable from its name
1184 *****************************************************************************
1185 * We do i_tries tries before giving up, just in case the variable is being
1186 * modified and called from a callback.
1187 *****************************************************************************/
1188 static int GetUnused( vlc_object_t *p_this, const char *psz_name )
1190 int i_var, i_tries = 0;
1191 vlc_object_internals_t *p_priv = vlc_internals( p_this );
1195 i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
1201 if( ! p_priv->p_vars[i_var].b_incallback )
1206 if( i_tries++ > 100 )
1208 msg_Err( p_this, "caught in a callback deadlock?" );
1209 return VLC_ETIMEOUT;
1212 vlc_mutex_unlock( &p_priv->var_lock );
1213 msleep( THREAD_SLEEP );
1214 vlc_mutex_lock( &p_priv->var_lock );
1218 /*****************************************************************************
1219 * HashString: our cool hash function
1220 *****************************************************************************
1221 * This function is not intended to be crypto-secure, we only want it to be
1222 * fast and not suck too much. This one is pretty fast and did 0 collisions
1223 * in wenglish's dictionary.
1224 *****************************************************************************/
1225 static uint32_t HashString( const char *psz_string )
1227 uint32_t i_hash = 0;
1229 while( *psz_string )
1231 i_hash += *psz_string++;
1232 i_hash += i_hash << 10;
1233 i_hash ^= i_hash >> 8;
1239 /*****************************************************************************
1240 * Insert: find an empty slot to insert a new variable
1241 *****************************************************************************
1242 * We use a recursive inner function indexed on the hash. This function does
1243 * nothing in the rare cases where a collision may occur, see Lookup()
1244 * to see how we handle them.
1245 * XXX: does this really need to be written recursively?
1246 *****************************************************************************/
1247 static int Insert( variable_t *p_vars, int i_count, const char *psz_name )
1254 return InsertInner( p_vars, i_count, HashString( psz_name ) );
1257 static int InsertInner( variable_t *p_vars, int i_count, uint32_t i_hash )
1261 if( i_hash <= p_vars[0].i_hash )
1266 if( i_hash >= p_vars[i_count - 1].i_hash )
1271 i_middle = i_count / 2;
1273 /* We know that 0 < i_middle */
1274 if( i_hash < p_vars[i_middle].i_hash )
1276 return InsertInner( p_vars, i_middle, i_hash );
1279 /* We know that i_middle + 1 < i_count */
1280 if( i_hash > p_vars[i_middle + 1].i_hash )
1282 return i_middle + 1 + InsertInner( p_vars + i_middle + 1,
1283 i_count - i_middle - 1,
1287 return i_middle + 1;
1290 /*****************************************************************************
1291 * Lookup: find an existing variable given its name
1292 *****************************************************************************
1293 * We use a recursive inner function indexed on the hash. Care is taken of
1294 * possible hash collisions.
1295 * XXX: does this really need to be written recursively?
1296 *****************************************************************************/
1297 static int Lookup( variable_t *p_vars, int i_count, const char *psz_name )
1307 i_hash = HashString( psz_name );
1309 i_pos = LookupInner( p_vars, i_count, i_hash );
1311 /* Hash not found */
1312 if( i_hash != p_vars[i_pos].i_hash )
1317 /* Hash found, entry found */
1318 if( !strcmp( psz_name, p_vars[i_pos].psz_name ) )
1323 /* Hash collision! This should be very rare, but we cannot guarantee
1324 * it will never happen. Just do an exhaustive search amongst all
1325 * entries with the same hash. */
1326 for( i = i_pos - 1 ; i > 0 && i_hash == p_vars[i].i_hash ; i-- )
1328 if( !strcmp( psz_name, p_vars[i].psz_name ) )
1334 for( i = i_pos + 1 ; i < i_count && i_hash == p_vars[i].i_hash ; i++ )
1336 if( !strcmp( psz_name, p_vars[i].psz_name ) )
1342 /* Hash found, but entry not found */
1346 static int LookupInner( variable_t *p_vars, int i_count, uint32_t i_hash )
1350 if( i_hash <= p_vars[0].i_hash )
1355 if( i_hash >= p_vars[i_count-1].i_hash )
1360 i_middle = i_count / 2;
1362 /* We know that 0 < i_middle */
1363 if( i_hash < p_vars[i_middle].i_hash )
1365 return LookupInner( p_vars, i_middle, i_hash );
1368 /* We know that i_middle + 1 < i_count */
1369 if( i_hash > p_vars[i_middle].i_hash )
1371 return i_middle + LookupInner( p_vars + i_middle,
1379 /*****************************************************************************
1380 * CheckValue: check that a value is valid wrt. a variable
1381 *****************************************************************************
1382 * This function checks p_val's value against p_var's limitations such as
1383 * minimal and maximal value, step, in-list position, and modifies p_val if
1385 ****************************************************************************/
1386 static void CheckValue ( variable_t *p_var, vlc_value_t *p_val )
1388 /* Check that our variable is in the list */
1389 if( p_var->i_type & VLC_VAR_HASCHOICE && p_var->choices.i_count )
1393 /* FIXME: the list is sorted, dude. Use something cleverer. */
1394 for( i = p_var->choices.i_count ; i-- ; )
1396 if( p_var->pf_cmp( *p_val, p_var->choices.p_values[i] ) == 0 )
1402 /* If not found, change it to anything vaguely valid */
1405 /* Free the old variable, get the new one, dup it */
1406 p_var->pf_free( p_val );
1407 *p_val = p_var->choices.p_values[p_var->i_default >= 0
1408 ? p_var->i_default : 0 ];
1409 p_var->pf_dup( p_val );
1413 /* Check that our variable is within the bounds */
1414 switch( p_var->i_type & VLC_VAR_TYPE )
1416 case VLC_VAR_INTEGER:
1417 if( p_var->i_type & VLC_VAR_HASSTEP && p_var->step.i_int
1418 && (p_val->i_int % p_var->step.i_int) )
1420 p_val->i_int = (p_val->i_int + (p_var->step.i_int / 2))
1421 / p_var->step.i_int * p_var->step.i_int;
1423 if( p_var->i_type & VLC_VAR_HASMIN
1424 && p_val->i_int < p_var->min.i_int )
1426 p_val->i_int = p_var->min.i_int;
1428 if( p_var->i_type & VLC_VAR_HASMAX
1429 && p_val->i_int > p_var->max.i_int )
1431 p_val->i_int = p_var->max.i_int;
1435 if( p_var->i_type & VLC_VAR_HASSTEP && p_var->step.f_float )
1437 float f_round = p_var->step.f_float * (float)(int)( 0.5 +
1438 p_val->f_float / p_var->step.f_float );
1439 if( p_val->f_float != f_round )
1441 p_val->f_float = f_round;
1444 if( p_var->i_type & VLC_VAR_HASMIN
1445 && p_val->f_float < p_var->min.f_float )
1447 p_val->f_float = p_var->min.f_float;
1449 if( p_var->i_type & VLC_VAR_HASMAX
1450 && p_val->f_float > p_var->max.f_float )
1452 p_val->f_float = p_var->max.f_float;
1461 /*****************************************************************************
1462 * InheritValue: try to inherit the value of this variable from the same one
1463 * in our closest parent.
1464 *****************************************************************************/
1465 static int InheritValue( vlc_object_t *p_this, const char *psz_name,
1466 vlc_value_t *p_val, int i_type )
1471 /* No need to take the structure lock,
1472 * we are only looking for our parents */
1474 if( !p_this->p_parent )
1476 switch( i_type & VLC_VAR_TYPE )
1479 case VLC_VAR_DIRECTORY:
1480 case VLC_VAR_STRING:
1481 case VLC_VAR_MODULE:
1482 p_val->psz_string = config_GetPsz( p_this, psz_name );
1483 if( !p_val->psz_string ) p_val->psz_string = strdup("");
1486 p_val->f_float = config_GetFloat( p_this, psz_name );
1488 case VLC_VAR_INTEGER:
1489 case VLC_VAR_HOTKEY:
1490 p_val->i_int = config_GetInt( p_this, psz_name );
1493 p_val->b_bool = config_GetInt( p_this, psz_name );
1497 char *psz_orig, *psz_var;
1498 vlc_list_t *p_list = malloc(sizeof(vlc_list_t));
1499 p_val->p_list = p_list;
1500 p_list->i_count = 0;
1502 psz_var = psz_orig = config_GetPsz( p_this, psz_name );
1503 while( psz_var && *psz_var )
1505 char *psz_item = psz_var;
1507 while( *psz_var && *psz_var != ',' ) psz_var++;
1508 if( *psz_var == ',' )
1513 val.i_int = strtol( psz_item, NULL, 0 );
1514 INSERT_ELEM( p_list->p_values, p_list->i_count,
1515 p_list->i_count, val );
1516 /* p_list->i_count is incremented twice by INSERT_ELEM */
1518 INSERT_ELEM( p_list->pi_types, p_list->i_count,
1519 p_list->i_count, VLC_VAR_INTEGER );
1532 vlc_object_internals_t *p_priv = vlc_internals( p_this->p_parent );
1534 /* Look for the variable */
1535 vlc_mutex_lock( &p_priv->var_lock );
1537 i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
1542 p_var = &p_priv->p_vars[i_var];
1544 /* Really get the variable */
1545 *p_val = p_var->val;
1547 /* Duplicate value if needed */
1548 p_var->pf_dup( p_val );
1550 vlc_mutex_unlock( &p_priv->var_lock );
1554 vlc_mutex_unlock( &p_priv->var_lock );
1556 /* We're still not there */
1558 return InheritValue( p_this->p_parent, psz_name, p_val, i_type );
1561 /**********************************************************************
1562 * Execute a var command on an object identified by its name
1563 **********************************************************************/
1564 int __var_Command( vlc_object_t *p_this, const char *psz_name,
1565 const char *psz_cmd, const char *psz_arg, char **psz_msg )
1567 vlc_object_t *p_obj = vlc_object_find_name( p_this->p_libvlc,
1568 psz_name, FIND_CHILD );
1574 *psz_msg = strdup( "Unknown destination object." );
1578 i_type = var_Type( p_obj, psz_cmd );
1579 if( !( i_type&VLC_VAR_ISCOMMAND ) )
1581 vlc_object_release( p_obj );
1583 *psz_msg = strdup( "Variable doesn't exist or isn't a command." );
1584 return VLC_EGENERIC;
1590 case VLC_VAR_INTEGER:
1591 i_ret = var_SetInteger( p_obj, psz_cmd, atoi( psz_arg ) );
1594 i_ret = var_SetFloat( p_obj, psz_cmd, atof( psz_arg ) );
1596 case VLC_VAR_STRING:
1597 i_ret = var_SetString( p_obj, psz_cmd, psz_arg );
1600 i_ret = var_SetBool( p_obj, psz_cmd, atoi( psz_arg ) );
1603 i_ret = VLC_EGENERIC;
1607 vlc_object_release( p_obj );
1611 *psz_msg = (char*)malloc( 80 );
1612 sprintf( *psz_msg, "%s on object %s returned %i (%s)",
1613 psz_cmd, psz_name, i_ret, vlc_error( i_ret ) );