Debian Codegraph @

Source/Core/DolphinNoGUI/PlatformWin32.cpp

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