emu_window_sdl2.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 "core/core.h"
  8. #include "core/perf_stats.h"
  9. #include "input_common/keyboard.h"
  10. #include "input_common/main.h"
  11. #include "input_common/motion_emu.h"
  12. #include "input_common/sdl/sdl.h"
  13. #include "yuzu_cmd/emu_window/emu_window_sdl2.h"
  14. EmuWindow_SDL2::EmuWindow_SDL2(bool fullscreen) {
  15. if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) {
  16. LOG_CRITICAL(Frontend, "Failed to initialize SDL2! Exiting...");
  17. exit(1);
  18. }
  19. InputCommon::Init();
  20. SDL_SetMainReady();
  21. }
  22. EmuWindow_SDL2::~EmuWindow_SDL2() {
  23. InputCommon::Shutdown();
  24. SDL_Quit();
  25. }
  26. void EmuWindow_SDL2::OnMouseMotion(s32 x, s32 y) {
  27. TouchMoved((unsigned)std::max(x, 0), (unsigned)std::max(y, 0));
  28. InputCommon::GetMotionEmu()->Tilt(x, y);
  29. }
  30. void EmuWindow_SDL2::OnMouseButton(u32 button, u8 state, s32 x, s32 y) {
  31. if (button == SDL_BUTTON_LEFT) {
  32. if (state == SDL_PRESSED) {
  33. TouchPressed((unsigned)std::max(x, 0), (unsigned)std::max(y, 0));
  34. } else {
  35. TouchReleased();
  36. }
  37. } else if (button == SDL_BUTTON_RIGHT) {
  38. if (state == SDL_PRESSED) {
  39. InputCommon::GetMotionEmu()->BeginTilt(x, y);
  40. } else {
  41. InputCommon::GetMotionEmu()->EndTilt();
  42. }
  43. }
  44. }
  45. std::pair<unsigned, unsigned> EmuWindow_SDL2::TouchToPixelPos(float touch_x, float touch_y) const {
  46. int w, h;
  47. SDL_GetWindowSize(render_window, &w, &h);
  48. touch_x *= w;
  49. touch_y *= h;
  50. return {static_cast<unsigned>(std::max(std::round(touch_x), 0.0f)),
  51. static_cast<unsigned>(std::max(std::round(touch_y), 0.0f))};
  52. }
  53. void EmuWindow_SDL2::OnFingerDown(float x, float y) {
  54. // TODO(NeatNit): keep track of multitouch using the fingerID and a dictionary of some kind
  55. // This isn't critical because the best we can do when we have that is to average them, like the
  56. // 3DS does
  57. const auto [px, py] = TouchToPixelPos(x, y);
  58. TouchPressed(px, py);
  59. }
  60. void EmuWindow_SDL2::OnFingerMotion(float x, float y) {
  61. const auto [px, py] = TouchToPixelPos(x, y);
  62. TouchMoved(px, py);
  63. }
  64. void EmuWindow_SDL2::OnFingerUp() {
  65. TouchReleased();
  66. }
  67. void EmuWindow_SDL2::OnKeyEvent(int key, u8 state) {
  68. if (state == SDL_PRESSED) {
  69. InputCommon::GetKeyboard()->PressKey(key);
  70. } else if (state == SDL_RELEASED) {
  71. InputCommon::GetKeyboard()->ReleaseKey(key);
  72. }
  73. }
  74. bool EmuWindow_SDL2::IsOpen() const {
  75. return is_open;
  76. }
  77. bool EmuWindow_SDL2::IsShown() const {
  78. return is_shown;
  79. }
  80. void EmuWindow_SDL2::OnResize() {
  81. int width, height;
  82. SDL_GetWindowSize(render_window, &width, &height);
  83. UpdateCurrentFramebufferLayout(width, height);
  84. }
  85. void EmuWindow_SDL2::Fullscreen() {
  86. if (SDL_SetWindowFullscreen(render_window, SDL_WINDOW_FULLSCREEN) == 0) {
  87. return;
  88. }
  89. LOG_ERROR(Frontend, "Fullscreening failed: {}", SDL_GetError());
  90. // Try a different fullscreening method
  91. LOG_INFO(Frontend, "Attempting to use borderless fullscreen...");
  92. if (SDL_SetWindowFullscreen(render_window, SDL_WINDOW_FULLSCREEN_DESKTOP) == 0) {
  93. return;
  94. }
  95. LOG_ERROR(Frontend, "Borderless fullscreening failed: {}", SDL_GetError());
  96. // Fallback algorithm: Maximise window.
  97. // Works on all systems (unless something is seriously wrong), so no fallback for this one.
  98. LOG_INFO(Frontend, "Falling back on a maximised window...");
  99. SDL_MaximizeWindow(render_window);
  100. }
  101. void EmuWindow_SDL2::PollEvents() {
  102. SDL_Event event;
  103. // SDL_PollEvent returns 0 when there are no more events in the event queue
  104. while (SDL_PollEvent(&event)) {
  105. switch (event.type) {
  106. case SDL_WINDOWEVENT:
  107. switch (event.window.event) {
  108. case SDL_WINDOWEVENT_SIZE_CHANGED:
  109. case SDL_WINDOWEVENT_RESIZED:
  110. case SDL_WINDOWEVENT_MAXIMIZED:
  111. case SDL_WINDOWEVENT_RESTORED:
  112. OnResize();
  113. break;
  114. case SDL_WINDOWEVENT_MINIMIZED:
  115. case SDL_WINDOWEVENT_EXPOSED:
  116. is_shown = event.window.event == SDL_WINDOWEVENT_EXPOSED;
  117. OnResize();
  118. break;
  119. case SDL_WINDOWEVENT_CLOSE:
  120. is_open = false;
  121. break;
  122. }
  123. break;
  124. case SDL_KEYDOWN:
  125. case SDL_KEYUP:
  126. OnKeyEvent(static_cast<int>(event.key.keysym.scancode), event.key.state);
  127. break;
  128. case SDL_MOUSEMOTION:
  129. // ignore if it came from touch
  130. if (event.button.which != SDL_TOUCH_MOUSEID)
  131. OnMouseMotion(event.motion.x, event.motion.y);
  132. break;
  133. case SDL_MOUSEBUTTONDOWN:
  134. case SDL_MOUSEBUTTONUP:
  135. // ignore if it came from touch
  136. if (event.button.which != SDL_TOUCH_MOUSEID) {
  137. OnMouseButton(event.button.button, event.button.state, event.button.x,
  138. event.button.y);
  139. }
  140. break;
  141. case SDL_FINGERDOWN:
  142. OnFingerDown(event.tfinger.x, event.tfinger.y);
  143. break;
  144. case SDL_FINGERMOTION:
  145. OnFingerMotion(event.tfinger.x, event.tfinger.y);
  146. break;
  147. case SDL_FINGERUP:
  148. OnFingerUp();
  149. break;
  150. case SDL_QUIT:
  151. is_open = false;
  152. break;
  153. default:
  154. break;
  155. }
  156. }
  157. const u32 current_time = SDL_GetTicks();
  158. if (current_time > last_time + 2000) {
  159. const auto results = Core::System::GetInstance().GetAndResetPerfStats();
  160. const auto title = fmt::format(
  161. "yuzu {} | {}-{} | FPS: {:.0f} ({:.0%})", Common::g_build_fullname,
  162. Common::g_scm_branch, Common::g_scm_desc, results.game_fps, results.emulation_speed);
  163. SDL_SetWindowTitle(render_window, title.c_str());
  164. last_time = current_time;
  165. }
  166. }
  167. void EmuWindow_SDL2::OnMinimalClientAreaChangeRequest(std::pair<unsigned, unsigned> minimal_size) {
  168. SDL_SetWindowMinimumSize(render_window, minimal_size.first, minimal_size.second);
  169. }