Skip to content

Commit

Permalink
Prevent input propagation when initiating dwmext actions
Browse files Browse the repository at this point in the history
  • Loading branch information
Aurumaker72 committed Nov 16, 2023
1 parent 858a8f3 commit 0f50e68
Showing 1 changed file with 83 additions and 79 deletions.
162 changes: 83 additions & 79 deletions dwmext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ HHOOK keyboard_hook;
#define HEIGHT(rect) (rect.bottom - rect.top)

HWND get_top_level_parent(HWND hwnd)
{
{
if (GetWindowLongPtr(hwnd, GWL_STYLE) & WS_CHILD)
{
return GetAncestor(hwnd, GA_ROOT);
Expand All @@ -38,101 +38,106 @@ void unhook() {
}

LRESULT CALLBACK mouse_hook_proc(int n_code, WPARAM w_param, LPARAM l_param) {
if (n_code >= 0) {
auto mouse_info = reinterpret_cast<MSLLHOOKSTRUCT*>(l_param);

if (w_param == WM_LBUTTONDOWN) {
if (n_code < 0)
{
return CallNextHookEx(NULL, n_code, w_param, l_param);
}
bool handled = false;
auto mouse_info = reinterpret_cast<MSLLHOOKSTRUCT*>(l_param);

if (GetKeyState(relative_move_key) & 0x8000)
{
relative_move_hwnd = get_top_level_parent(WindowFromPoint(mouse_info->pt));
RECT rect = { 0 };
GetWindowRect(relative_move_hwnd, &rect);

relative_move_diff = {
mouse_info->pt.x - rect.left,
mouse_info->pt.y - rect.top,
};
}
if (w_param == WM_LBUTTONDOWN) {

if (GetKeyState(relative_size_key) & 0x8000)
{
relative_size_hwnd = get_top_level_parent(WindowFromPoint(mouse_info->pt));
GetWindowRect(relative_size_hwnd, &relative_size_rect);
relative_size_mouse_position = mouse_info->pt;
}
if (GetKeyState(relative_move_key) & 0x8000)
{
relative_move_hwnd = get_top_level_parent(WindowFromPoint(mouse_info->pt));
RECT rect = { 0 };
GetWindowRect(relative_move_hwnd, &rect);

relative_move_diff = {
mouse_info->pt.x - rect.left,
mouse_info->pt.y - rect.top,
};
handled = true;
}

if (GetKeyState(relative_size_key) & 0x8000)
{
relative_size_hwnd = get_top_level_parent(WindowFromPoint(mouse_info->pt));
GetWindowRect(relative_size_hwnd, &relative_size_rect);
relative_size_mouse_position = mouse_info->pt;
handled = true;
}
if (w_param == WM_LBUTTONUP)
}
if (w_param == WM_LBUTTONUP)
{
if (relative_move_hwnd || relative_size_hwnd)
{
if (relative_size_hwnd)
{
UpdateWindow(relative_size_hwnd);
}
handled = true;
}

relative_move_hwnd = nullptr;
relative_size_hwnd = nullptr;
relative_move_hwnd = nullptr;
relative_size_hwnd = nullptr;



}
if (w_param == WM_MOUSEMOVE)
{
if (relative_move_hwnd)
{
SetWindowPos(relative_move_hwnd, nullptr, mouse_info->pt.x - relative_move_diff.x, mouse_info->pt.y - relative_move_diff.y, 0, 0, SWP_NOSIZE);
}
if (w_param == WM_MOUSEMOVE)

if (relative_size_hwnd)
{
if (relative_move_hwnd)
RECT current_rect = { 0 };
GetWindowRect(relative_size_hwnd, &current_rect);

POINT diff = {
mouse_info->pt.x - relative_size_mouse_position.x,
mouse_info->pt.y - relative_size_mouse_position.y,
};


POINT relative = {
relative_size_mouse_position.x - relative_size_rect.left,
relative_size_mouse_position.y - relative_size_rect.top,
};

// top-left corner
if (relative.x < WIDTH(current_rect) / 2 && relative.y < HEIGHT(current_rect) / 2)
{
current_rect.left = relative_size_rect.left + diff.x;
current_rect.top = relative_size_rect.top + diff.y;
}

// top-right corner
if (relative.x > WIDTH(current_rect) / 2 && relative.y < HEIGHT(current_rect) / 2)
{
current_rect.top = relative_size_rect.top + diff.y;
current_rect.right = relative_size_rect.right + diff.x;
}

// bottom-left corner
if (relative.x < WIDTH(current_rect) / 2 && relative.y > HEIGHT(current_rect) / 2)
{
SetWindowPos(relative_move_hwnd, nullptr, mouse_info->pt.x - relative_move_diff.x, mouse_info->pt.y - relative_move_diff.y, 0, 0, SWP_NOSIZE);
current_rect.left = relative_size_rect.left + diff.x;
current_rect.bottom = relative_size_rect.bottom + diff.y;
}

if (relative_size_hwnd)
// bottom-right corner
if (relative.x > WIDTH(current_rect) / 2 && relative.y > HEIGHT(current_rect) / 2)
{
RECT current_rect = { 0 };
GetWindowRect(relative_size_hwnd, &current_rect);

POINT diff = {
mouse_info->pt.x - relative_size_mouse_position.x,
mouse_info->pt.y - relative_size_mouse_position.y,
};


POINT relative = {
relative_size_mouse_position.x - relative_size_rect.left,
relative_size_mouse_position.y - relative_size_rect.top,
};

// top-left corner
if (relative.x < WIDTH(current_rect) / 2 && relative.y < HEIGHT(current_rect) / 2)
{
current_rect.left = relative_size_rect.left + diff.x;
current_rect.top = relative_size_rect.top + diff.y;
}

// top-right corner
if (relative.x > WIDTH(current_rect) / 2 && relative.y < HEIGHT(current_rect) / 2)
{
current_rect.top = relative_size_rect.top + diff.y;
current_rect.right = relative_size_rect.right + diff.x;
}

// bottom-left corner
if (relative.x < WIDTH(current_rect) / 2 && relative.y > HEIGHT(current_rect) / 2)
{
current_rect.left = relative_size_rect.left + diff.x;
current_rect.bottom = relative_size_rect.bottom + diff.y;
}

// bottom-right corner
if (relative.x > WIDTH(current_rect) / 2 && relative.y > HEIGHT(current_rect) / 2)
{
current_rect.right = relative_size_rect.right + diff.x;
current_rect.bottom = relative_size_rect.bottom + diff.y;
}

// NOTE: we dont redraw the window until resizing finishes, as setwindowpos is ridiculously expensive (but how does dwm do this smoothly???)
MoveWindow(relative_size_hwnd, current_rect.left, current_rect.top, WIDTH(current_rect), HEIGHT(current_rect), true);
current_rect.right = relative_size_rect.right + diff.x;
current_rect.bottom = relative_size_rect.bottom + diff.y;
}

MoveWindow(relative_size_hwnd, current_rect.left, current_rect.top, WIDTH(current_rect), HEIGHT(current_rect), true);
}
}

return CallNextHookEx(NULL, n_code, w_param, l_param);

return handled ? 1 : CallNextHookEx(NULL, n_code, w_param, l_param);
}

LRESULT CALLBACK keyboard_hook_proc(int n_code, WPARAM w_param, LPARAM l_param)
Expand Down Expand Up @@ -161,7 +166,6 @@ int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
AllocConsole();
freopen("CONOUT$", "w", stdout);
MessageBox(nullptr, L"Dwmext is running.\n CTRL + F11 to exit.", L"dwmext", MB_ICONINFORMATION);

#endif

printf("running...\n");
Expand Down

0 comments on commit 0f50e68

Please sign in to comment.