Refactor out std::find_if for monitors

This commit is contained in:
scorpion-26 2024-03-16 16:56:28 +01:00
parent c7b80bd045
commit 91532b952e
3 changed files with 44 additions and 45 deletions

View file

@ -40,5 +40,26 @@ namespace Wayland
std::string GtkMonitorIDToName(int32_t monitorID); std::string GtkMonitorIDToName(int32_t monitorID);
int32_t NameToGtkMonitorID(const std::string& name); int32_t NameToGtkMonitorID(const std::string& name);
template<typename Predicate>
inline const Monitor* FindMonitor(Predicate&& pred)
{
auto& mons = GetMonitors();
auto it = std::find_if(mons.begin(), mons.end(),
[&](const std::pair<wl_output*, Monitor>& 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(); void Shutdown();
} }

View file

@ -3,8 +3,6 @@
#include "CSS.h" #include "CSS.h"
#include "Wayland.h" #include "Wayland.h"
#include <thread>
#include <gtk/gtk.h> #include <gtk/gtk.h>
#include <gtk-layer-shell.h> #include <gtk-layer-shell.h>
@ -84,28 +82,23 @@ void Window::Run()
Wayland::PollEvents(); Wayland::PollEvents();
GdkDisplay* display = gdk_display_get_default(); GdkDisplay* display = gdk_display_get_default();
auto& mons = Wayland::GetMonitors();
// HACK: Discrepancies are mostly caused by the HEADLESS monitor. Assume that. // 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) != mons.size(); bool bGotHeadless = (size_t)gdk_display_get_n_monitors(display) != Wayland::GetMonitors().size();
if (bGotHeadless) if (bGotHeadless)
{ {
LOG("Window: Found discrepancy between GDK and Wayland!"); LOG("Window: Found discrepancy between GDK and Wayland!");
} }
// Try to find our target monitor // Try to find our target monitor
auto it = std::find_if(mons.begin(), mons.end(), const Wayland::Monitor* targetMonitor = Wayland::FindMonitorByName(m_TargetMonitor);
[&](const std::pair<wl_output*, Wayland::Monitor>& mon) if (targetMonitor)
{
return mon.second.name == m_TargetMonitor;
});
if (it != mons.end())
{ {
// Found target monitor, snap back. // Found target monitor, snap back.
if (m_MainWidget) if (m_MainWidget)
Destroy(); Destroy();
m_MonitorName = m_TargetMonitor; 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(); Create();
continue; continue;
} }
@ -114,15 +107,15 @@ void Window::Run()
if (m_MainWidget == nullptr) if (m_MainWidget == nullptr)
{ {
// Find a non-headless monitor // Find a non-headless monitor
auto it = std::find_if(mons.begin(), mons.end(), const Wayland::Monitor* replacementMonitor = Wayland::FindMonitor(
[&](const std::pair<wl_output*, Wayland::Monitor>& mon) [&](const Wayland::Monitor& mon)
{ {
return mon.second.name.find("HEADLESS") == std::string::npos; return mon.name.find("HEADLESS") == std::string::npos;
}); });
if (it == mons.end()) if (!replacementMonitor)
continue; continue;
m_MonitorName = it->second.name; m_MonitorName = replacementMonitor->name;
m_Monitor = gdk_display_get_monitor(display, bGotHeadless ? it->second.ID + 1 : it->second.ID); m_Monitor = gdk_display_get_monitor(display, bGotHeadless ? replacementMonitor->ID + 1 : replacementMonitor->ID);
Create(); Create();
continue; continue;
} }
@ -250,14 +243,10 @@ int Window::GetWidth() const
/*GdkRectangle rect{}; /*GdkRectangle rect{};
gdk_monitor_get_geometry(m_Monitor, &rect); gdk_monitor_get_geometry(m_Monitor, &rect);
return rect.width;*/ return rect.width;*/
auto& mons = Wayland::GetMonitors();
auto it = std::find_if(mons.begin(), mons.end(), const Wayland::Monitor* mon = Wayland::FindMonitorByName(m_MonitorName);
[&](const std::pair<wl_output*, Wayland::Monitor>& mon) ASSERT(mon, "Window: Couldn't find monitor");
{ return mon->width;
return mon.second.name == m_MonitorName;
});
ASSERT(it != mons.end(), "Window: Couldn't find monitor");
return it->second.width;
} }
int Window::GetHeight() const int Window::GetHeight() const
@ -266,14 +255,9 @@ int Window::GetHeight() const
gdk_monitor_get_geometry(m_Monitor, &rect); gdk_monitor_get_geometry(m_Monitor, &rect);
return rect.height;*/ return rect.height;*/
auto& mons = Wayland::GetMonitors(); const Wayland::Monitor* mon = Wayland::FindMonitorByName(m_MonitorName);
auto it = std::find_if(mons.begin(), mons.end(), ASSERT(mon, "Window: Couldn't find monitor");
[&](const std::pair<wl_output*, Wayland::Monitor>& mon) return mon->width;
{
return mon.second.name == m_MonitorName;
});
ASSERT(it != mons.end(), "Window: Couldn't find monitor");
return it->second.width;
} }
void Window::MonitorAdded(GdkDisplay*, GdkMonitor*) void Window::MonitorAdded(GdkDisplay*, GdkMonitor*)

View file

@ -20,18 +20,12 @@ namespace Workspaces
} }
System::WorkspaceStatus GetStatus(uint32_t workspaceId) System::WorkspaceStatus GetStatus(uint32_t workspaceId)
{ {
auto& mons = ::Wayland::GetMonitors(); const ::Wayland::Monitor* monitor = ::Wayland::FindMonitorByName(lastPolledMonitor);
auto it = std::find_if(mons.begin(), mons.end(), if (!monitor)
[&](const std::pair<wl_output*, ::Wayland::Monitor>& mon)
{
return mon.second.name == lastPolledMonitor;
});
if (it == mons.end())
{ {
LOG("Polled monitor doesn't exist!"); LOG("Polled monitor doesn't exist!");
return System::WorkspaceStatus::Dead; return System::WorkspaceStatus::Dead;
} }
const ::Wayland::Monitor& monitor = it->second;
auto& workspaces = ::Wayland::GetWorkspaces(); auto& workspaces = ::Wayland::GetWorkspaces();
auto workspaceIt = std::find_if(workspaces.begin(), workspaces.end(), auto workspaceIt = std::find_if(workspaces.begin(), workspaces.end(),
@ -44,7 +38,7 @@ namespace Workspaces
return System::WorkspaceStatus::Dead; return System::WorkspaceStatus::Dead;
} }
const WaylandWorkspaceGroup& group = ::Wayland::GetWorkspaceGroups().at(monitor.workspaceGroup); const WaylandWorkspaceGroup& group = ::Wayland::GetWorkspaceGroups().at(monitor->workspaceGroup);
if (group.lastActiveWorkspace) if (group.lastActiveWorkspace)
{ {
const WaylandWorkspace& activeWorkspace = workspaces.at(group.lastActiveWorkspace); const WaylandWorkspace& activeWorkspace = workspaces.at(group.lastActiveWorkspace);