emu_window_sdl2.cpp 8.7 KB

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