Skip to content

Commit

Permalink
Use ShowWindowAsync whenever possible to avoid problems with SendMess…
Browse files Browse the repository at this point in the history
…age (...) deadlocks
  • Loading branch information
Kaldaien committed Dec 15, 2024
1 parent baeee78 commit 28ad2a9
Show file tree
Hide file tree
Showing 6 changed files with 316 additions and 214 deletions.
65 changes: 64 additions & 1 deletion include/SpecialK/window.h
Original file line number Diff line number Diff line change
Expand Up @@ -852,7 +852,68 @@ bool
EqualRect (&rectGame, &rectIntersect);
};

BOOL WINAPI SK_ShowWindow (HWND hWnd, int nCmdShow);
using GetWindowInfo_pfn = BOOL (WINAPI *)(HWND, PWINDOWINFO);
using GetDpiForSystem_pfn = UINT (WINAPI *)(void);
using GetDpiForWindow_pfn = UINT (WINAPI *)(HWND hwnd);
using EnableNonClientDpiScaling_pfn = BOOL (WINAPI *)(HWND hwnd);
using GetSystemDpiForProcess_pfn = UINT (WINAPI *)(HANDLE hProcess);
using GetSystemMetricsForDpi_pfn = int (WINAPI *)(int nIndex,
UINT dpi);
using SystemParametersInfoForDpi_pfn = BOOL (WINAPI *)(UINT uiAction,
UINT uiParam,
PVOID pvParam,
UINT fWinIni,
UINT dpi);
using SetThreadDpiHostingBehavior_pfn =
DPI_HOSTING_BEHAVIOR (WINAPI *)(DPI_HOSTING_BEHAVIOR value);
using SetThreadDpiAwarenessContext_pfn =
DPI_AWARENESS_CONTEXT (WINAPI *)(DPI_AWARENESS_CONTEXT dpiContext);
using GetThreadDpiAwarenessContext_pfn =
DPI_AWARENESS_CONTEXT (WINAPI *)(void);
using GetAwarenessFromDpiAwarenessContext_pfn =
DPI_AWARENESS (WINAPI *)(DPI_AWARENESS_CONTEXT value);
using SetProcessDpiAwarenessContext_pfn = BOOL (WINAPI *)(DPI_AWARENESS_CONTEXT value);

using AdjustWindowRectExForDpi_pfn = BOOL (WINAPI *)(LPRECT lpRect,
DWORD dwStyle,
BOOL bMenu,
DWORD dwExStyle,
UINT dpi);

using GetFocus_pfn = HWND (WINAPI *)(void);
using SetWindowDisplayAffinity_pfn = BOOL (WINAPI *)(HWND,DWORD);
using GetGUIThreadInfo_pfn = BOOL (WINAPI *)(DWORD,PGUITHREADINFO);
using GetActiveWindow_pfn = HWND (WINAPI *)(void);
using SetActiveWindow_pfn = HWND (WINAPI *)(HWND);
using GetForegroundWindow_pfn = HWND (WINAPI *)(void);
using BringWindowToTop_pfn = BOOL (WINAPI *)(HWND);
using SetForegroundWindow_pfn = BOOL (WINAPI *)(HWND);
using SetWindowsHookEx_pfn = HHOOK (WINAPI *)(int, HOOKPROC, HINSTANCE, DWORD);
using UnhookWindowsHookEx_pfn = BOOL (WINAPI *)(HHOOK);

using TranslateMessage_pfn = BOOL (WINAPI *)(_In_ const MSG *lpMsg);
using DispatchMessage_pfn = LRESULT (WINAPI *)(_In_ const MSG *lpmsg);
using GetMessage_pfn = BOOL (WINAPI *)(_Out_ LPMSG lpMsg,
_In_opt_ HWND hWnd,
_In_ UINT wMsgFilterMin,
_In_ UINT wMsgFilterMax);
using PeekMessage_pfn = BOOL (WINAPI *)(_Out_ LPMSG lpMsg,
_In_opt_ HWND hWnd,
_In_ UINT wMsgFilterMin,
_In_ UINT wMsgFilterMax,
_In_ UINT wRemoveMsg);

using SendOrPostMessage_pfn = BOOL (WINAPI *)(HWND,UINT,WPARAM,LPARAM);

BOOL
WINAPI
SK_PostMessage ( _In_opt_ HWND hWnd,
_In_ UINT Msg,
_In_ WPARAM wParam,
_In_ LPARAM lParam );

BOOL WINAPI SK_ShowWindow (HWND hWnd, int nCmdShow);
BOOL WINAPI SK_ShowWindowAsync (HWND hWnd, int nCmdShow);

bool SK_Window_HasBorder (HWND hWnd = game_window.hWnd);
void SK_Window_RemoveBorders (void);
Expand Down Expand Up @@ -881,4 +942,6 @@ RECT SK_Input_RestoreClipRect (void);

LRESULT WINAPI SK_COMPAT_SafeCallProc (sk_window_s* pWin, HWND hWnd_, UINT Msg, WPARAM wParam, LPARAM lParam);

void SK_Window_UninitHooks (void);

