emu_window.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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/framebuffer_layout.h"
  9. #include "common/math_util.h"
  10. #include "core/hle/service/hid/hid.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. virtual void ReloadSetKeymaps() = 0;
  47. /**
  48. * Signals a button press action to the HID module.
  49. * @param pad_state indicates which button to press
  50. * @note only handles real buttons (A/B/X/Y/...), excluding analog inputs like the circle pad.
  51. */
  52. void ButtonPressed(Service::HID::PadState pad_state);
  53. /**
  54. * Signals a button release action to the HID module.
  55. * @param pad_state indicates which button to press
  56. * @note only handles real buttons (A/B/X/Y/...), excluding analog inputs like the circle pad.
  57. */
  58. void ButtonReleased(Service::HID::PadState pad_state);
  59. /**
  60. * Signals a circle pad change action to the HID module.
  61. * @param x new x-coordinate of the circle pad, in the range [-1.0, 1.0]
  62. * @param y new y-coordinate of the circle pad, in the range [-1.0, 1.0]
  63. * @note the coordinates will be normalized if the radius is larger than 1
  64. */
  65. void CirclePadUpdated(float x, float y);
  66. /**
  67. * Signal that a touch pressed event has occurred (e.g. mouse click pressed)
  68. * @param framebuffer_x Framebuffer x-coordinate that was pressed
  69. * @param framebuffer_y Framebuffer y-coordinate that was pressed
  70. */
  71. void TouchPressed(unsigned framebuffer_x, unsigned framebuffer_y);
  72. /// Signal that a touch released event has occurred (e.g. mouse click released)
  73. void TouchReleased();
  74. /**
  75. * Signal that a touch movement event has occurred (e.g. mouse was moved over the emu window)
  76. * @param framebuffer_x Framebuffer x-coordinate
  77. * @param framebuffer_y Framebuffer y-coordinate
  78. */
  79. void TouchMoved(unsigned framebuffer_x, unsigned framebuffer_y);
  80. /**
  81. * Gets the current pad state (which buttons are pressed).
  82. * @note This should be called by the core emu thread to get a state set by the window thread.
  83. * @note This doesn't include analog input like circle pad direction
  84. * @todo Fix this function to be thread-safe.
  85. * @return PadState object indicating the current pad state
  86. */
  87. Service::HID::PadState GetPadState() const {
  88. return pad_state;
  89. }
  90. /**
  91. * Gets the current circle pad state.
  92. * @note This should be called by the core emu thread to get a state set by the window thread.
  93. * @todo Fix this function to be thread-safe.
  94. * @return std::tuple of (x, y), where `x` and `y` are the circle pad coordinates
  95. */
  96. std::tuple<s16, s16> GetCirclePadState() const {
  97. return std::make_tuple(circle_pad_x, circle_pad_y);
  98. }
  99. /**
  100. * Gets the current touch screen state (touch X/Y coordinates and whether or not it is pressed).
  101. * @note This should be called by the core emu thread to get a state set by the window thread.
  102. * @todo Fix this function to be thread-safe.
  103. * @return std::tuple of (x, y, pressed) where `x` and `y` are the touch coordinates and
  104. * `pressed` is true if the touch screen is currently being pressed
  105. */
  106. std::tuple<u16, u16, bool> GetTouchState() const {
  107. return std::make_tuple(touch_x, touch_y, touch_pressed);
  108. }
  109. /**
  110. * Gets the current accelerometer state (acceleration along each three axis).
  111. * Axis explained:
  112. * +x is the same direction as LEFT on D-pad.
  113. * +y is normal to the touch screen, pointing outward.
  114. * +z is the same direction as UP on D-pad.
  115. * Units:
  116. * 1 unit of return value = 1/512 g (measured by hw test),
  117. * where g is the gravitational acceleration (9.8 m/sec2).
  118. * @note This should be called by the core emu thread to get a state set by the window thread.
  119. * @todo Implement accelerometer input in front-end.
  120. * @return std::tuple of (x, y, z)
  121. */
  122. std::tuple<s16, s16, s16> GetAccelerometerState() const {
  123. // stubbed
  124. return std::make_tuple(0, -512, 0);
  125. }
  126. /**
  127. * Gets the current gyroscope state (angular rates about each three axis).
  128. * Axis explained:
  129. * +x is the same direction as LEFT on D-pad.
  130. * +y is normal to the touch screen, pointing outward.
  131. * +z is the same direction as UP on D-pad.
  132. * Orientation is determined by right-hand rule.
  133. * Units:
  134. * 1 unit of return value = (1/coef) deg/sec,
  135. * where coef is the return value of GetGyroscopeRawToDpsCoefficient().
  136. * @note This should be called by the core emu thread to get a state set by the window thread.
  137. * @todo Implement gyroscope input in front-end.
  138. * @return std::tuple of (x, y, z)
  139. */
  140. std::tuple<s16, s16, s16> GetGyroscopeState() const {
  141. // stubbed
  142. return std::make_tuple(0, 0, 0);
  143. }
  144. /**
  145. * Gets the coefficient for units conversion of gyroscope state.
  146. * The conversion formula is r = coefficient * v,
  147. * where v is angular rate in deg/sec,
  148. * and r is the gyroscope state.
  149. * @return float-type coefficient
  150. */
  151. f32 GetGyroscopeRawToDpsCoefficient() const {
  152. return 14.375f; // taken from hw test, and gyroscope's document
  153. }
  154. /**
  155. * Returns currently active configuration.
  156. * @note Accesses to the returned object need not be consistent because it may be modified in
  157. * another thread
  158. */
  159. const WindowConfig& GetActiveConfig() const {
  160. return active_config;
  161. }
  162. /**
  163. * Requests the internal configuration to be replaced by the specified argument at some point in
  164. * the future.
  165. * @note This method is thread-safe, because it delays configuration changes to the GUI event
  166. * loop. Hence there is no guarantee on when the requested configuration will be active.
  167. */
  168. void SetConfig(const WindowConfig& val) {
  169. config = val;
  170. }
  171. /**
  172. * Gets the framebuffer layout (width, height, and screen regions)
  173. * @note This method is thread-safe
  174. */
  175. const Layout::FramebufferLayout& GetFramebufferLayout() const {
  176. return framebuffer_layout;
  177. }
  178. /**
  179. * Convenience method to update the current frame layout
  180. * Read from the current settings to determine which layout to use.
  181. */
  182. void UpdateCurrentFramebufferLayout(unsigned width, unsigned height);
  183. protected:
  184. EmuWindow() {
  185. // TODO: Find a better place to set this.
  186. config.min_client_area_size = std::make_pair(400u, 480u);
  187. active_config = config;
  188. pad_state.hex = 0;
  189. touch_x = 0;
  190. touch_y = 0;
  191. circle_pad_x = 0;
  192. circle_pad_y = 0;
  193. touch_pressed = false;
  194. }
  195. virtual ~EmuWindow() {}
  196. /**
  197. * Processes any pending configuration changes from the last SetConfig call.
  198. * This method invokes OnMinimalClientAreaChangeRequest if the corresponding configuration
  199. * field changed.
  200. * @note Implementations will usually want to call this from the GUI thread.
  201. * @todo Actually call this in existing implementations.
  202. */
  203. void ProcessConfigurationChanges() {
  204. // TODO: For proper thread safety, we should eventually implement a proper
  205. // multiple-writer/single-reader queue...
  206. if (config.min_client_area_size != active_config.min_client_area_size) {
  207. OnMinimalClientAreaChangeRequest(config.min_client_area_size);
  208. config.min_client_area_size = active_config.min_client_area_size;
  209. }
  210. }
  211. /**
  212. * Update framebuffer layout with the given parameter.
  213. * @note EmuWindow implementations will usually use this in window resize event handlers.
  214. */
  215. void NotifyFramebufferLayoutChanged(const Layout::FramebufferLayout& layout) {
  216. framebuffer_layout = layout;
  217. }
  218. /**
  219. * Update internal client area size with the given parameter.
  220. * @note EmuWindow implementations will usually use this in window resize event handlers.
  221. */
  222. void NotifyClientAreaSizeChanged(const std::pair<unsigned, unsigned>& size) {
  223. client_area_width = size.first;
  224. client_area_height = size.second;
  225. }
  226. private:
  227. /**
  228. * Handler called when the minimal client area was requested to be changed via SetConfig.
  229. * For the request to be honored, EmuWindow implementations will usually reimplement this
  230. * function.
  231. */
  232. virtual void OnMinimalClientAreaChangeRequest(
  233. const std::pair<unsigned, unsigned>& minimal_size) {
  234. // By default, ignore this request and do nothing.
  235. }
  236. Layout::FramebufferLayout framebuffer_layout; ///< Current framebuffer layout
  237. unsigned client_area_width; ///< Current client width, should be set by window impl.
  238. unsigned client_area_height; ///< Current client height, should be set by window impl.
  239. WindowConfig config; ///< Internal configuration (changes pending for being applied in
  240. /// ProcessConfigurationChanges)
  241. WindowConfig active_config; ///< Internal active configuration
  242. bool touch_pressed; ///< True if touchpad area is currently pressed, otherwise false
  243. u16 touch_x; ///< Touchpad X-position in native 3DS pixel coordinates (0-320)
  244. u16 touch_y; ///< Touchpad Y-position in native 3DS pixel coordinates (0-240)
  245. s16 circle_pad_x; ///< Circle pad X-position in native 3DS pixel coordinates (-156 - 156)
  246. s16 circle_pad_y; ///< Circle pad Y-position in native 3DS pixel coordinates (-156 - 156)
  247. /**
  248. * Clip the provided coordinates to be inside the touchscreen area.
  249. */
  250. std::tuple<unsigned, unsigned> ClipToTouchScreen(unsigned new_x, unsigned new_y);
  251. Service::HID::PadState pad_state;
  252. };