carol: add touch dll functions

This commit is contained in:
Hay1tsme 2023-05-31 04:54:38 -04:00
parent 02201dfba5
commit 4c67843f08
7 changed files with 152 additions and 5 deletions

View File

@ -27,6 +27,12 @@ const struct dll_bind_sym carol_dll_syms[] = {
}, { }, {
.sym = "carol_io_controlbd_init", .sym = "carol_io_controlbd_init",
.off = offsetof(struct carol_dll, controlbd_init), .off = offsetof(struct carol_dll, controlbd_init),
}, {
.sym = "carol_io_touch_start",
.off = offsetof(struct carol_dll, touch_start),
}, {
.sym = "carol_io_touch_stop",
.off = offsetof(struct carol_dll, touch_stop),
} }
}; };

View File

@ -12,6 +12,8 @@ struct carol_dll {
HRESULT (*touch_init)(); HRESULT (*touch_init)();
HRESULT (*ledbd_init)(); HRESULT (*ledbd_init)();
HRESULT (*controlbd_init)(); HRESULT (*controlbd_init)();
void (*touch_start)(carol_io_touch_callback_t callback);
void (*touch_stop)();
}; };
struct carol_dll_config { struct carol_dll_config {

View File

@ -18,3 +18,5 @@ EXPORTS
carol_io_touch_init carol_io_touch_init
carol_io_ledbd_init carol_io_ledbd_init
carol_io_controlbd_init carol_io_controlbd_init
carol_io_touch_start
carol_io_touch_stop

View File

@ -32,12 +32,17 @@ static HRESULT touch_frame_decode(struct touch_req *dest, struct iobuf *iobuf);
static HRESULT handle_touch_ack_cmd(const struct touch_req *req); static HRESULT handle_touch_ack_cmd(const struct touch_req *req);
static HRESULT handle_touch_name_cmd(const struct touch_req *req); static HRESULT handle_touch_name_cmd(const struct touch_req *req);
static HRESULT handle_touch_id_cmd(const struct touch_req *req); static HRESULT handle_touch_id_cmd(const struct touch_req *req);
static void touch_scan_auto(const bool is_pressed, const uint32_t mouse_x, const uint32_t mouse_y);
static CRITICAL_SECTION touch_lock; static CRITICAL_SECTION touch_lock;
static struct uart touch_uart; static struct uart touch_uart;
static uint8_t touch_written_bytes[520]; static uint8_t touch_written_bytes[528];
static uint8_t touch_readable_bytes[520]; static uint8_t touch_readable_bytes[528];
static bool should_stream = false; static bool should_stream = false;
static bool last_pressed;
static uint16_t last_x;
static uint16_t last_y;
HRESULT touch_hook_init(const struct touch_config *cfg) HRESULT touch_hook_init(const struct touch_config *cfg)
{ {
@ -118,13 +123,13 @@ static HRESULT touch_handle_irp_locked(struct irp *irp)
} }
else if (!strcmp("OI", (char *)req.cmd)) { else if (!strcmp("OI", (char *)req.cmd)) {
hr = handle_touch_id_cmd(&req); hr = handle_touch_id_cmd(&req);
should_stream = true; // possibly send stuff after we get this? //carol_dll.touch_start(touch_scan_auto);
} }
else if (!strcmp("NM", (char *)req.cmd)) { else if (!strcmp("NM", (char *)req.cmd)) {
hr = handle_touch_name_cmd(&req); hr = handle_touch_name_cmd(&req);
} }
else if (!strcmp("R", (char *)req.cmd)) { else if (!strcmp("R", (char *)req.cmd)) {
should_stream = false; carol_dll.touch_stop();
dprintf("Touch: Reset\n"); dprintf("Touch: Reset\n");
hr = handle_touch_ack_cmd(&req); hr = handle_touch_ack_cmd(&req);
} }
@ -154,6 +159,58 @@ static HRESULT handle_touch_id_cmd(const struct touch_req *req)
return iobuf_write(&touch_uart.readable, "\001EX1234\015", 8); return iobuf_write(&touch_uart.readable, "\001EX1234\015", 8);
} }
static void touch_scan_auto(const bool is_pressed, const uint32_t mouse_x, const uint32_t mouse_y)
{
struct touch_auto_resp resp;
uint16_t tmp_x;
uint16_t tmp_y;
bool flg = false;
memset(&resp, 0, sizeof(resp));
resp.rep_id = 0x17;
resp.touches[0].status = 0x04;
resp.count = 1;
if (is_pressed) {
resp.touches[0].status = 0x07;
resp.touches[0].touch_id = 1;
tmp_x = mouse_x & 0x7FFF;
tmp_y = mouse_y & 0x7FFF;
// flip
resp.touches[0].x = (tmp_x << 8) | (tmp_x >> 8);
resp.touches[0].y = (tmp_y << 8) | (tmp_y >> 8);
flg = resp.touches[0].x != last_x || resp.touches[0].y != last_y;
#if 1
if (flg)
dprintf("Touch: Mouse down! x %04X y: %04X\n", resp.touches[0].x, resp.touches[0].y);
#endif
last_x = resp.touches[0].x;
last_y = resp.touches[0].y;
} else if (last_pressed) {
resp.touches[0].x = last_x;
resp.touches[0].y = last_y;
}
last_pressed = is_pressed;
EnterCriticalSection(&touch_lock);
iobuf_write(&touch_uart.readable, &resp, sizeof(resp));
LeaveCriticalSection(&touch_lock);
#if 1
//if (flg) {
dprintf("Touch: RX Buffer: (pos %08x)\n", (uint32_t)touch_uart.readable.pos);
dump_iobuf(&touch_uart.readable);
//}
#endif
}
/* Decodes the response into a struct that's easier to work with. */ /* Decodes the response into a struct that's easier to work with. */
static HRESULT touch_frame_decode(struct touch_req *dest, struct iobuf *iobuf) static HRESULT touch_frame_decode(struct touch_req *dest, struct iobuf *iobuf)
{ {

View File

@ -4,6 +4,8 @@
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h> #include <stdint.h>
#pragma pack(push, 1)
struct touch_config { struct touch_config {
bool enable; bool enable;
}; };
@ -16,4 +18,21 @@ struct touch_req {
size_t data_len; // length of data size_t data_len; // length of data
}; };
struct touch_report {
uint8_t status;
uint8_t touch_id;
uint16_t x;
uint16_t y;
};
struct touch_auto_resp {
uint8_t rep_id;
struct touch_report touches[10];
uint8_t count;
uint16_t scan_time;
//uint8_t padding[456];
};
#pragma pack(pop)
HRESULT touch_hook_init(const struct touch_config *cfg); HRESULT touch_hook_init(const struct touch_config *cfg);

View File

@ -8,9 +8,13 @@
#include "carolio/carolio.h" #include "carolio/carolio.h"
#include "carolio/config.h" #include "carolio/config.h"
static unsigned int __stdcall carol_io_touch_thread_proc(void *ctx);
static bool carol_io_coin; static bool carol_io_coin;
static uint16_t carol_io_coins; static uint16_t carol_io_coins;
static struct carol_io_config carol_io_cfg; static struct carol_io_config carol_io_cfg;
static bool carol_io_touch_stop_flag;
static HANDLE carol_io_touch_thread;
uint16_t carol_io_get_api_version(void) uint16_t carol_io_get_api_version(void)
{ {
@ -82,3 +86,54 @@ HRESULT carol_io_controlbd_init()
{ {
return S_OK; return S_OK;
} }
void carol_io_touch_start(carol_io_touch_callback_t callback)
{
if (carol_io_touch_thread != NULL) {
return;
}
carol_io_touch_stop_flag = false;
carol_io_touch_thread = (HANDLE) _beginthreadex(
NULL,
0,
carol_io_touch_thread_proc,
callback,
0,
NULL
);
}
void carol_io_touch_stop()
{
carol_io_touch_stop_flag = 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;
POINT lpPoint;
callback = ctx;
while (!carol_io_touch_stop_flag) {
if (GetAsyncKeyState(VK_LBUTTON) & 0x8000) {
mouse_is_down = true;
if (GetCursorPos(&lpPoint)) {
mX = lpPoint.x;
mY = lpPoint.y;
}
} else {
mouse_is_down = false;
}
callback(mouse_is_down, mX, mY);
Sleep(1);
}
return 0;
}

View File

@ -5,6 +5,8 @@
#include <stdbool.h> #include <stdbool.h>
#include <stdint.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);
/* Get the version of the Project carol IO API that this DLL supports. This /* 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 function should return a positive 16-bit integer, where the high byte is
the major version and the low byte is the minor version (as defined by the the major version and the low byte is the minor version (as defined by the
@ -52,3 +54,7 @@ HRESULT carol_io_touch_init();
HRESULT carol_io_ledbd_init(); HRESULT carol_io_ledbd_init();
HRESULT carol_io_controlbd_init(); HRESULT carol_io_controlbd_init();
void carol_io_touch_start(carol_io_touch_callback_t callback);
void carol_io_touch_stop();