📄 variables.c
字号:
entry ); vlc_mutex_unlock( &p_this->var_lock ); return VLC_SUCCESS;}/** * Remove a callback from a variable * * pf_callback and p_data have to be given again, because different objects * might have registered the same callback function. */int __var_DelCallback( vlc_object_t *p_this, const char *psz_name, vlc_callback_t pf_callback, void *p_data ){ int i_entry, i_var; variable_t *p_var; vlc_mutex_lock( &p_this->var_lock ); i_var = GetUnused( p_this, psz_name ); if( i_var < 0 ) { vlc_mutex_unlock( &p_this->var_lock ); return i_var; } p_var = &p_this->p_vars[i_var]; for( i_entry = p_var->i_entries ; i_entry-- ; ) { if( p_var->p_entries[i_entry].pf_callback == pf_callback && p_var->p_entries[i_entry].p_data == p_data ) { break; } } if( i_entry < 0 ) { vlc_mutex_unlock( &p_this->var_lock ); return VLC_EGENERIC; } REMOVE_ELEM( p_var->p_entries, p_var->i_entries, i_entry ); vlc_mutex_unlock( &p_this->var_lock ); return VLC_SUCCESS;}/* Following functions are local *//***************************************************************************** * GetUnused: find an unused variable from its name ***************************************************************************** * We do i_tries tries before giving up, just in case the variable is being * modified and called from a callback. *****************************************************************************/static int GetUnused( vlc_object_t *p_this, const char *psz_name ){ int i_var, i_tries = 0; while( VLC_TRUE ) { i_var = Lookup( p_this->p_vars, p_this->i_vars, psz_name ); if( i_var < 0 ) { return VLC_ENOVAR; } if( ! p_this->p_vars[i_var].b_incallback ) { return i_var; } if( i_tries++ > 100 ) { msg_Err( p_this, "caught in a callback deadlock?" ); return VLC_ETIMEOUT; } vlc_mutex_unlock( &p_this->var_lock ); msleep( THREAD_SLEEP ); vlc_mutex_lock( &p_this->var_lock ); }}/***************************************************************************** * HashString: our cool hash function ***************************************************************************** * This function is not intended to be crypto-secure, we only want it to be * fast and not suck too much. This one is pretty fast and did 0 collisions * in wenglish's dictionary. *****************************************************************************/static uint32_t HashString( const char *psz_string ){ uint32_t i_hash = 0; while( *psz_string ) { i_hash += *psz_string++; i_hash += i_hash << 10; i_hash ^= i_hash >> 8; } return i_hash;}/***************************************************************************** * Insert: find an empty slot to insert a new variable ***************************************************************************** * We use a recursive inner function indexed on the hash. This function does * nothing in the rare cases where a collision may occur, see Lookup() * to see how we handle them. * XXX: does this really need to be written recursively? *****************************************************************************/static int Insert( variable_t *p_vars, int i_count, const char *psz_name ){ if( i_count == 0 ) { return 0; } return InsertInner( p_vars, i_count, HashString( psz_name ) );}static int InsertInner( variable_t *p_vars, int i_count, uint32_t i_hash ){ int i_middle; if( i_hash <= p_vars[0].i_hash ) { return 0; } if( i_hash >= p_vars[i_count - 1].i_hash ) { return i_count; } i_middle = i_count / 2; /* We know that 0 < i_middle */ if( i_hash < p_vars[i_middle].i_hash ) { return InsertInner( p_vars, i_middle, i_hash ); } /* We know that i_middle + 1 < i_count */ if( i_hash > p_vars[i_middle + 1].i_hash ) { return i_middle + 1 + InsertInner( p_vars + i_middle + 1, i_count - i_middle - 1, i_hash ); } return i_middle + 1;}/***************************************************************************** * Lookup: find an existing variable given its name ***************************************************************************** * We use a recursive inner function indexed on the hash. Care is taken of * possible hash collisions. * XXX: does this really need to be written recursively? *****************************************************************************/static int Lookup( variable_t *p_vars, int i_count, const char *psz_name ){ uint32_t i_hash; int i, i_pos; if( i_count == 0 ) { return -1; } i_hash = HashString( psz_name ); i_pos = LookupInner( p_vars, i_count, i_hash ); /* Hash not found */ if( i_hash != p_vars[i_pos].i_hash ) { return -1; } /* Hash found, entry found */ if( !strcmp( psz_name, p_vars[i_pos].psz_name ) ) { return i_pos; } /* Hash collision! This should be very rare, but we cannot guarantee * it will never happen. Just do an exhaustive search amongst all * entries with the same hash. */ for( i = i_pos - 1 ; i > 0 && i_hash == p_vars[i].i_hash ; i-- ) { if( !strcmp( psz_name, p_vars[i].psz_name ) ) { return i; } } for( i = i_pos + 1 ; i < i_count && i_hash == p_vars[i].i_hash ; i++ ) { if( !strcmp( psz_name, p_vars[i].psz_name ) ) { return i; } } /* Hash found, but entry not found */ return -1;}static int LookupInner( variable_t *p_vars, int i_count, uint32_t i_hash ){ int i_middle; if( i_hash <= p_vars[0].i_hash ) { return 0; } if( i_hash >= p_vars[i_count-1].i_hash ) { return i_count - 1; } i_middle = i_count / 2; /* We know that 0 < i_middle */ if( i_hash < p_vars[i_middle].i_hash ) { return LookupInner( p_vars, i_middle, i_hash ); } /* We know that i_middle + 1 < i_count */ if( i_hash > p_vars[i_middle].i_hash ) { return i_middle + LookupInner( p_vars + i_middle, i_count - i_middle, i_hash ); } return i_middle;}/***************************************************************************** * CheckValue: check that a value is valid wrt. a variable ***************************************************************************** * This function checks p_val's value against p_var's limitations such as * minimal and maximal value, step, in-list position, and modifies p_val if * necessary. *****************************************************************************/static void CheckValue ( variable_t *p_var, vlc_value_t *p_val ){ /* Check that our variable is in the list */ if( p_var->i_type & VLC_VAR_HASCHOICE && p_var->choices.i_count ) { int i; /* 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 ) { break; } } /* If not found, change it to anything vaguely valid */ if( i < 0 ) { /* Free the old variable, get the new one, dup it */ p_var->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 ); } } /* Check that our variable is within the bounds */ switch( p_var->i_type & VLC_VAR_TYPE ) { case VLC_VAR_INTEGER: if( p_var->i_type & VLC_VAR_HASSTEP && p_var->step.i_int && (p_val->i_int % p_var->step.i_int) ) { p_val->i_int = (p_val->i_int + (p_var->step.i_int / 2)) / p_var->step.i_int * p_var->step.i_int; } if( p_var->i_type & VLC_VAR_HASMIN && p_val->i_int < p_var->min.i_int ) { p_val->i_int = p_var->min.i_int; } if( p_var->i_type & VLC_VAR_HASMAX && p_val->i_int > p_var->max.i_int ) { p_val->i_int = p_var->max.i_int; } break; case VLC_VAR_FLOAT: if( p_var->i_type & VLC_VAR_HASSTEP && p_var->step.f_float ) { float f_round = p_var->step.f_float * (float)(int)( 0.5 + p_val->f_float / p_var->step.f_float ); if( p_val->f_float != f_round ) { p_val->f_float = f_round; } } if( p_var->i_type & VLC_VAR_HASMIN && p_val->f_float < p_var->min.f_float ) { p_val->f_float = p_var->min.f_float; } if( p_var->i_type & VLC_VAR_HASMAX && p_val->f_float > p_var->max.f_float ) { p_val->f_float = p_var->max.f_float; } break; case VLC_VAR_TIME: /* FIXME: TODO */ break; }}/***************************************************************************** * InheritValue: try to inherit the value of this variable from the same one * in our closest parent. *****************************************************************************/static int InheritValue( vlc_object_t *p_this, const char *psz_name, vlc_value_t *p_val, int i_type ){ 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 ) { switch( i_type & VLC_VAR_TYPE ) { case VLC_VAR_FILE: case VLC_VAR_DIRECTORY: 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; case VLC_VAR_FLOAT: 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: p_val->b_bool = config_GetInt( p_this, psz_name ); break; case VLC_VAR_LIST: { char *psz_orig, *psz_var; vlc_list_t *p_list = malloc(sizeof(vlc_list_t)); p_val->p_list = p_list; p_list->i_count = 0; psz_var = psz_orig = config_GetPsz( p_this, psz_name ); while( psz_var && *psz_var ) { char *psz_item = psz_var; vlc_value_t val; while( *psz_var && *psz_var != ',' ) psz_var++; if( *psz_var == ',' ) { *psz_var = '\0'; psz_var++; } val.i_int = strtol( psz_item, NULL, 0 ); INSERT_ELEM( p_list->p_values, p_list->i_count, p_list->i_count, val ); /* p_list->i_count is incremented twice by INSERT_ELEM */ p_list->i_count--; INSERT_ELEM( p_list->pi_types, p_list->i_count, p_list->i_count, VLC_VAR_INTEGER ); } if( psz_orig ) free( psz_orig ); break; } default: return VLC_ENOOBJ; break; } return VLC_SUCCESS; } /* Look for the variable */ vlc_mutex_lock( &p_this->p_parent->var_lock ); i_var = Lookup( p_this->p_parent->p_vars, p_this->p_parent->i_vars, psz_name ); if( i_var >= 0 ) { /* We found it! */ p_var = &p_this->p_parent->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_this->p_parent->var_lock ); return VLC_SUCCESS; } vlc_mutex_unlock( &p_this->p_parent->var_lock ); /* We're still not there */ return InheritValue( p_this->p_parent, psz_name, p_val, i_type );}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -