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"
32 /*****************************************************************************
34 *****************************************************************************/
35 struct callback_entry_t
37 vlc_callback_t pf_callback;
41 /*****************************************************************************
42 * Local comparison functions, returns 0 if v == w, < 0 if v < w, > 0 if v > w
43 *****************************************************************************/
44 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; }
45 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; }
46 static int CmpTime( vlc_value_t v, vlc_value_t w )
48 return v.i_time == w.i_time ? 0 : v.i_time > w.i_time ? 1 : -1;
50 static int CmpString( vlc_value_t v, vlc_value_t w ) { return strcmp( v.psz_string, w.psz_string ); }
51 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; }
52 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; }
54 /*****************************************************************************
55 * Local duplication functions, and local deallocation functions
56 *****************************************************************************/
57 static void DupDummy( vlc_value_t *p_val ) { (void)p_val; /* unused */ }
58 static void DupString( vlc_value_t *p_val ) { p_val->psz_string = strdup( p_val->psz_string ); }
60 static void DupList( vlc_value_t *p_val )
63 vlc_list_t *p_list = malloc( sizeof(vlc_list_t) );
65 p_list->i_count = p_val->p_list->i_count;
66 if( p_val->p_list->i_count )
68 p_list->p_values = malloc( p_list->i_count * sizeof(vlc_value_t) );
69 p_list->pi_types = malloc( p_list->i_count * sizeof(int) );
73 p_list->p_values = NULL;
74 p_list->pi_types = NULL;
77 for( i = 0; i < p_list->i_count; i++ )
79 p_list->p_values[i] = p_val->p_list->p_values[i];
80 p_list->pi_types[i] = p_val->p_list->pi_types[i];
81 switch( p_val->p_list->pi_types[i] & VLC_VAR_TYPE )
85 DupString( &p_list->p_values[i] );
92 p_val->p_list = p_list;
95 static void FreeDummy( vlc_value_t *p_val ) { (void)p_val; /* unused */ }
96 static void FreeString( vlc_value_t *p_val ) { free( p_val->psz_string ); }
97 static void FreeMutex( vlc_value_t *p_val ) { vlc_mutex_destroy( (vlc_mutex_t*)p_val->p_address ); free( p_val->p_address ); }
99 static void FreeList( vlc_value_t *p_val )
102 for( i = 0; i < p_val->p_list->i_count; i++ )
104 switch( p_val->p_list->pi_types[i] & VLC_VAR_TYPE )
107 FreeString( &p_val->p_list->p_values[i] );
110 FreeMutex( &p_val->p_list->p_values[i] );
117 if( p_val->p_list->i_count )
119 free( p_val->p_list->p_values );
120 free( p_val->p_list->pi_types );
122 free( p_val->p_list );
125 /*****************************************************************************
127 *****************************************************************************/
128 static int GetUnused ( vlc_object_t *, const char * );
129 static uint32_t HashString ( const char * );
130 static int Insert ( variable_t *, int, const char * );
131 static int InsertInner ( variable_t *, int, uint32_t );
132 static int Lookup ( variable_t *, int, const char * );
133 static int LookupInner ( variable_t *, int, uint32_t );
135 static void CheckValue ( variable_t *, vlc_value_t * );
137 static int InheritValue( vlc_object_t *, const char *, vlc_value_t *,
141 * Initialize a vlc variable
143 * We hash the given string and insert it into the sorted list. The insertion
144 * may require slow memory copies, but think about what we gain in the log(n)
145 * lookup phase when setting/getting the variable value!
147 * \param p_this The object in which to create the variable
148 * \param psz_name The name of the variable
149 * \param i_type The variables type. Must be one of \ref var_type combined with
150 * zero or more \ref var_flags
152 int __var_Create( vlc_object_t *p_this, const char *psz_name, int i_type )
156 static vlc_list_t dummy_null_list = {0, NULL, NULL};
157 vlc_object_internals_t *p_priv = p_this->p_internals;
159 vlc_mutex_lock( &p_priv->var_lock );
161 /* FIXME: if the variable already exists, we don't duplicate it. But we
162 * duplicate the lookups. It's not that serious, but if anyone finds some
163 * time to rework Insert() so that only one lookup has to be done, feel
165 i_new = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
169 /* If the types differ, variable creation failed. */
170 if( (i_type & ~(VLC_VAR_DOINHERIT|VLC_VAR_ISCOMMAND)) != p_priv->p_vars[i_new].i_type )
172 vlc_mutex_unlock( &p_priv->var_lock );
176 p_priv->p_vars[i_new].i_usage++;
177 if( i_type & VLC_VAR_ISCOMMAND )
178 p_priv->p_vars[i_new].i_type |= VLC_VAR_ISCOMMAND;
179 vlc_mutex_unlock( &p_priv->var_lock );
183 i_new = Insert( p_priv->p_vars, p_priv->i_vars, psz_name );
185 if( (p_priv->i_vars & 15) == 15 )
187 p_priv->p_vars = realloc( p_priv->p_vars,
188 (p_priv->i_vars+17) * sizeof(variable_t) );
191 memmove( p_priv->p_vars + i_new + 1,
192 p_priv->p_vars + i_new,
193 (p_priv->i_vars - i_new) * sizeof(variable_t) );
197 p_var = &p_priv->p_vars[i_new];
198 memset( p_var, 0, sizeof(*p_var) );
200 p_var->i_hash = HashString( psz_name );
201 p_var->psz_name = strdup( psz_name );
202 p_var->psz_text = NULL;
204 p_var->i_type = i_type & ~VLC_VAR_DOINHERIT;
205 memset( &p_var->val, 0, sizeof(vlc_value_t) );
207 p_var->pf_dup = DupDummy;
208 p_var->pf_free = FreeDummy;
212 p_var->i_default = -1;
213 p_var->choices.i_count = 0;
214 p_var->choices.p_values = NULL;
215 p_var->choices_text.i_count = 0;
216 p_var->choices_text.p_values = NULL;
218 p_var->b_incallback = VLC_FALSE;
219 p_var->i_entries = 0;
220 p_var->p_entries = NULL;
222 /* Always initialize the variable, even if it is a list variable; this
223 * will lead to errors if the variable is not initialized, but it will
224 * not cause crashes in the variable handling. */
225 switch( i_type & VLC_VAR_TYPE )
228 p_var->pf_cmp = CmpBool;
229 p_var->val.b_bool = VLC_FALSE;
231 case VLC_VAR_INTEGER:
233 p_var->pf_cmp = CmpInt;
234 p_var->val.i_int = 0;
239 case VLC_VAR_DIRECTORY:
240 case VLC_VAR_VARIABLE:
241 p_var->pf_cmp = CmpString;
242 p_var->pf_dup = DupString;
243 p_var->pf_free = FreeString;
244 p_var->val.psz_string = "";
247 p_var->pf_cmp = CmpFloat;
248 p_var->val.f_float = 0.0;
251 p_var->pf_cmp = CmpTime;
252 p_var->val.i_time = 0;
254 case VLC_VAR_ADDRESS:
255 p_var->pf_cmp = CmpAddress;
256 p_var->val.p_address = NULL;
259 p_var->pf_cmp = CmpAddress;
260 p_var->pf_free = FreeMutex;
261 p_var->val.p_address = malloc( sizeof(vlc_mutex_t) );
262 vlc_mutex_init( p_this, (vlc_mutex_t*)p_var->val.p_address );
265 p_var->pf_cmp = CmpAddress;
266 p_var->pf_dup = DupList;
267 p_var->pf_free = FreeList;
268 p_var->val.p_list = &dummy_null_list;
272 /* Duplicate the default data we stored. */
273 p_var->pf_dup( &p_var->val );
275 if( i_type & VLC_VAR_DOINHERIT )
279 if( InheritValue( p_this, psz_name, &val, p_var->i_type )
282 /* Free data if needed */
283 p_var->pf_free( &p_var->val );
284 /* Set the variable */
287 if( i_type & VLC_VAR_HASCHOICE )
289 /* We must add the inherited value to our choice list */
290 p_var->i_default = 0;
292 INSERT_ELEM( p_var->choices.p_values, p_var->choices.i_count,
294 INSERT_ELEM( p_var->choices_text.p_values,
295 p_var->choices_text.i_count, 0, val );
296 p_var->pf_dup( &p_var->choices.p_values[0] );
297 p_var->choices_text.p_values[0].psz_string = NULL;
302 vlc_mutex_unlock( &p_priv->var_lock );
308 * Destroy a vlc variable
310 * Look for the variable and destroy it if it is found. As in var_Create we
311 * do a call to memmove() but we have performance counterparts elsewhere.
313 * \param p_this The object that holds the variable
314 * \param psz_name The name of the variable
316 int __var_Destroy( vlc_object_t *p_this, const char *psz_name )
320 vlc_object_internals_t *p_priv = p_this->p_internals;
322 vlc_mutex_lock( &p_priv->var_lock );
324 i_var = GetUnused( p_this, psz_name );
327 vlc_mutex_unlock( &p_priv->var_lock );
331 p_var = &p_priv->p_vars[i_var];
333 if( p_var->i_usage > 1 )
336 vlc_mutex_unlock( &p_priv->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_priv->p_vars + i_var,
366 p_priv->p_vars + i_var + 1,
367 (p_priv->i_vars - i_var - 1) * sizeof(variable_t) );
369 if( (p_priv->i_vars & 15) == 0 )
371 p_priv->p_vars = realloc( p_priv->p_vars,
372 (p_priv->i_vars) * sizeof( variable_t ) );
377 vlc_mutex_unlock( &p_priv->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 )
397 vlc_object_internals_t *p_priv = p_this->p_internals;
399 vlc_mutex_lock( &p_priv->var_lock );
401 i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
405 vlc_mutex_unlock( &p_priv->var_lock );
409 p_var = &p_priv->p_vars[i_var];
414 if( p_var->i_type & VLC_VAR_HASMIN )
416 p_var->pf_free( &p_var->min );
418 p_var->i_type |= VLC_VAR_HASMIN;
420 p_var->pf_dup( &p_var->min );
421 CheckValue( p_var, &p_var->val );
424 if( p_var->i_type & VLC_VAR_HASMAX )
426 p_var->pf_free( &p_var->max );
428 p_var->i_type |= VLC_VAR_HASMAX;
430 p_var->pf_dup( &p_var->max );
431 CheckValue( p_var, &p_var->val );
433 case VLC_VAR_SETSTEP:
434 if( p_var->i_type & VLC_VAR_HASSTEP )
436 p_var->pf_free( &p_var->step );
438 p_var->i_type |= VLC_VAR_HASSTEP;
439 p_var->step = *p_val;
440 p_var->pf_dup( &p_var->step );
441 CheckValue( p_var, &p_var->val );
443 case VLC_VAR_ADDCHOICE:
444 /* FIXME: the list is sorted, dude. Use something cleverer. */
445 for( i = p_var->choices.i_count ; i-- ; )
447 if( p_var->pf_cmp( p_var->choices.p_values[i], *p_val ) < 0 )
453 /* The new place is i+1 */
456 if( p_var->i_default >= i )
461 INSERT_ELEM( p_var->choices.p_values, p_var->choices.i_count,
463 INSERT_ELEM( p_var->choices_text.p_values,
464 p_var->choices_text.i_count, i, *p_val );
465 p_var->pf_dup( &p_var->choices.p_values[i] );
466 p_var->choices_text.p_values[i].psz_string =
467 ( p_val2 && p_val2->psz_string ) ?
468 strdup( p_val2->psz_string ) : NULL;
470 CheckValue( p_var, &p_var->val );
472 case VLC_VAR_DELCHOICE:
473 /* FIXME: the list is sorted, dude. Use something cleverer. */
474 for( i = 0 ; i < p_var->choices.i_count ; i++ )
476 if( p_var->pf_cmp( p_var->choices.p_values[i], *p_val ) == 0 )
482 if( i == p_var->choices.i_count )
485 vlc_mutex_unlock( &p_priv->var_lock );
489 if( p_var->i_default > i )
493 else if( p_var->i_default == i )
495 p_var->i_default = -1;
498 p_var->pf_free( &p_var->choices.p_values[i] );
499 if( p_var->choices_text.p_values[i].psz_string )
500 free( p_var->choices_text.p_values[i].psz_string );
501 REMOVE_ELEM( p_var->choices.p_values, p_var->choices.i_count, i );
502 REMOVE_ELEM( p_var->choices_text.p_values,
503 p_var->choices_text.i_count, i );
505 CheckValue( p_var, &p_var->val );
507 case VLC_VAR_CHOICESCOUNT:
508 p_val->i_int = p_var->choices.i_count;
510 case VLC_VAR_CLEARCHOICES:
511 for( i = 0 ; i < p_var->choices.i_count ; i++ )
513 p_var->pf_free( &p_var->choices.p_values[i] );
515 for( i = 0 ; i < p_var->choices_text.i_count ; i++ )
517 if( p_var->choices_text.p_values[i].psz_string )
518 free( p_var->choices_text.p_values[i].psz_string );
520 if( p_var->choices.i_count ) free( p_var->choices.p_values );
521 if( p_var->choices_text.i_count ) free( p_var->choices_text.p_values );
523 p_var->choices.i_count = 0;
524 p_var->choices.p_values = NULL;
525 p_var->choices_text.i_count = 0;
526 p_var->choices_text.p_values = NULL;
527 p_var->i_default = -1;
529 case VLC_VAR_SETDEFAULT:
530 /* FIXME: the list is sorted, dude. Use something cleverer. */
531 for( i = 0 ; i < p_var->choices.i_count ; i++ )
533 if( p_var->pf_cmp( p_var->choices.p_values[i], *p_val ) == 0 )
539 if( i == p_var->choices.i_count )
545 p_var->i_default = i;
546 CheckValue( p_var, &p_var->val );
548 case VLC_VAR_SETVALUE:
549 /* Duplicate data if needed */
550 p_var->pf_dup( p_val );
551 /* Backup needed stuff */
553 /* Check boundaries and list */
554 CheckValue( p_var, p_val );
555 /* Set the variable */
557 /* Free data if needed */
558 p_var->pf_free( &oldval );
560 case VLC_VAR_GETCHOICES:
561 case VLC_VAR_GETLIST:
562 p_val->p_list = malloc( sizeof(vlc_list_t) );
563 if( p_val2 ) p_val2->p_list = malloc( sizeof(vlc_list_t) );
564 if( p_var->choices.i_count )
566 p_val->p_list->p_values = malloc( p_var->choices.i_count
567 * sizeof(vlc_value_t) );
568 p_val->p_list->pi_types = malloc( p_var->choices.i_count
572 p_val2->p_list->p_values =
573 malloc( p_var->choices.i_count * sizeof(vlc_value_t) );
574 p_val2->p_list->pi_types =
575 malloc( p_var->choices.i_count * sizeof(int) );
578 p_val->p_list->i_count = p_var->choices.i_count;
579 if( p_val2 ) p_val2->p_list->i_count = p_var->choices.i_count;
580 for( i = 0 ; i < p_var->choices.i_count ; i++ )
582 p_val->p_list->p_values[i] = p_var->choices.p_values[i];
583 p_val->p_list->pi_types[i] = p_var->i_type;
584 p_var->pf_dup( &p_val->p_list->p_values[i] );
587 p_val2->p_list->p_values[i].psz_string =
588 p_var->choices_text.p_values[i].psz_string ?
589 strdup(p_var->choices_text.p_values[i].psz_string) : NULL;
590 p_val2->p_list->pi_types[i] = VLC_VAR_STRING;
594 case VLC_VAR_FREELIST:
596 if( p_val2 && p_val2->p_list )
598 for( i = 0; i < p_val2->p_list->i_count; i++ )
599 if( p_val2->p_list->p_values[i].psz_string )
600 free( p_val2->p_list->p_values[i].psz_string );
601 if( p_val2->p_list->i_count )
603 free( p_val2->p_list->p_values );
604 free( p_val2->p_list->pi_types );
606 free( p_val2->p_list );
609 case VLC_VAR_SETTEXT:
610 if( p_var->psz_text ) free( p_var->psz_text );
611 if( p_val && p_val->psz_string )
612 p_var->psz_text = strdup( p_val->psz_string );
614 case VLC_VAR_GETTEXT:
615 p_val->psz_string = NULL;
616 if( p_var->psz_text )
618 p_val->psz_string = strdup( p_var->psz_text );
621 case VLC_VAR_INHERITVALUE:
625 if( InheritValue( p_this, psz_name, &val, p_var->i_type )
628 /* Duplicate already done */
630 /* Backup needed stuff */
632 /* Check boundaries and list */
633 CheckValue( p_var, &val );
634 /* Set the variable */
636 /* Free data if needed */
637 p_var->pf_free( &oldval );
643 p_var->pf_dup( p_val );
647 case VLC_VAR_TRIGGER_CALLBACKS:
649 /* Deal with callbacks. Tell we're in a callback, release the lock,
650 * call stored functions, retake the lock. */
651 if( p_var->i_entries )
654 int i_entries = p_var->i_entries;
655 callback_entry_t *p_entries = p_var->p_entries;
657 p_var->b_incallback = VLC_TRUE;
658 vlc_mutex_unlock( &p_priv->var_lock );
661 for( ; i_entries-- ; )
663 p_entries[i_entries].pf_callback( p_this, psz_name, p_var->val, p_var->val,
664 p_entries[i_entries].p_data );
667 vlc_mutex_lock( &p_priv->var_lock );
669 i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
672 msg_Err( p_this, "variable %s has disappeared", psz_name );
673 vlc_mutex_unlock( &p_priv->var_lock );
677 p_var = &p_priv->p_vars[i_var];
678 p_var->b_incallback = VLC_FALSE;
687 vlc_mutex_unlock( &p_priv->var_lock );
693 * Request a variable's type
695 * \return The variable type if it exists, or 0 if the
696 * variable could not be found.
699 int __var_Type( vlc_object_t *p_this, const char *psz_name )
702 vlc_object_internals_t *p_priv = p_this->p_internals;
704 vlc_mutex_lock( &p_priv->var_lock );
706 i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
710 vlc_mutex_unlock( &p_priv->var_lock );
714 i_type = p_priv->p_vars[i_var].i_type;
716 vlc_mutex_unlock( &p_priv->var_lock );
722 * Set a variable's value
724 * \param p_this The object that hold the variable
725 * \param psz_name The name of the variable
726 * \param val the value to set
728 int __var_Set( vlc_object_t *p_this, const char *psz_name, vlc_value_t val )
733 vlc_object_internals_t *p_priv = p_this->p_internals;
735 vlc_mutex_lock( &p_priv->var_lock );
737 i_var = GetUnused( p_this, psz_name );
740 vlc_mutex_unlock( &p_priv->var_lock );
744 p_var = &p_priv->p_vars[i_var];
746 /* Duplicate data if needed */
747 p_var->pf_dup( &val );
749 /* Backup needed stuff */
752 /* Check boundaries and list */
753 CheckValue( p_var, &val );
755 /* Set the variable */
758 /* Deal with callbacks. Tell we're in a callback, release the lock,
759 * call stored functions, retake the lock. */
760 if( p_var->i_entries )
763 int i_entries = p_var->i_entries;
764 callback_entry_t *p_entries = p_var->p_entries;
766 p_var->b_incallback = VLC_TRUE;
767 vlc_mutex_unlock( &p_priv->var_lock );
770 for( ; i_entries-- ; )
772 p_entries[i_entries].pf_callback( p_this, psz_name, oldval, val,
773 p_entries[i_entries].p_data );
776 vlc_mutex_lock( &p_priv->var_lock );
778 i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
781 msg_Err( p_this, "variable %s has disappeared", psz_name );
782 vlc_mutex_unlock( &p_priv->var_lock );
786 p_var = &p_priv->p_vars[i_var];
787 p_var->b_incallback = VLC_FALSE;
790 /* Free data if needed */
791 p_var->pf_free( &oldval );
793 vlc_mutex_unlock( &p_priv->var_lock );
799 * Get a variable's value
801 * \param p_this The object that holds the variable
802 * \param psz_name The name of the variable
803 * \param p_val Pointer to a vlc_value_t that will hold the variable's value
804 * after the function is finished
806 int __var_Get( vlc_object_t *p_this, const char *psz_name, vlc_value_t *p_val )
810 vlc_object_internals_t *p_priv = p_this->p_internals;
812 vlc_mutex_lock( &p_priv->var_lock );
814 i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
818 vlc_mutex_unlock( &p_priv->var_lock );
822 p_var = &p_priv->p_vars[i_var];
824 /* Really get the variable */
827 /* Duplicate value if needed */
828 p_var->pf_dup( p_val );
830 vlc_mutex_unlock( &p_priv->var_lock );
837 * Finds a process-wide mutex, creates it if needed, and locks it.
838 * Unlock with vlc_mutex_unlock().
840 vlc_mutex_t *var_AcquireMutex( const char *name )
842 libvlc_global_data_t *p_global = vlc_global();
845 if( var_Create( p_global, name, VLC_VAR_MUTEX ) )
848 var_Get( p_global, name, &val );
849 vlc_mutex_lock( &val.p_address );
850 return val.p_address;
855 * Register a callback in a variable
857 * We store a function pointer that will be called upon variable
860 * \param p_this The object that holds the variable
861 * \param psz_name The name of the variable
862 * \param pf_callback The function pointer
863 * \param p_data A generic pointer that will be passed as the last
864 * argument to the callback function.
866 * \warning The callback function is run in the thread that calls var_Set on
867 * the variable. Use proper locking. This thread may not have much
868 * time to spare, so keep callback functions short.
870 int __var_AddCallback( vlc_object_t *p_this, const char *psz_name,
871 vlc_callback_t pf_callback, void *p_data )
875 callback_entry_t entry;
876 vlc_object_internals_t *p_priv = p_this->p_internals;
878 entry.pf_callback = pf_callback;
879 entry.p_data = p_data;
881 vlc_mutex_lock( &p_priv->var_lock );
883 i_var = GetUnused( p_this, psz_name );
886 vlc_mutex_unlock( &p_priv->var_lock );
890 p_var = &p_priv->p_vars[i_var];
892 INSERT_ELEM( p_var->p_entries,
897 vlc_mutex_unlock( &p_priv->var_lock );
903 * Remove a callback from a variable
905 * pf_callback and p_data have to be given again, because different objects
906 * might have registered the same callback function.
908 int __var_DelCallback( vlc_object_t *p_this, const char *psz_name,
909 vlc_callback_t pf_callback, void *p_data )
913 vlc_object_internals_t *p_priv = p_this->p_internals;
915 vlc_mutex_lock( &p_priv->var_lock );
917 i_var = GetUnused( p_this, psz_name );
920 vlc_mutex_unlock( &p_priv->var_lock );
924 p_var = &p_priv->p_vars[i_var];
926 for( i_entry = p_var->i_entries ; i_entry-- ; )
928 if( p_var->p_entries[i_entry].pf_callback == pf_callback
929 && p_var->p_entries[i_entry].p_data == p_data )
937 vlc_mutex_unlock( &p_priv->var_lock );
941 REMOVE_ELEM( p_var->p_entries, p_var->i_entries, i_entry );
943 vlc_mutex_unlock( &p_priv->var_lock );
949 * Trigger callback on a variable
951 * \param p_this The object that hold the variable
952 * \param psz_name The name of the variable
954 int __var_TriggerCallback( vlc_object_t *p_this, const char *psz_name )
959 vlc_object_internals_t *p_priv = p_this->p_internals;
961 vlc_mutex_lock( &p_priv->var_lock );
963 i_var = GetUnused( p_this, psz_name );
966 vlc_mutex_unlock( &p_priv->var_lock );
970 p_var = &p_priv->p_vars[i_var];
972 /* Backup needed stuff */
975 /* Deal with callbacks. Tell we're in a callback, release the lock,
976 * call stored functions, retake the lock. */
977 if( p_var->i_entries )
980 int i_entries = p_var->i_entries;
981 callback_entry_t *p_entries = p_var->p_entries;
983 p_var->b_incallback = VLC_TRUE;
984 vlc_mutex_unlock( &p_priv->var_lock );
987 for( ; i_entries-- ; )
989 p_entries[i_entries].pf_callback( p_this, psz_name, oldval, oldval,
990 p_entries[i_entries].p_data );
993 vlc_mutex_lock( &p_priv->var_lock );
995 i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
998 msg_Err( p_this, "variable %s has disappeared", psz_name );
999 vlc_mutex_unlock( &p_priv->var_lock );
1003 p_var = &p_priv->p_vars[i_var];
1004 p_var->b_incallback = VLC_FALSE;
1007 vlc_mutex_unlock( &p_priv->var_lock );
1011 /** Parse a stringified option
1012 * This function parse a string option and create the associated object
1014 * The option must be of the form "[no[-]]foo[=bar]" where foo is the
1015 * option name and bar is the value of the option.
1016 * \param p_obj the object in which the variable must be created
1017 * \param psz_option the option to parse
1020 void __var_OptionParse( vlc_object_t *p_obj, const char *psz_option )
1022 char *psz_name, *psz_value = strchr( psz_option, '=' );
1023 int i_name_len, i_type;
1024 vlc_bool_t b_isno = VLC_FALSE;
1027 if( psz_value ) i_name_len = psz_value - psz_option;
1028 else i_name_len = strlen( psz_option );
1030 /* It's too much of a hassle to remove the ':' when we parse
1031 * the cmd line :) */
1032 if( i_name_len && *psz_option == ':' )
1038 if( i_name_len == 0 ) return;
1040 psz_name = strndup( psz_option, i_name_len );
1041 if( psz_value ) psz_value++;
1043 /* FIXME: :programs should be handled generically */
1044 if( !strcmp( psz_name, "programs" ) )
1045 i_type = VLC_VAR_LIST;
1047 i_type = config_GetType( p_obj, psz_name );
1049 if( !i_type && !psz_value )
1051 /* check for "no-foo" or "nofoo" */
1052 if( !strncmp( psz_name, "no-", 3 ) )
1054 memmove( psz_name, psz_name + 3, strlen(psz_name) + 1 - 3 );
1056 else if( !strncmp( psz_name, "no", 2 ) )
1058 memmove( psz_name, psz_name + 2, strlen(psz_name) + 1 - 2 );
1060 else goto cleanup; /* Option doesn't exist */
1063 i_type = config_GetType( p_obj, psz_name );
1065 if( !i_type ) goto cleanup; /* Option doesn't exist */
1067 else if( !i_type ) goto cleanup; /* Option doesn't exist */
1069 if( ( i_type != VLC_VAR_BOOL ) &&
1070 ( !psz_value || !*psz_value ) ) goto cleanup; /* Invalid value */
1072 /* Create the variable in the input object.
1073 * Children of the input object will be able to retreive this value
1074 * thanks to the inheritance property of the object variables. */
1075 var_Create( p_obj, psz_name, i_type );
1080 val.b_bool = !b_isno;
1083 case VLC_VAR_INTEGER:
1084 val.i_int = strtol( psz_value, NULL, 0 );
1088 val.f_float = atof( psz_value );
1091 case VLC_VAR_STRING:
1092 case VLC_VAR_MODULE:
1094 case VLC_VAR_DIRECTORY:
1095 val.psz_string = psz_value;
1100 char *psz_orig, *psz_var;
1101 vlc_list_t *p_list = malloc(sizeof(vlc_list_t));
1102 val.p_list = p_list;
1103 p_list->i_count = 0;
1105 psz_var = psz_orig = strdup(psz_value);
1106 while( psz_var && *psz_var )
1108 char *psz_item = psz_var;
1110 while( *psz_var && *psz_var != ',' ) psz_var++;
1111 if( *psz_var == ',' )
1116 val2.i_int = strtol( psz_item, NULL, 0 );
1117 INSERT_ELEM( p_list->p_values, p_list->i_count,
1118 p_list->i_count, val2 );
1119 /* p_list->i_count is incremented twice by INSERT_ELEM */
1121 INSERT_ELEM( p_list->pi_types, p_list->i_count,
1122 p_list->i_count, VLC_VAR_INTEGER );
1124 if( psz_orig ) free( psz_orig );
1133 var_Set( p_obj, psz_name, val );
1136 if( psz_name ) free( psz_name );
1141 /* Following functions are local */
1143 /*****************************************************************************
1144 * GetUnused: find an unused variable from its name
1145 *****************************************************************************
1146 * We do i_tries tries before giving up, just in case the variable is being
1147 * modified and called from a callback.
1148 *****************************************************************************/
1149 static int GetUnused( vlc_object_t *p_this, const char *psz_name )
1151 int i_var, i_tries = 0;
1152 vlc_object_internals_t *p_priv = p_this->p_internals;
1156 i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
1162 if( ! p_priv->p_vars[i_var].b_incallback )
1167 if( i_tries++ > 100 )
1169 msg_Err( p_this, "caught in a callback deadlock?" );
1170 return VLC_ETIMEOUT;
1173 vlc_mutex_unlock( &p_priv->var_lock );
1174 msleep( THREAD_SLEEP );
1175 vlc_mutex_lock( &p_priv->var_lock );
1179 /*****************************************************************************
1180 * HashString: our cool hash function
1181 *****************************************************************************
1182 * This function is not intended to be crypto-secure, we only want it to be
1183 * fast and not suck too much. This one is pretty fast and did 0 collisions
1184 * in wenglish's dictionary.
1185 *****************************************************************************/
1186 static uint32_t HashString( const char *psz_string )
1188 uint32_t i_hash = 0;
1190 while( *psz_string )
1192 i_hash += *psz_string++;
1193 i_hash += i_hash << 10;
1194 i_hash ^= i_hash >> 8;
1200 /*****************************************************************************
1201 * Insert: find an empty slot to insert a new variable
1202 *****************************************************************************
1203 * We use a recursive inner function indexed on the hash. This function does
1204 * nothing in the rare cases where a collision may occur, see Lookup()
1205 * to see how we handle them.
1206 * XXX: does this really need to be written recursively?
1207 *****************************************************************************/
1208 static int Insert( variable_t *p_vars, int i_count, const char *psz_name )
1215 return InsertInner( p_vars, i_count, HashString( psz_name ) );
1218 static int InsertInner( variable_t *p_vars, int i_count, uint32_t i_hash )
1222 if( i_hash <= p_vars[0].i_hash )
1227 if( i_hash >= p_vars[i_count - 1].i_hash )
1232 i_middle = i_count / 2;
1234 /* We know that 0 < i_middle */
1235 if( i_hash < p_vars[i_middle].i_hash )
1237 return InsertInner( p_vars, i_middle, i_hash );
1240 /* We know that i_middle + 1 < i_count */
1241 if( i_hash > p_vars[i_middle + 1].i_hash )
1243 return i_middle + 1 + InsertInner( p_vars + i_middle + 1,
1244 i_count - i_middle - 1,
1248 return i_middle + 1;
1251 /*****************************************************************************
1252 * Lookup: find an existing variable given its name
1253 *****************************************************************************
1254 * We use a recursive inner function indexed on the hash. Care is taken of
1255 * possible hash collisions.
1256 * XXX: does this really need to be written recursively?
1257 *****************************************************************************/
1258 static int Lookup( variable_t *p_vars, int i_count, const char *psz_name )
1268 i_hash = HashString( psz_name );
1270 i_pos = LookupInner( p_vars, i_count, i_hash );
1272 /* Hash not found */
1273 if( i_hash != p_vars[i_pos].i_hash )
1278 /* Hash found, entry found */
1279 if( !strcmp( psz_name, p_vars[i_pos].psz_name ) )
1284 /* Hash collision! This should be very rare, but we cannot guarantee
1285 * it will never happen. Just do an exhaustive search amongst all
1286 * entries with the same hash. */
1287 for( i = i_pos - 1 ; i > 0 && i_hash == p_vars[i].i_hash ; i-- )
1289 if( !strcmp( psz_name, p_vars[i].psz_name ) )
1295 for( i = i_pos + 1 ; i < i_count && i_hash == p_vars[i].i_hash ; i++ )
1297 if( !strcmp( psz_name, p_vars[i].psz_name ) )
1303 /* Hash found, but entry not found */
1307 static int LookupInner( variable_t *p_vars, int i_count, uint32_t i_hash )
1311 if( i_hash <= p_vars[0].i_hash )
1316 if( i_hash >= p_vars[i_count-1].i_hash )
1321 i_middle = i_count / 2;
1323 /* We know that 0 < i_middle */
1324 if( i_hash < p_vars[i_middle].i_hash )
1326 return LookupInner( p_vars, i_middle, i_hash );
1329 /* We know that i_middle + 1 < i_count */
1330 if( i_hash > p_vars[i_middle].i_hash )
1332 return i_middle + LookupInner( p_vars + i_middle,
1340 /*****************************************************************************
1341 * CheckValue: check that a value is valid wrt. a variable
1342 *****************************************************************************
1343 * This function checks p_val's value against p_var's limitations such as
1344 * minimal and maximal value, step, in-list position, and modifies p_val if
1346 ****************************************************************************/
1347 static void CheckValue ( variable_t *p_var, vlc_value_t *p_val )
1349 /* Check that our variable is in the list */
1350 if( p_var->i_type & VLC_VAR_HASCHOICE && p_var->choices.i_count )
1354 /* FIXME: the list is sorted, dude. Use something cleverer. */
1355 for( i = p_var->choices.i_count ; i-- ; )
1357 if( p_var->pf_cmp( *p_val, p_var->choices.p_values[i] ) == 0 )
1363 /* If not found, change it to anything vaguely valid */
1366 /* Free the old variable, get the new one, dup it */
1367 p_var->pf_free( p_val );
1368 *p_val = p_var->choices.p_values[p_var->i_default >= 0
1369 ? p_var->i_default : 0 ];
1370 p_var->pf_dup( p_val );
1374 /* Check that our variable is within the bounds */
1375 switch( p_var->i_type & VLC_VAR_TYPE )
1377 case VLC_VAR_INTEGER:
1378 if( p_var->i_type & VLC_VAR_HASSTEP && p_var->step.i_int
1379 && (p_val->i_int % p_var->step.i_int) )
1381 p_val->i_int = (p_val->i_int + (p_var->step.i_int / 2))
1382 / p_var->step.i_int * p_var->step.i_int;
1384 if( p_var->i_type & VLC_VAR_HASMIN
1385 && p_val->i_int < p_var->min.i_int )
1387 p_val->i_int = p_var->min.i_int;
1389 if( p_var->i_type & VLC_VAR_HASMAX
1390 && p_val->i_int > p_var->max.i_int )
1392 p_val->i_int = p_var->max.i_int;
1396 if( p_var->i_type & VLC_VAR_HASSTEP && p_var->step.f_float )
1398 float f_round = p_var->step.f_float * (float)(int)( 0.5 +
1399 p_val->f_float / p_var->step.f_float );
1400 if( p_val->f_float != f_round )
1402 p_val->f_float = f_round;
1405 if( p_var->i_type & VLC_VAR_HASMIN
1406 && p_val->f_float < p_var->min.f_float )
1408 p_val->f_float = p_var->min.f_float;
1410 if( p_var->i_type & VLC_VAR_HASMAX
1411 && p_val->f_float > p_var->max.f_float )
1413 p_val->f_float = p_var->max.f_float;
1422 /*****************************************************************************
1423 * InheritValue: try to inherit the value of this variable from the same one
1424 * in our closest parent.
1425 *****************************************************************************/
1426 static int InheritValue( vlc_object_t *p_this, const char *psz_name,
1427 vlc_value_t *p_val, int i_type )
1432 /* No need to take the structure lock,
1433 * we are only looking for our parents */
1435 if( !p_this->p_parent )
1437 switch( i_type & VLC_VAR_TYPE )
1440 case VLC_VAR_DIRECTORY:
1441 case VLC_VAR_STRING:
1442 case VLC_VAR_MODULE:
1443 p_val->psz_string = config_GetPsz( p_this, psz_name );
1444 if( !p_val->psz_string ) p_val->psz_string = strdup("");
1447 p_val->f_float = config_GetFloat( p_this, psz_name );
1449 case VLC_VAR_INTEGER:
1450 case VLC_VAR_HOTKEY:
1451 p_val->i_int = config_GetInt( p_this, psz_name );
1454 p_val->b_bool = config_GetInt( p_this, psz_name );
1458 char *psz_orig, *psz_var;
1459 vlc_list_t *p_list = malloc(sizeof(vlc_list_t));
1460 p_val->p_list = p_list;
1461 p_list->i_count = 0;
1463 psz_var = psz_orig = config_GetPsz( p_this, psz_name );
1464 while( psz_var && *psz_var )
1466 char *psz_item = psz_var;
1468 while( *psz_var && *psz_var != ',' ) psz_var++;
1469 if( *psz_var == ',' )
1474 val.i_int = strtol( psz_item, NULL, 0 );
1475 INSERT_ELEM( p_list->p_values, p_list->i_count,
1476 p_list->i_count, val );
1477 /* p_list->i_count is incremented twice by INSERT_ELEM */
1479 INSERT_ELEM( p_list->pi_types, p_list->i_count,
1480 p_list->i_count, VLC_VAR_INTEGER );
1482 if( psz_orig ) free( psz_orig );
1493 vlc_object_internals_t *p_priv = p_this->p_parent->p_internals;
1495 /* Look for the variable */
1496 vlc_mutex_lock( &p_priv->var_lock );
1498 i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
1503 p_var = &p_priv->p_vars[i_var];
1505 /* Really get the variable */
1506 *p_val = p_var->val;
1508 /* Duplicate value if needed */
1509 p_var->pf_dup( p_val );
1511 vlc_mutex_unlock( &p_priv->var_lock );
1515 vlc_mutex_unlock( &p_priv->var_lock );
1517 /* We're still not there */
1519 return InheritValue( p_this->p_parent, psz_name, p_val, i_type );
1522 /**********************************************************************
1523 * Execute a var command on an object identified by its name
1524 **********************************************************************/
1525 int __var_Command( vlc_object_t *p_this, const char *psz_name,
1526 const char *psz_cmd, const char *psz_arg, char **psz_msg )
1528 vlc_object_t *p_obj = vlc_object_find_name( p_this->p_libvlc,
1529 psz_name, FIND_CHILD );
1535 *psz_msg = strdup( "Unknown destination object." );
1539 i_type = var_Type( p_obj, psz_cmd );
1540 if( !( i_type&VLC_VAR_ISCOMMAND ) )
1542 vlc_object_release( p_obj );
1544 *psz_msg = strdup( "Variable doesn't exist or isn't a command." );
1545 return VLC_EGENERIC;
1551 case VLC_VAR_INTEGER:
1552 i_ret = var_SetInteger( p_obj, psz_cmd, atoi( psz_arg ) );
1555 i_ret = var_SetFloat( p_obj, psz_cmd, atof( psz_arg ) );
1557 case VLC_VAR_STRING:
1558 i_ret = var_SetString( p_obj, psz_cmd, psz_arg );
1561 i_ret = var_SetBool( p_obj, psz_cmd, atoi( psz_arg ) );
1564 i_ret = VLC_EGENERIC;
1568 vlc_object_release( p_obj );
1572 *psz_msg = (char*)malloc( 80 );
1573 sprintf( *psz_msg, "%s on object %s returned %i (%s)",
1574 psz_cmd, psz_name, i_ret, vlc_error( i_ret ) );