blob: f44e563573878411d376bf61cb2083b519478b1f [file] [log] [blame]
Dees_Troy30b962e2012-10-19 20:48:59 -04001/* keyboard.cpp - GUIKeyboard object
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15 * 02110-1301, USA.
16 *
17 * The code was written from scratch by Dees_Troy dees_troy at
18 * yahoo
19 *
20 * Copyright (c) 2012
21 */
Dees_Troy51a0e822012-09-05 15:24:24 -040022
23#include <stdarg.h>
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27#include <fcntl.h>
28#include <sys/reboot.h>
29#include <sys/stat.h>
30#include <sys/time.h>
31#include <sys/mman.h>
32#include <sys/types.h>
33#include <sys/ioctl.h>
34#include <time.h>
35#include <unistd.h>
36#include <stdlib.h>
37
38#include <string>
39
40extern "C" {
41#include "../common.h"
42#include "../minuitwrp/minui.h"
43#include "../recovery_ui.h"
44}
45
46#include "rapidxml.hpp"
47#include "objects.hpp"
48
49GUIKeyboard::GUIKeyboard(xml_node<>* node)
50 : Conditional(node)
51{
52 int layoutindex, rowindex, keyindex, Xindex, Yindex, keyHeight = 0, keyWidth = 0;
Dees_Troy30b962e2012-10-19 20:48:59 -040053 rowY = colX = -1;
54 highlightRenderCount = hasHighlight = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -040055 char resource[9], layout[7], row[4], key[5], longpress[6];
56 xml_attribute<>* attr;
57 xml_node<>* child;
58 xml_node<>* keylayout;
59 xml_node<>* keyrow;
60
61 for (layoutindex=0; layoutindex<MAX_KEYBOARD_LAYOUTS; layoutindex++)
62 keyboardImg[layoutindex] = NULL;
63
64 mRendered = false;
65 currentLayout = 1;
66 mAction = NULL;
67 KeyboardHeight = KeyboardWidth = cursorLocation = 0;
68
69 if (!node) return;
70
71 // Load the action
72 child = node->first_node("action");
73 if (child)
74 {
75 mAction = new GUIAction(node);
76 }
77
Dees_Troy30b962e2012-10-19 20:48:59 -040078 memset(&mHighlightColor, 0, sizeof(COLOR));
79 child = node->first_node("highlight");
80 if (child) {
81 attr = child->first_attribute("color");
82 if (attr) {
83 hasHighlight = 1;
84 std::string color = attr->value();
85 ConvertStrToColor(color, &mHighlightColor);
86 }
87 }
88
Dees_Troy51a0e822012-09-05 15:24:24 -040089 // Load the images for the different layouts
90 child = node->first_node("layout");
91 if (child)
92 {
93 layoutindex = 1;
94 strcpy(resource, "resource1");
95 attr = child->first_attribute(resource);
96 while (attr && layoutindex < (MAX_KEYBOARD_LAYOUTS + 1)) {
97 keyboardImg[layoutindex - 1] = PageManager::FindResource(attr->value());
98
99 layoutindex++;
100 resource[8] = (char)(layoutindex + 48);
101 attr = child->first_attribute(resource);
102 }
103 }
104
105 // Check the first image to get height and width
106 if (keyboardImg[0] && keyboardImg[0]->GetResource())
107 {
108 KeyboardWidth = gr_get_width(keyboardImg[0]->GetResource());
109 KeyboardHeight = gr_get_height(keyboardImg[0]->GetResource());
110 }
111
112 // Load all of the layout maps
113 layoutindex = 1;
114 strcpy(layout, "layout1");
115 keylayout = node->first_node(layout);
116 while (keylayout)
117 {
118 if (layoutindex > MAX_KEYBOARD_LAYOUTS) {
119 LOGE("Too many layouts defined in keyboard.\n");
120 return;
121 }
122
123 child = keylayout->first_node("keysize");
124 if (child) {
125 attr = child->first_attribute("height");
126 if (attr)
127 keyHeight = atoi(attr->value());
128 else
129 keyHeight = 0;
130 attr = child->first_attribute("width");
131 if (attr)
132 keyWidth = atoi(attr->value());
133 else
134 keyWidth = 0;
135 }
136
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) {
144 LOGE("Too many rows defined in keyboard.\n");
145 return;
146 }
147
148 Yindex += keyHeight;
149 row_heights[layoutindex - 1][rowindex - 1] = Yindex;
150
151 keyindex = 1;
152 Xindex = 0;
153 strcpy(key, "key01");
154 attr = keyrow->first_attribute(key);
155
156 while (attr) {
157 string stratt;
158 char keyinfo[255];
159
160 if (keyindex > MAX_KEYBOARD_KEYS) {
161 LOGE("Too many keys defined in a keyboard row.\n");
162 return;
163 }
164
165 stratt = attr->value();
166 if (strlen(stratt.c_str()) >= 255) {
167 LOGE("Key info on layout%i, row%i, key%dd is too long.\n", layoutindex, rowindex, keyindex);
168 return;
169 }
170
171 strcpy(keyinfo, stratt.c_str());
172
173 if (strlen(keyinfo) == 0) {
174 LOGE("No key info on layout%i, row%i, key%dd.\n", layoutindex, rowindex, keyindex);
175 return;
176 }
177
178 if (strlen(keyinfo) == 1) {
179 // This is a single key, simple definition
180 keyboard_keys[layoutindex - 1][rowindex - 1][keyindex - 1].key = keyinfo[0];
181 Xindex += keyWidth;
182 keyboard_keys[layoutindex - 1][rowindex - 1][keyindex - 1].end_x = Xindex - 1;
183 } else {
184 // This key has extra data
185 char* ptr;
186 char* offset;
187 char* keyitem;
188 char foratoi[10];
189
190 ptr = keyinfo;
191 offset = keyinfo;
192 while (*ptr > 32 && *ptr != ':')
193 ptr++;
194 if (*ptr != 0)
195 *ptr = 0;
196
197 strcpy(foratoi, offset);
198 Xindex += atoi(foratoi);
199 keyboard_keys[layoutindex - 1][rowindex - 1][keyindex - 1].end_x = Xindex - 1;
200
201 ptr++;
202 if (*ptr == 0) {
203 // This is an empty area
204 keyboard_keys[layoutindex - 1][rowindex - 1][keyindex - 1].key = 0;
205 } else if (strlen(ptr) == 1) {
206 // This is the character that this key uses
207 keyboard_keys[layoutindex - 1][rowindex - 1][keyindex - 1].key = *ptr;
208 } else if (*ptr == 'c') {
209 // This is an ASCII character code
210 keyitem = ptr + 2;
211 strcpy(foratoi, keyitem);
212 keyboard_keys[layoutindex - 1][rowindex - 1][keyindex - 1].key = atoi(foratoi);
213 } else if (*ptr == 'l') {
214 // This is a different layout
215 keyitem = ptr + 6;
216 keyboard_keys[layoutindex - 1][rowindex - 1][keyindex - 1].key = KEYBOARD_LAYOUT;
217 strcpy(foratoi, keyitem);
218 keyboard_keys[layoutindex - 1][rowindex - 1][keyindex - 1].layout = atoi(foratoi);
219 } else if (*ptr == 'a') {
220 // This is an action
221 keyboard_keys[layoutindex - 1][rowindex - 1][keyindex - 1].key = KEYBOARD_ACTION;
222 } else
223 LOGE("Invalid key info on layout%i, row%i, key%02i.\n", layoutindex, rowindex, keyindex);
224 }
225
226 // PROCESS LONG PRESS INFO IF EXISTS
227 sprintf(longpress, "long%02i", keyindex);
228 attr = keyrow->first_attribute(longpress);
229 if (attr) {
230 stratt = attr->value();
231 if (strlen(stratt.c_str()) >= 255) {
232 LOGE("Key info on layout%i, row%i, key%dd is too long.\n", layoutindex, rowindex, keyindex);
233 return;
234 }
235
236 strcpy(keyinfo, stratt.c_str());
237
238 if (strlen(keyinfo) == 0) {
239 LOGE("No long press info on layout%i, row%i, long%dd.\n", layoutindex, rowindex, keyindex);
240 return;
241 }
242
243 if (strlen(keyinfo) == 1) {
244 // This is a single key, simple definition
245 keyboard_keys[layoutindex - 1][rowindex - 1][keyindex - 1].longpresskey = keyinfo[0];
246 } else {
247 // This key has extra data
248 char* ptr;
249 char* offset;
250 char* keyitem;
251 char foratoi[10];
252
253 ptr = keyinfo;
254 offset = keyinfo;
255 while (*ptr > 32 && *ptr != ':')
256 ptr++;
257 if (*ptr != 0)
258 *ptr = 0;
259
260 strcpy(foratoi, offset);
261 Xindex += atoi(foratoi);
262
263 ptr++;
264 if (*ptr == 0) {
265 // This is an empty area
266 keyboard_keys[layoutindex - 1][rowindex - 1][keyindex - 1].longpresskey = 0;
267 } else if (strlen(ptr) == 1) {
268 // This is the character that this key uses
269 keyboard_keys[layoutindex - 1][rowindex - 1][keyindex - 1].longpresskey = *ptr;
270 } else if (*ptr == 'c') {
271 // This is an ASCII character code
272 keyitem = ptr + 2;
273 strcpy(foratoi, keyitem);
274 keyboard_keys[layoutindex - 1][rowindex - 1][keyindex - 1].longpresskey = atoi(foratoi);
275 } else if (*ptr == 'l') {
276 // This is a different layout
277 keyitem = ptr + 6;
278 keyboard_keys[layoutindex - 1][rowindex - 1][keyindex - 1].longpresskey = KEYBOARD_LAYOUT;
279 strcpy(foratoi, keyitem);
280 keyboard_keys[layoutindex - 1][rowindex - 1][keyindex - 1].layout = atoi(foratoi);
281 } else if (*ptr == 'a') {
282 // This is an action
283 keyboard_keys[layoutindex - 1][rowindex - 1][keyindex - 1].longpresskey = KEYBOARD_ACTION;
284 } else
285 LOGE("Invalid long press key info on layout%i, row%i, long%02i.\n", layoutindex, rowindex, keyindex);
286 }
287 }
288 keyindex++;
289 sprintf(key, "key%02i", keyindex);
290 attr = keyrow->first_attribute(key);
291 }
292 rowindex++;
293 row[3] = (char)(rowindex + 48);
294 keyrow = keylayout->first_node(row);
295 }
296 layoutindex++;
297 layout[6] = (char)(layoutindex + 48);
298 keylayout = node->first_node(layout);
299 }
300
301 int x, y, w, h;
302 // Load the placement
303 LoadPlacement(node->first_node("placement"), &x, &y, &w, &h);
304 SetActionPos(x, y, KeyboardWidth, KeyboardHeight);
305 SetRenderPos(x, y, w, h);
306 return;
307}
308
309GUIKeyboard::~GUIKeyboard()
310{
311 int layoutindex;
312
313 for (layoutindex=0; layoutindex<MAX_KEYBOARD_LAYOUTS; layoutindex++)
314 if (keyboardImg[layoutindex]) delete keyboardImg[layoutindex];
315}
316
317int GUIKeyboard::Render(void)
318{
319 if (!isConditionTrue())
320 {
321 mRendered = false;
322 return 0;
323 }
324
325 int ret = 0;
326
327 if (keyboardImg[currentLayout - 1] && keyboardImg[currentLayout - 1]->GetResource())
328 gr_blit(keyboardImg[currentLayout - 1]->GetResource(), 0, 0, KeyboardWidth, KeyboardHeight, mRenderX, mRenderY);
329
Dees_Troy30b962e2012-10-19 20:48:59 -0400330 if (hasHighlight && highlightRenderCount != 0) {
331 int boxheight, boxwidth, x;
332 if (rowY == 0)
333 boxheight = row_heights[currentLayout - 1][rowY];
334 else
335 boxheight = row_heights[currentLayout - 1][rowY] - row_heights[currentLayout - 1][rowY - 1];
336 if (colX == 0) {
337 x = mRenderX;
338 boxwidth = keyboard_keys[currentLayout - 1][rowY][colX].end_x;
339 } else {
340 x = mRenderX + keyboard_keys[currentLayout - 1][rowY][colX - 1].end_x;
341 boxwidth = keyboard_keys[currentLayout - 1][rowY][colX].end_x - keyboard_keys[currentLayout - 1][rowY][colX - 1].end_x;
342 }
343 gr_color(mHighlightColor.red, mHighlightColor.green, mHighlightColor.blue, mHighlightColor.alpha);
344 gr_fill(x, mRenderY + row_heights[currentLayout - 1][rowY - 1], boxwidth, boxheight);
345 if (highlightRenderCount > 0)
346 highlightRenderCount--;
347 } else
348 mRendered = true;
349
Dees_Troy51a0e822012-09-05 15:24:24 -0400350 return ret;
351}
352
353int GUIKeyboard::Update(void)
354{
355 if (!isConditionTrue()) return (mRendered ? 2 : 0);
356 if (!mRendered) return 2;
357
358 return 0;
359}
360
361int GUIKeyboard::SetRenderPos(int x, int y, int w, int h)
362{
363 mRenderX = x;
364 mRenderY = y;
365 if (w || h)
366 {
367 mRenderW = KeyboardWidth;
368 mRenderH = KeyboardHeight;
369 }
370
371 if (mAction) mAction->SetActionPos(mRenderX, mRenderY, mRenderW, mRenderH);
372 SetActionPos(mRenderX, mRenderY, mRenderW, mRenderH);
373 return 0;
374}
375
376int GUIKeyboard::GetSelection(int x, int y)
377{
Dees_Troy30b962e2012-10-19 20:48:59 -0400378 if (x < mRenderX || x - mRenderX > (int)KeyboardWidth || y < mRenderY || y - mRenderY > (int)KeyboardHeight) return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400379 return 0;
380}
381
382int GUIKeyboard::NotifyTouch(TOUCH_STATE state, int x, int y)
383{
384 static int startSelection = -1, was_held = 0, startX = 0;
385 static unsigned char initial_key = 0;
386 unsigned int indexy, indexx, rely, relx, rowIndex = 0;
387
388 rely = y - mRenderY;
389 relx = x - mRenderX;
390
391 if (!isConditionTrue()) return -1;
392
393 switch (state)
394 {
395 case TOUCH_START:
396 if (GetSelection(x, y) == 0) {
397 startSelection = -1;
398 was_held = 0;
399 startX = x;
400 // Find the correct row
401 for (indexy=0; indexy<MAX_KEYBOARD_ROWS; indexy++) {
402 if (row_heights[currentLayout - 1][indexy] > rely) {
403 rowIndex = indexy;
Dees_Troy30b962e2012-10-19 20:48:59 -0400404 rowY = indexy;
Dees_Troy51a0e822012-09-05 15:24:24 -0400405 indexy = MAX_KEYBOARD_ROWS;
406 }
407 }
408
409 // Find the correct key (column)
410 for (indexx=0; indexx<MAX_KEYBOARD_KEYS; indexx++) {
411 if (keyboard_keys[currentLayout - 1][rowIndex][indexx].end_x > relx) {
412 // This is the key that was pressed!
413 initial_key = keyboard_keys[currentLayout - 1][rowIndex][indexx].key;
Dees_Troy30b962e2012-10-19 20:48:59 -0400414 colX = indexx;
Dees_Troy51a0e822012-09-05 15:24:24 -0400415 indexx = MAX_KEYBOARD_KEYS;
416 }
417 }
Dees_Troy30b962e2012-10-19 20:48:59 -0400418 if (initial_key != 0)
419 highlightRenderCount = -1;
420 else
421 highlightRenderCount = 0;
422 mRendered = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400423 } else {
Dees_Troy30b962e2012-10-19 20:48:59 -0400424 if (highlightRenderCount != 0)
425 mRendered = false;
426 highlightRenderCount = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400427 startSelection = 0;
428 }
429 break;
430 case TOUCH_DRAG:
431 break;
432 case TOUCH_RELEASE:
Dees_Troy0cb64e52012-10-15 15:56:10 -0400433 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_LEFT);
439 return 0;
Dees_Troy0cb64e52012-10-15 15:56:10 -0400440 } else if (x > startX + (KeyboardWidth * 0.5)) {
Dees_Troy30b962e2012-10-19 20:48:59 -0400441 if (highlightRenderCount != 0) {
442 highlightRenderCount = 0;
443 mRendered = false;
444 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400445 PageManager::NotifyKeyboard(KEYBOARD_SWIPE_RIGHT);
446 return 0;
447 }
448
449 case TOUCH_HOLD:
450 case TOUCH_REPEAT:
451
Dees_Troy30b962e2012-10-19 20:48:59 -0400452 if (startSelection == 0 || GetSelection(x, y) == -1) {
453 if (highlightRenderCount != 0) {
454 highlightRenderCount = 0;
455 mRendered = false;
456 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400457 return 0;
Dees_Troy30b962e2012-10-19 20:48:59 -0400458 } else if (highlightRenderCount != 0) {
459 if (state == TOUCH_RELEASE)
460 highlightRenderCount = 2;
461 else
462 highlightRenderCount = -1;
463 mRendered = false;
464 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400465
466 // Find the correct row
467 for (indexy=0; indexy<MAX_KEYBOARD_ROWS; indexy++) {
468 if (row_heights[currentLayout - 1][indexy] > rely) {
469 rowIndex = indexy;
470 indexy = MAX_KEYBOARD_ROWS;
471 }
472 }
473
474 // Find the correct key (column)
475 for (indexx=0; indexx<MAX_KEYBOARD_KEYS; indexx++) {
476 if (keyboard_keys[currentLayout - 1][rowIndex][indexx].end_x > relx) {
477 // This is the key that was pressed!
478 if (keyboard_keys[currentLayout - 1][rowIndex][indexx].key != initial_key) {
479 // We dragged off of the starting key
480 startSelection = 0;
481 break;
482 } else if (state == TOUCH_RELEASE && was_held == 0) {
483 if ((int)keyboard_keys[currentLayout - 1][rowIndex][indexx].key < KEYBOARD_SPECIAL_KEYS && (int)keyboard_keys[currentLayout - 1][rowIndex][indexx].key > 0) {
484 // Regular key
485 PageManager::NotifyKeyboard(keyboard_keys[currentLayout - 1][rowIndex][indexx].key);
486 } else if ((int)keyboard_keys[currentLayout - 1][rowIndex][indexx].key == KEYBOARD_LAYOUT) {
487 // Switch layouts
488 currentLayout = keyboard_keys[currentLayout - 1][rowIndex][indexx].layout;
489 mRendered = false;
490 } else if ((int)keyboard_keys[currentLayout - 1][rowIndex][indexx].key == KEYBOARD_ACTION) {
491 // Action
Dees_Troy30b962e2012-10-19 20:48:59 -0400492 highlightRenderCount = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400493 if (mAction) {
494 // Keyboard has its own action defined
495 return (mAction ? mAction->NotifyTouch(state, x, y) : 1);
496 } else {
497 // Send action notification
498 PageManager::NotifyKeyboard(keyboard_keys[currentLayout - 1][rowIndex][indexx].key);
499 }
500 }
501 } else if (state == TOUCH_HOLD) {
502 was_held = 1;
503 if ((int)keyboard_keys[currentLayout - 1][rowIndex][indexx].key == KEYBOARD_BACKSPACE) {
504 // Repeat backspace
505 PageManager::NotifyKeyboard(keyboard_keys[currentLayout - 1][rowIndex][indexx].key);
506 } else if ((int)keyboard_keys[currentLayout - 1][rowIndex][indexx].longpresskey < KEYBOARD_SPECIAL_KEYS && (int)keyboard_keys[currentLayout - 1][rowIndex][indexx].longpresskey > 0) {
507 // Long Press Key
508 PageManager::NotifyKeyboard(keyboard_keys[currentLayout - 1][rowIndex][indexx].longpresskey);
509 }
510 } else if (state == TOUCH_REPEAT) {
511 was_held = 1;
512 if ((int)keyboard_keys[currentLayout - 1][rowIndex][indexx].key == KEYBOARD_BACKSPACE) {
513 // Repeat backspace
514 PageManager::NotifyKeyboard(keyboard_keys[currentLayout - 1][rowIndex][indexx].key);
515 }
516 }
517 indexx = MAX_KEYBOARD_KEYS;
518 }
519 }
520 break;
521 }
522
523 return 0;
524}