touch_screen.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include "common/param_package.h"
  4. #include "input_common/drivers/touch_screen.h"
  5. namespace InputCommon {
  6. constexpr PadIdentifier identifier = {
  7. .guid = Common::UUID{},
  8. .port = 0,
  9. .pad = 0,
  10. };
  11. TouchScreen::TouchScreen(std::string input_engine_) : InputEngine(std::move(input_engine_)) {
  12. PreSetController(identifier);
  13. ReleaseAllTouch();
  14. }
  15. void TouchScreen::TouchMoved(float x, float y, std::size_t finger_id) {
  16. const auto index = GetIndexFromFingerId(finger_id);
  17. if (!index) {
  18. // Touch doesn't exist handle it as a new one
  19. TouchPressed(x, y, finger_id);
  20. return;
  21. }
  22. const auto i = index.value();
  23. fingers[i].is_active = true;
  24. SetButton(identifier, static_cast<int>(i), true);
  25. SetAxis(identifier, static_cast<int>(i * 2), x);
  26. SetAxis(identifier, static_cast<int>(i * 2 + 1), y);
  27. }
  28. void TouchScreen::TouchPressed(float x, float y, std::size_t finger_id) {
  29. if (GetIndexFromFingerId(finger_id)) {
  30. // Touch already exist. Just update the data
  31. TouchMoved(x, y, finger_id);
  32. return;
  33. }
  34. const auto index = GetNextFreeIndex();
  35. if (!index) {
  36. // No free entries. Ignore input
  37. return;
  38. }
  39. const auto i = index.value();
  40. fingers[i].is_enabled = true;
  41. fingers[i].finger_id = finger_id;
  42. TouchMoved(x, y, finger_id);
  43. }
  44. void TouchScreen::TouchReleased(std::size_t finger_id) {
  45. const auto index = GetIndexFromFingerId(finger_id);
  46. if (!index) {
  47. return;
  48. }
  49. const auto i = index.value();
  50. fingers[i].is_enabled = false;
  51. SetButton(identifier, static_cast<int>(i), false);
  52. SetAxis(identifier, static_cast<int>(i * 2), 0.0f);
  53. SetAxis(identifier, static_cast<int>(i * 2 + 1), 0.0f);
  54. }
  55. std::optional<std::size_t> TouchScreen::GetIndexFromFingerId(std::size_t finger_id) const {
  56. for (std::size_t index = 0; index < MAX_FINGER_COUNT; ++index) {
  57. const auto& finger = fingers[index];
  58. if (!finger.is_enabled) {
  59. continue;
  60. }
  61. if (finger.finger_id == finger_id) {
  62. return index;
  63. }
  64. }
  65. return std::nullopt;
  66. }
  67. std::optional<std::size_t> TouchScreen::GetNextFreeIndex() const {
  68. for (std::size_t index = 0; index < MAX_FINGER_COUNT; ++index) {
  69. if (!fingers[index].is_enabled) {
  70. return index;
  71. }
  72. }
  73. return std::nullopt;
  74. }
  75. void TouchScreen::ClearActiveFlag() {
  76. for (auto& finger : fingers) {
  77. finger.is_active = false;
  78. }
  79. }
  80. void TouchScreen::ReleaseInactiveTouch() {
  81. for (const auto& finger : fingers) {
  82. if (!finger.is_active) {
  83. TouchReleased(finger.finger_id);
  84. }
  85. }
  86. }
  87. void TouchScreen::ReleaseAllTouch() {
  88. for (const auto& finger : fingers) {
  89. if (finger.is_enabled) {
  90. TouchReleased(finger.finger_id);
  91. }
  92. }
  93. }
  94. } // namespace InputCommon