1
0
Fork 0
forked from len0rd/rockbox

Keyboard code cleanup & optimisation. Corrected potential overflow problem.

git-svn-id: svn://svn.rockbox.org/rockbox/trunk@7397 a1c6a512-1295-4272-9138-f99709370657
This commit is contained in:
Jens Arnold 2005-08-24 06:48:39 +00:00
parent 6c00e38665
commit c7e4f5f259
2 changed files with 40 additions and 74 deletions

View file

@ -102,7 +102,6 @@ int kbd_input(char* text, int buflen)
int editpos, curpos, leftpos; int editpos, curpos, leftpos;
unsigned short* line = kbd_setupkeys(page, &linelen); unsigned short* line = kbd_setupkeys(page, &linelen);
unsigned char temptext[12]; unsigned char temptext[12];
char c;
int button, lastbutton = 0; int button, lastbutton = 0;
@ -130,11 +129,11 @@ int kbd_input(char* text, int buflen)
/* Draw insert chars */ /* Draw insert chars */
temptext[0] = KEYBOARD_INSERT_LEFT; temptext[0] = KEYBOARD_INSERT_LEFT;
temptext[1] = line[x%linelen]; temptext[1] = line[x];
temptext[2] = KEYBOARD_INSERT_RIGHT; temptext[2] = KEYBOARD_INSERT_RIGHT;
for (i = 1; i < 8; i++) for (i = 1; i < 8; i++)
{ {
temptext[i+2] = line[(i+x)%linelen]; temptext[i+2] = line[(x+i)%linelen];
} }
temptext[i+2] = 0; temptext[i+2] = 0;
lcd_puts(1, 0, temptext); lcd_puts(1, 0, temptext);
@ -194,9 +193,7 @@ int kbd_input(char* text, int buflen)
} }
else else
{ {
if (x < linelen - 1) if (++x >= linelen)
x++;
else
x = 0; x = 0;
kbd_spellchar(line[x]); kbd_spellchar(line[x]);
} }
@ -214,9 +211,7 @@ int kbd_input(char* text, int buflen)
} }
else else
{ {
if (x) if (--x < 0)
x--;
else
x = linelen - 1; x = linelen - 1;
kbd_spellchar(line[x]); kbd_spellchar(line[x]);
} }
@ -242,20 +237,12 @@ int kbd_input(char* text, int buflen)
} }
else /* inserts the selected char */ else /* inserts the selected char */
{ {
if (len < buflen) if (len + 1 < buflen)
{ {
c = line[x]; for (i = len ; i > editpos; i--)
if (editpos == len) text[i] = text[i-1];
{ text[len+1] = 0;
text[len] = c; text[editpos] = line[x];
text[len+1] = 0;
}
else
{
for (i = len ; i >= editpos; i--)
text[i+1] = text[i];
text[editpos] = c;
}
editpos++; editpos++;
} }
} }

View file

