2020-06-21 21:31:57 +02:00
|
|
|
// Copyright 2020 yuzu Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2020-06-21 18:36:28 +02:00
|
|
|
#include <atomic>
|
|
|
|
#include <list>
|
|
|
|
#include <mutex>
|
|
|
|
#include <utility>
|
2020-07-12 21:41:32 +02:00
|
|
|
#include "common/assert.h"
|
2020-06-21 18:36:28 +02:00
|
|
|
#include "common/threadsafe_queue.h"
|
2020-06-21 21:31:57 +02:00
|
|
|
#include "input_common/gcadapter/gc_adapter.h"
|
|
|
|
#include "input_common/gcadapter/gc_poller.h"
|
2020-06-21 18:36:28 +02:00
|
|
|
|
|
|
|
namespace InputCommon {
|
|
|
|
|
|
|
|
class GCButton final : public Input::ButtonDevice {
|
|
|
|
public:
|
2020-10-13 01:11:22 +02:00
|
|
|
explicit GCButton(u32 port_, s32 button_, GCAdapter::Adapter* adapter)
|
2020-06-22 00:43:01 +02:00
|
|
|
: port(port_), button(button_), gcadapter(adapter) {}
|
2020-06-21 18:36:28 +02:00
|
|
|
|
|
|
|
~GCButton() override;
|
|
|
|
|
|
|
|
bool GetStatus() const override {
|
2020-07-14 17:23:10 +02:00
|
|
|
if (gcadapter->DeviceConnected(port)) {
|
2020-10-13 01:11:22 +02:00
|
|
|
return (gcadapter->GetPadState(port).buttons & button) != 0;
|
2020-07-14 17:23:10 +02:00
|
|
|
}
|
|
|
|
return false;
|
2020-06-21 18:36:28 +02:00
|
|
|
}
|
|
|
|
|
2020-10-13 01:11:22 +02:00
|
|
|
bool SetRumblePlay(f32 amp_high, f32 amp_low, f32 freq_high, f32 freq_low) const override {
|
|
|
|
const float amplitude = amp_high + amp_low > 2.0f ? 1.0f : (amp_high + amp_low) * 0.5f;
|
|
|
|
const auto new_amp =
|
|
|
|
static_cast<f32>(pow(amplitude, 0.5f) * (3.0f - 2.0f * pow(amplitude, 0.15f)));
|
|
|
|
|
|
|
|
return gcadapter->RumblePlay(port, new_amp);
|
|
|
|
}
|
|
|
|
|
2020-06-21 18:36:28 +02:00
|
|
|
private:
|
2020-10-14 08:51:14 +02:00
|
|
|
const u32 port;
|
2020-10-13 01:11:22 +02:00
|
|
|
const s32 button;
|
|
|
|
GCAdapter::Adapter* gcadapter;
|
2020-06-21 18:36:28 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
class GCAxisButton final : public Input::ButtonDevice {
|
|
|
|
public:
|
2020-10-14 08:51:14 +02:00
|
|
|
explicit GCAxisButton(u32 port_, u32 axis_, float threshold_, bool trigger_if_greater_,
|
2020-09-07 08:45:04 +02:00
|
|
|
const GCAdapter::Adapter* adapter)
|
2020-06-22 00:43:01 +02:00
|
|
|
: port(port_), axis(axis_), threshold(threshold_), trigger_if_greater(trigger_if_greater_),
|
2020-10-13 01:11:22 +02:00
|
|
|
gcadapter(adapter) {}
|
2020-06-21 18:36:28 +02:00
|
|
|
|
|
|
|
bool GetStatus() const override {
|
2020-07-14 17:23:10 +02:00
|
|
|
if (gcadapter->DeviceConnected(port)) {
|
2020-10-13 01:11:22 +02:00
|
|
|
const float current_axis_value = gcadapter->GetPadState(port).axis_values.at(axis);
|
|
|
|
const float axis_value = current_axis_value / 128.0f;
|
2020-07-14 17:23:10 +02:00
|
|
|
if (trigger_if_greater) {
|
|
|
|
// TODO: Might be worthwile to set a slider for the trigger threshold. It is
|
|
|
|
// currently always set to 0.5 in configure_input_player.cpp ZL/ZR HandleClick
|
|
|
|
return axis_value > threshold;
|
|
|
|
}
|
|
|
|
return axis_value < -threshold;
|
2020-06-21 18:36:28 +02:00
|
|
|
}
|
2020-07-14 17:23:10 +02:00
|
|
|
return false;
|
2020-06-21 18:36:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2020-10-14 08:51:14 +02:00
|
|
|
const u32 port;
|
|
|
|
const u32 axis;
|
2020-06-21 18:36:28 +02:00
|
|
|
float threshold;
|
|
|
|
bool trigger_if_greater;
|
2020-09-07 08:45:04 +02:00
|
|
|
const GCAdapter::Adapter* gcadapter;
|
2020-06-21 18:36:28 +02:00
|
|
|
};
|
|
|
|
|
2020-06-22 03:50:58 +02:00
|
|
|
GCButtonFactory::GCButtonFactory(std::shared_ptr<GCAdapter::Adapter> adapter_)
|
2020-06-22 05:56:56 +02:00
|
|
|
: adapter(std::move(adapter_)) {}
|
2020-06-21 18:36:28 +02:00
|
|
|
|
2020-06-22 03:15:58 +02:00
|
|
|
GCButton::~GCButton() = default;
|
2020-06-21 18:36:28 +02:00
|
|
|
|
|
|
|
std::unique_ptr<Input::ButtonDevice> GCButtonFactory::Create(const Common::ParamPackage& params) {
|
2020-10-14 08:51:14 +02:00
|
|
|
const auto button_id = params.Get("button", 0);
|
|
|
|
const auto port = static_cast<u32>(params.Get("port", 0));
|
2020-07-02 21:54:44 +02:00
|
|
|
|
2020-10-13 01:11:22 +02:00
|
|
|
constexpr s32 PAD_STICK_ID = static_cast<s32>(GCAdapter::PadButton::Stick);
|
2020-07-02 21:54:44 +02:00
|
|
|
|
|
|
|
// button is not an axis/stick button
|
|
|
|
if (button_id != PAD_STICK_ID) {
|
2020-07-25 09:35:40 +02:00
|
|
|
return std::make_unique<GCButton>(port, button_id, adapter.get());
|
2020-07-02 21:54:44 +02:00
|
|
|
}
|
|
|
|
|
2020-06-21 18:36:28 +02:00
|
|
|
// For Axis buttons, used by the binary sticks.
|
2020-07-02 21:54:44 +02:00
|
|
|
if (button_id == PAD_STICK_ID) {
|
2020-06-21 18:36:28 +02:00
|
|
|
const int axis = params.Get("axis", 0);
|
2020-07-02 21:54:44 +02:00
|
|
|
const float threshold = params.Get("threshold", 0.25f);
|
2020-06-21 18:36:28 +02:00
|
|
|
const std::string direction_name = params.Get("direction", "");
|
|
|
|
bool trigger_if_greater;
|
|
|
|
if (direction_name == "+") {
|
|
|
|
trigger_if_greater = true;
|
|
|
|
} else if (direction_name == "-") {
|
|
|
|
trigger_if_greater = false;
|
|
|
|
} else {
|
|
|
|
trigger_if_greater = true;
|
|
|
|
LOG_ERROR(Input, "Unknown direction {}", direction_name);
|
|
|
|
}
|
2020-06-22 05:56:56 +02:00
|
|
|
return std::make_unique<GCAxisButton>(port, axis, threshold, trigger_if_greater,
|
|
|
|
adapter.get());
|
2020-06-21 18:36:28 +02:00
|
|
|
}
|
2020-07-12 21:41:32 +02:00
|
|
|
|
|
|
|
UNREACHABLE();
|
|
|
|
return nullptr;
|
2020-06-21 18:36:28 +02:00
|
|
|
}
|
|
|
|
|
2020-07-12 21:43:05 +02:00
|
|
|
Common::ParamPackage GCButtonFactory::GetNextInput() const {
|
2020-06-21 18:36:28 +02:00
|
|
|
Common::ParamPackage params;
|
2020-06-22 00:43:01 +02:00
|
|
|
GCAdapter::GCPadStatus pad;
|
2020-06-23 23:37:15 +02:00
|
|
|
auto& queue = adapter->GetPadQueue();
|
2020-10-13 01:11:22 +02:00
|
|
|
while (queue.Pop(pad)) {
|
|
|
|
// This while loop will break on the earliest detected button
|
|
|
|
params.Set("engine", "gcpad");
|
|
|
|
params.Set("port", static_cast<s32>(pad.port));
|
|
|
|
if (pad.button != GCAdapter::PadButton::Undefined) {
|
|
|
|
params.Set("button", static_cast<u16>(pad.button));
|
|
|
|
}
|
2020-06-23 23:37:15 +02:00
|
|
|
|
2020-10-13 01:11:22 +02:00
|
|
|
// For Axis button implementation
|
|
|
|
if (pad.axis != GCAdapter::PadAxes::Undefined) {
|
|
|
|
params.Set("axis", static_cast<u8>(pad.axis));
|
|
|
|
params.Set("button", static_cast<u16>(GCAdapter::PadButton::Stick));
|
|
|
|
params.Set("threshold", "0.25");
|
|
|
|
if (pad.axis_value > 0) {
|
|
|
|
params.Set("direction", "+");
|
|
|
|
} else {
|
|
|
|
params.Set("direction", "-");
|
2020-06-21 18:36:28 +02:00
|
|
|
}
|
2020-10-13 01:11:22 +02:00
|
|
|
break;
|
2020-06-21 18:36:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return params;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GCButtonFactory::BeginConfiguration() {
|
|
|
|
polling = true;
|
2020-06-22 00:43:01 +02:00
|
|
|
adapter->BeginConfiguration();
|
2020-06-21 18:36:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void GCButtonFactory::EndConfiguration() {
|
|
|
|
polling = false;
|
2020-06-22 00:43:01 +02:00
|
|
|
adapter->EndConfiguration();
|
2020-06-21 18:36:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class GCAnalog final : public Input::AnalogDevice {
|
|
|
|
public:
|
2020-10-14 08:51:14 +02:00
|
|
|
explicit GCAnalog(u32 port_, u32 axis_x_, u32 axis_y_, float deadzone_,
|
|
|
|
const GCAdapter::Adapter* adapter, float range_)
|
2020-07-07 03:58:31 +02:00
|
|
|
: port(port_), axis_x(axis_x_), axis_y(axis_y_), deadzone(deadzone_), gcadapter(adapter),
|
2020-08-26 05:03:10 +02:00
|
|
|
range(range_) {}
|
2020-06-21 18:36:28 +02:00
|
|
|
|
2020-10-14 08:51:14 +02:00
|
|
|
float GetAxis(u32 axis) const {
|
2020-07-14 17:23:10 +02:00
|
|
|
if (gcadapter->DeviceConnected(port)) {
|
|
|
|
std::lock_guard lock{mutex};
|
2020-10-14 08:51:14 +02:00
|
|
|
const auto axis_value =
|
2020-10-13 01:11:22 +02:00
|
|
|
static_cast<float>(gcadapter->GetPadState(port).axis_values.at(axis));
|
|
|
|
return (axis_value) / (100.0f * range);
|
2020-07-14 17:23:10 +02:00
|
|
|
}
|
|
|
|
return 0.0f;
|
2020-06-21 18:36:28 +02:00
|
|
|
}
|
|
|
|
|
2020-10-14 08:51:14 +02:00
|
|
|
std::pair<float, float> GetAnalog(u32 analog_axis_x, u32 analog_axis_y) const {
|
|
|
|
float x = GetAxis(analog_axis_x);
|
|
|
|
float y = GetAxis(analog_axis_y);
|
2020-06-21 18:36:28 +02:00
|
|
|
|
|
|
|
// Make sure the coordinates are in the unit circle,
|
|
|
|
// otherwise normalize it.
|
|
|
|
float r = x * x + y * y;
|
|
|
|
if (r > 1.0f) {
|
|
|
|
r = std::sqrt(r);
|
|
|
|
x /= r;
|
|
|
|
y /= r;
|
|
|
|
}
|
|
|
|
|
2020-06-22 05:56:56 +02:00
|
|
|
return {x, y};
|
2020-06-21 18:36:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::tuple<float, float> GetStatus() const override {
|
|
|
|
const auto [x, y] = GetAnalog(axis_x, axis_y);
|
|
|
|
const float r = std::sqrt((x * x) + (y * y));
|
|
|
|
if (r > deadzone) {
|
2020-06-22 05:56:56 +02:00
|
|
|
return {x / r * (r - deadzone) / (1 - deadzone),
|
|
|
|
y / r * (r - deadzone) / (1 - deadzone)};
|
2020-06-21 18:36:28 +02:00
|
|
|
}
|
2020-06-22 05:56:56 +02:00
|
|
|
return {0.0f, 0.0f};
|
2020-06-21 18:36:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool GetAnalogDirectionStatus(Input::AnalogDirection direction) const override {
|
|
|
|
const auto [x, y] = GetStatus();
|
2020-08-19 10:17:38 +02:00
|
|
|
const float directional_deadzone = 0.5f;
|
2020-06-21 18:36:28 +02:00
|
|
|
switch (direction) {
|
|
|
|
case Input::AnalogDirection::RIGHT:
|
|
|
|
return x > directional_deadzone;
|
|
|
|
case Input::AnalogDirection::LEFT:
|
|
|
|
return x < -directional_deadzone;
|
|
|
|
case Input::AnalogDirection::UP:
|
|
|
|
return y > directional_deadzone;
|
|
|
|
case Input::AnalogDirection::DOWN:
|
|
|
|
return y < -directional_deadzone;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2020-10-14 08:51:14 +02:00
|
|
|
const u32 port;
|
|
|
|
const u32 axis_x;
|
|
|
|
const u32 axis_y;
|
2020-06-21 18:36:28 +02:00
|
|
|
const float deadzone;
|
2020-09-07 08:45:04 +02:00
|
|
|
const GCAdapter::Adapter* gcadapter;
|
2020-07-17 17:56:15 +02:00
|
|
|
const float range;
|
2020-06-21 18:36:28 +02:00
|
|
|
mutable std::mutex mutex;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// An analog device factory that creates analog devices from GC Adapter
|
2020-06-22 03:50:58 +02:00
|
|
|
GCAnalogFactory::GCAnalogFactory(std::shared_ptr<GCAdapter::Adapter> adapter_)
|
2020-06-22 05:56:56 +02:00
|
|
|
: adapter(std::move(adapter_)) {}
|
2020-06-21 18:36:28 +02:00
|
|
|
|
|
|
|
/**
|
2020-06-21 21:31:57 +02:00
|
|
|
* Creates analog device from joystick axes
|
|
|
|
* @param params contains parameters for creating the device:
|
|
|
|
* - "port": the nth gcpad on the adapter
|
|
|
|
* - "axis_x": the index of the axis to be bind as x-axis
|
|
|
|
* - "axis_y": the index of the axis to be bind as y-axis
|
|
|
|
*/
|
2020-06-21 18:36:28 +02:00
|
|
|
std::unique_ptr<Input::AnalogDevice> GCAnalogFactory::Create(const Common::ParamPackage& params) {
|
2020-10-14 08:51:14 +02:00
|
|
|
const auto port = static_cast<u32>(params.Get("port", 0));
|
|
|
|
const auto axis_x = static_cast<u32>(params.Get("axis_x", 0));
|
|
|
|
const auto axis_y = static_cast<u32>(params.Get("axis_y", 1));
|
|
|
|
const auto deadzone = std::clamp(params.Get("deadzone", 0.0f), 0.0f, 1.0f);
|
|
|
|
const auto range = std::clamp(params.Get("range", 1.0f), 0.50f, 1.50f);
|
2020-06-21 18:36:28 +02:00
|
|
|
|
2020-07-17 17:56:15 +02:00
|
|
|
return std::make_unique<GCAnalog>(port, axis_x, axis_y, deadzone, adapter.get(), range);
|
2020-06-21 18:36:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void GCAnalogFactory::BeginConfiguration() {
|
|
|
|
polling = true;
|
2020-06-22 00:43:01 +02:00
|
|
|
adapter->BeginConfiguration();
|
2020-06-21 18:36:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void GCAnalogFactory::EndConfiguration() {
|
|
|
|
polling = false;
|
2020-06-22 00:43:01 +02:00
|
|
|
adapter->EndConfiguration();
|
2020-06-21 18:36:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Common::ParamPackage GCAnalogFactory::GetNextInput() {
|
2020-06-22 00:43:01 +02:00
|
|
|
GCAdapter::GCPadStatus pad;
|
2020-10-13 01:11:22 +02:00
|
|
|
Common::ParamPackage params;
|
2020-06-23 23:37:15 +02:00
|
|
|
auto& queue = adapter->GetPadQueue();
|
2020-10-13 01:11:22 +02:00
|
|
|
while (queue.Pop(pad)) {
|
|
|
|
if (pad.button != GCAdapter::PadButton::Undefined) {
|
|
|
|
params.Set("engine", "gcpad");
|
|
|
|
params.Set("port", static_cast<s32>(pad.port));
|
|
|
|
params.Set("button", static_cast<u16>(pad.button));
|
|
|
|
return params;
|
|
|
|
}
|
|
|
|
if (pad.axis == GCAdapter::PadAxes::Undefined ||
|
|
|
|
std::abs(static_cast<float>(pad.axis_value) / 128.0f) < 0.1f) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// An analog device needs two axes, so we need to store the axis for later and wait for
|
|
|
|
// a second input event. The axes also must be from the same joystick.
|
|
|
|
const u8 axis = static_cast<u8>(pad.axis);
|
|
|
|
if (axis == 0 || axis == 1) {
|
|
|
|
analog_x_axis = 0;
|
|
|
|
analog_y_axis = 1;
|
|
|
|
controller_number = static_cast<s32>(pad.port);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (axis == 2 || axis == 3) {
|
|
|
|
analog_x_axis = 2;
|
|
|
|
analog_y_axis = 3;
|
|
|
|
controller_number = static_cast<s32>(pad.port);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (analog_x_axis == -1) {
|
|
|
|
analog_x_axis = axis;
|
|
|
|
controller_number = static_cast<s32>(pad.port);
|
|
|
|
} else if (analog_y_axis == -1 && analog_x_axis != axis &&
|
|
|
|
controller_number == static_cast<s32>(pad.port)) {
|
|
|
|
analog_y_axis = axis;
|
|
|
|
break;
|
2020-06-21 18:36:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (analog_x_axis != -1 && analog_y_axis != -1) {
|
|
|
|
params.Set("engine", "gcpad");
|
|
|
|
params.Set("port", controller_number);
|
|
|
|
params.Set("axis_x", analog_x_axis);
|
|
|
|
params.Set("axis_y", analog_y_axis);
|
|
|
|
analog_x_axis = -1;
|
|
|
|
analog_y_axis = -1;
|
|
|
|
controller_number = -1;
|
|
|
|
return params;
|
|
|
|
}
|
|
|
|
return params;
|
|
|
|
}
|
2020-06-22 00:43:01 +02:00
|
|
|
|
2020-06-21 18:36:28 +02:00
|
|
|
} // namespace InputCommon
|