PlatformWin32.cpp 5.2 KB

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