emu_window.h 8.4 KB

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