From: notaz Date: Sun, 25 Sep 2011 12:38:21 +0000 (+0000) Subject: menu: don't assume onoff vars are int X-Git-Tag: v1.85~124 X-Git-Url: https://notaz.gp2x.de/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4187c0ea575c33fe8c636ac6aa256ec634369478;p=picodrive.git menu: don't assume onoff vars are int git-svn-id: file:///home/notaz/opt/svn/PicoDrive@933 be3aeb3a-fb24-0410-a615-afba39da0efa --- diff --git a/platform/common/menu.c b/platform/common/menu.c index f55b48d4..1513d395 100644 --- a/platform/common/menu.c +++ b/platform/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/platform/common/menu.h b/platform/common/menu.h index f34d5a86..79b82783 100644 --- a/platform/common/menu.h +++ b/platform/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 }