1
0
Fork 0
forked from len0rd/rockbox

Rearrange and cleanup settings code

git-svn-id: svn://svn.rockbox.org/rockbox/trunk@13851 a1c6a512-1295-4272-9138-f99709370657
This commit is contained in:
Jonathan Gordon 2007-07-11 05:41:23 +00:00
parent 50dc0cabe3
commit a5278fa3db
12 changed files with 691 additions and 626 deletions

View file

@ -932,51 +932,6 @@ void talk_setting(void *global_settings_variable)
talk_id(setting->lang_id,false);
}
static int selected_setting; /* Used by the callback */
static void dec_sound_formatter(char *buffer, int buffer_size,
int val, const char *unit)
{
val = sound_val2phys(selected_setting, val);
char sign = ' ';
if(val < 0)
{
sign = '-';
val = abs(val);
}
int integer = val / 10;
int dec = val % 10;
snprintf(buffer, buffer_size, "%c%d.%d %s", sign, integer, dec, unit);
}
bool set_sound(const unsigned char * string,
int* variable,
int setting)
{
int talkunit = UNIT_INT;
const char* unit = sound_unit(setting);
int numdec = sound_numdecimals(setting);
int steps = sound_steps(setting);
int min = sound_min(setting);
int max = sound_max(setting);
sound_set_type* sound_callback = sound_get_fn(setting);
if (*unit == 'd') /* crude reconstruction */
talkunit = UNIT_DB;
else if (*unit == '%')
talkunit = UNIT_PERCENT;
else if (*unit == 'H')
talkunit = UNIT_HERTZ;
if (!numdec)
return set_int(string, unit, talkunit, variable, sound_callback,
steps, min, max, NULL );
else
{/* Decimal number */
selected_setting=setting;
return set_int(string, unit, talkunit, variable, sound_callback,
steps, min, max, &dec_sound_formatter );
}
}
bool set_bool(const char* string, bool* variable )
{
return set_bool_options(string, variable,
@ -985,15 +940,6 @@ bool set_bool(const char* string, bool* variable )
NULL);
}
/* wrapper to convert from int param to bool param in set_option */
static void (*boolfunction)(bool);
static void bool_funcwrapper(int value)
{
if (value)
boolfunction(true);
else
boolfunction(false);
}
bool set_bool_options(const char* string, bool* variable,
const char* yes_str, int yes_voice,
@ -1006,237 +952,11 @@ bool set_bool_options(const char* string, bool* variable,
};
bool result;
boolfunction = function;
result = set_option(string, variable, BOOL, names, 2,
function ? bool_funcwrapper : NULL);
(void (*)(int))function);
return result;
}
static void talk_unit(int unit, int value, long (*get_talk_id)(int value))
{
if (talk_menus_enabled())
{
if (get_talk_id)
{
talk_id(get_talk_id(value),false);
}
else if (unit < UNIT_LAST)
{ /* use the available unit definition */
talk_value(value, unit, false);
}
else
{ /* say the number, followed by an arbitrary voice ID */
talk_number(value, false);
talk_id(unit, true);
}
}
}
struct value_setting_data {
enum optiontype type;
/* used for "value" settings.. */
int max;
int step;
int voice_unit;
const char * unit;
void (*formatter)(char* dest, int dest_length,
int value, const char* unit);
long (*get_talk_id)(int value);
/* used for BOOL and "choice" settings */
struct opt_items* options;
};
static char * value_setting_get_name_cb(int selected_item,void * data, char *buffer)
{
struct value_setting_data* cb_data =
(struct value_setting_data*)data;
if (cb_data->type == INT && !cb_data->options)
{
int item = cb_data->max -(selected_item*cb_data->step);
if (cb_data->formatter)
cb_data->formatter(buffer, MAX_PATH,item,cb_data->unit);
else
snprintf(buffer, MAX_PATH,"%d %s",item,cb_data->unit);
}
else strcpy(buffer,P2STR(cb_data->options[selected_item].string));
return buffer;
}
#define type_fromvoidptr(type, value) \
(type == INT)? \
(int)(*(int*)(value)) \
: \
(bool)(*(bool*)(value))
static bool do_set_setting(const unsigned char* string, void *variable,
int nb_items,int selected,
struct value_setting_data *cb_data,
void (*function)(int))
{
int action;
bool done = false;
struct gui_synclist lists;
int oldvalue;
bool allow_wrap = true;
if (cb_data->type == INT)
{
oldvalue = *(int*)variable;
if (variable == &global_settings.volume)
allow_wrap = false;
}
else oldvalue = *(bool*)variable;
gui_synclist_init(&lists,value_setting_get_name_cb,(void*)cb_data,false,1);
gui_synclist_set_title(&lists, (char*)string,Icon_Questionmark);
gui_synclist_set_icon_callback(&lists,NULL);
gui_synclist_set_nb_items(&lists,nb_items);
gui_synclist_limit_scroll(&lists,true);
gui_synclist_select_item(&lists, selected);
if (talk_menus_enabled())
{
if (cb_data->type == INT && !cb_data->options)
talk_unit(cb_data->voice_unit, *(int*)variable, cb_data->get_talk_id);
else
talk_id(cb_data->options[selected].voice_id, false);
}
gui_synclist_draw(&lists);
action_signalscreenchange();
while (!done)
{
action = get_action(CONTEXT_LIST,TIMEOUT_BLOCK);
if (action == ACTION_NONE)
continue;
if (gui_synclist_do_button(&lists,action,
allow_wrap?LIST_WRAP_UNLESS_HELD:LIST_WRAP_OFF))
{
if (talk_menus_enabled())
{
int value;
if (cb_data->type == INT && !cb_data->options)
{
value = cb_data->max -
gui_synclist_get_sel_pos(&lists)*cb_data->step;
talk_unit(cb_data->voice_unit, value, cb_data->get_talk_id);
}
else
{
value = gui_synclist_get_sel_pos(&lists);
talk_id(cb_data->options[value].voice_id, false);
}
}
if (cb_data->type == INT && !cb_data->options)
*(int*)variable = cb_data->max -
gui_synclist_get_sel_pos(&lists)*cb_data->step;
else if (cb_data->type == BOOL)
*(bool*)variable = gui_synclist_get_sel_pos(&lists) ? true : false;
else *(int*)variable = gui_synclist_get_sel_pos(&lists);
}
else if (action == ACTION_STD_CANCEL)
{
if (cb_data->type == INT)
{
if (*(int*)variable != oldvalue)
{
gui_syncsplash(HZ/2, str(LANG_MENU_SETTING_CANCEL));
*(int*)variable = oldvalue;
}
}
else
{
if (*(bool*)variable != (bool)oldvalue)
{
gui_syncsplash(HZ/2, str(LANG_MENU_SETTING_CANCEL));
*(bool*)variable = (bool)oldvalue;
}
}
done = true;
}
else if (action == ACTION_STD_OK)
{
done = true;
}
else if(default_event_handler(action) == SYS_USB_CONNECTED)
return true;
gui_syncstatusbar_draw(&statusbars, false);
if ( function )
function(type_fromvoidptr(cb_data->type,variable));
}
if (cb_data->type == INT)
{
if (oldvalue != *(int*)variable)
settings_save();
}
else if (oldvalue != *(bool*)variable)
settings_save();
action_signalscreenchange();
return false;
}
static const char *unit_strings[] =
{
[UNIT_INT]
= "",
[UNIT_MS]
= "ms",
[UNIT_SEC]
= "s",
[UNIT_MIN]
= "min",
[UNIT_HOUR]
= "hr",
[UNIT_KHZ]
= "KHz",
[UNIT_DB]
= "dB",
[UNIT_PERCENT]
= "%",
[UNIT_MAH]
= "mAh",
[UNIT_PIXEL]
= "px",
[UNIT_PER_SEC]
= "per sec",
[UNIT_HERTZ]
= "Hz",
[UNIT_MB]
= "MB",
[UNIT_KBIT]
= "kb/s",
};
bool set_int_ex(const unsigned char* string,
const char* unit,
int voice_unit,
int* variable,
void (*function)(int),
int step,
int min,
int max,
void (*formatter)(char*, int, int, const char*),
long (*get_talk_id)(int))
{
int count = (max-min)/step + 1;
#if CONFIG_KEYPAD != PLAYER_PAD
struct value_setting_data data = {
INT,max, step, voice_unit,unit,formatter,get_talk_id,NULL };
if (voice_unit < UNIT_LAST)
data.unit = unit_strings[voice_unit];
else
data.unit = str(voice_unit);
return do_set_setting(string,variable,count,
(max-*variable)/step, &data,function);
#else
struct value_setting_data data = {
INT,min, -step, voice_unit,unit,formatter,get_talk_id,NULL };
if (voice_unit < UNIT_LAST)
data.unit = unit_strings[voice_unit];
else
data.unit = str(voice_unit);
return do_set_setting(string,variable,count,
(*variable-min)/step, &data,function);
#endif
}
bool set_int(const unsigned char* string,
const char* unit,
int voice_unit,
@ -1250,25 +970,7 @@ bool set_int(const unsigned char* string,
return set_int_ex(string, unit, voice_unit, variable, function,
step, min, max, formatter, NULL);
}
/* NOTE: the 'type' parameter specifies the actual type of the variable
that 'variable' points to. not the value within. Only variables with
type 'bool' should use parameter BOOL.
The type separation is necessary since int and bool are fundamentally
different and bit-incompatible types and can not share the same access
code. */
bool set_option(const char* string, void* variable, enum optiontype type,
const struct opt_items* options, int numoptions, void (*function)(int))
{
struct value_setting_data data = {
type,0, 0, 0,NULL,NULL,NULL,(struct opt_items*)options };
int selected;
if (type == BOOL)
selected = *(bool*)variable ? 1 : 0;
else selected = *(int*)variable;
return do_set_setting(string,variable,numoptions,
selected, &data,function);
}
/** extra stuff which is probably misplaced **/
@ -1303,85 +1005,3 @@ void set_file(char* filename, char* setting, int maxlen)
settings_save();
}
#ifdef HAVE_RECORDING
/* This array holds the record timer interval lengths, in seconds */
static const unsigned long rec_timer_seconds[] =
{
0, /* 0 means OFF */
5*60, /* 00:05 */
10*60, /* 00:10 */
15*60, /* 00:15 */
30*60, /* 00:30 */
60*60, /* 01:00 */
74*60, /* 74:00 */
80*60, /* 80:00 */
2*60*60, /* 02:00 */
4*60*60, /* 04:00 */
6*60*60, /* 06:00 */
8*60*60, /* 08:00 */
10L*60*60, /* 10:00 */
12L*60*60, /* 12:00 */
18L*60*60, /* 18:00 */
24L*60*60 /* 24:00 */
};
unsigned int rec_timesplit_seconds(void)
{
return rec_timer_seconds[global_settings.rec_timesplit];
}
/* This array holds the record size interval lengths, in bytes */
static const unsigned long rec_size_bytes[] =
{
0, /* 0 means OFF */
5*1024*1024, /* 5MB */
10*1024*1024, /* 10MB */
15*1024*1024, /* 15MB */
32*1024*1024, /* 32MB */
64*1024*1024, /* 64MB */
75*1024*1024, /* 75MB */
100*1024*1024, /* 100MB */
128*1024*1024, /* 128MB */
256*1024*1024, /* 256MB */
512*1024*1024, /* 512MB */
650*1024*1024, /* 650MB */
700*1024*1024, /* 700MB */
1024*1024*1024, /* 1GB */
1536*1024*1024, /* 1.5GB */
1792*1024*1024, /* 1.75GB */
};
unsigned long rec_sizesplit_bytes(void)
{
return rec_size_bytes[global_settings.rec_sizesplit];
}
/*
* Time strings used for the trigger durations.
* Keep synchronous to trigger_times in settings_apply_trigger
*/
const char * const trig_durations[TRIG_DURATION_COUNT] =
{
"0s", "1s", "2s", "5s",
"10s", "15s", "20s", "25s", "30s",
"1min", "2min", "5min", "10min"
};
void settings_apply_trigger(void)
{
/* Keep synchronous to trig_durations and trig_durations_conf*/
static const long trigger_times[TRIG_DURATION_COUNT] = {
0, HZ, 2*HZ, 5*HZ,
10*HZ, 15*HZ, 20*HZ, 25*HZ, 30*HZ,
60*HZ, 2*60*HZ, 5*60*HZ, 10*60*HZ
};
peak_meter_define_trigger(
global_settings.rec_start_thres,
trigger_times[global_settings.rec_start_duration],
MIN(trigger_times[global_settings.rec_start_duration] / 2, 2*HZ),
global_settings.rec_stop_thres,
trigger_times[global_settings.rec_stop_postrec],
trigger_times[global_settings.rec_stop_gap]
);
}
#endif