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