summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/input_common/motion_input.cpp32
-rw-r--r--src/input_common/motion_input.h3
-rw-r--r--src/input_common/sdl/sdl_impl.cpp190
-rw-r--r--src/input_common/sdl/sdl_impl.h2
-rw-r--r--src/input_common/udp/client.cpp8
5 files changed, 230 insertions, 5 deletions
diff --git a/src/input_common/motion_input.cpp b/src/input_common/motion_input.cpp
index 22a849866..b99d3497f 100644
--- a/src/input_common/motion_input.cpp
+++ b/src/input_common/motion_input.cpp
@@ -2,6 +2,7 @@
2// Licensed under GPLv2 or any later version 2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included 3// Refer to the license.txt file included
4 4
5#include <random>
5#include "common/math_util.h" 6#include "common/math_util.h"
6#include "input_common/motion_input.h" 7#include "input_common/motion_input.h"
7 8
@@ -159,6 +160,37 @@ Common::Vec3f MotionInput::GetRotations() const {
159 return rotations; 160 return rotations;
160} 161}
161 162
163Input::MotionStatus MotionInput::GetMotion() const {
164 const Common::Vec3f gyroscope = GetGyroscope();
165 const Common::Vec3f accelerometer = GetAcceleration();
166 const Common::Vec3f rotation = GetRotations();
167 const std::array<Common::Vec3f, 3> orientation = GetOrientation();
168 return {accelerometer, gyroscope, rotation, orientation};
169}
170
171Input::MotionStatus MotionInput::GetRandomMotion(int accel_magnitude, int gyro_magnitude) const {
172 std::random_device device;
173 std::mt19937 gen(device());
174 std::uniform_int_distribution<s16> distribution(-1000, 1000);
175 const Common::Vec3f gyroscope = {
176 distribution(gen) * 0.001f,
177 distribution(gen) * 0.001f,
178 distribution(gen) * 0.001f,
179 };
180 const Common::Vec3f accelerometer = {
181 distribution(gen) * 0.001f,
182 distribution(gen) * 0.001f,
183 distribution(gen) * 0.001f,
184 };
185 const Common::Vec3f rotation = {};
186 const std::array<Common::Vec3f, 3> orientation = {
187 Common::Vec3f{1.0f, 0, 0},
188 Common::Vec3f{0, 1.0f, 0},
189 Common::Vec3f{0, 0, 1.0f},
190 };
191 return {accelerometer * accel_magnitude, gyroscope * gyro_magnitude, rotation, orientation};
192}
193
162void MotionInput::ResetOrientation() { 194void MotionInput::ResetOrientation() {
163 if (!reset_enabled) { 195 if (!reset_enabled) {
164 return; 196 return;
diff --git a/src/input_common/motion_input.h b/src/input_common/motion_input.h
index 54b4439d9..12b7d0d3f 100644
--- a/src/input_common/motion_input.h
+++ b/src/input_common/motion_input.h
@@ -7,6 +7,7 @@
7#include "common/common_types.h" 7#include "common/common_types.h"
8#include "common/quaternion.h" 8#include "common/quaternion.h"
9#include "common/vector_math.h" 9#include "common/vector_math.h"
10#include "core/frontend/input.h"
10 11
11namespace InputCommon { 12namespace InputCommon {
12 13
@@ -37,6 +38,8 @@ public:
37 Common::Vec3f GetGyroscope() const; 38 Common::Vec3f GetGyroscope() const;
38 Common::Vec3f GetRotations() const; 39 Common::Vec3f GetRotations() const;
39 Common::Quaternion<f32> GetQuaternion() const; 40 Common::Quaternion<f32> GetQuaternion() const;
41 Input::MotionStatus GetMotion() const;
42 Input::MotionStatus GetRandomMotion(int accel_magnitude, int gyro_magnitude) const;
40 43
41 bool IsMoving(f32 sensitivity) const; 44 bool IsMoving(f32 sensitivity) const;
42 bool IsCalibrated(f32 sensitivity) const; 45 bool IsCalibrated(f32 sensitivity) const;
diff --git a/src/input_common/sdl/sdl_impl.cpp b/src/input_common/sdl/sdl_impl.cpp
index a9e676f4b..0b0095978 100644
--- a/src/input_common/sdl/sdl_impl.cpp
+++ b/src/input_common/sdl/sdl_impl.cpp
@@ -21,6 +21,7 @@
21#include "common/param_package.h" 21#include "common/param_package.h"
22#include "common/threadsafe_queue.h" 22#include "common/threadsafe_queue.h"
23#include "core/frontend/input.h" 23#include "core/frontend/input.h"
24#include "input_common/motion_input.h"
24#include "input_common/sdl/sdl_impl.h" 25#include "input_common/sdl/sdl_impl.h"
25#include "input_common/settings.h" 26#include "input_common/settings.h"
26 27
@@ -95,6 +96,10 @@ public:
95 return std::make_tuple(x, y); 96 return std::make_tuple(x, y);
96 } 97 }
97 98
99 const InputCommon::MotionInput& GetMotion() const {
100 return motion;
101 }
102
98 void SetHat(int hat, Uint8 direction) { 103 void SetHat(int hat, Uint8 direction) {
99 std::lock_guard lock{mutex}; 104 std::lock_guard lock{mutex};
100 state.hats.insert_or_assign(hat, direction); 105 state.hats.insert_or_assign(hat, direction);
@@ -142,6 +147,9 @@ private:
142 std::unique_ptr<SDL_Joystick, decltype(&SDL_JoystickClose)> sdl_joystick; 147 std::unique_ptr<SDL_Joystick, decltype(&SDL_JoystickClose)> sdl_joystick;
143 std::unique_ptr<SDL_GameController, decltype(&SDL_GameControllerClose)> sdl_controller; 148 std::unique_ptr<SDL_GameController, decltype(&SDL_GameControllerClose)> sdl_controller;
144 mutable std::mutex mutex; 149 mutable std::mutex mutex;
150
151 // motion is initalized without PID values as motion input is not aviable for SDL2
152 InputCommon::MotionInput motion{0.0f, 0.0f, 0.0f};
145}; 153};
146 154
147std::shared_ptr<SDLJoystick> SDLState::GetSDLJoystickByGUID(const std::string& guid, int port) { 155std::shared_ptr<SDLJoystick> SDLState::GetSDLJoystickByGUID(const std::string& guid, int port) {
@@ -386,6 +394,68 @@ private:
386 const float range; 394 const float range;
387}; 395};
388 396
397class SDLDirectionMotion final : public Input::MotionDevice {
398public:
399 explicit SDLDirectionMotion(std::shared_ptr<SDLJoystick> joystick_, int hat_, Uint8 direction_)
400 : joystick(std::move(joystick_)), hat(hat_), direction(direction_) {}
401
402 Input::MotionStatus GetStatus() const override {
403 if (joystick->GetHatDirection(hat, direction)) {
404 return joystick->GetMotion().GetRandomMotion(2, 6);
405 }
406 return joystick->GetMotion().GetRandomMotion(0, 0);
407 }
408
409private:
410 std::shared_ptr<SDLJoystick> joystick;
411 int hat;
412 Uint8 direction;
413};
414
415class SDLAxisMotion final : public Input::MotionDevice {
416public:
417 explicit SDLAxisMotion(std::shared_ptr<SDLJoystick> joystick_, int axis_, float threshold_,
418 bool trigger_if_greater_)
419 : joystick(std::move(joystick_)), axis(axis_), threshold(threshold_),
420 trigger_if_greater(trigger_if_greater_) {}
421
422 Input::MotionStatus GetStatus() const override {
423 const float axis_value = joystick->GetAxis(axis, 1.0f);
424 bool trigger = axis_value < threshold;
425 if (trigger_if_greater) {
426 trigger = axis_value > threshold;
427 }
428
429 if (trigger) {
430 return joystick->GetMotion().GetRandomMotion(2, 6);
431 }
432 return joystick->GetMotion().GetRandomMotion(0, 0);
433 }
434
435private:
436 std::shared_ptr<SDLJoystick> joystick;
437 int axis;
438 float threshold;
439 bool trigger_if_greater;
440};
441
442class SDLButtonMotion final : public Input::MotionDevice {
443public:
444 explicit SDLButtonMotion(std::shared_ptr<SDLJoystick> joystick_, int button_)
445 : joystick(std::move(joystick_)), button(button_) {}
446
447 Input::MotionStatus GetStatus() const override {
448 if (joystick->GetButton(button)) {
449 return joystick->GetMotion().GetRandomMotion(2, 6);
450 }
451 return joystick->GetMotion().GetRandomMotion(0, 0);
452 }
453
454private:
455 std::shared_ptr<SDLJoystick> joystick;
456 int button;
457};
458
389/// A button device factory that creates button devices from SDL joystick 459/// A button device factory that creates button devices from SDL joystick
390class SDLButtonFactory final : public Input::Factory<Input::ButtonDevice> { 460class SDLButtonFactory final : public Input::Factory<Input::ButtonDevice> {
391public: 461public:
@@ -492,12 +562,78 @@ private:
492 SDLState& state; 562 SDLState& state;
493}; 563};
494 564
565/// A motion device factory that creates motion devices from SDL joystick
566class SDLMotionFactory final : public Input::Factory<Input::MotionDevice> {
567public:
568 explicit SDLMotionFactory(SDLState& state_) : state(state_) {}
569 /**
570 * Creates motion device from joystick axes
571 * @param params contains parameters for creating the device:
572 * - "guid": the guid of the joystick to bind
573 * - "port": the nth joystick of the same type
574 */
575 std::unique_ptr<Input::MotionDevice> Create(const Common::ParamPackage& params) override {
576 const std::string guid = params.Get("guid", "0");
577 const int port = params.Get("port", 0);
578
579 auto joystick = state.GetSDLJoystickByGUID(guid, port);
580
581 if (params.Has("hat")) {
582 const int hat = params.Get("hat", 0);
583 const std::string direction_name = params.Get("direction", "");
584 Uint8 direction;
585 if (direction_name == "up") {
586 direction = SDL_HAT_UP;
587 } else if (direction_name == "down") {
588 direction = SDL_HAT_DOWN;
589 } else if (direction_name == "left") {
590 direction = SDL_HAT_LEFT;
591 } else if (direction_name == "right") {
592 direction = SDL_HAT_RIGHT;
593 } else {
594 direction = 0;
595 }
596 // This is necessary so accessing GetHat with hat won't crash
597 joystick->SetHat(hat, SDL_HAT_CENTERED);
598 return std::make_unique<SDLDirectionMotion>(joystick, hat, direction);
599 }
600
601 if (params.Has("axis")) {
602 const int axis = params.Get("axis", 0);
603 const float threshold = params.Get("threshold", 0.5f);
604 const std::string direction_name = params.Get("direction", "");
605 bool trigger_if_greater;
606 if (direction_name == "+") {
607 trigger_if_greater = true;
608 } else if (direction_name == "-") {
609 trigger_if_greater = false;
610 } else {
611 trigger_if_greater = true;
612 LOG_ERROR(Input, "Unknown direction {}", direction_name);
613 }
614 // This is necessary so accessing GetAxis with axis won't crash
615 joystick->SetAxis(axis, 0);
616 return std::make_unique<SDLAxisMotion>(joystick, axis, threshold, trigger_if_greater);
617 }
618
619 const int button = params.Get("button", 0);
620 // This is necessary so accessing GetButton with button won't crash
621 joystick->SetButton(button, false);
622 return std::make_unique<SDLButtonMotion>(joystick, button);
623 }
624
625private:
626 SDLState& state;
627};
628
495SDLState::SDLState() { 629SDLState::SDLState() {
496 using namespace Input; 630 using namespace Input;
497 analog_factory = std::make_shared<SDLAnalogFactory>(*this); 631 analog_factory = std::make_shared<SDLAnalogFactory>(*this);
498 button_factory = std::make_shared<SDLButtonFactory>(*this); 632 button_factory = std::make_shared<SDLButtonFactory>(*this);
633 motion_factory = std::make_shared<SDLMotionFactory>(*this);
499 RegisterFactory<AnalogDevice>("sdl", analog_factory); 634 RegisterFactory<AnalogDevice>("sdl", analog_factory);
500 RegisterFactory<ButtonDevice>("sdl", button_factory); 635 RegisterFactory<ButtonDevice>("sdl", button_factory);
636 RegisterFactory<MotionDevice>("sdl", motion_factory);
501 637
502 // If the frontend is going to manage the event loop, then we dont start one here 638 // If the frontend is going to manage the event loop, then we dont start one here
503 start_thread = !SDL_WasInit(SDL_INIT_JOYSTICK); 639 start_thread = !SDL_WasInit(SDL_INIT_JOYSTICK);
@@ -533,6 +669,7 @@ SDLState::~SDLState() {
533 using namespace Input; 669 using namespace Input;
534 UnregisterFactory<ButtonDevice>("sdl"); 670 UnregisterFactory<ButtonDevice>("sdl");
535 UnregisterFactory<AnalogDevice>("sdl"); 671 UnregisterFactory<AnalogDevice>("sdl");
672 UnregisterFactory<MotionDevice>("sdl");
536 673
537 CloseJoysticks(); 674 CloseJoysticks();
538 SDL_DelEventWatch(&SDLEventWatcher, this); 675 SDL_DelEventWatch(&SDLEventWatcher, this);
@@ -644,6 +781,27 @@ Common::ParamPackage SDLEventToButtonParamPackage(SDLState& state, const SDL_Eve
644 return {}; 781 return {};
645} 782}
646 783
784Common::ParamPackage SDLEventToMotionParamPackage(SDLState& state, const SDL_Event& event) {
785 switch (event.type) {
786 case SDL_JOYAXISMOTION: {
787 const auto joystick = state.GetSDLJoystickBySDLID(event.jaxis.which);
788 return BuildAnalogParamPackageForButton(joystick->GetPort(), joystick->GetGUID(),
789 event.jaxis.axis, event.jaxis.value);
790 }
791 case SDL_JOYBUTTONUP: {
792 const auto joystick = state.GetSDLJoystickBySDLID(event.jbutton.which);
793 return BuildButtonParamPackageForButton(joystick->GetPort(), joystick->GetGUID(),
794 event.jbutton.button);
795 }
796 case SDL_JOYHATMOTION: {
797 const auto joystick = state.GetSDLJoystickBySDLID(event.jhat.which);
798 return BuildHatParamPackageForButton(joystick->GetPort(), joystick->GetGUID(),
799 event.jhat.hat, event.jhat.value);
800 }
801 }
802 return {};
803}
804
647Common::ParamPackage BuildParamPackageForBinding(int port, const std::string& guid, 805Common::ParamPackage BuildParamPackageForBinding(int port, const std::string& guid,
648 const SDL_GameControllerButtonBind& binding) { 806 const SDL_GameControllerButtonBind& binding) {
649 switch (binding.bindType) { 807 switch (binding.bindType) {
@@ -809,6 +967,35 @@ public:
809 } 967 }
810}; 968};
811 969
970class SDLMotionPoller final : public SDLPoller {
971public:
972 explicit SDLMotionPoller(SDLState& state_) : SDLPoller(state_) {}
973
974 Common::ParamPackage GetNextInput() override {
975 SDL_Event event;
976 while (state.event_queue.Pop(event)) {
977 const auto package = FromEvent(event);
978 if (package) {
979 return *package;
980 }
981 }
982 return {};
983 }
984 [[nodiscard]] std::optional<Common::ParamPackage> FromEvent(const SDL_Event& event) const {
985 switch (event.type) {
986 case SDL_JOYAXISMOTION:
987 if (std::abs(event.jaxis.value / 32767.0) < 0.5) {
988 break;
989 }
990 [[fallthrough]];
991 case SDL_JOYBUTTONUP:
992 case SDL_JOYHATMOTION:
993 return {SDLEventToMotionParamPackage(state, event)};
994 }
995 return std::nullopt;
996 }
997};
998
812/** 999/**
813 * Attempts to match the press to a controller joy axis (left/right stick) and if a match 1000 * Attempts to match the press to a controller joy axis (left/right stick) and if a match
814 * isn't found, checks if the event matches anything from SDLButtonPoller and uses that 1001 * isn't found, checks if the event matches anything from SDLButtonPoller and uses that
@@ -900,6 +1087,9 @@ SDLState::Pollers SDLState::GetPollers(InputCommon::Polling::DeviceType type) {
900 case InputCommon::Polling::DeviceType::Button: 1087 case InputCommon::Polling::DeviceType::Button:
901 pollers.emplace_back(std::make_unique<Polling::SDLButtonPoller>(*this)); 1088 pollers.emplace_back(std::make_unique<Polling::SDLButtonPoller>(*this));
902 break; 1089 break;
1090 case InputCommon::Polling::DeviceType::Motion:
1091 pollers.emplace_back(std::make_unique<Polling::SDLMotionPoller>(*this));
1092 break;
903 } 1093 }
904 1094
905 return pollers; 1095 return pollers;
diff --git a/src/input_common/sdl/sdl_impl.h b/src/input_common/sdl/sdl_impl.h
index bd19ba61d..b9bb4dc56 100644
--- a/src/input_common/sdl/sdl_impl.h
+++ b/src/input_common/sdl/sdl_impl.h
@@ -21,6 +21,7 @@ namespace InputCommon::SDL {
21 21
22class SDLAnalogFactory; 22class SDLAnalogFactory;
23class SDLButtonFactory; 23class SDLButtonFactory;
24class SDLMotionFactory;
24class SDLJoystick; 25class SDLJoystick;
25 26
26class SDLState : public State { 27class SDLState : public State {
@@ -71,6 +72,7 @@ private:
71 72
72 std::shared_ptr<SDLButtonFactory> button_factory; 73 std::shared_ptr<SDLButtonFactory> button_factory;
73 std::shared_ptr<SDLAnalogFactory> analog_factory; 74 std::shared_ptr<SDLAnalogFactory> analog_factory;
75 std::shared_ptr<SDLMotionFactory> motion_factory;
74 76
75 bool start_thread = false; 77 bool start_thread = false;
76 std::atomic<bool> initialized = false; 78 std::atomic<bool> initialized = false;
diff --git a/src/input_common/udp/client.cpp b/src/input_common/udp/client.cpp
index 2b6a68d4b..b6323d56f 100644
--- a/src/input_common/udp/client.cpp
+++ b/src/input_common/udp/client.cpp
@@ -219,14 +219,10 @@ void Client::OnPadData(Response::PadData data) {
219 clients[client].motion.SetGyroscope(raw_gyroscope / 312.0f); 219 clients[client].motion.SetGyroscope(raw_gyroscope / 312.0f);
220 clients[client].motion.UpdateRotation(time_difference); 220 clients[client].motion.UpdateRotation(time_difference);
221 clients[client].motion.UpdateOrientation(time_difference); 221 clients[client].motion.UpdateOrientation(time_difference);
222 Common::Vec3f gyroscope = clients[client].motion.GetGyroscope();
223 Common::Vec3f accelerometer = clients[client].motion.GetAcceleration();
224 Common::Vec3f rotation = clients[client].motion.GetRotations();
225 std::array<Common::Vec3f, 3> orientation = clients[client].motion.GetOrientation();
226 222
227 { 223 {
228 std::lock_guard guard(clients[client].status.update_mutex); 224 std::lock_guard guard(clients[client].status.update_mutex);
229 clients[client].status.motion_status = {accelerometer, gyroscope, rotation, orientation}; 225 clients[client].status.motion_status = clients[client].motion.GetMotion();
230 226
231 // TODO: add a setting for "click" touch. Click touch refers to a device that differentiates 227 // TODO: add a setting for "click" touch. Click touch refers to a device that differentiates
232 // between a simple "tap" and a hard press that causes the touch screen to click. 228 // between a simple "tap" and a hard press that causes the touch screen to click.
@@ -250,6 +246,8 @@ void Client::OnPadData(Response::PadData data) {
250 clients[client].status.touch_status = {x, y, is_active}; 246 clients[client].status.touch_status = {x, y, is_active};
251 247
252 if (configuring) { 248 if (configuring) {
249 const Common::Vec3f gyroscope = clients[client].motion.GetGyroscope();
250 const Common::Vec3f accelerometer = clients[client].motion.GetAcceleration();
253 UpdateYuzuSettings(client, accelerometer, gyroscope, is_active); 251 UpdateYuzuSettings(client, accelerometer, gyroscope, is_active);
254 } 252 }
255 } 253 }