HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
os_settings_win32_impl.hpp
1// Copyright Take Vos 2022.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
5#pragma once
6
8
9#include "os_settings_intf.hpp"
10#include "../win32/win32.hpp"
11#include "../telemetry/telemetry.hpp"
12#include "../utility/utility.hpp"
13#include "../path/path.hpp"
14#include "../macros.hpp"
15
16hi_export_module(hikogui.settings.os_settings : impl);
17
18namespace hi { inline namespace v1 {
19
20[[nodiscard]] inline std::vector<uuid> os_settings::preferred_gpus(hi::policy performance_policy) noexcept
21{
22 auto r = std::vector<uuid>{};
23
24 auto actual_policy = os_settings::gpu_policy();
25 if (actual_policy == hi::policy::unspecified) {
27 }
28 if (actual_policy == hi::policy::unspecified) {
30 }
31 hilet actual_policy_ = actual_policy == hi::policy::low_power ? DXGI_GPU_PREFERENCE_MINIMUM_POWER :
32 actual_policy == hi::policy::high_performance ? DXGI_GPU_PREFERENCE_HIGH_PERFORMANCE :
34
35 IDXGIFactory *factory = nullptr;
37 hi_log_error("Could not IDXGIFactory. {}", get_last_error_message());
38 return r;
39 }
40 hi_assert_not_null(factory);
41 hilet d1 = defer([&] {
42 factory->Release();
43 });
44
45 IDXGIFactory6 *factory6 = nullptr;
46 if (FAILED(factory->QueryInterface(__uuidof(IDXGIFactory6), (void **)&factory6))) {
47 hi_log_error("Could not IDXGIFactory::QueryInterface(IDXGIFactory6). {}", get_last_error_message());
48 return r;
49 }
50 hi_assert_not_null(factory6);
51 hilet d2 = defer([&] {
52 factory6->Release();
53 });
54
55 IDXGIAdapter1 *adapter = nullptr;
56 for (UINT i = 0;
57 SUCCEEDED(factory6->EnumAdapterByGpuPreference(i, actual_policy_, __uuidof(IDXGIAdapter1), (void **)&adapter));
58 ++i) {
59 hilet d3 = defer([&] {
60 adapter->Release();
61 });
62
64 if (FAILED(adapter->GetDesc1(&description))) {
65 hi_log_error("Could not IDXGIAdapter1::GetDesc1(). {}", get_last_error_message());
66 return r;
67 }
68
69 static_assert(sizeof(description.AdapterLuid) <= sizeof(uuid));
70 r.emplace_back();
71 std::memcpy(std::addressof(r.back()), std::addressof(description.AdapterLuid), sizeof(description.AdapterLuid));
72 }
73
74 return r;
75}
76
86[[nodiscard]] inline std::vector<language_tag> os_settings::gather_languages()
87{
89
90 // The official APIs to get the current languages do not work.
91 // Either they return the languages in a random order.
92 // Or they return only three languages, but not nessarily the first three
93 // Or they do not update at runtime.
94 // The only way that works is to get the registry from the Control Panel application.
95 if (hilet languages = win32_RegGetValue<std::vector<std::string>>(
96 HKEY_CURRENT_USER, "Control Panel\\International\\User Profile", "Languages")) {
97 r.reserve(languages->size());
98 for (hilet& language : *languages) {
99 r.push_back(language_tag{language});
100 }
101 } else {
102 hi_log_error("Could not read languages: {}", std::error_code{languages.error()}.message());
103 r.push_back(language_tag{"en"});
104 }
105
106 return r;
107}
108
109[[nodiscard]] inline std::expected<std::locale, std::error_code> os_settings::gather_locale() noexcept
110{
111 if (auto name = win32_GetUserDefaultLocaleName()) {
112 return std::locale(*name);
113
114 } else {
115 return std::unexpected{std::error_code{name.error()}};
116 }
117}
118
119[[nodiscard]] inline hi::theme_mode os_settings::gather_theme_mode()
120{
123 "Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize",
124 "AppsUseLightTheme")) {
125 return *result ? theme_mode::light : theme_mode::dark;
126
127 } else {
128 hi_log_error("Could not read theme mode: {}", std::error_code{result.error()}.message());
129 return theme_mode::light;
130 }
131}
132
133[[nodiscard]] inline hi::subpixel_orientation os_settings::gather_subpixel_orientation()
134{
135 {
138 throw os_error(std::format("Could not get system parameter SPI_GETFONTSMOOTHING: {}", get_last_error_message()));
139 }
140
141 if (has_font_smoothing == FALSE) {
142 // Font smoothing is disabled.
143 return hi::subpixel_orientation::unknown;
144 }
145 }
146
147 {
150 throw os_error(std::format("Could not get system parameter SPI_GETFONTSMOOTHINGTYPE: {}", get_last_error_message()));
151 }
152
154 // Font smoothing is not clear type.
155 return hi::subpixel_orientation::unknown;
156 }
157 }
158
159 {
162 throw os_error(std::format("Could not get system parameter SPI_GETCLEARTYPE: {}", get_last_error_message()));
163 }
164
165 if (has_clear_type == FALSE) {
166 // ClearType is disabled.
167 return hi::subpixel_orientation::unknown;
168 }
169 }
170
171 {
174 throw os_error(
175 std::format("Could not get system parameter SPI_GETFONTSMOOTHINGORIENTATION: {}", get_last_error_message()));
176 }
177
179 // Font smoothing is not clear type.
180 return hi::subpixel_orientation::horizontal_bgr;
182 // Font smoothing is not clear type.
183 return hi::subpixel_orientation::horizontal_rgb;
184 } else {
185 throw os_error(std::format("Unknown result from SPI_GETFONTSMOOTHINGORIENTATION: {}", font_smooth_orientation));
186 }
187 }
188}
189
190[[nodiscard]] inline bool os_settings::gather_uniform_HDR()
191{
192 // Microsoft Windows 10 switches display mode when getting a HDR surface
193 // The switching causes all application to display using a different color and brightness calibration.
194 return false;
195}
196
197[[nodiscard]] inline std::chrono::milliseconds os_settings::gather_double_click_interval()
198{
200}
201
202[[nodiscard]] inline float os_settings::gather_double_click_distance()
203{
204 hilet width = GetSystemMetrics(SM_CXDOUBLECLK);
205 if (width <= 0) {
206 throw os_error("Could not retrieve SM_CXDOUBLECLK");
207 }
208
209 hilet height = GetSystemMetrics(SM_CYDOUBLECLK);
210 if (height <= 0) {
211 throw os_error("Could not retrieve SM_CYDOUBLECLK");
212 }
213
214 hilet diameter = std::max(width, height);
215 return diameter * 0.5f;
216}
217
218[[nodiscard]] inline std::chrono::milliseconds os_settings::gather_keyboard_repeat_delay()
219{
220 using namespace std::literals::chrono_literals;
221
222 INT r;
224 throw os_error(std::format("Could not get system parameter SPI_GETKEYBOARDDELAY: {}", get_last_error_message()));
225 }
226
227 // SPI_GETKEYBOARDDELAY values are between 0 (250ms) to 3 (1s).
228 constexpr auto bias = 250ms;
229 constexpr auto gain = 250ms;
230
231 return bias + r * gain;
232}
233
234[[nodiscard]] inline std::chrono::milliseconds os_settings::gather_keyboard_repeat_interval()
235{
236 using namespace std::literals::chrono_literals;
237
238 INT r;
240 throw os_error(std::format("Could not get system parameter SPI_GETKEYBOARDSPEED: {}", get_last_error_message()));
241 }
242
243 // SPI_GETKEYBOARDSPEED values are between 0 (2.5 per/sec) to 31 (30 per/sec).
244 constexpr auto bias = 2.5f;
245 constexpr auto gain = 0.887f;
246 hilet rate = bias + r * gain;
247 return std::chrono::duration_cast<std::chrono::milliseconds>(1000ms / rate);
248}
249
250[[nodiscard]] inline std::chrono::milliseconds os_settings::gather_cursor_blink_interval()
251{
252 using namespace std::literals::chrono_literals;
253
254 hilet r = GetCaretBlinkTime();
255 if (r == 0) {
256 throw os_error(std::format("Could not get caret blink time: {}", get_last_error_message()));
257
258 } else if (r == INFINITE) {
260
261 } else {
262 // GetGaretBlinkTime() gives the time for a half-period.
263 return std::chrono::milliseconds{r} * 2;
264 }
265}
266
267[[nodiscard]] inline std::chrono::milliseconds os_settings::gather_cursor_blink_delay()
268{
269 // The blink delay is not available in the OS, we can use the keyboard repeat delay.
270 return std::max(gather_keyboard_repeat_delay(), gather_keyboard_repeat_interval());
271}
272
273[[nodiscard]] inline float os_settings::gather_minimum_window_width()
274{
275 hilet width = GetSystemMetrics(SM_CXMINTRACK);
276 if (width == 0) {
277 throw os_error("Could not retrieve SM_CXMINTRACK");
278 }
279 return narrow_cast<float>(width);
280}
281
282[[nodiscard]] inline float os_settings::gather_minimum_window_height()
283{
284 hilet height = GetSystemMetrics(SM_CYMINTRACK);
285 if (height == 0) {
286 throw os_error("Could not retrieve SM_CYMINTRACK");
287 }
288
289 return narrow_cast<float>(height);
290}
291
292[[nodiscard]] inline float os_settings::gather_maximum_window_width()
293{
294 hilet width = GetSystemMetrics(SM_CXMAXTRACK);
295 if (width == 0) {
296 throw os_error("Could not retrieve SM_CXMAXTRACK");
297 }
298 return narrow_cast<float>(width);
299}
300
301[[nodiscard]] inline float os_settings::gather_maximum_window_height()
302{
303 hilet height = GetSystemMetrics(SM_CYMAXTRACK);
304 if (height == 0) {
305 throw os_error("Could not retrieve SM_CYMAXTRACK");
306 }
307
308 return narrow_cast<float>(height);
309}
310
311[[nodiscard]] inline uintptr_t os_settings::gather_primary_monitor_id()
312{
313 hilet origin = POINT{0, 0};
315 return std::bit_cast<uintptr_t>(monitor);
316}
317
318[[nodiscard]] inline aarectangle os_settings::gather_primary_monitor_rectangle()
319{
320 hilet width = GetSystemMetrics(SM_CXSCREEN);
321 if (width == 0) {
322 throw os_error("Could not retrieve SM_CXSCREEN");
323 }
324
325 hilet height = GetSystemMetrics(SM_CYSCREEN);
326 if (height == 0) {
327 throw os_error("Could not retrieve SM_CYSCREEN");
328 }
329
330 // The origin of the primary monitor is also the origin of the desktop.
331 return aarectangle{extent2{narrow_cast<float>(width), narrow_cast<float>(height)}};
332}
333
334[[nodiscard]] inline aarectangle os_settings::gather_desktop_rectangle()
335{
337 if (primary_monitor_height == 0) {
338 throw os_error("Could not retrieve SM_CYSCREEN");
339 }
340
343
345 if (width == 0) {
346 throw os_error("Could not retrieve SM_CXVIRTUALSCREEN");
347 }
348
349 hilet height = GetSystemMetrics(SM_CYVIRTUALSCREEN);
350 if (height == 0) {
351 throw os_error("Could not retrieve SM_CYVIRTUALSCREEN");
352 }
353
354 hilet bottom = top + height;
355
356 // Calculate the bottom as compared to a y-axis up coordinate system.
357 hilet inv_bottom = primary_monitor_height - bottom; // 0, 600
358 return aarectangle{
360}
361
362[[nodiscard]] inline policy os_settings::gather_gpu_policy()
363{
364 using namespace std::literals;
365
367 hilet user_gpu_preferences_key = "Software\\Microsoft\\DirectX\\UserGpuPreferences";
368
370 for (auto entry : std::views::split(std::string_view{*result}, ";"sv)) {
371 auto entry_sv = std::string_view{entry};
372 if (entry_sv.starts_with("GpuPreference=")) {
373 if (entry_sv.ends_with("=0")) {
374 return policy::unspecified;
375 } else if (entry_sv.ends_with("=1")) {
376 return policy::low_power;
377 } else if (entry_sv.ends_with("=2")) {
378 return policy::high_performance;
379 } else {
380 hi_log_error("Unexpected GpuPreference value \"{}\".", entry_sv);
381 return policy::unspecified;
382 }
383 }
384 }
385
386 hi_log_error("Could not find GpuPreference entry.");
387 return policy::unspecified;
388
389 } else if (result.error() == win32_error::file_not_found) {
390 return policy::unspecified;
391
392 } else{
393 hi_log_error("Could not read gpu profile policy: {}", std::error_code{result.error()}.message());
394 return policy::unspecified;
395 }
396}
397
398}} // namespace hi::v1
Rules for working with win32 headers.
std::filesystem::path get_path(path_location location)
Get the single and only path.
Definition path_location_intf.hpp:112
@ executable_file
A single file where the current running executable is located.
@ bottom
Align to the bottom.
@ top
Align to the top.
@ left
Align the text to the left side.
STL namespace.
DOXYGEN BUG.
Definition algorithm.hpp:16
geometry/margins.hpp
Definition lookahead_iterator.hpp:5
std::string get_last_error_message()
Get the OS error message from the last error received on this thread.
Definition exception_win32_impl.hpp:31
policy
The performance policy to use.
Definition policy.hpp:18
constexpr Out narrow_cast(In const &rhs) noexcept
Cast numeric values without loss of precision.
Definition cast.hpp:377
T addressof(T... args)
T max(T... args)
T memcpy(T... args)
T message(T... args)
T reserve(T... args)
T unexpected(T... args)