Skip to content

Commit

Permalink
Filter wnd messages in window mode
Browse files Browse the repository at this point in the history
  • Loading branch information
elishacloud committed Sep 30, 2023
1 parent cf7313e commit 3abc22d
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Dllmain/BuildNo.rc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
#define BUILD_NUMBER 6850
#define BUILD_NUMBER 6851
77 changes: 77 additions & 0 deletions Utils/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,15 @@ namespace Utils
FARPROC pGetModuleFileNameW = nullptr;
FARPROC p_CreateProcessA = nullptr;
FARPROC p_CreateProcessW = nullptr;
WNDPROC OriginalWndProc = nullptr;
std::vector<type_dll> custom_dll; // Used for custom dll's and asi plugins

// Function declarations
DWORD_PTR GetProcessMask();
void InitializeASI(HMODULE hModule);
void FindFiles(WIN32_FIND_DATA*);
void *memmem(const void *l, size_t l_len, const void *s, size_t s_len);
LRESULT CALLBACK WndProcFilter(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
}

// Execute a specified string
Expand Down Expand Up @@ -926,3 +928,78 @@ DWORD Utils::GetVideoRam(UINT AdapterNo)

return retSize;
}
bool Utils::SetWndProcFilter(HWND hWnd)
{
// Check window handle
if (!IsWindow(hWnd))
{
Logging::Log() << __FUNCTION__ << " Error: hWnd invalid!";
return false;
}

// Check if WndProc is already overloaded
if (OriginalWndProc)
{
Logging::Log() << __FUNCTION__ << " Error: WndProc already overloaded!";
return false;
}

LOG_LIMIT(3, __FUNCTION__ << " Setting new WndProc " << hWnd);

// Store existing WndProc
OriginalWndProc = (WNDPROC)GetWindowLong(hWnd, GWL_WNDPROC);

// Set new WndProc
if (!OriginalWndProc || !SetWindowLong(hWnd, GWL_WNDPROC, (LONG)WndProcFilter))
{
Logging::Log() << __FUNCTION__ << " Failed to overload WndProc!";
OriginalWndProc = nullptr;
return false;
}

return true;
}

bool Utils::RestoreWndProcFilter(HWND hWnd)
{
// Check window handle
if (!IsWindow(hWnd))
{
Logging::Log() << __FUNCTION__ << " Error: hWnd invalid!";
return false;
}

// Check if WndProc is overloaded
if (!OriginalWndProc)
{
Logging::Log() << __FUNCTION__ << " Error: WndProc is not yet overloaded!";
return false;
}

// Get current WndProc
WNDPROC CurrentWndProc = (WNDPROC)GetWindowLong(hWnd, GWL_WNDPROC);

// Check if WndProc is overloaded
if (CurrentWndProc != WndProcFilter)
{
Logging::Log() << __FUNCTION__ << " Error: WndProc does not match!";
return false;
}

// Resetting WndProc
if (!SetWindowLong(hWnd, GWL_WNDPROC, (LONG)OriginalWndProc))
{
Logging::Log() << __FUNCTION__ << " Failed to reset WndProc";
return false;
}

OriginalWndProc = nullptr;
return true;
}

LRESULT CALLBACK Utils::WndProcFilter(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
Logging::LogDebug() << __FUNCTION__ << " " << Logging::hex(uMsg);

return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
2 changes: 2 additions & 0 deletions Utils/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ namespace Utils
DWORD GetBitCount(HWND hWnd);
DWORD GetWindowHeight(HWND hWnd);
void DisableGameUX();
bool SetWndProcFilter(HWND hWnd);
bool RestoreWndProcFilter(HWND hWnd);
void GetScreenSize(HWND hwnd, LONG &screenWidth, LONG &screenHeight);
void GetScreenSize(HWND hwnd, DWORD &screenWidth, DWORD &screenHeight);
void GetDesktopRect(HWND hWnd, RECT& screenRect);
Expand Down
23 changes: 16 additions & 7 deletions d3d9/IDirect3D9Ex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,12 @@ void UpdatePresentParameter(D3DPRESENT_PARAMETERS* pPresentationParameters, HWND
{
bool AnyChange = (LastBufferWidth != DeviceDetails.BufferWidth || LastBufferHeight != DeviceDetails.BufferHeight || LastDeviceWindow != DeviceDetails.DeviceWindow);

// Overload WndProc
if (Config.EnableWindowMode)
{
Utils::SetWndProcFilter(DeviceDetails.DeviceWindow);
}

// Adjust window
RECT Rect;
GetClientRect(DeviceDetails.DeviceWindow, &Rect);
Expand All @@ -512,7 +518,7 @@ void UpdatePresentParameter(D3DPRESENT_PARAMETERS* pPresentationParameters, HWND
}

// Set fullscreen resolution
if (Config.FullscreenWindowMode && AnyChange)
if (AnyChange && Config.FullscreenWindowMode)
{
// Get monitor info
MONITORINFOEX infoex = {};
Expand All @@ -528,11 +534,17 @@ void UpdatePresentParameter(D3DPRESENT_PARAMETERS* pPresentationParameters, HWND
newSettings.dmPelsHeight = DeviceDetails.BufferHeight;
newSettings.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT;
ChangeDisplaySettingsEx(bRet ? infoex.szDevice : nullptr, &newSettings, nullptr, CDS_FULLSCREEN, nullptr);

// Peek messages to help prevent a "Not Responding" window
Utils::CheckMessageQueue(DeviceDetails.DeviceWindow);
}
}

if (Config.EnableWindowMode)
{
// Resetting WndProc
Utils::RestoreWndProcFilter(DeviceDetails.DeviceWindow);

// Peek messages to help prevent a "Not Responding" window
Utils::CheckMessageQueue(DeviceDetails.DeviceWindow);
}
}
}

Expand Down Expand Up @@ -646,7 +658,4 @@ void AdjustWindow(HWND MainhWnd, LONG displayWidth, LONG displayHeight)
SetWindowPos(MainhWnd, HWND_TOP, xLoc, yLoc, Rect.right, Rect.bottom, SWP_SHOWWINDOW | SWP_NOZORDER);
}
}

