forked from AngryFender/Remini
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwindowapi.cpp
More file actions
232 lines (197 loc) · 6.1 KB
/
Copy pathwindowapi.cpp
File metadata and controls
232 lines (197 loc) · 6.1 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
#include "windowapi.h"
// ---------------------------------------------------------------------------
// Singleton
// ---------------------------------------------------------------------------
WindowApi &WindowApi::instance()
{
static WindowApi s_instance;
return s_instance;
}
WindowApi::WindowApi(QObject *parent) : QObject(parent) {}
WindowApi::~WindowApi() { cleanUp(); }
// ============================ Windows ============================
#ifdef Q_OS_WIN
HHOOK WindowApi::SetWindowsHookExInvoke(int idHook, HOOKPROC lpfn,
HINSTANCE hmod, DWORD dwThreadId)
{
return SetWindowsHookExW(idHook, lpfn, hmod, dwThreadId);
}
BOOL WindowApi::UnhookWindowsHookExInoke(HHOOK hhk)
{
return UnhookWindowsHookEx(hhk);
}
LRESULT WindowApi::CallNextHookExInvoke(HHOOK hhk, int nCode, WPARAM wParam,
LPARAM lParam)
{
return CallNextHookEx(hhk, nCode, wParam, lParam);
}
WId WindowApi::GetForegroundWindowInvoke()
{
return reinterpret_cast<WId>(GetForegroundWindow());
}
void WindowApi::installHook()
{
keyboardProcHook = SetWindowsHookExInvoke(WH_KEYBOARD_LL, detectKeys,
GetModuleHandle(nullptr), 0);
}
void WindowApi::cleanUp()
{
if (keyboardProcHook)
{
UnhookWindowsHookExInoke(keyboardProcHook);
keyboardProcHook = nullptr;
}
}
// ---------------------------------------------------------------------------
// Low-level keyboard hook callback.
//
// `detectKeys` is declared `static` in the header because SetWindowsHookExW
// requires a plain function pointer (a non-static member function cannot be
// passed directly). A static member has no `this` pointer, so any access to
// the instance fields must go through the singleton accessor.
//
// Virtual-key codes:
// 'J' - the J key
// VK_MENU - the Alt key (generic; VK_LMENU / VK_RMENU for left/right)
// ---------------------------------------------------------------------------
LRESULT CALLBACK WindowApi::detectKeys(int code, WPARAM wParam, LPARAM lParam)
{
WindowApi &self = instance();
if (code >= 0)
{
const bool isKeyDown =
(wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN);
const bool isKeyUp = (wParam == WM_KEYUP || wParam == WM_SYSKEYUP);
const auto *kbStruct =
reinterpret_cast<const KBDLLHOOKSTRUCT *>(lParam);
const DWORD vkCode = kbStruct->vkCode;
if (vkCode == 'J' || vkCode == VK_MENU)
{
if (isKeyDown)
{
if (vkCode == 'J')
self.isKeyJPressedDown = true;
if (vkCode == VK_MENU)
self.isKeyAltPressedDown = true;
}
if (isKeyUp)
{
if (vkCode == 'J')
self.isKeyJPressedDown = false;
if (vkCode == VK_MENU)
self.isKeyAltPressedDown = false;
}
if (self.isKeyAltPressedDown && self.isKeyJPressedDown)
{
// `emit` is a no-op macro; this is just a normal signal call.
emit self.showApp();
self.isKeyJPressedDown = false;
return 1; // swallow the keystroke so it does not reach other
// apps
}
}
}
return self.CallNextHookExInvoke(self.keyboardProcHook, code, wParam,
lParam);
}
// ============================ Linux ============================
#elif defined(Q_OS_LINUX)
// Qt headers first.
#include <QGuiApplication>
#include <QString>
#include <QTimer>
// X11 last, so its macros (Bool, None, Status, ...) cannot break Qt headers.
#include <X11/Xlib.h>
#include <X11/keysym.h>
bool WindowApi::isWayland() const
{
const QString platform = QGuiApplication::platformName();
return platform.contains(QStringLiteral("wayland"), Qt::CaseInsensitive);
}
void WindowApi::installHook()
{
if (isWayland())
{
hookInstalled = false;
return;
}
Display *dpy = XOpenDisplay(nullptr);
if (!dpy)
{
hookInstalled = false;
return;
}
display = dpy;
Window root = DefaultRootWindow(dpy);
rootWindow = static_cast<unsigned long>(root);
keycodeJ = XKeysymToKeycode(dpy, XK_j);
// Grab Alt+J while also tolerating CapsLock / NumLock being on.
const unsigned int locks[] = {0, LockMask, Mod2Mask, LockMask | Mod2Mask};
for (unsigned int lock : locks)
{
XGrabKey(dpy, keycodeJ, Mod1Mask | lock, root, True, GrabModeAsync,
GrabModeAsync);
}
XFlush(dpy);
timer = new QTimer(this);
timer->setInterval(50);
connect(timer, &QTimer::timeout, this, &WindowApi::pollEvents);
timer->start();
hookInstalled = true;
}
void WindowApi::cleanUp()
{
if (!hookInstalled)
return;
if (timer)
{
timer->stop();
delete timer; // QObject child of `this`; direct delete is safe here
timer = nullptr;
}
Display *dpy = static_cast<Display *>(display);
if (dpy)
{
Window root = static_cast<Window>(rootWindow);
const unsigned int locks[] = {0, LockMask, Mod2Mask,
LockMask | Mod2Mask};
for (unsigned int lock : locks)
{
XUngrabKey(dpy, keycodeJ, Mod1Mask | lock, root);
}
XCloseDisplay(dpy);
display = nullptr;
}
hookInstalled = false;
}
WId WindowApi::GetForegroundWindowInvoke()
{
if (isWayland())
return 0;
Display *dpy = static_cast<Display *>(display);
if (!dpy)
{
dpy = XOpenDisplay(nullptr);
if (!dpy)
return 0;
display = dpy;
}
Window focused = 0;
int revert = 0;
XGetInputFocus(dpy, &focused, &revert);
return static_cast<WId>(focused);
}
void WindowApi::pollEvents()
{
Display *dpy = static_cast<Display *>(display);
if (!dpy)
return;
while (XPending(dpy))
{
XEvent ev;
XNextEvent(dpy, &ev);
if (ev.type == KeyPress)
emit showApp();
}
}
#endif