From 8a7cee50c347b79d0f68c0426ce5a07f25b462de Mon Sep 17 00:00:00 2001
From: tangjie02 <tangjie02@kylinsec.com.cn>
Date: Mon, 8 May 2023 18:04:07 +0800
Subject: [PATCH] fix(edid): Fix the problem that cannot match correct monitor
 when has same edid.
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

- 修复edid相同时无法正确匹配到对应的显示器问题

Signed-off-by: tangjie02 <tangjie02@kylinsec.com.cn>
---
 plugins/display/display-manager.cpp | 28 +++++++++++++++++++++++++---
 plugins/display/display-manager.h   |  3 +++
 2 files changed, 28 insertions(+), 3 deletions(-)

diff --git a/plugins/display/display-manager.cpp b/plugins/display/display-manager.cpp
index 0860253..f80e248 100644
--- a/plugins/display/display-manager.cpp
+++ b/plugins/display/display-manager.cpp
@@ -410,12 +410,13 @@ bool DisplayManager::apply_screen_config(const ScreenConfigInfo &screen_config,
 
     for (const auto &c_monitor : c_monitors)
     {
-        std::string uid = c_monitor.uid();
-        auto monitor = this->get_monitor_by_uid(uid);
+        auto monitor = this->match_best_monitor(c_monitor.uid(), c_monitor.name());
 
         if (!monitor)
         {
-            KLOG_WARNING("Cannot find monitor for %s.", uid.c_str());
+            KLOG_WARNING("cannot find monitor for uid=%s, name=%s.",
+                         c_monitor.uid().c_str(),
+                         c_monitor.name().c_str());
             return false;
         }
 
@@ -800,6 +801,27 @@ std::shared_ptr<DisplayMonitor> DisplayManager::get_monitor_by_name(const std::s
     return nullptr;
 }
 
+std::shared_ptr<DisplayMonitor> DisplayManager::match_best_monitor(const std::string &uid,
+                                                                   const std::string &name)
+{
+    std::shared_ptr<DisplayMonitor> retval;
+    for (const auto &iter : this->monitors_)
+    {
+        if (!retval && iter.second->get_uid() == uid)
+        {
+            retval = iter.second;
+        }
+
+        // 完美匹配则直接退出
+        if (iter.second->get_uid() == uid && iter.second->name_get() == name)
+        {
+            retval = iter.second;
+            break;
+        }
+    }
+    return retval;
+}
+
 std::string DisplayManager::get_monitors_uid()
 {
     std::vector<std::string> result;
diff --git a/plugins/display/display-manager.h b/plugins/display/display-manager.h
index c2fdce6..3d866ff 100644
--- a/plugins/display/display-manager.h
+++ b/plugins/display/display-manager.h
@@ -107,6 +107,9 @@ private:
     std::shared_ptr<DisplayMonitor> get_monitor(uint32_t id);
     std::shared_ptr<DisplayMonitor> get_monitor_by_uid(const std::string& uid);
     std::shared_ptr<DisplayMonitor> get_monitor_by_name(const std::string& name);
+    // 优先匹配uid，如果有多个uid匹配，则再匹配name
+    std::shared_ptr<DisplayMonitor> match_best_monitor(const std::string& uid,
+                                                       const std::string& name);
 
     // 将uid进行排序后拼接
     std::string get_monitors_uid();
-- 
2.36.1

