-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.cpp
173 lines (151 loc) · 5.18 KB
/
util.cpp
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
#include "pch.h"
#include "util.h"
#include <cstdarg>
namespace util = trainlist8::util;
namespace trainlist8::util {
namespace {
WS_CHANNEL_STATE getChannelState(WS_CHANNEL *channel) {
WS_CHANNEL_STATE state;
winrt::check_hresult(WsGetChannelProperty(channel, WS_CHANNEL_PROPERTY_STATE, &state, sizeof(state), nullptr));
return state;
}
}
}
util::AsyncWSAdapter::AsyncWSAdapter() :
event(CreateEventW(nullptr, FALSE, FALSE, nullptr)),
context{
.callback = &rawCallback,
.callbackState = this},
asyncResult() {
}
util::AsyncWSAdapter::AsyncWSAdapter(AsyncWSAdapter &&moveref) :
event(std::move(moveref.event)),
context{
.callback=&rawCallback,
.callbackState = this},
asyncResult(moveref.asyncResult) {
}
util::AsyncWSAdapter::operator WS_ASYNC_CONTEXT *() {
return &context;
}
winrt::Windows::Foundation::IAsyncAction util::AsyncWSAdapter::checkChannelOperation(HRESULT immediateResult, WS_CHANNEL *channel) {
if(immediateResult != WS_S_ASYNC) {
winrt::check_hresult(immediateResult);
co_return;
}
(co_await winrt::get_cancellation_token()).callback([channel]() {
WsAbortChannel(channel, nullptr);
});
co_await winrt::resume_on_signal(event.get());
if(asyncResult == WS_E_OPERATION_ABORTED) {
asyncResult = ERROR_CANCELLED;
}
winrt::check_hresult(asyncResult);
}
void WINAPI util::AsyncWSAdapter::rawCallback(HRESULT result, WS_CALLBACK_MODEL, void *state) {
AsyncWSAdapter &adapter = *static_cast<AsyncWSAdapter *>(state);
adapter.asyncResult = result;
winrt::check_bool(SetEvent(adapter.event.get()));
}
void util::ChannelDeleter::operator()(WS_CHANNEL *channel) const {
WS_CHANNEL_STATE state = getChannelState(channel);
if(state == WS_CHANNEL_STATE_OPEN || state == WS_CHANNEL_STATE_FAULTED) {
// A channel must be closed before it can be freed. Close, then immediately abort so the close completes immediately.
winrt::handle event(CreateEventW(nullptr, FALSE, FALSE, nullptr));
WS_ASYNC_CONTEXT context = {
.callback = &rawCallback,
.callbackState = event.get(),
};
if(WsCloseChannel(channel, &context, nullptr) == WS_S_ASYNC) {
WsAbortChannel(channel, nullptr);
WaitForSingleObject(event.get(), INFINITE);
}
}
WsFreeChannel(channel);
}
void WINAPI util::ChannelDeleter::rawCallback(HRESULT, WS_CALLBACK_MODEL, void *state) {
HANDLE event = reinterpret_cast<HANDLE>(state);
SetEvent(event);
}
void util::FontDeleter::operator()(HFONT font) const {
DeleteObject(font);
}
void util::ImageListDeleter::operator()(HIMAGELIST imageList) const {
ImageList_Destroy(imageList);
}
void util::HeapDeleter::operator()(WS_HEAP *heap) const {
WsFreeHeap(heap);
}
void util::MessageDeleter::operator()(WS_MESSAGE *message) const {
WsFreeMessage(message);
}
util::WindowClassRegistration::WindowClassRegistration(const WNDCLASSEXW &wc) :
className(wc.lpszClassName),
instance(wc.hInstance) {
winrt::check_bool(RegisterClassExW(&wc));
}
util::WindowClassRegistration::~WindowClassRegistration() {
UnregisterClassW(className, instance);
}
std::unique_ptr<HFONT, util::FontDeleter> util::createMessageBoxFont(unsigned int size, unsigned int dpi) {
NONCLIENTMETRICSW ncm{};
ncm.cbSize = sizeof(ncm);
winrt::check_bool(SystemParametersInfoW(SPI_GETNONCLIENTMETRICS, sizeof(ncm), &ncm, 0));
LOGFONT fontSpec = ncm.lfMessageFont;
fontSpec.lfHeight = -MulDiv(size, dpi, 72);
fontSpec.lfWidth = 0;
HFONT font = CreateFontIndirectW(&fontSpec);
if(!font) {
winrt::throw_last_error();
}
return std::unique_ptr<HFONT, util::FontDeleter>(font);
}
HWND util::createWindowEx(DWORD exStyle, const wchar_t *className, const wchar_t *windowName, DWORD style, int x, int y, int width, int height, HWND parent, HMENU menu, HINSTANCE instance, void *param) {
HWND ret = CreateWindowEx(exStyle, className, windowName, style, x, y, width, height, parent, menu, instance, param);
if(!ret) {
winrt::throw_last_error();
}
return ret;
}
HICON util::loadIconWithScaleDown(HINSTANCE instance, const wchar_t *name, int width, int height) {
HICON ret = nullptr;
HRESULT result = LoadIconWithScaleDown(instance, name, width, height, &ret);
if(SUCCEEDED(result)) {
return ret;
} else {
winrt::throw_hresult(result);
}
}
HANDLE util::loadImage(HINSTANCE instance, const wchar_t *name, unsigned int type, int width, int height, unsigned int options) {
HANDLE ret = LoadImageW(instance, name, type, width, height, options);
if(!ret) {
winrt::throw_last_error();
}
return ret;
}
std::wstring util::loadString(HINSTANCE instance, unsigned int id) {
return std::wstring(loadStringView(instance, id));
}
std::wstring_view util::loadStringView(HINSTANCE instance, unsigned int id) {
wchar_t *ptr;
int len = LoadStringW(instance, id, reinterpret_cast<wchar_t *>(&ptr), 0);
if(!len) {
winrt::throw_last_error();
}
return std::wstring_view(ptr, ptr + len);
}
std::wstring util::loadAndFormatString(HINSTANCE instance, unsigned int id, ...) {
const std::wstring &messageTemplate = loadString(instance, id);
std::wstring ret(65536, '\0');
std::va_list args;
va_start(args, id);
DWORD len = FormatMessageW(FORMAT_MESSAGE_FROM_STRING, messageTemplate.c_str(), 0, 0, ret.data(), ret.size(), &args);
va_end(args);
if(len) {
ret.resize(len);
ret.shrink_to_fit();
return ret;
} else {
winrt::throw_last_error();
}
}