emu_window.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 <memory>
  6. #include <tuple>
  7. #include <utility>
  8. #include "common/common_types.h"
  9. #include "core/frontend/framebuffer_layout.h"
  10. namespace Core::Frontend {
  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 = false;
  34. int res_width = 0;
  35. int res_height = 0;
  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. * Returns currently active configuration.
  62. * @note Accesses to the returned object need not be consistent because it may be modified in
  63. * another thread
  64. */
  65. const WindowConfig& GetActiveConfig() const {
  66. return active_config;
  67. }
  68. /**
  69. * Requests the internal configuration to be replaced by the specified argument at some point in
  70. * the future.
  71. * @note This method is thread-safe, because it delays configuration changes to the GUI event
  72. * loop. Hence there is no guarantee on when the requested configuration will be active.
  73. */
  74. void SetConfig(const WindowConfig& val) {
  75. config = val;
  76. }
  77. /**
  78. * Gets the framebuffer layout (width, height, and screen regions)
  79. * @note This method is thread-safe
  80. */
  81. const Layout::FramebufferLayout& GetFramebufferLayout() const {
  82. return framebuffer_layout;
  83. }
  84. /**
  85. * Convenience method to update the current frame layout
  86. * Read from the current settings to determine which layout to use.
  87. */
  88. void UpdateCurrentFramebufferLayout(unsigned width, unsigned height);
  89. protected:
  90. EmuWindow();
  91. virtual ~EmuWindow();
  92. /**
  93. * Processes any pending configuration changes from the last SetConfig call.
  94. * This method invokes OnMinimalClientAreaChangeRequest if the corresponding configuration
  95. * field changed.
  96. * @note Implementations will usually want to call this from the GUI thread.
  97. * @todo Actually call this in existing implementations.
  98. */
  99. void ProcessConfigurationChanges() {
  100. // TODO: For proper thread safety, we should eventually implement a proper
  101. // multiple-writer/single-reader queue...
  102. if (config.min_client_area_size != active_config.min_client_area_size) {
  103. OnMinimalClientAreaChangeRequest(config.min_client_area_size);
  104. config.min_client_area_size = active_config.min_client_area_size;
  105. }
  106. }
  107. /**
  108. * Update framebuffer layout with the given parameter.
  109. * @note EmuWindow implementations will usually use this in window resize event handlers.
  110. */
  111. void NotifyFramebufferLayoutChanged(const Layout::FramebufferLayout& layout) {
  112. framebuffer_layout = layout;
  113. }
  114. /**
  115. * Update internal client area size with the given parameter.
  116. * @note EmuWindow implementations will usually use this in window resize event handlers.
  117. */
  118. void NotifyClientAreaSizeChanged(const std::pair<unsigned, unsigned>& size) {
  119. client_area_width = size.first;
  120. client_area_height = size.second;
  121. }
  122. private:
  123. /**
  124. * Handler called when the minimal client area was requested to be changed via SetConfig.
  125. * For the request to be honored, EmuWindow implementations will usually reimplement this
  126. * function.
  127. */
  128. virtual void OnMinimalClientAreaChangeRequest(
  129. const std::pair<unsigned, unsigned>& minimal_size) {
  130. // By default, ignore this request and do nothing.
  131. }
  132. Layout::FramebufferLayout framebuffer_layout; ///< Current framebuffer layout
  133. unsigned client_area_width; ///< Current client width, should be set by window impl.
  134. unsigned client_area_height; ///< Current client height, should be set by window impl.
  135. WindowConfig config; ///< Internal configuration (changes pending for being applied in
  136. /// ProcessConfigurationChanges)
  137. WindowConfig active_config; ///< Internal active configuration
  138. class TouchState;
  139. std::shared_ptr<TouchState> touch_state;
  140. /**
  141. * Clip the provided coordinates to be inside the touchscreen area.
  142. */
  143. std::tuple<unsigned, unsigned> ClipToTouchScreen(unsigned new_x, unsigned new_y);
  144. };
  145. } // namespace Core::Frontend