blob: 95cdbf1c803344e922f3233fe1bceb183c9c0756 [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>
33
34#include <string>
35
36extern "C" {
Dees_Troy2673cec2013-04-02 20:22:16 +000037#include "../twcommon.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040038#include "../minuitwrp/minui.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040039}
40
41#include "rapidxml.hpp"
42#include "objects.hpp"
43
44GUIKeyboard::GUIKeyboard(xml_node<>* node)
45 : Conditional(node)
46{
47 int layoutindex, rowindex, keyindex, Xindex, Yindex, keyHeight = 0, keyWidth = 0;
Dees_Troy30b962e2012-10-19 20:48:59 -040048 rowY = colX = -1;
49 highlightRenderCount = hasHighlight = 0;
Dees_Troy3a16cb52013-01-10 15:16:02 +000050 char resource[10], layout[8], row[5], key[6], longpress[7];
Dees_Troy51a0e822012-09-05 15:24:24 -040051 xml_attribute<>* attr;
52 xml_node<>* child;
53 xml_node<>* keylayout;
54 xml_node<>* keyrow;
55
56 for (layoutindex=0; layoutindex<MAX_KEYBOARD_LAYOUTS; layoutindex++)
57 keyboardImg[layoutindex] = NULL;
58
59 mRendered = false;
60 currentLayout = 1;
61 mAction = NULL;
62 KeyboardHeight = KeyboardWidth = cursorLocation = 0;
63
64 if (!node) return;
65
66 // Load the action
67 child = node->first_node("action");
68 if (child)
69 {
70 mAction = new GUIAction(node);
71 }
72
Dees_Troy30b962e2012-10-19 20:48:59 -040073 memset(&mHighlightColor, 0, sizeof(COLOR));
74 child = node->first_node("highlight");
75 if (child) {
76 attr = child->first_attribute("color");
77 if (attr) {
78 hasHighlight = 1;
79 std::string color = attr->value();
80 ConvertStrToColor(color, &mHighlightColor);
81 }
82 }
83
Dees_Troy51a0e822012-09-05 15:24:24 -040084 // Load the images for the different layouts
85 child = node->first_node("layout");
86 if (child)
87 {
88 layoutindex = 1;
89 strcpy(resource, "resource1");
90 attr = child->first_attribute(resource);
91 while (attr && layoutindex < (MAX_KEYBOARD_LAYOUTS + 1)) {
92 keyboardImg[layoutindex - 1] = PageManager::FindResource(attr->value());
93
94 layoutindex++;
95 resource[8] = (char)(layoutindex + 48);
96 attr = child->first_attribute(resource);
97 }
98 }
99
100 // Check the first image to get height and width
101 if (keyboardImg[0] && keyboardImg[0]->GetResource())
102 {
103 KeyboardWidth = gr_get_width(keyboardImg[0]->GetResource());
104 KeyboardHeight = gr_get_height(keyboardImg[0]->GetResource());
105 }
106
107 // Load all of the layout maps
108 layoutindex = 1;
109 strcpy(layout, "layout1");
110 keylayout = node->first_node(layout);
111 while (keylayout)
112 {
113 if (layoutindex > MAX_KEYBOARD_LAYOUTS) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000114 LOGERR("Too many layouts defined in keyboard.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400115 return;
116 }
117
118 child = keylayout->first_node("keysize");
119 if (child) {
120 attr = child->first_attribute("height");
121 if (attr)
122 keyHeight = atoi(attr->value());
123 else
124 keyHeight = 0;
125 attr = child->first_attribute("width");
126 if (attr)
127 keyWidth = atoi(attr->value());
128 else
129 keyWidth = 0;
130 }
131
132 rowindex = 1;
133 Yindex = 0;
134 strcpy(row, "row1");
135 keyrow = keylayout->first_node(row);
136 while (keyrow)
137 {
138 if (rowindex > MAX_KEYBOARD_ROWS) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000139 LOGERR("Too many rows defined in keyboard.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400140 return;
141 }
142
143 Yindex += keyHeight;
144 row_heights[layoutindex - 1][rowindex - 1] = Yindex;
145
146 keyindex = 1;
147 Xindex = 0;
148 strcpy(key, "key01");
149 attr = keyrow->first_attribute(key);
150
151 while (attr) {
152 string stratt;
153 char keyinfo[255];
154
155 if (keyindex > MAX_KEYBOARD_KEYS) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000156 LOGERR("Too many keys defined in a keyboard row.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400157 return;
158 }
159
160 stratt = attr->value();
161 if (strlen(stratt.c_str()) >= 255) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000162 LOGERR("Key info on layout%i, row%i, key%dd is too long.\n", layoutindex, rowindex, keyindex);
Dees_Troy51a0e822012-09-05 15:24:24 -0400163 return;
164 }
165
166 strcpy(keyinfo, stratt.c_str());
167
168 if (strlen(keyinfo) == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000169 LOGERR("No key info on layout%i, row%i, key%dd.\n", layoutindex, rowindex, keyindex);
Dees_Troy51a0e822012-09-05 15:24:24 -0400170 return;
171 }
172
173 if (strlen(keyinfo) == 1) {
174 // This is a single key, simple definition
175 keyboard_keys[layoutindex - 1][rowindex - 1][keyindex - 1].key = keyinfo[0];
176 Xindex += keyWidth;
177 keyboard_keys[layoutindex - 1][rowindex - 1][keyindex - 1].end_x = Xindex - 1;
178 } else {
179 // This key has extra data
180 char* ptr;
181 char* offset;
182 char* keyitem;
183 char foratoi[10];
184
185 ptr = keyinfo;
186 offset = keyinfo;
187 while (*ptr > 32 && *ptr != ':')
188 ptr++;
189 if (*ptr != 0)
190 *ptr = 0;
191
192 strcpy(foratoi, offset);
193 Xindex += atoi(foratoi);
194 keyboard_keys[layoutindex - 1][rowindex - 1][keyindex - 1].end_x = Xindex - 1;
195
196 ptr++;
197 if (*ptr == 0) {
198 // This is an empty area
199 keyboard_keys[layoutindex - 1][rowindex - 1][keyindex - 1].key = 0;
200 } else if (strlen(ptr) == 1) {
201 // This is the character that this key uses
202 keyboard_keys[layoutindex - 1][rowindex - 1][keyindex - 1].key = *ptr;
203 } else if (*ptr == 'c') {
204 // This is an ASCII character code
205 keyitem = ptr + 2;
206 strcpy(foratoi, keyitem);
207 keyboard_keys[layoutindex - 1][rowindex - 1][keyindex - 1].key = atoi(foratoi);
208 } else if (*ptr == 'l') {
209 // This is a different layout
210 keyitem = ptr + 6;
211 keyboard_keys[layoutindex - 1][rowindex - 1][keyindex - 1].key = KEYBOARD_LAYOUT;
212 strcpy(foratoi, keyitem);
213 keyboard_keys[layoutindex - 1][rowindex - 1][keyindex - 1].layout = atoi(foratoi);
214 } else if (*ptr == 'a') {
215 // This is an action
216 keyboard_keys[layoutindex - 1][rowindex - 1][keyindex - 1].key = KEYBOARD_ACTION;
217 } else
Dees_Troy2673cec2013-04-02 20:22:16 +0000218 LOGERR("Invalid key info on layout%i, row%i, key%02i.\n", layoutindex, rowindex, keyindex);
Dees_Troy51a0e822012-09-05 15:24:24 -0400219 }
220
221 // PROCESS LONG PRESS INFO IF EXISTS
222 sprintf(longpress, "long%02i", keyindex);
223 attr = keyrow->first_attribute(longpress);
224 if (attr) {
225 stratt = attr->value();
226 if (strlen(stratt.c_str()) >= 255) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000227 LOGERR("Key info on layout%i, row%i, key%dd is too long.\n", layoutindex, rowindex, keyindex);
Dees_Troy51a0e822012-09-05 15:24:24 -0400228 return;
229 }
230
231 strcpy(keyinfo, stratt.c_str());
232
233 if (strlen(keyinfo) == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000234 LOGERR("No long press info on layout%i, row%i, long%dd.\n", layoutindex, rowindex, keyindex);
Dees_Troy51a0e822012-09-05 15:24:24 -0400235 return;
236 }
237
238 if (strlen(keyinfo) == 1) {
239 // This is a single key, simple definition
240 keyboard_keys[layoutindex - 1][rowindex - 1][keyindex - 1].longpresskey = keyinfo[0];
241 } else {
242 // This key has extra data
243 char* ptr;
244 char* offset;
245 char* keyitem;
246 char foratoi[10];
247
248 ptr = keyinfo;
249 offset = keyinfo;
250 while (*ptr > 32 && *ptr != ':')
251 ptr++;
252 if (*ptr != 0)
253 *ptr = 0;
254
255 strcpy(foratoi, offset);
256 Xindex += atoi(foratoi);
257
258 ptr++;
259 if (*ptr == 0) {
260 // This is an empty area
261 keyboard_keys[layoutindex - 1][rowindex - 1][keyindex - 1].longpresskey = 0;
262 } else if (strlen(ptr) == 1) {
263 // This is the character that this key uses
264 keyboard_keys[layoutindex - 1][rowindex - 1][keyindex - 1].longpresskey = *ptr;
265 } else if (*ptr == 'c') {
266 // This is an ASCII character code
267 keyitem = ptr + 2;
268 strcpy(foratoi, keyitem);
269 keyboard_keys[layoutindex - 1][rowindex - 1][keyindex - 1].longpresskey = atoi(foratoi);
270 } else if (*ptr == 'l') {
271 // This is a different layout
272 keyitem = ptr + 6;
273 keyboard_keys[layoutindex - 1][rowindex - 1][keyindex - 1].longpresskey = KEYBOARD_LAYOUT;
274 strcpy(foratoi, keyitem);
275 keyboard_keys[layoutindex - 1][rowindex - 1][keyindex - 1].layout = atoi(foratoi);
276 } else if (*ptr == 'a') {
277 // This is an action
278 keyboard_keys[layoutindex - 1][rowindex - 1][keyindex - 1].longpresskey = KEYBOARD_ACTION;
279 } else
Dees_Troy2673cec2013-04-02 20:22:16 +0000280 LOGERR("Invalid long press key info on layout%i, row%i, long%02i.\n", layoutindex, rowindex, keyindex);
Dees_Troy51a0e822012-09-05 15:24:24 -0400281 }
282 }
283 keyindex++;
284 sprintf(key, "key%02i", keyindex);
285 attr = keyrow->first_attribute(key);
286 }
287 rowindex++;
288 row[3] = (char)(rowindex + 48);
289 keyrow = keylayout->first_node(row);
290 }
291 layoutindex++;
292 layout[6] = (char)(layoutindex + 48);
293 keylayout = node->first_node(layout);
294 }
295
296 int x, y, w, h;
297 // Load the placement
298 LoadPlacement(node->first_node("placement"), &x, &y, &w, &h);
299 SetActionPos(x, y, KeyboardWidth, KeyboardHeight);
300 SetRenderPos(x, y, w, h);
301 return;
302}
303
304GUIKeyboard::~GUIKeyboard()
305{
306 int layoutindex;
307
308 for (layoutindex=0; layoutindex<MAX_KEYBOARD_LAYOUTS; layoutindex++)
309 if (keyboardImg[layoutindex]) delete keyboardImg[layoutindex];
310}
311
312int GUIKeyboard::Render(void)
313{
314 if (!isConditionTrue())
315 {
316 mRendered = false;
317 return 0;
318 }
319
320 int ret = 0;
321
322 if (keyboardImg[currentLayout - 1] && keyboardImg[currentLayout - 1]->GetResource())
323 gr_blit(keyboardImg[currentLayout - 1]->GetResource(), 0, 0, KeyboardWidth, KeyboardHeight, mRenderX, mRenderY);
324
Dees_Troy30b962e2012-10-19 20:48:59 -0400325 if (hasHighlight && highlightRenderCount != 0) {
326 int boxheight, boxwidth, x;
327 if (rowY == 0)
328 boxheight = row_heights[currentLayout - 1][rowY];
329 else
330 boxheight = row_heights[currentLayout - 1][rowY] - row_heights[currentLayout - 1][rowY - 1];
331 if (colX == 0) {
332 x = mRenderX;
333 boxwidth = keyboard_keys[currentLayout - 1][rowY][colX].end_x;
334 } else {
335 x = mRenderX + keyboard_keys[currentLayout - 1][rowY][colX - 1].end_x;
336 boxwidth = keyboard_keys[currentLayout - 1][rowY][colX].end_x - keyboard_keys[currentLayout - 1][rowY][colX - 1].end_x;
337 }
338 gr_color(mHighlightColor.red, mHighlightColor.green, mHighlightColor.blue, mHighlightColor.alpha);
339 gr_fill(x, mRenderY + row_heights[currentLayout - 1][rowY - 1], boxwidth, boxheight);
340 if (highlightRenderCount > 0)
341 highlightRenderCount--;
342 } else
343 mRendered = true;
344
Dees_Troy51a0e822012-09-05 15:24:24 -0400345 return ret;
346}
347
348int GUIKeyboard::Update(void)
349{
350 if (!isConditionTrue()) return (mRendered ? 2 : 0);
351 if (!mRendered) return 2;
352
353 return 0;
354}
355
356int GUIKeyboard::SetRenderPos(int x, int y, int w, int h)
357{
358 mRenderX = x;
359 mRenderY = y;
360 if (w || h)
361 {
362 mRenderW = KeyboardWidth;
363 mRenderH = KeyboardHeight;
364 }
365
366 if (mAction) mAction->SetActionPos(mRenderX, mRenderY, mRenderW, mRenderH);
367 SetActionPos(mRenderX, mRenderY, mRenderW, mRenderH);
368 return 0;
369}
370
371int GUIKeyboard::GetSelection(int x, int y)
372{
Dees_Troy30b962e2012-10-19 20:48:59 -0400373 if (x < mRenderX || x - mRenderX > (int)KeyboardWidth || y < mRenderY || y - mRenderY > (int)KeyboardHeight) return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400374 return 0;
375}
376
377int GUIKeyboard::NotifyTouch(TOUCH_STATE state, int x, int y)
378{
379 static int startSelection = -1, was_held = 0, startX = 0;
380 static unsigned char initial_key = 0;
381 unsigned int indexy, indexx, rely, relx, rowIndex = 0;
382
383 rely = y - mRenderY;
384 relx = x - mRenderX;
385
386 if (!isConditionTrue()) return -1;
387
388 switch (state)
389 {
390 case TOUCH_START:
391 if (GetSelection(x, y) == 0) {
392 startSelection = -1;
393 was_held = 0;
394 startX = x;
395 // Find the correct row
396 for (indexy=0; indexy<MAX_KEYBOARD_ROWS; indexy++) {
397 if (row_heights[currentLayout - 1][indexy] > rely) {
398 rowIndex = indexy;
Dees_Troy30b962e2012-10-19 20:48:59 -0400399 rowY = indexy;
Dees_Troy51a0e822012-09-05 15:24:24 -0400400 indexy = MAX_KEYBOARD_ROWS;
401 }
402 }
403
404 // Find the correct key (column)
405 for (indexx=0; indexx<MAX_KEYBOARD_KEYS; indexx++) {
406 if (keyboard_keys[currentLayout - 1][rowIndex][indexx].end_x > relx) {
407 // This is the key that was pressed!
408 initial_key = keyboard_keys[currentLayout - 1][rowIndex][indexx].key;
Dees_Troy30b962e2012-10-19 20:48:59 -0400409 colX = indexx;
Dees_Troy51a0e822012-09-05 15:24:24 -0400410 indexx = MAX_KEYBOARD_KEYS;
411 }
412 }
Dees_Troy30b962e2012-10-19 20:48:59 -0400413 if (initial_key != 0)
414 highlightRenderCount = -1;
415 else
416 highlightRenderCount = 0;
417 mRendered = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400418 } else {
Dees_Troy30b962e2012-10-19 20:48:59 -0400419 if (highlightRenderCount != 0)
420 mRendered = false;
421 highlightRenderCount = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400422 startSelection = 0;
423 }
424 break;
425 case TOUCH_DRAG:
426 break;
427 case TOUCH_RELEASE:
Dees_Troy0cb64e52012-10-15 15:56:10 -0400428 if (x < startX - (KeyboardWidth * 0.5)) {
Dees_Troy30b962e2012-10-19 20:48:59 -0400429 if (highlightRenderCount != 0) {
430 highlightRenderCount = 0;
431 mRendered = false;
432 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400433 PageManager::NotifyKeyboard(KEYBOARD_SWIPE_LEFT);
434 return 0;
Dees_Troy0cb64e52012-10-15 15:56:10 -0400435 } else if (x > startX + (KeyboardWidth * 0.5)) {
Dees_Troy30b962e2012-10-19 20:48:59 -0400436 if (highlightRenderCount != 0) {
437 highlightRenderCount = 0;
438 mRendered = false;
439 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400440 PageManager::NotifyKeyboard(KEYBOARD_SWIPE_RIGHT);
441 return 0;
442 }
443
444 case TOUCH_HOLD:
445 case TOUCH_REPEAT:
446
Dees_Troy30b962e2012-10-19 20:48:59 -0400447 if (startSelection == 0 || GetSelection(x, y) == -1) {
448 if (highlightRenderCount != 0) {
449 highlightRenderCount = 0;
450 mRendered = false;
451 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400452 return 0;
Dees_Troy30b962e2012-10-19 20:48:59 -0400453 } else if (highlightRenderCount != 0) {
454 if (state == TOUCH_RELEASE)
455 highlightRenderCount = 2;
456 else
457 highlightRenderCount = -1;
458 mRendered = false;
459 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400460
461 // Find the correct row
462 for (indexy=0; indexy<MAX_KEYBOARD_ROWS; indexy++) {
463 if (row_heights[currentLayout - 1][indexy] > rely) {
464 rowIndex = indexy;
465 indexy = MAX_KEYBOARD_ROWS;
466 }
467 }
468
469 // Find the correct key (column)
470 for (indexx=0; indexx<MAX_KEYBOARD_KEYS; indexx++) {
471 if (keyboard_keys[currentLayout - 1][rowIndex][indexx].end_x > relx) {
472 // This is the key that was pressed!
473 if (keyboard_keys[currentLayout - 1][rowIndex][indexx].key != initial_key) {
474 // We dragged off of the starting key
475 startSelection = 0;
476 break;
477 } else if (state == TOUCH_RELEASE && was_held == 0) {
478 if ((int)keyboard_keys[currentLayout - 1][rowIndex][indexx].key < KEYBOARD_SPECIAL_KEYS && (int)keyboard_keys[currentLayout - 1][rowIndex][indexx].key > 0) {
479 // Regular key
480 PageManager::NotifyKeyboard(keyboard_keys[currentLayout - 1][rowIndex][indexx].key);
481 } else if ((int)keyboard_keys[currentLayout - 1][rowIndex][indexx].key == KEYBOARD_LAYOUT) {
482 // Switch layouts
483 currentLayout = keyboard_keys[currentLayout - 1][rowIndex][indexx].layout;
484 mRendered = false;
485 } else if ((int)keyboard_keys[currentLayout - 1][rowIndex][indexx].key == KEYBOARD_ACTION) {
486 // Action
Dees_Troy30b962e2012-10-19 20:48:59 -0400487 highlightRenderCount = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400488 if (mAction) {
489 // Keyboard has its own action defined
490 return (mAction ? mAction->NotifyTouch(state, x, y) : 1);
491 } else {
492 // Send action notification
493 PageManager::NotifyKeyboard(keyboard_keys[currentLayout - 1][rowIndex][indexx].key);
494 }
495 }
496 } else if (state == TOUCH_HOLD) {
497 was_held = 1;
498 if ((int)keyboard_keys[currentLayout - 1][rowIndex][indexx].key == KEYBOARD_BACKSPACE) {
499 // Repeat backspace
500 PageManager::NotifyKeyboard(keyboard_keys[currentLayout - 1][rowIndex][indexx].key);
501 } else if ((int)keyboard_keys[currentLayout - 1][rowIndex][indexx].longpresskey < KEYBOARD_SPECIAL_KEYS && (int)keyboard_keys[currentLayout - 1][rowIndex][indexx].longpresskey > 0) {
502 // Long Press Key
503 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}