1
0
mirror of synced 2024-12-18 07:05:52 +01:00
bananatools/platform/misc.c

106 lines
2.3 KiB
C
Raw Normal View History

2023-01-03 05:35:53 +01:00
#include <windows.h>
#include <winuser.h>
#include <assert.h>
#include <stdint.h>
#include "hook/table.h"
#include "platform/misc.h"
#include "util/dprintf.h"
// BlockInput
// ShowCursor
static BOOL WINAPI my_BlockInput(BOOL fBlockIt);
static int WINAPI my_ShowCursor(BOOL bShow);
static BOOL WINAPI my_GetCursorInfo(PCURSORINFO pci);
static BOOL (WINAPI *next_BlockInput)(BOOL fBlockIt);
static int (WINAPI *next_ShowCursor)(BOOL bShow);
static BOOL (WINAPI *next_GetCursorInfo)(PCURSORINFO pci);
BOOL block_input_hook = true;
BOOL show_cursor_hook = true;
int real_cursor_state = 0;
static const struct hook_symbol misc_hook_syms[] = {
{
.name = "BlockInput",
.patch = my_BlockInput,
.link = (void **) &next_BlockInput,
}, {
.name = "ShowCursor",
.patch = my_ShowCursor,
.link = (void **) &next_ShowCursor,
}, {
.name = "GetCursorInfo",
.patch = my_GetCursorInfo,
.link = (void **) &next_GetCursorInfo,
}
};
HRESULT misc_hook_init(const struct misc_config *cfg)
{
assert(cfg != NULL);
show_cursor_hook = cfg->show_cursor_hook;
block_input_hook = cfg->block_input_hook;
dprintf("Misc: init\n");
hook_table_apply(
NULL,
"User32.dll",
misc_hook_syms,
_countof(misc_hook_syms));
return S_OK;
}
static BOOL WINAPI my_BlockInput(BOOL fBlockIt)
{
if (!block_input_hook) {
return next_BlockInput(fBlockIt);
}
dprintf("Misc: Block BlockInput -> %d\n", fBlockIt);
return true;
}
static int WINAPI my_ShowCursor(BOOL bShow)
{
if (!show_cursor_hook) {
return next_ShowCursor(bShow);
}
dprintf("Misc: Block ShowCursor -> %d\n", bShow);
// Keep track for GetCursorInfo
if (bShow) {
return ++real_cursor_state;
} else {
return --real_cursor_state;
}
}
static BOOL WINAPI my_GetCursorInfo(PCURSORINFO pci)
{
if (!show_cursor_hook) {
return next_GetCursorInfo(pci);
}
// Game only seems to read the flags field,
// so that's all we'll mess with
dprintf("Misc: my_GetCursorInfo\n");
if (real_cursor_state >= 0) {
pci->flags = CURSOR_SHOWING;
} else {
pci->flags = 0;
}
return true;
}