input_interpreter.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // SPDX-FileCopyrightText: Copyright 2020 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include "core/core.h"
  4. #include "core/hid/hid_types.h"
  5. #include "core/hid/input_interpreter.h"
  6. #include "core/hle/service/hid/controllers/npad.h"
  7. #include "core/hle/service/hid/hid_server.h"
  8. #include "core/hle/service/hid/resource_manager.h"
  9. #include "core/hle/service/sm/sm.h"
  10. InputInterpreter::InputInterpreter(Core::System& system)
  11. : npad{system.ServiceManager()
  12. .GetService<Service::HID::IHidServer>("hid")
  13. ->GetResourceManager()
  14. ->GetController<Service::HID::Controller_NPad>(Service::HID::HidController::NPad)} {
  15. ResetButtonStates();
  16. }
  17. InputInterpreter::~InputInterpreter() = default;
  18. void InputInterpreter::PollInput() {
  19. const auto button_state = npad.GetAndResetPressState();
  20. previous_index = current_index;
  21. current_index = (current_index + 1) % button_states.size();
  22. button_states[current_index] = button_state;
  23. }
  24. void InputInterpreter::ResetButtonStates() {
  25. previous_index = 0;
  26. current_index = 0;
  27. button_states[0] = Core::HID::NpadButton::All;
  28. for (std::size_t i = 1; i < button_states.size(); ++i) {
  29. button_states[i] = Core::HID::NpadButton::None;
  30. }
  31. }
  32. bool InputInterpreter::IsButtonPressed(Core::HID::NpadButton button) const {
  33. return True(button_states[current_index] & button);
  34. }
  35. bool InputInterpreter::IsButtonPressedOnce(Core::HID::NpadButton button) const {
  36. const bool current_press = True(button_states[current_index] & button);
  37. const bool previous_press = True(button_states[previous_index] & button);
  38. return current_press && !previous_press;
  39. }
  40. bool InputInterpreter::IsButtonHeld(Core::HID::NpadButton button) const {
  41. Core::HID::NpadButton held_buttons{button_states[0]};
  42. for (std::size_t i = 1; i < button_states.size(); ++i) {
  43. held_buttons &= button_states[i];
  44. }
  45. return True(held_buttons & button);
  46. }