Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1 | // hardwarekeyboard.cpp - HardwareKeyboard object |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 2 | |
| 3 | #include <stdarg.h> |
| 4 | #include <stdio.h> |
| 5 | #include <stdlib.h> |
| 6 | #include <string.h> |
| 7 | #include <fcntl.h> |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 8 | #include <sys/stat.h> |
| 9 | #include <sys/time.h> |
| 10 | #include <sys/mman.h> |
| 11 | #include <sys/types.h> |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 12 | #include <time.h> |
| 13 | #include <unistd.h> |
| 14 | #include <stdlib.h> |
| 15 | |
| 16 | #include <string> |
| 17 | |
| 18 | extern "C" { |
Ethan Yonker | e13fa63 | 2015-01-27 11:30:03 -0600 | [diff] [blame] | 19 | #include "../common.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 20 | } |
| 21 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 22 | #include "objects.hpp" |
that | 9fa5153 | 2015-01-29 02:04:47 +0100 | [diff] [blame] | 23 | #include <linux/input.h> |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 24 | |
that | f37aec2 | 2015-02-01 13:38:35 +0100 | [diff] [blame] | 25 | HardwareKeyboard::HardwareKeyboard() |
| 26 | : mLastKeyChar(0) |
| 27 | { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 28 | } |
| 29 | |
that | f37aec2 | 2015-02-01 13:38:35 +0100 | [diff] [blame] | 30 | HardwareKeyboard::~HardwareKeyboard() |
| 31 | { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 32 | } |
| 33 | |
that | f37aec2 | 2015-02-01 13:38:35 +0100 | [diff] [blame] | 34 | // Map keys to other keys. |
| 35 | static int TranslateKeyCode(int key_code) |
| 36 | { |
| 37 | switch (key_code) { |
| 38 | case KEY_HOMEPAGE: // Home key on Asus Transformer hardware keyboard |
| 39 | return KEY_HOME; |
| 40 | case KEY_SLEEP: // Lock key on Asus Transformer hardware keyboard |
| 41 | return KEY_POWER; |
| 42 | } |
| 43 | return key_code; |
| 44 | } |
Ethan Yonker | e13fa63 | 2015-01-27 11:30:03 -0600 | [diff] [blame] | 45 | |
that | f37aec2 | 2015-02-01 13:38:35 +0100 | [diff] [blame] | 46 | int HardwareKeyboard::KeyDown(int key_code) |
| 47 | { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 48 | #ifdef _EVENT_LOGGING |
Ethan Yonker | e13fa63 | 2015-01-27 11:30:03 -0600 | [diff] [blame] | 49 | LOGE("HardwareKeyboard::KeyDown %i\n", key_code); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 50 | #endif |
that | f37aec2 | 2015-02-01 13:38:35 +0100 | [diff] [blame] | 51 | key_code = TranslateKeyCode(key_code); |
| 52 | |
| 53 | // determine if any Shift key is held down |
| 54 | bool shiftkey = false; |
| 55 | std::set<int>::iterator it = mPressedKeys.find(KEY_LEFTSHIFT); |
| 56 | if (it == mPressedKeys.end()) |
| 57 | it = mPressedKeys.find(KEY_RIGHTSHIFT); |
| 58 | if (it != mPressedKeys.end()) |
| 59 | shiftkey = true; |
| 60 | |
| 61 | mPressedKeys.insert(key_code); |
| 62 | |
| 63 | int keyboard = -1; |
| 64 | |
Ethan Yonker | e13fa63 | 2015-01-27 11:30:03 -0600 | [diff] [blame] | 65 | switch (key_code) { |
Ethan Yonker | e13fa63 | 2015-01-27 11:30:03 -0600 | [diff] [blame] | 66 | case KEY_A: |
| 67 | if (shiftkey) |
| 68 | keyboard = 'A'; |
| 69 | else |
| 70 | keyboard = 'a'; |
| 71 | break; |
| 72 | case KEY_B: |
| 73 | if (shiftkey) |
| 74 | keyboard = 'B'; |
| 75 | else |
| 76 | keyboard = 'b'; |
| 77 | break; |
| 78 | case KEY_C: |
| 79 | if (shiftkey) |
| 80 | keyboard = 'C'; |
| 81 | else |
| 82 | keyboard = 'c'; |
| 83 | break; |
| 84 | case KEY_D: |
| 85 | if (shiftkey) |
| 86 | keyboard = 'D'; |
| 87 | else |
| 88 | keyboard = 'd'; |
| 89 | break; |
| 90 | case KEY_E: |
| 91 | if (shiftkey) |
| 92 | keyboard = 'E'; |
| 93 | else |
| 94 | keyboard = 'e'; |
| 95 | break; |
| 96 | case KEY_F: |
| 97 | if (shiftkey) |
| 98 | keyboard = 'F'; |
| 99 | else |
| 100 | keyboard = 'f'; |
| 101 | break; |
| 102 | case KEY_G: |
| 103 | if (shiftkey) |
| 104 | keyboard = 'G'; |
| 105 | else |
| 106 | keyboard = 'g'; |
| 107 | break; |
| 108 | case KEY_H: |
| 109 | if (shiftkey) |
| 110 | keyboard = 'H'; |
| 111 | else |
| 112 | keyboard = 'h'; |
| 113 | break; |
| 114 | case KEY_I: |
| 115 | if (shiftkey) |
| 116 | keyboard = 'I'; |
| 117 | else |
| 118 | keyboard = 'i'; |
| 119 | break; |
| 120 | case KEY_J: |
| 121 | if (shiftkey) |
| 122 | keyboard = 'J'; |
| 123 | else |
| 124 | keyboard = 'j'; |
| 125 | break; |
| 126 | case KEY_K: |
| 127 | if (shiftkey) |
| 128 | keyboard = 'K'; |
| 129 | else |
| 130 | keyboard = 'k'; |
| 131 | break; |
| 132 | case KEY_L: |
| 133 | if (shiftkey) |
| 134 | keyboard = 'L'; |
| 135 | else |
| 136 | keyboard = 'l'; |
| 137 | break; |
| 138 | case KEY_M: |
| 139 | if (shiftkey) |
| 140 | keyboard = 'M'; |
| 141 | else |
| 142 | keyboard = 'm'; |
| 143 | break; |
| 144 | case KEY_N: |
| 145 | if (shiftkey) |
| 146 | keyboard = 'N'; |
| 147 | else |
| 148 | keyboard = 'n'; |
| 149 | break; |
| 150 | case KEY_O: |
| 151 | if (shiftkey) |
| 152 | keyboard = 'O'; |
| 153 | else |
| 154 | keyboard = 'o'; |
| 155 | break; |
| 156 | case KEY_P: |
| 157 | if (shiftkey) |
| 158 | keyboard = 'P'; |
| 159 | else |
| 160 | keyboard = 'p'; |
| 161 | break; |
| 162 | case KEY_Q: |
| 163 | if (shiftkey) |
| 164 | keyboard = 'Q'; |
| 165 | else |
| 166 | keyboard = 'q'; |
| 167 | break; |
| 168 | case KEY_R: |
| 169 | if (shiftkey) |
| 170 | keyboard = 'R'; |
| 171 | else |
| 172 | keyboard = 'r'; |
| 173 | break; |
| 174 | case KEY_S: |
| 175 | if (shiftkey) |
| 176 | keyboard = 'S'; |
| 177 | else |
| 178 | keyboard = 's'; |
| 179 | break; |
| 180 | case KEY_T: |
| 181 | if (shiftkey) |
| 182 | keyboard = 'T'; |
| 183 | else |
| 184 | keyboard = 't'; |
| 185 | break; |
| 186 | case KEY_U: |
| 187 | if (shiftkey) |
| 188 | keyboard = 'U'; |
| 189 | else |
| 190 | keyboard = 'u'; |
| 191 | break; |
| 192 | case KEY_V: |
| 193 | if (shiftkey) |
| 194 | keyboard = 'V'; |
| 195 | else |
| 196 | keyboard = 'v'; |
| 197 | break; |
| 198 | case KEY_W: |
| 199 | if (shiftkey) |
| 200 | keyboard = 'W'; |
| 201 | else |
| 202 | keyboard = 'w'; |
| 203 | break; |
| 204 | case KEY_X: |
| 205 | if (shiftkey) |
| 206 | keyboard = 'X'; |
| 207 | else |
| 208 | keyboard = 'x'; |
| 209 | break; |
| 210 | case KEY_Y: |
| 211 | if (shiftkey) |
| 212 | keyboard = 'Y'; |
| 213 | else |
| 214 | keyboard = 'y'; |
| 215 | break; |
| 216 | case KEY_Z: |
| 217 | if (shiftkey) |
| 218 | keyboard = 'Z'; |
| 219 | else |
| 220 | keyboard = 'z'; |
| 221 | break; |
| 222 | case KEY_0: |
| 223 | if (shiftkey) |
| 224 | keyboard = ')'; |
| 225 | else |
| 226 | keyboard = '0'; |
| 227 | break; |
| 228 | case KEY_1: |
| 229 | if (shiftkey) |
| 230 | keyboard = '!'; |
| 231 | else |
| 232 | keyboard = '1'; |
| 233 | break; |
| 234 | case KEY_2: |
| 235 | if (shiftkey) |
| 236 | keyboard = '@'; |
| 237 | else |
| 238 | keyboard = '2'; |
| 239 | break; |
| 240 | case KEY_3: |
| 241 | if (shiftkey) |
| 242 | keyboard = '#'; |
| 243 | else |
| 244 | keyboard = '3'; |
| 245 | break; |
| 246 | case KEY_4: |
| 247 | if (shiftkey) |
| 248 | keyboard = '$'; |
| 249 | else |
| 250 | keyboard = '4'; |
| 251 | break; |
| 252 | case KEY_5: |
| 253 | if (shiftkey) |
| 254 | keyboard = '%'; |
| 255 | else |
| 256 | keyboard = '5'; |
| 257 | break; |
| 258 | case KEY_6: |
| 259 | if (shiftkey) |
| 260 | keyboard = '^'; |
| 261 | else |
| 262 | keyboard = '6'; |
| 263 | break; |
| 264 | case KEY_7: |
| 265 | if (shiftkey) |
| 266 | keyboard = '&'; |
| 267 | else |
| 268 | keyboard = '7'; |
| 269 | break; |
| 270 | case KEY_8: |
| 271 | if (shiftkey) |
| 272 | keyboard = '*'; |
| 273 | else |
| 274 | keyboard = '8'; |
| 275 | break; |
| 276 | case KEY_9: |
| 277 | if (shiftkey) |
| 278 | keyboard = '('; |
| 279 | else |
| 280 | keyboard = '9'; |
| 281 | break; |
| 282 | case KEY_SPACE: |
| 283 | keyboard = ' '; |
| 284 | break; |
| 285 | case KEY_BACKSPACE: |
| 286 | keyboard = KEYBOARD_BACKSPACE; |
| 287 | break; |
| 288 | case KEY_ENTER: |
| 289 | keyboard = KEYBOARD_ACTION; |
| 290 | break; |
| 291 | case KEY_SLASH: |
| 292 | if (shiftkey) |
| 293 | keyboard = '?'; |
| 294 | else |
| 295 | keyboard = '/'; |
| 296 | break; |
| 297 | case KEY_DOT: |
| 298 | if (shiftkey) |
| 299 | keyboard = '>'; |
| 300 | else |
| 301 | keyboard = '.'; |
| 302 | break; |
| 303 | case KEY_COMMA: |
| 304 | if (shiftkey) |
| 305 | keyboard = '<'; |
| 306 | else |
| 307 | keyboard = ','; |
| 308 | break; |
| 309 | case KEY_MINUS: |
| 310 | if (shiftkey) |
| 311 | keyboard = '_'; |
| 312 | else |
| 313 | keyboard = '-'; |
| 314 | break; |
| 315 | case KEY_GRAVE: |
| 316 | if (shiftkey) |
| 317 | keyboard = '~'; |
| 318 | else |
| 319 | keyboard = '`'; |
| 320 | break; |
| 321 | case KEY_EQUAL: |
| 322 | if (shiftkey) |
| 323 | keyboard = '+'; |
| 324 | else |
| 325 | keyboard = '='; |
| 326 | break; |
| 327 | case KEY_LEFTBRACE: |
| 328 | if (shiftkey) |
| 329 | keyboard = '{'; |
| 330 | else |
| 331 | keyboard = '['; |
| 332 | break; |
| 333 | case KEY_RIGHTBRACE: |
| 334 | if (shiftkey) |
| 335 | keyboard = '}'; |
| 336 | else |
| 337 | keyboard = ']'; |
| 338 | break; |
| 339 | case KEY_BACKSLASH: |
| 340 | if (shiftkey) |
| 341 | keyboard = '|'; |
| 342 | else |
| 343 | keyboard = '\\'; |
| 344 | break; |
| 345 | case KEY_SEMICOLON: |
| 346 | if (shiftkey) |
| 347 | keyboard = ':'; |
| 348 | else |
| 349 | keyboard = ';'; |
| 350 | break; |
| 351 | case KEY_APOSTROPHE: |
| 352 | if (shiftkey) |
| 353 | keyboard = '\"'; |
| 354 | else |
| 355 | keyboard = '\''; |
| 356 | break; |
| 357 | case KEY_UP: // Up arrow |
| 358 | keyboard = KEYBOARD_ARROW_UP; |
| 359 | break; |
| 360 | case KEY_DOWN: // Down arrow |
| 361 | keyboard = KEYBOARD_ARROW_DOWN; |
| 362 | break; |
| 363 | case KEY_LEFT: // Left arrow |
| 364 | keyboard = KEYBOARD_ARROW_LEFT; |
| 365 | break; |
| 366 | case KEY_RIGHT: // Right arrow |
| 367 | keyboard = KEYBOARD_ARROW_RIGHT; |
| 368 | break; |
that | f37aec2 | 2015-02-01 13:38:35 +0100 | [diff] [blame] | 369 | |
Ethan Yonker | e13fa63 | 2015-01-27 11:30:03 -0600 | [diff] [blame] | 370 | #ifdef _EVENT_LOGGING |
| 371 | default: |
| 372 | LOGE("Unmapped keycode: %i\n", key_code); |
| 373 | break; |
| 374 | #endif |
| 375 | } |
| 376 | if (keyboard != -1) { |
that | f37aec2 | 2015-02-01 13:38:35 +0100 | [diff] [blame] | 377 | mLastKeyChar = keyboard; |
| 378 | // NotifyKeyboard means: "report character to input widget". KEYBOARD_* codes are special, others are ASCII chars. |
Ethan Yonker | e13fa63 | 2015-01-27 11:30:03 -0600 | [diff] [blame] | 379 | if (!PageManager::NotifyKeyboard(keyboard)) |
| 380 | return 1; // Return 1 to enable key repeat |
| 381 | } else { |
that | f37aec2 | 2015-02-01 13:38:35 +0100 | [diff] [blame] | 382 | mLastKeyChar = 0; |
| 383 | PageManager::NotifyKey(key_code, true); |
Ethan Yonker | e13fa63 | 2015-01-27 11:30:03 -0600 | [diff] [blame] | 384 | } |
| 385 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 386 | } |
| 387 | |
that | f37aec2 | 2015-02-01 13:38:35 +0100 | [diff] [blame] | 388 | int HardwareKeyboard::KeyUp(int key_code) |
| 389 | { |
| 390 | #ifdef _EVENT_LOGGING |
| 391 | LOGE("HardwareKeyboard::KeyUp %i\n", key_code); |
| 392 | #endif |
| 393 | key_code = TranslateKeyCode(key_code); |
Vojtech Bocek | 0b7fe50 | 2014-03-13 17:36:52 +0100 | [diff] [blame] | 394 | std::set<int>::iterator itr = mPressedKeys.find(key_code); |
Ethan Yonker | e13fa63 | 2015-01-27 11:30:03 -0600 | [diff] [blame] | 395 | if (itr != mPressedKeys.end()) { |
Vojtech Bocek | 0b7fe50 | 2014-03-13 17:36:52 +0100 | [diff] [blame] | 396 | mPressedKeys.erase(itr); |
| 397 | PageManager::NotifyKey(key_code, false); |
| 398 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 399 | return 0; |
| 400 | } |
| 401 | |
that | f37aec2 | 2015-02-01 13:38:35 +0100 | [diff] [blame] | 402 | int HardwareKeyboard::KeyRepeat() |
| 403 | { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 404 | #ifdef _EVENT_LOGGING |
that | f37aec2 | 2015-02-01 13:38:35 +0100 | [diff] [blame] | 405 | LOGE("HardwareKeyboard::KeyRepeat: %i\n", mLastKeyChar); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 406 | #endif |
that | f37aec2 | 2015-02-01 13:38:35 +0100 | [diff] [blame] | 407 | if (mLastKeyChar) |
| 408 | PageManager::NotifyKeyboard(mLastKeyChar); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 409 | return 0; |
| 410 | } |
Vojtech Bocek | 0b7fe50 | 2014-03-13 17:36:52 +0100 | [diff] [blame] | 411 | |
that | f37aec2 | 2015-02-01 13:38:35 +0100 | [diff] [blame] | 412 | void HardwareKeyboard::ConsumeKeyRelease(int key) |
| 413 | { |
| 414 | // causes following KeyUp event to suppress notifications |
Vojtech Bocek | 0b7fe50 | 2014-03-13 17:36:52 +0100 | [diff] [blame] | 415 | mPressedKeys.erase(key); |
| 416 | } |