From b14b04635c537b2fa9f620f03c0167389914e45a Mon Sep 17 00:00:00 2001 From: Dave Marquard Date: Sat, 21 Nov 2026 17:05:56 -0800 Subject: [PATCH 1/3] fix(macOS): keep cg device ids stable across display id changes On Apple Silicon there are no IODisplayConnect services, so displays without an EDID (most of them, and every virtual display) get a macos-cg-* id. Its hash included the CGDirectDisplayID and unit number, which macOS reassigns when a display reconnects or a virtual display is recreated, so the device id changed with them. Leave both out when the display reports a nonzero serial; vendor, model and serial identify it. --- .../display_device/macos/mac_api_utils.h | 21 +++++++++++++++++++ src/macos/mac_api_layer.cpp | 8 +------ src/macos/mac_api_utils.cpp | 12 +++++++++++ tests/unit/macos/test_mac_api_utils.cpp | 14 +++++++++++++ 4 files changed, 48 insertions(+), 7 deletions(-) diff --git a/src/macos/include/display_device/macos/mac_api_utils.h b/src/macos/include/display_device/macos/mac_api_utils.h index de71b31..dcead73 100644 --- a/src/macos/include/display_device/macos/mac_api_utils.h +++ b/src/macos/include/display_device/macos/mac_api_utils.h @@ -4,6 +4,10 @@ */ #pragma once +// system includes +#include +#include + // local includes #include "types.h" @@ -33,4 +37,21 @@ namespace display_device::mac_utils { * @returns True if resolution matches exactly and refresh rate is close enough. */ [[nodiscard]] bool fuzzyCompareModes(const MacDisplayMode &lhs, const MacDisplayMode &rhs); + + /** + * @brief Build the identity text hashed into a "macos-cg-*" device id. + * + * Used when a display has neither an EDID nor IOKit metadata, which on Apple Silicon is most + * displays (there are no IODisplayConnect services) and every virtual display. The display id + * and unit number are reassigned when a display reconnects or a virtual display is recreated, so + * they are only included when the display reports no serial number; vendor, model and a nonzero + * serial already identify it. + * @param vendor CGDisplayVendorNumber value. + * @param model CGDisplayModelNumber value. + * @param serial CGDisplaySerialNumber value. + * @param unit CGDisplayUnitNumber value. + * @param display_id Display id. + * @returns Text to hash. + */ + [[nodiscard]] std::string makeCgDeviceIdentity(std::uint32_t vendor, std::uint32_t model, std::uint32_t serial, std::uint32_t unit, MacDisplayId display_id); } // namespace display_device::mac_utils diff --git a/src/macos/mac_api_layer.cpp b/src/macos/mac_api_layer.cpp index 925b826..43f7f39 100644 --- a/src/macos/mac_api_layer.cpp +++ b/src/macos/mac_api_layer.cpp @@ -588,13 +588,7 @@ namespace display_device { } } - std::ostringstream fallback; - fallback << "vendor=" << CGDisplayVendorNumber(display_id) << ';' - << "model=" << CGDisplayModelNumber(display_id) << ';' - << "serial=" << CGDisplaySerialNumber(display_id) << ';' - << "unit=" << CGDisplayUnitNumber(display_id) << ';' - << "display=" << display_id; - return makeDeviceId("cg", hashText(fallback.str())); + return makeDeviceId("cg", hashText(mac_utils::makeCgDeviceIdentity(CGDisplayVendorNumber(display_id), CGDisplayModelNumber(display_id), CGDisplaySerialNumber(display_id), CGDisplayUnitNumber(display_id), display_id))); } std::optional MacApiLayer::getCurrentDisplayMode(const MacDisplayId display_id) const { diff --git a/src/macos/mac_api_utils.cpp b/src/macos/mac_api_utils.cpp index eec9b97..1dc3656 100644 --- a/src/macos/mac_api_utils.cpp +++ b/src/macos/mac_api_utils.cpp @@ -7,6 +7,7 @@ // system includes #include +#include namespace display_device::mac_utils { bool isSuccess(const MacApiError error_code) { @@ -27,4 +28,15 @@ namespace display_device::mac_utils { return lhs.m_resolution == rhs.m_resolution && fuzzyCompareRefreshRates(lhs.m_refresh_rate, rhs.m_refresh_rate); } + + std::string makeCgDeviceIdentity(const std::uint32_t vendor, const std::uint32_t model, const std::uint32_t serial, const std::uint32_t unit, const MacDisplayId display_id) { + std::ostringstream identity; + identity << "vendor=" << vendor << ';' + << "model=" << model << ';' + << "serial=" << serial; + if (serial == 0) { + identity << ';' << "unit=" << unit << ';' << "display=" << display_id; + } + return identity.str(); + } } // namespace display_device::mac_utils diff --git a/tests/unit/macos/test_mac_api_utils.cpp b/tests/unit/macos/test_mac_api_utils.cpp index 593ae6d..dee1e3b 100644 --- a/tests/unit/macos/test_mac_api_utils.cpp +++ b/tests/unit/macos/test_mac_api_utils.cpp @@ -25,3 +25,17 @@ TEST_S(FuzzyCompareModes) { EXPECT_FALSE(display_device::mac_utils::fuzzyCompareModes({{1920, 720}, {60, 1}}, {{1920, 1080}, {60, 1}})); EXPECT_FALSE(display_device::mac_utils::fuzzyCompareModes({{1920, 1080}, {60, 1}}, {{1920, 1080}, {50, 1}})); } + +TEST_S(MakeCgDeviceIdentity) { + using display_device::mac_utils::makeCgDeviceIdentity; + + // With a serial, the identity survives the display id and unit number changing (reconnects, + // virtual displays being recreated). + EXPECT_EQ(makeCgDeviceIdentity(22084, 1, 1398099505, 2, 9), "vendor=22084;model=1;serial=1398099505"); + EXPECT_EQ(makeCgDeviceIdentity(22084, 1, 1398099505, 2, 9), makeCgDeviceIdentity(22084, 1, 1398099505, 3, 30)); + EXPECT_NE(makeCgDeviceIdentity(22084, 1, 1398099505, 2, 9), makeCgDeviceIdentity(22084, 1, 1398099506, 2, 9)); + + // Without one, the display id and unit number are all that tell identical displays apart. + EXPECT_EQ(makeCgDeviceIdentity(1552, 41007, 0, 0, 1), "vendor=1552;model=41007;serial=0;unit=0;display=1"); + EXPECT_NE(makeCgDeviceIdentity(1552, 41007, 0, 0, 1), makeCgDeviceIdentity(1552, 41007, 0, 1, 2)); +} From 9ebc7d9f32236f2cbd62e7989f79a49c94bf98da Mon Sep 17 00:00:00 2001 From: Dave Marquard Date: Sat, 21 Nov 2026 21:59:11 -0800 Subject: [PATCH 2/3] fix(macOS): keep cg ids distinct for duplicate or reserved serials Treat 0xFFFFFFFF, which Apple documents as an exception value, like 0: neither identifies a display. When another online display reports the same vendor, model and serial, keep the display id and unit number in the identity as before, so both displays are still enumerated and each id resolves to its own display. Only a display with a usable, unique serial gets the stable id. --- .../display_device/macos/mac_api_utils.h | 35 ++++++++++++----- src/macos/mac_api_layer.cpp | 16 +++++++- src/macos/mac_api_utils.cpp | 21 +++++++--- tests/unit/macos/test_mac_api_utils.cpp | 39 +++++++++++++++---- tests/unit/macos/test_mac_display_device.cpp | 34 ++++++++++++++++ 5 files changed, 122 insertions(+), 23 deletions(-) diff --git a/src/macos/include/display_device/macos/mac_api_utils.h b/src/macos/include/display_device/macos/mac_api_utils.h index dcead73..6111508 100644 --- a/src/macos/include/display_device/macos/mac_api_utils.h +++ b/src/macos/include/display_device/macos/mac_api_utils.h @@ -7,6 +7,7 @@ // system includes #include #include +#include // local includes #include "types.h" @@ -38,20 +39,36 @@ namespace display_device::mac_utils { */ [[nodiscard]] bool fuzzyCompareModes(const MacDisplayMode &lhs, const MacDisplayMode &rhs); + /** + * @brief CoreGraphics values that identify a display without an EDID or IOKit metadata. + */ + struct CgDisplayInfo { + MacDisplayId m_display_id; ///< Display id (reassigned when a display reconnects). + std::uint32_t m_vendor; ///< CGDisplayVendorNumber value. + std::uint32_t m_model; ///< CGDisplayModelNumber value. + std::uint32_t m_serial; ///< CGDisplaySerialNumber value. + std::uint32_t m_unit; ///< CGDisplayUnitNumber value (depends on connection order). + }; + + /** + * @brief Check whether a CGDisplaySerialNumber value identifies a display. + * @param serial CGDisplaySerialNumber value. + * @returns False for 0 (no serial) and 0xFFFFFFFF (documented by Apple as an exception value). + */ + [[nodiscard]] bool isUsableCgSerial(std::uint32_t serial); + /** * @brief Build the identity text hashed into a "macos-cg-*" device id. * * Used when a display has neither an EDID nor IOKit metadata, which on Apple Silicon is most * displays (there are no IODisplayConnect services) and every virtual display. The display id - * and unit number are reassigned when a display reconnects or a virtual display is recreated, so - * they are only included when the display reports no serial number; vendor, model and a nonzero - * serial already identify it. - * @param vendor CGDisplayVendorNumber value. - * @param model CGDisplayModelNumber value. - * @param serial CGDisplaySerialNumber value. - * @param unit CGDisplayUnitNumber value. - * @param display_id Display id. + * and unit number change when a display reconnects or a virtual display is recreated, so they are + * left out when vendor, model and serial identify the display on their own: the serial is usable + * and no other online display reports the same vendor, model and serial. Otherwise they are kept, + * so displays that can't be told apart by serial still get distinct ids. + * @param display Display to build the identity for. + * @param online_displays Every online display (may include `display` itself). * @returns Text to hash. */ - [[nodiscard]] std::string makeCgDeviceIdentity(std::uint32_t vendor, std::uint32_t model, std::uint32_t serial, std::uint32_t unit, MacDisplayId display_id); + [[nodiscard]] std::string makeCgDeviceIdentity(const CgDisplayInfo &display, const std::vector &online_displays); } // namespace display_device::mac_utils diff --git a/src/macos/mac_api_layer.cpp b/src/macos/mac_api_layer.cpp index 43f7f39..17876b1 100644 --- a/src/macos/mac_api_layer.cpp +++ b/src/macos/mac_api_layer.cpp @@ -20,6 +20,7 @@ #include #include #include +#include // local includes #include "display_device/logging.h" @@ -460,6 +461,15 @@ namespace display_device { [[nodiscard]] std::string makeDeviceId(const std::string_view prefix, const std::uint64_t hash) { return std::format("macos-{}-{:016x}", prefix, hash); } + + /** + * @brief Read the CoreGraphics identity values of a display. + * @param display_id Display id. + * @return Identity values. + */ + [[nodiscard]] mac_utils::CgDisplayInfo makeCgDisplayInfo(const MacDisplayId display_id) { + return {display_id, CGDisplayVendorNumber(display_id), CGDisplayModelNumber(display_id), CGDisplaySerialNumber(display_id), CGDisplayUnitNumber(display_id)}; + } } // namespace bool MacApiLayer::isApiAccessAvailable() const { @@ -588,7 +598,11 @@ namespace display_device { } } - return makeDeviceId("cg", hashText(mac_utils::makeCgDeviceIdentity(CGDisplayVendorNumber(display_id), CGDisplayModelNumber(display_id), CGDisplaySerialNumber(display_id), CGDisplayUnitNumber(display_id), display_id))); + std::vector online_displays; + for (const auto online_id : getDisplayIds(MacQueryType::Online)) { + online_displays.push_back(makeCgDisplayInfo(online_id)); + } + return makeDeviceId("cg", hashText(mac_utils::makeCgDeviceIdentity(makeCgDisplayInfo(display_id), online_displays))); } std::optional MacApiLayer::getCurrentDisplayMode(const MacDisplayId display_id) const { diff --git a/src/macos/mac_api_utils.cpp b/src/macos/mac_api_utils.cpp index 1dc3656..f7dfba5 100644 --- a/src/macos/mac_api_utils.cpp +++ b/src/macos/mac_api_utils.cpp @@ -6,6 +6,7 @@ #include "display_device/macos/mac_api_utils.h" // system includes +#include #include #include @@ -29,13 +30,21 @@ namespace display_device::mac_utils { fuzzyCompareRefreshRates(lhs.m_refresh_rate, rhs.m_refresh_rate); } - std::string makeCgDeviceIdentity(const std::uint32_t vendor, const std::uint32_t model, const std::uint32_t serial, const std::uint32_t unit, const MacDisplayId display_id) { + bool isUsableCgSerial(const std::uint32_t serial) { + return serial != 0 && serial != 0xFFFFFFFF; + } + + std::string makeCgDeviceIdentity(const CgDisplayInfo &display, const std::vector &online_displays) { + const auto shares_serial {[&display](const CgDisplayInfo &other) { + return other.m_display_id != display.m_display_id && other.m_vendor == display.m_vendor && other.m_model == display.m_model && other.m_serial == display.m_serial; + }}; + std::ostringstream identity; - identity << "vendor=" << vendor << ';' - << "model=" << model << ';' - << "serial=" << serial; - if (serial == 0) { - identity << ';' << "unit=" << unit << ';' << "display=" << display_id; + identity << "vendor=" << display.m_vendor << ';' + << "model=" << display.m_model << ';' + << "serial=" << display.m_serial; + if (!isUsableCgSerial(display.m_serial) || std::ranges::any_of(online_displays, shares_serial)) { + identity << ';' << "unit=" << display.m_unit << ';' << "display=" << display.m_display_id; } return identity.str(); } diff --git a/tests/unit/macos/test_mac_api_utils.cpp b/tests/unit/macos/test_mac_api_utils.cpp index dee1e3b..a2fc9f7 100644 --- a/tests/unit/macos/test_mac_api_utils.cpp +++ b/tests/unit/macos/test_mac_api_utils.cpp @@ -1,3 +1,6 @@ +// system includes +#include + // local includes #include "display_device/macos/mac_api_utils.h" #include "fixtures/fixtures.h" @@ -26,16 +29,38 @@ TEST_S(FuzzyCompareModes) { EXPECT_FALSE(display_device::mac_utils::fuzzyCompareModes({{1920, 1080}, {60, 1}}, {{1920, 1080}, {50, 1}})); } +TEST_S(IsUsableCgSerial) { + EXPECT_TRUE(display_device::mac_utils::isUsableCgSerial(1398099505)); + EXPECT_FALSE(display_device::mac_utils::isUsableCgSerial(0)); + EXPECT_FALSE(display_device::mac_utils::isUsableCgSerial(0xFFFFFFFF)); +} + TEST_S(MakeCgDeviceIdentity) { + using display_device::mac_utils::CgDisplayInfo; using display_device::mac_utils::makeCgDeviceIdentity; - // With a serial, the identity survives the display id and unit number changing (reconnects, + // A unique serial: the identity survives the display id and unit number changing (reconnects, // virtual displays being recreated). - EXPECT_EQ(makeCgDeviceIdentity(22084, 1, 1398099505, 2, 9), "vendor=22084;model=1;serial=1398099505"); - EXPECT_EQ(makeCgDeviceIdentity(22084, 1, 1398099505, 2, 9), makeCgDeviceIdentity(22084, 1, 1398099505, 3, 30)); - EXPECT_NE(makeCgDeviceIdentity(22084, 1, 1398099505, 2, 9), makeCgDeviceIdentity(22084, 1, 1398099506, 2, 9)); + const CgDisplayInfo virtual_display {9, 22084, 1, 1398099505, 2}; + const CgDisplayInfo recreated {30, 22084, 1, 1398099505, 3}; + const CgDisplayInfo other_panel {1, 1552, 41007, 7, 0}; + EXPECT_EQ(makeCgDeviceIdentity(virtual_display, {virtual_display, other_panel}), "vendor=22084;model=1;serial=1398099505"); + EXPECT_EQ(makeCgDeviceIdentity(virtual_display, {virtual_display}), makeCgDeviceIdentity(recreated, {recreated})); + + // No usable serial (0, or Apple's 0xFFFFFFFF exception value): the display id and unit number + // are all that tell identical displays apart. + for (const auto serial : {0U, 0xFFFFFFFFU}) { + const CgDisplayInfo first {1, 1552, 41007, serial, 0}; + const CgDisplayInfo second {2, 1552, 41007, serial, 1}; + EXPECT_EQ(makeCgDeviceIdentity(first, {first}), std::format("vendor=1552;model=41007;serial={};unit=0;display=1", serial)); + EXPECT_NE(makeCgDeviceIdentity(first, {first, second}), makeCgDeviceIdentity(second, {first, second})); + } - // Without one, the display id and unit number are all that tell identical displays apart. - EXPECT_EQ(makeCgDeviceIdentity(1552, 41007, 0, 0, 1), "vendor=1552;model=41007;serial=0;unit=0;display=1"); - EXPECT_NE(makeCgDeviceIdentity(1552, 41007, 0, 0, 1), makeCgDeviceIdentity(1552, 41007, 0, 1, 2)); + // Two online displays reporting the same vendor, model and serial keep distinct identities. + const CgDisplayInfo twin_a {4, 1552, 41007, 16843009, 0}; + const CgDisplayInfo twin_b {5, 1552, 41007, 16843009, 1}; + EXPECT_EQ(makeCgDeviceIdentity(twin_a, {twin_a, twin_b}), "vendor=1552;model=41007;serial=16843009;unit=0;display=4"); + EXPECT_NE(makeCgDeviceIdentity(twin_a, {twin_a, twin_b}), makeCgDeviceIdentity(twin_b, {twin_a, twin_b})); + // Once the twin is gone, the remaining display gets the stable identity again. + EXPECT_EQ(makeCgDeviceIdentity(twin_a, {twin_a}), "vendor=1552;model=41007;serial=16843009"); } diff --git a/tests/unit/macos/test_mac_display_device.cpp b/tests/unit/macos/test_mac_display_device.cpp index 0d9e31a..35975e8 100644 --- a/tests/unit/macos/test_mac_display_device.cpp +++ b/tests/unit/macos/test_mac_display_device.cpp @@ -204,6 +204,40 @@ TEST_F_S(EnumAvailableDevices) { EXPECT_EQ(m_mac_dd.enumAvailableDevices(), expected_list); } +TEST_F_S(EnumAvailableDevices, IdenticalSerialsStayDistinct) { + // Two fallback displays that report the same vendor, model and serial (e.g. two identical panels + // with a fixed serial) must both be listed, and each id must resolve to its own display. + using display_device::mac_utils::CgDisplayInfo; + const CgDisplayInfo twin_a {1, 1552, 41007, 16843009, 0}; + const CgDisplayInfo twin_b {2, 1552, 41007, 16843009, 1}; + const std::string id_a {display_device::mac_utils::makeCgDeviceIdentity(twin_a, {twin_a, twin_b})}; + const std::string id_b {display_device::mac_utils::makeCgDeviceIdentity(twin_b, {twin_a, twin_b})}; + + EXPECT_CALL(*m_layer, getDisplayIds(display_device::MacQueryType::Online)) + .WillRepeatedly(Return(display_device::MacDisplayIdList {1, 2})); + EXPECT_CALL(*m_layer, getDeviceId(1)) + .WillRepeatedly(Return(id_a)); + EXPECT_CALL(*m_layer, getDeviceId(2)) + .WillRepeatedly(Return(id_b)); + EXPECT_CALL(*m_layer, getDisplayName(1)) + .WillRepeatedly(Return("1")); + EXPECT_CALL(*m_layer, getDisplayName(2)) + .WillRepeatedly(Return("2")); + EXPECT_CALL(*m_layer, getFriendlyName(::testing::_)) + .WillRepeatedly(Return("Panel")); + EXPECT_CALL(*m_layer, getEdid(::testing::_)) + .WillRepeatedly(Return(std::vector {})); + EXPECT_CALL(*m_layer, isActive(::testing::_)) + .WillRepeatedly(Return(false)); + + const auto devices {m_mac_dd.enumAvailableDevices()}; + ASSERT_EQ(devices.size(), 2); + EXPECT_EQ(devices[0].m_device_id, id_a); + EXPECT_EQ(devices[1].m_device_id, id_b); + EXPECT_EQ(m_mac_dd.getDisplayName(id_a), "1"); + EXPECT_EQ(m_mac_dd.getDisplayName(id_b), "2"); +} + TEST_F_S(GetDisplayName) { EXPECT_CALL(*m_layer, getDisplayIds(display_device::MacQueryType::Online)) .Times(1) From 58d1e8be371fa9b15cf774740a0973a945da6783 Mon Sep 17 00:00:00 2001 From: Dave Marquard Date: Sun, 22 Nov 2026 07:29:10 -0800 Subject: [PATCH 3/3] fix(macOS): resolve a saved cg id after its twin is plugged in or unplugged Whether a macos-cg-* id includes the display id and unit number depends on whether another online display reports the same vendor, model and serial, so a display that stays connected switches between the two forms when such a display is plugged in or unplugged, and an id saved under the other form stopped resolving. getDisplayId() still matches the current id first. If nothing matches, it compares against each online display's other form (getDeviceIdAlias()) and accepts it only if exactly one online display has it. A serial-only id saved before an identical display was plugged in matches both, so it is logged and left unresolved rather than handed to one of them. Verified with CGVirtualDisplay hotplug on macOS 27.2.1: an id saved while a twin was online now resolves after the twin is unplugged, an id saved alone is unresolved (with a warning) once a twin is plugged in, and an id is never resolved to the other display. --- .../display_device/macos/mac_api_layer.h | 5 ++ .../macos/mac_api_layer_interface.h | 10 +++ .../display_device/macos/mac_api_utils.h | 15 +++++ .../display_device/macos/mac_display_device.h | 3 + src/macos/mac_api_layer.cpp | 30 +++++++-- src/macos/mac_api_utils.cpp | 61 ++++++++++++++---- src/macos/mac_display_device_general.cpp | 24 ++++++- tests/unit/macos/test_mac_api_utils.cpp | 26 ++++++++ tests/unit/macos/test_mac_display_device.cpp | 63 +++++++++++++++++++ tests/unit/macos/utils/mock_mac_api_layer.h | 1 + 10 files changed, 221 insertions(+), 17 deletions(-) diff --git a/src/macos/include/display_device/macos/mac_api_layer.h b/src/macos/include/display_device/macos/mac_api_layer.h index 60fbbc4..608480b 100644 --- a/src/macos/include/display_device/macos/mac_api_layer.h +++ b/src/macos/include/display_device/macos/mac_api_layer.h @@ -48,6 +48,11 @@ namespace display_device { */ [[nodiscard]] std::string getDeviceId(MacDisplayId display_id) const override; + /** + * @copydoc MacApiLayerInterface::getDeviceIdAlias + */ + [[nodiscard]] std::string getDeviceIdAlias(MacDisplayId display_id) const override; + /** * @copydoc MacApiLayerInterface::getCurrentDisplayMode */ diff --git a/src/macos/include/display_device/macos/mac_api_layer_interface.h b/src/macos/include/display_device/macos/mac_api_layer_interface.h index 6d77e2c..e8f761c 100644 --- a/src/macos/include/display_device/macos/mac_api_layer_interface.h +++ b/src/macos/include/display_device/macos/mac_api_layer_interface.h @@ -66,6 +66,16 @@ namespace display_device { */ [[nodiscard]] virtual std::string getDeviceId(MacDisplayId display_id) const = 0; + /** + * @brief Get the other device id a display may have been saved under. + * + * A "macos-cg-*" id depends on whether another online display reports the same vendor, model + * and serial, so a display that stays connected can have had a different id when it was saved. + * @param display_id Display to query. + * @returns The other device id, or empty string if the display only has one. + */ + [[nodiscard]] virtual std::string getDeviceIdAlias(MacDisplayId display_id) const = 0; + /** * @brief Get the current display mode. * @param display_id Display to query. diff --git a/src/macos/include/display_device/macos/mac_api_utils.h b/src/macos/include/display_device/macos/mac_api_utils.h index 6111508..e9bbb64 100644 --- a/src/macos/include/display_device/macos/mac_api_utils.h +++ b/src/macos/include/display_device/macos/mac_api_utils.h @@ -6,6 +6,7 @@ // system includes #include +#include #include #include @@ -71,4 +72,18 @@ namespace display_device::mac_utils { * @returns Text to hash. */ [[nodiscard]] std::string makeCgDeviceIdentity(const CgDisplayInfo &display, const std::vector &online_displays); + + /** + * @brief Build the other identity text a display's "macos-cg-*" device id may have been saved with. + * + * Whether makeCgDeviceIdentity() includes the display id and unit number depends on whether + * another online display reports the same vendor, model and serial, so a display that stays + * connected while such a display is plugged in or unplugged switches between the two forms. This + * returns the form makeCgDeviceIdentity() does not currently give, so a saved id can still be + * matched. Displays without a usable serial only ever have one form. + * @param display Display to build the identity for. + * @param online_displays Every online display (may include `display` itself). + * @returns The other identity text, or nothing if the display has only one. + */ + [[nodiscard]] std::optional makeCgDeviceIdentityAlias(const CgDisplayInfo &display, const std::vector &online_displays); } // namespace display_device::mac_utils diff --git a/src/macos/include/display_device/macos/mac_display_device.h b/src/macos/include/display_device/macos/mac_display_device.h index 3089965..1249831 100644 --- a/src/macos/include/display_device/macos/mac_display_device.h +++ b/src/macos/include/display_device/macos/mac_display_device.h @@ -92,6 +92,9 @@ namespace display_device { private: /** * @brief Resolve a library device id to a CoreGraphics display id. + * + * Matches the display's current device id first, then its alias (see + * MacApiLayerInterface::getDeviceIdAlias) if exactly one online display has it. * @param device_id Device id to resolve. * @param query_type Display list type to search. * @return Display id, or empty optional if not found. diff --git a/src/macos/mac_api_layer.cpp b/src/macos/mac_api_layer.cpp index 17876b1..b301b7a 100644 --- a/src/macos/mac_api_layer.cpp +++ b/src/macos/mac_api_layer.cpp @@ -470,6 +470,19 @@ namespace display_device { [[nodiscard]] mac_utils::CgDisplayInfo makeCgDisplayInfo(const MacDisplayId display_id) { return {display_id, CGDisplayVendorNumber(display_id), CGDisplayModelNumber(display_id), CGDisplaySerialNumber(display_id), CGDisplayUnitNumber(display_id)}; } + + /** + * @brief Read the CoreGraphics identity values of every online display. + * @param layer API layer to list the displays with. + * @return Identity values. + */ + [[nodiscard]] std::vector makeOnlineCgDisplayInfos(const MacApiLayer &layer) { + std::vector online_displays; + for (const auto online_id : layer.getDisplayIds(MacQueryType::Online)) { + online_displays.push_back(makeCgDisplayInfo(online_id)); + } + return online_displays; + } } // namespace bool MacApiLayer::isApiAccessAvailable() const { @@ -598,11 +611,20 @@ namespace display_device { } } - std::vector online_displays; - for (const auto online_id : getDisplayIds(MacQueryType::Online)) { - online_displays.push_back(makeCgDisplayInfo(online_id)); + return makeDeviceId("cg", hashText(mac_utils::makeCgDeviceIdentity(makeCgDisplayInfo(display_id), makeOnlineCgDisplayInfos(*this)))); + } + + std::string MacApiLayer::getDeviceIdAlias(const MacDisplayId display_id) const { + // Only "macos-cg-*" ids have an alias; keep in step with getDeviceId(). + if (!getEdid(display_id).empty()) { + return {}; } - return makeDeviceId("cg", hashText(mac_utils::makeCgDeviceIdentity(makeCgDisplayInfo(display_id), online_displays))); + if (const auto dictionary {copyDisplayInfo(display_id)}; dictionary && !makeIokitMetadata(dictionary.get()).empty()) { + return {}; + } + + const auto alias {mac_utils::makeCgDeviceIdentityAlias(makeCgDisplayInfo(display_id), makeOnlineCgDisplayInfos(*this))}; + return alias ? makeDeviceId("cg", hashText(*alias)) : std::string {}; } std::optional MacApiLayer::getCurrentDisplayMode(const MacDisplayId display_id) const { diff --git a/src/macos/mac_api_utils.cpp b/src/macos/mac_api_utils.cpp index f7dfba5..af2da9f 100644 --- a/src/macos/mac_api_utils.cpp +++ b/src/macos/mac_api_utils.cpp @@ -34,18 +34,57 @@ namespace display_device::mac_utils { return serial != 0 && serial != 0xFFFFFFFF; } + namespace { + /** + * @brief Build the identity text from vendor, model and serial only. + * @param display Display to build the identity for. + * @returns Identity text. + */ + [[nodiscard]] std::string makeSerialIdentity(const CgDisplayInfo &display) { + std::ostringstream identity; + identity << "vendor=" << display.m_vendor << ';' + << "model=" << display.m_model << ';' + << "serial=" << display.m_serial; + return identity.str(); + } + + /** + * @brief Build the identity text that also includes the unit number and display id. + * @param display Display to build the identity for. + * @returns Identity text. + */ + [[nodiscard]] std::string makeQualifiedIdentity(const CgDisplayInfo &display) { + std::ostringstream identity; + identity << makeSerialIdentity(display) << ';' + << "unit=" << display.m_unit << ';' + << "display=" << display.m_display_id; + return identity.str(); + } + + /** + * @brief Check whether another online display reports the same vendor, model and serial. + * @param display Display to check. + * @param online_displays Every online display (may include `display` itself). + * @returns True if such a display is online. + */ + [[nodiscard]] bool hasTwinOnline(const CgDisplayInfo &display, const std::vector &online_displays) { + return std::ranges::any_of(online_displays, [&display](const CgDisplayInfo &other) { + return other.m_display_id != display.m_display_id && other.m_vendor == display.m_vendor && other.m_model == display.m_model && other.m_serial == display.m_serial; + }); + } + } // namespace + std::string makeCgDeviceIdentity(const CgDisplayInfo &display, const std::vector &online_displays) { - const auto shares_serial {[&display](const CgDisplayInfo &other) { - return other.m_display_id != display.m_display_id && other.m_vendor == display.m_vendor && other.m_model == display.m_model && other.m_serial == display.m_serial; - }}; - - std::ostringstream identity; - identity << "vendor=" << display.m_vendor << ';' - << "model=" << display.m_model << ';' - << "serial=" << display.m_serial; - if (!isUsableCgSerial(display.m_serial) || std::ranges::any_of(online_displays, shares_serial)) { - identity << ';' << "unit=" << display.m_unit << ';' << "display=" << display.m_display_id; + if (!isUsableCgSerial(display.m_serial) || hasTwinOnline(display, online_displays)) { + return makeQualifiedIdentity(display); + } + return makeSerialIdentity(display); + } + + std::optional makeCgDeviceIdentityAlias(const CgDisplayInfo &display, const std::vector &online_displays) { + if (!isUsableCgSerial(display.m_serial)) { + return std::nullopt; } - return identity.str(); + return hasTwinOnline(display, online_displays) ? makeSerialIdentity(display) : makeQualifiedIdentity(display); } } // namespace display_device::mac_utils diff --git a/src/macos/mac_display_device_general.cpp b/src/macos/mac_display_device_general.cpp index 506da51..5e3a12b 100644 --- a/src/macos/mac_display_device_general.cpp +++ b/src/macos/mac_display_device_general.cpp @@ -6,6 +6,7 @@ #include "display_device/macos/mac_display_device.h" // system includes +#include #include // local includes @@ -78,12 +79,31 @@ namespace display_device { return std::nullopt; } - for (const auto display_id : m_m_api->getDisplayIds(query_type)) { + const auto display_ids {m_m_api->getDisplayIds(query_type)}; + for (const auto display_id : display_ids) { if (m_m_api->getDeviceId(display_id) == device_id) { return display_id; } } - return std::nullopt; + // The id may have been saved while a display with the same vendor, model and serial was (or + // wasn't) online, in which case it is now the display's alias. Twins can't be told apart, so + // accept the alias only if exactly one online display has it. + std::optional alias_match; + for (const auto display_id : query_type == MacQueryType::Online ? display_ids : m_m_api->getDisplayIds(MacQueryType::Online)) { + if (m_m_api->getDeviceIdAlias(display_id) != device_id) { + continue; + } + if (alias_match) { + DD_LOG(warning) << "Device id " << device_id << " matches more than one display with the same vendor, model and serial; not resolving it."; + return std::nullopt; + } + alias_match = display_id; + } + + if (alias_match && std::ranges::find(display_ids, *alias_match) == std::ranges::end(display_ids)) { + return std::nullopt; + } + return alias_match; } } // namespace display_device diff --git a/tests/unit/macos/test_mac_api_utils.cpp b/tests/unit/macos/test_mac_api_utils.cpp index a2fc9f7..4032769 100644 --- a/tests/unit/macos/test_mac_api_utils.cpp +++ b/tests/unit/macos/test_mac_api_utils.cpp @@ -64,3 +64,29 @@ TEST_S(MakeCgDeviceIdentity) { // Once the twin is gone, the remaining display gets the stable identity again. EXPECT_EQ(makeCgDeviceIdentity(twin_a, {twin_a}), "vendor=1552;model=41007;serial=16843009"); } + +TEST_S(MakeCgDeviceIdentityAlias) { + using display_device::mac_utils::CgDisplayInfo; + using display_device::mac_utils::makeCgDeviceIdentity; + using display_device::mac_utils::makeCgDeviceIdentityAlias; + + const CgDisplayInfo twin_a {4, 1552, 41007, 16843009, 0}; + const CgDisplayInfo twin_b {5, 1552, 41007, 16843009, 1}; + + // A saved alone, then B plugged in: A's saved id becomes its alias, and B has the same alias. + const auto saved_alone {makeCgDeviceIdentity(twin_a, {twin_a})}; + EXPECT_EQ(makeCgDeviceIdentityAlias(twin_a, {twin_a, twin_b}), saved_alone); + EXPECT_EQ(makeCgDeviceIdentityAlias(twin_b, {twin_a, twin_b}), saved_alone); + + // A saved with B online, then B unplugged (A keeps its display id and unit): A's saved id becomes + // its alias, which only A has. + const auto saved_with_twin {makeCgDeviceIdentity(twin_a, {twin_a, twin_b})}; + EXPECT_EQ(makeCgDeviceIdentityAlias(twin_a, {twin_a}), saved_with_twin); + EXPECT_NE(makeCgDeviceIdentityAlias(twin_b, {twin_b}), saved_with_twin); + + // No usable serial: only one form. + for (const auto serial : {0U, 0xFFFFFFFFU}) { + const CgDisplayInfo display {1, 1552, 41007, serial, 0}; + EXPECT_EQ(makeCgDeviceIdentityAlias(display, {display}), std::nullopt); + } +} diff --git a/tests/unit/macos/test_mac_display_device.cpp b/tests/unit/macos/test_mac_display_device.cpp index 35975e8..2b8adca 100644 --- a/tests/unit/macos/test_mac_display_device.cpp +++ b/tests/unit/macos/test_mac_display_device.cpp @@ -262,10 +262,60 @@ TEST_F_S(GetDisplayNameUnknownDevice) { EXPECT_CALL(*m_layer, getDeviceId(1)) .Times(1) .WillOnce(Return("DeviceId1")); + EXPECT_CALL(*m_layer, getDeviceIdAlias(1)) + .Times(1) + .WillOnce(Return("")); EXPECT_EQ(m_mac_dd.getDisplayName("DeviceId2"), ""); } +TEST_F_S(GetDisplayName, ResolvesUniqueAlias) { + // Saved while a twin was online; the twin has since been unplugged, so the display's current id + // is the serial-only one and the saved id is its alias. + EXPECT_CALL(*m_layer, getDisplayIds(display_device::MacQueryType::Online)) + .Times(1) + .WillOnce(Return(display_device::MacDisplayIdList {1, 2})); + EXPECT_CALL(*m_layer, getDeviceId(1)) + .Times(1) + .WillOnce(Return("SerialId1")); + EXPECT_CALL(*m_layer, getDeviceId(2)) + .Times(1) + .WillOnce(Return("DeviceId2")); + EXPECT_CALL(*m_layer, getDeviceIdAlias(1)) + .Times(1) + .WillOnce(Return("QualifiedId1")); + EXPECT_CALL(*m_layer, getDeviceIdAlias(2)) + .Times(1) + .WillOnce(Return("")); + EXPECT_CALL(*m_layer, getDisplayName(1)) + .Times(1) + .WillOnce(Return("1")); + + EXPECT_EQ(m_mac_dd.getDisplayName("QualifiedId1"), "1"); +} + +TEST_F_S(GetDisplayName, AmbiguousAliasUnresolved) { + // Saved while the display was the only one with its serial; an identical twin has since been + // plugged in, so both now have the saved serial-only id as their alias. Neither may be picked. + EXPECT_CALL(*m_layer, getDisplayIds(display_device::MacQueryType::Online)) + .Times(1) + .WillOnce(Return(display_device::MacDisplayIdList {1, 2})); + EXPECT_CALL(*m_layer, getDeviceId(1)) + .Times(1) + .WillOnce(Return("QualifiedId1")); + EXPECT_CALL(*m_layer, getDeviceId(2)) + .Times(1) + .WillOnce(Return("QualifiedId2")); + EXPECT_CALL(*m_layer, getDeviceIdAlias(1)) + .Times(1) + .WillOnce(Return("SerialId")); + EXPECT_CALL(*m_layer, getDeviceIdAlias(2)) + .Times(1) + .WillOnce(Return("SerialId")); + + EXPECT_EQ(m_mac_dd.getDisplayName("SerialId"), ""); +} + TEST_F_S(TopologyRead) { EXPECT_CALL(*m_layer, getDisplayIds(display_device::MacQueryType::Active)) .Times(1) @@ -348,6 +398,19 @@ TEST_F_S(SetDisplayModes, EmptyModes) { TEST_F_S(SetDisplayModes, InactiveDisplay) { Sequence sequence; expectActiveDeviceLookup(sequence, "DeviceId2"); + EXPECT_CALL(*m_layer, getDisplayIds(display_device::MacQueryType::Online)) + .Times(1) + .InSequence(sequence) + .WillOnce(Return(display_device::MacDisplayIdList {1, 2})); + EXPECT_CALL(*m_layer, getDeviceIdAlias(1)) + .Times(1) + .InSequence(sequence) + .WillOnce(Return("")); + // The id is the alias of an online display that isn't active, so it must not resolve. + EXPECT_CALL(*m_layer, getDeviceIdAlias(2)) + .Times(1) + .InSequence(sequence) + .WillOnce(Return("DeviceId1")); EXPECT_FALSE(m_mac_dd.setDisplayModes({{"DeviceId1", {{1920, 1080}, {60, 1}}}})); } diff --git a/tests/unit/macos/utils/mock_mac_api_layer.h b/tests/unit/macos/utils/mock_mac_api_layer.h index 37f86d8..ef8aed1 100644 --- a/tests/unit/macos/utils/mock_mac_api_layer.h +++ b/tests/unit/macos/utils/mock_mac_api_layer.h @@ -16,6 +16,7 @@ namespace display_device { MOCK_METHOD(std::optional, createDisplaySleepAssertion, (const std::string &), (override)); MOCK_METHOD(bool, releasePowerAssertion, (MacPowerAssertionId), (override)); MOCK_METHOD(std::string, getDeviceId, (MacDisplayId), (const, override)); + MOCK_METHOD(std::string, getDeviceIdAlias, (MacDisplayId), (const, override)); MOCK_METHOD(std::optional, getCurrentDisplayMode, (MacDisplayId), (const, override)); MOCK_METHOD(MacDisplayModeList, getDisplayModes, (MacDisplayId), (const, override)); MOCK_METHOD(std::string, getDisplayName, (MacDisplayId), (const, override));