diff --git a/carolhook/carol-dll.c b/carolhook/carol-dll.c index 9aa1484..ce3adf9 100644 --- a/carolhook/carol-dll.c +++ b/carolhook/carol-dll.c @@ -27,6 +27,12 @@ const struct dll_bind_sym carol_dll_syms[] = { }, { .sym = "carol_io_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), } }; diff --git a/carolhook/carol-dll.h b/carolhook/carol-dll.h index d56df64..9f8c7ff 100644 --- a/carolhook/carol-dll.h +++ b/carolhook/carol-dll.h @@ -12,6 +12,8 @@ struct carol_dll { HRESULT (*touch_init)(); HRESULT (*ledbd_init)(); HRESULT (*controlbd_init)(); + void (*touch_start)(carol_io_touch_callback_t callback); + void (*touch_stop)(); }; struct carol_dll_config { diff --git a/carolhook/carolhook.def b/carolhook/carolhook.def index 260058e..5102937 100644 --- a/carolhook/carolhook.def +++ b/carolhook/carolhook.def @@ -18,3 +18,5 @@ EXPORTS carol_io_touch_init carol_io_ledbd_init carol_io_controlbd_init + carol_io_touch_start + carol_io_touch_stop diff --git a/carolhook/touch.c b/carolhook/touch.c index c062e90..46527b8 100644 --- a/carolhook/touch.c +++ b/carolhook/touch.c @@ -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_name_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 struct uart touch_uart; -static uint8_t touch_written_bytes[520]; -static uint8_t touch_readable_bytes[520]; +static uint8_t touch_written_bytes[528]; +static uint8_t touch_readable_bytes[528]; 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) { @@ -118,13 +123,13 @@ static HRESULT touch_handle_irp_locked(struct irp *irp) } else if (!strcmp("OI", (char *)req.cmd)) { 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)) { hr = handle_touch_name_cmd(&req); } else if (!strcmp("R", (char *)req.cmd)) { - should_stream = false; + carol_dll.touch_stop(); dprintf("Touch: Reset\n"); 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); } +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. */ static HRESULT touch_frame_decode(struct touch_req *dest, struct iobuf *iobuf) { diff --git a/carolhook/touch.h b/carolhook/touch.h index 2ae3031..a815cd1 100644 --- a/carolhook/touch.h +++ b/carolhook/touch.h @@ -4,6 +4,8 @@ #include #include +#pragma pack(push, 1) + struct touch_config { bool enable; }; @@ -16,4 +18,21 @@ struct touch_req { 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); \ No newline at end of file diff --git a/carolio/carolio.c b/carolio/carolio.c index 67339c9..3f4339b 100644 --- a/carolio/carolio.c +++ b/carolio/carolio.c @@ -8,9 +8,13 @@ #include "carolio/carolio.h" #include "carolio/config.h" +static unsigned int __stdcall carol_io_touch_thread_proc(void *ctx); + static bool carol_io_coin; 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; uint16_t carol_io_get_api_version(void) { @@ -81,4 +85,55 @@ HRESULT carol_io_ledbd_init() HRESULT carol_io_controlbd_init() { 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; } \ No newline at end of file diff --git a/carolio/carolio.h b/carolio/carolio.h index 7310f54..6c85e3d 100644 --- a/carolio/carolio.h +++ b/carolio/carolio.h @@ -5,6 +5,8 @@ #include #include +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 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 @@ -51,4 +53,8 @@ HRESULT carol_io_touch_init(); HRESULT carol_io_ledbd_init(); -HRESULT carol_io_controlbd_init(); \ No newline at end of file +HRESULT carol_io_controlbd_init(); + +void carol_io_touch_start(carol_io_touch_callback_t callback); + +void carol_io_touch_stop(); \ No newline at end of file