2019-05-04 00:36:23 +00:00
|
|
|
#include <windows.h>
|
|
|
|
#include <xinput.h>
|
|
|
|
|
2019-05-04 22:47:34 +00:00
|
|
|
#include <assert.h>
|
2019-05-04 00:36:23 +00:00
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
|
2019-05-04 22:47:34 +00:00
|
|
|
#include "idzio/backend.h"
|
2019-05-04 00:36:23 +00:00
|
|
|
#include "idzio/idzio.h"
|
2019-05-04 22:55:32 +00:00
|
|
|
#include "idzio/shifter.h"
|
2019-05-04 22:47:34 +00:00
|
|
|
#include "idzio/xi.h"
|
2019-05-04 00:36:23 +00:00
|
|
|
|
2019-05-04 22:47:34 +00:00
|
|
|
#include "util/dprintf.h"
|
2019-05-04 00:36:23 +00:00
|
|
|
|
2019-05-04 22:47:34 +00:00
|
|
|
static void idz_xi_jvs_read_buttons(uint8_t *gamebtn_out);
|
|
|
|
static void idz_xi_jvs_read_shifter(uint8_t *gear);
|
|
|
|
static void idz_xi_jvs_read_analogs(struct idz_io_analog_state *out);
|
|
|
|
|
|
|
|
static const struct idz_io_backend idz_xi_backend = {
|
|
|
|
.jvs_read_buttons = idz_xi_jvs_read_buttons,
|
|
|
|
.jvs_read_shifter = idz_xi_jvs_read_shifter,
|
|
|
|
.jvs_read_analogs = idz_xi_jvs_read_analogs,
|
|
|
|
};
|
|
|
|
|
|
|
|
HRESULT idz_xi_init(const struct idz_io_backend **backend)
|
2019-05-04 00:36:23 +00:00
|
|
|
{
|
2019-05-04 22:47:34 +00:00
|
|
|
assert(backend != NULL);
|
|
|
|
|
2019-09-29 20:37:25 +00:00
|
|
|
dprintf("XInput: Using XInput controller\n");
|
2019-05-04 22:47:34 +00:00
|
|
|
*backend = &idz_xi_backend;
|
|
|
|
|
2019-05-04 00:36:23 +00:00
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2019-05-04 22:47:34 +00:00
|
|
|
static void idz_xi_jvs_read_buttons(uint8_t *gamebtn_out)
|
2019-05-04 00:36:23 +00:00
|
|
|
{
|
|
|
|
uint8_t gamebtn;
|
|
|
|
XINPUT_STATE xi;
|
|
|
|
WORD xb;
|
|
|
|
|
2019-05-04 22:47:34 +00:00
|
|
|
assert(gamebtn_out != NULL);
|
2019-05-04 00:36:23 +00:00
|
|
|
|
2019-05-04 22:47:34 +00:00
|
|
|
gamebtn = 0;
|
2019-05-04 00:36:23 +00:00
|
|
|
|
|
|
|
memset(&xi, 0, sizeof(xi));
|
|
|
|
XInputGetState(0, &xi);
|
|
|
|
xb = xi.Gamepad.wButtons;
|
|
|
|
|
|
|
|
if (xb & XINPUT_GAMEPAD_DPAD_UP) {
|
|
|
|
gamebtn |= IDZ_IO_GAMEBTN_UP;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (xb & XINPUT_GAMEPAD_DPAD_DOWN) {
|
|
|
|
gamebtn |= IDZ_IO_GAMEBTN_DOWN;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (xb & XINPUT_GAMEPAD_DPAD_LEFT) {
|
|
|
|
gamebtn |= IDZ_IO_GAMEBTN_LEFT;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (xb & XINPUT_GAMEPAD_DPAD_RIGHT) {
|
|
|
|
gamebtn |= IDZ_IO_GAMEBTN_RIGHT;
|
|
|
|
}
|
|
|
|
|
2019-05-09 18:28:12 +00:00
|
|
|
if (xb & (XINPUT_GAMEPAD_START | XINPUT_GAMEPAD_A)) {
|
2019-05-04 00:36:23 +00:00
|
|
|
gamebtn |= IDZ_IO_GAMEBTN_START;
|
|
|
|
}
|
|
|
|
|
2019-05-09 18:28:12 +00:00
|
|
|
if (xb & (XINPUT_GAMEPAD_BACK | XINPUT_GAMEPAD_B)) {
|
2019-05-04 00:36:23 +00:00
|
|
|
gamebtn |= IDZ_IO_GAMEBTN_VIEW_CHANGE;
|
|
|
|
}
|
|
|
|
|
|
|
|
*gamebtn_out = gamebtn;
|
|
|
|
}
|
|
|
|
|
2019-05-04 22:47:34 +00:00
|
|
|
static void idz_xi_jvs_read_shifter(uint8_t *gear)
|
2019-05-04 00:36:23 +00:00
|
|
|
{
|
2019-05-04 22:55:32 +00:00
|
|
|
bool shift_dn;
|
|
|
|
bool shift_up;
|
2019-05-04 00:36:23 +00:00
|
|
|
XINPUT_STATE xi;
|
|
|
|
WORD xb;
|
|
|
|
|
2019-05-04 22:47:34 +00:00
|
|
|
assert(gear != NULL);
|
|
|
|
|
2019-05-04 00:36:23 +00:00
|
|
|
memset(&xi, 0, sizeof(xi));
|
|
|
|
XInputGetState(0, &xi);
|
|
|
|
xb = xi.Gamepad.wButtons;
|
|
|
|
|
|
|
|
if (xb & XINPUT_GAMEPAD_START) {
|
2019-05-04 22:55:32 +00:00
|
|
|
/* Reset to Neutral when start is pressed */
|
|
|
|
idz_shifter_reset();
|
2019-05-04 00:36:23 +00:00
|
|
|
}
|
|
|
|
|
2019-05-04 22:55:32 +00:00
|
|
|
shift_dn = xb & (XINPUT_GAMEPAD_Y | XINPUT_GAMEPAD_LEFT_SHOULDER);
|
|
|
|
shift_up = xb & (XINPUT_GAMEPAD_X | XINPUT_GAMEPAD_RIGHT_SHOULDER);
|
2019-05-04 00:36:23 +00:00
|
|
|
|
2019-05-04 22:55:32 +00:00
|
|
|
idz_shifter_update(shift_dn, shift_up);
|
2019-05-04 00:36:23 +00:00
|
|
|
|
2019-05-04 22:55:32 +00:00
|
|
|
*gear = idz_shifter_current_gear();
|
2019-05-04 00:36:23 +00:00
|
|
|
}
|
|
|
|
|
2019-05-04 22:47:34 +00:00
|
|
|
static void idz_xi_jvs_read_analogs(struct idz_io_analog_state *out)
|
2019-05-04 00:36:23 +00:00
|
|
|
{
|
|
|
|
XINPUT_STATE xi;
|
|
|
|
int left;
|
|
|
|
int right;
|
|
|
|
|
2019-05-04 22:47:34 +00:00
|
|
|
assert(out != NULL);
|
|
|
|
|
2019-05-04 00:36:23 +00:00
|
|
|
memset(&xi, 0, sizeof(xi));
|
|
|
|
XInputGetState(0, &xi);
|
|
|
|
|
|
|
|
left = xi.Gamepad.sThumbLX;
|
|
|
|
|
|
|
|
if (left < -XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE) {
|
|
|
|
left += XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE;
|
|
|
|
} else if (left > XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE) {
|
|
|
|
left -= XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE;
|
|
|
|
} else {
|
|
|
|
left = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
right = xi.Gamepad.sThumbRX;
|
|
|
|
|
|
|
|
if (right < -XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE) {
|
|
|
|
right += XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE;
|
|
|
|
} else if (right > XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE) {
|
|
|
|
right -= XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE;
|
|
|
|
} else {
|
|
|
|
right = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
out->wheel = (left + right) / 2;
|
|
|
|
out->accel = xi.Gamepad.bRightTrigger << 8;
|
|
|
|
out->brake = xi.Gamepad.bLeftTrigger << 8;
|
|
|
|
}
|