Both of this isn't needed anymore as it's done at the end of the function.
[kugel-rb/myfork.git] / apps / recorder / keyboard.c
blob9ef602116025b4746cf4ab4fb11331499474b7b6
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);
778 ret = -1; done = true;
779 break;
781 case ACTION_KBD_PAGE_FLIP:
783 int k;
784 #ifdef KBD_MORSE_INPUT
785 if (morse_mode)
786 break;
787 #endif
788 if (++pm->page >= pm->pages)
789 pm->page = 0;
791 k = get_param_k(pm);
792 kbd_spellchar(pm->kbd_buf[k]);
793 break;
796 #ifdef KBD_MORSE_INPUT
797 case ACTION_KBD_MORSE_INPUT:
798 morse_mode = !morse_mode;
800 FOR_NB_SCREENS(l)
802 struct keyboard_parameters *pm = &param[l];
803 struct screen *sc = &screens[l];
805 pm->x = pm->y = pm->page = 0;
807 if (morse_mode)
809 pm->old_main_y = pm->main_y;
810 pm->main_y = sc->getheight() - pm->font_h;
812 else
814 pm->main_y = pm->old_main_y;
817 /* FIXME: We should talk something like Morse mode.. */
818 break;
819 #endif /* KBD_MORSE_INPUT */
821 case ACTION_KBD_RIGHT:
822 #ifdef KBD_MODES
823 #ifdef KBD_MORSE_INPUT
824 /* allow cursor change in non line edit morse mode */
825 if (pm->line_edit || morse_mode)
826 #else
827 /* right doubles as cursor_right in line_edit */
828 if (pm->line_edit)
829 #endif
831 pm->hangul = false;
833 if (editpos < len_utf8)
835 int c = utf8seek(text, ++editpos);
836 kbd_spellchar(text[c]);
838 #if CONFIG_CODEC == SWCODEC
839 else if (global_settings.talk_menu)
840 pcmbuf_beep(1000, 150, 1500);
841 #endif
843 else
844 #endif /* KBD_MODES */
846 int k;
847 #ifdef KBD_MORSE_INPUT
848 if (morse_mode)
849 break;
850 #endif
851 if (++pm->x >= pm->max_chars)
853 #ifndef KBD_PAGE_FLIP
854 /* no dedicated flip key - flip page on wrap */
855 if (++pm->page >= pm->pages)
856 pm->page = 0;
857 #endif
858 pm->x = 0;
861 k = get_param_k(pm);
862 kbd_spellchar(pm->kbd_buf[k]);
864 break;
866 case ACTION_KBD_LEFT:
867 #ifdef KBD_MODES
868 #ifdef KBD_MORSE_INPUT
869 /* allow cursor change in non line edit morse mode */
870 if (pm->line_edit || morse_mode)
871 #else
872 /* left doubles as cursor_left in line_edit */
873 if (pm->line_edit)
874 #endif
876 pm->hangul = false;
878 if (editpos > 0)
880 int c = utf8seek(text, --editpos);
881 kbd_spellchar(text[c]);
883 #if CONFIG_CODEC == SWCODEC
884 else if (global_settings.talk_menu)
885 pcmbuf_beep(1000, 150, 1500);
886 #endif
888 else
889 #endif /* KBD_MODES */
891 int k;
892 #ifdef KBD_MORSE_INPUT
893 if (morse_mode)
894 break;
895 #endif
896 if (--pm->x < 0)
898 #ifndef KBD_PAGE_FLIP
899 /* no dedicated flip key - flip page on wrap */
900 if (--pm->page < 0)
901 pm->page = pm->pages - 1;
902 #endif
903 pm->x = pm->max_chars - 1;
906 k = get_param_k(pm);
907 kbd_spellchar(pm->kbd_buf[k]);
909 break;
911 case ACTION_KBD_DOWN:
912 #ifdef KBD_MORSE_INPUT
913 #ifdef KBD_MODES
914 if (morse_mode)
916 pm->line_edit = !pm->line_edit;
917 if(pm->line_edit)
918 say_edit();
920 else
921 #else
922 if (morse_mode)
923 break;
924 #endif
925 #endif /* KBD_MORSE_INPUT */
927 #ifdef KBD_MODES
928 if (pm->line_edit)
930 pm->y = 0;
931 pm->line_edit = false;
933 else
934 #endif
935 if (++pm->y >= pm->lines)
936 #ifdef KBD_MODES
938 pm->line_edit = true;
939 say_edit();
941 #else
942 pm->y = 0;
943 #endif
945 #ifdef KBD_MODES
946 if (!pm->line_edit)
947 #endif
949 int k = get_param_k(pm);
950 kbd_spellchar(pm->kbd_buf[k]);
952 break;
954 case ACTION_KBD_UP:
955 #ifdef KBD_MORSE_INPUT
956 #ifdef KBD_MODES
957 if (morse_mode)
959 pm->line_edit = !pm->line_edit;
960 if(pm->line_edit)
961 say_edit();
963 else
964 #else
965 if (morse_mode)
966 break;
967 #endif
968 #endif /* KBD_MORSE_INPUT */
970 #ifdef KBD_MODES
971 if (pm->line_edit)
973 pm->y = pm->lines - 1;
974 pm->line_edit = false;
976 else
977 #endif
978 if (--pm->y < 0)
979 #ifdef KBD_MODES
981 pm->line_edit = true;
982 say_edit();
984 #else
985 pm->y = pm->lines - 1;
986 #endif
988 #ifdef KBD_MODES
989 if (!pm->line_edit)
990 #endif
992 int k = get_param_k(pm);
993 kbd_spellchar(pm->kbd_buf[k]);
995 break;
997 case ACTION_KBD_DONE:
998 /* accepts what was entered and continues */
999 ret = 0;
1000 done = true;
1001 break;
1003 #ifdef KBD_MORSE_INPUT
1004 case ACTION_KBD_MORSE_SELECT:
1005 if (morse_mode && morse_reading)
1007 morse_code <<= 1;
1008 if ((current_tick - morse_tick) > HZ/5)
1009 morse_code |= 0x01;
1012 break;
1013 #endif /* KBD_MORSE_INPUT */
1015 case ACTION_KBD_SELECT:
1016 case ACTION_KBD_SELECT_REM:
1017 #ifdef KBD_MORSE_INPUT
1018 #ifdef KBD_MODES
1019 if (morse_mode && !pm->line_edit)
1020 #else
1021 if (morse_mode)
1022 #endif
1024 morse_tick = current_tick;
1026 if (!morse_reading)
1028 morse_reading = true;
1029 morse_code = 1;
1031 break;
1033 #endif /* KBD_MORSE_INPUT */
1035 /* inserts the selected char */
1036 #ifdef KBD_MODES
1037 if (pm->line_edit)
1038 { /* select doubles as backspace in line_edit */
1039 if (pm->hangul)
1041 if (pm->htail)
1042 pm->htail = 0;
1043 else if (pm->hvowel)
1044 pm->hvowel = 0;
1045 else
1046 pm->hangul = false;
1049 kbd_delchar(text, &editpos);
1051 if (pm->hangul)
1053 if (pm->hvowel)
1054 ch = hangul_join(pm->hlead, pm->hvowel, pm->htail);
1055 else
1056 ch = pm->hlead;
1057 kbd_inschar(text, buflen, &editpos, ch);
1060 else
1061 #endif /* KBD_MODES */
1063 /* find input char */
1064 int k = get_param_k(pm);
1065 ch = (k < pm->nchars) ? pm->kbd_buf[k] : ' ';
1067 /* check for hangul input */
1068 if (ch >= 0x3131 && ch <= 0x3163)
1070 unsigned short tmp;
1072 if (!pm->hangul)
1074 pm->hlead = pm->hvowel = pm->htail = 0;
1075 pm->hangul = true;
1078 if (!pm->hvowel)
1080 pm->hvowel = ch;
1082 else if (!pm->htail)
1084 pm->htail = ch;
1086 else
1087 { /* previous hangul complete */
1088 /* check whether tail is actually lead of next char */
1089 tmp = hangul_join(pm->htail, ch, 0);
1091 if (tmp != 0xfffd)
1093 tmp = hangul_join(pm->hlead, pm->hvowel, 0);
1094 kbd_delchar(text, &editpos);
1095 kbd_inschar(text, buflen, &editpos, tmp);
1096 /* insert dummy char */
1097 kbd_inschar(text, buflen, &editpos, ' ');
1098 pm->hlead = pm->htail;
1099 pm->hvowel = ch;
1100 pm->htail = 0;
1102 else
1104 pm->hvowel = pm->htail = 0;
1105 pm->hlead = ch;
1109 /* combine into hangul */
1110 tmp = hangul_join(pm->hlead, pm->hvowel, pm->htail);
1112 if (tmp != 0xfffd)
1114 kbd_delchar(text, &editpos);
1115 ch = tmp;
1117 else
1119 pm->hvowel = pm->htail = 0;
1120 pm->hlead = ch;
1123 else
1125 pm->hangul = false;
1128 /* insert char */
1129 kbd_inschar(text, buflen, &editpos, ch);
1132 if (global_settings.talk_menu) /* voice UI? */
1133 talk_spell(text, false);
1135 /* speak revised text */
1136 break;
1138 #if !defined (KBD_MODES) || defined (KBD_CURSOR_KEYS)
1139 case ACTION_KBD_BACKSPACE:
1140 if (pm->hangul)
1142 if (pm->htail)
1143 pm->htail = 0;
1144 else if (pm->hvowel)
1145 pm->hvowel = 0;
1146 else
1147 pm->hangul = false;
1150 kbd_delchar(text, &editpos);
1152 if (pm->hangul)
1154 if (pm->hvowel)
1155 ch = hangul_join(pm->hlead, pm->hvowel, pm->htail);
1156 else
1157 ch = pm->hlead;
1158 kbd_inschar(text, buflen, &editpos, ch);
1161 if (global_settings.talk_menu) /* voice UI? */
1162 talk_spell(text, false); /* speak revised text */
1163 break;
1165 case ACTION_KBD_CURSOR_RIGHT:
1166 pm->hangul = false;
1168 if (editpos < len_utf8)
1170 int c = utf8seek(text, ++editpos);
1171 kbd_spellchar(text[c]);
1173 #if CONFIG_CODEC == SWCODEC
1174 else if (global_settings.talk_menu)
1175 pcmbuf_beep(1000, 150, 1500);
1176 #endif
1177 break;
1179 case ACTION_KBD_CURSOR_LEFT:
1180 pm->hangul = false;
1182 if (editpos > 0)
1184 int c = utf8seek(text, --editpos);
1185 kbd_spellchar(text[c]);
1187 #if CONFIG_CODEC == SWCODEC
1188 else if (global_settings.talk_menu)
1189 pcmbuf_beep(1000, 150, 1500);
1190 #endif
1191 break;
1192 #endif /* !defined (KBD_MODES) || defined (KBD_CURSOR_KEYS) */
1194 case BUTTON_NONE:
1195 #ifdef KBD_MORSE_INPUT
1196 if (morse_reading)
1198 int j;
1199 logf("Morse: 0x%02x", morse_code);
1200 morse_reading = false;
1202 for (j = 0; morse_alphabets[j] != '\0'; j++)
1204 if (morse_codes[j] == morse_code)
1205 break ;
1208 if (morse_alphabets[j] == '\0')
1210 logf("Morse code not found");
1211 break ;
1214 /* turn off hangul input */
1215 FOR_NB_SCREENS(l)
1216 param[l].hangul = false;
1217 kbd_inschar(text, buflen, &editpos, morse_alphabets[j]);
1219 if (global_settings.talk_menu) /* voice UI? */
1220 talk_spell(text, false); /* speak revised text */
1222 #endif /* KBD_MORSE_INPUT */
1223 break;
1225 default:
1226 if (default_event_handler(button) == SYS_USB_CONNECTED)
1228 FOR_NB_SCREENS(l)
1229 screens[l].setfont(FONT_SYSFIXED);
1231 break;
1233 } /* end switch */
1235 if (button != BUTTON_NONE)
1237 cur_blink = true;
1241 #ifdef HAVE_BUTTONBAR
1242 global_settings.buttonbar = buttonbar_config;
1243 #endif
1245 FOR_NB_SCREENS(l)
1246 screens[l].setfont(FONT_UI);
1247 viewportmanager_set_statusbar(oldbars);
1249 if (ret < 0)
1250 splash(HZ/2, ID2P(LANG_CANCEL));
1251 return ret;