Merge duplicating code to move cursor left/right.
[kugel-rb.git] / apps / recorder / keyboard.c
blobe6a3c927e3011e02138422bc17ba498cf65d6df6
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 by Björn Stenberg
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
21 #include "kernel.h"
22 #include "system.h"
23 #include "version.h"
24 #include <string.h>
25 #include "font.h"
26 #include "screens.h"
27 #include "talk.h"
28 #include "settings.h"
29 #include "misc.h"
30 #include "rbunicode.h"
31 #include "buttonbar.h"
32 #include "logf.h"
33 #include "hangul.h"
34 #include "action.h"
35 #include "icon.h"
36 #include "pcmbuf.h"
37 #include "lang.h"
38 #include "keyboard.h"
39 #include "viewport.h"
40 #include "file.h"
41 #include "splash.h"
42 #include "appevents.h"
44 #ifndef O_BINARY
45 #define O_BINARY 0
46 #endif
49 #define DEFAULT_MARGIN 6
50 #define KBD_BUF_SIZE 500
52 #if (CONFIG_KEYPAD == IRIVER_H100_PAD) || \
53 (CONFIG_KEYPAD == IRIVER_H300_PAD)
54 #define KBD_CURSOR_KEYS /* certain key combos move the cursor even if not
55 in line edit mode */
56 #define KBD_MODES /* I-Rivers can use picker, line edit and cursor keys */
57 #define KBD_MORSE_INPUT /* I-Rivers have a Morse input mode */
59 #elif CONFIG_KEYPAD == ONDIO_PAD /* restricted Ondio keypad */
60 #define KBD_MODES /* Ondio uses 2 modes, picker and line edit */
62 #elif (CONFIG_KEYPAD == IPOD_1G2G_PAD) || (CONFIG_KEYPAD == IPOD_3G_PAD) \
63 || (CONFIG_KEYPAD == IPOD_4G_PAD)
64 #define KBD_MODES /* iPod uses 2 modes, picker and line edit */
65 #define KBD_MORSE_INPUT
67 #elif CONFIG_KEYPAD == IRIVER_IFP7XX_PAD
68 #define KBD_MODES /* iFP7xx uses 2 modes, picker and line edit */
70 #elif (CONFIG_KEYPAD == IAUDIO_X5M5_PAD) || (CONFIG_KEYPAD == IAUDIO_M3_PAD)
71 #define KBD_MODES /* iAudios use 2 modes, picker and line edit */
73 #elif CONFIG_KEYPAD == IRIVER_H10_PAD
74 #define KBD_MODES /* iriver H10 uses 2 modes, picker and line edit */
75 #define KBD_MORSE_INPUT
77 #elif CONFIG_KEYPAD == GIGABEAT_PAD
78 #define KBD_CURSOR_KEYS
79 #define KBD_MODES
80 #define KBD_MORSE_INPUT
82 #elif CONFIG_KEYPAD == SANSA_E200_PAD \
83 || CONFIG_KEYPAD == SANSA_FUZE_PAD \
84 || CONFIG_KEYPAD == SANSA_C200_PAD \
85 || CONFIG_KEYPAD == SAMSUNG_YH_PAD
86 #define KBD_CURSOR_KEYS
87 #define KBD_MODES
89 #elif CONFIG_KEYPAD == MROBE100_PAD
90 #define KBD_MORSE_INPUT
91 #endif
93 struct keyboard_parameters
95 const unsigned char* default_kbd;
96 int DEFAULT_LINES;
97 unsigned short kbd_buf[KBD_BUF_SIZE];
98 int nchars;
99 int font_w;
100 int font_h;
101 int text_w;
102 struct font* font;
103 int curfont;
104 int main_x;
105 int main_y;
106 int max_chars;
107 int max_chars_text;
108 int lines;
109 int pages;
110 int keyboard_margin;
111 int old_main_y;
112 int curpos;
113 int leftpos;
114 int page;
115 int x;
116 int y;
117 #ifdef KBD_MODES
118 bool line_edit;
119 #endif
120 bool hangul;
121 unsigned short hlead, hvowel, htail;
124 static struct keyboard_parameters kbd_param[NB_SCREENS];
125 static bool kbd_loaded = false;
127 #ifdef KBD_MORSE_INPUT
128 /* FIXME: We should put this to a configuration file. */
129 static const char *morse_alphabets =
130 "abcdefghijklmnopqrstuvwxyz1234567890,.?-@ ";
131 static const unsigned char morse_codes[] = {
132 0x05,0x18,0x1a,0x0c,0x02,0x12,0x0e,0x10,0x04,0x17,0x0d,0x14,0x07,
133 0x06,0x0f,0x16,0x1d,0x0a,0x08,0x03,0x09,0x11,0x0b,0x19,0x1b,0x1c,
134 0x2f,0x27,0x23,0x21,0x20,0x30,0x38,0x3c,0x3e,0x3f,
135 0x73,0x55,0x4c,0x61,0x5a,0x80 };
137 static bool morse_mode = false;
138 #endif
140 /* Loads a custom keyboard into memory
141 call with NULL to reset keyboard */
142 int load_kbd(unsigned char* filename)
144 int fd, l;
145 int i = 0;
146 unsigned char buf[4];
148 FOR_NB_SCREENS(l)
150 struct keyboard_parameters *pm = &kbd_param[l];
151 pm->x = pm->y = pm->page = 0;
154 if (filename == NULL)
156 kbd_loaded = false;
157 return 0;
160 fd = open_utf8(filename, O_RDONLY|O_BINARY);
161 if (fd < 0)
162 return 1;
164 while (read(fd, buf, 1) == 1 && i < KBD_BUF_SIZE)
166 /* check how many bytes to read for this character */
167 static const unsigned char sizes[4] = { 0x80, 0xe0, 0xf0, 0xf5 };
168 size_t count;
169 unsigned short ch;
171 for (count = 0; count < ARRAYLEN(sizes); count++)
173 if (buf[0] < sizes[count])
174 break;
177 if (count >= ARRAYLEN(sizes))
178 continue; /* Invalid size. */
180 if (read(fd, &buf[1], count) != (ssize_t)count)
182 close(fd);
183 kbd_loaded = false;
184 return 1;
187 utf8decode(buf, &ch);
188 FOR_NB_SCREENS(l)
189 kbd_param[l].kbd_buf[i] = ch;
191 if (ch != 0xFEFF && ch != '\r') /*skip BOM & carriage returns */
193 i++;
197 close(fd);
198 kbd_loaded = true;
200 FOR_NB_SCREENS(l)
201 kbd_param[l].nchars = i;
203 return 0;
206 /* helper function to spell a char if voice UI is enabled */
207 static void kbd_spellchar(unsigned short c)
209 if (global_settings.talk_menu) /* voice UI? */
211 unsigned char tmp[5];
212 /* store char to pass to talk_spell */
213 unsigned char* utf8 = utf8encode(c, tmp);
214 *utf8 = 0;
216 if(c == ' ')
217 talk_id(VOICE_BLANK, false);
218 else
219 talk_spell(tmp, false);
223 #ifdef KBD_MODES
224 static void say_edit(void)
226 if(global_settings.talk_menu)
227 talk_id(VOICE_EDIT, false);
229 #endif
231 static void kbd_inschar(unsigned char* text, int buflen,
232 int* editpos, unsigned short ch)
234 int i, j, k, len;
235 unsigned char tmp[4];
236 unsigned char* utf8;
238 len = strlen(text);
239 k = utf8length(text);
240 utf8 = utf8encode(ch, tmp);
241 j = (long)utf8 - (long)tmp;
243 if (len + j < buflen)
245 for (i = len+j; k >= *editpos; i--)
247 text[i] = text[i-j];
248 if ((text[i] & MASK) != COMP)
249 k--;
252 while (j--)
253 text[i--] = tmp[j];
255 (*editpos)++;
259 static void kbd_delchar(unsigned char* text, int* editpos)
261 int i = 0;
262 unsigned char* utf8;
264 if (*editpos > 0)
266 utf8 = text + utf8seek(text, *editpos);
270 i++;
271 utf8--;
273 while ((*utf8 & MASK) == COMP);
275 while (utf8[i])
277 *utf8 = utf8[i];
278 utf8++;
281 *utf8 = 0;
282 (*editpos)--;
286 /* Lookup k value based on state of param (pm) */
287 static unsigned short get_kbd_ch(const struct keyboard_parameters *pm)
289 int k = (pm->page*pm->lines + pm->y)*pm->max_chars + pm->x;
290 return (k < pm->nchars)? pm->kbd_buf[k]: ' ';
293 int kbd_input(char* text, int buflen)
295 bool done = false;
296 #ifdef CPU_ARM
297 /* This seems to keep the sizes for ARM way down */
298 struct keyboard_parameters * volatile param = kbd_param;
299 #else
300 struct keyboard_parameters * const param = kbd_param;
301 #endif
302 int l; /* screen loop variable */
303 int editpos; /* Edit position on all screens */
304 const int statusbar_size = 0;
305 unsigned short ch;
306 unsigned char *utf8;
307 bool cur_blink = true; /* Cursor on/off flag */
308 int ret = 0; /* assume success */
309 #ifdef KBD_MORSE_INPUT
310 bool morse_reading = false;
311 unsigned char morse_code = 0;
312 int morse_tick = 0;
313 #endif
314 int oldbars = viewportmanager_set_statusbar(VP_SB_HIDE_ALL);
315 FOR_NB_SCREENS(l)
317 struct keyboard_parameters *pm = &param[l];
318 #if LCD_WIDTH >= 160 && LCD_HEIGHT >= 96
319 struct screen *sc = &screens[l];
321 if (sc->getwidth() >= 160 && sc->getheight() >= 96)
323 pm->default_kbd =
324 "ABCDEFG abcdefg !?\" @#$%+'\n"
325 "HIJKLMN hijklmn 789 &_()-`\n"
326 "OPQRSTU opqrstu 456 §|{}/<\n"
327 "VWXYZ., vwxyz.,0123 ~=[]*>\n"
328 "ÀÁÂÃÄÅÆ ÌÍÎÏ ÈÉÊË ¢£¤¥¦§©®\n"
329 "àáâãäåæ ìíîï èéêë «»°ºª¹²³\n"
330 "ÓÒÔÕÖØ ÇÐÞÝß ÙÚÛÜ ¯±×÷¡¿µ·\n"
331 "òóôõöø çðþýÿ ùúûü ¼½¾¬¶¨:;";
333 pm->DEFAULT_LINES = 8;
335 else
336 #endif /* LCD_WIDTH >= 160 && LCD_HEIGHT >= 96 */
338 pm->default_kbd =
339 "ABCDEFG !?\" @#$%+'\n"
340 "HIJKLMN 789 &_()-`\n"
341 "OPQRSTU 456 §|{}/<\n"
342 "VWXYZ.,0123 ~=[]*>\n"
344 "abcdefg ¢£¤¥¦§©®¬\n"
345 "hijklmn «»°ºª¹²³¶\n"
346 "opqrstu ¯±×÷¡¿µ·¨\n"
347 "vwxyz., :;¼½¾ \n"
349 "ÀÁÂÃÄÅÆ ÌÍÎÏ ÈÉÊË\n"
350 "àáâãäåæ ìíîï èéêë\n"
351 "ÓÒÔÕÖØ ÇÐÞÝß ÙÚÛÜ\n"
352 "òóôõöø çðþýÿ ùúûü";
354 pm->DEFAULT_LINES = 4;
358 char outline[256];
359 #ifdef HAVE_BUTTONBAR
360 struct gui_buttonbar buttonbar;
361 bool buttonbar_config = global_settings.buttonbar;
363 global_settings.buttonbar = true;
364 gui_buttonbar_init(&buttonbar);
366 FOR_NB_SCREENS(l)
367 gui_buttonbar_set_display(&buttonbar, &screens[l]);
368 #endif
370 FOR_NB_SCREENS(l)
372 struct keyboard_parameters *pm = &param[l];
374 if ( !kbd_loaded )
376 /* Copy default keyboard to buffer */
377 const unsigned char *p = pm->default_kbd;
378 int i = 0;
380 pm->curfont = FONT_SYSFIXED;
382 while (*p != 0)
383 p = utf8decode(p, &pm->kbd_buf[i++]);
385 pm->nchars = i;
387 else
389 pm->curfont = FONT_UI;
393 FOR_NB_SCREENS(l)
395 struct keyboard_parameters *pm = &param[l];
396 struct screen *sc = &screens[l];
397 int i, w;
399 pm->font = font_get(pm->curfont);
400 pm->font_h = pm->font->height;
402 /* check if FONT_UI fits the screen */
403 if (2*pm->font_h + 3 + statusbar_size +
404 BUTTONBAR_HEIGHT > sc->getheight())
406 pm->font = font_get(FONT_SYSFIXED);
407 pm->font_h = pm->font->height;
408 pm->curfont = FONT_SYSFIXED;
411 /* find max width of keyboard glyphs.
412 * since we're going to be adding spaces,
413 * max width is at least their width */
414 pm->font_w = font_get_width(pm->font, ' ');
415 for (i = 0; i < pm->nchars; i++)
417 if (pm->kbd_buf[i] != '\n')
419 w = font_get_width(pm->font, pm->kbd_buf[i]);
420 if (w > pm->font_w)
421 pm->font_w = w;
426 FOR_NB_SCREENS(l)
428 struct keyboard_parameters *pm = &param[l];
429 struct screen *sc = &screens[l];
430 int i = 0;
432 pm->max_chars = sc->getwidth() / pm->font_w;
434 /* Pad lines with spaces */
435 while (i < pm->nchars)
437 if (pm->kbd_buf[i] == '\n')
439 int k = pm->max_chars - i % ( pm->max_chars ) - 1;
440 int j;
442 if (k == pm->max_chars - 1)
444 pm->nchars--;
446 for (j = i; j < pm->nchars; j++)
448 pm->kbd_buf[j] = pm->kbd_buf[j + 1];
451 else
453 if (pm->nchars + k - 1 >= KBD_BUF_SIZE)
454 { /* We don't want to overflow the buffer */
455 k = KBD_BUF_SIZE - pm->nchars;
458 for (j = pm->nchars + k - 1; j > i + k; j--)
460 pm->kbd_buf[j] = pm->kbd_buf[j-k];
463 pm->nchars += k;
464 k++;
466 while (k--)
468 pm->kbd_buf[i++] = ' ';
472 else
474 i++;
479 /* Find max width for text string */
480 FOR_NB_SCREENS(l)
482 struct keyboard_parameters *pm = &param[l];
483 struct screen *sc = &screens[l];
484 int icon_w, sc_w, sc_h;
486 pm->text_w = pm->font_w;
488 utf8 = text;
489 while (*utf8)
491 int w;
492 utf8 = (unsigned char*)utf8decode(utf8, &ch);
493 w = font_get_width(pm->font, ch);
494 if (w > pm->text_w)
495 pm->text_w = w;
498 icon_w = get_icon_width(l);
499 sc_w = sc->getwidth();
500 sc_h = sc->getheight();
501 pm->max_chars_text = (sc_w - icon_w * 2 - 2) / pm->text_w;
502 if(pm->max_chars_text < 3 && icon_w > pm->text_w)
503 pm->max_chars_text = sc_w / pm->text_w - 2;
505 if (!kbd_loaded)
507 pm->lines = pm->DEFAULT_LINES;
508 pm->keyboard_margin = DEFAULT_MARGIN;
510 else
512 pm->lines = (sc_h - BUTTONBAR_HEIGHT - statusbar_size)
513 / pm->font_h - 1;
514 pm->keyboard_margin = sc_h - BUTTONBAR_HEIGHT - statusbar_size
515 - (pm->lines+1)*pm->font_h;
517 if (pm->keyboard_margin < 3)
519 pm->lines--;
520 pm->keyboard_margin += pm->font_h;
523 if (pm->keyboard_margin > 6)
524 pm->keyboard_margin = 6;
527 pm->pages = (pm->nchars + (pm->lines*pm->max_chars-1))
528 / (pm->lines*pm->max_chars);
530 if (pm->pages == 1 && kbd_loaded)
531 pm->lines = (pm->nchars + pm->max_chars - 1) / pm->max_chars;
533 pm->main_y = pm->font_h*pm->lines + pm->keyboard_margin + statusbar_size;
534 pm->main_x = 0;
535 pm->keyboard_margin -= pm->keyboard_margin/2;
537 #ifdef KBD_MORSE_INPUT
538 pm->old_main_y = pm->main_y;
539 if (morse_mode)
540 pm->main_y = sc_h - pm->font_h;
541 #endif
544 /* Initial edit position is after last character */
545 editpos = utf8length(text);
547 if (global_settings.talk_menu) /* voice UI? */
548 talk_spell(text, true); /* spell initial text */
551 while (!done)
553 /* These declarations are assigned to the screen on which the key
554 action occurred - pointers save a lot of space over array notation
555 when accessing the same array element countless times */
556 int button;
557 #if NB_SCREENS > 1
558 int button_screen;
559 #else
560 const int button_screen = 0;
561 #endif
562 struct keyboard_parameters *pm;
563 struct screen *sc;
565 int len_utf8 = utf8length(text);
567 FOR_NB_SCREENS(l)
568 screens[l].clear_display();
570 #ifdef KBD_MORSE_INPUT
571 if (morse_mode)
573 FOR_NB_SCREENS(l)
575 /* declare scoped pointers inside screen loops - hide the
576 declarations from previous block level */
577 const int w = 6; /* sysfixed font width */
578 struct screen *sc = &screens[l];
579 int i, x, y;
581 /* Draw morse code screen with sysfont */
582 sc->setfont(FONT_SYSFIXED);
583 x = 0;
584 y = statusbar_size;
585 outline[1] = '\0';
587 /* Draw morse code table with code descriptions. */
588 for (i = 0; morse_alphabets[i] != '\0'; i++)
590 int morse_len;
591 int j;
593 outline[0] = morse_alphabets[i];
594 sc->putsxy(x, y, outline);
596 for (j = 0; (morse_codes[i] >> j) > 0x01; j++) ;
597 morse_len = j;
599 x += w + 3;
600 for (j = 0; j < morse_len; j++)
602 if ((morse_codes[i] >> (morse_len-j-1)) & 0x01)
603 sc->fillrect(x + j*4, y + 2, 3, 4);
604 else
605 sc->fillrect(x + j*4, y + 3, 1, 2);
608 x += w*5 - 3;
609 if (x + w*6 >= sc->getwidth())
611 x = 0;
612 y += 8; /* sysfixed font height */
617 else
618 #endif /* KBD_MORSE_INPUT */
620 /* draw page */
621 FOR_NB_SCREENS(l)
623 struct keyboard_parameters *pm = &param[l];
624 struct screen *sc = &screens[l];
625 int i, j, k;
627 sc->setfont(pm->curfont);
629 k = pm->page*pm->max_chars*pm->lines;
631 for (i = j = 0; j < pm->lines && k < pm->nchars; k++)
633 int w;
634 utf8 = utf8encode(pm->kbd_buf[k], outline);
635 *utf8 = 0;
637 sc->getstringsize(outline, &w, NULL);
638 sc->putsxy(i*pm->font_w + (pm->font_w-w) / 2,
639 j*pm->font_h + statusbar_size, outline);
641 if (++i >= pm->max_chars)
643 i = 0;
644 j++;
650 FOR_NB_SCREENS(l)
652 struct keyboard_parameters *pm = &param[l];
653 struct screen *sc = &screens[l];
654 int i = 0, j = 0, icon_w;
655 int text_w = pm->text_w;
656 int sc_w = sc->getwidth();
657 int text_margin = (sc_w - text_w * pm->max_chars_text) / 2;
659 /* Clear text area one pixel above separator line so any overdraw
660 doesn't collide */
661 sc->set_drawmode(DRMODE_SOLID | DRMODE_INVERSEVID);
662 sc->fillrect(0, pm->main_y - pm->keyboard_margin - 1,
663 sc_w, pm->font_h + 4);
664 sc->set_drawmode(DRMODE_SOLID);
666 sc->hline(0, sc_w - 1, pm->main_y - pm->keyboard_margin);
668 /* write out the text */
669 sc->setfont(pm->curfont);
671 pm->curpos = MIN(editpos, pm->max_chars_text
672 - MIN(len_utf8 - editpos, 2));
673 pm->leftpos = editpos - pm->curpos;
674 utf8 = text + utf8seek(text, pm->leftpos);
676 while (*utf8 && i < pm->max_chars_text)
678 outline[j++] = *utf8++;
680 if ((*utf8 & MASK) != COMP)
682 int w;
683 outline[j] = 0;
684 j=0;
685 sc->getstringsize(outline, &w, NULL);
686 sc->putsxy(text_margin + i*text_w + (text_w-w)/2,
687 pm->main_y, outline);
688 i++;
692 icon_w = get_icon_width(l);
693 if (pm->leftpos > 0)
695 /* Draw nicer bitmap arrow if room, else settle for "<". */
696 if (text_margin >= icon_w)
698 screen_put_icon_with_offset(sc, 0, 0,
699 (text_margin - icon_w) / 2,
700 pm->main_y, Icon_Reverse_Cursor);
702 else
704 int w;
705 sc->getstringsize("<", &w, NULL);
706 sc->putsxy(text_margin - w, pm->main_y, "<");
710 if (len_utf8 - pm->leftpos > pm->max_chars_text)
712 /* Draw nicer bitmap arrow if room, else settle for ">". */
713 if (text_margin >= icon_w)
715 screen_put_icon_with_offset(sc, 0, 0,
716 sc_w - (text_margin + icon_w) / 2,
717 pm->main_y, Icon_Cursor);
719 else
721 sc->putsxy(sc_w - text_margin, pm->main_y, ">");
725 /* cursor */
726 i = text_margin + pm->curpos * text_w;
728 if (cur_blink)
729 sc->vline(i, pm->main_y, pm->main_y + pm->font_h - 1);
731 if (pm->hangul) /* draw underbar */
732 sc->hline(i - text_w, i, pm->main_y + pm->font_h - 1);
735 cur_blink = !cur_blink;
737 #ifdef HAVE_BUTTONBAR
738 /* draw the button bar */
739 gui_buttonbar_set(&buttonbar, "Shift", "OK", "Del");
740 gui_buttonbar_draw(&buttonbar);
741 #endif
743 FOR_NB_SCREENS(l)
745 struct keyboard_parameters *pm = &param[l];
746 struct screen *sc = &screens[l];
748 sc->set_drawmode(DRMODE_COMPLEMENT);
749 #ifdef KBD_MODES
750 if (pm->line_edit)
751 sc->fillrect(0, pm->main_y - pm->keyboard_margin + 2,
752 sc->getwidth(), pm->font_h + 2);
753 else /* highlight the key that has focus */
754 #endif
755 #ifdef KBD_MORSE_INPUT
756 if(!morse_mode)
757 #endif
758 sc->fillrect(pm->font_w*pm->x,
759 statusbar_size + pm->font_h*pm->y,
760 pm->font_w, pm->font_h);
761 sc->set_drawmode(DRMODE_SOLID);
764 FOR_NB_SCREENS(l)
765 screens[l].update();
767 button = get_action(CONTEXT_KEYBOARD, HZ/2);
768 #if NB_SCREENS > 1
769 button_screen = (get_action_statuscode(NULL) & ACTION_REMOTE) ? 1 : 0;
770 #endif
771 pm = &param[button_screen];
772 sc = &screens[button_screen];
774 #if defined(KBD_MODES) || defined(KBD_MORSE_INPUT)
775 /* Remap some buttons to allow to move
776 * cursor in line edit mode and morse mode. */
777 #if defined(KBD_MODES) && defined(KBD_MORSE_INPUT)
778 if (pm->line_edit || morse_mode)
779 #elif defined(KBD_MODES)
780 if (pm->line_edit)
781 #else /* defined(KBD_MORSE_INPUT) */
782 if (morse_mode)
783 #endif
785 if (button == ACTION_KBD_LEFT)
786 button = ACTION_KBD_CURSOR_LEFT;
787 if (button == ACTION_KBD_RIGHT)
788 button = ACTION_KBD_CURSOR_RIGHT;
790 #endif /* defined(KBD_MODES) || defined(KBD_MORSE_INPUT) */
792 switch ( button )
794 case ACTION_KBD_ABORT:
795 ret = -1;
796 done = true;
797 break;
799 case ACTION_KBD_PAGE_FLIP:
801 #ifdef KBD_MORSE_INPUT
802 if (morse_mode)
803 break;
804 #endif
805 if (++pm->page >= pm->pages)
806 pm->page = 0;
808 ch = get_kbd_ch(pm);
809 kbd_spellchar(ch);
810 break;
813 #ifdef KBD_MORSE_INPUT
814 case ACTION_KBD_MORSE_INPUT:
815 morse_mode = !morse_mode;
817 FOR_NB_SCREENS(l)
819 struct keyboard_parameters *pm = &param[l];
820 struct screen *sc = &screens[l];
822 if (morse_mode)
824 pm->old_main_y = pm->main_y;
825 pm->main_y = sc->getheight() - pm->font_h;
827 else
829 pm->main_y = pm->old_main_y;
832 /* FIXME: We should talk something like Morse mode.. */
833 break;
834 #endif /* KBD_MORSE_INPUT */
836 case ACTION_KBD_RIGHT:
837 if (++pm->x >= pm->max_chars)
839 #ifndef KBD_PAGE_FLIP
840 /* no dedicated flip key - flip page on wrap */
841 if (++pm->page >= pm->pages)
842 pm->page = 0;
843 #endif
844 pm->x = 0;
847 ch = get_kbd_ch(pm);
848 kbd_spellchar(ch);
849 break;
851 case ACTION_KBD_LEFT:
852 if (--pm->x < 0)
854 #ifndef KBD_PAGE_FLIP
855 /* no dedicated flip key - flip page on wrap */
856 if (--pm->page < 0)
857 pm->page = pm->pages - 1;
858 #endif
859 pm->x = pm->max_chars - 1;
862 ch = get_kbd_ch(pm);
863 kbd_spellchar(ch);
864 break;
866 case ACTION_KBD_DOWN:
867 #ifdef KBD_MORSE_INPUT
868 #ifdef KBD_MODES
869 if (morse_mode)
871 pm->line_edit = !pm->line_edit;
872 if(pm->line_edit)
873 say_edit();
875 else
876 #else
877 if (morse_mode)
878 break;
879 #endif
880 #endif /* KBD_MORSE_INPUT */
882 #ifdef KBD_MODES
883 if (pm->line_edit)
885 pm->y = 0;
886 pm->line_edit = false;
888 else
889 #endif
890 if (++pm->y >= pm->lines)
891 #ifdef KBD_MODES
893 pm->line_edit = true;
894 say_edit();
896 #else
897 pm->y = 0;
898 #endif
900 #ifdef KBD_MODES
901 if (!pm->line_edit)
902 #endif
904 ch = get_kbd_ch(pm);
905 kbd_spellchar(ch);
907 break;
909 case ACTION_KBD_UP:
910 #ifdef KBD_MORSE_INPUT
911 #ifdef KBD_MODES
912 if (morse_mode)
914 pm->line_edit = !pm->line_edit;
915 if(pm->line_edit)
916 say_edit();
918 else
919 #else
920 if (morse_mode)
921 break;
922 #endif
923 #endif /* KBD_MORSE_INPUT */
925 #ifdef KBD_MODES
926 if (pm->line_edit)
928 pm->y = pm->lines - 1;
929 pm->line_edit = false;
931 else
932 #endif
933 if (--pm->y < 0)
934 #ifdef KBD_MODES
936 pm->line_edit = true;
937 say_edit();
939 #else
940 pm->y = pm->lines - 1;
941 #endif
943 #ifdef KBD_MODES
944 if (!pm->line_edit)
945 #endif
947 ch = get_kbd_ch(pm);
948 kbd_spellchar(ch);
950 break;
952 case ACTION_KBD_DONE:
953 /* accepts what was entered and continues */
954 ret = 0;
955 done = true;
956 break;
958 #ifdef KBD_MORSE_INPUT
959 case ACTION_KBD_MORSE_SELECT:
960 if (morse_mode && morse_reading)
962 morse_code <<= 1;
963 if ((current_tick - morse_tick) > HZ/5)
964 morse_code |= 0x01;
967 break;
968 #endif /* KBD_MORSE_INPUT */
970 case ACTION_KBD_SELECT:
971 #ifdef KBD_MORSE_INPUT
972 #ifdef KBD_MODES
973 if (morse_mode && !pm->line_edit)
974 #else
975 if (morse_mode)
976 #endif
978 morse_tick = current_tick;
980 if (!morse_reading)
982 morse_reading = true;
983 morse_code = 1;
985 break;
987 #endif /* KBD_MORSE_INPUT */
989 /* inserts the selected char */
990 #ifdef KBD_MODES
991 if (pm->line_edit)
992 { /* select doubles as backspace in line_edit */
993 if (pm->hangul)
995 if (pm->htail)
996 pm->htail = 0;
997 else if (pm->hvowel)
998 pm->hvowel = 0;
999 else
1000 pm->hangul = false;
1003 kbd_delchar(text, &editpos);
1005 if (pm->hangul)
1007 if (pm->hvowel)
1008 ch = hangul_join(pm->hlead, pm->hvowel, pm->htail);
1009 else
1010 ch = pm->hlead;
1011 kbd_inschar(text, buflen, &editpos, ch);
1014 else
1015 #endif /* KBD_MODES */
1017 /* find input char */
1018 ch = get_kbd_ch(pm);
1020 /* check for hangul input */
1021 if (ch >= 0x3131 && ch <= 0x3163)
1023 unsigned short tmp;
1025 if (!pm->hangul)
1027 pm->hlead = pm->hvowel = pm->htail = 0;
1028 pm->hangul = true;
1031 if (!pm->hvowel)
1033 pm->hvowel = ch;
1035 else if (!pm->htail)
1037 pm->htail = ch;
1039 else
1040 { /* previous hangul complete */
1041 /* check whether tail is actually lead of next char */
1042 tmp = hangul_join(pm->htail, ch, 0);
1044 if (tmp != 0xfffd)
1046 tmp = hangul_join(pm->hlead, pm->hvowel, 0);
1047 kbd_delchar(text, &editpos);
1048 kbd_inschar(text, buflen, &editpos, tmp);
1049 /* insert dummy char */
1050 kbd_inschar(text, buflen, &editpos, ' ');
1051 pm->hlead = pm->htail;
1052 pm->hvowel = ch;
1053 pm->htail = 0;
1055 else
1057 pm->hvowel = pm->htail = 0;
1058 pm->hlead = ch;
1062 /* combine into hangul */
1063 tmp = hangul_join(pm->hlead, pm->hvowel, pm->htail);
1065 if (tmp != 0xfffd)
1067 kbd_delchar(text, &editpos);
1068 ch = tmp;
1070 else
1072 pm->hvowel = pm->htail = 0;
1073 pm->hlead = ch;
1076 else
1078 pm->hangul = false;
1081 /* insert char */
1082 kbd_inschar(text, buflen, &editpos, ch);
1085 if (global_settings.talk_menu) /* voice UI? */
1086 talk_spell(text, false);
1088 /* speak revised text */
1089 break;
1091 #if !defined (KBD_MODES) || defined (KBD_CURSOR_KEYS)
1092 case ACTION_KBD_BACKSPACE:
1093 if (pm->hangul)
1095 if (pm->htail)
1096 pm->htail = 0;
1097 else if (pm->hvowel)
1098 pm->hvowel = 0;
1099 else
1100 pm->hangul = false;
1103 kbd_delchar(text, &editpos);
1105 if (pm->hangul)
1107 if (pm->hvowel)
1108 ch = hangul_join(pm->hlead, pm->hvowel, pm->htail);
1109 else
1110 ch = pm->hlead;
1111 kbd_inschar(text, buflen, &editpos, ch);
1114 if (global_settings.talk_menu) /* voice UI? */
1115 talk_spell(text, false); /* speak revised text */
1116 break;
1117 #endif /* !defined (KBD_MODES) || defined (KBD_CURSOR_KEYS) */
1119 case ACTION_KBD_CURSOR_RIGHT:
1120 pm->hangul = false;
1122 if (editpos < len_utf8)
1124 int c = utf8seek(text, ++editpos);
1125 kbd_spellchar(text[c]);
1127 #if CONFIG_CODEC == SWCODEC
1128 else if (global_settings.talk_menu)
1129 pcmbuf_beep(1000, 150, 1500);
1130 #endif
1131 break;
1133 case ACTION_KBD_CURSOR_LEFT:
1134 pm->hangul = false;
1136 if (editpos > 0)
1138 int c = utf8seek(text, --editpos);
1139 kbd_spellchar(text[c]);
1141 #if CONFIG_CODEC == SWCODEC
1142 else if (global_settings.talk_menu)
1143 pcmbuf_beep(1000, 150, 1500);
1144 #endif
1145 break;
1147 case BUTTON_NONE:
1148 #ifdef KBD_MORSE_INPUT
1149 if (morse_reading)
1151 int j;
1152 logf("Morse: 0x%02x", morse_code);
1153 morse_reading = false;
1155 for (j = 0; morse_alphabets[j] != '\0'; j++)
1157 if (morse_codes[j] == morse_code)
1158 break ;
1161 if (morse_alphabets[j] == '\0')
1163 logf("Morse code not found");
1164 break ;
1167 /* turn off hangul input */
1168 FOR_NB_SCREENS(l)
1169 param[l].hangul = false;
1170 kbd_inschar(text, buflen, &editpos, morse_alphabets[j]);
1172 if (global_settings.talk_menu) /* voice UI? */
1173 talk_spell(text, false); /* speak revised text */
1175 #endif /* KBD_MORSE_INPUT */
1176 break;
1178 default:
1179 if (default_event_handler(button) == SYS_USB_CONNECTED)
1181 FOR_NB_SCREENS(l)
1182 screens[l].setfont(FONT_SYSFIXED);
1184 break;
1186 } /* end switch */
1188 if (button != BUTTON_NONE)
1190 cur_blink = true;
1194 #ifdef HAVE_BUTTONBAR
1195 global_settings.buttonbar = buttonbar_config;
1196 #endif
1198 if (ret < 0)
1199 splash(HZ/2, ID2P(LANG_CANCEL));
1201 FOR_NB_SCREENS(l)
1202 screens[l].setfont(FONT_UI);
1203 viewportmanager_set_statusbar(oldbars);
1204 send_event(GUI_EVENT_REFRESH, NULL);
1206 return ret;