diff options
Diffstat (limited to 'src/core/hid/input_interpreter.h')
| -rw-r--r-- | src/core/hid/input_interpreter.h | 112 |
1 files changed, 112 insertions, 0 deletions
diff --git a/src/core/hid/input_interpreter.h b/src/core/hid/input_interpreter.h new file mode 100644 index 000000000..70c34d474 --- /dev/null +++ b/src/core/hid/input_interpreter.h | |||
| @@ -0,0 +1,112 @@ | |||
| 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 <array> | ||
| 8 | |||
| 9 | #include "common/common_types.h" | ||
| 10 | |||
| 11 | namespace Core { | ||
| 12 | class System; | ||
| 13 | } | ||
| 14 | |||
| 15 | namespace Core::HID { | ||
| 16 | enum class NpadButton : u64; | ||
| 17 | } | ||
| 18 | |||
| 19 | namespace Service::HID { | ||
| 20 | class Controller_NPad; | ||
| 21 | } | ||
| 22 | |||
| 23 | /** | ||
| 24 | * The InputInterpreter class interfaces with HID to retrieve button press states. | ||
| 25 | * Input is intended to be polled every 50ms so that a button is considered to be | ||
| 26 | * held down after 400ms has elapsed since the initial button press and subsequent | ||
| 27 | * repeated presses occur every 50ms. | ||
| 28 | */ | ||
| 29 | class InputInterpreter { | ||
| 30 | public: | ||
| 31 | explicit InputInterpreter(Core::System& system); | ||
| 32 | virtual ~InputInterpreter(); | ||
| 33 | |||
| 34 | /// Gets a button state from HID and inserts it into the array of button states. | ||
| 35 | void PollInput(); | ||
| 36 | |||
| 37 | /// Resets all the button states to their defaults. | ||
| 38 | void ResetButtonStates(); | ||
| 39 | |||
| 40 | /** | ||
| 41 | * Checks whether the button is pressed. | ||
| 42 | * | ||
| 43 | * @param button The button to check. | ||
| 44 | * | ||
| 45 | * @returns True when the button is pressed. | ||
| 46 | */ | ||
| 47 | [[nodiscard]] bool IsButtonPressed(Core::HID::NpadButton button) const; | ||
| 48 | |||
| 49 | /** | ||
| 50 | * Checks whether any of the buttons in the parameter list is pressed. | ||
| 51 | * | ||
| 52 | * @tparam HIDButton The buttons to check. | ||
| 53 | * | ||
| 54 | * @returns True when at least one of the buttons is pressed. | ||
| 55 | */ | ||
| 56 | template <Core::HID::NpadButton... T> | ||
| 57 | [[nodiscard]] bool IsAnyButtonPressed() { | ||
| 58 | return (IsButtonPressed(T) || ...); | ||
| 59 | } | ||
| 60 | |||
| 61 | /** | ||
| 62 | * The specified button is considered to be pressed once | ||
| 63 | * if it is currently pressed and not pressed previously. | ||
| 64 | * | ||
| 65 | * @param button The button to check. | ||
| 66 | * | ||
| 67 | * @returns True when the button is pressed once. | ||
| 68 | */ | ||
| 69 | [[nodiscard]] bool IsButtonPressedOnce(Core::HID::NpadButton button) const; | ||
| 70 | |||
| 71 | /** | ||
| 72 | * Checks whether any of the buttons in the parameter list is pressed once. | ||
| 73 | * | ||
| 74 | * @tparam T The buttons to check. | ||
| 75 | * | ||
| 76 | * @returns True when at least one of the buttons is pressed once. | ||
| 77 | */ | ||
| 78 | template <Core::HID::NpadButton... T> | ||
| 79 | [[nodiscard]] bool IsAnyButtonPressedOnce() const { | ||
| 80 | return (IsButtonPressedOnce(T) || ...); | ||
| 81 | } | ||
| 82 | |||
| 83 | /** | ||
| 84 | * The specified button is considered to be held down if it is pressed in all 9 button states. | ||
| 85 | * | ||
| 86 | * @param button The button to check. | ||
| 87 | * | ||
| 88 | * @returns True when the button is held down. | ||
| 89 | */ | ||
| 90 | [[nodiscard]] bool IsButtonHeld(Core::HID::NpadButton button) const; | ||
| 91 | |||
| 92 | /** | ||
| 93 | * Checks whether any of the buttons in the parameter list is held down. | ||
| 94 | * | ||
| 95 | * @tparam T The buttons to check. | ||
| 96 | * | ||
| 97 | * @returns True when at least one of the buttons is held down. | ||
| 98 | */ | ||
| 99 | template <Core::HID::NpadButton... T> | ||
| 100 | [[nodiscard]] bool IsAnyButtonHeld() const { | ||
| 101 | return (IsButtonHeld(T) || ...); | ||
| 102 | } | ||
| 103 | |||
| 104 | private: | ||
| 105 | Service::HID::Controller_NPad& npad; | ||
| 106 | |||
| 107 | /// Stores 9 consecutive button states polled from HID. | ||
| 108 | std::array<Core::HID::NpadButton, 9> button_states{}; | ||
| 109 | |||
| 110 | std::size_t previous_index{}; | ||
| 111 | std::size_t current_index{}; | ||
| 112 | }; | ||