segatools/mai2hook/touch.c

233 lines
5.8 KiB
C

#include <windows.h>
#include <stdbool.h>
#include <stdint.h>
#include "hooklib/reg.h"
#include "hooklib/uart.h"
#include "hooklib/fdshark.h"
#include "mai2hook/touch.h"
#include "mai2hook/mai2-dll.h"
#include "util/dprintf.h"
const char CMD_START = '{';
const char CMD_END = '}';
const char RESP_START = '(';
const char RESP_END = ')';
const char BLANK = '@';
// Commands with parameters
const char CMD_RATIO[1] = "r";
const char CMD_SENS[1] = "k";
const char CMD_SENS_CHECK[2] = "th";
// Commands that never change
const char CMD_RESET[7] = "{RSET}"; // Reset board to default state
const char CMD_STAT[7] = "{STAT}"; // Start sending touch state
const char CMD_HALT[7] = "{HALT}"; // Stop sending touch state
static HRESULT read_fake_com0(void *bytes, uint32_t *nbytes);
static HRESULT read_fake_com1(void *bytes, uint32_t *nbytes);
static HRESULT read_fake_com2(void *bytes, uint32_t *nbytes);
static HRESULT touch_handle_irp(struct irp *irp);
static HRESULT touch0_handle_irp_locked(struct irp *irp);
static HRESULT touch1_handle_irp_locked(struct irp *irp);
bool touch0_auto = false;
bool touch1_auto = false;
static CRITICAL_SECTION touch0_lock;
static struct uart touch0_uart;
static uint8_t touch0_written_bytes[520];
static uint8_t touch0_readable_bytes[520];
static CRITICAL_SECTION touch1_lock;
static struct uart touch1_uart;
static uint8_t touch1_written_bytes[520];
static uint8_t touch1_readable_bytes[520];
static const struct reg_hook_val fake_com_keys[] = {
{
.name = L"\\Device\\RealTouchBoard0",
.read = read_fake_com0,
.type = REG_SZ,
},{
.name = L"\\Device\\RealTouchBoard1",
.read = read_fake_com1,
.type = REG_SZ,
},{
.name = L"\\Device\\RealLedBoard0",
.read = read_fake_com2,
.type = REG_SZ,
},
};
HRESULT touch_hook_init(const struct touch_config *cfg)
{
HRESULT hr;
if (!cfg->enable) {
return S_FALSE;
}
dprintf("Mai2 touch: Init\n");
InitializeCriticalSection(&touch0_lock);
InitializeCriticalSection(&touch1_lock);
hr = reg_hook_push_key(
HKEY_LOCAL_MACHINE,
L"HARDWARE\\DEVICEMAP\\SERIALCOMM",
fake_com_keys,
_countof(fake_com_keys));
if (FAILED(hr)) {
return hr;
}
uart_init(&touch0_uart, 3);
touch0_uart.written.bytes = touch0_written_bytes;
touch0_uart.written.nbytes = sizeof(touch0_written_bytes);
touch0_uart.readable.bytes = touch0_readable_bytes;
touch0_uart.readable.nbytes = sizeof(touch0_readable_bytes);
uart_init(&touch1_uart, 4);
touch1_uart.written.bytes = touch1_written_bytes;
touch1_uart.written.nbytes = sizeof(touch1_written_bytes);
touch1_uart.readable.bytes = touch1_readable_bytes;
touch1_uart.readable.nbytes = sizeof(touch1_readable_bytes);
return iohook_push_handler(touch_handle_irp);
}
static HRESULT read_fake_com0(void *bytes, uint32_t *nbytes)
{
// dprintf("Mai2 Touch: Read COM3 reg val\n");
return reg_hook_read_wstr(bytes, nbytes, L"COM3");
}
static HRESULT read_fake_com1(void *bytes, uint32_t *nbytes)
{
// dprintf("Mai2 Touch: Read COM4 reg val\n");
return reg_hook_read_wstr(bytes, nbytes, L"COM4");
}
static HRESULT read_fake_com2(void *bytes, uint32_t *nbytes)
{
// dprintf("Mai2 LED: Read COM20 reg val\n");
return reg_hook_read_wstr(bytes, nbytes, L"COM20");
}
static HRESULT touch_handle_irp(struct irp *irp)
{
HRESULT hr;
assert(irp != NULL);
if (uart_match_irp(&touch0_uart, irp)) {
EnterCriticalSection(&touch0_lock);
hr = touch0_handle_irp_locked(irp);
LeaveCriticalSection(&touch0_lock);
}
else if (uart_match_irp(&touch1_uart, irp)) {
EnterCriticalSection(&touch1_lock);
hr = touch1_handle_irp_locked(irp);
LeaveCriticalSection(&touch1_lock);
}
else {
return iohook_invoke_next(irp);
}
return hr;
}
static HRESULT touch0_handle_irp_locked(struct irp *irp)
{
HRESULT hr = S_OK;
if (irp->op == IRP_OP_OPEN) {
dprintf("Mai2 touch0: Starting backend\n");
//hr = mai2_dll.touch_init();
if (FAILED(hr)) {
dprintf("Mai2 touch: Backend error: %x\n", (int) hr);
return hr;
}
}
hr = uart_handle_irp(&touch0_uart, irp);
if (FAILED(hr) || irp->op != IRP_OP_WRITE) {
return hr;
}
for (;;) {
#if 0
dprintf("TX0 Buffer:\n");
dump_iobuf(&touch0_uart.written);
#endif
//hr = touch_frame_decode(&req, &touch0_uart.written, 0);
if (hr != S_OK) {
if (FAILED(hr)) {
dprintf("Mai2 touch: Deframe error: %x\n", (int) hr);
}
return hr;
}
//hr = touch_req_dispatch(&req);
if (FAILED(hr)) {
dprintf("Mai2 touch: Processing error: %x\n", (int) hr);
}
return hr;
}
}
static HRESULT touch1_handle_irp_locked(struct irp *irp)
{
HRESULT hr = S_OK;
if (irp->op == IRP_OP_OPEN) {
dprintf("Mai2 touch1: Starting backend\n");
//hr = mai2_dll.touch_init();
if (FAILED(hr)) {
dprintf("Mai2 touch: Backend error: %x\n", (int) hr);
return hr;
}
}
hr = uart_handle_irp(&touch0_uart, irp);
if (FAILED(hr) || irp->op != IRP_OP_WRITE) {
return hr;
}
for (;;) {
#if 0
dprintf("TX0 Buffer:\n");
dump_iobuf(&touch0_uart.written);
#endif
//hr = touch_frame_decode(&req, &touch0_uart.written, 0);
if (hr != S_OK) {
if (FAILED(hr)) {
dprintf("Mai2 touch: Deframe error: %x\n", (int) hr);
}
return hr;
}
//hr = touch_req_dispatch(&req);
if (FAILED(hr)) {
dprintf("Mai2 touch: Processing error: %x\n", (int) hr);
}
return hr;
}
}