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 *****************************************************************************/
28 #include "variables.h"
31 # include <stdlib.h> /* realloc() */
34 /*****************************************************************************
36 *****************************************************************************/
37 struct callback_entry_t
39 vlc_callback_t pf_callback;
43 /*****************************************************************************
44 * Local comparison functions, returns 0 if v == w, < 0 if v < w, > 0 if v > w
45 *****************************************************************************/
46 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; }
47 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; }
48 static int CmpTime( vlc_value_t v, vlc_value_t w )
50 return v.i_time == w.i_time ? 0 : v.i_time > w.i_time ? 1 : -1;
52 static int CmpString( vlc_value_t v, vlc_value_t w ) { return strcmp( v.psz_string, w.psz_string ); }
53 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; }
54 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; }
56 /*****************************************************************************
57 * Local duplication functions, and local deallocation functions
58 *****************************************************************************/
59 static void DupDummy( vlc_value_t *p_val ) { (void)p_val; /* unused */ }
60 static void DupString( vlc_value_t *p_val ) { p_val->psz_string = strdup( p_val->psz_string ); }
62 static void DupList( vlc_value_t *p_val )
65 vlc_list_t *p_list = malloc( sizeof(vlc_list_t) );
67 p_list->i_count = p_val->p_list->i_count;
68 if( p_val->p_list->i_count )
70 p_list->p_values = malloc( p_list->i_count * sizeof(vlc_value_t) );
71 p_list->pi_types = malloc( p_list->i_count * sizeof(int) );
75 p_list->p_values = NULL;
76 p_list->pi_types = NULL;
79 for( i = 0; i < p_list->i_count; i++ )
81 p_list->p_values[i] = p_val->p_list->p_values[i];
82 p_list->pi_types[i] = p_val->p_list->pi_types[i];
83 switch( p_val->p_list->pi_types[i] & VLC_VAR_TYPE )
87 DupString( &p_list->p_values[i] );
94 p_val->p_list = p_list;
97 static void FreeDummy( vlc_value_t *p_val ) { (void)p_val; /* unused */ }
98 static void FreeString( vlc_value_t *p_val ) { free( p_val->psz_string ); }
99 static void FreeMutex( vlc_value_t *p_val ) { vlc_mutex_destroy( (vlc_mutex_t*)p_val->p_address ); free( p_val->p_address ); }
101 static void FreeList( vlc_value_t *p_val )
104 for( i = 0; i < p_val->p_list->i_count; i++ )
106 switch( p_val->p_list->pi_types[i] & VLC_VAR_TYPE )
109 FreeString( &p_val->p_list->p_values[i] );
112 FreeMutex( &p_val->p_list->p_values[i] );
119 if( p_val->p_list->i_count )
121 free( p_val->p_list->p_values );
122 free( p_val->p_list->pi_types );
124 free( p_val->p_list );
127 /*****************************************************************************
129 *****************************************************************************/
130 static int GetUnused ( vlc_object_t *, const char * );
131 static uint32_t HashString ( const char * );
132 static int Insert ( variable_t *, int, const char * );
133 static int InsertInner ( variable_t *, int, uint32_t );
134 static int Lookup ( variable_t *, int, const char * );
135 static int LookupInner ( variable_t *, int, uint32_t );
137 static void CheckValue ( variable_t *, vlc_value_t * );
139 static int InheritValue( vlc_object_t *, const char *, vlc_value_t *,
143 * Initialize a vlc variable
145 * We hash the given string and insert it into the sorted list. The insertion
146 * may require slow memory copies, but think about what we gain in the log(n)
147 * lookup phase when setting/getting the variable value!
149 * \param p_this The object in which to create the variable
150 * \param psz_name The name of the variable
151 * \param i_type The variables type. Must be one of \ref var_type combined with
152 * zero or more \ref var_flags
154 int __var_Create( vlc_object_t *p_this, const char *psz_name, int i_type )
158 static vlc_list_t dummy_null_list = {0, NULL, NULL};
160 vlc_mutex_lock( &p_this->var_lock );
162 /* FIXME: if the variable already exists, we don't duplicate it. But we
163 * duplicate the lookups. It's not that serious, but if anyone finds some
164 * time to rework Insert() so that only one lookup has to be done, feel
166 i_new = Lookup( p_this->p_vars, p_this->i_vars, psz_name );
170 /* If the types differ, variable creation failed. */
171 if( (i_type & ~(VLC_VAR_DOINHERIT|VLC_VAR_ISCOMMAND)) != p_this->p_vars[i_new].i_type )
173 vlc_mutex_unlock( &p_this->var_lock );
177 p_this->p_vars[i_new].i_usage++;
178 if( i_type & VLC_VAR_ISCOMMAND )
179 p_this->p_vars[i_new].i_type |= VLC_VAR_ISCOMMAND;
180 vlc_mutex_unlock( &p_this->var_lock );
184 i_new = Insert( p_this->p_vars, p_this->i_vars, psz_name );
186 if( (p_this->i_vars & 15) == 15 )
188 p_this->p_vars = realloc( p_this->p_vars,
189 (p_this->i_vars+17) * sizeof(variable_t) );
192 memmove( p_this->p_vars + i_new + 1,
193 p_this->p_vars + i_new,
194 (p_this->i_vars - i_new) * sizeof(variable_t) );
198 p_var = &p_this->p_vars[i_new];
199 memset( p_var, 0, sizeof(*p_var) );
201 p_var->i_hash = HashString( psz_name );
202 p_var->psz_name = strdup( psz_name );
203 p_var->psz_text = NULL;
205 p_var->i_type = i_type & ~VLC_VAR_DOINHERIT;
206 memset( &p_var->val, 0, sizeof(vlc_value_t) );
208 p_var->pf_dup = DupDummy;
209 p_var->pf_free = FreeDummy;
213 p_var->i_default = -1;
214 p_var->choices.i_count = 0;
215 p_var->choices.p_values = NULL;
216 p_var->choices_text.i_count = 0;
217 p_var->choices_text.p_values = NULL;
219 p_var->b_incallback = VLC_FALSE;
220 p_var->i_entries = 0;
221 p_var->p_entries = NULL;
223 /* Always initialize the variable, even if it is a list variable; this
224 * will lead to errors if the variable is not initialized, but it will
225 * not cause crashes in the variable handling. */
226 switch( i_type & VLC_VAR_TYPE )
229 p_var->pf_cmp = CmpBool;
230 p_var->val.b_bool = VLC_FALSE;
232 case VLC_VAR_INTEGER:
234 p_var->pf_cmp = CmpInt;
235 p_var->val.i_int = 0;
240 case VLC_VAR_DIRECTORY:
241 case VLC_VAR_VARIABLE:
242 p_var->pf_cmp = CmpString;
243 p_var->pf_dup = DupString;
244 p_var->pf_free = FreeString;
245 p_var->val.psz_string = "";
248 p_var->pf_cmp = CmpFloat;
249 p_var->val.f_float = 0.0;
252 p_var->pf_cmp = CmpTime;
253 p_var->val.i_time = 0;
255 case VLC_VAR_ADDRESS:
256 p_var->pf_cmp = CmpAddress;
257 p_var->val.p_address = NULL;
260 p_var->pf_cmp = CmpAddress;
261 p_var->pf_free = FreeMutex;
262 p_var->val.p_address = malloc( sizeof(vlc_mutex_t) );
263 vlc_mutex_init( p_this, (vlc_mutex_t*)p_var->val.p_address );
266 p_var->pf_cmp = CmpAddress;
267 p_var->pf_dup = DupList;
268 p_var->pf_free = FreeList;
269 p_var->val.p_list = &dummy_null_list;
273 /* Duplicate the default data we stored. */
274 p_var->pf_dup( &p_var->val );
276 if( i_type & VLC_VAR_DOINHERIT )
280 if( InheritValue( p_this, psz_name, &val, p_var->i_type )
283 /* Free data if needed */
284 p_var->pf_free( &p_var->val );
285 /* Set the variable */
288 if( i_type & VLC_VAR_HASCHOICE )
290 /* We must add the inherited value to our choice list */
291 p_var->i_default = 0;
293 INSERT_ELEM( p_var->choices.p_values, p_var->choices.i_count,
295 INSERT_ELEM( p_var->choices_text.p_values,
296 p_var->choices_text.i_count, 0, val );
297 p_var->pf_dup( &p_var->choices.p_values[0] );
298 p_var->choices_text.p_values[0].psz_string = NULL;
303 vlc_mutex_unlock( &p_this->var_lock );
309 * Destroy a vlc variable
311 * Look for the variable and destroy it if it is found. As in var_Create we
312 * do a call to memmove() but we have performance counterparts elsewhere.
314 * \param p_this The object that holds the variable
315 * \param psz_name The name of the variable
317 int __var_Destroy( vlc_object_t *p_this, const char *psz_name )
322 vlc_mutex_lock( &p_this->var_lock );
324 i_var = GetUnused( p_this, psz_name );
327 vlc_mutex_unlock( &p_this->var_lock );
331 p_var = &p_this->p_vars[i_var];
333 if( p_var->i_usage > 1 )
336 vlc_mutex_unlock( &p_this->var_lock );
340 /* Free value if needed */
341 p_var->pf_free( &p_var->val );
343 /* Free choice list if needed */
344 if( p_var->choices.i_count )
346 for( i = 0 ; i < p_var->choices.i_count ; i++ )
348 p_var->pf_free( &p_var->choices.p_values[i] );
349 if( p_var->choices_text.p_values[i].psz_string )
350 free( p_var->choices_text.p_values[i].psz_string );
352 free( p_var->choices.p_values );
353 free( p_var->choices_text.p_values );
356 /* Free callbacks if needed */
357 if( p_var->p_entries )
359 free( p_var->p_entries );
362 free( p_var->psz_name );
363 if( p_var->psz_text ) free( p_var->psz_text );
365 memmove( p_this->p_vars + i_var,
366 p_this->p_vars + i_var + 1,
367 (p_this->i_vars - i_var - 1) * sizeof(variable_t) );
369 if( (p_this->i_vars & 15) == 0 )
371 p_this->p_vars = realloc( p_this->p_vars,
372 (p_this->i_vars) * sizeof( variable_t ) );
377 vlc_mutex_unlock( &p_this->var_lock );
383 * Perform an action on a variable
385 * \param p_this The object that holds the variable
386 * \param psz_name The name of the variable
387 * \param i_action The action to perform. Must be one of \ref var_action
388 * \param p_val First action parameter
389 * \param p_val2 Second action parameter
391 int __var_Change( vlc_object_t *p_this, const char *psz_name,
392 int i_action, vlc_value_t *p_val, vlc_value_t *p_val2 )
398 vlc_mutex_lock( &p_this->var_lock );
400 i_var = Lookup( p_this->p_vars, p_this->i_vars, psz_name );
404 vlc_mutex_unlock( &p_this->var_lock );
408 p_var = &p_this->p_vars[i_var];
413 if( p_var->i_type & VLC_VAR_HASMIN )
415 p_var->pf_free( &p_var->min );
417 p_var->i_type |= VLC_VAR_HASMIN;
419 p_var->pf_dup( &p_var->min );
420 CheckValue( p_var, &p_var->val );
423 if( p_var->i_type & VLC_VAR_HASMAX )
425 p_var->pf_free( &p_var->max );
427 p_var->i_type |= VLC_VAR_HASMAX;
429 p_var->pf_dup( &p_var->max );
430 CheckValue( p_var, &p_var->val );
432 case VLC_VAR_SETSTEP:
433 if( p_var->i_type & VLC_VAR_HASSTEP )
435 p_var->pf_free( &p_var->step );
437 p_var->i_type |= VLC_VAR_HASSTEP;
438 p_var->step = *p_val;
439 p_var->pf_dup( &p_var->step );
440 CheckValue( p_var, &p_var->val );
442 case VLC_VAR_ADDCHOICE:
443 /* FIXME: the list is sorted, dude. Use something cleverer. */
444 for( i = p_var->choices.i_count ; i-- ; )
446 if( p_var->pf_cmp( p_var->choices.p_values[i], *p_val ) < 0 )
452 /* The new place is i+1 */
455 if( p_var->i_default >= i )
460 INSERT_ELEM( p_var->choices.p_values, p_var->choices.i_count,
462 INSERT_ELEM( p_var->choices_text.p_values,
463 p_var->choices_text.i_count, i, *p_val );
464 p_var->pf_dup( &p_var->choices.p_values[i] );
465 p_var->choices_text.p_values[i].psz_string =
466 ( p_val2 && p_val2->psz_string ) ?
467 strdup( p_val2->psz_string ) : NULL;
469 CheckValue( p_var, &p_var->val );
471 case VLC_VAR_DELCHOICE:
472 /* FIXME: the list is sorted, dude. Use something cleverer. */
473 for( i = 0 ; i < p_var->choices.i_count ; i++ )
475 if( p_var->pf_cmp( p_var->choices.p_values[i], *p_val ) == 0 )
481 if( i == p_var->choices.i_count )
484 vlc_mutex_unlock( &p_this->var_lock );
488 if( p_var->i_default > i )
492 else if( p_var->i_default == i )
494 p_var->i_default = -1;
497 p_var->pf_free( &p_var->choices.p_values[i] );
498 if( p_var->choices_text.p_values[i].psz_string )
499 free( p_var->choices_text.p_values[i].psz_string );
500 REMOVE_ELEM( p_var->choices.p_values, p_var->choices.i_count, i );
501 REMOVE_ELEM( p_var->choices_text.p_values,
502 p_var->choices_text.i_count, i );
504 CheckValue( p_var, &p_var->val );
506 case VLC_VAR_CHOICESCOUNT:
507 p_val->i_int = p_var->choices.i_count;
509 case VLC_VAR_CLEARCHOICES:
510 for( i = 0 ; i < p_var->choices.i_count ; i++ )
512 p_var->pf_free( &p_var->choices.p_values[i] );
514 for( i = 0 ; i < p_var->choices_text.i_count ; i++ )
516 if( p_var->choices_text.p_values[i].psz_string )
517 free( p_var->choices_text.p_values[i].psz_string );
519 if( p_var->choices.i_count ) free( p_var->choices.p_values );
520 if( p_var->choices_text.i_count ) free( p_var->choices_text.p_values );
522 p_var->choices.i_count = 0;
523 p_var->choices.p_values = NULL;
524 p_var->choices_text.i_count = 0;
525 p_var->choices_text.p_values = NULL;
526 p_var->i_default = -1;
528 case VLC_VAR_SETDEFAULT:
529 /* FIXME: the list is sorted, dude. Use something cleverer. */
530 for( i = 0 ; i < p_var->choices.i_count ; i++ )
532 if( p_var->pf_cmp( p_var->choices.p_values[i], *p_val ) == 0 )
538 if( i == p_var->choices.i_count )
544 p_var->i_default = i;
545 CheckValue( p_var, &p_var->val );
547 case VLC_VAR_SETVALUE:
548 /* Duplicate data if needed */
549 p_var->pf_dup( p_val );
550 /* Backup needed stuff */
552 /* Check boundaries and list */
553 CheckValue( p_var, p_val );
554 /* Set the variable */
556 /* Free data if needed */
557 p_var->pf_free( &oldval );
559 case VLC_VAR_GETCHOICES:
560 case VLC_VAR_GETLIST:
561 p_val->p_list = malloc( sizeof(vlc_list_t) );
562 if( p_val2 ) p_val2->p_list = malloc( sizeof(vlc_list_t) );
563 if( p_var->choices.i_count )
565 p_val->p_list->p_values = malloc( p_var->choices.i_count
566 * sizeof(vlc_value_t) );
567 p_val->p_list->pi_types = malloc( p_var->choices.i_count
571 p_val2->p_list->p_values =
572 malloc( p_var->choices.i_count * sizeof(vlc_value_t) );
573 p_val2->p_list->pi_types =
574 malloc( p_var->choices.i_count * sizeof(int) );
577 p_val->p_list->i_count = p_var->choices.i_count;
578 if( p_val2 ) p_val2->p_list->i_count = p_var->choices.i_count;
579 for( i = 0 ; i < p_var->choices.i_count ; i++ )
581 p_val->p_list->p_values[i] = p_var->choices.p_values[i];
582 p_val->p_list->pi_types[i] = p_var->i_type;
583 p_var->pf_dup( &p_val->p_list->p_values[i] );
586 p_val2->p_list->p_values[i].psz_string =
587 p_var->choices_text.p_values[i].psz_string ?
588 strdup(p_var->choices_text.p_values[i].psz_string) : NULL;
589 p_val2->p_list->pi_types[i] = VLC_VAR_STRING;
593 case VLC_VAR_FREELIST:
595 if( p_val2 && p_val2->p_list )
597 for( i = 0; i < p_val2->p_list->i_count; i++ )
598 if( p_val2->p_list->p_values[i].psz_string )
599 free( p_val2->p_list->p_values[i].psz_string );
600 if( p_val2->p_list->i_count )
602 free( p_val2->p_list->p_values );
603 free( p_val2->p_list->pi_types );
605 free( p_val2->p_list );
608 case VLC_VAR_SETTEXT:
609 if( p_var->psz_text ) free( p_var->psz_text );
610 if( p_val && p_val->psz_string )
611 p_var->psz_text = strdup( p_val->psz_string );
613 case VLC_VAR_GETTEXT:
614 p_val->psz_string = NULL;
615 if( p_var->psz_text )
617 p_val->psz_string = strdup( p_var->psz_text );
620 case VLC_VAR_INHERITVALUE:
624 if( InheritValue( p_this, psz_name, &val, p_var->i_type )
627 /* Duplicate already done */
629 /* Backup needed stuff */
631 /* Check boundaries and list */
632 CheckValue( p_var, &val );
633 /* Set the variable */
635 /* Free data if needed */
636 p_var->pf_free( &oldval );
642 p_var->pf_dup( p_val );
646 case VLC_VAR_TRIGGER_CALLBACKS:
648 /* Deal with callbacks. Tell we're in a callback, release the lock,
649 * call stored functions, retake the lock. */
650 if( p_var->i_entries )
653 int i_entries = p_var->i_entries;
654 callback_entry_t *p_entries = p_var->p_entries;
656 p_var->b_incallback = VLC_TRUE;
657 vlc_mutex_unlock( &p_this->var_lock );
660 for( ; i_entries-- ; )
662 p_entries[i_entries].pf_callback( p_this, psz_name, p_var->val, p_var->val,
663 p_entries[i_entries].p_data );
666 vlc_mutex_lock( &p_this->var_lock );
668 i_var = Lookup( p_this->p_vars, p_this->i_vars, psz_name );
671 msg_Err( p_this, "variable %s has disappeared", psz_name );
672 vlc_mutex_unlock( &p_this->var_lock );
676 p_var = &p_this->p_vars[i_var];
677 p_var->b_incallback = VLC_FALSE;
686 vlc_mutex_unlock( &p_this->var_lock );
692 * Request a variable's type
694 * \return The variable type if it exists, or 0 if the
695 * variable could not be found.
698 int __var_Type( vlc_object_t *p_this, const char *psz_name )
702 vlc_mutex_lock( &p_this->var_lock );
704 i_var = Lookup( p_this->p_vars, p_this->i_vars, psz_name );
708 vlc_mutex_unlock( &p_this->var_lock );
712 i_type = p_this->p_vars[i_var].i_type;
714 vlc_mutex_unlock( &p_this->var_lock );
720 * Set a variable's value
722 * \param p_this The object that hold the variable
723 * \param psz_name The name of the variable
724 * \param val the value to set
726 int __var_Set( vlc_object_t *p_this, const char *psz_name, vlc_value_t val )
732 vlc_mutex_lock( &p_this->var_lock );
734 i_var = GetUnused( p_this, psz_name );
737 vlc_mutex_unlock( &p_this->var_lock );
741 p_var = &p_this->p_vars[i_var];
743 /* Duplicate data if needed */
744 p_var->pf_dup( &val );
746 /* Backup needed stuff */
749 /* Check boundaries and list */
750 CheckValue( p_var, &val );
752 /* Set the variable */
755 /* Deal with callbacks. Tell we're in a callback, release the lock,
756 * call stored functions, retake the lock. */
757 if( p_var->i_entries )
760 int i_entries = p_var->i_entries;
761 callback_entry_t *p_entries = p_var->p_entries;
763 p_var->b_incallback = VLC_TRUE;
764 vlc_mutex_unlock( &p_this->var_lock );
767 for( ; i_entries-- ; )
769 p_entries[i_entries].pf_callback( p_this, psz_name, oldval, val,
770 p_entries[i_entries].p_data );
773 vlc_mutex_lock( &p_this->var_lock );
775 i_var = Lookup( p_this->p_vars, p_this->i_vars, psz_name );
778 msg_Err( p_this, "variable %s has disappeared", psz_name );
779 vlc_mutex_unlock( &p_this->var_lock );
783 p_var = &p_this->p_vars[i_var];
784 p_var->b_incallback = VLC_FALSE;
787 /* Free data if needed */
788 p_var->pf_free( &oldval );
790 vlc_mutex_unlock( &p_this->var_lock );
796 * Get a variable's value
798 * \param p_this The object that holds the variable
799 * \param psz_name The name of the variable
800 * \param p_val Pointer to a vlc_value_t that will hold the variable's value
801 * after the function is finished
803 int __var_Get( vlc_object_t *p_this, const char *psz_name, vlc_value_t *p_val )
808 vlc_mutex_lock( &p_this->var_lock );
810 i_var = Lookup( p_this->p_vars, p_this->i_vars, psz_name );
814 vlc_mutex_unlock( &p_this->var_lock );
818 p_var = &p_this->p_vars[i_var];
820 /* Really get the variable */
823 /* Duplicate value if needed */
824 p_var->pf_dup( p_val );
826 vlc_mutex_unlock( &p_this->var_lock );
832 * Register a callback in a variable
834 * We store a function pointer that will be called upon variable
837 * \param p_this The object that holds the variable
838 * \param psz_name The name of the variable
839 * \param pf_callback The function pointer
840 * \param p_data A generic pointer that will be passed as the last
841 * argument to the callback function.
843 * \warning The callback function is run in the thread that calls var_Set on
844 * the variable. Use proper locking. This thread may not have much
845 * time to spare, so keep callback functions short.
847 int __var_AddCallback( vlc_object_t *p_this, const char *psz_name,
848 vlc_callback_t pf_callback, void *p_data )
852 callback_entry_t entry;
854 entry.pf_callback = pf_callback;
855 entry.p_data = p_data;
857 vlc_mutex_lock( &p_this->var_lock );
859 i_var = GetUnused( p_this, psz_name );
862 vlc_mutex_unlock( &p_this->var_lock );
866 p_var = &p_this->p_vars[i_var];
868 INSERT_ELEM( p_var->p_entries,
873 vlc_mutex_unlock( &p_this->var_lock );
879 * Remove a callback from a variable
881 * pf_callback and p_data have to be given again, because different objects
882 * might have registered the same callback function.
884 int __var_DelCallback( vlc_object_t *p_this, const char *psz_name,
885 vlc_callback_t pf_callback, void *p_data )
890 vlc_mutex_lock( &p_this->var_lock );
892 i_var = GetUnused( p_this, psz_name );
895 vlc_mutex_unlock( &p_this->var_lock );
899 p_var = &p_this->p_vars[i_var];
901 for( i_entry = p_var->i_entries ; i_entry-- ; )
903 if( p_var->p_entries[i_entry].pf_callback == pf_callback
904 && p_var->p_entries[i_entry].p_data == p_data )
912 vlc_mutex_unlock( &p_this->var_lock );
916 REMOVE_ELEM( p_var->p_entries, p_var->i_entries, i_entry );
918 vlc_mutex_unlock( &p_this->var_lock );
924 * Trigger callback on a variable
926 * \param p_this The object that hold the variable
927 * \param psz_name The name of the variable
929 int __var_TriggerCallback( vlc_object_t *p_this, const char *psz_name )
935 vlc_mutex_lock( &p_this->var_lock );
937 i_var = GetUnused( p_this, psz_name );
940 vlc_mutex_unlock( &p_this->var_lock );
944 p_var = &p_this->p_vars[i_var];
946 /* Backup needed stuff */
949 /* Deal with callbacks. Tell we're in a callback, release the lock,
950 * call stored functions, retake the lock. */
951 if( p_var->i_entries )
954 int i_entries = p_var->i_entries;
955 callback_entry_t *p_entries = p_var->p_entries;
957 p_var->b_incallback = VLC_TRUE;
958 vlc_mutex_unlock( &p_this->var_lock );
961 for( ; i_entries-- ; )
963 p_entries[i_entries].pf_callback( p_this, psz_name, oldval, oldval,
964 p_entries[i_entries].p_data );
967 vlc_mutex_lock( &p_this->var_lock );
969 i_var = Lookup( p_this->p_vars, p_this->i_vars, psz_name );
972 msg_Err( p_this, "variable %s has disappeared", psz_name );
973 vlc_mutex_unlock( &p_this->var_lock );
977 p_var = &p_this->p_vars[i_var];
978 p_var->b_incallback = VLC_FALSE;
981 vlc_mutex_unlock( &p_this->var_lock );
985 /** Parse a stringified option
986 * This function parse a string option and create the associated object
988 * The option must be of the form "[no[-]]foo[=bar]" where foo is the
989 * option name and bar is the value of the option.
990 * \param p_obj the object in which the variable must be created
991 * \param psz_option the option to parse
994 void __var_OptionParse( vlc_object_t *p_obj, const char *psz_option )
996 char *psz_name, *psz_value = strchr( psz_option, '=' );
997 int i_name_len, i_type;
998 vlc_bool_t b_isno = VLC_FALSE;
1001 if( psz_value ) i_name_len = psz_value - psz_option;
1002 else i_name_len = strlen( psz_option );
1004 /* It's too much of a hassle to remove the ':' when we parse
1005 * the cmd line :) */
1006 if( i_name_len && *psz_option == ':' )
1012 if( i_name_len == 0 ) return;
1014 psz_name = strndup( psz_option, i_name_len );
1015 if( psz_value ) psz_value++;
1017 /* FIXME: :programs should be handled generically */
1018 if( !strcmp( psz_name, "programs" ) )
1019 i_type = VLC_VAR_LIST;
1021 i_type = config_GetType( p_obj, psz_name );
1023 if( !i_type && !psz_value )
1025 /* check for "no-foo" or "nofoo" */
1026 if( !strncmp( psz_name, "no-", 3 ) )
1028 memmove( psz_name, psz_name + 3, strlen(psz_name) + 1 - 3 );
1030 else if( !strncmp( psz_name, "no", 2 ) )
1032 memmove( psz_name, psz_name + 2, strlen(psz_name) + 1 - 2 );
1034 else goto cleanup; /* Option doesn't exist */
1037 i_type = config_GetType( p_obj, psz_name );
1039 if( !i_type ) goto cleanup; /* Option doesn't exist */
1041 else if( !i_type ) goto cleanup; /* Option doesn't exist */
1043 if( ( i_type != VLC_VAR_BOOL ) &&
1044 ( !psz_value || !*psz_value ) ) goto cleanup; /* Invalid value */
1046 /* Create the variable in the input object.
1047 * Children of the input object will be able to retreive this value
1048 * thanks to the inheritance property of the object variables. */
1049 var_Create( p_obj, psz_name, i_type );
1054 val.b_bool = !b_isno;
1057 case VLC_VAR_INTEGER:
1058 val.i_int = strtol( psz_value, NULL, 0 );
1062 val.f_float = atof( psz_value );
1065 case VLC_VAR_STRING:
1066 case VLC_VAR_MODULE:
1068 case VLC_VAR_DIRECTORY:
1069 val.psz_string = psz_value;
1074 char *psz_orig, *psz_var;
1075 vlc_list_t *p_list = malloc(sizeof(vlc_list_t));
1076 val.p_list = p_list;
1077 p_list->i_count = 0;
1079 psz_var = psz_orig = strdup(psz_value);
1080 while( psz_var && *psz_var )
1082 char *psz_item = psz_var;
1084 while( *psz_var && *psz_var != ',' ) psz_var++;
1085 if( *psz_var == ',' )
1090 val2.i_int = strtol( psz_item, NULL, 0 );
1091 INSERT_ELEM( p_list->p_values, p_list->i_count,
1092 p_list->i_count, val2 );
1093 /* p_list->i_count is incremented twice by INSERT_ELEM */
1095 INSERT_ELEM( p_list->pi_types, p_list->i_count,
1096 p_list->i_count, VLC_VAR_INTEGER );
1098 if( psz_orig ) free( psz_orig );
1107 var_Set( p_obj, psz_name, val );
1110 if( psz_name ) free( psz_name );
1115 /* Following functions are local */
1117 /*****************************************************************************
1118 * GetUnused: find an unused variable from its name
1119 *****************************************************************************
1120 * We do i_tries tries before giving up, just in case the variable is being
1121 * modified and called from a callback.
1122 *****************************************************************************/
1123 static int GetUnused( vlc_object_t *p_this, const char *psz_name )
1125 int i_var, i_tries = 0;
1129 i_var = Lookup( p_this->p_vars, p_this->i_vars, psz_name );
1135 if( ! p_this->p_vars[i_var].b_incallback )
1140 if( i_tries++ > 100 )
1142 msg_Err( p_this, "caught in a callback deadlock?" );
1143 return VLC_ETIMEOUT;
1146 vlc_mutex_unlock( &p_this->var_lock );
1147 msleep( THREAD_SLEEP );
1148 vlc_mutex_lock( &p_this->var_lock );
1152 /*****************************************************************************
1153 * HashString: our cool hash function
1154 *****************************************************************************
1155 * This function is not intended to be crypto-secure, we only want it to be
1156 * fast and not suck too much. This one is pretty fast and did 0 collisions
1157 * in wenglish's dictionary.
1158 *****************************************************************************/
1159 static uint32_t HashString( const char *psz_string )
1161 uint32_t i_hash = 0;
1163 while( *psz_string )
1165 i_hash += *psz_string++;
1166 i_hash += i_hash << 10;
1167 i_hash ^= i_hash >> 8;
1173 /*****************************************************************************
1174 * Insert: find an empty slot to insert a new variable
1175 *****************************************************************************
1176 * We use a recursive inner function indexed on the hash. This function does
1177 * nothing in the rare cases where a collision may occur, see Lookup()
1178 * to see how we handle them.
1179 * XXX: does this really need to be written recursively?
1180 *****************************************************************************/
1181 static int Insert( variable_t *p_vars, int i_count, const char *psz_name )
1188 return InsertInner( p_vars, i_count, HashString( psz_name ) );
1191 static int InsertInner( variable_t *p_vars, int i_count, uint32_t i_hash )
1195 if( i_hash <= p_vars[0].i_hash )
1200 if( i_hash >= p_vars[i_count - 1].i_hash )
1205 i_middle = i_count / 2;
1207 /* We know that 0 < i_middle */
1208 if( i_hash < p_vars[i_middle].i_hash )
1210 return InsertInner( p_vars, i_middle, i_hash );
1213 /* We know that i_middle + 1 < i_count */
1214 if( i_hash > p_vars[i_middle + 1].i_hash )
1216 return i_middle + 1 + InsertInner( p_vars + i_middle + 1,
1217 i_count - i_middle - 1,
1221 return i_middle + 1;
1224 /*****************************************************************************
1225 * Lookup: find an existing variable given its name
1226 *****************************************************************************
1227 * We use a recursive inner function indexed on the hash. Care is taken of
1228 * possible hash collisions.
1229 * XXX: does this really need to be written recursively?
1230 *****************************************************************************/
1231 static int Lookup( variable_t *p_vars, int i_count, const char *psz_name )
1241 i_hash = HashString( psz_name );
1243 i_pos = LookupInner( p_vars, i_count, i_hash );
1245 /* Hash not found */
1246 if( i_hash != p_vars[i_pos].i_hash )
1251 /* Hash found, entry found */
1252 if( !strcmp( psz_name, p_vars[i_pos].psz_name ) )
1257 /* Hash collision! This should be very rare, but we cannot guarantee
1258 * it will never happen. Just do an exhaustive search amongst all
1259 * entries with the same hash. */
1260 for( i = i_pos - 1 ; i > 0 && i_hash == p_vars[i].i_hash ; i-- )
1262 if( !strcmp( psz_name, p_vars[i].psz_name ) )
1268 for( i = i_pos + 1 ; i < i_count && i_hash == p_vars[i].i_hash ; i++ )
1270 if( !strcmp( psz_name, p_vars[i].psz_name ) )
1276 /* Hash found, but entry not found */
1280 static int LookupInner( variable_t *p_vars, int i_count, uint32_t i_hash )
1284 if( i_hash <= p_vars[0].i_hash )
1289 if( i_hash >= p_vars[i_count-1].i_hash )
1294 i_middle = i_count / 2;
1296 /* We know that 0 < i_middle */
1297 if( i_hash < p_vars[i_middle].i_hash )
1299 return LookupInner( p_vars, i_middle, i_hash );
1302 /* We know that i_middle + 1 < i_count */
1303 if( i_hash > p_vars[i_middle].i_hash )
1305 return i_middle + LookupInner( p_vars + i_middle,
1313 /*****************************************************************************
1314 * CheckValue: check that a value is valid wrt. a variable
1315 *****************************************************************************
1316 * This function checks p_val's value against p_var's limitations such as
1317 * minimal and maximal value, step, in-list position, and modifies p_val if
1319 ****************************************************************************/
1320 static void CheckValue ( variable_t *p_var, vlc_value_t *p_val )
1322 /* Check that our variable is in the list */
1323 if( p_var->i_type & VLC_VAR_HASCHOICE && p_var->choices.i_count )
1327 /* FIXME: the list is sorted, dude. Use something cleverer. */
1328 for( i = p_var->choices.i_count ; i-- ; )
1330 if( p_var->pf_cmp( *p_val, p_var->choices.p_values[i] ) == 0 )
1336 /* If not found, change it to anything vaguely valid */
1339 /* Free the old variable, get the new one, dup it */
1340 p_var->pf_free( p_val );
1341 *p_val = p_var->choices.p_values[p_var->i_default >= 0
1342 ? p_var->i_default : 0 ];
1343 p_var->pf_dup( p_val );
1347 /* Check that our variable is within the bounds */
1348 switch( p_var->i_type & VLC_VAR_TYPE )
1350 case VLC_VAR_INTEGER:
1351 if( p_var->i_type & VLC_VAR_HASSTEP && p_var->step.i_int
1352 && (p_val->i_int % p_var->step.i_int) )
1354 p_val->i_int = (p_val->i_int + (p_var->step.i_int / 2))
1355 / p_var->step.i_int * p_var->step.i_int;
1357 if( p_var->i_type & VLC_VAR_HASMIN
1358 && p_val->i_int < p_var->min.i_int )
1360 p_val->i_int = p_var->min.i_int;
1362 if( p_var->i_type & VLC_VAR_HASMAX
1363 && p_val->i_int > p_var->max.i_int )
1365 p_val->i_int = p_var->max.i_int;
1369 if( p_var->i_type & VLC_VAR_HASSTEP && p_var->step.f_float )
1371 float f_round = p_var->step.f_float * (float)(int)( 0.5 +
1372 p_val->f_float / p_var->step.f_float );
1373 if( p_val->f_float != f_round )
1375 p_val->f_float = f_round;
1378 if( p_var->i_type & VLC_VAR_HASMIN
1379 && p_val->f_float < p_var->min.f_float )
1381 p_val->f_float = p_var->min.f_float;
1383 if( p_var->i_type & VLC_VAR_HASMAX
1384 && p_val->f_float > p_var->max.f_float )
1386 p_val->f_float = p_var->max.f_float;
1395 /*****************************************************************************
1396 * InheritValue: try to inherit the value of this variable from the same one
1397 * in our closest parent.
1398 *****************************************************************************/
1399 static int InheritValue( vlc_object_t *p_this, const char *psz_name,
1400 vlc_value_t *p_val, int i_type )
1405 /* No need to take the structure lock,
1406 * we are only looking for our parents */
1408 if( !p_this->p_parent )
1410 switch( i_type & VLC_VAR_TYPE )
1413 case VLC_VAR_DIRECTORY:
1414 case VLC_VAR_STRING:
1415 case VLC_VAR_MODULE:
1416 p_val->psz_string = config_GetPsz( p_this, psz_name );
1417 if( !p_val->psz_string ) p_val->psz_string = strdup("");
1420 p_val->f_float = config_GetFloat( p_this, psz_name );
1422 case VLC_VAR_INTEGER:
1423 case VLC_VAR_HOTKEY:
1424 p_val->i_int = config_GetInt( p_this, psz_name );
1427 p_val->b_bool = config_GetInt( p_this, psz_name );
1431 char *psz_orig, *psz_var;
1432 vlc_list_t *p_list = malloc(sizeof(vlc_list_t));
1433 p_val->p_list = p_list;
1434 p_list->i_count = 0;
1436 psz_var = psz_orig = config_GetPsz( p_this, psz_name );
1437 while( psz_var && *psz_var )
1439 char *psz_item = psz_var;
1441 while( *psz_var && *psz_var != ',' ) psz_var++;
1442 if( *psz_var == ',' )
1447 val.i_int = strtol( psz_item, NULL, 0 );
1448 INSERT_ELEM( p_list->p_values, p_list->i_count,
1449 p_list->i_count, val );
1450 /* p_list->i_count is incremented twice by INSERT_ELEM */
1452 INSERT_ELEM( p_list->pi_types, p_list->i_count,
1453 p_list->i_count, VLC_VAR_INTEGER );
1455 if( psz_orig ) free( psz_orig );
1466 /* Look for the variable */
1467 vlc_mutex_lock( &p_this->p_parent->var_lock );
1469 i_var = Lookup( p_this->p_parent->p_vars, p_this->p_parent->i_vars,
1475 p_var = &p_this->p_parent->p_vars[i_var];
1477 /* Really get the variable */
1478 *p_val = p_var->val;
1480 /* Duplicate value if needed */
1481 p_var->pf_dup( p_val );
1483 vlc_mutex_unlock( &p_this->p_parent->var_lock );
1487 vlc_mutex_unlock( &p_this->p_parent->var_lock );
1489 /* We're still not there */
1491 return InheritValue( p_this->p_parent, psz_name, p_val, i_type );
1494 /**********************************************************************
1495 * Execute a var command on an object identified by its name
1496 **********************************************************************/
1497 int __var_Command( vlc_object_t *p_this, const char *psz_name,
1498 const char *psz_cmd, const char *psz_arg, char **psz_msg )
1500 vlc_object_t *p_obj = vlc_object_find_name( p_this->p_libvlc,
1501 psz_name, FIND_CHILD );
1507 *psz_msg = strdup( "Unknown destination object." );
1511 i_type = var_Type( p_obj, psz_cmd );
1512 if( !( i_type&VLC_VAR_ISCOMMAND ) )
1514 vlc_object_release( p_obj );
1516 *psz_msg = strdup( "Variable doesn't exist or isn't a command." );
1517 return VLC_EGENERIC;
1523 case VLC_VAR_INTEGER:
1524 i_ret = var_SetInteger( p_obj, psz_cmd, atoi( psz_arg ) );
1527 i_ret = var_SetFloat( p_obj, psz_cmd, atof( psz_arg ) );
1529 case VLC_VAR_STRING:
1530 i_ret = var_SetString( p_obj, psz_cmd, psz_arg );
1533 i_ret = var_SetBool( p_obj, psz_cmd, atoi( psz_arg ) );
1536 i_ret = VLC_EGENERIC;
1540 vlc_object_release( p_obj );
1544 *psz_msg = (char*)malloc( 80 );
1545 sprintf( *psz_msg, "%s on object %s returned %i (%s)",
1546 psz_cmd, psz_name, i_ret, vlc_error( i_ret ) );