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