blob: 99170bab72922295dd67f03212f9d84c8331bbf7 [file] [log] [blame]
Eddie James21b177e2018-12-11 13:14:46 -06001#pragma once
2
3#include <rfb/rfb.h>
4
5#include <map>
6#include <string>
7
8namespace ikvm
9{
10
11/*
12 * @class Input
13 * @brief Receives events from RFB clients and sends reports to the USB input
14 * device
15 */
16class Input
17{
18 public:
19 /*
20 * @brief Constructs Input object
21 *
Jae Hyun Yoo7dfac9f2019-01-15 10:14:59 -080022 * @param[in] kbdPath - Path to the USB keyboard device
23 * @param[in] ptrPath - Path to the USB mouse device
Eddie James21b177e2018-12-11 13:14:46 -060024 */
Jae Hyun Yoo7dfac9f2019-01-15 10:14:59 -080025 Input(const std::string& kbdPath, const std::string& ptrPath);
Eddie James21b177e2018-12-11 13:14:46 -060026 ~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
Eddie James23135dd2019-08-09 15:41:37 -050051 /* @brief Re-opens USB device in case the endpoint shutdown */
52 void restart();
Jae Hyun Yoo7dfac9f2019-01-15 10:14:59 -080053 /* @brief Sends a wakeup data packet to the USB input device */
54 void sendWakeupPacket();
Eddie James21b177e2018-12-11 13:14:46 -060055 /* @brief Sends an HID report to the USB input device */
56 void sendReport();
57
58 private:
Jae Hyun Yoo7dfac9f2019-01-15 10:14:59 -080059 static constexpr int NUM_MODIFIER_BITS = 4;
60 static constexpr int KEY_REPORT_LENGTH = 8;
61 static constexpr int PTR_REPORT_LENGTH = 5;
Eddie James21b177e2018-12-11 13:14:46 -060062
Eddie James21b177e2018-12-11 13:14:46 -060063 /* @brief HID modifier bits mapped to shift and control key codes */
Jae Hyun Yoo7dfac9f2019-01-15 10:14:59 -080064 static constexpr uint8_t shiftCtrlMap[NUM_MODIFIER_BITS] = {
65 0x02, // left shift
66 0x20, // right shift
67 0x01, // left control
68 0x10 // right control
69 };
Eddie James21b177e2018-12-11 13:14:46 -060070 /* @brief HID modifier bits mapped to meta and alt key codes */
Jae Hyun Yoo7dfac9f2019-01-15 10:14:59 -080071 static constexpr uint8_t metaAltMap[NUM_MODIFIER_BITS] = {
72 0x08, // left meta
73 0x80, // right meta
74 0x04, // left alt
75 0x40 // right alt
76 };
Eddie James21b177e2018-12-11 13:14:46 -060077 /*
78 * @brief Translates a RFB-specific key code to HID modifier bit
79 *
80 * @param[in] key - key code
81 */
Jae Hyun Yoo7dfac9f2019-01-15 10:14:59 -080082 static uint8_t keyToMod(rfbKeySym key);
Eddie James21b177e2018-12-11 13:14:46 -060083 /*
84 * @brief Translates a RFB-specific key code to HID scancode
85 *
86 * @param[in] key - key code
87 */
Jae Hyun Yoo7dfac9f2019-01-15 10:14:59 -080088 static uint8_t keyToScancode(rfbKeySym key);
Eddie James21b177e2018-12-11 13:14:46 -060089
Eddie James4749f932019-04-18 11:06:39 -050090 /* @brief Indicates whether or not a pointer report error has occurred */
91 bool pointerError;
Eddie James21b177e2018-12-11 13:14:46 -060092 /* @brief Indicates whether or not to send a keyboard report */
93 bool sendKeyboard;
94 /* @brief Indicates whether or not to send a pointer report */
95 bool sendPointer;
Jae Hyun Yoo7dfac9f2019-01-15 10:14:59 -080096 /* @brief File descriptor for the USB keyboard device */
97 int keyboardFd;
98 /* @brief File descriptor for the USB mouse device */
99 int pointerFd;
Eddie James21b177e2018-12-11 13:14:46 -0600100 /* @brief Data for keyboard report */
Jae Hyun Yoo7dfac9f2019-01-15 10:14:59 -0800101 uint8_t keyboardReport[KEY_REPORT_LENGTH];
Eddie James21b177e2018-12-11 13:14:46 -0600102 /* @brief Data for pointer report */
Jae Hyun Yoo7dfac9f2019-01-15 10:14:59 -0800103 uint8_t pointerReport[PTR_REPORT_LENGTH];
104 /* @brief Path to the USB keyboard device */
105 std::string keyboardPath;
106 /* @brief Path to the USB mouse device */
107 std::string pointerPath;
Eddie James21b177e2018-12-11 13:14:46 -0600108 /*
109 * @brief Mapping of RFB key code to report data index to keep track
110 * of which keys are down
111 */
112 std::map<int, int> keysDown;
113};
114
115} // namespace ikvm