Eddie James | 21b177e | 2018-12-11 13:14:46 -0600 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include <rfb/rfb.h> |
| 4 | |
| 5 | #include <map> |
| 6 | #include <string> |
| 7 | |
| 8 | namespace ikvm |
| 9 | { |
| 10 | |
| 11 | /* |
| 12 | * @class Input |
| 13 | * @brief Receives events from RFB clients and sends reports to the USB input |
| 14 | * device |
| 15 | */ |
| 16 | class Input |
| 17 | { |
| 18 | public: |
| 19 | /* |
| 20 | * @brief Constructs Input object |
| 21 | * |
Jae Hyun Yoo | 7dfac9f | 2019-01-15 10:14:59 -0800 | [diff] [blame] | 22 | * @param[in] kbdPath - Path to the USB keyboard device |
| 23 | * @param[in] ptrPath - Path to the USB mouse device |
Eddie James | 21b177e | 2018-12-11 13:14:46 -0600 | [diff] [blame] | 24 | */ |
Jae Hyun Yoo | 7dfac9f | 2019-01-15 10:14:59 -0800 | [diff] [blame] | 25 | Input(const std::string& kbdPath, const std::string& ptrPath); |
Eddie James | 21b177e | 2018-12-11 13:14:46 -0600 | [diff] [blame] | 26 | ~Input(); |
| 27 | Input(const Input&) = default; |
| 28 | Input& operator=(const Input&) = default; |
| 29 | Input(Input&&) = default; |
| 30 | Input& operator=(Input&&) = default; |
| 31 | |
| 32 | /* |
| 33 | * @brief RFB client key event handler |
| 34 | * |
| 35 | * @param[in] down - Boolean indicating whether key is pressed or not |
| 36 | * @param[in] key - Key code |
| 37 | * @param[in] cl - Handle to the RFB client |
| 38 | */ |
| 39 | static void keyEvent(rfbBool down, rfbKeySym key, rfbClientPtr cl); |
| 40 | /* |
| 41 | * @brief RFB client pointer event handler |
| 42 | * |
| 43 | * @param[in] buttonMask - Bitmask indicating which buttons have been |
| 44 | * pressed |
| 45 | * @param[in] x - Pointer x-coordinate |
| 46 | * @param[in] y - Pointer y-coordinate |
| 47 | * @param[in] cl - Handle to the RFB client |
| 48 | */ |
| 49 | static void pointerEvent(int buttonMask, int x, int y, rfbClientPtr cl); |
| 50 | |
Jae Hyun Yoo | 7dfac9f | 2019-01-15 10:14:59 -0800 | [diff] [blame] | 51 | /* @brief Sends a wakeup data packet to the USB input device */ |
| 52 | void sendWakeupPacket(); |
Eddie James | 21b177e | 2018-12-11 13:14:46 -0600 | [diff] [blame] | 53 | /* @brief Sends an HID report to the USB input device */ |
| 54 | void sendReport(); |
| 55 | |
| 56 | private: |
Jae Hyun Yoo | 7dfac9f | 2019-01-15 10:14:59 -0800 | [diff] [blame] | 57 | static constexpr int NUM_MODIFIER_BITS = 4; |
| 58 | static constexpr int KEY_REPORT_LENGTH = 8; |
| 59 | static constexpr int PTR_REPORT_LENGTH = 5; |
Eddie James | 21b177e | 2018-12-11 13:14:46 -0600 | [diff] [blame] | 60 | |
Eddie James | 21b177e | 2018-12-11 13:14:46 -0600 | [diff] [blame] | 61 | /* @brief HID modifier bits mapped to shift and control key codes */ |
Jae Hyun Yoo | 7dfac9f | 2019-01-15 10:14:59 -0800 | [diff] [blame] | 62 | static constexpr uint8_t shiftCtrlMap[NUM_MODIFIER_BITS] = { |
| 63 | 0x02, // left shift |
| 64 | 0x20, // right shift |
| 65 | 0x01, // left control |
| 66 | 0x10 // right control |
| 67 | }; |
Eddie James | 21b177e | 2018-12-11 13:14:46 -0600 | [diff] [blame] | 68 | /* @brief HID modifier bits mapped to meta and alt key codes */ |
Jae Hyun Yoo | 7dfac9f | 2019-01-15 10:14:59 -0800 | [diff] [blame] | 69 | static constexpr uint8_t metaAltMap[NUM_MODIFIER_BITS] = { |
| 70 | 0x08, // left meta |
| 71 | 0x80, // right meta |
| 72 | 0x04, // left alt |
| 73 | 0x40 // right alt |
| 74 | }; |
Eddie James | 21b177e | 2018-12-11 13:14:46 -0600 | [diff] [blame] | 75 | /* |
| 76 | * @brief Translates a RFB-specific key code to HID modifier bit |
| 77 | * |
| 78 | * @param[in] key - key code |
| 79 | */ |
Jae Hyun Yoo | 7dfac9f | 2019-01-15 10:14:59 -0800 | [diff] [blame] | 80 | static uint8_t keyToMod(rfbKeySym key); |
Eddie James | 21b177e | 2018-12-11 13:14:46 -0600 | [diff] [blame] | 81 | /* |
| 82 | * @brief Translates a RFB-specific key code to HID scancode |
| 83 | * |
| 84 | * @param[in] key - key code |
| 85 | */ |
Jae Hyun Yoo | 7dfac9f | 2019-01-15 10:14:59 -0800 | [diff] [blame] | 86 | static uint8_t keyToScancode(rfbKeySym key); |
Eddie James | 21b177e | 2018-12-11 13:14:46 -0600 | [diff] [blame] | 87 | |
| 88 | /* @brief Indicates whether or not to send a keyboard report */ |
| 89 | bool sendKeyboard; |
| 90 | /* @brief Indicates whether or not to send a pointer report */ |
| 91 | bool sendPointer; |
Jae Hyun Yoo | 7dfac9f | 2019-01-15 10:14:59 -0800 | [diff] [blame] | 92 | /* @brief File descriptor for the USB keyboard device */ |
| 93 | int keyboardFd; |
| 94 | /* @brief File descriptor for the USB mouse device */ |
| 95 | int pointerFd; |
Eddie James | 21b177e | 2018-12-11 13:14:46 -0600 | [diff] [blame] | 96 | /* @brief Data for keyboard report */ |
Jae Hyun Yoo | 7dfac9f | 2019-01-15 10:14:59 -0800 | [diff] [blame] | 97 | uint8_t keyboardReport[KEY_REPORT_LENGTH]; |
Eddie James | 21b177e | 2018-12-11 13:14:46 -0600 | [diff] [blame] | 98 | /* @brief Data for pointer report */ |
Jae Hyun Yoo | 7dfac9f | 2019-01-15 10:14:59 -0800 | [diff] [blame] | 99 | uint8_t pointerReport[PTR_REPORT_LENGTH]; |
| 100 | /* @brief Path to the USB keyboard device */ |
| 101 | std::string keyboardPath; |
| 102 | /* @brief Path to the USB mouse device */ |
| 103 | std::string pointerPath; |
Eddie James | 21b177e | 2018-12-11 13:14:46 -0600 | [diff] [blame] | 104 | /* |
| 105 | * @brief Mapping of RFB key code to report data index to keep track |
| 106 | * of which keys are down |
| 107 | */ |
| 108 | std::map<int, int> keysDown; |
| 109 | }; |
| 110 | |
| 111 | } // namespace ikvm |