touchscreen.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // Copyright 2018 yuzu emulator team
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <algorithm>
  5. #include <cstring>
  6. #include "common/common_types.h"
  7. #include "common/logging/log.h"
  8. #include "common/settings.h"
  9. #include "core/core_timing.h"
  10. #include "core/frontend/emu_window.h"
  11. #include "core/frontend/input.h"
  12. #include "core/hle/service/hid/controllers/touchscreen.h"
  13. namespace Service::HID {
  14. constexpr std::size_t SHARED_MEMORY_OFFSET = 0x400;
  15. Controller_Touchscreen::Controller_Touchscreen(Core::System& system_) : ControllerBase{system_} {}
  16. Controller_Touchscreen::~Controller_Touchscreen() = default;
  17. void Controller_Touchscreen::OnInit() {
  18. for (std::size_t id = 0; id < MAX_FINGERS; ++id) {
  19. mouse_finger_id[id] = MAX_FINGERS;
  20. keyboard_finger_id[id] = MAX_FINGERS;
  21. udp_finger_id[id] = MAX_FINGERS;
  22. }
  23. }
  24. void Controller_Touchscreen::OnRelease() {}
  25. void Controller_Touchscreen::OnUpdate(const Core::Timing::CoreTiming& core_timing, u8* data,
  26. std::size_t size) {
  27. shared_memory.header.timestamp = core_timing.GetCPUTicks();
  28. shared_memory.header.total_entry_count = 17;
  29. if (!IsControllerActivated()) {
  30. shared_memory.header.entry_count = 0;
  31. shared_memory.header.last_entry_index = 0;
  32. return;
  33. }
  34. shared_memory.header.entry_count = 16;
  35. const auto& last_entry =
  36. shared_memory.shared_memory_entries[shared_memory.header.last_entry_index];
  37. shared_memory.header.last_entry_index = (shared_memory.header.last_entry_index + 1) % 17;
  38. auto& cur_entry = shared_memory.shared_memory_entries[shared_memory.header.last_entry_index];
  39. cur_entry.sampling_number = last_entry.sampling_number + 1;
  40. cur_entry.sampling_number2 = cur_entry.sampling_number;
  41. const Input::TouchStatus& mouse_status = touch_mouse_device->GetStatus();
  42. const Input::TouchStatus& udp_status = touch_udp_device->GetStatus();
  43. for (std::size_t id = 0; id < mouse_status.size(); ++id) {
  44. mouse_finger_id[id] = UpdateTouchInputEvent(mouse_status[id], mouse_finger_id[id]);
  45. udp_finger_id[id] = UpdateTouchInputEvent(udp_status[id], udp_finger_id[id]);
  46. }
  47. if (Settings::values.use_touch_from_button) {
  48. const Input::TouchStatus& keyboard_status = touch_btn_device->GetStatus();
  49. for (std::size_t id = 0; id < mouse_status.size(); ++id) {
  50. keyboard_finger_id[id] =
  51. UpdateTouchInputEvent(keyboard_status[id], keyboard_finger_id[id]);
  52. }
  53. }
  54. std::array<Finger, 16> active_fingers;
  55. const auto end_iter = std::copy_if(fingers.begin(), fingers.end(), active_fingers.begin(),
  56. [](const auto& finger) { return finger.pressed; });
  57. const auto active_fingers_count =
  58. static_cast<std::size_t>(std::distance(active_fingers.begin(), end_iter));
  59. const u64 tick = core_timing.GetCPUTicks();
  60. cur_entry.entry_count = static_cast<s32_le>(active_fingers_count);
  61. for (std::size_t id = 0; id < MAX_FINGERS; ++id) {
  62. auto& touch_entry = cur_entry.states[id];
  63. if (id < active_fingers_count) {
  64. const auto& [active_x, active_y] = active_fingers[id].position;
  65. touch_entry.position = {
  66. .x = static_cast<u16>(active_x * Layout::ScreenUndocked::Width),
  67. .y = static_cast<u16>(active_y * Layout::ScreenUndocked::Height),
  68. };
  69. touch_entry.diameter_x = Settings::values.touchscreen.diameter_x;
  70. touch_entry.diameter_y = Settings::values.touchscreen.diameter_y;
  71. touch_entry.rotation_angle = Settings::values.touchscreen.rotation_angle;
  72. touch_entry.delta_time = tick - active_fingers[id].last_touch;
  73. fingers[active_fingers[id].id].last_touch = tick;
  74. touch_entry.finger = active_fingers[id].id;
  75. touch_entry.attribute.raw = active_fingers[id].attribute.raw;
  76. } else {
  77. // Clear touch entry
  78. touch_entry.attribute.raw = 0;
  79. touch_entry.position = {};
  80. touch_entry.diameter_x = 0;
  81. touch_entry.diameter_y = 0;
  82. touch_entry.rotation_angle = 0;
  83. touch_entry.delta_time = 0;
  84. touch_entry.finger = 0;
  85. }
  86. }
  87. std::memcpy(data + SHARED_MEMORY_OFFSET, &shared_memory, sizeof(TouchScreenSharedMemory));
  88. }
  89. void Controller_Touchscreen::OnLoadInputDevices() {
  90. touch_mouse_device = Input::CreateDevice<Input::TouchDevice>("engine:emu_window");
  91. touch_udp_device = Input::CreateDevice<Input::TouchDevice>("engine:cemuhookudp");
  92. touch_btn_device = Input::CreateDevice<Input::TouchDevice>("engine:touch_from_button");
  93. }
  94. std::optional<std::size_t> Controller_Touchscreen::GetUnusedFingerID() const {
  95. // Dont assign any touch input to a finger if disabled
  96. if (!Settings::values.touchscreen.enabled) {
  97. return std::nullopt;
  98. }
  99. std::size_t first_free_id = 0;
  100. while (first_free_id < MAX_FINGERS) {
  101. if (!fingers[first_free_id].pressed) {
  102. return first_free_id;
  103. } else {
  104. first_free_id++;
  105. }
  106. }
  107. return std::nullopt;
  108. }
  109. std::size_t Controller_Touchscreen::UpdateTouchInputEvent(
  110. const std::tuple<float, float, bool>& touch_input, std::size_t finger_id) {
  111. const auto& [x, y, pressed] = touch_input;
  112. if (finger_id > MAX_FINGERS) {
  113. LOG_ERROR(Service_HID, "Invalid finger id {}", finger_id);
  114. return MAX_FINGERS;
  115. }
  116. if (pressed) {
  117. Attributes attribute{};
  118. if (finger_id == MAX_FINGERS) {
  119. const auto first_free_id = GetUnusedFingerID();
  120. if (!first_free_id) {
  121. // Invalid finger id do nothing
  122. return MAX_FINGERS;
  123. }
  124. finger_id = first_free_id.value();
  125. fingers[finger_id].pressed = true;
  126. fingers[finger_id].id = static_cast<u32_le>(finger_id);
  127. attribute.start_touch.Assign(1);
  128. }
  129. fingers[finger_id].position = {x, y};
  130. fingers[finger_id].attribute = attribute;
  131. return finger_id;
  132. }
  133. if (finger_id != MAX_FINGERS) {
  134. if (!fingers[finger_id].attribute.end_touch) {
  135. fingers[finger_id].attribute.end_touch.Assign(1);
  136. fingers[finger_id].attribute.start_touch.Assign(0);
  137. return finger_id;
  138. }
  139. fingers[finger_id].pressed = false;
  140. }
  141. return MAX_FINGERS;
  142. }
  143. } // namespace Service::HID