blob: 36106c7c1eb3b059e71917c1402068f93f848aaf [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)
46 : Conditional(node)
47{
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{
307 int layoutindex;
308
309 for (layoutindex=0; layoutindex<MAX_KEYBOARD_LAYOUTS; layoutindex++)
310 if (keyboardImg[layoutindex]) delete keyboardImg[layoutindex];
311}
312
313int GUIKeyboard::Render(void)
314{
315 if (!isConditionTrue())
316 {
317 mRendered = false;
318 return 0;
319 }
320
321 int ret = 0;
322
323 if (keyboardImg[currentLayout - 1] && keyboardImg[currentLayout - 1]->GetResource())
324 gr_blit(keyboardImg[currentLayout - 1]->GetResource(), 0, 0, KeyboardWidth, KeyboardHeight, mRenderX, mRenderY);
325
Dees_Troy30b962e2012-10-19 20:48:59 -0400326 if (hasHighlight && highlightRenderCount != 0) {
327 int boxheight, boxwidth, x;
328 if (rowY == 0)
329 boxheight = row_heights[currentLayout - 1][rowY];
330 else
331 boxheight = row_heights[currentLayout - 1][rowY] - row_heights[currentLayout - 1][rowY - 1];
332 if (colX == 0) {
333 x = mRenderX;
334 boxwidth = keyboard_keys[currentLayout - 1][rowY][colX].end_x;
335 } else {
336 x = mRenderX + keyboard_keys[currentLayout - 1][rowY][colX - 1].end_x;
337 boxwidth = keyboard_keys[currentLayout - 1][rowY][colX].end_x - keyboard_keys[currentLayout - 1][rowY][colX - 1].end_x;
338 }
339 gr_color(mHighlightColor.red, mHighlightColor.green, mHighlightColor.blue, mHighlightColor.alpha);
340 gr_fill(x, mRenderY + row_heights[currentLayout - 1][rowY - 1], boxwidth, boxheight);
341 if (highlightRenderCount > 0)
342 highlightRenderCount--;
343 } else
344 mRendered = true;
345
Dees_Troy51a0e822012-09-05 15:24:24 -0400346 return ret;
347}
348
349int GUIKeyboard::Update(void)
350{
351 if (!isConditionTrue()) return (mRendered ? 2 : 0);
352 if (!mRendered) return 2;
353
354 return 0;
355}
356
357int GUIKeyboard::SetRenderPos(int x, int y, int w, int h)
358{
359 mRenderX = x;
360 mRenderY = y;
361 if (w || h)
362 {
363 mRenderW = KeyboardWidth;
364 mRenderH = KeyboardHeight;
365 }
366
367 if (mAction) mAction->SetActionPos(mRenderX, mRenderY, mRenderW, mRenderH);
368 SetActionPos(mRenderX, mRenderY, mRenderW, mRenderH);
369 return 0;
370}
371
372int GUIKeyboard::GetSelection(int x, int y)
373{
Dees_Troy30b962e2012-10-19 20:48:59 -0400374 if (x < mRenderX || x - mRenderX > (int)KeyboardWidth || y < mRenderY || y - mRenderY > (int)KeyboardHeight) return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400375 return 0;
376}
377
378int GUIKeyboard::NotifyTouch(TOUCH_STATE state, int x, int y)
379{
380 static int startSelection = -1, was_held = 0, startX = 0;
381 static unsigned char initial_key = 0;
382 unsigned int indexy, indexx, rely, relx, rowIndex = 0;
383
384 rely = y - mRenderY;
385 relx = x - mRenderX;
386
387 if (!isConditionTrue()) return -1;
388
389 switch (state)
390 {
391 case TOUCH_START:
392 if (GetSelection(x, y) == 0) {
393 startSelection = -1;
394 was_held = 0;
395 startX = x;
396 // Find the correct row
397 for (indexy=0; indexy<MAX_KEYBOARD_ROWS; indexy++) {
398 if (row_heights[currentLayout - 1][indexy] > rely) {
399 rowIndex = indexy;
Dees_Troy30b962e2012-10-19 20:48:59 -0400400 rowY = indexy;
Dees_Troy51a0e822012-09-05 15:24:24 -0400401 indexy = MAX_KEYBOARD_ROWS;
402 }
403 }
404
405 // Find the correct key (column)
406 for (indexx=0; indexx<MAX_KEYBOARD_KEYS; indexx++) {
407 if (keyboard_keys[currentLayout - 1][rowIndex][indexx].end_x > relx) {
408 // This is the key that was pressed!
409 initial_key = keyboard_keys[currentLayout - 1][rowIndex][indexx].key;
Dees_Troy30b962e2012-10-19 20:48:59 -0400410 colX = indexx;
Dees_Troy51a0e822012-09-05 15:24:24 -0400411 indexx = MAX_KEYBOARD_KEYS;
412 }
413 }
Dees_Troy30b962e2012-10-19 20:48:59 -0400414 if (initial_key != 0)
415 highlightRenderCount = -1;
416 else
417 highlightRenderCount = 0;
418 mRendered = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400419 } else {
Dees_Troy30b962e2012-10-19 20:48:59 -0400420 if (highlightRenderCount != 0)
421 mRendered = false;
422 highlightRenderCount = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400423 startSelection = 0;
424 }
425 break;
426 case TOUCH_DRAG:
427 break;
428 case TOUCH_RELEASE:
Dees_Troy0cb64e52012-10-15 15:56:10 -0400429 if (x < startX - (KeyboardWidth * 0.5)) {
Dees_Troy30b962e2012-10-19 20:48:59 -0400430 if (highlightRenderCount != 0) {
431 highlightRenderCount = 0;
432 mRendered = false;
433 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400434 PageManager::NotifyKeyboard(KEYBOARD_SWIPE_LEFT);
435 return 0;
Dees_Troy0cb64e52012-10-15 15:56:10 -0400436 } else if (x > startX + (KeyboardWidth * 0.5)) {
Dees_Troy30b962e2012-10-19 20:48:59 -0400437 if (highlightRenderCount != 0) {
438 highlightRenderCount = 0;
439 mRendered = false;
440 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400441 PageManager::NotifyKeyboard(KEYBOARD_SWIPE_RIGHT);
442 return 0;
443 }
444
445 case TOUCH_HOLD:
446 case TOUCH_REPEAT:
447
Dees_Troy30b962e2012-10-19 20:48:59 -0400448 if (startSelection == 0 || GetSelection(x, y) == -1) {
449 if (highlightRenderCount != 0) {
450 highlightRenderCount = 0;
451 mRendered = false;
452 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400453 return 0;
Dees_Troy30b962e2012-10-19 20:48:59 -0400454 } else if (highlightRenderCount != 0) {
455 if (state == TOUCH_RELEASE)
456 highlightRenderCount = 2;
457 else
458 highlightRenderCount = -1;
459 mRendered = false;
460 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400461
462 // Find the correct row
463 for (indexy=0; indexy<MAX_KEYBOARD_ROWS; indexy++) {
464 if (row_heights[currentLayout - 1][indexy] > rely) {
465 rowIndex = indexy;
466 indexy = MAX_KEYBOARD_ROWS;
467 }
468 }
469
470 // Find the correct key (column)
471 for (indexx=0; indexx<MAX_KEYBOARD_KEYS; indexx++) {
472 if (keyboard_keys[currentLayout - 1][rowIndex][indexx].end_x > relx) {
473 // This is the key that was pressed!
474 if (keyboard_keys[currentLayout - 1][rowIndex][indexx].key != initial_key) {
475 // We dragged off of the starting key
476 startSelection = 0;
477 break;
478 } else if (state == TOUCH_RELEASE && was_held == 0) {
Ethan Yonker03db3262014-02-05 08:02:06 -0600479 DataManager::Vibrate("tw_keyboard_vibrate");
Dees_Troy51a0e822012-09-05 15:24:24 -0400480 if ((int)keyboard_keys[currentLayout - 1][rowIndex][indexx].key < KEYBOARD_SPECIAL_KEYS && (int)keyboard_keys[currentLayout - 1][rowIndex][indexx].key > 0) {
481 // Regular key
482 PageManager::NotifyKeyboard(keyboard_keys[currentLayout - 1][rowIndex][indexx].key);
483 } else if ((int)keyboard_keys[currentLayout - 1][rowIndex][indexx].key == KEYBOARD_LAYOUT) {
484 // Switch layouts
485 currentLayout = keyboard_keys[currentLayout - 1][rowIndex][indexx].layout;
486 mRendered = false;
487 } else if ((int)keyboard_keys[currentLayout - 1][rowIndex][indexx].key == KEYBOARD_ACTION) {
488 // Action
Dees_Troy30b962e2012-10-19 20:48:59 -0400489 highlightRenderCount = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400490 if (mAction) {
491 // Keyboard has its own action defined
492 return (mAction ? mAction->NotifyTouch(state, x, y) : 1);
493 } else {
494 // Send action notification
495 PageManager::NotifyKeyboard(keyboard_keys[currentLayout - 1][rowIndex][indexx].key);
496 }
497 }
498 } else if (state == TOUCH_HOLD) {
499 was_held = 1;
500 if ((int)keyboard_keys[currentLayout - 1][rowIndex][indexx].key == KEYBOARD_BACKSPACE) {
501 // Repeat backspace
502 PageManager::NotifyKeyboard(keyboard_keys[currentLayout - 1][rowIndex][indexx].key);
503 } else if ((int)keyboard_keys[currentLayout - 1][rowIndex][indexx].longpresskey < KEYBOARD_SPECIAL_KEYS && (int)keyboard_keys[currentLayout - 1][rowIndex][indexx].longpresskey > 0) {
504 // Long Press Key
Ethan Yonker03db3262014-02-05 08:02:06 -0600505 DataManager::Vibrate("tw_keyboard_vibrate");
Dees_Troy51a0e822012-09-05 15:24:24 -0400506 PageManager::NotifyKeyboard(keyboard_keys[currentLayout - 1][rowIndex][indexx].longpresskey);
507 }
508 } else if (state == TOUCH_REPEAT) {
509 was_held = 1;
510 if ((int)keyboard_keys[currentLayout - 1][rowIndex][indexx].key == KEYBOARD_BACKSPACE) {
511 // Repeat backspace
512 PageManager::NotifyKeyboard(keyboard_keys[currentLayout - 1][rowIndex][indexx].key);
513 }
514 }
515 indexx = MAX_KEYBOARD_KEYS;
516 }
517 }
518 break;
519 }
520
521 return 0;
522}