From 91532b952eae6ef54bbe85242ed459a37f8cac67 Mon Sep 17 00:00:00 2001 From: scorpion-26 Date: Sat, 16 Mar 2024 16:56:28 +0100 Subject: [PATCH] Refactor out std::find_if for monitors --- src/Wayland.h | 21 +++++++++++++++++ src/Window.cpp | 56 +++++++++++++++++----------------------------- src/Workspaces.cpp | 12 +++------- 3 files changed, 44 insertions(+), 45 deletions(-) diff --git a/src/Wayland.h b/src/Wayland.h index 9f92e22..98423b3 100644 --- a/src/Wayland.h +++ b/src/Wayland.h @@ -40,5 +40,26 @@ namespace Wayland std::string GtkMonitorIDToName(int32_t monitorID); int32_t NameToGtkMonitorID(const std::string& name); + template + inline const Monitor* FindMonitor(Predicate&& pred) + { + auto& mons = GetMonitors(); + auto it = std::find_if(mons.begin(), mons.end(), + [&](const std::pair& mon) + { + return pred(mon.second); + }); + return it != mons.end() ? &it->second : nullptr; + } + + inline const Monitor* FindMonitorByName(const std::string& name) + { + return FindMonitor( + [&](const Monitor& mon) + { + return mon.name == name; + }); + } + void Shutdown(); } diff --git a/src/Window.cpp b/src/Window.cpp index db9a459..052cfc1 100644 --- a/src/Window.cpp +++ b/src/Window.cpp @@ -3,8 +3,6 @@ #include "CSS.h" #include "Wayland.h" -#include - #include #include @@ -84,28 +82,23 @@ void Window::Run() Wayland::PollEvents(); GdkDisplay* display = gdk_display_get_default(); - auto& mons = Wayland::GetMonitors(); - // HACK: Discrepancies are mostly caused by the HEADLESS monitor. Assume that. - bool bGotHeadless = (size_t)gdk_display_get_n_monitors(display) != mons.size(); + // HACK: Discrepancies are mostly caused by the HEADLESS monitor. Assume that Gdk ID 0 is headless + bool bGotHeadless = (size_t)gdk_display_get_n_monitors(display) != Wayland::GetMonitors().size(); if (bGotHeadless) { LOG("Window: Found discrepancy between GDK and Wayland!"); } // Try to find our target monitor - auto it = std::find_if(mons.begin(), mons.end(), - [&](const std::pair& mon) - { - return mon.second.name == m_TargetMonitor; - }); - if (it != mons.end()) + const Wayland::Monitor* targetMonitor = Wayland::FindMonitorByName(m_TargetMonitor); + if (targetMonitor) { // Found target monitor, snap back. if (m_MainWidget) Destroy(); m_MonitorName = m_TargetMonitor; - m_Monitor = gdk_display_get_monitor(display, bGotHeadless ? it->second.ID + 1 : it->second.ID); + m_Monitor = gdk_display_get_monitor(display, bGotHeadless ? targetMonitor->ID + 1 : targetMonitor->ID); Create(); continue; } @@ -114,15 +107,15 @@ void Window::Run() if (m_MainWidget == nullptr) { // Find a non-headless monitor - auto it = std::find_if(mons.begin(), mons.end(), - [&](const std::pair& mon) - { - return mon.second.name.find("HEADLESS") == std::string::npos; - }); - if (it == mons.end()) + const Wayland::Monitor* replacementMonitor = Wayland::FindMonitor( + [&](const Wayland::Monitor& mon) + { + return mon.name.find("HEADLESS") == std::string::npos; + }); + if (!replacementMonitor) continue; - m_MonitorName = it->second.name; - m_Monitor = gdk_display_get_monitor(display, bGotHeadless ? it->second.ID + 1 : it->second.ID); + m_MonitorName = replacementMonitor->name; + m_Monitor = gdk_display_get_monitor(display, bGotHeadless ? replacementMonitor->ID + 1 : replacementMonitor->ID); Create(); continue; } @@ -250,14 +243,10 @@ int Window::GetWidth() const /*GdkRectangle rect{}; gdk_monitor_get_geometry(m_Monitor, &rect); return rect.width;*/ - auto& mons = Wayland::GetMonitors(); - auto it = std::find_if(mons.begin(), mons.end(), - [&](const std::pair& mon) - { - return mon.second.name == m_MonitorName; - }); - ASSERT(it != mons.end(), "Window: Couldn't find monitor"); - return it->second.width; + + const Wayland::Monitor* mon = Wayland::FindMonitorByName(m_MonitorName); + ASSERT(mon, "Window: Couldn't find monitor"); + return mon->width; } int Window::GetHeight() const @@ -266,14 +255,9 @@ int Window::GetHeight() const gdk_monitor_get_geometry(m_Monitor, &rect); return rect.height;*/ - auto& mons = Wayland::GetMonitors(); - auto it = std::find_if(mons.begin(), mons.end(), - [&](const std::pair& mon) - { - return mon.second.name == m_MonitorName; - }); - ASSERT(it != mons.end(), "Window: Couldn't find monitor"); - return it->second.width; + const Wayland::Monitor* mon = Wayland::FindMonitorByName(m_MonitorName); + ASSERT(mon, "Window: Couldn't find monitor"); + return mon->width; } void Window::MonitorAdded(GdkDisplay*, GdkMonitor*) diff --git a/src/Workspaces.cpp b/src/Workspaces.cpp index 9f10f3b..885f841 100644 --- a/src/Workspaces.cpp +++ b/src/Workspaces.cpp @@ -20,18 +20,12 @@ namespace Workspaces } System::WorkspaceStatus GetStatus(uint32_t workspaceId) { - auto& mons = ::Wayland::GetMonitors(); - auto it = std::find_if(mons.begin(), mons.end(), - [&](const std::pair& mon) - { - return mon.second.name == lastPolledMonitor; - }); - if (it == mons.end()) + const ::Wayland::Monitor* monitor = ::Wayland::FindMonitorByName(lastPolledMonitor); + if (!monitor) { LOG("Polled monitor doesn't exist!"); return System::WorkspaceStatus::Dead; } - const ::Wayland::Monitor& monitor = it->second; auto& workspaces = ::Wayland::GetWorkspaces(); auto workspaceIt = std::find_if(workspaces.begin(), workspaces.end(), @@ -44,7 +38,7 @@ namespace Workspaces return System::WorkspaceStatus::Dead; } - const WaylandWorkspaceGroup& group = ::Wayland::GetWorkspaceGroups().at(monitor.workspaceGroup); + const WaylandWorkspaceGroup& group = ::Wayland::GetWorkspaceGroups().at(monitor->workspaceGroup); if (group.lastActiveWorkspace) { const WaylandWorkspace& activeWorkspace = workspaces.at(group.lastActiveWorkspace);