// Peek messages to help prevent a "Not Responding" window
Utils::CheckMessageQueue(MainhWnd);
}
2 changes: 1 addition & 1 deletion d3d9/IDirect3DDevice9Ex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1158,7 +1158,7 @@ HRESULT m_IDirect3DDevice9Ex::Clear(DWORD Count, CONST D3DRECT *pRects, DWORD Fl
{
Logging::LogDebug() << __FUNCTION__ << " (" << this << ")";

if (IsWindow(DeviceDetails.DeviceWindow) && (Config.FullscreenWindowMode || Config.EnableWindowMode))
if (IsWindow(DeviceDetails.DeviceWindow) && Config.FullscreenWindowMode)
{
// Peek messages to help prevent a "Not Responding" window
Utils::CheckMessageQueue(DeviceDetails.DeviceWindow);
Expand Down
10 changes: 2 additions & 8 deletions ddraw/IDirectDrawX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1652,7 +1652,7 @@ HRESULT m_IDirectDrawX::SetCooperativeLevel(HWND hWnd, DWORD dwFlags, DWORD Dire
Exclusive.RefreshRate = DisplayMode.RefreshRate;
}
// Set device flags
Device.IsWindowed = (!ExclusiveMode || Config.EnableWindowMode || Config.FullscreenWindowMode);
Device.IsWindowed = (!ExclusiveMode || Config.EnableWindowMode);
Device.AllowModeX = ((dwFlags & DDSCL_ALLOWMODEX) != 0);
Device.MultiThreaded = ((dwFlags & DDSCL_MULTITHREADED) != 0);
// The flag (DDSCL_FPUPRESERVE) is assumed by default in DirectX 6 and earlier.
Expand Down Expand Up @@ -1743,9 +1743,6 @@ HRESULT m_IDirectDrawX::SetCooperativeLevel(HWND hWnd, DWORD dwFlags, DWORD Dire
// Removing WS_CAPTION
SetWindowLong(hWnd, GWL_STYLE, lStyle & ~WS_CAPTION);
SetWindowPos(hWnd, HWND_TOP, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER);

// Peek messages to help prevent a "Not Responding" window
Utils::CheckMessageQueue(hWnd);
}
}

Expand Down Expand Up @@ -2359,7 +2356,7 @@ void m_IDirectDrawX::InitDdraw(DWORD DirectXVersion)
// Mouse hook
static bool EnableMouseHook = Config.DdrawEnableMouseHook &&
((Config.DdrawUseNativeResolution || Config.DdrawOverrideWidth || Config.DdrawOverrideHeight) &&
(!Config.EnableWindowMode || (Config.EnableWindowMode && Config.FullscreenWindowMode)));
(!Config.EnableWindowMode || Config.FullscreenWindowMode));

// Set mouse hook
if (!MouseHook.m_hook && EnableMouseHook)
Expand Down Expand Up @@ -3023,9 +3020,6 @@ HRESULT m_IDirectDrawX::CreateD3D9Device()
SendMessage(hWnd, WM_WINDOWPOSCHANGED, 0, (LPARAM)&winpos);
SendMessage(hWnd, WM_MOVE, 0, MAKELPARAM(NewRect.left, NewRect.top));
SendMessage(hWnd, WM_SIZE, SIZE_RESTORED, MAKELPARAM(NewRect.right - NewRect.left, NewRect.bottom - NewRect.top));

// Peek messages to help prevent a "Not Responding" window
Utils::CheckMessageQueue(hWnd);
}

// Store display frequency
Expand Down

0 comments on commit 3abc22d

Please sign in to comment.