X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=src%2Fmisc%2Fvariables.c;h=4a6d8e908d0f5d821a85d9ba77d1cd4f3d09b56d;hb=80cb294832cf77b8440957d75d24cae2ec853d0c;hp=8771c440f0a144675a5503b822026b77cffed1ac;hpb=6cd455080c449fab3177ede198a5c23a9a6d8d0e;p=vlc diff --git a/src/misc/variables.c b/src/misc/variables.c index 8771c440f0..4a6d8e908d 100644 --- a/src/misc/variables.c +++ b/src/misc/variables.c @@ -90,7 +90,7 @@ static void DupList( vlc_value_t *p_val ) { p_list->p_values[i] = p_val->p_list->p_values[i]; p_list->pi_types[i] = p_val->p_list->pi_types[i]; - switch( p_val->p_list->pi_types[i] & VLC_VAR_TYPE ) + switch( p_val->p_list->pi_types[i] & VLC_VAR_CLASS ) { case VLC_VAR_STRING: @@ -113,7 +113,7 @@ static void FreeList( vlc_value_t *p_val ) int i; for( i = 0; i < p_val->p_list->i_count; i++ ) { - switch( p_val->p_list->pi_types[i] & VLC_VAR_TYPE ) + switch( p_val->p_list->pi_types[i] & VLC_VAR_CLASS ) { case VLC_VAR_STRING: FreeString( &p_val->p_list->p_values[i] ); @@ -134,6 +134,17 @@ static void FreeList( vlc_value_t *p_val ) free( p_val->p_list ); } +static const struct variable_ops_t +void_ops = { NULL, DupDummy, FreeDummy, }, +addr_ops = { CmpAddress, DupDummy, FreeDummy, }, +bool_ops = { CmpBool, DupDummy, FreeDummy, }, +float_ops = { CmpFloat, DupDummy, FreeDummy, }, +int_ops = { CmpInt, DupDummy, FreeDummy, }, +list_ops = { CmpAddress, DupList, FreeList, }, +mutex_ops = { CmpAddress, DupDummy, FreeMutex, }, +string_ops = { CmpString, DupString, FreeString, }, +time_ops = { CmpTime, DupDummy, FreeDummy, }; + /***************************************************************************** * Local prototypes *****************************************************************************/ @@ -168,7 +179,6 @@ int __var_Create( vlc_object_t *p_this, const char *psz_name, int i_type ) static vlc_list_t dummy_null_list = {0, NULL, NULL}; vlc_object_internals_t *p_priv = vlc_internals( p_this ); - vlc_refcheck( p_this ); vlc_mutex_lock( &p_priv->var_lock ); /* FIXME: if the variable already exists, we don't duplicate it. But we @@ -217,9 +227,6 @@ int __var_Create( vlc_object_t *p_this, const char *psz_name, int i_type ) p_var->i_type = i_type & ~VLC_VAR_DOINHERIT; memset( &p_var->val, 0, sizeof(vlc_value_t) ); - p_var->pf_dup = DupDummy; - p_var->pf_free = FreeDummy; - p_var->i_usage = 1; p_var->i_default = -1; @@ -235,55 +242,48 @@ int __var_Create( vlc_object_t *p_this, const char *psz_name, int i_type ) /* Always initialize the variable, even if it is a list variable; this * will lead to errors if the variable is not initialized, but it will * not cause crashes in the variable handling. */ - switch( i_type & VLC_VAR_TYPE ) + switch( i_type & VLC_VAR_CLASS ) { case VLC_VAR_BOOL: - p_var->pf_cmp = CmpBool; + p_var->ops = &bool_ops; p_var->val.b_bool = false; break; case VLC_VAR_INTEGER: - case VLC_VAR_HOTKEY: - p_var->pf_cmp = CmpInt; + p_var->ops = &int_ops; p_var->val.i_int = 0; break; case VLC_VAR_STRING: - case VLC_VAR_MODULE: - case VLC_VAR_FILE: - case VLC_VAR_DIRECTORY: - case VLC_VAR_VARIABLE: - p_var->pf_cmp = CmpString; - p_var->pf_dup = DupString; - p_var->pf_free = FreeString; + p_var->ops = &string_ops; p_var->val.psz_string = NULL; break; case VLC_VAR_FLOAT: - p_var->pf_cmp = CmpFloat; + p_var->ops = &float_ops; p_var->val.f_float = 0.0; break; case VLC_VAR_TIME: - p_var->pf_cmp = CmpTime; + p_var->ops = &time_ops; p_var->val.i_time = 0; break; case VLC_VAR_ADDRESS: - p_var->pf_cmp = CmpAddress; + p_var->ops = &addr_ops; p_var->val.p_address = NULL; break; case VLC_VAR_MUTEX: - p_var->pf_cmp = CmpAddress; - p_var->pf_free = FreeMutex; + p_var->ops = &mutex_ops; p_var->val.p_address = malloc( sizeof(vlc_mutex_t) ); vlc_mutex_init( (vlc_mutex_t*)p_var->val.p_address ); break; case VLC_VAR_LIST: - p_var->pf_cmp = CmpAddress; - p_var->pf_dup = DupList; - p_var->pf_free = FreeList; + p_var->ops = &list_ops; p_var->val.p_list = &dummy_null_list; break; + default: + p_var->ops = &void_ops; + break; } /* Duplicate the default data we stored. */ - p_var->pf_dup( &p_var->val ); + p_var->ops->pf_dup( &p_var->val ); if( i_type & VLC_VAR_DOINHERIT ) { @@ -293,7 +293,7 @@ int __var_Create( vlc_object_t *p_this, const char *psz_name, int i_type ) == VLC_SUCCESS ) { /* Free data if needed */ - p_var->pf_free( &p_var->val ); + p_var->ops->pf_free( &p_var->val ); /* Set the variable */ p_var->val = val; @@ -306,7 +306,7 @@ int __var_Create( vlc_object_t *p_this, const char *psz_name, int i_type ) 0, val ); INSERT_ELEM( p_var->choices_text.p_values, p_var->choices_text.i_count, 0, val ); - p_var->pf_dup( &p_var->choices.p_values[0] ); + p_var->ops->pf_dup( &p_var->choices.p_values[0] ); p_var->choices_text.p_values[0].psz_string = NULL; } } @@ -332,7 +332,6 @@ int __var_Destroy( vlc_object_t *p_this, const char *psz_name ) variable_t *p_var; vlc_object_internals_t *p_priv = vlc_internals( p_this ); - vlc_refcheck( p_this ); vlc_mutex_lock( &p_priv->var_lock ); i_var = GetUnused( p_this, psz_name ); @@ -352,14 +351,14 @@ int __var_Destroy( vlc_object_t *p_this, const char *psz_name ) } /* Free value if needed */ - p_var->pf_free( &p_var->val ); + p_var->ops->pf_free( &p_var->val ); /* Free choice list if needed */ if( p_var->choices.i_count ) { for( i = 0 ; i < p_var->choices.i_count ; i++ ) { - p_var->pf_free( &p_var->choices.p_values[i] ); + p_var->ops->pf_free( &p_var->choices.p_values[i] ); free( p_var->choices_text.p_values[i].psz_string ); } free( p_var->choices.p_values ); @@ -409,7 +408,6 @@ int __var_Change( vlc_object_t *p_this, const char *psz_name, vlc_value_t oldval; vlc_object_internals_t *p_priv = vlc_internals( p_this ); - vlc_refcheck( p_this ); vlc_mutex_lock( &p_priv->var_lock ); i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name ); @@ -427,11 +425,11 @@ int __var_Change( vlc_object_t *p_this, const char *psz_name, case VLC_VAR_SETMIN: if( p_var->i_type & VLC_VAR_HASMIN ) { - p_var->pf_free( &p_var->min ); + p_var->ops->pf_free( &p_var->min ); } p_var->i_type |= VLC_VAR_HASMIN; p_var->min = *p_val; - p_var->pf_dup( &p_var->min ); + p_var->ops->pf_dup( &p_var->min ); CheckValue( p_var, &p_var->val ); break; case VLC_VAR_GETMIN: @@ -443,11 +441,11 @@ int __var_Change( vlc_object_t *p_this, const char *psz_name, case VLC_VAR_SETMAX: if( p_var->i_type & VLC_VAR_HASMAX ) { - p_var->pf_free( &p_var->max ); + p_var->ops->pf_free( &p_var->max ); } p_var->i_type |= VLC_VAR_HASMAX; p_var->max = *p_val; - p_var->pf_dup( &p_var->max ); + p_var->ops->pf_dup( &p_var->max ); CheckValue( p_var, &p_var->val ); break; case VLC_VAR_GETMAX: @@ -459,11 +457,11 @@ int __var_Change( vlc_object_t *p_this, const char *psz_name, case VLC_VAR_SETSTEP: if( p_var->i_type & VLC_VAR_HASSTEP ) { - p_var->pf_free( &p_var->step ); + p_var->ops->pf_free( &p_var->step ); } p_var->i_type |= VLC_VAR_HASSTEP; p_var->step = *p_val; - p_var->pf_dup( &p_var->step ); + p_var->ops->pf_dup( &p_var->step ); CheckValue( p_var, &p_var->val ); break; case VLC_VAR_GETSTEP: @@ -479,7 +477,7 @@ int __var_Change( vlc_object_t *p_this, const char *psz_name, 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->ops->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; @@ -489,7 +487,7 @@ int __var_Change( vlc_object_t *p_this, const char *psz_name, case VLC_VAR_DELCHOICE: for( i = 0 ; i < p_var->choices.i_count ; i++ ) { - if( p_var->pf_cmp( p_var->choices.p_values[i], *p_val ) == 0 ) + if( p_var->ops->pf_cmp( p_var->choices.p_values[i], *p_val ) == 0 ) { break; } @@ -511,7 +509,7 @@ int __var_Change( vlc_object_t *p_this, const char *psz_name, p_var->i_default = -1; } - p_var->pf_free( &p_var->choices.p_values[i] ); + p_var->ops->pf_free( &p_var->choices.p_values[i] ); 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, @@ -525,7 +523,7 @@ int __var_Change( vlc_object_t *p_this, const char *psz_name, case VLC_VAR_CLEARCHOICES: for( i = 0 ; i < p_var->choices.i_count ; i++ ) { - p_var->pf_free( &p_var->choices.p_values[i] ); + p_var->ops->pf_free( &p_var->choices.p_values[i] ); } for( i = 0 ; i < p_var->choices_text.i_count ; i++ ) free( p_var->choices_text.p_values[i].psz_string ); @@ -543,7 +541,7 @@ int __var_Change( vlc_object_t *p_this, const char *psz_name, /* 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 ) + if( p_var->ops->pf_cmp( p_var->choices.p_values[i], *p_val ) == 0 ) { break; } @@ -560,7 +558,7 @@ int __var_Change( vlc_object_t *p_this, const char *psz_name, break; case VLC_VAR_SETVALUE: /* Duplicate data if needed */ - p_var->pf_dup( p_val ); + p_var->ops->pf_dup( p_val ); /* Backup needed stuff */ oldval = p_var->val; /* Check boundaries and list */ @@ -568,7 +566,7 @@ int __var_Change( vlc_object_t *p_this, const char *psz_name, /* Set the variable */ p_var->val = *p_val; /* Free data if needed */ - p_var->pf_free( &oldval ); + p_var->ops->pf_free( &oldval ); break; case VLC_VAR_GETCHOICES: case VLC_VAR_GETLIST: @@ -594,7 +592,7 @@ int __var_Change( vlc_object_t *p_this, const char *psz_name, { 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] ); + p_var->ops->pf_dup( &p_val->p_list->p_values[i] ); if( p_val2 ) { p_val2->p_list->p_values[i].psz_string = @@ -648,13 +646,13 @@ int __var_Change( vlc_object_t *p_this, const char *psz_name, /* Set the variable */ p_var->val = val; /* Free data if needed */ - p_var->pf_free( &oldval ); + p_var->ops->pf_free( &oldval ); } if( p_val ) { *p_val = p_var->val; - p_var->pf_dup( p_val ); + p_var->ops->pf_dup( p_val ); } } break; @@ -750,7 +748,6 @@ int __var_Set( vlc_object_t *p_this, const char *psz_name, vlc_value_t val ) vlc_value_t oldval; vlc_object_internals_t *p_priv = vlc_internals( p_this ); - vlc_refcheck( p_this ); vlc_mutex_lock( &p_priv->var_lock ); i_var = GetUnused( p_this, psz_name ); @@ -763,7 +760,7 @@ int __var_Set( vlc_object_t *p_this, const char *psz_name, vlc_value_t val ) p_var = &p_priv->p_vars[i_var]; /* Duplicate data if needed */ - p_var->pf_dup( &val ); + p_var->ops->pf_dup( &val ); /* Backup needed stuff */ oldval = p_var->val; @@ -807,7 +804,7 @@ int __var_Set( vlc_object_t *p_this, const char *psz_name, vlc_value_t val ) } /* Free data if needed */ - p_var->pf_free( &oldval ); + p_var->ops->pf_free( &oldval ); vlc_mutex_unlock( &p_priv->var_lock ); @@ -828,7 +825,6 @@ int __var_Get( vlc_object_t *p_this, const char *psz_name, vlc_value_t *p_val ) variable_t *p_var; vlc_object_internals_t *p_priv = vlc_internals( p_this ); - vlc_refcheck( p_this ); vlc_mutex_lock( &p_priv->var_lock ); i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name ); @@ -845,7 +841,7 @@ int __var_Get( vlc_object_t *p_this, const char *psz_name, vlc_value_t *p_val ) *p_val = p_var->val; /* Duplicate value if needed */ - p_var->pf_dup( p_val ); + p_var->ops->pf_dup( p_val ); vlc_mutex_unlock( &p_priv->var_lock ); @@ -853,24 +849,6 @@ int __var_Get( vlc_object_t *p_this, const char *psz_name, vlc_value_t *p_val ) } -/** - * Finds a process-wide mutex, creates it if needed, and locks it. - * Unlock with vlc_mutex_unlock(). - */ -vlc_mutex_t *var_AcquireMutex( const char *name ) -{ - libvlc_global_data_t *p_global = vlc_global(); - vlc_value_t val; - - if( var_Create( p_global, name, VLC_VAR_MUTEX ) ) - return NULL; - - var_Get( p_global, name, &val ); - vlc_mutex_lock( val.p_address ); - return val.p_address; -} - - /** * Register a callback in a variable * @@ -895,7 +873,6 @@ int __var_AddCallback( vlc_object_t *p_this, const char *psz_name, callback_entry_t entry; vlc_object_internals_t *p_priv = vlc_internals( p_this ); - vlc_refcheck( p_this ); entry.pf_callback = pf_callback; entry.p_data = p_data; @@ -933,7 +910,6 @@ int __var_DelCallback( vlc_object_t *p_this, const char *psz_name, variable_t *p_var; vlc_object_internals_t *p_priv = vlc_internals( p_this ); - vlc_refcheck( p_this ); vlc_mutex_lock( &p_priv->var_lock ); i_var = GetUnused( p_this, psz_name ); @@ -1101,6 +1077,7 @@ void var_OptionParse( vlc_object_t *p_obj, const char *psz_option, { msg_Err( p_obj, "unsafe option \"%s\" has been ignored for " "security reasons", psz_name ); + free( psz_name ); return; } } @@ -1167,6 +1144,10 @@ void var_OptionParse( vlc_object_t *p_obj, const char *psz_option, var_Set( p_obj, psz_name, val ); + // If that's a list, remove all elements allocated + if( i_type == VLC_VAR_LIST ) + FreeList( &val ); + cleanup: free( psz_name ); } @@ -1388,7 +1369,7 @@ static void CheckValue ( variable_t *p_var, vlc_value_t *p_val ) /* FIXME: the list is sorted, dude. Use something cleverer. */ for( i = p_var->choices.i_count ; i-- ; ) { - if( p_var->pf_cmp( *p_val, p_var->choices.p_values[i] ) == 0 ) + if( p_var->ops->pf_cmp( *p_val, p_var->choices.p_values[i] ) == 0 ) { break; } @@ -1398,10 +1379,10 @@ static void CheckValue ( variable_t *p_var, vlc_value_t *p_val ) if( i < 0 ) { /* Free the old variable, get the new one, dup it */ - p_var->pf_free( p_val ); + p_var->ops->pf_free( p_val ); *p_val = p_var->choices.p_values[p_var->i_default >= 0 ? p_var->i_default : 0 ]; - p_var->pf_dup( p_val ); + p_var->ops->pf_dup( p_val ); } } @@ -1455,7 +1436,8 @@ static void CheckValue ( variable_t *p_var, vlc_value_t *p_val ) /***************************************************************************** * InheritValue: try to inherit the value of this variable from the same one - * in our closest parent. + * in our closest parent, libvlc or ultimately from the configuration. + * The function should always be entered with the object var_lock locked. *****************************************************************************/ static int InheritValue( vlc_object_t *p_this, const char *psz_name, vlc_value_t *p_val, int i_type ) @@ -1463,17 +1445,40 @@ static int InheritValue( vlc_object_t *p_this, const char *psz_name, int i_var; variable_t *p_var; - /* No need to take the structure lock, - * we are only looking for our parents */ - - if( !p_this->p_parent ) + if( p_this->p_parent || ( p_this->p_libvlc && p_this != p_this->p_libvlc ) ) { - switch( i_type & VLC_VAR_TYPE ) + vlc_object_internals_t *p_priv; + + if( p_this->p_parent ) + p_priv = vlc_internals( p_this->p_parent ); + else + p_priv = vlc_internals( p_this->p_libvlc ); + + i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name ); + + if( i_var >= 0 ) { - case VLC_VAR_FILE: - case VLC_VAR_DIRECTORY: + /* We found it! */ + p_var = &p_priv->p_vars[i_var]; + + /* Really get the variable */ + *p_val = p_var->val; + + /* Duplicate value if needed */ + p_var->ops->pf_dup( p_val ); + + return VLC_SUCCESS; + } + else if ( p_this->p_parent ) /* We are still not there */ + return InheritValue( p_this->p_parent, psz_name, p_val, i_type ); + + /* else take value from config */ + } + + + switch( i_type & VLC_VAR_CLASS ) + { case VLC_VAR_STRING: - case VLC_VAR_MODULE: p_val->psz_string = config_GetPsz( p_this, psz_name ); if( !p_val->psz_string ) p_val->psz_string = strdup(""); break; @@ -1481,7 +1486,6 @@ static int InheritValue( vlc_object_t *p_this, const char *psz_name, p_val->f_float = config_GetFloat( p_this, psz_name ); break; case VLC_VAR_INTEGER: - case VLC_VAR_HOTKEY: p_val->i_int = config_GetInt( p_this, psz_name ); break; case VLC_VAR_BOOL: @@ -1519,38 +1523,8 @@ static int InheritValue( vlc_object_t *p_this, const char *psz_name, default: return VLC_ENOOBJ; break; - } - - return VLC_SUCCESS; } - - vlc_object_internals_t *p_priv = vlc_internals( p_this->p_parent ); - - /* Look for the variable */ - vlc_mutex_lock( &p_priv->var_lock ); - - i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name ); - - if( i_var >= 0 ) - { - /* We found it! */ - p_var = &p_priv->p_vars[i_var]; - - /* Really get the variable */ - *p_val = p_var->val; - - /* Duplicate value if needed */ - p_var->pf_dup( p_val ); - - vlc_mutex_unlock( &p_priv->var_lock ); - return VLC_SUCCESS; - } - - vlc_mutex_unlock( &p_priv->var_lock ); - - /* We're still not there */ - - return InheritValue( p_this->p_parent, psz_name, p_val, i_type ); + return VLC_SUCCESS; } /********************************************************************** @@ -1570,7 +1544,6 @@ int __var_Command( vlc_object_t *p_this, const char *psz_name, return VLC_ENOOBJ; } - vlc_refcheck( p_this ); i_type = var_Type( p_obj, psz_cmd ); if( !( i_type&VLC_VAR_ISCOMMAND ) ) { @@ -1580,7 +1553,7 @@ int __var_Command( vlc_object_t *p_this, const char *psz_name, return VLC_EGENERIC; } - i_type &= 0xf0; + i_type &= VLC_VAR_CLASS; switch( i_type ) { case VLC_VAR_INTEGER: