Files
TeamTofuShop_taitools/sivaio/sivaio.c
T

116 lines
3.1 KiB
C

#include <windows.h>
#include <xinput.h>
#include <limits.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdbool.h>
#include "sivaio/sivaio.h"
#include "sivaio/config.h"
#include "util/dprintf.h"
static bool siva_io_coin = false;
static bool siva_io_service = false;
static uint16_t siva_coin_ct = 0;
static uint16_t siva_service_ct = 0;
static struct siva_input_config cfg;
static HANDLE selected_ri_dev = (HANDLE)-1;
uint16_t siva_io_get_api_version(void)
{
return 0x0100;
}
HRESULT siva_io_init(void)
{
wchar_t ri_dev_name[PATH_MAX] = L"\0";
UINT num_ri_devs = 0;
UINT len_ri_dev_name = 0;
DWORD err = 0;
PRAWINPUTDEVICELIST ri_devs = NULL;
dprintf("Siva IO: Init\n");
siva_io_config_load(&cfg, L".\\bananatools.ini");
if (GetRawInputDeviceList(NULL, &num_ri_devs, sizeof(RAWINPUTDEVICELIST)) != 0) {
err = GetLastError();
dprintf("Siva IO: GetRawInputDeviceList failed to get number of devices %08lX\n", err);
return E_FAIL;
}
ri_devs = malloc(sizeof(RAWINPUTDEVICELIST) * num_ri_devs);
GetRawInputDeviceList(ri_devs, &num_ri_devs, sizeof(RAWINPUTDEVICELIST));
if (num_ri_devs <= 0) {
err = GetLastError();
dprintf("Siva IO: GetRawInputDeviceList failed to get devices info %08lX\n", err);
free(ri_devs);
return E_FAIL;
}
dprintf("Siva IO: Raw Input device list (%d devices):\n", num_ri_devs);
for (int i = 0; i < num_ri_devs; i++) {
len_ri_dev_name = 0;
if (GetRawInputDeviceInfoW(ri_devs[i].hDevice, RIDI_DEVICENAME, NULL, &len_ri_dev_name) != 0) {
dprintf("Siva IO: GetRawInputDeviceInfoW #%d failed to get name size\n", i);
break;
}
if (len_ri_dev_name <= 0) {
dprintf("Siva IO: GetRawInputDeviceInfoW #%d len_ri_dev_name <= 0\n", i);
break;
}
if (len_ri_dev_name >= MAX_PATH) {
dprintf("\tDev #%d - (name too long, %d characters)\n", i, len_ri_dev_name);
continue;
}
if (GetRawInputDeviceInfoW(ri_devs[i].hDevice, RIDI_DEVICENAME, ri_dev_name, &len_ri_dev_name) <= 0) {
dprintf("Siva IO: GetRawInputDeviceInfoW #%d failed to get name\n", i);
break;
}
dprintf("\tDev #%d - %ls\n", i, ri_dev_name);
}
free(ri_devs);
return S_OK;
}
void siva_io_get_btns(uint8_t *btn, uint8_t *stick)
{
if (GetAsyncKeyState(cfg.test) & 0x8000) {
*btn |= BTN_TEST;
}
if (GetAsyncKeyState(cfg.service) & 0x8000) {
*btn |= BTN_SERVICE;
}
}
void siva_io_read_coin_counter(uint16_t *coins, uint16_t *services)
{
if (GetAsyncKeyState(cfg.coin) & 0x8000) {
if (!siva_io_coin) {
siva_io_coin = true;
siva_coin_ct++;
}
} else {
siva_io_coin = false;
}
if (GetAsyncKeyState(cfg.service) & 0x8000) {
if (!siva_io_service) {
siva_io_service = true;
siva_service_ct++;
}
} else {
siva_io_service = false;
}
*coins = siva_coin_ct;
*services = siva_service_ct;
}