blob: 13fd779491d7d3eee1be37a280e14e0e046ee456 [file] [log] [blame]
Dees Troy3be70a82013-10-22 14:25:12 +00001/*
bigbiffd58ba182020-03-23 10:02:29 -04002 Copyright 2012 to 2020 TeamWin
Dees Troy3be70a82013-10-22 14:25:12 +00003 This file is part of TWRP/TeamWin Recovery Project.
4
5 TWRP is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 TWRP is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with TWRP. If not, see <http://www.gnu.org/licenses/>.
17*/
Dees_Troy51a0e822012-09-05 15:24:24 -040018
that8834a0f2016-01-05 23:29:30 +010019#include <linux/input.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040020#include <stdlib.h>
21#include <string.h>
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +000022#include "../data.hpp"
Dees_Troy51a0e822012-09-05 15:24:24 -040023
24#include <string>
25
26extern "C" {
Dees_Troy2673cec2013-04-02 20:22:16 +000027#include "../twcommon.h"
Ethan Yonker63e414f2015-02-06 15:44:39 -060028#include "gui.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040029}
bigbiffd81833a2021-01-17 11:06:57 -050030#include "minuitwrp/minui.h"
31#include "minuitwrp/truetype.hpp"
Dees_Troy51a0e822012-09-05 15:24:24 -040032
33#include "rapidxml.hpp"
34#include "objects.hpp"
35
that8834a0f2016-01-05 23:29:30 +010036bool GUIKeyboard::CtrlActive = false;
37
Dees_Troy51a0e822012-09-05 15:24:24 -040038GUIKeyboard::GUIKeyboard(xml_node<>* node)
Vojtech Bocekede51c52014-02-07 23:58:09 +010039 : GUIObject(node)
Dees_Troy51a0e822012-09-05 15:24:24 -040040{
41 int layoutindex, rowindex, keyindex, Xindex, Yindex, keyHeight = 0, keyWidth = 0;
thatf6b20662015-06-25 21:51:37 +020042 currentKey = NULL;
Ethan Yonker21ff02a2015-02-18 14:35:00 -060043 highlightRenderCount = 0;
that8834a0f2016-01-05 23:29:30 +010044 hasHighlight = hasCapsHighlight = hasCtrlHighlight = false;
Dees_Troy3a16cb52013-01-10 15:16:02 +000045 char resource[10], layout[8], row[5], key[6], longpress[7];
Dees_Troy51a0e822012-09-05 15:24:24 -040046 xml_attribute<>* attr;
47 xml_node<>* child;
48 xml_node<>* keylayout;
49 xml_node<>* keyrow;
50
thatd86f49d2015-03-15 00:56:57 +010051 for (layoutindex=0; layoutindex<MAX_KEYBOARD_LAYOUTS; layoutindex++) {
52 layouts[layoutindex].keyboardImg = NULL;
53 memset(layouts[layoutindex].keys, 0, sizeof(Layout::keys));
54 memset(layouts[layoutindex].row_end_y, 0, sizeof(Layout::row_end_y));
55 }
Dees_Troy51a0e822012-09-05 15:24:24 -040056
57 mRendered = false;
58 currentLayout = 1;
thatd86f49d2015-03-15 00:56:57 +010059 CapsLockOn = false;
Dees_Troy51a0e822012-09-05 15:24:24 -040060
61 if (!node) return;
62
Ethan Yonker21ff02a2015-02-18 14:35:00 -060063 mHighlightColor = LoadAttrColor(FindNode(node, "highlight"), "color", &hasHighlight);
64 mCapsHighlightColor = LoadAttrColor(FindNode(node, "capshighlight"), "color", &hasCapsHighlight);
that8834a0f2016-01-05 23:29:30 +010065 mCtrlHighlightColor = LoadAttrColor(FindNode(node, "ctrlhighlight"), "color", &hasCtrlHighlight);
Ethan Yonkerc3120d42014-02-17 07:55:00 -060066
thatf256b722015-05-23 20:44:12 +020067 child = FindNode(node, "keymargin");
68 mKeyMarginX = LoadAttrIntScaleX(child, "x", 0);
69 mKeyMarginY = LoadAttrIntScaleY(child, "y", 0);
70
71 child = FindNode(node, "background");
72 mBackgroundColor = LoadAttrColor(child, "color", COLOR(32,32,32,255));
73
74 child = FindNode(node, "key-alphanumeric");
75 mFont = PageManager::GetResources()->FindFont(LoadAttrString(child, "font", "keylabel"));
76 mFontColor = LoadAttrColor(child, "textcolor", COLOR(255,255,255,255));
77 mKeyColorAlphanumeric = LoadAttrColor(child, "color", COLOR(0,0,0,0));
78
79 child = FindNode(node, "key-other");
80 mSmallFont = PageManager::GetResources()->FindFont(LoadAttrString(child, "font", "keylabel-small"));
81 mFontColorSmall = LoadAttrColor(child, "textcolor", COLOR(192,192,192,255));
82 mKeyColorOther = LoadAttrColor(child, "color", COLOR(0,0,0,0));
83
84 child = FindNode(node, "longpress");
85 mLongpressFont = PageManager::GetResources()->FindFont(LoadAttrString(child, "font", "keylabel-longpress"));
86 mLongpressFontColor = LoadAttrColor(child, "textcolor", COLOR(128,128,128,255));
thatf6b20662015-06-25 21:51:37 +020087 LoadPlacement(child, &longpressOffsetX, &longpressOffsetY);
thatf256b722015-05-23 20:44:12 +020088
89 LoadKeyLabels(node, 0); // load global key labels
90
thatd86f49d2015-03-15 00:56:57 +010091 // compatibility ugliness: resources should be specified in the layouts themselves instead
Dees_Troy51a0e822012-09-05 15:24:24 -040092 // Load the images for the different layouts
Ethan Yonker21ff02a2015-02-18 14:35:00 -060093 child = FindNode(node, "layout");
Dees_Troy51a0e822012-09-05 15:24:24 -040094 if (child)
95 {
96 layoutindex = 1;
97 strcpy(resource, "resource1");
98 attr = child->first_attribute(resource);
99 while (attr && layoutindex < (MAX_KEYBOARD_LAYOUTS + 1)) {
thatd86f49d2015-03-15 00:56:57 +0100100 layouts[layoutindex - 1].keyboardImg = LoadAttrImage(child, resource);
Dees_Troy51a0e822012-09-05 15:24:24 -0400101
102 layoutindex++;
103 resource[8] = (char)(layoutindex + 48);
104 attr = child->first_attribute(resource);
105 }
106 }
107
108 // Check the first image to get height and width
thatd86f49d2015-03-15 00:56:57 +0100109 if (layouts[0].keyboardImg && layouts[0].keyboardImg->GetResource())
Dees_Troy51a0e822012-09-05 15:24:24 -0400110 {
thatf256b722015-05-23 20:44:12 +0200111 mRenderW = layouts[0].keyboardImg->GetWidth();
112 mRenderH = layouts[0].keyboardImg->GetHeight();
Dees_Troy51a0e822012-09-05 15:24:24 -0400113 }
114
115 // Load all of the layout maps
116 layoutindex = 1;
117 strcpy(layout, "layout1");
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600118 keylayout = FindNode(node, layout);
Dees_Troy51a0e822012-09-05 15:24:24 -0400119 while (keylayout)
120 {
121 if (layoutindex > MAX_KEYBOARD_LAYOUTS) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000122 LOGERR("Too many layouts defined in keyboard.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400123 return;
124 }
125
thatf256b722015-05-23 20:44:12 +0200126 LoadKeyLabels(keylayout, layoutindex); // load per-layout key labels
127
thatd86f49d2015-03-15 00:56:57 +0100128 Layout& lay = layouts[layoutindex - 1];
129
Dees_Troy51a0e822012-09-05 15:24:24 -0400130 child = keylayout->first_node("keysize");
thatd86f49d2015-03-15 00:56:57 +0100131 keyHeight = LoadAttrIntScaleY(child, "height", 0);
132 keyWidth = LoadAttrIntScaleX(child, "width", 0);
133 // compatibility ugliness: capslock="0" means that this is the caps layout. Also it has nothing to do with keysize.
134 lay.is_caps = (LoadAttrInt(child, "capslock", 1) == 0);
135 // compatibility ugliness: revert_layout has nothing to do with keysize.
136 lay.revert_layout = LoadAttrInt(child, "revert_layout", -1);
Dees_Troy51a0e822012-09-05 15:24:24 -0400137
138 rowindex = 1;
139 Yindex = 0;
140 strcpy(row, "row1");
141 keyrow = keylayout->first_node(row);
142 while (keyrow)
143 {
144 if (rowindex > MAX_KEYBOARD_ROWS) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000145 LOGERR("Too many rows defined in keyboard.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400146 return;
147 }
148
149 Yindex += keyHeight;
thatd86f49d2015-03-15 00:56:57 +0100150 lay.row_end_y[rowindex - 1] = Yindex;
Dees_Troy51a0e822012-09-05 15:24:24 -0400151
152 keyindex = 1;
153 Xindex = 0;
154 strcpy(key, "key01");
155 attr = keyrow->first_attribute(key);
156
157 while (attr) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400158 if (keyindex > MAX_KEYBOARD_KEYS) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000159 LOGERR("Too many keys defined in a keyboard row.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400160 return;
161 }
162
that1a7ba972015-02-01 19:48:19 +0100163 const char* keyinfo = attr->value();
Dees_Troy51a0e822012-09-05 15:24:24 -0400164
165 if (strlen(keyinfo) == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000166 LOGERR("No key info on layout%i, row%i, key%dd.\n", layoutindex, rowindex, keyindex);
Dees_Troy51a0e822012-09-05 15:24:24 -0400167 return;
168 }
169
thatd86f49d2015-03-15 00:56:57 +0100170 if (ParseKey(keyinfo, lay.keys[rowindex - 1][keyindex - 1], Xindex, keyWidth, false))
that1a7ba972015-02-01 19:48:19 +0100171 LOGERR("Invalid key info on layout%i, row%i, key%02i.\n", layoutindex, rowindex, keyindex);
Dees_Troy51a0e822012-09-05 15:24:24 -0400172
Dees_Troy51a0e822012-09-05 15:24:24 -0400173
174 // PROCESS LONG PRESS INFO IF EXISTS
175 sprintf(longpress, "long%02i", keyindex);
176 attr = keyrow->first_attribute(longpress);
177 if (attr) {
that1a7ba972015-02-01 19:48:19 +0100178 const char* keyinfo = attr->value();
Dees_Troy51a0e822012-09-05 15:24:24 -0400179
180 if (strlen(keyinfo) == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000181 LOGERR("No long press info on layout%i, row%i, long%dd.\n", layoutindex, rowindex, keyindex);
Dees_Troy51a0e822012-09-05 15:24:24 -0400182 return;
183 }
184
thatd86f49d2015-03-15 00:56:57 +0100185 if (ParseKey(keyinfo, lay.keys[rowindex - 1][keyindex - 1], Xindex, keyWidth, true))
that1a7ba972015-02-01 19:48:19 +0100186 LOGERR("Invalid long press key info on layout%i, row%i, long%02i.\n", layoutindex, rowindex, keyindex);
Dees_Troy51a0e822012-09-05 15:24:24 -0400187 }
188 keyindex++;
189 sprintf(key, "key%02i", keyindex);
190 attr = keyrow->first_attribute(key);
191 }
192 rowindex++;
193 row[3] = (char)(rowindex + 48);
194 keyrow = keylayout->first_node(row);
195 }
196 layoutindex++;
197 layout[6] = (char)(layoutindex + 48);
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600198 keylayout = FindNode(node, layout);
Dees_Troy51a0e822012-09-05 15:24:24 -0400199 }
200
thate79878b2015-03-14 23:07:23 +0100201 int x, y;
Dees_Troy51a0e822012-09-05 15:24:24 -0400202 // Load the placement
thatf256b722015-05-23 20:44:12 +0200203 LoadPlacement(FindNode(node, "placement"), &x, &y, &mRenderW, &mRenderH);
204 SetRenderPos(x, y, mRenderW, mRenderH);
Dees_Troy51a0e822012-09-05 15:24:24 -0400205 return;
206}
207
208GUIKeyboard::~GUIKeyboard()
209{
that1a7ba972015-02-01 19:48:19 +0100210}
Dees_Troy51a0e822012-09-05 15:24:24 -0400211
thate79878b2015-03-14 23:07:23 +0100212int GUIKeyboard::ParseKey(const char* keyinfo, Key& key, int& Xindex, int keyWidth, bool longpress)
that1a7ba972015-02-01 19:48:19 +0100213{
thatf256b722015-05-23 20:44:12 +0200214 key.layout = 0;
that1a7ba972015-02-01 19:48:19 +0100215 int keychar = 0;
216 if (strlen(keyinfo) == 1) {
217 // This is a single key, simple definition
218 keychar = keyinfo[0];
219 } else {
220 // This key has extra data: {keywidth}:{what_the_key_does}
Ethan Yonker63e414f2015-02-06 15:44:39 -0600221 keyWidth = scale_theme_x(atoi(keyinfo));
that1a7ba972015-02-01 19:48:19 +0100222
223 const char* ptr = keyinfo;
224 while (*ptr > 32 && *ptr != ':')
225 ptr++;
226 if (*ptr != ':')
227 return -1; // no colon is an error
228 ptr++;
229
230 if (*ptr == 0) { // This is an empty area
231 keychar = 0;
232 } else if (strlen(ptr) == 1) { // This is the character that this key uses
233 keychar = *ptr;
234 } else if (*ptr == 'c') { // This is an ASCII character code: "c:{number}"
235 keychar = atoi(ptr + 2);
that8834a0f2016-01-05 23:29:30 +0100236 } else if (*ptr == 'k') { // This is a Linux keycode from input.h: "k:{number}"
237 keychar = -atoi(ptr + 2);
that1a7ba972015-02-01 19:48:19 +0100238 } else if (*ptr == 'l') { // This is a different layout: "layout{number}"
that1a7ba972015-02-01 19:48:19 +0100239 key.layout = atoi(ptr + 6);
that8834a0f2016-01-05 23:29:30 +0100240 } else if (*ptr == 'a') { // This is an action: "action" (the Enter key)
that1a7ba972015-02-01 19:48:19 +0100241 keychar = KEYBOARD_ACTION;
242 } else
243 return -1;
244 }
245
246 if (longpress) {
247 key.longpresskey = keychar;
248 } else {
249 key.key = keychar;
250 Xindex += keyWidth;
251 key.end_x = Xindex - 1;
252 }
253
254 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400255}
256
thatf256b722015-05-23 20:44:12 +0200257void GUIKeyboard::LoadKeyLabels(xml_node<>* parent, int layout)
258{
259 for (xml_node<>* child = parent->first_node(); child; child = child->next_sibling()) {
260 std::string name = child->name();
261 if (name == "keylabel") {
262 std::string keydef = LoadAttrString(child, "key", "");
263 Key tempkey;
264 int dummyX;
265 if (ParseKey(keydef.c_str(), tempkey, dummyX, 0, false) == 0) {
266 KeyLabel keylabel;
267 keylabel.key = tempkey.key;
268 keylabel.layout_from = layout;
269 keylabel.layout_to = tempkey.layout;
270 keylabel.text = LoadAttrString(child, "text", "");
271 keylabel.image = LoadAttrImage(child, "resource");
272 mKeyLabels.push_back(keylabel);
273 } else {
274 LOGERR("Ignoring invalid keylabel in layout %d: '%s'.\n", layout, keydef.c_str());
275 }
276 }
277 }
278}
279
thatf6b20662015-06-25 21:51:37 +0200280void GUIKeyboard::DrawKey(Key& key, int keyX, int keyY, int keyW, int keyH)
281{
that8834a0f2016-01-05 23:29:30 +0100282 int keychar = key.key;
283 if (!keychar && !key.layout)
thatf6b20662015-06-25 21:51:37 +0200284 return;
285
286 // key background
287 COLOR& c = (keychar >= 32 && keychar < 127) ? mKeyColorAlphanumeric : mKeyColorOther;
288 gr_color(c.red, c.green, c.blue, c.alpha);
289 keyX += mKeyMarginX;
290 keyY += mKeyMarginY;
291 keyW -= mKeyMarginX * 2;
292 keyH -= mKeyMarginY * 2;
293 gr_fill(keyX, keyY, keyW, keyH);
294
295 // key label
296 FontResource* labelFont = mFont;
297 string labelText;
298 ImageResource* labelImage = NULL;
299 if (keychar > 32 && keychar < 127) {
that8834a0f2016-01-05 23:29:30 +0100300 // TODO: this will eventually need UTF-8 support
thatf6b20662015-06-25 21:51:37 +0200301 labelText = (char) keychar;
that8834a0f2016-01-05 23:29:30 +0100302 if (CtrlActive) {
303 int ctrlchar = KeyCharToCtrlChar(keychar);
304 if (ctrlchar != keychar)
305 labelText = std::string("^") + (char)(ctrlchar + 64);
306 }
thatf6b20662015-06-25 21:51:37 +0200307 gr_color(mFontColor.red, mFontColor.green, mFontColor.blue, mFontColor.alpha);
308 }
309 else {
310 // search for a special key label
311 for (std::vector<KeyLabel>::iterator it = mKeyLabels.begin(); it != mKeyLabels.end(); ++it) {
312 if (it->layout_from > 0 && it->layout_from != currentLayout)
313 continue; // this label is for another layout
314 if (it->key == key.key && it->layout_to == key.layout)
315 {
316 // found a label
317 labelText = it->text;
318 labelImage = it->image;
319 break;
320 }
321 }
322 labelFont = mSmallFont;
323 gr_color(mFontColorSmall.red, mFontColorSmall.green, mFontColorSmall.blue, mFontColorSmall.alpha);
324 }
325
Ethan Yonker58f21322018-08-24 11:17:36 -0500326 if (labelImage && labelImage->GetResource())
thatf6b20662015-06-25 21:51:37 +0200327 {
328 int w = labelImage->GetWidth();
329 int h = labelImage->GetHeight();
330 int x = keyX + (keyW - w) / 2;
331 int y = keyY + (keyH - h) / 2;
332 gr_blit(labelImage->GetResource(), 0, 0, w, h, x, y);
333 }
Ethan Yonker58f21322018-08-24 11:17:36 -0500334 else if (!labelText.empty() && labelFont && labelFont->GetResource())
thatf6b20662015-06-25 21:51:37 +0200335 {
336 void* fontResource = labelFont->GetResource();
bigbiffd58ba182020-03-23 10:02:29 -0400337 int textW = twrpTruetype::gr_ttf_measureEx(labelText.c_str(), fontResource);
thatf6b20662015-06-25 21:51:37 +0200338 int textH = labelFont->GetHeight();
339 int textX = keyX + (keyW - textW) / 2;
340 int textY = keyY + (keyH - textH) / 2;
Ethan Yonkerb7a54a32015-10-05 10:16:27 -0500341 gr_textEx_scaleW(textX, textY, labelText.c_str(), fontResource, keyW, TOP_LEFT, 0);
thatf6b20662015-06-25 21:51:37 +0200342 }
343
344 // longpress key label (only if font is defined)
345 keychar = key.longpresskey;
Ethan Yonker58f21322018-08-24 11:17:36 -0500346 if (keychar > 32 && keychar < 127 && mLongpressFont && mLongpressFont->GetResource()) {
thatf6b20662015-06-25 21:51:37 +0200347 void* fontResource = mLongpressFont->GetResource();
348 gr_color(mLongpressFontColor.red, mLongpressFontColor.green, mLongpressFontColor.blue, mLongpressFontColor.alpha);
349 string text(1, keychar);
bigbiffd58ba182020-03-23 10:02:29 -0400350 int textW = twrpTruetype::gr_ttf_measureEx(text.c_str(), fontResource);
thatf6b20662015-06-25 21:51:37 +0200351 int textX = keyX + keyW - longpressOffsetX - textW;
352 int textY = keyY + longpressOffsetY;
Ethan Yonkerb7a54a32015-10-05 10:16:27 -0500353 gr_textEx_scaleW(textX, textY, text.c_str(), fontResource, keyW, TOP_LEFT, 0);
thatf6b20662015-06-25 21:51:37 +0200354 }
355}
356
that8834a0f2016-01-05 23:29:30 +0100357int GUIKeyboard::KeyCharToCtrlChar(int key)
358{
359 // convert upper and lower case to ctrl chars
360 // Ctrl+A to Ctrl+_ (we don't support entering null bytes)
361 if (key >= 65 && key <= 127 && key != 96)
362 return key & 0x1f;
363 return key; // pass on others (already ctrl chars, numbers, etc.) unchanged
364}
365
Dees_Troy51a0e822012-09-05 15:24:24 -0400366int GUIKeyboard::Render(void)
367{
368 if (!isConditionTrue())
369 {
370 mRendered = false;
371 return 0;
372 }
373
thatd86f49d2015-03-15 00:56:57 +0100374 Layout& lay = layouts[currentLayout - 1];
Dees_Troy51a0e822012-09-05 15:24:24 -0400375
thatf6b20662015-06-25 21:51:37 +0200376 bool drawKeys = false;
thatd86f49d2015-03-15 00:56:57 +0100377 if (lay.keyboardImg && lay.keyboardImg->GetResource())
thatf6b20662015-06-25 21:51:37 +0200378 // keyboard is image based
thatf256b722015-05-23 20:44:12 +0200379 gr_blit(lay.keyboardImg->GetResource(), 0, 0, mRenderW, mRenderH, mRenderX, mRenderY);
380 else {
thatf6b20662015-06-25 21:51:37 +0200381 // keyboard is software drawn
382 // fill background
thatf256b722015-05-23 20:44:12 +0200383 gr_color(mBackgroundColor.red, mBackgroundColor.green, mBackgroundColor.blue, mBackgroundColor.alpha);
384 gr_fill(mRenderX, mRenderY, mRenderW, mRenderH);
thatf6b20662015-06-25 21:51:37 +0200385 drawKeys = true;
386 }
thatf256b722015-05-23 20:44:12 +0200387
thatf6b20662015-06-25 21:51:37 +0200388 // draw keys
389 int y1 = 0;
390 for (int row = 0; row < MAX_KEYBOARD_ROWS; ++row) {
391 int rowY = mRenderY + y1;
392 int rowH = lay.row_end_y[row] - y1;
393 y1 = lay.row_end_y[row];
394 int x1 = 0;
395 for (int col = 0; col < MAX_KEYBOARD_KEYS; ++col) {
396 Key& key = lay.keys[row][col];
397 int keyY = rowY;
398 int keyH = rowH;
399 int keyX = mRenderX + x1;
400 int keyW = key.end_x - x1;
401 x1 = key.end_x;
thatf256b722015-05-23 20:44:12 +0200402
thatf6b20662015-06-25 21:51:37 +0200403 // Draw key for software drawn keyboard
404 if (drawKeys)
405 DrawKey(key, keyX, keyY, keyW, keyH);
406
407 // Draw highlight for capslock
that8834a0f2016-01-05 23:29:30 +0100408 if (hasCapsHighlight && lay.is_caps && CapsLockOn && key.layout > 0 && key.layout == lay.revert_layout) {
thatf6b20662015-06-25 21:51:37 +0200409 gr_color(mCapsHighlightColor.red, mCapsHighlightColor.green, mCapsHighlightColor.blue, mCapsHighlightColor.alpha);
thatf256b722015-05-23 20:44:12 +0200410 gr_fill(keyX, keyY, keyW, keyH);
thatf6b20662015-06-25 21:51:37 +0200411 }
thatf256b722015-05-23 20:44:12 +0200412
that8834a0f2016-01-05 23:29:30 +0100413 // Draw highlight for control
414 if (hasCtrlHighlight && key.key == -KEY_LEFTCTRL && CtrlActive) {
415 gr_color(mCtrlHighlightColor.red, mCtrlHighlightColor.green, mCtrlHighlightColor.blue, mCtrlHighlightColor.alpha);
416 gr_fill(keyX, keyY, keyW, keyH);
417 }
418
thatf6b20662015-06-25 21:51:37 +0200419 // Highlight current key
420 if (hasHighlight && &key == currentKey && highlightRenderCount != 0) {
421 gr_color(mHighlightColor.red, mHighlightColor.green, mHighlightColor.blue, mHighlightColor.alpha);
422 gr_fill(keyX, keyY, keyW, keyH);
thatf256b722015-05-23 20:44:12 +0200423 }
424 }
425 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400426
thatf6b20662015-06-25 21:51:37 +0200427 if (!hasHighlight || highlightRenderCount == 0)
Dees_Troy30b962e2012-10-19 20:48:59 -0400428 mRendered = true;
thatf6b20662015-06-25 21:51:37 +0200429 else if (highlightRenderCount > 0)
430 highlightRenderCount--;
thatd86f49d2015-03-15 00:56:57 +0100431 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400432}
433
434int GUIKeyboard::Update(void)
435{
436 if (!isConditionTrue()) return (mRendered ? 2 : 0);
437 if (!mRendered) return 2;
438
439 return 0;
440}
441
442int GUIKeyboard::SetRenderPos(int x, int y, int w, int h)
443{
thatf256b722015-05-23 20:44:12 +0200444 RenderObject::SetRenderPos(x, y, w, h);
Dees_Troy51a0e822012-09-05 15:24:24 -0400445 SetActionPos(mRenderX, mRenderY, mRenderW, mRenderH);
446 return 0;
447}
448
thate79878b2015-03-14 23:07:23 +0100449GUIKeyboard::Key* GUIKeyboard::HitTestKey(int x, int y)
Dees_Troy51a0e822012-09-05 15:24:24 -0400450{
thate79878b2015-03-14 23:07:23 +0100451 if (!IsInRegion(x, y))
452 return NULL;
453
454 int rely = y - mRenderY;
455 int relx = x - mRenderX;
456
thatd86f49d2015-03-15 00:56:57 +0100457 Layout& lay = layouts[currentLayout - 1];
458
thate79878b2015-03-14 23:07:23 +0100459 // Find the correct row
460 int row;
461 for (row = 0; row < MAX_KEYBOARD_ROWS; ++row) {
thatd86f49d2015-03-15 00:56:57 +0100462 if (lay.row_end_y[row] > rely)
thate79878b2015-03-14 23:07:23 +0100463 break;
464 }
465 if (row == MAX_KEYBOARD_ROWS)
466 return NULL;
467
468 // Find the correct key (column)
469 int col;
470 int x1 = 0;
471 for (col = 0; col < MAX_KEYBOARD_KEYS; ++col) {
thatd86f49d2015-03-15 00:56:57 +0100472 Key& key = lay.keys[row][col];
that8834a0f2016-01-05 23:29:30 +0100473 if (x1 <= relx && relx < key.end_x && (key.key != 0 || key.layout != 0)) {
thate79878b2015-03-14 23:07:23 +0100474 // This is the key that was pressed!
thate79878b2015-03-14 23:07:23 +0100475 return &key;
476 }
477 x1 = key.end_x;
478 }
479 return NULL;
Dees_Troy51a0e822012-09-05 15:24:24 -0400480}
481
482int GUIKeyboard::NotifyTouch(TOUCH_STATE state, int x, int y)
483{
thate79878b2015-03-14 23:07:23 +0100484 static int was_held = 0, startX = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400485
486 if (!isConditionTrue()) return -1;
487
488 switch (state)
489 {
490 case TOUCH_START:
thate79878b2015-03-14 23:07:23 +0100491 was_held = 0;
492 startX = x;
thatf6b20662015-06-25 21:51:37 +0200493 currentKey = HitTestKey(x, y);
494 if (currentKey)
thate79878b2015-03-14 23:07:23 +0100495 highlightRenderCount = -1;
496 else
Dees_Troy30b962e2012-10-19 20:48:59 -0400497 highlightRenderCount = 0;
thate79878b2015-03-14 23:07:23 +0100498 mRendered = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400499 break;
thate79878b2015-03-14 23:07:23 +0100500
Dees_Troy51a0e822012-09-05 15:24:24 -0400501 case TOUCH_DRAG:
502 break;
thate79878b2015-03-14 23:07:23 +0100503
Dees_Troy51a0e822012-09-05 15:24:24 -0400504 case TOUCH_RELEASE:
that8834a0f2016-01-05 23:29:30 +0100505 // TODO: we might want to notify of key releases here
thatf256b722015-05-23 20:44:12 +0200506 if (x < startX - (mRenderW * 0.5)) {
Dees_Troy30b962e2012-10-19 20:48:59 -0400507 if (highlightRenderCount != 0) {
508 highlightRenderCount = 0;
509 mRendered = false;
510 }
that8834a0f2016-01-05 23:29:30 +0100511 PageManager::NotifyCharInput(KEYBOARD_SWIPE_LEFT);
Dees_Troy51a0e822012-09-05 15:24:24 -0400512 return 0;
thatf256b722015-05-23 20:44:12 +0200513 } else if (x > startX + (mRenderW * 0.5)) {
Dees_Troy30b962e2012-10-19 20:48:59 -0400514 if (highlightRenderCount != 0) {
515 highlightRenderCount = 0;
516 mRendered = false;
517 }
that8834a0f2016-01-05 23:29:30 +0100518 PageManager::NotifyCharInput(KEYBOARD_SWIPE_RIGHT);
Dees_Troy51a0e822012-09-05 15:24:24 -0400519 return 0;
520 }
thate79878b2015-03-14 23:07:23 +0100521 // fall through
Dees_Troy51a0e822012-09-05 15:24:24 -0400522 case TOUCH_HOLD:
523 case TOUCH_REPEAT:
thatf6b20662015-06-25 21:51:37 +0200524 if (!currentKey) {
Dees_Troy30b962e2012-10-19 20:48:59 -0400525 if (highlightRenderCount != 0) {
526 highlightRenderCount = 0;
527 mRendered = false;
528 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400529 return 0;
thate79878b2015-03-14 23:07:23 +0100530 }
531
532 if (highlightRenderCount != 0) {
Dees_Troy30b962e2012-10-19 20:48:59 -0400533 if (state == TOUCH_RELEASE)
534 highlightRenderCount = 2;
535 else
536 highlightRenderCount = -1;
537 mRendered = false;
538 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400539
thatf6b20662015-06-25 21:51:37 +0200540 if (HitTestKey(x, y) != currentKey) {
thate79878b2015-03-14 23:07:23 +0100541 // We dragged off of the starting key
thatf6b20662015-06-25 21:51:37 +0200542 currentKey = NULL;
thate79878b2015-03-14 23:07:23 +0100543 if (highlightRenderCount != 0) {
544 highlightRenderCount = 0;
545 mRendered = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400546 }
thate79878b2015-03-14 23:07:23 +0100547 return 0;
548 } else {
thatf6b20662015-06-25 21:51:37 +0200549 Key& key = *currentKey;
that8834a0f2016-01-05 23:29:30 +0100550 bool repeatKey = false;
thatd86f49d2015-03-15 00:56:57 +0100551 Layout& lay = layouts[currentLayout - 1];
thate79878b2015-03-14 23:07:23 +0100552 if (state == TOUCH_RELEASE && was_held == 0) {
bigbiff bigbiff3ed778a2019-03-12 19:28:31 -0400553
554#ifndef TW_NO_HAPTICS
thate79878b2015-03-14 23:07:23 +0100555 DataManager::Vibrate("tw_keyboard_vibrate");
bigbiff bigbiff3ed778a2019-03-12 19:28:31 -0400556#endif
557
that8834a0f2016-01-05 23:29:30 +0100558 if (key.layout > 0) {
thate79878b2015-03-14 23:07:23 +0100559 // Switch layouts
thatd86f49d2015-03-15 00:56:57 +0100560 if (lay.is_caps && key.layout == lay.revert_layout && !CapsLockOn) {
561 CapsLockOn = true; // Set the caps lock
thate79878b2015-03-14 23:07:23 +0100562 } else {
thatd86f49d2015-03-15 00:56:57 +0100563 CapsLockOn = false; // Unset the caps lock and change layouts
thate79878b2015-03-14 23:07:23 +0100564 currentLayout = key.layout;
Dees_Troy51a0e822012-09-05 15:24:24 -0400565 }
thate79878b2015-03-14 23:07:23 +0100566 mRendered = false;
that8834a0f2016-01-05 23:29:30 +0100567 } else if (key.key == KEYBOARD_ACTION) {
thate79878b2015-03-14 23:07:23 +0100568 // Action
569 highlightRenderCount = 0;
570 // Send action notification
that8834a0f2016-01-05 23:29:30 +0100571 PageManager::NotifyCharInput(key.key);
572 } else if (key.key == -KEY_LEFTCTRL) {
573 CtrlActive = !CtrlActive; // toggle Control key state
574 mRendered = false; // render Ctrl key highlight
575 } else if (key.key != 0) {
thatd86f49d2015-03-15 00:56:57 +0100576 // Regular key
that8834a0f2016-01-05 23:29:30 +0100577 if (key.key > 0) {
578 // ASCII code or character
579 int keycode = key.key;
580 if (CtrlActive) {
581 CtrlActive = false;
582 mRendered = false;
583 keycode = KeyCharToCtrlChar(key.key);
584 }
585 PageManager::NotifyCharInput(keycode);
586 } else {
587 // Linux key code
588 PageManager::NotifyKey(-key.key, true);
589 PageManager::NotifyKey(-key.key, false);
590 }
thatd86f49d2015-03-15 00:56:57 +0100591 if (!CapsLockOn && lay.is_caps) {
592 // caps lock was not set, change layouts
593 currentLayout = lay.revert_layout;
594 mRendered = false;
595 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400596 }
thate79878b2015-03-14 23:07:23 +0100597 } else if (state == TOUCH_HOLD) {
598 was_held = 1;
that8834a0f2016-01-05 23:29:30 +0100599 if (key.longpresskey > 0) {
thate79878b2015-03-14 23:07:23 +0100600 // Long Press Key
bigbiff bigbiff3ed778a2019-03-12 19:28:31 -0400601
602#ifndef TW_NO_HAPTICS
thate79878b2015-03-14 23:07:23 +0100603 DataManager::Vibrate("tw_keyboard_vibrate");
bigbiff bigbiff3ed778a2019-03-12 19:28:31 -0400604#endif
605
that8834a0f2016-01-05 23:29:30 +0100606 PageManager::NotifyCharInput(key.longpresskey);
thate79878b2015-03-14 23:07:23 +0100607 }
that8834a0f2016-01-05 23:29:30 +0100608 else
609 repeatKey = true;
thate79878b2015-03-14 23:07:23 +0100610 } else if (state == TOUCH_REPEAT) {
611 was_held = 1;
that8834a0f2016-01-05 23:29:30 +0100612 repeatKey = true;
613 }
614 if (repeatKey) {
615 if (key.key == KEYBOARD_BACKSPACE) {
thate79878b2015-03-14 23:07:23 +0100616 // Repeat backspace
that8834a0f2016-01-05 23:29:30 +0100617 PageManager::NotifyCharInput(key.key);
618 }
619 switch (key.key)
620 {
621 // Repeat arrows
622 case -KEY_LEFT:
623 case -KEY_RIGHT:
624 case -KEY_UP:
625 case -KEY_DOWN:
626 PageManager::NotifyKey(-key.key, true);
627 PageManager::NotifyKey(-key.key, false);
628 break;
thate79878b2015-03-14 23:07:23 +0100629 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400630 }
631 }
632 break;
633 }
634
635 return 0;
636}
that8834a0f2016-01-05 23:29:30 +0100637
638void GUIKeyboard::SetPageFocus(int inFocus)
639{
640 if (inFocus)
641 CtrlActive = false;
642}