-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathshortcut_manager_linux.cpp
More file actions
397 lines (335 loc) · 10.8 KB
/
Copy pathshortcut_manager_linux.cpp
File metadata and controls
397 lines (335 loc) · 10.8 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
#include <algorithm>
#include <atomic>
#include <cctype>
#include <mutex>
#include <string>
#include <thread>
#include <unordered_map>
#include <vector>
#include <X11/Xlib.h>
#include <X11/keysym.h>
#include "../../shortcut_manager.h"
namespace nativeapi {
namespace {
std::string ToLower(std::string value) {
std::transform(value.begin(), value.end(), value.begin(),
[](unsigned char ch) { return static_cast<char>(std::tolower(ch)); });
return value;
}
std::vector<std::string> SplitAccelerator(const std::string& accelerator) {
std::vector<std::string> parts;
std::string current;
for (char ch : accelerator) {
if (ch == '+') {
if (!current.empty()) {
parts.push_back(current);
current.clear();
}
} else if (!std::isspace(static_cast<unsigned char>(ch))) {
current.push_back(ch);
}
}
if (!current.empty()) {
parts.push_back(current);
}
return parts;
}
bool ParseAcceleratorTokens(const std::string& accelerator,
std::vector<std::string>& modifiers,
std::string& key_token) {
modifiers.clear();
key_token.clear();
auto parts = SplitAccelerator(accelerator);
if (parts.empty()) {
return false;
}
for (auto& part : parts) {
std::string token = ToLower(part);
if (token == "ctrl" || token == "control" || token == "alt" || token == "option" ||
token == "shift" || token == "cmd" || token == "command" || token == "super" ||
token == "meta" || token == "cmdorctrl" || token == "commandorcontrol") {
modifiers.push_back(token);
} else {
if (!key_token.empty()) {
return false;
}
key_token = token;
}
}
return !key_token.empty();
}
// Token -> X11 KeySym.
//
// Mirrors the token set in src/shortcut_manager.cpp's validator and the tables
// in the macOS/Windows backends, so the same accelerator string means the same
// key on every platform.
KeySym KeySymFromToken(const std::string& token) {
static const std::unordered_map<std::string, KeySym> kKeySyms = {
// Whitespace and editing.
{"space", XK_space},
{"tab", XK_Tab},
{"enter", XK_Return},
{"return", XK_Return},
{"escape", XK_Escape},
{"esc", XK_Escape},
{"backspace", XK_BackSpace},
{"delete", XK_Delete},
{"forwarddelete", XK_Delete},
{"insert", XK_Insert},
{"help", XK_Help},
// Navigation.
{"home", XK_Home},
{"end", XK_End},
{"pageup", XK_Page_Up},
{"pagedown", XK_Page_Down},
{"up", XK_Up},
{"down", XK_Down},
{"left", XK_Left},
{"right", XK_Right},
// Punctuation, by name and by literal character.
{"plus", XK_plus},
{"equal", XK_equal}, {"=", XK_equal},
{"minus", XK_minus}, {"-", XK_minus},
{"comma", XK_comma}, {",", XK_comma},
{"period", XK_period}, {".", XK_period},
{"slash", XK_slash}, {"/", XK_slash},
{"backslash", XK_backslash}, {"\\", XK_backslash},
{"semicolon", XK_semicolon}, {";", XK_semicolon},
{"quote", XK_apostrophe}, {"'", XK_apostrophe},
{"leftbracket", XK_bracketleft}, {"[", XK_bracketleft},
{"rightbracket", XK_bracketright}, {"]", XK_bracketright},
{"grave", XK_grave}, {"backquote", XK_grave}, {"`", XK_grave},
// Keypad.
{"num0", XK_KP_0}, {"num1", XK_KP_1}, {"num2", XK_KP_2},
{"num3", XK_KP_3}, {"num4", XK_KP_4}, {"num5", XK_KP_5},
{"num6", XK_KP_6}, {"num7", XK_KP_7}, {"num8", XK_KP_8},
{"num9", XK_KP_9},
{"numdec", XK_KP_Decimal},
{"numadd", XK_KP_Add},
{"numsub", XK_KP_Subtract},
{"nummult", XK_KP_Multiply},
{"numdiv", XK_KP_Divide},
{"numenter", XK_KP_Enter},
};
// Letters and digits resolve through Xlib's own name table.
if (token.size() == 1) {
char ch = token[0];
if (std::isalpha(static_cast<unsigned char>(ch))) {
return XStringToKeysym(std::string(1, static_cast<char>(std::toupper(ch))).c_str());
}
if (std::isdigit(static_cast<unsigned char>(ch))) {
return XStringToKeysym(std::string(1, ch).c_str());
}
}
// Function keys. XK_F1..XK_F24 are contiguous.
if (token.size() > 1 && token[0] == 'f' &&
token.find_first_not_of("0123456789", 1) == std::string::npos) {
int fnum = std::stoi(token.substr(1));
if (fnum >= 1 && fnum <= 24) {
return XK_F1 + (fnum - 1);
}
return NoSymbol;
}
auto it = kKeySyms.find(token);
return it == kKeySyms.end() ? NoSymbol : it->second;
}
bool ParseAcceleratorLinux(const std::string& accelerator,
unsigned int& modifiers,
KeyCode& keycode,
::Display* display) {
modifiers = 0;
keycode = 0;
std::vector<std::string> modifier_tokens;
std::string key_token;
if (!ParseAcceleratorTokens(accelerator, modifier_tokens, key_token)) {
return false;
}
for (const auto& token : modifier_tokens) {
if (token == "ctrl" || token == "control" || token == "cmdorctrl" ||
token == "commandorcontrol") {
modifiers |= ControlMask;
} else if (token == "alt" || token == "option") {
modifiers |= Mod1Mask;
} else if (token == "shift") {
modifiers |= ShiftMask;
} else if (token == "cmd" || token == "command" || token == "super" || token == "meta") {
modifiers |= Mod4Mask;
}
}
KeySym keysym = KeySymFromToken(key_token);
if (keysym == NoSymbol) {
return false;
}
keycode = XKeysymToKeycode(display, keysym);
return keycode != 0;
}
} // namespace
class ShortcutManagerImpl final : public ShortcutManager::Impl {
public:
explicit ShortcutManagerImpl(ShortcutManager* manager) : manager_(manager) {
XInitThreads();
display_ = XOpenDisplay(nullptr);
if (display_) {
root_ = DefaultRootWindow(display_);
exit_atom_ = XInternAtom(display_, "NATIVEAPI_SHORTCUT_EXIT", False);
}
}
~ShortcutManagerImpl() override {
StopThread();
if (display_) {
XCloseDisplay(display_);
display_ = nullptr;
}
}
bool IsSupported() override { return display_ != nullptr; }
bool RegisterShortcut(const std::shared_ptr<Shortcut>& shortcut) override {
if (!display_) {
return false;
}
unsigned int modifiers = 0;
KeyCode keycode = 0;
if (!ParseAcceleratorLinux(shortcut->GetAccelerator(), modifiers, keycode, display_)) {
return false;
}
GrabKeyWithModifiers(keycode, modifiers);
{
std::lock_guard<std::mutex> lock(mutex_);
GrabInfo info{keycode, modifiers};
grabs_[shortcut->GetId()] = info;
combo_to_shortcut_[ComposeKey(modifiers, keycode)] = shortcut->GetId();
}
EnsureThread();
return true;
}
bool UnregisterShortcut(const std::shared_ptr<Shortcut>& shortcut) override {
if (!display_) {
return false;
}
GrabInfo info;
{
std::lock_guard<std::mutex> lock(mutex_);
auto it = grabs_.find(shortcut->GetId());
if (it == grabs_.end()) {
return false;
}
info = it->second;
grabs_.erase(it);
combo_to_shortcut_.erase(ComposeKey(info.modifiers, info.keycode));
}
UngrabKeyWithModifiers(info.keycode, info.modifiers);
return true;
}
void SetupEventMonitoring() override { EnsureThread(); }
void CleanupEventMonitoring() override {
// Keep thread running while shortcuts may still be registered.
}
private:
struct GrabInfo {
KeyCode keycode;
unsigned int modifiers;
};
void GrabKeyWithModifiers(KeyCode keycode, unsigned int modifiers) {
const unsigned int extra_masks[] = {0, LockMask, Mod2Mask, LockMask | Mod2Mask};
for (unsigned int mask : extra_masks) {
XGrabKey(display_, keycode, modifiers | mask, root_, True, GrabModeAsync, GrabModeAsync);
}
XSync(display_, False);
}
void UngrabKeyWithModifiers(KeyCode keycode, unsigned int modifiers) {
const unsigned int extra_masks[] = {0, LockMask, Mod2Mask, LockMask | Mod2Mask};
for (unsigned int mask : extra_masks) {
XUngrabKey(display_, keycode, modifiers | mask, root_);
}
XSync(display_, False);
}
uint32_t ComposeKey(unsigned int modifiers, KeyCode keycode) const {
return (static_cast<uint32_t>(modifiers & 0xFFFF) << 16) | (keycode & 0xFFFF);
}
void EnsureThread() {
if (running_.load() || !display_) {
return;
}
running_.store(true);
event_thread_ = std::thread([this]() { ThreadMain(); });
}
void StopThread() {
if (!running_.load()) {
return;
}
running_.store(false);
SendExitMessage();
if (event_thread_.joinable()) {
event_thread_.join();
}
}
void SendExitMessage() {
if (!display_) {
return;
}
XClientMessageEvent client_message = {};
client_message.type = ClientMessage;
client_message.message_type = exit_atom_;
client_message.window = root_;
client_message.format = 32;
XSendEvent(display_, root_, False, 0, reinterpret_cast<XEvent*>(&client_message));
XFlush(display_);
}
void ThreadMain() {
if (!display_) {
return;
}
XSelectInput(display_, root_, KeyPressMask);
while (running_.load()) {
XEvent event;
XNextEvent(display_, &event);
if (!running_.load()) {
break;
}
if (event.type == ClientMessage) {
if (event.xclient.message_type == exit_atom_) {
break;
}
}
if (event.type != KeyPress) {
continue;
}
unsigned int normalized_mods = event.xkey.state & ~(LockMask | Mod2Mask);
uint32_t combo = ComposeKey(normalized_mods, event.xkey.keycode);
ShortcutId shortcut_id = 0;
{
std::lock_guard<std::mutex> lock(mutex_);
auto it = combo_to_shortcut_.find(combo);
if (it == combo_to_shortcut_.end()) {
continue;
}
shortcut_id = it->second;
}
auto shortcut = manager_->Get(shortcut_id);
if (!shortcut) {
continue;
}
if (!manager_->IsEnabled() || !shortcut->IsEnabled()) {
continue;
}
manager_->EmitShortcutActivated(shortcut_id, shortcut->GetAccelerator());
shortcut->Invoke();
}
}
ShortcutManager* manager_;
// ::-qualified: nativeapi::Display/Window (via id_allocator.h) shadow the
// X11 typedefs inside this namespace.
::Display* display_ = nullptr;
::Window root_ = 0;
Atom exit_atom_ = None;
std::mutex mutex_;
std::unordered_map<ShortcutId, GrabInfo> grabs_;
std::unordered_map<uint32_t, ShortcutId> combo_to_shortcut_;
std::atomic<bool> running_{false};
std::thread event_thread_;
};
ShortcutManager::ShortcutManager()
: pimpl_(std::make_unique<ShortcutManagerImpl>(this)), next_shortcut_id_(1), enabled_(true) {}
ShortcutManager::~ShortcutManager() {
UnregisterAll();
}
} // namespace nativeapi