Skip to content

Commit b30c2e1

Browse files
authored
feat: externalize workarounds for Windows (#92)
1 parent 33adf25 commit b30c2e1

23 files changed

+223
-32
lines changed

src/common/include/display_device/detail/json_serializer_details.h

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ namespace nlohmann {
109109
nlohmann_json_t = std::nullopt;
110110
}
111111
else {
112-
nlohmann_json_t = nlohmann_json_j.template get<T>();
112+
nlohmann_json_t = nlohmann_json_j.get<T>();
113113
}
114114
}
115115
};
@@ -138,5 +138,23 @@ namespace nlohmann {
138138
}
139139
}
140140
};
141+
142+
// Specialization for chrono duration.
143+
template <class Rep, class Period>
144+
struct adl_serializer<std::chrono::duration<Rep, Period>> {
145+
using NanoRep = decltype(std::chrono::nanoseconds {}.count());
146+
static_assert(std::numeric_limits<Rep>::max() <= std::numeric_limits<NanoRep>::max(),
147+
"Duration support above nanoseconds have not been tested/verified yet!");
148+
149+
static void
150+
to_json(json &nlohmann_json_j, const std::chrono::duration<Rep, Period> &nlohmann_json_t) {
151+
nlohmann_json_j = nlohmann_json_t.count();
152+
}
153+
154+
static void
155+
from_json(const json &nlohmann_json_j, std::chrono::duration<Rep, Period> &nlohmann_json_t) {
156+
nlohmann_json_t = std::chrono::duration<Rep, Period> { nlohmann_json_j.get<Rep>() };
157+
}
158+
};
141159
} // namespace nlohmann
142160
#endif

