blob: f7413a4cd0f14c4bead740717b1940f2b13b4484 [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
Jae Hyun Yoo7dfac9f2019-01-15 10:14:59 -080051 /* @brief Sends a wakeup data packet to the USB input device */
52 void sendWakeupPacket();
Eddie James21b177e2018-12-11 13:14:46 -060053 /* @brief Sends an HID report to the USB input device */
54 void sendReport();
55
56 private:
Jae Hyun Yoo7dfac9f2019-01-15 10:14:59 -080057 static constexpr int NUM_MODIFIER_BITS = 4;
58 static constexpr int KEY_REPORT_LENGTH = 8;
59 static constexpr int PTR_REPORT_LENGTH = 5;
Eddie James21b177e2018-12-11 13:14:46 -060060
Eddie James21b177e2018-12-11 13:14:46 -060061 /* @brief HID modifier bits mapped to shift and control key codes */
Jae Hyun Yoo7dfac9f2019-01-15 10:14:59 -080062 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 James21b177e2018-12-11 13:14:46 -060068 /* @brief HID modifier bits mapped to meta and alt key codes */
Jae Hyun Yoo7dfac9f2019-01-15 10:14:59 -080069 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 James21b177e2018-12-11 13:14:46 -060075 /*
76 * @brief Translates a RFB-specific key code to HID modifier bit
77 *
78 * @param[in] key - key code
79 */
Jae Hyun Yoo7dfac9f2019-01-15 10:14:59 -080080 static uint8_t keyToMod(rfbKeySym key);
Eddie James21b177e2018-12-11 13:14:46 -060081 /*
82 * @brief Translates a RFB-specific key code to HID scancode
83 *
84 * @param[in] key - key code
85 */
Jae Hyun Yoo7dfac9f2019-01-15 10:14:59 -080086 static uint8_t keyToScancode(rfbKeySym key);
Eddie James21b177e2018-12-11 13:14:46 -060087
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 Yoo7dfac9f2019-01-15 10:14:59 -080092 /* @brief File descriptor for the USB keyboard device */
93 int keyboardFd;
94 /* @brief File descriptor for the USB mouse device */
95 int pointerFd;
Eddie James21b177e2018-12-11 13:14:46 -060096 /* @brief Data for keyboard report */
Jae Hyun Yoo7dfac9f2019-01-15 10:14:59 -080097 uint8_t keyboardReport[KEY_REPORT_LENGTH];
Eddie James21b177e2018-12-11 13:14:46 -060098 /* @brief Data for pointer report */
Jae Hyun Yoo7dfac9f2019-01-15 10:14:59 -080099 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 James21b177e2018-12-11 13:14:46 -0600104 /*
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