2019-05-04 02:36:23 +02:00
|
|
|
#include <windows.h>
|
|
|
|
#include <xinput.h>
|
|
|
|
|
2023-08-15 17:20:27 +02:00
|
|
|
#include <math.h>
|
2019-05-05 00:47:34 +02:00
|
|
|
#include <assert.h>
|
2019-05-04 02:36:23 +02:00
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
|
2019-05-05 00:47:34 +02:00
|
|
|
#include "idzio/backend.h"
|
2019-11-22 01:59:08 +01:00
|
|
|
#include "idzio/config.h"
|
2019-05-04 02:36:23 +02:00
|
|
|
#include "idzio/idzio.h"
|
2019-05-05 00:55:32 +02:00
|
|
|
#include "idzio/shifter.h"
|
2019-05-05 00:47:34 +02:00
|
|
|
#include "idzio/xi.h"
|
2019-05-04 02:36:23 +02:00
|
|
|
|
2019-05-05 00:47:34 +02:00
|
|
|
#include "util/dprintf.h"
|
2019-05-04 02:36:23 +02:00
|
|
|
|
2019-05-05 00:47:34 +02: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);
|
|
|
|
|
2019-11-22 01:59:08 +01:00
|
|
|
static HRESULT idz_xi_config_apply(const struct idz_xi_config *cfg);
|
|
|
|
|
2019-05-05 00:47:34 +02:00
|
|
|
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,
|
|
|
|
};
|
|
|
|
|
2019-11-22 01:59:08 +01:00
|
|
|
static bool idz_xi_single_stick_steering;
|
2023-08-15 17:20:27 +02:00
|
|
|
static bool idz_xi_linear_steering;
|
2023-08-29 02:22:05 +02:00
|
|
|
static uint16_t idz_xi_left_stick_deadzone;
|
|
|
|
static uint16_t idz_xi_right_stick_deadzone;
|
2019-11-22 01:59:08 +01:00
|
|
|
|
2023-10-05 00:46:54 +02:00
|
|
|
const uint16_t max_stick_value = 32767;
|
|
|
|
/* Apply steering wheel restriction. Real cabs only report about 76% of
|
|
|
|
the output value when the wheel is turned to either of its maximum positions. */
|
|
|
|
const uint16_t max_wheel_value = 24831;
|
|
|
|
|
2019-11-22 01:59:08 +01:00
|
|
|
HRESULT idz_xi_init(const struct idz_xi_config *cfg, const struct idz_io_backend **backend)
|
2019-05-04 02:36:23 +02:00
|
|
|
{
|
2019-11-22 01:59:08 +01:00
|
|
|
HRESULT hr;
|
|
|
|
assert(cfg != NULL);
|
2019-05-05 00:47:34 +02:00
|
|
|
assert(backend != NULL);
|
|
|
|
|
2019-11-22 01:59:08 +01:00
|
|
|
hr = idz_xi_config_apply(cfg);
|
|
|
|
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
|
2019-09-29 22:37:25 +02:00
|
|
|
dprintf("XInput: Using XInput controller\n");
|
2019-05-05 00:47:34 +02:00
|
|
|
*backend = &idz_xi_backend;
|
|
|
|
|
2019-05-04 02:36:23 +02:00
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2023-08-29 02:22:05 +02:00
|
|
|
static HRESULT idz_xi_config_apply(const struct idz_xi_config *cfg) {
|
|
|
|
/* Deadzones check */
|
|
|
|
if (cfg->left_stick_deadzone > 32767 || cfg->left_stick_deadzone < 0) {
|
|
|
|
dprintf("XInput: Left stick deadzone is too large or negative\n");
|
|
|
|
return E_INVALIDARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cfg->right_stick_deadzone > 32767 || cfg->right_stick_deadzone < 0) {
|
|
|
|
dprintf("XInput: Right stick deadzone is too large or negative\n");
|
|
|
|
return E_INVALIDARG;
|
|
|
|
}
|
|
|
|
|
2019-11-22 01:59:08 +01:00
|
|
|
dprintf("XInput: --- Begin configuration ---\n");
|
|
|
|
dprintf("XInput: Single Stick Steering : %i\n", cfg->single_stick_steering);
|
2023-08-15 17:20:27 +02:00
|
|
|
dprintf("XInput: Linear Steering . . . : %i\n", cfg->linear_steering);
|
2023-08-29 02:22:05 +02:00
|
|
|
dprintf("XInput: Left Deadzone . . . . : %i\n", cfg->left_stick_deadzone);
|
|
|
|
dprintf("XInput: Right Deadzone . . . : %i\n", cfg->right_stick_deadzone);
|
2019-11-22 01:59:08 +01:00
|
|
|
dprintf("XInput: --- End configuration ---\n");
|
|
|
|
|
|
|
|
idz_xi_single_stick_steering = cfg->single_stick_steering;
|
2023-08-15 17:20:27 +02:00
|
|
|
idz_xi_linear_steering = cfg->linear_steering;
|
2023-08-29 02:22:05 +02:00
|
|
|
idz_xi_left_stick_deadzone = cfg->left_stick_deadzone;
|
|
|
|
idz_xi_right_stick_deadzone = cfg->right_stick_deadzone;
|
2019-11-22 01:59:08 +01:00
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2019-05-05 00:47:34 +02:00
|
|
|
static void idz_xi_jvs_read_buttons(uint8_t *gamebtn_out)
|
2019-05-04 02:36:23 +02:00
|
|
|
{
|
|
|
|
uint8_t gamebtn;
|
|
|
|
XINPUT_STATE xi;
|
|
|
|
WORD xb;
|
|
|
|
|
2019-05-05 00:47:34 +02:00
|
|
|
assert(gamebtn_out != NULL);
|
2019-05-04 02:36:23 +02:00
|
|
|
|
2019-05-05 00:47:34 +02:00
|
|
|
gamebtn = 0;
|
2019-05-04 02:36:23 +02: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 20:28:12 +02:00
|
|
|
if (xb & (XINPUT_GAMEPAD_START | XINPUT_GAMEPAD_A)) {
|
2019-05-04 02:36:23 +02:00
|
|
|
gamebtn |= IDZ_IO_GAMEBTN_START;
|
|
|
|
}
|
|
|
|
|
2019-05-09 20:28:12 +02:00
|
|
|
if (xb & (XINPUT_GAMEPAD_BACK | XINPUT_GAMEPAD_B)) {
|
2019-05-04 02:36:23 +02:00
|
|
|
gamebtn |= IDZ_IO_GAMEBTN_VIEW_CHANGE;
|
|
|
|
}
|
|
|
|
|
|
|
|
*gamebtn_out = gamebtn;
|
|
|
|
}
|
|
|
|
|
2019-05-05 00:47:34 +02:00
|
|
|
static void idz_xi_jvs_read_shifter(uint8_t *gear)
|
2019-05-04 02:36:23 +02:00
|
|
|
{
|
2019-05-05 00:55:32 +02:00
|
|
|
bool shift_dn;
|
|
|
|
bool shift_up;
|
2019-05-04 02:36:23 +02:00
|
|
|
XINPUT_STATE xi;
|
|
|
|
WORD xb;
|
|
|
|
|
2019-05-05 00:47:34 +02:00
|
|
|
assert(gear != NULL);
|
|
|
|
|
2019-05-04 02:36:23 +02:00
|
|
|
memset(&xi, 0, sizeof(xi));
|
|
|
|
XInputGetState(0, &xi);
|
|
|
|
xb = xi.Gamepad.wButtons;
|
|
|
|
|
|
|
|
if (xb & XINPUT_GAMEPAD_START) {
|
2019-05-05 00:55:32 +02:00
|
|
|
/* Reset to Neutral when start is pressed */
|
|
|
|
idz_shifter_reset();
|
2019-05-04 02:36:23 +02:00
|
|
|
}
|
|
|
|
|
2019-05-05 00:55:32 +02:00
|
|
|
shift_dn = xb & (XINPUT_GAMEPAD_Y | XINPUT_GAMEPAD_LEFT_SHOULDER);
|
|
|
|
shift_up = xb & (XINPUT_GAMEPAD_X | XINPUT_GAMEPAD_RIGHT_SHOULDER);
|
2019-05-04 02:36:23 +02:00
|
|
|
|
2019-05-05 00:55:32 +02:00
|
|
|
idz_shifter_update(shift_dn, shift_up);
|
2019-05-04 02:36:23 +02:00
|
|
|
|
2019-05-05 00:55:32 +02:00
|
|
|
*gear = idz_shifter_current_gear();
|
2019-05-04 02:36:23 +02:00
|
|
|
}
|
|
|
|
|
2023-10-05 00:46:54 +02:00
|
|
|
static int16_t calculate_norm_steering(int16_t axis, uint16_t deadzone, bool linear_steering) {
|
|
|
|
// determine how far the controller is pushed
|
|
|
|
float magnitude = sqrt(axis*axis);
|
2023-08-15 17:20:27 +02:00
|
|
|
|
2023-10-05 00:46:54 +02:00
|
|
|
// determine the direction the controller is pushed
|
|
|
|
float norm_axis = axis / magnitude;
|
2023-08-15 17:20:27 +02:00
|
|
|
|
2023-10-05 00:46:54 +02:00
|
|
|
float norm_magnitude = 0.0;
|
2023-08-15 17:20:27 +02:00
|
|
|
|
2023-10-05 00:46:54 +02:00
|
|
|
// check if the controller is outside a circular dead zone
|
|
|
|
if (magnitude > deadzone)
|
|
|
|
{
|
|
|
|
// clip the magnitude at its expected maximum value
|
|
|
|
if (magnitude > max_stick_value) magnitude = max_stick_value;
|
2023-08-15 17:20:27 +02:00
|
|
|
|
2023-10-05 00:46:54 +02:00
|
|
|
// adjust magnitude relative to the end of the dead zone
|
|
|
|
magnitude -= deadzone;
|
2023-08-15 17:20:27 +02:00
|
|
|
|
2023-10-05 00:46:54 +02:00
|
|
|
// optionally normalize the magnitude with respect to its expected range
|
|
|
|
// giving a magnitude value of 0.0 to 1.0
|
|
|
|
norm_magnitude = magnitude / (max_stick_value - deadzone);
|
|
|
|
} else // if the controller is in the deadzone zero out the magnitude
|
|
|
|
{
|
|
|
|
magnitude = 0.0;
|
|
|
|
norm_magnitude = 0.0;
|
|
|
|
}
|
2023-08-15 17:20:27 +02:00
|
|
|
|
2023-10-05 00:46:54 +02:00
|
|
|
// apply non-linear transform to the axis
|
|
|
|
if (!linear_steering) {
|
|
|
|
return norm_axis * pow(norm_magnitude, 3.0) * max_wheel_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
return norm_axis * norm_magnitude * max_wheel_value;
|
2023-08-15 17:20:27 +02:00
|
|
|
}
|
|
|
|
|
2019-05-05 00:47:34 +02:00
|
|
|
static void idz_xi_jvs_read_analogs(struct idz_io_analog_state *out)
|
2019-05-04 02:36:23 +02:00
|
|
|
{
|
|
|
|
XINPUT_STATE xi;
|
|
|
|
int left;
|
|
|
|
int right;
|
|
|
|
|
2019-05-05 00:47:34 +02:00
|
|
|
assert(out != NULL);
|
|
|
|
|
2019-05-04 02:36:23 +02:00
|
|
|
memset(&xi, 0, sizeof(xi));
|
|
|
|
XInputGetState(0, &xi);
|
|
|
|
|
|
|
|
left = xi.Gamepad.sThumbLX;
|
|
|
|
right = xi.Gamepad.sThumbRX;
|
2023-08-15 17:20:27 +02:00
|
|
|
|
2023-10-05 00:46:54 +02:00
|
|
|
// normalize the steering axis
|
|
|
|
left = calculate_norm_steering(left, idz_xi_left_stick_deadzone, idz_xi_linear_steering);
|
|
|
|
right = calculate_norm_steering(right, idz_xi_right_stick_deadzone, idz_xi_linear_steering);
|
2019-05-04 02:36:23 +02:00
|
|
|
|
2019-11-22 01:59:08 +01:00
|
|
|
if(idz_xi_single_stick_steering) {
|
|
|
|
out->wheel = left;
|
|
|
|
} else {
|
|
|
|
out->wheel = (left + right) / 2;
|
|
|
|
}
|
|
|
|
|
2019-05-04 02:36:23 +02:00
|
|
|
out->accel = xi.Gamepad.bRightTrigger << 8;
|
|
|
|
out->brake = xi.Gamepad.bLeftTrigger << 8;
|
|
|
|
}
|