-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathWinEventObserver.cpp
319 lines (265 loc) · 10.2 KB
/
WinEventObserver.cpp
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
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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 <windows.h>
#include <winternl.h>
#include <winuser.h>
#include <wtsapi32.h>
#include <dbt.h>
#include "WinEventObserver.h"
#include "InputDeviceUtils.h"
#include "ScreenHelperWin.h"
#include "WindowsUIUtils.h"
#include "WinWindowOcclusionTracker.h"
#include "gfxDWriteFonts.h"
#include "gfxPlatform.h"
#include "mozilla/Assertions.h"
#include "mozilla/ClearOnShutdown.h"
#include "mozilla/Logging.h"
#include "mozilla/LookAndFeel.h"
#include "mozilla/WindowsVersion.h"
#include "nsLookAndFeel.h"
#include "nsStringFwd.h"
#include "nsWindowDbg.h"
#include "nsdefs.h"
#include "nsXULAppAPI.h"
// borrowed from devblogs.microsoft.com/oldnewthing/20041025-00/?p=37483, by way
// of the Chromium sandboxing code's "current_module.h"
extern "C" IMAGE_DOS_HEADER __ImageBase;
#define CURRENT_MODULE() reinterpret_cast<HMODULE>(&__ImageBase)
namespace mozilla::widget {
LazyLogModule gWinEventWindowLog("WinEventWindow");
#define OBS_LOG(...) \
MOZ_LOG(gWinEventWindowLog, ::mozilla::LogLevel::Info, (__VA_ARGS__))
namespace {
namespace evtwin_details {
static HWND sHiddenWindow = nullptr;
static bool sHiddenWindowShutdown = false;
HDEVNOTIFY sDeviceNotifyHandle = nullptr;
} // namespace evtwin_details
} // namespace
/* static */
void WinEventWindow::Ensure() {
MOZ_RELEASE_ASSERT(XRE_IsParentProcess());
MOZ_RELEASE_ASSERT(NS_IsMainThread());
using namespace evtwin_details;
if (sHiddenWindow) return;
if (sHiddenWindowShutdown) return;
HMODULE const hSelf = CURRENT_MODULE();
WNDCLASSW const wc = {.lpfnWndProc = WinEventWindow::WndProc,
.hInstance = hSelf,
.lpszClassName = kClassNameHidden};
ATOM const atom = ::RegisterClassW(&wc);
if (!atom) {
// This is known to be possible when the atom table no longer has free
// entries, which unfortunately happens more often than one might expect.
// See bug 1571516.
auto volatile const err [[maybe_unused]] = ::GetLastError();
MOZ_CRASH("could not register broadcast-receiver window-class");
}
sHiddenWindow =
::CreateWindowW((LPCWSTR)(uintptr_t)atom, L"WinEventWindow", 0, 0, 0, 0,
0, nullptr, nullptr, hSelf, nullptr);
if (!sHiddenWindow) {
MOZ_CRASH("could not create broadcast-receiver window");
}
sDeviceNotifyHandle = InputDeviceUtils::RegisterNotification(sHiddenWindow);
// It should be harmless to leak this window until destruction -- but other
// parts of Gecko may expect all windows to be destroyed, so do that.
mozilla::RunOnShutdown([]() {
InputDeviceUtils::UnregisterNotification(sDeviceNotifyHandle);
sHiddenWindowShutdown = true;
::DestroyWindow(sHiddenWindow);
sHiddenWindow = nullptr;
});
};
/* static */
HWND WinEventWindow::GetHwndForTestingOnly() {
return evtwin_details::sHiddenWindow;
}
// Callbacks for individual event types. These are private and internal
// implementation details of WinEventWindow.
namespace {
namespace evtwin_details {
static void NotifyThemeChanged(ThemeChangeKind aKind) {
LookAndFeel::NotifyChangedAllWindows(aKind);
}
static void OnSessionChange(WPARAM wParam, LPARAM lParam) {
if (wParam == WTS_SESSION_LOCK || wParam == WTS_SESSION_UNLOCK) {
DWORD currentSessionId;
BOOL const rv =
::ProcessIdToSessionId(::GetCurrentProcessId(), ¤tSessionId);
if (!rv) {
// A process should always have the relevant access privileges for itself,
// but the above call could still fail if, e.g., someone's playing games
// with function imports. If so, just assert and/or skip out.
//
// Should this turn out to somehow be a real concern, we could do
// ```
// DWORD const currentSessionId =
// ::NtCurrentTeb()->ProcessEnvironmentBlock->SessionId;
// ```
// instead, which is actually documented (albeit abjured against).
MOZ_ASSERT(false, "::ProcessIdToSessionId() failed");
return;
}
OBS_LOG("WinEventWindow OnSessionChange(): wParam=%zu lParam=%" PRIdLPTR
" currentSessionId=%lu",
wParam, lParam, currentSessionId);
// Ignore lock/unlock messages for other sessions -- which Windows actually
// _does_ send in some scenarios; see review of Chromium changeset 1929489:
//
// https://chromium-review.googlesource.com/c/chromium/src/+/1929489
if (currentSessionId != (DWORD)lParam) {
return;
}
if (auto* wwot = WinWindowOcclusionTracker::Get()) {
wwot->OnSessionChange(wParam);
}
}
}
static void OnPowerBroadcast(WPARAM wParam, LPARAM lParam) {
if (wParam == PBT_POWERSETTINGCHANGE) {
POWERBROADCAST_SETTING* setting = (POWERBROADCAST_SETTING*)lParam;
MOZ_ASSERT(setting);
if (::IsEqualGUID(setting->PowerSetting, GUID_SESSION_DISPLAY_STATUS) &&
setting->DataLength == sizeof(DWORD)) {
MONITOR_DISPLAY_STATE state{};
errno_t const err =
::memcpy_s(&state, sizeof(state), setting->Data, setting->DataLength);
if (err) {
MOZ_ASSERT(false, "bad data in POWERBROADCAST_SETTING in lParam");
return;
}
bool const displayOn = MONITOR_DISPLAY_STATE::PowerMonitorOff != state;
OBS_LOG("WinEventWindow OnPowerBroadcast(): displayOn=%d",
int(displayOn ? 1 : 0));
if (auto* wwot = WinWindowOcclusionTracker::Get()) {
wwot->OnDisplayStateChanged(displayOn);
}
}
}
}
static void OnSettingsChange(WPARAM wParam, LPARAM lParam) {
switch (wParam) {
case SPI_SETCLIENTAREAANIMATION:
case SPI_SETKEYBOARDDELAY:
case SPI_SETMOUSEVANISH:
case MOZ_SPI_SETCURSORSIZE:
// These need to update LookAndFeel cached values.
//
// They affect reduced motion settings / caret blink count / show pointer
// while typing / tooltip offset, so no need to invalidate style / layout.
NotifyThemeChanged(widget::ThemeChangeKind::MediaQueriesOnly);
return;
case SPI_SETFONTSMOOTHING:
case SPI_SETFONTSMOOTHINGTYPE:
gfxDWriteFont::UpdateSystemTextVars();
return;
case SPI_SETWORKAREA:
// NB: We also refresh screens on WM_DISPLAYCHANGE, but the rcWork
// values are sometimes wrong at that point. This message then arrives
// soon afterward, when we can get the right rcWork values.
ScreenHelperWin::RefreshScreens();
return;
default:
break;
}
if (lParam == 0) {
return;
}
nsDependentString lParamString{reinterpret_cast<const wchar_t*>(lParam)};
if (lParamString == u"ImmersiveColorSet"_ns) {
// This affects system colors (-moz-win-accentcolor), so gotta pass the
// style flag.
NotifyThemeChanged(widget::ThemeChangeKind::Style);
return;
}
// UserInteractionMode, ConvertibleSlateMode, and SystemDockMode may cause
// @media(pointer) queries to change, which layout needs to know about.
//
// The former two of those also imply that the current tablet-mode state needs
// to be updated.
if (lParamString == u"UserInteractionMode"_ns) {
// Documentation implies, and testing shows, that this is seen on Win10
// only.
Unused << NS_WARN_IF(mozilla::IsWin11OrLater());
WindowsUIUtils::UpdateInWin10TabletMode();
NotifyThemeChanged(widget::ThemeChangeKind::MediaQueriesOnly);
return;
}
if (lParamString == u"ConvertibleSlateMode"_ns) {
// Documentation implies, and testing shows, that this is not seen on Win10.
Unused << NS_WARN_IF(!mozilla::IsWin11OrLater());
WindowsUIUtils::UpdateInWin11TabletMode();
NotifyThemeChanged(widget::ThemeChangeKind::MediaQueriesOnly);
return;
}
if (lParamString == u"SystemDockMode"_ns) {
NotifyThemeChanged(widget::ThemeChangeKind::MediaQueriesOnly);
return;
}
}
static void OnDeviceChange(WPARAM wParam, LPARAM lParam) {
if (wParam == DBT_DEVICEARRIVAL || wParam == DBT_DEVICEREMOVECOMPLETE) {
DEV_BROADCAST_HDR* hdr = reinterpret_cast<DEV_BROADCAST_HDR*>(lParam);
// Check dbch_devicetype explicitly since we will get other device types
// (e.g. DBT_DEVTYP_VOLUME) for some reason, even if we specify
// DBT_DEVTYP_DEVICEINTERFACE in the filter for RegisterDeviceNotification.
if (hdr->dbch_devicetype == DBT_DEVTYP_DEVICEINTERFACE) {
// This can only change media queries (any-hover/any-pointer).
NotifyThemeChanged(widget::ThemeChangeKind::MediaQueriesOnly);
}
}
}
} // namespace evtwin_details
} // namespace
// static
LRESULT CALLBACK WinEventWindow::WndProc(HWND hwnd, UINT msg, WPARAM wParam,
LPARAM lParam) {
NativeEventLogger eventLogger("WinEventWindow", hwnd, msg, wParam, lParam);
switch (msg) {
case WM_WINDOWPOSCHANGING: {
// prevent rude external programs from making hidden window visible
LPWINDOWPOS info = (LPWINDOWPOS)lParam;
info->flags &= ~SWP_SHOWWINDOW;
} break;
case WM_WTSSESSION_CHANGE: {
evtwin_details::OnSessionChange(wParam, lParam);
} break;
case WM_POWERBROADCAST: {
evtwin_details::OnPowerBroadcast(wParam, lParam);
} break;
case WM_SYSCOLORCHANGE: {
// No need to invalidate layout for system color changes, but we need to
// invalidate style.
evtwin_details::NotifyThemeChanged(widget::ThemeChangeKind::Style);
} break;
case WM_THEMECHANGED: {
// We assume pretty much everything could've changed here.
evtwin_details::NotifyThemeChanged(
widget::ThemeChangeKind::StyleAndLayout);
} break;
case WM_FONTCHANGE: {
// update the global font list
gfxPlatform::GetPlatform()->UpdateFontList();
} break;
case WM_SETTINGCHANGE: {
evtwin_details::OnSettingsChange(wParam, lParam);
} break;
case WM_DEVICECHANGE: {
evtwin_details::OnDeviceChange(wParam, lParam);
} break;
case WM_DISPLAYCHANGE: {
ScreenHelperWin::RefreshScreens();
break;
}
}
LRESULT const ret = ::DefWindowProcW(hwnd, msg, wParam, lParam);
eventLogger.SetResult(ret, false);
return ret;
}
#undef OBS_LOG
} // namespace mozilla::widget