Refactor out std::find_if for monitors

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

View file

@ -40,5 +40,26 @@ namespace Wayland
std::string GtkMonitorIDToName(int32_t monitorID);
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();
}

View file

@ -3,8 +3,6 @@
#include "CSS.h"
#include "Wayland.h"
#include <thread>
#include <gtk/gtk.h>
#include <gtk-layer-shell.h>
@ -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<wl_output*, Wayland::Monitor>& 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<wl_output*, Wayland::Monitor>& 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<wl_output*, Wayland::Monitor>& 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<wl_output*, Wayland::Monitor>& 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*)

View file

@ -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<wl_output*, ::Wayland::Monitor>& 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);