#endif /* __SK__WINDOW_H__ */
48 changes: 27 additions & 21 deletions include/imgui/imgui_user.inl
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#define SK_ALLOW_EXPERIMENTAL_WINDOW_MANAGEMENT

#include <mmsystem.h>
#include <Windows.h>

Expand Down Expand Up @@ -1909,12 +1911,12 @@ SK_ImGui_PollGamepad_EndFrame (XINPUT_STATE* pState)
SK_GetForegroundWindow ();

auto show_cmd =
IsMinimized (game_window.hWnd) ? SW_SHOWNORMAL
: SW_SHOW;
IsIconic (game_window.hWnd) ? SW_SHOWNORMAL
: SW_SHOW;

ShowWindow (game_window.hWnd, show_cmd);
SK_ShowWindowAsync (game_window.hWnd, show_cmd);
SK_RealizeForegroundWindow (game_window.hWnd);
ShowWindow (game_window.hWnd, show_cmd);
SK_ShowWindowAsync (game_window.hWnd, show_cmd);
}

bChordActivated = true;
Expand All @@ -1936,12 +1938,12 @@ SK_ImGui_PollGamepad_EndFrame (XINPUT_STATE* pState)
IsWindow (hWndLastApp))
{
auto show_cmd =
IsMinimized (hWndLastApp) ? SW_SHOWNORMAL
: SW_SHOW;
IsIconic (hWndLastApp) ? SW_SHOWNORMAL
: SW_SHOW;

SK_ShowWindow (hWndLastApp, show_cmd);
SK_ShowWindowAsync (hWndLastApp, show_cmd);
SK_RealizeForegroundWindow (hWndLastApp);
SK_ShowWindow (hWndLastApp, show_cmd);
SK_ShowWindowAsync (hWndLastApp, show_cmd);
}

bChordActivated = true;
Expand Down Expand Up @@ -2192,8 +2194,8 @@ SK_ImGui_PollGamepad_EndFrame (XINPUT_STATE* pState)
( state.Gamepad.bLeftTrigger > XINPUT_GAMEPAD_TRIGGER_THRESHOLD) &&
(last_state.Gamepad.bLeftTrigger <= XINPUT_GAMEPAD_TRIGGER_THRESHOLD))
{
if ( IsIconic (game_window.hWnd))
SK_ShowWindow (game_window.hWnd, SW_SHOWNOACTIVATE);
if ( IsIconic (game_window.hWnd))
SK_ShowWindowAsync (game_window.hWnd, SW_SHOWNOACTIVATE);

bChordActivated = true;
}
Expand All @@ -2202,8 +2204,8 @@ SK_ImGui_PollGamepad_EndFrame (XINPUT_STATE* pState)
( state.Gamepad.bRightTrigger > XINPUT_GAMEPAD_TRIGGER_THRESHOLD) &&
(last_state.Gamepad.bRightTrigger <= XINPUT_GAMEPAD_TRIGGER_THRESHOLD))
{
if (! IsIconic (game_window.hWnd))
SK_ShowWindow (game_window.hWnd, SW_MINIMIZE);
if (! IsIconic (game_window.hWnd))
SK_ShowWindowAsync (game_window.hWnd, SW_MINIMIZE);

bChordActivated = true;
}
Expand Down Expand Up @@ -3615,20 +3617,24 @@ SK_ImGui_User_NewFrame (void)
static bool last_up = io.KeysDown [VK_UP];
if ( (io.KeysDown [VK_LWIN] || io.KeysDown [VK_RWIN]) && io.KeysDown [VK_DOWN] && !last_down && !SK_Window_HasBorder (game_window.hWnd))
{
if (! IsIconic (game_window.hWnd))
if (! IsIconic (game_window.hWnd))
{
//if (IsMaximized (game_window.hWnd))
// SK_ShowWindow (game_window.hWnd, SW_RESTORE);
//else
SK_ShowWindow (game_window.hWnd, SW_MINIMIZE);
#ifdef SK_ALLOW_EXPERIMENTAL_WINDOW_MANAGEMENT
if (IsZoomed (game_window.hWnd))
SK_ShowWindowAsync (game_window.hWnd, SW_RESTORE);
else
#endif
SK_ShowWindowAsync (game_window.hWnd, SW_MINIMIZE);
}
}
else if ((io.KeysDown [VK_LWIN] || io.KeysDown [VK_RWIN]) && io.KeysDown [VK_UP] && !last_up && !SK_Window_HasBorder (game_window.hWnd))
{
if (IsIconic (game_window.hWnd))
SK_ShowWindow (game_window.hWnd, SW_SHOWNOACTIVATE);
//else
// ShowWindow (game_window.hWnd, SW_MAXIMIZE); // This causes some games to break due to implicit activation
if (IsIconic (game_window.hWnd))
SK_ShowWindowAsync (game_window.hWnd, SW_SHOWNOACTIVATE);
#ifdef SK_ALLOW_EXPERIMENTAL_WINDOW_MANAGEMENT
else
SK_ShowWindowAsync (game_window.hWnd, SW_MAXIMIZE); // This causes some games to break due to implicit activation
#endif
}
last_down = io.KeysDown [VK_DOWN];
last_up = io.KeysDown [VK_DOWN];
Expand Down
32 changes: 14 additions & 18 deletions src/core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2566,7 +2566,7 @@ SK_Win32_CreateDummyWindow (HWND hWndParent)
{
HWND hWnd = (HWND)user;

SK_ShowWindow (hWnd, SW_HIDE);
SK_ShowWindowAsync (hWnd, SW_HIDE);

MSG msg = { };
while (SK_GetMessageW (&msg, 0, 0, 0))
Expand Down Expand Up @@ -2872,9 +2872,9 @@ constexpr UINT WM_SKIF_EVENT_SIGNAL = WM_USER + 0x3000;
SK_Inject_SetFocusWindow (hWndExisting);

if ( IsIconic (hWndExisting))
SK_ShowWindow (hWndExisting, SW_RESTORE);
SK_ShowWindowAsync (hWndExisting, SW_RESTORE);
else
SK_ShowWindow (hWndExisting, SW_SHOW);
SK_ShowWindowAsync (hWndExisting, SW_SHOW);
}
}
}
Expand Down Expand Up @@ -3448,12 +3448,12 @@ SK_FrameCallback ( SK_RenderBackend& rb,
}

