blob: ca7006dc36d3c1fd93a76d53662cfb9be9b0776c [file] [log] [blame]
Dees Troy3be70a82013-10-22 14:25:12 +00001/*
2 Copyright 2012 bigbiff/Dees_Troy TeamWin
3 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}
Ethan Yonkerfbb43532015-12-28 21:54:50 +010030#include "../minuitwrp/minui.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040031
32#include "rapidxml.hpp"
33#include "objects.hpp"
34
that8834a0f2016-01-05 23:29:30 +010035bool GUIKeyboard::CtrlActive = false;
36
Dees_Troy51a0e822012-09-05 15:24:24 -040037GUIKeyboard::GUIKeyboard(xml_node<>* node)
Vojtech Bocekede51c52014-02-07 23:58:09 +010038 : GUIObject(node)
Dees_Troy51a0e822012-09-05 15:24:24 -040039{
40 int layoutindex, rowindex, keyindex, Xindex, Yindex, keyHeight = 0, keyWidth = 0;
thatf6b20662015-06-25 21:51:37 +020041 currentKey = NULL;
Ethan Yonker21ff02a2015-02-18 14:35:00 -060042 highlightRenderCount = 0;
that8834a0f2016-01-05 23:29:30 +010043 hasHighlight = hasCapsHighlight = hasCtrlHighlight = false;
Dees_Troy3a16cb52013-01-10 15:16:02 +000044 char resource[10], layout[8], row[5], key[6], longpress[7];
Dees_Troy51a0e822012-09-05 15:24:24 -040045 xml_attribute<>* attr;
46 xml_node<>* child;
47 xml_node<>* keylayout;
48 xml_node<>* keyrow;
49
thatd86f49d2015-03-15 00:56:57 +010050 for (layoutindex=0; layoutindex<MAX_KEYBOARD_LAYOUTS; layoutindex++) {
51 layouts[layoutindex].keyboardImg = NULL;
52 memset(layouts[layoutindex].keys, 0, sizeof(Layout::keys));
53 memset(layouts[layoutindex].row_end_y, 0, sizeof(Layout::row_end_y));
54 }
Dees_Troy51a0e822012-09-05 15:24:24 -040055
56 mRendered = false;
57 currentLayout = 1;
thatd86f49d2015-03-15 00:56:57 +010058 CapsLockOn = false;
Dees_Troy51a0e822012-09-05 15:24:24 -040059
60 if (!node) return;
61
Ethan Yonker21ff02a2015-02-18 14:35:00 -060062 mHighlightColor = LoadAttrColor(FindNode(node, "highlight"), "color", &hasHighlight);
63 mCapsHighlightColor = LoadAttrColor(FindNode(node, "capshighlight"), "color", &hasCapsHighlight);
that8834a0f2016-01-05 23:29:30 +010064 mCtrlHighlightColor = LoadAttrColor(FindNode(node, "ctrlhighlight"), "color", &hasCtrlHighlight);
Ethan Yonkerc3120d42014-02-17 07:55:00 -060065
thatf256b722015-05-23 20:44:12 +020066 child = FindNode(node, "keymargin");
67 mKeyMarginX = LoadAttrIntScaleX(child, "x", 0);
68 mKeyMarginY = LoadAttrIntScaleY(child, "y", 0);
69
70 child = FindNode(node, "background");
71 mBackgroundColor = LoadAttrColor(child, "color", COLOR(32,32,32,255));
72
73 child = FindNode(node, "key-alphanumeric");
74 mFont = PageManager::GetResources()->FindFont(LoadAttrString(child, "font", "keylabel"));
75 mFontColor = LoadAttrColor(child, "textcolor", COLOR(255,255,255,255));
76 mKeyColorAlphanumeric = LoadAttrColor(child, "color", COLOR(0,0,0,0));
77
78 child = FindNode(node, "key-other");
79 mSmallFont = PageManager::GetResources()->FindFont(LoadAttrString(child, "font", "keylabel-small"));
80 mFontColorSmall = LoadAttrColor(child, "textcolor", COLOR(192,192,192,255));
81 mKeyColorOther = LoadAttrColor(child, "color", COLOR(0,0,0,0));
82
83 child = FindNode(node, "longpress");
84 mLongpressFont = PageManager::GetResources()->FindFont(LoadAttrString(child, "font", "keylabel-longpress"));
85 mLongpressFontColor = LoadAttrColor(child, "textcolor", COLOR(128,128,128,255));
thatf6b20662015-06-25 21:51:37 +020086 LoadPlacement(child, &longpressOffsetX, &longpressOffsetY);
thatf256b722015-05-23 20:44:12 +020087
88 LoadKeyLabels(node, 0); // load global key labels
89
thatd86f49d2015-03-15 00:56:57 +010090 // compatibility ugliness: resources should be specified in the layouts themselves instead
Dees_Troy51a0e822012-09-05 15:24:24 -040091 // Load the images for the different layouts
Ethan Yonker21ff02a2015-02-18 14:35:00 -060092 child = FindNode(node, "layout");
Dees_Troy51a0e822012-09-05 15:24:24 -040093 if (child)
94 {
95 layoutindex = 1;
96 strcpy(resource, "resource1");
97 attr = child->first_attribute(resource);
98 while (attr && layoutindex < (MAX_KEYBOARD_LAYOUTS + 1)) {
thatd86f49d2015-03-15 00:56:57 +010099 layouts[layoutindex - 1].keyboardImg = LoadAttrImage(child, resource);
Dees_Troy51a0e822012-09-05 15:24:24 -0400100
101 layoutindex++;
102 resource[8] = (char)(layoutindex + 48);
103 attr = child->first_attribute(resource);
104 }
105 }
106
107 // Check the first image to get height and width
thatd86f49d2015-03-15 00:56:57 +0100108 if (layouts[0].keyboardImg && layouts[0].keyboardImg->GetResource())
Dees_Troy51a0e822012-09-05 15:24:24 -0400109 {
thatf256b722015-05-23 20:44:12 +0200110 mRenderW = layouts[0].keyboardImg->GetWidth();
111 mRenderH = layouts[0].keyboardImg->GetHeight();
Dees_Troy51a0e822012-09-05 15:24:24 -0400112 }
113
114 // Load all of the layout maps
115 layoutindex = 1;
116 strcpy(layout, "layout1");
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600117 keylayout = FindNode(node, layout);
Dees_Troy51a0e822012-09-05 15:24:24 -0400118 while (keylayout)
119 {
120 if (layoutindex > MAX_KEYBOARD_LAYOUTS) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000121 LOGERR("Too many layouts defined in keyboard.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400122 return;
123 }
124
thatf256b722015-05-23 20:44:12 +0200125 LoadKeyLabels(keylayout, layoutindex); // load per-layout key labels
126
thatd86f49d2015-03-15 00:56:57 +0100127 Layout& lay = layouts[layoutindex - 1];
128
Dees_Troy51a0e822012-09-05 15:24:24 -0400129 child = keylayout->first_node("keysize");
thatd86f49d2015-03-15 00:56:57 +0100130 keyHeight = LoadAttrIntScaleY(child, "height", 0);
131 keyWidth = LoadAttrIntScaleX(child, "width", 0);
132 // compatibility ugliness: capslock="0" means that this is the caps layout. Also it has nothing to do with keysize.
133 lay.is_caps = (LoadAttrInt(child, "capslock", 1) == 0);
134 // compatibility ugliness: revert_layout has nothing to do with keysize.
135 lay.revert_layout = LoadAttrInt(child, "revert_layout", -1);
Dees_Troy51a0e822012-09-05 15:24:24 -0400136
137 rowindex = 1;
138 Yindex = 0;
139 strcpy(row, "row1");
140 keyrow = keylayout->first_node(row);
141 while (keyrow)
142 {
143 if (rowindex > MAX_KEYBOARD_ROWS) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000144 LOGERR("Too many rows defined in keyboard.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400145 return;
146 }
147
148 Yindex += keyHeight;
thatd86f49d2015-03-15 00:56:57 +0100149 lay.row_end_y[rowindex - 1] = Yindex;
Dees_Troy51a0e822012-09-05 15:24:24 -0400150
151 keyindex = 1;
152 Xindex = 0;
153 strcpy(key, "key01");
154 attr = keyrow->first_attribute(key);
155
156 while (attr) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400157 if (keyindex > MAX_KEYBOARD_KEYS) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000158 LOGERR("Too many keys defined in a keyboard row.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400159 return;
160 }
161
that1a7ba972015-02-01 19:48:19 +0100162 const char* keyinfo = attr->value();
Dees_Troy51a0e822012-09-05 15:24:24 -0400163
164 if (strlen(keyinfo) == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000165 LOGERR("No key info on layout%i, row%i, key%dd.\n", layoutindex, rowindex, keyindex);
Dees_Troy51a0e822012-09-05 15:24:24 -0400166 return;
167 }
168
thatd86f49d2015-03-15 00:56:57 +0100169 if (ParseKey(keyinfo, lay.keys[rowindex - 1][keyindex - 1], Xindex, keyWidth, false))
that1a7ba972015-02-01 19:48:19 +0100170 LOGERR("Invalid key info on layout%i, row%i, key%02i.\n", layoutindex, rowindex, keyindex);
Dees_Troy51a0e822012-09-05 15:24:24 -0400171
Dees_Troy51a0e822012-09-05 15:24:24 -0400172
173 // PROCESS LONG PRESS INFO IF EXISTS
174 sprintf(longpress, "long%02i", keyindex);
175 attr = keyrow->first_attribute(longpress);
176 if (attr) {
that1a7ba972015-02-01 19:48:19 +0100177 const char* keyinfo = attr->value();
Dees_Troy51a0e822012-09-05 15:24:24 -0400178
179 if (strlen(keyinfo) == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000180 LOGERR("No long press info on layout%i, row%i, long%dd.\n", layoutindex, rowindex, keyindex);
Dees_Troy51a0e822012-09-05 15:24:24 -0400181 return;
182 }
183
thatd86f49d2015-03-15 00:56:57 +0100184 if (ParseKey(keyinfo, lay.keys[rowindex - 1][keyindex - 1], Xindex, keyWidth, true))
that1a7ba972015-02-01 19:48:19 +0100185 LOGERR("Invalid long press key info on layout%i, row%i, long%02i.\n", layoutindex, rowindex, keyindex);
Dees_Troy51a0e822012-09-05 15:24:24 -0400186 }
187 keyindex++;
188 sprintf(key, "key%02i", keyindex);
189 attr = keyrow->first_attribute(key);
190 }
191 rowindex++;
192 row[3] = (char)(rowindex + 48);
193 keyrow = keylayout->first_node(row);
194 }
195 layoutindex++;
196 layout[6] = (char)(layoutindex + 48);
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600197 keylayout = FindNode(node, layout);
Dees_Troy51a0e822012-09-05 15:24:24 -0400198 }
199
thate79878b2015-03-14 23:07:23 +0100200 int x, y;
Dees_Troy51a0e822012-09-05 15:24:24 -0400201 // Load the placement
thatf256b722015-05-23 20:44:12 +0200202 LoadPlacement(FindNode(node, "placement"), &x, &y, &mRenderW, &mRenderH);
203 SetRenderPos(x, y, mRenderW, mRenderH);
Dees_Troy51a0e822012-09-05 15:24:24 -0400204 return;
205}
206
207GUIKeyboard::~GUIKeyboard()
208{
that1a7ba972015-02-01 19:48:19 +0100209}
Dees_Troy51a0e822012-09-05 15:24:24 -0400210
thate79878b2015-03-14 23:07:23 +0100211int GUIKeyboard::ParseKey(const char* keyinfo, Key& key, int& Xindex, int keyWidth, bool longpress)
that1a7ba972015-02-01 19:48:19 +0100212{
thatf256b722015-05-23 20:44:12 +0200213 key.layout = 0;
that1a7ba972015-02-01 19:48:19 +0100214 int keychar = 0;
215 if (strlen(keyinfo) == 1) {
216 // This is a single key, simple definition
217 keychar = keyinfo[0];
218 } else {
219 // This key has extra data: {keywidth}:{what_the_key_does}
Ethan Yonker63e414f2015-02-06 15:44:39 -0600220 keyWidth = scale_theme_x(atoi(keyinfo));
that1a7ba972015-02-01 19:48:19 +0100221
222 const char* ptr = keyinfo;
223 while (*ptr > 32 && *ptr != ':')
224 ptr++;
225 if (*ptr != ':')
226 return -1; // no colon is an error
227 ptr++;
228
229 if (*ptr == 0) { // This is an empty area
230 keychar = 0;
231 } else if (strlen(ptr) == 1) { // This is the character that this key uses
232 keychar = *ptr;
233 } else if (*ptr == 'c') { // This is an ASCII character code: "c:{number}"
234 keychar = atoi(ptr + 2);
that8834a0f2016-01-05 23:29:30 +0100235 } else if (*ptr == 'k') { // This is a Linux keycode from input.h: "k:{number}"
236 keychar = -atoi(ptr + 2);
that1a7ba972015-02-01 19:48:19 +0100237 } else if (*ptr == 'l') { // This is a different layout: "layout{number}"
that1a7ba972015-02-01 19:48:19 +0100238 key.layout = atoi(ptr + 6);
that8834a0f2016-01-05 23:29:30 +0100239 } else if (*ptr == 'a') { // This is an action: "action" (the Enter key)
that1a7ba972015-02-01 19:48:19 +0100240 keychar = KEYBOARD_ACTION;
241 } else
242 return -1;
243 }
244
245 if (longpress) {
246 key.longpresskey = keychar;
247 } else {
248 key.key = keychar;
249 Xindex += keyWidth;
250 key.end_x = Xindex - 1;
251 }
252
253 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400254}
255
thatf256b722015-05-23 20:44:12 +0200256void GUIKeyboard::LoadKeyLabels(xml_node<>* parent, int layout)
257{
258 for (xml_node<>* child = parent->first_node(); child; child = child->next_sibling()) {
259 std::string name = child->name();
260 if (name == "keylabel") {
261 std::string keydef = LoadAttrString(child, "key", "");
262 Key tempkey;
263 int dummyX;
264 if (ParseKey(keydef.c_str(), tempkey, dummyX, 0, false) == 0) {
265 KeyLabel keylabel;
266 keylabel.key = tempkey.key;
267 keylabel.layout_from = layout;
268 keylabel.layout_to = tempkey.layout;
269 keylabel.text = LoadAttrString(child, "text", "");
270 keylabel.image = LoadAttrImage(child, "resource");
271 mKeyLabels.push_back(keylabel);
272 } else {
273 LOGERR("Ignoring invalid keylabel in layout %d: '%s'.\n", layout, keydef.c_str());
274 }
275 }
276 }
277}
278
thatf6b20662015-06-25 21:51:37 +0200279void GUIKeyboard::DrawKey(Key& key, int keyX, int keyY, int keyW, int keyH)
280{
that8834a0f2016-01-05 23:29:30 +0100281 int keychar = key.key;
282 if (!keychar && !key.layout)
thatf6b20662015-06-25 21:51:37 +0200283 return;
284
285 // key background
286 COLOR& c = (keychar >= 32 && keychar < 127) ? mKeyColorAlphanumeric : mKeyColorOther;
287 gr_color(c.red, c.green, c.blue, c.alpha);
288 keyX += mKeyMarginX;
289 keyY += mKeyMarginY;
290 keyW -= mKeyMarginX * 2;
291 keyH -= mKeyMarginY * 2;
292 gr_fill(keyX, keyY, keyW, keyH);
293
294 // key label
295 FontResource* labelFont = mFont;
296 string labelText;
297 ImageResource* labelImage = NULL;
298 if (keychar > 32 && keychar < 127) {
that8834a0f2016-01-05 23:29:30 +0100299 // TODO: this will eventually need UTF-8 support
thatf6b20662015-06-25 21:51:37 +0200300 labelText = (char) keychar;
that8834a0f2016-01-05 23:29:30 +0100301 if (CtrlActive) {
302 int ctrlchar = KeyCharToCtrlChar(keychar);
303 if (ctrlchar != keychar)
304 labelText = std::string("^") + (char)(ctrlchar + 64);
305 }
thatf6b20662015-06-25 21:51:37 +0200306 gr_color(mFontColor.red, mFontColor.green, mFontColor.blue, mFontColor.alpha);
307 }
308 else {
309 // search for a special key label
310 for (std::vector<KeyLabel>::iterator it = mKeyLabels.begin(); it != mKeyLabels.end(); ++it) {
311 if (it->layout_from > 0 && it->layout_from != currentLayout)
312 continue; // this label is for another layout
313 if (it->key == key.key && it->layout_to == key.layout)
314 {
315 // found a label
316 labelText = it->text;
317 labelImage = it->image;
318 break;
319 }
320 }
321 labelFont = mSmallFont;
322 gr_color(mFontColorSmall.red, mFontColorSmall.green, mFontColorSmall.blue, mFontColorSmall.alpha);
323 }
324
Ethan Yonker58f21322018-08-24 11:17:36 -0500325 if (labelImage && labelImage->GetResource())
thatf6b20662015-06-25 21:51:37 +0200326 {
327 int w = labelImage->GetWidth();
328 int h = labelImage->GetHeight();
329 int x = keyX + (keyW - w) / 2;
330 int y = keyY + (keyH - h) / 2;
331 gr_blit(labelImage->GetResource(), 0, 0, w, h, x, y);
332 }
Ethan Yonker58f21322018-08-24 11:17:36 -0500333 else if (!labelText.empty() && labelFont && labelFont->GetResource())
thatf6b20662015-06-25 21:51:37 +0200334 {
335 void* fontResource = labelFont->GetResource();
Ethan Yonkerfbb43532015-12-28 21:54:50 +0100336 int textW = gr_ttf_measureEx(labelText.c_str(), fontResource);
thatf6b20662015-06-25 21:51:37 +0200337 int textH = labelFont->GetHeight();
338 int textX = keyX + (keyW - textW) / 2;
339 int textY = keyY + (keyH - textH) / 2;
Ethan Yonkerb7a54a32015-10-05 10:16:27 -0500340 gr_textEx_scaleW(textX, textY, labelText.c_str(), fontResource, keyW, TOP_LEFT, 0);
thatf6b20662015-06-25 21:51:37 +0200341 }
342
343 // longpress key label (only if font is defined)
344 keychar = key.longpresskey;
Ethan Yonker58f21322018-08-24 11:17:36 -0500345 if (keychar > 32 && keychar < 127 && mLongpressFont && mLongpressFont->GetResource()) {
thatf6b20662015-06-25 21:51:37 +0200346 void* fontResource = mLongpressFont->GetResource();
347 gr_color(mLongpressFontColor.red, mLongpressFontColor.green, mLongpressFontColor.blue, mLongpressFontColor.alpha);
348 string text(1, keychar);
Ethan Yonkerfbb43532015-12-28 21:54:50 +0100349 int textW = gr_ttf_measureEx(text.c_str(), fontResource);
thatf6b20662015-06-25 21:51:37 +0200350 int textX = keyX + keyW - longpressOffsetX - textW;
351 int textY = keyY + longpressOffsetY;
Ethan Yonkerb7a54a32015-10-05 10:16:27 -0500352 gr_textEx_scaleW(textX, textY, text.c_str(), fontResource, keyW, TOP_LEFT, 0);
thatf6b20662015-06-25 21:51:37 +0200353 }
354}
355
that8834a0f2016-01-05 23:29:30 +0100356int GUIKeyboard::KeyCharToCtrlChar(int key)
357{
358 // convert upper and lower case to ctrl chars
359 // Ctrl+A to Ctrl+_ (we don't support entering null bytes)
360 if (key >= 65 && key <= 127 && key != 96)
361 return key & 0x1f;
362 return key; // pass on others (already ctrl chars, numbers, etc.) unchanged
363}
364
Dees_Troy51a0e822012-09-05 15:24:24 -0400365int GUIKeyboard::Render(void)
366{
367 if (!isConditionTrue())
368 {
369 mRendered = false;
370 return 0;
371 }
372
thatd86f49d2015-03-15 00:56:57 +0100373 Layout& lay = layouts[currentLayout - 1];
Dees_Troy51a0e822012-09-05 15:24:24 -0400374
thatf6b20662015-06-25 21:51:37 +0200375 bool drawKeys = false;
thatd86f49d2015-03-15 00:56:57 +0100376 if (lay.keyboardImg && lay.keyboardImg->GetResource())
thatf6b20662015-06-25 21:51:37 +0200377 // keyboard is image based
thatf256b722015-05-23 20:44:12 +0200378 gr_blit(lay.keyboardImg->GetResource(), 0, 0, mRenderW, mRenderH, mRenderX, mRenderY);
379 else {
thatf6b20662015-06-25 21:51:37 +0200380 // keyboard is software drawn
381 // fill background
thatf256b722015-05-23 20:44:12 +0200382 gr_color(mBackgroundColor.red, mBackgroundColor.green, mBackgroundColor.blue, mBackgroundColor.alpha);
383 gr_fill(mRenderX, mRenderY, mRenderW, mRenderH);
thatf6b20662015-06-25 21:51:37 +0200384 drawKeys = true;
385 }
thatf256b722015-05-23 20:44:12 +0200386
thatf6b20662015-06-25 21:51:37 +0200387 // draw keys
388 int y1 = 0;
389 for (int row = 0; row < MAX_KEYBOARD_ROWS; ++row) {
390 int rowY = mRenderY + y1;
391 int rowH = lay.row_end_y[row] - y1;
392 y1 = lay.row_end_y[row];
393 int x1 = 0;
394 for (int col = 0; col < MAX_KEYBOARD_KEYS; ++col) {
395 Key& key = lay.keys[row][col];
396 int keyY = rowY;
397 int keyH = rowH;
398 int keyX = mRenderX + x1;
399 int keyW = key.end_x - x1;
400 x1 = key.end_x;
thatf256b722015-05-23 20:44:12 +0200401
thatf6b20662015-06-25 21:51:37 +0200402 // Draw key for software drawn keyboard
403 if (drawKeys)
404 DrawKey(key, keyX, keyY, keyW, keyH);
405
406 // Draw highlight for capslock
that8834a0f2016-01-05 23:29:30 +0100407 if (hasCapsHighlight && lay.is_caps && CapsLockOn && key.layout > 0 && key.layout == lay.revert_layout) {
thatf6b20662015-06-25 21:51:37 +0200408 gr_color(mCapsHighlightColor.red, mCapsHighlightColor.green, mCapsHighlightColor.blue, mCapsHighlightColor.alpha);
thatf256b722015-05-23 20:44:12 +0200409 gr_fill(keyX, keyY, keyW, keyH);
thatf6b20662015-06-25 21:51:37 +0200410 }
thatf256b722015-05-23 20:44:12 +0200411
that8834a0f2016-01-05 23:29:30 +0100412 // Draw highlight for control
413 if (hasCtrlHighlight && key.key == -KEY_LEFTCTRL && CtrlActive) {
414 gr_color(mCtrlHighlightColor.red, mCtrlHighlightColor.green, mCtrlHighlightColor.blue, mCtrlHighlightColor.alpha);
415 gr_fill(keyX, keyY, keyW, keyH);
416 }
417
thatf6b20662015-06-25 21:51:37 +0200418 // Highlight current key
419 if (hasHighlight && &key == currentKey && highlightRenderCount != 0) {
420 gr_color(mHighlightColor.red, mHighlightColor.green, mHighlightColor.blue, mHighlightColor.alpha);
421 gr_fill(keyX, keyY, keyW, keyH);
thatf256b722015-05-23 20:44:12 +0200422 }
423 }
424 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400425
thatf6b20662015-06-25 21:51:37 +0200426 if (!hasHighlight || highlightRenderCount == 0)
Dees_Troy30b962e2012-10-19 20:48:59 -0400427 mRendered = true;
thatf6b20662015-06-25 21:51:37 +0200428 else if (highlightRenderCount > 0)
429 highlightRenderCount--;
thatd86f49d2015-03-15 00:56:57 +0100430 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400431}
432
433int GUIKeyboard::Update(void)
434{
435 if (!isConditionTrue()) return (mRendered ? 2 : 0);
436 if (!mRendered) return 2;
437
438 return 0;
439}
440
441int GUIKeyboard::SetRenderPos(int x, int y, int w, int h)
442{
thatf256b722015-05-23 20:44:12 +0200443 RenderObject::SetRenderPos(x, y, w, h);
Dees_Troy51a0e822012-09-05 15:24:24 -0400444 SetActionPos(mRenderX, mRenderY, mRenderW, mRenderH);
445 return 0;
446}
447
thate79878b2015-03-14 23:07:23 +0100448GUIKeyboard::Key* GUIKeyboard::HitTestKey(int x, int y)
Dees_Troy51a0e822012-09-05 15:24:24 -0400449{
thate79878b2015-03-14 23:07:23 +0100450 if (!IsInRegion(x, y))
451 return NULL;
452
453 int rely = y - mRenderY;
454 int relx = x - mRenderX;
455
thatd86f49d2015-03-15 00:56:57 +0100456 Layout& lay = layouts[currentLayout - 1];
457
thate79878b2015-03-14 23:07:23 +0100458 // Find the correct row
459 int row;
460 for (row = 0; row < MAX_KEYBOARD_ROWS; ++row) {
thatd86f49d2015-03-15 00:56:57 +0100461 if (lay.row_end_y[row] > rely)
thate79878b2015-03-14 23:07:23 +0100462 break;
463 }
464 if (row == MAX_KEYBOARD_ROWS)
465 return NULL;
466
467 // Find the correct key (column)
468 int col;
469 int x1 = 0;
470 for (col = 0; col < MAX_KEYBOARD_KEYS; ++col) {
thatd86f49d2015-03-15 00:56:57 +0100471 Key& key = lay.keys[row][col];
that8834a0f2016-01-05 23:29:30 +0100472 if (x1 <= relx && relx < key.end_x && (key.key != 0 || key.layout != 0)) {
thate79878b2015-03-14 23:07:23 +0100473 // This is the key that was pressed!
thate79878b2015-03-14 23:07:23 +0100474 return &key;
475 }
476 x1 = key.end_x;
477 }
478 return NULL;
Dees_Troy51a0e822012-09-05 15:24:24 -0400479}
480
481int GUIKeyboard::NotifyTouch(TOUCH_STATE state, int x, int y)
482{
thate79878b2015-03-14 23:07:23 +0100483 static int was_held = 0, startX = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400484
485 if (!isConditionTrue()) return -1;
486
487 switch (state)
488 {
489 case TOUCH_START:
thate79878b2015-03-14 23:07:23 +0100490 was_held = 0;
491 startX = x;
thatf6b20662015-06-25 21:51:37 +0200492 currentKey = HitTestKey(x, y);
493 if (currentKey)
thate79878b2015-03-14 23:07:23 +0100494 highlightRenderCount = -1;
495 else
Dees_Troy30b962e2012-10-19 20:48:59 -0400496 highlightRenderCount = 0;
thate79878b2015-03-14 23:07:23 +0100497 mRendered = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400498 break;
thate79878b2015-03-14 23:07:23 +0100499
Dees_Troy51a0e822012-09-05 15:24:24 -0400500 case TOUCH_DRAG:
501 break;
thate79878b2015-03-14 23:07:23 +0100502
Dees_Troy51a0e822012-09-05 15:24:24 -0400503 case TOUCH_RELEASE:
that8834a0f2016-01-05 23:29:30 +0100504 // TODO: we might want to notify of key releases here
thatf256b722015-05-23 20:44:12 +0200505 if (x < startX - (mRenderW * 0.5)) {
Dees_Troy30b962e2012-10-19 20:48:59 -0400506 if (highlightRenderCount != 0) {
507 highlightRenderCount = 0;
508 mRendered = false;
509 }
that8834a0f2016-01-05 23:29:30 +0100510 PageManager::NotifyCharInput(KEYBOARD_SWIPE_LEFT);
Dees_Troy51a0e822012-09-05 15:24:24 -0400511 return 0;
thatf256b722015-05-23 20:44:12 +0200512 } else if (x > startX + (mRenderW * 0.5)) {
Dees_Troy30b962e2012-10-19 20:48:59 -0400513 if (highlightRenderCount != 0) {
514 highlightRenderCount = 0;
515 mRendered = false;
516 }
that8834a0f2016-01-05 23:29:30 +0100517 PageManager::NotifyCharInput(KEYBOARD_SWIPE_RIGHT);
Dees_Troy51a0e822012-09-05 15:24:24 -0400518 return 0;
519 }
thate79878b2015-03-14 23:07:23 +0100520 // fall through
Dees_Troy51a0e822012-09-05 15:24:24 -0400521 case TOUCH_HOLD:
522 case TOUCH_REPEAT:
thatf6b20662015-06-25 21:51:37 +0200523 if (!currentKey) {
Dees_Troy30b962e2012-10-19 20:48:59 -0400524 if (highlightRenderCount != 0) {
525 highlightRenderCount = 0;
526 mRendered = false;
527 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400528 return 0;
thate79878b2015-03-14 23:07:23 +0100529 }
530
531 if (highlightRenderCount != 0) {
Dees_Troy30b962e2012-10-19 20:48:59 -0400532 if (state == TOUCH_RELEASE)
533 highlightRenderCount = 2;
534 else
535 highlightRenderCount = -1;
536 mRendered = false;
537 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400538
thatf6b20662015-06-25 21:51:37 +0200539 if (HitTestKey(x, y) != currentKey) {
thate79878b2015-03-14 23:07:23 +0100540 // We dragged off of the starting key
thatf6b20662015-06-25 21:51:37 +0200541 currentKey = NULL;
thate79878b2015-03-14 23:07:23 +0100542 if (highlightRenderCount != 0) {
543 highlightRenderCount = 0;
544 mRendered = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400545 }
thate79878b2015-03-14 23:07:23 +0100546 return 0;
547 } else {
thatf6b20662015-06-25 21:51:37 +0200548 Key& key = *currentKey;
that8834a0f2016-01-05 23:29:30 +0100549 bool repeatKey = false;
thatd86f49d2015-03-15 00:56:57 +0100550 Layout& lay = layouts[currentLayout - 1];
thate79878b2015-03-14 23:07:23 +0100551 if (state == TOUCH_RELEASE && was_held == 0) {
552 DataManager::Vibrate("tw_keyboard_vibrate");
that8834a0f2016-01-05 23:29:30 +0100553 if (key.layout > 0) {
thate79878b2015-03-14 23:07:23 +0100554 // Switch layouts
thatd86f49d2015-03-15 00:56:57 +0100555 if (lay.is_caps && key.layout == lay.revert_layout && !CapsLockOn) {
556 CapsLockOn = true; // Set the caps lock
thate79878b2015-03-14 23:07:23 +0100557 } else {
thatd86f49d2015-03-15 00:56:57 +0100558 CapsLockOn = false; // Unset the caps lock and change layouts
thate79878b2015-03-14 23:07:23 +0100559 currentLayout = key.layout;
Dees_Troy51a0e822012-09-05 15:24:24 -0400560 }
thate79878b2015-03-14 23:07:23 +0100561 mRendered = false;
that8834a0f2016-01-05 23:29:30 +0100562 } else if (key.key == KEYBOARD_ACTION) {
thate79878b2015-03-14 23:07:23 +0100563 // Action
564 highlightRenderCount = 0;
565 // Send action notification
that8834a0f2016-01-05 23:29:30 +0100566 PageManager::NotifyCharInput(key.key);
567 } else if (key.key == -KEY_LEFTCTRL) {
568 CtrlActive = !CtrlActive; // toggle Control key state
569 mRendered = false; // render Ctrl key highlight
570 } else if (key.key != 0) {
thatd86f49d2015-03-15 00:56:57 +0100571 // Regular key
that8834a0f2016-01-05 23:29:30 +0100572 if (key.key > 0) {
573 // ASCII code or character
574 int keycode = key.key;
575 if (CtrlActive) {
576 CtrlActive = false;
577 mRendered = false;
578 keycode = KeyCharToCtrlChar(key.key);
579 }
580 PageManager::NotifyCharInput(keycode);
581 } else {
582 // Linux key code
583 PageManager::NotifyKey(-key.key, true);
584 PageManager::NotifyKey(-key.key, false);
585 }
thatd86f49d2015-03-15 00:56:57 +0100586 if (!CapsLockOn && lay.is_caps) {
587 // caps lock was not set, change layouts
588 currentLayout = lay.revert_layout;
589 mRendered = false;
590 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400591 }
thate79878b2015-03-14 23:07:23 +0100592 } else if (state == TOUCH_HOLD) {
593 was_held = 1;
that8834a0f2016-01-05 23:29:30 +0100594 if (key.longpresskey > 0) {
thate79878b2015-03-14 23:07:23 +0100595 // Long Press Key
596 DataManager::Vibrate("tw_keyboard_vibrate");
that8834a0f2016-01-05 23:29:30 +0100597 PageManager::NotifyCharInput(key.longpresskey);
thate79878b2015-03-14 23:07:23 +0100598 }
that8834a0f2016-01-05 23:29:30 +0100599 else
600 repeatKey = true;
thate79878b2015-03-14 23:07:23 +0100601 } else if (state == TOUCH_REPEAT) {
602 was_held = 1;
that8834a0f2016-01-05 23:29:30 +0100603 repeatKey = true;
604 }
605 if (repeatKey) {
606 if (key.key == KEYBOARD_BACKSPACE) {
thate79878b2015-03-14 23:07:23 +0100607 // Repeat backspace
that8834a0f2016-01-05 23:29:30 +0100608 PageManager::NotifyCharInput(key.key);
609 }
610 switch (key.key)
611 {
612 // Repeat arrows
613 case -KEY_LEFT:
614 case -KEY_RIGHT:
615 case -KEY_UP:
616 case -KEY_DOWN:
617 PageManager::NotifyKey(-key.key, true);
618 PageManager::NotifyKey(-key.key, false);
619 break;
thate79878b2015-03-14 23:07:23 +0100620 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400621 }
622 }
623 break;
624 }
625
626 return 0;
627}
that8834a0f2016-01-05 23:29:30 +0100628
629void GUIKeyboard::SetPageFocus(int inFocus)
630{
631 if (inFocus)
632 CtrlActive = false;
633}