summaryrefslogtreecommitdiffstats
path: root/src/input_common/helpers
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/input_common/helpers/stick_from_buttons.cpp317
-rw-r--r--[-rwxr-xr-x]src/input_common/helpers/stick_from_buttons.h (renamed from src/input_common/analog_from_button.h)7
-rw-r--r--src/input_common/helpers/touch_from_buttons.cpp84
-rw-r--r--src/input_common/helpers/touch_from_buttons.h (renamed from src/input_common/touch_from_button.h)7
-rw-r--r--src/input_common/helpers/udp_protocol.cpp (renamed from src/input_common/udp/protocol.cpp)2
-rw-r--r--src/input_common/helpers/udp_protocol.h (renamed from src/input_common/udp/protocol.h)75
6 files changed, 461 insertions, 31 deletions
diff --git a/src/input_common/helpers/stick_from_buttons.cpp b/src/input_common/helpers/stick_from_buttons.cpp
new file mode 100644
index 000000000..e23394f5f
--- /dev/null
+++ b/src/input_common/helpers/stick_from_buttons.cpp
@@ -0,0 +1,317 @@
+// Copyright 2017 Citra Emulator Project
+// Licensed under GPLv2 or any later version
+// Refer to the license.txt file included.
+
+#include <chrono>
+#include <cmath>
+#include "common/math_util.h"
+#include "common/settings.h"
+#include "input_common/helpers/stick_from_buttons.h"
+
+namespace InputCommon {
+
+class Stick final : public Common::Input::InputDevice {
+public:
+ using Button = std::unique_ptr<Common::Input::InputDevice>;
+
+ Stick(Button up_, Button down_, Button left_, Button right_, Button modifier_,
+ float modifier_scale_, float modifier_angle_)
+ : up(std::move(up_)), down(std::move(down_)), left(std::move(left_)),
+ right(std::move(right_)), modifier(std::move(modifier_)), modifier_scale(modifier_scale_),
+ modifier_angle(modifier_angle_) {
+ up->SetCallback({
+ .on_change =
+ [this](const Common::Input::CallbackStatus& callback_) {
+ UpdateUpButtonStatus(callback_);
+ },
+ });
+ down->SetCallback({
+ .on_change =
+ [this](const Common::Input::CallbackStatus& callback_) {
+ UpdateDownButtonStatus(callback_);
+ },
+ });
+ left->SetCallback({
+ .on_change =
+ [this](const Common::Input::CallbackStatus& callback_) {
+ UpdateLeftButtonStatus(callback_);
+ },
+ });
+ right->SetCallback({
+ .on_change =
+ [this](const Common::Input::CallbackStatus& callback_) {
+ UpdateRightButtonStatus(callback_);
+ },
+ });
+ modifier->SetCallback({
+ .on_change =
+ [this](const Common::Input::CallbackStatus& callback_) {
+ UpdateModButtonStatus(callback_);
+ },
+ });
+ last_x_axis_value = 0.0f;
+ last_y_axis_value = 0.0f;
+ }
+
+ bool IsAngleGreater(float old_angle, float new_angle) const {
+ constexpr float TAU = Common::PI * 2.0f;
+ // Use wider angle to ease the transition.
+ constexpr float aperture = TAU * 0.15f;
+ const float top_limit = new_angle + aperture;
+ return (old_angle > new_angle && old_angle <= top_limit) ||
+ (old_angle + TAU > new_angle && old_angle + TAU <= top_limit);
+ }
+
+ bool IsAngleSmaller(float old_angle, float new_angle) const {
+ constexpr float TAU = Common::PI * 2.0f;
+ // Use wider angle to ease the transition.
+ constexpr float aperture = TAU * 0.15f;
+ const float bottom_limit = new_angle - aperture;
+ return (old_angle >= bottom_limit && old_angle < new_angle) ||
+ (old_angle - TAU >= bottom_limit && old_angle - TAU < new_angle);
+ }
+
+ float GetAngle(std::chrono::time_point<std::chrono::steady_clock> now) const {
+ constexpr float TAU = Common::PI * 2.0f;
+ float new_angle = angle;
+
+ auto time_difference = static_cast<float>(
+ std::chrono::duration_cast<std::chrono::microseconds>(now - last_update).count());
+ time_difference /= 1000.0f * 1000.0f;
+ if (time_difference > 0.5f) {
+ time_difference = 0.5f;
+ }
+
+ if (IsAngleGreater(new_angle, goal_angle)) {
+ new_angle -= modifier_angle * time_difference;
+ if (new_angle < 0) {
+ new_angle += TAU;
+ }
+ if (!IsAngleGreater(new_angle, goal_angle)) {
+ return goal_angle;
+ }
+ } else if (IsAngleSmaller(new_angle, goal_angle)) {
+ new_angle += modifier_angle * time_difference;
+ if (new_angle >= TAU) {
+ new_angle -= TAU;
+ }
+ if (!IsAngleSmaller(new_angle, goal_angle)) {
+ return goal_angle;
+ }
+ } else {
+ return goal_angle;
+ }
+ return new_angle;
+ }
+
+ void SetGoalAngle(bool r, bool l, bool u, bool d) {
+ // Move to the right
+ if (r && !u && !d) {
+ goal_angle = 0.0f;
+ }
+
+ // Move to the upper right
+ if (r && u && !d) {
+ goal_angle = Common::PI * 0.25f;
+ }
+
+ // Move up
+ if (u && !l && !r) {
+ goal_angle = Common::PI * 0.5f;
+ }
+
+ // Move to the upper left
+ if (l && u && !d) {
+ goal_angle = Common::PI * 0.75f;
+ }
+
+ // Move to the left
+ if (l && !u && !d) {
+ goal_angle = Common::PI;
+ }
+
+ // Move to the bottom left
+ if (l && !u && d) {
+ goal_angle = Common::PI * 1.25f;
+ }
+
+ // Move down
+ if (d && !l && !r) {
+ goal_angle = Common::PI * 1.5f;
+ }
+
+ // Move to the bottom right
+ if (r && !u && d) {
+ goal_angle = Common::PI * 1.75f;
+ }
+ }
+
+ void UpdateUpButtonStatus(const Common::Input::CallbackStatus& button_callback) {
+ up_status = button_callback.button_status.value;
+ UpdateStatus();
+ }
+
+ void UpdateDownButtonStatus(const Common::Input::CallbackStatus& button_callback) {
+ down_status = button_callback.button_status.value;
+ UpdateStatus();
+ }
+
+ void UpdateLeftButtonStatus(const Common::Input::CallbackStatus& button_callback) {
+ left_status = button_callback.button_status.value;
+ UpdateStatus();
+ }
+
+ void UpdateRightButtonStatus(const Common::Input::CallbackStatus& button_callback) {
+ right_status = button_callback.button_status.value;
+ UpdateStatus();
+ }
+
+ void UpdateModButtonStatus(const Common::Input::CallbackStatus& button_callback) {
+ modifier_status = button_callback.button_status.value;
+ UpdateStatus();
+ }
+
+ void UpdateStatus() {
+ const float coef = modifier_status ? modifier_scale : 1.0f;
+
+ bool r = right_status;
+ bool l = left_status;
+ bool u = up_status;
+ bool d = down_status;
+
+ // Eliminate contradictory movements
+ if (r && l) {
+ r = false;
+ l = false;
+ }
+ if (u && d) {
+ u = false;
+ d = false;
+ }
+
+ // Move if a key is pressed
+ if (r || l || u || d) {
+ amplitude = coef;
+ } else {
+ amplitude = 0;
+ }
+
+ const auto now = std::chrono::steady_clock::now();
+ const auto time_difference = static_cast<u64>(
+ std::chrono::duration_cast<std::chrono::milliseconds>(now - last_update).count());
+
+ if (time_difference < 10) {
+ // Disable analog mode if inputs are too fast
+ SetGoalAngle(r, l, u, d);
+ angle = goal_angle;
+ } else {
+ angle = GetAngle(now);
+ SetGoalAngle(r, l, u, d);
+ }
+
+ last_update = now;
+ Common::Input::CallbackStatus status{
+ .type = Common::Input::InputType::Stick,
+ .stick_status = GetStatus(),
+ };
+ last_x_axis_value = status.stick_status.x.raw_value;
+ last_y_axis_value = status.stick_status.y.raw_value;
+ TriggerOnChange(status);
+ }
+
+ void ForceUpdate() override {
+ up->ForceUpdate();
+ down->ForceUpdate();
+ left->ForceUpdate();
+ right->ForceUpdate();
+ modifier->ForceUpdate();
+ }
+
+ void SoftUpdate() override {
+ Common::Input::CallbackStatus status{
+ .type = Common::Input::InputType::Stick,
+ .stick_status = GetStatus(),
+ };
+ if (last_x_axis_value == status.stick_status.x.raw_value &&
+ last_y_axis_value == status.stick_status.y.raw_value) {
+ return;
+ }
+ last_x_axis_value = status.stick_status.x.raw_value;
+ last_y_axis_value = status.stick_status.y.raw_value;
+ TriggerOnChange(status);
+ }
+
+ Common::Input::StickStatus GetStatus() const {
+ Common::Input::StickStatus status{};
+ status.x.properties = properties;
+ status.y.properties = properties;
+ if (Settings::values.emulate_analog_keyboard) {
+ const auto now = std::chrono::steady_clock::now();
+ float angle_ = GetAngle(now);
+ status.x.raw_value = std::cos(angle_) * amplitude;
+ status.y.raw_value = std::sin(angle_) * amplitude;
+ return status;
+ }
+ constexpr float SQRT_HALF = 0.707106781f;
+ int x = 0, y = 0;
+ if (right_status) {
+ ++x;
+ }
+ if (left_status) {
+ --x;
+ }
+ if (up_status) {
+ ++y;
+ }
+ if (down_status) {
+ --y;
+ }
+ const float coef = modifier_status ? modifier_scale : 1.0f;
+ status.x.raw_value = static_cast<float>(x) * coef * (y == 0 ? 1.0f : SQRT_HALF);
+ status.y.raw_value = static_cast<float>(y) * coef * (x == 0 ? 1.0f : SQRT_HALF);
+ return status;
+ }
+
+private:
+ Button up;
+ Button down;
+ Button left;
+ Button right;
+ Button modifier;
+ float modifier_scale{};
+ float modifier_angle{};
+ float angle{};
+ float goal_angle{};
+ float amplitude{};
+ bool up_status{};
+ bool down_status{};
+ bool left_status{};
+ bool right_status{};
+ bool modifier_status{};
+ float last_x_axis_value{};
+ float last_y_axis_value{};
+ const Common::Input::AnalogProperties properties{0.0f, 1.0f, 0.5f, 0.0f, false};
+ std::chrono::time_point<std::chrono::steady_clock> last_update;
+};
+
+std::unique_ptr<Common::Input::InputDevice> StickFromButton::Create(
+ const Common::ParamPackage& params) {
+ const std::string null_engine = Common::ParamPackage{{"engine", "null"}}.Serialize();
+ auto up = Common::Input::CreateDeviceFromString<Common::Input::InputDevice>(
+ params.Get("up", null_engine));
+ auto down = Common::Input::CreateDeviceFromString<Common::Input::InputDevice>(
+ params.Get("down", null_engine));
+ auto left = Common::Input::CreateDeviceFromString<Common::Input::InputDevice>(
+ params.Get("left", null_engine));
+ auto right = Common::Input::CreateDeviceFromString<Common::Input::InputDevice>(
+ params.Get("right", null_engine));
+ auto modifier = Common::Input::CreateDeviceFromString<Common::Input::InputDevice>(
+ params.Get("modifier", null_engine));
+ auto modifier_scale = params.Get("modifier_scale", 0.5f);
+ auto modifier_angle = params.Get("modifier_angle", 5.5f);
+ return std::make_unique<Stick>(std::move(up), std::move(down), std::move(left),
+ std::move(right), std::move(modifier), modifier_scale,
+ modifier_angle);
+}
+
+} // namespace InputCommon
diff --git a/src/input_common/analog_from_button.h b/src/input_common/helpers/stick_from_buttons.h
index bbd583dd9..437ace4f7 100755..100644
--- a/src/input_common/analog_from_button.h
+++ b/src/input_common/helpers/stick_from_buttons.h
@@ -4,8 +4,7 @@
#pragma once
-#include <memory>
-#include "core/frontend/input.h"
+#include "common/input.h"
namespace InputCommon {
@@ -13,7 +12,7 @@ namespace InputCommon {
* An analog device factory that takes direction button devices and combines them into a analog
* device.
*/
-class AnalogFromButton final : public Input::Factory<Input::AnalogDevice> {
+class StickFromButton final : public Common::Input::Factory<Common::Input::InputDevice> {
public:
/**
* Creates an analog device from direction button devices
@@ -25,7 +24,7 @@ public:
* - "modifier": a serialized ParamPackage for creating a button device as the modifier
* - "modifier_scale": a float for the multiplier the modifier gives to the position
*/
- std::unique_ptr<Input::AnalogDevice> Create(const Common::ParamPackage& params) override;
+ std::unique_ptr<Common::Input::InputDevice> Create(const Common::ParamPackage& params) override;
};
} // namespace InputCommon
diff --git a/src/input_common/helpers/touch_from_buttons.cpp b/src/input_common/helpers/touch_from_buttons.cpp
new file mode 100644
index 000000000..ece1e3b32
--- /dev/null
+++ b/src/input_common/helpers/touch_from_buttons.cpp
@@ -0,0 +1,84 @@
+// Copyright 2020 Citra Emulator Project
+// Licensed under GPLv2 or any later version
+// Refer to the license.txt file included.
+
+#include <algorithm>
+#include "common/settings.h"
+#include "core/frontend/framebuffer_layout.h"
+#include "input_common/helpers/touch_from_buttons.h"
+
+namespace InputCommon {
+
+class TouchFromButtonDevice final : public Common::Input::InputDevice {
+public:
+ using Button = std::unique_ptr<Common::Input::InputDevice>;
+ TouchFromButtonDevice(Button button_, int touch_id_, float x_, float y_)
+ : button(std::move(button_)), touch_id(touch_id_), x(x_), y(y_) {
+ last_button_value = false;
+ button->SetCallback({
+ .on_change =
+ [this](const Common::Input::CallbackStatus& callback_) {
+ UpdateButtonStatus(callback_);
+ },
+ });
+ button->ForceUpdate();
+ }
+
+ void ForceUpdate() override {
+ button->ForceUpdate();
+ }
+
+ Common::Input::TouchStatus GetStatus(bool pressed) const {
+ const Common::Input::ButtonStatus button_status{
+ .value = pressed,
+ };
+ Common::Input::TouchStatus status{
+ .pressed = button_status,
+ .x = {},
+ .y = {},
+ .id = touch_id,
+ };
+ status.x.properties = properties;
+ status.y.properties = properties;
+
+ if (!pressed) {
+ return status;
+ }
+
+ status.x.raw_value = x;
+ status.y.raw_value = y;
+ return status;
+ }
+
+ void UpdateButtonStatus(const Common::Input::CallbackStatus& button_callback) {
+ const Common::Input::CallbackStatus status{
+ .type = Common::Input::InputType::Touch,
+ .touch_status = GetStatus(button_callback.button_status.value),
+ };
+ if (last_button_value != button_callback.button_status.value) {
+ last_button_value = button_callback.button_status.value;
+ TriggerOnChange(status);
+ }
+ }
+
+private:
+ Button button;
+ bool last_button_value;
+ const int touch_id;
+ const float x;
+ const float y;
+ const Common::Input::AnalogProperties properties{0.0f, 1.0f, 0.5f, 0.0f, false};
+};
+
+std::unique_ptr<Common::Input::InputDevice> TouchFromButton::Create(
+ const Common::ParamPackage& params) {
+ const std::string null_engine = Common::ParamPackage{{"engine", "null"}}.Serialize();
+ auto button = Common::Input::CreateDeviceFromString<Common::Input::InputDevice>(
+ params.Get("button", null_engine));
+ const auto touch_id = params.Get("touch_id", 0);
+ const float x = params.Get("x", 0.0f) / 1280.0f;
+ const float y = params.Get("y", 0.0f) / 720.0f;
+ return std::make_unique<TouchFromButtonDevice>(std::move(button), touch_id, x, y);
+}
+
+} // namespace InputCommon
diff --git a/src/input_common/touch_from_button.h b/src/input_common/helpers/touch_from_buttons.h
index 8b4d1aa96..628f18215 100644
--- a/src/input_common/touch_from_button.h
+++ b/src/input_common/helpers/touch_from_buttons.h
@@ -4,20 +4,19 @@
#pragma once
-#include <memory>
-#include "core/frontend/input.h"
+#include "common/input.h"
namespace InputCommon {
/**
* A touch device factory that takes a list of button devices and combines them into a touch device.
*/
-class TouchFromButtonFactory final : public Input::Factory<Input::TouchDevice> {
+class TouchFromButton final : public Common::Input::Factory<Common::Input::InputDevice> {
public:
/**
* Creates a touch device from a list of button devices
*/
- std::unique_ptr<Input::TouchDevice> Create(const Common::ParamPackage& params) override;
+ std::unique_ptr<Common::Input::InputDevice> Create(const Common::ParamPackage& params) override;
};
} // namespace InputCommon
diff --git a/src/input_common/udp/protocol.cpp b/src/input_common/helpers/udp_protocol.cpp
index 5e50bd612..cdeab7e11 100644
--- a/src/input_common/udp/protocol.cpp
+++ b/src/input_common/helpers/udp_protocol.cpp
@@ -5,7 +5,7 @@
#include <cstddef>
#include <cstring>
#include "common/logging/log.h"
-#include "input_common/udp/protocol.h"
+#include "input_common/helpers/udp_protocol.h"
namespace InputCommon::CemuhookUDP {
diff --git a/src/input_common/udp/protocol.h b/src/input_common/helpers/udp_protocol.h
index 1bdc9209e..bcba12c58 100644
--- a/src/input_common/udp/protocol.h
+++ b/src/input_common/helpers/udp_protocol.h
@@ -56,6 +56,12 @@ constexpr Type GetMessageType();
namespace Request {
+enum RegisterFlags : u8 {
+ AllPads,
+ PadID,
+ PadMACAdddress,
+};
+
struct Version {};
/**
* Requests the server to send information about what controllers are plugged into the ports
@@ -77,13 +83,8 @@ static_assert(std::is_trivially_copyable_v<PortInfo>,
* timeout seems to be 5 seconds.
*/
struct PadData {
- enum class Flags : u8 {
- AllPorts,
- Id,
- Mac,
- };
/// Determines which method will be used as a look up for the controller
- Flags flags{};
+ RegisterFlags flags{};
/// Index of the port of the controller to retrieve data about
u8 port_id{};
/// Mac address of the controller to retrieve data about
@@ -113,6 +114,36 @@ Message<T> Create(const T data, const u32 client_id = 0) {
namespace Response {
+enum class ConnectionType : u8 {
+ None,
+ Usb,
+ Bluetooth,
+};
+
+enum class State : u8 {
+ Disconnected,
+ Reserved,
+ Connected,
+};
+
+enum class Model : u8 {
+ None,
+ PartialGyro,
+ FullGyro,
+ Generic,
+};
+
+enum class Battery : u8 {
+ None = 0x00,
+ Dying = 0x01,
+ Low = 0x02,
+ Medium = 0x03,
+ High = 0x04,
+ Full = 0x05,
+ Charging = 0xEE,
+ Charged = 0xEF,
+};
+
struct Version {
u16_le version{};
};
@@ -122,11 +153,11 @@ static_assert(std::is_trivially_copyable_v<Version>,
struct PortInfo {
u8 id{};
- u8 state{};
- u8 model{};
- u8 connection_type{};
+ State state{};
+ Model model{};
+ ConnectionType connection_type{};
MacAddress mac;
- u8 battery{};
+ Battery battery{};
u8 is_pad_active{};
};
static_assert(sizeof(PortInfo) == 12, "UDP Response PortInfo struct has wrong size");
@@ -177,18 +208,18 @@ struct PadData {
u8 right_stick_y{};
struct AnalogButton {
- u8 button_8{};
- u8 button_7{};
- u8 button_6{};
- u8 button_5{};
- u8 button_12{};
- u8 button_11{};
- u8 button_10{};
- u8 button_9{};
- u8 button_16{};
- u8 button_15{};
- u8 button_14{};
- u8 button_13{};
+ u8 button_dpad_left_analog{};
+ u8 button_dpad_down_analog{};
+ u8 button_dpad_right_analog{};
+ u8 button_dpad_up_analog{};
+ u8 button_square_analog{};
+ u8 button_cross_analog{};
+ u8 button_circle_analog{};
+ u8 button_triangle_analog{};
+ u8 button_r1_analog{};
+ u8 button_l1_analog{};
+ u8 trigger_r2{};
+ u8 trigger_l2{};
} analog_button;
std::array<TouchPad, 2> touch;