Skip to content
This repository has been archived by the owner on Dec 19, 2023. It is now read-only.

Commit

Permalink
win: add more sanity checks
Browse files Browse the repository at this point in the history
  • Loading branch information
wangwenx190 committed Jul 18, 2023
1 parent 2cd4d13 commit 8e73e3e
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 12 deletions.
3 changes: 2 additions & 1 deletion include/FramelessHelper/Core/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,8 @@ FRAMELESSHELPER_CORE_API void bringWindowToFront(const WId windowId);
[[nodiscard]] FRAMELESSHELPER_CORE_API QRect getWindowRestoreGeometry(const WId windowId);
FRAMELESSHELPER_CORE_API void removeMicaWindow(const WId windowId);
FRAMELESSHELPER_CORE_API void removeSysMenuHook(const WId windowId);
FRAMELESSHELPER_CORE_API quint64 queryMouseState();
FRAMELESSHELPER_CORE_API quint64 queryMouseButtonState();
FRAMELESSHELPER_CORE_API bool isValidWindow(const WId windowId, const bool checkVisible, const bool checkTopLevel);
#endif // Q_OS_WINDOWS

#ifdef Q_OS_LINUX
Expand Down
4 changes: 2 additions & 2 deletions src/core/framelesshelper_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -624,16 +624,16 @@ bool FramelessHelperWin::nativeEventFilter(const QByteArray &eventType, void *me
// Anyway, we should skip the entire processing in this case.
return false;
}
const auto windowId = reinterpret_cast<WId>(hWnd);
// Let's be extra safe.
if (IsWindow(hWnd) == FALSE) {
if (!Utils::isValidWindow(windowId, true, true)) {
return false;
}
const UINT uMsg = msg->message;
// WM_QUIT won't be posted to the WindowProc function.
if ((uMsg == WM_CLOSE) || (uMsg == WM_DESTROY)) {
return false;
}
const auto windowId = reinterpret_cast<WId>(hWnd);
if (!g_win32Helper()->data.contains(windowId)) {
return false;
}
Expand Down
48 changes: 39 additions & 9 deletions src/core/utils_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2464,31 +2464,61 @@ void Utils::removeSysMenuHook(const WId windowId)
g_utilsHelper()->data.remove(windowId);
}

quint64 Utils::queryMouseState()
quint64 Utils::queryMouseButtonState()
{
WPARAM result = 0;
if (GetKeyState(VK_LBUTTON) < 0) {
quint64 result = 0;
if (::GetKeyState(VK_LBUTTON) < 0) {
result |= MK_LBUTTON;
}
if (GetKeyState(VK_RBUTTON) < 0) {
if (::GetKeyState(VK_RBUTTON) < 0) {
result |= MK_RBUTTON;
}
if (GetKeyState(VK_SHIFT) < 0) {
if (::GetKeyState(VK_SHIFT) < 0) {
result |= MK_SHIFT;
}
if (GetKeyState(VK_CONTROL) < 0) {
if (::GetKeyState(VK_CONTROL) < 0) {
result |= MK_CONTROL;
}
if (GetKeyState(VK_MBUTTON) < 0) {
if (::GetKeyState(VK_MBUTTON) < 0) {
result |= MK_MBUTTON;
}
if (GetKeyState(VK_XBUTTON1) < 0) {
if (::GetKeyState(VK_XBUTTON1) < 0) {
result |= MK_XBUTTON1;
}
if (GetKeyState(VK_XBUTTON2) < 0) {
if (::GetKeyState(VK_XBUTTON2) < 0) {
result |= MK_XBUTTON2;
}
return result;
}

bool Utils::isValidWindow(const WId windowId, const bool checkVisible, const bool checkTopLevel)
{
Q_ASSERT(windowId);
if (!windowId) {
return false;
}
const auto hwnd = reinterpret_cast<HWND>(windowId);
if (::IsWindow(hwnd) == FALSE) {
return false;
}
RECT rect = { 0, 0, 0, 0 };
if (::GetWindowRect(hwnd, &rect) == FALSE) {
return false;
}
if ((rect.left >= rect.right) || (rect.top >= rect.bottom)) {
return false;
}
if (checkVisible) {
if (::IsWindowVisible(hwnd) == FALSE) {
return false;
}
}
if (checkTopLevel) {
if (::GetAncestor(hwnd, GA_ROOT) != hwnd) {
return false;
}
}
return true;
}

FRAMELESSHELPER_END_NAMESPACE

0 comments on commit 8e73e3e

Please sign in to comment.