|
| 1 | +// class header include |
| 2 | +#include "displaydevice/windows/windisplaydevice.h" |
| 3 | + |
| 4 | +// system includes |
| 5 | +#include <ranges> |
| 6 | + |
| 7 | +// local includes |
| 8 | +#include "displaydevice/logging.h" |
| 9 | +#include "displaydevice/windows/winapiutils.h" |
| 10 | + |
| 11 | +namespace display_device { |
| 12 | + namespace { |
| 13 | + /** @brief HDR state map without optional values. */ |
| 14 | + using HdrStateMapNoOpt = std::map<std::string, HdrState>; |
| 15 | + |
| 16 | + /** |
| 17 | + * @see setHdrStates for a description as this was split off to reduce cognitive complexity. |
| 18 | + */ |
| 19 | + bool |
| 20 | + doSetHdrStates(WinApiLayerInterface &w_api, const PathAndModeData &display_data, const HdrStateMapNoOpt &states, HdrStateMapNoOpt *changed_states) { |
| 21 | + const auto try_set_state { |
| 22 | + [&w_api, &display_data](const auto &device_id, const auto &state, auto ¤t_state) { |
| 23 | + const auto path { win_utils::getActivePath(w_api, device_id, display_data.m_paths) }; |
| 24 | + if (!path) { |
| 25 | + DD_LOG(error) << "Failed to find device for " << device_id << "!"; |
| 26 | + return false; |
| 27 | + } |
| 28 | + |
| 29 | + const auto current_state_int { w_api.getHdrState(*path) }; |
| 30 | + if (!current_state_int) { |
| 31 | + DD_LOG(error) << "HDR state cannot be changed for " << device_id << "!"; |
| 32 | + return false; |
| 33 | + } |
| 34 | + |
| 35 | + if (state != *current_state_int) { |
| 36 | + if (!w_api.setHdrState(*path, state)) { |
| 37 | + // Error already logged |
| 38 | + return false; |
| 39 | + } |
| 40 | + |
| 41 | + current_state = current_state_int; |
| 42 | + } |
| 43 | + |
| 44 | + return true; |
| 45 | + } |
| 46 | + }; |
| 47 | + |
| 48 | + for (const auto &[device_id, state] : states) { |
| 49 | + std::optional<HdrState> current_state; |
| 50 | + if (try_set_state(device_id, state, current_state)) { |
| 51 | + if (current_state && changed_states != nullptr) { |
| 52 | + (*changed_states)[device_id] = *current_state; |
| 53 | + } |
| 54 | + } |
| 55 | + // If we are undoing changes we don't want to return early and continue regardless of what error we get. |
| 56 | + else if (changed_states != nullptr) { |
| 57 | + return false; |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + return true; |
| 62 | + } |
| 63 | + |
| 64 | + } // namespace |
| 65 | + |
| 66 | + HdrStateMap |
| 67 | + WinDisplayDevice::getCurrentHdrStates(const std::set<std::string> &device_ids) const { |
| 68 | + if (device_ids.empty()) { |
| 69 | + DD_LOG(error) << "Device id set is empty!"; |
| 70 | + return {}; |
| 71 | + } |
| 72 | + |
| 73 | + const auto display_data { m_w_api->queryDisplayConfig(QueryType::Active) }; |
| 74 | + if (!display_data) { |
| 75 | + // Error already logged |
| 76 | + return {}; |
| 77 | + } |
| 78 | + |
| 79 | + HdrStateMap states; |
| 80 | + for (const auto &device_id : device_ids) { |
| 81 | + const auto path { win_utils::getActivePath(*m_w_api, device_id, display_data->m_paths) }; |
| 82 | + if (!path) { |
| 83 | + DD_LOG(error) << "Failed to find device for " << device_id << "!"; |
| 84 | + return {}; |
| 85 | + } |
| 86 | + |
| 87 | + states[device_id] = m_w_api->getHdrState(*path); |
| 88 | + } |
| 89 | + |
| 90 | + return states; |
| 91 | + } |
| 92 | + |
| 93 | + bool |
| 94 | + WinDisplayDevice::setHdrStates(const HdrStateMap &states) { |
| 95 | + if (states.empty()) { |
| 96 | + DD_LOG(error) << "States map is empty!"; |
| 97 | + return false; |
| 98 | + } |
| 99 | + |
| 100 | + HdrStateMapNoOpt states_without_opt; |
| 101 | + std::ranges::copy(states | |
| 102 | + std::ranges::views::filter([](const auto &entry) { return static_cast<bool>(entry.second); }) | |
| 103 | + std::views::transform([](const auto &entry) { return std::make_pair(entry.first, *entry.second); }), |
| 104 | + std::inserter(states_without_opt, std::begin(states_without_opt))); |
| 105 | + |
| 106 | + if (states_without_opt.empty()) { |
| 107 | + // Return early as there is nothing to do... |
| 108 | + return true; |
| 109 | + } |
| 110 | + |
| 111 | + const auto display_data { m_w_api->queryDisplayConfig(QueryType::Active) }; |
| 112 | + if (!display_data) { |
| 113 | + // Error already logged |
| 114 | + return {}; |
| 115 | + } |
| 116 | + |
| 117 | + HdrStateMapNoOpt changed_states; |
| 118 | + if (!doSetHdrStates(*m_w_api, *display_data, states_without_opt, &changed_states)) { |
| 119 | + if (!changed_states.empty()) { |
| 120 | + doSetHdrStates(*m_w_api, *display_data, changed_states, nullptr); // return value does not matter |
| 121 | + } |
| 122 | + return false; |
| 123 | + } |
| 124 | + |
| 125 | + return true; |
| 126 | + } |
| 127 | +} // namespace display_device |
0 commit comments