blob: 78e27a164dd6190097aaff65bfe3e39e0abaa02a [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
19#include <stdarg.h>
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23#include <fcntl.h>
24#include <sys/reboot.h>
25#include <sys/stat.h>
26#include <sys/time.h>
27#include <sys/mman.h>
28#include <sys/types.h>
29#include <sys/ioctl.h>
30#include <time.h>
31#include <unistd.h>
32#include <stdlib.h>
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +000033#include "../data.hpp"
Dees_Troy51a0e822012-09-05 15:24:24 -040034
35#include <string>
36
37extern "C" {
Dees_Troy2673cec2013-04-02 20:22:16 +000038#include "../twcommon.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040039#include "../minuitwrp/minui.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040040}
41
42#include "rapidxml.hpp"
43#include "objects.hpp"
44
45GUIKeyboard::GUIKeyboard(xml_node<>* node)
Vojtech Bocekede51c52014-02-07 23:58:09 +010046 : GUIObject(node)
Dees_Troy51a0e822012-09-05 15:24:24 -040047{
48 int layoutindex, rowindex, keyindex, Xindex, Yindex, keyHeight = 0, keyWidth = 0;
Dees_Troy30b962e2012-10-19 20:48:59 -040049 rowY = colX = -1;
50 highlightRenderCount = hasHighlight = 0;
Dees_Troy3a16cb52013-01-10 15:16:02 +000051 char resource[10], layout[8], row[5], key[6], longpress[7];
Dees_Troy51a0e822012-09-05 15:24:24 -040052 xml_attribute<>* attr;
53 xml_node<>* child;
54 xml_node<>* keylayout;
55 xml_node<>* keyrow;
56
57 for (layoutindex=0; layoutindex<MAX_KEYBOARD_LAYOUTS; layoutindex++)
58 keyboardImg[layoutindex] = NULL;
59
60 mRendered = false;
61 currentLayout = 1;
62 mAction = NULL;
63 KeyboardHeight = KeyboardWidth = cursorLocation = 0;
64
65 if (!node) return;
66
67 // Load the action
68 child = node->first_node("action");
69 if (child)
70 {
71 mAction = new GUIAction(node);
72 }
73
Dees_Troy30b962e2012-10-19 20:48:59 -040074 memset(&mHighlightColor, 0, sizeof(COLOR));
75 child = node->first_node("highlight");
76 if (child) {
77 attr = child->first_attribute("color");
78 if (attr) {
79 hasHighlight = 1;
80 std::string color = attr->value();
81 ConvertStrToColor(color, &mHighlightColor);
82 }
83 }
84
Dees_Troy51a0e822012-09-05 15:24:24 -040085 // Load the images for the different layouts
86 child = node->first_node("layout");
87 if (child)
88 {
89 layoutindex = 1;
90 strcpy(resource, "resource1");
91 attr = child->first_attribute(resource);
92 while (attr && layoutindex < (MAX_KEYBOARD_LAYOUTS + 1)) {
93 keyboardImg[layoutindex - 1] = PageManager::FindResource(attr->value());
94
95 layoutindex++;
96 resource[8] = (char)(layoutindex + 48);
97 attr = child->first_attribute(resource);
98 }
99 }
100
101 // Check the first image to get height and width
102 if (keyboardImg[0] && keyboardImg[0]->GetResource())
103 {
104 KeyboardWidth = gr_get_width(keyboardImg[0]->GetResource());
105 KeyboardHeight = gr_get_height(keyboardImg[0]->GetResource());
106 }
107
108 // Load all of the layout maps
109 layoutindex = 1;
110 strcpy(layout, "layout1");
111 keylayout = node->first_node(layout);
112 while (keylayout)
113 {
114 if (layoutindex > MAX_KEYBOARD_LAYOUTS) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000115 LOGERR("Too many layouts defined in keyboard.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400116 return;
117 }
118
119 child = keylayout->first_node("keysize");
120 if (child) {
121 attr = child->first_attribute("height");
122 if (attr)
123 keyHeight = atoi(attr->value());
124 else
125 keyHeight = 0;
126 attr = child->first_attribute("width");
127 if (attr)
128 keyWidth = atoi(attr->value());
129 else
130 keyWidth = 0;
131 }
132
133 rowindex = 1;
134 Yindex = 0;
135 strcpy(row, "row1");
136 keyrow = keylayout->first_node(row);
137 while (keyrow)
138 {
139 if (rowindex > MAX_KEYBOARD_ROWS) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000140 LOGERR("Too many rows defined in keyboard.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400141 return;
142 }
143
144 Yindex += keyHeight;
145 row_heights[layoutindex - 1][rowindex - 1] = Yindex;
146
147 keyindex = 1;
148 Xindex = 0;
149 strcpy(key, "key01");
150 attr = keyrow->first_attribute(key);
151
152 while (attr) {
153 string stratt;
154 char keyinfo[255];
155
156 if (keyindex > MAX_KEYBOARD_KEYS) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000157 LOGERR("Too many keys defined in a keyboard row.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400158 return;
159 }
160
161 stratt = attr->value();
162 if (strlen(stratt.c_str()) >= 255) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000163 LOGERR("Key info on layout%i, row%i, key%dd is too long.\n", layoutindex, rowindex, keyindex);
Dees_Troy51a0e822012-09-05 15:24:24 -0400164 return;
165 }
166
167 strcpy(keyinfo, stratt.c_str());
168
169 if (strlen(keyinfo) == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000170 LOGERR("No key info on layout%i, row%i, key%dd.\n", layoutindex, rowindex, keyindex);
Dees_Troy51a0e822012-09-05 15:24:24 -0400171 return;
172 }
173
174 if (strlen(keyinfo) == 1) {
175 // This is a single key, simple definition
176 keyboard_keys[layoutindex - 1][rowindex - 1][keyindex - 1].key = keyinfo[0];
177 Xindex += keyWidth;
178 keyboard_keys[layoutindex - 1][rowindex - 1][keyindex - 1].end_x = Xindex - 1;
179 } else {
180 // This key has extra data
181 char* ptr;
182 char* offset;
183 char* keyitem;
184 char foratoi[10];
185
186 ptr = keyinfo;
187 offset = keyinfo;
188 while (*ptr > 32 && *ptr != ':')
189 ptr++;
190 if (*ptr != 0)
191 *ptr = 0;
192
193 strcpy(foratoi, offset);
194 Xindex += atoi(foratoi);
195 keyboard_keys[layoutindex - 1][rowindex - 1][keyindex - 1].end_x = Xindex - 1;
196
197 ptr++;
198 if (*ptr == 0) {
199 // This is an empty area
200 keyboard_keys[layoutindex - 1][rowindex - 1][keyindex - 1].key = 0;
201 } else if (strlen(ptr) == 1) {
202 // This is the character that this key uses
203 keyboard_keys[layoutindex - 1][rowindex - 1][keyindex - 1].key = *ptr;
204 } else if (*ptr == 'c') {
205 // This is an ASCII character code
206 keyitem = ptr + 2;
207 strcpy(foratoi, keyitem);
208 keyboard_keys[layoutindex - 1][rowindex - 1][keyindex - 1].key = atoi(foratoi);
209 } else if (*ptr == 'l') {
210 // This is a different layout
211 keyitem = ptr + 6;
212 keyboard_keys[layoutindex - 1][rowindex - 1][keyindex - 1].key = KEYBOARD_LAYOUT;
213 strcpy(foratoi, keyitem);
214 keyboard_keys[layoutindex - 1][rowindex - 1][keyindex - 1].layout = atoi(foratoi);
215 } else if (*ptr == 'a') {
216 // This is an action
217 keyboard_keys[layoutindex - 1][rowindex - 1][keyindex - 1].key = KEYBOARD_ACTION;
218 } else
Dees_Troy2673cec2013-04-02 20:22:16 +0000219 LOGERR("Invalid key info on layout%i, row%i, key%02i.\n", layoutindex, rowindex, keyindex);
Dees_Troy51a0e822012-09-05 15:24:24 -0400220 }
221
222 // PROCESS LONG PRESS INFO IF EXISTS
223 sprintf(longpress, "long%02i", keyindex);
224 attr = keyrow->first_attribute(longpress);
225 if (attr) {
226 stratt = attr->value();
227 if (strlen(stratt.c_str()) >= 255) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000228 LOGERR("Key info on layout%i, row%i, key%dd is too long.\n", layoutindex, rowindex, keyindex);
Dees_Troy51a0e822012-09-05 15:24:24 -0400229 return;
230 }
231
232 strcpy(keyinfo, stratt.c_str());
233
234 if (strlen(keyinfo) == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000235 LOGERR("No long press info on layout%i, row%i, long%dd.\n", layoutindex, rowindex, keyindex);
Dees_Troy51a0e822012-09-05 15:24:24 -0400236 return;
237 }
238
239 if (strlen(keyinfo) == 1) {
240 // This is a single key, simple definition
241 keyboard_keys[layoutindex - 1][rowindex - 1][keyindex - 1].longpresskey = keyinfo[0];
242 } else {
243 // This key has extra data
244 char* ptr;
245 char* offset;
246 char* keyitem;
247 char foratoi[10];
248
249 ptr = keyinfo;
250 offset = keyinfo;
251 while (*ptr > 32 && *ptr != ':')
252 ptr++;
253 if (*ptr != 0)
254 *ptr = 0;
255
256 strcpy(foratoi, offset);
257 Xindex += atoi(foratoi);
258
259 ptr++;
260 if (*ptr == 0) {
261 // This is an empty area
262 keyboard_keys[layoutindex - 1][rowindex - 1][keyindex - 1].longpresskey = 0;
263 } else if (strlen(ptr) == 1) {
264 // This is the character that this key uses
265 keyboard_keys[layoutindex - 1][rowindex - 1][keyindex - 1].longpresskey = *ptr;
266 } else if (*ptr == 'c') {
267 // This is an ASCII character code
268 keyitem = ptr + 2;
269 strcpy(foratoi, keyitem);
270 keyboard_keys[layoutindex - 1][rowindex - 1][keyindex - 1].longpresskey = atoi(foratoi);
271 } else if (*ptr == 'l') {
272 // This is a different layout
273 keyitem = ptr + 6;
274 keyboard_keys[layoutindex - 1][rowindex - 1][keyindex - 1].longpresskey = KEYBOARD_LAYOUT;
275 strcpy(foratoi, keyitem);
276 keyboard_keys[layoutindex - 1][rowindex - 1][keyindex - 1].layout = atoi(foratoi);
277 } else if (*ptr == 'a') {
278 // This is an action
279 keyboard_keys[layoutindex - 1][rowindex - 1][keyindex - 1].longpresskey = KEYBOARD_ACTION;
280 } else
Dees_Troy2673cec2013-04-02 20:22:16 +0000281 LOGERR("Invalid long press key info on layout%i, row%i, long%02i.\n", layoutindex, rowindex, keyindex);
Dees_Troy51a0e822012-09-05 15:24:24 -0400282 }
283 }
284 keyindex++;
285 sprintf(key, "key%02i", keyindex);
286 attr = keyrow->first_attribute(key);
287 }
288 rowindex++;
289 row[3] = (char)(rowindex + 48);
290 keyrow = keylayout->first_node(row);
291 }
292 layoutindex++;
293 layout[6] = (char)(layoutindex + 48);
294 keylayout = node->first_node(layout);
295 }
296
297 int x, y, w, h;
298 // Load the placement
299 LoadPlacement(node->first_node("placement"), &x, &y, &w, &h);
300 SetActionPos(x, y, KeyboardWidth, KeyboardHeight);
301 SetRenderPos(x, y, w, h);
302 return;
303}
304
305GUIKeyboard::~GUIKeyboard()
306{
Dees_Troy51a0e822012-09-05 15:24:24 -0400307
Dees_Troy51a0e822012-09-05 15:24:24 -0400308}
309
310int GUIKeyboard::Render(void)
311{
312 if (!isConditionTrue())
313 {
314 mRendered = false;
315 return 0;
316 }
317
318 int ret = 0;
319
320 if (keyboardImg[currentLayout - 1] && keyboardImg[currentLayout - 1]->GetResource())
321 gr_blit(keyboardImg[currentLayout - 1]->GetResource(), 0, 0, KeyboardWidth, KeyboardHeight, mRenderX, mRenderY);
322
Dees_Troy30b962e2012-10-19 20:48:59 -0400323 if (hasHighlight && highlightRenderCount != 0) {
324 int boxheight, boxwidth, x;
325 if (rowY == 0)
326 boxheight = row_heights[currentLayout - 1][rowY];
327 else
328 boxheight = row_heights[currentLayout - 1][rowY] - row_heights[currentLayout - 1][rowY - 1];
329 if (colX == 0) {
330 x = mRenderX;
331 boxwidth = keyboard_keys[currentLayout - 1][rowY][colX].end_x;
332 } else {
333 x = mRenderX + keyboard_keys[currentLayout - 1][rowY][colX - 1].end_x;
334 boxwidth = keyboard_keys[currentLayout - 1][rowY][colX].end_x - keyboard_keys[currentLayout - 1][rowY][colX - 1].end_x;
335 }
336 gr_color(mHighlightColor.red, mHighlightColor.green, mHighlightColor.blue, mHighlightColor.alpha);
337 gr_fill(x, mRenderY + row_heights[currentLayout - 1][rowY - 1], boxwidth, boxheight);
338 if (highlightRenderCount > 0)
339 highlightRenderCount--;
340 } else
341 mRendered = true;
342
Dees_Troy51a0e822012-09-05 15:24:24 -0400343 return ret;
344}
345
346int GUIKeyboard::Update(void)
347{
348 if (!isConditionTrue()) return (mRendered ? 2 : 0);
349 if (!mRendered) return 2;
350
351 return 0;
352}
353
354int GUIKeyboard::SetRenderPos(int x, int y, int w, int h)
355{
356 mRenderX = x;
357 mRenderY = y;
358 if (w || h)
359 {
360 mRenderW = KeyboardWidth;
361 mRenderH = KeyboardHeight;
362 }
363
364 if (mAction) mAction->SetActionPos(mRenderX, mRenderY, mRenderW, mRenderH);
365 SetActionPos(mRenderX, mRenderY, mRenderW, mRenderH);
366 return 0;
367}
368
369int GUIKeyboard::GetSelection(int x, int y)
370{
Dees_Troy30b962e2012-10-19 20:48:59 -0400371 if (x < mRenderX || x - mRenderX > (int)KeyboardWidth || y < mRenderY || y - mRenderY > (int)KeyboardHeight) return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400372 return 0;
373}
374
375int GUIKeyboard::NotifyTouch(TOUCH_STATE state, int x, int y)
376{
377 static int startSelection = -1, was_held = 0, startX = 0;
378 static unsigned char initial_key = 0;
379 unsigned int indexy, indexx, rely, relx, rowIndex = 0;
380
381 rely = y - mRenderY;
382 relx = x - mRenderX;
383
384 if (!isConditionTrue()) return -1;
385
386 switch (state)
387 {
388 case TOUCH_START:
389 if (GetSelection(x, y) == 0) {
390 startSelection = -1;
391 was_held = 0;
392 startX = x;
393 // Find the correct row
394 for (indexy=0; indexy<MAX_KEYBOARD_ROWS; indexy++) {
395 if (row_heights[currentLayout - 1][indexy] > rely) {
396 rowIndex = indexy;
Dees_Troy30b962e2012-10-19 20:48:59 -0400397 rowY = indexy;
Dees_Troy51a0e822012-09-05 15:24:24 -0400398 indexy = MAX_KEYBOARD_ROWS;
399 }
400 }
401
402 // Find the correct key (column)
403 for (indexx=0; indexx<MAX_KEYBOARD_KEYS; indexx++) {
404 if (keyboard_keys[currentLayout - 1][rowIndex][indexx].end_x > relx) {
405 // This is the key that was pressed!
406 initial_key = keyboard_keys[currentLayout - 1][rowIndex][indexx].key;
Dees_Troy30b962e2012-10-19 20:48:59 -0400407 colX = indexx;
Dees_Troy51a0e822012-09-05 15:24:24 -0400408 indexx = MAX_KEYBOARD_KEYS;
409 }
410 }
Dees_Troy30b962e2012-10-19 20:48:59 -0400411 if (initial_key != 0)
412 highlightRenderCount = -1;
413 else
414 highlightRenderCount = 0;
415 mRendered = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400416 } else {
Dees_Troy30b962e2012-10-19 20:48:59 -0400417 if (highlightRenderCount != 0)
418 mRendered = false;
419 highlightRenderCount = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400420 startSelection = 0;
421 }
422 break;
423 case TOUCH_DRAG:
424 break;
425 case TOUCH_RELEASE:
Dees_Troy0cb64e52012-10-15 15:56:10 -0400426 if (x < startX - (KeyboardWidth * 0.5)) {
Dees_Troy30b962e2012-10-19 20:48:59 -0400427 if (highlightRenderCount != 0) {
428 highlightRenderCount = 0;
429 mRendered = false;
430 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400431 PageManager::NotifyKeyboard(KEYBOARD_SWIPE_LEFT);
432 return 0;
Dees_Troy0cb64e52012-10-15 15:56:10 -0400433 } else if (x > startX + (KeyboardWidth * 0.5)) {
Dees_Troy30b962e2012-10-19 20:48:59 -0400434 if (highlightRenderCount != 0) {
435 highlightRenderCount = 0;
436 mRendered = false;
437 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400438 PageManager::NotifyKeyboard(KEYBOARD_SWIPE_RIGHT);
439 return 0;
440 }
441
442 case TOUCH_HOLD:
443 case TOUCH_REPEAT:
444
Dees_Troy30b962e2012-10-19 20:48:59 -0400445 if (startSelection == 0 || GetSelection(x, y) == -1) {
446 if (highlightRenderCount != 0) {
447 highlightRenderCount = 0;
448 mRendered = false;
449 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400450 return 0;
Dees_Troy30b962e2012-10-19 20:48:59 -0400451 } else if (highlightRenderCount != 0) {
452 if (state == TOUCH_RELEASE)
453 highlightRenderCount = 2;
454 else
455 highlightRenderCount = -1;
456 mRendered = false;
457 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400458
459 // Find the correct row
460 for (indexy=0; indexy<MAX_KEYBOARD_ROWS; indexy++) {
461 if (row_heights[currentLayout - 1][indexy] > rely) {
462 rowIndex = indexy;
463 indexy = MAX_KEYBOARD_ROWS;
464 }
465 }
466
467 // Find the correct key (column)
468 for (indexx=0; indexx<MAX_KEYBOARD_KEYS; indexx++) {
469 if (keyboard_keys[currentLayout - 1][rowIndex][indexx].end_x > relx) {
470 // This is the key that was pressed!
471 if (keyboard_keys[currentLayout - 1][rowIndex][indexx].key != initial_key) {
472 // We dragged off of the starting key
473 startSelection = 0;
474 break;
475 } else if (state == TOUCH_RELEASE && was_held == 0) {
Ethan Yonker03db3262014-02-05 08:02:06 -0600476 DataManager::Vibrate("tw_keyboard_vibrate");
Dees_Troy51a0e822012-09-05 15:24:24 -0400477 if ((int)keyboard_keys[currentLayout - 1][rowIndex][indexx].key < KEYBOARD_SPECIAL_KEYS && (int)keyboard_keys[currentLayout - 1][rowIndex][indexx].key > 0) {
478 // Regular key
479 PageManager::NotifyKeyboard(keyboard_keys[currentLayout - 1][rowIndex][indexx].key);
480 } else if ((int)keyboard_keys[currentLayout - 1][rowIndex][indexx].key == KEYBOARD_LAYOUT) {
481 // Switch layouts
482 currentLayout = keyboard_keys[currentLayout - 1][rowIndex][indexx].layout;
483 mRendered = false;
484 } else if ((int)keyboard_keys[currentLayout - 1][rowIndex][indexx].key == KEYBOARD_ACTION) {
485 // Action
Dees_Troy30b962e2012-10-19 20:48:59 -0400486 highlightRenderCount = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400487 if (mAction) {
488 // Keyboard has its own action defined
489 return (mAction ? mAction->NotifyTouch(state, x, y) : 1);
490 } else {
491 // Send action notification
492 PageManager::NotifyKeyboard(keyboard_keys[currentLayout - 1][rowIndex][indexx].key);
493 }
494 }
495 } else if (state == TOUCH_HOLD) {
496 was_held = 1;
497 if ((int)keyboard_keys[currentLayout - 1][rowIndex][indexx].key == KEYBOARD_BACKSPACE) {
498 // Repeat backspace
499 PageManager::NotifyKeyboard(keyboard_keys[currentLayout - 1][rowIndex][indexx].key);
500 } else if ((int)keyboard_keys[currentLayout - 1][rowIndex][indexx].longpresskey < KEYBOARD_SPECIAL_KEYS && (int)keyboard_keys[currentLayout - 1][rowIndex][indexx].longpresskey > 0) {
501 // Long Press Key
Ethan Yonker03db3262014-02-05 08:02:06 -0600502 DataManager::Vibrate("tw_keyboard_vibrate");
Dees_Troy51a0e822012-09-05 15:24:24 -0400503 PageManager::NotifyKeyboard(keyboard_keys[currentLayout - 1][rowIndex][indexx].longpresskey);
504 }
505 } else if (state == TOUCH_REPEAT) {
506 was_held = 1;
507 if ((int)keyboard_keys[currentLayout - 1][rowIndex][indexx].key == KEYBOARD_BACKSPACE) {
508 // Repeat backspace
509 PageManager::NotifyKeyboard(keyboard_keys[currentLayout - 1][rowIndex][indexx].key);
510 }
511 }
512 indexx = MAX_KEYBOARD_KEYS;
513 }
514 }
515 break;
516 }
517
518 return 0;
519}