mirror of
https://github.com/Rockbox/rockbox.git
synced 2026-10-10 08:03:04 -04:00
On large touchscreens the software keyboard can now run in "point mode". Keys are laid out finger-sized and tapped directly instead of being navigated with a cursor. Targets opt in with HAVE_KBD_POINT_MODE. Key size is derived from LCD_DPI so a key is about 5mm across whatever the panel density, falling back to a fixed size where DPI is unknown. A phone-style default layout in the UI font, across three flip pages of eight lines. Page two is page one shifted (i.e caps). Page three completes Latin-1 and adds additional accented letters common to the other European locales. Space is drawn as an icon, since the UI font has no glyph for it. Change-Id: Ibe2e305b3c22b1f9152358cf3864b9445b67b463 Co-Author: Claude Opus 4.8
1779 lines
54 KiB
C
1779 lines
54 KiB
C
/***************************************************************************
|
|
* __________ __ ___.
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
* \/ \/ \/ \/ \/
|
|
*
|
|
* Copyright (C) 2002 by Björn Stenberg
|
|
*
|
|
* This program is free software; you can redistribute it and/or
|
|
* modify it under the terms of the GNU General Public License
|
|
* as published by the Free Software Foundation; either version 2
|
|
* of the License, or (at your option) any later version.
|
|
*
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
* KIND, either express or implied.
|
|
*
|
|
****************************************************************************/
|
|
#include "kernel.h"
|
|
#include "system.h"
|
|
#include "string-extra.h"
|
|
#include "font.h"
|
|
#include "screens.h"
|
|
#include "talk.h"
|
|
#include "settings.h"
|
|
#include "misc.h"
|
|
#include "rbunicode.h"
|
|
#include "logf.h"
|
|
#include "hangul.h"
|
|
#include "action.h"
|
|
#include "icon.h"
|
|
#include "pcmbuf.h"
|
|
#include "lang.h"
|
|
#include "bidi.h"
|
|
#include "keyboard.h"
|
|
#include "viewport.h"
|
|
#include "file.h"
|
|
#include "splash.h"
|
|
#include "core_alloc.h"
|
|
|
|
#ifndef O_BINARY
|
|
#define O_BINARY 0
|
|
#endif
|
|
|
|
|
|
#define DEFAULT_MARGIN 6
|
|
#define KBD_BUF_SIZE 500
|
|
|
|
/* Sentinel picker cell, drawn as an icon (the UI font has no glyph for it):
|
|
* KBD_SPACE inserts a space. */
|
|
#define KBD_SPACE 0x2423
|
|
|
|
#ifdef HAVE_TOUCHSCREEN
|
|
/* Increase grid size so it responds better to taps */
|
|
#ifdef HAVE_KBD_POINT_MODE
|
|
#ifndef MIN_GRID_SIZE
|
|
#if defined(LCD_DPI) && (LCD_DPI > 0)
|
|
#define MIN_GRID_SIZE ((LCD_DPI * 50 + 127) / 254)
|
|
#else
|
|
#define MIN_GRID_SIZE 44
|
|
#endif
|
|
#endif
|
|
#else
|
|
#define MIN_GRID_SIZE 16
|
|
#endif
|
|
#define GRID_SIZE(s, x) \
|
|
((s) == SCREEN_MAIN && MIN_GRID_SIZE > (x) ? MIN_GRID_SIZE: (x))
|
|
#endif
|
|
|
|
#if (CONFIG_KEYPAD == IRIVER_H100_PAD) \
|
|
|| (CONFIG_KEYPAD == IRIVER_H300_PAD) \
|
|
|| (CONFIG_KEYPAD == IPOD_1G2G_PAD) \
|
|
|| (CONFIG_KEYPAD == IPOD_3G_PAD) \
|
|
|| (CONFIG_KEYPAD == IPOD_4G_PAD) \
|
|
|| (CONFIG_KEYPAD == IRIVER_H10_PAD) \
|
|
|| (CONFIG_KEYPAD == GIGABEAT_PAD) \
|
|
|| (CONFIG_KEYPAD == GIGABEAT_S_PAD) \
|
|
|| (CONFIG_KEYPAD == MROBE100_PAD) \
|
|
|| (CONFIG_KEYPAD == SANSA_E200_PAD) \
|
|
|| (CONFIG_KEYPAD == PHILIPS_HDD1630_PAD) \
|
|
|| (CONFIG_KEYPAD == PHILIPS_HDD6330_PAD) \
|
|
|| (CONFIG_KEYPAD == PHILIPS_SA9200_PAD) \
|
|
|| (CONFIG_KEYPAD == PBELL_VIBE500_PAD) \
|
|
|| (CONFIG_KEYPAD == SANSA_CONNECT_PAD) \
|
|
|| (CONFIG_KEYPAD == SAMSUNG_YH820_PAD) \
|
|
|| (CONFIG_KEYPAD == SAMSUNG_YH92X_PAD)
|
|
|
|
/* certain key combos toggle input mode between keyboard input and Morse input */
|
|
#define KBD_TOGGLE_INPUT
|
|
#endif
|
|
|
|
#define CHANGED_PICKER 1
|
|
#define CHANGED_CURSOR 2
|
|
#define CHANGED_TEXT 3
|
|
|
|
enum ekbd_viewports
|
|
{
|
|
eKBD_VP_TEXT = 0,
|
|
eKBD_VP_PICKER,
|
|
eKBD_VP_MENU,
|
|
eKBD_COUNT_VP_COUNT
|
|
};
|
|
|
|
struct keyboard_parameters
|
|
{
|
|
struct viewport *kbd_viewports;
|
|
ucschar_t kbd_buf[KBD_BUF_SIZE];
|
|
ucschar_t *kbd_buf_ptr;
|
|
unsigned short max_line_len;
|
|
int default_lines;
|
|
unsigned int last_k;
|
|
unsigned int last_i;
|
|
unsigned short font_w;
|
|
unsigned short font_h;
|
|
unsigned int text_w;
|
|
int curfont;
|
|
int main_y;
|
|
#ifdef HAVE_MORSE_INPUT
|
|
int old_main_y;
|
|
#endif
|
|
int max_chars;
|
|
int max_chars_text;
|
|
int lines;
|
|
int pages;
|
|
int keyboard_margin;
|
|
int curpos;
|
|
int leftpos;
|
|
int page;
|
|
int x;
|
|
int y;
|
|
bool line_edit;
|
|
#ifdef HAVE_TOUCHSCREEN
|
|
bool show_buttons;
|
|
#endif
|
|
};
|
|
|
|
struct edit_state
|
|
{
|
|
char* text;
|
|
int buflen;
|
|
int len_utf8;
|
|
int editpos; /* Edit position on all screens */
|
|
bool cur_blink; /* Cursor on/off flag */
|
|
bool hangul;
|
|
ucschar_t hlead, hvowel, htail;
|
|
#ifdef HAVE_MORSE_INPUT
|
|
bool morse_mode;
|
|
bool morse_reading;
|
|
unsigned char morse_code;
|
|
int morse_tick;
|
|
#endif
|
|
int changed;
|
|
};
|
|
|
|
static struct keyboard_parameters kbd_param[NB_SCREENS];
|
|
static bool kbd_loaded = false;
|
|
|
|
#ifdef HAVE_MORSE_INPUT
|
|
/* FIXME: We should put this to a configuration file. */
|
|
static const char * const morse_alphabets =
|
|
"abcdefghijklmnopqrstuvwxyz1234567890,.?-@ ";
|
|
static const unsigned char morse_codes[] = {
|
|
0x05,0x18,0x1a,0x0c,0x02,0x12,0x0e,0x10,0x04,0x17,0x0d,0x14,0x07,
|
|
0x06,0x0f,0x16,0x1d,0x0a,0x08,0x03,0x09,0x11,0x0b,0x19,0x1b,0x1c,
|
|
0x2f,0x27,0x23,0x21,0x20,0x30,0x38,0x3c,0x3e,0x3f,
|
|
0x73,0x55,0x4c,0x61,0x5a,0x80 };
|
|
#endif
|
|
|
|
static void keyboard_layout(struct viewport *kbd_vp,
|
|
struct keyboard_parameters *pm,
|
|
struct screen *sc)
|
|
{
|
|
/*Note: viewports are initialized to vp_default by kbd_create_viewports */
|
|
|
|
unsigned short sc_w = sc->getwidth();
|
|
unsigned short sc_h = sc->getheight();
|
|
|
|
/* TEXT */
|
|
struct viewport *vp = &kbd_vp[eKBD_VP_TEXT];
|
|
/* make sure height is even for the text box */
|
|
unsigned short text_height = (MAX(pm->font_h, (unsigned int)get_icon_height(sc->screen_type)) & ~1) + 2;
|
|
vp->x = 0; /* LEFT */
|
|
vp->y = 0; /* TOP */
|
|
vp->width = sc_w;
|
|
vp->height = text_height;
|
|
vp->font = pm->curfont;
|
|
text_height += vp->x + 3;
|
|
|
|
/* MENU */
|
|
vp = &kbd_vp[eKBD_VP_MENU];
|
|
int menu_w = 0;//pm->font_w * MENU_CHARS; /* NOT IMPLEMENTED */
|
|
vp->x = 0; /* LEFT */
|
|
#ifdef HAVE_LCD_SPLIT
|
|
vp->y = MAX(LCD_SPLIT_POS, text_height); /* Sansa Clip/Clip+ */
|
|
#else
|
|
vp->y = text_height; /* TOP */
|
|
#endif
|
|
vp->width = menu_w;
|
|
vp->height = 0;
|
|
vp->font = pm->curfont;
|
|
menu_w += vp->x;
|
|
|
|
/* PICKER */
|
|
vp = &kbd_vp[eKBD_VP_PICKER];
|
|
vp->x = menu_w; /* LEFT */
|
|
#ifdef HAVE_LCD_SPLIT
|
|
vp->y = MAX(LCD_SPLIT_POS, text_height - 2); /* Sansa Clip/Clip+ */
|
|
#else
|
|
vp->y = text_height - 2; /* TOP */
|
|
#endif
|
|
vp->width = sc_w - menu_w;
|
|
vp->height = sc_h - vp->y; /* (MAX SIZE) - OVERWRITTEN */
|
|
vp->font = pm->curfont;
|
|
}
|
|
|
|
static int kbd_create_viewports(struct keyboard_parameters * kbd_param)
|
|
{
|
|
static struct viewport viewports[NB_SCREENS][eKBD_COUNT_VP_COUNT];
|
|
int i;
|
|
FOR_NB_SCREENS(l)
|
|
{
|
|
kbd_param[l].kbd_viewports = viewports[l];
|
|
for (i = 0; i < eKBD_COUNT_VP_COUNT; i++)
|
|
{
|
|
struct viewport *vp = &kbd_param[l].kbd_viewports[i];
|
|
viewport_set_defaults(vp, l);
|
|
/* The keyboard lays out its own cells/grid by absolute x, so the
|
|
* automatic RTL right-alignment (which mirrors every putsxy) would
|
|
* reverse the picker grid and the edit line. Clear it here and do
|
|
* RTL handling explicitly in kbd_draw_edit_line instead. */
|
|
vp->flags &= ~VP_FLAG_ALIGNMENT_MASK;
|
|
vp->font = FONT_UI;
|
|
}
|
|
}
|
|
|
|
return sizeof(viewports);
|
|
}
|
|
|
|
/* Loads a custom keyboard into memory
|
|
call with NULL to reset keyboard */
|
|
int load_kbd(unsigned char* filename)
|
|
{
|
|
int fd;
|
|
int i, line_len, max_line_len;
|
|
unsigned char buf[4];
|
|
ucschar_t *pbuf;
|
|
|
|
if (filename == NULL)
|
|
{
|
|
kbd_loaded = false;
|
|
return 0;
|
|
}
|
|
|
|
fd = open_utf8(filename, O_RDONLY|O_BINARY);
|
|
if (fd < 0)
|
|
return 1;
|
|
|
|
pbuf = kbd_param[0].kbd_buf;
|
|
line_len = 0;
|
|
max_line_len = 1;
|
|
i = 1;
|
|
while (read(fd, buf, 1) == 1 && i < KBD_BUF_SIZE-1)
|
|
{
|
|
/* check how many bytes to read for this character */
|
|
static const unsigned char sizes[4] = { 0x80, 0xe0, 0xf0, 0xf5 };
|
|
size_t count;
|
|
ucschar_t ch;
|
|
|
|
for (count = 0; count < ARRAYLEN(sizes); count++)
|
|
{
|
|
if (buf[0] < sizes[count])
|
|
break;
|
|
}
|
|
|
|
if (count >= ARRAYLEN(sizes))
|
|
continue; /* Invalid size. */
|
|
|
|
if (read(fd, &buf[1], count) != (ssize_t)count)
|
|
{
|
|
close(fd);
|
|
kbd_loaded = false;
|
|
return 1;
|
|
}
|
|
|
|
utf8decode(buf, &ch);
|
|
if (ch != 0xFEFF && ch != '\r') /* skip BOM & carriage returns */
|
|
{
|
|
i++;
|
|
if (ch == '\n')
|
|
{
|
|
if (max_line_len < line_len)
|
|
max_line_len = line_len;
|
|
*pbuf = line_len;
|
|
pbuf += line_len + 1;
|
|
line_len = 0;
|
|
}
|
|
else
|
|
pbuf[++line_len] = ch;
|
|
}
|
|
}
|
|
|
|
close(fd);
|
|
kbd_loaded = true;
|
|
|
|
if (max_line_len < line_len)
|
|
max_line_len = line_len;
|
|
if (i == 1 || line_len != 0) /* ignore last empty line */
|
|
{
|
|
*pbuf = line_len;
|
|
pbuf += line_len + 1;
|
|
}
|
|
*pbuf = 0xFEFF; /* mark end of characters */
|
|
i++;
|
|
FOR_NB_SCREENS(l)
|
|
{
|
|
struct keyboard_parameters *pm = &kbd_param[l];
|
|
#if NB_SCREENS > 1
|
|
if (l > 0)
|
|
memcpy(pm->kbd_buf, kbd_param[0].kbd_buf, i*sizeof(ucschar_t));
|
|
#endif
|
|
/* initialize parameters */
|
|
pm->x = pm->y = pm->page = 0;
|
|
pm->default_lines = 0;
|
|
pm->max_line_len = max_line_len;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
/* helper function to spell a char */
|
|
static void kbd_spellchar(ucschar_t c)
|
|
{
|
|
unsigned char tmp[5];
|
|
/* store char to pass to talk_spell */
|
|
unsigned char* utf8 = utf8encode(c, tmp);
|
|
*utf8 = 0;
|
|
|
|
if (c == ' ')
|
|
talk_id(VOICE_BLANK, false);
|
|
else
|
|
talk_spell(tmp, false);
|
|
}
|
|
|
|
static void kbd_inschar(struct edit_state *state, ucschar_t ch)
|
|
{
|
|
int i, j, len;
|
|
unsigned char tmp[4];
|
|
unsigned char* utf8;
|
|
|
|
len = strlen(state->text);
|
|
utf8 = utf8encode(ch, tmp);
|
|
j = (intptr_t)utf8 - (intptr_t)tmp;
|
|
|
|
if (len + j < state->buflen)
|
|
{
|
|
i = utf8seek(state->text, state->editpos);
|
|
utf8 = state->text + i;
|
|
memmove(utf8 + j, utf8, len - i + 1);
|
|
memcpy(utf8, tmp, j);
|
|
state->editpos++;
|
|
state->changed = CHANGED_TEXT;
|
|
}
|
|
}
|
|
|
|
static void kbd_delchar(struct edit_state *state)
|
|
{
|
|
int i, j, len;
|
|
unsigned char* utf8;
|
|
|
|
if (state->editpos > 0)
|
|
{
|
|
state->editpos--;
|
|
len = strlen(state->text);
|
|
i = utf8seek(state->text, state->editpos);
|
|
utf8 = state->text + i;
|
|
j = utf8seek(utf8, 1);
|
|
memmove(utf8, utf8 + j, len - i - j + 1);
|
|
state->changed = CHANGED_TEXT;
|
|
}
|
|
}
|
|
|
|
/* Lookup k value based on state of param (pm) */
|
|
static ucschar_t get_kbd_ch(struct keyboard_parameters *pm, int x, int y)
|
|
{
|
|
unsigned int n, i = 0, k = pm->page*pm->lines + y;
|
|
ucschar_t *pbuf;
|
|
if (k >= pm->last_k)
|
|
{
|
|
i = pm->last_i;
|
|
k -= pm->last_k;
|
|
}
|
|
for (pbuf = &pm->kbd_buf_ptr[i]; (i = *pbuf) != 0xFEFF; pbuf += i + 1)
|
|
{
|
|
n = i ? (i + pm->max_chars - 1) / pm->max_chars : 1;
|
|
if (k < n) break;
|
|
k -= n;
|
|
}
|
|
if (y == 0 && i != 0xFEFF)
|
|
{
|
|
pm->last_k = pm->page*pm->lines - k;
|
|
pm->last_i = pbuf - pm->kbd_buf_ptr;
|
|
}
|
|
k = k * pm->max_chars + x;
|
|
return (*pbuf != 0xFEFF && k < *pbuf)? pbuf[k+1]: ' ';
|
|
}
|
|
|
|
#ifdef HAVE_TOUCHSCREEN
|
|
/* Number of real characters in the grid row drawn at picker row y. */
|
|
static int get_kbd_row_len(struct keyboard_parameters *pm, int y)
|
|
{
|
|
unsigned int n, i, k = pm->page*pm->lines + y;
|
|
ucschar_t *pbuf;
|
|
for (pbuf = pm->kbd_buf_ptr; (i = *pbuf) != 0xFEFF; pbuf += i + 1)
|
|
{
|
|
n = i ? (i + pm->max_chars - 1) / pm->max_chars : 1;
|
|
if (k < n)
|
|
{
|
|
int len = (int)i - (int)(k * pm->max_chars);
|
|
if (len > (int)pm->max_chars)
|
|
len = pm->max_chars;
|
|
return len < 0 ? 0 : len;
|
|
}
|
|
k -= n;
|
|
}
|
|
return 0;
|
|
}
|
|
#endif /* HAVE_TOUCHSCREEN */
|
|
|
|
static void kbd_calc_pm_params(struct keyboard_parameters *pm,
|
|
struct screen *sc, struct edit_state *state);
|
|
static void kbd_calc_vp_params(struct keyboard_parameters *pm,
|
|
struct screen *sc, struct edit_state *state);
|
|
static void kbd_draw_picker(struct keyboard_parameters *pm,
|
|
struct screen *sc, struct edit_state *state);
|
|
static void kbd_draw_edit_line(struct keyboard_parameters *pm,
|
|
struct screen *sc, struct edit_state *state);
|
|
#ifdef HAVE_TOUCHSCREEN
|
|
static void kbd_draw_buttons(struct keyboard_parameters *pm, struct screen *sc);
|
|
static int keyboard_touchscreen(struct keyboard_parameters *pm,
|
|
struct screen *sc, struct edit_state *state);
|
|
#endif
|
|
static void kbd_insert_selected(struct keyboard_parameters *pm,
|
|
struct edit_state *state);
|
|
static void kbd_backspace(struct edit_state *state);
|
|
static void kbd_move_cursor(struct edit_state *state, int dir);
|
|
static void kbd_move_picker_horizontal(struct keyboard_parameters *pm,
|
|
struct edit_state *state, int dir);
|
|
static void kbd_move_picker_vertical(struct keyboard_parameters *pm,
|
|
struct edit_state *state, int dir);
|
|
|
|
int kbd_input(char* text, int buflen, ucschar_t *kbd)
|
|
{
|
|
bool done = false;
|
|
struct keyboard_parameters * const param = kbd_param;
|
|
struct edit_state state;
|
|
ucschar_t ch;
|
|
int ret = 0; /* assume success */
|
|
FOR_NB_SCREENS(l)
|
|
{
|
|
viewportmanager_theme_enable(l, false, NULL);
|
|
}
|
|
|
|
if (kbd_create_viewports(param) <= 0)
|
|
{
|
|
splash(HZ * 2,"Error: No Viewport Allocated, OOM?");
|
|
goto cleanup;
|
|
}
|
|
|
|
#ifdef HAVE_TOUCHSCREEN
|
|
enum touchscreen_mode old_mode = touchscreen_get_mode();
|
|
#ifdef HAVE_KBD_POINT_MODE
|
|
touchscreen_set_mode(TOUCHSCREEN_POINT);
|
|
#else
|
|
/* keyboard is unusuable in pointing mode so force 3x3 for now.
|
|
* TODO - fix properly by using a bigger font and changing the layout */
|
|
touchscreen_set_mode(TOUCHSCREEN_BUTTON);
|
|
#endif
|
|
#endif
|
|
/* initialize state */
|
|
state.text = text;
|
|
state.buflen = buflen;
|
|
/* Initial edit position is after last character */
|
|
state.editpos = utf8length(state.text);
|
|
state.cur_blink = true;
|
|
#ifdef HAVE_MORSE_INPUT
|
|
state.morse_mode = global_settings.morse_input;
|
|
state.morse_reading = false;
|
|
#endif
|
|
state.hangul = false;
|
|
state.changed = 0;
|
|
|
|
if (!kbd_loaded)
|
|
{
|
|
/* Copy default keyboard to buffer */
|
|
FOR_NB_SCREENS(l)
|
|
{
|
|
struct keyboard_parameters *pm = ¶m[l];
|
|
ucschar_t *pbuf;
|
|
const unsigned char *p;
|
|
int len = 0;
|
|
|
|
#ifdef HAVE_KBD_POINT_MODE
|
|
if (touchscreen_get_mode() == TOUCHSCREEN_POINT)
|
|
{
|
|
#if (LCD_WIDTH >= 480) && (LCD_HEIGHT >= 640)
|
|
/* Three pages of eight lines. Page two is page one shifted, cell
|
|
* for cell; page three completes Latin-1 and adds the accented
|
|
* letters common to the other European locales. The space bar
|
|
* occupies the same cells on every page. Ten columns need the
|
|
* width, and all eight lines have to be visible at once. */
|
|
p = "1234567890\n"
|
|
"qwertyuiop\n"
|
|
"asdfghjkl;\n"
|
|
"zxcvbnm,./\n"
|
|
"àáâäåçèéêë\n"
|
|
"ìíîïñòóôöù\n"
|
|
"úûüÿ'æøðþý\n"
|
|
"`-=␣␣␣␣[]\\\n"
|
|
"!@#$%^&*()\n"
|
|
"QWERTYUIOP\n"
|
|
"ASDFGHJKL:\n"
|
|
"ZXCVBNM<>?\n"
|
|
"ÀÁÂÄÅÇÈÉÊË\n"
|
|
"ÌÍÎÏÑÒÓÔÖÙ\n"
|
|
"ÚÛÜŸ\"ÆØÐÞÝ\n"
|
|
"~_+␣␣␣␣{}|\n"
|
|
"¡¢£¤¥¦§¨©ª\n"
|
|
"«¬®¯°±²³´µ\n"
|
|
"¶·¸¹º»¼½¾¿\n"
|
|
"×÷ßÃãÕõ€Œœ\n"
|
|
"ĐđČčĎďĚěŇň\n"
|
|
"ŘřŠšŤťŮůŽž\n"
|
|
"ĄąĆćĘꣳŃń\n"
|
|
"ŚśŹ␣␣␣␣źŻż";
|
|
pm->default_lines = 8;
|
|
pm->max_line_len = 10;
|
|
#else
|
|
# error "HAVE_KBD_POINT_MODE: no point-mode layout for this screen size"
|
|
#endif
|
|
}
|
|
else
|
|
#endif
|
|
{
|
|
#if LCD_WIDTH >= 160 && LCD_HEIGHT >= 96
|
|
struct screen *sc = &screens[l];
|
|
|
|
if (sc->getwidth() >= 160 && sc->getheight() >= 96)
|
|
{
|
|
p = "ABCDEFG abcdefg !?\" @#$%+'\n"
|
|
"HIJKLMN hijklmn 789 &_()-`\n"
|
|
"OPQRSTU opqrstu 456 §|{}/<\n"
|
|
"VWXYZ., vwxyz.,0123 ~=[]*>\n"
|
|
"ÀÁÂÃÄÅÆ ÌÍÎÏ ÈÉÊË ¢£¤¥¦§©®\n"
|
|
"àáâãäåæ ìíîï èéêë «»°ºª¹²³\n"
|
|
"ÓÒÔÕÖØ ÇÐÞÝß ÙÚÛÜ ¯±×÷¡¿µ·\n"
|
|
"òóôõöø çðþýÿ ùúûü ¼½¾¬¶¨:;";
|
|
|
|
pm->default_lines = 8;
|
|
pm->max_line_len = 26;
|
|
}
|
|
else
|
|
#endif /* LCD_WIDTH >= 160 && LCD_HEIGHT >= 96 */
|
|
#if (LCD_WIDTH == 128 && LCD_HEIGHT == 64) || (LCD_WIDTH == 96 && LCD_HEIGHT == 96)
|
|
/* CLIP PLUS & XDUOO X3 || CLIP ZIP*/
|
|
{
|
|
p = "ABCDEFG !?\" $%+'\n"
|
|
"HIJKLMN 789 &_-`\n"
|
|
"OPQRSTU 456 §|/\n"
|
|
"VWXYZ.,0123 ~=*#\n"
|
|
|
|
"abcdefg ¢£¤¥¦§©®\n"
|
|
"hijklmn «»ºª¹²³\n"
|
|
"opqrstu ¯±×÷¡¿µ·\n"
|
|
"vwxyz.,¨:;¼½¾¬¶°\n"
|
|
|
|
"< ({[-*?\"!'@#$%\n"
|
|
"> )}]+/\\=& 789_`\n"
|
|
"¢£¤¥¦§©®¬§ 456x|\n"
|
|
"«»°ºª¹²³¶.,0123~\n"
|
|
|
|
"ÀÁÂÃÄÅÆ ÌÍÎÏÈÉÊË\n"
|
|
"àáâãäåæ ìíîïèéêë\n"
|
|
"ÓÒÔÕÖØ ÇÐÞÝßÙÚÛÜ\n"
|
|
"òóôõöø çðþýÿùúûü";
|
|
|
|
#if ((LCD_WIDTH == 128 && LCD_HEIGHT == 64))
|
|
pm->default_lines = 4;
|
|
#else
|
|
pm->default_lines = 8;
|
|
#endif
|
|
pm->max_line_len = 16;
|
|
}
|
|
#else
|
|
{
|
|
p = "ABCDEFG !?\" @#$%+'\n"
|
|
"HIJKLMN 789 &_()-`\n"
|
|
"OPQRSTU 456 §|{}/<\n"
|
|
"VWXYZ.,0123 ~=[]*>\n"
|
|
|
|
"abcdefg ¢£¤¥¦§©®¬\n"
|
|
"hijklmn «»°ºª¹²³¶\n"
|
|
"opqrstu ¯±×÷¡¿µ·¨\n"
|
|
"vwxyz., :;¼½¾ \n"
|
|
|
|
"ÀÁÂÃÄÅÆ ÌÍÎÏ ÈÉÊË\n"
|
|
"àáâãäåæ ìíîï èéêë\n"
|
|
"ÓÒÔÕÖØ ÇÐÞÝß ÙÚÛÜ\n"
|
|
"òóôõöø çðþýÿ ùúûü";
|
|
|
|
pm->default_lines = 4;
|
|
pm->max_line_len = 18;
|
|
}
|
|
#endif
|
|
}
|
|
pbuf = pm->kbd_buf;
|
|
while (*p)
|
|
{
|
|
p = utf8decode(p, &pbuf[len+1]);
|
|
if (pbuf[len+1] == '\n')
|
|
{
|
|
*pbuf = len;
|
|
pbuf += len+1;
|
|
len = 0;
|
|
}
|
|
else
|
|
len++;
|
|
}
|
|
*pbuf = len;
|
|
pbuf[len+1] = 0xFEFF; /* mark end of characters */
|
|
|
|
/* initialize parameters */
|
|
pm->x = pm->y = pm->page = 0;
|
|
}
|
|
kbd_loaded = true;
|
|
}
|
|
|
|
FOR_NB_SCREENS(l)
|
|
{
|
|
struct keyboard_parameters *pm = ¶m[l];
|
|
|
|
if(kbd) /* user supplied custom layout */
|
|
pm->kbd_buf_ptr = kbd;
|
|
else
|
|
pm->kbd_buf_ptr = pm->kbd_buf; /* internal layout buffer */
|
|
|
|
struct screen *sc = &screens[l];
|
|
|
|
kbd_calc_pm_params(pm, sc, &state);
|
|
|
|
keyboard_layout(pm->kbd_viewports, pm, sc);
|
|
/* have all the params we need lets set up our viewports */
|
|
kbd_calc_vp_params(pm, sc, &state);
|
|
/* We want these the same height */
|
|
pm->kbd_viewports[eKBD_VP_MENU].height = pm->main_y;
|
|
pm->kbd_viewports[eKBD_VP_PICKER].height = pm->main_y;
|
|
}
|
|
|
|
if (global_settings.talk_menu) /* voice UI? */
|
|
talk_spell(state.text, true); /* spell initial text */
|
|
|
|
while (!done)
|
|
{
|
|
/* These declarations are assigned to the screen on which the key
|
|
action occurred - pointers save a lot of space over array notation
|
|
when accessing the same array element countless times */
|
|
int button;
|
|
#if NB_SCREENS > 1
|
|
int button_screen;
|
|
#else
|
|
const int button_screen = 0;
|
|
#endif
|
|
struct keyboard_parameters *pm;
|
|
|
|
state.len_utf8 = utf8length(state.text);
|
|
|
|
FOR_NB_SCREENS(l)
|
|
{
|
|
/* declare scoped pointers inside screen loops - hide the
|
|
declarations from previous block level */
|
|
struct screen *sc = &screens[l];
|
|
pm = ¶m[l];
|
|
sc->clear_display();
|
|
kbd_draw_picker(pm, sc, &state);
|
|
kbd_draw_edit_line(pm, sc, &state);
|
|
#ifdef HAVE_TOUCHSCREEN
|
|
if (pm->show_buttons)
|
|
kbd_draw_buttons(pm, sc);
|
|
#endif
|
|
}
|
|
|
|
FOR_NB_SCREENS(l)
|
|
screens[l].update();
|
|
|
|
state.cur_blink = !state.cur_blink;
|
|
|
|
button = get_action(
|
|
#ifdef HAVE_MORSE_INPUT
|
|
state.morse_mode? CONTEXT_MORSE_INPUT:
|
|
#endif
|
|
CONTEXT_KEYBOARD, HZ/2);
|
|
#if NB_SCREENS > 1
|
|
button_screen = (get_action_statuscode(NULL) & ACTION_REMOTE) ? 1 : 0;
|
|
#endif
|
|
pm = ¶m[button_screen];
|
|
#ifdef HAVE_TOUCHSCREEN
|
|
if (button == ACTION_TOUCHSCREEN)
|
|
{
|
|
struct screen *sc = &screens[button_screen];
|
|
button = keyboard_touchscreen(pm, sc, &state);
|
|
}
|
|
#endif
|
|
|
|
/* Remap some buttons to allow to move
|
|
* cursor in line edit mode and morse mode. */
|
|
if (pm->line_edit
|
|
#ifdef HAVE_MORSE_INPUT
|
|
|| state.morse_mode
|
|
#endif /* HAVE_MORSE_INPUT */
|
|
)
|
|
{
|
|
if (button == ACTION_KBD_LEFT)
|
|
button = ACTION_KBD_CURSOR_LEFT;
|
|
if (button == ACTION_KBD_RIGHT)
|
|
button = ACTION_KBD_CURSOR_RIGHT;
|
|
}
|
|
|
|
switch ( button )
|
|
{
|
|
case ACTION_KBD_DONE:
|
|
/* accepts what was entered and continues */
|
|
ret = 0;
|
|
done = true;
|
|
break;
|
|
|
|
case ACTION_KBD_ABORT:
|
|
ret = -1;
|
|
done = true;
|
|
break;
|
|
|
|
case ACTION_KBD_PAGE_FLIP:
|
|
#ifdef HAVE_MORSE_INPUT
|
|
if (state.morse_mode)
|
|
break;
|
|
#endif
|
|
if (++pm->page >= pm->pages)
|
|
pm->page = 0;
|
|
|
|
state.changed = CHANGED_PICKER;
|
|
break;
|
|
|
|
case ACTION_KBD_RIGHT:
|
|
kbd_move_picker_horizontal(pm, &state, 1);
|
|
break;
|
|
|
|
case ACTION_KBD_LEFT:
|
|
kbd_move_picker_horizontal(pm, &state, -1);
|
|
break;
|
|
|
|
case ACTION_KBD_DOWN:
|
|
kbd_move_picker_vertical(pm, &state, 1);
|
|
break;
|
|
|
|
case ACTION_KBD_UP:
|
|
kbd_move_picker_vertical(pm, &state, -1);
|
|
break;
|
|
|
|
#ifdef HAVE_MORSE_INPUT
|
|
#ifdef KBD_TOGGLE_INPUT
|
|
case ACTION_KBD_MORSE_INPUT:
|
|
state.morse_mode = !state.morse_mode;
|
|
state.changed = CHANGED_PICKER;
|
|
|
|
FOR_NB_SCREENS(l)
|
|
{
|
|
struct keyboard_parameters *pm = ¶m[l];
|
|
int y = pm->main_y;
|
|
pm->main_y = pm->old_main_y;
|
|
pm->old_main_y = y;
|
|
}
|
|
break;
|
|
#endif /* KBD_TOGGLE_INPUT */
|
|
|
|
case ACTION_KBD_MORSE_SELECT:
|
|
if (state.morse_mode && state.morse_reading)
|
|
{
|
|
state.morse_code <<= 1;
|
|
if ((current_tick - state.morse_tick) > HZ/5)
|
|
state.morse_code |= 0x01;
|
|
}
|
|
break;
|
|
#endif /* HAVE_MORSE_INPUT */
|
|
|
|
case ACTION_KBD_SELECT:
|
|
/* select doubles as backspace in line_edit */
|
|
if (pm->line_edit)
|
|
kbd_backspace(&state);
|
|
else
|
|
#ifdef HAVE_MORSE_INPUT
|
|
if (state.morse_mode)
|
|
{
|
|
state.morse_tick = current_tick;
|
|
|
|
if (!state.morse_reading)
|
|
{
|
|
state.morse_reading = true;
|
|
state.morse_code = 1;
|
|
}
|
|
}
|
|
else
|
|
#endif /* HAVE_MORSE_INPUT */
|
|
kbd_insert_selected(pm, &state);
|
|
break;
|
|
|
|
case ACTION_KBD_BACKSPACE:
|
|
kbd_backspace(&state);
|
|
break;
|
|
|
|
case ACTION_KBD_CURSOR_RIGHT:
|
|
/* move the caret to the visual right: one position forward in
|
|
* LTR text, one position back in RTL text */
|
|
kbd_move_cursor(&state,
|
|
text_is_rtl((const unsigned char *)state.text) ? -1 : 1);
|
|
break;
|
|
|
|
case ACTION_KBD_CURSOR_LEFT:
|
|
kbd_move_cursor(&state,
|
|
text_is_rtl((const unsigned char *)state.text) ? 1 : -1);
|
|
break;
|
|
|
|
case ACTION_NONE:
|
|
#ifdef HAVE_MORSE_INPUT
|
|
if (state.morse_reading)
|
|
{
|
|
int j;
|
|
logf("Morse: 0x%02x", state.morse_code);
|
|
state.morse_reading = false;
|
|
|
|
for (j = 0; morse_alphabets[j] != '\0'; j++)
|
|
{
|
|
if (morse_codes[j] == state.morse_code)
|
|
break ;
|
|
}
|
|
|
|
if (morse_alphabets[j] == '\0')
|
|
{
|
|
logf("Morse code not found");
|
|
break ;
|
|
}
|
|
|
|
/* turn off hangul input */
|
|
state.hangul = false;
|
|
kbd_inschar(&state, morse_alphabets[j]);
|
|
}
|
|
#endif /* HAVE_MORSE_INPUT */
|
|
break;
|
|
|
|
default:
|
|
if (default_event_handler(button) == SYS_USB_CONNECTED)
|
|
{
|
|
FOR_NB_SCREENS(l)
|
|
screens[l].setfont(FONT_SYSFIXED);
|
|
}
|
|
break;
|
|
|
|
} /* end switch */
|
|
|
|
if (button != ACTION_NONE)
|
|
{
|
|
state.cur_blink = true;
|
|
}
|
|
if (global_settings.talk_menu) /* voice UI? */
|
|
{
|
|
if (state.changed == CHANGED_PICKER)
|
|
{
|
|
if (pm->line_edit)
|
|
{
|
|
talk_id(VOICE_EDIT, false);
|
|
}
|
|
else
|
|
#ifdef HAVE_MORSE_INPUT
|
|
/* FIXME: We should talk something like Morse mode.. */
|
|
if (!state.morse_mode)
|
|
#endif
|
|
{
|
|
ch = get_kbd_ch(pm, pm->x, pm->y);
|
|
kbd_spellchar(ch);
|
|
}
|
|
}
|
|
else if (state.changed == CHANGED_CURSOR)
|
|
{
|
|
int c = utf8seek(state.text, state.editpos);
|
|
ucschar_t ch;
|
|
utf8decode(&state.text[c], &ch);
|
|
kbd_spellchar(ch);
|
|
}
|
|
else if (state.changed == CHANGED_TEXT)
|
|
talk_spell(state.text, false); /* speak revised text */
|
|
}
|
|
state.changed = 0;
|
|
}
|
|
|
|
if (ret < 0)
|
|
splash(HZ/2, ID2P(LANG_CANCEL));
|
|
|
|
#ifdef HAVE_TOUCHSCREEN
|
|
touchscreen_set_mode(old_mode);
|
|
#endif
|
|
|
|
#if defined(HAVE_MORSE_INPUT) && defined(KBD_TOGGLE_INPUT)
|
|
if (global_settings.morse_input != state.morse_mode)
|
|
{
|
|
global_settings.morse_input = state.morse_mode;
|
|
settings_save();
|
|
}
|
|
#endif /* HAVE_MORSE_INPUT && KBD_TOGGLE_INPUT */
|
|
cleanup:
|
|
FOR_NB_SCREENS(l)
|
|
{
|
|
screens[l].setfont(FONT_UI);
|
|
viewportmanager_theme_undo(l, false);
|
|
}
|
|
return ret;
|
|
}
|
|
static void kbd_calc_pm_params(struct keyboard_parameters *pm,
|
|
struct screen *sc, struct edit_state *state)
|
|
{
|
|
struct font* font;
|
|
const unsigned char *p;
|
|
ucschar_t ch, *pbuf;
|
|
unsigned int i, w;
|
|
#ifdef HAVE_TOUCHSCREEN
|
|
pm->show_buttons = (sc->screen_type == SCREEN_MAIN &&
|
|
(touchscreen_get_mode() == TOUCHSCREEN_POINT));
|
|
#endif
|
|
|
|
#ifdef HAVE_TOUCHSCREEN
|
|
if (touchscreen_get_mode() == TOUCHSCREEN_POINT)
|
|
pm->curfont = sc->getuifont();
|
|
else
|
|
#endif
|
|
pm->curfont = pm->default_lines ? FONT_SYSFIXED : sc->getuifont();
|
|
font = font_get(pm->curfont);
|
|
pm->font_h = font->height;
|
|
|
|
/* check if FONT_UI fits the screen */
|
|
if (pm->font_h*2 + 3 > sc->getheight())
|
|
{
|
|
pm->curfont = FONT_SYSFIXED;
|
|
font = font_get(FONT_SYSFIXED);
|
|
pm->font_h = font->height;
|
|
}
|
|
#ifdef HAVE_TOUCHSCREEN
|
|
pm->font_h = GRID_SIZE(sc->screen_type, pm->font_h);
|
|
#endif
|
|
|
|
/* find max width of keyboard glyphs.
|
|
* since we're going to be adding spaces,
|
|
* max width is at least their width */
|
|
pm->font_w = font_get_width(font, ' ');
|
|
for (pbuf = pm->kbd_buf_ptr; *pbuf != 0xFEFF; pbuf += i)
|
|
{
|
|
for (i = 0; ++i <= *pbuf; )
|
|
{
|
|
w = font_get_width(font, pbuf[i]);
|
|
if (pm->font_w < w)
|
|
pm->font_w = w;
|
|
}
|
|
}
|
|
|
|
/* Find max width for text string */
|
|
pm->text_w = pm->font_w;
|
|
p = state->text;
|
|
while (*p)
|
|
{
|
|
p = utf8decode(p, &ch);
|
|
w = font_get_width(font, ch);
|
|
if (pm->text_w < w)
|
|
pm->text_w = w;
|
|
}
|
|
|
|
#ifdef HAVE_TOUCHSCREEN
|
|
pm->font_w = GRID_SIZE(sc->screen_type, pm->font_w);
|
|
#endif
|
|
|
|
}
|
|
|
|
static void kbd_calc_vp_params(struct keyboard_parameters *pm,
|
|
struct screen *sc, struct edit_state *state)
|
|
{
|
|
(void) state;
|
|
struct viewport *vp = &pm->kbd_viewports[eKBD_VP_PICKER];
|
|
unsigned int icon_w, sc_w, sc_h;
|
|
int i, total_lines;
|
|
ucschar_t *pbuf;
|
|
/* calculate how many characters to put in a row. */
|
|
icon_w = get_icon_width(sc->screen_type);
|
|
|
|
sc_w = vp->width; /**sc->getwidth();**/
|
|
#ifdef HAVE_TOUCHSCREEN
|
|
if (touchscreen_get_mode() == TOUCHSCREEN_POINT)
|
|
pm->font_w = sc_w / pm->max_line_len; /* fill width, allow >10 cols */
|
|
else
|
|
#endif
|
|
if (pm->font_w < sc_w / pm->max_line_len)
|
|
pm->font_w = sc_w / pm->max_line_len;
|
|
pm->max_chars = sc_w / pm->font_w;
|
|
pm->max_chars_text = (sc_w - icon_w * 2 - 2) / pm->text_w;
|
|
if (pm->max_chars_text < 3 && icon_w > pm->text_w)
|
|
pm->max_chars_text = sc_w / pm->text_w - 2;
|
|
|
|
/* calculate pm->pages and pm->lines */
|
|
sc_h = vp->height;/**sc->getheight()**/;
|
|
#ifdef HAVE_TOUCHSCREEN
|
|
int button_h = 0;
|
|
bool flippage_button = false;
|
|
/* add space for buttons */
|
|
if (pm->show_buttons)
|
|
{
|
|
/* reserve place for OK/Del/Cancel buttons, use ui font for them */
|
|
button_h = GRID_SIZE(sc->screen_type, sc->getcharheight());
|
|
sc_h -= MAX(MIN_GRID_SIZE*2, button_h);
|
|
}
|
|
recalc_param:
|
|
#endif
|
|
pm->lines = sc_h / pm->font_h - 1;
|
|
|
|
if (pm->default_lines && pm->lines > pm->default_lines)
|
|
pm->lines = pm->default_lines;
|
|
|
|
pm->keyboard_margin = sc_h - (pm->lines+1)*pm->font_h;
|
|
|
|
if (pm->keyboard_margin < 3 && pm->lines > 1)
|
|
{
|
|
pm->lines--;
|
|
pm->keyboard_margin += pm->font_h;
|
|
}
|
|
|
|
if (pm->keyboard_margin > DEFAULT_MARGIN)
|
|
pm->keyboard_margin = DEFAULT_MARGIN;
|
|
|
|
total_lines = 0;
|
|
for (pbuf = pm->kbd_buf_ptr; (i = *pbuf) != 0xFEFF; pbuf += i + 1)
|
|
total_lines += (i ? (i + pm->max_chars - 1) / pm->max_chars : 1);
|
|
|
|
pm->pages = (total_lines + pm->lines - 1) / pm->lines;
|
|
pm->lines = (total_lines + pm->pages - 1) / pm->pages;
|
|
#ifdef HAVE_TOUCHSCREEN
|
|
if (pm->pages > 1 && pm->show_buttons && !flippage_button)
|
|
{
|
|
/* add space for flip page button */
|
|
sc_h -= button_h;
|
|
flippage_button = true;
|
|
goto recalc_param;
|
|
}
|
|
#endif
|
|
if (pm->page >= pm->pages)
|
|
pm->x = pm->y = pm->page = 0;
|
|
|
|
pm->main_y = pm->font_h*pm->lines + pm->keyboard_margin;
|
|
pm->keyboard_margin -= pm->keyboard_margin/2;
|
|
#ifdef HAVE_TOUCHSCREEN
|
|
/* flip page button is put between picker and edit line */
|
|
if (flippage_button)
|
|
pm->main_y += button_h;
|
|
#endif
|
|
|
|
#ifdef HAVE_MORSE_INPUT
|
|
pm->old_main_y = sc_h - pm->font_h;
|
|
if (state->morse_mode)
|
|
{
|
|
int y = pm->main_y;
|
|
pm->main_y = pm->old_main_y;
|
|
pm->old_main_y = y;
|
|
}
|
|
#endif
|
|
}
|
|
|
|
static void kbd_draw_picker(struct keyboard_parameters *pm,
|
|
struct screen *sc, struct edit_state *state)
|
|
{
|
|
struct viewport *last;
|
|
struct viewport *vp = &pm->kbd_viewports[eKBD_VP_PICKER];
|
|
last = sc->set_viewport(vp);
|
|
sc->clear_viewport();
|
|
|
|
char outline[8];
|
|
#ifdef HAVE_MORSE_INPUT
|
|
if (state->morse_mode)
|
|
{
|
|
const int w = SYSFONT_WIDTH, h = SYSFONT_HEIGHT; /* sysfixed font width, height */
|
|
const int rect_morse_long_width = 1;
|
|
const int rect_morse_short_width = 1;
|
|
const int rect_morse_long_height = (h * 75 / 100);
|
|
const int rect_morse_short_height = rect_morse_long_height / 2;
|
|
const int spacing_between_morse_codes = 4;
|
|
const int morse_code_long_height_offset_percentage = (100 - (100 * rect_morse_long_height / h)) / 2;
|
|
const int morse_code_long_height_offset = (h * morse_code_long_height_offset_percentage / 100);
|
|
const int morse_code_short_height_offset_percentage = (100 - (100 * rect_morse_short_height / rect_morse_long_height)) / 2;
|
|
const int morse_code_short_height_offset = (rect_morse_long_height * morse_code_short_height_offset_percentage / 100);
|
|
const int letters_block_spacing = w / 2;
|
|
int i, iNext, j, x, y;
|
|
int sc_w = vp->width, sc_h = vp->height;//pm->main_y - pm->keyboard_margin - 1;
|
|
|
|
/* Draw morse code screen with sysfont */
|
|
sc->setfont(FONT_SYSFIXED);
|
|
x = 0;
|
|
y = 0;
|
|
outline[1] = '\0';
|
|
|
|
/* Draw morse code table with code descriptions. */
|
|
for (i = 0; morse_alphabets[i] != '\0'; i++) {
|
|
int morse_code;
|
|
outline[0] = morse_alphabets[i];
|
|
sc->putsxy(x, y, outline);
|
|
morse_code = morse_codes[i];
|
|
for (j = 0; morse_code > 0x01; morse_code >>= 1) {
|
|
j++;
|
|
}
|
|
x += w + 3 + j * (rect_morse_long_width + spacing_between_morse_codes);
|
|
morse_code = morse_codes[i];
|
|
for (; morse_code > 0x01; morse_code >>= 1) {
|
|
x -= (rect_morse_long_width + spacing_between_morse_codes);
|
|
if (morse_code & 0x01) {
|
|
sc->fillrect(x, y + morse_code_long_height_offset,
|
|
rect_morse_long_width, rect_morse_long_height);
|
|
} else {
|
|
sc->fillrect(x, y + morse_code_long_height_offset + morse_code_short_height_offset,
|
|
rect_morse_short_width, rect_morse_short_height);
|
|
}
|
|
}
|
|
x += j * (rect_morse_long_width + spacing_between_morse_codes);
|
|
iNext = i + 1;
|
|
if (morse_alphabets[iNext] == '\0') {
|
|
break;
|
|
}
|
|
morse_code = morse_codes[iNext];
|
|
for (j = 0; morse_code > 0x01; morse_code >>= 1) {
|
|
j++;
|
|
}
|
|
// If the next one will go out of line
|
|
bool needNewLine = x + w + 3 + j * (rect_morse_long_width + spacing_between_morse_codes) + letters_block_spacing >= sc_w;
|
|
if (needNewLine) {
|
|
if (y + h >= sc_h) {
|
|
// No more height space
|
|
break;
|
|
}
|
|
x = 0;
|
|
y += h;
|
|
} else {
|
|
// Some pixels for spacing in the same line
|
|
x += letters_block_spacing;
|
|
}
|
|
}
|
|
}
|
|
else
|
|
#else
|
|
(void) state;
|
|
#endif /* HAVE_MORSE_INPUT */
|
|
{
|
|
/* draw page */
|
|
int i, j;
|
|
int w, h;
|
|
ucschar_t ch;
|
|
unsigned char *utf8;
|
|
|
|
sc->setfont(pm->curfont);
|
|
|
|
for (j = 0; j < pm->lines; j++)
|
|
{
|
|
for (i = 0; i < pm->max_chars; i++)
|
|
{
|
|
int cx = i*pm->font_w, cy = j*pm->font_h;
|
|
ch = get_kbd_ch(pm, i, j);
|
|
if (ch == KBD_SPACE)
|
|
{
|
|
/* space bar: a bar with short upturned ends. Cells join;
|
|
* the run's first/last cell adds the vertical end tick. */
|
|
int th = MAX(2, pm->font_h/14);
|
|
int by = cy + (pm->font_h*2)/3;
|
|
int tk = pm->font_h/6;
|
|
int rx = cx + pm->font_w - th;
|
|
sc->fillrect(cx, by, pm->font_w, th);
|
|
if (i == 0 || get_kbd_ch(pm, i-1, j) != KBD_SPACE)
|
|
sc->fillrect(cx, by - tk, th, tk + th);
|
|
if (get_kbd_ch(pm, i+1, j) != KBD_SPACE)
|
|
sc->fillrect(rx, by - tk, th, tk + th);
|
|
continue;
|
|
}
|
|
utf8 = utf8encode(ch, outline);
|
|
*utf8 = 0;
|
|
|
|
sc->getstringsize(outline, &w, &h);
|
|
sc->putsxy(cx + (pm->font_w-w) / 2,
|
|
cy + (pm->font_h-h) / 2, outline);
|
|
}
|
|
}
|
|
|
|
if (!pm->line_edit)
|
|
{
|
|
/* highlight the focused key; the space bar spans several cells and
|
|
* highlights as one */
|
|
int hx = pm->x, hw = 1;
|
|
if (get_kbd_ch(pm, hx, pm->y) == KBD_SPACE)
|
|
{
|
|
while (hx > 0 && get_kbd_ch(pm, hx - 1, pm->y) == KBD_SPACE)
|
|
hx--;
|
|
while (get_kbd_ch(pm, hx + hw, pm->y) == KBD_SPACE)
|
|
hw++;
|
|
}
|
|
sc->set_drawmode(DRMODE_COMPLEMENT);
|
|
sc->fillrect(pm->font_w*hx, pm->font_h*pm->y,
|
|
pm->font_w*hw, pm->font_h);
|
|
sc->set_drawmode(DRMODE_SOLID);
|
|
}
|
|
}
|
|
sc->set_viewport(last);
|
|
}
|
|
|
|
/* Pixel width of the single UTF-8 character that starts at `p`. */
|
|
static int kbd_char_width(struct screen *sc, const unsigned char *p)
|
|
{
|
|
ucschar_t c;
|
|
unsigned char tmp[8];
|
|
unsigned char *e;
|
|
int w;
|
|
utf8decode(p, &c);
|
|
e = utf8encode(c, tmp);
|
|
*e = '\0';
|
|
sc->getstringsize((char *)tmp, &w, NULL);
|
|
return w;
|
|
}
|
|
|
|
/* Visual x-offset (pixels, from the start of the drawn line) of the caret that
|
|
* sits at logical character index `cpos` of the visible string `vis`. It mirrors
|
|
* how the text engine lays the line out: bidi with LTR base orientation, where
|
|
* non-RTL runs keep their order while Hebrew/Arabic runs are reversed in place.
|
|
* The string is scanned once, run by run, accumulating a running offset and
|
|
* stopping at the run that holds the caret, so no per-character buffer is kept.
|
|
* (Like the rest of the edit line, it does not handle diacritics that combine
|
|
* several codepoints into a single displayed glyph.) */
|
|
static int kbd_caret_visual_x(struct screen *sc, const char *vis, int cpos)
|
|
{
|
|
const unsigned char *p = (const unsigned char *)vis;
|
|
int x = 0; /* offset contributed by runs to the left of the caret */
|
|
int pos = 0; /* logical index of the next character */
|
|
|
|
while (*p)
|
|
{
|
|
ucschar_t c;
|
|
utf8decode(p, &c);
|
|
bool heb = is_rtl_char(c);
|
|
int run_start = pos;
|
|
int w_run = 0; /* total width of this run */
|
|
int w_before = 0; /* width of this run's chars before the caret */
|
|
|
|
while (*p)
|
|
{
|
|
const unsigned char *next = utf8decode(p, &c);
|
|
/* Spaces/tabs are direction-neutral: they stay inside the current
|
|
* run so a multi-word Hebrew string is one reversed run (matching
|
|
* the bidi engine) instead of splitting at every space, which would
|
|
* misplace the caret. */
|
|
if ((bool)is_rtl_char(c) != heb && c != ' ' && c != '\t')
|
|
break; /* end of run */
|
|
int w = kbd_char_width(sc, p);
|
|
if (pos < cpos)
|
|
w_before += w;
|
|
w_run += w;
|
|
pos++;
|
|
if (next == p) /* guard against a malformed byte */
|
|
break;
|
|
p = next;
|
|
}
|
|
|
|
if (cpos <= run_start) /* caret is before this run */
|
|
break;
|
|
if (cpos >= pos) /* caret is past this whole run */
|
|
{
|
|
x += w_run;
|
|
continue;
|
|
}
|
|
/* caret is inside this run */
|
|
x += heb ? (w_run - w_before) /* reversed: later chars sit to the left */
|
|
: w_before; /* in order: earlier chars sit to the left */
|
|
break;
|
|
}
|
|
return x;
|
|
}
|
|
|
|
static void kbd_draw_edit_line(struct keyboard_parameters *pm,
|
|
struct screen *sc, struct edit_state *state)
|
|
{
|
|
int w;
|
|
int icon_w, icon_y;
|
|
struct viewport *last;
|
|
struct viewport *vp = &pm->kbd_viewports[eKBD_VP_TEXT];
|
|
last = sc->set_viewport(vp);
|
|
sc->clear_viewport();
|
|
sc->hline(1, vp->width - 1, vp->height - 1);
|
|
|
|
int sc_w = vp->width;
|
|
int y = (vp->height - pm->font_h) / 2;
|
|
|
|
sc->setfont(pm->curfont);
|
|
|
|
/* Base direction follows the text itself (first strong character): a
|
|
* Hebrew/Arabic name is laid out right-to-left while a Latin name stays
|
|
* readable left-to-right, even under an RTL UI language. */
|
|
bool rtl = text_is_rtl((const unsigned char *)state->text);
|
|
|
|
icon_w = get_icon_width(sc->screen_type);
|
|
icon_y = (vp->height - get_icon_height(sc->screen_type)) / 2;
|
|
|
|
/* Decide how much of the text to show. If the whole name fits we show it
|
|
* all using (almost) the full width; only when it is genuinely too long do
|
|
* we scroll and reserve a slot on each side for the scroll indicators.
|
|
* Widths are measured per glyph (not from a fixed representative width), so
|
|
* every pixel is used and a name that fits is never shown as "scrolled". */
|
|
int total_w;
|
|
sc->getstringsize(state->text, &total_w, NULL);
|
|
|
|
int pad = 2;
|
|
bool overflow = total_w > sc_w - 2 * pad;
|
|
int margin = overflow ? (icon_w + 2) : pad;
|
|
int area_l = margin;
|
|
int area_r = sc_w - margin;
|
|
int avail = area_r - area_l;
|
|
if (avail < 1)
|
|
avail = sc_w;
|
|
|
|
int leftpos, endchar;
|
|
if (!overflow)
|
|
{
|
|
leftpos = 0;
|
|
endchar = state->len_utf8;
|
|
}
|
|
else
|
|
{
|
|
/* keep the caret in view: take the characters just before it until the
|
|
* line is full, then extend forward as far as still fits */
|
|
int acc = 0;
|
|
leftpos = state->editpos;
|
|
while (leftpos > 0)
|
|
{
|
|
int cw = kbd_char_width(sc,
|
|
state->text + utf8seek(state->text, leftpos - 1));
|
|
if (acc + cw > avail)
|
|
break;
|
|
acc += cw;
|
|
leftpos--;
|
|
}
|
|
endchar = state->editpos;
|
|
while (endchar < state->len_utf8)
|
|
{
|
|
int cw = kbd_char_width(sc,
|
|
state->text + utf8seek(state->text, endchar));
|
|
if (acc + cw > avail)
|
|
break;
|
|
acc += cw;
|
|
endchar++;
|
|
}
|
|
}
|
|
pm->leftpos = leftpos;
|
|
pm->curpos = state->editpos - leftpos;
|
|
|
|
/* The whole visible window is drawn as ONE string so the bidi engine lays
|
|
* out mixed Hebrew/Latin runs correctly (splitting it at the caret would
|
|
* break a run and garble the text). The caret is placed from the width of
|
|
* the logical text before it: exact for uniform-direction text, and a
|
|
* sensible approximation inside mixed runs. */
|
|
int startbyte = utf8seek(state->text, leftpos);
|
|
int endbyte = utf8seek(state->text, endchar);
|
|
|
|
char linebuf[256];
|
|
int vlen = endbyte - startbyte;
|
|
if (vlen < 0) vlen = 0;
|
|
if (vlen >= (int)sizeof(linebuf)) vlen = sizeof(linebuf) - 1;
|
|
memcpy(linebuf, state->text + startbyte, vlen);
|
|
linebuf[vlen] = '\0';
|
|
|
|
int vis_w = 0;
|
|
sc->getstringsize(linebuf, &vis_w, NULL);
|
|
|
|
/* Place the line: right-aligned for an RTL name, left-aligned otherwise. */
|
|
int textx = rtl ? (area_r - vis_w) : area_l;
|
|
if (textx < area_l)
|
|
textx = area_l;
|
|
sc->putsxy(textx, y, linebuf);
|
|
|
|
/* Caret = line origin + the visual offset of the logical caret position,
|
|
* computed the same way bidi lays the line out (so it is correct for mixed
|
|
* Hebrew/Latin too). */
|
|
int cursor_x = textx + kbd_caret_visual_x(sc, linebuf, pm->curpos);
|
|
|
|
/* indicator: earlier text scrolled out of view (opposite side for RTL) */
|
|
if (pm->leftpos > 0)
|
|
{
|
|
if (margin >= icon_w)
|
|
screen_put_icon_with_offset(sc, 0, 0,
|
|
rtl ? (sc_w - icon_w) : 0, icon_y,
|
|
rtl ? Icon_Cursor : Icon_Reverse_Cursor);
|
|
else
|
|
{
|
|
sc->getstringsize(rtl ? ">" : "<", &w, NULL);
|
|
sc->putsxy(rtl ? (sc_w - w) : 0, y, rtl ? ">" : "<");
|
|
}
|
|
}
|
|
|
|
/* indicator: later text scrolled out of view */
|
|
if (endchar < state->len_utf8)
|
|
{
|
|
if (margin >= icon_w)
|
|
screen_put_icon_with_offset(sc, 0, 0,
|
|
rtl ? 0 : (sc_w - icon_w), icon_y,
|
|
rtl ? Icon_Reverse_Cursor : Icon_Cursor);
|
|
else
|
|
{
|
|
sc->getstringsize(rtl ? "<" : ">", &w, NULL);
|
|
sc->putsxy(rtl ? 0 : (sc_w - w), y, rtl ? "<" : ">");
|
|
}
|
|
}
|
|
|
|
/* caret */
|
|
if (state->cur_blink)
|
|
sc->vline(cursor_x, y, y + pm->font_h - 1);
|
|
|
|
if (state->hangul) /* draw underbar */
|
|
sc->hline(cursor_x - pm->text_w, cursor_x, y + pm->font_h - 1);
|
|
|
|
if (pm->line_edit)
|
|
{
|
|
sc->set_drawmode(DRMODE_COMPLEMENT);
|
|
sc->fillrect(0, y - 1, sc_w, pm->font_h + 2);
|
|
sc->set_drawmode(DRMODE_SOLID);
|
|
}
|
|
|
|
sc->set_viewport(last);
|
|
}
|
|
|
|
#ifdef HAVE_TOUCHSCREEN
|
|
static void kbd_draw_buttons(struct keyboard_parameters *pm, struct screen *sc)
|
|
{
|
|
struct viewport vp;
|
|
int button_h, text_h, text_y;
|
|
int sc_w = sc->getwidth(), sc_h = sc->getheight();
|
|
viewport_set_defaults(&vp, sc->screen_type);
|
|
vp.flags |= VP_FLAG_ALIGN_CENTER;
|
|
struct viewport *last_vp = sc->set_viewport(&vp);
|
|
text_h = sc->getcharheight();
|
|
button_h = GRID_SIZE(sc->screen_type, text_h);
|
|
text_y = (button_h - text_h) / 2 + 1;
|
|
vp.x = 0;
|
|
vp.y = 0;
|
|
vp.width = sc_w;
|
|
vp.height = button_h;
|
|
/* draw buttons */
|
|
if (pm->pages > 1)
|
|
{
|
|
/* button to flip page */
|
|
vp.y = pm->kbd_viewports[eKBD_VP_PICKER].y + pm->lines*pm->font_h;
|
|
sc->hline(0, sc_w - 1, 0);
|
|
sc->putsxy(0, text_y, ">");
|
|
}
|
|
/* OK/Del/Cancel buttons */
|
|
button_h = MAX(MIN_GRID_SIZE*2, button_h);
|
|
text_y = (button_h - text_h) / 2 + 1;
|
|
vp.y = sc_h - button_h - 1;
|
|
vp.height = button_h;
|
|
sc->hline(0, sc_w - 1, 0);
|
|
vp.width = sc_w/3;
|
|
sc->putsxy(0, text_y, str(LANG_KBD_OK));
|
|
vp.x += vp.width;
|
|
sc->vline(0, 0, button_h);
|
|
sc->putsxy(0, text_y, str(LANG_KBD_DELETE));
|
|
vp.x += vp.width;
|
|
sc->vline(0, 0, button_h);
|
|
sc->putsxy(0, text_y, str(LANG_KBD_CANCEL));
|
|
sc->set_viewport(last_vp);
|
|
}
|
|
|
|
static int keyboard_touchscreen(struct keyboard_parameters *pm,
|
|
struct screen *sc, struct edit_state *state)
|
|
{
|
|
short x, y;
|
|
const int button = action_get_touchscreen_press(&x, &y);
|
|
const int sc_w = sc->getwidth(), sc_h = sc->getheight();
|
|
int button_h = MAX(MIN_GRID_SIZE*2, sc->getcharheight());
|
|
#ifdef HAVE_MORSE_INPUT
|
|
if (state->morse_mode && y < pm->main_y - pm->keyboard_margin)
|
|
{
|
|
/* don't return ACTION_NONE since it has effect in morse mode. */
|
|
return button == BUTTON_TOUCHSCREEN? ACTION_KBD_SELECT:
|
|
button & BUTTON_REL? ACTION_KBD_MORSE_SELECT: ACTION_STD_OK;
|
|
}
|
|
#else
|
|
(void) state;
|
|
#endif
|
|
if (x < 0 || y < 0)
|
|
return ACTION_NONE;
|
|
/* The picker grid is drawn in its own viewport, below the edit line, so
|
|
* translate the absolute touch into that viewport (see kbd_draw_picker). */
|
|
struct viewport *picker_vp = &pm->kbd_viewports[eKBD_VP_PICKER];
|
|
int picker_x = x - picker_vp->x;
|
|
int picker_y = y - picker_vp->y;
|
|
if (picker_y >= 0 && picker_y < pm->lines*pm->font_h)
|
|
{
|
|
int row = picker_y/pm->font_h;
|
|
/* ignore taps past the end of a short row */
|
|
if (picker_x >= 0 && picker_x/pm->font_w < get_kbd_row_len(pm, row))
|
|
{
|
|
/* picker area */
|
|
state->changed = CHANGED_PICKER;
|
|
pm->x = picker_x/pm->font_w;
|
|
pm->y = row;
|
|
pm->line_edit = false;
|
|
if (button == BUTTON_REL)
|
|
return ACTION_KBD_SELECT;
|
|
}
|
|
}
|
|
else if (pm->pages > 1 && picker_y >= pm->lines*pm->font_h &&
|
|
picker_y < pm->lines*pm->font_h +
|
|
GRID_SIZE(sc->screen_type, sc->getcharheight()))
|
|
{
|
|
/* flip-page button, drawn just below the grid; test the same band in
|
|
* picker-relative coords so ">" is tappable. */
|
|
if (button == BUTTON_REL)
|
|
return ACTION_KBD_PAGE_FLIP;
|
|
}
|
|
else if (y < sc_h - button_h)
|
|
{
|
|
/* edit line */
|
|
if (button & (BUTTON_REPEAT|BUTTON_REL))
|
|
{
|
|
if (x < sc_w/2)
|
|
return ACTION_KBD_CURSOR_LEFT;
|
|
else
|
|
return ACTION_KBD_CURSOR_RIGHT;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
/* OK/Del/Cancel button */
|
|
if (button == BUTTON_REL)
|
|
{
|
|
if (x < sc_w/3)
|
|
return ACTION_KBD_DONE;
|
|
else if (x < (sc_w/3) * 2)
|
|
return ACTION_KBD_BACKSPACE;
|
|
else
|
|
return ACTION_KBD_ABORT;
|
|
}
|
|
}
|
|
return ACTION_NONE;
|
|
}
|
|
#endif
|
|
|
|
/* inserts the selected char */
|
|
static void kbd_insert_selected(struct keyboard_parameters *pm,
|
|
struct edit_state *state)
|
|
{
|
|
/* find input char */
|
|
ucschar_t ch = get_kbd_ch(pm, pm->x, pm->y);
|
|
|
|
if (ch == KBD_SPACE)
|
|
ch = ' ';
|
|
|
|
/* check for hangul input */
|
|
if (ch >= 0x3131 && ch <= 0x3163)
|
|
{
|
|
ucschar_t tmp;
|
|
|
|
if (!state->hangul)
|
|
{
|
|
state->hlead = state->hvowel = state->htail = 0;
|
|
state->hangul = true;
|
|
}
|
|
|
|
if (!state->hvowel)
|
|
{
|
|
state->hvowel = ch;
|
|
}
|
|
else if (!state->htail)
|
|
{
|
|
state->htail = ch;
|
|
}
|
|
else
|
|
{
|
|
/* previous hangul complete */
|
|
/* check whether tail is actually lead of next char */
|
|
tmp = hangul_join(state->htail, ch, 0);
|
|
|
|
if (tmp != 0xfffd)
|
|
{
|
|
tmp = hangul_join(state->hlead, state->hvowel, 0);
|
|
kbd_delchar(state);
|
|
kbd_inschar(state, tmp);
|
|
/* insert dummy char */
|
|
kbd_inschar(state, ' ');
|
|
state->hlead = state->htail;
|
|
state->hvowel = ch;
|
|
state->htail = 0;
|
|
}
|
|
else
|
|
{
|
|
state->hvowel = state->htail = 0;
|
|
state->hlead = ch;
|
|
}
|
|
}
|
|
|
|
/* combine into hangul */
|
|
tmp = hangul_join(state->hlead, state->hvowel, state->htail);
|
|
|
|
if (tmp != 0xfffd)
|
|
{
|
|
kbd_delchar(state);
|
|
ch = tmp;
|
|
}
|
|
else
|
|
{
|
|
state->hvowel = state->htail = 0;
|
|
state->hlead = ch;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
state->hangul = false;
|
|
}
|
|
|
|
/* insert char */
|
|
kbd_inschar(state, ch);
|
|
}
|
|
|
|
static void kbd_backspace(struct edit_state *state)
|
|
{
|
|
ucschar_t ch;
|
|
if (state->hangul)
|
|
{
|
|
if (state->htail)
|
|
state->htail = 0;
|
|
else if (state->hvowel)
|
|
state->hvowel = 0;
|
|
else
|
|
state->hangul = false;
|
|
}
|
|
|
|
kbd_delchar(state);
|
|
|
|
if (state->hangul)
|
|
{
|
|
if (state->hvowel)
|
|
ch = hangul_join(state->hlead, state->hvowel, state->htail);
|
|
else
|
|
ch = state->hlead;
|
|
kbd_inschar(state, ch);
|
|
}
|
|
}
|
|
|
|
static void kbd_move_cursor(struct edit_state *state, int dir)
|
|
{
|
|
state->hangul = false;
|
|
state->editpos += dir;
|
|
|
|
if (state->editpos >= 0 && state->editpos <= state->len_utf8)
|
|
{
|
|
state->changed = CHANGED_CURSOR;
|
|
}
|
|
else if (global_settings.list_wraparound && state->editpos > state->len_utf8)
|
|
{
|
|
state->editpos = 0;
|
|
if (global_settings.talk_menu) beep_play(1000, 150, 1500);
|
|
}
|
|
else if (global_settings.list_wraparound && state->editpos < 0)
|
|
{
|
|
state->editpos = state->len_utf8;
|
|
if (global_settings.talk_menu) beep_play(1000, 150, 1500);
|
|
}
|
|
else if (!global_settings.list_wraparound)
|
|
state->editpos -= dir;
|
|
}
|
|
|
|
static void kbd_move_picker_horizontal(struct keyboard_parameters *pm,
|
|
struct edit_state *state, int dir)
|
|
{
|
|
state->changed = CHANGED_PICKER;
|
|
|
|
pm->x += dir;
|
|
/* The space bar spans several cells but highlights and inserts as one key,
|
|
* so step over the whole run instead of stopping inside it, which looks
|
|
* like the cursor is stuck. */
|
|
if (pm->x >= 0 && pm->x < pm->max_chars &&
|
|
get_kbd_ch(pm, pm->x, pm->y) == KBD_SPACE &&
|
|
get_kbd_ch(pm, pm->x - dir, pm->y) == KBD_SPACE)
|
|
{
|
|
while (pm->x >= 0 && pm->x < pm->max_chars &&
|
|
get_kbd_ch(pm, pm->x, pm->y) == KBD_SPACE)
|
|
pm->x += dir;
|
|
}
|
|
|
|
if (pm->x < 0)
|
|
{
|
|
if (!global_settings.list_wraparound && pm->page == 0)
|
|
{
|
|
pm->x = 0;
|
|
return;
|
|
}
|
|
if (--pm->page < 0)
|
|
pm->page = pm->pages - 1;
|
|
pm->x = pm->max_chars - 1;
|
|
}
|
|
else if (pm->x >= pm->max_chars)
|
|
{
|
|
if (!global_settings.list_wraparound && pm->page == pm->pages - 1)
|
|
{
|
|
pm->x = pm->max_chars - 1;
|
|
return;
|
|
}
|
|
if (++pm->page >= pm->pages)
|
|
pm->page = 0;
|
|
pm->x = 0;
|
|
}
|
|
}
|
|
|
|
static void kbd_move_picker_vertical(struct keyboard_parameters *pm,
|
|
struct edit_state *state, int dir)
|
|
{
|
|
state->changed = CHANGED_PICKER;
|
|
|
|
#ifdef HAVE_MORSE_INPUT
|
|
if (state->morse_mode)
|
|
{
|
|
pm->line_edit = !pm->line_edit;
|
|
return;
|
|
}
|
|
#endif /* HAVE_MORSE_INPUT */
|
|
|
|
pm->y += dir;
|
|
|
|
if (!global_settings.list_wraparound)
|
|
{
|
|
#if 0 /* edit line below picker */
|
|
if (pm->y >= pm->lines)
|
|
{
|
|
pm->y = pm->lines;
|
|
pm->line_edit = true;
|
|
}
|
|
else if (pm->y < 0)
|
|
pm->y = 0;
|
|
else if (pm->line_edit)
|
|
pm->line_edit = false;
|
|
#else /* edit line above picker */
|
|
if (pm->y >= pm->lines)
|
|
{
|
|
pm->y = pm->lines;
|
|
}
|
|
else if (pm->y < 0)
|
|
{
|
|
pm->line_edit = true;
|
|
pm->y = 0;
|
|
}
|
|
else if (pm->line_edit)
|
|
{
|
|
pm->line_edit = false;
|
|
pm->y = 0;
|
|
}
|
|
#endif
|
|
return;
|
|
}
|
|
|
|
if (pm->line_edit)
|
|
{
|
|
pm->y = (dir > 0 ? 0 : pm->lines - 1);
|
|
pm->line_edit = false;
|
|
}
|
|
else if (pm->y < 0 || pm->y >= pm->lines)
|
|
{
|
|
pm->line_edit = true;
|
|
}
|
|
}
|