Bump version numbers for 3.13
[maemo-rb.git] / apps / recorder / keyboard.c
bloba6c015aebf61a51eb0335b6ec564e779812dbc64
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 "string-extra.h"
24 #include "font.h"
25 #include "screens.h"
26 #include "talk.h"
27 #include "settings.h"
28 #include "misc.h"
29 #include "rbunicode.h"
30 #include "buttonbar.h"
31 #include "logf.h"
32 #include "hangul.h"
33 #include "action.h"
34 #include "icon.h"
35 #include "pcmbuf.h"
36 #include "lang.h"
37 #include "keyboard.h"
38 #include "viewport.h"
39 #include "file.h"
40 #include "splash.h"
42 #ifndef O_BINARY
43 #define O_BINARY 0
44 #endif
47 #define DEFAULT_MARGIN 6
48 #define KBD_BUF_SIZE 500
50 #ifdef HAVE_TOUCHSCREEN
51 #define MIN_GRID_SIZE 16
52 #define GRID_SIZE(s, x) \
53 ((s) == SCREEN_MAIN && MIN_GRID_SIZE > (x) ? MIN_GRID_SIZE: (x))
54 #endif
56 #if (CONFIG_KEYPAD == IRIVER_H100_PAD) \
57 || (CONFIG_KEYPAD == IRIVER_H300_PAD) \
58 || (CONFIG_KEYPAD == IPOD_1G2G_PAD) \
59 || (CONFIG_KEYPAD == IPOD_3G_PAD) \
60 || (CONFIG_KEYPAD == IPOD_4G_PAD) \
61 || (CONFIG_KEYPAD == IRIVER_H10_PAD) \
62 || (CONFIG_KEYPAD == GIGABEAT_PAD) \
63 || (CONFIG_KEYPAD == GIGABEAT_S_PAD) \
64 || (CONFIG_KEYPAD == MROBE100_PAD) \
65 || (CONFIG_KEYPAD == SANSA_E200_PAD) \
66 || (CONFIG_KEYPAD == PHILIPS_HDD1630_PAD) \
67 || (CONFIG_KEYPAD == PHILIPS_HDD6330_PAD) \
68 || (CONFIG_KEYPAD == PHILIPS_SA9200_PAD) \
69 || (CONFIG_KEYPAD == PBELL_VIBE500_PAD) \
70 || (CONFIG_KEYPAD == SANSA_CONNECT_PAD)
71 /* certain key combos toggle input mode between keyboard input and Morse input */
72 #define KBD_TOGGLE_INPUT
73 #endif
75 #define CHANGED_PICKER 1
76 #define CHANGED_CURSOR 2
77 #define CHANGED_TEXT 3
79 struct keyboard_parameters
81 unsigned short kbd_buf[KBD_BUF_SIZE];
82 unsigned short max_line_len;
83 int default_lines;
84 int last_k;
85 int last_i;
86 int font_w;
87 int font_h;
88 int text_w;
89 int curfont;
90 int main_y;
91 #ifdef HAVE_MORSE_INPUT
92 int old_main_y;
93 #endif
94 int max_chars;
95 int max_chars_text;
96 int lines;
97 int pages;
98 int keyboard_margin;
99 int curpos;
100 int leftpos;
101 int page;
102 int x;
103 int y;
104 bool line_edit;
105 #ifdef HAVE_TOUCHSCREEN
106 bool show_buttons;
107 #endif
110 struct edit_state
112 char* text;
113 int buflen;
114 int len_utf8;
115 int editpos; /* Edit position on all screens */
116 bool cur_blink; /* Cursor on/off flag */
117 bool hangul;
118 unsigned short hlead, hvowel, htail;
119 #ifdef HAVE_MORSE_INPUT
120 bool morse_mode;
121 bool morse_reading;
122 unsigned char morse_code;
123 int morse_tick;
124 #endif
125 int changed;
128 static struct keyboard_parameters kbd_param[NB_SCREENS];
129 static bool kbd_loaded = false;
131 #ifdef HAVE_MORSE_INPUT
132 /* FIXME: We should put this to a configuration file. */
133 static const char * const morse_alphabets =
134 "abcdefghijklmnopqrstuvwxyz1234567890,.?-@ ";
135 static const unsigned char morse_codes[] = {
136 0x05,0x18,0x1a,0x0c,0x02,0x12,0x0e,0x10,0x04,0x17,0x0d,0x14,0x07,
137 0x06,0x0f,0x16,0x1d,0x0a,0x08,0x03,0x09,0x11,0x0b,0x19,0x1b,0x1c,
138 0x2f,0x27,0x23,0x21,0x20,0x30,0x38,0x3c,0x3e,0x3f,
139 0x73,0x55,0x4c,0x61,0x5a,0x80 };
140 #endif
142 /* Loads a custom keyboard into memory
143 call with NULL to reset keyboard */
144 int load_kbd(unsigned char* filename)
146 int fd;
147 int i, line_len, max_line_len;
148 unsigned char buf[4];
149 unsigned short *pbuf;
151 if (filename == NULL)
153 kbd_loaded = false;
154 return 0;
157 fd = open_utf8(filename, O_RDONLY|O_BINARY);
158 if (fd < 0)
159 return 1;
161 pbuf = kbd_param[0].kbd_buf;
162 line_len = 0;
163 max_line_len = 1;
164 i = 1;
165 while (read(fd, buf, 1) == 1 && i < KBD_BUF_SIZE-1)
167 /* check how many bytes to read for this character */
168 static const unsigned char sizes[4] = { 0x80, 0xe0, 0xf0, 0xf5 };
169 size_t count;
170 unsigned short ch;
172 for (count = 0; count < ARRAYLEN(sizes); count++)
174 if (buf[0] < sizes[count])
175 break;
178 if (count >= ARRAYLEN(sizes))
179 continue; /* Invalid size. */
181 if (read(fd, &buf[1], count) != (ssize_t)count)
183 close(fd);
184 kbd_loaded = false;
185 return 1;
188 utf8decode(buf, &ch);
189 if (ch != 0xFEFF && ch != '\r') /* skip BOM & carriage returns */
191 i++;
192 if (ch == '\n')
194 if (max_line_len < line_len)
195 max_line_len = line_len;
196 *pbuf = line_len;
197 pbuf += line_len + 1;
198 line_len = 0;
200 else
201 pbuf[++line_len] = ch;
205 close(fd);
206 kbd_loaded = true;
208 if (max_line_len < line_len)
209 max_line_len = line_len;
210 if (i == 1 || line_len != 0) /* ignore last empty line */
212 *pbuf = line_len;
213 pbuf += line_len + 1;
215 *pbuf = 0xFEFF; /* mark end of characters */
216 i++;
217 FOR_NB_SCREENS(l)
219 struct keyboard_parameters *pm = &kbd_param[l];
220 #if NB_SCREENS > 1
221 if (l > 0)
222 memcpy(pm->kbd_buf, kbd_param[0].kbd_buf, i*sizeof(unsigned short));
223 #endif
224 /* initialize parameters */
225 pm->x = pm->y = pm->page = 0;
226 pm->default_lines = 0;
227 pm->max_line_len = max_line_len;
230 return 0;
233 /* helper function to spell a char */
234 static void kbd_spellchar(unsigned short c)
236 unsigned char tmp[5];
237 /* store char to pass to talk_spell */
238 unsigned char* utf8 = utf8encode(c, tmp);
239 *utf8 = 0;
241 if (c == ' ')
242 talk_id(VOICE_BLANK, false);
243 else
244 talk_spell(tmp, false);
247 static void kbd_inschar(struct edit_state *state, unsigned short ch)
249 int i, j, len;
250 unsigned char tmp[4];
251 unsigned char* utf8;
253 len = strlen(state->text);
254 utf8 = utf8encode(ch, tmp);
255 j = (long)utf8 - (long)tmp;
257 if (len + j < state->buflen)
259 i = utf8seek(state->text, state->editpos);
260 utf8 = state->text + i;
261 memmove(utf8 + j, utf8, len - i + 1);
262 memcpy(utf8, tmp, j);
263 state->editpos++;
264 state->changed = CHANGED_TEXT;
268 static void kbd_delchar(struct edit_state *state)
270 int i, j, len;
271 unsigned char* utf8;
273 if (state->editpos > 0)
275 state->editpos--;
276 len = strlen(state->text);
277 i = utf8seek(state->text, state->editpos);
278 utf8 = state->text + i;
279 j = utf8seek(utf8, 1);
280 memmove(utf8, utf8 + j, len - i - j + 1);
281 state->changed = CHANGED_TEXT;
285 /* Lookup k value based on state of param (pm) */
286 static unsigned short get_kbd_ch(struct keyboard_parameters *pm, int x, int y)
288 int i = 0, k = pm->page*pm->lines + y, n;
289 unsigned short *pbuf;
290 if (k >= pm->last_k)
292 i = pm->last_i;
293 k -= pm->last_k;
295 for (pbuf = &pm->kbd_buf[i]; (i = *pbuf) != 0xFEFF; pbuf += i + 1)
297 n = i ? (i + pm->max_chars - 1) / pm->max_chars : 1;
298 if (k < n) break;
299 k -= n;
301 if (y == 0 && i != 0xFEFF)
303 pm->last_k = pm->page*pm->lines - k;
304 pm->last_i = pbuf - pm->kbd_buf;
306 k = k * pm->max_chars + x;
307 return (*pbuf != 0xFEFF && k < *pbuf)? pbuf[k+1]: ' ';
310 static void kbd_calc_params(struct keyboard_parameters *pm,
311 struct screen *sc, struct edit_state *state);
312 static void kbd_draw_picker(struct keyboard_parameters *pm,
313 struct screen *sc, struct edit_state *state);
314 static void kbd_draw_edit_line(struct keyboard_parameters *pm,
315 struct screen *sc, struct edit_state *state);
316 #ifdef HAVE_TOUCHSCREEN
317 static void kbd_draw_buttons(struct keyboard_parameters *pm, struct screen *sc);
318 static int keyboard_touchscreen(struct keyboard_parameters *pm,
319 struct screen *sc, struct edit_state *state);
320 #endif
321 static void kbd_insert_selected(struct keyboard_parameters *pm,
322 struct edit_state *state);
323 static void kbd_backspace(struct edit_state *state);
324 static void kbd_move_cursor(struct edit_state *state, int dir);
325 static void kbd_move_picker_horizontal(struct keyboard_parameters *pm,
326 struct edit_state *state, int dir);
327 static void kbd_move_picker_vertical(struct keyboard_parameters *pm,
328 struct edit_state *state, int dir);
330 int kbd_input(char* text, int buflen)
332 bool done = false;
333 struct keyboard_parameters * const param = kbd_param;
334 struct edit_state state;
335 unsigned short ch;
336 int ret = 0; /* assume success */
337 FOR_NB_SCREENS(l)
339 viewportmanager_theme_enable(l, false, NULL);
342 #ifdef HAVE_BUTTONBAR
343 struct gui_buttonbar buttonbar;
344 bool buttonbar_config = global_settings.buttonbar;
346 global_settings.buttonbar = true;
347 gui_buttonbar_init(&buttonbar);
348 gui_buttonbar_set_display(&buttonbar, &screens[SCREEN_MAIN]);
349 #endif
351 /* initialize state */
352 state.text = text;
353 state.buflen = buflen;
354 /* Initial edit position is after last character */
355 state.editpos = utf8length(state.text);
356 state.cur_blink = true;
357 #ifdef HAVE_MORSE_INPUT
358 state.morse_mode = global_settings.morse_input;
359 state.morse_reading = false;
360 #endif
361 state.hangul = false;
362 state.changed = 0;
364 if (!kbd_loaded)
366 /* Copy default keyboard to buffer */
367 FOR_NB_SCREENS(l)
369 struct keyboard_parameters *pm = &param[l];
370 unsigned short *pbuf;
371 const unsigned char *p;
372 int len = 0;
374 #if LCD_WIDTH >= 160 && LCD_HEIGHT >= 96
375 struct screen *sc = &screens[l];
377 if (sc->getwidth() >= 160 && sc->getheight() >= 96)
379 p = "ABCDEFG abcdefg !?\" @#$%+'\n"
380 "HIJKLMN hijklmn 789 &_()-`\n"
381 "OPQRSTU opqrstu 456 §|{}/<\n"
382 "VWXYZ., vwxyz.,0123 ~=[]*>\n"
383 "ÀÁÂÃÄÅÆ ÌÍÎÏ ÈÉÊË ¢£¤¥¦§©®\n"
384 "àáâãäåæ ìíîï èéêë «»°ºª¹²³\n"
385 "ÓÒÔÕÖØ ÇÐÞÝß ÙÚÛÜ ¯±×÷¡¿µ·\n"
386 "òóôõöø çðþýÿ ùúûü ¼½¾¬¶¨:;";
388 pm->default_lines = 8;
389 pm->max_line_len = 26;
391 else
392 #endif /* LCD_WIDTH >= 160 && LCD_HEIGHT >= 96 */
394 p = "ABCDEFG !?\" @#$%+'\n"
395 "HIJKLMN 789 &_()-`\n"
396 "OPQRSTU 456 §|{}/<\n"
397 "VWXYZ.,0123 ~=[]*>\n"
399 "abcdefg ¢£¤¥¦§©®¬\n"
400 "hijklmn «»°ºª¹²³¶\n"
401 "opqrstu ¯±×÷¡¿µ·¨\n"
402 "vwxyz., :;¼½¾ \n"
404 "ÀÁÂÃÄÅÆ ÌÍÎÏ ÈÉÊË\n"
405 "àáâãäåæ ìíîï èéêë\n"
406 "ÓÒÔÕÖØ ÇÐÞÝß ÙÚÛÜ\n"
407 "òóôõöø çðþýÿ ùúûü";
409 pm->default_lines = 4;
410 pm->max_line_len = 18;
413 pbuf = pm->kbd_buf;
414 while (*p)
416 p = utf8decode(p, &pbuf[len+1]);
417 if (pbuf[len+1] == '\n')
419 *pbuf = len;
420 pbuf += len+1;
421 len = 0;
423 else
424 len++;
426 *pbuf = len;
427 pbuf[len+1] = 0xFEFF; /* mark end of characters */
429 /* initialize parameters */
430 pm->x = pm->y = pm->page = 0;
432 kbd_loaded = true;
435 FOR_NB_SCREENS(l)
437 struct keyboard_parameters *pm = &param[l];
438 struct screen *sc = &screens[l];
439 kbd_calc_params(pm, sc, &state);
442 if (global_settings.talk_menu) /* voice UI? */
443 talk_spell(state.text, true); /* spell initial text */
445 while (!done)
447 /* These declarations are assigned to the screen on which the key
448 action occurred - pointers save a lot of space over array notation
449 when accessing the same array element countless times */
450 int button;
451 #if NB_SCREENS > 1
452 int button_screen;
453 #else
454 const int button_screen = 0;
455 #endif
456 struct keyboard_parameters *pm;
458 state.len_utf8 = utf8length(state.text);
460 FOR_NB_SCREENS(l)
462 /* declare scoped pointers inside screen loops - hide the
463 declarations from previous block level */
464 struct keyboard_parameters *pm = &param[l];
465 struct screen *sc = &screens[l];
466 sc->clear_display();
467 kbd_draw_picker(pm, sc, &state);
468 kbd_draw_edit_line(pm, sc, &state);
469 #ifdef HAVE_TOUCHSCREEN
470 if (pm->show_buttons)
471 kbd_draw_buttons(pm, sc);
472 #endif
475 #ifdef HAVE_BUTTONBAR
476 /* draw the button bar */
477 gui_buttonbar_set(&buttonbar, "Shift", "OK", "Del");
478 gui_buttonbar_draw(&buttonbar);
479 #endif
481 FOR_NB_SCREENS(l)
482 screens[l].update();
484 state.cur_blink = !state.cur_blink;
486 button = get_action(
487 #ifdef HAVE_MORSE_INPUT
488 state.morse_mode? CONTEXT_MORSE_INPUT:
489 #endif
490 CONTEXT_KEYBOARD, HZ/2);
491 #if NB_SCREENS > 1
492 button_screen = (get_action_statuscode(NULL) & ACTION_REMOTE) ? 1 : 0;
493 #endif
494 pm = &param[button_screen];
495 #ifdef HAVE_TOUCHSCREEN
496 if (button == ACTION_TOUCHSCREEN)
498 struct screen *sc = &screens[button_screen];
499 button = keyboard_touchscreen(pm, sc, &state);
501 #endif
503 /* Remap some buttons to allow to move
504 * cursor in line edit mode and morse mode. */
505 if (pm->line_edit
506 #ifdef HAVE_MORSE_INPUT
507 || state.morse_mode
508 #endif /* HAVE_MORSE_INPUT */
511 if (button == ACTION_KBD_LEFT)
512 button = ACTION_KBD_CURSOR_LEFT;
513 if (button == ACTION_KBD_RIGHT)
514 button = ACTION_KBD_CURSOR_RIGHT;
517 switch ( button )
519 case ACTION_KBD_DONE:
520 /* accepts what was entered and continues */
521 ret = 0;
522 done = true;
523 break;
525 case ACTION_KBD_ABORT:
526 ret = -1;
527 done = true;
528 break;
530 case ACTION_KBD_PAGE_FLIP:
531 #ifdef HAVE_MORSE_INPUT
532 if (state.morse_mode)
533 break;
534 #endif
535 if (++pm->page >= pm->pages)
536 pm->page = 0;
538 state.changed = CHANGED_PICKER;
539 break;
541 case ACTION_KBD_RIGHT:
542 kbd_move_picker_horizontal(pm, &state, 1);
543 break;
545 case ACTION_KBD_LEFT:
546 kbd_move_picker_horizontal(pm, &state, -1);
547 break;
549 case ACTION_KBD_DOWN:
550 kbd_move_picker_vertical(pm, &state, 1);
551 break;
553 case ACTION_KBD_UP:
554 kbd_move_picker_vertical(pm, &state, -1);
555 break;
557 #ifdef HAVE_MORSE_INPUT
558 #ifdef KBD_TOGGLE_INPUT
559 case ACTION_KBD_MORSE_INPUT:
560 state.morse_mode = !state.morse_mode;
561 state.changed = CHANGED_PICKER;
563 FOR_NB_SCREENS(l)
565 struct keyboard_parameters *pm = &param[l];
566 int y = pm->main_y;
567 pm->main_y = pm->old_main_y;
568 pm->old_main_y = y;
570 break;
571 #endif /* KBD_TOGGLE_INPUT */
573 case ACTION_KBD_MORSE_SELECT:
574 if (state.morse_mode && state.morse_reading)
576 state.morse_code <<= 1;
577 if ((current_tick - state.morse_tick) > HZ/5)
578 state.morse_code |= 0x01;
580 break;
581 #endif /* HAVE_MORSE_INPUT */
583 case ACTION_KBD_SELECT:
584 /* select doubles as backspace in line_edit */
585 if (pm->line_edit)
586 kbd_backspace(&state);
587 else
588 #ifdef HAVE_MORSE_INPUT
589 if (state.morse_mode)
591 state.morse_tick = current_tick;
593 if (!state.morse_reading)
595 state.morse_reading = true;
596 state.morse_code = 1;
599 else
600 #endif /* HAVE_MORSE_INPUT */
601 kbd_insert_selected(pm, &state);
602 break;
604 case ACTION_KBD_BACKSPACE:
605 kbd_backspace(&state);
606 break;
608 case ACTION_KBD_CURSOR_RIGHT:
609 kbd_move_cursor(&state, 1);
610 break;
612 case ACTION_KBD_CURSOR_LEFT:
613 kbd_move_cursor(&state, -1);
614 break;
616 case ACTION_NONE:
617 #ifdef HAVE_MORSE_INPUT
618 if (state.morse_reading)
620 int j;
621 logf("Morse: 0x%02x", state.morse_code);
622 state.morse_reading = false;
624 for (j = 0; morse_alphabets[j] != '\0'; j++)
626 if (morse_codes[j] == state.morse_code)
627 break ;
630 if (morse_alphabets[j] == '\0')
632 logf("Morse code not found");
633 break ;
636 /* turn off hangul input */
637 state.hangul = false;
638 kbd_inschar(&state, morse_alphabets[j]);
640 #endif /* HAVE_MORSE_INPUT */
641 break;
643 default:
644 if (default_event_handler(button) == SYS_USB_CONNECTED)
646 FOR_NB_SCREENS(l)
647 screens[l].setfont(FONT_SYSFIXED);
649 break;
651 } /* end switch */
653 if (button != ACTION_NONE)
655 state.cur_blink = true;
657 if (global_settings.talk_menu) /* voice UI? */
659 if (state.changed == CHANGED_PICKER)
661 if (pm->line_edit)
663 talk_id(VOICE_EDIT, false);
665 else
666 #ifdef HAVE_MORSE_INPUT
667 /* FIXME: We should talk something like Morse mode.. */
668 if (!state.morse_mode)
669 #endif
671 ch = get_kbd_ch(pm, pm->x, pm->y);
672 kbd_spellchar(ch);
675 else if (state.changed == CHANGED_CURSOR)
677 int c = utf8seek(state.text, state.editpos);
678 kbd_spellchar(state.text[c]);
680 else if (state.changed == CHANGED_TEXT)
681 talk_spell(state.text, false); /* speak revised text */
683 state.changed = 0;
686 #ifdef HAVE_BUTTONBAR
687 global_settings.buttonbar = buttonbar_config;
688 #endif
690 if (ret < 0)
691 splash(HZ/2, ID2P(LANG_CANCEL));
693 #if defined(HAVE_MORSE_INPUT) && defined(KBD_TOGGLE_INPUT)
694 if (global_settings.morse_input != state.morse_mode)
696 global_settings.morse_input = state.morse_mode;
697 settings_save();
699 #endif /* HAVE_MORSE_INPUT && KBD_TOGGLE_INPUT */
701 FOR_NB_SCREENS(l)
703 screens[l].setfont(FONT_UI);
704 viewportmanager_theme_undo(l, false);
706 return ret;
709 static void kbd_calc_params(struct keyboard_parameters *pm,
710 struct screen *sc, struct edit_state *state)
712 struct font* font;
713 const unsigned char *p;
714 unsigned short ch, *pbuf;
715 int icon_w, sc_w, sc_h, w;
716 int i, total_lines;
717 #ifdef HAVE_TOUCHSCREEN
718 int button_h = 0;
719 bool flippage_button = false;
720 pm->show_buttons = (sc->screen_type == SCREEN_MAIN &&
721 (touchscreen_get_mode() == TOUCHSCREEN_POINT));
722 #endif
724 pm->curfont = pm->default_lines ? FONT_SYSFIXED : sc->getuifont();
725 font = font_get(pm->curfont);
726 pm->font_h = font->height;
728 /* check if FONT_UI fits the screen */
729 if (2*pm->font_h + 3 + BUTTONBAR_HEIGHT > sc->getheight())
731 pm->curfont = FONT_SYSFIXED;
732 font = font_get(FONT_SYSFIXED);
733 pm->font_h = font->height;
735 #ifdef HAVE_TOUCHSCREEN
736 pm->font_h = GRID_SIZE(sc->screen_type, pm->font_h);
737 #endif
739 /* find max width of keyboard glyphs.
740 * since we're going to be adding spaces,
741 * max width is at least their width */
742 pm->font_w = font_get_width(font, ' ');
743 for (pbuf = pm->kbd_buf; *pbuf != 0xFEFF; pbuf += i)
745 for (i = 0; ++i <= *pbuf; )
747 w = font_get_width(font, pbuf[i]);
748 if (pm->font_w < w)
749 pm->font_w = w;
753 /* Find max width for text string */
754 pm->text_w = pm->font_w;
755 p = state->text;
756 while (*p)
758 p = utf8decode(p, &ch);
759 w = font_get_width(font, ch);
760 if (pm->text_w < w)
761 pm->text_w = w;
764 #ifdef HAVE_TOUCHSCREEN
765 pm->font_w = GRID_SIZE(sc->screen_type, pm->font_w);
766 #endif
767 /* calculate how many characters to put in a row. */
768 icon_w = get_icon_width(sc->screen_type);
769 sc_w = sc->getwidth();
770 if (pm->font_w < sc_w / pm->max_line_len)
771 pm->font_w = sc_w / pm->max_line_len;
772 pm->max_chars = sc_w / pm->font_w;
773 pm->max_chars_text = (sc_w - icon_w * 2 - 2) / pm->text_w;
774 if (pm->max_chars_text < 3 && icon_w > pm->text_w)
775 pm->max_chars_text = sc_w / pm->text_w - 2;
777 /* calculate pm->pages and pm->lines */
778 sc_h = sc->getheight();
779 #ifdef HAVE_TOUCHSCREEN
780 /* add space for buttons */
781 if (pm->show_buttons)
783 /* reserve place for OK/Del/Cancel buttons, use ui font for them */
784 button_h = GRID_SIZE(sc->screen_type, sc->getcharheight());
785 sc_h -= MAX(MIN_GRID_SIZE*2, button_h);
787 recalc_param:
788 #endif
789 pm->lines = (sc_h - BUTTONBAR_HEIGHT) / pm->font_h - 1;
791 if (pm->default_lines && pm->lines > pm->default_lines)
792 pm->lines = pm->default_lines;
794 pm->keyboard_margin = sc_h - BUTTONBAR_HEIGHT
795 - (pm->lines+1)*pm->font_h;
797 if (pm->keyboard_margin < 3 && pm->lines > 1)
799 pm->lines--;
800 pm->keyboard_margin += pm->font_h;
803 if (pm->keyboard_margin > DEFAULT_MARGIN)
804 pm->keyboard_margin = DEFAULT_MARGIN;
806 total_lines = 0;
807 for (pbuf = pm->kbd_buf; (i = *pbuf) != 0xFEFF; pbuf += i + 1)
808 total_lines += (i ? (i + pm->max_chars - 1) / pm->max_chars : 1);
810 pm->pages = (total_lines + pm->lines - 1) / pm->lines;
811 pm->lines = (total_lines + pm->pages - 1) / pm->pages;
812 #ifdef HAVE_TOUCHSCREEN
813 if (pm->pages > 1 && pm->show_buttons && !flippage_button)
815 /* add space for flip page button */
816 sc_h -= button_h;
817 flippage_button = true;
818 goto recalc_param;
820 #endif
821 if (pm->page >= pm->pages)
822 pm->x = pm->y = pm->page = 0;
824 pm->main_y = pm->font_h*pm->lines + pm->keyboard_margin;
825 pm->keyboard_margin -= pm->keyboard_margin/2;
826 #ifdef HAVE_TOUCHSCREEN
827 /* flip page button is put between piker and edit line */
828 if (flippage_button)
829 pm->main_y += button_h;
830 #endif
832 #ifdef HAVE_MORSE_INPUT
833 pm->old_main_y = sc_h - pm->font_h - BUTTONBAR_HEIGHT;
834 if (state->morse_mode)
836 int y = pm->main_y;
837 pm->main_y = pm->old_main_y;
838 pm->old_main_y = y;
840 #endif
843 static void kbd_draw_picker(struct keyboard_parameters *pm,
844 struct screen *sc, struct edit_state *state)
846 char outline[8];
847 #ifdef HAVE_MORSE_INPUT
848 if (state->morse_mode)
850 const int w = 6, h = 8; /* sysfixed font width, height */
851 int i, j, x, y;
852 int sc_w = sc->getwidth(), sc_h = pm->main_y - pm->keyboard_margin - 1;
854 /* Draw morse code screen with sysfont */
855 sc->setfont(FONT_SYSFIXED);
856 x = 0;
857 y = 0;
858 outline[1] = '\0';
860 /* Draw morse code table with code descriptions. */
861 for (i = 0; morse_alphabets[i] != '\0'; i++)
863 int morse_code;
865 outline[0] = morse_alphabets[i];
866 sc->putsxy(x, y, outline);
868 morse_code = morse_codes[i];
869 for (j = 0; morse_code > 0x01; morse_code >>= 1)
870 j++;
872 x += w + 3 + j*4;
873 morse_code = morse_codes[i];
874 for (; morse_code > 0x01; morse_code >>= 1)
876 x -= 4;
877 if (morse_code & 0x01)
878 sc->fillrect(x, y + 2, 3, 4);
879 else
880 sc->fillrect(x, y + 3, 1, 2);
883 x += w*5 - 3;
884 if (x + w*6 >= sc_w)
886 x = 0;
887 y += h;
888 if (y + h >= sc_h)
889 break;
893 else
894 #else
895 (void) state;
896 #endif /* HAVE_MORSE_INPUT */
898 /* draw page */
899 int i, j;
900 int w, h;
901 unsigned short ch;
902 unsigned char *utf8;
904 sc->setfont(pm->curfont);
906 for (j = 0; j < pm->lines; j++)
908 for (i = 0; i < pm->max_chars; i++)
910 ch = get_kbd_ch(pm, i, j);
911 utf8 = utf8encode(ch, outline);
912 *utf8 = 0;
914 sc->getstringsize(outline, &w, &h);
915 sc->putsxy(i*pm->font_w + (pm->font_w-w) / 2,
916 j*pm->font_h + (pm->font_h-h) / 2, outline);
920 if (!pm->line_edit)
922 /* highlight the key that has focus */
923 sc->set_drawmode(DRMODE_COMPLEMENT);
924 sc->fillrect(pm->font_w*pm->x, pm->font_h*pm->y,
925 pm->font_w, pm->font_h);
926 sc->set_drawmode(DRMODE_SOLID);
931 static void kbd_draw_edit_line(struct keyboard_parameters *pm,
932 struct screen *sc, struct edit_state *state)
934 char outline[8];
935 unsigned char *utf8;
936 int i = 0, j = 0, icon_w, w;
937 int sc_w = sc->getwidth();
938 int y = pm->main_y - pm->keyboard_margin;
939 int text_margin = (sc_w - pm->text_w * pm->max_chars_text) / 2;
941 /* Clear text area one pixel above separator line so any overdraw
942 doesn't collide */
943 screen_clear_area(sc, 0, y - 1, sc_w, pm->font_h + 6);
945 sc->hline(0, sc_w - 1, y);
947 /* write out the text */
948 sc->setfont(pm->curfont);
950 pm->leftpos = MAX(0, MIN(state->len_utf8, state->editpos + 2)
951 - pm->max_chars_text);
952 pm->curpos = state->editpos - pm->leftpos;
953 utf8 = state->text + utf8seek(state->text, pm->leftpos);
955 while (*utf8 && i < pm->max_chars_text)
957 j = utf8seek(utf8, 1);
958 strlcpy(outline, utf8, j+1);
959 sc->getstringsize(outline, &w, NULL);
960 sc->putsxy(text_margin + i*pm->text_w + (pm->text_w-w)/2,
961 pm->main_y, outline);
962 utf8 += j;
963 i++;
966 icon_w = get_icon_width(sc->screen_type);
967 if (pm->leftpos > 0)
969 /* Draw nicer bitmap arrow if room, else settle for "<". */
970 if (text_margin >= icon_w)
972 screen_put_icon_with_offset(sc, 0, 0,
973 (text_margin - icon_w) / 2,
974 pm->main_y, Icon_Reverse_Cursor);
976 else
978 sc->getstringsize("<", &w, NULL);
979 sc->putsxy(text_margin - w, pm->main_y, "<");
983 if (state->len_utf8 - pm->leftpos > pm->max_chars_text)
985 /* Draw nicer bitmap arrow if room, else settle for ">". */
986 if (text_margin >= icon_w)
988 screen_put_icon_with_offset(sc, 0, 0,
989 sc_w - (text_margin + icon_w) / 2,
990 pm->main_y, Icon_Cursor);
992 else
994 sc->putsxy(sc_w - text_margin, pm->main_y, ">");
998 /* cursor */
999 i = text_margin + pm->curpos * pm->text_w;
1001 if (state->cur_blink)
1002 sc->vline(i, pm->main_y, pm->main_y + pm->font_h - 1);
1004 if (state->hangul) /* draw underbar */
1005 sc->hline(i - pm->text_w, i, pm->main_y + pm->font_h - 1);
1007 if (pm->line_edit)
1009 sc->set_drawmode(DRMODE_COMPLEMENT);
1010 sc->fillrect(0, y + 2, sc_w, pm->font_h + 2);
1011 sc->set_drawmode(DRMODE_SOLID);
1015 #ifdef HAVE_TOUCHSCREEN
1016 static void kbd_draw_buttons(struct keyboard_parameters *pm, struct screen *sc)
1018 struct viewport vp;
1019 int button_h, text_h, text_y;
1020 int sc_w = sc->getwidth(), sc_h = sc->getheight();
1021 viewport_set_defaults(&vp, sc->screen_type);
1022 vp.flags |= VP_FLAG_ALIGN_CENTER;
1023 sc->set_viewport(&vp);
1024 text_h = sc->getcharheight();
1025 button_h = GRID_SIZE(sc->screen_type, text_h);
1026 text_y = (button_h - text_h) / 2 + 1;
1027 vp.x = 0;
1028 vp.y = 0;
1029 vp.width = sc_w;
1030 vp.height = button_h;
1031 /* draw buttons */
1032 if (pm->pages > 1)
1034 /* button to flip page. */
1035 vp.y = pm->lines*pm->font_h;
1036 sc->hline(0, sc_w - 1, 0);
1037 sc->putsxy(0, text_y, ">");
1039 /* OK/Del/Cancel buttons */
1040 button_h = MAX(MIN_GRID_SIZE*2, button_h);
1041 text_y = (button_h - text_h) / 2 + 1;
1042 vp.y = sc_h - button_h - 1;
1043 vp.height = button_h;
1044 sc->hline(0, sc_w - 1, 0);
1045 vp.width = sc_w/3;
1046 sc->putsxy(0, text_y, str(LANG_KBD_OK));
1047 vp.x += vp.width;
1048 sc->vline(0, 0, button_h);
1049 sc->putsxy(0, text_y, str(LANG_KBD_DELETE));
1050 vp.x += vp.width;
1051 sc->vline(0, 0, button_h);
1052 sc->putsxy(0, text_y, str(LANG_KBD_CANCEL));
1053 sc->set_viewport(NULL);
1056 static int keyboard_touchscreen(struct keyboard_parameters *pm,
1057 struct screen *sc, struct edit_state *state)
1059 short x, y;
1060 const int button = action_get_touchscreen_press(&x, &y);
1061 const int sc_w = sc->getwidth(), sc_h = sc->getheight();
1062 int button_h = MAX(MIN_GRID_SIZE*2, sc->getcharheight());
1063 #ifdef HAVE_MORSE_INPUT
1064 if (state->morse_mode && y < pm->main_y - pm->keyboard_margin)
1066 /* don't return ACTION_NONE since it has effect in morse mode. */
1067 return button == BUTTON_TOUCHSCREEN? ACTION_KBD_SELECT:
1068 button & BUTTON_REL? ACTION_KBD_MORSE_SELECT: ACTION_STD_OK;
1070 #else
1071 (void) state;
1072 #endif
1073 if (x < 0 || y < 0)
1074 return ACTION_NONE;
1075 if (y < pm->lines*pm->font_h)
1077 if (x/pm->font_w < pm->max_chars)
1079 /* picker area */
1080 state->changed = CHANGED_PICKER;
1081 pm->x = x/pm->font_w;
1082 pm->y = y/pm->font_h;
1083 pm->line_edit = false;
1084 if (button == BUTTON_REL)
1085 return ACTION_KBD_SELECT;
1088 else if (y < pm->main_y - pm->keyboard_margin)
1090 /* button to flip page */
1091 if (button == BUTTON_REL)
1092 return ACTION_KBD_PAGE_FLIP;
1094 else if (y < sc_h - button_h)
1096 /* edit line */
1097 if (button & (BUTTON_REPEAT|BUTTON_REL))
1099 if (x < sc_w/2)
1100 return ACTION_KBD_CURSOR_LEFT;
1101 else
1102 return ACTION_KBD_CURSOR_RIGHT;
1105 else
1107 /* OK/Del/Cancel button */
1108 if (button == BUTTON_REL)
1110 if (x < sc_w/3)
1111 return ACTION_KBD_DONE;
1112 else if (x < (sc_w/3) * 2)
1113 return ACTION_KBD_BACKSPACE;
1114 else
1115 return ACTION_KBD_ABORT;
1118 return ACTION_NONE;
1120 #endif
1122 /* inserts the selected char */
1123 static void kbd_insert_selected(struct keyboard_parameters *pm,
1124 struct edit_state *state)
1126 /* find input char */
1127 unsigned short ch = get_kbd_ch(pm, pm->x, pm->y);
1129 /* check for hangul input */
1130 if (ch >= 0x3131 && ch <= 0x3163)
1132 unsigned short tmp;
1134 if (!state->hangul)
1136 state->hlead = state->hvowel = state->htail = 0;
1137 state->hangul = true;
1140 if (!state->hvowel)
1142 state->hvowel = ch;
1144 else if (!state->htail)
1146 state->htail = ch;
1148 else
1150 /* previous hangul complete */
1151 /* check whether tail is actually lead of next char */
1152 tmp = hangul_join(state->htail, ch, 0);
1154 if (tmp != 0xfffd)
1156 tmp = hangul_join(state->hlead, state->hvowel, 0);
1157 kbd_delchar(state);
1158 kbd_inschar(state, tmp);
1159 /* insert dummy char */
1160 kbd_inschar(state, ' ');
1161 state->hlead = state->htail;
1162 state->hvowel = ch;
1163 state->htail = 0;
1165 else
1167 state->hvowel = state->htail = 0;
1168 state->hlead = ch;
1172 /* combine into hangul */
1173 tmp = hangul_join(state->hlead, state->hvowel, state->htail);
1175 if (tmp != 0xfffd)
1177 kbd_delchar(state);
1178 ch = tmp;
1180 else
1182 state->hvowel = state->htail = 0;
1183 state->hlead = ch;
1186 else
1188 state->hangul = false;
1191 /* insert char */
1192 kbd_inschar(state, ch);
1195 static void kbd_backspace(struct edit_state *state)
1197 unsigned short ch;
1198 if (state->hangul)
1200 if (state->htail)
1201 state->htail = 0;
1202 else if (state->hvowel)
1203 state->hvowel = 0;
1204 else
1205 state->hangul = false;
1208 kbd_delchar(state);
1210 if (state->hangul)
1212 if (state->hvowel)
1213 ch = hangul_join(state->hlead, state->hvowel, state->htail);
1214 else
1215 ch = state->hlead;
1216 kbd_inschar(state, ch);
1220 static void kbd_move_cursor(struct edit_state *state, int dir)
1222 state->hangul = false;
1223 state->editpos += dir;
1225 if (state->editpos >= 0 && state->editpos <= state->len_utf8)
1227 state->changed = CHANGED_CURSOR;
1229 else
1231 state->editpos -= dir;
1232 #if CONFIG_CODEC == SWCODEC
1233 if (global_settings.talk_menu)
1234 beep_play(1000, 150, 1500);
1235 #endif
1239 static void kbd_move_picker_horizontal(struct keyboard_parameters *pm,
1240 struct edit_state *state, int dir)
1242 state->changed = CHANGED_PICKER;
1244 pm->x += dir;
1245 if (pm->x < 0)
1247 if (--pm->page < 0)
1248 pm->page = pm->pages - 1;
1249 pm->x = pm->max_chars - 1;
1251 else if (pm->x >= pm->max_chars)
1253 if (++pm->page >= pm->pages)
1254 pm->page = 0;
1255 pm->x = 0;
1259 static void kbd_move_picker_vertical(struct keyboard_parameters *pm,
1260 struct edit_state *state, int dir)
1262 state->changed = CHANGED_PICKER;
1264 #ifdef HAVE_MORSE_INPUT
1265 if (state->morse_mode)
1267 pm->line_edit = !pm->line_edit;
1268 return;
1270 #endif /* HAVE_MORSE_INPUT */
1272 pm->y += dir;
1273 if (pm->line_edit)
1275 pm->y = (dir > 0 ? 0 : pm->lines - 1);
1276 pm->line_edit = false;
1278 else if (pm->y < 0 || pm->y >= pm->lines)
1280 pm->line_edit = true;