emu_window_sdl2.cpp 6.3 KB

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