From 92bef70f7d1e0240f0a5baa0bfbc6f506c1e5c67 Mon Sep 17 00:00:00 2001 From: notaz Date: Sun, 25 Sep 2011 12:38:21 +0000 Subject: [PATCH] menu: don't assume onoff vars are int git-svn-id: file:///home/notaz/opt/svn/PicoDrive/platform@933 be3aeb3a-fb24-0410-a615-afba39da0efa --- common/menu.c | 38 ++++++++++++++++++++++++++++++-------- common/menu.h | 5 ++++- 2 files changed, 34 insertions(+), 9 deletions(-) diff --git a/common/menu.c b/common/menu.c index f55b48d..1513d39 100644 --- a/common/menu.c +++ b/common/menu.c @@ -1,5 +1,5 @@ /* - * (C) Gražvydas "notaz" Ignotas, 2006-2010 + * (C) Gražvydas "notaz" Ignotas, 2006-2011 * * This work is licensed under the terms of any of these licenses * (at your option): @@ -372,6 +372,28 @@ static int me_count(const menu_entry *ent) return ret; } +static unsigned int me_read_onoff(const menu_entry *ent) +{ + // guess var size based on mask to avoid reading too much + if (ent->mask & 0xffff0000) + return *(unsigned int *)ent->var & ent->mask; + else if (ent->mask & 0xff00) + return *(unsigned short *)ent->var & ent->mask; + else + return *(unsigned char *)ent->var & ent->mask; +} + +static void me_toggle_onoff(menu_entry *ent) +{ + // guess var size based on mask to avoid reading too much + if (ent->mask & 0xffff0000) + *(unsigned int *)ent->var ^= ent->mask; + else if (ent->mask & 0xff00) + *(unsigned short *)ent->var ^= ent->mask; + else + *(unsigned char *)ent->var ^= ent->mask; +} + static void me_draw(const menu_entry *entries, int sel, void (*draw_more)(void)) { const menu_entry *ent, *ent_sel = entries; @@ -477,7 +499,7 @@ static void me_draw(const menu_entry *entries, int sel, void (*draw_more)(void)) case MB_NONE: break; case MB_OPT_ONOFF: - text_out16(x + col2_offs, y, (*(int *)ent->var & ent->mask) ? "ON" : "OFF"); + text_out16(x + col2_offs, y, me_read_onoff(ent) ? "ON" : "OFF"); break; case MB_OPT_RANGE: text_out16(x + col2_offs, y, "%i", *(int *)ent->var); @@ -545,7 +567,7 @@ static int me_process(menu_entry *entry, int is_next, int is_lr) { case MB_OPT_ONOFF: case MB_OPT_CUSTONOFF: - *(int *)entry->var ^= entry->mask; + me_toggle_onoff(entry); return 1; case MB_OPT_RANGE: case MB_OPT_CUSTRANGE: @@ -560,11 +582,11 @@ static int me_process(menu_entry *entry, int is_next, int is_lr) names = (const char **)entry->data; for (c = 0; names[c] != NULL; c++) ; - *(int *)entry->var += is_next ? 1 : -1; - if (*(int *)entry->var < 0) - *(int *)entry->var = 0; - if (*(int *)entry->var >= c) - *(int *)entry->var = c - 1; + *(signed char *)entry->var += is_next ? 1 : -1; + if (*(signed char *)entry->var < 0) + *(signed char *)entry->var = 0; + if (*(signed char *)entry->var >= c) + *(signed char *)entry->var = c - 1; return 1; default: return 0; diff --git a/common/menu.h b/common/menu.h index f34d5a8..79b8278 100644 --- a/common/menu.h +++ b/common/menu.h @@ -61,8 +61,11 @@ typedef struct #define mee_onoff(name, id, var, mask) \ mee_onoff_h(name, id, var, mask, NULL) +#define mee_range_h(name, id, var, min, max, help) \ + { name, MB_OPT_RANGE, id, &(var), 0, min, max, 1, 1, 1, NULL, NULL, NULL, help } + #define mee_range(name, id, var, min, max) \ - { name, MB_OPT_RANGE, id, &(var), 0, min, max, 1, 1, 1, NULL, NULL, NULL, NULL } + mee_range_h(name, id, var, min, max, NULL) #define mee_range_hide(name, id, var, min, max) \ { name, MB_OPT_RANGE, id, &(var), 0, min, max, 0, 1, 0, NULL, NULL, NULL, NULL } -- 2.39.5