-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathkeyboard_monitor_linux.cpp
More file actions
219 lines (177 loc) · 6.32 KB
/
keyboard_monitor_linux.cpp
File metadata and controls
219 lines (177 loc) · 6.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#include <iostream>
#include <string>
#include <memory>
#include <thread>
#include <atomic>
#include <chrono>
#include "../../keyboard_monitor.h"
// Import X11 headers after including the header to avoid conflicts
// We'll need to undefine None to avoid conflicts with our enum
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/extensions/XInput2.h>
#include <X11/keysym.h>
// Handle the None conflict from X11
#ifdef None
#undef None
#endif
namespace nativeapi {
class KeyboardMonitor::Impl {
public:
Impl(KeyboardMonitor* monitor) : monitor_(monitor), display_(nullptr), monitoring_(false) {}
Display* display_;
std::atomic<bool> monitoring_;
std::thread monitoring_thread_;
KeyboardMonitor* monitor_;
int xi_opcode_;
void MonitoringLoop();
void InitializeXInput();
void CleanupXInput();
uint32_t GetModifierState();
};
KeyboardMonitor::KeyboardMonitor() : impl_(std::make_unique<Impl>(this)) {}
KeyboardMonitor::~KeyboardMonitor() {
Stop();
}
void KeyboardMonitor::Impl::InitializeXInput() {
display_ = XOpenDisplay(nullptr);
if (!display_) {
std::cerr << "Failed to open X display" << std::endl;
return;
}
// Check for XInput extension
int event, error;
if (!XQueryExtension(display_, "XInputExtension", &xi_opcode_, &event, &error)) {
std::cerr << "XInput extension not available" << std::endl;
XCloseDisplay(display_);
display_ = nullptr;
return;
}
// Check XInput version
int major = 2, minor = 0;
if (XIQueryVersion(display_, &major, &minor) != Success) {
std::cerr << "XInput 2.0 not available" << std::endl;
XCloseDisplay(display_);
display_ = nullptr;
return;
}
// Select for keyboard events on root window
XIEventMask eventmask;
unsigned char mask[XIMaskLen(XI_LASTEVENT)] = {0};
eventmask.deviceid = XIAllMasterDevices;
eventmask.mask_len = sizeof(mask);
eventmask.mask = mask;
XISetMask(mask, XI_KeyPress);
XISetMask(mask, XI_KeyRelease);
Window root = DefaultRootWindow(display_);
if (XISelectEvents(display_, root, &eventmask, 1) != Success) {
std::cerr << "Failed to select XI events" << std::endl;
XCloseDisplay(display_);
display_ = nullptr;
return;
}
}
void KeyboardMonitor::Impl::CleanupXInput() {
if (display_) {
XCloseDisplay(display_);
display_ = nullptr;
}
}
uint32_t KeyboardMonitor::Impl::GetModifierState() {
uint32_t modifier_keys = static_cast<uint32_t>(ModifierKey::None);
if (!display_) return modifier_keys;
// Query current keyboard state
char keys[32];
XQueryKeymap(display_, keys);
// Check for common modifier keycodes
// These keycodes may vary by system, but are common defaults
int shift_keycode = XKeysymToKeycode(display_, XK_Shift_L);
int ctrl_keycode = XKeysymToKeycode(display_, XK_Control_L);
int alt_keycode = XKeysymToKeycode(display_, XK_Alt_L);
int meta_keycode = XKeysymToKeycode(display_, XK_Super_L);
int caps_keycode = XKeysymToKeycode(display_, XK_Caps_Lock);
int num_keycode = XKeysymToKeycode(display_, XK_Num_Lock);
int scroll_keycode = XKeysymToKeycode(display_, XK_Scroll_Lock);
// Check if keys are pressed
if (shift_keycode && (keys[shift_keycode / 8] & (1 << (shift_keycode % 8)))) {
modifier_keys |= static_cast<uint32_t>(ModifierKey::Shift);
}
if (ctrl_keycode && (keys[ctrl_keycode / 8] & (1 << (ctrl_keycode % 8)))) {
modifier_keys |= static_cast<uint32_t>(ModifierKey::Ctrl);
}
if (alt_keycode && (keys[alt_keycode / 8] & (1 << (alt_keycode % 8)))) {
modifier_keys |= static_cast<uint32_t>(ModifierKey::Alt);
}
if (meta_keycode && (keys[meta_keycode / 8] & (1 << (meta_keycode % 8)))) {
modifier_keys |= static_cast<uint32_t>(ModifierKey::Meta);
}
if (caps_keycode && (keys[caps_keycode / 8] & (1 << (caps_keycode % 8)))) {
modifier_keys |= static_cast<uint32_t>(ModifierKey::CapsLock);
}
if (num_keycode && (keys[num_keycode / 8] & (1 << (num_keycode % 8)))) {
modifier_keys |= static_cast<uint32_t>(ModifierKey::NumLock);
}
if (scroll_keycode && (keys[scroll_keycode / 8] & (1 << (scroll_keycode % 8)))) {
modifier_keys |= static_cast<uint32_t>(ModifierKey::ScrollLock);
}
return modifier_keys;
}
void KeyboardMonitor::Impl::MonitoringLoop() {
if (!display_) return;
while (monitoring_) {
// Check for pending events
while (XPending(display_) && monitoring_) {
XEvent event;
XNextEvent(display_, &event);
// Handle XI2 events
if (event.xcookie.type == GenericEvent && event.xcookie.extension == xi_opcode_) {
if (XGetEventData(display_, &event.xcookie)) {
XIDeviceEvent* xi_event = (XIDeviceEvent*)event.xcookie.data;
if (xi_event->evtype == XI_KeyPress) {
KeyPressedEvent key_event(xi_event->detail);
monitor_->EmitSync(key_event);
ModifierKeysChangedEvent modifier_event(GetModifierState());
monitor_->EmitSync(modifier_event);
} else if (xi_event->evtype == XI_KeyRelease) {
KeyReleasedEvent key_event(xi_event->detail);
monitor_->EmitSync(key_event);
ModifierKeysChangedEvent modifier_event(GetModifierState());
monitor_->EmitSync(modifier_event);
}
XFreeEventData(display_, &event.xcookie);
}
}
}
// Small sleep to prevent excessive CPU usage
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
}
void KeyboardMonitor::Start() {
if (impl_->monitoring_) {
return; // Already started
}
impl_->InitializeXInput();
if (!impl_->display_) {
std::cerr << "Failed to initialize X11 display for keyboard monitoring" << std::endl;
return;
}
impl_->monitoring_ = true;
impl_->monitoring_thread_ = std::thread(&KeyboardMonitor::Impl::MonitoringLoop, impl_.get());
std::cout << "Keyboard monitor started successfully" << std::endl;
}
void KeyboardMonitor::Stop() {
if (!impl_->monitoring_) {
return; // Already stopped
}
impl_->monitoring_ = false;
// Wait for monitoring thread to finish
if (impl_->monitoring_thread_.joinable()) {
impl_->monitoring_thread_.join();
}
impl_->CleanupXInput();
std::cout << "Keyboard monitor stopped successfully" << std::endl;
}
bool KeyboardMonitor::IsMonitoring() const {
return impl_->monitoring_;
}
} // namespace nativeapi