Forward each subclassed window to its own original WndProc

This commit is contained in:
cdozdil committed 2026-11-11 01:26:45 +03:00
1 parent 75d6d8b2df
commit ff5cd97dcd
3 files changed
+17 -2

No files matched your search

@@ -11,6 +11,7 @@
#include <mutex>
#include <optional>
#include <string>
#include <unordered_map>
#include <winerror.h>
#include <Xinput.h>
@@ -128,6 +129,9 @@ struct InputState
HWND InputRootHwnd = nullptr;
WNDPROC OriginalWndProc = nullptr;
// Each subclassed window's own original, another WndProc on top of ours can keep calling us for a window we
// no longer track
std::unordered_map<HWND, WNDPROC> OriginalWndProcs;
bool Initialized = false;
bool HooksInstalled = false;
@@ -707,6 +707,13 @@ void ClearTransientState()
_state.LastMouseClientPos = _state.MouseClientPos;
}
// The window's own original, not the current input window's, they differ once the input window changed
static WNDPROC OriginalWndProcLocked(HWND hwnd)
{
const auto it = _state.OriginalWndProcs.find(hwnd);
return it != _state.OriginalWndProcs.end() ? it->second : nullptr;
}
LRESULT CALLBACK OptiInputWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
if (msg == WM_NULL)
@@ -715,7 +722,7 @@ LRESULT CALLBACK OptiInputWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lPa
{
std::unique_lock lock(_state.Mutex);
originalWndProc = _state.OriginalWndProc;
originalWndProc = OriginalWndProcLocked(hwnd);
}
if (originalWndProc != nullptr)
@@ -731,7 +738,7 @@ LRESULT CALLBACK OptiInputWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lPa
{
std::unique_lock lock(_state.Mutex);
originalWndProc = _state.OriginalWndProc;
originalWndProc = OriginalWndProcLocked(hwnd);
OPTIINPUT_LOG_VERBOSE("WndProc dispatch hwnd:{} msg:{}({:#x}) input:{} original:{} menu:{} focused:{}",
static_cast<void*>(hwnd), WindowMessageName(msg), static_cast<unsigned>(msg),
static_cast<void*>(_state.InputHwnd), reinterpret_cast<std::uintptr_t>(originalWndProc),
@@ -398,6 +398,7 @@ bool InstallWindowSubclass(HWND hwnd)
}
_state.OriginalWndProc = reinterpret_cast<WNDPROC>(previous);
_state.OriginalWndProcs[hwnd] = _state.OriginalWndProc;
_state.WndProcSubclassed = true;
LOG_INFO("subclass installed hwnd:{} previousWndProc:{} optiWndProc:{}", static_cast<void*>(hwnd),
@@ -513,6 +514,9 @@ bool RemoveWindowSubclass(bool preserveLostChain)
GetLastError());
return false;
}
// Restored, nothing chains through us for it anymore
_state.OriginalWndProcs.erase(_state.InputHwnd);
}
else
{