forked from mozilla-firefox/firefox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInputFilter.cpp
More file actions
281 lines (248 loc) · 7.88 KB
/
InputFilter.cpp
File metadata and controls
281 lines (248 loc) · 7.88 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
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "InputFilter.h"
namespace mozilla {
namespace widget {
// Static member definitions
std::map<HWND, bool> InputFilter::sEnabledWindows;
std::mutex InputFilter::sMutex;
std::map<HWND, InputFilter::CursorPos> InputFilter::sCursorPositions;
std::mutex InputFilter::sCursorMutex;
std::map<HWND, InputFilter::KeyboardState> InputFilter::sKeyboardStates;
std::mutex InputFilter::sKeyboardMutex;
std::map<HWND, InputFilter::MouseButtonState> InputFilter::sMouseButtonStates;
std::mutex InputFilter::sMouseButtonMutex;
HWND InputFilter::GetTopLevelWindow(HWND hwnd) {
if (!hwnd) return nullptr;
HWND parent = hwnd;
HWND next;
while ((next = ::GetParent(parent)) != nullptr) {
parent = next;
}
return parent;
}
// Window enable/disable
void InputFilter::EnableForWindow(HWND hwnd) {
HWND topLevel = GetTopLevelWindow(hwnd);
if (!topLevel) topLevel = hwnd;
std::lock_guard<std::mutex> lock(sMutex);
sEnabledWindows[topLevel] = true;
}
void InputFilter::DisableForWindow(HWND hwnd) {
HWND topLevel = GetTopLevelWindow(hwnd);
if (!topLevel) topLevel = hwnd;
std::lock_guard<std::mutex> lock(sMutex);
sEnabledWindows[topLevel] = false;
}
bool InputFilter::IsEnabledForWindow(HWND hwnd) {
HWND topLevel = GetTopLevelWindow(hwnd);
if (!topLevel) topLevel = hwnd;
std::lock_guard<std::mutex> lock(sMutex);
auto it = sEnabledWindows.find(topLevel);
return (it != sEnabledWindows.end()) ? it->second : false;
}
void InputFilter::RemoveWindow(HWND hwnd) {
HWND topLevel = GetTopLevelWindow(hwnd);
if (!topLevel) topLevel = hwnd;
{
std::lock_guard<std::mutex> lock(sMutex);
sEnabledWindows.erase(topLevel);
}
{
std::lock_guard<std::mutex> lock(sCursorMutex);
sCursorPositions.erase(topLevel);
}
{
std::lock_guard<std::mutex> lock(sKeyboardMutex);
sKeyboardStates.erase(topLevel);
}
{
std::lock_guard<std::mutex> lock(sMouseButtonMutex);
sMouseButtonStates.erase(topLevel);
}
}
// Message type detection
bool InputFilter::IsKeyboardMessage(UINT msg) {
switch (msg) {
case WM_KEYDOWN:
case WM_KEYUP:
case WM_SYSKEYDOWN:
case WM_SYSKEYUP:
case WM_CHAR:
case WM_SYSCHAR:
case WM_DEADCHAR:
case WM_SYSDEADCHAR:
case WM_UNICHAR:
case WM_HOTKEY:
case WM_IME_KEYDOWN:
case WM_IME_KEYUP:
case WM_IME_CHAR:
case WM_IME_COMPOSITION:
case WM_IME_STARTCOMPOSITION:
case WM_IME_ENDCOMPOSITION:
return true;
default:
return false;
}
}
bool InputFilter::IsMouseMessage(UINT msg) {
switch (msg) {
case WM_MOUSEMOVE:
case WM_LBUTTONDOWN:
case WM_LBUTTONUP:
case WM_LBUTTONDBLCLK:
case WM_RBUTTONDOWN:
case WM_RBUTTONUP:
case WM_RBUTTONDBLCLK:
case WM_MBUTTONDOWN:
case WM_MBUTTONUP:
case WM_MBUTTONDBLCLK:
case WM_XBUTTONDOWN:
case WM_XBUTTONUP:
case WM_XBUTTONDBLCLK:
case WM_MOUSEWHEEL:
case WM_MOUSEHWHEEL:
case WM_NCMOUSEMOVE:
case WM_NCLBUTTONDOWN:
case WM_NCLBUTTONUP:
case WM_NCLBUTTONDBLCLK:
case WM_NCRBUTTONDOWN:
case WM_NCRBUTTONUP:
case WM_NCRBUTTONDBLCLK:
case WM_NCMBUTTONDOWN:
case WM_NCMBUTTONUP:
case WM_NCMBUTTONDBLCLK:
return true;
default:
return false;
}
}
bool InputFilter::IsInputMessage(UINT msg) {
return IsKeyboardMessage(msg) || IsMouseMessage(msg);
}
bool InputFilter::ShouldBlockNativeInput(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
// If filtering not enabled for this window, don't block anything
if (!IsEnabledForWindow(hwnd)) {
return false;
}
// Only block input messages (keyboard and mouse)
if (!IsInputMessage(msg)) {
return false;
}
// Block all native input when filter is enabled
// MouseMux will inject its own events which bypass this filter
return true;
}
// Cursor position tracking
void InputFilter::SetCursorPosForWindow(HWND hwnd, int screenX, int screenY) {
HWND topLevel = GetTopLevelWindow(hwnd);
if (!topLevel) topLevel = hwnd;
std::lock_guard<std::mutex> lock(sCursorMutex);
CursorPos& pos = sCursorPositions[topLevel];
pos.screenX = screenX;
pos.screenY = screenY;
pos.valid = true;
}
bool InputFilter::GetCursorPosForWindow(HWND hwnd, POINT* outPos) {
HWND topLevel = GetTopLevelWindow(hwnd);
if (!topLevel) topLevel = hwnd;
std::lock_guard<std::mutex> lock(sCursorMutex);
auto it = sCursorPositions.find(topLevel);
if (it != sCursorPositions.end() && it->second.valid) {
outPos->x = it->second.screenX;
outPos->y = it->second.screenY;
return true;
}
return false;
}
// Keyboard state management
void InputFilter::SetKeyStateForWindow(HWND hwnd, BYTE* keyState) {
HWND topLevel = GetTopLevelWindow(hwnd);
if (!topLevel) topLevel = hwnd;
std::lock_guard<std::mutex> lock(sKeyboardMutex);
KeyboardState& state = sKeyboardStates[topLevel];
memcpy(state.keys, keyState, 256);
state.valid = true;
}
bool InputFilter::GetKeyStateForWindow(HWND hwnd, BYTE* outKeyState) {
HWND topLevel = GetTopLevelWindow(hwnd);
if (!topLevel) topLevel = hwnd;
std::lock_guard<std::mutex> lock(sKeyboardMutex);
auto it = sKeyboardStates.find(topLevel);
if (it != sKeyboardStates.end() && it->second.valid) {
memcpy(outKeyState, it->second.keys, 256);
return true;
}
return false;
}
void InputFilter::SetSingleKeyState(HWND hwnd, int vkey, bool down, bool toggled) {
if (vkey < 0 || vkey > 255) return;
HWND topLevel = GetTopLevelWindow(hwnd);
if (!topLevel) topLevel = hwnd;
std::lock_guard<std::mutex> lock(sKeyboardMutex);
KeyboardState& state = sKeyboardStates[topLevel];
// High bit (0x80) = key is down
// Low bit (0x01) = key is toggled (for toggle keys like CapsLock)
BYTE val = 0;
if (down) val |= 0x80;
if (toggled) val |= 0x01;
state.keys[vkey] = val;
state.valid = true;
}
// Mouse button state management
void InputFilter::SetMouseButtonState(HWND hwnd, bool left, bool right, bool middle) {
HWND topLevel = GetTopLevelWindow(hwnd);
if (!topLevel) topLevel = hwnd;
std::lock_guard<std::mutex> lock(sMouseButtonMutex);
MouseButtonState& state = sMouseButtonStates[topLevel];
state.left = left;
state.right = right;
state.middle = middle;
}
WORD InputFilter::GetMouseButtonState(HWND hwnd) {
HWND topLevel = GetTopLevelWindow(hwnd);
if (!topLevel) topLevel = hwnd;
std::lock_guard<std::mutex> lock(sMouseButtonMutex);
auto it = sMouseButtonStates.find(topLevel);
if (it == sMouseButtonStates.end()) {
return 0;
}
WORD flags = 0;
if (it->second.left) flags |= MK_LBUTTON;
if (it->second.right) flags |= MK_RBUTTON;
if (it->second.middle) flags |= MK_MBUTTON;
return flags;
}
// Thread-local current window for KeyboardLayout to query
static thread_local HWND sCurrentProcessingWindow = nullptr;
void InputFilter::SetCurrentWindow(HWND hwnd) {
sCurrentProcessingWindow = hwnd;
}
HWND InputFilter::GetCurrentWindow() {
return sCurrentProcessingWindow;
}
void InputFilter::ClearCurrentWindow() {
sCurrentProcessingWindow = nullptr;
}
bool InputFilter::GetCurrentMouseButtons(uint16_t* outButtons) {
HWND hwnd = sCurrentProcessingWindow;
if (!hwnd || !outButtons) {
return false;
}
if (!IsEnabledForWindow(hwnd)) {
return false; // Use native GetKeyState
}
// Get button state from our tracked state
WORD flags = GetMouseButtonState(hwnd);
*outButtons = 0;
// Convert MK_* flags to MouseButtonsFlag values
// MouseButtonsFlag::ePrimaryFlag = 1, eSecondaryFlag = 2, eMiddleFlag = 4
if (flags & MK_LBUTTON) *outButtons |= 1; // ePrimaryFlag
if (flags & MK_RBUTTON) *outButtons |= 2; // eSecondaryFlag
if (flags & MK_MBUTTON) *outButtons |= 4; // eMiddleFlag
return true;
}
} // namespace widget
} // namespace mozilla