forked from openbmc/obmc-ikvm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathikvm_input.cpp
556 lines (495 loc) · 14.3 KB
/
ikvm_input.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
#include "ikvm_input.hpp"
#include "ikvm_server.hpp"
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <rfb/keysym.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <phosphor-logging/elog-errors.hpp>
#include <phosphor-logging/elog.hpp>
#include <phosphor-logging/log.hpp>
#include <xyz/openbmc_project/Common/File/error.hpp>
#include "scancodes.hpp"
namespace fs = std::filesystem;
namespace ikvm
{
using namespace phosphor::logging;
using namespace sdbusplus::xyz::openbmc_project::Common::File::Error;
Input::Input(const std::string& kbdPath, const std::string& ptrPath) :
keyboardFd(-1), pointerFd(-1), keyboardReport{0}, pointerReport{0},
keyboardPath(kbdPath), pointerPath(ptrPath)
{
hidUdcStream.exceptions(std::ofstream::failbit | std::ofstream::badbit);
hidUdcStream.open(hidUdcPath, std::ios::out | std::ios::app);
}
Input::~Input()
{
if (keyboardFd >= 0)
{
close(keyboardFd);
}
if (pointerFd >= 0)
{
close(pointerFd);
}
disconnect();
hidUdcStream.close();
}
void Input::connect()
{
try
{
for (const auto& port : fs::directory_iterator(usbVirtualHubPath))
{
if (fs::is_directory(port) && !fs::is_symlink(port) &&
!fs::exists(port.path() / "gadget/suspended"))
{
const std::string portId = port.path().filename();
hidUdcStream << portId << std::endl;
break;
}
}
}
catch (fs::filesystem_error& e)
{
log<level::ERR>("Failed to search USB virtual hub port",
entry("ERROR=%s", e.what()));
return;
}
catch (std::ofstream::failure& e)
{
log<level::ERR>("Failed to connect HID gadget",
entry("ERROR=%s", e.what()));
return;
}
if (!keyboardPath.empty())
{
keyboardFd = open(keyboardPath.c_str(),
O_RDWR | O_CLOEXEC | O_NONBLOCK);
if (keyboardFd < 0)
{
log<level::ERR>("Failed to open input device",
entry("PATH=%s", keyboardPath.c_str()),
entry("ERROR=%s", strerror(errno)));
elog<Open>(xyz::openbmc_project::Common::File::Open::ERRNO(errno),
xyz::openbmc_project::Common::File::Open::PATH(
keyboardPath.c_str()));
}
}
if (!pointerPath.empty())
{
pointerFd = open(pointerPath.c_str(), O_RDWR | O_CLOEXEC | O_NONBLOCK);
if (pointerFd < 0)
{
log<level::ERR>("Failed to open input device",
entry("PATH=%s", pointerPath.c_str()),
entry("ERROR=%s", strerror(errno)));
elog<Open>(xyz::openbmc_project::Common::File::Open::ERRNO(errno),
xyz::openbmc_project::Common::File::Open::PATH(
pointerPath.c_str()));
}
}
}
void Input::disconnect()
{
if (keyboardFd >= 0)
{
close(keyboardFd);
keyboardFd = -1;
}
if (pointerFd >= 0)
{
close(pointerFd);
pointerFd = -1;
}
try
{
hidUdcStream << "" << std::endl;
}
catch (std::ofstream::failure& e)
{
log<level::ERR>("Failed to disconnect HID gadget",
entry("ERROR=%s", e.what()));
}
}
void Input::keyEvent(rfbBool down, rfbKeySym key, rfbClientPtr cl)
{
Server::ClientData* cd = (Server::ClientData*)cl->clientData;
Input* input = cd->input;
bool sendKeyboard = false;
if (input->keyboardFd < 0)
{
return;
}
if (down)
{
uint8_t sc = keyToScancode(key);
if (sc)
{
if (input->keysDown.find(key) == input->keysDown.end())
{
for (unsigned int i = 2; i < KEY_REPORT_LENGTH; ++i)
{
if (!input->keyboardReport[i])
{
input->keyboardReport[i] = sc;
input->keysDown.insert(std::make_pair(key, i));
sendKeyboard = true;
break;
}
}
}
}
else
{
uint8_t mod = keyToMod(key);
if (mod)
{
input->keyboardReport[0] |= mod;
sendKeyboard = true;
}
}
}
else
{
auto it = input->keysDown.find(key);
if (it != input->keysDown.end())
{
input->keyboardReport[it->second] = 0;
input->keysDown.erase(it);
sendKeyboard = true;
}
else
{
uint8_t mod = keyToMod(key);
if (mod)
{
input->keyboardReport[0] &= ~mod;
sendKeyboard = true;
}
}
}
if (sendKeyboard)
{
input->writeKeyboard(input->keyboardReport);
}
}
void Input::pointerEvent(int buttonMask, int x, int y, rfbClientPtr cl)
{
Server::ClientData* cd = (Server::ClientData*)cl->clientData;
Input* input = cd->input;
Server* server = (Server*)cl->screen->screenData;
const Video& video = server->getVideo();
if (input->pointerFd < 0)
{
return;
}
input->pointerReport[0] = ((buttonMask & 0x4) >> 1) |
((buttonMask & 0x2) << 1) | (buttonMask & 0x1);
if (x >= 0 && (unsigned int)x < video.getWidth())
{
uint16_t xx = (uint16_t)(x * (SHRT_MAX + 1) / video.getWidth());
memcpy(&input->pointerReport[1], &xx, 2);
}
if (y >= 0 && (unsigned int)y < video.getHeight())
{
uint16_t yy = (uint16_t)(y * (SHRT_MAX + 1) / video.getHeight());
memcpy(&input->pointerReport[3], &yy, 2);
}
rfbDefaultPtrAddEvent(buttonMask, x, y, cl);
input->writePointer(input->pointerReport);
}
void Input::sendWakeupPacket()
{
uint8_t wakeupReport[KEY_REPORT_LENGTH] = {0};
if (pointerFd >= 0)
{
uint16_t xy = SHRT_MAX / 2;
memcpy(&wakeupReport[1], &xy, 2);
memcpy(&wakeupReport[3], &xy, 2);
writePointer(wakeupReport);
}
if (keyboardFd >= 0)
{
memset(&wakeupReport[0], 0, KEY_REPORT_LENGTH);
wakeupReport[0] = keyToMod(XK_Shift_L);
if (!writeKeyboard(wakeupReport))
{
return;
}
wakeupReport[0] = 0;
writeKeyboard(wakeupReport);
}
}
uint8_t Input::keyToMod(rfbKeySym key)
{
uint8_t mod = 0;
if (key >= XK_Shift_L && key <= XK_Control_R)
{
mod = shiftCtrlMap[key - XK_Shift_L];
}
else if (key >= XK_Meta_L && key <= XK_Alt_R)
{
mod = metaAltMap[key - XK_Meta_L];
}
return mod;
}
uint8_t Input::keyToScancode(rfbKeySym key)
{
uint8_t scancode = 0;
if ((key >= 'A' && key <= 'Z') || (key >= 'a' && key <= 'z'))
{
scancode = USBHID_KEY_A + ((key & 0x5F) - 'A');
}
else if (key >= '1' && key <= '9')
{
scancode = USBHID_KEY_1 + (key - '1');
}
else if (key >= XK_F1 && key <= XK_F12)
{
scancode = USBHID_KEY_F1 + (key - XK_F1);
}
else if (key >= XK_KP_F1 && key <= XK_KP_F4)
{
scancode = USBHID_KEY_F1 + (key - XK_KP_F1);
}
else if (key >= XK_KP_1 && key <= XK_KP_9)
{
scancode = USBHID_KEY_KP_1 + (key - XK_KP_1);
}
else
{
switch (key)
{
case XK_exclam:
scancode = USBHID_KEY_1;
break;
case XK_at:
scancode = USBHID_KEY_2;
break;
case XK_numbersign:
scancode = USBHID_KEY_3;
break;
case XK_dollar:
scancode = USBHID_KEY_4;
break;
case XK_percent:
scancode = USBHID_KEY_5;
break;
case XK_asciicircum:
scancode = USBHID_KEY_6;
break;
case XK_ampersand:
scancode = USBHID_KEY_7;
break;
case XK_asterisk:
scancode = USBHID_KEY_8;
break;
case XK_parenleft:
scancode = USBHID_KEY_9;
break;
case XK_0:
case XK_parenright:
scancode = USBHID_KEY_0;
break;
case XK_Return:
scancode = USBHID_KEY_RETURN;
break;
case XK_Escape:
scancode = USBHID_KEY_ESC;
break;
case XK_BackSpace:
scancode = USBHID_KEY_BACKSPACE;
break;
case XK_Tab:
case XK_KP_Tab:
scancode = USBHID_KEY_TAB;
break;
case XK_space:
case XK_KP_Space:
scancode = USBHID_KEY_SPACE;
break;
case XK_minus:
case XK_underscore:
scancode = USBHID_KEY_MINUS;
break;
case XK_plus:
case XK_equal:
scancode = USBHID_KEY_EQUAL;
break;
case XK_bracketleft:
case XK_braceleft:
scancode = USBHID_KEY_LEFTBRACE;
break;
case XK_bracketright:
case XK_braceright:
scancode = USBHID_KEY_RIGHTBRACE;
break;
case XK_backslash:
case XK_bar:
scancode = USBHID_KEY_BACKSLASH;
break;
case XK_colon:
case XK_semicolon:
scancode = USBHID_KEY_SEMICOLON;
break;
case XK_quotedbl:
case XK_apostrophe:
scancode = USBHID_KEY_APOSTROPHE;
break;
case XK_grave:
case XK_asciitilde:
scancode = USBHID_KEY_GRAVE;
break;
case XK_comma:
case XK_less:
scancode = USBHID_KEY_COMMA;
break;
case XK_period:
case XK_greater:
scancode = USBHID_KEY_DOT;
break;
case XK_slash:
case XK_question:
scancode = USBHID_KEY_SLASH;
break;
case XK_Caps_Lock:
scancode = USBHID_KEY_CAPSLOCK;
break;
case XK_Print:
scancode = USBHID_KEY_PRINT;
break;
case XK_Scroll_Lock:
scancode = USBHID_KEY_SCROLLLOCK;
break;
case XK_Pause:
scancode = USBHID_KEY_PAUSE;
break;
case XK_Insert:
case XK_KP_Insert:
scancode = USBHID_KEY_INSERT;
break;
case XK_Home:
case XK_KP_Home:
scancode = USBHID_KEY_HOME;
break;
case XK_Page_Up:
case XK_KP_Page_Up:
scancode = USBHID_KEY_PAGEUP;
break;
case XK_Delete:
case XK_KP_Delete:
scancode = USBHID_KEY_DELETE;
break;
case XK_End:
case XK_KP_End:
scancode = USBHID_KEY_END;
break;
case XK_Page_Down:
case XK_KP_Page_Down:
scancode = USBHID_KEY_PAGEDOWN;
break;
case XK_Right:
case XK_KP_Right:
scancode = USBHID_KEY_RIGHT;
break;
case XK_Left:
case XK_KP_Left:
scancode = USBHID_KEY_LEFT;
break;
case XK_Down:
case XK_KP_Down:
scancode = USBHID_KEY_DOWN;
break;
case XK_Up:
case XK_KP_Up:
scancode = USBHID_KEY_UP;
break;
case XK_Num_Lock:
scancode = USBHID_KEY_NUMLOCK;
break;
case XK_KP_Enter:
scancode = USBHID_KEY_KP_ENTER;
break;
case XK_KP_Equal:
scancode = USBHID_KEY_KP_EQUAL;
break;
case XK_KP_Multiply:
scancode = USBHID_KEY_KP_MULTIPLY;
break;
case XK_KP_Add:
scancode = USBHID_KEY_KP_ADD;
break;
case XK_KP_Subtract:
scancode = USBHID_KEY_KP_SUBTRACT;
break;
case XK_KP_Decimal:
scancode = USBHID_KEY_KP_DECIMAL;
break;
case XK_KP_Divide:
scancode = USBHID_KEY_KP_DIVIDE;
break;
case XK_KP_0:
scancode = USBHID_KEY_KP_0;
break;
}
}
return scancode;
}
bool Input::writeKeyboard(const uint8_t *report)
{
std::unique_lock<std::mutex> lk(keyMutex);
uint retryCount = HID_REPORT_RETRY_MAX;
while (retryCount > 0)
{
if (write(keyboardFd, report, KEY_REPORT_LENGTH) == KEY_REPORT_LENGTH)
{
break;
}
if (errno != EAGAIN)
{
if (errno != ESHUTDOWN)
{
log<level::ERR>("Failed to write keyboard report",
entry("ERROR=%s", strerror(errno)));
}
break;
}
lk.unlock();
std::this_thread::sleep_for(std::chrono::milliseconds(10));
lk.lock();
retryCount--;
}
if (!retryCount || errno)
{
return false;
}
return true;
}
void Input::writePointer(const uint8_t *report)
{
std::unique_lock<std::mutex> lk(ptrMutex);
uint retryCount = HID_REPORT_RETRY_MAX;
while (retryCount > 0)
{
if (write(pointerFd, report, PTR_REPORT_LENGTH) == PTR_REPORT_LENGTH)
{
break;
}
if (errno != EAGAIN)
{
if (errno != ESHUTDOWN)
{
log<level::ERR>("Failed to write pointer report",
entry("ERROR=%s", strerror(errno)));
}
break;
}
lk.unlock();
std::this_thread::sleep_for(std::chrono::milliseconds(10));
lk.lock();
retryCount--;
}
}
} // namespace ikvm