diff --git a/board/io3.c b/board/io3.c index a99a3e3..ee566b7 100644 --- a/board/io3.c +++ b/board/io3.c @@ -481,7 +481,7 @@ static HRESULT io3_cmd_read_analogs( struct iobuf *resp_buf) { struct jvs_req_read_analogs req; - uint16_t state; + uint16_t analogs[8]; uint8_t i; HRESULT hr; @@ -493,6 +493,12 @@ static HRESULT io3_cmd_read_analogs( return hr; } + if (req.nanalogs > _countof(analogs)) { + dprintf("JVS I/O: Invalid analog count %i\n", req.nanalogs); + + return E_FAIL; + } + //dprintf("JVS I/O: Read analogs, nanalogs=%i\n", req.nanalogs); /* Write report byte */ @@ -505,14 +511,14 @@ static HRESULT io3_cmd_read_analogs( /* Write analogs */ - for (i = 0 ; i < req.nanalogs ; i++) { - if (io3->ops->read_analog != NULL) { - state = io3->ops->read_analog(io3->ops_ctx, i); - } else { - state = 0; - } + memset(analogs, 0, sizeof(analogs)); - hr = iobuf_write_be16(resp_buf, state); + if (io3->ops->read_analogs != NULL) { + io3->ops->read_analogs(io3->ops_ctx, analogs, req.nanalogs); + } + + for (i = 0 ; i < req.nanalogs ; i++) { + hr = iobuf_write_be16(resp_buf, analogs[i]); if (FAILED(hr)) { return hr; diff --git a/board/io3.h b/board/io3.h index 0d6df07..4bae608 100644 --- a/board/io3.h +++ b/board/io3.h @@ -17,7 +17,7 @@ struct io3_ops { void (*reset)(void *ctx); void (*write_gpio)(void *ctx, uint32_t state); void (*read_switches)(void *ctx, struct io3_switch_state *out); - uint16_t (*read_analog)(void *ctx, uint8_t analog_no); + void (*read_analogs)(void *ctx, uint16_t *analogs, uint8_t nanalogs); uint16_t (*read_coin_counter)(void *ctx, uint8_t slot_no); }; diff --git a/idzhook/jvs.c b/idzhook/jvs.c index 0ad8f63..9d278c2 100644 --- a/idzhook/jvs.c +++ b/idzhook/jvs.c @@ -17,13 +17,16 @@ #include "util/dprintf.h" +static void idz_jvs_read_analogs( + void *ctx, + uint16_t *analogs, + uint8_t nanalogs); static void idz_jvs_read_switches(void *ctx, struct io3_switch_state *out); -static uint16_t idz_jvs_read_analog(void *ctx, uint8_t analog_no); static uint16_t idz_jvs_read_coin_counter(void *ctx, uint8_t slot_no); static const struct io3_ops idz_jvs_io3_ops = { .read_switches = idz_jvs_read_switches, - .read_analog = idz_jvs_read_analog, + .read_analogs = idz_jvs_read_analogs, .read_coin_counter = idz_jvs_read_coin_counter, }; @@ -127,23 +130,23 @@ static void idz_jvs_read_switches(void *ctx, struct io3_switch_state *out) } } -static uint16_t idz_jvs_read_analog(void *ctx, uint8_t analog_no) +static void idz_jvs_read_analogs( + void *ctx, + uint16_t *analogs, + uint8_t nanalogs) { XINPUT_STATE xi; int left; int right; - if (analog_no > 2) { - return 0; - } + assert(analogs != NULL); memset(&xi, 0, sizeof(xi)); XInputGetState(0, &xi); - switch (analog_no) { - case 0: - /* Wheel */ + /* Wheel */ + if (nanalogs > 0) { left = xi.Gamepad.sThumbLX; if (left < -XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE) { @@ -164,18 +167,19 @@ static uint16_t idz_jvs_read_analog(void *ctx, uint8_t analog_no) right = 0; } - return 0x8000 + (left + right) / 2; + analogs[0] = 0x8000 + (left + right) / 2; + } - case 1: - /* Accel */ - return xi.Gamepad.bRightTrigger << 8; + /* Accel */ - case 2: - /* Brake */ - return xi.Gamepad.bLeftTrigger << 8; + if (nanalogs > 1) { + analogs[1] = xi.Gamepad.bRightTrigger << 8; + } - default: - return 0; + /* Brake */ + + if (nanalogs > 2) { + analogs[2] = xi.Gamepad.bLeftTrigger << 8; } }