blob: 9ca607c0d6286fbda6cf8f41495c5cf42bc62a75 [file] [log] [blame]
Dees_Troy51a0e822012-09-05 15:24:24 -04001// hardwarekeyboard.cpp - HardwareKeyboard object
Dees_Troy51a0e822012-09-05 15:24:24 -04002
3#include <stdarg.h>
4#include <stdio.h>
5#include <stdlib.h>
6#include <string.h>
7#include <fcntl.h>
Dees_Troy51a0e822012-09-05 15:24:24 -04008#include <sys/stat.h>
9#include <sys/time.h>
10#include <sys/mman.h>
11#include <sys/types.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040012#include <time.h>
13#include <unistd.h>
14#include <stdlib.h>
15
16#include <string>
17
18extern "C" {
Ethan Yonkere13fa632015-01-27 11:30:03 -060019#include "../common.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040020}
21
Dees_Troy51a0e822012-09-05 15:24:24 -040022#include "objects.hpp"
that9fa51532015-01-29 02:04:47 +010023#include <linux/input.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040024
thatf37aec22015-02-01 13:38:35 +010025HardwareKeyboard::HardwareKeyboard()
26 : mLastKeyChar(0)
27{
Dees_Troy51a0e822012-09-05 15:24:24 -040028}
29
thatf37aec22015-02-01 13:38:35 +010030HardwareKeyboard::~HardwareKeyboard()
31{
Dees_Troy51a0e822012-09-05 15:24:24 -040032}
33
thatf37aec22015-02-01 13:38:35 +010034// Map keys to other keys.
35static int TranslateKeyCode(int key_code)
36{
37 switch (key_code) {
thatf37aec22015-02-01 13:38:35 +010038 case KEY_SLEEP: // Lock key on Asus Transformer hardware keyboard
39 return KEY_POWER;
40 }
41 return key_code;
42}
Ethan Yonkere13fa632015-01-27 11:30:03 -060043
that8834a0f2016-01-05 23:29:30 +010044static int KeyCodeToChar(int key_code, bool shiftkey, bool ctrlkey)
thatf37aec22015-02-01 13:38:35 +010045{
thatf37aec22015-02-01 13:38:35 +010046 int keyboard = -1;
47
Ethan Yonkere13fa632015-01-27 11:30:03 -060048 switch (key_code) {
Ethan Yonkere13fa632015-01-27 11:30:03 -060049 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;
that8834a0f2016-01-05 23:29:30 +0100271 case KEY_TAB:
272 keyboard = KEYBOARD_TAB;
273 break;
Ethan Yonkere13fa632015-01-27 11:30:03 -0600274 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;
thatf37aec22015-02-01 13:38:35 +0100343
Ethan Yonkere13fa632015-01-27 11:30:03 -0600344#ifdef _EVENT_LOGGING
345 default:
346 LOGE("Unmapped keycode: %i\n", key_code);
347 break;
348#endif
349 }
that8834a0f2016-01-05 23:29:30 +0100350 if (ctrlkey)
351 {
352 if (keyboard >= 96)
353 keyboard -= 96;
354 else
355 keyboard = -1;
356 }
357 return keyboard;
358}
359
360bool HardwareKeyboard::IsKeyDown(int key_code)
361{
362 std::set<int>::iterator it = mPressedKeys.find(key_code);
363 return (it != mPressedKeys.end());
364}
365
366int 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 Yonkere13fa632015-01-27 11:30:03 -0600382 return 1; // Return 1 to enable key repeat
383 } else {
thatf37aec22015-02-01 13:38:35 +0100384 mLastKeyChar = 0;
that8834a0f2016-01-05 23:29:30 +0100385 mLastKey = key_code;
386 if (!PageManager::NotifyKey(key_code, true))
387 return 1; // Return 1 to enable key repeat
Ethan Yonkere13fa632015-01-27 11:30:03 -0600388 }
389 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400390}
391
thatf37aec22015-02-01 13:38:35 +0100392int 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 Bocek0b7fe502014-03-13 17:36:52 +0100398 std::set<int>::iterator itr = mPressedKeys.find(key_code);
Ethan Yonkere13fa632015-01-27 11:30:03 -0600399 if (itr != mPressedKeys.end()) {
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100400 mPressedKeys.erase(itr);
401 PageManager::NotifyKey(key_code, false);
402 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400403 return 0;
404}
405
thatf37aec22015-02-01 13:38:35 +0100406int HardwareKeyboard::KeyRepeat()
407{
Dees_Troy51a0e822012-09-05 15:24:24 -0400408#ifdef _EVENT_LOGGING
thatf37aec22015-02-01 13:38:35 +0100409 LOGE("HardwareKeyboard::KeyRepeat: %i\n", mLastKeyChar);
Dees_Troy51a0e822012-09-05 15:24:24 -0400410#endif
thatf37aec22015-02-01 13:38:35 +0100411 if (mLastKeyChar)
that8834a0f2016-01-05 23:29:30 +0100412 PageManager::NotifyCharInput(mLastKeyChar);
413 else if (mLastKey)
414 PageManager::NotifyKey(mLastKey, true);
Dees_Troy51a0e822012-09-05 15:24:24 -0400415 return 0;
416}
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100417
thatf37aec22015-02-01 13:38:35 +0100418void HardwareKeyboard::ConsumeKeyRelease(int key)
419{
420 // causes following KeyUp event to suppress notifications
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100421 mPressedKeys.erase(key);
422}