@ -33,7 +33,7 @@
#define KEYBOARD_LINES 4 #define KEYBOARD_LINES 4
#define KEYBOARD_PAGES 3 #define KEYBOARD_PAGES 3
#define KEYBOARD_MARGIN 3
#if (CONFIG_KEYPAD == IRIVER_H100_PAD) || \ #if (CONFIG_KEYPAD == IRIVER_H100_PAD) || \
(CONFIG_KEYPAD == IRIVER_H300_PAD) (CONFIG_KEYPAD == IRIVER_H300_PAD)
@ -74,33 +74,22 @@
#endif #endif
static const char * const kbdpages[KEYBOARD_PAGES][KEYBOARD_LINES] = {
{ "ABCDEFG !?\" @#$%+'",
"HIJKLMN 789 &_()-`",
"OPQRSTU 456 §|{}/<",
"VWXYZ.,0123 ~=[]*>" },
static void kbd_setupkeys(const char* line[KEYBOARD_LINES], int page) { "abcdefg ¢£¤¥¦§©®¬",
{ "hijklmn «»°ºª¹²³¶",
switch (page) "opqrstu ¯±×÷¡¿µ·¨",
{ "vwxyz., ¼½¾ " },
case 0:
line[0] = "ABCDEFG !?\" @#$%+'";
line[1] = "HIJKLMN 789 &_()-`";
line[2] = "OPQRSTU 456 §|{}/<";
line[3] = "VWXYZ.,0123 ~=[]*>";
break;
case 1: { "ÀÁÂÃÄÅÆ ÌÍÎÏ ÈÉÊË",
line[0] = "abcdefg ¢£¤¥¦§©®¬"; "àáâãäåæ ìíîï èéêë",
line[1] = "hijklmn «»°ºª¹²³¶"; "ÓÒÔÕÖØ ÇÐÞÝß ÙÚÛÜ",
line[2] = "opqrstu ¯±×÷¡¿µ·¨"; "òóôõöø çðþýÿ ùúûü" },
line[3] = "vwxyz., ¼½¾ "; };
break;
case 2:
line[0] = "ÀÁÂÃÄÅÆ ÌÍÎÏ ÈÉÊË";
line[1] = "àáâãäåæ ìíîï èéêë";
line[2] = "ÓÒÔÕÖØ ÇÐÞÝß ÙÚÛÜ";
line[3] = "òóôõöø çðþýÿ ùúûü";
break;
}
}
/* helper function to spell a char if voice UI is enabled */ /* helper function to spell a char if voice UI is enabled */
static void kbd_spellchar(char c) static void kbd_spellchar(char c)
@ -121,18 +110,17 @@ int kbd_input(char* text, int buflen)
int font_w = 0, font_h = 0, i; int font_w = 0, font_h = 0, i;
int x = 0, y = 0; int x = 0, y = 0;
int main_x, main_y, max_chars, margin; int main_x, main_y, max_chars;
int status_y1, status_y2; int status_y1, status_y2;
int len; int len;
int editpos, curpos, leftpos; int editpos, curpos, leftpos;
bool redraw = true; bool redraw = true;
const char* line[KEYBOARD_LINES]; const char * const *line;
#ifdef KBD_MODES #ifdef KBD_MODES
bool line_edit = false; bool line_edit = false;
#endif #endif
char outline[256]; char outline[256];
char c = 0;
struct font* font = font_get(FONT_SYSFIXED); struct font* font = font_get(FONT_SYSFIXED);
int button, lastbutton = 0; int button, lastbutton = 0;
@ -140,8 +128,7 @@ int kbd_input(char* text, int buflen)
font_w = font->maxwidth; font_w = font->maxwidth;
font_h = font->height; font_h = font->height;
margin = 3; main_y = (KEYBOARD_LINES + 1) * font_h + (2*KEYBOARD_MARGIN);
main_y = (KEYBOARD_LINES + 1) * font_h + margin*2;
main_x = 0; main_x = 0;
status_y1 = LCD_HEIGHT - font_h; status_y1 = LCD_HEIGHT - font_h;
status_y2 = LCD_HEIGHT; status_y2 = LCD_HEIGHT;
@ -149,7 +136,7 @@ int kbd_input(char* text, int buflen)
editpos = strlen(text); editpos = strlen(text);
max_chars = LCD_WIDTH / font_w - 2; /* leave room for < and > */ max_chars = LCD_WIDTH / font_w - 2; /* leave room for < and > */
kbd_setupkeys(line, page); line = kbdpages[0];
if (global_settings.talk_menu) /* voice UI? */ if (global_settings.talk_menu) /* voice UI? */
talk_spell(text, true); /* spell initial text */ talk_spell(text, true); /* spell initial text */
@ -169,7 +156,7 @@ int kbd_input(char* text, int buflen)
lcd_putsxy(0, 8+i * font_h, line[i]); lcd_putsxy(0, 8+i * font_h, line[i]);
/* separator */ /* separator */
lcd_hline(0, LCD_WIDTH - 1, main_y - margin); lcd_hline(0, LCD_WIDTH - 1, main_y - KEYBOARD_MARGIN);
/* write out the text */ /* write out the text */
curpos = MIN(editpos, max_chars - MIN(len - editpos, 2)); curpos = MIN(editpos, max_chars - MIN(len - editpos, 2));
@ -221,10 +208,10 @@ int kbd_input(char* text, int buflen)
break; break;
#ifdef KBD_PAGE_FLIP #ifdef KBD_PAGE_FLIP
case KBD_PAGE_FLIP: case KBD_PAGE_FLIP:
if (++page == KEYBOARD_PAGES) if (++page == KEYBOARD_PAGES)
page = 0; page = 0;
kbd_setupkeys(line, page); line = kbdpages[page];
kbd_spellchar(line[y][x]); kbd_spellchar(line[y][x]);
break; break;
#endif #endif
@ -242,7 +229,7 @@ int kbd_input(char* text, int buflen)
} }
else else
#endif #endif
{ {
if (x < (int)strlen(line[y]) - 1) if (x < (int)strlen(line[y]) - 1)
x++; x++;
else else
@ -251,7 +238,7 @@ int kbd_input(char* text, int buflen)
#ifndef KBD_PAGE_FLIP /* no dedicated flip key - flip page on wrap */ #ifndef KBD_PAGE_FLIP /* no dedicated flip key - flip page on wrap */
if (++page == KEYBOARD_PAGES) if (++page == KEYBOARD_PAGES)
page = 0; page = 0;
kbd_setupkeys(line, page); line = kbdpages[page];
#endif #endif
} }
kbd_spellchar(line[y][x]); kbd_spellchar(line[y][x]);
@ -271,7 +258,7 @@ int kbd_input(char* text, int buflen)
} }
else else
#endif #endif
{ {
if (x) if (x)
x--; x--;
else else
@ -279,7 +266,7 @@ int kbd_input(char* text, int buflen)
#ifndef KBD_PAGE_FLIP /* no dedicated flip key - flip page on wrap */ #ifndef KBD_PAGE_FLIP /* no dedicated flip key - flip page on wrap */
if (--page < 0) if (--page < 0)
page = (KEYBOARD_PAGES-1); page = (KEYBOARD_PAGES-1);
kbd_setupkeys(line, page); line = kbdpages[page];
#endif #endif
x = strlen(line[y]) - 1; x = strlen(line[y]) - 1;
} }
@ -364,20 +351,12 @@ int kbd_input(char* text, int buflen)
else else
#endif #endif
{ {
if (len < buflen) if (len + 1 < buflen)
{ {
c = line[y][x]; for (i = len ; i > editpos; i--)
if (editpos == len) text[i] = text[i-1];
{ text[len+1] = 0;
text[len] = c; text[editpos] = line[y][x];
text[len+1] = 0;
}
else
{
for (i = len ; i >= editpos; i--)
text[i+1] = text[i];
text[editpos] = c;
}
editpos++; editpos++;
} }
} }