summaryrefslogtreecommitdiff
path: root/src/input_common/sdl/sdl_impl.cpp
diff options
context:
space:
mode:
authorGravatar german2020-09-17 20:26:34 -0500
committerGravatar german2020-09-25 17:59:52 -0500
commit03b574ae2272fc8465e7d38f21b198fcb1885186 (patch)
treee6e70906677339fce1a7bedafab8d8fcb39026b4 /src/input_common/sdl/sdl_impl.cpp
parentMerge pull request #4717 from lioncash/debug (diff)
downloadyuzu-03b574ae2272fc8465e7d38f21b198fcb1885186.tar.gz
yuzu-03b574ae2272fc8465e7d38f21b198fcb1885186.tar.xz
yuzu-03b574ae2272fc8465e7d38f21b198fcb1885186.zip
Add random motion input to SDL
Diffstat (limited to 'src/input_common/sdl/sdl_impl.cpp')
-rw-r--r--src/input_common/sdl/sdl_impl.cpp190
1 files changed, 190 insertions, 0 deletions
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;