Skip to content

Commit

Permalink
feat(win_api_layer): add implicit flag validation for setDisplayConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
FrogTheFrog committed Jan 12, 2025
1 parent 2c431bc commit 1722b6e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ namespace display_device {
/**
* @brief Direct wrapper around the SetDisplayConfig WinAPI.
*
* It implements no additional logic, just a direct pass-trough.
* It implements additional implicit validation logic.
*
* @param paths List of paths to pass.
* @param modes List of modes to pass.
Expand Down
28 changes: 21 additions & 7 deletions src/windows/win_api_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -536,13 +536,27 @@ namespace display_device {

LONG
WinApiLayer::setDisplayConfig(std::vector<DISPLAYCONFIG_PATH_INFO> paths, std::vector<DISPLAYCONFIG_MODE_INFO> modes, UINT32 flags) {
// std::vector::data() "may or may not return a null pointer, if size() is 0", therefore we want to enforce nullptr...
return ::SetDisplayConfig(
paths.size(),
paths.empty() ? nullptr : paths.data(),
modes.size(),
modes.empty() ? nullptr : modes.data(),
flags);
const auto do_set_display_config { [&paths, &modes](UINT32 flags) {
// std::vector::data() "may or may not return a null pointer, if size() is 0", therefore we want to enforce nullptr...
return ::SetDisplayConfig(
paths.size(),
paths.empty() ? nullptr : paths.data(),
modes.size(),
modes.empty() ? nullptr : modes.data(),
flags);
} };

if (flags & SDC_APPLY) {
auto validation_flags { flags };
validation_flags &= ~SDC_APPLY;
validation_flags |= SDC_VALIDATE;

if (const auto result = do_set_display_config(validation_flags); result != ERROR_SUCCESS) {
return result;
}
}

return do_set_display_config(flags);
}

std::optional<HdrState>
Expand Down

0 comments on commit 1722b6e

Please sign in to comment.