blob: 7c8120d3ce07141aef5ef2b8ea4f763de27e663d [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 *
22 * @param[in] p - Path to the USB input device
23 */
24 Input(const std::string& p);
25 ~Input();
26 Input(const Input&) = default;
27 Input& operator=(const Input&) = default;
28 Input(Input&&) = default;
29 Input& operator=(Input&&) = default;
30
31 /*
32 * @brief RFB client key event handler
33 *
34 * @param[in] down - Boolean indicating whether key is pressed or not
35 * @param[in] key - Key code
36 * @param[in] cl - Handle to the RFB client
37 */
38 static void keyEvent(rfbBool down, rfbKeySym key, rfbClientPtr cl);
39 /*
40 * @brief RFB client pointer event handler
41 *
42 * @param[in] buttonMask - Bitmask indicating which buttons have been
43 * pressed
44 * @param[in] x - Pointer x-coordinate
45 * @param[in] y - Pointer y-coordinate
46 * @param[in] cl - Handle to the RFB client
47 */
48 static void pointerEvent(int buttonMask, int x, int y, rfbClientPtr cl);
49
50 /*
51 * @brief Sends a data packet to the USB input device
52 *
53 * @param[in] data - pointer to data
54 * @param[in] size - number of bytes to send
55 */
56 void sendRaw(char* data, int size);
57 /* @brief Sends an HID report to the USB input device */
58 void sendReport();
59
60 private:
61 enum
62 {
63 NUM_MODIFIER_BITS = 4,
64 POINTER_LENGTH = 6,
65 REPORT_LENGTH = 8
66 };
67
68 /* @brief Keyboard HID identifier byte */
69 static const char keyboardID;
70 /* @brief Pointer HID identifier byte */
71 static const char pointerID;
72 /* @brief HID modifier bits mapped to shift and control key codes */
73 static const char shiftCtrlMap[NUM_MODIFIER_BITS];
74 /* @brief HID modifier bits mapped to meta and alt key codes */
75 static const char metaAltMap[NUM_MODIFIER_BITS];
76
77 /*
78 * @brief Translates a RFB-specific key code to HID modifier bit
79 *
80 * @param[in] key - key code
81 */
82 static char keyToMod(rfbKeySym key);
83 /*
84 * @brief Translates a RFB-specific key code to HID scancode
85 *
86 * @param[in] key - key code
87 */
88 static char keyToScancode(rfbKeySym key);
89
90 /* @brief Indicates whether or not to send a keyboard report */
91 bool sendKeyboard;
92 /* @brief Indicates whether or not to send a pointer report */
93 bool sendPointer;
94 /* @brief File descriptor for the USB input device */
95 int fd;
96 /* @brief Data for keyboard report */
97 char keyboardReport[REPORT_LENGTH];
98 /* @brief Data for pointer report */
99 char pointerReport[REPORT_LENGTH];
100 /* @brief Path to the USB input device */
101 std::string path;
102 /*
103 * @brief Mapping of RFB key code to report data index to keep track
104 * of which keys are down
105 */
106 std::map<int, int> keysDown;
107};
108
109} // namespace ikvm