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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
|
#include "deimos/core/os.h"
#include "deimos/core/api_registry.h"
#include "deimos/core/allocator.h"
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
namespace deimos
{
gsl::cwzstring Utf8ToUtf16Z(StringView src, Span<wchar_t> buffer)
{
Expects(!buffer.empty() && buffer.size() > src.size());
const int res = ::MultiByteToWideChar(CP_UTF8, 0, src.data(), (int)src.size_bytes(), buffer.data(), (int)buffer.size() - 1);
if (res < 0) { return L"< MBTWC ERROR >"; }
buffer[res] = L'\0';
return buffer.data();
}
class Win32ConsoleApiImpl : public OsConsoleApi
{
HANDLE m_stdout;
HANDLE m_stderr;
constexpr HANDLE Handle(OsConsoleType type) const
{
switch (type)
{
using enum OsConsoleType;
case kStdOut: return m_stdout;
case kStdErr: return m_stderr;
}
}
public:
Win32ConsoleApiImpl() :
m_stdout{::GetStdHandle(STD_OUTPUT_HANDLE)},
m_stderr{::GetStdHandle(STD_ERROR_HANDLE)}
{}
void Write(OsConsoleType type, Span<const std::byte> data) override
{
::WriteConsoleA(Handle(type), data.data(), (DWORD)data.size(), nullptr, nullptr);
}
};
class Win32DllApiImpl : public OsDllApi
{
public:
OsDll* Open(gsl::czstring name) override
{
return std::bit_cast<OsDll*>(::LoadLibraryA(name));
}
void* GetSymbol(OsDll* dll, gsl::czstring name) override
{
return std::bit_cast<void*>(::GetProcAddress(std::bit_cast<HMODULE>(dll), name));
}
};
class Win32VirtualMemoryApiImpl : public OsVirtualMemoryApi
{
public:
gsl::owner<void*> Map(int64_t size) override
{
return ::VirtualAlloc(nullptr, (SIZE_T)size, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
}
void Unmap(gsl::owner<void*> ptr) override
{
::VirtualFree(ptr, 0, MEM_RELEASE);
}
gsl::owner<void*> Reserve(int64_t size) override
{
return ::VirtualAlloc(nullptr, (SIZE_T)size, MEM_RESERVE, PAGE_READWRITE);
}
void Commit(void* ptr, int64_t size) override
{
::VirtualAlloc(ptr, (SIZE_T)size, MEM_COMMIT, PAGE_READWRITE);
}
};
struct OsWindow
{
HWND hwnd{};
bool destroyed{};
bool quit_requested{};
};
class Win32WindowApiImpl : public OsWindowApi
{
static constexpr wchar_t kClassName[] = L"Deimos window class";
HINSTANCE m_hinstance;
Allocator* m_allocator;
bool m_class_registered = false;
static LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
if (uMsg == WM_CREATE)
{
const auto* create = (const CREATESTRUCT*)lParam; // NOLINT
::SetWindowLongPtrW(hwnd, GWLP_USERDATA, std::bit_cast<LONG_PTR>(create->lpCreateParams));
return 0;
}
auto* window = std::bit_cast<OsWindow*>(::GetWindowLongPtrW(hwnd, GWLP_USERDATA));
switch (uMsg)
{
case WM_CLOSE:
{
window->quit_requested = true;
::DestroyWindow(hwnd);
return 0;
}
case WM_DESTROY:
{
window->destroyed = true;
::PostQuitMessage(0);
return 0;
}
default:
return ::DefWindowProcW(hwnd, uMsg, wParam, lParam);
}
}
void RegisterClass()
{
Expects(!m_class_registered);
WNDCLASSW wnd_class{};
wnd_class.lpfnWndProc = WindowProc;
wnd_class.hInstance = m_hinstance;
wnd_class.lpszClassName = kClassName;
::RegisterClassW(&wnd_class);
m_class_registered = true;
}
public:
explicit Win32WindowApiImpl(Allocator* allocator) :
m_hinstance{::GetModuleHandleW(nullptr)},
m_allocator{allocator}
{}
StatusOr<gsl::owner<OsWindow*>> Create(gsl::czstring title, int32_t width, int32_t height) override
{
if (!m_class_registered)
{
RegisterClass();
}
Ensures(m_class_registered);
wchar_t title_w_buffer[128]{};
gsl::cwzstring title_w = Utf8ToUtf16Z(title, title_w_buffer);
auto* window = m_allocator->New<OsWindow>();
HWND hwnd = ::CreateWindowExW(
0, kClassName, title_w, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, width, height,
nullptr, nullptr, m_hinstance, window);
if (hwnd == nullptr)
{
return InternalError("Error while creating Win32 window");
}
::ShowWindow(hwnd, SW_SHOW);
window->hwnd = hwnd;
return window;
}
void Update(OsWindow* window) override
{
if (window->destroyed) { return; }
MSG msg;
while (::PeekMessageW(&msg, window->hwnd, 0, 0, PM_REMOVE) != 0)
{
TranslateMessage(&msg);
DispatchMessageW(&msg);
}
}
bool QuitRequested(const OsWindow* window) override
{
return window->quit_requested;
}
#if DEIMOS_OS_WIN32
void* Win32Hwnd(const OsWindow* window) override
{
return std::bit_cast<void*>(window->hwnd);
}
void* Win32Hinstance(const OsWindow*) override
{
return std::bit_cast<void*>(m_hinstance);
}
#endif
};
class Win32OsApiImpl : public OsApi
{
Win32ConsoleApiImpl m_console_api;
Win32DllApiImpl m_dll_api;
Win32VirtualMemoryApiImpl m_virtual_memory_api;
Win32WindowApiImpl m_window_api;
public:
explicit Win32OsApiImpl(Allocator* allocator) :
m_window_api(allocator)
{
console = &m_console_api;
dll = &m_dll_api;
virtual_memory = &m_virtual_memory_api;
window = &m_window_api;
}
};
void RegisterOsApi(ApiRegistry* api_registry)
{
auto* allocator_api = api_registry->Get<AllocatorApi>();
Allocator* allocator = allocator_api->CreateChild(allocator_api->system, "OS");
auto* os_api = allocator_api->system->New<Win32OsApiImpl>(allocator);
api_registry->Set(os_api);
}
} // namespace deimos
|