2010-10-24 01:02:08 +03:00
|
|
|
#include <windows.h>
|
2024-09-08 19:14:58 +02:00
|
|
|
#include <windowsx.h>
|
|
|
|
#include <math.h>
|
2021-06-17 09:42:04 +02:00
|
|
|
#include "debug.h"
|
2021-09-09 03:49:54 +02:00
|
|
|
#include "winapi_hooks.h"
|
2020-10-13 09:20:52 +02:00
|
|
|
#include "dd.h"
|
2019-03-19 06:57:49 +01:00
|
|
|
#include "hook.h"
|
2023-08-20 23:12:00 +02:00
|
|
|
#include "utils.h"
|
2023-09-22 00:38:42 +02:00
|
|
|
#include "config.h"
|
2024-09-08 19:14:58 +02:00
|
|
|
#include "mouse.h"
|
2010-11-10 19:25:05 +02:00
|
|
|
|
|
|
|
|
2022-09-20 08:59:36 +02:00
|
|
|
BOOL g_mouse_locked;
|
2021-09-09 03:49:54 +02:00
|
|
|
HHOOK g_mouse_hook;
|
|
|
|
HOOKPROC g_mouse_proc;
|
2024-09-08 19:14:58 +02:00
|
|
|
static HHOOK g_mouse_gm_hook;
|
|
|
|
|
|
|
|
void mouse_hook_init()
|
|
|
|
{
|
|
|
|
if (g_mouse_gm_hook && UnhookWindowsHookEx(g_mouse_gm_hook))
|
|
|
|
{
|
|
|
|
g_mouse_gm_hook = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!g_ddraw.gui_thread_id)
|
|
|
|
return;
|
|
|
|
|
|
|
|
g_mouse_gm_hook =
|
|
|
|
real_SetWindowsHookExA(
|
|
|
|
WH_GETMESSAGE,
|
|
|
|
mouse_gm_hook_proc,
|
|
|
|
NULL,
|
|
|
|
g_ddraw.gui_thread_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
void mouse_hook_exit()
|
|
|
|
{
|
|
|
|
if (g_mouse_gm_hook)
|
|
|
|
{
|
|
|
|
UnhookWindowsHookEx(g_mouse_gm_hook);
|
|
|
|
}
|
|
|
|
}
|
2021-09-09 03:49:54 +02:00
|
|
|
|
2010-10-24 01:02:08 +03:00
|
|
|
void mouse_lock()
|
|
|
|
{
|
2024-03-22 22:27:00 +01:00
|
|
|
if (g_config.devmode || g_ddraw.bnet_active || !g_ddraw.hwnd)
|
2012-08-09 14:35:28 +03:00
|
|
|
return;
|
|
|
|
|
2024-03-22 22:27:00 +01:00
|
|
|
if (g_hook_active && !g_mouse_locked && !util_is_minimized(g_ddraw.hwnd))
|
2010-10-24 01:02:08 +03:00
|
|
|
{
|
2024-03-22 22:27:00 +01:00
|
|
|
int game_count = InterlockedExchangeAdd((LONG*)&g_ddraw.show_cursor_count, 0);
|
2021-06-17 04:12:05 +02:00
|
|
|
int cur_count = real_ShowCursor(TRUE) - 1;
|
|
|
|
real_ShowCursor(FALSE);
|
|
|
|
|
|
|
|
if (cur_count > game_count)
|
|
|
|
{
|
|
|
|
while (real_ShowCursor(FALSE) > game_count);
|
|
|
|
}
|
2021-06-17 08:17:50 +02:00
|
|
|
else if (cur_count < game_count)
|
2018-11-15 10:47:18 +01:00
|
|
|
{
|
2021-06-17 04:12:05 +02:00
|
|
|
while (real_ShowCursor(TRUE) < game_count);
|
2018-11-15 10:47:18 +01:00
|
|
|
}
|
2021-06-12 05:45:28 +02:00
|
|
|
|
2024-03-22 22:27:00 +01:00
|
|
|
real_SetCursor((HCURSOR)InterlockedExchangeAdd((LONG*)&g_ddraw.old_cursor, 0));
|
2021-06-19 04:28:15 +02:00
|
|
|
|
2021-09-19 09:07:26 +02:00
|
|
|
RECT rc = { 0 };
|
2024-03-22 22:27:00 +01:00
|
|
|
real_GetClientRect(g_ddraw.hwnd, &rc);
|
|
|
|
real_MapWindowPoints(g_ddraw.hwnd, HWND_DESKTOP, (LPPOINT)&rc, 2);
|
|
|
|
OffsetRect(&rc, g_ddraw.render.viewport.x, g_ddraw.render.viewport.y);
|
2021-09-19 09:07:26 +02:00
|
|
|
|
2024-03-22 22:27:00 +01:00
|
|
|
int cur_x = InterlockedExchangeAdd((LONG*)&g_ddraw.cursor.x, 0);
|
|
|
|
int cur_y = InterlockedExchangeAdd((LONG*)&g_ddraw.cursor.y, 0);
|
2021-09-19 09:07:26 +02:00
|
|
|
|
|
|
|
real_SetCursorPos(
|
2024-03-22 22:27:00 +01:00
|
|
|
g_config.adjmouse ? (int)(rc.left + (cur_x * g_ddraw.mouse.scale_x)) : rc.left + cur_x,
|
|
|
|
g_config.adjmouse ? (int)(rc.top + (cur_y * g_ddraw.mouse.scale_y)) : rc.top + cur_y);
|
2021-09-19 09:07:26 +02:00
|
|
|
|
2024-03-22 22:27:00 +01:00
|
|
|
CopyRect(&rc, &g_ddraw.mouse.rc);
|
|
|
|
real_MapWindowPoints(g_ddraw.hwnd, HWND_DESKTOP, (LPPOINT)&rc, 2);
|
2021-06-16 05:57:19 +02:00
|
|
|
real_ClipCursor(&rc);
|
2018-10-26 05:27:10 +02:00
|
|
|
|
2022-09-20 08:59:36 +02:00
|
|
|
g_mouse_locked = TRUE;
|
2010-10-24 01:02:08 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void mouse_unlock()
|
|
|
|
{
|
2024-03-22 22:27:00 +01:00
|
|
|
if (g_config.devmode || !g_hook_active || !g_ddraw.hwnd)
|
2010-10-24 19:47:51 +03:00
|
|
|
return;
|
|
|
|
|
2022-09-20 08:59:36 +02:00
|
|
|
if (g_mouse_locked)
|
2010-10-24 01:02:08 +03:00
|
|
|
{
|
2022-09-20 08:59:36 +02:00
|
|
|
g_mouse_locked = FALSE;
|
2011-07-08 18:22:43 +03:00
|
|
|
|
2021-09-19 09:07:26 +02:00
|
|
|
real_ClipCursor(NULL);
|
2024-08-30 16:02:32 +02:00
|
|
|
ReleaseCapture();
|
2021-09-19 09:07:26 +02:00
|
|
|
|
|
|
|
RECT rc = { 0 };
|
2024-03-22 22:27:00 +01:00
|
|
|
real_GetClientRect(g_ddraw.hwnd, &rc);
|
|
|
|
real_MapWindowPoints(g_ddraw.hwnd, HWND_DESKTOP, (LPPOINT)&rc, 2);
|
|
|
|
OffsetRect(&rc, g_ddraw.render.viewport.x, g_ddraw.render.viewport.y);
|
2021-06-11 20:30:43 +02:00
|
|
|
|
2024-03-22 22:27:00 +01:00
|
|
|
int cur_x = InterlockedExchangeAdd((LONG*)&g_ddraw.cursor.x, 0);
|
|
|
|
int cur_y = InterlockedExchangeAdd((LONG*)&g_ddraw.cursor.y, 0);
|
2021-06-17 06:13:16 +02:00
|
|
|
|
2019-03-19 06:57:49 +01:00
|
|
|
real_SetCursorPos(
|
2024-03-22 22:27:00 +01:00
|
|
|
(int)(rc.left + (cur_x * g_ddraw.mouse.scale_x)),
|
|
|
|
(int)(rc.top + (cur_y * g_ddraw.mouse.scale_y)));
|
2021-09-19 09:07:26 +02:00
|
|
|
|
2023-07-07 23:54:59 +02:00
|
|
|
real_SetCursor(LoadCursor(NULL, IDC_ARROW));
|
|
|
|
|
2021-09-19 09:07:26 +02:00
|
|
|
while (real_ShowCursor(TRUE) < 0);
|
2018-11-01 20:09:42 +01:00
|
|
|
}
|
2019-08-06 04:37:06 +02:00
|
|
|
}
|
2021-09-09 03:49:54 +02:00
|
|
|
|
2024-09-08 19:14:58 +02:00
|
|
|
LRESULT CALLBACK mouse_gm_hook_proc(int code, WPARAM wParam, LPARAM lParam)
|
|
|
|
{
|
2024-09-10 15:42:00 +02:00
|
|
|
if (code < 0 || !lParam || !g_ddraw.width)
|
|
|
|
return CallNextHookEx(g_mouse_gm_hook, code, wParam, lParam);
|
|
|
|
|
2024-09-08 19:14:58 +02:00
|
|
|
MSG* msg = (MSG*)lParam;
|
|
|
|
|
2024-09-10 15:42:00 +02:00
|
|
|
if (!g_config.windowed || real_ScreenToClient(g_ddraw.hwnd, &msg->pt))
|
|
|
|
{
|
|
|
|
int x = max(msg->pt.x - g_ddraw.mouse.x_adjust, 0);
|
|
|
|
int y = max(msg->pt.y - g_ddraw.mouse.y_adjust, 0);
|
|
|
|
|
|
|
|
if (g_config.adjmouse)
|
|
|
|
{
|
|
|
|
x = (DWORD)(roundf(x * g_ddraw.mouse.unscale_x));
|
|
|
|
y = (DWORD)(roundf(y * g_ddraw.mouse.unscale_y));
|
|
|
|
}
|
|
|
|
|
|
|
|
msg->pt.x = min(x, g_ddraw.width - 1);
|
|
|
|
msg->pt.y = min(y, g_ddraw.height - 1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
msg->pt.x = InterlockedExchangeAdd((LONG*)&g_ddraw.cursor.x, 0);
|
|
|
|
msg->pt.y = InterlockedExchangeAdd((LONG*)&g_ddraw.cursor.y, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (msg->hwnd != g_ddraw.hwnd)
|
2024-09-08 19:14:58 +02:00
|
|
|
return CallNextHookEx(g_mouse_gm_hook, code, wParam, lParam);
|
|
|
|
|
|
|
|
switch (LOWORD(msg->message))
|
|
|
|
{
|
2024-09-10 15:42:00 +02:00
|
|
|
/* button up messages reactivate cursor lock */
|
2024-09-08 19:14:58 +02:00
|
|
|
case WM_LBUTTONUP:
|
|
|
|
case WM_RBUTTONUP:
|
|
|
|
case WM_MBUTTONUP:
|
2024-09-10 15:42:00 +02:00
|
|
|
{
|
|
|
|
if (!g_config.devmode && !g_mouse_locked)
|
|
|
|
{
|
|
|
|
int x = GET_X_LPARAM(msg->lParam);
|
|
|
|
int y = GET_Y_LPARAM(msg->lParam);
|
|
|
|
|
|
|
|
if (x > g_ddraw.render.viewport.x + g_ddraw.render.viewport.width ||
|
|
|
|
x < g_ddraw.render.viewport.x ||
|
|
|
|
y > g_ddraw.render.viewport.y + g_ddraw.render.viewport.height ||
|
|
|
|
y < g_ddraw.render.viewport.y)
|
|
|
|
{
|
|
|
|
x = g_ddraw.width / 2;
|
|
|
|
y = g_ddraw.height / 2;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
x = (DWORD)((x - g_ddraw.render.viewport.x) * g_ddraw.mouse.unscale_x);
|
|
|
|
y = (DWORD)((y - g_ddraw.render.viewport.y) * g_ddraw.mouse.unscale_y);
|
|
|
|
}
|
|
|
|
|
|
|
|
x = min(x, g_ddraw.width - 1);
|
|
|
|
y = min(y, g_ddraw.height - 1);
|
|
|
|
|
|
|
|
InterlockedExchange((LONG*)&g_ddraw.cursor.x, x);
|
|
|
|
InterlockedExchange((LONG*)&g_ddraw.cursor.y, y);
|
|
|
|
|
|
|
|
mouse_lock();
|
|
|
|
|
|
|
|
msg->message = MAKELONG(WM_NULL, HIWORD(msg->message));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* fall through for lParam */
|
|
|
|
}
|
|
|
|
/* down messages are ignored if we have no cursor lock */
|
2024-09-08 19:14:58 +02:00
|
|
|
case WM_XBUTTONDBLCLK:
|
|
|
|
case WM_XBUTTONDOWN:
|
|
|
|
case WM_XBUTTONUP:
|
|
|
|
case WM_MOUSEWHEEL:
|
|
|
|
case WM_MOUSEHOVER:
|
|
|
|
case WM_LBUTTONDBLCLK:
|
|
|
|
case WM_MBUTTONDBLCLK:
|
|
|
|
case WM_RBUTTONDBLCLK:
|
|
|
|
case WM_LBUTTONDOWN:
|
|
|
|
case WM_RBUTTONDOWN:
|
|
|
|
case WM_MBUTTONDOWN:
|
|
|
|
case WM_MOUSEMOVE:
|
|
|
|
{
|
2024-09-10 15:42:00 +02:00
|
|
|
if (!g_config.devmode && !g_mouse_locked)
|
|
|
|
{
|
|
|
|
msg->message = MAKELONG(WM_NULL, HIWORD(msg->message));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-09-08 19:14:58 +02:00
|
|
|
if (LOWORD(msg->message) == WM_MOUSEWHEEL)
|
|
|
|
{
|
|
|
|
POINT pt = { GET_X_LPARAM(msg->lParam), GET_Y_LPARAM(msg->lParam) };
|
|
|
|
real_ScreenToClient(g_ddraw.hwnd, &pt);
|
|
|
|
msg->lParam = MAKELPARAM(pt.x, pt.y);
|
|
|
|
}
|
|
|
|
|
|
|
|
int x = max(GET_X_LPARAM(msg->lParam) - g_ddraw.mouse.x_adjust, 0);
|
|
|
|
int y = max(GET_Y_LPARAM(msg->lParam) - g_ddraw.mouse.y_adjust, 0);
|
|
|
|
|
|
|
|
if (g_config.adjmouse)
|
|
|
|
{
|
|
|
|
if (g_config.vhack && !g_config.devmode)
|
|
|
|
{
|
|
|
|
POINT pt = { 0, 0 };
|
|
|
|
fake_GetCursorPos(&pt);
|
|
|
|
|
|
|
|
x = pt.x;
|
|
|
|
y = pt.y;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
x = (DWORD)(roundf(x * g_ddraw.mouse.unscale_x));
|
|
|
|
y = (DWORD)(roundf(y * g_ddraw.mouse.unscale_y));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
x = min(x, g_ddraw.width - 1);
|
|
|
|
y = min(y, g_ddraw.height - 1);
|
|
|
|
|
2024-09-10 15:40:35 +02:00
|
|
|
InterlockedExchange((LONG*)&g_ddraw.cursor.x, x);
|
|
|
|
InterlockedExchange((LONG*)&g_ddraw.cursor.y, y);
|
|
|
|
|
2024-09-08 19:14:58 +02:00
|
|
|
msg->lParam = MAKELPARAM(x, y);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return CallNextHookEx(g_mouse_gm_hook, code, wParam, lParam);
|
|
|
|
}
|
|
|
|
|
|
|
|
LRESULT CALLBACK mouse_hook_proc(int code, WPARAM wParam, LPARAM lParam)
|
2021-09-09 03:49:54 +02:00
|
|
|
{
|
2024-03-22 22:27:00 +01:00
|
|
|
if (!g_ddraw.ref)
|
2024-09-08 19:14:58 +02:00
|
|
|
return g_mouse_proc(code, wParam, lParam);
|
2021-09-09 03:49:54 +02:00
|
|
|
|
2024-09-08 19:14:58 +02:00
|
|
|
if (code < 0 || (!g_config.devmode && !g_mouse_locked))
|
|
|
|
return CallNextHookEx(g_mouse_hook, code, wParam, lParam);
|
2021-09-09 03:49:54 +02:00
|
|
|
|
|
|
|
fake_GetCursorPos(&((MOUSEHOOKSTRUCT*)lParam)->pt);
|
|
|
|
|
2024-09-08 19:14:58 +02:00
|
|
|
return g_mouse_proc(code, wParam, lParam);
|
2021-09-09 03:49:54 +02:00
|
|
|
}
|