blob: d95e6313f62c67144511bca7a64483eee893c693 [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
Eddie James7cf1f1d2019-09-30 15:05:16 -0500200 writePointer(wakeupReport);
Jae Hyun Yoo7dfac9f2019-01-15 10:14:59 -0800201 }
202
Jae Hyun Yooeaf5c5b2019-02-20 15:03:17 -0800203 if (keyboardFd >= 0)
Eddie James21b177e2018-12-11 13:14:46 -0600204 {
Jae Hyun Yooeaf5c5b2019-02-20 15:03:17 -0800205 memset(&wakeupReport[0], 0, KEY_REPORT_LENGTH);
206
207 wakeupReport[0] = keyToMod(XK_Shift_L);
208
Eddie James7cf1f1d2019-09-30 15:05:16 -0500209 if (!writeKeyboard(wakeupReport))
Jae Hyun Yooeaf5c5b2019-02-20 15:03:17 -0800210 {
Jae Hyun Yooeaf5c5b2019-02-20 15:03:17 -0800211 return;
212 }
213
214 wakeupReport[0] = 0;
215
Eddie James7cf1f1d2019-09-30 15:05:16 -0500216 writeKeyboard(wakeupReport);
Eddie James21b177e2018-12-11 13:14:46 -0600217 }
218}
219
220void Input::sendReport()
221{
Jae Hyun Yoo7dfac9f2019-01-15 10:14:59 -0800222 if (sendKeyboard && keyboardFd >= 0)
Eddie James21b177e2018-12-11 13:14:46 -0600223 {
Eddie James7cf1f1d2019-09-30 15:05:16 -0500224 writeKeyboard(keyboardReport);
Eddie James21b177e2018-12-11 13:14:46 -0600225
226 sendKeyboard = false;
227 }
228
Jae Hyun Yoo7dfac9f2019-01-15 10:14:59 -0800229 if (sendPointer && pointerFd >= 0)
Eddie James21b177e2018-12-11 13:14:46 -0600230 {
Eddie James7cf1f1d2019-09-30 15:05:16 -0500231 writePointer(pointerReport);
Eddie James21b177e2018-12-11 13:14:46 -0600232
233 sendPointer = false;
234 }
235}
236
Jae Hyun Yoo7dfac9f2019-01-15 10:14:59 -0800237uint8_t Input::keyToMod(rfbKeySym key)
Eddie James21b177e2018-12-11 13:14:46 -0600238{
Jae Hyun Yoo7dfac9f2019-01-15 10:14:59 -0800239 uint8_t mod = 0;
Eddie James21b177e2018-12-11 13:14:46 -0600240
241 if (key >= XK_Shift_L && key <= XK_Control_R)
242 {
243 mod = shiftCtrlMap[key - XK_Shift_L];
244 }
245 else if (key >= XK_Meta_L && key <= XK_Alt_R)
246 {
247 mod = metaAltMap[key - XK_Meta_L];
248 }
249
250 return mod;
251}
252
Jae Hyun Yoo7dfac9f2019-01-15 10:14:59 -0800253uint8_t Input::keyToScancode(rfbKeySym key)
Eddie James21b177e2018-12-11 13:14:46 -0600254{
Jae Hyun Yoo7dfac9f2019-01-15 10:14:59 -0800255 uint8_t scancode = 0;
Eddie James21b177e2018-12-11 13:14:46 -0600256
257 if ((key >= 'A' && key <= 'Z') || (key >= 'a' && key <= 'z'))
258 {
259 scancode = USBHID_KEY_A + ((key & 0x5F) - 'A');
260 }
261 else if (key >= '1' && key <= '9')
262 {
263 scancode = USBHID_KEY_1 + (key - '1');
264 }
265 else if (key >= XK_F1 && key <= XK_F12)
266 {
267 scancode = USBHID_KEY_F1 + (key - XK_F1);
268 }
Jae Hyun Yoo513d95e2019-08-20 11:26:53 -0700269 else if (key >= XK_KP_F1 && key <= XK_KP_F4)
270 {
271 scancode = USBHID_KEY_F1 + (key - XK_KP_F1);
272 }
273 else if (key >= XK_KP_1 && key <= XK_KP_9)
274 {
275 scancode = USBHID_KEY_KP_1 + (key - XK_KP_1);
276 }
Eddie James21b177e2018-12-11 13:14:46 -0600277 else
278 {
279 switch (key)
280 {
281 case XK_exclam:
282 scancode = USBHID_KEY_1;
283 break;
284 case XK_at:
285 scancode = USBHID_KEY_2;
286 break;
287 case XK_numbersign:
288 scancode = USBHID_KEY_3;
289 break;
290 case XK_dollar:
291 scancode = USBHID_KEY_4;
292 break;
293 case XK_percent:
294 scancode = USBHID_KEY_5;
295 break;
296 case XK_asciicircum:
297 scancode = USBHID_KEY_6;
298 break;
299 case XK_ampersand:
300 scancode = USBHID_KEY_7;
301 break;
302 case XK_asterisk:
303 scancode = USBHID_KEY_8;
304 break;
305 case XK_parenleft:
306 scancode = USBHID_KEY_9;
307 break;
308 case XK_0:
309 case XK_parenright:
310 scancode = USBHID_KEY_0;
311 break;
312 case XK_Return:
313 scancode = USBHID_KEY_RETURN;
314 break;
315 case XK_Escape:
316 scancode = USBHID_KEY_ESC;
317 break;
318 case XK_BackSpace:
319 scancode = USBHID_KEY_BACKSPACE;
320 break;
321 case XK_Tab:
Jae Hyun Yoo513d95e2019-08-20 11:26:53 -0700322 case XK_KP_Tab:
Eddie James21b177e2018-12-11 13:14:46 -0600323 scancode = USBHID_KEY_TAB;
324 break;
325 case XK_space:
Jae Hyun Yoo513d95e2019-08-20 11:26:53 -0700326 case XK_KP_Space:
Eddie James21b177e2018-12-11 13:14:46 -0600327 scancode = USBHID_KEY_SPACE;
328 break;
329 case XK_minus:
330 case XK_underscore:
331 scancode = USBHID_KEY_MINUS;
332 break;
333 case XK_plus:
334 case XK_equal:
335 scancode = USBHID_KEY_EQUAL;
336 break;
337 case XK_bracketleft:
338 case XK_braceleft:
339 scancode = USBHID_KEY_LEFTBRACE;
340 break;
341 case XK_bracketright:
342 case XK_braceright:
343 scancode = USBHID_KEY_RIGHTBRACE;
344 break;
345 case XK_backslash:
346 case XK_bar:
347 scancode = USBHID_KEY_BACKSLASH;
348 break;
349 case XK_colon:
350 case XK_semicolon:
351 scancode = USBHID_KEY_SEMICOLON;
352 break;
353 case XK_quotedbl:
354 case XK_apostrophe:
355 scancode = USBHID_KEY_APOSTROPHE;
356 break;
357 case XK_grave:
358 case XK_asciitilde:
359 scancode = USBHID_KEY_GRAVE;
360 break;
361 case XK_comma:
362 case XK_less:
363 scancode = USBHID_KEY_COMMA;
364 break;
365 case XK_period:
366 case XK_greater:
367 scancode = USBHID_KEY_DOT;
368 break;
369 case XK_slash:
370 case XK_question:
371 scancode = USBHID_KEY_SLASH;
372 break;
373 case XK_Caps_Lock:
374 scancode = USBHID_KEY_CAPSLOCK;
375 break;
376 case XK_Print:
377 scancode = USBHID_KEY_PRINT;
378 break;
379 case XK_Scroll_Lock:
380 scancode = USBHID_KEY_SCROLLLOCK;
381 break;
382 case XK_Pause:
383 scancode = USBHID_KEY_PAUSE;
384 break;
385 case XK_Insert:
Jae Hyun Yoo513d95e2019-08-20 11:26:53 -0700386 case XK_KP_Insert:
Eddie James21b177e2018-12-11 13:14:46 -0600387 scancode = USBHID_KEY_INSERT;
388 break;
389 case XK_Home:
Jae Hyun Yoo513d95e2019-08-20 11:26:53 -0700390 case XK_KP_Home:
Eddie James21b177e2018-12-11 13:14:46 -0600391 scancode = USBHID_KEY_HOME;
392 break;
393 case XK_Page_Up:
Jae Hyun Yoo513d95e2019-08-20 11:26:53 -0700394 case XK_KP_Page_Up:
Eddie James21b177e2018-12-11 13:14:46 -0600395 scancode = USBHID_KEY_PAGEUP;
396 break;
397 case XK_Delete:
Jae Hyun Yoo513d95e2019-08-20 11:26:53 -0700398 case XK_KP_Delete:
Eddie James21b177e2018-12-11 13:14:46 -0600399 scancode = USBHID_KEY_DELETE;
400 break;
401 case XK_End:
Jae Hyun Yoo513d95e2019-08-20 11:26:53 -0700402 case XK_KP_End:
Eddie James21b177e2018-12-11 13:14:46 -0600403 scancode = USBHID_KEY_END;
404 break;
405 case XK_Page_Down:
Jae Hyun Yoo513d95e2019-08-20 11:26:53 -0700406 case XK_KP_Page_Down:
Eddie James21b177e2018-12-11 13:14:46 -0600407 scancode = USBHID_KEY_PAGEDOWN;
408 break;
409 case XK_Right:
Jae Hyun Yoo513d95e2019-08-20 11:26:53 -0700410 case XK_KP_Right:
Eddie James21b177e2018-12-11 13:14:46 -0600411 scancode = USBHID_KEY_RIGHT;
412 break;
413 case XK_Left:
Jae Hyun Yoo513d95e2019-08-20 11:26:53 -0700414 case XK_KP_Left:
Eddie James21b177e2018-12-11 13:14:46 -0600415 scancode = USBHID_KEY_LEFT;
416 break;
417 case XK_Down:
Jae Hyun Yoo513d95e2019-08-20 11:26:53 -0700418 case XK_KP_Down:
Eddie James21b177e2018-12-11 13:14:46 -0600419 scancode = USBHID_KEY_DOWN;
420 break;
421 case XK_Up:
Jae Hyun Yoo513d95e2019-08-20 11:26:53 -0700422 case XK_KP_Up:
Eddie James21b177e2018-12-11 13:14:46 -0600423 scancode = USBHID_KEY_UP;
424 break;
425 case XK_Num_Lock:
426 scancode = USBHID_KEY_NUMLOCK;
427 break;
Jae Hyun Yoo513d95e2019-08-20 11:26:53 -0700428 case XK_KP_Enter:
429 scancode = USBHID_KEY_KP_ENTER;
430 break;
431 case XK_KP_Equal:
432 scancode = USBHID_KEY_KP_EQUAL;
433 break;
434 case XK_KP_Multiply:
435 scancode = USBHID_KEY_KP_MULTIPLY;
436 break;
437 case XK_KP_Add:
438 scancode = USBHID_KEY_KP_ADD;
439 break;
440 case XK_KP_Subtract:
441 scancode = USBHID_KEY_KP_SUBTRACT;
442 break;
443 case XK_KP_Decimal:
444 scancode = USBHID_KEY_KP_DECIMAL;
445 break;
446 case XK_KP_Divide:
447 scancode = USBHID_KEY_KP_DIVIDE;
448 break;
449 case XK_KP_0:
450 scancode = USBHID_KEY_KP_0;
451 break;
Eddie James21b177e2018-12-11 13:14:46 -0600452 }
453 }
454
455 return scancode;
456}
457
Eddie James7cf1f1d2019-09-30 15:05:16 -0500458bool Input::writeKeyboard(const uint8_t *report)
459{
460 if (write(keyboardFd, report, KEY_REPORT_LENGTH) != KEY_REPORT_LENGTH)
461 {
462 log<level::ERR>("Failed to write keyboard report",
463 entry("ERROR=%s", strerror(errno)));
464
465 if (errno == ESHUTDOWN)
466 {
467 close(keyboardFd);
468 keyboardFd = -1;
469 }
470
471 return false;
472 }
473
474 return true;
475}
476
477void Input::writePointer(const uint8_t *report)
478{
479 if (write(pointerFd, report, PTR_REPORT_LENGTH) != PTR_REPORT_LENGTH)
480 {
481 if (!pointerError)
482 {
483 log<level::ERR>("Failed to write pointer report",
484 entry("ERROR=%s", strerror(errno)));
485 pointerError = true;
486 }
487
488 if (errno == ESHUTDOWN)
489 {
490 close(pointerFd);
491 pointerFd = -1;
492 }
493 }
494 else
495 {
496 pointerError = false;
497 }
498}
499
Eddie James21b177e2018-12-11 13:14:46 -0600500} // namespace ikvm