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
Original file line numberDiff line numberDiff line change
Expand Up@@ -109,7 +109,7 @@ namespace nlohmann {
nlohmann_json_t = std::nullopt;
}
else {
nlohmann_json_t = nlohmann_json_j.template get<T>();
nlohmann_json_t = nlohmann_json_j.get<T>();
}
}
};
Expand DownExpand Up@@ -138,5 +138,23 @@ namespace nlohmann {
}
}
};

// Specialization for chrono duration.
template <class Rep, class Period>
struct adl_serializer<std::chrono::duration<Rep, Period>> {
using NanoRep = decltype(std::chrono::nanoseconds {}.count());
static_assert(std::numeric_limits<Rep>::max() <= std::numeric_limits<NanoRep>::max(),
"Duration support above nanoseconds have not been tested/verified yet!");

static void
to_json(json &nlohmann_json_j, const std::chrono::duration<Rep, Period> &nlohmann_json_t) {
nlohmann_json_j = nlohmann_json_t.count();
}

static void
from_json(const json &nlohmann_json_j, std::chrono::duration<Rep, Period> &nlohmann_json_t) {
nlohmann_json_t = std::chrono::duration<Rep, Period> { nlohmann_json_j.get<Rep>() };
}
};
} // namespace nlohmann
#endif
1 change: 1 addition & 0 deletions src/common/include/display_device/json.h
Original file line numberDiff line numberDiff line change
Expand Up@@ -25,6 +25,7 @@
namespace display_device {
extern const std::optional<unsigned int> JSON_COMPACT;

DD_JSON_DECLARE_CONVERTER(EnumeratedDevice)
DD_JSON_DECLARE_CONVERTER(EnumeratedDeviceList)
DD_JSON_DECLARE_CONVERTER(SingleDisplayConfiguration)
DD_JSON_DECLARE_CONVERTER(std::set<std::string>)
Expand Down
1 change: 1 addition & 0 deletions src/common/json.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -13,6 +13,7 @@
// clang-format on

namespace display_device {
DD_JSON_DEFINE_CONVERTER(EnumeratedDevice)
DD_JSON_DEFINE_CONVERTER(EnumeratedDeviceList)
DD_JSON_DEFINE_CONVERTER(SingleDisplayConfiguration)
DD_JSON_DEFINE_CONVERTER(std::set<std::string>)
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -14,5 +14,6 @@ namespace display_device {
DD_JSON_DECLARE_SERIALIZE_TYPE(SingleDisplayConfigState::Initial)
DD_JSON_DECLARE_SERIALIZE_TYPE(SingleDisplayConfigState::Modified)
DD_JSON_DECLARE_SERIALIZE_TYPE(SingleDisplayConfigState)
DD_JSON_DECLARE_SERIALIZE_TYPE(WinWorkarounds)
} // namespace display_device
#endif
1 change: 1 addition & 0 deletions src/windows/include/display_device/windows/json.h
Original file line numberDiff line numberDiff line change
Expand Up@@ -14,4 +14,5 @@ namespace display_device {
DD_JSON_DECLARE_CONVERTER(DeviceDisplayModeMap)
DD_JSON_DECLARE_CONVERTER(HdrStateMap)
DD_JSON_DECLARE_CONVERTER(SingleDisplayConfigState)
DD_JSON_DECLARE_CONVERTER(WinWorkarounds)
} // namespace display_device
10 changes: 4 additions & 6 deletions src/windows/include/display_device/windows/settings_manager.h
Original file line numberDiff line numberDiff line change
Expand Up@@ -5,7 +5,6 @@
#pragma once

// system includes
#include <chrono>
#include <memory>

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

/** For details @see SettingsManagerInterface::enumAvailableDevices */
[[nodiscard]] EnumeratedDeviceList
Expand DownExpand Up@@ -115,9 +116,6 @@ namespace display_device {
std::shared_ptr<WinDisplayDeviceInterface> m_dd_api;
std::shared_ptr<AudioContextInterface> m_audio_context_api;
std::unique_ptr<PersistentState> m_persistence_state;

private:
/** @see win_utils::blankHdrStates for more details. */
std::chrono::milliseconds m_hdr_blank_delay { 500 }; // 500ms should be more than enough...
WinWorkarounds m_workarounds;
};
} // namespace display_device
Original file line numberDiff line numberDiff line change
Expand Up@@ -165,9 +165,10 @@ namespace display_device::win_utils {
*
* @param win_dd Interface for interacting with the OS.
* @param delay Delay between OFF and ON states (ON -> OFF -> DELAY -> ON).
* If null optional is provided, the function does nothing.
*/
void
blankHdrStates(WinDisplayDeviceInterface &win_dd, std::chrono::millisecondsdelay);
blankHdrStates(WinDisplayDeviceInterface &win_dd, const std::optional<std::chrono::milliseconds> &delay);

/**
* @brief Make guard function for the topology.
Expand Down
14 changes: 14 additions & 0 deletions src/windows/include/display_device/windows/types.h
Original file line numberDiff line numberDiff line change
Expand Up@@ -11,6 +11,7 @@
#include <windows.h>

// system includes
#include <chrono>
#include <functional>
#include <map>
#include <set>
Expand DownExpand Up@@ -171,4 +172,17 @@ namespace display_device {
* @brief Default function type used for cleanup/guard functions.
*/
using DdGuardFn = std::function<void()>;

/**
* @brief Settings for workarounds/hacks for Windows.
*/
struct WinWorkarounds {
std::optional<std::chrono::milliseconds> m_hdr_blank_delay { std::nullopt }; ///< @seealso{win_utils::blankHdrStates for more details.}

/**
* @brief Comparator for strict equality.
*/
friend bool
operator==(const WinWorkarounds &lhs, const WinWorkarounds &rhs);
};
} // namespace display_device
1 change: 1 addition & 0 deletions src/windows/json.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -19,4 +19,5 @@ namespace display_device {
DD_JSON_DEFINE_CONVERTER(DeviceDisplayModeMap)
DD_JSON_DEFINE_CONVERTER(HdrStateMap)
DD_JSON_DEFINE_CONVERTER(SingleDisplayConfigState)
DD_JSON_DEFINE_CONVERTER(WinWorkarounds)
} // namespace display_device
1 change: 1 addition & 0 deletions src/windows/json_serializer.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -15,4 +15,5 @@ namespace display_device {
DD_JSON_DEFINE_SERIALIZE_STRUCT(SingleDisplayConfigState::Initial, topology, primary_devices)
DD_JSON_DEFINE_SERIALIZE_STRUCT(SingleDisplayConfigState::Modified, topology, original_modes, original_hdr_states, original_primary_device)
DD_JSON_DEFINE_SERIALIZE_STRUCT(SingleDisplayConfigState, initial, modified)
DD_JSON_DEFINE_SERIALIZE_STRUCT(WinWorkarounds, hdr_blank_delay)
} // namespace display_device
2 changes: 1 addition & 1 deletion src/windows/settings_manager_apply.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -47,7 +47,7 @@ namespace display_device {
bool system_settings_touched { false };
boost::scope::scope_exit hdr_blank_always_executed_guard { [this, &system_settings_touched]() {
if (system_settings_touched) {
win_utils::blankHdrStates(*m_dd_api, m_hdr_blank_delay);
win_utils::blankHdrStates(*m_dd_api, m_workarounds.m_hdr_blank_delay);
}
} };

Expand Down
10 changes: 8 additions & 2 deletions src/windows/settings_manager_general.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -8,15 +8,18 @@
// local includes
#include "display_device/logging.h"
#include "display_device/noop_audio_context.h"
#include "display_device/windows/json.h"

namespace display_device {
SettingsManager::SettingsManager(
std::shared_ptr<WinDisplayDeviceInterface> dd_api,
std::shared_ptr<AudioContextInterface> audio_context_api,
std::unique_ptr<PersistentState> persistent_state):
std::unique_ptr<PersistentState> persistent_state,
WinWorkarounds workarounds):
m_dd_api { std::move(dd_api) },
m_audio_context_api { std::move(audio_context_api) },
m_persistence_state { std::move(persistent_state) } {
m_persistence_state { std::move(persistent_state) },
m_workarounds { std::move(workarounds) } {
if (!m_dd_api) {
throw std::logic_error { "Nullptr provided for WinDisplayDeviceInterface in SettingsManager!" };
}
Expand All@@ -28,6 +31,9 @@ namespace display_device {
if (!m_persistence_state) {
throw std::logic_error { "Nullptr provided for PersistentState in SettingsManager!" };
}

DD_LOG(info) << "Provided workaround settings for SettingsManager:\n"
<< toJson(m_workarounds);
}

EnumeratedDeviceList
Expand Down
2 changes: 1 addition & 1 deletion src/windows/settings_manager_revert.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -47,7 +47,7 @@ namespace display_device {
bool system_settings_touched { false };
boost::scope::scope_exit hdr_blank_always_executed_guard { [this, &system_settings_touched]() {
if (system_settings_touched) {
win_utils::blankHdrStates(*m_dd_api, m_hdr_blank_delay);
win_utils::blankHdrStates(*m_dd_api, m_workarounds.m_hdr_blank_delay);
}
} };
boost::scope::scope_exit topology_prep_guard { [this, &cached_state, &current_topology, &system_settings_touched]() {
Expand Down
10 changes: 7 additions & 3 deletions src/windows/settings_utils.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -358,7 +358,11 @@ namespace display_device::win_utils {
}

void
blankHdrStates(WinDisplayDeviceInterface &win_dd, const std::chrono::milliseconds delay) {
blankHdrStates(WinDisplayDeviceInterface &win_dd, const std::optional<std::chrono::milliseconds> &delay) {
if (!delay) {
return;
}

const auto topology { win_dd.getCurrentTopology() };
if (!win_dd.isTopologyValid(topology)) {
DD_LOG(error) << "Got an invalid topology while trying to blank HDR states!";
Expand DownExpand Up@@ -390,13 +394,13 @@ namespace display_device::win_utils {
return;
}

DD_LOG(info) << "Applying HDR state \"blank\" workaround (" << delay.count() << "ms) to devices: " << toJson(device_ids, JSON_COMPACT);
DD_LOG(info) << "Applying HDR state \"blank\" workaround (" << delay->count() << "ms) to devices: " << toJson(device_ids, JSON_COMPACT);
if (!win_dd.setHdrStates(inverse_states)) {
DD_LOG(error) << "Failed to apply inverse HDR states during \"blank\"!";
return;
}

std::this_thread::sleep_for(delay);
std::this_thread::sleep_for(*delay);
if (!win_dd.setHdrStates(original_states)) {
DD_LOG(error) << "Failed to apply original HDR states during \"blank\"!";
}
Expand Down
5 changes: 5 additions & 0 deletions src/windows/types.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -30,4 +30,9 @@ namespace display_device {
operator==(const SingleDisplayConfigState &lhs, const SingleDisplayConfigState &rhs) {
return lhs.m_initial == rhs.m_initial && lhs.m_modified == rhs.m_modified;
}

bool
operator==(const WinWorkarounds &lhs, const WinWorkarounds &rhs) {
return lhs.m_hdr_blank_delay == rhs.m_hdr_blank_delay;
}
} // namespace display_device
66 changes: 66 additions & 0 deletions tests/unit/general/test_json.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -44,11 +44,25 @@ namespace display_device {
DD_JSON_DEFINE_CONVERTER(TestEnum)
DD_JSON_DEFINE_CONVERTER(TestStruct)
DD_JSON_DEFINE_CONVERTER(TestVariant)
DD_JSON_DEFINE_CONVERTER(std::chrono::nanoseconds)
DD_JSON_DEFINE_CONVERTER(std::chrono::microseconds)
DD_JSON_DEFINE_CONVERTER(std::chrono::milliseconds)
DD_JSON_DEFINE_CONVERTER(std::chrono::seconds)
DD_JSON_DEFINE_CONVERTER(std::chrono::minutes)
DD_JSON_DEFINE_CONVERTER(std::chrono::hours)
DD_JSON_DEFINE_CONVERTER(std::chrono::days)
DD_JSON_DEFINE_CONVERTER(std::chrono::weeks)
DD_JSON_DEFINE_CONVERTER(std::chrono::months)
DD_JSON_DEFINE_CONVERTER(std::chrono::years)
} // namespace display_device

namespace {
// Specialized TEST macro(s) for this test file
#define TEST_S(...) DD_MAKE_TEST(TEST, JsonTest, __VA_ARGS__)

// Additional convenience global const(s)
constexpr auto MIN_NANO_VAL { std::numeric_limits<decltype(std::chrono::nanoseconds {}.count())>::min() };
constexpr auto MAX_NANO_VAL { std::numeric_limits<decltype(std::chrono::nanoseconds {}.count())>::max() };
} // namespace

TEST_S(ToJson, NoError, WithSuccessParam) {
Expand DownExpand Up@@ -192,3 +206,55 @@ TEST_S(FromJson, TestVariant, UnknownVariantType) {
EXPECT_FALSE(display_device::fromJson(R"({"type":"SomeUnknownType","value":123.0})", variant, &error_message));
EXPECT_EQ(error_message, "Could not parse variant from type SomeUnknownType!");
}

TEST_S(ToJson, ChronoDuration) {
using namespace std::chrono;

EXPECT_EQ(display_device::toJson(nanoseconds { 2000000000 }, std::nullopt, nullptr), R"(2000000000)");
EXPECT_EQ(display_device::toJson(microseconds { 2000000 }, std::nullopt, nullptr), R"(2000000)");
EXPECT_EQ(display_device::toJson(milliseconds { 2000 }, std::nullopt, nullptr), R"(2000)");
EXPECT_EQ(display_device::toJson(seconds { 2 }, std::nullopt, nullptr), R"(2)");
EXPECT_EQ(display_device::toJson(minutes { 20 }, std::nullopt, nullptr), R"(20)");
EXPECT_EQ(display_device::toJson(hours { 20 }, std::nullopt, nullptr), R"(20)");
EXPECT_EQ(display_device::toJson(days { 20 }, std::nullopt, nullptr), R"(20)");
EXPECT_EQ(display_device::toJson(weeks { 20 }, std::nullopt, nullptr), R"(20)");
EXPECT_EQ(display_device::toJson(months { 20 }, std::nullopt, nullptr), R"(20)");
EXPECT_EQ(display_device::toJson(years { 20 }, std::nullopt, nullptr), R"(20)");
}

TEST_S(FromJson, ChronoDuration) {
const auto doTest { []<class T>(const std::string &string_input, T expected_value) {
T value {};

EXPECT_TRUE(display_device::fromJson(string_input, value, nullptr));
EXPECT_EQ(value, expected_value);
} };

using namespace std::chrono;

doTest(R"(2000000000)", nanoseconds { 2000000000 });
doTest(R"(2000000)", microseconds { 2000000 });
doTest(R"(2000)", milliseconds { 2000 });
doTest(R"(2)", seconds { 2 });
doTest(R"(20)", minutes { 20 });
doTest(R"(20)", hours { 20 });
doTest(R"(20)", days { 20 });
doTest(R"(20)", weeks { 20 });
doTest(R"(20)", months { 20 });
doTest(R"(20)", years { 20 });
}

TEST_S(ToJson, ChronoDuration, Ranges) {
EXPECT_EQ(display_device::toJson(std::chrono::nanoseconds { MIN_NANO_VAL }, std::nullopt, nullptr), std::to_string(MIN_NANO_VAL));
EXPECT_EQ(display_device::toJson(std::chrono::nanoseconds { MAX_NANO_VAL }, std::nullopt, nullptr), std::to_string(MAX_NANO_VAL));
}

TEST_S(FromJson, ChronoDuration, Ranges) {
std::chrono::nanoseconds value {};

EXPECT_TRUE(display_device::fromJson(std::to_string(MIN_NANO_VAL), value, nullptr));
EXPECT_EQ(value, std::chrono::nanoseconds { MIN_NANO_VAL });

EXPECT_TRUE(display_device::fromJson(std::to_string(MAX_NANO_VAL), value, nullptr));
EXPECT_EQ(value, std::chrono::nanoseconds { MAX_NANO_VAL });
}
31 changes: 31 additions & 0 deletions tests/unit/general/test_json_converter.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -6,6 +6,37 @@ namespace {
#define TEST_F_S(...) DD_MAKE_TEST(TEST_F, JsonConverterTest, __VA_ARGS__)
} // namespace

TEST_F_S(EnumeratedDevice) {
display_device::EnumeratedDevice item_1 {
"ID_1",
"NAME_2",
"FU_NAME_3",
display_device::EnumeratedDevice::Info {
{ 1920, 1080 },
display_device::Rational { 175, 100 },
119.9554,
false,
{ 1, 2 },
display_device::HdrState::Enabled }
};
display_device::EnumeratedDevice item_2 {
"ID_2",
"NAME_2",
"FU_NAME_2",
display_device::EnumeratedDevice::Info {
{ 1920, 1080 },
1.75,
display_device::Rational { 1199554, 10000 },
true,
{ 0, 0 },
display_device::HdrState::Disabled }
};

executeTestCase(display_device::EnumeratedDevice {}, R"({"device_id":"","display_name":"","friendly_name":"","info":null})");
executeTestCase(item_1, R"({"device_id":"ID_1","display_name":"NAME_2","friendly_name":"FU_NAME_3","info":{"hdr_state":"Enabled","origin_point":{"x":1,"y":2},"primary":false,"refresh_rate":{"type":"double","value":119.9554},"resolution":{"height":1080,"width":1920},"resolution_scale":{"type":"rational","value":{"denominator":100,"numerator":175}}}})");
executeTestCase(item_2, R"({"device_id":"ID_2","display_name":"NAME_2","friendly_name":"FU_NAME_2","info":{"hdr_state":"Disabled","origin_point":{"x":0,"y":0},"primary":true,"refresh_rate":{"type":"rational","value":{"denominator":10000,"numerator":1199554}},"resolution":{"height":1080,"width":1920},"resolution_scale":{"type":"double","value":1.75}}})");
}

TEST_F_S(EnumeratedDeviceList) {
display_device::EnumeratedDevice item_1 {
"ID_1",
Expand Down
9 changes: 9 additions & 0 deletions tests/unit/windows/test_json_converter.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -40,3 +40,12 @@ TEST_F_S(SingleDisplayConfigState) {
executeTestCase(display_device::SingleDisplayConfigState {}, R"({"initial":{"primary_devices":[],"topology":[]},"modified":{"original_hdr_states":{},"original_modes":{},"original_primary_device":"","topology":[]}})");
executeTestCase(valid_input, R"({"initial":{"primary_devices":["DeviceId1"],"topology":[["DeviceId1"]]},"modified":{"original_hdr_states":{"DeviceId2":"Disabled"},"original_modes":{"DeviceId2":{"refresh_rate":{"denominator":1,"numerator":120},"resolution":{"height":1080,"width":1920}}},"original_primary_device":"DeviceId2","topology":[["DeviceId2"]]}})");
}

TEST_F_S(WinWorkarounds) {
display_device::WinWorkarounds input {
std::chrono::milliseconds { 500 }
};

executeTestCase(display_device::WinWorkarounds {}, R"({"hdr_blank_delay":null})");
executeTestCase(input, R"({"hdr_blank_delay":500})");
}
5 changes: 4 additions & 1 deletion tests/unit/windows/test_settings_manager_apply.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -47,7 +47,10 @@ namespace {
display_device::SettingsManager &
getImpl() {
if (!m_impl) {
m_impl = std::make_unique<display_device::SettingsManager>(m_dd_api, m_audio_context_api, std::make_unique<display_device::PersistentState>(m_settings_persistence_api));
m_impl = std::make_unique<display_device::SettingsManager>(m_dd_api, m_audio_context_api, std::make_unique<display_device::PersistentState>(m_settings_persistence_api),
display_device::WinWorkarounds {
.m_hdr_blank_delay = std::chrono::milliseconds { 123 } // Value is irrelevant for the tests
});
}

return *m_impl;
Expand Down
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Add copy buttons to all
 blocks\n(function() {\n function addCopyButtons() {\n document.querySelectorAll('pre code').forEach(function(codeBlock) {\n if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;\n codeBlock.parentElement.setAttribute('data-copy-added', 'true');\n \n var btn = document.createElement('button');\n btn.textContent = 'Copy';\n btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';\n btn.onmouseover = function() { this.style.opacity = '1'; };\n btn.onmouseout = function() { this.style.opacity = '0.7'; };\n btn.onclick = function() {\n navigator.clipboard.writeText(codeBlock.textContent).then(function() {\n btn.textContent = 'Copied!';\n setTimeout(function() { btn.textContent = 'Copy'; }, 1500);\n });\n };\n codeBlock.parentElement.style.position = 'relative';\n codeBlock.parentElement.appendChild(btn);\n });\n }\n \n addCopyButtons();\n \n // Re-run on dynamic content\n var observer = new MutationObserver(addCopyButtons);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Add Copy Buttons to Code Blocks");
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
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
Original file line numberDiff line numberDiff line change
Expand Up@@ -109,7 +109,7 @@ namespace nlohmann {
nlohmann_json_t = std::nullopt;
}
else {
nlohmann_json_t = nlohmann_json_j.template get<T>();
nlohmann_json_t = nlohmann_json_j.get<T>();
}
}
};
Expand DownExpand Up@@ -138,5 +138,23 @@ namespace nlohmann {
}
}
};

// Specialization for chrono duration.
template <class Rep, class Period>
struct adl_serializer<std::chrono::duration<Rep, Period>> {
using NanoRep = decltype(std::chrono::nanoseconds {}.count());
static_assert(std::numeric_limits<Rep>::max() <= std::numeric_limits<NanoRep>::max(),
"Duration support above nanoseconds have not been tested/verified yet!");

static void
to_json(json &nlohmann_json_j, const std::chrono::duration<Rep, Period> &nlohmann_json_t) {
nlohmann_json_j = nlohmann_json_t.count();
}

static void
from_json(const json &nlohmann_json_j, std::chrono::duration<Rep, Period> &nlohmann_json_t) {
nlohmann_json_t = std::chrono::duration<Rep, Period> { nlohmann_json_j.get<Rep>() };
}
};
} // namespace nlohmann
#endif
1 change: 1 addition & 0 deletions src/common/include/display_device/json.h
Original file line numberDiff line numberDiff line change
Expand Up@@ -25,6 +25,7 @@
namespace display_device {
extern const std::optional<unsigned int> JSON_COMPACT;

DD_JSON_DECLARE_CONVERTER(EnumeratedDevice)
DD_JSON_DECLARE_CONVERTER(EnumeratedDeviceList)
DD_JSON_DECLARE_CONVERTER(SingleDisplayConfiguration)
DD_JSON_DECLARE_CONVERTER(std::set<std::string>)
Expand Down
1 change: 1 addition & 0 deletions src/common/json.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -13,6 +13,7 @@
// clang-format on

namespace display_device {
DD_JSON_DEFINE_CONVERTER(EnumeratedDevice)
DD_JSON_DEFINE_CONVERTER(EnumeratedDeviceList)
DD_JSON_DEFINE_CONVERTER(SingleDisplayConfiguration)
DD_JSON_DEFINE_CONVERTER(std::set<std::string>)
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -14,5 +14,6 @@ namespace display_device {
DD_JSON_DECLARE_SERIALIZE_TYPE(SingleDisplayConfigState::Initial)
DD_JSON_DECLARE_SERIALIZE_TYPE(SingleDisplayConfigState::Modified)
DD_JSON_DECLARE_SERIALIZE_TYPE(SingleDisplayConfigState)
DD_JSON_DECLARE_SERIALIZE_TYPE(WinWorkarounds)
} // namespace display_device
#endif
1 change: 1 addition & 0 deletions src/windows/include/display_device/windows/json.h
Original file line numberDiff line numberDiff line change
Expand Up@@ -14,4 +14,5 @@ namespace display_device {
DD_JSON_DECLARE_CONVERTER(DeviceDisplayModeMap)
DD_JSON_DECLARE_CONVERTER(HdrStateMap)
DD_JSON_DECLARE_CONVERTER(SingleDisplayConfigState)
DD_JSON_DECLARE_CONVERTER(WinWorkarounds)
} // namespace display_device
10 changes: 4 additions & 6 deletions src/windows/include/display_device/windows/settings_manager.h
Original file line numberDiff line numberDiff line change
Expand Up@@ -5,7 +5,6 @@
#pragma once

// system includes
#include <chrono>
#include <memory>

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

/** For details @see SettingsManagerInterface::enumAvailableDevices */
[[nodiscard]] EnumeratedDeviceList
Expand DownExpand Up@@ -115,9 +116,6 @@ namespace display_device {
std::shared_ptr<WinDisplayDeviceInterface> m_dd_api;
std::shared_ptr<AudioContextInterface> m_audio_context_api;
std::unique_ptr<PersistentState> m_persistence_state;

private:
/** @see win_utils::blankHdrStates for more details. */
std::chrono::milliseconds m_hdr_blank_delay { 500 }; // 500ms should be more than enough...
WinWorkarounds m_workarounds;
};
} // namespace display_device
Original file line numberDiff line numberDiff line change
Expand Up@@ -165,9 +165,10 @@ namespace display_device::win_utils {
*
* @param win_dd Interface for interacting with the OS.
* @param delay Delay between OFF and ON states (ON -> OFF -> DELAY -> ON).
* If null optional is provided, the function does nothing.
*/
void
blankHdrStates(WinDisplayDeviceInterface &win_dd, std::chrono::millisecondsdelay);
blankHdrStates(WinDisplayDeviceInterface &win_dd, const std::optional<std::chrono::milliseconds> &delay);

/**
* @brief Make guard function for the topology.
Expand Down
14 changes: 14 additions & 0 deletions src/windows/include/display_device/windows/types.h
Original file line numberDiff line numberDiff line change
Expand Up@@ -11,6 +11,7 @@
#include <windows.h>

// system includes
#include <chrono>
#include <functional>
#include <map>
#include <set>
Expand DownExpand Up@@ -171,4 +172,17 @@ namespace display_device {
* @brief Default function type used for cleanup/guard functions.
*/
using DdGuardFn = std::function<void()>;

/**
* @brief Settings for workarounds/hacks for Windows.
*/
struct WinWorkarounds {
std::optional<std::chrono::milliseconds> m_hdr_blank_delay { std::nullopt }; ///< @seealso{win_utils::blankHdrStates for more details.}

/**
* @brief Comparator for strict equality.
*/
friend bool
operator==(const WinWorkarounds &lhs, const WinWorkarounds &rhs);
};
} // namespace display_device
1 change: 1 addition & 0 deletions src/windows/json.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -19,4 +19,5 @@ namespace display_device {
DD_JSON_DEFINE_CONVERTER(DeviceDisplayModeMap)
DD_JSON_DEFINE_CONVERTER(HdrStateMap)
DD_JSON_DEFINE_CONVERTER(SingleDisplayConfigState)
DD_JSON_DEFINE_CONVERTER(WinWorkarounds)
} // namespace display_device
1 change: 1 addition & 0 deletions src/windows/json_serializer.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -15,4 +15,5 @@ namespace display_device {
DD_JSON_DEFINE_SERIALIZE_STRUCT(SingleDisplayConfigState::Initial, topology, primary_devices)
DD_JSON_DEFINE_SERIALIZE_STRUCT(SingleDisplayConfigState::Modified, topology, original_modes, original_hdr_states, original_primary_device)
DD_JSON_DEFINE_SERIALIZE_STRUCT(SingleDisplayConfigState, initial, modified)
DD_JSON_DEFINE_SERIALIZE_STRUCT(WinWorkarounds, hdr_blank_delay)
} // namespace display_device
2 changes: 1 addition & 1 deletion src/windows/settings_manager_apply.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -47,7 +47,7 @@ namespace display_device {
bool system_settings_touched { false };
boost::scope::scope_exit hdr_blank_always_executed_guard { [this, &system_settings_touched]() {
if (system_settings_touched) {
win_utils::blankHdrStates(*m_dd_api, m_hdr_blank_delay);
win_utils::blankHdrStates(*m_dd_api, m_workarounds.m_hdr_blank_delay);
}
} };

Expand Down
10 changes: 8 additions & 2 deletions src/windows/settings_manager_general.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -8,15 +8,18 @@
// local includes
#include "display_device/logging.h"
#include "display_device/noop_audio_context.h"
#include "display_device/windows/json.h"

namespace display_device {
SettingsManager::SettingsManager(
std::shared_ptr<WinDisplayDeviceInterface> dd_api,
std::shared_ptr<AudioContextInterface> audio_context_api,
std::unique_ptr<PersistentState> persistent_state):
std::unique_ptr<PersistentState> persistent_state,
WinWorkarounds workarounds):
m_dd_api { std::move(dd_api) },
m_audio_context_api { std::move(audio_context_api) },
m_persistence_state { std::move(persistent_state) } {
m_persistence_state { std::move(persistent_state) },
m_workarounds { std::move(workarounds) } {
if (!m_dd_api) {
throw std::logic_error { "Nullptr provided for WinDisplayDeviceInterface in SettingsManager!" };
}
Expand All@@ -28,6 +31,9 @@ namespace display_device {
if (!m_persistence_state) {
throw std::logic_error { "Nullptr provided for PersistentState in SettingsManager!" };
}

DD_LOG(info) << "Provided workaround settings for SettingsManager:\n"
<< toJson(m_workarounds);
}

EnumeratedDeviceList
Expand Down
2 changes: 1 addition & 1 deletion src/windows/settings_manager_revert.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -47,7 +47,7 @@ namespace display_device {
bool system_settings_touched { false };
boost::scope::scope_exit hdr_blank_always_executed_guard { [this, &system_settings_touched]() {
if (system_settings_touched) {
win_utils::blankHdrStates(*m_dd_api, m_hdr_blank_delay);
win_utils::blankHdrStates(*m_dd_api, m_workarounds.m_hdr_blank_delay);
}
} };
boost::scope::scope_exit topology_prep_guard { [this, &cached_state, &current_topology, &system_settings_touched]() {
Expand Down
10 changes: 7 additions & 3 deletions src/windows/settings_utils.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -358,7 +358,11 @@ namespace display_device::win_utils {
}

void
blankHdrStates(WinDisplayDeviceInterface &win_dd, const std::chrono::milliseconds delay) {
blankHdrStates(WinDisplayDeviceInterface &win_dd, const std::optional<std::chrono::milliseconds> &delay) {
if (!delay) {
return;
}

const auto topology { win_dd.getCurrentTopology() };
if (!win_dd.isTopologyValid(topology)) {
DD_LOG(error) << "Got an invalid topology while trying to blank HDR states!";
Expand DownExpand Up@@ -390,13 +394,13 @@ namespace display_device::win_utils {
return;
}

DD_LOG(info) << "Applying HDR state \"blank\" workaround (" << delay.count() << "ms) to devices: " << toJson(device_ids, JSON_COMPACT);
DD_LOG(info) << "Applying HDR state \"blank\" workaround (" << delay->count() << "ms) to devices: " << toJson(device_ids, JSON_COMPACT);
if (!win_dd.setHdrStates(inverse_states)) {
DD_LOG(error) << "Failed to apply inverse HDR states during \"blank\"!";
return;
}

std::this_thread::sleep_for(delay);
std::this_thread::sleep_for(*delay);
if (!win_dd.setHdrStates(original_states)) {
DD_LOG(error) << "Failed to apply original HDR states during \"blank\"!";
}
Expand Down
5 changes: 5 additions & 0 deletions src/windows/types.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -30,4 +30,9 @@ namespace display_device {
operator==(const SingleDisplayConfigState &lhs, const SingleDisplayConfigState &rhs) {
return lhs.m_initial == rhs.m_initial && lhs.m_modified == rhs.m_modified;
}

bool
operator==(const WinWorkarounds &lhs, const WinWorkarounds &rhs) {
return lhs.m_hdr_blank_delay == rhs.m_hdr_blank_delay;
}
} // namespace display_device
66 changes: 66 additions & 0 deletions tests/unit/general/test_json.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -44,11 +44,25 @@ namespace display_device {
DD_JSON_DEFINE_CONVERTER(TestEnum)
DD_JSON_DEFINE_CONVERTER(TestStruct)
DD_JSON_DEFINE_CONVERTER(TestVariant)
DD_JSON_DEFINE_CONVERTER(std::chrono::nanoseconds)
DD_JSON_DEFINE_CONVERTER(std::chrono::microseconds)
DD_JSON_DEFINE_CONVERTER(std::chrono::milliseconds)
DD_JSON_DEFINE_CONVERTER(std::chrono::seconds)
DD_JSON_DEFINE_CONVERTER(std::chrono::minutes)
DD_JSON_DEFINE_CONVERTER(std::chrono::hours)
DD_JSON_DEFINE_CONVERTER(std::chrono::days)
DD_JSON_DEFINE_CONVERTER(std::chrono::weeks)
DD_JSON_DEFINE_CONVERTER(std::chrono::months)
DD_JSON_DEFINE_CONVERTER(std::chrono::years)
} // namespace display_device

namespace {
// Specialized TEST macro(s) for this test file
#define TEST_S(...) DD_MAKE_TEST(TEST, JsonTest, __VA_ARGS__)

// Additional convenience global const(s)
constexpr auto MIN_NANO_VAL { std::numeric_limits<decltype(std::chrono::nanoseconds {}.count())>::min() };
constexpr auto MAX_NANO_VAL { std::numeric_limits<decltype(std::chrono::nanoseconds {}.count())>::max() };
} // namespace

TEST_S(ToJson, NoError, WithSuccessParam) {
Expand DownExpand Up@@ -192,3 +206,55 @@ TEST_S(FromJson, TestVariant, UnknownVariantType) {
EXPECT_FALSE(display_device::fromJson(R"({"type":"SomeUnknownType","value":123.0})", variant, &error_message));
EXPECT_EQ(error_message, "Could not parse variant from type SomeUnknownType!");
}

TEST_S(ToJson, ChronoDuration) {
using namespace std::chrono;

EXPECT_EQ(display_device::toJson(nanoseconds { 2000000000 }, std::nullopt, nullptr), R"(2000000000)");
EXPECT_EQ(display_device::toJson(microseconds { 2000000 }, std::nullopt, nullptr), R"(2000000)");
EXPECT_EQ(display_device::toJson(milliseconds { 2000 }, std::nullopt, nullptr), R"(2000)");
EXPECT_EQ(display_device::toJson(seconds { 2 }, std::nullopt, nullptr), R"(2)");
EXPECT_EQ(display_device::toJson(minutes { 20 }, std::nullopt, nullptr), R"(20)");
EXPECT_EQ(display_device::toJson(hours { 20 }, std::nullopt, nullptr), R"(20)");
EXPECT_EQ(display_device::toJson(days { 20 }, std::nullopt, nullptr), R"(20)");
EXPECT_EQ(display_device::toJson(weeks { 20 }, std::nullopt, nullptr), R"(20)");
EXPECT_EQ(display_device::toJson(months { 20 }, std::nullopt, nullptr), R"(20)");
EXPECT_EQ(display_device::toJson(years { 20 }, std::nullopt, nullptr), R"(20)");
}

TEST_S(FromJson, ChronoDuration) {
const auto doTest { []<class T>(const std::string &string_input, T expected_value) {
T value {};

EXPECT_TRUE(display_device::fromJson(string_input, value, nullptr));
EXPECT_EQ(value, expected_value);
} };

using namespace std::chrono;

doTest(R"(2000000000)", nanoseconds { 2000000000 });
doTest(R"(2000000)", microseconds { 2000000 });
doTest(R"(2000)", milliseconds { 2000 });
doTest(R"(2)", seconds { 2 });
doTest(R"(20)", minutes { 20 });
doTest(R"(20)", hours { 20 });
doTest(R"(20)", days { 20 });
doTest(R"(20)", weeks { 20 });
doTest(R"(20)", months { 20 });
doTest(R"(20)", years { 20 });
}

TEST_S(ToJson, ChronoDuration, Ranges) {
EXPECT_EQ(display_device::toJson(std::chrono::nanoseconds { MIN_NANO_VAL }, std::nullopt, nullptr), std::to_string(MIN_NANO_VAL));
EXPECT_EQ(display_device::toJson(std::chrono::nanoseconds { MAX_NANO_VAL }, std::nullopt, nullptr), std::to_string(MAX_NANO_VAL));
}

TEST_S(FromJson, ChronoDuration, Ranges) {
std::chrono::nanoseconds value {};

EXPECT_TRUE(display_device::fromJson(std::to_string(MIN_NANO_VAL), value, nullptr));
EXPECT_EQ(value, std::chrono::nanoseconds { MIN_NANO_VAL });

EXPECT_TRUE(display_device::fromJson(std::to_string(MAX_NANO_VAL), value, nullptr));
EXPECT_EQ(value, std::chrono::nanoseconds { MAX_NANO_VAL });
}
31 changes: 31 additions & 0 deletions tests/unit/general/test_json_converter.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -6,6 +6,37 @@ namespace {
#define TEST_F_S(...) DD_MAKE_TEST(TEST_F, JsonConverterTest, __VA_ARGS__)
} // namespace

TEST_F_S(EnumeratedDevice) {
display_device::EnumeratedDevice item_1 {
"ID_1",
"NAME_2",
"FU_NAME_3",
display_device::EnumeratedDevice::Info {
{ 1920, 1080 },
display_device::Rational { 175, 100 },
119.9554,
false,
{ 1, 2 },
display_device::HdrState::Enabled }
};
display_device::EnumeratedDevice item_2 {
"ID_2",
"NAME_2",
"FU_NAME_2",
display_device::EnumeratedDevice::Info {
{ 1920, 1080 },
1.75,
display_device::Rational { 1199554, 10000 },
true,
{ 0, 0 },
display_device::HdrState::Disabled }
};

executeTestCase(display_device::EnumeratedDevice {}, R"({"device_id":"","display_name":"","friendly_name":"","info":null})");
executeTestCase(item_1, R"({"device_id":"ID_1","display_name":"NAME_2","friendly_name":"FU_NAME_3","info":{"hdr_state":"Enabled","origin_point":{"x":1,"y":2},"primary":false,"refresh_rate":{"type":"double","value":119.9554},"resolution":{"height":1080,"width":1920},"resolution_scale":{"type":"rational","value":{"denominator":100,"numerator":175}}}})");
executeTestCase(item_2, R"({"device_id":"ID_2","display_name":"NAME_2","friendly_name":"FU_NAME_2","info":{"hdr_state":"Disabled","origin_point":{"x":0,"y":0},"primary":true,"refresh_rate":{"type":"rational","value":{"denominator":10000,"numerator":1199554}},"resolution":{"height":1080,"width":1920},"resolution_scale":{"type":"double","value":1.75}}})");
}

TEST_F_S(EnumeratedDeviceList) {
display_device::EnumeratedDevice item_1 {
"ID_1",
Expand Down
9 changes: 9 additions & 0 deletions tests/unit/windows/test_json_converter.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -40,3 +40,12 @@ TEST_F_S(SingleDisplayConfigState) {
executeTestCase(display_device::SingleDisplayConfigState {}, R"({"initial":{"primary_devices":[],"topology":[]},"modified":{"original_hdr_states":{},"original_modes":{},"original_primary_device":"","topology":[]}})");
executeTestCase(valid_input, R"({"initial":{"primary_devices":["DeviceId1"],"topology":[["DeviceId1"]]},"modified":{"original_hdr_states":{"DeviceId2":"Disabled"},"original_modes":{"DeviceId2":{"refresh_rate":{"denominator":1,"numerator":120},"resolution":{"height":1080,"width":1920}}},"original_primary_device":"DeviceId2","topology":[["DeviceId2"]]}})");
}

TEST_F_S(WinWorkarounds) {
display_device::WinWorkarounds input {
std::chrono::milliseconds { 500 }
};

executeTestCase(display_device::WinWorkarounds {}, R"({"hdr_blank_delay":null})");
executeTestCase(input, R"({"hdr_blank_delay":500})");
}
5 changes: 4 additions & 1 deletion tests/unit/windows/test_settings_manager_apply.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -47,7 +47,10 @@ namespace {
display_device::SettingsManager &
getImpl() {
if (!m_impl) {
m_impl = std::make_unique<display_device::SettingsManager>(m_dd_api, m_audio_context_api, std::make_unique<display_device::PersistentState>(m_settings_persistence_api));
m_impl = std::make_unique<display_device::SettingsManager>(m_dd_api, m_audio_context_api, std::make_unique<display_device::PersistentState>(m_settings_persistence_api),
display_device::WinWorkarounds {
.m_hdr_blank_delay = std::chrono::milliseconds { 123 } // Value is irrelevant for the tests
});
}

return *m_impl;
Expand Down
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Force GitHub README to respect dark mode\n(function() {\n var style = document.createElement('style');\n style.textContent = '\n .markdown-body {\n color-scheme: dark light;\n }\n .markdown-body pre { background: #161b22 !important; }\n .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; }\n .markdown-body table th, .markdown-body table td { border-color: #30363d !important; }\n .markdown-body img { background: #0d1117; }\n .markdown-body blockquote { border-left-color: #8b949e; }\n .markdown-body hr { border-color: #30363d; }\n ';\n document.head.appendChild(style);\n})();", "GitHub Dark Mode README Fix"); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
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
Original file line numberDiff line numberDiff line change
Expand Up@@ -109,7 +109,7 @@ namespace nlohmann {
nlohmann_json_t = std::nullopt;
}
else {
nlohmann_json_t = nlohmann_json_j.template get<T>();
nlohmann_json_t = nlohmann_json_j.get<T>();
}
}
};
Expand DownExpand Up@@ -138,5 +138,23 @@ namespace nlohmann {
}
}
};

// Specialization for chrono duration.
template <class Rep, class Period>
struct adl_serializer<std::chrono::duration<Rep, Period>> {
using NanoRep = decltype(std::chrono::nanoseconds {}.count());
static_assert(std::numeric_limits<Rep>::max() <= std::numeric_limits<NanoRep>::max(),
"Duration support above nanoseconds have not been tested/verified yet!");

static void
to_json(json &nlohmann_json_j, const std::chrono::duration<Rep, Period> &nlohmann_json_t) {
nlohmann_json_j = nlohmann_json_t.count();
}

static void
from_json(const json &nlohmann_json_j, std::chrono::duration<Rep, Period> &nlohmann_json_t) {
nlohmann_json_t = std::chrono::duration<Rep, Period> { nlohmann_json_j.get<Rep>() };
}
};
} // namespace nlohmann
#endif
1 change: 1 addition & 0 deletions src/common/include/display_device/json.h
Original file line numberDiff line numberDiff line change
Expand Up@@ -25,6 +25,7 @@
namespace display_device {
extern const std::optional<unsigned int> JSON_COMPACT;

DD_JSON_DECLARE_CONVERTER(EnumeratedDevice)
DD_JSON_DECLARE_CONVERTER(EnumeratedDeviceList)
DD_JSON_DECLARE_CONVERTER(SingleDisplayConfiguration)
DD_JSON_DECLARE_CONVERTER(std::set<std::string>)
Expand Down
1 change: 1 addition & 0 deletions src/common/json.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -13,6 +13,7 @@
// clang-format on

namespace display_device {
DD_JSON_DEFINE_CONVERTER(EnumeratedDevice)
DD_JSON_DEFINE_CONVERTER(EnumeratedDeviceList)
DD_JSON_DEFINE_CONVERTER(SingleDisplayConfiguration)
DD_JSON_DEFINE_CONVERTER(std::set<std::string>)
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -14,5 +14,6 @@ namespace display_device {
DD_JSON_DECLARE_SERIALIZE_TYPE(SingleDisplayConfigState::Initial)
DD_JSON_DECLARE_SERIALIZE_TYPE(SingleDisplayConfigState::Modified)
DD_JSON_DECLARE_SERIALIZE_TYPE(SingleDisplayConfigState)
DD_JSON_DECLARE_SERIALIZE_TYPE(WinWorkarounds)
} // namespace display_device
#endif
1 change: 1 addition & 0 deletions src/windows/include/display_device/windows/json.h
Original file line numberDiff line numberDiff line change
Expand Up@@ -14,4 +14,5 @@ namespace display_device {
DD_JSON_DECLARE_CONVERTER(DeviceDisplayModeMap)
DD_JSON_DECLARE_CONVERTER(HdrStateMap)
DD_JSON_DECLARE_CONVERTER(SingleDisplayConfigState)
DD_JSON_DECLARE_CONVERTER(WinWorkarounds)
} // namespace display_device
10 changes: 4 additions & 6 deletions src/windows/include/display_device/windows/settings_manager.h
Original file line numberDiff line numberDiff line change
Expand Up@@ -5,7 +5,6 @@
#pragma once

// system includes
#include <chrono>
#include <memory>

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

/** For details @see SettingsManagerInterface::enumAvailableDevices */
[[nodiscard]] EnumeratedDeviceList
Expand DownExpand Up@@ -115,9 +116,6 @@ namespace display_device {
std::shared_ptr<WinDisplayDeviceInterface> m_dd_api;
std::shared_ptr<AudioContextInterface> m_audio_context_api;
std::unique_ptr<PersistentState> m_persistence_state;

private:
/** @see win_utils::blankHdrStates for more details. */
std::chrono::milliseconds m_hdr_blank_delay { 500 }; // 500ms should be more than enough...
WinWorkarounds m_workarounds;
};
} // namespace display_device
Original file line numberDiff line numberDiff line change
Expand Up@@ -165,9 +165,10 @@ namespace display_device::win_utils {
*
* @param win_dd Interface for interacting with the OS.
* @param delay Delay between OFF and ON states (ON -> OFF -> DELAY -> ON).
* If null optional is provided, the function does nothing.
*/
void
blankHdrStates(WinDisplayDeviceInterface &win_dd, std::chrono::millisecondsdelay);
blankHdrStates(WinDisplayDeviceInterface &win_dd, const std::optional<std::chrono::milliseconds> &delay);

/**
* @brief Make guard function for the topology.
Expand Down
14 changes: 14 additions & 0 deletions src/windows/include/display_device/windows/types.h
Original file line numberDiff line numberDiff line change
Expand Up@@ -11,6 +11,7 @@
#include <windows.h>

// system includes
#include <chrono>
#include <functional>
#include <map>
#include <set>
Expand DownExpand Up@@ -171,4 +172,17 @@ namespace display_device {
* @brief Default function type used for cleanup/guard functions.
*/
using DdGuardFn = std::function<void()>;

/**
* @brief Settings for workarounds/hacks for Windows.
*/
struct WinWorkarounds {
std::optional<std::chrono::milliseconds> m_hdr_blank_delay { std::nullopt }; ///< @seealso{win_utils::blankHdrStates for more details.}

/**
* @brief Comparator for strict equality.
*/
friend bool
operator==(const WinWorkarounds &lhs, const WinWorkarounds &rhs);
};
} // namespace display_device
1 change: 1 addition & 0 deletions src/windows/json.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -19,4 +19,5 @@ namespace display_device {
DD_JSON_DEFINE_CONVERTER(DeviceDisplayModeMap)
DD_JSON_DEFINE_CONVERTER(HdrStateMap)
DD_JSON_DEFINE_CONVERTER(SingleDisplayConfigState)
DD_JSON_DEFINE_CONVERTER(WinWorkarounds)
} // namespace display_device
1 change: 1 addition & 0 deletions src/windows/json_serializer.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -15,4 +15,5 @@ namespace display_device {
DD_JSON_DEFINE_SERIALIZE_STRUCT(SingleDisplayConfigState::Initial, topology, primary_devices)
DD_JSON_DEFINE_SERIALIZE_STRUCT(SingleDisplayConfigState::Modified, topology, original_modes, original_hdr_states, original_primary_device)
DD_JSON_DEFINE_SERIALIZE_STRUCT(SingleDisplayConfigState, initial, modified)
DD_JSON_DEFINE_SERIALIZE_STRUCT(WinWorkarounds, hdr_blank_delay)
} // namespace display_device
2 changes: 1 addition & 1 deletion src/windows/settings_manager_apply.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -47,7 +47,7 @@ namespace display_device {
bool system_settings_touched { false };
boost::scope::scope_exit hdr_blank_always_executed_guard { [this, &system_settings_touched]() {
if (system_settings_touched) {
win_utils::blankHdrStates(*m_dd_api, m_hdr_blank_delay);
win_utils::blankHdrStates(*m_dd_api, m_workarounds.m_hdr_blank_delay);
}
} };

Expand Down
10 changes: 8 additions & 2 deletions src/windows/settings_manager_general.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -8,15 +8,18 @@
// local includes
#include "display_device/logging.h"
#include "display_device/noop_audio_context.h"
#include "display_device/windows/json.h"

namespace display_device {
SettingsManager::SettingsManager(
std::shared_ptr<WinDisplayDeviceInterface> dd_api,
std::shared_ptr<AudioContextInterface> audio_context_api,
std::unique_ptr<PersistentState> persistent_state):
std::unique_ptr<PersistentState> persistent_state,
WinWorkarounds workarounds):
m_dd_api { std::move(dd_api) },
m_audio_context_api { std::move(audio_context_api) },
m_persistence_state { std::move(persistent_state) } {
m_persistence_state { std::move(persistent_state) },
m_workarounds { std::move(workarounds) } {
if (!m_dd_api) {
throw std::logic_error { "Nullptr provided for WinDisplayDeviceInterface in SettingsManager!" };
}
Expand All@@ -28,6 +31,9 @@ namespace display_device {
if (!m_persistence_state) {
throw std::logic_error { "Nullptr provided for PersistentState in SettingsManager!" };
}

DD_LOG(info) << "Provided workaround settings for SettingsManager:\n"
<< toJson(m_workarounds);
}

EnumeratedDeviceList
Expand Down
2 changes: 1 addition & 1 deletion src/windows/settings_manager_revert.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -47,7 +47,7 @@ namespace display_device {
bool system_settings_touched { false };
boost::scope::scope_exit hdr_blank_always_executed_guard { [this, &system_settings_touched]() {
if (system_settings_touched) {
win_utils::blankHdrStates(*m_dd_api, m_hdr_blank_delay);
win_utils::blankHdrStates(*m_dd_api, m_workarounds.m_hdr_blank_delay);
}
} };
boost::scope::scope_exit topology_prep_guard { [this, &cached_state, &current_topology, &system_settings_touched]() {
Expand Down
10 changes: 7 additions & 3 deletions src/windows/settings_utils.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -358,7 +358,11 @@ namespace display_device::win_utils {
}

void
blankHdrStates(WinDisplayDeviceInterface &win_dd, const std::chrono::milliseconds delay) {
blankHdrStates(WinDisplayDeviceInterface &win_dd, const std::optional<std::chrono::milliseconds> &delay) {
if (!delay) {
return;
}

const auto topology { win_dd.getCurrentTopology() };
if (!win_dd.isTopologyValid(topology)) {
DD_LOG(error) << "Got an invalid topology while trying to blank HDR states!";
Expand DownExpand Up@@ -390,13 +394,13 @@ namespace display_device::win_utils {
return;
}

DD_LOG(info) << "Applying HDR state \"blank\" workaround (" << delay.count() << "ms) to devices: " << toJson(device_ids, JSON_COMPACT);
DD_LOG(info) << "Applying HDR state \"blank\" workaround (" << delay->count() << "ms) to devices: " << toJson(device_ids, JSON_COMPACT);
if (!win_dd.setHdrStates(inverse_states)) {
DD_LOG(error) << "Failed to apply inverse HDR states during \"blank\"!";
return;
}

std::this_thread::sleep_for(delay);
std::this_thread::sleep_for(*delay);
if (!win_dd.setHdrStates(original_states)) {
DD_LOG(error) << "Failed to apply original HDR states during \"blank\"!";
}
Expand Down
5 changes: 5 additions & 0 deletions src/windows/types.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -30,4 +30,9 @@ namespace display_device {
operator==(const SingleDisplayConfigState &lhs, const SingleDisplayConfigState &rhs) {
return lhs.m_initial == rhs.m_initial && lhs.m_modified == rhs.m_modified;
}

bool
operator==(const WinWorkarounds &lhs, const WinWorkarounds &rhs) {
return lhs.m_hdr_blank_delay == rhs.m_hdr_blank_delay;
}
} // namespace display_device
66 changes: 66 additions & 0 deletions tests/unit/general/test_json.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -44,11 +44,25 @@ namespace display_device {
DD_JSON_DEFINE_CONVERTER(TestEnum)
DD_JSON_DEFINE_CONVERTER(TestStruct)
DD_JSON_DEFINE_CONVERTER(TestVariant)
DD_JSON_DEFINE_CONVERTER(std::chrono::nanoseconds)
DD_JSON_DEFINE_CONVERTER(std::chrono::microseconds)
DD_JSON_DEFINE_CONVERTER(std::chrono::milliseconds)
DD_JSON_DEFINE_CONVERTER(std::chrono::seconds)
DD_JSON_DEFINE_CONVERTER(std::chrono::minutes)
DD_JSON_DEFINE_CONVERTER(std::chrono::hours)
DD_JSON_DEFINE_CONVERTER(std::chrono::days)
DD_JSON_DEFINE_CONVERTER(std::chrono::weeks)
DD_JSON_DEFINE_CONVERTER(std::chrono::months)
DD_JSON_DEFINE_CONVERTER(std::chrono::years)
} // namespace display_device

namespace {
// Specialized TEST macro(s) for this test file
#define TEST_S(...) DD_MAKE_TEST(TEST, JsonTest, __VA_ARGS__)

// Additional convenience global const(s)
constexpr auto MIN_NANO_VAL { std::numeric_limits<decltype(std::chrono::nanoseconds {}.count())>::min() };
constexpr auto MAX_NANO_VAL { std::numeric_limits<decltype(std::chrono::nanoseconds {}.count())>::max() };
} // namespace

TEST_S(ToJson, NoError, WithSuccessParam) {
Expand DownExpand Up@@ -192,3 +206,55 @@ TEST_S(FromJson, TestVariant, UnknownVariantType) {
EXPECT_FALSE(display_device::fromJson(R"({"type":"SomeUnknownType","value":123.0})", variant, &error_message));
EXPECT_EQ(error_message, "Could not parse variant from type SomeUnknownType!");
}

TEST_S(ToJson, ChronoDuration) {
using namespace std::chrono;

EXPECT_EQ(display_device::toJson(nanoseconds { 2000000000 }, std::nullopt, nullptr), R"(2000000000)");
EXPECT_EQ(display_device::toJson(microseconds { 2000000 }, std::nullopt, nullptr), R"(2000000)");
EXPECT_EQ(display_device::toJson(milliseconds { 2000 }, std::nullopt, nullptr), R"(2000)");
EXPECT_EQ(display_device::toJson(seconds { 2 }, std::nullopt, nullptr), R"(2)");
EXPECT_EQ(display_device::toJson(minutes { 20 }, std::nullopt, nullptr), R"(20)");
EXPECT_EQ(display_device::toJson(hours { 20 }, std::nullopt, nullptr), R"(20)");
EXPECT_EQ(display_device::toJson(days { 20 }, std::nullopt, nullptr), R"(20)");
EXPECT_EQ(display_device::toJson(weeks { 20 }, std::nullopt, nullptr), R"(20)");
EXPECT_EQ(display_device::toJson(months { 20 }, std::nullopt, nullptr), R"(20)");
EXPECT_EQ(display_device::toJson(years { 20 }, std::nullopt, nullptr), R"(20)");
}

TEST_S(FromJson, ChronoDuration) {
const auto doTest { []<class T>(const std::string &string_input, T expected_value) {
T value {};

EXPECT_TRUE(display_device::fromJson(string_input, value, nullptr));
EXPECT_EQ(value, expected_value);
} };

using namespace std::chrono;

doTest(R"(2000000000)", nanoseconds { 2000000000 });
doTest(R"(2000000)", microseconds { 2000000 });
doTest(R"(2000)", milliseconds { 2000 });
doTest(R"(2)", seconds { 2 });
doTest(R"(20)", minutes { 20 });
doTest(R"(20)", hours { 20 });
doTest(R"(20)", days { 20 });
doTest(R"(20)", weeks { 20 });
doTest(R"(20)", months { 20 });
doTest(R"(20)", years { 20 });
}

TEST_S(ToJson, ChronoDuration, Ranges) {
EXPECT_EQ(display_device::toJson(std::chrono::nanoseconds { MIN_NANO_VAL }, std::nullopt, nullptr), std::to_string(MIN_NANO_VAL));
EXPECT_EQ(display_device::toJson(std::chrono::nanoseconds { MAX_NANO_VAL }, std::nullopt, nullptr), std::to_string(MAX_NANO_VAL));
}

TEST_S(FromJson, ChronoDuration, Ranges) {
std::chrono::nanoseconds value {};

EXPECT_TRUE(display_device::fromJson(std::to_string(MIN_NANO_VAL), value, nullptr));
EXPECT_EQ(value, std::chrono::nanoseconds { MIN_NANO_VAL });

EXPECT_TRUE(display_device::fromJson(std::to_string(MAX_NANO_VAL), value, nullptr));
EXPECT_EQ(value, std::chrono::nanoseconds { MAX_NANO_VAL });
}
31 changes: 31 additions & 0 deletions tests/unit/general/test_json_converter.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -6,6 +6,37 @@ namespace {
#define TEST_F_S(...) DD_MAKE_TEST(TEST_F, JsonConverterTest, __VA_ARGS__)
} // namespace

TEST_F_S(EnumeratedDevice) {
display_device::EnumeratedDevice item_1 {
"ID_1",
"NAME_2",
"FU_NAME_3",
display_device::EnumeratedDevice::Info {
{ 1920, 1080 },
display_device::Rational { 175, 100 },
119.9554,
false,
{ 1, 2 },
display_device::HdrState::Enabled }
};
display_device::EnumeratedDevice item_2 {
"ID_2",
"NAME_2",
"FU_NAME_2",
display_device::EnumeratedDevice::Info {
{ 1920, 1080 },
1.75,
display_device::Rational { 1199554, 10000 },
true,
{ 0, 0 },
display_device::HdrState::Disabled }
};

executeTestCase(display_device::EnumeratedDevice {}, R"({"device_id":"","display_name":"","friendly_name":"","info":null})");
executeTestCase(item_1, R"({"device_id":"ID_1","display_name":"NAME_2","friendly_name":"FU_NAME_3","info":{"hdr_state":"Enabled","origin_point":{"x":1,"y":2},"primary":false,"refresh_rate":{"type":"double","value":119.9554},"resolution":{"height":1080,"width":1920},"resolution_scale":{"type":"rational","value":{"denominator":100,"numerator":175}}}})");
executeTestCase(item_2, R"({"device_id":"ID_2","display_name":"NAME_2","friendly_name":"FU_NAME_2","info":{"hdr_state":"Disabled","origin_point":{"x":0,"y":0},"primary":true,"refresh_rate":{"type":"rational","value":{"denominator":10000,"numerator":1199554}},"resolution":{"height":1080,"width":1920},"resolution_scale":{"type":"double","value":1.75}}})");
}

TEST_F_S(EnumeratedDeviceList) {
display_device::EnumeratedDevice item_1 {
"ID_1",
Expand Down
9 changes: 9 additions & 0 deletions tests/unit/windows/test_json_converter.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -40,3 +40,12 @@ TEST_F_S(SingleDisplayConfigState) {
executeTestCase(display_device::SingleDisplayConfigState {}, R"({"initial":{"primary_devices":[],"topology":[]},"modified":{"original_hdr_states":{},"original_modes":{},"original_primary_device":"","topology":[]}})");
executeTestCase(valid_input, R"({"initial":{"primary_devices":["DeviceId1"],"topology":[["DeviceId1"]]},"modified":{"original_hdr_states":{"DeviceId2":"Disabled"},"original_modes":{"DeviceId2":{"refresh_rate":{"denominator":1,"numerator":120},"resolution":{"height":1080,"width":1920}}},"original_primary_device":"DeviceId2","topology":[["DeviceId2"]]}})");
}

TEST_F_S(WinWorkarounds) {
display_device::WinWorkarounds input {
std::chrono::milliseconds { 500 }
};

executeTestCase(display_device::WinWorkarounds {}, R"({"hdr_blank_delay":null})");
executeTestCase(input, R"({"hdr_blank_delay":500})");
}
5 changes: 4 additions & 1 deletion tests/unit/windows/test_settings_manager_apply.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -47,7 +47,10 @@ namespace {
display_device::SettingsManager &
getImpl() {
if (!m_impl) {
m_impl = std::make_unique<display_device::SettingsManager>(m_dd_api, m_audio_context_api, std::make_unique<display_device::PersistentState>(m_settings_persistence_api));
m_impl = std::make_unique<display_device::SettingsManager>(m_dd_api, m_audio_context_api, std::make_unique<display_device::PersistentState>(m_settings_persistence_api),
display_device::WinWorkarounds {
.m_hdr_blank_delay = std::chrono::milliseconds { 123 } // Value is irrelevant for the tests
});
}

return *m_impl;
Expand Down
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Highlight search terms from Google/DuckDuckGo/Bing referrer\n(function() {\n var ref = document.referrer;\n var terms = [];\n \n if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) {\n var url = new URL(ref);\n var q = url.searchParams.get('q') || url.searchParams.get('p');\n if (q) {\n terms = q.split(/\\s+/).filter(function(t) { return t.length > 2; });\n }\n }\n \n if (terms.length === 0) return;\n \n var style = document.createElement('style');\n style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }';\n document.head.appendChild(style);\n \n function highlight(node) {\n if (node.nodeType === 3) { // text node\n var text = node.textContent;\n var found = false;\n terms.forEach(function(term) {\n var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\') + ')', 'gi');\n if (regex.test(text)) {\n found = true;\n var frag = document.createDocumentFragment();\n var parts = text.split(regex);\n parts.forEach(function(part, i) {\n if (i % 2 === 0) {\n frag.appendChild(document.createTextNode(part));\n } else {\n var span = document.createElement('span');\n span.className = 'userscript-highlight';\n span.textContent = part;\n frag.appendChild(span);\n }\n });\n node.parentNode.replaceChild(frag, node);\n }\n });\n } else if (node.nodeType === 1 && node.childNodes) { // element\n var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT'];\n if (!skipTags.includes(node.tagName)) {\n Array.from(node.childNodes).forEach(highlight);\n }\n }\n }\n \n highlight(document.body);\n \n // Re-highlight on dynamic content\n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1 || node.nodeType === 3) highlight(node);\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Highlight Search Terms"); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
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
Original file line numberDiff line numberDiff line change
Expand Up@@ -109,7 +109,7 @@ namespace nlohmann {
nlohmann_json_t = std::nullopt;
}
else {
nlohmann_json_t = nlohmann_json_j.template get<T>();
nlohmann_json_t = nlohmann_json_j.get<T>();
}
}
};
Expand DownExpand Up@@ -138,5 +138,23 @@ namespace nlohmann {
}
}
};

// Specialization for chrono duration.
template <class Rep, class Period>
struct adl_serializer<std::chrono::duration<Rep, Period>> {
using NanoRep = decltype(std::chrono::nanoseconds {}.count());
static_assert(std::numeric_limits<Rep>::max() <= std::numeric_limits<NanoRep>::max(),
"Duration support above nanoseconds have not been tested/verified yet!");

static void
to_json(json &nlohmann_json_j, const std::chrono::duration<Rep, Period> &nlohmann_json_t) {
nlohmann_json_j = nlohmann_json_t.count();
}

static void
from_json(const json &nlohmann_json_j, std::chrono::duration<Rep, Period> &nlohmann_json_t) {
nlohmann_json_t = std::chrono::duration<Rep, Period> { nlohmann_json_j.get<Rep>() };
}
};
} // namespace nlohmann
#endif
1 change: 1 addition & 0 deletions src/common/include/display_device/json.h
Original file line numberDiff line numberDiff line change
Expand Up@@ -25,6 +25,7 @@
namespace display_device {
extern const std::optional<unsigned int> JSON_COMPACT;

DD_JSON_DECLARE_CONVERTER(EnumeratedDevice)
DD_JSON_DECLARE_CONVERTER(EnumeratedDeviceList)
DD_JSON_DECLARE_CONVERTER(SingleDisplayConfiguration)
DD_JSON_DECLARE_CONVERTER(std::set<std::string>)
Expand Down
1 change: 1 addition & 0 deletions src/common/json.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -13,6 +13,7 @@
// clang-format on

namespace display_device {
DD_JSON_DEFINE_CONVERTER(EnumeratedDevice)
DD_JSON_DEFINE_CONVERTER(EnumeratedDeviceList)
DD_JSON_DEFINE_CONVERTER(SingleDisplayConfiguration)
DD_JSON_DEFINE_CONVERTER(std::set<std::string>)
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -14,5 +14,6 @@ namespace display_device {
DD_JSON_DECLARE_SERIALIZE_TYPE(SingleDisplayConfigState::Initial)
DD_JSON_DECLARE_SERIALIZE_TYPE(SingleDisplayConfigState::Modified)
DD_JSON_DECLARE_SERIALIZE_TYPE(SingleDisplayConfigState)
DD_JSON_DECLARE_SERIALIZE_TYPE(WinWorkarounds)
} // namespace display_device
#endif
1 change: 1 addition & 0 deletions src/windows/include/display_device/windows/json.h
Original file line numberDiff line numberDiff line change
Expand Up@@ -14,4 +14,5 @@ namespace display_device {
DD_JSON_DECLARE_CONVERTER(DeviceDisplayModeMap)
DD_JSON_DECLARE_CONVERTER(HdrStateMap)
DD_JSON_DECLARE_CONVERTER(SingleDisplayConfigState)
DD_JSON_DECLARE_CONVERTER(WinWorkarounds)
} // namespace display_device
10 changes: 4 additions & 6 deletions src/windows/include/display_device/windows/settings_manager.h
Original file line numberDiff line numberDiff line change
Expand Up@@ -5,7 +5,6 @@
#pragma once

// system includes
#include <chrono>
#include <memory>

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

/** For details @see SettingsManagerInterface::enumAvailableDevices */
[[nodiscard]] EnumeratedDeviceList
Expand DownExpand Up@@ -115,9 +116,6 @@ namespace display_device {
std::shared_ptr<WinDisplayDeviceInterface> m_dd_api;
std::shared_ptr<AudioContextInterface> m_audio_context_api;
std::unique_ptr<PersistentState> m_persistence_state;

private:
/** @see win_utils::blankHdrStates for more details. */
std::chrono::milliseconds m_hdr_blank_delay { 500 }; // 500ms should be more than enough...
WinWorkarounds m_workarounds;
};
} // namespace display_device
Original file line numberDiff line numberDiff line change
Expand Up@@ -165,9 +165,10 @@ namespace display_device::win_utils {
*
* @param win_dd Interface for interacting with the OS.
* @param delay Delay between OFF and ON states (ON -> OFF -> DELAY -> ON).
* If null optional is provided, the function does nothing.
*/
void
blankHdrStates(WinDisplayDeviceInterface &win_dd, std::chrono::millisecondsdelay);
blankHdrStates(WinDisplayDeviceInterface &win_dd, const std::optional<std::chrono::milliseconds> &delay);

/**
* @brief Make guard function for the topology.
Expand Down
14 changes: 14 additions & 0 deletions src/windows/include/display_device/windows/types.h
Original file line numberDiff line numberDiff line change
Expand Up@@ -11,6 +11,7 @@
#include <windows.h>

// system includes
#include <chrono>
#include <functional>
#include <map>
#include <set>
Expand DownExpand Up@@ -171,4 +172,17 @@ namespace display_device {
* @brief Default function type used for cleanup/guard functions.
*/
using DdGuardFn = std::function<void()>;

/**
* @brief Settings for workarounds/hacks for Windows.
*/
struct WinWorkarounds {
std::optional<std::chrono::milliseconds> m_hdr_blank_delay { std::nullopt }; ///< @seealso{win_utils::blankHdrStates for more details.}

/**
* @brief Comparator for strict equality.
*/
friend bool
operator==(const WinWorkarounds &lhs, const WinWorkarounds &rhs);
};
} // namespace display_device
1 change: 1 addition & 0 deletions src/windows/json.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -19,4 +19,5 @@ namespace display_device {
DD_JSON_DEFINE_CONVERTER(DeviceDisplayModeMap)
DD_JSON_DEFINE_CONVERTER(HdrStateMap)
DD_JSON_DEFINE_CONVERTER(SingleDisplayConfigState)
DD_JSON_DEFINE_CONVERTER(WinWorkarounds)
} // namespace display_device
1 change: 1 addition & 0 deletions src/windows/json_serializer.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -15,4 +15,5 @@ namespace display_device {
DD_JSON_DEFINE_SERIALIZE_STRUCT(SingleDisplayConfigState::Initial, topology, primary_devices)
DD_JSON_DEFINE_SERIALIZE_STRUCT(SingleDisplayConfigState::Modified, topology, original_modes, original_hdr_states, original_primary_device)
DD_JSON_DEFINE_SERIALIZE_STRUCT(SingleDisplayConfigState, initial, modified)
DD_JSON_DEFINE_SERIALIZE_STRUCT(WinWorkarounds, hdr_blank_delay)
} // namespace display_device
2 changes: 1 addition & 1 deletion src/windows/settings_manager_apply.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -47,7 +47,7 @@ namespace display_device {
bool system_settings_touched { false };
boost::scope::scope_exit hdr_blank_always_executed_guard { [this, &system_settings_touched]() {
if (system_settings_touched) {
win_utils::blankHdrStates(*m_dd_api, m_hdr_blank_delay);
win_utils::blankHdrStates(*m_dd_api, m_workarounds.m_hdr_blank_delay);
}
} };

Expand Down
10 changes: 8 additions & 2 deletions src/windows/settings_manager_general.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -8,15 +8,18 @@
// local includes
#include "display_device/logging.h"
#include "display_device/noop_audio_context.h"
#include "display_device/windows/json.h"

namespace display_device {
SettingsManager::SettingsManager(
std::shared_ptr<WinDisplayDeviceInterface> dd_api,
std::shared_ptr<AudioContextInterface> audio_context_api,
std::unique_ptr<PersistentState> persistent_state):
std::unique_ptr<PersistentState> persistent_state,
WinWorkarounds workarounds):
m_dd_api { std::move(dd_api) },
m_audio_context_api { std::move(audio_context_api) },
m_persistence_state { std::move(persistent_state) } {
m_persistence_state { std::move(persistent_state) },
m_workarounds { std::move(workarounds) } {
if (!m_dd_api) {
throw std::logic_error { "Nullptr provided for WinDisplayDeviceInterface in SettingsManager!" };
}
Expand All@@ -28,6 +31,9 @@ namespace display_device {
if (!m_persistence_state) {
throw std::logic_error { "Nullptr provided for PersistentState in SettingsManager!" };
}

DD_LOG(info) << "Provided workaround settings for SettingsManager:\n"
<< toJson(m_workarounds);
}

EnumeratedDeviceList
Expand Down
2 changes: 1 addition & 1 deletion src/windows/settings_manager_revert.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -47,7 +47,7 @@ namespace display_device {
bool system_settings_touched { false };
boost::scope::scope_exit hdr_blank_always_executed_guard { [this, &system_settings_touched]() {
if (system_settings_touched) {
win_utils::blankHdrStates(*m_dd_api, m_hdr_blank_delay);
win_utils::blankHdrStates(*m_dd_api, m_workarounds.m_hdr_blank_delay);
}
} };
boost::scope::scope_exit topology_prep_guard { [this, &cached_state, &current_topology, &system_settings_touched]() {
Expand Down
10 changes: 7 additions & 3 deletions src/windows/settings_utils.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -358,7 +358,11 @@ namespace display_device::win_utils {
}

void
blankHdrStates(WinDisplayDeviceInterface &win_dd, const std::chrono::milliseconds delay) {
blankHdrStates(WinDisplayDeviceInterface &win_dd, const std::optional<std::chrono::milliseconds> &delay) {
if (!delay) {
return;
}

const auto topology { win_dd.getCurrentTopology() };
if (!win_dd.isTopologyValid(topology)) {
DD_LOG(error) << "Got an invalid topology while trying to blank HDR states!";
Expand DownExpand Up@@ -390,13 +394,13 @@ namespace display_device::win_utils {
return;
}

DD_LOG(info) << "Applying HDR state \"blank\" workaround (" << delay.count() << "ms) to devices: " << toJson(device_ids, JSON_COMPACT);
DD_LOG(info) << "Applying HDR state \"blank\" workaround (" << delay->count() << "ms) to devices: " << toJson(device_ids, JSON_COMPACT);
if (!win_dd.setHdrStates(inverse_states)) {
DD_LOG(error) << "Failed to apply inverse HDR states during \"blank\"!";
return;
}

std::this_thread::sleep_for(delay);
std::this_thread::sleep_for(*delay);
if (!win_dd.setHdrStates(original_states)) {
DD_LOG(error) << "Failed to apply original HDR states during \"blank\"!";
}
Expand Down
5 changes: 5 additions & 0 deletions src/windows/types.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -30,4 +30,9 @@ namespace display_device {
operator==(const SingleDisplayConfigState &lhs, const SingleDisplayConfigState &rhs) {
return lhs.m_initial == rhs.m_initial && lhs.m_modified == rhs.m_modified;
}

bool
operator==(const WinWorkarounds &lhs, const WinWorkarounds &rhs) {
return lhs.m_hdr_blank_delay == rhs.m_hdr_blank_delay;
}
} // namespace display_device
66 changes: 66 additions & 0 deletions tests/unit/general/test_json.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -44,11 +44,25 @@ namespace display_device {
DD_JSON_DEFINE_CONVERTER(TestEnum)
DD_JSON_DEFINE_CONVERTER(TestStruct)
DD_JSON_DEFINE_CONVERTER(TestVariant)
DD_JSON_DEFINE_CONVERTER(std::chrono::nanoseconds)
DD_JSON_DEFINE_CONVERTER(std::chrono::microseconds)
DD_JSON_DEFINE_CONVERTER(std::chrono::milliseconds)
DD_JSON_DEFINE_CONVERTER(std::chrono::seconds)
DD_JSON_DEFINE_CONVERTER(std::chrono::minutes)
DD_JSON_DEFINE_CONVERTER(std::chrono::hours)
DD_JSON_DEFINE_CONVERTER(std::chrono::days)
DD_JSON_DEFINE_CONVERTER(std::chrono::weeks)
DD_JSON_DEFINE_CONVERTER(std::chrono::months)
DD_JSON_DEFINE_CONVERTER(std::chrono::years)
} // namespace display_device

namespace {
// Specialized TEST macro(s) for this test file
#define TEST_S(...) DD_MAKE_TEST(TEST, JsonTest, __VA_ARGS__)

// Additional convenience global const(s)
constexpr auto MIN_NANO_VAL { std::numeric_limits<decltype(std::chrono::nanoseconds {}.count())>::min() };
constexpr auto MAX_NANO_VAL { std::numeric_limits<decltype(std::chrono::nanoseconds {}.count())>::max() };
} // namespace

TEST_S(ToJson, NoError, WithSuccessParam) {
Expand DownExpand Up@@ -192,3 +206,55 @@ TEST_S(FromJson, TestVariant, UnknownVariantType) {
EXPECT_FALSE(display_device::fromJson(R"({"type":"SomeUnknownType","value":123.0})", variant, &error_message));
EXPECT_EQ(error_message, "Could not parse variant from type SomeUnknownType!");
}

TEST_S(ToJson, ChronoDuration) {
using namespace std::chrono;

EXPECT_EQ(display_device::toJson(nanoseconds { 2000000000 }, std::nullopt, nullptr), R"(2000000000)");
EXPECT_EQ(display_device::toJson(microseconds { 2000000 }, std::nullopt, nullptr), R"(2000000)");
EXPECT_EQ(display_device::toJson(milliseconds { 2000 }, std::nullopt, nullptr), R"(2000)");
EXPECT_EQ(display_device::toJson(seconds { 2 }, std::nullopt, nullptr), R"(2)");
EXPECT_EQ(display_device::toJson(minutes { 20 }, std::nullopt, nullptr), R"(20)");
EXPECT_EQ(display_device::toJson(hours { 20 }, std::nullopt, nullptr), R"(20)");
EXPECT_EQ(display_device::toJson(days { 20 }, std::nullopt, nullptr), R"(20)");
EXPECT_EQ(display_device::toJson(weeks { 20 }, std::nullopt, nullptr), R"(20)");
EXPECT_EQ(display_device::toJson(months { 20 }, std::nullopt, nullptr), R"(20)");
EXPECT_EQ(display_device::toJson(years { 20 }, std::nullopt, nullptr), R"(20)");
}

TEST_S(FromJson, ChronoDuration) {
const auto doTest { []<class T>(const std::string &string_input, T expected_value) {
T value {};

EXPECT_TRUE(display_device::fromJson(string_input, value, nullptr));
EXPECT_EQ(value, expected_value);
} };

using namespace std::chrono;

doTest(R"(2000000000)", nanoseconds { 2000000000 });
doTest(R"(2000000)", microseconds { 2000000 });
doTest(R"(2000)", milliseconds { 2000 });
doTest(R"(2)", seconds { 2 });
doTest(R"(20)", minutes { 20 });
doTest(R"(20)", hours { 20 });
doTest(R"(20)", days { 20 });
doTest(R"(20)", weeks { 20 });
doTest(R"(20)", months { 20 });
doTest(R"(20)", years { 20 });
}

TEST_S(ToJson, ChronoDuration, Ranges) {
EXPECT_EQ(display_device::toJson(std::chrono::nanoseconds { MIN_NANO_VAL }, std::nullopt, nullptr), std::to_string(MIN_NANO_VAL));
EXPECT_EQ(display_device::toJson(std::chrono::nanoseconds { MAX_NANO_VAL }, std::nullopt, nullptr), std::to_string(MAX_NANO_VAL));
}

TEST_S(FromJson, ChronoDuration, Ranges) {
std::chrono::nanoseconds value {};

EXPECT_TRUE(display_device::fromJson(std::to_string(MIN_NANO_VAL), value, nullptr));
EXPECT_EQ(value, std::chrono::nanoseconds { MIN_NANO_VAL });

EXPECT_TRUE(display_device::fromJson(std::to_string(MAX_NANO_VAL), value, nullptr));
EXPECT_EQ(value, std::chrono::nanoseconds { MAX_NANO_VAL });
}
31 changes: 31 additions & 0 deletions tests/unit/general/test_json_converter.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -6,6 +6,37 @@ namespace {
#define TEST_F_S(...) DD_MAKE_TEST(TEST_F, JsonConverterTest, __VA_ARGS__)
} // namespace

TEST_F_S(EnumeratedDevice) {
display_device::EnumeratedDevice item_1 {
"ID_1",
"NAME_2",
"FU_NAME_3",
display_device::EnumeratedDevice::Info {
{ 1920, 1080 },
display_device::Rational { 175, 100 },
119.9554,
false,
{ 1, 2 },
display_device::HdrState::Enabled }
};
display_device::EnumeratedDevice item_2 {
"ID_2",
"NAME_2",
"FU_NAME_2",
display_device::EnumeratedDevice::Info {
{ 1920, 1080 },
1.75,
display_device::Rational { 1199554, 10000 },
true,
{ 0, 0 },
display_device::HdrState::Disabled }
};

executeTestCase(display_device::EnumeratedDevice {}, R"({"device_id":"","display_name":"","friendly_name":"","info":null})");
executeTestCase(item_1, R"({"device_id":"ID_1","display_name":"NAME_2","friendly_name":"FU_NAME_3","info":{"hdr_state":"Enabled","origin_point":{"x":1,"y":2},"primary":false,"refresh_rate":{"type":"double","value":119.9554},"resolution":{"height":1080,"width":1920},"resolution_scale":{"type":"rational","value":{"denominator":100,"numerator":175}}}})");
executeTestCase(item_2, R"({"device_id":"ID_2","display_name":"NAME_2","friendly_name":"FU_NAME_2","info":{"hdr_state":"Disabled","origin_point":{"x":0,"y":0},"primary":true,"refresh_rate":{"type":"rational","value":{"denominator":10000,"numerator":1199554}},"resolution":{"height":1080,"width":1920},"resolution_scale":{"type":"double","value":1.75}}})");
}

TEST_F_S(EnumeratedDeviceList) {
display_device::EnumeratedDevice item_1 {
"ID_1",
Expand Down
9 changes: 9 additions & 0 deletions tests/unit/windows/test_json_converter.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -40,3 +40,12 @@ TEST_F_S(SingleDisplayConfigState) {
executeTestCase(display_device::SingleDisplayConfigState {}, R"({"initial":{"primary_devices":[],"topology":[]},"modified":{"original_hdr_states":{},"original_modes":{},"original_primary_device":"","topology":[]}})");
executeTestCase(valid_input, R"({"initial":{"primary_devices":["DeviceId1"],"topology":[["DeviceId1"]]},"modified":{"original_hdr_states":{"DeviceId2":"Disabled"},"original_modes":{"DeviceId2":{"refresh_rate":{"denominator":1,"numerator":120},"resolution":{"height":1080,"width":1920}}},"original_primary_device":"DeviceId2","topology":[["DeviceId2"]]}})");
}

TEST_F_S(WinWorkarounds) {
display_device::WinWorkarounds input {
std::chrono::milliseconds { 500 }
};

executeTestCase(display_device::WinWorkarounds {}, R"({"hdr_blank_delay":null})");
executeTestCase(input, R"({"hdr_blank_delay":500})");
}
5 changes: 4 additions & 1 deletion tests/unit/windows/test_settings_manager_apply.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -47,7 +47,10 @@ namespace {
display_device::SettingsManager &
getImpl() {
if (!m_impl) {
m_impl = std::make_unique<display_device::SettingsManager>(m_dd_api, m_audio_context_api, std::make_unique<display_device::PersistentState>(m_settings_persistence_api));
m_impl = std::make_unique<display_device::SettingsManager>(m_dd_api, m_audio_context_api, std::make_unique<display_device::PersistentState>(m_settings_persistence_api),
display_device::WinWorkarounds {
.m_hdr_blank_delay = std::chrono::milliseconds { 123 } // Value is irrelevant for the tests
});
}

return *m_impl;
Expand Down
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Strip utm_, fbclid, gclid, etc. from all links on page\n(function() {\n var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content',\n 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid',\n 'ref', 'ref_src', 'source', 'medium', 'campaign'];\n \n function cleanUrl(url) {\n try {\n var u = new URL(url, window.location.origin);\n var changed = false;\n trackingParams.forEach(function(p) {\n if (u.searchParams.has(p)) {\n u.searchParams.delete(p);\n changed = true;\n }\n });\n return changed ? u.toString() : url;\n } catch (e) {\n return url;\n }\n }\n \n function cleanLinks() {\n document.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n \n cleanLinks();\n \n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1) {\n if (node.tagName === 'A') cleanLinks();\n node.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Remove Tracking Parameters from Links"); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + '
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
Original file line numberDiff line numberDiff line change
Expand Up@@ -109,7 +109,7 @@ namespace nlohmann {
nlohmann_json_t = std::nullopt;
}
else {
nlohmann_json_t = nlohmann_json_j.template get<T>();
nlohmann_json_t = nlohmann_json_j.get<T>();
}
}
};
Expand DownExpand Up@@ -138,5 +138,23 @@ namespace nlohmann {
}
}
};

// Specialization for chrono duration.
template <class Rep, class Period>
struct adl_serializer<std::chrono::duration<Rep, Period>> {
using NanoRep = decltype(std::chrono::nanoseconds {}.count());
static_assert(std::numeric_limits<Rep>::max() <= std::numeric_limits<NanoRep>::max(),
"Duration support above nanoseconds have not been tested/verified yet!");

static void
to_json(json &nlohmann_json_j, const std::chrono::duration<Rep, Period> &nlohmann_json_t) {
nlohmann_json_j = nlohmann_json_t.count();
}

static void
from_json(const json &nlohmann_json_j, std::chrono::duration<Rep, Period> &nlohmann_json_t) {
nlohmann_json_t = std::chrono::duration<Rep, Period> { nlohmann_json_j.get<Rep>() };
}
};
} // namespace nlohmann
#endif
1 change: 1 addition & 0 deletions src/common/include/display_device/json.h
Original file line numberDiff line numberDiff line change
Expand Up@@ -25,6 +25,7 @@
namespace display_device {
extern const std::optional<unsigned int> JSON_COMPACT;

DD_JSON_DECLARE_CONVERTER(EnumeratedDevice)
DD_JSON_DECLARE_CONVERTER(EnumeratedDeviceList)
DD_JSON_DECLARE_CONVERTER(SingleDisplayConfiguration)
DD_JSON_DECLARE_CONVERTER(std::set<std::string>)
Expand Down
1 change: 1 addition & 0 deletions src/common/json.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -13,6 +13,7 @@
// clang-format on

namespace display_device {
DD_JSON_DEFINE_CONVERTER(EnumeratedDevice)
DD_JSON_DEFINE_CONVERTER(EnumeratedDeviceList)
DD_JSON_DEFINE_CONVERTER(SingleDisplayConfiguration)
DD_JSON_DEFINE_CONVERTER(std::set<std::string>)
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -14,5 +14,6 @@ namespace display_device {
DD_JSON_DECLARE_SERIALIZE_TYPE(SingleDisplayConfigState::Initial)
DD_JSON_DECLARE_SERIALIZE_TYPE(SingleDisplayConfigState::Modified)
DD_JSON_DECLARE_SERIALIZE_TYPE(SingleDisplayConfigState)
DD_JSON_DECLARE_SERIALIZE_TYPE(WinWorkarounds)
} // namespace display_device
#endif
1 change: 1 addition & 0 deletions src/windows/include/display_device/windows/json.h
Original file line numberDiff line numberDiff line change
Expand Up@@ -14,4 +14,5 @@ namespace display_device {
DD_JSON_DECLARE_CONVERTER(DeviceDisplayModeMap)
DD_JSON_DECLARE_CONVERTER(HdrStateMap)
DD_JSON_DECLARE_CONVERTER(SingleDisplayConfigState)
DD_JSON_DECLARE_CONVERTER(WinWorkarounds)
} // namespace display_device
10 changes: 4 additions & 6 deletions src/windows/include/display_device/windows/settings_manager.h
Original file line numberDiff line numberDiff line change
Expand Up@@ -5,7 +5,6 @@
#pragma once

// system includes
#include <chrono>
#include <memory>

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

/** For details @see SettingsManagerInterface::enumAvailableDevices */
[[nodiscard]] EnumeratedDeviceList
Expand DownExpand Up@@ -115,9 +116,6 @@ namespace display_device {
std::shared_ptr<WinDisplayDeviceInterface> m_dd_api;
std::shared_ptr<AudioContextInterface> m_audio_context_api;
std::unique_ptr<PersistentState> m_persistence_state;

private:
/** @see win_utils::blankHdrStates for more details. */
std::chrono::milliseconds m_hdr_blank_delay { 500 }; // 500ms should be more than enough...
WinWorkarounds m_workarounds;
};
} // namespace display_device
Original file line numberDiff line numberDiff line change
Expand Up@@ -165,9 +165,10 @@ namespace display_device::win_utils {
*
* @param win_dd Interface for interacting with the OS.
* @param delay Delay between OFF and ON states (ON -> OFF -> DELAY -> ON).
* If null optional is provided, the function does nothing.
*/
void
blankHdrStates(WinDisplayDeviceInterface &win_dd, std::chrono::millisecondsdelay);
blankHdrStates(WinDisplayDeviceInterface &win_dd, const std::optional<std::chrono::milliseconds> &delay);

/**
* @brief Make guard function for the topology.
Expand Down
14 changes: 14 additions & 0 deletions src/windows/include/display_device/windows/types.h
Original file line numberDiff line numberDiff line change
Expand Up@@ -11,6 +11,7 @@
#include <windows.h>

// system includes
#include <chrono>
#include <functional>
#include <map>
#include <set>
Expand DownExpand Up@@ -171,4 +172,17 @@ namespace display_device {
* @brief Default function type used for cleanup/guard functions.
*/
using DdGuardFn = std::function<void()>;

/**
* @brief Settings for workarounds/hacks for Windows.
*/
struct WinWorkarounds {
std::optional<std::chrono::milliseconds> m_hdr_blank_delay { std::nullopt }; ///< @seealso{win_utils::blankHdrStates for more details.}

/**
* @brief Comparator for strict equality.
*/
friend bool
operator==(const WinWorkarounds &lhs, const WinWorkarounds &rhs);
};
} // namespace display_device
1 change: 1 addition & 0 deletions src/windows/json.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -19,4 +19,5 @@ namespace display_device {
DD_JSON_DEFINE_CONVERTER(DeviceDisplayModeMap)
DD_JSON_DEFINE_CONVERTER(HdrStateMap)
DD_JSON_DEFINE_CONVERTER(SingleDisplayConfigState)
DD_JSON_DEFINE_CONVERTER(WinWorkarounds)
} // namespace display_device
1 change: 1 addition & 0 deletions src/windows/json_serializer.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -15,4 +15,5 @@ namespace display_device {
DD_JSON_DEFINE_SERIALIZE_STRUCT(SingleDisplayConfigState::Initial, topology, primary_devices)
DD_JSON_DEFINE_SERIALIZE_STRUCT(SingleDisplayConfigState::Modified, topology, original_modes, original_hdr_states, original_primary_device)
DD_JSON_DEFINE_SERIALIZE_STRUCT(SingleDisplayConfigState, initial, modified)
DD_JSON_DEFINE_SERIALIZE_STRUCT(WinWorkarounds, hdr_blank_delay)
} // namespace display_device
2 changes: 1 addition & 1 deletion src/windows/settings_manager_apply.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -47,7 +47,7 @@ namespace display_device {
bool system_settings_touched { false };
boost::scope::scope_exit hdr_blank_always_executed_guard { [this, &system_settings_touched]() {
if (system_settings_touched) {
win_utils::blankHdrStates(*m_dd_api, m_hdr_blank_delay);
win_utils::blankHdrStates(*m_dd_api, m_workarounds.m_hdr_blank_delay);
}
} };

Expand Down
10 changes: 8 additions & 2 deletions src/windows/settings_manager_general.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -8,15 +8,18 @@
// local includes
#include "display_device/logging.h"
#include "display_device/noop_audio_context.h"
#include "display_device/windows/json.h"

namespace display_device {
SettingsManager::SettingsManager(
std::shared_ptr<WinDisplayDeviceInterface> dd_api,
std::shared_ptr<AudioContextInterface> audio_context_api,
std::unique_ptr<PersistentState> persistent_state):
std::unique_ptr<PersistentState> persistent_state,
WinWorkarounds workarounds):
m_dd_api { std::move(dd_api) },
m_audio_context_api { std::move(audio_context_api) },
m_persistence_state { std::move(persistent_state) } {
m_persistence_state { std::move(persistent_state) },
m_workarounds { std::move(workarounds) } {
if (!m_dd_api) {
throw std::logic_error { "Nullptr provided for WinDisplayDeviceInterface in SettingsManager!" };
}
Expand All@@ -28,6 +31,9 @@ namespace display_device {
if (!m_persistence_state) {
throw std::logic_error { "Nullptr provided for PersistentState in SettingsManager!" };
}

DD_LOG(info) << "Provided workaround settings for SettingsManager:\n"
<< toJson(m_workarounds);
}

EnumeratedDeviceList
Expand Down
2 changes: 1 addition & 1 deletion src/windows/settings_manager_revert.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -47,7 +47,7 @@ namespace display_device {
bool system_settings_touched { false };
boost::scope::scope_exit hdr_blank_always_executed_guard { [this, &system_settings_touched]() {
if (system_settings_touched) {
win_utils::blankHdrStates(*m_dd_api, m_hdr_blank_delay);
win_utils::blankHdrStates(*m_dd_api, m_workarounds.m_hdr_blank_delay);
}
} };
boost::scope::scope_exit topology_prep_guard { [this, &cached_state, &current_topology, &system_settings_touched]() {
Expand Down
10 changes: 7 additions & 3 deletions src/windows/settings_utils.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -358,7 +358,11 @@ namespace display_device::win_utils {
}

void
blankHdrStates(WinDisplayDeviceInterface &win_dd, const std::chrono::milliseconds delay) {
blankHdrStates(WinDisplayDeviceInterface &win_dd, const std::optional<std::chrono::milliseconds> &delay) {
if (!delay) {
return;
}

const auto topology { win_dd.getCurrentTopology() };
if (!win_dd.isTopologyValid(topology)) {
DD_LOG(error) << "Got an invalid topology while trying to blank HDR states!";
Expand DownExpand Up@@ -390,13 +394,13 @@ namespace display_device::win_utils {
return;
}

DD_LOG(info) << "Applying HDR state \"blank\" workaround (" << delay.count() << "ms) to devices: " << toJson(device_ids, JSON_COMPACT);
DD_LOG(info) << "Applying HDR state \"blank\" workaround (" << delay->count() << "ms) to devices: " << toJson(device_ids, JSON_COMPACT);
if (!win_dd.setHdrStates(inverse_states)) {
DD_LOG(error) << "Failed to apply inverse HDR states during \"blank\"!";
return;
}

std::this_thread::sleep_for(delay);
std::this_thread::sleep_for(*delay);
if (!win_dd.setHdrStates(original_states)) {
DD_LOG(error) << "Failed to apply original HDR states during \"blank\"!";
}
Expand Down
5 changes: 5 additions & 0 deletions src/windows/types.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -30,4 +30,9 @@ namespace display_device {
operator==(const SingleDisplayConfigState &lhs, const SingleDisplayConfigState &rhs) {
return lhs.m_initial == rhs.m_initial && lhs.m_modified == rhs.m_modified;
}

bool
operator==(const WinWorkarounds &lhs, const WinWorkarounds &rhs) {
return lhs.m_hdr_blank_delay == rhs.m_hdr_blank_delay;
}
} // namespace display_device
66 changes: 66 additions & 0 deletions tests/unit/general/test_json.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -44,11 +44,25 @@ namespace display_device {
DD_JSON_DEFINE_CONVERTER(TestEnum)
DD_JSON_DEFINE_CONVERTER(TestStruct)
DD_JSON_DEFINE_CONVERTER(TestVariant)
DD_JSON_DEFINE_CONVERTER(std::chrono::nanoseconds)
DD_JSON_DEFINE_CONVERTER(std::chrono::microseconds)
DD_JSON_DEFINE_CONVERTER(std::chrono::milliseconds)
DD_JSON_DEFINE_CONVERTER(std::chrono::seconds)
DD_JSON_DEFINE_CONVERTER(std::chrono::minutes)
DD_JSON_DEFINE_CONVERTER(std::chrono::hours)
DD_JSON_DEFINE_CONVERTER(std::chrono::days)
DD_JSON_DEFINE_CONVERTER(std::chrono::weeks)
DD_JSON_DEFINE_CONVERTER(std::chrono::months)
DD_JSON_DEFINE_CONVERTER(std::chrono::years)
} // namespace display_device

namespace {
// Specialized TEST macro(s) for this test file
#define TEST_S(...) DD_MAKE_TEST(TEST, JsonTest, __VA_ARGS__)

// Additional convenience global const(s)
constexpr auto MIN_NANO_VAL { std::numeric_limits<decltype(std::chrono::nanoseconds {}.count())>::min() };
constexpr auto MAX_NANO_VAL { std::numeric_limits<decltype(std::chrono::nanoseconds {}.count())>::max() };
} // namespace

TEST_S(ToJson, NoError, WithSuccessParam) {
Expand DownExpand Up@@ -192,3 +206,55 @@ TEST_S(FromJson, TestVariant, UnknownVariantType) {
EXPECT_FALSE(display_device::fromJson(R"({"type":"SomeUnknownType","value":123.0})", variant, &error_message));
EXPECT_EQ(error_message, "Could not parse variant from type SomeUnknownType!");
}

TEST_S(ToJson, ChronoDuration) {
using namespace std::chrono;

EXPECT_EQ(display_device::toJson(nanoseconds { 2000000000 }, std::nullopt, nullptr), R"(2000000000)");
EXPECT_EQ(display_device::toJson(microseconds { 2000000 }, std::nullopt, nullptr), R"(2000000)");
EXPECT_EQ(display_device::toJson(milliseconds { 2000 }, std::nullopt, nullptr), R"(2000)");
EXPECT_EQ(display_device::toJson(seconds { 2 }, std::nullopt, nullptr), R"(2)");
EXPECT_EQ(display_device::toJson(minutes { 20 }, std::nullopt, nullptr), R"(20)");
EXPECT_EQ(display_device::toJson(hours { 20 }, std::nullopt, nullptr), R"(20)");
EXPECT_EQ(display_device::toJson(days { 20 }, std::nullopt, nullptr), R"(20)");
EXPECT_EQ(display_device::toJson(weeks { 20 }, std::nullopt, nullptr), R"(20)");
EXPECT_EQ(display_device::toJson(months { 20 }, std::nullopt, nullptr), R"(20)");
EXPECT_EQ(display_device::toJson(years { 20 }, std::nullopt, nullptr), R"(20)");
}

TEST_S(FromJson, ChronoDuration) {
const auto doTest { []<class T>(const std::string &string_input, T expected_value) {
T value {};

EXPECT_TRUE(display_device::fromJson(string_input, value, nullptr));
EXPECT_EQ(value, expected_value);
} };

using namespace std::chrono;

doTest(R"(2000000000)", nanoseconds { 2000000000 });
doTest(R"(2000000)", microseconds { 2000000 });
doTest(R"(2000)", milliseconds { 2000 });
doTest(R"(2)", seconds { 2 });
doTest(R"(20)", minutes { 20 });
doTest(R"(20)", hours { 20 });
doTest(R"(20)", days { 20 });
doTest(R"(20)", weeks { 20 });
doTest(R"(20)", months { 20 });
doTest(R"(20)", years { 20 });
}

TEST_S(ToJson, ChronoDuration, Ranges) {
EXPECT_EQ(display_device::toJson(std::chrono::nanoseconds { MIN_NANO_VAL }, std::nullopt, nullptr), std::to_string(MIN_NANO_VAL));
EXPECT_EQ(display_device::toJson(std::chrono::nanoseconds { MAX_NANO_VAL }, std::nullopt, nullptr), std::to_string(MAX_NANO_VAL));
}

TEST_S(FromJson, ChronoDuration, Ranges) {
std::chrono::nanoseconds value {};

EXPECT_TRUE(display_device::fromJson(std::to_string(MIN_NANO_VAL), value, nullptr));
EXPECT_EQ(value, std::chrono::nanoseconds { MIN_NANO_VAL });

EXPECT_TRUE(display_device::fromJson(std::to_string(MAX_NANO_VAL), value, nullptr));
EXPECT_EQ(value, std::chrono::nanoseconds { MAX_NANO_VAL });
}
31 changes: 31 additions & 0 deletions tests/unit/general/test_json_converter.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -6,6 +6,37 @@ namespace {
#define TEST_F_S(...) DD_MAKE_TEST(TEST_F, JsonConverterTest, __VA_ARGS__)
} // namespace

TEST_F_S(EnumeratedDevice) {
display_device::EnumeratedDevice item_1 {
"ID_1",
"NAME_2",
"FU_NAME_3",
display_device::EnumeratedDevice::Info {
{ 1920, 1080 },
display_device::Rational { 175, 100 },
119.9554,
false,
{ 1, 2 },
display_device::HdrState::Enabled }
};
display_device::EnumeratedDevice item_2 {
"ID_2",
"NAME_2",
"FU_NAME_2",
display_device::EnumeratedDevice::Info {
{ 1920, 1080 },
1.75,
display_device::Rational { 1199554, 10000 },
true,
{ 0, 0 },
display_device::HdrState::Disabled }
};

executeTestCase(display_device::EnumeratedDevice {}, R"({"device_id":"","display_name":"","friendly_name":"","info":null})");
executeTestCase(item_1, R"({"device_id":"ID_1","display_name":"NAME_2","friendly_name":"FU_NAME_3","info":{"hdr_state":"Enabled","origin_point":{"x":1,"y":2},"primary":false,"refresh_rate":{"type":"double","value":119.9554},"resolution":{"height":1080,"width":1920},"resolution_scale":{"type":"rational","value":{"denominator":100,"numerator":175}}}})");
executeTestCase(item_2, R"({"device_id":"ID_2","display_name":"NAME_2","friendly_name":"FU_NAME_2","info":{"hdr_state":"Disabled","origin_point":{"x":0,"y":0},"primary":true,"refresh_rate":{"type":"rational","value":{"denominator":10000,"numerator":1199554}},"resolution":{"height":1080,"width":1920},"resolution_scale":{"type":"double","value":1.75}}})");
}

TEST_F_S(EnumeratedDeviceList) {
display_device::EnumeratedDevice item_1 {
"ID_1",
Expand Down
9 changes: 9 additions & 0 deletions tests/unit/windows/test_json_converter.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -40,3 +40,12 @@ TEST_F_S(SingleDisplayConfigState) {
executeTestCase(display_device::SingleDisplayConfigState {}, R"({"initial":{"primary_devices":[],"topology":[]},"modified":{"original_hdr_states":{},"original_modes":{},"original_primary_device":"","topology":[]}})");
executeTestCase(valid_input, R"({"initial":{"primary_devices":["DeviceId1"],"topology":[["DeviceId1"]]},"modified":{"original_hdr_states":{"DeviceId2":"Disabled"},"original_modes":{"DeviceId2":{"refresh_rate":{"denominator":1,"numerator":120},"resolution":{"height":1080,"width":1920}}},"original_primary_device":"DeviceId2","topology":[["DeviceId2"]]}})");
}

TEST_F_S(WinWorkarounds) {
display_device::WinWorkarounds input {
std::chrono::milliseconds { 500 }
};

executeTestCase(display_device::WinWorkarounds {}, R"({"hdr_blank_delay":null})");
executeTestCase(input, R"({"hdr_blank_delay":500})");
}
5 changes: 4 additions & 1 deletion tests/unit/windows/test_settings_manager_apply.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -47,7 +47,10 @@ namespace {
display_device::SettingsManager &
getImpl() {
if (!m_impl) {
m_impl = std::make_unique<display_device::SettingsManager>(m_dd_api, m_audio_context_api, std::make_unique<display_device::PersistentState>(m_settings_persistence_api));
m_impl = std::make_unique<display_device::SettingsManager>(m_dd_api, m_audio_context_api, std::make_unique<display_device::PersistentState>(m_settings_persistence_api),
display_device::WinWorkarounds {
.m_hdr_blank_delay = std::chrono::milliseconds { 123 } // Value is irrelevant for the tests
});
}

return *m_impl;
Expand Down
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Auto-enable theater mode on YouTube\n(function() {\n function tryTheater() {\n var btn = document.querySelector('button[aria-label=\"Theater mode\"], ytd-player #player button[title=\"Theater mode\"]');\n if (btn && !btn.classList.contains('activated')) {\n btn.click();\n }\n }\n \n // Try immediately\n tryTheater();\n \n // Try after navigation (SPA)\n var lastUrl = location.href;\n setInterval(function() {\n if (location.href !== lastUrl) {\n lastUrl = location.href;\n setTimeout(tryTheater, 500);\n }\n }, 1000);\n \n // Also try on player load\n var observer = new MutationObserver(tryTheater);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "YouTube Theater Mode Default"); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
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
Original file line numberDiff line numberDiff line change
Expand Up@@ -109,7 +109,7 @@ namespace nlohmann {
nlohmann_json_t = std::nullopt;
}
else {
nlohmann_json_t = nlohmann_json_j.template get<T>();
nlohmann_json_t = nlohmann_json_j.get<T>();
}
}
};
Expand DownExpand Up@@ -138,5 +138,23 @@ namespace nlohmann {
}
}
};

// Specialization for chrono duration.
template <class Rep, class Period>
struct adl_serializer<std::chrono::duration<Rep, Period>> {
using NanoRep = decltype(std::chrono::nanoseconds {}.count());
static_assert(std::numeric_limits<Rep>::max() <= std::numeric_limits<NanoRep>::max(),
"Duration support above nanoseconds have not been tested/verified yet!");

static void
to_json(json &nlohmann_json_j, const std::chrono::duration<Rep, Period> &nlohmann_json_t) {
nlohmann_json_j = nlohmann_json_t.count();
}

static void
from_json(const json &nlohmann_json_j, std::chrono::duration<Rep, Period> &nlohmann_json_t) {
nlohmann_json_t = std::chrono::duration<Rep, Period> { nlohmann_json_j.get<Rep>() };
}
};
} // namespace nlohmann
#endif
1 change: 1 addition & 0 deletions src/common/include/display_device/json.h
Original file line numberDiff line numberDiff line change
Expand Up@@ -25,6 +25,7 @@
namespace display_device {
extern const std::optional<unsigned int> JSON_COMPACT;

DD_JSON_DECLARE_CONVERTER(EnumeratedDevice)
DD_JSON_DECLARE_CONVERTER(EnumeratedDeviceList)
DD_JSON_DECLARE_CONVERTER(SingleDisplayConfiguration)
DD_JSON_DECLARE_CONVERTER(std::set<std::string>)
Expand Down
1 change: 1 addition & 0 deletions src/common/json.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -13,6 +13,7 @@
// clang-format on

namespace display_device {
DD_JSON_DEFINE_CONVERTER(EnumeratedDevice)
DD_JSON_DEFINE_CONVERTER(EnumeratedDeviceList)
DD_JSON_DEFINE_CONVERTER(SingleDisplayConfiguration)
DD_JSON_DEFINE_CONVERTER(std::set<std::string>)
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -14,5 +14,6 @@ namespace display_device {
DD_JSON_DECLARE_SERIALIZE_TYPE(SingleDisplayConfigState::Initial)
DD_JSON_DECLARE_SERIALIZE_TYPE(SingleDisplayConfigState::Modified)
DD_JSON_DECLARE_SERIALIZE_TYPE(SingleDisplayConfigState)
DD_JSON_DECLARE_SERIALIZE_TYPE(WinWorkarounds)
} // namespace display_device
#endif
1 change: 1 addition & 0 deletions src/windows/include/display_device/windows/json.h
Original file line numberDiff line numberDiff line change
Expand Up@@ -14,4 +14,5 @@ namespace display_device {
DD_JSON_DECLARE_CONVERTER(DeviceDisplayModeMap)
DD_JSON_DECLARE_CONVERTER(HdrStateMap)
DD_JSON_DECLARE_CONVERTER(SingleDisplayConfigState)
DD_JSON_DECLARE_CONVERTER(WinWorkarounds)
} // namespace display_device
10 changes: 4 additions & 6 deletions src/windows/include/display_device/windows/settings_manager.h
Original file line numberDiff line numberDiff line change
Expand Up@@ -5,7 +5,6 @@
#pragma once

// system includes
#include <chrono>
#include <memory>

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

/** For details @see SettingsManagerInterface::enumAvailableDevices */
[[nodiscard]] EnumeratedDeviceList
Expand DownExpand Up@@ -115,9 +116,6 @@ namespace display_device {
std::shared_ptr<WinDisplayDeviceInterface> m_dd_api;
std::shared_ptr<AudioContextInterface> m_audio_context_api;
std::unique_ptr<PersistentState> m_persistence_state;

private:
/** @see win_utils::blankHdrStates for more details. */
std::chrono::milliseconds m_hdr_blank_delay { 500 }; // 500ms should be more than enough...
WinWorkarounds m_workarounds;
};
} // namespace display_device
Original file line numberDiff line numberDiff line change
Expand Up@@ -165,9 +165,10 @@ namespace display_device::win_utils {
*
* @param win_dd Interface for interacting with the OS.
* @param delay Delay between OFF and ON states (ON -> OFF -> DELAY -> ON).
* If null optional is provided, the function does nothing.
*/
void
blankHdrStates(WinDisplayDeviceInterface &win_dd, std::chrono::millisecondsdelay);
blankHdrStates(WinDisplayDeviceInterface &win_dd, const std::optional<std::chrono::milliseconds> &delay);

/**
* @brief Make guard function for the topology.
Expand Down
14 changes: 14 additions & 0 deletions src/windows/include/display_device/windows/types.h
Original file line numberDiff line numberDiff line change
Expand Up@@ -11,6 +11,7 @@
#include <windows.h>

// system includes
#include <chrono>
#include <functional>
#include <map>
#include <set>
Expand DownExpand Up@@ -171,4 +172,17 @@ namespace display_device {
* @brief Default function type used for cleanup/guard functions.
*/
using DdGuardFn = std::function<void()>;

/**
* @brief Settings for workarounds/hacks for Windows.
*/
struct WinWorkarounds {
std::optional<std::chrono::milliseconds> m_hdr_blank_delay { std::nullopt }; ///< @seealso{win_utils::blankHdrStates for more details.}

/**
* @brief Comparator for strict equality.
*/
friend bool
operator==(const WinWorkarounds &lhs, const WinWorkarounds &rhs);
};
} // namespace display_device
1 change: 1 addition & 0 deletions src/windows/json.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -19,4 +19,5 @@ namespace display_device {
DD_JSON_DEFINE_CONVERTER(DeviceDisplayModeMap)
DD_JSON_DEFINE_CONVERTER(HdrStateMap)
DD_JSON_DEFINE_CONVERTER(SingleDisplayConfigState)
DD_JSON_DEFINE_CONVERTER(WinWorkarounds)
} // namespace display_device
1 change: 1 addition & 0 deletions src/windows/json_serializer.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -15,4 +15,5 @@ namespace display_device {
DD_JSON_DEFINE_SERIALIZE_STRUCT(SingleDisplayConfigState::Initial, topology, primary_devices)
DD_JSON_DEFINE_SERIALIZE_STRUCT(SingleDisplayConfigState::Modified, topology, original_modes, original_hdr_states, original_primary_device)
DD_JSON_DEFINE_SERIALIZE_STRUCT(SingleDisplayConfigState, initial, modified)
DD_JSON_DEFINE_SERIALIZE_STRUCT(WinWorkarounds, hdr_blank_delay)
} // namespace display_device
2 changes: 1 addition & 1 deletion src/windows/settings_manager_apply.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -47,7 +47,7 @@ namespace display_device {
bool system_settings_touched { false };
boost::scope::scope_exit hdr_blank_always_executed_guard { [this, &system_settings_touched]() {
if (system_settings_touched) {
win_utils::blankHdrStates(*m_dd_api, m_hdr_blank_delay);
win_utils::blankHdrStates(*m_dd_api, m_workarounds.m_hdr_blank_delay);
}
} };

Expand Down
10 changes: 8 additions & 2 deletions src/windows/settings_manager_general.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -8,15 +8,18 @@
// local includes
#include "display_device/logging.h"
#include "display_device/noop_audio_context.h"
#include "display_device/windows/json.h"

namespace display_device {
SettingsManager::SettingsManager(
std::shared_ptr<WinDisplayDeviceInterface> dd_api,
std::shared_ptr<AudioContextInterface> audio_context_api,
std::unique_ptr<PersistentState> persistent_state):
std::unique_ptr<PersistentState> persistent_state,
WinWorkarounds workarounds):
m_dd_api { std::move(dd_api) },
m_audio_context_api { std::move(audio_context_api) },
m_persistence_state { std::move(persistent_state) } {
m_persistence_state { std::move(persistent_state) },
m_workarounds { std::move(workarounds) } {
if (!m_dd_api) {
throw std::logic_error { "Nullptr provided for WinDisplayDeviceInterface in SettingsManager!" };
}
Expand All@@ -28,6 +31,9 @@ namespace display_device {
if (!m_persistence_state) {
throw std::logic_error { "Nullptr provided for PersistentState in SettingsManager!" };
}

DD_LOG(info) << "Provided workaround settings for SettingsManager:\n"
<< toJson(m_workarounds);
}

EnumeratedDeviceList
Expand Down
2 changes: 1 addition & 1 deletion src/windows/settings_manager_revert.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -47,7 +47,7 @@ namespace display_device {
bool system_settings_touched { false };
boost::scope::scope_exit hdr_blank_always_executed_guard { [this, &system_settings_touched]() {
if (system_settings_touched) {
win_utils::blankHdrStates(*m_dd_api, m_hdr_blank_delay);
win_utils::blankHdrStates(*m_dd_api, m_workarounds.m_hdr_blank_delay);
}
} };
boost::scope::scope_exit topology_prep_guard { [this, &cached_state, &current_topology, &system_settings_touched]() {
Expand Down
10 changes: 7 additions & 3 deletions src/windows/settings_utils.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -358,7 +358,11 @@ namespace display_device::win_utils {
}

void
blankHdrStates(WinDisplayDeviceInterface &win_dd, const std::chrono::milliseconds delay) {
blankHdrStates(WinDisplayDeviceInterface &win_dd, const std::optional<std::chrono::milliseconds> &delay) {
if (!delay) {
return;
}

const auto topology { win_dd.getCurrentTopology() };
if (!win_dd.isTopologyValid(topology)) {
DD_LOG(error) << "Got an invalid topology while trying to blank HDR states!";
Expand DownExpand Up@@ -390,13 +394,13 @@ namespace display_device::win_utils {
return;
}

DD_LOG(info) << "Applying HDR state \"blank\" workaround (" << delay.count() << "ms) to devices: " << toJson(device_ids, JSON_COMPACT);
DD_LOG(info) << "Applying HDR state \"blank\" workaround (" << delay->count() << "ms) to devices: " << toJson(device_ids, JSON_COMPACT);
if (!win_dd.setHdrStates(inverse_states)) {
DD_LOG(error) << "Failed to apply inverse HDR states during \"blank\"!";
return;
}

std::this_thread::sleep_for(delay);
std::this_thread::sleep_for(*delay);
if (!win_dd.setHdrStates(original_states)) {
DD_LOG(error) << "Failed to apply original HDR states during \"blank\"!";
}
Expand Down
5 changes: 5 additions & 0 deletions src/windows/types.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -30,4 +30,9 @@ namespace display_device {
operator==(const SingleDisplayConfigState &lhs, const SingleDisplayConfigState &rhs) {
return lhs.m_initial == rhs.m_initial && lhs.m_modified == rhs.m_modified;
}

bool
operator==(const WinWorkarounds &lhs, const WinWorkarounds &rhs) {
return lhs.m_hdr_blank_delay == rhs.m_hdr_blank_delay;
}
} // namespace display_device
66 changes: 66 additions & 0 deletions tests/unit/general/test_json.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -44,11 +44,25 @@ namespace display_device {
DD_JSON_DEFINE_CONVERTER(TestEnum)
DD_JSON_DEFINE_CONVERTER(TestStruct)
DD_JSON_DEFINE_CONVERTER(TestVariant)
DD_JSON_DEFINE_CONVERTER(std::chrono::nanoseconds)
DD_JSON_DEFINE_CONVERTER(std::chrono::microseconds)
DD_JSON_DEFINE_CONVERTER(std::chrono::milliseconds)
DD_JSON_DEFINE_CONVERTER(std::chrono::seconds)
DD_JSON_DEFINE_CONVERTER(std::chrono::minutes)
DD_JSON_DEFINE_CONVERTER(std::chrono::hours)
DD_JSON_DEFINE_CONVERTER(std::chrono::days)
DD_JSON_DEFINE_CONVERTER(std::chrono::weeks)
DD_JSON_DEFINE_CONVERTER(std::chrono::months)
DD_JSON_DEFINE_CONVERTER(std::chrono::years)
} // namespace display_device

namespace {
// Specialized TEST macro(s) for this test file
#define TEST_S(...) DD_MAKE_TEST(TEST, JsonTest, __VA_ARGS__)

// Additional convenience global const(s)
constexpr auto MIN_NANO_VAL { std::numeric_limits<decltype(std::chrono::nanoseconds {}.count())>::min() };
constexpr auto MAX_NANO_VAL { std::numeric_limits<decltype(std::chrono::nanoseconds {}.count())>::max() };
} // namespace

TEST_S(ToJson, NoError, WithSuccessParam) {
Expand DownExpand Up@@ -192,3 +206,55 @@ TEST_S(FromJson, TestVariant, UnknownVariantType) {
EXPECT_FALSE(display_device::fromJson(R"({"type":"SomeUnknownType","value":123.0})", variant, &error_message));
EXPECT_EQ(error_message, "Could not parse variant from type SomeUnknownType!");
}

TEST_S(ToJson, ChronoDuration) {
using namespace std::chrono;

EXPECT_EQ(display_device::toJson(nanoseconds { 2000000000 }, std::nullopt, nullptr), R"(2000000000)");
EXPECT_EQ(display_device::toJson(microseconds { 2000000 }, std::nullopt, nullptr), R"(2000000)");
EXPECT_EQ(display_device::toJson(milliseconds { 2000 }, std::nullopt, nullptr), R"(2000)");
EXPECT_EQ(display_device::toJson(seconds { 2 }, std::nullopt, nullptr), R"(2)");
EXPECT_EQ(display_device::toJson(minutes { 20 }, std::nullopt, nullptr), R"(20)");
EXPECT_EQ(display_device::toJson(hours { 20 }, std::nullopt, nullptr), R"(20)");
EXPECT_EQ(display_device::toJson(days { 20 }, std::nullopt, nullptr), R"(20)");
EXPECT_EQ(display_device::toJson(weeks { 20 }, std::nullopt, nullptr), R"(20)");
EXPECT_EQ(display_device::toJson(months { 20 }, std::nullopt, nullptr), R"(20)");
EXPECT_EQ(display_device::toJson(years { 20 }, std::nullopt, nullptr), R"(20)");
}

TEST_S(FromJson, ChronoDuration) {
const auto doTest { []<class T>(const std::string &string_input, T expected_value) {
T value {};

EXPECT_TRUE(display_device::fromJson(string_input, value, nullptr));
EXPECT_EQ(value, expected_value);
} };

using namespace std::chrono;

doTest(R"(2000000000)", nanoseconds { 2000000000 });
doTest(R"(2000000)", microseconds { 2000000 });
doTest(R"(2000)", milliseconds { 2000 });
doTest(R"(2)", seconds { 2 });
doTest(R"(20)", minutes { 20 });
doTest(R"(20)", hours { 20 });
doTest(R"(20)", days { 20 });
doTest(R"(20)", weeks { 20 });
doTest(R"(20)", months { 20 });
doTest(R"(20)", years { 20 });
}

TEST_S(ToJson, ChronoDuration, Ranges) {
EXPECT_EQ(display_device::toJson(std::chrono::nanoseconds { MIN_NANO_VAL }, std::nullopt, nullptr), std::to_string(MIN_NANO_VAL));
EXPECT_EQ(display_device::toJson(std::chrono::nanoseconds { MAX_NANO_VAL }, std::nullopt, nullptr), std::to_string(MAX_NANO_VAL));
}

TEST_S(FromJson, ChronoDuration, Ranges) {
std::chrono::nanoseconds value {};

EXPECT_TRUE(display_device::fromJson(std::to_string(MIN_NANO_VAL), value, nullptr));
EXPECT_EQ(value, std::chrono::nanoseconds { MIN_NANO_VAL });

EXPECT_TRUE(display_device::fromJson(std::to_string(MAX_NANO_VAL), value, nullptr));
EXPECT_EQ(value, std::chrono::nanoseconds { MAX_NANO_VAL });
}
31 changes: 31 additions & 0 deletions tests/unit/general/test_json_converter.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -6,6 +6,37 @@ namespace {
#define TEST_F_S(...) DD_MAKE_TEST(TEST_F, JsonConverterTest, __VA_ARGS__)
} // namespace

TEST_F_S(EnumeratedDevice) {
display_device::EnumeratedDevice item_1 {
"ID_1",
"NAME_2",
"FU_NAME_3",
display_device::EnumeratedDevice::Info {
{ 1920, 1080 },
display_device::Rational { 175, 100 },
119.9554,
false,
{ 1, 2 },
display_device::HdrState::Enabled }
};
display_device::EnumeratedDevice item_2 {
"ID_2",
"NAME_2",
"FU_NAME_2",
display_device::EnumeratedDevice::Info {
{ 1920, 1080 },
1.75,
display_device::Rational { 1199554, 10000 },
true,
{ 0, 0 },
display_device::HdrState::Disabled }
};

executeTestCase(display_device::EnumeratedDevice {}, R"({"device_id":"","display_name":"","friendly_name":"","info":null})");
executeTestCase(item_1, R"({"device_id":"ID_1","display_name":"NAME_2","friendly_name":"FU_NAME_3","info":{"hdr_state":"Enabled","origin_point":{"x":1,"y":2},"primary":false,"refresh_rate":{"type":"double","value":119.9554},"resolution":{"height":1080,"width":1920},"resolution_scale":{"type":"rational","value":{"denominator":100,"numerator":175}}}})");
executeTestCase(item_2, R"({"device_id":"ID_2","display_name":"NAME_2","friendly_name":"FU_NAME_2","info":{"hdr_state":"Disabled","origin_point":{"x":0,"y":0},"primary":true,"refresh_rate":{"type":"rational","value":{"denominator":10000,"numerator":1199554}},"resolution":{"height":1080,"width":1920},"resolution_scale":{"type":"double","value":1.75}}})");
}

TEST_F_S(EnumeratedDeviceList) {
display_device::EnumeratedDevice item_1 {
"ID_1",
Expand Down
9 changes: 9 additions & 0 deletions tests/unit/windows/test_json_converter.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -40,3 +40,12 @@ TEST_F_S(SingleDisplayConfigState) {
executeTestCase(display_device::SingleDisplayConfigState {}, R"({"initial":{"primary_devices":[],"topology":[]},"modified":{"original_hdr_states":{},"original_modes":{},"original_primary_device":"","topology":[]}})");
executeTestCase(valid_input, R"({"initial":{"primary_devices":["DeviceId1"],"topology":[["DeviceId1"]]},"modified":{"original_hdr_states":{"DeviceId2":"Disabled"},"original_modes":{"DeviceId2":{"refresh_rate":{"denominator":1,"numerator":120},"resolution":{"height":1080,"width":1920}}},"original_primary_device":"DeviceId2","topology":[["DeviceId2"]]}})");
}

TEST_F_S(WinWorkarounds) {
display_device::WinWorkarounds input {
std::chrono::milliseconds { 500 }
};

executeTestCase(display_device::WinWorkarounds {}, R"({"hdr_blank_delay":null})");
executeTestCase(input, R"({"hdr_blank_delay":500})");
}
5 changes: 4 additions & 1 deletion tests/unit/windows/test_settings_manager_apply.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -47,7 +47,10 @@ namespace {
display_device::SettingsManager &
getImpl() {
if (!m_impl) {
m_impl = std::make_unique<display_device::SettingsManager>(m_dd_api, m_audio_context_api, std::make_unique<display_device::PersistentState>(m_settings_persistence_api));
m_impl = std::make_unique<display_device::SettingsManager>(m_dd_api, m_audio_context_api, std::make_unique<display_device::PersistentState>(m_settings_persistence_api),
display_device::WinWorkarounds {
.m_hdr_blank_delay = std::chrono::milliseconds { 123 } // Value is irrelevant for the tests
});
}

return *m_impl;
Expand Down
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Remove or un-stick sticky/fixed headers that block content\n(function() {\n function unstick() {\n document.querySelectorAll('header, nav, [role=\"banner\"], .header, .navbar, .sticky, .fixed-top, [style*=\"position: fixed\"], [style*=\"position:sticky\"]').forEach(function(el) {\n if (el.style.position === 'fixed' || el.style.position === 'sticky' || \n getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') {\n el.style.position = 'static';\n el.style.top = 'auto';\n el.style.zIndex = 'auto';\n }\n });\n }\n \n unstick();\n \n var observer = new MutationObserver(unstick);\n observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] });\n})();", "Kill Sticky Headers"); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
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
Original file line numberDiff line numberDiff line change
Expand Up@@ -109,7 +109,7 @@ namespace nlohmann {
nlohmann_json_t = std::nullopt;
}
else {
nlohmann_json_t = nlohmann_json_j.template get<T>();
nlohmann_json_t = nlohmann_json_j.get<T>();
}
}
};
Expand DownExpand Up@@ -138,5 +138,23 @@ namespace nlohmann {
}
}
};

// Specialization for chrono duration.
template <class Rep, class Period>
struct adl_serializer<std::chrono::duration<Rep, Period>> {
using NanoRep = decltype(std::chrono::nanoseconds {}.count());
static_assert(std::numeric_limits<Rep>::max() <= std::numeric_limits<NanoRep>::max(),
"Duration support above nanoseconds have not been tested/verified yet!");

static void
to_json(json &nlohmann_json_j, const std::chrono::duration<Rep, Period> &nlohmann_json_t) {
nlohmann_json_j = nlohmann_json_t.count();
}

static void
from_json(const json &nlohmann_json_j, std::chrono::duration<Rep, Period> &nlohmann_json_t) {
nlohmann_json_t = std::chrono::duration<Rep, Period> { nlohmann_json_j.get<Rep>() };
}
};
} // namespace nlohmann
#endif
1 change: 1 addition & 0 deletions src/common/include/display_device/json.h
Original file line numberDiff line numberDiff line change
Expand Up@@ -25,6 +25,7 @@
namespace display_device {
extern const std::optional<unsigned int> JSON_COMPACT;

DD_JSON_DECLARE_CONVERTER(EnumeratedDevice)
DD_JSON_DECLARE_CONVERTER(EnumeratedDeviceList)
DD_JSON_DECLARE_CONVERTER(SingleDisplayConfiguration)
DD_JSON_DECLARE_CONVERTER(std::set<std::string>)
Expand Down
1 change: 1 addition & 0 deletions src/common/json.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -13,6 +13,7 @@
// clang-format on

namespace display_device {
DD_JSON_DEFINE_CONVERTER(EnumeratedDevice)
DD_JSON_DEFINE_CONVERTER(EnumeratedDeviceList)
DD_JSON_DEFINE_CONVERTER(SingleDisplayConfiguration)
DD_JSON_DEFINE_CONVERTER(std::set<std::string>)
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -14,5 +14,6 @@ namespace display_device {
DD_JSON_DECLARE_SERIALIZE_TYPE(SingleDisplayConfigState::Initial)
DD_JSON_DECLARE_SERIALIZE_TYPE(SingleDisplayConfigState::Modified)
DD_JSON_DECLARE_SERIALIZE_TYPE(SingleDisplayConfigState)
DD_JSON_DECLARE_SERIALIZE_TYPE(WinWorkarounds)
} // namespace display_device
#endif
1 change: 1 addition & 0 deletions src/windows/include/display_device/windows/json.h
Original file line numberDiff line numberDiff line change
Expand Up@@ -14,4 +14,5 @@ namespace display_device {
DD_JSON_DECLARE_CONVERTER(DeviceDisplayModeMap)
DD_JSON_DECLARE_CONVERTER(HdrStateMap)
DD_JSON_DECLARE_CONVERTER(SingleDisplayConfigState)
DD_JSON_DECLARE_CONVERTER(WinWorkarounds)
} // namespace display_device
10 changes: 4 additions & 6 deletions src/windows/include/display_device/windows/settings_manager.h
Original file line numberDiff line numberDiff line change
Expand Up@@ -5,7 +5,6 @@
#pragma once

// system includes
#include <chrono>
#include <memory>

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

/** For details @see SettingsManagerInterface::enumAvailableDevices */
[[nodiscard]] EnumeratedDeviceList
Expand DownExpand Up@@ -115,9 +116,6 @@ namespace display_device {
std::shared_ptr<WinDisplayDeviceInterface> m_dd_api;
std::shared_ptr<AudioContextInterface> m_audio_context_api;
std::unique_ptr<PersistentState> m_persistence_state;

private:
/** @see win_utils::blankHdrStates for more details. */
std::chrono::milliseconds m_hdr_blank_delay { 500 }; // 500ms should be more than enough...
WinWorkarounds m_workarounds;
};
} // namespace display_device
Original file line numberDiff line numberDiff line change
Expand Up@@ -165,9 +165,10 @@ namespace display_device::win_utils {
*
* @param win_dd Interface for interacting with the OS.
* @param delay Delay between OFF and ON states (ON -> OFF -> DELAY -> ON).
* If null optional is provided, the function does nothing.
*/
void
blankHdrStates(WinDisplayDeviceInterface &win_dd, std::chrono::millisecondsdelay);
blankHdrStates(WinDisplayDeviceInterface &win_dd, const std::optional<std::chrono::milliseconds> &delay);

/**
* @brief Make guard function for the topology.
Expand Down
14 changes: 14 additions & 0 deletions src/windows/include/display_device/windows/types.h
Original file line numberDiff line numberDiff line change
Expand Up@@ -11,6 +11,7 @@
#include <windows.h>

// system includes
#include <chrono>
#include <functional>
#include <map>
#include <set>
Expand DownExpand Up@@ -171,4 +172,17 @@ namespace display_device {
* @brief Default function type used for cleanup/guard functions.
*/
using DdGuardFn = std::function<void()>;

/**
* @brief Settings for workarounds/hacks for Windows.
*/
struct WinWorkarounds {
std::optional<std::chrono::milliseconds> m_hdr_blank_delay { std::nullopt }; ///< @seealso{win_utils::blankHdrStates for more details.}

/**
* @brief Comparator for strict equality.
*/
friend bool
operator==(const WinWorkarounds &lhs, const WinWorkarounds &rhs);
};
} // namespace display_device
1 change: 1 addition & 0 deletions src/windows/json.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -19,4 +19,5 @@ namespace display_device {
DD_JSON_DEFINE_CONVERTER(DeviceDisplayModeMap)
DD_JSON_DEFINE_CONVERTER(HdrStateMap)
DD_JSON_DEFINE_CONVERTER(SingleDisplayConfigState)
DD_JSON_DEFINE_CONVERTER(WinWorkarounds)
} // namespace display_device
1 change: 1 addition & 0 deletions src/windows/json_serializer.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -15,4 +15,5 @@ namespace display_device {
DD_JSON_DEFINE_SERIALIZE_STRUCT(SingleDisplayConfigState::Initial, topology, primary_devices)
DD_JSON_DEFINE_SERIALIZE_STRUCT(SingleDisplayConfigState::Modified, topology, original_modes, original_hdr_states, original_primary_device)
DD_JSON_DEFINE_SERIALIZE_STRUCT(SingleDisplayConfigState, initial, modified)
DD_JSON_DEFINE_SERIALIZE_STRUCT(WinWorkarounds, hdr_blank_delay)
} // namespace display_device
2 changes: 1 addition & 1 deletion src/windows/settings_manager_apply.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -47,7 +47,7 @@ namespace display_device {
bool system_settings_touched { false };
boost::scope::scope_exit hdr_blank_always_executed_guard { [this, &system_settings_touched]() {
if (system_settings_touched) {
win_utils::blankHdrStates(*m_dd_api, m_hdr_blank_delay);
win_utils::blankHdrStates(*m_dd_api, m_workarounds.m_hdr_blank_delay);
}
} };

Expand Down
10 changes: 8 additions & 2 deletions src/windows/settings_manager_general.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -8,15 +8,18 @@
// local includes
#include "display_device/logging.h"
#include "display_device/noop_audio_context.h"
#include "display_device/windows/json.h"

namespace display_device {
SettingsManager::SettingsManager(
std::shared_ptr<WinDisplayDeviceInterface> dd_api,
std::shared_ptr<AudioContextInterface> audio_context_api,
std::unique_ptr<PersistentState> persistent_state):
std::unique_ptr<PersistentState> persistent_state,
WinWorkarounds workarounds):
m_dd_api { std::move(dd_api) },
m_audio_context_api { std::move(audio_context_api) },
m_persistence_state { std::move(persistent_state) } {
m_persistence_state { std::move(persistent_state) },
m_workarounds { std::move(workarounds) } {
if (!m_dd_api) {
throw std::logic_error { "Nullptr provided for WinDisplayDeviceInterface in SettingsManager!" };
}
Expand All@@ -28,6 +31,9 @@ namespace display_device {
if (!m_persistence_state) {
throw std::logic_error { "Nullptr provided for PersistentState in SettingsManager!" };
}

DD_LOG(info) << "Provided workaround settings for SettingsManager:\n"
<< toJson(m_workarounds);
}

EnumeratedDeviceList
Expand Down
2 changes: 1 addition & 1 deletion src/windows/settings_manager_revert.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -47,7 +47,7 @@ namespace display_device {
bool system_settings_touched { false };
boost::scope::scope_exit hdr_blank_always_executed_guard { [this, &system_settings_touched]() {
if (system_settings_touched) {
win_utils::blankHdrStates(*m_dd_api, m_hdr_blank_delay);
win_utils::blankHdrStates(*m_dd_api, m_workarounds.m_hdr_blank_delay);
}
} };
boost::scope::scope_exit topology_prep_guard { [this, &cached_state, &current_topology, &system_settings_touched]() {
Expand Down
10 changes: 7 additions & 3 deletions src/windows/settings_utils.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -358,7 +358,11 @@ namespace display_device::win_utils {
}

void
blankHdrStates(WinDisplayDeviceInterface &win_dd, const std::chrono::milliseconds delay) {
blankHdrStates(WinDisplayDeviceInterface &win_dd, const std::optional<std::chrono::milliseconds> &delay) {
if (!delay) {
return;
}

const auto topology { win_dd.getCurrentTopology() };
if (!win_dd.isTopologyValid(topology)) {
DD_LOG(error) << "Got an invalid topology while trying to blank HDR states!";
Expand DownExpand Up@@ -390,13 +394,13 @@ namespace display_device::win_utils {
return;
}

DD_LOG(info) << "Applying HDR state \"blank\" workaround (" << delay.count() << "ms) to devices: " << toJson(device_ids, JSON_COMPACT);
DD_LOG(info) << "Applying HDR state \"blank\" workaround (" << delay->count() << "ms) to devices: " << toJson(device_ids, JSON_COMPACT);
if (!win_dd.setHdrStates(inverse_states)) {
DD_LOG(error) << "Failed to apply inverse HDR states during \"blank\"!";
return;
}

std::this_thread::sleep_for(delay);
std::this_thread::sleep_for(*delay);
if (!win_dd.setHdrStates(original_states)) {
DD_LOG(error) << "Failed to apply original HDR states during \"blank\"!";
}
Expand Down
5 changes: 5 additions & 0 deletions src/windows/types.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -30,4 +30,9 @@ namespace display_device {
operator==(const SingleDisplayConfigState &lhs, const SingleDisplayConfigState &rhs) {
return lhs.m_initial == rhs.m_initial && lhs.m_modified == rhs.m_modified;
}

bool
operator==(const WinWorkarounds &lhs, const WinWorkarounds &rhs) {
return lhs.m_hdr_blank_delay == rhs.m_hdr_blank_delay;
}
} // namespace display_device
66 changes: 66 additions & 0 deletions tests/unit/general/test_json.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -44,11 +44,25 @@ namespace display_device {
DD_JSON_DEFINE_CONVERTER(TestEnum)
DD_JSON_DEFINE_CONVERTER(TestStruct)
DD_JSON_DEFINE_CONVERTER(TestVariant)
DD_JSON_DEFINE_CONVERTER(std::chrono::nanoseconds)
DD_JSON_DEFINE_CONVERTER(std::chrono::microseconds)
DD_JSON_DEFINE_CONVERTER(std::chrono::milliseconds)
DD_JSON_DEFINE_CONVERTER(std::chrono::seconds)
DD_JSON_DEFINE_CONVERTER(std::chrono::minutes)
DD_JSON_DEFINE_CONVERTER(std::chrono::hours)
DD_JSON_DEFINE_CONVERTER(std::chrono::days)
DD_JSON_DEFINE_CONVERTER(std::chrono::weeks)
DD_JSON_DEFINE_CONVERTER(std::chrono::months)
DD_JSON_DEFINE_CONVERTER(std::chrono::years)
} // namespace display_device

namespace {
// Specialized TEST macro(s) for this test file
#define TEST_S(...) DD_MAKE_TEST(TEST, JsonTest, __VA_ARGS__)

// Additional convenience global const(s)
constexpr auto MIN_NANO_VAL { std::numeric_limits<decltype(std::chrono::nanoseconds {}.count())>::min() };
constexpr auto MAX_NANO_VAL { std::numeric_limits<decltype(std::chrono::nanoseconds {}.count())>::max() };
} // namespace

TEST_S(ToJson, NoError, WithSuccessParam) {
Expand DownExpand Up@@ -192,3 +206,55 @@ TEST_S(FromJson, TestVariant, UnknownVariantType) {
EXPECT_FALSE(display_device::fromJson(R"({"type":"SomeUnknownType","value":123.0})", variant, &error_message));
EXPECT_EQ(error_message, "Could not parse variant from type SomeUnknownType!");
}

TEST_S(ToJson, ChronoDuration) {
using namespace std::chrono;

EXPECT_EQ(display_device::toJson(nanoseconds { 2000000000 }, std::nullopt, nullptr), R"(2000000000)");
EXPECT_EQ(display_device::toJson(microseconds { 2000000 }, std::nullopt, nullptr), R"(2000000)");
EXPECT_EQ(display_device::toJson(milliseconds { 2000 }, std::nullopt, nullptr), R"(2000)");
EXPECT_EQ(display_device::toJson(seconds { 2 }, std::nullopt, nullptr), R"(2)");
EXPECT_EQ(display_device::toJson(minutes { 20 }, std::nullopt, nullptr), R"(20)");
EXPECT_EQ(display_device::toJson(hours { 20 }, std::nullopt, nullptr), R"(20)");
EXPECT_EQ(display_device::toJson(days { 20 }, std::nullopt, nullptr), R"(20)");
EXPECT_EQ(display_device::toJson(weeks { 20 }, std::nullopt, nullptr), R"(20)");
EXPECT_EQ(display_device::toJson(months { 20 }, std::nullopt, nullptr), R"(20)");
EXPECT_EQ(display_device::toJson(years { 20 }, std::nullopt, nullptr), R"(20)");
}

TEST_S(FromJson, ChronoDuration) {
const auto doTest { []<class T>(const std::string &string_input, T expected_value) {
T value {};

EXPECT_TRUE(display_device::fromJson(string_input, value, nullptr));
EXPECT_EQ(value, expected_value);
} };

using namespace std::chrono;

doTest(R"(2000000000)", nanoseconds { 2000000000 });
doTest(R"(2000000)", microseconds { 2000000 });
doTest(R"(2000)", milliseconds { 2000 });
doTest(R"(2)", seconds { 2 });
doTest(R"(20)", minutes { 20 });
doTest(R"(20)", hours { 20 });
doTest(R"(20)", days { 20 });
doTest(R"(20)", weeks { 20 });
doTest(R"(20)", months { 20 });
doTest(R"(20)", years { 20 });
}

TEST_S(ToJson, ChronoDuration, Ranges) {
EXPECT_EQ(display_device::toJson(std::chrono::nanoseconds { MIN_NANO_VAL }, std::nullopt, nullptr), std::to_string(MIN_NANO_VAL));
EXPECT_EQ(display_device::toJson(std::chrono::nanoseconds { MAX_NANO_VAL }, std::nullopt, nullptr), std::to_string(MAX_NANO_VAL));
}

TEST_S(FromJson, ChronoDuration, Ranges) {
std::chrono::nanoseconds value {};

EXPECT_TRUE(display_device::fromJson(std::to_string(MIN_NANO_VAL), value, nullptr));
EXPECT_EQ(value, std::chrono::nanoseconds { MIN_NANO_VAL });

EXPECT_TRUE(display_device::fromJson(std::to_string(MAX_NANO_VAL), value, nullptr));
EXPECT_EQ(value, std::chrono::nanoseconds { MAX_NANO_VAL });
}
31 changes: 31 additions & 0 deletions tests/unit/general/test_json_converter.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -6,6 +6,37 @@ namespace {
#define TEST_F_S(...) DD_MAKE_TEST(TEST_F, JsonConverterTest, __VA_ARGS__)
} // namespace

TEST_F_S(EnumeratedDevice) {
display_device::EnumeratedDevice item_1 {
"ID_1",
"NAME_2",
"FU_NAME_3",
display_device::EnumeratedDevice::Info {
{ 1920, 1080 },
display_device::Rational { 175, 100 },
119.9554,
false,
{ 1, 2 },
display_device::HdrState::Enabled }
};
display_device::EnumeratedDevice item_2 {
"ID_2",
"NAME_2",
"FU_NAME_2",
display_device::EnumeratedDevice::Info {
{ 1920, 1080 },
1.75,
display_device::Rational { 1199554, 10000 },
true,
{ 0, 0 },
display_device::HdrState::Disabled }
};

executeTestCase(display_device::EnumeratedDevice {}, R"({"device_id":"","display_name":"","friendly_name":"","info":null})");
executeTestCase(item_1, R"({"device_id":"ID_1","display_name":"NAME_2","friendly_name":"FU_NAME_3","info":{"hdr_state":"Enabled","origin_point":{"x":1,"y":2},"primary":false,"refresh_rate":{"type":"double","value":119.9554},"resolution":{"height":1080,"width":1920},"resolution_scale":{"type":"rational","value":{"denominator":100,"numerator":175}}}})");
executeTestCase(item_2, R"({"device_id":"ID_2","display_name":"NAME_2","friendly_name":"FU_NAME_2","info":{"hdr_state":"Disabled","origin_point":{"x":0,"y":0},"primary":true,"refresh_rate":{"type":"rational","value":{"denominator":10000,"numerator":1199554}},"resolution":{"height":1080,"width":1920},"resolution_scale":{"type":"double","value":1.75}}})");
}

TEST_F_S(EnumeratedDeviceList) {
display_device::EnumeratedDevice item_1 {
"ID_1",
Expand Down
9 changes: 9 additions & 0 deletions tests/unit/windows/test_json_converter.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -40,3 +40,12 @@ TEST_F_S(SingleDisplayConfigState) {
executeTestCase(display_device::SingleDisplayConfigState {}, R"({"initial":{"primary_devices":[],"topology":[]},"modified":{"original_hdr_states":{},"original_modes":{},"original_primary_device":"","topology":[]}})");
executeTestCase(valid_input, R"({"initial":{"primary_devices":["DeviceId1"],"topology":[["DeviceId1"]]},"modified":{"original_hdr_states":{"DeviceId2":"Disabled"},"original_modes":{"DeviceId2":{"refresh_rate":{"denominator":1,"numerator":120},"resolution":{"height":1080,"width":1920}}},"original_primary_device":"DeviceId2","topology":[["DeviceId2"]]}})");
}

TEST_F_S(WinWorkarounds) {
display_device::WinWorkarounds input {
std::chrono::milliseconds { 500 }
};

executeTestCase(display_device::WinWorkarounds {}, R"({"hdr_blank_delay":null})");
executeTestCase(input, R"({"hdr_blank_delay":500})");
}
5 changes: 4 additions & 1 deletion tests/unit/windows/test_settings_manager_apply.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -47,7 +47,10 @@ namespace {
display_device::SettingsManager &
getImpl() {
if (!m_impl) {
m_impl = std::make_unique<display_device::SettingsManager>(m_dd_api, m_audio_context_api, std::make_unique<display_device::PersistentState>(m_settings_persistence_api));
m_impl = std::make_unique<display_device::SettingsManager>(m_dd_api, m_audio_context_api, std::make_unique<display_device::PersistentState>(m_settings_persistence_api),
display_device::WinWorkarounds {
.m_hdr_blank_delay = std::chrono::milliseconds { 123 } // Value is irrelevant for the tests
});
}

return *m_impl;
Expand Down
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Universal Dark Mode - works on any site\n(function() {\n var enabled = true;\n \n function applyDarkMode() {\n if (!enabled) return;\n \n // Create style element if it doesn't exist\n var style = document.getElementById('universal-dark-mode-style');\n if (!style) {\n style = document.createElement('style');\n style.id = 'universal-dark-mode-style';\n document.head.appendChild(style);\n }\n \n // Dark mode CSS - inverts colors but preserves images/video\n style.textContent = '\n /* Invert everything except media */\n html {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #1a1a2e !important;\n }\n \n /* Restore images, videos, iframes, canvas */\n img, video, iframe, canvas, svg, picture, [style*=\"background-image\"] {\n filter: invert(1) hue-rotate(180deg) !important;\n }\n \n /* Preserve specific elements that should not be inverted */\n .no-dark-mode, .no-dark-mode *,\n [data-theme=\"light\"], [data-theme=\"light\"],\n .ace_editor, .ace_editor *,\n .CodeMirror, .CodeMirror *,\n .monaco-editor, .monaco-editor *,\n .markdown-body pre, .markdown-body pre *,\n .highlight, .highlight *,\n pre code, pre code * {\n filter: none !important;\n }\n \n /* Fix common UI elements */\n .modal, .popup, .dropdown-menu, .tooltip, .popover {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #2d2d44 !important;\n border-color: #444 !important;\n }\n \n /* Scrollbars */\n ::-webkit-scrollbar { background: #1a1a2e !important; }\n ::-webkit-scrollbar-thumb { background: #444 !important; }\n ::-webkit-scrollbar-thumb:hover { background: #555 !important; }\n \n /* Selection */\n ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ';\n }\n \n function removeDarkMode() {\n var style = document.getElementById('universal-dark-mode-style');\n if (style) style.remove();\n }\n \n // Toggle with Alt+Shift+D\n document.addEventListener('keydown', function(e) {\n if (e.altKey && e.shiftKey && e.key === 'D') {\n e.preventDefault();\n enabled = !enabled;\n if (enabled) {\n applyDarkMode();\n console.log('[Universal Dark Mode] Enabled');\n } else {\n removeDarkMode();\n console.log('[Universal Dark Mode] Disabled');\n }\n }\n });\n \n // Apply on load\n applyDarkMode();\n \n // Re-apply on dynamic content\n var observer = new MutationObserver(function(mutations) {\n if (enabled && !document.getElementById('universal-dark-mode-style')) {\n applyDarkMode();\n }\n });\n observer.observe(document.head, { childList: true });\n \n console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle');\n})();", "Universal Dark Mode"); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })();
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
Original file line numberDiff line numberDiff line change
Expand Up@@ -109,7 +109,7 @@ namespace nlohmann {
nlohmann_json_t = std::nullopt;
}
else {
nlohmann_json_t = nlohmann_json_j.template get<T>();
nlohmann_json_t = nlohmann_json_j.get<T>();
}
}
};
Expand DownExpand Up@@ -138,5 +138,23 @@ namespace nlohmann {
}
}
};

// Specialization for chrono duration.
template <class Rep, class Period>
struct adl_serializer<std::chrono::duration<Rep, Period>> {
using NanoRep = decltype(std::chrono::nanoseconds {}.count());
static_assert(std::numeric_limits<Rep>::max() <= std::numeric_limits<NanoRep>::max(),
"Duration support above nanoseconds have not been tested/verified yet!");

static void
to_json(json &nlohmann_json_j, const std::chrono::duration<Rep, Period> &nlohmann_json_t) {
nlohmann_json_j = nlohmann_json_t.count();
}

static void
from_json(const json &nlohmann_json_j, std::chrono::duration<Rep, Period> &nlohmann_json_t) {
nlohmann_json_t = std::chrono::duration<Rep, Period> { nlohmann_json_j.get<Rep>() };
}
};
} // namespace nlohmann
#endif
1 change: 1 addition & 0 deletions src/common/include/display_device/json.h
Original file line numberDiff line numberDiff line change
Expand Up@@ -25,6 +25,7 @@
namespace display_device {
extern const std::optional<unsigned int> JSON_COMPACT;

DD_JSON_DECLARE_CONVERTER(EnumeratedDevice)
DD_JSON_DECLARE_CONVERTER(EnumeratedDeviceList)
DD_JSON_DECLARE_CONVERTER(SingleDisplayConfiguration)
DD_JSON_DECLARE_CONVERTER(std::set<std::string>)
Expand Down
1 change: 1 addition & 0 deletions src/common/json.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -13,6 +13,7 @@
// clang-format on

namespace display_device {
DD_JSON_DEFINE_CONVERTER(EnumeratedDevice)
DD_JSON_DEFINE_CONVERTER(EnumeratedDeviceList)
DD_JSON_DEFINE_CONVERTER(SingleDisplayConfiguration)
DD_JSON_DEFINE_CONVERTER(std::set<std::string>)
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -14,5 +14,6 @@ namespace display_device {
DD_JSON_DECLARE_SERIALIZE_TYPE(SingleDisplayConfigState::Initial)
DD_JSON_DECLARE_SERIALIZE_TYPE(SingleDisplayConfigState::Modified)
DD_JSON_DECLARE_SERIALIZE_TYPE(SingleDisplayConfigState)
DD_JSON_DECLARE_SERIALIZE_TYPE(WinWorkarounds)
} // namespace display_device
#endif
1 change: 1 addition & 0 deletions src/windows/include/display_device/windows/json.h
Original file line numberDiff line numberDiff line change
Expand Up@@ -14,4 +14,5 @@ namespace display_device {
DD_JSON_DECLARE_CONVERTER(DeviceDisplayModeMap)
DD_JSON_DECLARE_CONVERTER(HdrStateMap)
DD_JSON_DECLARE_CONVERTER(SingleDisplayConfigState)
DD_JSON_DECLARE_CONVERTER(WinWorkarounds)
} // namespace display_device
10 changes: 4 additions & 6 deletions src/windows/include/display_device/windows/settings_manager.h
Original file line numberDiff line numberDiff line change
Expand Up@@ -5,7 +5,6 @@
#pragma once

// system includes
#include <chrono>
#include <memory>

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

/** For details @see SettingsManagerInterface::enumAvailableDevices */
[[nodiscard]] EnumeratedDeviceList
Expand DownExpand Up@@ -115,9 +116,6 @@ namespace display_device {
std::shared_ptr<WinDisplayDeviceInterface> m_dd_api;
std::shared_ptr<AudioContextInterface> m_audio_context_api;
std::unique_ptr<PersistentState> m_persistence_state;

private:
/** @see win_utils::blankHdrStates for more details. */
std::chrono::milliseconds m_hdr_blank_delay { 500 }; // 500ms should be more than enough...
WinWorkarounds m_workarounds;
};
} // namespace display_device
Original file line numberDiff line numberDiff line change
Expand Up@@ -165,9 +165,10 @@ namespace display_device::win_utils {
*
* @param win_dd Interface for interacting with the OS.
* @param delay Delay between OFF and ON states (ON -> OFF -> DELAY -> ON).
* If null optional is provided, the function does nothing.
*/
void
blankHdrStates(WinDisplayDeviceInterface &win_dd, std::chrono::millisecondsdelay);
blankHdrStates(WinDisplayDeviceInterface &win_dd, const std::optional<std::chrono::milliseconds> &delay);

/**
* @brief Make guard function for the topology.
Expand Down
14 changes: 14 additions & 0 deletions src/windows/include/display_device/windows/types.h
Original file line numberDiff line numberDiff line change
Expand Up@@ -11,6 +11,7 @@
#include <windows.h>

// system includes
#include <chrono>
#include <functional>
#include <map>
#include <set>
Expand DownExpand Up@@ -171,4 +172,17 @@ namespace display_device {
* @brief Default function type used for cleanup/guard functions.
*/
using DdGuardFn = std::function<void()>;

/**
* @brief Settings for workarounds/hacks for Windows.
*/
struct WinWorkarounds {
std::optional<std::chrono::milliseconds> m_hdr_blank_delay { std::nullopt }; ///< @seealso{win_utils::blankHdrStates for more details.}

/**
* @brief Comparator for strict equality.
*/
friend bool
operator==(const WinWorkarounds &lhs, const WinWorkarounds &rhs);
};
} // namespace display_device
1 change: 1 addition & 0 deletions src/windows/json.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -19,4 +19,5 @@ namespace display_device {
DD_JSON_DEFINE_CONVERTER(DeviceDisplayModeMap)
DD_JSON_DEFINE_CONVERTER(HdrStateMap)
DD_JSON_DEFINE_CONVERTER(SingleDisplayConfigState)
DD_JSON_DEFINE_CONVERTER(WinWorkarounds)
} // namespace display_device
1 change: 1 addition & 0 deletions src/windows/json_serializer.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -15,4 +15,5 @@ namespace display_device {
DD_JSON_DEFINE_SERIALIZE_STRUCT(SingleDisplayConfigState::Initial, topology, primary_devices)
DD_JSON_DEFINE_SERIALIZE_STRUCT(SingleDisplayConfigState::Modified, topology, original_modes, original_hdr_states, original_primary_device)
DD_JSON_DEFINE_SERIALIZE_STRUCT(SingleDisplayConfigState, initial, modified)
DD_JSON_DEFINE_SERIALIZE_STRUCT(WinWorkarounds, hdr_blank_delay)
} // namespace display_device
2 changes: 1 addition & 1 deletion src/windows/settings_manager_apply.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -47,7 +47,7 @@ namespace display_device {
bool system_settings_touched { false };
boost::scope::scope_exit hdr_blank_always_executed_guard { [this, &system_settings_touched]() {
if (system_settings_touched) {
win_utils::blankHdrStates(*m_dd_api, m_hdr_blank_delay);
win_utils::blankHdrStates(*m_dd_api, m_workarounds.m_hdr_blank_delay);
}
} };

Expand Down
10 changes: 8 additions & 2 deletions src/windows/settings_manager_general.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -8,15 +8,18 @@
// local includes
#include "display_device/logging.h"
#include "display_device/noop_audio_context.h"
#include "display_device/windows/json.h"

namespace display_device {
SettingsManager::SettingsManager(
std::shared_ptr<WinDisplayDeviceInterface> dd_api,
std::shared_ptr<AudioContextInterface> audio_context_api,
std::unique_ptr<PersistentState> persistent_state):
std::unique_ptr<PersistentState> persistent_state,
WinWorkarounds workarounds):
m_dd_api { std::move(dd_api) },
m_audio_context_api { std::move(audio_context_api) },
m_persistence_state { std::move(persistent_state) } {
m_persistence_state { std::move(persistent_state) },
m_workarounds { std::move(workarounds) } {
if (!m_dd_api) {
throw std::logic_error { "Nullptr provided for WinDisplayDeviceInterface in SettingsManager!" };
}
Expand All@@ -28,6 +31,9 @@ namespace display_device {
if (!m_persistence_state) {
throw std::logic_error { "Nullptr provided for PersistentState in SettingsManager!" };
}

DD_LOG(info) << "Provided workaround settings for SettingsManager:\n"
<< toJson(m_workarounds);
}

EnumeratedDeviceList
Expand Down
2 changes: 1 addition & 1 deletion src/windows/settings_manager_revert.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -47,7 +47,7 @@ namespace display_device {
bool system_settings_touched { false };
boost::scope::scope_exit hdr_blank_always_executed_guard { [this, &system_settings_touched]() {
if (system_settings_touched) {
win_utils::blankHdrStates(*m_dd_api, m_hdr_blank_delay);
win_utils::blankHdrStates(*m_dd_api, m_workarounds.m_hdr_blank_delay);
}
} };
boost::scope::scope_exit topology_prep_guard { [this, &cached_state, &current_topology, &system_settings_touched]() {
Expand Down
10 changes: 7 additions & 3 deletions src/windows/settings_utils.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -358,7 +358,11 @@ namespace display_device::win_utils {
}

void
blankHdrStates(WinDisplayDeviceInterface &win_dd, const std::chrono::milliseconds delay) {
blankHdrStates(WinDisplayDeviceInterface &win_dd, const std::optional<std::chrono::milliseconds> &delay) {
if (!delay) {
return;
}

const auto topology { win_dd.getCurrentTopology() };
if (!win_dd.isTopologyValid(topology)) {
DD_LOG(error) << "Got an invalid topology while trying to blank HDR states!";
Expand DownExpand Up@@ -390,13 +394,13 @@ namespace display_device::win_utils {
return;
}

DD_LOG(info) << "Applying HDR state \"blank\" workaround (" << delay.count() << "ms) to devices: " << toJson(device_ids, JSON_COMPACT);
DD_LOG(info) << "Applying HDR state \"blank\" workaround (" << delay->count() << "ms) to devices: " << toJson(device_ids, JSON_COMPACT);
if (!win_dd.setHdrStates(inverse_states)) {
DD_LOG(error) << "Failed to apply inverse HDR states during \"blank\"!";
return;
}

std::this_thread::sleep_for(delay);
std::this_thread::sleep_for(*delay);
if (!win_dd.setHdrStates(original_states)) {
DD_LOG(error) << "Failed to apply original HDR states during \"blank\"!";
}
Expand Down
5 changes: 5 additions & 0 deletions src/windows/types.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -30,4 +30,9 @@ namespace display_device {
operator==(const SingleDisplayConfigState &lhs, const SingleDisplayConfigState &rhs) {
return lhs.m_initial == rhs.m_initial && lhs.m_modified == rhs.m_modified;
}

bool
operator==(const WinWorkarounds &lhs, const WinWorkarounds &rhs) {
return lhs.m_hdr_blank_delay == rhs.m_hdr_blank_delay;
}
} // namespace display_device
66 changes: 66 additions & 0 deletions tests/unit/general/test_json.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -44,11 +44,25 @@ namespace display_device {
DD_JSON_DEFINE_CONVERTER(TestEnum)
DD_JSON_DEFINE_CONVERTER(TestStruct)
DD_JSON_DEFINE_CONVERTER(TestVariant)
DD_JSON_DEFINE_CONVERTER(std::chrono::nanoseconds)
DD_JSON_DEFINE_CONVERTER(std::chrono::microseconds)
DD_JSON_DEFINE_CONVERTER(std::chrono::milliseconds)
DD_JSON_DEFINE_CONVERTER(std::chrono::seconds)
DD_JSON_DEFINE_CONVERTER(std::chrono::minutes)
DD_JSON_DEFINE_CONVERTER(std::chrono::hours)
DD_JSON_DEFINE_CONVERTER(std::chrono::days)
DD_JSON_DEFINE_CONVERTER(std::chrono::weeks)
DD_JSON_DEFINE_CONVERTER(std::chrono::months)
DD_JSON_DEFINE_CONVERTER(std::chrono::years)
} // namespace display_device

namespace {
// Specialized TEST macro(s) for this test file
#define TEST_S(...) DD_MAKE_TEST(TEST, JsonTest, __VA_ARGS__)

// Additional convenience global const(s)
constexpr auto MIN_NANO_VAL { std::numeric_limits<decltype(std::chrono::nanoseconds {}.count())>::min() };
constexpr auto MAX_NANO_VAL { std::numeric_limits<decltype(std::chrono::nanoseconds {}.count())>::max() };
} // namespace

TEST_S(ToJson, NoError, WithSuccessParam) {
Expand DownExpand Up@@ -192,3 +206,55 @@ TEST_S(FromJson, TestVariant, UnknownVariantType) {
EXPECT_FALSE(display_device::fromJson(R"({"type":"SomeUnknownType","value":123.0})", variant, &error_message));
EXPECT_EQ(error_message, "Could not parse variant from type SomeUnknownType!");
}

TEST_S(ToJson, ChronoDuration) {
using namespace std::chrono;

EXPECT_EQ(display_device::toJson(nanoseconds { 2000000000 }, std::nullopt, nullptr), R"(2000000000)");
EXPECT_EQ(display_device::toJson(microseconds { 2000000 }, std::nullopt, nullptr), R"(2000000)");
EXPECT_EQ(display_device::toJson(milliseconds { 2000 }, std::nullopt, nullptr), R"(2000)");
EXPECT_EQ(display_device::toJson(seconds { 2 }, std::nullopt, nullptr), R"(2)");
EXPECT_EQ(display_device::toJson(minutes { 20 }, std::nullopt, nullptr), R"(20)");
EXPECT_EQ(display_device::toJson(hours { 20 }, std::nullopt, nullptr), R"(20)");
EXPECT_EQ(display_device::toJson(days { 20 }, std::nullopt, nullptr), R"(20)");
EXPECT_EQ(display_device::toJson(weeks { 20 }, std::nullopt, nullptr), R"(20)");
EXPECT_EQ(display_device::toJson(months { 20 }, std::nullopt, nullptr), R"(20)");
EXPECT_EQ(display_device::toJson(years { 20 }, std::nullopt, nullptr), R"(20)");
}

TEST_S(FromJson, ChronoDuration) {
const auto doTest { []<class T>(const std::string &string_input, T expected_value) {
T value {};

EXPECT_TRUE(display_device::fromJson(string_input, value, nullptr));
EXPECT_EQ(value, expected_value);
} };

using namespace std::chrono;

doTest(R"(2000000000)", nanoseconds { 2000000000 });
doTest(R"(2000000)", microseconds { 2000000 });
doTest(R"(2000)", milliseconds { 2000 });
doTest(R"(2)", seconds { 2 });
doTest(R"(20)", minutes { 20 });
doTest(R"(20)", hours { 20 });
doTest(R"(20)", days { 20 });
doTest(R"(20)", weeks { 20 });
doTest(R"(20)", months { 20 });
doTest(R"(20)", years { 20 });
}

TEST_S(ToJson, ChronoDuration, Ranges) {
EXPECT_EQ(display_device::toJson(std::chrono::nanoseconds { MIN_NANO_VAL }, std::nullopt, nullptr), std::to_string(MIN_NANO_VAL));
EXPECT_EQ(display_device::toJson(std::chrono::nanoseconds { MAX_NANO_VAL }, std::nullopt, nullptr), std::to_string(MAX_NANO_VAL));
}

TEST_S(FromJson, ChronoDuration, Ranges) {
std::chrono::nanoseconds value {};

EXPECT_TRUE(display_device::fromJson(std::to_string(MIN_NANO_VAL), value, nullptr));
EXPECT_EQ(value, std::chrono::nanoseconds { MIN_NANO_VAL });

EXPECT_TRUE(display_device::fromJson(std::to_string(MAX_NANO_VAL), value, nullptr));
EXPECT_EQ(value, std::chrono::nanoseconds { MAX_NANO_VAL });
}
31 changes: 31 additions & 0 deletions tests/unit/general/test_json_converter.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -6,6 +6,37 @@ namespace {
#define TEST_F_S(...) DD_MAKE_TEST(TEST_F, JsonConverterTest, __VA_ARGS__)
} // namespace

TEST_F_S(EnumeratedDevice) {
display_device::EnumeratedDevice item_1 {
"ID_1",
"NAME_2",
"FU_NAME_3",
display_device::EnumeratedDevice::Info {
{ 1920, 1080 },
display_device::Rational { 175, 100 },
119.9554,
false,
{ 1, 2 },
display_device::HdrState::Enabled }
};
display_device::EnumeratedDevice item_2 {
"ID_2",
"NAME_2",
"FU_NAME_2",
display_device::EnumeratedDevice::Info {
{ 1920, 1080 },
1.75,
display_device::Rational { 1199554, 10000 },
true,
{ 0, 0 },
display_device::HdrState::Disabled }
};

executeTestCase(display_device::EnumeratedDevice {}, R"({"device_id":"","display_name":"","friendly_name":"","info":null})");
executeTestCase(item_1, R"({"device_id":"ID_1","display_name":"NAME_2","friendly_name":"FU_NAME_3","info":{"hdr_state":"Enabled","origin_point":{"x":1,"y":2},"primary":false,"refresh_rate":{"type":"double","value":119.9554},"resolution":{"height":1080,"width":1920},"resolution_scale":{"type":"rational","value":{"denominator":100,"numerator":175}}}})");
executeTestCase(item_2, R"({"device_id":"ID_2","display_name":"NAME_2","friendly_name":"FU_NAME_2","info":{"hdr_state":"Disabled","origin_point":{"x":0,"y":0},"primary":true,"refresh_rate":{"type":"rational","value":{"denominator":10000,"numerator":1199554}},"resolution":{"height":1080,"width":1920},"resolution_scale":{"type":"double","value":1.75}}})");
}

TEST_F_S(EnumeratedDeviceList) {
display_device::EnumeratedDevice item_1 {
"ID_1",
Expand Down
9 changes: 9 additions & 0 deletions tests/unit/windows/test_json_converter.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -40,3 +40,12 @@ TEST_F_S(SingleDisplayConfigState) {
executeTestCase(display_device::SingleDisplayConfigState {}, R"({"initial":{"primary_devices":[],"topology":[]},"modified":{"original_hdr_states":{},"original_modes":{},"original_primary_device":"","topology":[]}})");
executeTestCase(valid_input, R"({"initial":{"primary_devices":["DeviceId1"],"topology":[["DeviceId1"]]},"modified":{"original_hdr_states":{"DeviceId2":"Disabled"},"original_modes":{"DeviceId2":{"refresh_rate":{"denominator":1,"numerator":120},"resolution":{"height":1080,"width":1920}}},"original_primary_device":"DeviceId2","topology":[["DeviceId2"]]}})");
}

TEST_F_S(WinWorkarounds) {
display_device::WinWorkarounds input {
std::chrono::milliseconds { 500 }
};

executeTestCase(display_device::WinWorkarounds {}, R"({"hdr_blank_delay":null})");
executeTestCase(input, R"({"hdr_blank_delay":500})");
}
5 changes: 4 additions & 1 deletion tests/unit/windows/test_settings_manager_apply.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -47,7 +47,10 @@ namespace {
display_device::SettingsManager &
getImpl() {
if (!m_impl) {
m_impl = std::make_unique<display_device::SettingsManager>(m_dd_api, m_audio_context_api, std::make_unique<display_device::PersistentState>(m_settings_persistence_api));
m_impl = std::make_unique<display_device::SettingsManager>(m_dd_api, m_audio_context_api, std::make_unique<display_device::PersistentState>(m_settings_persistence_api),
display_device::WinWorkarounds {
.m_hdr_blank_delay = std::chrono::milliseconds { 123 } // Value is irrelevant for the tests
});
}

return *m_impl;
Expand Down
Loading