blob: 7b6d006fa541a94392c8cf46e7f88afc4e219393 [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" {
Dees_Troy2673cec2013-04-02 20:22:16 +000041#include "../twcommon.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040042#include "../minuitwrp/minui.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040043}
44
45#include "rapidxml.hpp"
46#include "objects.hpp"
47
48GUIKeyboard::GUIKeyboard(xml_node<>* node)
49 : Conditional(node)
50{
51 int layoutindex, rowindex, keyindex, Xindex, Yindex, keyHeight = 0, keyWidth = 0;
Dees_Troy30b962e2012-10-19 20:48:59 -040052 rowY = colX = -1;
53 highlightRenderCount = hasHighlight = 0;
Dees_Troy3a16cb52013-01-10 15:16:02 +000054 char resource[10], layout[8], row[5], key[6], longpress[7];
Dees_Troy51a0e822012-09-05 15:24:24 -040055 xml_attribute<>* attr;
56 xml_node<>* child;
57 xml_node<>* keylayout;
58 xml_node<>* keyrow;
59
60 for (layoutindex=0; layoutindex<MAX_KEYBOARD_LAYOUTS; layoutindex++)
61 keyboardImg[layoutindex] = NULL;
62
63 mRendered = false;
64 currentLayout = 1;
65 mAction = NULL;
66 KeyboardHeight = KeyboardWidth = cursorLocation = 0;
67
68 if (!node) return;
69
70 // Load the action
71 child = node->first_node("action");
72 if (child)
73 {
74 mAction = new GUIAction(node);
75 }
76
Dees_Troy30b962e2012-10-19 20:48:59 -040077 memset(&mHighlightColor, 0, sizeof(COLOR));
78 child = node->first_node("highlight");
79 if (child) {
80 attr = child->first_attribute("color");
81 if (attr) {
82 hasHighlight = 1;
83 std::string color = attr->value();
84 ConvertStrToColor(color, &mHighlightColor);
85 }
86 }
87
Dees_Troy51a0e822012-09-05 15:24:24 -040088 // Load the images for the different layouts
89 child = node->first_node("layout");
90 if (child)
91 {
92 layoutindex = 1;
93 strcpy(resource, "resource1");
94 attr = child->first_attribute(resource);
95 while (attr && layoutindex < (MAX_KEYBOARD_LAYOUTS + 1)) {
96 keyboardImg[layoutindex - 1] = PageManager::FindResource(attr->value());
97
98 layoutindex++;
99 resource[8] = (char)(layoutindex + 48);
100 attr = child->first_attribute(resource);
101 }
102 }
103
104 // Check the first image to get height and width
105 if (keyboardImg[0] && keyboardImg[0]->GetResource())
106 {
107 KeyboardWidth = gr_get_width(keyboardImg[0]->GetResource());
108 KeyboardHeight = gr_get_height(keyboardImg[0]->GetResource());
109 }
110
111 // Load all of the layout maps
112 layoutindex = 1;
113 strcpy(layout, "layout1");
114 keylayout = node->first_node(layout);
115 while (keylayout)
116 {
117 if (layoutindex > MAX_KEYBOARD_LAYOUTS) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000118 LOGERR("Too many layouts defined in keyboard.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400119 return;
120 }
121
122 child = keylayout->first_node("keysize");
123 if (child) {
124 attr = child->first_attribute("height");
125 if (attr)
126 keyHeight = atoi(attr->value());
127 else
128 keyHeight = 0;
129 attr = child->first_attribute("width");
130 if (attr)
131 keyWidth = atoi(attr->value());
132 else
133 keyWidth = 0;
134 }
135
136 rowindex = 1;
137 Yindex = 0;
138 strcpy(row, "row1");
139 keyrow = keylayout->first_node(row);
140 while (keyrow)
141 {
142 if (rowindex > MAX_KEYBOARD_ROWS) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000143 LOGERR("Too many rows defined in keyboard.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400144 return;
145 }
146
147 Yindex += keyHeight;
148 row_heights[layoutindex - 1][rowindex - 1] = Yindex;
149
150 keyindex = 1;
151 Xindex = 0;
152 strcpy(key, "key01");
153 attr = keyrow->first_attribute(key);
154
155 while (attr) {
156 string stratt;
157 char keyinfo[255];
158
159 if (keyindex > MAX_KEYBOARD_KEYS) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000160 LOGERR("Too many keys defined in a keyboard row.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400161 return;
162 }
163
164 stratt = attr->value();
165 if (strlen(stratt.c_str()) >= 255) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000166 LOGERR("Key info on layout%i, row%i, key%dd is too long.\n", layoutindex, rowindex, keyindex);
Dees_Troy51a0e822012-09-05 15:24:24 -0400167 return;
168 }
169
170 strcpy(keyinfo, stratt.c_str());
171
172 if (strlen(keyinfo) == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000173 LOGERR("No key info on layout%i, row%i, key%dd.\n", layoutindex, rowindex, keyindex);
Dees_Troy51a0e822012-09-05 15:24:24 -0400174 return;
175 }
176
177 if (strlen(keyinfo) == 1) {
178 // This is a single key, simple definition
179 keyboard_keys[layoutindex - 1][rowindex - 1][keyindex - 1].key = keyinfo[0];
180 Xindex += keyWidth;
181 keyboard_keys[layoutindex - 1][rowindex - 1][keyindex - 1].end_x = Xindex - 1;
182 } else {
183 // This key has extra data
184 char* ptr;
185 char* offset;
186 char* keyitem;
187 char foratoi[10];
188
189 ptr = keyinfo;
190 offset = keyinfo;
191 while (*ptr > 32 && *ptr != ':')
192 ptr++;
193 if (*ptr != 0)
194 *ptr = 0;
195
196 strcpy(foratoi, offset);
197 Xindex += atoi(foratoi);
198 keyboard_keys[layoutindex - 1][rowindex - 1][keyindex - 1].end_x = Xindex - 1;
199
200 ptr++;
201 if (*ptr == 0) {
202 // This is an empty area
203 keyboard_keys[layoutindex - 1][rowindex - 1][keyindex - 1].key = 0;
204 } else if (strlen(ptr) == 1) {
205 // This is the character that this key uses
206 keyboard_keys[layoutindex - 1][rowindex - 1][keyindex - 1].key = *ptr;
207 } else if (*ptr == 'c') {
208 // This is an ASCII character code
209 keyitem = ptr + 2;
210 strcpy(foratoi, keyitem);
211 keyboard_keys[layoutindex - 1][rowindex - 1][keyindex - 1].key = atoi(foratoi);
212 } else if (*ptr == 'l') {
213 // This is a different layout
214 keyitem = ptr + 6;
215 keyboard_keys[layoutindex - 1][rowindex - 1][keyindex - 1].key = KEYBOARD_LAYOUT;
216 strcpy(foratoi, keyitem);
217 keyboard_keys[layoutindex - 1][rowindex - 1][keyindex - 1].layout = atoi(foratoi);
218 } else if (*ptr == 'a') {
219 // This is an action
220 keyboard_keys[layoutindex - 1][rowindex - 1][keyindex - 1].key = KEYBOARD_ACTION;
221 } else
Dees_Troy2673cec2013-04-02 20:22:16 +0000222 LOGERR("Invalid key info on layout%i, row%i, key%02i.\n", layoutindex, rowindex, keyindex);
Dees_Troy51a0e822012-09-05 15:24:24 -0400223 }
224
225 // PROCESS LONG PRESS INFO IF EXISTS
226 sprintf(longpress, "long%02i", keyindex);
227 attr = keyrow->first_attribute(longpress);
228 if (attr) {
229 stratt = attr->value();
230 if (strlen(stratt.c_str()) >= 255) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000231 LOGERR("Key info on layout%i, row%i, key%dd is too long.\n", layoutindex, rowindex, keyindex);
Dees_Troy51a0e822012-09-05 15:24:24 -0400232 return;
233 }
234
235 strcpy(keyinfo, stratt.c_str());
236
237 if (strlen(keyinfo) == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000238 LOGERR("No long press info on layout%i, row%i, long%dd.\n", layoutindex, rowindex, keyindex);
Dees_Troy51a0e822012-09-05 15:24:24 -0400239 return;
240 }
241
242 if (strlen(keyinfo) == 1) {
243 // This is a single key, simple definition
244 keyboard_keys[layoutindex - 1][rowindex - 1][keyindex - 1].longpresskey = keyinfo[0];
245 } else {
246 // This key has extra data
247 char* ptr;
248 char* offset;
249 char* keyitem;
250 char foratoi[10];
251
252 ptr = keyinfo;
253 offset = keyinfo;
254 while (*ptr > 32 && *ptr != ':')
255 ptr++;
256 if (*ptr != 0)
257 *ptr = 0;
258
259 strcpy(foratoi, offset);
260 Xindex += atoi(foratoi);
261
262 ptr++;
263 if (*ptr == 0) {
264 // This is an empty area
265 keyboard_keys[layoutindex - 1][rowindex - 1][keyindex - 1].longpresskey = 0;
266 } else if (strlen(ptr) == 1) {
267 // This is the character that this key uses
268 keyboard_keys[layoutindex - 1][rowindex - 1][keyindex - 1].longpresskey = *ptr;
269 } else if (*ptr == 'c') {
270 // This is an ASCII character code
271 keyitem = ptr + 2;
272 strcpy(foratoi, keyitem);
273 keyboard_keys[layoutindex - 1][rowindex - 1][keyindex - 1].longpresskey = atoi(foratoi);
274 } else if (*ptr == 'l') {
275 // This is a different layout
276 keyitem = ptr + 6;
277 keyboard_keys[layoutindex - 1][rowindex - 1][keyindex - 1].longpresskey = KEYBOARD_LAYOUT;
278 strcpy(foratoi, keyitem);
279 keyboard_keys[layoutindex - 1][rowindex - 1][keyindex - 1].layout = atoi(foratoi);
280 } else if (*ptr == 'a') {
281 // This is an action
282 keyboard_keys[layoutindex - 1][rowindex - 1][keyindex - 1].longpresskey = KEYBOARD_ACTION;
283 } else
Dees_Troy2673cec2013-04-02 20:22:16 +0000284 LOGERR("Invalid long press key info on layout%i, row%i, long%02i.\n", layoutindex, rowindex, keyindex);
Dees_Troy51a0e822012-09-05 15:24:24 -0400285 }
286 }
287 keyindex++;
288 sprintf(key, "key%02i", keyindex);
289 attr = keyrow->first_attribute(key);
290 }
291 rowindex++;
292 row[3] = (char)(rowindex + 48);
293 keyrow = keylayout->first_node(row);
294 }
295 layoutindex++;
296 layout[6] = (char)(layoutindex + 48);
297 keylayout = node->first_node(layout);
298 }
299
300 int x, y, w, h;
301 // Load the placement
302 LoadPlacement(node->first_node("placement"), &x, &y, &w, &h);
303 SetActionPos(x, y, KeyboardWidth, KeyboardHeight);
304 SetRenderPos(x, y, w, h);
305 return;
306}
307
308GUIKeyboard::~GUIKeyboard()
309{
310 int layoutindex;
311
312 for (layoutindex=0; layoutindex<MAX_KEYBOARD_LAYOUTS; layoutindex++)
313 if (keyboardImg[layoutindex]) delete keyboardImg[layoutindex];
314}
315
316int GUIKeyboard::Render(void)
317{
318 if (!isConditionTrue())
319 {
320 mRendered = false;
321 return 0;
322 }
323
324 int ret = 0;
325
326 if (keyboardImg[currentLayout - 1] && keyboardImg[currentLayout - 1]->GetResource())
327 gr_blit(keyboardImg[currentLayout - 1]->GetResource(), 0, 0, KeyboardWidth, KeyboardHeight, mRenderX, mRenderY);
328
Dees_Troy30b962e2012-10-19 20:48:59 -0400329 if (hasHighlight && highlightRenderCount != 0) {
330 int boxheight, boxwidth, x;
331 if (rowY == 0)
332 boxheight = row_heights[currentLayout - 1][rowY];
333 else
334 boxheight = row_heights[currentLayout - 1][rowY] - row_heights[currentLayout - 1][rowY - 1];
335 if (colX == 0) {
336 x = mRenderX;
337 boxwidth = keyboard_keys[currentLayout - 1][rowY][colX].end_x;
338 } else {
339 x = mRenderX + keyboard_keys[currentLayout - 1][rowY][colX - 1].end_x;
340 boxwidth = keyboard_keys[currentLayout - 1][rowY][colX].end_x - keyboard_keys[currentLayout - 1][rowY][colX - 1].end_x;
341 }
342 gr_color(mHighlightColor.red, mHighlightColor.green, mHighlightColor.blue, mHighlightColor.alpha);
343 gr_fill(x, mRenderY + row_heights[currentLayout - 1][rowY - 1], boxwidth, boxheight);
344 if (highlightRenderCount > 0)
345 highlightRenderCount--;
346 } else
347 mRendered = true;
348
Dees_Troy51a0e822012-09-05 15:24:24 -0400349 return ret;
350}
351
352int GUIKeyboard::Update(void)
353{
354 if (!isConditionTrue()) return (mRendered ? 2 : 0);
355 if (!mRendered) return 2;
356
357 return 0;
358}
359
360int GUIKeyboard::SetRenderPos(int x, int y, int w, int h)
361{
362 mRenderX = x;
363 mRenderY = y;
364 if (w || h)
365 {
366 mRenderW = KeyboardWidth;
367 mRenderH = KeyboardHeight;
368 }
369
370 if (mAction) mAction->SetActionPos(mRenderX, mRenderY, mRenderW, mRenderH);
371 SetActionPos(mRenderX, mRenderY, mRenderW, mRenderH);
372 return 0;
373}
374
375int GUIKeyboard::GetSelection(int x, int y)
376{
Dees_Troy30b962e2012-10-19 20:48:59 -0400377 if (x < mRenderX || x - mRenderX > (int)KeyboardWidth || y < mRenderY || y - mRenderY > (int)KeyboardHeight) return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400378 return 0;
379}
380
381int GUIKeyboard::NotifyTouch(TOUCH_STATE state, int x, int y)
382{
383 static int startSelection = -1, was_held = 0, startX = 0;
384 static unsigned char initial_key = 0;
385 unsigned int indexy, indexx, rely, relx, rowIndex = 0;
386
387 rely = y - mRenderY;
388 relx = x - mRenderX;
389
390 if (!isConditionTrue()) return -1;
391
392 switch (state)
393 {
394 case TOUCH_START:
395 if (GetSelection(x, y) == 0) {
396 startSelection = -1;
397 was_held = 0;
398 startX = x;
399 // Find the correct row
400 for (indexy=0; indexy<MAX_KEYBOARD_ROWS; indexy++) {
401 if (row_heights[currentLayout - 1][indexy] > rely) {
402 rowIndex = indexy;
Dees_Troy30b962e2012-10-19 20:48:59 -0400403 rowY = indexy;
Dees_Troy51a0e822012-09-05 15:24:24 -0400404 indexy = MAX_KEYBOARD_ROWS;
405 }
406 }
407
408 // Find the correct key (column)
409 for (indexx=0; indexx<MAX_KEYBOARD_KEYS; indexx++) {
410 if (keyboard_keys[currentLayout - 1][rowIndex][indexx].end_x > relx) {
411 // This is the key that was pressed!
412 initial_key = keyboard_keys[currentLayout - 1][rowIndex][indexx].key;
Dees_Troy30b962e2012-10-19 20:48:59 -0400413 colX = indexx;
Dees_Troy51a0e822012-09-05 15:24:24 -0400414 indexx = MAX_KEYBOARD_KEYS;
415 }
416 }
Dees_Troy30b962e2012-10-19 20:48:59 -0400417 if (initial_key != 0)
418 highlightRenderCount = -1;
419 else
420 highlightRenderCount = 0;
421 mRendered = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400422 } else {
Dees_Troy30b962e2012-10-19 20:48:59 -0400423 if (highlightRenderCount != 0)
424 mRendered = false;
425 highlightRenderCount = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400426 startSelection = 0;
427 }
428 break;
429 case TOUCH_DRAG:
430 break;
431 case TOUCH_RELEASE:
Dees_Troy0cb64e52012-10-15 15:56:10 -0400432 if (x < startX - (KeyboardWidth * 0.5)) {
Dees_Troy30b962e2012-10-19 20:48:59 -0400433 if (highlightRenderCount != 0) {
434 highlightRenderCount = 0;
435 mRendered = false;
436 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400437 PageManager::NotifyKeyboard(KEYBOARD_SWIPE_LEFT);
438 return 0;
Dees_Troy0cb64e52012-10-15 15:56:10 -0400439 } else if (x > startX + (KeyboardWidth * 0.5)) {
Dees_Troy30b962e2012-10-19 20:48:59 -0400440 if (highlightRenderCount != 0) {
441 highlightRenderCount = 0;
442 mRendered = false;
443 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400444 PageManager::NotifyKeyboard(KEYBOARD_SWIPE_RIGHT);
445 return 0;
446 }
447
448 case TOUCH_HOLD:
449 case TOUCH_REPEAT:
450
Dees_Troy30b962e2012-10-19 20:48:59 -0400451 if (startSelection == 0 || GetSelection(x, y) == -1) {
452 if (highlightRenderCount != 0) {
453 highlightRenderCount = 0;
454 mRendered = false;
455 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400456 return 0;
Dees_Troy30b962e2012-10-19 20:48:59 -0400457 } else if (highlightRenderCount != 0) {
458 if (state == TOUCH_RELEASE)
459 highlightRenderCount = 2;
460 else
461 highlightRenderCount = -1;
462 mRendered = false;
463 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400464
465 // Find the correct row
466 for (indexy=0; indexy<MAX_KEYBOARD_ROWS; indexy++) {
467 if (row_heights[currentLayout - 1][indexy] > rely) {
468 rowIndex = indexy;
469 indexy = MAX_KEYBOARD_ROWS;
470 }
471 }
472
473 // Find the correct key (column)
474 for (indexx=0; indexx<MAX_KEYBOARD_KEYS; indexx++) {
475 if (keyboard_keys[currentLayout - 1][rowIndex][indexx].end_x > relx) {
476 // This is the key that was pressed!
477 if (keyboard_keys[currentLayout - 1][rowIndex][indexx].key != initial_key) {
478 // We dragged off of the starting key
479 startSelection = 0;
480 break;
481 } else if (state == TOUCH_RELEASE && was_held == 0) {
482 if ((int)keyboard_keys[currentLayout - 1][rowIndex][indexx].key < KEYBOARD_SPECIAL_KEYS && (int)keyboard_keys[currentLayout - 1][rowIndex][indexx].key > 0) {
483 // Regular key
484 PageManager::NotifyKeyboard(keyboard_keys[currentLayout - 1][rowIndex][indexx].key);
485 } else if ((int)keyboard_keys[currentLayout - 1][rowIndex][indexx].key == KEYBOARD_LAYOUT) {
486 // Switch layouts
487 currentLayout = keyboard_keys[currentLayout - 1][rowIndex][indexx].layout;
488 mRendered = false;
489 } else if ((int)keyboard_keys[currentLayout - 1][rowIndex][indexx].key == KEYBOARD_ACTION) {
490 // Action
Dees_Troy30b962e2012-10-19 20:48:59 -0400491 highlightRenderCount = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400492 if (mAction) {
493 // Keyboard has its own action defined
494 return (mAction ? mAction->NotifyTouch(state, x, y) : 1);
495 } else {
496 // Send action notification
497 PageManager::NotifyKeyboard(keyboard_keys[currentLayout - 1][rowIndex][indexx].key);
498 }
499 }
500 } else if (state == TOUCH_HOLD) {
501 was_held = 1;
502 if ((int)keyboard_keys[currentLayout - 1][rowIndex][indexx].key == KEYBOARD_BACKSPACE) {
503 // Repeat backspace
504 PageManager::NotifyKeyboard(keyboard_keys[currentLayout - 1][rowIndex][indexx].key);
505 } else if ((int)keyboard_keys[currentLayout - 1][rowIndex][indexx].longpresskey < KEYBOARD_SPECIAL_KEYS && (int)keyboard_keys[currentLayout - 1][rowIndex][indexx].longpresskey > 0) {
506 // Long Press Key
507 PageManager::NotifyKeyboard(keyboard_keys[currentLayout - 1][rowIndex][indexx].longpresskey);
508 }
509 } else if (state == TOUCH_REPEAT) {
510 was_held = 1;
511 if ((int)keyboard_keys[currentLayout - 1][rowIndex][indexx].key == KEYBOARD_BACKSPACE) {
512 // Repeat backspace
513 PageManager::NotifyKeyboard(keyboard_keys[currentLayout - 1][rowIndex][indexx].key);
514 }
515 }
516 indexx = MAX_KEYBOARD_KEYS;
517 }
518 }
519 break;
520 }
521
522 return 0;
523}