Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1 | // keyboard.cpp - GUIKeyboard object |
| 2 | |
| 3 | #include <stdarg.h> |
| 4 | #include <stdio.h> |
| 5 | #include <stdlib.h> |
| 6 | #include <string.h> |
| 7 | #include <fcntl.h> |
| 8 | #include <sys/reboot.h> |
| 9 | #include <sys/stat.h> |
| 10 | #include <sys/time.h> |
| 11 | #include <sys/mman.h> |
| 12 | #include <sys/types.h> |
| 13 | #include <sys/ioctl.h> |
| 14 | #include <time.h> |
| 15 | #include <unistd.h> |
| 16 | #include <stdlib.h> |
| 17 | |
| 18 | #include <string> |
| 19 | |
| 20 | extern "C" { |
| 21 | #include "../common.h" |
| 22 | #include "../minuitwrp/minui.h" |
| 23 | #include "../recovery_ui.h" |
| 24 | } |
| 25 | |
| 26 | #include "rapidxml.hpp" |
| 27 | #include "objects.hpp" |
| 28 | |
| 29 | GUIKeyboard::GUIKeyboard(xml_node<>* node) |
| 30 | : Conditional(node) |
| 31 | { |
| 32 | int layoutindex, rowindex, keyindex, Xindex, Yindex, keyHeight = 0, keyWidth = 0; |
| 33 | char resource[9], layout[7], row[4], key[5], longpress[6]; |
| 34 | xml_attribute<>* attr; |
| 35 | xml_node<>* child; |
| 36 | xml_node<>* keylayout; |
| 37 | xml_node<>* keyrow; |
| 38 | |
| 39 | for (layoutindex=0; layoutindex<MAX_KEYBOARD_LAYOUTS; layoutindex++) |
| 40 | keyboardImg[layoutindex] = NULL; |
| 41 | |
| 42 | mRendered = false; |
| 43 | currentLayout = 1; |
| 44 | mAction = NULL; |
| 45 | KeyboardHeight = KeyboardWidth = cursorLocation = 0; |
| 46 | |
| 47 | if (!node) return; |
| 48 | |
| 49 | // Load the action |
| 50 | child = node->first_node("action"); |
| 51 | if (child) |
| 52 | { |
| 53 | mAction = new GUIAction(node); |
| 54 | } |
| 55 | |
| 56 | // Load the images for the different layouts |
| 57 | child = node->first_node("layout"); |
| 58 | if (child) |
| 59 | { |
| 60 | layoutindex = 1; |
| 61 | strcpy(resource, "resource1"); |
| 62 | attr = child->first_attribute(resource); |
| 63 | while (attr && layoutindex < (MAX_KEYBOARD_LAYOUTS + 1)) { |
| 64 | keyboardImg[layoutindex - 1] = PageManager::FindResource(attr->value()); |
| 65 | |
| 66 | layoutindex++; |
| 67 | resource[8] = (char)(layoutindex + 48); |
| 68 | attr = child->first_attribute(resource); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | // Check the first image to get height and width |
| 73 | if (keyboardImg[0] && keyboardImg[0]->GetResource()) |
| 74 | { |
| 75 | KeyboardWidth = gr_get_width(keyboardImg[0]->GetResource()); |
| 76 | KeyboardHeight = gr_get_height(keyboardImg[0]->GetResource()); |
| 77 | } |
| 78 | |
| 79 | // Load all of the layout maps |
| 80 | layoutindex = 1; |
| 81 | strcpy(layout, "layout1"); |
| 82 | keylayout = node->first_node(layout); |
| 83 | while (keylayout) |
| 84 | { |
| 85 | if (layoutindex > MAX_KEYBOARD_LAYOUTS) { |
| 86 | LOGE("Too many layouts defined in keyboard.\n"); |
| 87 | return; |
| 88 | } |
| 89 | |
| 90 | child = keylayout->first_node("keysize"); |
| 91 | if (child) { |
| 92 | attr = child->first_attribute("height"); |
| 93 | if (attr) |
| 94 | keyHeight = atoi(attr->value()); |
| 95 | else |
| 96 | keyHeight = 0; |
| 97 | attr = child->first_attribute("width"); |
| 98 | if (attr) |
| 99 | keyWidth = atoi(attr->value()); |
| 100 | else |
| 101 | keyWidth = 0; |
| 102 | } |
| 103 | |
| 104 | rowindex = 1; |
| 105 | Yindex = 0; |
| 106 | strcpy(row, "row1"); |
| 107 | keyrow = keylayout->first_node(row); |
| 108 | while (keyrow) |
| 109 | { |
| 110 | if (rowindex > MAX_KEYBOARD_ROWS) { |
| 111 | LOGE("Too many rows defined in keyboard.\n"); |
| 112 | return; |
| 113 | } |
| 114 | |
| 115 | Yindex += keyHeight; |
| 116 | row_heights[layoutindex - 1][rowindex - 1] = Yindex; |
| 117 | |
| 118 | keyindex = 1; |
| 119 | Xindex = 0; |
| 120 | strcpy(key, "key01"); |
| 121 | attr = keyrow->first_attribute(key); |
| 122 | |
| 123 | while (attr) { |
| 124 | string stratt; |
| 125 | char keyinfo[255]; |
| 126 | |
| 127 | if (keyindex > MAX_KEYBOARD_KEYS) { |
| 128 | LOGE("Too many keys defined in a keyboard row.\n"); |
| 129 | return; |
| 130 | } |
| 131 | |
| 132 | stratt = attr->value(); |
| 133 | if (strlen(stratt.c_str()) >= 255) { |
| 134 | LOGE("Key info on layout%i, row%i, key%dd is too long.\n", layoutindex, rowindex, keyindex); |
| 135 | return; |
| 136 | } |
| 137 | |
| 138 | strcpy(keyinfo, stratt.c_str()); |
| 139 | |
| 140 | if (strlen(keyinfo) == 0) { |
| 141 | LOGE("No key info on layout%i, row%i, key%dd.\n", layoutindex, rowindex, keyindex); |
| 142 | return; |
| 143 | } |
| 144 | |
| 145 | if (strlen(keyinfo) == 1) { |
| 146 | // This is a single key, simple definition |
| 147 | keyboard_keys[layoutindex - 1][rowindex - 1][keyindex - 1].key = keyinfo[0]; |
| 148 | Xindex += keyWidth; |
| 149 | keyboard_keys[layoutindex - 1][rowindex - 1][keyindex - 1].end_x = Xindex - 1; |
| 150 | } else { |
| 151 | // This key has extra data |
| 152 | char* ptr; |
| 153 | char* offset; |
| 154 | char* keyitem; |
| 155 | char foratoi[10]; |
| 156 | |
| 157 | ptr = keyinfo; |
| 158 | offset = keyinfo; |
| 159 | while (*ptr > 32 && *ptr != ':') |
| 160 | ptr++; |
| 161 | if (*ptr != 0) |
| 162 | *ptr = 0; |
| 163 | |
| 164 | strcpy(foratoi, offset); |
| 165 | Xindex += atoi(foratoi); |
| 166 | keyboard_keys[layoutindex - 1][rowindex - 1][keyindex - 1].end_x = Xindex - 1; |
| 167 | |
| 168 | ptr++; |
| 169 | if (*ptr == 0) { |
| 170 | // This is an empty area |
| 171 | keyboard_keys[layoutindex - 1][rowindex - 1][keyindex - 1].key = 0; |
| 172 | } else if (strlen(ptr) == 1) { |
| 173 | // This is the character that this key uses |
| 174 | keyboard_keys[layoutindex - 1][rowindex - 1][keyindex - 1].key = *ptr; |
| 175 | } else if (*ptr == 'c') { |
| 176 | // This is an ASCII character code |
| 177 | keyitem = ptr + 2; |
| 178 | strcpy(foratoi, keyitem); |
| 179 | keyboard_keys[layoutindex - 1][rowindex - 1][keyindex - 1].key = atoi(foratoi); |
| 180 | } else if (*ptr == 'l') { |
| 181 | // This is a different layout |
| 182 | keyitem = ptr + 6; |
| 183 | keyboard_keys[layoutindex - 1][rowindex - 1][keyindex - 1].key = KEYBOARD_LAYOUT; |
| 184 | strcpy(foratoi, keyitem); |
| 185 | keyboard_keys[layoutindex - 1][rowindex - 1][keyindex - 1].layout = atoi(foratoi); |
| 186 | } else if (*ptr == 'a') { |
| 187 | // This is an action |
| 188 | keyboard_keys[layoutindex - 1][rowindex - 1][keyindex - 1].key = KEYBOARD_ACTION; |
| 189 | } else |
| 190 | LOGE("Invalid key info on layout%i, row%i, key%02i.\n", layoutindex, rowindex, keyindex); |
| 191 | } |
| 192 | |
| 193 | // PROCESS LONG PRESS INFO IF EXISTS |
| 194 | sprintf(longpress, "long%02i", keyindex); |
| 195 | attr = keyrow->first_attribute(longpress); |
| 196 | if (attr) { |
| 197 | stratt = attr->value(); |
| 198 | if (strlen(stratt.c_str()) >= 255) { |
| 199 | LOGE("Key info on layout%i, row%i, key%dd is too long.\n", layoutindex, rowindex, keyindex); |
| 200 | return; |
| 201 | } |
| 202 | |
| 203 | strcpy(keyinfo, stratt.c_str()); |
| 204 | |
| 205 | if (strlen(keyinfo) == 0) { |
| 206 | LOGE("No long press info on layout%i, row%i, long%dd.\n", layoutindex, rowindex, keyindex); |
| 207 | return; |
| 208 | } |
| 209 | |
| 210 | if (strlen(keyinfo) == 1) { |
| 211 | // This is a single key, simple definition |
| 212 | keyboard_keys[layoutindex - 1][rowindex - 1][keyindex - 1].longpresskey = keyinfo[0]; |
| 213 | } else { |
| 214 | // This key has extra data |
| 215 | char* ptr; |
| 216 | char* offset; |
| 217 | char* keyitem; |
| 218 | char foratoi[10]; |
| 219 | |
| 220 | ptr = keyinfo; |
| 221 | offset = keyinfo; |
| 222 | while (*ptr > 32 && *ptr != ':') |
| 223 | ptr++; |
| 224 | if (*ptr != 0) |
| 225 | *ptr = 0; |
| 226 | |
| 227 | strcpy(foratoi, offset); |
| 228 | Xindex += atoi(foratoi); |
| 229 | |
| 230 | ptr++; |
| 231 | if (*ptr == 0) { |
| 232 | // This is an empty area |
| 233 | keyboard_keys[layoutindex - 1][rowindex - 1][keyindex - 1].longpresskey = 0; |
| 234 | } else if (strlen(ptr) == 1) { |
| 235 | // This is the character that this key uses |
| 236 | keyboard_keys[layoutindex - 1][rowindex - 1][keyindex - 1].longpresskey = *ptr; |
| 237 | } else if (*ptr == 'c') { |
| 238 | // This is an ASCII character code |
| 239 | keyitem = ptr + 2; |
| 240 | strcpy(foratoi, keyitem); |
| 241 | keyboard_keys[layoutindex - 1][rowindex - 1][keyindex - 1].longpresskey = atoi(foratoi); |
| 242 | } else if (*ptr == 'l') { |
| 243 | // This is a different layout |
| 244 | keyitem = ptr + 6; |
| 245 | keyboard_keys[layoutindex - 1][rowindex - 1][keyindex - 1].longpresskey = KEYBOARD_LAYOUT; |
| 246 | strcpy(foratoi, keyitem); |
| 247 | keyboard_keys[layoutindex - 1][rowindex - 1][keyindex - 1].layout = atoi(foratoi); |
| 248 | } else if (*ptr == 'a') { |
| 249 | // This is an action |
| 250 | keyboard_keys[layoutindex - 1][rowindex - 1][keyindex - 1].longpresskey = KEYBOARD_ACTION; |
| 251 | } else |
| 252 | LOGE("Invalid long press key info on layout%i, row%i, long%02i.\n", layoutindex, rowindex, keyindex); |
| 253 | } |
| 254 | } |
| 255 | keyindex++; |
| 256 | sprintf(key, "key%02i", keyindex); |
| 257 | attr = keyrow->first_attribute(key); |
| 258 | } |
| 259 | rowindex++; |
| 260 | row[3] = (char)(rowindex + 48); |
| 261 | keyrow = keylayout->first_node(row); |
| 262 | } |
| 263 | layoutindex++; |
| 264 | layout[6] = (char)(layoutindex + 48); |
| 265 | keylayout = node->first_node(layout); |
| 266 | } |
| 267 | |
| 268 | int x, y, w, h; |
| 269 | // Load the placement |
| 270 | LoadPlacement(node->first_node("placement"), &x, &y, &w, &h); |
| 271 | SetActionPos(x, y, KeyboardWidth, KeyboardHeight); |
| 272 | SetRenderPos(x, y, w, h); |
| 273 | return; |
| 274 | } |
| 275 | |
| 276 | GUIKeyboard::~GUIKeyboard() |
| 277 | { |
| 278 | int layoutindex; |
| 279 | |
| 280 | for (layoutindex=0; layoutindex<MAX_KEYBOARD_LAYOUTS; layoutindex++) |
| 281 | if (keyboardImg[layoutindex]) delete keyboardImg[layoutindex]; |
| 282 | } |
| 283 | |
| 284 | int GUIKeyboard::Render(void) |
| 285 | { |
| 286 | if (!isConditionTrue()) |
| 287 | { |
| 288 | mRendered = false; |
| 289 | return 0; |
| 290 | } |
| 291 | |
| 292 | int ret = 0; |
| 293 | |
| 294 | if (keyboardImg[currentLayout - 1] && keyboardImg[currentLayout - 1]->GetResource()) |
| 295 | gr_blit(keyboardImg[currentLayout - 1]->GetResource(), 0, 0, KeyboardWidth, KeyboardHeight, mRenderX, mRenderY); |
| 296 | |
| 297 | mRendered = true; |
| 298 | return ret; |
| 299 | } |
| 300 | |
| 301 | int GUIKeyboard::Update(void) |
| 302 | { |
| 303 | if (!isConditionTrue()) return (mRendered ? 2 : 0); |
| 304 | if (!mRendered) return 2; |
| 305 | |
| 306 | return 0; |
| 307 | } |
| 308 | |
| 309 | int GUIKeyboard::SetRenderPos(int x, int y, int w, int h) |
| 310 | { |
| 311 | mRenderX = x; |
| 312 | mRenderY = y; |
| 313 | if (w || h) |
| 314 | { |
| 315 | mRenderW = KeyboardWidth; |
| 316 | mRenderH = KeyboardHeight; |
| 317 | } |
| 318 | |
| 319 | if (mAction) mAction->SetActionPos(mRenderX, mRenderY, mRenderW, mRenderH); |
| 320 | SetActionPos(mRenderX, mRenderY, mRenderW, mRenderH); |
| 321 | return 0; |
| 322 | } |
| 323 | |
| 324 | int GUIKeyboard::GetSelection(int x, int y) |
| 325 | { |
| 326 | if (x < mRenderX || x - mRenderX > mRenderW || y < mRenderY || y - mRenderY > mRenderH) return -1; |
| 327 | return 0; |
| 328 | } |
| 329 | |
| 330 | int GUIKeyboard::NotifyTouch(TOUCH_STATE state, int x, int y) |
| 331 | { |
| 332 | static int startSelection = -1, was_held = 0, startX = 0; |
| 333 | static unsigned char initial_key = 0; |
| 334 | unsigned int indexy, indexx, rely, relx, rowIndex = 0; |
| 335 | |
| 336 | rely = y - mRenderY; |
| 337 | relx = x - mRenderX; |
| 338 | |
| 339 | if (!isConditionTrue()) return -1; |
| 340 | |
| 341 | switch (state) |
| 342 | { |
| 343 | case TOUCH_START: |
| 344 | if (GetSelection(x, y) == 0) { |
| 345 | startSelection = -1; |
| 346 | was_held = 0; |
| 347 | startX = x; |
| 348 | // Find the correct row |
| 349 | for (indexy=0; indexy<MAX_KEYBOARD_ROWS; indexy++) { |
| 350 | if (row_heights[currentLayout - 1][indexy] > rely) { |
| 351 | rowIndex = indexy; |
| 352 | indexy = MAX_KEYBOARD_ROWS; |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | // Find the correct key (column) |
| 357 | for (indexx=0; indexx<MAX_KEYBOARD_KEYS; indexx++) { |
| 358 | if (keyboard_keys[currentLayout - 1][rowIndex][indexx].end_x > relx) { |
| 359 | // This is the key that was pressed! |
| 360 | initial_key = keyboard_keys[currentLayout - 1][rowIndex][indexx].key; |
| 361 | indexx = MAX_KEYBOARD_KEYS; |
| 362 | } |
| 363 | } |
| 364 | } else { |
| 365 | startSelection = 0; |
| 366 | } |
| 367 | break; |
| 368 | case TOUCH_DRAG: |
| 369 | break; |
| 370 | case TOUCH_RELEASE: |
| 371 | if (x < startX - (mRenderW * 0.5)) { |
| 372 | PageManager::NotifyKeyboard(KEYBOARD_SWIPE_LEFT); |
| 373 | return 0; |
| 374 | } else if (x > startX + (mRenderW * 0.5)) { |
| 375 | PageManager::NotifyKeyboard(KEYBOARD_SWIPE_RIGHT); |
| 376 | return 0; |
| 377 | } |
| 378 | |
| 379 | case TOUCH_HOLD: |
| 380 | case TOUCH_REPEAT: |
| 381 | |
| 382 | if (startSelection == 0 || GetSelection(x, y) == -1) |
| 383 | return 0; |
| 384 | |
| 385 | // Find the correct row |
| 386 | for (indexy=0; indexy<MAX_KEYBOARD_ROWS; indexy++) { |
| 387 | if (row_heights[currentLayout - 1][indexy] > rely) { |
| 388 | rowIndex = indexy; |
| 389 | indexy = MAX_KEYBOARD_ROWS; |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | // Find the correct key (column) |
| 394 | for (indexx=0; indexx<MAX_KEYBOARD_KEYS; indexx++) { |
| 395 | if (keyboard_keys[currentLayout - 1][rowIndex][indexx].end_x > relx) { |
| 396 | // This is the key that was pressed! |
| 397 | if (keyboard_keys[currentLayout - 1][rowIndex][indexx].key != initial_key) { |
| 398 | // We dragged off of the starting key |
| 399 | startSelection = 0; |
| 400 | break; |
| 401 | } else if (state == TOUCH_RELEASE && was_held == 0) { |
| 402 | if ((int)keyboard_keys[currentLayout - 1][rowIndex][indexx].key < KEYBOARD_SPECIAL_KEYS && (int)keyboard_keys[currentLayout - 1][rowIndex][indexx].key > 0) { |
| 403 | // Regular key |
| 404 | PageManager::NotifyKeyboard(keyboard_keys[currentLayout - 1][rowIndex][indexx].key); |
| 405 | } else if ((int)keyboard_keys[currentLayout - 1][rowIndex][indexx].key == KEYBOARD_LAYOUT) { |
| 406 | // Switch layouts |
| 407 | currentLayout = keyboard_keys[currentLayout - 1][rowIndex][indexx].layout; |
| 408 | mRendered = false; |
| 409 | } else if ((int)keyboard_keys[currentLayout - 1][rowIndex][indexx].key == KEYBOARD_ACTION) { |
| 410 | // Action |
| 411 | if (mAction) { |
| 412 | // Keyboard has its own action defined |
| 413 | return (mAction ? mAction->NotifyTouch(state, x, y) : 1); |
| 414 | } else { |
| 415 | // Send action notification |
| 416 | PageManager::NotifyKeyboard(keyboard_keys[currentLayout - 1][rowIndex][indexx].key); |
| 417 | } |
| 418 | } |
| 419 | } else if (state == TOUCH_HOLD) { |
| 420 | was_held = 1; |
| 421 | if ((int)keyboard_keys[currentLayout - 1][rowIndex][indexx].key == KEYBOARD_BACKSPACE) { |
| 422 | // Repeat backspace |
| 423 | PageManager::NotifyKeyboard(keyboard_keys[currentLayout - 1][rowIndex][indexx].key); |
| 424 | } else if ((int)keyboard_keys[currentLayout - 1][rowIndex][indexx].longpresskey < KEYBOARD_SPECIAL_KEYS && (int)keyboard_keys[currentLayout - 1][rowIndex][indexx].longpresskey > 0) { |
| 425 | // Long Press Key |
| 426 | PageManager::NotifyKeyboard(keyboard_keys[currentLayout - 1][rowIndex][indexx].longpresskey); |
| 427 | } |
| 428 | } else if (state == TOUCH_REPEAT) { |
| 429 | was_held = 1; |
| 430 | if ((int)keyboard_keys[currentLayout - 1][rowIndex][indexx].key == KEYBOARD_BACKSPACE) { |
| 431 | // Repeat backspace |
| 432 | PageManager::NotifyKeyboard(keyboard_keys[currentLayout - 1][rowIndex][indexx].key); |
| 433 | } |
| 434 | } |
| 435 | indexx = MAX_KEYBOARD_KEYS; |
| 436 | } |
| 437 | } |
| 438 | break; |
| 439 | } |
| 440 | |
| 441 | return 0; |
| 442 | } |