2021-09-20 22:29:43 +01:00
|
|
|
// Copyright 2021 yuzu Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included
|
|
|
|
|
|
|
|
#include <random>
|
|
|
|
|
|
|
|
#include "common/input.h"
|
|
|
|
#include "core/hid/input_converter.h"
|
|
|
|
|
|
|
|
namespace Core::HID {
|
|
|
|
|
2021-10-31 03:23:10 +00:00
|
|
|
Common::Input::BatteryStatus TransformToBattery(const Common::Input::CallbackStatus& callback) {
|
|
|
|
Common::Input::BatteryStatus battery{Common::Input::BatteryStatus::None};
|
2021-09-20 22:29:43 +01:00
|
|
|
switch (callback.type) {
|
2021-10-31 03:23:10 +00:00
|
|
|
case Common::Input::InputType::Analog:
|
|
|
|
case Common::Input::InputType::Trigger: {
|
2021-09-20 22:29:43 +01:00
|
|
|
const auto value = TransformToTrigger(callback).analog.value;
|
2021-10-31 03:23:10 +00:00
|
|
|
battery = Common::Input::BatteryLevel::Empty;
|
2021-09-20 22:29:43 +01:00
|
|
|
if (value > 0.2f) {
|
2021-10-31 03:23:10 +00:00
|
|
|
battery = Common::Input::BatteryLevel::Critical;
|
2021-09-20 22:29:43 +01:00
|
|
|
}
|
|
|
|
if (value > 0.4f) {
|
2021-10-31 03:23:10 +00:00
|
|
|
battery = Common::Input::BatteryLevel::Low;
|
2021-09-20 22:29:43 +01:00
|
|
|
}
|
|
|
|
if (value > 0.6f) {
|
2021-10-31 03:23:10 +00:00
|
|
|
battery = Common::Input::BatteryLevel::Medium;
|
2021-09-20 22:29:43 +01:00
|
|
|
}
|
|
|
|
if (value > 0.8f) {
|
2021-10-31 03:23:10 +00:00
|
|
|
battery = Common::Input::BatteryLevel::Full;
|
2021-09-20 22:29:43 +01:00
|
|
|
}
|
|
|
|
if (value >= 1.0f) {
|
2021-10-31 03:23:10 +00:00
|
|
|
battery = Common::Input::BatteryLevel::Charging;
|
2021-09-20 22:29:43 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2021-10-31 03:23:10 +00:00
|
|
|
case Common::Input::InputType::Button:
|
|
|
|
battery = callback.button_status.value ? Common::Input::BatteryLevel::Charging
|
|
|
|
: Common::Input::BatteryLevel::Critical;
|
2021-10-25 05:23:54 +01:00
|
|
|
break;
|
2021-10-31 03:23:10 +00:00
|
|
|
case Common::Input::InputType::Battery:
|
2021-09-20 22:29:43 +01:00
|
|
|
battery = callback.battery_status;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
LOG_ERROR(Input, "Conversion from type {} to battery not implemented", callback.type);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return battery;
|
|
|
|
}
|
|
|
|
|
2021-10-31 03:23:10 +00:00
|
|
|
Common::Input::ButtonStatus TransformToButton(const Common::Input::CallbackStatus& callback) {
|
|
|
|
Common::Input::ButtonStatus status{};
|
2021-09-20 22:29:43 +01:00
|
|
|
switch (callback.type) {
|
2021-10-31 03:23:10 +00:00
|
|
|
case Common::Input::InputType::Analog:
|
|
|
|
case Common::Input::InputType::Trigger:
|
2021-10-31 15:41:44 +00:00
|
|
|
status.value = TransformToTrigger(callback).pressed.value;
|
2021-09-20 22:29:43 +01:00
|
|
|
break;
|
2021-10-31 03:23:10 +00:00
|
|
|
case Common::Input::InputType::Button:
|
2021-09-20 22:29:43 +01:00
|
|
|
status = callback.button_status;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
LOG_ERROR(Input, "Conversion from type {} to button not implemented", callback.type);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (status.inverted) {
|
|
|
|
status.value = !status.value;
|
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2021-10-31 03:23:10 +00:00
|
|
|
Common::Input::MotionStatus TransformToMotion(const Common::Input::CallbackStatus& callback) {
|
|
|
|
Common::Input::MotionStatus status{};
|
2021-09-20 22:29:43 +01:00
|
|
|
switch (callback.type) {
|
2021-10-31 03:23:10 +00:00
|
|
|
case Common::Input::InputType::Button: {
|
2021-11-02 01:49:14 +00:00
|
|
|
Common::Input::AnalogProperties properties{
|
|
|
|
.deadzone = 0.0f,
|
|
|
|
.range = 1.0f,
|
|
|
|
.offset = 0.0f,
|
|
|
|
};
|
|
|
|
status.delta_timestamp = 5000;
|
|
|
|
status.force_update = true;
|
|
|
|
status.accel.x = {
|
|
|
|
.value = 0.0f,
|
|
|
|
.raw_value = 0.0f,
|
|
|
|
.properties = properties,
|
|
|
|
};
|
|
|
|
status.accel.y = {
|
|
|
|
.value = 0.0f,
|
|
|
|
.raw_value = 0.0f,
|
|
|
|
.properties = properties,
|
|
|
|
};
|
|
|
|
status.accel.z = {
|
|
|
|
.value = 0.0f,
|
|
|
|
.raw_value = -1.0f,
|
|
|
|
.properties = properties,
|
|
|
|
};
|
|
|
|
status.gyro.x = {
|
|
|
|
.value = 0.0f,
|
|
|
|
.raw_value = 0.0f,
|
|
|
|
.properties = properties,
|
|
|
|
};
|
|
|
|
status.gyro.y = {
|
|
|
|
.value = 0.0f,
|
|
|
|
.raw_value = 0.0f,
|
|
|
|
.properties = properties,
|
|
|
|
};
|
|
|
|
status.gyro.z = {
|
|
|
|
.value = 0.0f,
|
|
|
|
.raw_value = 0.0f,
|
|
|
|
.properties = properties,
|
|
|
|
};
|
2021-09-20 22:29:43 +01:00
|
|
|
if (TransformToButton(callback).value) {
|
|
|
|
std::random_device device;
|
|
|
|
std::mt19937 gen(device());
|
|
|
|
std::uniform_int_distribution<s16> distribution(-1000, 1000);
|
2021-11-02 01:49:14 +00:00
|
|
|
status.accel.x.raw_value = static_cast<f32>(distribution(gen)) * 0.001f;
|
|
|
|
status.accel.y.raw_value = static_cast<f32>(distribution(gen)) * 0.001f;
|
|
|
|
status.accel.z.raw_value = static_cast<f32>(distribution(gen)) * 0.001f;
|
|
|
|
status.gyro.x.raw_value = static_cast<f32>(distribution(gen)) * 0.001f;
|
|
|
|
status.gyro.y.raw_value = static_cast<f32>(distribution(gen)) * 0.001f;
|
|
|
|
status.gyro.z.raw_value = static_cast<f32>(distribution(gen)) * 0.001f;
|
2021-09-20 22:29:43 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2021-10-31 03:23:10 +00:00
|
|
|
case Common::Input::InputType::Motion:
|
2021-09-20 22:29:43 +01:00
|
|
|
status = callback.motion_status;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
LOG_ERROR(Input, "Conversion from type {} to motion not implemented", callback.type);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
SanitizeAnalog(status.accel.x, false);
|
|
|
|
SanitizeAnalog(status.accel.y, false);
|
|
|
|
SanitizeAnalog(status.accel.z, false);
|
|
|
|
SanitizeAnalog(status.gyro.x, false);
|
|
|
|
SanitizeAnalog(status.gyro.y, false);
|
|
|
|
SanitizeAnalog(status.gyro.z, false);
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2021-10-31 03:23:10 +00:00
|
|
|
Common::Input::StickStatus TransformToStick(const Common::Input::CallbackStatus& callback) {
|
|
|
|
Common::Input::StickStatus status{};
|
2021-09-20 22:29:43 +01:00
|
|
|
|
|
|
|
switch (callback.type) {
|
2021-10-31 03:23:10 +00:00
|
|
|
case Common::Input::InputType::Stick:
|
2021-09-20 22:29:43 +01:00
|
|
|
status = callback.stick_status;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
LOG_ERROR(Input, "Conversion from type {} to stick not implemented", callback.type);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
SanitizeStick(status.x, status.y, true);
|
2021-10-23 05:04:06 +01:00
|
|
|
const auto& properties_x = status.x.properties;
|
|
|
|
const auto& properties_y = status.y.properties;
|
2021-09-20 22:29:43 +01:00
|
|
|
const float x = status.x.value;
|
|
|
|
const float y = status.y.value;
|
|
|
|
|
|
|
|
// Set directional buttons
|
|
|
|
status.right = x > properties_x.threshold;
|
|
|
|
status.left = x < -properties_x.threshold;
|
|
|
|
status.up = y > properties_y.threshold;
|
|
|
|
status.down = y < -properties_y.threshold;
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2021-10-31 03:23:10 +00:00
|
|
|
Common::Input::TouchStatus TransformToTouch(const Common::Input::CallbackStatus& callback) {
|
|
|
|
Common::Input::TouchStatus status{};
|
2021-09-20 22:29:43 +01:00
|
|
|
|
|
|
|
switch (callback.type) {
|
2021-10-31 03:23:10 +00:00
|
|
|
case Common::Input::InputType::Touch:
|
2021-09-20 22:29:43 +01:00
|
|
|
status = callback.touch_status;
|
|
|
|
break;
|
2021-11-15 03:28:38 +00:00
|
|
|
case Common::Input::InputType::Stick:
|
|
|
|
status.x = callback.stick_status.x;
|
|
|
|
status.y = callback.stick_status.y;
|
|
|
|
break;
|
2021-09-20 22:29:43 +01:00
|
|
|
default:
|
|
|
|
LOG_ERROR(Input, "Conversion from type {} to touch not implemented", callback.type);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
SanitizeAnalog(status.x, true);
|
|
|
|
SanitizeAnalog(status.y, true);
|
|
|
|
float& x = status.x.value;
|
|
|
|
float& y = status.y.value;
|
|
|
|
|
|
|
|
// Adjust if value is inverted
|
|
|
|
x = status.x.properties.inverted ? 1.0f + x : x;
|
|
|
|
y = status.y.properties.inverted ? 1.0f + y : y;
|
|
|
|
|
|
|
|
// clamp value
|
|
|
|
x = std::clamp(x, 0.0f, 1.0f);
|
|
|
|
y = std::clamp(y, 0.0f, 1.0f);
|
|
|
|
|
|
|
|
if (status.pressed.inverted) {
|
|
|
|
status.pressed.value = !status.pressed.value;
|
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2021-10-31 03:23:10 +00:00
|
|
|
Common::Input::TriggerStatus TransformToTrigger(const Common::Input::CallbackStatus& callback) {
|
|
|
|
Common::Input::TriggerStatus status{};
|
2021-09-20 22:29:43 +01:00
|
|
|
float& raw_value = status.analog.raw_value;
|
|
|
|
bool calculate_button_value = true;
|
|
|
|
|
|
|
|
switch (callback.type) {
|
2021-10-31 03:23:10 +00:00
|
|
|
case Common::Input::InputType::Analog:
|
2021-09-20 22:29:43 +01:00
|
|
|
status.analog.properties = callback.analog_status.properties;
|
|
|
|
raw_value = callback.analog_status.raw_value;
|
|
|
|
break;
|
2021-10-31 03:23:10 +00:00
|
|
|
case Common::Input::InputType::Button:
|
2021-09-20 22:29:43 +01:00
|
|
|
status.analog.properties.range = 1.0f;
|
|
|
|
status.analog.properties.inverted = callback.button_status.inverted;
|
|
|
|
raw_value = callback.button_status.value ? 1.0f : 0.0f;
|
|
|
|
break;
|
2021-10-31 03:23:10 +00:00
|
|
|
case Common::Input::InputType::Trigger:
|
2021-09-20 22:29:43 +01:00
|
|
|
status = callback.trigger_status;
|
|
|
|
calculate_button_value = false;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
LOG_ERROR(Input, "Conversion from type {} to trigger not implemented", callback.type);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
SanitizeAnalog(status.analog, true);
|
2021-10-23 05:04:06 +01:00
|
|
|
const auto& properties = status.analog.properties;
|
2021-09-20 22:29:43 +01:00
|
|
|
float& value = status.analog.value;
|
|
|
|
|
|
|
|
// Set button status
|
|
|
|
if (calculate_button_value) {
|
2021-10-31 15:41:44 +00:00
|
|
|
status.pressed.value = value > properties.threshold;
|
2021-09-20 22:29:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Adjust if value is inverted
|
|
|
|
value = properties.inverted ? 1.0f + value : value;
|
|
|
|
|
|
|
|
// clamp value
|
|
|
|
value = std::clamp(value, 0.0f, 1.0f);
|
|
|
|
|
2021-11-14 20:09:29 +00:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
Common::Input::AnalogStatus TransformToAnalog(const Common::Input::CallbackStatus& callback) {
|
|
|
|
Common::Input::AnalogStatus status{};
|
|
|
|
|
|
|
|
switch (callback.type) {
|
|
|
|
case Common::Input::InputType::Analog:
|
|
|
|
status.properties = callback.analog_status.properties;
|
|
|
|
status.raw_value = callback.analog_status.raw_value;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
LOG_ERROR(Input, "Conversion from type {} to analog not implemented", callback.type);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
SanitizeAnalog(status, false);
|
|
|
|
|
|
|
|
// Adjust if value is inverted
|
|
|
|
status.value = status.properties.inverted ? -status.value : status.value;
|
|
|
|
|
2021-09-20 22:29:43 +01:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2021-10-31 03:23:10 +00:00
|
|
|
void SanitizeAnalog(Common::Input::AnalogStatus& analog, bool clamp_value) {
|
2021-10-23 05:04:06 +01:00
|
|
|
const auto& properties = analog.properties;
|
2021-09-20 22:29:43 +01:00
|
|
|
float& raw_value = analog.raw_value;
|
|
|
|
float& value = analog.value;
|
|
|
|
|
|
|
|
if (!std::isnormal(raw_value)) {
|
|
|
|
raw_value = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Apply center offset
|
|
|
|
raw_value -= properties.offset;
|
|
|
|
|
|
|
|
// Set initial values to be formated
|
|
|
|
value = raw_value;
|
|
|
|
|
|
|
|
// Calculate vector size
|
|
|
|
const float r = std::abs(value);
|
|
|
|
|
|
|
|
// Return zero if value is smaller than the deadzone
|
|
|
|
if (r <= properties.deadzone || properties.deadzone == 1.0f) {
|
|
|
|
analog.value = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Adjust range of value
|
|
|
|
const float deadzone_factor =
|
|
|
|
1.0f / r * (r - properties.deadzone) / (1.0f - properties.deadzone);
|
|
|
|
value = value * deadzone_factor / properties.range;
|
|
|
|
|
|
|
|
// Invert direction if needed
|
|
|
|
if (properties.inverted) {
|
|
|
|
value = -value;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clamp value
|
|
|
|
if (clamp_value) {
|
|
|
|
value = std::clamp(value, -1.0f, 1.0f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-31 03:23:10 +00:00
|
|
|
void SanitizeStick(Common::Input::AnalogStatus& analog_x, Common::Input::AnalogStatus& analog_y,
|
|
|
|
bool clamp_value) {
|
2021-10-23 05:04:06 +01:00
|
|
|
const auto& properties_x = analog_x.properties;
|
|
|
|
const auto& properties_y = analog_y.properties;
|
2021-09-20 22:29:43 +01:00
|
|
|
float& raw_x = analog_x.raw_value;
|
|
|
|
float& raw_y = analog_y.raw_value;
|
|
|
|
float& x = analog_x.value;
|
|
|
|
float& y = analog_y.value;
|
|
|
|
|
|
|
|
if (!std::isnormal(raw_x)) {
|
|
|
|
raw_x = 0;
|
|
|
|
}
|
|
|
|
if (!std::isnormal(raw_y)) {
|
|
|
|
raw_y = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Apply center offset
|
|
|
|
raw_x += properties_x.offset;
|
|
|
|
raw_y += properties_y.offset;
|
|
|
|
|
|
|
|
// Apply X scale correction from offset
|
|
|
|
if (std::abs(properties_x.offset) < 0.5f) {
|
|
|
|
if (raw_x > 0) {
|
|
|
|
raw_x /= 1 + properties_x.offset;
|
|
|
|
} else {
|
|
|
|
raw_x /= 1 - properties_x.offset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Apply Y scale correction from offset
|
|
|
|
if (std::abs(properties_y.offset) < 0.5f) {
|
|
|
|
if (raw_y > 0) {
|
|
|
|
raw_y /= 1 + properties_y.offset;
|
|
|
|
} else {
|
|
|
|
raw_y /= 1 - properties_y.offset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Invert direction if needed
|
|
|
|
raw_x = properties_x.inverted ? -raw_x : raw_x;
|
|
|
|
raw_y = properties_y.inverted ? -raw_y : raw_y;
|
|
|
|
|
|
|
|
// Set initial values to be formated
|
|
|
|
x = raw_x;
|
|
|
|
y = raw_y;
|
|
|
|
|
|
|
|
// Calculate vector size
|
|
|
|
float r = x * x + y * y;
|
|
|
|
r = std::sqrt(r);
|
|
|
|
|
|
|
|
// TODO(German77): Use deadzone and range of both axis
|
|
|
|
|
|
|
|
// Return zero if values are smaller than the deadzone
|
|
|
|
if (r <= properties_x.deadzone || properties_x.deadzone >= 1.0f) {
|
|
|
|
x = 0;
|
|
|
|
y = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Adjust range of joystick
|
|
|
|
const float deadzone_factor =
|
|
|
|
1.0f / r * (r - properties_x.deadzone) / (1.0f - properties_x.deadzone);
|
|
|
|
x = x * deadzone_factor / properties_x.range;
|
|
|
|
y = y * deadzone_factor / properties_x.range;
|
|
|
|
r = r * deadzone_factor / properties_x.range;
|
|
|
|
|
|
|
|
// Normalize joystick
|
|
|
|
if (clamp_value && r > 1.0f) {
|
|
|
|
x /= r;
|
|
|
|
y /= r;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Core::HID
|