emu_window_sdl2.cpp 8.7 KB

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