// Activate the window
//if (IsMinimized ( game_window.hWnd))
// ShowWindowAsync (game_window.hWnd, SW_SHOWNORMAL);
if (IsIconic (game_window.hWnd))
ShowWindowAsync (game_window.hWnd, SW_RESTORE);
//if (IsIconic (game_window.hWnd))
// SK_ShowWindowAsync (game_window.hWnd, SW_SHOWNORMAL);
if (IsIconic (game_window.hWnd))
SK_ShowWindowAsync (game_window.hWnd, SW_RESTORE);
else
ShowWindowAsync (game_window.hWnd, SW_SHOW);
SK_ShowWindowAsync (game_window.hWnd, SW_SHOW);

if (! SetForegroundWindow (game_window.hWnd))
SK_RealizeForegroundWindow (game_window.hWnd);
Expand Down Expand Up @@ -3802,21 +3802,17 @@ SK_BackgroundRender_EndFrame (void)
& ~WS_EX_TOOLWINDOW );
// And remove one that prevents taskbar activation...

if (IsMinimized (hWndGame))
ShowWindowAsync ( hWndGame,
SW_SHOWNORMAL );
else
ShowWindowAsync ( hWndGame,
SW_SHOW );
if (IsIconic ( hWndGame ))
ShowWindowAsync ( hWndGame, SW_SHOWNORMAL );
else ShowWindowAsync ( hWndGame, SW_SHOW );

if (lpStyleExNew != lpStyleEx)
{
SK_SetWindowLongPtrW ( hWndGame,
GWL_EXSTYLE,
lpStyleExNew );
SK_SetWindowLongPtrW ( hWndGame, GWL_EXSTYLE,
lpStyleExNew );
}

SK_RealizeForegroundWindow ( hWndGame );
SK_RealizeForegroundWindow ( hWndGame );
}
}

Expand Down
9 changes: 6 additions & 3 deletions src/input/windows.gaming.input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1005,8 +1005,10 @@ RoGetActivationFactory_Detour ( _In_ HSTRING activatableClassId,
{
if ((! config.input.gamepad.xinput.emulate) && SK_GetCurrentGameID () != SK_GAME_ID::ForzaHorizon5 &&
SK_GetCurrentGameID () != SK_GAME_ID::StarWarsOutlaws &&
(! SK_GetCurrentRenderBackend ().windows.sdl) &&
(! SK_GetCurrentRenderBackend ().windows.nixxes))
(! SK_GetCurrentRenderBackend ().windows.sdl) &&
(! SK_GetCurrentRenderBackend ().windows.nixxes) &&
(! SK_GetCurrentRenderBackend ().windows.unity) &&
(! config.input.gamepad.xinput.blackout_api))
{
SK_RunOnce (SK_Thread_CreateEx([](LPVOID)->DWORD
{
Expand All @@ -1016,7 +1018,8 @@ RoGetActivationFactory_Detour ( _In_ HSTRING activatableClassId,
}

if ((! SK_GetCurrentRenderBackend ().windows.sdl) &&
(! SK_GetCurrentRenderBackend ().windows.nixxes))
(! SK_GetCurrentRenderBackend ().windows.nixxes) &&
(! SK_GetCurrentRenderBackend ().windows.unity))
{
SK_ImGui_CreateNotification ( "WindowsGamingInput.Compatibility",
SK_ImGui_Toast::Warning,
Expand Down
2 changes: 1 addition & 1 deletion src/render/dxgi/dxgi_swapchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1628,7 +1628,7 @@ SK_DXGI_SwapChain_SetFullscreenState_Impl (
pSwapChain1 (pSwapChain);
pSwapChain1->GetHwnd (&hWnd);
SK_RealizeForegroundWindow ( hWnd);
SK_ShowWindow (hWnd, SW_SHOW);
SK_ShowWindowAsync (hWnd, SW_SHOW);
}
}

Expand Down
Loading

0 comments on commit 28ad2a9

Please sign in to comment.