blob: 1767cbea73627d769b868c5c941839bf0a2f41bb [file] [log] [blame]
Eddie James21b177e2018-12-11 13:14:46 -06001#include "ikvm_input.hpp"
2
3#include "ikvm_server.hpp"
4
5#include <err.h>
6#include <errno.h>
7#include <fcntl.h>
8#include <rfb/keysym.h>
9#include <sys/stat.h>
10#include <sys/types.h>
11
12#include <phosphor-logging/elog-errors.hpp>
13#include <phosphor-logging/elog.hpp>
14#include <phosphor-logging/log.hpp>
15#include <xyz/openbmc_project/Common/File/error.hpp>
16
Jae Hyun Yoo7dfac9f2019-01-15 10:14:59 -080017#include "scancodes.hpp"
Eddie James21b177e2018-12-11 13:14:46 -060018
19namespace ikvm
20{
21
22using namespace phosphor::logging;
23using namespace sdbusplus::xyz::openbmc_project::Common::File::Error;
24
Jae Hyun Yoo7dfac9f2019-01-15 10:14:59 -080025Input::Input(const std::string& kbdPath, const std::string& ptrPath) :
Eddie James4749f932019-04-18 11:06:39 -050026 pointerError(false), sendKeyboard(false), sendPointer(false),
Jae Hyun Yoo7dfac9f2019-01-15 10:14:59 -080027 keyboardFd(-1), pointerFd(-1), keyboardReport{0}, pointerReport{0},
28 keyboardPath(kbdPath), pointerPath(ptrPath)
Eddie James21b177e2018-12-11 13:14:46 -060029{
Jae Hyun Yoo7dfac9f2019-01-15 10:14:59 -080030 if (!keyboardPath.empty())
Eddie James21b177e2018-12-11 13:14:46 -060031 {
Jae Hyun Yoo7dfac9f2019-01-15 10:14:59 -080032 keyboardFd = open(keyboardPath.c_str(), O_RDWR | O_CLOEXEC);
33 if (keyboardFd < 0)
34 {
35 log<level::ERR>("Failed to open input device",
36 entry("PATH=%s", keyboardPath.c_str()),
37 entry("ERROR=%s", strerror(errno)));
38 elog<Open>(xyz::openbmc_project::Common::File::Open::ERRNO(errno),
39 xyz::openbmc_project::Common::File::Open::PATH(
40 keyboardPath.c_str()));
41 }
Eddie James21b177e2018-12-11 13:14:46 -060042 }
43
Jae Hyun Yoo7dfac9f2019-01-15 10:14:59 -080044 if (!pointerPath.empty())
45 {
Eddie James4749f932019-04-18 11:06:39 -050046 pointerFd = open(pointerPath.c_str(), O_RDWR | O_CLOEXEC | O_NONBLOCK);
Jae Hyun Yoo7dfac9f2019-01-15 10:14:59 -080047 if (pointerFd < 0)
48 {
49 log<level::ERR>("Failed to open input device",
50 entry("PATH=%s", pointerPath.c_str()),
51 entry("ERROR=%s", strerror(errno)));
52 elog<Open>(xyz::openbmc_project::Common::File::Open::ERRNO(errno),
53 xyz::openbmc_project::Common::File::Open::PATH(
54 pointerPath.c_str()));
55 }
56 }
Eddie James21b177e2018-12-11 13:14:46 -060057}
58
59Input::~Input()
60{
Jae Hyun Yoo7dfac9f2019-01-15 10:14:59 -080061 if (keyboardFd >= 0)
62 {
63 close(keyboardFd);
64 }
65
66 if (pointerFd >= 0)
67 {
68 close(pointerFd);
69 }
Eddie James21b177e2018-12-11 13:14:46 -060070}
71
72void Input::keyEvent(rfbBool down, rfbKeySym key, rfbClientPtr cl)
73{
74 Server::ClientData* cd = (Server::ClientData*)cl->clientData;
75 Input* input = cd->input;
76
77 if (down)
78 {
Jae Hyun Yoo7dfac9f2019-01-15 10:14:59 -080079 uint8_t sc = keyToScancode(key);
Eddie James21b177e2018-12-11 13:14:46 -060080
81 if (sc)
82 {
83 if (input->keysDown.find(key) == input->keysDown.end())
84 {
Jae Hyun Yoo7dfac9f2019-01-15 10:14:59 -080085 for (unsigned int i = 2; i < KEY_REPORT_LENGTH; ++i)
Eddie James21b177e2018-12-11 13:14:46 -060086 {
87 if (!input->keyboardReport[i])
88 {
89 input->keyboardReport[i] = sc;
90 input->keysDown.insert(std::make_pair(key, i));
91 input->sendKeyboard = true;
92 break;
93 }
94 }
95 }
96 }
97 else
98 {
Jae Hyun Yoo7dfac9f2019-01-15 10:14:59 -080099 uint8_t mod = keyToMod(key);
Eddie James21b177e2018-12-11 13:14:46 -0600100
101 if (mod)
102 {
Jae Hyun Yoo7dfac9f2019-01-15 10:14:59 -0800103 input->keyboardReport[0] |= mod;
Eddie James21b177e2018-12-11 13:14:46 -0600104 input->sendKeyboard = true;
105 }
106 }
107 }
108 else
109 {
110 auto it = input->keysDown.find(key);
111
112 if (it != input->keysDown.end())
113 {
114 input->keyboardReport[it->second] = 0;
115 input->keysDown.erase(it);
116 input->sendKeyboard = true;
117 }
118 else
119 {
Jae Hyun Yoo7dfac9f2019-01-15 10:14:59 -0800120 uint8_t mod = keyToMod(key);
Eddie James21b177e2018-12-11 13:14:46 -0600121
122 if (mod)
123 {
Jae Hyun Yoo7dfac9f2019-01-15 10:14:59 -0800124 input->keyboardReport[0] &= ~mod;
Eddie James21b177e2018-12-11 13:14:46 -0600125 input->sendKeyboard = true;
126 }
127 }
128 }
129}
130
131void Input::pointerEvent(int buttonMask, int x, int y, rfbClientPtr cl)
132{
133 Server::ClientData* cd = (Server::ClientData*)cl->clientData;
134 Input* input = cd->input;
135 Server* server = (Server*)cl->screen->screenData;
136 const Video& video = server->getVideo();
137
Jae Hyun Yoo33890982019-03-19 10:20:27 -0700138 input->pointerReport[0] = ((buttonMask & 0x4) >> 1) |
139 ((buttonMask & 0x2) << 1) | (buttonMask & 0x1);
Eddie James21b177e2018-12-11 13:14:46 -0600140
141 if (x >= 0 && (unsigned int)x < video.getWidth())
142 {
Jae Hyun Yoo2bc661d2019-02-25 13:52:47 -0800143 uint16_t xx = (uint16_t)(x * (SHRT_MAX + 1) / video.getWidth());
Eddie James21b177e2018-12-11 13:14:46 -0600144
Jae Hyun Yoo7dfac9f2019-01-15 10:14:59 -0800145 memcpy(&input->pointerReport[1], &xx, 2);
Eddie James21b177e2018-12-11 13:14:46 -0600146 }
147
148 if (y >= 0 && (unsigned int)y < video.getHeight())
149 {
Jae Hyun Yoo2bc661d2019-02-25 13:52:47 -0800150 uint16_t yy = (uint16_t)(y * (SHRT_MAX + 1) / video.getHeight());
Eddie James21b177e2018-12-11 13:14:46 -0600151
Jae Hyun Yoo7dfac9f2019-01-15 10:14:59 -0800152 memcpy(&input->pointerReport[3], &yy, 2);
Eddie James21b177e2018-12-11 13:14:46 -0600153 }
154
155 input->sendPointer = true;
156 rfbDefaultPtrAddEvent(buttonMask, x, y, cl);
157}
158
Eddie James23135dd2019-08-09 15:41:37 -0500159void Input::restart()
160{
161 if (!keyboardPath.empty() && keyboardFd < 0)
162 {
163 keyboardFd = open(keyboardPath.c_str(), O_RDWR | O_CLOEXEC);
164 if (keyboardFd < 0)
165 {
166 log<level::ERR>("Failed to open input device",
167 entry("PATH=%s", keyboardPath.c_str()),
168 entry("ERROR=%s", strerror(errno)));
169 }
170
171 sendKeyboard = false;
172 }
173
174 if (!pointerPath.empty() && pointerFd < 0)
175 {
176 pointerFd = open(pointerPath.c_str(), O_RDWR | O_CLOEXEC | O_NONBLOCK);
177 if (pointerFd < 0)
178 {
179 log<level::ERR>("Failed to open input device",
180 entry("PATH=%s", pointerPath.c_str()),
181 entry("ERROR=%s", strerror(errno)));
182 }
183
184 pointerError = false;
185 sendPointer = false;
186 }
187}
188
Jae Hyun Yoo7dfac9f2019-01-15 10:14:59 -0800189void Input::sendWakeupPacket()
Eddie James21b177e2018-12-11 13:14:46 -0600190{
Jae Hyun Yooeaf5c5b2019-02-20 15:03:17 -0800191 uint8_t wakeupReport[KEY_REPORT_LENGTH] = {0};
Jae Hyun Yoo7dfac9f2019-01-15 10:14:59 -0800192
Jae Hyun Yooeaf5c5b2019-02-20 15:03:17 -0800193 if (pointerFd >= 0)
Jae Hyun Yoo7dfac9f2019-01-15 10:14:59 -0800194 {
Jae Hyun Yooeaf5c5b2019-02-20 15:03:17 -0800195 uint16_t xy = SHRT_MAX / 2;
196
197 memcpy(&wakeupReport[1], &xy, 2);
198 memcpy(&wakeupReport[3], &xy, 2);
199
200 if (write(pointerFd, wakeupReport, PTR_REPORT_LENGTH) !=
201 PTR_REPORT_LENGTH)
202 {
203 log<level::ERR>("Failed to write pointer report",
204 entry("ERROR=%s", strerror(errno)));
205 }
Jae Hyun Yoo7dfac9f2019-01-15 10:14:59 -0800206 }
207
Jae Hyun Yooeaf5c5b2019-02-20 15:03:17 -0800208 if (keyboardFd >= 0)
Eddie James21b177e2018-12-11 13:14:46 -0600209 {
Jae Hyun Yooeaf5c5b2019-02-20 15:03:17 -0800210 memset(&wakeupReport[0], 0, KEY_REPORT_LENGTH);
211
212 wakeupReport[0] = keyToMod(XK_Shift_L);
213
214 if (write(keyboardFd, wakeupReport, KEY_REPORT_LENGTH) !=
215 KEY_REPORT_LENGTH)
216 {
217 log<level::ERR>("Failed to write keyboard report",
218 entry("ERROR=%s", strerror(errno)));
219 return;
220 }
221
222 wakeupReport[0] = 0;
223
224 if (write(keyboardFd, wakeupReport, KEY_REPORT_LENGTH) !=
225 KEY_REPORT_LENGTH)
226 {
227 log<level::ERR>("Failed to write keyboard report",
228 entry("ERROR=%s", strerror(errno)));
229 }
Eddie James21b177e2018-12-11 13:14:46 -0600230 }
231}
232
233void Input::sendReport()
234{
Jae Hyun Yoo7dfac9f2019-01-15 10:14:59 -0800235 if (sendKeyboard && keyboardFd >= 0)
Eddie James21b177e2018-12-11 13:14:46 -0600236 {
Jae Hyun Yoo7dfac9f2019-01-15 10:14:59 -0800237 if (write(keyboardFd, keyboardReport, KEY_REPORT_LENGTH) !=
238 KEY_REPORT_LENGTH)
Eddie James21b177e2018-12-11 13:14:46 -0600239 {
240 log<level::ERR>("Failed to write keyboard report",
241 entry("ERROR=%s", strerror(errno)));
Eddie James23135dd2019-08-09 15:41:37 -0500242
243 if (errno == ESHUTDOWN)
244 {
245 close(keyboardFd);
246 keyboardFd = -1;
247 }
Eddie James21b177e2018-12-11 13:14:46 -0600248 }
249
250 sendKeyboard = false;
251 }
252
Jae Hyun Yoo7dfac9f2019-01-15 10:14:59 -0800253 if (sendPointer && pointerFd >= 0)
Eddie James21b177e2018-12-11 13:14:46 -0600254 {
Jae Hyun Yoo7dfac9f2019-01-15 10:14:59 -0800255 if (write(pointerFd, pointerReport, PTR_REPORT_LENGTH) !=
256 PTR_REPORT_LENGTH)
Eddie James21b177e2018-12-11 13:14:46 -0600257 {
Eddie James4749f932019-04-18 11:06:39 -0500258 if (!pointerError)
259 {
260 log<level::ERR>("Failed to write pointer report",
261 entry("ERROR=%s", strerror(errno)));
262 pointerError = true;
263 }
Eddie James23135dd2019-08-09 15:41:37 -0500264
265 if (errno == ESHUTDOWN)
266 {
267 close(pointerFd);
268 pointerFd = -1;
269 }
270 }
271 else
272 {
273 pointerError = false;
Eddie James21b177e2018-12-11 13:14:46 -0600274 }
275
276 sendPointer = false;
277 }
278}
279
Jae Hyun Yoo7dfac9f2019-01-15 10:14:59 -0800280uint8_t Input::keyToMod(rfbKeySym key)
Eddie James21b177e2018-12-11 13:14:46 -0600281{
Jae Hyun Yoo7dfac9f2019-01-15 10:14:59 -0800282 uint8_t mod = 0;
Eddie James21b177e2018-12-11 13:14:46 -0600283
284 if (key >= XK_Shift_L && key <= XK_Control_R)
285 {
286 mod = shiftCtrlMap[key - XK_Shift_L];
287 }
288 else if (key >= XK_Meta_L && key <= XK_Alt_R)
289 {
290 mod = metaAltMap[key - XK_Meta_L];
291 }
292
293 return mod;
294}
295
Jae Hyun Yoo7dfac9f2019-01-15 10:14:59 -0800296uint8_t Input::keyToScancode(rfbKeySym key)
Eddie James21b177e2018-12-11 13:14:46 -0600297{
Jae Hyun Yoo7dfac9f2019-01-15 10:14:59 -0800298 uint8_t scancode = 0;
Eddie James21b177e2018-12-11 13:14:46 -0600299
300 if ((key >= 'A' && key <= 'Z') || (key >= 'a' && key <= 'z'))
301 {
302 scancode = USBHID_KEY_A + ((key & 0x5F) - 'A');
303 }
304 else if (key >= '1' && key <= '9')
305 {
306 scancode = USBHID_KEY_1 + (key - '1');
307 }
308 else if (key >= XK_F1 && key <= XK_F12)
309 {
310 scancode = USBHID_KEY_F1 + (key - XK_F1);
311 }
Jae Hyun Yoo513d95e2019-08-20 11:26:53 -0700312 else if (key >= XK_KP_F1 && key <= XK_KP_F4)
313 {
314 scancode = USBHID_KEY_F1 + (key - XK_KP_F1);
315 }
316 else if (key >= XK_KP_1 && key <= XK_KP_9)
317 {
318 scancode = USBHID_KEY_KP_1 + (key - XK_KP_1);
319 }
Eddie James21b177e2018-12-11 13:14:46 -0600320 else
321 {
322 switch (key)
323 {
324 case XK_exclam:
325 scancode = USBHID_KEY_1;
326 break;
327 case XK_at:
328 scancode = USBHID_KEY_2;
329 break;
330 case XK_numbersign:
331 scancode = USBHID_KEY_3;
332 break;
333 case XK_dollar:
334 scancode = USBHID_KEY_4;
335 break;
336 case XK_percent:
337 scancode = USBHID_KEY_5;
338 break;
339 case XK_asciicircum:
340 scancode = USBHID_KEY_6;
341 break;
342 case XK_ampersand:
343 scancode = USBHID_KEY_7;
344 break;
345 case XK_asterisk:
346 scancode = USBHID_KEY_8;
347 break;
348 case XK_parenleft:
349 scancode = USBHID_KEY_9;
350 break;
351 case XK_0:
352 case XK_parenright:
353 scancode = USBHID_KEY_0;
354 break;
355 case XK_Return:
356 scancode = USBHID_KEY_RETURN;
357 break;
358 case XK_Escape:
359 scancode = USBHID_KEY_ESC;
360 break;
361 case XK_BackSpace:
362 scancode = USBHID_KEY_BACKSPACE;
363 break;
364 case XK_Tab:
Jae Hyun Yoo513d95e2019-08-20 11:26:53 -0700365 case XK_KP_Tab:
Eddie James21b177e2018-12-11 13:14:46 -0600366 scancode = USBHID_KEY_TAB;
367 break;
368 case XK_space:
Jae Hyun Yoo513d95e2019-08-20 11:26:53 -0700369 case XK_KP_Space:
Eddie James21b177e2018-12-11 13:14:46 -0600370 scancode = USBHID_KEY_SPACE;
371 break;
372 case XK_minus:
373 case XK_underscore:
374 scancode = USBHID_KEY_MINUS;
375 break;
376 case XK_plus:
377 case XK_equal:
378 scancode = USBHID_KEY_EQUAL;
379 break;
380 case XK_bracketleft:
381 case XK_braceleft:
382 scancode = USBHID_KEY_LEFTBRACE;
383 break;
384 case XK_bracketright:
385 case XK_braceright:
386 scancode = USBHID_KEY_RIGHTBRACE;
387 break;
388 case XK_backslash:
389 case XK_bar:
390 scancode = USBHID_KEY_BACKSLASH;
391 break;
392 case XK_colon:
393 case XK_semicolon:
394 scancode = USBHID_KEY_SEMICOLON;
395 break;
396 case XK_quotedbl:
397 case XK_apostrophe:
398 scancode = USBHID_KEY_APOSTROPHE;
399 break;
400 case XK_grave:
401 case XK_asciitilde:
402 scancode = USBHID_KEY_GRAVE;
403 break;
404 case XK_comma:
405 case XK_less:
406 scancode = USBHID_KEY_COMMA;
407 break;
408 case XK_period:
409 case XK_greater:
410 scancode = USBHID_KEY_DOT;
411 break;
412 case XK_slash:
413 case XK_question:
414 scancode = USBHID_KEY_SLASH;
415 break;
416 case XK_Caps_Lock:
417 scancode = USBHID_KEY_CAPSLOCK;
418 break;
419 case XK_Print:
420 scancode = USBHID_KEY_PRINT;
421 break;
422 case XK_Scroll_Lock:
423 scancode = USBHID_KEY_SCROLLLOCK;
424 break;
425 case XK_Pause:
426 scancode = USBHID_KEY_PAUSE;
427 break;
428 case XK_Insert:
Jae Hyun Yoo513d95e2019-08-20 11:26:53 -0700429 case XK_KP_Insert:
Eddie James21b177e2018-12-11 13:14:46 -0600430 scancode = USBHID_KEY_INSERT;
431 break;
432 case XK_Home:
Jae Hyun Yoo513d95e2019-08-20 11:26:53 -0700433 case XK_KP_Home:
Eddie James21b177e2018-12-11 13:14:46 -0600434 scancode = USBHID_KEY_HOME;
435 break;
436 case XK_Page_Up:
Jae Hyun Yoo513d95e2019-08-20 11:26:53 -0700437 case XK_KP_Page_Up:
Eddie James21b177e2018-12-11 13:14:46 -0600438 scancode = USBHID_KEY_PAGEUP;
439 break;
440 case XK_Delete:
Jae Hyun Yoo513d95e2019-08-20 11:26:53 -0700441 case XK_KP_Delete:
Eddie James21b177e2018-12-11 13:14:46 -0600442 scancode = USBHID_KEY_DELETE;
443 break;
444 case XK_End:
Jae Hyun Yoo513d95e2019-08-20 11:26:53 -0700445 case XK_KP_End:
Eddie James21b177e2018-12-11 13:14:46 -0600446 scancode = USBHID_KEY_END;
447 break;
448 case XK_Page_Down:
Jae Hyun Yoo513d95e2019-08-20 11:26:53 -0700449 case XK_KP_Page_Down:
Eddie James21b177e2018-12-11 13:14:46 -0600450 scancode = USBHID_KEY_PAGEDOWN;
451 break;
452 case XK_Right:
Jae Hyun Yoo513d95e2019-08-20 11:26:53 -0700453 case XK_KP_Right:
Eddie James21b177e2018-12-11 13:14:46 -0600454 scancode = USBHID_KEY_RIGHT;
455 break;
456 case XK_Left:
Jae Hyun Yoo513d95e2019-08-20 11:26:53 -0700457 case XK_KP_Left:
Eddie James21b177e2018-12-11 13:14:46 -0600458 scancode = USBHID_KEY_LEFT;
459 break;
460 case XK_Down:
Jae Hyun Yoo513d95e2019-08-20 11:26:53 -0700461 case XK_KP_Down:
Eddie James21b177e2018-12-11 13:14:46 -0600462 scancode = USBHID_KEY_DOWN;
463 break;
464 case XK_Up:
Jae Hyun Yoo513d95e2019-08-20 11:26:53 -0700465 case XK_KP_Up:
Eddie James21b177e2018-12-11 13:14:46 -0600466 scancode = USBHID_KEY_UP;
467 break;
468 case XK_Num_Lock:
469 scancode = USBHID_KEY_NUMLOCK;
470 break;
Jae Hyun Yoo513d95e2019-08-20 11:26:53 -0700471 case XK_KP_Enter:
472 scancode = USBHID_KEY_KP_ENTER;
473 break;
474 case XK_KP_Equal:
475 scancode = USBHID_KEY_KP_EQUAL;
476 break;
477 case XK_KP_Multiply:
478 scancode = USBHID_KEY_KP_MULTIPLY;
479 break;
480 case XK_KP_Add:
481 scancode = USBHID_KEY_KP_ADD;
482 break;
483 case XK_KP_Subtract:
484 scancode = USBHID_KEY_KP_SUBTRACT;
485 break;
486 case XK_KP_Decimal:
487 scancode = USBHID_KEY_KP_DECIMAL;
488 break;
489 case XK_KP_Divide:
490 scancode = USBHID_KEY_KP_DIVIDE;
491 break;
492 case XK_KP_0:
493 scancode = USBHID_KEY_KP_0;
494 break;
Eddie James21b177e2018-12-11 13:14:46 -0600495 }
496 }
497
498 return scancode;
499}
500
501} // namespace ikvm