emu_window_sdl2.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. // Copyright 2016 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <SDL.h>
  5. #include "common/logging/log.h"
  6. #include "common/scm_rev.h"
  7. #include "common/settings.h"
  8. #include "core/core.h"
  9. #include "core/perf_stats.h"
  10. #include "input_common/keyboard.h"
  11. #include "input_common/main.h"
  12. #include "input_common/mouse/mouse_input.h"
  13. #include "input_common/sdl/sdl.h"
  14. #include "yuzu_cmd/emu_window/emu_window_sdl2.h"
  15. #include "yuzu_cmd/yuzu_icon.h"
  16. EmuWindow_SDL2::EmuWindow_SDL2(InputCommon::InputSubsystem* input_subsystem_, Core::System& system_)
  17. : input_subsystem{input_subsystem_}, system{system_} {
  18. if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) {
  19. LOG_CRITICAL(Frontend, "Failed to initialize SDL2! Exiting...");
  20. exit(1);
  21. }
  22. input_subsystem->Initialize();
  23. SDL_SetMainReady();
  24. }
  25. EmuWindow_SDL2::~EmuWindow_SDL2() {
  26. input_subsystem->Shutdown();
  27. SDL_Quit();
  28. }
  29. void EmuWindow_SDL2::OnMouseMotion(s32 x, s32 y) {
  30. TouchMoved((unsigned)std::max(x, 0), (unsigned)std::max(y, 0), 0);
  31. input_subsystem->GetMouse()->MouseMove(x, y, 0, 0);
  32. }
  33. MouseInput::MouseButton EmuWindow_SDL2::SDLButtonToMouseButton(u32 button) const {
  34. switch (button) {
  35. case SDL_BUTTON_LEFT:
  36. return MouseInput::MouseButton::Left;
  37. case SDL_BUTTON_RIGHT:
  38. return MouseInput::MouseButton::Right;
  39. case SDL_BUTTON_MIDDLE:
  40. return MouseInput::MouseButton::Wheel;
  41. case SDL_BUTTON_X1:
  42. return MouseInput::MouseButton::Backward;
  43. case SDL_BUTTON_X2:
  44. return MouseInput::MouseButton::Forward;
  45. default:
  46. return MouseInput::MouseButton::Undefined;
  47. }
  48. }
  49. void EmuWindow_SDL2::OnMouseButton(u32 button, u8 state, s32 x, s32 y) {
  50. const auto mouse_button = SDLButtonToMouseButton(button);
  51. if (button == SDL_BUTTON_LEFT) {
  52. if (state == SDL_PRESSED) {
  53. TouchPressed((unsigned)std::max(x, 0), (unsigned)std::max(y, 0), 0);
  54. } else {
  55. TouchReleased(0);
  56. }
  57. } else {
  58. if (state == SDL_PRESSED) {
  59. input_subsystem->GetMouse()->PressButton(x, y, mouse_button);
  60. } else {
  61. input_subsystem->GetMouse()->ReleaseButton(mouse_button);
  62. }
  63. }
  64. }
  65. std::pair<unsigned, unsigned> EmuWindow_SDL2::TouchToPixelPos(float touch_x, float touch_y) const {
  66. int w, h;
  67. SDL_GetWindowSize(render_window, &w, &h);
  68. touch_x *= w;
  69. touch_y *= h;
  70. return {static_cast<unsigned>(std::max(std::round(touch_x), 0.0f)),
  71. static_cast<unsigned>(std::max(std::round(touch_y), 0.0f))};
  72. }
  73. void EmuWindow_SDL2::OnFingerDown(float x, float y) {
  74. // TODO(NeatNit): keep track of multitouch using the fingerID and a dictionary of some kind
  75. // This isn't critical because the best we can do when we have that is to average them, like the
  76. // 3DS does
  77. const auto [px, py] = TouchToPixelPos(x, y);
  78. TouchPressed(px, py, 0);
  79. }
  80. void EmuWindow_SDL2::OnFingerMotion(float x, float y) {
  81. const auto [px, py] = TouchToPixelPos(x, y);
  82. TouchMoved(px, py, 0);
  83. }
  84. void EmuWindow_SDL2::OnFingerUp() {
  85. TouchReleased(0);
  86. }
  87. void EmuWindow_SDL2::OnKeyEvent(int key, u8 state) {
  88. if (state == SDL_PRESSED) {
  89. input_subsystem->GetKeyboard()->PressKey(key);
  90. } else if (state == SDL_RELEASED) {
  91. input_subsystem->GetKeyboard()->ReleaseKey(key);
  92. }
  93. }
  94. bool EmuWindow_SDL2::IsOpen() const {
  95. return is_open;
  96. }
  97. bool EmuWindow_SDL2::IsShown() const {
  98. return is_shown;
  99. }
  100. void EmuWindow_SDL2::OnResize() {
  101. int width, height;
  102. SDL_GetWindowSize(render_window, &width, &height);
  103. UpdateCurrentFramebufferLayout(width, height);
  104. }
  105. void EmuWindow_SDL2::Fullscreen() {
  106. switch (Settings::values.fullscreen_mode.GetValue()) {
  107. case Settings::FullscreenMode::Exclusive:
  108. // Set window size to render size before entering fullscreen -- SDL does not resize to
  109. // display dimensions in this mode.
  110. // TODO: Multiply the window size by resolution_factor (for both docked modes)
  111. if (Settings::values.use_docked_mode) {
  112. SDL_SetWindowSize(render_window, Layout::ScreenDocked::Width,
  113. Layout::ScreenDocked::Height);
  114. }
  115. if (SDL_SetWindowFullscreen(render_window, SDL_WINDOW_FULLSCREEN) == 0) {
  116. return;
  117. }
  118. LOG_ERROR(Frontend, "Fullscreening failed: {}", SDL_GetError());
  119. LOG_INFO(Frontend, "Attempting to use borderless fullscreen...");
  120. [[fallthrough]];
  121. case Settings::FullscreenMode::Borderless:
  122. if (SDL_SetWindowFullscreen(render_window, SDL_WINDOW_FULLSCREEN_DESKTOP) == 0) {
  123. return;
  124. }
  125. LOG_ERROR(Frontend, "Borderless fullscreening failed: {}", SDL_GetError());
  126. [[fallthrough]];
  127. default:
  128. // Fallback algorithm: Maximise window.
  129. // Works on all systems (unless something is seriously wrong), so no fallback for this one.
  130. LOG_INFO(Frontend, "Falling back on a maximised window...");
  131. SDL_MaximizeWindow(render_window);
  132. break;
  133. }
  134. }
  135. void EmuWindow_SDL2::WaitEvent() {
  136. // Called on main thread
  137. SDL_Event event;
  138. if (!SDL_WaitEvent(&event)) {
  139. LOG_CRITICAL(Frontend, "SDL_WaitEvent failed: {}", SDL_GetError());
  140. exit(1);
  141. }
  142. switch (event.type) {
  143. case SDL_WINDOWEVENT:
  144. switch (event.window.event) {
  145. case SDL_WINDOWEVENT_SIZE_CHANGED:
  146. case SDL_WINDOWEVENT_RESIZED:
  147. case SDL_WINDOWEVENT_MAXIMIZED:
  148. case SDL_WINDOWEVENT_RESTORED:
  149. OnResize();
  150. break;
  151. case SDL_WINDOWEVENT_MINIMIZED:
  152. case SDL_WINDOWEVENT_EXPOSED:
  153. is_shown = event.window.event == SDL_WINDOWEVENT_EXPOSED;
  154. OnResize();
  155. break;
  156. case SDL_WINDOWEVENT_CLOSE:
  157. is_open = false;
  158. break;
  159. }
  160. break;
  161. case SDL_KEYDOWN:
  162. case SDL_KEYUP:
  163. OnKeyEvent(static_cast<int>(event.key.keysym.scancode), event.key.state);
  164. break;
  165. case SDL_MOUSEMOTION:
  166. // ignore if it came from touch
  167. if (event.button.which != SDL_TOUCH_MOUSEID)
  168. OnMouseMotion(event.motion.x, event.motion.y);
  169. break;
  170. case SDL_MOUSEBUTTONDOWN:
  171. case SDL_MOUSEBUTTONUP:
  172. // ignore if it came from touch
  173. if (event.button.which != SDL_TOUCH_MOUSEID) {
  174. OnMouseButton(event.button.button, event.button.state, event.button.x, event.button.y);
  175. }
  176. break;
  177. case SDL_FINGERDOWN:
  178. OnFingerDown(event.tfinger.x, event.tfinger.y);
  179. break;
  180. case SDL_FINGERMOTION:
  181. OnFingerMotion(event.tfinger.x, event.tfinger.y);
  182. break;
  183. case SDL_FINGERUP:
  184. OnFingerUp();
  185. break;
  186. case SDL_QUIT:
  187. is_open = false;
  188. break;
  189. default:
  190. break;
  191. }
  192. const u32 current_time = SDL_GetTicks();
  193. if (current_time > last_time + 2000) {
  194. const auto results = system.GetAndResetPerfStats();
  195. const auto title =
  196. fmt::format("yuzu {} | {}-{} | FPS: {:.0f} ({:.0f}%)", Common::g_build_fullname,
  197. Common::g_scm_branch, Common::g_scm_desc, results.average_game_fps,
  198. results.emulation_speed * 100.0);
  199. SDL_SetWindowTitle(render_window, title.c_str());
  200. last_time = current_time;
  201. }
  202. }
  203. void EmuWindow_SDL2::SetWindowIcon() {
  204. SDL_RWops* const yuzu_icon_stream = SDL_RWFromConstMem((void*)yuzu_icon, yuzu_icon_size);
  205. if (yuzu_icon_stream == nullptr) {
  206. LOG_WARNING(Frontend, "Failed to create yuzu icon stream.");
  207. return;
  208. }
  209. SDL_Surface* const window_icon = SDL_LoadBMP_RW(yuzu_icon_stream, 1);
  210. if (window_icon == nullptr) {
  211. LOG_WARNING(Frontend, "Failed to read BMP from stream.");
  212. return;
  213. }
  214. // The icon is attached to the window pointer
  215. SDL_SetWindowIcon(render_window, window_icon);
  216. SDL_FreeSurface(window_icon);
  217. }
  218. void EmuWindow_SDL2::OnMinimalClientAreaChangeRequest(std::pair<u32, u32> minimal_size) {
  219. SDL_SetWindowMinimumSize(render_window, minimal_size.first, minimal_size.second);
  220. }