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