emu_window.h 8.5 KB

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