puzzles: fix configuration menu (again!)

Change-Id: I440f3cea1bbeb3e7bda4aebefaaece3e8f3d44fd
This commit is contained in:
Franklin Wei 2017-01-13 23:18:20 -05:00
parent 8e4429853d
commit 823f726f83

View file

@ -805,8 +805,11 @@ static int list_choose(const char *list_str, const char *title)
} }
} }
static void do_configure_item(config_item *cfg) #define CONFIGMENU_FREEDSTR 1
#define CONFIGMENU_SUCCESS 2
static int do_configure_item(config_item *cfg)
{ {
int rc = 0;
switch(cfg->type) switch(cfg->type)
{ {
case C_STRING: case C_STRING:
@ -819,34 +822,46 @@ static void do_configure_item(config_item *cfg)
if(rb->kbd_input(newstr, MAX_STRLEN) < 0) if(rb->kbd_input(newstr, MAX_STRLEN) < 0)
{ {
sfree(newstr); sfree(newstr);
break; return rc;
} }
if(strcmp(newstr, cfg->sval))
rc |= CONFIGMENU_SUCCESS;
sfree(cfg->sval); sfree(cfg->sval);
cfg->sval = newstr; cfg->sval = newstr;
break; rc |= CONFIGMENU_FREEDSTR;
return rc;
} }
case C_BOOLEAN: case C_BOOLEAN:
{ {
bool res = cfg->ival != 0; bool res = cfg->ival != 0;
bool orig = res;
rb->set_bool(cfg->name, &res); rb->set_bool(cfg->name, &res);
/* seems to reset backdrop */ /* seems to reset backdrop */
rb->lcd_set_backdrop(NULL); rb->lcd_set_backdrop(NULL);
cfg->ival = res; cfg->ival = res;
if(cfg->ival != orig)
rc |= CONFIGMENU_SUCCESS;
break; break;
} }
case C_CHOICES: case C_CHOICES:
{ {
int old = cfg->ival;
int sel = list_choose(cfg->sval, cfg->name); int sel = list_choose(cfg->sval, cfg->name);
if(sel >= 0) if(sel >= 0)
{
cfg->ival = sel; cfg->ival = sel;
}
if(cfg->ival != old)
rc |= CONFIGMENU_SUCCESS;
break; break;
} }
default: default:
fatal("bad type"); fatal("bad type");
break; break;
} }
return rc;
} }
const char *config_formatter(int sel, void *data, char *buf, size_t len) const char *config_formatter(int sel, void *data, char *buf, size_t len)
@ -904,14 +919,24 @@ static bool config_menu(void)
config_item old; config_item old;
int pos = rb->gui_synclist_get_sel_pos(&list); int pos = rb->gui_synclist_get_sel_pos(&list);
memcpy(&old, config + pos, sizeof(old)); memcpy(&old, config + pos, sizeof(old));
do_configure_item(config + pos); char *old_str;
if(old.type == C_STRING)
old_str = dupstr(old.sval);
int rc = do_configure_item(config + pos);
char *err = midend_set_config(me, CFG_SETTINGS, config); char *err = midend_set_config(me, CFG_SETTINGS, config);
if(err) if(err)
{ {
rb->splash(HZ, err); rb->splash(HZ, err);
memcpy(config + pos, &old, sizeof(old)); memcpy(config + pos, &old, sizeof(old));
if(rc & CONFIGMENU_FREEDSTR)
config[pos].sval = old_str;
} }
else else if(old.type == C_STRING)
{
/* success, and we duplicated the old string, so free it */
sfree(old_str);
}
if(!err && (rc & CONFIGMENU_SUCCESS))
{ {
success = true; success = true;
} }