diff options
author | Tuur Vanhoutte <[email protected]> | 2023-07-09 23:08:40 +0200 |
---|---|---|
committer | GitHub <[email protected]> | 2023-07-09 23:08:40 +0200 |
commit | b99ac063eae32995ba83047a4c5d4cb3490a4b10 (patch) | |
tree | e0a517df4037fd8e96f77dd2680308ae2efc8c39 | |
parent | b33d82734fb3b88b87e1c89cbf2ec8883ea029ac (diff) | |
download | Hyprland-b99ac063eae32995ba83047a4c5d4cb3490a4b10.tar.gz Hyprland-b99ac063eae32995ba83047a4c5d4cb3490a4b10.zip |
Reuse same ID when reconnecting monitor, otherwise use minimum available ID (#2666)
Fixes #2601
-rw-r--r-- | src/Compositor.cpp | 22 | ||||
-rw-r--r-- | src/Compositor.hpp | 4 | ||||
-rw-r--r-- | src/events/Monitors.cpp | 2 |
3 files changed, 20 insertions, 8 deletions
diff --git a/src/Compositor.cpp b/src/Compositor.cpp index ae3ef109..50ea0b5d 100644 --- a/src/Compositor.cpp +++ b/src/Compositor.cpp @@ -1,6 +1,7 @@ #include "Compositor.hpp" #include "helpers/Splashes.hpp" #include <random> +#include <unordered_set> #include "debug/HyprCtl.hpp" #include "debug/CrashReporter.hpp" #ifdef USES_SYSTEMD @@ -1697,14 +1698,23 @@ void CCompositor::updateWindowAnimatedDecorationValues(CWindow* pWindow) { d->updateWindow(pWindow); } -int CCompositor::getNextAvailableMonitorID() { - int64_t topID = -1; - for (auto& m : m_vRealMonitors) { - if ((int64_t)m->ID > topID) - topID = m->ID; +int CCompositor::getNextAvailableMonitorID(std::string const& name) { + // reuse ID if it's already in the map + if (m_mMonitorIDMap.contains(name)) + return m_mMonitorIDMap[name]; + + // otherwise, find minimum available ID that is not in the map + std::unordered_set<int> usedIDs; + for (auto const& monitor : m_vRealMonitors) { + usedIDs.insert(monitor->ID); } - return topID + 1; + int nextID = 0; + while (usedIDs.count(nextID) > 0) { + nextID++; + } + m_mMonitorIDMap[name] = nextID; + return nextID; } void CCompositor::swapActiveWorkspaces(CMonitor* pMonitorA, CMonitor* pMonitorB) { diff --git a/src/Compositor.hpp b/src/Compositor.hpp index 6f1be1b6..e4f5483f 100644 --- a/src/Compositor.hpp +++ b/src/Compositor.hpp @@ -101,6 +101,8 @@ class CCompositor { std::vector<CWindow*> m_vWindowsFadingOut; std::vector<SLayerSurface*> m_vSurfacesFadingOut; + std::unordered_map<std::string, int64_t> m_mMonitorIDMap; + void initServer(); void startCompositor(); void cleanup(); @@ -168,7 +170,7 @@ class CCompositor { CMonitor* getMonitorInDirection(const char&); void updateAllWindowsAnimatedDecorationValues(); void updateWindowAnimatedDecorationValues(CWindow*); - int getNextAvailableMonitorID(); + int getNextAvailableMonitorID(std::string const & name); void moveWorkspaceToMonitor(CWorkspace*, CMonitor*); void swapActiveWorkspaces(CMonitor*, CMonitor*); CMonitor* getMonitorFromString(const std::string&); diff --git a/src/events/Monitors.cpp b/src/events/Monitors.cpp index 6455d4df..73f5a6f6 100644 --- a/src/events/Monitors.cpp +++ b/src/events/Monitors.cpp @@ -76,7 +76,7 @@ void Events::listener_newOutput(wl_listener* listener, void* data) { Debug::log(LOG, "Adding completely new monitor."); PNEWMONITORWRAP = &g_pCompositor->m_vRealMonitors.emplace_back(std::make_shared<CMonitor>()); - (*PNEWMONITORWRAP)->ID = g_pCompositor->getNextAvailableMonitorID(); + (*PNEWMONITORWRAP)->ID = g_pCompositor->getNextAvailableMonitorID(OUTPUT->name); } const auto PNEWMONITOR = PNEWMONITORWRAP->get(); |