segatools/hkbhook/io4.c

110 lines
2.1 KiB
C

#include <windows.h>
#include <stdbool.h>
#include <stdint.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include "board/io4.h"
#include "hkbhook/hkb-dll.h"
#include "util/dprintf.h"
bool hkb_io_coin = false;
uint16_t hkb_io_coins = 0;
static HRESULT hkb_io4_poll(void *ctx, struct io4_state *state);
static const struct io4_ops hkb_io4_ops = {
.poll = hkb_io4_poll,
};
HRESULT hkb_io4_hook_init(const struct io4_config *cfg)
{
HRESULT hr;
assert(hkb_dll.init != NULL);
hr = io4_hook_init(cfg, &hkb_io4_ops, NULL);
if (FAILED(hr)) {
return hr;
}
return hkb_dll.init();
}
static HRESULT hkb_io4_poll(void *ctx, struct io4_state *state)
{
uint8_t opbtn;
uint8_t gamebtn;
HRESULT hr;
assert(hkb_dll.poll != NULL);
memset(state, 0, sizeof(*state));
hr = hkb_dll.poll(&opbtn, &gamebtn);
if (FAILED(hr)) {
return hr;
}
if (opbtn & HKB_IO_OPBTN_TEST) {
state->buttons[0] |= IO4_BUTTON_TEST;
}
if (opbtn & HKB_IO_OPBTN_SERVICE) {
state->buttons[0] |= IO4_BUTTON_SERVICE;
}
if (opbtn & HKB_IO_OPBTN_COIN) {
if (!hkb_io_coin) {
hkb_io_coin = true;
hkb_io_coins++;
state->buttons[0] |= 1 << 25;
}
}
else {
hkb_io_coin = false;
}
state->chutes[0] = 128 + 256 * hkb_io_coins;
if (gamebtn & HKB_IO_GAMEBTN_RIGHT) {
state->buttons[1] |= 1 << 1;
}
if (gamebtn & HKB_IO_GAMEBTN_LEFT) {
state->buttons[1] |= 1 << 0;
}
if (gamebtn & HKB_IO_GAMEBTN_UP) {
state->buttons[1] |= 1 << 15;
}
if (gamebtn & HKB_IO_GAMEBTN_DOWN) {
state->buttons[1] |= 1 << 14;
}
if (gamebtn & HKB_IO_GAMEBTN_ENTER) {
state->buttons[1] |= 1 << 13;
}
if (gamebtn & HKB_IO_GAMEBTN_CANCEL) {
state->buttons[1] |= 1 << 12;
}
if (gamebtn & HKB_IO_GAMEBTN_ARR_RIGHT) {
state->buttons[1] |= 1 << 2;
}
if (gamebtn & HKB_IO_GAMEBTN_ARR_LEFT) {
state->buttons[1] |= 1 << 3;
}
return S_OK;
}