emu_window_sdl2.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. // SPDX-FileCopyrightText: 2016 Citra Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include <SDL.h>
  4. #include "common/logging/log.h"
  5. #include "common/scm_rev.h"
  6. #include "common/settings.h"
  7. #include "core/core.h"
  8. #include "core/hid/hid_core.h"
  9. #include "core/perf_stats.h"
  10. #include "input_common/drivers/keyboard.h"
  11. #include "input_common/drivers/mouse.h"
  12. #include "input_common/drivers/touch_screen.h"
  13. #include "input_common/main.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. input_subsystem->Initialize();
  19. if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER) < 0) {
  20. LOG_CRITICAL(Frontend, "Failed to initialize SDL2! Exiting...");
  21. exit(1);
  22. }
  23. SDL_SetMainReady();
  24. }
  25. EmuWindow_SDL2::~EmuWindow_SDL2() {
  26. system.HIDCore().UnloadInputDevices();
  27. input_subsystem->Shutdown();
  28. SDL_Quit();
  29. }
  30. InputCommon::MouseButton EmuWindow_SDL2::SDLButtonToMouseButton(u32 button) const {
  31. switch (button) {
  32. case SDL_BUTTON_LEFT:
  33. return InputCommon::MouseButton::Left;
  34. case SDL_BUTTON_RIGHT:
  35. return InputCommon::MouseButton::Right;
  36. case SDL_BUTTON_MIDDLE:
  37. return InputCommon::MouseButton::Wheel;
  38. case SDL_BUTTON_X1:
  39. return InputCommon::MouseButton::Backward;
  40. case SDL_BUTTON_X2:
  41. return InputCommon::MouseButton::Forward;
  42. default:
  43. return InputCommon::MouseButton::Undefined;
  44. }
  45. }
  46. std::pair<float, float> EmuWindow_SDL2::MouseToTouchPos(s32 touch_x, s32 touch_y) const {
  47. int w, h;
  48. SDL_GetWindowSize(render_window, &w, &h);
  49. const float fx = static_cast<float>(touch_x) / w;
  50. const float fy = static_cast<float>(touch_y) / h;
  51. return {std::clamp<float>(fx, 0.0f, 1.0f), std::clamp<float>(fy, 0.0f, 1.0f)};
  52. }
  53. void EmuWindow_SDL2::OnMouseButton(u32 button, u8 state, s32 x, s32 y) {
  54. const auto mouse_button = SDLButtonToMouseButton(button);
  55. if (state == SDL_PRESSED) {
  56. const auto [touch_x, touch_y] = MouseToTouchPos(x, y);
  57. input_subsystem->GetMouse()->PressButton(x, y, touch_x, touch_y, mouse_button);
  58. } else {
  59. input_subsystem->GetMouse()->ReleaseButton(mouse_button);
  60. }
  61. }
  62. void EmuWindow_SDL2::OnMouseMotion(s32 x, s32 y) {
  63. const auto [touch_x, touch_y] = MouseToTouchPos(x, y);
  64. input_subsystem->GetMouse()->MouseMove(x, y, touch_x, touch_y, 0, 0);
  65. }
  66. void EmuWindow_SDL2::OnFingerDown(float x, float y, std::size_t id) {
  67. input_subsystem->GetTouchScreen()->TouchPressed(x, y, id);
  68. }
  69. void EmuWindow_SDL2::OnFingerMotion(float x, float y, std::size_t id) {
  70. input_subsystem->GetTouchScreen()->TouchMoved(x, y, id);
  71. }
  72. void EmuWindow_SDL2::OnFingerUp() {
  73. input_subsystem->GetTouchScreen()->ReleaseAllTouch();
  74. }
  75. void EmuWindow_SDL2::OnKeyEvent(int key, u8 state) {
  76. if (state == SDL_PRESSED) {
  77. input_subsystem->GetKeyboard()->PressKey(static_cast<std::size_t>(key));
  78. } else if (state == SDL_RELEASED) {
  79. input_subsystem->GetKeyboard()->ReleaseKey(static_cast<std::size_t>(key));
  80. }
  81. }
  82. bool EmuWindow_SDL2::IsOpen() const {
  83. return is_open;
  84. }
  85. bool EmuWindow_SDL2::IsShown() const {
  86. return is_shown;
  87. }
  88. void EmuWindow_SDL2::OnResize() {
  89. int width, height;
  90. SDL_GL_GetDrawableSize(render_window, &width, &height);
  91. UpdateCurrentFramebufferLayout(width, height);
  92. }
  93. void EmuWindow_SDL2::ShowCursor(bool show_cursor) {
  94. SDL_ShowCursor(show_cursor ? SDL_ENABLE : SDL_DISABLE);
  95. }
  96. void EmuWindow_SDL2::Fullscreen() {
  97. SDL_DisplayMode display_mode;
  98. switch (Settings::values.fullscreen_mode.GetValue()) {
  99. case Settings::FullscreenMode::Exclusive:
  100. // Set window size to render size before entering fullscreen -- SDL2 does not resize window
  101. // to display dimensions automatically in this mode.
  102. if (SDL_GetDesktopDisplayMode(0, &display_mode) == 0) {
  103. SDL_SetWindowSize(render_window, display_mode.w, display_mode.h);
  104. } else {
  105. LOG_ERROR(Frontend, "SDL_GetDesktopDisplayMode failed: {}", SDL_GetError());
  106. }
  107. if (SDL_SetWindowFullscreen(render_window, SDL_WINDOW_FULLSCREEN) == 0) {
  108. return;
  109. }
  110. LOG_ERROR(Frontend, "Fullscreening failed: {}", SDL_GetError());
  111. LOG_INFO(Frontend, "Attempting to use borderless fullscreen...");
  112. [[fallthrough]];
  113. case Settings::FullscreenMode::Borderless:
  114. if (SDL_SetWindowFullscreen(render_window, SDL_WINDOW_FULLSCREEN_DESKTOP) == 0) {
  115. return;
  116. }
  117. LOG_ERROR(Frontend, "Borderless fullscreening failed: {}", SDL_GetError());
  118. [[fallthrough]];
  119. default:
  120. // Fallback algorithm: Maximise window.
  121. // Works on all systems (unless something is seriously wrong), so no fallback for this one.
  122. LOG_INFO(Frontend, "Falling back on a maximised window...");
  123. SDL_MaximizeWindow(render_window);
  124. break;
  125. }
  126. }
  127. void EmuWindow_SDL2::WaitEvent() {
  128. // Called on main thread
  129. SDL_Event event;
  130. if (!SDL_WaitEvent(&event)) {
  131. const char* error = SDL_GetError();
  132. if (!error || strcmp(error, "") == 0) {
  133. // https://github.com/libsdl-org/SDL/issues/5780
  134. // Sometimes SDL will return without actually having hit an error condition;
  135. // just ignore it in this case.
  136. return;
  137. }
  138. LOG_CRITICAL(Frontend, "SDL_WaitEvent failed: {}", error);
  139. exit(1);
  140. }
  141. switch (event.type) {
  142. case SDL_WINDOWEVENT:
  143. switch (event.window.event) {
  144. case SDL_WINDOWEVENT_SIZE_CHANGED:
  145. case SDL_WINDOWEVENT_RESIZED:
  146. case SDL_WINDOWEVENT_MAXIMIZED:
  147. case SDL_WINDOWEVENT_RESTORED:
  148. OnResize();
  149. break;
  150. case SDL_WINDOWEVENT_MINIMIZED:
  151. case SDL_WINDOWEVENT_EXPOSED:
  152. is_shown = event.window.event == SDL_WINDOWEVENT_EXPOSED;
  153. OnResize();
  154. break;
  155. case SDL_WINDOWEVENT_CLOSE:
  156. is_open = false;
  157. break;
  158. }
  159. break;
  160. case SDL_KEYDOWN:
  161. case SDL_KEYUP:
  162. OnKeyEvent(static_cast<int>(event.key.keysym.scancode), event.key.state);
  163. break;
  164. case SDL_MOUSEMOTION:
  165. // ignore if it came from touch
  166. if (event.button.which != SDL_TOUCH_MOUSEID)
  167. OnMouseMotion(event.motion.x, event.motion.y);
  168. break;
  169. case SDL_MOUSEBUTTONDOWN:
  170. case SDL_MOUSEBUTTONUP:
  171. // ignore if it came from touch
  172. if (event.button.which != SDL_TOUCH_MOUSEID) {
  173. OnMouseButton(event.button.button, event.button.state, event.button.x, event.button.y);
  174. }
  175. break;
  176. case SDL_FINGERDOWN:
  177. OnFingerDown(event.tfinger.x, event.tfinger.y,
  178. static_cast<std::size_t>(event.tfinger.touchId));
  179. break;
  180. case SDL_FINGERMOTION:
  181. OnFingerMotion(event.tfinger.x, event.tfinger.y,
  182. static_cast<std::size_t>(event.tfinger.touchId));
  183. break;
  184. case SDL_FINGERUP:
  185. OnFingerUp();
  186. break;
  187. case SDL_QUIT:
  188. is_open = false;
  189. break;
  190. default:
  191. break;
  192. }
  193. const u32 current_time = SDL_GetTicks();
  194. if (current_time > last_time + 2000) {
  195. const auto results = system.GetAndResetPerfStats();
  196. const auto title =
  197. fmt::format("yuzu {} | {}-{} | FPS: {:.0f} ({:.0f}%)", Common::g_build_fullname,
  198. Common::g_scm_branch, Common::g_scm_desc, results.average_game_fps,
  199. results.emulation_speed * 100.0);
  200. SDL_SetWindowTitle(render_window, title.c_str());
  201. last_time = current_time;
  202. }
  203. }
  204. // Credits to Samantas5855 and others for this function.
  205. void EmuWindow_SDL2::SetWindowIcon() {
  206. SDL_RWops* const yuzu_icon_stream = SDL_RWFromConstMem((void*)yuzu_icon, yuzu_icon_size);
  207. if (yuzu_icon_stream == nullptr) {
  208. LOG_WARNING(Frontend, "Failed to create yuzu icon stream.");
  209. return;
  210. }
  211. SDL_Surface* const window_icon = SDL_LoadBMP_RW(yuzu_icon_stream, 1);
  212. if (window_icon == nullptr) {
  213. LOG_WARNING(Frontend, "Failed to read BMP from stream.");
  214. return;
  215. }
  216. // The icon is attached to the window pointer
  217. SDL_SetWindowIcon(render_window, window_icon);
  218. SDL_FreeSurface(window_icon);
  219. }
  220. void EmuWindow_SDL2::OnMinimalClientAreaChangeRequest(std::pair<u32, u32> minimal_size) {
  221. SDL_SetWindowMinimumSize(render_window, minimal_size.first, minimal_size.second);
  222. }