emu_window.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <mutex>
  6. #include <tuple>
  7. #include <utility>
  8. #include "common/common_types.h"
  9. #include "common/math_util.h"
  10. #include "core/frontend/framebuffer_layout.h"
  11. /**
  12. * Abstraction class used to provide an interface between emulation code and the frontend
  13. * (e.g. SDL, QGLWidget, GLFW, etc...).
  14. *
  15. * Design notes on the interaction between EmuWindow and the emulation core:
  16. * - Generally, decisions on anything visible to the user should be left up to the GUI.
  17. * For example, the emulation core should not try to dictate some window title or size.
  18. * This stuff is not the core's business and only causes problems with regards to thread-safety
  19. * anyway.
  20. * - Under certain circumstances, it may be desirable for the core to politely request the GUI
  21. * to set e.g. a minimum window size. However, the GUI should always be free to ignore any
  22. * such hints.
  23. * - EmuWindow may expose some of its state as read-only to the emulation core, however care
  24. * should be taken to make sure the provided information is self-consistent. This requires
  25. * some sort of synchronization (most of this is still a TODO).
  26. * - DO NOT TREAT THIS CLASS AS A GUI TOOLKIT ABSTRACTION LAYER. That's not what it is. Please
  27. * re-read the upper points again and think about it if you don't see this.
  28. */
  29. class EmuWindow {
  30. public:
  31. /// Data structure to store emuwindow configuration
  32. struct WindowConfig {
  33. bool fullscreen;
  34. int res_width;
  35. int res_height;
  36. std::pair<unsigned, unsigned> min_client_area_size;
  37. };
  38. /// Swap buffers to display the next frame
  39. virtual void SwapBuffers() = 0;
  40. /// Polls window events
  41. virtual void PollEvents() = 0;
  42. /// Makes the graphics context current for the caller thread
  43. virtual void MakeCurrent() = 0;
  44. /// Releases (dunno if this is the "right" word) the GLFW context from the caller thread
  45. virtual void DoneCurrent() = 0;
  46. /**
  47. * Signal that a touch pressed event has occurred (e.g. mouse click pressed)
  48. * @param framebuffer_x Framebuffer x-coordinate that was pressed
  49. * @param framebuffer_y Framebuffer y-coordinate that was pressed
  50. */
  51. void TouchPressed(unsigned framebuffer_x, unsigned framebuffer_y);
  52. /// Signal that a touch released event has occurred (e.g. mouse click released)
  53. void TouchReleased();
  54. /**
  55. * Signal that a touch movement event has occurred (e.g. mouse was moved over the emu window)
  56. * @param framebuffer_x Framebuffer x-coordinate
  57. * @param framebuffer_y Framebuffer y-coordinate
  58. */
  59. void TouchMoved(unsigned framebuffer_x, unsigned framebuffer_y);
  60. /**
  61. * Gets the current touch screen state (touch X/Y coordinates and whether or not it is pressed).
  62. * @note This should be called by the core emu thread to get a state set by the window thread.
  63. * @todo Fix this function to be thread-safe.
  64. * @return std::tuple of (x, y, pressed) where `x` and `y` are the touch coordinates and
  65. * `pressed` is true if the touch screen is currently being pressed
  66. */
  67. std::tuple<u16, u16, bool> GetTouchState() const {
  68. return std::make_tuple(touch_x, touch_y, touch_pressed);
  69. }
  70. /**
  71. * Returns currently active configuration.
  72. * @note Accesses to the returned object need not be consistent because it may be modified in
  73. * another thread
  74. */
  75. const WindowConfig& GetActiveConfig() const {
  76. return active_config;
  77. }
  78. /**
  79. * Requests the internal configuration to be replaced by the specified argument at some point in
  80. * the future.
  81. * @note This method is thread-safe, because it delays configuration changes to the GUI event
  82. * loop. Hence there is no guarantee on when the requested configuration will be active.
  83. */
  84. void SetConfig(const WindowConfig& val) {
  85. config = val;
  86. }
  87. /**
  88. * Gets the framebuffer layout (width, height, and screen regions)
  89. * @note This method is thread-safe
  90. */
  91. const Layout::FramebufferLayout& GetFramebufferLayout() const {
  92. return framebuffer_layout;
  93. }
  94. /**
  95. * Convenience method to update the current frame layout
  96. * Read from the current settings to determine which layout to use.
  97. */
  98. void UpdateCurrentFramebufferLayout(unsigned width, unsigned height);
  99. protected:
  100. EmuWindow() {
  101. // TODO: Find a better place to set this.
  102. config.min_client_area_size = std::make_pair(400u, 480u);
  103. active_config = config;
  104. touch_x = 0;
  105. touch_y = 0;
  106. touch_pressed = false;
  107. }
  108. virtual ~EmuWindow() {}
  109. /**
  110. * Processes any pending configuration changes from the last SetConfig call.
  111. * This method invokes OnMinimalClientAreaChangeRequest if the corresponding configuration
  112. * field changed.
  113. * @note Implementations will usually want to call this from the GUI thread.
  114. * @todo Actually call this in existing implementations.
  115. */
  116. void ProcessConfigurationChanges() {
  117. // TODO: For proper thread safety, we should eventually implement a proper
  118. // multiple-writer/single-reader queue...
  119. if (config.min_client_area_size != active_config.min_client_area_size) {
  120. OnMinimalClientAreaChangeRequest(config.min_client_area_size);
  121. config.min_client_area_size = active_config.min_client_area_size;
  122. }
  123. }
  124. /**
  125. * Update framebuffer layout with the given parameter.
  126. * @note EmuWindow implementations will usually use this in window resize event handlers.
  127. */
  128. void NotifyFramebufferLayoutChanged(const Layout::FramebufferLayout& layout) {
  129. framebuffer_layout = layout;
  130. }
  131. /**
  132. * Update internal client area size with the given parameter.
  133. * @note EmuWindow implementations will usually use this in window resize event handlers.
  134. */
  135. void NotifyClientAreaSizeChanged(const std::pair<unsigned, unsigned>& size) {
  136. client_area_width = size.first;
  137. client_area_height = size.second;
  138. }
  139. private:
  140. /**
  141. * Handler called when the minimal client area was requested to be changed via SetConfig.
  142. * For the request to be honored, EmuWindow implementations will usually reimplement this
  143. * function.
  144. */
  145. virtual void OnMinimalClientAreaChangeRequest(
  146. const std::pair<unsigned, unsigned>& minimal_size) {
  147. // By default, ignore this request and do nothing.
  148. }
  149. Layout::FramebufferLayout framebuffer_layout; ///< Current framebuffer layout
  150. unsigned client_area_width; ///< Current client width, should be set by window impl.
  151. unsigned client_area_height; ///< Current client height, should be set by window impl.
  152. WindowConfig config; ///< Internal configuration (changes pending for being applied in
  153. /// ProcessConfigurationChanges)
  154. WindowConfig active_config; ///< Internal active configuration
  155. bool touch_pressed; ///< True if touchpad area is currently pressed, otherwise false
  156. u16 touch_x; ///< Touchpad X-position in native 3DS pixel coordinates (0-320)
  157. u16 touch_y; ///< Touchpad Y-position in native 3DS pixel coordinates (0-240)
  158. /**
  159. * Clip the provided coordinates to be inside the touchscreen area.
  160. */
  161. std::tuple<unsigned, unsigned> ClipToTouchScreen(unsigned new_x, unsigned new_y);
  162. };