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"
16hi_export_module(hikogui.settings.os_settings : impl);
18hi_export
namespace hi {
inline namespace v1 {
25 if (actual_policy == hi::policy::unspecified) {
26 actual_policy = performance_policy;
28 if (actual_policy == hi::policy::unspecified) {
31 auto const 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 :
33 DXGI_GPU_PREFERENCE_UNSPECIFIED;
35 IDXGIFactory *factory =
nullptr;
36 if (FAILED(CreateDXGIFactory(__uuidof(IDXGIFactory), (
void **)&factory))) {
40 hi_assert_not_null(factory);
41 auto const d1 = defer([&] {
45 IDXGIFactory6 *factory6 =
nullptr;
46 if (FAILED(factory->QueryInterface(__uuidof(IDXGIFactory6), (
void **)&factory6))) {
50 hi_assert_not_null(factory6);
51 auto const d2 = defer([&] {
55 IDXGIAdapter1 *adapter =
nullptr;
57 SUCCEEDED(factory6->EnumAdapterByGpuPreference(i, actual_policy_, __uuidof(IDXGIAdapter1), (
void **)&adapter));
59 auto const d3 = defer([&] {
63 DXGI_ADAPTER_DESC1 description;
64 if (FAILED(adapter->GetDesc1(&description))) {
69 static_assert(
sizeof(description.AdapterLuid) <=
sizeof(uuid));
96 HKEY_CURRENT_USER,
"Control Panel\\International\\User Profile",
"Languages")) {
97 r.reserve(languages->size());
98 for (
auto const& language : *languages) {
99 r.push_back(language_tag{language});
102 hi_log_error(
"Could not read languages: {}", std::error_code{languages.error()}.message());
109[[nodiscard]] hi_inline std::expected<std::locale, std::error_code> os_settings::gather_locale() noexcept
111 if (
auto name = win32_GetUserDefaultLocaleName()) {
112 return std::locale(*name);
119[[nodiscard]] hi_inline
bool os_settings::gather_left_to_right() noexcept
121 if (
auto locale = gather_locale()) {
124 auto locale_name = locale->name();
127 if (
auto i = locale_name.find(
'.'); i != locale_name.npos) {
128 locale_name = locale_name.substr(0, i);
144 if (
auto languages = gather_languages(); not languages.empty()) {
145 return languages.front().expand().left_to_right();
152[[nodiscard]] hi_inline hi::theme_mode os_settings::gather_theme_mode()
156 "Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize",
157 "AppsUseLightTheme")) {
158 return *result ? theme_mode::light : theme_mode::dark;
161 hi_log_error(
"Could not read theme mode: {}", std::error_code{result.error()}.message());
162 return theme_mode::light;
166[[nodiscard]] hi_inline hi::subpixel_orientation os_settings::gather_subpixel_orientation()
169 BOOL has_font_smoothing;
170 if (not SystemParametersInfoW(SPI_GETFONTSMOOTHING, 0, &has_font_smoothing, 0)) {
174 if (has_font_smoothing == FALSE) {
176 return hi::subpixel_orientation::unknown;
181 UINT font_smooth_type;
182 if (not SystemParametersInfoW(SPI_GETFONTSMOOTHINGTYPE, 0, &font_smooth_type, 0)) {
186 if (font_smooth_type != FE_FONTSMOOTHINGCLEARTYPE) {
188 return hi::subpixel_orientation::unknown;
194 if (not SystemParametersInfoW(SPI_GETCLEARTYPE, 0, &has_clear_type, 0)) {
198 if (has_clear_type == FALSE) {
200 return hi::subpixel_orientation::unknown;
205 UINT font_smooth_orientation;
206 if (not SystemParametersInfoW(SPI_GETFONTSMOOTHINGORIENTATION, 0, &font_smooth_orientation, 0)) {
208 std::format(
"Could not get system parameter SPI_GETFONTSMOOTHINGORIENTATION: {}",
get_last_error_message()));
211 if (font_smooth_orientation == FE_FONTSMOOTHINGORIENTATIONBGR) {
213 return hi::subpixel_orientation::horizontal_bgr;
214 }
else if (font_smooth_orientation == FE_FONTSMOOTHINGORIENTATIONRGB) {
216 return hi::subpixel_orientation::horizontal_rgb;
218 throw os_error(std::format(
"Unknown result from SPI_GETFONTSMOOTHINGORIENTATION: {}", font_smooth_orientation));
223[[nodiscard]] hi_inline
bool os_settings::gather_uniform_HDR()
230[[nodiscard]] hi_inline std::chrono::milliseconds os_settings::gather_double_click_interval()
232 return std::chrono::milliseconds{GetDoubleClickTime()};
235[[nodiscard]] hi_inline
float os_settings::gather_double_click_distance()
237 auto const width = GetSystemMetrics(SM_CXDOUBLECLK);
239 throw os_error(
"Could not retrieve SM_CXDOUBLECLK");
242 auto const height = GetSystemMetrics(SM_CYDOUBLECLK);
244 throw os_error(
"Could not retrieve SM_CYDOUBLECLK");
247 auto const diameter =
std::max(width, height);
248 return diameter * 0.5f;
251[[nodiscard]] hi_inline std::chrono::milliseconds os_settings::gather_keyboard_repeat_delay()
253 using namespace std::literals::chrono_literals;
256 if (not SystemParametersInfoW(SPI_GETKEYBOARDDELAY, 0, &r, 0)) {
261 constexpr auto bias = 250ms;
262 constexpr auto gain = 250ms;
264 return bias + r * gain;
267[[nodiscard]] hi_inline std::chrono::milliseconds os_settings::gather_keyboard_repeat_interval()
269 using namespace std::literals::chrono_literals;
272 if (not SystemParametersInfoW(SPI_GETKEYBOARDSPEED, 0, &r, 0)) {
277 constexpr auto bias = 2.5f;
278 constexpr auto gain = 0.887f;
279 auto const rate = bias + r * gain;
283[[nodiscard]] hi_inline std::chrono::milliseconds os_settings::gather_cursor_blink_interval()
285 using namespace std::literals::chrono_literals;
287 auto const r = GetCaretBlinkTime();
291 }
else if (r == INFINITE) {
296 return std::chrono::milliseconds{r} * 2;
300[[nodiscard]] hi_inline std::chrono::milliseconds os_settings::gather_cursor_blink_delay()
303 return std::max(gather_keyboard_repeat_delay(), gather_keyboard_repeat_interval());
306[[nodiscard]] hi_inline
float os_settings::gather_minimum_window_width()
308 auto const width = GetSystemMetrics(SM_CXMINTRACK);
310 throw os_error(
"Could not retrieve SM_CXMINTRACK");
315[[nodiscard]] hi_inline
float os_settings::gather_minimum_window_height()
317 auto const height = GetSystemMetrics(SM_CYMINTRACK);
319 throw os_error(
"Could not retrieve SM_CYMINTRACK");
325[[nodiscard]] hi_inline
float os_settings::gather_maximum_window_width()
327 auto const width = GetSystemMetrics(SM_CXMAXTRACK);
329 throw os_error(
"Could not retrieve SM_CXMAXTRACK");
334[[nodiscard]] hi_inline
float os_settings::gather_maximum_window_height()
336 auto const height = GetSystemMetrics(SM_CYMAXTRACK);
338 throw os_error(
"Could not retrieve SM_CYMAXTRACK");
344[[nodiscard]] hi_inline uintptr_t os_settings::gather_primary_monitor_id()
346 auto const origin = POINT{0, 0};
347 auto const monitor = MonitorFromPoint(origin, MONITOR_DEFAULTTOPRIMARY);
348 return std::bit_cast<uintptr_t>(monitor);
351[[nodiscard]] hi_inline
aarectangle os_settings::gather_primary_monitor_rectangle()
353 auto const width = GetSystemMetrics(SM_CXSCREEN);
355 throw os_error(
"Could not retrieve SM_CXSCREEN");
358 auto const height = GetSystemMetrics(SM_CYSCREEN);
360 throw os_error(
"Could not retrieve SM_CYSCREEN");
367[[nodiscard]] hi_inline
aarectangle os_settings::gather_desktop_rectangle()
369 auto const primary_monitor_height = GetSystemMetrics(SM_CYSCREEN);
370 if (primary_monitor_height == 0) {
371 throw os_error(
"Could not retrieve SM_CYSCREEN");
374 auto const left = GetSystemMetrics(SM_XVIRTUALSCREEN);
375 auto const top = GetSystemMetrics(SM_YVIRTUALSCREEN);
377 auto const width = GetSystemMetrics(SM_CXVIRTUALSCREEN);
379 throw os_error(
"Could not retrieve SM_CXVIRTUALSCREEN");
382 auto const height = GetSystemMetrics(SM_CYVIRTUALSCREEN);
384 throw os_error(
"Could not retrieve SM_CYVIRTUALSCREEN");
390 auto const inv_bottom = primary_monitor_height -
bottom;
395[[nodiscard]] hi_inline
policy os_settings::gather_gpu_policy()
397 using namespace std::literals;
400 auto const user_gpu_preferences_key =
"Software\\Microsoft\\DirectX\\UserGpuPreferences";
403 for (
auto entry : std::views::split(std::string_view{*result},
";"sv)) {
404 auto entry_sv = std::string_view{entry};
405 if (entry_sv.starts_with(
"GpuPreference=")) {
406 if (entry_sv.ends_with(
"=0")) {
407 return policy::unspecified;
408 }
else if (entry_sv.ends_with(
"=1")) {
409 return policy::low_power;
410 }
else if (entry_sv.ends_with(
"=2")) {
411 return policy::high_performance;
413 hi_log_error(
"Unexpected GpuPreference value \"{}\".", entry_sv);
414 return policy::unspecified;
419 hi_log_error(
"Could not find GpuPreference entry.");
420 return policy::unspecified;
422 }
else if (result.error() == win32_error::file_not_found) {
423 return policy::unspecified;
426 hi_log_error(
"Could not read gpu profile policy: {}", std::error_code{result.error()}.message());
427 return policy::unspecified;
Rules for working with win32 headers.
@ bottom
Align to the bottom.
Definition alignment.hpp:40
@ top
Align to the top.
Definition alignment.hpp:32
@ left
Align the text to the left side.
Definition alignment.hpp:118
std::filesystem::path executable_file() noexcept
Get the full path to this executable.
The HikoGUI namespace.
Definition recursive_iterator.hpp:15
The HikoGUI API version 1.
Definition recursive_iterator.hpp:16
std::expected< T, win32_error > win32_RegGetValue(HKEY key, std::string_view path, std::string_view name)=delete
Read from the registry value.
Definition winreg.hpp:282
hi_export std::string get_last_error_message()
Get the OS error message from the last error received on this thread.
Definition exception_win32_impl.hpp:30
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:378
Class which represents an axis-aligned rectangle.
Definition aarectangle.hpp:33
A high-level geometric extent.
Definition extent2.hpp:32
The IETF BCP 47 language tag.
Definition language_tag_intf.hpp:30
bool left_to_right() const noexcept
The language direction for this language-tag.
Definition language_tag_intf.hpp:169
language_tag expand() const noexcept
Expand the language tag to include script and language.
Definition language_tag_impl.hpp:2043
static std::vector< uuid > preferred_gpus(hi::policy performance_policy) noexcept
Get a list of GPUs ordered best to worst.
Definition os_settings_win32_impl.hpp:20
static hi::policy gpu_policy() noexcept
Get the policy for selecting a GPU.
Definition os_settings_intf.hpp:258
Exception thrown during an operating system call.
Definition exception_intf.hpp:184
T duration_cast(T... args)