Code cleanup in codec_thread, playback and pcmbuf; more elegant solution to leftover...
[kugel-rb.git] / apps / recorder / keyboard.c
blobf2f5d285087458d18d47f7df8074e83421abd2fc
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_MORSE_INPUT && !defined KBD_MODES
775 if (morse_mode)
777 /* Remap some buttons for morse mode. */
778 if (button == ACTION_KBD_LEFT)
779 button = ACTION_KBD_CURSOR_LEFT;
780 if (button == ACTION_KBD_RIGHT)
781 button = ACTION_KBD_CURSOR_RIGHT;
783 #endif
785 switch ( button )
787 case ACTION_KBD_ABORT:
788 ret = -1;
789 done = true;
790 break;
792 case ACTION_KBD_PAGE_FLIP:
794 #ifdef KBD_MORSE_INPUT
795 if (morse_mode)
796 break;
797 #endif
798 if (++pm->page >= pm->pages)
799 pm->page = 0;
801 ch = get_kbd_ch(pm);
802 kbd_spellchar(ch);
803 break;
806 #ifdef KBD_MORSE_INPUT
807 case ACTION_KBD_MORSE_INPUT:
808 morse_mode = !morse_mode;
810 FOR_NB_SCREENS(l)
812 struct keyboard_parameters *pm = &param[l];
813 struct screen *sc = &screens[l];
815 if (morse_mode)
817 pm->old_main_y = pm->main_y;
818 pm->main_y = sc->getheight() - pm->font_h;
820 else
822 pm->main_y = pm->old_main_y;
825 /* FIXME: We should talk something like Morse mode.. */
826 break;
827 #endif /* KBD_MORSE_INPUT */
829 case ACTION_KBD_RIGHT:
830 #ifdef KBD_MODES
831 #ifdef KBD_MORSE_INPUT
832 /* allow cursor change in non line edit morse mode */
833 if (pm->line_edit || morse_mode)
834 #else
835 /* right doubles as cursor_right in line_edit */
836 if (pm->line_edit)
837 #endif
839 pm->hangul = false;
841 if (editpos < len_utf8)
843 int c = utf8seek(text, ++editpos);
844 kbd_spellchar(text[c]);
846 #if CONFIG_CODEC == SWCODEC
847 else if (global_settings.talk_menu)
848 pcmbuf_beep(1000, 150, 1500);
849 #endif
851 else
852 #endif /* KBD_MODES */
854 #ifdef KBD_MORSE_INPUT
855 if (morse_mode)
856 break;
857 #endif
858 if (++pm->x >= pm->max_chars)
860 #ifndef KBD_PAGE_FLIP
861 /* no dedicated flip key - flip page on wrap */
862 if (++pm->page >= pm->pages)
863 pm->page = 0;
864 #endif
865 pm->x = 0;
868 ch = get_kbd_ch(pm);
869 kbd_spellchar(ch);
871 break;
873 case ACTION_KBD_LEFT:
874 #ifdef KBD_MODES
875 #ifdef KBD_MORSE_INPUT
876 /* allow cursor change in non line edit morse mode */
877 if (pm->line_edit || morse_mode)
878 #else
879 /* left doubles as cursor_left in line_edit */
880 if (pm->line_edit)
881 #endif
883 pm->hangul = false;
885 if (editpos > 0)
887 int c = utf8seek(text, --editpos);
888 kbd_spellchar(text[c]);
890 #if CONFIG_CODEC == SWCODEC
891 else if (global_settings.talk_menu)
892 pcmbuf_beep(1000, 150, 1500);
893 #endif
895 else
896 #endif /* KBD_MODES */
898 #ifdef KBD_MORSE_INPUT
899 if (morse_mode)
900 break;
901 #endif
902 if (--pm->x < 0)
904 #ifndef KBD_PAGE_FLIP
905 /* no dedicated flip key - flip page on wrap */
906 if (--pm->page < 0)
907 pm->page = pm->pages - 1;
908 #endif
909 pm->x = pm->max_chars - 1;
912 ch = get_kbd_ch(pm);
913 kbd_spellchar(ch);
915 break;
917 case ACTION_KBD_DOWN:
918 #ifdef KBD_MORSE_INPUT
919 #ifdef KBD_MODES
920 if (morse_mode)
922 pm->line_edit = !pm->line_edit;
923 if(pm->line_edit)
924 say_edit();
926 else
927 #else
928 if (morse_mode)
929 break;
930 #endif
931 #endif /* KBD_MORSE_INPUT */
933 #ifdef KBD_MODES
934 if (pm->line_edit)
936 pm->y = 0;
937 pm->line_edit = false;
939 else
940 #endif
941 if (++pm->y >= pm->lines)
942 #ifdef KBD_MODES
944 pm->line_edit = true;
945 say_edit();
947 #else
948 pm->y = 0;
949 #endif
951 #ifdef KBD_MODES
952 if (!pm->line_edit)
953 #endif
955 ch = get_kbd_ch(pm);
956 kbd_spellchar(ch);
958 break;
960 case ACTION_KBD_UP:
961 #ifdef KBD_MORSE_INPUT
962 #ifdef KBD_MODES
963 if (morse_mode)
965 pm->line_edit = !pm->line_edit;
966 if(pm->line_edit)
967 say_edit();
969 else
970 #else
971 if (morse_mode)
972 break;
973 #endif
974 #endif /* KBD_MORSE_INPUT */
976 #ifdef KBD_MODES
977 if (pm->line_edit)
979 pm->y = pm->lines - 1;
980 pm->line_edit = false;
982 else
983 #endif
984 if (--pm->y < 0)
985 #ifdef KBD_MODES
987 pm->line_edit = true;
988 say_edit();
990 #else
991 pm->y = pm->lines - 1;
992 #endif
994 #ifdef KBD_MODES
995 if (!pm->line_edit)
996 #endif
998 ch = get_kbd_ch(pm);
999 kbd_spellchar(ch);
1001 break;
1003 case ACTION_KBD_DONE:
1004 /* accepts what was entered and continues */
1005 ret = 0;
1006 done = true;
1007 break;
1009 #ifdef KBD_MORSE_INPUT
1010 case ACTION_KBD_MORSE_SELECT:
1011 if (morse_mode && morse_reading)
1013 morse_code <<= 1;
1014 if ((current_tick - morse_tick) > HZ/5)
1015 morse_code |= 0x01;
1018 break;
1019 #endif /* KBD_MORSE_INPUT */
1021 case ACTION_KBD_SELECT:
1022 case ACTION_KBD_SELECT_REM:
1023 #ifdef KBD_MORSE_INPUT
1024 #ifdef KBD_MODES
1025 if (morse_mode && !pm->line_edit)
1026 #else
1027 if (morse_mode)
1028 #endif
1030 morse_tick = current_tick;
1032 if (!morse_reading)
1034 morse_reading = true;
1035 morse_code = 1;
1037 break;
1039 #endif /* KBD_MORSE_INPUT */
1041 /* inserts the selected char */
1042 #ifdef KBD_MODES
1043 if (pm->line_edit)
1044 { /* select doubles as backspace in line_edit */
1045 if (pm->hangul)
1047 if (pm->htail)
1048 pm->htail = 0;
1049 else if (pm->hvowel)
1050 pm->hvowel = 0;
1051 else
1052 pm->hangul = false;
1055 kbd_delchar(text, &editpos);
1057 if (pm->hangul)
1059 if (pm->hvowel)
1060 ch = hangul_join(pm->hlead, pm->hvowel, pm->htail);
1061 else
1062 ch = pm->hlead;
1063 kbd_inschar(text, buflen, &editpos, ch);
1066 else
1067 #endif /* KBD_MODES */
1069 /* find input char */
1070 ch = get_kbd_ch(pm);
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 if (ret < 0)
1251 splash(HZ/2, ID2P(LANG_CANCEL));
1253 FOR_NB_SCREENS(l)
1254 screens[l].setfont(FONT_UI);
1255 viewportmanager_set_statusbar(oldbars);
1256 send_event(GUI_EVENT_REFRESH, NULL);
1258 return ret;