📄 variables.c
字号:
/***************************************************************************** * variables.c: routines for object variables handling ***************************************************************************** * Copyright (C) 2002-2004 VideoLAN * $Id: variables.c 10101 2005-03-02 16:47:31Z robux4 $ * * Authors: Samuel Hocevar <sam@zoy.org> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA. *****************************************************************************//***************************************************************************** * Preamble *****************************************************************************/#include <vlc/vlc.h>#ifdef HAVE_STDLIB_H# include <stdlib.h> /* realloc() */#endif/***************************************************************************** * Private types *****************************************************************************/struct callback_entry_t{ vlc_callback_t pf_callback; void * p_data;};/***************************************************************************** * Local comparison functions, returns 0 if v == w, < 0 if v < w, > 0 if v > w *****************************************************************************/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; }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; }static int CmpTime( vlc_value_t v, vlc_value_t w ){ return v.i_time == w.i_time ? 0 : v.i_time > w.i_time ? 1 : -1;}static int CmpString( vlc_value_t v, vlc_value_t w ) { return strcmp( v.psz_string, w.psz_string ); }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; }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; }/***************************************************************************** * Local duplication functions, and local deallocation functions *****************************************************************************/static void DupDummy( vlc_value_t *p_val ) { (void)p_val; /* unused */ }static void DupString( vlc_value_t *p_val ) { p_val->psz_string = strdup( p_val->psz_string ); }static void DupList( vlc_value_t *p_val ){ int i; vlc_list_t *p_list = malloc( sizeof(vlc_list_t) ); p_list->i_count = p_val->p_list->i_count; if( p_val->p_list->i_count ) { p_list->p_values = malloc( p_list->i_count * sizeof(vlc_value_t) ); p_list->pi_types = malloc( p_list->i_count * sizeof(int) ); } else { p_list->p_values = NULL; p_list->pi_types = NULL; } for( i = 0; i < p_list->i_count; i++ ) { 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 ) { case VLC_VAR_STRING: DupString( &p_list->p_values[i] ); break; default: break; } } p_val->p_list = p_list;}static void FreeDummy( vlc_value_t *p_val ) { (void)p_val; /* unused */ }static void FreeString( vlc_value_t *p_val ) { free( p_val->psz_string ); }static void FreeMutex( vlc_value_t *p_val ) { vlc_mutex_destroy( (vlc_mutex_t*)p_val->p_address ); free( p_val->p_address ); }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 ) { case VLC_VAR_STRING: FreeString( &p_val->p_list->p_values[i] ); break; case VLC_VAR_MUTEX: FreeMutex( &p_val->p_list->p_values[i] ); break; default: break; } } if( p_val->p_list->i_count ) { free( p_val->p_list->p_values ); free( p_val->p_list->pi_types ); } free( p_val->p_list );}/***************************************************************************** * Local prototypes *****************************************************************************/static int GetUnused ( vlc_object_t *, const char * );static uint32_t HashString ( const char * );static int Insert ( variable_t *, int, const char * );static int InsertInner ( variable_t *, int, uint32_t );static int Lookup ( variable_t *, int, const char * );static int LookupInner ( variable_t *, int, uint32_t );static void CheckValue ( variable_t *, vlc_value_t * );static int InheritValue( vlc_object_t *, const char *, vlc_value_t *, int );/** * Initialize a vlc variable * * We hash the given string and insert it into the sorted list. The insertion * may require slow memory copies, but think about what we gain in the log(n) * lookup phase when setting/getting the variable value! * * \param p_this The object in which to create the variable * \param psz_name The name of the variable * \param i_type The variables type. Must be one of \ref var_type combined with * zero or more \ref var_flags */int __var_Create( vlc_object_t *p_this, const char *psz_name, int i_type ){ int i_new; variable_t *p_var; static vlc_list_t dummy_null_list = {0, NULL, NULL}; vlc_mutex_lock( &p_this->var_lock ); /* FIXME: if the variable already exists, we don't duplicate it. But we * duplicate the lookups. It's not that serious, but if anyone finds some * time to rework Insert() so that only one lookup has to be done, feel * free to do so. */ i_new = Lookup( p_this->p_vars, p_this->i_vars, psz_name ); if( i_new >= 0 ) { /* If the types differ, variable creation failed. */ if( (i_type & ~VLC_VAR_DOINHERIT) != p_this->p_vars[i_new].i_type ) { vlc_mutex_unlock( &p_this->var_lock ); return VLC_EBADVAR; } p_this->p_vars[i_new].i_usage++; vlc_mutex_unlock( &p_this->var_lock ); return VLC_SUCCESS; } i_new = Insert( p_this->p_vars, p_this->i_vars, psz_name ); if( (p_this->i_vars & 15) == 15 ) { p_this->p_vars = realloc( p_this->p_vars, (p_this->i_vars+17) * sizeof(variable_t) ); } memmove( p_this->p_vars + i_new + 1, p_this->p_vars + i_new, (p_this->i_vars - i_new) * sizeof(variable_t) ); p_this->i_vars++; p_var = &p_this->p_vars[i_new]; p_var->i_hash = HashString( psz_name ); p_var->psz_name = strdup( psz_name ); p_var->psz_text = NULL; 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; 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->b_incallback = VLC_FALSE; p_var->i_entries = 0; p_var->p_entries = NULL; /* 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 ) { case VLC_VAR_BOOL: p_var->pf_cmp = CmpBool; p_var->val.b_bool = VLC_FALSE; break; case VLC_VAR_INTEGER: case VLC_VAR_HOTKEY: p_var->pf_cmp = CmpInt; 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->val.psz_string = ""; break; case VLC_VAR_FLOAT: p_var->pf_cmp = CmpFloat; p_var->val.f_float = 0.0; break; case VLC_VAR_TIME: p_var->pf_cmp = CmpTime; p_var->val.i_time = 0; break; case VLC_VAR_ADDRESS: p_var->pf_cmp = CmpAddress; p_var->val.p_address = NULL; break; case VLC_VAR_MUTEX: p_var->pf_cmp = CmpAddress; p_var->pf_free = FreeMutex; p_var->val.p_address = malloc( sizeof(vlc_mutex_t) ); vlc_mutex_init( p_this, (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->val.p_list = &dummy_null_list; break; } /* Duplicate the default data we stored. */ p_var->pf_dup( &p_var->val ); if( i_type & VLC_VAR_DOINHERIT ) { vlc_value_t val; if( InheritValue( p_this, psz_name, &val, p_var->i_type ) == VLC_SUCCESS ); { /* Free data if needed */ p_var->pf_free( &p_var->val ); /* Set the variable */ p_var->val = val; if( i_type & VLC_VAR_HASCHOICE ) { /* We must add the inherited value to our choice list */ p_var->i_default = 0; INSERT_ELEM( p_var->choices.p_values, p_var->choices.i_count, 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->choices_text.p_values[0].psz_string = NULL; } } } vlc_mutex_unlock( &p_this->var_lock ); return VLC_SUCCESS;}/** * Destroy a vlc variable * * Look for the variable and destroy it if it is found. As in var_Create we * do a call to memmove() but we have performance counterparts elsewhere. * * \param p_this The object that holds the variable * \param psz_name The name of the variable */int __var_Destroy( vlc_object_t *p_this, const char *psz_name ){ int i_var, i; 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]; if( p_var->i_usage > 1 ) { p_var->i_usage--; vlc_mutex_unlock( &p_this->var_lock ); return VLC_SUCCESS; } /* Free value if needed */ p_var->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] ); if( p_var->choices_text.p_values[i].psz_string ) free( p_var->choices_text.p_values[i].psz_string ); } free( p_var->choices.p_values ); free( p_var->choices_text.p_values ); } /* Free callbacks if needed */ if( p_var->p_entries ) { free( p_var->p_entries ); } free( p_var->psz_name ); if( p_var->psz_text ) free( p_var->psz_text ); memmove( p_this->p_vars + i_var, p_this->p_vars + i_var + 1, (p_this->i_vars - i_var - 1) * sizeof(variable_t) ); if( (p_this->i_vars & 15) == 0 ) { p_this->p_vars = realloc( p_this->p_vars, (p_this->i_vars) * sizeof( variable_t ) ); } p_this->i_vars--; vlc_mutex_unlock( &p_this->var_lock ); return VLC_SUCCESS;}/** * 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;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -