diff --git a/DDrawCompat/DDraw/ActivateAppHandler.cpp b/DDrawCompat/DDraw/ActivateAppHandler.cpp index b941b40..5e53a7c 100644 --- a/DDrawCompat/DDraw/ActivateAppHandler.cpp +++ b/DDrawCompat/DDraw/ActivateAppHandler.cpp @@ -1,3 +1,5 @@ +#include + #include #include "Common/Hook.h" @@ -12,6 +14,8 @@ namespace { Win32::FontSmoothing::SystemSettings g_fontSmoothingSettings = {}; WNDPROC g_origDdWndProc = nullptr; + std::atomic g_activateAppThreadId = 0; + HWND g_delayedFocusWnd = nullptr; void activateApp() { @@ -43,7 +47,16 @@ namespace { deactivateApp(); } + + g_activateAppThreadId = GetCurrentThreadId(); + g_delayedFocusWnd = nullptr; LRESULT result = g_origDdWndProc(hwnd, uMsg, wParam, lParam); + g_activateAppThreadId = 0; + if (g_delayedFocusWnd) + { + CALL_ORIG_FUNC(SetFocus)(g_delayedFocusWnd); + } + isDisplayChangeNotificationEnabled = true; DDraw::RealPrimarySurface::enableUpdates(); return LOG_RESULT(result); @@ -62,12 +75,39 @@ namespace return LOG_RESULT(g_origDdWndProc(hwnd, uMsg, wParam, lParam)); } + + HWND WINAPI setFocus(HWND hWnd) + { + if (GetCurrentThreadId() == g_activateAppThreadId && IsWindow(hWnd)) + { + g_delayedFocusWnd = hWnd; + return GetFocus(); + } + return CALL_ORIG_FUNC(SetFocus)(hWnd); + } + + BOOL WINAPI showWindow(HWND hWnd, int nCmdShow) + { + if (GetCurrentThreadId() == g_activateAppThreadId && IsWindow(hWnd)) + { + BOOL result = IsWindowVisible(hWnd); + ShowWindowAsync(hWnd, nCmdShow); + return result; + } + return CALL_ORIG_FUNC(ShowWindow)(hWnd, nCmdShow); + } } namespace DDraw { namespace ActivateAppHandler { + void installHooks() + { + HOOK_FUNCTION(user32, SetFocus, setFocus); + HOOK_FUNCTION(user32, ShowWindow, showWindow); + } + void setCooperativeLevel(HWND hwnd, DWORD flags) { static bool isDdWndProcHooked = false; diff --git a/DDrawCompat/DDraw/ActivateAppHandler.h b/DDrawCompat/DDraw/ActivateAppHandler.h index df0ce9f..96aa995 100644 --- a/DDrawCompat/DDraw/ActivateAppHandler.h +++ b/DDrawCompat/DDraw/ActivateAppHandler.h @@ -8,6 +8,7 @@ namespace DDraw { namespace ActivateAppHandler { + void installHooks(); void setCooperativeLevel(HWND hwnd, DWORD flags); } } diff --git a/DDrawCompat/DDraw/Hooks.cpp b/DDrawCompat/DDraw/Hooks.cpp index 3b9f873..60f02cb 100644 --- a/DDrawCompat/DDraw/Hooks.cpp +++ b/DDrawCompat/DDraw/Hooks.cpp @@ -103,6 +103,7 @@ namespace DDraw void installHooks() { RealPrimarySurface::init(); + ActivateAppHandler::installHooks(); Win32::Registry::unsetValue( HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\DirectDraw", "EmulationOnly");