+/**
+ * Perform an action on a variable
+ *
+ * \param p_this The object that holds the variable
+ * \param psz_name The name of the variable
+ * \param i_action The action to perform. Must be one of \ref var_action
+ * \param p_val First action parameter
+ * \param p_val2 Second action parameter
+ */
+int __var_Change( vlc_object_t *p_this, const char *psz_name,
+ int i_action, vlc_value_t *p_val, vlc_value_t *p_val2 )
+{
+ int i_var, i;
+ variable_t *p_var;
+ vlc_value_t oldval;
+
+ vlc_mutex_lock( &p_this->var_lock );
+
+ i_var = Lookup( p_this->p_vars, p_this->i_vars, psz_name );
+
+ if( i_var < 0 )
+ {
+ vlc_mutex_unlock( &p_this->var_lock );
+ return VLC_ENOVAR;
+ }
+
+ p_var = &p_this->p_vars[i_var];
+
+ switch( i_action )
+ {
+ case VLC_VAR_SETMIN:
+ if( p_var->i_type & VLC_VAR_HASMIN )
+ {
+ p_var->pf_free( &p_var->min );
+ }
+ p_var->i_type |= VLC_VAR_HASMIN;
+ p_var->min = *p_val;
+ p_var->pf_dup( &p_var->min );
+ CheckValue( p_var, &p_var->val );
+ break;
+ case VLC_VAR_SETMAX:
+ if( p_var->i_type & VLC_VAR_HASMAX )
+ {
+ p_var->pf_free( &p_var->max );
+ }
+ p_var->i_type |= VLC_VAR_HASMAX;
+ p_var->max = *p_val;
+ p_var->pf_dup( &p_var->max );
+ CheckValue( p_var, &p_var->val );
+ break;
+ case VLC_VAR_SETSTEP:
+ if( p_var->i_type & VLC_VAR_HASSTEP )
+ {
+ p_var->pf_free( &p_var->step );
+ }
+ p_var->i_type |= VLC_VAR_HASSTEP;
+ p_var->step = *p_val;
+ p_var->pf_dup( &p_var->step );
+ CheckValue( p_var, &p_var->val );
+ break;
+ case VLC_VAR_ADDCHOICE:
+ /* FIXME: the list is sorted, dude. Use something cleverer. */
+ for( i = p_var->choices.i_count ; i-- ; )
+ {
+ if( p_var->pf_cmp( p_var->choices.p_values[i], *p_val ) < 0 )
+ {
+ break;
+ }
+ }
+
+ /* The new place is i+1 */
+ i++;
+
+ if( p_var->i_default >= i )
+ {
+ p_var->i_default++;
+ }
+
+ INSERT_ELEM( p_var->choices.p_values, p_var->choices.i_count,
+ i, *p_val );
+ INSERT_ELEM( p_var->choices_text.p_values,
+ p_var->choices_text.i_count, i, *p_val );
+ p_var->pf_dup( &p_var->choices.p_values[i] );
+ p_var->choices_text.p_values[i].psz_string =
+ ( p_val2 && p_val2->psz_string ) ?
+ strdup( p_val2->psz_string ) : NULL;
+
+ CheckValue( p_var, &p_var->val );
+ break;
+ case VLC_VAR_DELCHOICE:
+ /* FIXME: the list is sorted, dude. Use something cleverer. */
+ for( i = 0 ; i < p_var->choices.i_count ; i++ )
+ {
+ if( p_var->pf_cmp( p_var->choices.p_values[i], *p_val ) == 0 )
+ {
+ break;
+ }
+ }
+
+ if( i == p_var->choices.i_count )
+ {
+ /* Not found */
+ vlc_mutex_unlock( &p_this->var_lock );
+ return VLC_EGENERIC;
+ }
+
+ if( p_var->i_default > i )
+ {
+ p_var->i_default--;
+ }
+ else if( p_var->i_default == i )
+ {
+ p_var->i_default = -1;
+ }
+
+ p_var->pf_free( &p_var->choices.p_values[i] );
+ if( p_var->choices_text.p_values[i].psz_string )
+ free( p_var->choices_text.p_values[i].psz_string );
+ REMOVE_ELEM( p_var->choices.p_values, p_var->choices.i_count, i );
+ REMOVE_ELEM( p_var->choices_text.p_values,
+ p_var->choices_text.i_count, i );
+
+ CheckValue( p_var, &p_var->val );
+ break;
+ case VLC_VAR_CHOICESCOUNT:
+ p_val->i_int = p_var->choices.i_count;
+ break;
+ case VLC_VAR_CLEARCHOICES:
+ for( i = 0 ; i < p_var->choices.i_count ; i++ )
+ {
+ p_var->pf_free( &p_var->choices.p_values[i] );
+ }
+ for( i = 0 ; i < p_var->choices_text.i_count ; i++ )
+ {
+ if( p_var->choices_text.p_values[i].psz_string )
+ free( p_var->choices_text.p_values[i].psz_string );
+ }
+ if( p_var->choices.i_count ) free( p_var->choices.p_values );
+ if( p_var->choices_text.i_count ) free( p_var->choices_text.p_values );
+
+ p_var->choices.i_count = 0;
+ p_var->choices.p_values = NULL;
+ p_var->choices_text.i_count = 0;
+ p_var->choices_text.p_values = NULL;
+ p_var->i_default = -1;
+ break;
+ case VLC_VAR_SETDEFAULT:
+ /* FIXME: the list is sorted, dude. Use something cleverer. */
+ for( i = 0 ; i < p_var->choices.i_count ; i++ )
+ {
+ if( p_var->pf_cmp( p_var->choices.p_values[i], *p_val ) == 0 )
+ {
+ break;
+ }
+ }
+
+ if( i == p_var->choices.i_count )
+ {
+ /* Not found */
+ break;
+ }
+
+ p_var->i_default = i;
+ CheckValue( p_var, &p_var->val );
+ break;
+ case VLC_VAR_SETVALUE:
+ /* Duplicate data if needed */
+ p_var->pf_dup( p_val );
+ /* Backup needed stuff */
+ oldval = p_var->val;
+ /* Check boundaries and list */
+ CheckValue( p_var, p_val );
+ /* Set the variable */
+ p_var->val = *p_val;
+ /* Free data if needed */
+ p_var->pf_free( &oldval );
+ break;
+ case VLC_VAR_GETCHOICES:
+ case VLC_VAR_GETLIST:
+ p_val->p_list = malloc( sizeof(vlc_list_t) );
+ if( p_val2 ) p_val2->p_list = malloc( sizeof(vlc_list_t) );
+ if( p_var->choices.i_count )
+ {
+ p_val->p_list->p_values = malloc( p_var->choices.i_count
+ * sizeof(vlc_value_t) );
+ p_val->p_list->pi_types = malloc( p_var->choices.i_count
+ * sizeof(int) );
+ if( p_val2 )
+ {
+ p_val2->p_list->p_values =
+ malloc( p_var->choices.i_count * sizeof(vlc_value_t) );
+ p_val2->p_list->pi_types =
+ malloc( p_var->choices.i_count * sizeof(int) );
+ }
+ }
+ p_val->p_list->i_count = p_var->choices.i_count;
+ if( p_val2 ) p_val2->p_list->i_count = p_var->choices.i_count;
+ for( i = 0 ; i < p_var->choices.i_count ; i++ )
+ {
+ p_val->p_list->p_values[i] = p_var->choices.p_values[i];
+ p_val->p_list->pi_types[i] = p_var->i_type;
+ p_var->pf_dup( &p_val->p_list->p_values[i] );
+ if( p_val2 )
+ {
+ p_val2->p_list->p_values[i].psz_string =
+ p_var->choices_text.p_values[i].psz_string ?
+ strdup(p_var->choices_text.p_values[i].psz_string) : NULL;
+ p_val2->p_list->pi_types[i] = VLC_VAR_STRING;
+ }
+ }
+ break;
+ case VLC_VAR_FREELIST:
+ FreeList( p_val );
+ if( p_val2 && p_val2->p_list )
+ {
+ for( i = 0; i < p_val2->p_list->i_count; i++ )
+ if( p_val2->p_list->p_values[i].psz_string )
+ free( p_val2->p_list->p_values[i].psz_string );
+ if( p_val2->p_list->i_count )
+ {
+ free( p_val2->p_list->p_values );
+ free( p_val2->p_list->pi_types );
+ }
+ free( p_val2->p_list );
+ }
+ break;
+ case VLC_VAR_SETTEXT:
+ if( p_var->psz_text ) free( p_var->psz_text );
+ if( p_val && p_val->psz_string )
+ p_var->psz_text = strdup( p_val->psz_string );
+ break;
+ case VLC_VAR_GETTEXT:
+ p_val->psz_string = NULL;
+ if( p_var->psz_text )
+ {
+ p_val->psz_string = strdup( p_var->psz_text );
+ }
+ break;
+ case VLC_VAR_INHERITVALUE:
+ {
+ vlc_value_t val;
+
+ if( InheritValue( p_this, psz_name, &val, p_var->i_type )
+ == VLC_SUCCESS );
+ {
+ /* Duplicate already done */
+
+ /* Backup needed stuff */
+ oldval = p_var->val;
+ /* Check boundaries and list */
+ CheckValue( p_var, &val );
+ /* Set the variable */
+ p_var->val = val;
+ /* Free data if needed */
+ p_var->pf_free( &oldval );
+ }
+
+ if( p_val )
+ {
+ *p_val = p_var->val;
+ p_var->pf_dup( p_val );
+ }
+ }
+ break;
+ case VLC_VAR_TRIGGER_CALLBACKS:
+ {
+ /* Deal with callbacks. Tell we're in a callback, release the lock,
+ * call stored functions, retake the lock. */
+ if( p_var->i_entries )
+ {
+ int i_var;
+ int i_entries = p_var->i_entries;
+ callback_entry_t *p_entries = p_var->p_entries;
+
+ p_var->b_incallback = VLC_TRUE;
+ vlc_mutex_unlock( &p_this->var_lock );
+
+ /* The real calls */
+ for( ; i_entries-- ; )
+ {
+ p_entries[i_entries].pf_callback( p_this, psz_name, p_var->val, p_var->val,
+ p_entries[i_entries].p_data );
+ }
+
+ vlc_mutex_lock( &p_this->var_lock );
+
+ i_var = Lookup( p_this->p_vars, p_this->i_vars, psz_name );
+ if( i_var < 0 )
+ {
+ msg_Err( p_this, "variable %s has disappeared", psz_name );
+ vlc_mutex_unlock( &p_this->var_lock );
+ return VLC_ENOVAR;
+ }
+
+ p_var = &p_this->p_vars[i_var];
+ p_var->b_incallback = VLC_FALSE;
+ }
+ }
+ break;
+
+ default:
+ break;
+ }
+
+ vlc_mutex_unlock( &p_this->var_lock );
+
+ return VLC_SUCCESS;
+}
+
+/**
+ * Request a variable's type
+ *
+ * \return The variable type if it exists, or 0 if the
+ * variable could not be found.
+ * \see \ref var_type
+ */