carol: somewhat-working touch board??

This commit is contained in:
2023-11-27 23:23:00 -05:00
parent 962e14dc9b
commit 4dcf01f643
8 changed files with 186 additions and 39 deletions

View File

@ -7,6 +7,7 @@
#include "carolio/carolio.h"
#include "carolio/config.h"
#include "util/dprintf.h"
static unsigned int __stdcall carol_io_touch_thread_proc(void *ctx);
@ -15,6 +16,7 @@ static uint16_t carol_io_coins;
static struct carol_io_config carol_io_cfg;
static bool carol_io_touch_stop_flag;
static HANDLE carol_io_touch_thread;
static bool carol_io_window_focus = false;
uint16_t carol_io_get_api_version(void)
{
@ -110,22 +112,62 @@ void carol_io_touch_stop()
carol_io_touch_stop_flag = true;
}
void check_fg_wind(void)
{
HWND hwnd = GetForegroundWindow();
wchar_t window_class[MAX_PATH];
/* Unlike every other game, we can't use GetWindowText here. Why?
From MSDN:
"If the window does not have a caption, the return value is a null
string. This behavior is by design. It allows applications to call
GetWindowText without becoming unresponsive if the process that owns
the target window is not responding. However, if the target window
is not responding and it belongs to the calling application,
GetWindowText will cause the calling application to become
unresponsive."
Great, thanks Microsoft, very cool. Luckily Carol sets its class
name to the window title too so we can use that. */
GetClassNameW(hwnd, window_class, MAX_PATH);
if (wcscmp(window_class, L"WONDER Master")) {
if (carol_io_window_focus) {
dprintf("Carol IO: Window focus lost\n");
carol_io_window_focus = false;
}
} else if (!carol_io_window_focus) {
dprintf("Carol IO: Window focus regained\n");
carol_io_window_focus = true;
}
}
static unsigned int __stdcall carol_io_touch_thread_proc(void *ctx)
{
carol_io_touch_callback_t callback;
bool mouse_is_down = false;
uint32_t mX = 0;
uint32_t mY = 0;
uint16_t mX = 0;
uint16_t mY = 0;
POINT lpPoint;
HWND hwnd;
callback = ctx;
while (!carol_io_touch_stop_flag) {
check_fg_wind();
if (GetAsyncKeyState(VK_LBUTTON) & 0x8000) {
mouse_is_down = true;
if (GetCursorPos(&lpPoint)) {
mX = lpPoint.x;
mY = lpPoint.y;
if (GetCursorPos(&lpPoint)) {
hwnd = GetForegroundWindow();
if (ScreenToClient(hwnd, &lpPoint)) {
if (lpPoint.x < 0) lpPoint.x = 0;
if (lpPoint.y < 0) lpPoint.y = 0;
mX = (uint16_t)lpPoint.x;
mY = (uint16_t)lpPoint.y;
}
}
} else {
mouse_is_down = false;

View File

@ -5,7 +5,7 @@
#include <stdbool.h>
#include <stdint.h>
typedef void (*carol_io_touch_callback_t)(const bool is_pressed, const uint32_t mouse_x, const uint32_t mouse_y);
typedef void (*carol_io_touch_callback_t)(const bool is_pressed, const uint16_t mouse_x, const uint16_t mouse_y);
/* Get the version of the Project carol IO API that this DLL supports. This
function should return a positive 16-bit integer, where the high byte is