diff --git a/common/util/fg-detect.c b/common/util/fg-detect.c index e5ed83c..a05e128 100644 --- a/common/util/fg-detect.c +++ b/common/util/fg-detect.c @@ -31,6 +31,7 @@ static HWND get_window(){ return NULL; } } + dprintf("FG-Detect: Program window detected\n"); window_handle = hwnd; return window_handle; diff --git a/games/mercuryhook/touch.c b/games/mercuryhook/touch.c index 89189bb..6c16c9f 100644 --- a/games/mercuryhook/touch.c +++ b/games/mercuryhook/touch.c @@ -20,6 +20,7 @@ #include "util/dprintf.h" #include "util/dump.h" +#include "util/fg-detect.h" const char SYNC_BOARD_VER[6] = "190523"; const char UNIT_BOARD_VER[6] = "190514"; @@ -36,8 +37,8 @@ static uint8_t calc_checksum(const void *ptr, size_t nbytes); static HRESULT touch_handle_get_sync_board_ver(const struct touch_req *req); static HRESULT touch_handle_next_read(const struct touch_req *req); static HRESULT touch_handle_get_unit_board_ver(const struct touch_req *req); -static HRESULT touch_handle_mystery1(const struct touch_req *req); -static HRESULT touch_handle_mystery2(const struct touch_req *req); +static HRESULT touch_handle_get_unit_board_alive(const struct touch_req *req); +static HRESULT touch_handle_set_onoff_threshold(const struct touch_req *req); static HRESULT touch_handle_start_auto_scan(const struct touch_req *req); static void touch_res_auto_scan(const bool *state); @@ -46,6 +47,9 @@ static uint8_t input_frame_count_1 = 0; static bool touch0_auto = false; static bool touch1_auto = false; +static uint8_t prev_dataR[TOUCH_DATA_SIZE] = { 0 }; +static uint8_t prev_dataL[TOUCH_DATA_SIZE] = { 0 }; + static CRITICAL_SECTION touch0_lock; static struct uart touch0_uart; static uint8_t touch0_written_bytes[520]; @@ -211,17 +215,17 @@ static HRESULT touch_req_dispatch(const struct touch_req *req) return touch_handle_next_read(req); case CMD_GET_UNIT_BOARD_VER: return touch_handle_get_unit_board_ver(req); - case CMD_MYSTERY1: - return touch_handle_mystery1(req); - case CMD_MYSTERY2: - return touch_handle_mystery2(req); + case CMD_GET_UNIT_BOARD_ALIVE: + return touch_handle_get_unit_board_alive(req); + case CMD_SET_ONOFF_THRESHOLD: + return touch_handle_set_onoff_threshold(req); case CMD_START_AUTO_SCAN: return touch_handle_start_auto_scan(req); case CMD_BEGIN_WRITE: dprintf("Wacca Touch: Begin write for side %d\n", req->side); return S_OK; case CMD_NEXT_WRITE: - dprintf("Wacca Touch: continue write for side %d\n", req->side); + dprintf("Wacca Touch: Continue write for side %d\n", req->side); return S_OK; default: dprintf("Wacca Touch: Unhandled command %02x\n", req->cmd); @@ -236,7 +240,7 @@ static HRESULT touch_handle_get_sync_board_ver(const struct touch_req *req) memset(&resp, 0, sizeof(resp)); dprintf("Wacca Touch%d: Get sync board version\n", req->side); - resp.cmd = 0xa0; + resp.cmd = CMD_GET_SYNC_BOARD_VER; memcpy(resp.version, SYNC_BOARD_VER, sizeof(SYNC_BOARD_VER)); resp.checksum = 0; resp.checksum = calc_checksum(&resp, sizeof(resp)); @@ -259,7 +263,6 @@ static HRESULT touch_handle_next_read(const struct touch_req *req) memset(&resp, 0, sizeof(resp)); dprintf("Wacca Touch%d: Read section %2hx\n", req->side, req->data[2]); - switch (req->data[2]) { // These can be found in the config file case 0x30: @@ -303,7 +306,7 @@ static HRESULT touch_handle_get_unit_board_ver(const struct touch_req *req) memcpy(&resp.version[7 + (6 * i)], UNIT_BOARD_VER, sizeof(UNIT_BOARD_VER)); } - resp.cmd = 0xa8; + resp.cmd = CMD_GET_UNIT_BOARD_VER; resp.checksum = 0; if (req->side == 0) { @@ -336,14 +339,14 @@ static HRESULT touch_handle_get_unit_board_ver(const struct touch_req *req) return hr; } -static HRESULT touch_handle_mystery1(const struct touch_req *req) +static HRESULT touch_handle_get_unit_board_alive(const struct touch_req *req) { - struct touch_resp_mystery1 resp; + struct touch_resp_get_unit_board_alive resp; HRESULT hr; memset(&resp, 0, sizeof(resp)); - dprintf("Wacca Touch%d: Command A2\n", req->side); + dprintf("Wacca Touch%d: Get unit board alive\n", req->side); - resp.cmd = 0xa2; + resp.cmd = CMD_GET_UNIT_BOARD_ALIVE; resp.data = 0x3f; resp.checksum = 0; resp.checksum = calc_checksum(&resp, sizeof(resp)); @@ -357,14 +360,14 @@ static HRESULT touch_handle_mystery1(const struct touch_req *req) return hr; } -static HRESULT touch_handle_mystery2(const struct touch_req *req) +static HRESULT touch_handle_set_onoff_threshold(const struct touch_req *req) { - struct touch_resp_mystery2 resp; + struct touch_resp_set_onoff_threshold resp; HRESULT hr; memset(&resp, 0, sizeof(resp)); - dprintf("Wacca Touch%d: Command 94\n", req->side); + dprintf("Wacca Touch%d: Set on/off threshold\n", req->side); - resp.cmd = 0x94; + resp.cmd = CMD_SET_ONOFF_THRESHOLD; resp.data = 0; resp.checksum = 0; resp.checksum = calc_checksum(&resp, sizeof(resp)); @@ -397,7 +400,7 @@ static HRESULT touch_handle_start_auto_scan(const struct touch_req *req) dprintf("\n"); #endif - resp.cmd = 0xC9; + resp.cmd = CMD_START_AUTO_SCAN; resp.data = 0; resp.checksum = 0x49; @@ -427,10 +430,13 @@ static void touch_res_auto_scan(const bool *state) { struct touch_input_frame frame0; struct touch_input_frame frame1; + memset(&frame0, 0, sizeof(frame0)); memset(&frame1, 0, sizeof(frame1)); - uint8_t dataR[24] = { 0 }; - uint8_t dataL[24] = { 0 }; + + uint8_t dataR[TOUCH_DATA_SIZE] = { 0 }; + uint8_t dataL[TOUCH_DATA_SIZE] = { 0 }; + // this changes every input on a real board but // the game doesn't seem to care about it... uint8_t data2[9] = { 0x0d, 0x03, 0x02, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00 }; @@ -449,16 +455,26 @@ static void touch_res_auto_scan(const bool *state) input_frame_count_1 = 0; } - for (int i = 0; i < 24; i++) { - for (int j = 0; j < 5; j++) { - if (state[counter]) { - dataR[i] |= (1 << j); + fgdet_poll(); + if (fgdet_in_foreground()) { + for (int i = 0; i < 24; i++) { + for (int j = 0; j < 5; j++) { + if (state[counter]) { + dataR[i] |= (1 << j); + } + if (state[counter+120]) { + dataL[i] |= (1 << j); + } + counter++; } - if (state[counter+120]) { - dataL[i] |= (1 << j); - } - counter++; } + + memcpy(prev_dataL, dataL, sizeof(dataL)); + memcpy(prev_dataR, dataR, sizeof(dataR)); + } else { + // if we're unfocused, freeze the current input + memcpy(dataL, prev_dataL, sizeof(prev_dataL)); + memcpy(dataR, prev_dataR, sizeof(prev_dataR)); } memcpy(frame0.data1, dataR, sizeof(dataR)); @@ -474,14 +490,14 @@ static void touch_res_auto_scan(const bool *state) frame1.checksum = calc_checksum(&frame1, sizeof(frame1)); if (touch0_auto) { - //dprintf("Wacca Touch: Touch0 auto frame #%2hx sent\n", frame0.count); + // dprintf("Wacca Touch: Touch0 auto frame #%2hx sent\n", frame0.count); EnterCriticalSection(&touch0_lock); iobuf_write(&touch0_uart.readable, &frame0, sizeof(frame0)); LeaveCriticalSection(&touch0_lock); } if (touch1_auto) { - //dprintf("Wacca Touch: Touch1 auto frame #%2hx sent\n", frame0.count); + // dprintf("Wacca Touch: Touch1 auto frame #%2hx sent\n", frame0.count); EnterCriticalSection(&touch1_lock); iobuf_write(&touch1_uart.readable, &frame1, sizeof(frame1)); LeaveCriticalSection(&touch1_lock); @@ -519,9 +535,10 @@ static uint8_t calc_checksum(const void *ptr, size_t nbytes) src = ptr; for (size_t i = 0; i < nbytes; i++) { - //dprintf("Wacca Touch: Calculating %2hx\n", src[i]); + // dprintf("Wacca Touch: Calculating %2hx\n", src[i]); checksum = checksum^(src[i]); } - //dprintf("Wacca Touch: Checksum is %2hx\n", checksum&0x7f); + + // dprintf("Wacca Touch: Checksum is %2hx\n", checksum&0x7f); return checksum&0x7f; } diff --git a/games/mercuryhook/touch.h b/games/mercuryhook/touch.h index cc665c8..3445214 100644 --- a/games/mercuryhook/touch.h +++ b/games/mercuryhook/touch.h @@ -8,15 +8,19 @@ struct touch_config { bool enable; }; +enum { + TOUCH_DATA_SIZE = 0x18, +}; + enum touch_cmd { - CMD_GET_SYNC_BOARD_VER = 0xa0, - CMD_NEXT_READ = 0x72, - CMD_GET_UNIT_BOARD_VER = 0xa8, - CMD_MYSTERY1 = 0xa2, - CMD_MYSTERY2 = 0x94, - CMD_START_AUTO_SCAN = 0xc9, - CMD_BEGIN_WRITE = 0x77, - CMD_NEXT_WRITE = 0x20 + CMD_NEXT_WRITE = 0x20, + CMD_NEXT_READ = 0x72, + CMD_BEGIN_WRITE = 0x77, + CMD_SET_ONOFF_THRESHOLD = 0x94, + CMD_GET_SYNC_BOARD_VER = 0xA0, + CMD_GET_UNIT_BOARD_ALIVE = 0xA2, + CMD_GET_UNIT_BOARD_VER = 0xA8, + CMD_START_AUTO_SCAN = 0xC9 }; struct touch_req { @@ -28,7 +32,7 @@ struct touch_req { struct touch_input_frame { uint8_t cmd; - uint8_t data1[24]; + uint8_t data1[TOUCH_DATA_SIZE]; uint8_t data2[9]; uint8_t count; uint8_t checksum; @@ -51,13 +55,13 @@ struct touch_resp_get_unit_board_ver { uint8_t checksum; }; -struct touch_resp_mystery1 { +struct touch_resp_get_unit_board_alive { uint8_t cmd; uint8_t data; uint8_t checksum; }; -struct touch_resp_mystery2 { +struct touch_resp_set_onoff_threshold { uint8_t cmd; uint8_t data; uint8_t checksum;