input_interpreter.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // Copyright 2020 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include "core/core.h"
  5. #include "core/hid/hid_types.h"
  6. #include "core/hid/input_interpreter.h"
  7. #include "core/hle/service/hid/controllers/npad.h"
  8. #include "core/hle/service/hid/hid.h"
  9. #include "core/hle/service/sm/sm.h"
  10. InputInterpreter::InputInterpreter(Core::System& system)
  11. : npad{system.ServiceManager()
  12. .GetService<Service::HID::Hid>("hid")
  13. ->GetAppletResource()
  14. ->GetController<Service::HID::Controller_NPad>(Service::HID::HidController::NPad)} {
  15. ResetButtonStates();
  16. }
  17. InputInterpreter::~InputInterpreter() = default;
  18. void InputInterpreter::PollInput() {
  19. const u32 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] = 0xFFFFFFFF;
  28. for (std::size_t i = 1; i < button_states.size(); ++i) {
  29. button_states[i] = 0;
  30. }
  31. }
  32. bool InputInterpreter::IsButtonPressed(Core::HID::NpadButton button) const {
  33. return (button_states[current_index] & static_cast<u32>(button)) != 0;
  34. }
  35. bool InputInterpreter::IsButtonPressedOnce(Core::HID::NpadButton button) const {
  36. const bool current_press = (button_states[current_index] & static_cast<u32>(button)) != 0;
  37. const bool previous_press = (button_states[previous_index] & static_cast<u32>(button)) != 0;
  38. return current_press && !previous_press;
  39. }
  40. bool InputInterpreter::IsButtonHeld(Core::HID::NpadButton button) const {
  41. u32 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 (held_buttons & static_cast<u32>(button)) != 0;
  46. }