blob: 202b761d2dd3bd30a80e8009cbdf115d258089d0 [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
Jae Hyun Yoo7dfac9f2019-01-15 10:14:59 -0800159void Input::sendWakeupPacket()
Eddie James21b177e2018-12-11 13:14:46 -0600160{
Jae Hyun Yooeaf5c5b2019-02-20 15:03:17 -0800161 uint8_t wakeupReport[KEY_REPORT_LENGTH] = {0};
Jae Hyun Yoo7dfac9f2019-01-15 10:14:59 -0800162
Jae Hyun Yooeaf5c5b2019-02-20 15:03:17 -0800163 if (pointerFd >= 0)
Jae Hyun Yoo7dfac9f2019-01-15 10:14:59 -0800164 {
Jae Hyun Yooeaf5c5b2019-02-20 15:03:17 -0800165 uint16_t xy = SHRT_MAX / 2;
166
167 memcpy(&wakeupReport[1], &xy, 2);
168 memcpy(&wakeupReport[3], &xy, 2);
169
170 if (write(pointerFd, wakeupReport, PTR_REPORT_LENGTH) !=
171 PTR_REPORT_LENGTH)
172 {
173 log<level::ERR>("Failed to write pointer report",
174 entry("ERROR=%s", strerror(errno)));
175 }
Jae Hyun Yoo7dfac9f2019-01-15 10:14:59 -0800176 }
177
Jae Hyun Yooeaf5c5b2019-02-20 15:03:17 -0800178 if (keyboardFd >= 0)
Eddie James21b177e2018-12-11 13:14:46 -0600179 {
Jae Hyun Yooeaf5c5b2019-02-20 15:03:17 -0800180 memset(&wakeupReport[0], 0, KEY_REPORT_LENGTH);
181
182 wakeupReport[0] = keyToMod(XK_Shift_L);
183
184 if (write(keyboardFd, wakeupReport, KEY_REPORT_LENGTH) !=
185 KEY_REPORT_LENGTH)
186 {
187 log<level::ERR>("Failed to write keyboard report",
188 entry("ERROR=%s", strerror(errno)));
189 return;
190 }
191
192 wakeupReport[0] = 0;
193
194 if (write(keyboardFd, wakeupReport, KEY_REPORT_LENGTH) !=
195 KEY_REPORT_LENGTH)
196 {
197 log<level::ERR>("Failed to write keyboard report",
198 entry("ERROR=%s", strerror(errno)));
199 }
Eddie James21b177e2018-12-11 13:14:46 -0600200 }
201}
202
203void Input::sendReport()
204{
Jae Hyun Yoo7dfac9f2019-01-15 10:14:59 -0800205 if (sendKeyboard && keyboardFd >= 0)
Eddie James21b177e2018-12-11 13:14:46 -0600206 {
Jae Hyun Yoo7dfac9f2019-01-15 10:14:59 -0800207 if (write(keyboardFd, keyboardReport, KEY_REPORT_LENGTH) !=
208 KEY_REPORT_LENGTH)
Eddie James21b177e2018-12-11 13:14:46 -0600209 {
210 log<level::ERR>("Failed to write keyboard report",
211 entry("ERROR=%s", strerror(errno)));
212 }
213
214 sendKeyboard = false;
215 }
216
Jae Hyun Yoo7dfac9f2019-01-15 10:14:59 -0800217 if (sendPointer && pointerFd >= 0)
Eddie James21b177e2018-12-11 13:14:46 -0600218 {
Jae Hyun Yoo7dfac9f2019-01-15 10:14:59 -0800219 if (write(pointerFd, pointerReport, PTR_REPORT_LENGTH) !=
220 PTR_REPORT_LENGTH)
Eddie James21b177e2018-12-11 13:14:46 -0600221 {
Eddie James4749f932019-04-18 11:06:39 -0500222 if (!pointerError)
223 {
224 log<level::ERR>("Failed to write pointer report",
225 entry("ERROR=%s", strerror(errno)));
226 pointerError = true;
227 }
Eddie James21b177e2018-12-11 13:14:46 -0600228 }
229
230 sendPointer = false;
231 }
232}
233
Jae Hyun Yoo7dfac9f2019-01-15 10:14:59 -0800234uint8_t Input::keyToMod(rfbKeySym key)
Eddie James21b177e2018-12-11 13:14:46 -0600235{
Jae Hyun Yoo7dfac9f2019-01-15 10:14:59 -0800236 uint8_t mod = 0;
Eddie James21b177e2018-12-11 13:14:46 -0600237
238 if (key >= XK_Shift_L && key <= XK_Control_R)
239 {
240 mod = shiftCtrlMap[key - XK_Shift_L];
241 }
242 else if (key >= XK_Meta_L && key <= XK_Alt_R)
243 {
244 mod = metaAltMap[key - XK_Meta_L];
245 }
246
247 return mod;
248}
249
Jae Hyun Yoo7dfac9f2019-01-15 10:14:59 -0800250uint8_t Input::keyToScancode(rfbKeySym key)
Eddie James21b177e2018-12-11 13:14:46 -0600251{
Jae Hyun Yoo7dfac9f2019-01-15 10:14:59 -0800252 uint8_t scancode = 0;
Eddie James21b177e2018-12-11 13:14:46 -0600253
254 if ((key >= 'A' && key <= 'Z') || (key >= 'a' && key <= 'z'))
255 {
256 scancode = USBHID_KEY_A + ((key & 0x5F) - 'A');
257 }
258 else if (key >= '1' && key <= '9')
259 {
260 scancode = USBHID_KEY_1 + (key - '1');
261 }
262 else if (key >= XK_F1 && key <= XK_F12)
263 {
264 scancode = USBHID_KEY_F1 + (key - XK_F1);
265 }
266 else
267 {
268 switch (key)
269 {
270 case XK_exclam:
271 scancode = USBHID_KEY_1;
272 break;
273 case XK_at:
274 scancode = USBHID_KEY_2;
275 break;
276 case XK_numbersign:
277 scancode = USBHID_KEY_3;
278 break;
279 case XK_dollar:
280 scancode = USBHID_KEY_4;
281 break;
282 case XK_percent:
283 scancode = USBHID_KEY_5;
284 break;
285 case XK_asciicircum:
286 scancode = USBHID_KEY_6;
287 break;
288 case XK_ampersand:
289 scancode = USBHID_KEY_7;
290 break;
291 case XK_asterisk:
292 scancode = USBHID_KEY_8;
293 break;
294 case XK_parenleft:
295 scancode = USBHID_KEY_9;
296 break;
297 case XK_0:
298 case XK_parenright:
299 scancode = USBHID_KEY_0;
300 break;
301 case XK_Return:
302 scancode = USBHID_KEY_RETURN;
303 break;
304 case XK_Escape:
305 scancode = USBHID_KEY_ESC;
306 break;
307 case XK_BackSpace:
308 scancode = USBHID_KEY_BACKSPACE;
309 break;
310 case XK_Tab:
311 scancode = USBHID_KEY_TAB;
312 break;
313 case XK_space:
314 scancode = USBHID_KEY_SPACE;
315 break;
316 case XK_minus:
317 case XK_underscore:
318 scancode = USBHID_KEY_MINUS;
319 break;
320 case XK_plus:
321 case XK_equal:
322 scancode = USBHID_KEY_EQUAL;
323 break;
324 case XK_bracketleft:
325 case XK_braceleft:
326 scancode = USBHID_KEY_LEFTBRACE;
327 break;
328 case XK_bracketright:
329 case XK_braceright:
330 scancode = USBHID_KEY_RIGHTBRACE;
331 break;
332 case XK_backslash:
333 case XK_bar:
334 scancode = USBHID_KEY_BACKSLASH;
335 break;
336 case XK_colon:
337 case XK_semicolon:
338 scancode = USBHID_KEY_SEMICOLON;
339 break;
340 case XK_quotedbl:
341 case XK_apostrophe:
342 scancode = USBHID_KEY_APOSTROPHE;
343 break;
344 case XK_grave:
345 case XK_asciitilde:
346 scancode = USBHID_KEY_GRAVE;
347 break;
348 case XK_comma:
349 case XK_less:
350 scancode = USBHID_KEY_COMMA;
351 break;
352 case XK_period:
353 case XK_greater:
354 scancode = USBHID_KEY_DOT;
355 break;
356 case XK_slash:
357 case XK_question:
358 scancode = USBHID_KEY_SLASH;
359 break;
360 case XK_Caps_Lock:
361 scancode = USBHID_KEY_CAPSLOCK;
362 break;
363 case XK_Print:
364 scancode = USBHID_KEY_PRINT;
365 break;
366 case XK_Scroll_Lock:
367 scancode = USBHID_KEY_SCROLLLOCK;
368 break;
369 case XK_Pause:
370 scancode = USBHID_KEY_PAUSE;
371 break;
372 case XK_Insert:
373 scancode = USBHID_KEY_INSERT;
374 break;
375 case XK_Home:
376 scancode = USBHID_KEY_HOME;
377 break;
378 case XK_Page_Up:
379 scancode = USBHID_KEY_PAGEUP;
380 break;
381 case XK_Delete:
382 scancode = USBHID_KEY_DELETE;
383 break;
384 case XK_End:
385 scancode = USBHID_KEY_END;
386 break;
387 case XK_Page_Down:
388 scancode = USBHID_KEY_PAGEDOWN;
389 break;
390 case XK_Right:
391 scancode = USBHID_KEY_RIGHT;
392 break;
393 case XK_Left:
394 scancode = USBHID_KEY_LEFT;
395 break;
396 case XK_Down:
397 scancode = USBHID_KEY_DOWN;
398 break;
399 case XK_Up:
400 scancode = USBHID_KEY_UP;
401 break;
402 case XK_Num_Lock:
403 scancode = USBHID_KEY_NUMLOCK;
404 break;
405 }
406 }
407
408 return scancode;
409}
410
411} // namespace ikvm