Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/common/logging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,11 @@ namespace display_device {
namespace {
std::tm threadSafeLocaltime(const std::time_t &time) {
#if defined(_MSC_VER) // MSVCRT (2005+): std::localtime is threadsafe
const auto tm_ptr {std::localtime(&time)};
if (const auto tm_ptr {std::localtime(&time)}; tm_ptr) {
#else // POSIX
std::tm buffer;
const auto tm_ptr {localtime_r(&time, &buffer)};
if (const auto tm_ptr {localtime_r(&time, &buffer)}; tm_ptr) {
#endif // _MSC_VER
if (tm_ptr) {
return *tm_ptr;
}
return {};
Expand Down
4 changes: 2 additions & 2 deletions src/common/types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ namespace display_device {
}

// ---- Verify fixed header
static const std::vector fixed_header {std::byte {0x00}, std::byte {0xFF}, std::byte {0xFF}, std::byte {0xFF}, std::byte {0xFF}, std::byte {0xFF}, std::byte {0xFF}, std::byte {0x00}};
if (!std::equal(std::begin(fixed_header), std::end(fixed_header), std::begin(data))) {
if (static const std::array fixed_header {std::byte {0x00}, std::byte {0xFF}, std::byte {0xFF}, std::byte {0xFF}, std::byte {0xFF}, std::byte {0xFF}, std::byte {0xFF}, std::byte {0x00}};
!std::equal(std::begin(fixed_header), std::end(fixed_header), std::begin(data))) {
DD_LOG(warning) << "EDID data does not contain fixed header.";
return std::nullopt;
}
Expand Down
14 changes: 6 additions & 8 deletions src/windows/settings_manager_apply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ namespace display_device {
boost::scope::scope_exit topology_prep_guard {[this, topology = topology_before_changes, was_captured = m_audio_context_api->isCaptured(), &release_context]() {
// It is possible that during topology preparation, some settings will be reverted for the modified topology.
// To keel it simple, these settings will not be restored!
const auto result {m_dd_api->setTopology(topology)};
if (!result) {
if (const auto result {m_dd_api->setTopology(topology)}; !result) {
DD_LOG(error) << "Failed to revert back to topology in the topology guard!";
if (release_context) {
// We are currently in the topology for which the context was captured.
Expand Down Expand Up @@ -240,9 +239,8 @@ namespace display_device {

if (ensure_primary) {
const auto original_primary_device {cached_primary_device.empty() ? current_primary_device : cached_primary_device};
const auto &new_primary_device {device_to_configure};

if (!try_change(new_primary_device, "Changing primary display to:\n", "Failed to apply new configuration, because a new primary device could not be set!")) {
if (const auto &new_primary_device {device_to_configure}; !try_change(new_primary_device, "Changing primary display to:\n", "Failed to apply new configuration, because a new primary device could not be set!")) {
// Error already logged
return false;
}
Expand Down Expand Up @@ -301,9 +299,9 @@ namespace display_device {
if (change_required) {
const bool configuring_primary_devices {config.m_device_id.empty()};
const auto original_display_modes {cached_display_modes.empty() ? current_display_modes : cached_display_modes};
const auto new_display_modes {win_utils::computeNewDisplayModes(config.m_resolution, config.m_refresh_rate, configuring_primary_devices, device_to_configure, additional_devices_to_configure, original_display_modes)};

if (!try_change(new_display_modes, "Changing display modes to:\n", "Failed to apply new configuration, because new display modes could not be set!")) {
if (const auto new_display_modes {win_utils::computeNewDisplayModes(config.m_resolution, config.m_refresh_rate, configuring_primary_devices, device_to_configure, additional_devices_to_configure, original_display_modes)};
!try_change(new_display_modes, "Changing display modes to:\n", "Failed to apply new configuration, because new display modes could not be set!")) {
// Error already logged
return false;
}
Expand Down Expand Up @@ -357,9 +355,9 @@ namespace display_device {
if (change_required) {
const bool configuring_primary_devices {config.m_device_id.empty()};
const auto original_hdr_states {cached_hdr_states.empty() ? current_hdr_states : cached_hdr_states};
const auto new_hdr_states {win_utils::computeNewHdrStates(config.m_hdr_state, configuring_primary_devices, device_to_configure, additional_devices_to_configure, original_hdr_states)};

if (!try_change(new_hdr_states, "Changing HDR states to:\n", "Failed to apply new configuration, because new HDR states could not be set!")) {
if (const auto new_hdr_states {win_utils::computeNewHdrStates(config.m_hdr_state, configuring_primary_devices, device_to_configure, additional_devices_to_configure, original_hdr_states)};
!try_change(new_hdr_states, "Changing HDR states to:\n", "Failed to apply new configuration, because new HDR states could not be set!")) {
// Error already logged
return false;
}
Expand Down
3 changes: 1 addition & 2 deletions src/windows/settings_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -315,8 +315,7 @@ namespace display_device::win_utils {

if (hdr_state) {
const auto try_update_new_state = [&new_states, &hdr_state](const std::string &device_id) {
const auto current_state {new_states[device_id]};
if (!current_state) {
if (const auto current_state {new_states[device_id]}; !current_state) {
return;
}

Expand Down
18 changes: 6 additions & 12 deletions src/windows/win_api_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,7 @@ namespace display_device {
target_name.header.type = DISPLAYCONFIG_DEVICE_INFO_GET_TARGET_NAME;
target_name.header.size = sizeof(target_name);

LONG result {DisplayConfigGetDeviceInfo(&target_name.header)};
if (result != ERROR_SUCCESS) {
if (LONG result {DisplayConfigGetDeviceInfo(&target_name.header)}; result != ERROR_SUCCESS) {
DD_LOG(error) << w_api.getErrorString(result) << " failed to get target device name!";
return {};
}
Expand Down Expand Up @@ -252,8 +251,7 @@ namespace display_device {
std::optional<std::tuple<std::wstring, std::vector<std::byte>>> getInstanceIdAndEdid(const WinApiLayerInterface &w_api, const std::wstring &device_path) {
static const GUID monitor_guid {0xe6f07b5f, 0xee97, 0x4a90, {0xb0, 0x76, 0x33, 0xf5, 0x7b, 0xf4, 0xea, 0xa7}};

HDEVINFO dev_info_handle {SetupDiGetClassDevsW(&monitor_guid, nullptr, nullptr, DIGCF_DEVICEINTERFACE)};
if (dev_info_handle) {
if (HDEVINFO dev_info_handle {SetupDiGetClassDevsW(&monitor_guid, nullptr, nullptr, DIGCF_DEVICEINTERFACE)}; dev_info_handle) {
const auto dev_info_handle_cleanup {
boost::scope::scope_exit([&dev_info_handle, &w_api]() {
if (!SetupDiDestroyDeviceInfoList(dev_info_handle)) {
Expand Down Expand Up @@ -353,8 +351,7 @@ namespace display_device {
condition_mask = VerSetConditionMask(condition_mask, VER_MINORVERSION, VER_GREATER_EQUAL); // Minor version condition
condition_mask = VerSetConditionMask(condition_mask, VER_BUILDNUMBER, VER_GREATER_EQUAL); // Build number condition

BOOL result {VerifyVersionInfoA(&os_version_info, VER_MAJORVERSION | VER_MINORVERSION | VER_BUILDNUMBER, condition_mask)};
if (result == FALSE) {
if (BOOL result {VerifyVersionInfoA(&os_version_info, VER_MAJORVERSION | VER_MINORVERSION | VER_BUILDNUMBER, condition_mask)}; result == FALSE) {
DD_LOG(verbose) << w_api.getErrorString(static_cast<LONG>(GetLastError())) << " \"is_W11_24H2_OrAbove\" returned false.";
return false;
}
Expand Down Expand Up @@ -444,8 +441,7 @@ namespace display_device {
}

std::vector<std::byte> device_id_data;
auto instance_id_and_edid {getInstanceIdAndEdid(*this, device_path)};
if (instance_id_and_edid) {
if (auto instance_id_and_edid {getInstanceIdAndEdid(*this, device_path)}; instance_id_and_edid) {
// Instance ID is unique in the system and persists restarts, but not driver re-installs.
// It looks like this:
// DISPLAY\ACI27EC\5&4FD2DE4&5&UID4352 (also used in the device path it seems)
Expand Down Expand Up @@ -556,8 +552,7 @@ namespace display_device {
source_name.header.type = DISPLAYCONFIG_DEVICE_INFO_GET_SOURCE_NAME;
source_name.header.size = sizeof(source_name);

LONG result {DisplayConfigGetDeviceInfo(&source_name.header)};
if (result != ERROR_SUCCESS) {
if (LONG result {DisplayConfigGetDeviceInfo(&source_name.header)}; result != ERROR_SUCCESS) {
DD_LOG(error) << getErrorString(result) << " failed to get display name!";
return {};
}
Expand Down Expand Up @@ -661,8 +656,7 @@ namespace display_device {
return FALSE;
}

MONITORINFOEXA monitor_info {sizeof(MONITORINFOEXA)};
if (GetMonitorInfoA(monitor, &monitor_info)) {
if (MONITORINFOEXA monitor_info {sizeof(MONITORINFOEXA)}; GetMonitorInfoA(monitor, &monitor_info)) {
if (data->m_display_name == monitor_info.szDevice) {
data->m_width = monitor_info.rcMonitor.right - monitor_info.rcMonitor.left;
return FALSE;
Expand Down
15 changes: 5 additions & 10 deletions src/windows/win_api_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,7 @@ namespace display_device::win_utils {
return std::nullopt;
}

const auto display_name {w_api.getDisplayName(path)};
if (display_name.empty()) {
if (const auto display_name {w_api.getDisplayName(path)}; display_name.empty()) {
return std::nullopt;
}

Expand Down Expand Up @@ -239,8 +238,7 @@ namespace display_device::win_utils {
continue;
}

const auto prev_device_id_for_path_it {paths_to_ids.find(device_info->m_device_path)};
if (prev_device_id_for_path_it != std::end(paths_to_ids)) {
if (const auto prev_device_id_for_path_it {paths_to_ids.find(device_info->m_device_path)}; prev_device_id_for_path_it != std::end(paths_to_ids)) {
if (prev_device_id_for_path_it->second != device_info->m_device_id) {
DD_LOG(error) << "Duplicate display device id found: " << device_info->m_device_id << " (device path: " << device_info->m_device_path << ")";
return {};
Expand All @@ -256,8 +254,7 @@ namespace display_device::win_utils {
paths_to_ids[device_info->m_device_path] = device_info->m_device_id;
}

auto path_data_it {path_data.find(device_info->m_device_id)};
if (path_data_it != std::end(path_data)) {
if (auto path_data_it {path_data.find(device_info->m_device_id)}; path_data_it != std::end(path_data)) {
if (path_data_it->second.m_adapter_id != path.sourceInfo.adapterId) {
// Sanity check, should not be possible since adapter in embedded in the device path
DD_LOG(error) << "Device path " << device_info->m_device_path << " has different adapters!";
Expand Down Expand Up @@ -297,8 +294,7 @@ namespace display_device::win_utils {
UINT32 group_id {0};
StringUnorderedMap<std::unordered_set<UINT32>> used_source_ids_per_adapter;
const auto is_source_id_already_used = [&used_source_ids_per_adapter](const LUID &adapter_id, UINT32 source_id) {
auto entry_it {used_source_ids_per_adapter.find(toString(adapter_id))};
if (entry_it != std::end(used_source_ids_per_adapter)) {
if (auto entry_it {used_source_ids_per_adapter.find(toString(adapter_id))}; entry_it != std::end(used_source_ids_per_adapter)) {
return entry_it->second.contains(source_id);
}

Expand All @@ -308,8 +304,7 @@ namespace display_device::win_utils {
for (const auto &group : new_topology) {
StringUnorderedMap<UINT32> used_source_ids_per_adapter_per_group;
const auto get_already_used_source_id_in_group = [&used_source_ids_per_adapter_per_group](const LUID &adapter_id) -> std::optional<UINT32> {
auto entry_it {used_source_ids_per_adapter_per_group.find(toString(adapter_id))};
if (entry_it != std::end(used_source_ids_per_adapter_per_group)) {
if (auto entry_it {used_source_ids_per_adapter_per_group.find(toString(adapter_id))}; entry_it != std::end(used_source_ids_per_adapter_per_group)) {
return entry_it->second;
}

Expand Down
3 changes: 1 addition & 2 deletions src/windows/win_display_device_hdr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,7 @@ namespace display_device {
return {};
}

HdrStateMapNoOpt changed_states;
if (!doSetHdrStates(*m_w_api, *display_data, states_without_opt, &changed_states)) {
if (HdrStateMapNoOpt changed_states; !doSetHdrStates(*m_w_api, *display_data, states_without_opt, &changed_states)) {
if (!changed_states.empty()) {
doSetHdrStates(*m_w_api, *display_data, changed_states, nullptr); // return value does not matter
}
Expand Down
6 changes: 2 additions & 4 deletions src/windows/win_display_device_modes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,7 @@ namespace display_device {
flags |= SDC_ALLOW_CHANGES;
}

const LONG result {w_api.setDisplayConfig(display_data->m_paths, display_data->m_modes, flags)};
if (result != ERROR_SUCCESS) {
if (const LONG result {w_api.setDisplayConfig(display_data->m_paths, display_data->m_modes, flags)}; result != ERROR_SUCCESS) {
DD_LOG(error) << w_api.getErrorString(result) << " failed to set display mode!";
return false;
}
Expand Down Expand Up @@ -203,8 +202,7 @@ namespace display_device {
return true;
};

auto current_modes {getCurrentDisplayModes(device_ids)};
if (!current_modes.empty()) {
if (auto current_modes {getCurrentDisplayModes(device_ids)}; !current_modes.empty()) {
if (all_modes_match(current_modes)) {
return true;
}
Expand Down
3 changes: 1 addition & 2 deletions src/windows/win_display_device_primary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,7 @@ namespace display_device {
}

const UINT32 flags {SDC_APPLY | SDC_USE_SUPPLIED_DISPLAY_CONFIG | SDC_SAVE_TO_DATABASE | SDC_VIRTUAL_MODE_AWARE};
const LONG result {m_w_api->setDisplayConfig(display_data->m_paths, display_data->m_modes, flags)};
if (result != ERROR_SUCCESS) {
if (const LONG result {m_w_api->setDisplayConfig(display_data->m_paths, display_data->m_modes, flags)}; result != ERROR_SUCCESS) {
DD_LOG(error) << m_w_api->getErrorString(result) << " failed to set primary mode for " << device_id << "!";
return false;
}
Expand Down
6 changes: 2 additions & 4 deletions src/windows/win_display_device_topology.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ namespace display_device {
}

UINT32 flags {SDC_APPLY | SDC_TOPOLOGY_SUPPLIED | SDC_ALLOW_PATH_ORDER_CHANGES | SDC_VIRTUAL_MODE_AWARE};
LONG result {w_api.setDisplayConfig(paths, {}, flags)};
if (result == ERROR_GEN_FAILURE) {
if (LONG result {w_api.setDisplayConfig(paths, {}, flags)}; result == ERROR_GEN_FAILURE) {
DD_LOG(warning) << w_api.getErrorString(result) << " failed to change topology using the topology from Windows DB! Asking Windows to create the topology.";

flags = SDC_APPLY | SDC_USE_SUPPLIED_DISPLAY_CONFIG | SDC_ALLOW_CHANGES /* This flag is probably not needed, but who knows really... (not MSDOCS at least) */ | SDC_VIRTUAL_MODE_AWARE | SDC_SAVE_TO_DATABASE;
Expand Down Expand Up @@ -160,8 +159,7 @@ namespace display_device {
}

if (doSetTopology(*m_w_api, new_topology, *original_data)) {
const auto updated_topology {getCurrentTopology()};
if (isTopologyValid(updated_topology)) {
if (const auto updated_topology {getCurrentTopology()}; isTopologyValid(updated_topology)) {
if (isTopologyTheSame(new_topology, updated_topology)) {
return true;
} else {
Expand Down
3 changes: 1 addition & 2 deletions tests/fixtures/fixtures.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ const std::vector<std::string> &BaseTest::getArgs() const {
}

std::optional<std::string> BaseTest::getArgWithMatchingPattern(const std::string &pattern, bool remove_match) const {
const auto &args {getArgs()};
if (!args.empty()) {
if (const auto &args {getArgs()}; !args.empty()) {
const std::regex re_pattern {pattern};

// We are skipping the first arg which is always binary name/path.
Expand Down
3 changes: 1 addition & 2 deletions tests/fixtures/test_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ namespace ut_consts {

bool testRegex(const std::string &input, const std::string &pattern) {
std::regex regex(pattern);
std::smatch match;
if (!std::regex_match(input, match, regex)) {
if (std::smatch match; !std::regex_match(input, match, regex)) {
std::cout << "Regex test failed:\n"
<< " Input : " << input << "\n"
<< " Pattern: " << pattern << std::endl;
Expand Down
Loading