PlatformWin32.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. // Copyright 2019 Dolphin Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include "DolphinNoGUI/Platform.h"
  4. #include "Core/Config/MainSettings.h"
  5. #include "Core/ConfigManager.h"
  6. #include "Core/Core.h"
  7. #include "Core/System.h"
  8. #include <Windows.h>
  9. #include <climits>
  10. #include <dwmapi.h>
  11. #include "VideoCommon/Present.h"
  12. #include "resource.h"
  13. namespace
  14. {
  15. class PlatformWin32 final : public Platform
  16. {
  17. public:
  18. ~PlatformWin32() override;
  19. bool Init() override;
  20. void SetTitle(const std::string& string) override;
  21. void MainLoop() override;
  22. WindowSystemInfo GetWindowSystemInfo() const override;
  23. private:
  24. static constexpr TCHAR WINDOW_CLASS_NAME[] = _T("DolphinNoGUI");
  25. static LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
  26. static bool RegisterRenderWindowClass();
  27. bool CreateRenderWindow();
  28. void UpdateWindowPosition();
  29. void ProcessEvents();
  30. HWND m_hwnd{};
  31. int m_window_x = Config::Get(Config::MAIN_RENDER_WINDOW_XPOS);
  32. int m_window_y = Config::Get(Config::MAIN_RENDER_WINDOW_YPOS);
  33. int m_window_width = Config::Get(Config::MAIN_RENDER_WINDOW_WIDTH);
  34. int m_window_height = Config::Get(Config::MAIN_RENDER_WINDOW_HEIGHT);
  35. };
  36. PlatformWin32::~PlatformWin32()
  37. {
  38. if (m_hwnd)
  39. DestroyWindow(m_hwnd);
  40. }
  41. bool PlatformWin32::RegisterRenderWindowClass()
  42. {
  43. WNDCLASSEX wc = {};
  44. wc.cbSize = sizeof(WNDCLASSEX);
  45. wc.style = 0;
  46. wc.lpfnWndProc = WndProc;
  47. wc.cbClsExtra = 0;
  48. wc.cbWndExtra = 0;
  49. wc.hInstance = GetModuleHandle(nullptr);
  50. wc.hIcon = LoadIcon(nullptr, IDI_ICON1);
  51. wc.hCursor = LoadCursor(nullptr, IDC_ARROW);
  52. wc.hbrBackground = reinterpret_cast<HBRUSH>(COLOR_WINDOW + 1);
  53. wc.lpszMenuName = nullptr;
  54. wc.lpszClassName = WINDOW_CLASS_NAME;
  55. wc.hIconSm = LoadIcon(nullptr, IDI_ICON1);
  56. if (!RegisterClassEx(&wc))
  57. {
  58. MessageBox(nullptr, _T("Window registration failed."), _T("Error"), MB_ICONERROR | MB_OK);
  59. return false;
  60. }
  61. return true;
  62. }
  63. bool PlatformWin32::CreateRenderWindow()
  64. {
  65. m_hwnd = CreateWindowEx(WS_EX_CLIENTEDGE, WINDOW_CLASS_NAME, _T("Dolphin"), WS_OVERLAPPEDWINDOW,
  66. m_window_x < 0 ? CW_USEDEFAULT : m_window_x,
  67. m_window_y < 0 ? CW_USEDEFAULT : m_window_y, m_window_width,
  68. m_window_height, nullptr, nullptr, GetModuleHandle(nullptr), this);
  69. if (!m_hwnd)
  70. {
  71. MessageBox(nullptr, _T("CreateWindowEx failed."), _T("Error"), MB_ICONERROR | MB_OK);
  72. return false;
  73. }
  74. ShowWindow(m_hwnd, SW_SHOW);
  75. UpdateWindow(m_hwnd);
  76. return true;
  77. }
  78. bool PlatformWin32::Init()
  79. {
  80. if (!RegisterRenderWindowClass() || !CreateRenderWindow())
  81. return false;
  82. // TODO: Enter fullscreen if enabled.
  83. if (Config::Get(Config::MAIN_FULLSCREEN))
  84. {
  85. ProcessEvents();
  86. }
  87. if (Config::Get(Config::MAIN_DISABLE_SCREENSAVER))
  88. SetThreadExecutionState(ES_CONTINUOUS | ES_DISPLAY_REQUIRED | ES_SYSTEM_REQUIRED);
  89. UpdateWindowPosition();
  90. return true;
  91. }
  92. void PlatformWin32::SetTitle(const std::string& string)
  93. {
  94. SetWindowTextW(m_hwnd, UTF8ToWString(string).c_str());
  95. }
  96. void PlatformWin32::MainLoop()
  97. {
  98. while (IsRunning())
  99. {
  100. UpdateRunningFlag();
  101. Core::HostDispatchJobs(Core::System::GetInstance());
  102. ProcessEvents();
  103. UpdateWindowPosition();
  104. // TODO: Is this sleep appropriate?
  105. std::this_thread::sleep_for(std::chrono::milliseconds(1));
  106. }
  107. }
  108. WindowSystemInfo PlatformWin32::GetWindowSystemInfo() const
  109. {
  110. WindowSystemInfo wsi;
  111. wsi.type = WindowSystemType::Windows;
  112. wsi.render_window = reinterpret_cast<void*>(m_hwnd);
  113. wsi.render_surface = reinterpret_cast<void*>(m_hwnd);
  114. return wsi;
  115. }
  116. void PlatformWin32::UpdateWindowPosition()
  117. {
  118. if (m_window_fullscreen)
  119. return;
  120. RECT rc = {};
  121. if (!GetWindowRect(m_hwnd, &rc))
  122. return;
  123. m_window_x = rc.left;
  124. m_window_y = rc.top;
  125. m_window_width = rc.right - rc.left;
  126. m_window_height = rc.bottom - rc.top;
  127. }
  128. void PlatformWin32::ProcessEvents()
  129. {
  130. MSG msg;
  131. while (PeekMessage(&msg, m_hwnd, 0, 0, PM_REMOVE))
  132. {
  133. TranslateMessage(&msg);
  134. DispatchMessage(&msg);
  135. }
  136. }
  137. LRESULT PlatformWin32::WndProc(const HWND hwnd, const UINT msg, const WPARAM wParam,
  138. const LPARAM lParam)
  139. {
  140. PlatformWin32* platform = reinterpret_cast<PlatformWin32*>(GetWindowLongPtr(hwnd, GWLP_USERDATA));
  141. switch (msg)
  142. {
  143. case WM_NCCREATE:
  144. {
  145. platform = static_cast<PlatformWin32*>(reinterpret_cast<CREATESTRUCT*>(lParam)->lpCreateParams);
  146. SetWindowLongPtr(hwnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(platform));
  147. return DefWindowProc(hwnd, msg, wParam, lParam);
  148. }
  149. case WM_CREATE:
  150. {
  151. if (hwnd)
  152. {
  153. // Remove rounded corners from the render window on Windows 11
  154. constexpr DWM_WINDOW_CORNER_PREFERENCE corner_preference = DWMWCP_DONOTROUND;
  155. DwmSetWindowAttribute(hwnd, DWMWA_WINDOW_CORNER_PREFERENCE, &corner_preference,
  156. sizeof(corner_preference));
  157. }
  158. }
  159. break;
  160. case WM_SIZE:
  161. {
  162. if (g_presenter)
  163. g_presenter->ResizeSurface();
  164. }
  165. break;
  166. case WM_CLOSE:
  167. platform->RequestShutdown();
  168. break;
  169. default:
  170. return DefWindowProc(hwnd, msg, wParam, lParam);
  171. }
  172. return 0;
  173. }
  174. } // namespace
  175. std::unique_ptr<Platform> Platform::CreateWin32Platform()
  176. {
  177. return std::make_unique<PlatformWin32>();
  178. }