1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 ****************************************************************************/
23 #include "string-extra.h"
29 #include "rbunicode.h"
30 #include "buttonbar.h"
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))
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 /* certain key combos toggle input mode between keyboard input and Morse input */
71 #define KBD_TOGGLE_INPUT
74 #define CHANGED_PICKER 1
75 #define CHANGED_CURSOR 2
76 #define CHANGED_TEXT 3
78 struct keyboard_parameters
80 unsigned short kbd_buf
[KBD_BUF_SIZE
];
81 unsigned short max_line_len
;
90 #ifdef HAVE_MORSE_INPUT
104 #ifdef HAVE_TOUCHSCREEN
114 int editpos
; /* Edit position on all screens */
115 bool cur_blink
; /* Cursor on/off flag */
117 unsigned short hlead
, hvowel
, htail
;
118 #ifdef HAVE_MORSE_INPUT
121 unsigned char morse_code
;
127 static struct keyboard_parameters kbd_param
[NB_SCREENS
];
128 static bool kbd_loaded
= false;
130 #ifdef HAVE_MORSE_INPUT
131 /* FIXME: We should put this to a configuration file. */
132 static const char *morse_alphabets
=
133 "abcdefghijklmnopqrstuvwxyz1234567890,.?-@ ";
134 static const unsigned char morse_codes
[] = {
135 0x05,0x18,0x1a,0x0c,0x02,0x12,0x0e,0x10,0x04,0x17,0x0d,0x14,0x07,
136 0x06,0x0f,0x16,0x1d,0x0a,0x08,0x03,0x09,0x11,0x0b,0x19,0x1b,0x1c,
137 0x2f,0x27,0x23,0x21,0x20,0x30,0x38,0x3c,0x3e,0x3f,
138 0x73,0x55,0x4c,0x61,0x5a,0x80 };
141 /* Loads a custom keyboard into memory
142 call with NULL to reset keyboard */
143 int load_kbd(unsigned char* filename
)
146 int i
, line_len
, max_line_len
;
147 unsigned char buf
[4];
148 unsigned short *pbuf
;
150 if (filename
== NULL
)
156 fd
= open_utf8(filename
, O_RDONLY
|O_BINARY
);
160 pbuf
= kbd_param
[0].kbd_buf
;
164 while (read(fd
, buf
, 1) == 1 && i
< KBD_BUF_SIZE
-1)
166 /* check how many bytes to read for this character */
167 static const unsigned char sizes
[4] = { 0x80, 0xe0, 0xf0, 0xf5 };
171 for (count
= 0; count
< ARRAYLEN(sizes
); count
++)
173 if (buf
[0] < sizes
[count
])
177 if (count
>= ARRAYLEN(sizes
))
178 continue; /* Invalid size. */
180 if (read(fd
, &buf
[1], count
) != (ssize_t
)count
)
187 utf8decode(buf
, &ch
);
188 if (ch
!= 0xFEFF && ch
!= '\r') /* skip BOM & carriage returns */
193 if (max_line_len
< line_len
)
194 max_line_len
= line_len
;
196 pbuf
+= line_len
+ 1;
200 pbuf
[++line_len
] = ch
;
207 if (max_line_len
< line_len
)
208 max_line_len
= line_len
;
209 if (i
== 1 || line_len
!= 0) /* ignore last empty line */
212 pbuf
+= line_len
+ 1;
214 *pbuf
= 0xFEFF; /* mark end of characters */
218 struct keyboard_parameters
*pm
= &kbd_param
[l
];
221 memcpy(pm
->kbd_buf
, kbd_param
[0].kbd_buf
, i
*sizeof(unsigned short));
223 /* initialize parameters */
224 pm
->x
= pm
->y
= pm
->page
= 0;
225 pm
->default_lines
= 0;
226 pm
->max_line_len
= max_line_len
;
232 /* helper function to spell a char */
233 static void kbd_spellchar(unsigned short c
)
235 unsigned char tmp
[5];
236 /* store char to pass to talk_spell */
237 unsigned char* utf8
= utf8encode(c
, tmp
);
241 talk_id(VOICE_BLANK
, false);
243 talk_spell(tmp
, false);
246 static void kbd_inschar(struct edit_state
*state
, unsigned short ch
)
249 unsigned char tmp
[4];
252 len
= strlen(state
->text
);
253 utf8
= utf8encode(ch
, tmp
);
254 j
= (long)utf8
- (long)tmp
;
256 if (len
+ j
< state
->buflen
)
258 i
= utf8seek(state
->text
, state
->editpos
);
259 utf8
= state
->text
+ i
;
260 memmove(utf8
+ j
, utf8
, len
- i
+ 1);
261 memcpy(utf8
, tmp
, j
);
263 state
->changed
= CHANGED_TEXT
;
267 static void kbd_delchar(struct edit_state
*state
)
272 if (state
->editpos
> 0)
275 len
= strlen(state
->text
);
276 i
= utf8seek(state
->text
, state
->editpos
);
277 utf8
= state
->text
+ i
;
278 j
= utf8seek(utf8
, 1);
279 memmove(utf8
, utf8
+ j
, len
- i
- j
+ 1);
280 state
->changed
= CHANGED_TEXT
;
284 /* Lookup k value based on state of param (pm) */
285 static unsigned short get_kbd_ch(struct keyboard_parameters
*pm
, int x
, int y
)
287 int i
= 0, k
= pm
->page
*pm
->lines
+ y
, n
;
288 unsigned short *pbuf
;
294 for (pbuf
= &pm
->kbd_buf
[i
]; (i
= *pbuf
) != 0xFEFF; pbuf
+= i
+ 1)
296 n
= i
? (i
+ pm
->max_chars
- 1) / pm
->max_chars
: 1;
300 if (y
== 0 && i
!= 0xFEFF)
302 pm
->last_k
= pm
->page
*pm
->lines
- k
;
303 pm
->last_i
= pbuf
- pm
->kbd_buf
;
305 k
= k
* pm
->max_chars
+ x
;
306 return (*pbuf
!= 0xFEFF && k
< *pbuf
)? pbuf
[k
+1]: ' ';
309 static void kbd_calc_params(struct keyboard_parameters
*pm
,
310 struct screen
*sc
, struct edit_state
*state
);
311 static void kbd_draw_picker(struct keyboard_parameters
*pm
,
312 struct screen
*sc
, struct edit_state
*state
);
313 static void kbd_draw_edit_line(struct keyboard_parameters
*pm
,
314 struct screen
*sc
, struct edit_state
*state
);
315 #ifdef HAVE_TOUCHSCREEN
316 static void kbd_draw_buttons(struct keyboard_parameters
*pm
, struct screen
*sc
);
317 static int keyboard_touchscreen(struct keyboard_parameters
*pm
,
318 struct screen
*sc
, struct edit_state
*state
);
320 static void kbd_insert_selected(struct keyboard_parameters
*pm
,
321 struct edit_state
*state
);
322 static void kbd_backspace(struct edit_state
*state
);
323 static void kbd_move_cursor(struct edit_state
*state
, int dir
);
324 static void kbd_move_picker_horizontal(struct keyboard_parameters
*pm
,
325 struct edit_state
*state
, int dir
);
326 static void kbd_move_picker_vertical(struct keyboard_parameters
*pm
,
327 struct edit_state
*state
, int dir
);
329 int kbd_input(char* text
, int buflen
)
332 struct keyboard_parameters
* const param
= kbd_param
;
333 struct edit_state state
;
334 int l
; /* screen loop variable */
336 int ret
= 0; /* assume success */
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
]);
351 /* initialize state */
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;
361 state
.hangul
= false;
366 /* Copy default keyboard to buffer */
369 struct keyboard_parameters
*pm
= ¶m
[l
];
370 unsigned short *pbuf
;
371 const unsigned char *p
;
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;
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"
404 "ÀÁÂÃÄÅÆ ÌÍÎÏ ÈÉÊË\n"
405 "àáâãäåæ ìíîï èéêë\n"
406 "ÓÒÔÕÖØ ÇÐÞÝß ÙÚÛÜ\n"
409 pm
->default_lines
= 4;
410 pm
->max_line_len
= 18;
416 p
= utf8decode(p
, &pbuf
[len
+1]);
417 if (pbuf
[len
+1] == '\n')
427 pbuf
[len
+1] = 0xFEFF; /* mark end of characters */
429 /* initialize parameters */
430 pm
->x
= pm
->y
= pm
->page
= 0;
437 struct keyboard_parameters
*pm
= ¶m
[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 */
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 */
454 const int button_screen
= 0;
456 struct keyboard_parameters
*pm
;
458 state
.len_utf8
= utf8length(state
.text
);
462 /* declare scoped pointers inside screen loops - hide the
463 declarations from previous block level */
464 struct keyboard_parameters
*pm
= ¶m
[l
];
465 struct screen
*sc
= &screens
[l
];
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
);
475 #ifdef HAVE_BUTTONBAR
476 /* draw the button bar */
477 gui_buttonbar_set(&buttonbar
, "Shift", "OK", "Del");
478 gui_buttonbar_draw(&buttonbar
);
484 state
.cur_blink
= !state
.cur_blink
;
487 #ifdef HAVE_MORSE_INPUT
488 state
.morse_mode
? CONTEXT_MORSE_INPUT
:
490 CONTEXT_KEYBOARD
, HZ
/2);
492 button_screen
= (get_action_statuscode(NULL
) & ACTION_REMOTE
) ? 1 : 0;
494 pm
= ¶m
[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
);
503 /* Remap some buttons to allow to move
504 * cursor in line edit mode and morse mode. */
506 #ifdef HAVE_MORSE_INPUT
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
;
519 case ACTION_KBD_DONE
:
520 /* accepts what was entered and continues */
525 case ACTION_KBD_ABORT
:
530 case ACTION_KBD_PAGE_FLIP
:
531 #ifdef HAVE_MORSE_INPUT
532 if (state
.morse_mode
)
535 if (++pm
->page
>= pm
->pages
)
538 state
.changed
= CHANGED_PICKER
;
541 case ACTION_KBD_RIGHT
:
542 kbd_move_picker_horizontal(pm
, &state
, 1);
545 case ACTION_KBD_LEFT
:
546 kbd_move_picker_horizontal(pm
, &state
, -1);
549 case ACTION_KBD_DOWN
:
550 kbd_move_picker_vertical(pm
, &state
, 1);
554 kbd_move_picker_vertical(pm
, &state
, -1);
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
;
565 struct keyboard_parameters
*pm
= ¶m
[l
];
567 pm
->main_y
= pm
->old_main_y
;
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;
581 #endif /* HAVE_MORSE_INPUT */
583 case ACTION_KBD_SELECT
:
584 /* select doubles as backspace in line_edit */
586 kbd_backspace(&state
);
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;
600 #endif /* HAVE_MORSE_INPUT */
601 kbd_insert_selected(pm
, &state
);
604 case ACTION_KBD_BACKSPACE
:
605 kbd_backspace(&state
);
608 case ACTION_KBD_CURSOR_RIGHT
:
609 kbd_move_cursor(&state
, 1);
612 case ACTION_KBD_CURSOR_LEFT
:
613 kbd_move_cursor(&state
, -1);
617 #ifdef HAVE_MORSE_INPUT
618 if (state
.morse_reading
)
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
)
630 if (morse_alphabets
[j
] == '\0')
632 logf("Morse code not found");
636 /* turn off hangul input */
637 state
.hangul
= false;
638 kbd_inschar(&state
, morse_alphabets
[j
]);
640 #endif /* HAVE_MORSE_INPUT */
644 if (default_event_handler(button
) == SYS_USB_CONNECTED
)
647 screens
[l
].setfont(FONT_SYSFIXED
);
653 if (button
!= ACTION_NONE
)
655 state
.cur_blink
= true;
657 if (global_settings
.talk_menu
) /* voice UI? */
659 if (state
.changed
== CHANGED_PICKER
)
663 talk_id(VOICE_EDIT
, false);
666 #ifdef HAVE_MORSE_INPUT
667 /* FIXME: We should talk something like Morse mode.. */
668 if (!state
.morse_mode
)
671 ch
= get_kbd_ch(pm
, pm
->x
, pm
->y
);
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 */
686 #ifdef HAVE_BUTTONBAR
687 global_settings
.buttonbar
= buttonbar_config
;
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
;
699 #endif /* HAVE_MORSE_INPUT && KBD_TOGGLE_INPUT */
703 screens
[l
].setfont(FONT_UI
);
704 viewportmanager_theme_undo(l
, false);
709 static void kbd_calc_params(struct keyboard_parameters
*pm
,
710 struct screen
*sc
, struct edit_state
*state
)
713 const unsigned char *p
;
714 unsigned short ch
, *pbuf
;
715 int icon_w
, sc_w
, sc_h
, w
;
717 #ifdef HAVE_TOUCHSCREEN
719 bool flippage_button
= false;
720 pm
->show_buttons
= (sc
->screen_type
== SCREEN_MAIN
&&
721 (touchscreen_get_mode() == TOUCHSCREEN_POINT
));
724 pm
->curfont
= pm
->default_lines
? FONT_SYSFIXED
: FONT_UI
;
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
);
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
]);
753 /* Find max width for text string */
754 pm
->text_w
= pm
->font_w
;
758 p
= utf8decode(p
, &ch
);
759 w
= font_get_width(font
, ch
);
764 #ifdef HAVE_TOUCHSCREEN
765 pm
->font_w
= GRID_SIZE(sc
->screen_type
, pm
->font_w
);
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
);
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)
800 pm
->keyboard_margin
+= pm
->font_h
;
803 if (pm
->keyboard_margin
> DEFAULT_MARGIN
)
804 pm
->keyboard_margin
= DEFAULT_MARGIN
;
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 */
817 flippage_button
= true;
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 */
829 pm
->main_y
+= button_h
;
832 #ifdef HAVE_MORSE_INPUT
833 pm
->old_main_y
= sc_h
- pm
->font_h
- BUTTONBAR_HEIGHT
;
834 if (state
->morse_mode
)
837 pm
->main_y
= pm
->old_main_y
;
843 static void kbd_draw_picker(struct keyboard_parameters
*pm
,
844 struct screen
*sc
, struct edit_state
*state
)
847 #ifdef HAVE_MORSE_INPUT
848 if (state
->morse_mode
)
850 const int w
= 6, h
= 8; /* sysfixed font width, height */
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
);
860 /* Draw morse code table with code descriptions. */
861 for (i
= 0; morse_alphabets
[i
] != '\0'; i
++)
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)
873 morse_code
= morse_codes
[i
];
874 for (; morse_code
> 0x01; morse_code
>>= 1)
877 if (morse_code
& 0x01)
878 sc
->fillrect(x
, y
+ 2, 3, 4);
880 sc
->fillrect(x
, y
+ 3, 1, 2);
896 #endif /* HAVE_MORSE_INPUT */
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
);
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
);
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
)
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
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
);
966 icon_w
= get_icon_width(sc
->screen_type
);
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
);
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
);
994 sc
->putsxy(sc_w
- text_margin
, pm
->main_y
, ">");
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);
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
)
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;
1030 vp
.height
= button_h
;
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);
1046 sc
->putsxy(0, text_y
, str(LANG_KBD_OK
));
1048 sc
->vline(0, 0, button_h
);
1049 sc
->putsxy(0, text_y
, str(LANG_KBD_DELETE
));
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
)
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
;
1075 if (y
< pm
->lines
*pm
->font_h
)
1077 if (x
/pm
->font_w
< pm
->max_chars
)
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
)
1097 if (button
& (BUTTON_REPEAT
|BUTTON_REL
))
1100 return ACTION_KBD_CURSOR_LEFT
;
1102 return ACTION_KBD_CURSOR_RIGHT
;
1107 /* OK/Del/Cancel button */
1108 if (button
== BUTTON_REL
)
1111 return ACTION_KBD_DONE
;
1112 else if (x
< (sc_w
/3) * 2)
1113 return ACTION_KBD_BACKSPACE
;
1115 return ACTION_KBD_ABORT
;
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)
1136 state
->hlead
= state
->hvowel
= state
->htail
= 0;
1137 state
->hangul
= true;
1144 else if (!state
->htail
)
1150 /* previous hangul complete */
1151 /* check whether tail is actually lead of next char */
1152 tmp
= hangul_join(state
->htail
, ch
, 0);
1156 tmp
= hangul_join(state
->hlead
, state
->hvowel
, 0);
1158 kbd_inschar(state
, tmp
);
1159 /* insert dummy char */
1160 kbd_inschar(state
, ' ');
1161 state
->hlead
= state
->htail
;
1167 state
->hvowel
= state
->htail
= 0;
1172 /* combine into hangul */
1173 tmp
= hangul_join(state
->hlead
, state
->hvowel
, state
->htail
);
1182 state
->hvowel
= state
->htail
= 0;
1188 state
->hangul
= false;
1192 kbd_inschar(state
, ch
);
1195 static void kbd_backspace(struct edit_state
*state
)
1202 else if (state
->hvowel
)
1205 state
->hangul
= false;
1213 ch
= hangul_join(state
->hlead
, state
->hvowel
, state
->htail
);
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
;
1231 state
->editpos
-= dir
;
1232 #if CONFIG_CODEC == SWCODEC
1233 if (global_settings
.talk_menu
)
1234 pcmbuf_beep(1000, 150, 1500);
1239 static void kbd_move_picker_horizontal(struct keyboard_parameters
*pm
,
1240 struct edit_state
*state
, int dir
)
1242 state
->changed
= CHANGED_PICKER
;
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
)
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
;
1270 #endif /* HAVE_MORSE_INPUT */
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;