src/common/include/display_device/json.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
namespace display_device {
2626
extern const std::optional<unsigned int> JSON_COMPACT;
2727

28+
DD_JSON_DECLARE_CONVERTER(EnumeratedDevice)
2829
DD_JSON_DECLARE_CONVERTER(EnumeratedDeviceList)
2930
DD_JSON_DECLARE_CONVERTER(SingleDisplayConfiguration)
3031
DD_JSON_DECLARE_CONVERTER(std::set<std::string>)

src/common/json.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
// clang-format on
1414

1515
namespace display_device {
16+
DD_JSON_DEFINE_CONVERTER(EnumeratedDevice)
1617
DD_JSON_DEFINE_CONVERTER(EnumeratedDeviceList)
1718
DD_JSON_DEFINE_CONVERTER(SingleDisplayConfiguration)
1819
DD_JSON_DEFINE_CONVERTER(std::set<std::string>)

src/windows/include/display_device/windows/detail/json_serializer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@ namespace display_device {
1414
DD_JSON_DECLARE_SERIALIZE_TYPE(SingleDisplayConfigState::Initial)
1515
DD_JSON_DECLARE_SERIALIZE_TYPE(SingleDisplayConfigState::Modified)
1616
DD_JSON_DECLARE_SERIALIZE_TYPE(SingleDisplayConfigState)
17+
DD_JSON_DECLARE_SERIALIZE_TYPE(WinWorkarounds)
1718
} // namespace display_device
1819
#endif

src/windows/include/display_device/windows/json.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ namespace display_device {
1414
DD_JSON_DECLARE_CONVERTER(DeviceDisplayModeMap)
1515
DD_JSON_DECLARE_CONVERTER(HdrStateMap)
1616
DD_JSON_DECLARE_CONVERTER(SingleDisplayConfigState)
17+
DD_JSON_DECLARE_CONVERTER(WinWorkarounds)
1718
} // namespace display_device

src/windows/include/display_device/windows/settings_manager.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
#pragma once
66

77
// system includes
8-
#include <chrono>
98
#include <memory>
109

1110
// local includes
@@ -25,11 +24,13 @@ namespace display_device {
2524
* @param dd_api A pointer to the Windows Display Device interface. Will throw on nullptr!
2625
* @param audio_context_api [Optional] A pointer to the Audio Context interface.
2726
* @param persistent_state A pointer to a class for managing persistence.
27+
* @param workarounds Workaround settings for the APIs.
2828
*/
2929
explicit SettingsManager(
3030
std::shared_ptr<WinDisplayDeviceInterface> dd_api,
3131
std::shared_ptr<AudioContextInterface> audio_context_api,
32-
std::unique_ptr<PersistentState> persistent_state);
32+
std::unique_ptr<PersistentState> persistent_state,
33+
WinWorkarounds workarounds);
3334

3435
/** For details @see SettingsManagerInterface::enumAvailableDevices */
3536
[[nodiscard]] EnumeratedDeviceList
@@ -115,9 +116,6 @@ namespace display_device {
115116
std::shared_ptr<WinDisplayDeviceInterface> m_dd_api;
116117
std::shared_ptr<AudioContextInterface> m_audio_context_api;
117118
std::unique_ptr<PersistentState> m_persistence_state;
118-
119-
private:
120-
/** @see win_utils::blankHdrStates for more details. */
121-
std::chrono::milliseconds m_hdr_blank_delay { 500 }; // 500ms should be more than enough...
119+
WinWorkarounds m_workarounds;
122120
};
123121
} // namespace display_device

src/windows/include/display_device/windows/settings_utils.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,10 @@ namespace display_device::win_utils {
165165
*
166166
* @param win_dd Interface for interacting with the OS.
167167
* @param delay Delay between OFF and ON states (ON -> OFF -> DELAY -> ON).
168+
* If null optional is provided, the function does nothing.
168169
*/
169170
void
170-
blankHdrStates(WinDisplayDeviceInterface &win_dd, std::chrono::milliseconds delay);
171+
blankHdrStates(WinDisplayDeviceInterface &win_dd, const std::optional<std::chrono::milliseconds> &delay);
171172

172173
/**
173174
* @brief Make guard function for the topology.

src/windows/include/display_device/windows/types.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <windows.h>
1212

1313
// system includes
14+
#include <chrono>
1415
#include <functional>
1516
#include <map>
1617
#include <set>
@@ -171,4 +172,17 @@ namespace display_device {
171172
* @brief Default function type used for cleanup/guard functions.
172173
*/
173174
using DdGuardFn = std::function<void()>;
175+
176+
/**
177+
* @brief Settings for workarounds/hacks for Windows.
178+
*/
179+
struct WinWorkarounds {
180+
std::optional<std::chrono::milliseconds> m_hdr_blank_delay { std::nullopt }; ///< @seealso{win_utils::blankHdrStates for more details.}
181+
182+
/**
183+
* @brief Comparator for strict equality.
184+
*/
185+
friend bool
186+
operator==(const WinWorkarounds &lhs, const WinWorkarounds &rhs);
187+
};
174188
} // namespace display_device

src/windows/json.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@ namespace display_device {
1919
DD_JSON_DEFINE_CONVERTER(DeviceDisplayModeMap)
2020
DD_JSON_DEFINE_CONVERTER(HdrStateMap)
2121
DD_JSON_DEFINE_CONVERTER(SingleDisplayConfigState)
22+
DD_JSON_DEFINE_CONVERTER(WinWorkarounds)
2223
} // namespace display_device

src/windows/json_serializer.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ namespace display_device {
1515
DD_JSON_DEFINE_SERIALIZE_STRUCT(SingleDisplayConfigState::Initial, topology, primary_devices)
1616
DD_JSON_DEFINE_SERIALIZE_STRUCT(SingleDisplayConfigState::Modified, topology, original_modes, original_hdr_states, original_primary_device)
1717
DD_JSON_DEFINE_SERIALIZE_STRUCT(SingleDisplayConfigState, initial, modified)
18+
DD_JSON_DEFINE_SERIALIZE_STRUCT(WinWorkarounds, hdr_blank_delay)
1819
} // namespace display_device

0 commit comments

Comments
 (0)