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