Fix FS#7511, icon could overlap the line to edit.
[kugel-rb.git] / apps / recorder / keyboard.c
blobd87272cfeecc9938abaf1a35f631aafd9f7591ed
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 sc->setfont(pm->curfont);
412 /* find max width of keyboard glyphs.
413 * since we're going to be adding spaces,
414 * max width is at least their width */
415 pm->font_w = font_get_width(pm->font, ' ');
416 for (i = 0; i < pm->nchars; i++)
418 if (pm->kbd_buf[i] != '\n')
420 w = font_get_width(pm->font, pm->kbd_buf[i]);
421 if (w > pm->font_w)
422 pm->font_w = w;
427 FOR_NB_SCREENS(l)
429 struct keyboard_parameters *pm = &param[l];
430 struct screen *sc = &screens[l];
431 int i = 0;
433 pm->max_chars = sc->getwidth() / pm->font_w;
435 /* Pad lines with spaces */
436 while (i < pm->nchars)
438 if (pm->kbd_buf[i] == '\n')
440 int k = pm->max_chars - i % ( pm->max_chars ) - 1;
441 int j;
443 if (k == pm->max_chars - 1)
445 pm->nchars--;
447 for (j = i; j < pm->nchars; j++)
449 pm->kbd_buf[j] = pm->kbd_buf[j + 1];
452 else
454 if (pm->nchars + k - 1 >= KBD_BUF_SIZE)
455 { /* We don't want to overflow the buffer */
456 k = KBD_BUF_SIZE - pm->nchars;
459 for (j = pm->nchars + k - 1; j > i + k; j--)
461 pm->kbd_buf[j] = pm->kbd_buf[j-k];
464 pm->nchars += k;
465 k++;
467 while (k--)
469 pm->kbd_buf[i++] = ' ';
473 else
475 i++;
480 /* Find max width for text string */
481 FOR_NB_SCREENS(l)
483 struct keyboard_parameters *pm = &param[l];
484 struct screen *sc = &screens[l];
485 int icon_w;
487 pm->text_w = pm->font_w;
489 utf8 = text;
490 while (*utf8)
492 int w;
493 utf8 = (unsigned char*)utf8decode(utf8, &ch);
494 w = font_get_width(pm->font, ch);
495 if (w > pm->text_w)
496 pm->text_w = w;
499 icon_w = get_icon_width(l);
500 pm->max_chars_text = (sc->getwidth() - icon_w * 2)
501 / pm->text_w;
502 if(pm->max_chars_text < 3 && icon_w > pm->text_w)
503 pm->max_chars_text = sc->getwidth() / 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->getheight() - BUTTONBAR_HEIGHT - statusbar_size)
513 / pm->font_h - 1;
514 pm->keyboard_margin = sc->getheight() - BUTTONBAR_HEIGHT -
515 statusbar_size - (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->getheight() - 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(pm->curpos*text_w, (pm->curpos+1)*text_w,
733 pm->main_y + pm->font_h - 1);
736 cur_blink = !cur_blink;
738 #ifdef HAVE_BUTTONBAR
739 /* draw the button bar */
740 gui_buttonbar_set(&buttonbar, "Shift", "OK", "Del");
741 gui_buttonbar_draw(&buttonbar);
742 #endif
744 FOR_NB_SCREENS(l)
746 struct keyboard_parameters *pm = &param[l];
747 struct screen *sc = &screens[l];
749 sc->set_drawmode(DRMODE_COMPLEMENT);
750 #ifdef KBD_MODES
751 if (pm->line_edit)
752 sc->fillrect(0, pm->main_y - pm->keyboard_margin + 2,
753 sc->getwidth(), pm->font_h + 2);
754 else /* highlight the key that has focus */
755 #endif
756 #ifdef KBD_MORSE_INPUT
757 if(!morse_mode)
758 #endif
759 sc->fillrect(pm->font_w*pm->x,
760 statusbar_size + pm->font_h*pm->y,
761 pm->font_w, pm->font_h);
762 sc->set_drawmode(DRMODE_SOLID);
765 FOR_NB_SCREENS(l)
766 screens[l].update();
768 button = get_action(CONTEXT_KEYBOARD, HZ/2);
769 #if NB_SCREENS > 1
770 button_screen = (get_action_statuscode(NULL) & ACTION_REMOTE) ? 1 : 0;
771 #endif
772 pm = &param[button_screen];
773 sc = &screens[button_screen];
775 #if defined KBD_MORSE_INPUT && !defined KBD_MODES
776 if (morse_mode)
778 /* Remap some buttons for morse mode. */
779 if (button == ACTION_KBD_LEFT)
780 button = ACTION_KBD_CURSOR_LEFT;
781 if (button == ACTION_KBD_RIGHT)
782 button = ACTION_KBD_CURSOR_RIGHT;
784 #endif
786 switch ( button )
788 case ACTION_KBD_ABORT:
789 ret = -1;
790 done = true;
791 break;
793 case ACTION_KBD_PAGE_FLIP:
795 #ifdef KBD_MORSE_INPUT
796 if (morse_mode)
797 break;
798 #endif
799 if (++pm->page >= pm->pages)
800 pm->page = 0;
802 ch = get_kbd_ch(pm);
803 kbd_spellchar(ch);
804 break;
807 #ifdef KBD_MORSE_INPUT
808 case ACTION_KBD_MORSE_INPUT:
809 morse_mode = !morse_mode;
811 FOR_NB_SCREENS(l)
813 struct keyboard_parameters *pm = &param[l];
814 struct screen *sc = &screens[l];
816 if (morse_mode)
818 pm->old_main_y = pm->main_y;
819 pm->main_y = sc->getheight() - pm->font_h;
821 else
823 pm->main_y = pm->old_main_y;
826 /* FIXME: We should talk something like Morse mode.. */
827 break;
828 #endif /* KBD_MORSE_INPUT */
830 case ACTION_KBD_RIGHT:
831 #ifdef KBD_MODES
832 #ifdef KBD_MORSE_INPUT
833 /* allow cursor change in non line edit morse mode */
834 if (pm->line_edit || morse_mode)
835 #else
836 /* right doubles as cursor_right in line_edit */
837 if (pm->line_edit)
838 #endif
840 pm->hangul = false;
842 if (editpos < len_utf8)
844 int c = utf8seek(text, ++editpos);
845 kbd_spellchar(text[c]);
847 #if CONFIG_CODEC == SWCODEC
848 else if (global_settings.talk_menu)
849 pcmbuf_beep(1000, 150, 1500);
850 #endif
852 else
853 #endif /* KBD_MODES */
855 #ifdef KBD_MORSE_INPUT
856 if (morse_mode)
857 break;
858 #endif
859 if (++pm->x >= pm->max_chars)
861 #ifndef KBD_PAGE_FLIP
862 /* no dedicated flip key - flip page on wrap */
863 if (++pm->page >= pm->pages)
864 pm->page = 0;
865 #endif
866 pm->x = 0;
869 ch = get_kbd_ch(pm);
870 kbd_spellchar(ch);
872 break;
874 case ACTION_KBD_LEFT:
875 #ifdef KBD_MODES
876 #ifdef KBD_MORSE_INPUT
877 /* allow cursor change in non line edit morse mode */
878 if (pm->line_edit || morse_mode)
879 #else
880 /* left doubles as cursor_left in line_edit */
881 if (pm->line_edit)
882 #endif
884 pm->hangul = false;
886 if (editpos > 0)
888 int c = utf8seek(text, --editpos);
889 kbd_spellchar(text[c]);
891 #if CONFIG_CODEC == SWCODEC
892 else if (global_settings.talk_menu)
893 pcmbuf_beep(1000, 150, 1500);
894 #endif
896 else
897 #endif /* KBD_MODES */
899 #ifdef KBD_MORSE_INPUT
900 if (morse_mode)
901 break;
902 #endif
903 if (--pm->x < 0)
905 #ifndef KBD_PAGE_FLIP
906 /* no dedicated flip key - flip page on wrap */
907 if (--pm->page < 0)
908 pm->page = pm->pages - 1;
909 #endif
910 pm->x = pm->max_chars - 1;
913 ch = get_kbd_ch(pm);
914 kbd_spellchar(ch);
916 break;
918 case ACTION_KBD_DOWN:
919 #ifdef KBD_MORSE_INPUT
920 #ifdef KBD_MODES
921 if (morse_mode)
923 pm->line_edit = !pm->line_edit;
924 if(pm->line_edit)
925 say_edit();
927 else
928 #else
929 if (morse_mode)
930 break;
931 #endif
932 #endif /* KBD_MORSE_INPUT */
934 #ifdef KBD_MODES
935 if (pm->line_edit)
937 pm->y = 0;
938 pm->line_edit = false;
940 else
941 #endif
942 if (++pm->y >= pm->lines)
943 #ifdef KBD_MODES
945 pm->line_edit = true;
946 say_edit();
948 #else
949 pm->y = 0;
950 #endif
952 #ifdef KBD_MODES
953 if (!pm->line_edit)
954 #endif
956 ch = get_kbd_ch(pm);
957 kbd_spellchar(ch);
959 break;
961 case ACTION_KBD_UP:
962 #ifdef KBD_MORSE_INPUT
963 #ifdef KBD_MODES
964 if (morse_mode)
966 pm->line_edit = !pm->line_edit;
967 if(pm->line_edit)
968 say_edit();
970 else
971 #else
972 if (morse_mode)
973 break;
974 #endif
975 #endif /* KBD_MORSE_INPUT */
977 #ifdef KBD_MODES
978 if (pm->line_edit)
980 pm->y = pm->lines - 1;
981 pm->line_edit = false;
983 else
984 #endif
985 if (--pm->y < 0)
986 #ifdef KBD_MODES
988 pm->line_edit = true;
989 say_edit();
991 #else
992 pm->y = pm->lines - 1;
993 #endif
995 #ifdef KBD_MODES
996 if (!pm->line_edit)
997 #endif
999 ch = get_kbd_ch(pm);
1000 kbd_spellchar(ch);
1002 break;
1004 case ACTION_KBD_DONE:
1005 /* accepts what was entered and continues */
1006 ret = 0;
1007 done = true;
1008 break;
1010 #ifdef KBD_MORSE_INPUT
1011 case ACTION_KBD_MORSE_SELECT:
1012 if (morse_mode && morse_reading)
1014 morse_code <<= 1;
1015 if ((current_tick - morse_tick) > HZ/5)
1016 morse_code |= 0x01;
1019 break;
1020 #endif /* KBD_MORSE_INPUT */
1022 case ACTION_KBD_SELECT:
1023 case ACTION_KBD_SELECT_REM:
1024 #ifdef KBD_MORSE_INPUT
1025 #ifdef KBD_MODES
1026 if (morse_mode && !pm->line_edit)
1027 #else
1028 if (morse_mode)
1029 #endif
1031 morse_tick = current_tick;
1033 if (!morse_reading)
1035 morse_reading = true;
1036 morse_code = 1;
1038 break;
1040 #endif /* KBD_MORSE_INPUT */
1042 /* inserts the selected char */
1043 #ifdef KBD_MODES
1044 if (pm->line_edit)
1045 { /* select doubles as backspace in line_edit */
1046 if (pm->hangul)
1048 if (pm->htail)
1049 pm->htail = 0;
1050 else if (pm->hvowel)
1051 pm->hvowel = 0;
1052 else
1053 pm->hangul = false;
1056 kbd_delchar(text, &editpos);
1058 if (pm->hangul)
1060 if (pm->hvowel)
1061 ch = hangul_join(pm->hlead, pm->hvowel, pm->htail);
1062 else
1063 ch = pm->hlead;
1064 kbd_inschar(text, buflen, &editpos, ch);
1067 else
1068 #endif /* KBD_MODES */
1070 /* find input char */
1071 ch = get_kbd_ch(pm);
1073 /* check for hangul input */
1074 if (ch >= 0x3131 && ch <= 0x3163)
1076 unsigned short tmp;
1078 if (!pm->hangul)
1080 pm->hlead = pm->hvowel = pm->htail = 0;
1081 pm->hangul = true;
1084 if (!pm->hvowel)
1086 pm->hvowel = ch;
1088 else if (!pm->htail)
1090 pm->htail = ch;
1092 else
1093 { /* previous hangul complete */
1094 /* check whether tail is actually lead of next char */
1095 tmp = hangul_join(pm->htail, ch, 0);
1097 if (tmp != 0xfffd)
1099 tmp = hangul_join(pm->hlead, pm->hvowel, 0);
1100 kbd_delchar(text, &editpos);
1101 kbd_inschar(text, buflen, &editpos, tmp);
1102 /* insert dummy char */
1103 kbd_inschar(text, buflen, &editpos, ' ');
1104 pm->hlead = pm->htail;
1105 pm->hvowel = ch;
1106 pm->htail = 0;
1108 else
1110 pm->hvowel = pm->htail = 0;
1111 pm->hlead = ch;
1115 /* combine into hangul */
1116 tmp = hangul_join(pm->hlead, pm->hvowel, pm->htail);
1118 if (tmp != 0xfffd)
1120 kbd_delchar(text, &editpos);
1121 ch = tmp;
1123 else
1125 pm->hvowel = pm->htail = 0;
1126 pm->hlead = ch;
1129 else
1131 pm->hangul = false;
1134 /* insert char */
1135 kbd_inschar(text, buflen, &editpos, ch);
1138 if (global_settings.talk_menu) /* voice UI? */
1139 talk_spell(text, false);
1141 /* speak revised text */
1142 break;
1144 #if !defined (KBD_MODES) || defined (KBD_CURSOR_KEYS)
1145 case ACTION_KBD_BACKSPACE:
1146 if (pm->hangul)
1148 if (pm->htail)
1149 pm->htail = 0;
1150 else if (pm->hvowel)
1151 pm->hvowel = 0;
1152 else
1153 pm->hangul = false;
1156 kbd_delchar(text, &editpos);
1158 if (pm->hangul)
1160 if (pm->hvowel)
1161 ch = hangul_join(pm->hlead, pm->hvowel, pm->htail);
1162 else
1163 ch = pm->hlead;
1164 kbd_inschar(text, buflen, &editpos, ch);
1167 if (global_settings.talk_menu) /* voice UI? */
1168 talk_spell(text, false); /* speak revised text */
1169 break;
1171 case ACTION_KBD_CURSOR_RIGHT:
1172 pm->hangul = false;
1174 if (editpos < len_utf8)
1176 int c = utf8seek(text, ++editpos);
1177 kbd_spellchar(text[c]);
1179 #if CONFIG_CODEC == SWCODEC
1180 else if (global_settings.talk_menu)
1181 pcmbuf_beep(1000, 150, 1500);
1182 #endif
1183 break;
1185 case ACTION_KBD_CURSOR_LEFT:
1186 pm->hangul = false;
1188 if (editpos > 0)
1190 int c = utf8seek(text, --editpos);
1191 kbd_spellchar(text[c]);
1193 #if CONFIG_CODEC == SWCODEC
1194 else if (global_settings.talk_menu)
1195 pcmbuf_beep(1000, 150, 1500);
1196 #endif
1197 break;
1198 #endif /* !defined (KBD_MODES) || defined (KBD_CURSOR_KEYS) */
1200 case BUTTON_NONE:
1201 #ifdef KBD_MORSE_INPUT
1202 if (morse_reading)
1204 int j;
1205 logf("Morse: 0x%02x", morse_code);
1206 morse_reading = false;
1208 for (j = 0; morse_alphabets[j] != '\0'; j++)
1210 if (morse_codes[j] == morse_code)
1211 break ;
1214 if (morse_alphabets[j] == '\0')
1216 logf("Morse code not found");
1217 break ;
1220 /* turn off hangul input */
1221 FOR_NB_SCREENS(l)
1222 param[l].hangul = false;
1223 kbd_inschar(text, buflen, &editpos, morse_alphabets[j]);
1225 if (global_settings.talk_menu) /* voice UI? */
1226 talk_spell(text, false); /* speak revised text */
1228 #endif /* KBD_MORSE_INPUT */
1229 break;
1231 default:
1232 if (default_event_handler(button) == SYS_USB_CONNECTED)
1234 FOR_NB_SCREENS(l)
1235 screens[l].setfont(FONT_SYSFIXED);
1237 break;
1239 } /* end switch */
1241 if (button != BUTTON_NONE)
1243 cur_blink = true;
1247 #ifdef HAVE_BUTTONBAR
1248 global_settings.buttonbar = buttonbar_config;
1249 #endif
1251 if (ret < 0)
1252 splash(HZ/2, ID2P(LANG_CANCEL));
1254 FOR_NB_SCREENS(l)
1255 screens[l].setfont(FONT_UI);
1256 viewportmanager_set_statusbar(oldbars);
1257 send_event(GUI_EVENT_REFRESH, NULL);
1259 return ret;