Make kbd_input() show a cancel splash to indicate user abort better and for better...
[kugel-rb/myfork.git] / apps / recorder / keyboard.c
blob7eb798143e4f4f746027854385244536a5a32649
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"
42 #ifndef O_BINARY
43 #define O_BINARY 0
44 #endif
47 #define DEFAULT_MARGIN 6
48 #define KBD_BUF_SIZE 500
50 #if (CONFIG_KEYPAD == IRIVER_H100_PAD) || \
51 (CONFIG_KEYPAD == IRIVER_H300_PAD)
52 #define KBD_CURSOR_KEYS /* certain key combos move the cursor even if not
53 in line edit mode */
54 #define KBD_MODES /* I-Rivers can use picker, line edit and cursor keys */
55 #define KBD_MORSE_INPUT /* I-Rivers have a Morse input mode */
57 #elif CONFIG_KEYPAD == ONDIO_PAD /* restricted Ondio keypad */
58 #define KBD_MODES /* Ondio uses 2 modes, picker and line edit */
60 #elif (CONFIG_KEYPAD == IPOD_1G2G_PAD) || (CONFIG_KEYPAD == IPOD_3G_PAD) \
61 || (CONFIG_KEYPAD == IPOD_4G_PAD)
62 #define KBD_MODES /* iPod uses 2 modes, picker and line edit */
63 #define KBD_MORSE_INPUT
65 #elif CONFIG_KEYPAD == IRIVER_IFP7XX_PAD
66 #define KBD_MODES /* iFP7xx uses 2 modes, picker and line edit */
68 #elif (CONFIG_KEYPAD == IAUDIO_X5M5_PAD) || (CONFIG_KEYPAD == IAUDIO_M3_PAD)
69 #define KBD_MODES /* iAudios use 2 modes, picker and line edit */
71 #elif CONFIG_KEYPAD == IRIVER_H10_PAD
72 #define KBD_MODES /* iriver H10 uses 2 modes, picker and line edit */
73 #define KBD_MORSE_INPUT
75 #elif CONFIG_KEYPAD == GIGABEAT_PAD
76 #define KBD_CURSOR_KEYS
77 #define KBD_MODES
78 #define KBD_MORSE_INPUT
80 #elif CONFIG_KEYPAD == SANSA_E200_PAD \
81 || CONFIG_KEYPAD == SANSA_FUZE_PAD
82 #define KBD_CURSOR_KEYS
83 #define KBD_MODES
85 #elif CONFIG_KEYPAD == SANSA_C200_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 struct font* font;
102 int curfont;
103 int main_x;
104 int main_y;
105 int max_chars;
106 int max_chars_text;
107 int lines;
108 int pages;
109 int keyboard_margin;
110 int old_main_y;
111 int curpos;
112 int leftpos;
113 int page;
114 int x;
115 int y;
116 #ifdef KBD_MODES
117 bool line_edit;
118 #endif
119 bool hangul;
120 unsigned short hlead, hvowel, htail;
123 static struct keyboard_parameters kbd_param[NB_SCREENS];
124 static bool kbd_loaded = false;
126 #ifdef KBD_MORSE_INPUT
127 /* FIXME: We should put this to a configuration file. */
128 static const char *morse_alphabets =
129 "abcdefghijklmnopqrstuvwxyz1234567890,.?-@ ";
130 static const unsigned char morse_codes[] = {
131 0x05,0x18,0x1a,0x0c,0x02,0x12,0x0e,0x10,0x04,0x17,0x0d,0x14,0x07,
132 0x06,0x0f,0x16,0x1d,0x0a,0x08,0x03,0x09,0x11,0x0b,0x19,0x1b,0x1c,
133 0x2f,0x27,0x23,0x21,0x20,0x30,0x38,0x3c,0x3e,0x3f,
134 0x73,0x55,0x4c,0x61,0x5a,0x80 };
136 static bool morse_mode = false;
137 #endif
139 /* Loads a custom keyboard into memory
140 call with NULL to reset keyboard */
141 int load_kbd(unsigned char* filename)
143 int fd, l;
144 int i = 0;
145 unsigned char buf[4];
147 if (filename == NULL)
149 kbd_loaded = false;
150 return 0;
153 fd = open_utf8(filename, O_RDONLY|O_BINARY);
154 if (fd < 0)
155 return 1;
157 while (read(fd, buf, 1) == 1 && i < KBD_BUF_SIZE)
159 /* check how many bytes to read for this character */
160 static const unsigned char sizes[4] = { 0x80, 0xe0, 0xf0, 0xf5 };
161 size_t count;
163 for (count = 0; count < ARRAYLEN(sizes); count++)
165 if (buf[0] < sizes[count])
166 break;
169 if (count >= ARRAYLEN(sizes))
170 continue; /* Invalid size. */
172 if (read(fd, &buf[1], count) != (ssize_t)count)
174 close(fd);
175 kbd_loaded = false;
176 return 1;
179 FOR_NB_SCREENS(l)
180 utf8decode(buf, &kbd_param[l].kbd_buf[i]);
182 if (kbd_param[0].kbd_buf[i] != 0xFEFF &&
183 kbd_param[0].kbd_buf[i] != '\r') /*skip BOM & carriage returns */
185 i++;
189 close(fd);
190 kbd_loaded = true;
192 FOR_NB_SCREENS(l)
193 kbd_param[l].nchars = i;
195 return 0;
198 /* helper function to spell a char if voice UI is enabled */
199 static void kbd_spellchar(unsigned short c)
201 if (global_settings.talk_menu) /* voice UI? */
203 unsigned char tmp[5];
204 /* store char to pass to talk_spell */
205 unsigned char* utf8 = utf8encode(c, tmp);
206 *utf8 = 0;
208 if(c == ' ')
209 talk_id(VOICE_BLANK, false);
210 else
211 talk_spell(tmp, false);
215 #ifdef KBD_MODES
216 static void say_edit(void)
218 if(global_settings.talk_menu)
219 talk_id(VOICE_EDIT, false);
221 #endif
223 static void kbd_inschar(unsigned char* text, int buflen,
224 int* editpos, unsigned short ch)
226 int i, j, k, len;
227 unsigned char tmp[4];
228 unsigned char* utf8;
230 len = strlen(text);
231 k = utf8length(text);
232 utf8 = utf8encode(ch, tmp);
233 j = (long)utf8 - (long)tmp;
235 if (len + j < buflen)
237 for (i = len+j; k >= *editpos; i--)
239 text[i] = text[i-j];
240 if ((text[i] & MASK) != COMP)
241 k--;
244 while (j--)
245 text[i--] = tmp[j];
247 (*editpos)++;
251 static void kbd_delchar(unsigned char* text, int* editpos)
253 int i = 0;
254 unsigned char* utf8;
256 if (*editpos > 0)
258 utf8 = text + utf8seek(text, *editpos);
262 i++;
263 utf8--;
265 while ((*utf8 & MASK) == COMP);
267 while (utf8[i])
269 *utf8 = utf8[i];
270 utf8++;
273 *utf8 = 0;
274 (*editpos)--;
278 /* Lookup k value based on state of param (pm) */
279 static int get_param_k(const struct keyboard_parameters *pm)
281 return (pm->page*pm->lines + pm->y)*pm->max_chars + pm->x;
284 int kbd_input(char* text, int buflen)
286 bool done = false;
287 #ifdef CPU_ARM
288 /* This seems to keep the sizes for ARM way down */
289 struct keyboard_parameters * volatile param = kbd_param;
290 #else
291 struct keyboard_parameters * const param = kbd_param;
292 #endif
293 int l; /* screen loop variable */
294 int text_w = 0;
295 int editpos; /* Edit position on all screens */
296 const int statusbar_size = 0;
297 unsigned short ch;
298 unsigned char *utf8;
299 bool cur_blink = true; /* Cursor on/off flag */
300 int ret;
301 #ifdef KBD_MORSE_INPUT
302 bool morse_reading = false;
303 unsigned char morse_code = 0;
304 int morse_tick = 0;
305 char buf[2];
306 #endif
307 int oldbars = viewportmanager_set_statusbar(VP_SB_HIDE_ALL);
308 FOR_NB_SCREENS(l)
310 struct keyboard_parameters *pm = &param[l];
311 #if LCD_WIDTH >= 160 && LCD_HEIGHT >= 96
312 struct screen *sc = &screens[l];
314 if (sc->getwidth() >= 160 && sc->getheight() >= 96)
316 pm->default_kbd =
317 "ABCDEFG abcdefg !?\" @#$%+'\n"
318 "HIJKLMN hijklmn 789 &_()-`\n"
319 "OPQRSTU opqrstu 456 §|{}/<\n"
320 "VWXYZ., vwxyz.,0123 ~=[]*>\n"
321 "ÀÁÂÃÄÅÆ ÌÍÎÏ ÈÉÊË ¢£¤¥¦§©®\n"
322 "àáâãäåæ ìíîï èéêë «»°ºª¹²³\n"
323 "ÓÒÔÕÖØ ÇÐÞÝß ÙÚÛÜ ¯±×÷¡¿µ·\n"
324 "òóôõöø çðþýÿ ùúûü ¼½¾¬¶¨:;";
326 pm->DEFAULT_LINES = 8;
328 else
329 #endif /* LCD_WIDTH >= 160 && LCD_HEIGHT >= 96 */
331 pm->default_kbd =
332 "ABCDEFG !?\" @#$%+'\n"
333 "HIJKLMN 789 &_()-`\n"
334 "OPQRSTU 456 §|{}/<\n"
335 "VWXYZ.,0123 ~=[]*>\n"
337 "abcdefg ¢£¤¥¦§©®¬\n"
338 "hijklmn «»°ºª¹²³¶\n"
339 "opqrstu ¯±×÷¡¿µ·¨\n"
340 "vwxyz., :;¼½¾ \n"
342 "ÀÁÂÃÄÅÆ ÌÍÎÏ ÈÉÊË\n"
343 "àáâãäåæ ìíîï èéêë\n"
344 "ÓÒÔÕÖØ ÇÐÞÝß ÙÚÛÜ\n"
345 "òóôõöø çðþýÿ ùúûü";
347 pm->DEFAULT_LINES = 4;
351 char outline[256];
352 #ifdef HAVE_BUTTONBAR
353 struct gui_buttonbar buttonbar;
354 bool buttonbar_config = global_settings.buttonbar;
356 global_settings.buttonbar = true;
357 gui_buttonbar_init(&buttonbar);
359 FOR_NB_SCREENS(l)
360 gui_buttonbar_set_display(&buttonbar, &screens[l]);
361 #endif
363 FOR_NB_SCREENS(l)
365 struct keyboard_parameters *pm = &param[l];
367 if ( !kbd_loaded )
369 /* Copy default keyboard to buffer */
370 const unsigned char *p = pm->default_kbd;
371 int i = 0;
373 pm->curfont = FONT_SYSFIXED;
375 while (*p != 0)
376 p = utf8decode(p, &pm->kbd_buf[i++]);
378 pm->nchars = i;
380 else
382 pm->curfont = FONT_UI;
386 FOR_NB_SCREENS(l)
388 struct keyboard_parameters *pm = &param[l];
389 struct screen *sc = &screens[l];
390 int i, w;
392 pm->font = font_get(pm->curfont);
393 pm->font_h = pm->font->height;
395 /* check if FONT_UI fits the screen */
396 if (2*pm->font_h + 3 + statusbar_size +
397 BUTTONBAR_HEIGHT > sc->getheight())
399 pm->font = font_get(FONT_SYSFIXED);
400 pm->font_h = pm->font->height;
401 pm->curfont = FONT_SYSFIXED;
404 sc->setfont(pm->curfont);
405 pm->font_w = 0; /* reset font width */
406 /* find max width of keyboard glyphs */
407 for (i = 0; i < pm->nchars; i++)
409 w = font_get_width(pm->font, pm->kbd_buf[i]);
410 if ( w > pm->font_w )
411 pm->font_w = w;
414 /* Since we're going to be adding spaces, make sure that we check
415 * their width too */
416 w = font_get_width( pm->font, ' ' );
417 if ( w > pm->font_w )
418 pm->font_w = w;
421 FOR_NB_SCREENS(l)
423 struct keyboard_parameters *pm = &param[l];
424 struct screen *sc = &screens[l];
425 int i = 0;
427 /* Pad lines with spaces */
428 while (i < pm->nchars)
430 if (pm->kbd_buf[i] == '\n')
432 int k = sc->getwidth() / pm->font_w
433 - i % ( sc->getwidth() / pm->font_w ) - 1;
434 int j;
436 if (k == sc->getwidth() / pm->font_w - 1)
438 pm->nchars--;
440 for (j = i; j < pm->nchars; j++)
442 pm->kbd_buf[j] = pm->kbd_buf[j + 1];
445 else
447 if (pm->nchars + k - 1 >= KBD_BUF_SIZE)
448 { /* We don't want to overflow the buffer */
449 k = KBD_BUF_SIZE - pm->nchars;
452 for (j = pm->nchars + k - 1; j > i + k; j--)
454 pm->kbd_buf[j] = pm->kbd_buf[j-k];
457 pm->nchars += k;
458 k++;
460 while (k--)
462 pm->kbd_buf[i++] = ' ';
466 else
468 i++;
473 /* Find max width for text string */
474 utf8 = text;
475 FOR_NB_SCREENS(l)
477 struct keyboard_parameters *pm = &param[l];
478 struct screen *sc = &screens[l];
480 text_w = pm->font_w;
482 while (*utf8)
484 int w = font_get_width(pm->font, ch);
485 utf8 = (unsigned char*)utf8decode(utf8, &ch);
487 if (w > text_w)
488 text_w = w;
491 pm->max_chars_text = sc->getwidth() / text_w - 2;
493 /* Calculate keyboard grid size */
494 pm->max_chars = sc->getwidth() / pm->font_w;
496 if (!kbd_loaded)
498 pm->lines = pm->DEFAULT_LINES;
499 pm->keyboard_margin = DEFAULT_MARGIN;
501 else
503 pm->lines = (sc->getheight() - BUTTONBAR_HEIGHT - statusbar_size)
504 / pm->font_h - 1;
505 pm->keyboard_margin = sc->getheight() - BUTTONBAR_HEIGHT -
506 statusbar_size - (pm->lines+1)*pm->font_h;
508 if (pm->keyboard_margin < 3)
510 pm->lines--;
511 pm->keyboard_margin += pm->font_h;
514 if (pm->keyboard_margin > 6)
515 pm->keyboard_margin = 6;
518 pm->pages = (pm->nchars + (pm->lines*pm->max_chars-1))
519 / (pm->lines*pm->max_chars);
521 if (pm->pages == 1 && kbd_loaded)
522 pm->lines = (pm->nchars + pm->max_chars - 1) / pm->max_chars;
524 pm->main_y = pm->font_h*pm->lines + pm->keyboard_margin + statusbar_size;
525 pm->main_x = 0;
526 pm->keyboard_margin -= pm->keyboard_margin/2;
528 #ifdef KBD_MORSE_INPUT
529 pm->old_main_y = pm->main_y;
530 if (morse_mode)
531 pm->main_y = sc->getheight() - pm->font_h;
532 #endif
535 /* Initial edit position is after last character */
536 editpos = utf8length(text);
538 if (global_settings.talk_menu) /* voice UI? */
539 talk_spell(text, true); /* spell initial text */
542 while (!done)
544 /* These declarations are assigned to the screen on which the key
545 action occurred - pointers save a lot of space over array notation
546 when accessing the same array element countless times */
547 int button;
548 #if NB_SCREENS > 1
549 int button_screen;
550 #else
551 const int button_screen = 0;
552 #endif
553 struct keyboard_parameters *pm;
554 struct screen *sc;
556 int len_utf8 = utf8length(text);
558 FOR_NB_SCREENS(l)
559 screens[l].clear_display();
561 #ifdef KBD_MORSE_INPUT
562 if (morse_mode)
564 FOR_NB_SCREENS(l)
566 /* declare scoped pointers inside screen loops - hide the
567 declarations from previous block level */
568 const int w = 6; /* sysfixed font width */
569 struct keyboard_parameters *pm = &param[l];
570 struct screen *sc = &screens[l];
571 int i;
573 sc->setfont(FONT_SYSFIXED); /* Draw morse code screen with sysfont */
574 pm->x = 0;
575 pm->y = statusbar_size;
576 buf[1] = '\0';
578 /* Draw morse code table with code descriptions. */
579 for (i = 0; morse_alphabets[i] != '\0'; i++)
581 int morse_len;
582 int j;
584 buf[0] = morse_alphabets[i];
585 sc->putsxy(pm->x, pm->y, buf);
587 for (j = 0; (morse_codes[i] >> j) > 0x01; j++) ;
588 morse_len = j;
590 pm->x += w + 3;
591 for (j = 0; j < morse_len; j++)
593 if ((morse_codes[i] >> (morse_len-j-1)) & 0x01)
594 sc->fillrect(pm->x + j*4, pm->y + 2, 3, 4);
595 else
596 sc->fillrect(pm->x + j*4, pm->y + 3, 1, 2);
599 pm->x += w*5 - 3;
600 if (pm->x >= sc->getwidth() - w*6)
602 pm->x = 0;
603 pm->y += 8; /* sysfixed font height */
608 else
609 #endif /* KBD_MORSE_INPUT */
611 /* draw page */
612 FOR_NB_SCREENS(l)
614 struct keyboard_parameters *pm = &param[l];
615 struct screen *sc = &screens[l];
616 int i, j, k;
618 sc->setfont(pm->curfont);
620 k = pm->page*pm->max_chars*pm->lines;
622 for (i = j = 0; j < pm->lines && k < pm->nchars; k++)
624 int w;
625 utf8 = utf8encode(pm->kbd_buf[k], outline);
626 *utf8 = 0;
628 sc->getstringsize(outline, &w, NULL);
629 sc->putsxy(i*pm->font_w + (pm->font_w-w) / 2,
630 j*pm->font_h + statusbar_size, outline);
632 if (++i >= pm->max_chars)
634 i = 0;
635 j++;
641 /* separator */
642 FOR_NB_SCREENS(l)
644 struct keyboard_parameters *pm = &param[l];
645 struct screen *sc = &screens[l];
646 int i = 0, j = 0;
648 /* Clear text area one pixel above separator line so any overdraw
649 doesn't collide */
650 sc->set_drawmode(DRMODE_SOLID | DRMODE_INVERSEVID);
651 sc->fillrect(0, pm->main_y - pm->keyboard_margin - 1,
652 sc->getwidth(), pm->font_h + 4);
653 sc->set_drawmode(DRMODE_SOLID);
655 sc->hline(0, sc->getwidth() - 1, pm->main_y - pm->keyboard_margin);
657 /* write out the text */
658 sc->setfont(pm->curfont);
660 pm->curpos = MIN(editpos, pm->max_chars_text
661 - MIN(len_utf8 - editpos, 2));
662 pm->leftpos = editpos - pm->curpos;
663 utf8 = text + utf8seek(text, pm->leftpos);
665 text_w = pm->font_w;
667 while (*utf8 && i < pm->max_chars_text)
669 outline[j++] = *utf8++;
671 if ((*utf8 & MASK) != COMP)
673 int w;
674 outline[j] = 0;
675 j=0;
676 i++;
677 sc->getstringsize(outline, &w, NULL);
678 sc->putsxy(i*text_w + (text_w-w)/2, pm->main_y, outline);
682 if (pm->leftpos > 0)
684 /* Draw nicer bitmap arrow if room, else settle for "<". */
685 if (text_w >= 6 && pm->font_h >= 8)
687 screen_put_iconxy(sc, (text_w - 6) / 2,
688 pm->main_y + (pm->font_h - 8) / 2 ,
689 Icon_Reverse_Cursor);
691 else
693 int w;
694 sc->getstringsize("<", &w, NULL);
695 sc->putsxy(text_w - w, pm->main_y, "<");
699 if (len_utf8 - pm->leftpos > pm->max_chars_text)
701 /* Draw nicer bitmap arrow if room, else settle for ">". */
702 if (text_w >= 6 && pm->font_h >= 8)
704 screen_put_iconxy(sc, sc->getwidth() - text_w +
705 (text_w - 6) / 2,
706 pm->main_y + (pm->font_h - 8) / 2,
707 Icon_Cursor);
709 else
711 sc->putsxy(sc->getwidth() - text_w, pm->main_y, ">");
715 /* cursor */
716 i = (pm->curpos + 1) * text_w;
718 if (cur_blink)
719 sc->vline(i, pm->main_y, pm->main_y + pm->font_h - 1);
721 if (pm->hangul) /* draw underbar */
722 sc->hline(pm->curpos*text_w, (pm->curpos+1)*text_w,
723 pm->main_y + pm->font_h - 1);
726 cur_blink = !cur_blink;
728 #ifdef HAVE_BUTTONBAR
729 /* draw the button bar */
730 gui_buttonbar_set(&buttonbar, "Shift", "OK", "Del");
731 gui_buttonbar_draw(&buttonbar);
732 #endif
734 FOR_NB_SCREENS(l)
736 struct keyboard_parameters *pm = &param[l];
737 struct screen *sc = &screens[l];
739 sc->set_drawmode(DRMODE_COMPLEMENT);
740 #ifdef KBD_MODES
741 if (pm->line_edit)
742 sc->fillrect(0, pm->main_y - pm->keyboard_margin + 2,
743 sc->getwidth(), pm->font_h + 2);
744 else /* highlight the key that has focus */
745 #endif
746 sc->fillrect(pm->font_w*pm->x,
747 statusbar_size + pm->font_h*pm->y,
748 pm->font_w, pm->font_h);
749 sc->set_drawmode(DRMODE_SOLID);
752 FOR_NB_SCREENS(l)
753 screens[l].update();
755 button = get_action(CONTEXT_KEYBOARD, HZ/2);
756 #if NB_SCREENS > 1
757 button_screen = (get_action_statuscode(NULL) & ACTION_REMOTE) ? 1 : 0;
758 #endif
759 pm = &param[button_screen];
760 sc = &screens[button_screen];
762 #if defined KBD_MORSE_INPUT && !defined KBD_MODES
763 if (morse_mode)
765 /* Remap some buttons for morse mode. */
766 if (button == ACTION_KBD_LEFT)
767 button = ACTION_KBD_CURSOR_LEFT;
768 if (button == ACTION_KBD_RIGHT)
769 button = ACTION_KBD_CURSOR_RIGHT;
771 #endif
773 switch ( button )
775 case ACTION_KBD_ABORT:
776 FOR_NB_SCREENS(l)
777 screens[l].setfont(FONT_UI);
779 #ifdef HAVE_BUTTONBAR
780 global_settings.buttonbar=buttonbar_config;
781 #endif
782 viewportmanager_set_statusbar(oldbars);
783 ret = -1; done = true;
784 break;
786 case ACTION_KBD_PAGE_FLIP:
788 int k;
789 #ifdef KBD_MORSE_INPUT
790 if (morse_mode)
791 break;
792 #endif
793 if (++pm->page >= pm->pages)
794 pm->page = 0;
796 k = get_param_k(pm);
797 kbd_spellchar(pm->kbd_buf[k]);
798 break;
801 #ifdef KBD_MORSE_INPUT
802 case ACTION_KBD_MORSE_INPUT:
803 morse_mode = !morse_mode;
805 FOR_NB_SCREENS(l)
807 struct keyboard_parameters *pm = &param[l];
808 struct screen *sc = &screens[l];
810 pm->x = pm->y = pm->page = 0;
812 if (morse_mode)
814 pm->old_main_y = pm->main_y;
815 pm->main_y = sc->getheight() - pm->font_h;
817 else
819 pm->main_y = pm->old_main_y;
822 /* FIXME: We should talk something like Morse mode.. */
823 break;
824 #endif /* KBD_MORSE_INPUT */
826 case ACTION_KBD_RIGHT:
827 #ifdef KBD_MODES
828 #ifdef KBD_MORSE_INPUT
829 /* allow cursor change in non line edit morse mode */
830 if (pm->line_edit || morse_mode)
831 #else
832 /* right doubles as cursor_right in line_edit */
833 if (pm->line_edit)
834 #endif
836 pm->hangul = false;
838 if (editpos < len_utf8)
840 int c = utf8seek(text, ++editpos);
841 kbd_spellchar(text[c]);
843 #if CONFIG_CODEC == SWCODEC
844 else if (global_settings.talk_menu)
845 pcmbuf_beep(1000, 150, 1500);
846 #endif
848 else
849 #endif /* KBD_MODES */
851 int k;
852 #ifdef KBD_MORSE_INPUT
853 if (morse_mode)
854 break;
855 #endif
856 if (++pm->x >= pm->max_chars)
858 #ifndef KBD_PAGE_FLIP
859 /* no dedicated flip key - flip page on wrap */
860 if (++pm->page >= pm->pages)
861 pm->page = 0;
862 #endif
863 pm->x = 0;
866 k = get_param_k(pm);
867 kbd_spellchar(pm->kbd_buf[k]);
869 break;
871 case ACTION_KBD_LEFT:
872 #ifdef KBD_MODES
873 #ifdef KBD_MORSE_INPUT
874 /* allow cursor change in non line edit morse mode */
875 if (pm->line_edit || morse_mode)
876 #else
877 /* left doubles as cursor_left in line_edit */
878 if (pm->line_edit)
879 #endif
881 pm->hangul = false;
883 if (editpos > 0)
885 int c = utf8seek(text, --editpos);
886 kbd_spellchar(text[c]);
888 #if CONFIG_CODEC == SWCODEC
889 else if (global_settings.talk_menu)
890 pcmbuf_beep(1000, 150, 1500);
891 #endif
893 else
894 #endif /* KBD_MODES */
896 int k;
897 #ifdef KBD_MORSE_INPUT
898 if (morse_mode)
899 break;
900 #endif
901 if (--pm->x < 0)
903 #ifndef KBD_PAGE_FLIP
904 /* no dedicated flip key - flip page on wrap */
905 if (--pm->page < 0)
906 pm->page = pm->pages - 1;
907 #endif
908 pm->x = pm->max_chars - 1;
911 k = get_param_k(pm);
912 kbd_spellchar(pm->kbd_buf[k]);
914 break;
916 case ACTION_KBD_DOWN:
917 #ifdef KBD_MORSE_INPUT
918 #ifdef KBD_MODES
919 if (morse_mode)
921 pm->line_edit = !pm->line_edit;
922 if(pm->line_edit)
923 say_edit();
925 else
926 #else
927 if (morse_mode)
928 break;
929 #endif
930 #endif /* KBD_MORSE_INPUT */
932 #ifdef KBD_MODES
933 if (pm->line_edit)
935 pm->y = 0;
936 pm->line_edit = false;
938 else
939 #endif
940 if (++pm->y >= pm->lines)
941 #ifdef KBD_MODES
943 pm->line_edit = true;
944 say_edit();
946 #else
947 pm->y = 0;
948 #endif
950 #ifdef KBD_MODES
951 if (!pm->line_edit)
952 #endif
954 int k = get_param_k(pm);
955 kbd_spellchar(pm->kbd_buf[k]);
957 break;
959 case ACTION_KBD_UP:
960 #ifdef KBD_MORSE_INPUT
961 #ifdef KBD_MODES
962 if (morse_mode)
964 pm->line_edit = !pm->line_edit;
965 if(pm->line_edit)
966 say_edit();
968 else
969 #else
970 if (morse_mode)
971 break;
972 #endif
973 #endif /* KBD_MORSE_INPUT */
975 #ifdef KBD_MODES
976 if (pm->line_edit)
978 pm->y = pm->lines - 1;
979 pm->line_edit = false;
981 else
982 #endif
983 if (--pm->y < 0)
984 #ifdef KBD_MODES
986 pm->line_edit = true;
987 say_edit();
989 #else
990 pm->y = pm->lines - 1;
991 #endif
993 #ifdef KBD_MODES
994 if (!pm->line_edit)
995 #endif
997 int k = get_param_k(pm);
998 kbd_spellchar(pm->kbd_buf[k]);
1000 break;
1002 case ACTION_KBD_DONE:
1003 /* accepts what was entered and continues */
1004 ret = 0;
1005 done = true;
1006 break;
1008 #ifdef KBD_MORSE_INPUT
1009 case ACTION_KBD_MORSE_SELECT:
1010 if (morse_mode && morse_reading)
1012 morse_code <<= 1;
1013 if ((current_tick - morse_tick) > HZ/5)
1014 morse_code |= 0x01;
1017 break;
1018 #endif /* KBD_MORSE_INPUT */
1020 case ACTION_KBD_SELECT:
1021 case ACTION_KBD_SELECT_REM:
1022 #ifdef KBD_MORSE_INPUT
1023 #ifdef KBD_MODES
1024 if (morse_mode && !pm->line_edit)
1025 #else
1026 if (morse_mode)
1027 #endif
1029 morse_tick = current_tick;
1031 if (!morse_reading)
1033 morse_reading = true;
1034 morse_code = 1;
1036 break;
1038 #endif /* KBD_MORSE_INPUT */
1040 /* inserts the selected char */
1041 #ifdef KBD_MODES
1042 if (pm->line_edit)
1043 { /* select doubles as backspace in line_edit */
1044 if (pm->hangul)
1046 if (pm->htail)
1047 pm->htail = 0;
1048 else if (pm->hvowel)
1049 pm->hvowel = 0;
1050 else
1051 pm->hangul = false;
1054 kbd_delchar(text, &editpos);
1056 if (pm->hangul)
1058 if (pm->hvowel)
1059 ch = hangul_join(pm->hlead, pm->hvowel, pm->htail);
1060 else
1061 ch = pm->hlead;
1062 kbd_inschar(text, buflen, &editpos, ch);
1065 else
1066 #endif /* KBD_MODES */
1068 /* find input char */
1069 int k = get_param_k(pm);
1070 ch = (k < pm->nchars) ? pm->kbd_buf[k] : ' ';
1072 /* check for hangul input */
1073 if (ch >= 0x3131 && ch <= 0x3163)
1075 unsigned short tmp;
1077 if (!pm->hangul)
1079 pm->hlead = pm->hvowel = pm->htail = 0;
1080 pm->hangul = true;
1083 if (!pm->hvowel)
1085 pm->hvowel = ch;
1087 else if (!pm->htail)
1089 pm->htail = ch;
1091 else
1092 { /* previous hangul complete */
1093 /* check whether tail is actually lead of next char */
1094 tmp = hangul_join(pm->htail, ch, 0);
1096 if (tmp != 0xfffd)
1098 tmp = hangul_join(pm->hlead, pm->hvowel, 0);
1099 kbd_delchar(text, &editpos);
1100 kbd_inschar(text, buflen, &editpos, tmp);
1101 /* insert dummy char */
1102 kbd_inschar(text, buflen, &editpos, ' ');
1103 pm->hlead = pm->htail;
1104 pm->hvowel = ch;
1105 pm->htail = 0;
1107 else
1109 pm->hvowel = pm->htail = 0;
1110 pm->hlead = ch;
1114 /* combine into hangul */
1115 tmp = hangul_join(pm->hlead, pm->hvowel, pm->htail);
1117 if (tmp != 0xfffd)
1119 kbd_delchar(text, &editpos);
1120 ch = tmp;
1122 else
1124 pm->hvowel = pm->htail = 0;
1125 pm->hlead = ch;
1128 else
1130 pm->hangul = false;
1133 /* insert char */
1134 kbd_inschar(text, buflen, &editpos, ch);
1137 if (global_settings.talk_menu) /* voice UI? */
1138 talk_spell(text, false);
1140 /* speak revised text */
1141 break;
1143 #if !defined (KBD_MODES) || defined (KBD_CURSOR_KEYS)
1144 case ACTION_KBD_BACKSPACE:
1145 if (pm->hangul)
1147 if (pm->htail)
1148 pm->htail = 0;
1149 else if (pm->hvowel)
1150 pm->hvowel = 0;
1151 else
1152 pm->hangul = false;
1155 kbd_delchar(text, &editpos);
1157 if (pm->hangul)
1159 if (pm->hvowel)
1160 ch = hangul_join(pm->hlead, pm->hvowel, pm->htail);
1161 else
1162 ch = pm->hlead;
1163 kbd_inschar(text, buflen, &editpos, ch);
1166 if (global_settings.talk_menu) /* voice UI? */
1167 talk_spell(text, false); /* speak revised text */
1168 break;
1170 case ACTION_KBD_CURSOR_RIGHT:
1171 pm->hangul = false;
1173 if (editpos < len_utf8)
1175 int c = utf8seek(text, ++editpos);
1176 kbd_spellchar(text[c]);
1178 #if CONFIG_CODEC == SWCODEC
1179 else if (global_settings.talk_menu)
1180 pcmbuf_beep(1000, 150, 1500);
1181 #endif
1182 break;
1184 case ACTION_KBD_CURSOR_LEFT:
1185 pm->hangul = false;
1187 if (editpos > 0)
1189 int c = utf8seek(text, --editpos);
1190 kbd_spellchar(text[c]);
1192 #if CONFIG_CODEC == SWCODEC
1193 else if (global_settings.talk_menu)
1194 pcmbuf_beep(1000, 150, 1500);
1195 #endif
1196 break;
1197 #endif /* !defined (KBD_MODES) || defined (KBD_CURSOR_KEYS) */
1199 case BUTTON_NONE:
1200 #ifdef KBD_MORSE_INPUT
1201 if (morse_reading)
1203 int j;
1204 logf("Morse: 0x%02x", morse_code);
1205 morse_reading = false;
1207 for (j = 0; morse_alphabets[j] != '\0'; j++)
1209 if (morse_codes[j] == morse_code)
1210 break ;
1213 if (morse_alphabets[j] == '\0')
1215 logf("Morse code not found");
1216 break ;
1219 /* turn off hangul input */
1220 FOR_NB_SCREENS(l)
1221 param[l].hangul = false;
1222 kbd_inschar(text, buflen, &editpos, morse_alphabets[j]);
1224 if (global_settings.talk_menu) /* voice UI? */
1225 talk_spell(text, false); /* speak revised text */
1227 #endif /* KBD_MORSE_INPUT */
1228 break;
1230 default:
1231 if (default_event_handler(button) == SYS_USB_CONNECTED)
1233 FOR_NB_SCREENS(l)
1234 screens[l].setfont(FONT_SYSFIXED);
1236 break;
1238 } /* end switch */
1240 if (button != BUTTON_NONE)
1242 cur_blink = true;
1246 #ifdef HAVE_BUTTONBAR
1247 global_settings.buttonbar = buttonbar_config;
1248 #endif
1250 FOR_NB_SCREENS(l)
1251 screens[l].setfont(FONT_UI);
1252 viewportmanager_set_statusbar(oldbars);
1254 if (ret < 0)
1255 splash(HZ/2, ID2P(LANG_CANCEL));
1256 return ret;