blob: 3b8fdc9ae4b16d94094c67fe2cfb7cc74eec5670 [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"
Dees_Troy51a0e822012-09-05 15:24:24 -040028#include "../minuitwrp/minui.h"
Ethan Yonker63e414f2015-02-06 15:44:39 -060029#include "gui.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040030}
31
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
325 if (labelImage)
326 {
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 }
333 else if (!labelText.empty())
334 {
335 void* fontResource = labelFont->GetResource();
336 int textW = gr_measureEx(labelText.c_str(), fontResource);
337 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;
345 if (keychar > 32 && keychar < 127 && mLongpressFont->GetResource()) {
346 void* fontResource = mLongpressFont->GetResource();
347 gr_color(mLongpressFontColor.red, mLongpressFontColor.green, mLongpressFontColor.blue, mLongpressFontColor.alpha);
348 string text(1, keychar);
349 int textH = mLongpressFont->GetHeight();
350 int textW = gr_measureEx(text.c_str(), fontResource);
351 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) {
553 DataManager::Vibrate("tw_keyboard_vibrate");
that8834a0f2016-01-05 23:29:30 +0100554 if (key.layout > 0) {
thate79878b2015-03-14 23:07:23 +0100555 // Switch layouts
thatd86f49d2015-03-15 00:56:57 +0100556 if (lay.is_caps && key.layout == lay.revert_layout && !CapsLockOn) {
557 CapsLockOn = true; // Set the caps lock
thate79878b2015-03-14 23:07:23 +0100558 } else {
thatd86f49d2015-03-15 00:56:57 +0100559 CapsLockOn = false; // Unset the caps lock and change layouts
thate79878b2015-03-14 23:07:23 +0100560 currentLayout = key.layout;
Dees_Troy51a0e822012-09-05 15:24:24 -0400561 }
thate79878b2015-03-14 23:07:23 +0100562 mRendered = false;
that8834a0f2016-01-05 23:29:30 +0100563 } else if (key.key == KEYBOARD_ACTION) {
thate79878b2015-03-14 23:07:23 +0100564 // Action
565 highlightRenderCount = 0;
566 // Send action notification
that8834a0f2016-01-05 23:29:30 +0100567 PageManager::NotifyCharInput(key.key);
568 } else if (key.key == -KEY_LEFTCTRL) {
569 CtrlActive = !CtrlActive; // toggle Control key state
570 mRendered = false; // render Ctrl key highlight
571 } else if (key.key != 0) {
thatd86f49d2015-03-15 00:56:57 +0100572 // Regular key
that8834a0f2016-01-05 23:29:30 +0100573 if (key.key > 0) {
574 // ASCII code or character
575 int keycode = key.key;
576 if (CtrlActive) {
577 CtrlActive = false;
578 mRendered = false;
579 keycode = KeyCharToCtrlChar(key.key);
580 }
581 PageManager::NotifyCharInput(keycode);
582 } else {
583 // Linux key code
584 PageManager::NotifyKey(-key.key, true);
585 PageManager::NotifyKey(-key.key, false);
586 }
thatd86f49d2015-03-15 00:56:57 +0100587 if (!CapsLockOn && lay.is_caps) {
588 // caps lock was not set, change layouts
589 currentLayout = lay.revert_layout;
590 mRendered = false;
591 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400592 }
thate79878b2015-03-14 23:07:23 +0100593 } else if (state == TOUCH_HOLD) {
594 was_held = 1;
that8834a0f2016-01-05 23:29:30 +0100595 if (key.longpresskey > 0) {
thate79878b2015-03-14 23:07:23 +0100596 // Long Press Key
597 DataManager::Vibrate("tw_keyboard_vibrate");
that8834a0f2016-01-05 23:29:30 +0100598 PageManager::NotifyCharInput(key.longpresskey);
thate79878b2015-03-14 23:07:23 +0100599 }
that8834a0f2016-01-05 23:29:30 +0100600 else
601 repeatKey = true;
thate79878b2015-03-14 23:07:23 +0100602 } else if (state == TOUCH_REPEAT) {
603 was_held = 1;
that8834a0f2016-01-05 23:29:30 +0100604 repeatKey = true;
605 }
606 if (repeatKey) {
607 if (key.key == KEYBOARD_BACKSPACE) {
thate79878b2015-03-14 23:07:23 +0100608 // Repeat backspace
that8834a0f2016-01-05 23:29:30 +0100609 PageManager::NotifyCharInput(key.key);
610 }
611 switch (key.key)
612 {
613 // Repeat arrows
614 case -KEY_LEFT:
615 case -KEY_RIGHT:
616 case -KEY_UP:
617 case -KEY_DOWN:
618 PageManager::NotifyKey(-key.key, true);
619 PageManager::NotifyKey(-key.key, false);
620 break;
thate79878b2015-03-14 23:07:23 +0100621 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400622 }
623 }
624 break;
625 }
626
627 return 0;
628}
that8834a0f2016-01-05 23:29:30 +0100629
630void GUIKeyboard::SetPageFocus(int inFocus)
631{
632 if (inFocus)
633 CtrlActive = false;
634}