blob: 12c4737a1a15367b9f9e0bb8eac065edc4e980c6 [file] [log] [blame]
Matt Mowere04eee72016-12-31 00:38:57 -06001/*
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_Troy51a0e822012-09-05 15:24:24 -040019// hardwarekeyboard.cpp - HardwareKeyboard object
Dees_Troy51a0e822012-09-05 15:24:24 -040020
21#include <stdarg.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <fcntl.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040026#include <sys/stat.h>
27#include <sys/time.h>
28#include <sys/mman.h>
29#include <sys/types.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040030#include <time.h>
31#include <unistd.h>
32#include <stdlib.h>
33
34#include <string>
35
36extern "C" {
bigbiff1f9e4842020-10-31 11:33:15 -040037#include "common.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040038}
39
Matt Mower91584b22016-01-09 14:35:34 -060040#include "../twcommon.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040041#include "objects.hpp"
that9fa51532015-01-29 02:04:47 +010042#include <linux/input.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040043
thatf37aec22015-02-01 13:38:35 +010044HardwareKeyboard::HardwareKeyboard()
45 : mLastKeyChar(0)
46{
Dees_Troy51a0e822012-09-05 15:24:24 -040047}
48
thatf37aec22015-02-01 13:38:35 +010049HardwareKeyboard::~HardwareKeyboard()
50{
Dees_Troy51a0e822012-09-05 15:24:24 -040051}
52
thatf37aec22015-02-01 13:38:35 +010053// Map keys to other keys.
54static int TranslateKeyCode(int key_code)
55{
56 switch (key_code) {
thatf37aec22015-02-01 13:38:35 +010057 case KEY_SLEEP: // Lock key on Asus Transformer hardware keyboard
58 return KEY_POWER;
59 }
60 return key_code;
61}
Ethan Yonkere13fa632015-01-27 11:30:03 -060062
that8834a0f2016-01-05 23:29:30 +010063static int KeyCodeToChar(int key_code, bool shiftkey, bool ctrlkey)
thatf37aec22015-02-01 13:38:35 +010064{
thatf37aec22015-02-01 13:38:35 +010065 int keyboard = -1;
66
Ethan Yonkere13fa632015-01-27 11:30:03 -060067 switch (key_code) {
Ethan Yonkere13fa632015-01-27 11:30:03 -060068 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;
that8834a0f2016-01-05 23:29:30 +0100290 case KEY_TAB:
291 keyboard = KEYBOARD_TAB;
292 break;
Ethan Yonkere13fa632015-01-27 11:30:03 -0600293 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;
thatf37aec22015-02-01 13:38:35 +0100362
Ethan Yonkere13fa632015-01-27 11:30:03 -0600363#ifdef _EVENT_LOGGING
364 default:
Matt Mower91584b22016-01-09 14:35:34 -0600365 LOGERR("Unmapped keycode: %i\n", key_code);
Ethan Yonkere13fa632015-01-27 11:30:03 -0600366 break;
367#endif
368 }
that8834a0f2016-01-05 23:29:30 +0100369 if (ctrlkey)
370 {
371 if (keyboard >= 96)
372 keyboard -= 96;
373 else
374 keyboard = -1;
375 }
376 return keyboard;
377}
378
379bool HardwareKeyboard::IsKeyDown(int key_code)
380{
381 std::set<int>::iterator it = mPressedKeys.find(key_code);
382 return (it != mPressedKeys.end());
383}
384
385int HardwareKeyboard::KeyDown(int key_code)
386{
387#ifdef _EVENT_LOGGING
Matt Mower91584b22016-01-09 14:35:34 -0600388 LOGERR("HardwareKeyboard::KeyDown %i\n", key_code);
that8834a0f2016-01-05 23:29:30 +0100389#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 Yonkere13fa632015-01-27 11:30:03 -0600401 return 1; // Return 1 to enable key repeat
402 } else {
thatf37aec22015-02-01 13:38:35 +0100403 mLastKeyChar = 0;
that8834a0f2016-01-05 23:29:30 +0100404 mLastKey = key_code;
405 if (!PageManager::NotifyKey(key_code, true))
406 return 1; // Return 1 to enable key repeat
Ethan Yonkere13fa632015-01-27 11:30:03 -0600407 }
408 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400409}
410
thatf37aec22015-02-01 13:38:35 +0100411int HardwareKeyboard::KeyUp(int key_code)
412{
413#ifdef _EVENT_LOGGING
Matt Mower91584b22016-01-09 14:35:34 -0600414 LOGERR("HardwareKeyboard::KeyUp %i\n", key_code);
thatf37aec22015-02-01 13:38:35 +0100415#endif
416 key_code = TranslateKeyCode(key_code);
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100417 std::set<int>::iterator itr = mPressedKeys.find(key_code);
Ethan Yonkere13fa632015-01-27 11:30:03 -0600418 if (itr != mPressedKeys.end()) {
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100419 mPressedKeys.erase(itr);
420 PageManager::NotifyKey(key_code, false);
421 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400422 return 0;
423}
424
thatf37aec22015-02-01 13:38:35 +0100425int HardwareKeyboard::KeyRepeat()
426{
Dees_Troy51a0e822012-09-05 15:24:24 -0400427#ifdef _EVENT_LOGGING
Matt Mower91584b22016-01-09 14:35:34 -0600428 LOGERR("HardwareKeyboard::KeyRepeat: %i\n", mLastKeyChar);
Dees_Troy51a0e822012-09-05 15:24:24 -0400429#endif
thatf37aec22015-02-01 13:38:35 +0100430 if (mLastKeyChar)
that8834a0f2016-01-05 23:29:30 +0100431 PageManager::NotifyCharInput(mLastKeyChar);
432 else if (mLastKey)
433 PageManager::NotifyKey(mLastKey, true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400434 return 0;
435}
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100436
thatf37aec22015-02-01 13:38:35 +0100437void HardwareKeyboard::ConsumeKeyRelease(int key)
438{
439 // causes following KeyUp event to suppress notifications
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100440 mPressedKeys.erase(key);
441}