diff options
| -rw-r--r-- | src/input_common/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | src/input_common/main.cpp | 4 | ||||
| -rw-r--r-- | src/input_common/motion_from_button.cpp | 34 | ||||
| -rw-r--r-- | src/input_common/motion_from_button.h | 25 | ||||
| -rw-r--r-- | src/input_common/motion_input.cpp | 32 | ||||
| -rw-r--r-- | src/input_common/motion_input.h | 3 | ||||
| -rw-r--r-- | src/input_common/sdl/sdl_impl.cpp | 190 | ||||
| -rw-r--r-- | src/input_common/sdl/sdl_impl.h | 2 | ||||
| -rw-r--r-- | src/input_common/udp/client.cpp | 8 |
9 files changed, 295 insertions, 5 deletions
diff --git a/src/input_common/CMakeLists.txt b/src/input_common/CMakeLists.txt index 09361e37e..c84685214 100644 --- a/src/input_common/CMakeLists.txt +++ b/src/input_common/CMakeLists.txt | |||
| @@ -7,6 +7,8 @@ add_library(input_common STATIC | |||
| 7 | main.h | 7 | main.h |
| 8 | motion_emu.cpp | 8 | motion_emu.cpp |
| 9 | motion_emu.h | 9 | motion_emu.h |
| 10 | motion_from_button.cpp | ||
| 11 | motion_from_button.h | ||
| 10 | motion_input.cpp | 12 | motion_input.cpp |
| 11 | motion_input.h | 13 | motion_input.h |
| 12 | settings.cpp | 14 | settings.cpp |
diff --git a/src/input_common/main.cpp b/src/input_common/main.cpp index 8da829132..3d97d95f7 100644 --- a/src/input_common/main.cpp +++ b/src/input_common/main.cpp | |||
| @@ -11,6 +11,7 @@ | |||
| 11 | #include "input_common/keyboard.h" | 11 | #include "input_common/keyboard.h" |
| 12 | #include "input_common/main.h" | 12 | #include "input_common/main.h" |
| 13 | #include "input_common/motion_emu.h" | 13 | #include "input_common/motion_emu.h" |
| 14 | #include "input_common/motion_from_button.h" | ||
| 14 | #include "input_common/touch_from_button.h" | 15 | #include "input_common/touch_from_button.h" |
| 15 | #include "input_common/udp/client.h" | 16 | #include "input_common/udp/client.h" |
| 16 | #include "input_common/udp/udp.h" | 17 | #include "input_common/udp/udp.h" |
| @@ -32,6 +33,8 @@ struct InputSubsystem::Impl { | |||
| 32 | Input::RegisterFactory<Input::ButtonDevice>("keyboard", keyboard); | 33 | Input::RegisterFactory<Input::ButtonDevice>("keyboard", keyboard); |
| 33 | Input::RegisterFactory<Input::AnalogDevice>("analog_from_button", | 34 | Input::RegisterFactory<Input::AnalogDevice>("analog_from_button", |
| 34 | std::make_shared<AnalogFromButton>()); | 35 | std::make_shared<AnalogFromButton>()); |
| 36 | Input::RegisterFactory<Input::MotionDevice>("keyboard", | ||
| 37 | std::make_shared<MotionFromButton>()); | ||
| 35 | motion_emu = std::make_shared<MotionEmu>(); | 38 | motion_emu = std::make_shared<MotionEmu>(); |
| 36 | Input::RegisterFactory<Input::MotionDevice>("motion_emu", motion_emu); | 39 | Input::RegisterFactory<Input::MotionDevice>("motion_emu", motion_emu); |
| 37 | Input::RegisterFactory<Input::TouchDevice>("touch_from_button", | 40 | Input::RegisterFactory<Input::TouchDevice>("touch_from_button", |
| @@ -50,6 +53,7 @@ struct InputSubsystem::Impl { | |||
| 50 | 53 | ||
| 51 | void Shutdown() { | 54 | void Shutdown() { |
| 52 | Input::UnregisterFactory<Input::ButtonDevice>("keyboard"); | 55 | Input::UnregisterFactory<Input::ButtonDevice>("keyboard"); |
| 56 | Input::UnregisterFactory<Input::MotionDevice>("keyboard"); | ||
| 53 | keyboard.reset(); | 57 | keyboard.reset(); |
| 54 | Input::UnregisterFactory<Input::AnalogDevice>("analog_from_button"); | 58 | Input::UnregisterFactory<Input::AnalogDevice>("analog_from_button"); |
| 55 | Input::UnregisterFactory<Input::MotionDevice>("motion_emu"); | 59 | Input::UnregisterFactory<Input::MotionDevice>("motion_emu"); |
diff --git a/src/input_common/motion_from_button.cpp b/src/input_common/motion_from_button.cpp new file mode 100644 index 000000000..9d459f963 --- /dev/null +++ b/src/input_common/motion_from_button.cpp | |||
| @@ -0,0 +1,34 @@ | |||
| 1 | // Copyright 2020 yuzu Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #include "input_common/motion_from_button.h" | ||
| 6 | #include "input_common/motion_input.h" | ||
| 7 | |||
| 8 | namespace InputCommon { | ||
| 9 | |||
| 10 | class MotionKey final : public Input::MotionDevice { | ||
| 11 | public: | ||
| 12 | using Button = std::unique_ptr<Input::ButtonDevice>; | ||
| 13 | |||
| 14 | MotionKey(Button key_) : key(std::move(key_)) {} | ||
| 15 | |||
| 16 | Input::MotionStatus GetStatus() const override { | ||
| 17 | |||
| 18 | if (key->GetStatus()) { | ||
| 19 | return motion.GetRandomMotion(2, 6); | ||
| 20 | } | ||
| 21 | return motion.GetRandomMotion(0, 0); | ||
| 22 | } | ||
| 23 | |||
| 24 | private: | ||
| 25 | Button key; | ||
| 26 | InputCommon::MotionInput motion{0.0f, 0.0f, 0.0f}; | ||
| 27 | }; | ||
| 28 | |||
| 29 | std::unique_ptr<Input::MotionDevice> MotionFromButton::Create(const Common::ParamPackage& params) { | ||
| 30 | auto key = Input::CreateDevice<Input::ButtonDevice>(params.Serialize()); | ||
| 31 | return std::make_unique<MotionKey>(std::move(key)); | ||
| 32 | } | ||
| 33 | |||
| 34 | } // namespace InputCommon | ||
diff --git a/src/input_common/motion_from_button.h b/src/input_common/motion_from_button.h new file mode 100644 index 000000000..a959046fb --- /dev/null +++ b/src/input_common/motion_from_button.h | |||
| @@ -0,0 +1,25 @@ | |||
| 1 | // Copyright 2020 yuzu Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include "core/frontend/input.h" | ||
| 8 | |||
| 9 | namespace InputCommon { | ||
| 10 | |||
| 11 | /** | ||
| 12 | * An motion device factory that takes a keyboard button and uses it as a random | ||
| 13 | * motion device. | ||
| 14 | */ | ||
| 15 | class MotionFromButton final : public Input::Factory<Input::MotionDevice> { | ||
| 16 | public: | ||
| 17 | /** | ||
| 18 | * Creates an motion device from button devices | ||
| 19 | * @param params contains parameters for creating the device: | ||
| 20 | * - "key": a serialized ParamPackage for creating a button device | ||
| 21 | */ | ||
| 22 | std::unique_ptr<Input::MotionDevice> Create(const Common::ParamPackage& params) override; | ||
| 23 | }; | ||
| 24 | |||
| 25 | } // namespace InputCommon | ||
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 | ||
| 163 | Input::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 | |||
| 171 | Input::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 | |||
| 162 | void MotionInput::ResetOrientation() { | 194 | void 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 | ||
| 11 | namespace InputCommon { | 12 | namespace 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 27a96c18b..bd480570a 100644 --- a/src/input_common/sdl/sdl_impl.cpp +++ b/src/input_common/sdl/sdl_impl.cpp | |||
| @@ -22,6 +22,7 @@ | |||
| 22 | #include "common/param_package.h" | 22 | #include "common/param_package.h" |
| 23 | #include "common/threadsafe_queue.h" | 23 | #include "common/threadsafe_queue.h" |
| 24 | #include "core/frontend/input.h" | 24 | #include "core/frontend/input.h" |
| 25 | #include "input_common/motion_input.h" | ||
| 25 | #include "input_common/sdl/sdl_impl.h" | 26 | #include "input_common/sdl/sdl_impl.h" |
| 26 | #include "input_common/settings.h" | 27 | #include "input_common/settings.h" |
| 27 | 28 | ||
| @@ -123,6 +124,10 @@ public: | |||
| 123 | return std::make_tuple(x, y); | 124 | return std::make_tuple(x, y); |
| 124 | } | 125 | } |
| 125 | 126 | ||
| 127 | const InputCommon::MotionInput& GetMotion() const { | ||
| 128 | return motion; | ||
| 129 | } | ||
| 130 | |||
| 126 | void SetHat(int hat, Uint8 direction) { | 131 | void SetHat(int hat, Uint8 direction) { |
| 127 | std::lock_guard lock{mutex}; | 132 | std::lock_guard lock{mutex}; |
| 128 | state.hats.insert_or_assign(hat, direction); | 133 | state.hats.insert_or_assign(hat, direction); |
| @@ -173,6 +178,9 @@ private: | |||
| 173 | std::unique_ptr<SDL_Joystick, decltype(&SDL_JoystickClose)> sdl_joystick; | 178 | std::unique_ptr<SDL_Joystick, decltype(&SDL_JoystickClose)> sdl_joystick; |
| 174 | std::unique_ptr<SDL_GameController, decltype(&SDL_GameControllerClose)> sdl_controller; | 179 | std::unique_ptr<SDL_GameController, decltype(&SDL_GameControllerClose)> sdl_controller; |
| 175 | mutable std::mutex mutex; | 180 | mutable std::mutex mutex; |
| 181 | |||
| 182 | // motion is initalized without PID values as motion input is not aviable for SDL2 | ||
| 183 | InputCommon::MotionInput motion{0.0f, 0.0f, 0.0f}; | ||
| 176 | }; | 184 | }; |
| 177 | 185 | ||
| 178 | std::shared_ptr<SDLJoystick> SDLState::GetSDLJoystickByGUID(const std::string& guid, int port) { | 186 | std::shared_ptr<SDLJoystick> SDLState::GetSDLJoystickByGUID(const std::string& guid, int port) { |
| @@ -423,6 +431,68 @@ private: | |||
| 423 | const float range; | 431 | const float range; |
| 424 | }; | 432 | }; |
| 425 | 433 | ||
| 434 | class SDLDirectionMotion final : public Input::MotionDevice { | ||
| 435 | public: | ||
| 436 | explicit SDLDirectionMotion(std::shared_ptr<SDLJoystick> joystick_, int hat_, Uint8 direction_) | ||
| 437 | : joystick(std::move(joystick_)), hat(hat_), direction(direction_) {} | ||
| 438 | |||
| 439 | Input::MotionStatus GetStatus() const override { | ||
| 440 | if (joystick->GetHatDirection(hat, direction)) { | ||
| 441 | return joystick->GetMotion().GetRandomMotion(2, 6); | ||
| 442 | } | ||
| 443 | return joystick->GetMotion().GetRandomMotion(0, 0); | ||
| 444 | } | ||
| 445 | |||
| 446 | private: | ||
| 447 | std::shared_ptr<SDLJoystick> joystick; | ||
| 448 | int hat; | ||
| 449 | Uint8 direction; | ||
| 450 | }; | ||
| 451 | |||
| 452 | class SDLAxisMotion final : public Input::MotionDevice { | ||
| 453 | public: | ||
| 454 | explicit SDLAxisMotion(std::shared_ptr<SDLJoystick> joystick_, int axis_, float threshold_, | ||
| 455 | bool trigger_if_greater_) | ||
| 456 | : joystick(std::move(joystick_)), axis(axis_), threshold(threshold_), | ||
| 457 | trigger_if_greater(trigger_if_greater_) {} | ||
| 458 | |||
| 459 | Input::MotionStatus GetStatus() const override { | ||
| 460 | const float axis_value = joystick->GetAxis(axis, 1.0f); | ||
| 461 | bool trigger = axis_value < threshold; | ||
| 462 | if (trigger_if_greater) { | ||
| 463 | trigger = axis_value > threshold; | ||
| 464 | } | ||
| 465 | |||
| 466 | if (trigger) { | ||
| 467 | return joystick->GetMotion().GetRandomMotion(2, 6); | ||
| 468 | } | ||
| 469 | return joystick->GetMotion().GetRandomMotion(0, 0); | ||
| 470 | } | ||
| 471 | |||
| 472 | private: | ||
| 473 | std::shared_ptr<SDLJoystick> joystick; | ||
| 474 | int axis; | ||
| 475 | float threshold; | ||
| 476 | bool trigger_if_greater; | ||
| 477 | }; | ||
| 478 | |||
| 479 | class SDLButtonMotion final : public Input::MotionDevice { | ||
| 480 | public: | ||
| 481 | explicit SDLButtonMotion(std::shared_ptr<SDLJoystick> joystick_, int button_) | ||
| 482 | : joystick(std::move(joystick_)), button(button_) {} | ||
| 483 | |||
| 484 | Input::MotionStatus GetStatus() const override { | ||
| 485 | if (joystick->GetButton(button)) { | ||
| 486 | return joystick->GetMotion().GetRandomMotion(2, 6); | ||
| 487 | } | ||
| 488 | return joystick->GetMotion().GetRandomMotion(0, 0); | ||
| 489 | } | ||
| 490 | |||
| 491 | private: | ||
| 492 | std::shared_ptr<SDLJoystick> joystick; | ||
| 493 | int button; | ||
| 494 | }; | ||
| 495 | |||
| 426 | /// A button device factory that creates button devices from SDL joystick | 496 | /// A button device factory that creates button devices from SDL joystick |
| 427 | class SDLButtonFactory final : public Input::Factory<Input::ButtonDevice> { | 497 | class SDLButtonFactory final : public Input::Factory<Input::ButtonDevice> { |
| 428 | public: | 498 | public: |
| @@ -529,12 +599,78 @@ private: | |||
| 529 | SDLState& state; | 599 | SDLState& state; |
| 530 | }; | 600 | }; |
| 531 | 601 | ||
| 602 | /// A motion device factory that creates motion devices from SDL joystick | ||
| 603 | class SDLMotionFactory final : public Input::Factory<Input::MotionDevice> { | ||
| 604 | public: | ||
| 605 | explicit SDLMotionFactory(SDLState& state_) : state(state_) {} | ||
| 606 | /** | ||
| 607 | * Creates motion device from joystick axes | ||
| 608 | * @param params contains parameters for creating the device: | ||
| 609 | * - "guid": the guid of the joystick to bind | ||
| 610 | * - "port": the nth joystick of the same type | ||
| 611 | */ | ||
| 612 | std::unique_ptr<Input::MotionDevice> Create(const Common::ParamPackage& params) override { | ||
| 613 | const std::string guid = params.Get("guid", "0"); | ||
| 614 | const int port = params.Get("port", 0); | ||
| 615 | |||
| 616 | auto joystick = state.GetSDLJoystickByGUID(guid, port); | ||
| 617 | |||
| 618 | if (params.Has("hat")) { | ||
| 619 | const int hat = params.Get("hat", 0); | ||
| 620 | const std::string direction_name = params.Get("direction", ""); | ||
| 621 | Uint8 direction; | ||
| 622 | if (direction_name == "up") { | ||
| 623 | direction = SDL_HAT_UP; | ||
| 624 | } else if (direction_name == "down") { | ||
| 625 | direction = SDL_HAT_DOWN; | ||
| 626 | } else if (direction_name == "left") { | ||
| 627 | direction = SDL_HAT_LEFT; | ||
| 628 | } else if (direction_name == "right") { | ||
| 629 | direction = SDL_HAT_RIGHT; | ||
| 630 | } else { | ||
| 631 | direction = 0; | ||
| 632 | } | ||
| 633 | // This is necessary so accessing GetHat with hat won't crash | ||
| 634 | joystick->SetHat(hat, SDL_HAT_CENTERED); | ||
| 635 | return std::make_unique<SDLDirectionMotion>(joystick, hat, direction); | ||
| 636 | } | ||
| 637 | |||
| 638 | if (params.Has("axis")) { | ||
| 639 | const int axis = params.Get("axis", 0); | ||
| 640 | const float threshold = params.Get("threshold", 0.5f); | ||
| 641 | const std::string direction_name = params.Get("direction", ""); | ||
| 642 | bool trigger_if_greater; | ||
| 643 | if (direction_name == "+") { | ||
| 644 | trigger_if_greater = true; | ||
| 645 | } else if (direction_name == "-") { | ||
| 646 | trigger_if_greater = false; | ||
| 647 | } else { | ||
| 648 | trigger_if_greater = true; | ||
| 649 | LOG_ERROR(Input, "Unknown direction {}", direction_name); | ||
| 650 | } | ||
| 651 | // This is necessary so accessing GetAxis with axis won't crash | ||
| 652 | joystick->SetAxis(axis, 0); | ||
| 653 | return std::make_unique<SDLAxisMotion>(joystick, axis, threshold, trigger_if_greater); | ||
| 654 | } | ||
| 655 | |||
| 656 | const int button = params.Get("button", 0); | ||
| 657 | // This is necessary so accessing GetButton with button won't crash | ||
| 658 | joystick->SetButton(button, false); | ||
| 659 | return std::make_unique<SDLButtonMotion>(joystick, button); | ||
| 660 | } | ||
| 661 | |||
| 662 | private: | ||
| 663 | SDLState& state; | ||
| 664 | }; | ||
| 665 | |||
| 532 | SDLState::SDLState() { | 666 | SDLState::SDLState() { |
| 533 | using namespace Input; | 667 | using namespace Input; |
| 534 | analog_factory = std::make_shared<SDLAnalogFactory>(*this); | 668 | analog_factory = std::make_shared<SDLAnalogFactory>(*this); |
| 535 | button_factory = std::make_shared<SDLButtonFactory>(*this); | 669 | button_factory = std::make_shared<SDLButtonFactory>(*this); |
| 670 | motion_factory = std::make_shared<SDLMotionFactory>(*this); | ||
| 536 | RegisterFactory<AnalogDevice>("sdl", analog_factory); | 671 | RegisterFactory<AnalogDevice>("sdl", analog_factory); |
| 537 | RegisterFactory<ButtonDevice>("sdl", button_factory); | 672 | RegisterFactory<ButtonDevice>("sdl", button_factory); |
| 673 | RegisterFactory<MotionDevice>("sdl", motion_factory); | ||
| 538 | 674 | ||
| 539 | // If the frontend is going to manage the event loop, then we dont start one here | 675 | // If the frontend is going to manage the event loop, then we dont start one here |
| 540 | start_thread = !SDL_WasInit(SDL_INIT_JOYSTICK); | 676 | start_thread = !SDL_WasInit(SDL_INIT_JOYSTICK); |
| @@ -570,6 +706,7 @@ SDLState::~SDLState() { | |||
| 570 | using namespace Input; | 706 | using namespace Input; |
| 571 | UnregisterFactory<ButtonDevice>("sdl"); | 707 | UnregisterFactory<ButtonDevice>("sdl"); |
| 572 | UnregisterFactory<AnalogDevice>("sdl"); | 708 | UnregisterFactory<AnalogDevice>("sdl"); |
| 709 | UnregisterFactory<MotionDevice>("sdl"); | ||
| 573 | 710 | ||
| 574 | CloseJoysticks(); | 711 | CloseJoysticks(); |
| 575 | SDL_DelEventWatch(&SDLEventWatcher, this); | 712 | SDL_DelEventWatch(&SDLEventWatcher, this); |
| @@ -681,6 +818,27 @@ Common::ParamPackage SDLEventToButtonParamPackage(SDLState& state, const SDL_Eve | |||
| 681 | return {}; | 818 | return {}; |
| 682 | } | 819 | } |
| 683 | 820 | ||
| 821 | Common::ParamPackage SDLEventToMotionParamPackage(SDLState& state, const SDL_Event& event) { | ||
| 822 | switch (event.type) { | ||
| 823 | case SDL_JOYAXISMOTION: { | ||
| 824 | const auto joystick = state.GetSDLJoystickBySDLID(event.jaxis.which); | ||
| 825 | return BuildAnalogParamPackageForButton(joystick->GetPort(), joystick->GetGUID(), | ||
| 826 | event.jaxis.axis, event.jaxis.value); | ||
| 827 | } | ||
| 828 | case SDL_JOYBUTTONUP: { | ||
| 829 | const auto joystick = state.GetSDLJoystickBySDLID(event.jbutton.which); | ||
| 830 | return BuildButtonParamPackageForButton(joystick->GetPort(), joystick->GetGUID(), | ||
| 831 | event.jbutton.button); | ||
| 832 | } | ||
| 833 | case SDL_JOYHATMOTION: { | ||
| 834 | const auto joystick = state.GetSDLJoystickBySDLID(event.jhat.which); | ||
| 835 | return BuildHatParamPackageForButton(joystick->GetPort(), joystick->GetGUID(), | ||
| 836 | event.jhat.hat, event.jhat.value); | ||
| 837 | } | ||
| 838 | } | ||
| 839 | return {}; | ||
| 840 | } | ||
| 841 | |||
| 684 | Common::ParamPackage BuildParamPackageForBinding(int port, const std::string& guid, | 842 | Common::ParamPackage BuildParamPackageForBinding(int port, const std::string& guid, |
| 685 | const SDL_GameControllerButtonBind& binding) { | 843 | const SDL_GameControllerButtonBind& binding) { |
| 686 | switch (binding.bindType) { | 844 | switch (binding.bindType) { |
| @@ -846,6 +1004,35 @@ public: | |||
| 846 | } | 1004 | } |
| 847 | }; | 1005 | }; |
| 848 | 1006 | ||
| 1007 | class SDLMotionPoller final : public SDLPoller { | ||
| 1008 | public: | ||
| 1009 | explicit SDLMotionPoller(SDLState& state_) : SDLPoller(state_) {} | ||
| 1010 | |||
| 1011 | Common::ParamPackage GetNextInput() override { | ||
| 1012 | SDL_Event event; | ||
| 1013 | while (state.event_queue.Pop(event)) { | ||
| 1014 | const auto package = FromEvent(event); | ||
| 1015 | if (package) { | ||
| 1016 | return *package; | ||
| 1017 | } | ||
| 1018 | } | ||
| 1019 | return {}; | ||
| 1020 | } | ||
| 1021 | [[nodiscard]] std::optional<Common::ParamPackage> FromEvent(const SDL_Event& event) const { | ||
| 1022 | switch (event.type) { | ||
| 1023 | case SDL_JOYAXISMOTION: | ||
| 1024 | if (std::abs(event.jaxis.value / 32767.0) < 0.5) { | ||
| 1025 | break; | ||
| 1026 | } | ||
| 1027 | [[fallthrough]]; | ||
| 1028 | case SDL_JOYBUTTONUP: | ||
| 1029 | case SDL_JOYHATMOTION: | ||
| 1030 | return {SDLEventToMotionParamPackage(state, event)}; | ||
| 1031 | } | ||
| 1032 | return std::nullopt; | ||
| 1033 | } | ||
| 1034 | }; | ||
| 1035 | |||
| 849 | /** | 1036 | /** |
| 850 | * Attempts to match the press to a controller joy axis (left/right stick) and if a match | 1037 | * Attempts to match the press to a controller joy axis (left/right stick) and if a match |
| 851 | * isn't found, checks if the event matches anything from SDLButtonPoller and uses that | 1038 | * isn't found, checks if the event matches anything from SDLButtonPoller and uses that |
| @@ -937,6 +1124,9 @@ SDLState::Pollers SDLState::GetPollers(InputCommon::Polling::DeviceType type) { | |||
| 937 | case InputCommon::Polling::DeviceType::Button: | 1124 | case InputCommon::Polling::DeviceType::Button: |
| 938 | pollers.emplace_back(std::make_unique<Polling::SDLButtonPoller>(*this)); | 1125 | pollers.emplace_back(std::make_unique<Polling::SDLButtonPoller>(*this)); |
| 939 | break; | 1126 | break; |
| 1127 | case InputCommon::Polling::DeviceType::Motion: | ||
| 1128 | pollers.emplace_back(std::make_unique<Polling::SDLMotionPoller>(*this)); | ||
| 1129 | break; | ||
| 940 | } | 1130 | } |
| 941 | 1131 | ||
| 942 | return pollers; | 1132 | 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 | ||
| 22 | class SDLAnalogFactory; | 22 | class SDLAnalogFactory; |
| 23 | class SDLButtonFactory; | 23 | class SDLButtonFactory; |
| 24 | class SDLMotionFactory; | ||
| 24 | class SDLJoystick; | 25 | class SDLJoystick; |
| 25 | 26 | ||
| 26 | class SDLState : public State { | 27 | class 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 cf72f6fef..9d0b9f31d 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 | } |