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