emu_window.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <mutex>
  5. #include "common/settings.h"
  6. #include "core/frontend/emu_window.h"
  7. #include "core/frontend/input.h"
  8. namespace Core::Frontend {
  9. GraphicsContext::~GraphicsContext() = default;
  10. class EmuWindow::TouchState : public Input::Factory<Input::TouchDevice>,
  11. public std::enable_shared_from_this<TouchState> {
  12. public:
  13. std::unique_ptr<Input::TouchDevice> Create(const Common::ParamPackage&) override {
  14. return std::make_unique<Device>(shared_from_this());
  15. }
  16. std::mutex mutex;
  17. Input::TouchStatus status;
  18. private:
  19. class Device : public Input::TouchDevice {
  20. public:
  21. explicit Device(std::weak_ptr<TouchState>&& touch_state_) : touch_state(touch_state_) {}
  22. Input::TouchStatus GetStatus() const override {
  23. if (auto state = touch_state.lock()) {
  24. std::lock_guard guard{state->mutex};
  25. return state->status;
  26. }
  27. return {};
  28. }
  29. private:
  30. std::weak_ptr<TouchState> touch_state;
  31. };
  32. };
  33. EmuWindow::EmuWindow() {
  34. // TODO: Find a better place to set this.
  35. config.min_client_area_size =
  36. std::make_pair(Layout::MinimumSize::Width, Layout::MinimumSize::Height);
  37. active_config = config;
  38. touch_state = std::make_shared<TouchState>();
  39. Input::RegisterFactory<Input::TouchDevice>("emu_window", touch_state);
  40. }
  41. EmuWindow::~EmuWindow() {
  42. Input::UnregisterFactory<Input::TouchDevice>("emu_window");
  43. }
  44. /**
  45. * Check if the given x/y coordinates are within the touchpad specified by the framebuffer layout
  46. * @param layout FramebufferLayout object describing the framebuffer size and screen positions
  47. * @param framebuffer_x Framebuffer x-coordinate to check
  48. * @param framebuffer_y Framebuffer y-coordinate to check
  49. * @return True if the coordinates are within the touchpad, otherwise false
  50. */
  51. static bool IsWithinTouchscreen(const Layout::FramebufferLayout& layout, u32 framebuffer_x,
  52. u32 framebuffer_y) {
  53. return (framebuffer_y >= layout.screen.top && framebuffer_y < layout.screen.bottom &&
  54. framebuffer_x >= layout.screen.left && framebuffer_x < layout.screen.right);
  55. }
  56. std::pair<u32, u32> EmuWindow::ClipToTouchScreen(u32 new_x, u32 new_y) const {
  57. new_x = std::max(new_x, framebuffer_layout.screen.left);
  58. new_x = std::min(new_x, framebuffer_layout.screen.right - 1);
  59. new_y = std::max(new_y, framebuffer_layout.screen.top);
  60. new_y = std::min(new_y, framebuffer_layout.screen.bottom - 1);
  61. return std::make_pair(new_x, new_y);
  62. }
  63. void EmuWindow::TouchPressed(u32 framebuffer_x, u32 framebuffer_y, size_t id) {
  64. if (!IsWithinTouchscreen(framebuffer_layout, framebuffer_x, framebuffer_y)) {
  65. return;
  66. }
  67. if (id >= touch_state->status.size()) {
  68. return;
  69. }
  70. std::lock_guard guard{touch_state->mutex};
  71. const float x =
  72. static_cast<float>(framebuffer_x - framebuffer_layout.screen.left) /
  73. static_cast<float>(framebuffer_layout.screen.right - framebuffer_layout.screen.left);
  74. const float y =
  75. static_cast<float>(framebuffer_y - framebuffer_layout.screen.top) /
  76. static_cast<float>(framebuffer_layout.screen.bottom - framebuffer_layout.screen.top);
  77. touch_state->status[id] = std::make_tuple(x, y, true);
  78. }
  79. void EmuWindow::TouchReleased(size_t id) {
  80. if (id >= touch_state->status.size()) {
  81. return;
  82. }
  83. std::lock_guard guard{touch_state->mutex};
  84. touch_state->status[id] = std::make_tuple(0.0f, 0.0f, false);
  85. }
  86. void EmuWindow::TouchMoved(u32 framebuffer_x, u32 framebuffer_y, size_t id) {
  87. if (id >= touch_state->status.size()) {
  88. return;
  89. }
  90. if (!std::get<2>(touch_state->status[id])) {
  91. return;
  92. }
  93. if (!IsWithinTouchscreen(framebuffer_layout, framebuffer_x, framebuffer_y)) {
  94. std::tie(framebuffer_x, framebuffer_y) = ClipToTouchScreen(framebuffer_x, framebuffer_y);
  95. }
  96. TouchPressed(framebuffer_x, framebuffer_y, id);
  97. }
  98. void EmuWindow::UpdateCurrentFramebufferLayout(u32 width, u32 height) {
  99. NotifyFramebufferLayoutChanged(Layout::DefaultFrameLayout(width, height));
  100. }
  101. } // namespace Core::Frontend