emu_window.cpp 4.4 KB

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