Eddie James | 21b177e | 2018-12-11 13:14:46 -0600 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include <rfb/rfb.h> |
| 4 | |
Jae Hyun Yoo | c11257d | 2020-07-22 23:39:18 -0700 | [diff] [blame] | 5 | #include <filesystem> |
| 6 | #include <fstream> |
Eddie James | 21b177e | 2018-12-11 13:14:46 -0600 | [diff] [blame] | 7 | #include <map> |
Jae Hyun Yoo | 673ac2e | 2020-07-30 00:29:19 -0700 | [diff] [blame] | 8 | #include <mutex> |
Eddie James | 21b177e | 2018-12-11 13:14:46 -0600 | [diff] [blame] | 9 | #include <string> |
| 10 | |
| 11 | namespace ikvm |
| 12 | { |
| 13 | |
| 14 | /* |
| 15 | * @class Input |
| 16 | * @brief Receives events from RFB clients and sends reports to the USB input |
| 17 | * device |
| 18 | */ |
| 19 | class Input |
| 20 | { |
| 21 | public: |
| 22 | /* |
| 23 | * @brief Constructs Input object |
| 24 | * |
Jae Hyun Yoo | 7dfac9f | 2019-01-15 10:14:59 -0800 | [diff] [blame] | 25 | * @param[in] kbdPath - Path to the USB keyboard device |
| 26 | * @param[in] ptrPath - Path to the USB mouse device |
Eddie James | 21b177e | 2018-12-11 13:14:46 -0600 | [diff] [blame] | 27 | */ |
Jae Hyun Yoo | 7dfac9f | 2019-01-15 10:14:59 -0800 | [diff] [blame] | 28 | Input(const std::string& kbdPath, const std::string& ptrPath); |
Eddie James | 21b177e | 2018-12-11 13:14:46 -0600 | [diff] [blame] | 29 | ~Input(); |
| 30 | Input(const Input&) = default; |
| 31 | Input& operator=(const Input&) = default; |
| 32 | Input(Input&&) = default; |
| 33 | Input& operator=(Input&&) = default; |
| 34 | |
Jae Hyun Yoo | c11257d | 2020-07-22 23:39:18 -0700 | [diff] [blame] | 35 | /* @brief Connects HID gadget to host */ |
| 36 | void connect(); |
| 37 | /* @brief Disconnects HID gadget from host */ |
| 38 | void disconnect(); |
Eddie James | 21b177e | 2018-12-11 13:14:46 -0600 | [diff] [blame] | 39 | /* |
| 40 | * @brief RFB client key event handler |
| 41 | * |
| 42 | * @param[in] down - Boolean indicating whether key is pressed or not |
| 43 | * @param[in] key - Key code |
| 44 | * @param[in] cl - Handle to the RFB client |
| 45 | */ |
| 46 | static void keyEvent(rfbBool down, rfbKeySym key, rfbClientPtr cl); |
| 47 | /* |
| 48 | * @brief RFB client pointer event handler |
| 49 | * |
| 50 | * @param[in] buttonMask - Bitmask indicating which buttons have been |
| 51 | * pressed |
| 52 | * @param[in] x - Pointer x-coordinate |
| 53 | * @param[in] y - Pointer y-coordinate |
| 54 | * @param[in] cl - Handle to the RFB client |
| 55 | */ |
| 56 | static void pointerEvent(int buttonMask, int x, int y, rfbClientPtr cl); |
| 57 | |
Jae Hyun Yoo | 7dfac9f | 2019-01-15 10:14:59 -0800 | [diff] [blame] | 58 | /* @brief Sends a wakeup data packet to the USB input device */ |
| 59 | void sendWakeupPacket(); |
Eddie James | 21b177e | 2018-12-11 13:14:46 -0600 | [diff] [blame] | 60 | |
| 61 | private: |
Jae Hyun Yoo | 7dfac9f | 2019-01-15 10:14:59 -0800 | [diff] [blame] | 62 | static constexpr int NUM_MODIFIER_BITS = 4; |
| 63 | static constexpr int KEY_REPORT_LENGTH = 8; |
Tejas Patil | 3fa0bfb | 2021-12-23 15:44:47 +0530 | [diff] [blame] | 64 | static constexpr int PTR_REPORT_LENGTH = 6; |
Eddie James | 21b177e | 2018-12-11 13:14:46 -0600 | [diff] [blame] | 65 | |
Eddie James | 21b177e | 2018-12-11 13:14:46 -0600 | [diff] [blame] | 66 | /* @brief HID modifier bits mapped to shift and control key codes */ |
Jae Hyun Yoo | 7dfac9f | 2019-01-15 10:14:59 -0800 | [diff] [blame] | 67 | static constexpr uint8_t shiftCtrlMap[NUM_MODIFIER_BITS] = { |
| 68 | 0x02, // left shift |
| 69 | 0x20, // right shift |
| 70 | 0x01, // left control |
| 71 | 0x10 // right control |
| 72 | }; |
Eddie James | 21b177e | 2018-12-11 13:14:46 -0600 | [diff] [blame] | 73 | /* @brief HID modifier bits mapped to meta and alt key codes */ |
Jae Hyun Yoo | 7dfac9f | 2019-01-15 10:14:59 -0800 | [diff] [blame] | 74 | static constexpr uint8_t metaAltMap[NUM_MODIFIER_BITS] = { |
| 75 | 0x08, // left meta |
| 76 | 0x80, // right meta |
| 77 | 0x04, // left alt |
| 78 | 0x40 // right alt |
| 79 | }; |
Jae Hyun Yoo | c11257d | 2020-07-22 23:39:18 -0700 | [diff] [blame] | 80 | /* @brief Path to the HID gadget UDC */ |
| 81 | static constexpr const char* hidUdcPath = |
| 82 | "/sys/kernel/config/usb_gadget/obmc_hid/UDC"; |
| 83 | /* @brief Path to the USB virtual hub */ |
| 84 | static constexpr const char* usbVirtualHubPath = |
| 85 | "/sys/bus/platform/devices/1e6a0000.usb-vhub"; |
Jae Hyun Yoo | 673ac2e | 2020-07-30 00:29:19 -0700 | [diff] [blame] | 86 | /* @brief Retry limit for writing an HID report */ |
| 87 | static constexpr int HID_REPORT_RETRY_MAX = 5; |
Eddie James | 21b177e | 2018-12-11 13:14:46 -0600 | [diff] [blame] | 88 | /* |
| 89 | * @brief Translates a RFB-specific key code to HID modifier bit |
| 90 | * |
| 91 | * @param[in] key - key code |
| 92 | */ |
Jae Hyun Yoo | 7dfac9f | 2019-01-15 10:14:59 -0800 | [diff] [blame] | 93 | static uint8_t keyToMod(rfbKeySym key); |
Eddie James | 21b177e | 2018-12-11 13:14:46 -0600 | [diff] [blame] | 94 | /* |
| 95 | * @brief Translates a RFB-specific key code to HID scancode |
| 96 | * |
| 97 | * @param[in] key - key code |
| 98 | */ |
Jae Hyun Yoo | 7dfac9f | 2019-01-15 10:14:59 -0800 | [diff] [blame] | 99 | static uint8_t keyToScancode(rfbKeySym key); |
Eddie James | 21b177e | 2018-12-11 13:14:46 -0600 | [diff] [blame] | 100 | |
George Liu | f79f6f5 | 2022-07-06 09:32:35 +0800 | [diff] [blame] | 101 | bool writeKeyboard(const uint8_t* report); |
| 102 | void writePointer(const uint8_t* report); |
Eddie James | 7cf1f1d | 2019-09-30 15:05:16 -0500 | [diff] [blame] | 103 | |
Jae Hyun Yoo | 7dfac9f | 2019-01-15 10:14:59 -0800 | [diff] [blame] | 104 | /* @brief File descriptor for the USB keyboard device */ |
| 105 | int keyboardFd; |
| 106 | /* @brief File descriptor for the USB mouse device */ |
| 107 | int pointerFd; |
Eddie James | 21b177e | 2018-12-11 13:14:46 -0600 | [diff] [blame] | 108 | /* @brief Data for keyboard report */ |
Jae Hyun Yoo | 7dfac9f | 2019-01-15 10:14:59 -0800 | [diff] [blame] | 109 | uint8_t keyboardReport[KEY_REPORT_LENGTH]; |
Eddie James | 21b177e | 2018-12-11 13:14:46 -0600 | [diff] [blame] | 110 | /* @brief Data for pointer report */ |
Jae Hyun Yoo | 7dfac9f | 2019-01-15 10:14:59 -0800 | [diff] [blame] | 111 | uint8_t pointerReport[PTR_REPORT_LENGTH]; |
| 112 | /* @brief Path to the USB keyboard device */ |
| 113 | std::string keyboardPath; |
| 114 | /* @brief Path to the USB mouse device */ |
| 115 | std::string pointerPath; |
Eddie James | 21b177e | 2018-12-11 13:14:46 -0600 | [diff] [blame] | 116 | /* |
| 117 | * @brief Mapping of RFB key code to report data index to keep track |
| 118 | * of which keys are down |
| 119 | */ |
| 120 | std::map<int, int> keysDown; |
Jae Hyun Yoo | c11257d | 2020-07-22 23:39:18 -0700 | [diff] [blame] | 121 | /* @brief Handle of the HID gadget UDC */ |
| 122 | std::ofstream hidUdcStream; |
Jae Hyun Yoo | 673ac2e | 2020-07-30 00:29:19 -0700 | [diff] [blame] | 123 | /* @brief Mutex for sending keyboard reports */ |
| 124 | std::mutex keyMutex; |
| 125 | /* @brief Mutex for sending pointer reports */ |
| 126 | std::mutex ptrMutex; |
Eddie James | 21b177e | 2018-12-11 13:14:46 -0600 | [diff] [blame] | 127 | }; |
| 128 | |
| 129 | } // namespace ikvm |