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