touchscreen.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // Copyright 2018 yuzu emulator team
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <cstring>
  5. #include "common/common_types.h"
  6. #include "core/core_timing.h"
  7. #include "core/frontend/emu_window.h"
  8. #include "core/frontend/input.h"
  9. #include "core/hle/service/hid/controllers/touchscreen.h"
  10. #include "core/settings.h"
  11. namespace Service::HID {
  12. constexpr std::size_t SHARED_MEMORY_OFFSET = 0x400;
  13. Controller_Touchscreen::Controller_Touchscreen() = default;
  14. Controller_Touchscreen::~Controller_Touchscreen() = default;
  15. void Controller_Touchscreen::OnInit() {}
  16. void Controller_Touchscreen::OnRelease() {}
  17. void Controller_Touchscreen::OnUpdate(u8* data, std::size_t size) {
  18. shared_memory.header.timestamp = CoreTiming::GetTicks();
  19. shared_memory.header.total_entry_count = 17;
  20. if (!IsControllerActivated()) {
  21. shared_memory.header.entry_count = 0;
  22. shared_memory.header.last_entry_index = 0;
  23. return;
  24. }
  25. shared_memory.header.entry_count = 16;
  26. const auto& last_entry =
  27. shared_memory.shared_memory_entries[shared_memory.header.last_entry_index];
  28. shared_memory.header.last_entry_index = (shared_memory.header.last_entry_index + 1) % 17;
  29. auto& cur_entry = shared_memory.shared_memory_entries[shared_memory.header.last_entry_index];
  30. cur_entry.sampling_number = last_entry.sampling_number + 1;
  31. cur_entry.sampling_number2 = cur_entry.sampling_number;
  32. const auto [x, y, pressed] = touch_device->GetStatus();
  33. auto& touch_entry = cur_entry.states[0];
  34. touch_entry.attribute.raw = 0;
  35. if (pressed && Settings::values.touchscreen.enabled) {
  36. touch_entry.x = static_cast<u16>(x * Layout::ScreenUndocked::Width);
  37. touch_entry.y = static_cast<u16>(y * Layout::ScreenUndocked::Height);
  38. touch_entry.diameter_x = Settings::values.touchscreen.diameter_x;
  39. touch_entry.diameter_y = Settings::values.touchscreen.diameter_y;
  40. touch_entry.rotation_angle = Settings::values.touchscreen.rotation_angle;
  41. const u64 tick = CoreTiming::GetTicks();
  42. touch_entry.delta_time = tick - last_touch;
  43. last_touch = tick;
  44. touch_entry.finger = Settings::values.touchscreen.finger;
  45. cur_entry.entry_count = 1;
  46. } else {
  47. cur_entry.entry_count = 0;
  48. }
  49. std::memcpy(data + SHARED_MEMORY_OFFSET, &shared_memory, sizeof(TouchScreenSharedMemory));
  50. }
  51. void Controller_Touchscreen::OnLoadInputDevices() {
  52. touch_device = Input::CreateDevice<Input::TouchDevice>(Settings::values.touchscreen.device);
  53. }
  54. } // namespace Service::HID