Skip to content

Commit

Permalink
merge PeekMessage and GetMessage hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
FunkyFr3sh committed Sep 11, 2024
1 parent a9f0b8f commit cd021dc
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 62 deletions.
1 change: 0 additions & 1 deletion inc/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ typedef struct CNCDDRAWCONFIG
int resolutions;
int fixchilds;
BOOL hook_peekmessage;
BOOL hook_getmessage;

/* Undocumented settings */

Expand Down
6 changes: 2 additions & 4 deletions src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ void cfg_load()
GET_INT(g_config.resolutions, "resolutions", RESLIST_NORMAL);
GET_INT(g_config.fixchilds, "fixchilds", FIX_CHILDS_DETECT_PAINT);
GET_BOOL(g_config.hook_peekmessage, "hook_peekmessage", FALSE);
GET_BOOL(g_config.hook_getmessage, "hook_getmessage", FALSE);
GET_STRING("win_version", "", g_config.win_version, sizeof(g_config.win_version));

/* Undocumented settings */
Expand Down Expand Up @@ -300,9 +299,8 @@ static void cfg_create_ini()
"; Note: Disables upscaling if a child window was detected (to ensure the game is fully playable, may look weird though)\n"
"fixchilds=2\n"
"\n"
"; Enable one of the following settings if your cursor doesn't work properly when upscaling is enabled\n"
"; Enable the following setting if your cursor doesn't work properly when upscaling is enabled\n"
"hook_peekmessage=false\n"
"hook_getmessage=false\n"
"\n"
"\n"
"; Undocumented settings - You may or may not change these (You should rather focus on the settings above)\n"
Expand Down Expand Up @@ -844,7 +842,7 @@ static void cfg_create_ini()
"\n"
"; Enemy Infestation\n"
"[EI]\n"
"hook_getmessage=true\n"
"hook_peekmessage=true\n"
"\n"
"; Fairy Tale About Father Frost, Ivan and Nastya\n"
"[mrazik]\n"
Expand Down
88 changes: 31 additions & 57 deletions src/winapi_hooks.c
Original file line number Diff line number Diff line change
Expand Up @@ -614,15 +614,38 @@ HHOOK WINAPI fake_SetWindowsHookExA(int idHook, HOOKPROC lpfn, HINSTANCE hmod, D

void HandleMessage(LPMSG lpMsg, HWND hWnd, UINT wMsgFilterMin, UINT wMsgFilterMax, UINT wRemoveMsg)
{
if (g_ddraw.ref && g_ddraw.width)
if (lpMsg && g_ddraw.ref && g_ddraw.hwnd && g_ddraw.width)
{
switch (lpMsg->message)
if (!g_config.windowed || real_ScreenToClient(g_ddraw.hwnd, &lpMsg->pt))
{
int x = max(lpMsg->pt.x - g_ddraw.mouse.x_adjust, 0);
int y = max(lpMsg->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));
}

lpMsg->pt.x = min(x, g_ddraw.width - 1);
lpMsg->pt.y = min(y, g_ddraw.height - 1);
}
else
{
lpMsg->pt.x = InterlockedExchangeAdd((LONG*)&g_ddraw.cursor.x, 0);
lpMsg->pt.y = InterlockedExchangeAdd((LONG*)&g_ddraw.cursor.y, 0);
}

if (lpMsg->hwnd != g_ddraw.hwnd || !g_config.hook_peekmessage)
return;

switch (LOWORD(lpMsg->message))
{
case WM_LBUTTONUP:
case WM_RBUTTONUP:
case WM_MBUTTONUP:
{
if (!g_config.devmode && !g_mouse_locked)
if (!g_config.devmode && !g_mouse_locked && (wRemoveMsg & PM_REMOVE))
{
int x = GET_X_LPARAM(lpMsg->lParam);
int y = GET_Y_LPARAM(lpMsg->lParam);
Expand Down Expand Up @@ -672,7 +695,6 @@ void HandleMessage(LPMSG lpMsg, HWND hWnd, UINT wMsgFilterMin, UINT wMsgFilterMa
{
if (!g_config.devmode && !g_mouse_locked)
{
// Does not work with 'New Robinson'
if (!wMsgFilterMin &&
!wMsgFilterMax &&
!(wRemoveMsg & (PM_QS_INPUT | PM_QS_PAINT | PM_QS_POSTMESSAGE | PM_QS_SENDMESSAGE)))
Expand All @@ -682,7 +704,7 @@ void HandleMessage(LPMSG lpMsg, HWND hWnd, UINT wMsgFilterMin, UINT wMsgFilterMa
}
}

if (lpMsg->message == WM_MOUSEWHEEL)
if (LOWORD(lpMsg->message) == WM_MOUSEWHEEL)
{
POINT pt = { GET_X_LPARAM(lpMsg->lParam), GET_Y_LPARAM(lpMsg->lParam) };
real_ScreenToClient(g_ddraw.hwnd, &pt);
Expand Down Expand Up @@ -732,33 +754,9 @@ BOOL WINAPI fake_GetMessageA(LPMSG lpMsg, HWND hWnd, UINT wMsgFilterMin, UINT wM
g_ddraw.last_msg_pull_tick = timeGetTime();

BOOL result = real_GetMessageA(lpMsg, hWnd, wMsgFilterMin, wMsgFilterMax);

if (result && lpMsg && g_ddraw.ref && g_ddraw.hwnd && g_ddraw.width && !g_config.fixmousehook)
if (result)
{
if (!g_config.windowed || real_ScreenToClient(g_ddraw.hwnd, &lpMsg->pt))
{
int x = max(lpMsg->pt.x - g_ddraw.mouse.x_adjust, 0);
int y = max(lpMsg->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));
}

lpMsg->pt.x = min(x, g_ddraw.width - 1);
lpMsg->pt.y = min(y, g_ddraw.height - 1);
}
else
{
lpMsg->pt.x = InterlockedExchangeAdd((LONG*)&g_ddraw.cursor.x, 0);
lpMsg->pt.y = InterlockedExchangeAdd((LONG*)&g_ddraw.cursor.y, 0);
}

if (g_config.hook_getmessage)
{
HandleMessage(lpMsg, hWnd, wMsgFilterMin, wMsgFilterMax, PM_REMOVE);
}
HandleMessage(lpMsg, hWnd, wMsgFilterMin, wMsgFilterMax, PM_REMOVE);
}

return result;
Expand All @@ -770,33 +768,9 @@ BOOL WINAPI fake_PeekMessageA(LPMSG lpMsg, HWND hWnd, UINT wMsgFilterMin, UINT w
g_ddraw.last_msg_pull_tick = timeGetTime();

BOOL result = real_PeekMessageA(lpMsg, hWnd, wMsgFilterMin, wMsgFilterMax, wRemoveMsg);

if (result && lpMsg && g_ddraw.ref && g_ddraw.hwnd && g_ddraw.width && !g_config.fixmousehook)
if (result)
{
if (!g_config.windowed || real_ScreenToClient(g_ddraw.hwnd, &lpMsg->pt))
{
int x = max(lpMsg->pt.x - g_ddraw.mouse.x_adjust, 0);
int y = max(lpMsg->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));
}

lpMsg->pt.x = min(x, g_ddraw.width - 1);
lpMsg->pt.y = min(y, g_ddraw.height - 1);
}
else
{
lpMsg->pt.x = InterlockedExchangeAdd((LONG*)&g_ddraw.cursor.x, 0);
lpMsg->pt.y = InterlockedExchangeAdd((LONG*)&g_ddraw.cursor.y, 0);
}

if (g_config.hook_peekmessage)
{
HandleMessage(lpMsg, hWnd, wMsgFilterMin, wMsgFilterMax, wRemoveMsg);
}
HandleMessage(lpMsg, hWnd, wMsgFilterMin, wMsgFilterMax, wRemoveMsg);
}

return result;
Expand Down

0 comments on commit cd021dc

Please sign in to comment.