Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add More Monitor 'Auto' Positions. #5670

Merged
merged 13 commits into from
Apr 23, 2024
53 changes: 41 additions & 12 deletions src/Compositor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2781,26 +2781,55 @@ void CCompositor::arrangeMonitors() {
++it;
}

// auto left
int maxOffset = 0;
// Variables to store the max and min values of monitors on each axis.
int maxXOffsetRight = 0;
int maxXOffsetLeft = 0;
int maxYOffsetUp = 0;
int maxYOffsetDown = 0;

// Finds the max and min values of explicitely placed monitors.
for (auto& m : arranged) {
if (m->vecPosition.x + m->vecSize.x > maxOffset)
maxOffset = m->vecPosition.x + m->vecSize.x;
if (m->vecPosition.x + m->vecSize.x > maxXOffsetRight)
maxXOffsetRight = m->vecPosition.x + m->vecSize.x;
if (m->vecPosition.x < maxXOffsetLeft)
maxXOffsetLeft = m->vecPosition.x;
if (m->vecPosition.y + m->vecSize.y > maxYOffsetDown)
maxYOffsetDown = m->vecPosition.y + m->vecSize.y;
if (m->vecPosition.y < maxYOffsetUp)
maxYOffsetUp = m->vecPosition.y;
}

// Iterates through all non-explicitly placed monitors.
for (auto& m : toArrange) {
Debug::log(LOG, "arrangeMonitors: {} auto [{}, {:.2f}]", m->szName, maxOffset, 0.f);
m->moveTo({maxOffset, 0});
maxOffset += m->vecSize.x;
Debug::log(LOG, "arrangeMonitors: {} auto [{}, {:.2f}]", m->szName, maxXOffsetRight, 0.f);
// Moves the monitor to their appropriate position on the x/y axis and
// increments/decrements the corresponding max offset.
if (m->activeMonitorRule.autoDir == eAutoDirs::DIR_AUTO_UP) {
m->moveTo({0, maxYOffsetUp - m->vecSize.y});
maxYOffsetUp = m->vecPosition.y;
} else if (m->activeMonitorRule.autoDir == eAutoDirs::DIR_AUTO_DOWN) {
m->moveTo({0, maxYOffsetDown});
maxYOffsetDown += m->vecSize.y;
} else if (m->activeMonitorRule.autoDir == eAutoDirs::DIR_AUTO_LEFT) {
m->moveTo({maxXOffsetLeft - m->vecSize.x, 0});
maxXOffsetLeft = m->vecPosition.x;
} else if (m->activeMonitorRule.autoDir == eAutoDirs::DIR_AUTO_RIGHT) {
m->moveTo({maxXOffsetRight, 0});
maxXOffsetRight += m->vecSize.x;
} else {
Debug::log(WARN,
"Invalid auto direction. Valid options are 'auto',"
"'auto-up', 'auto-down', 'auto-left', and 'auto-right'.");
}
}

// reset maxOffset (reuse)
// reset maxXOffsetRight (reuse)
// and set xwayland positions aka auto for all
maxOffset = 0;
maxXOffsetRight = 0;
for (auto& m : m_vMonitors) {
Debug::log(LOG, "arrangeMonitors: {} xwayland [{}, {:.2f}]", m->szName, maxOffset, 0.f);
m->vecXWaylandPosition = {maxOffset, 0};
maxOffset += (*PXWLFORCESCALEZERO ? m->vecTransformedSize.x : m->vecSize.x);
Debug::log(LOG, "arrangeMonitors: {} xwayland [{}, {:.2f}]", m->szName, maxXOffsetRight, 0.f);
m->vecXWaylandPosition = {maxXOffsetRight, 0};
maxXOffsetRight += (*PXWLFORCESCALEZERO ? m->vecTransformedSize.x : m->vecSize.x);

if (*PXWLFORCESCALEZERO)
m->xwaylandScale = m->scale;
Expand Down
10 changes: 10 additions & 0 deletions src/config/ConfigManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1641,6 +1641,16 @@ std::optional<std::string> CConfigManager::handleMonitor(const std::string& comm

if (ARGS[2].starts_with("auto")) {
newrule.offset = Vector2D(-INT32_MAX, -INT32_MAX);
// If this is the first monitor rule needs to be on the right.
if (ARGS[2] == "auto-right" || ARGS[2] == "auto" || m_dMonitorRules.empty())
newrule.autoDir = eAutoDirs::DIR_AUTO_RIGHT;
else if (ARGS[2] == "auto-left")
newrule.autoDir = eAutoDirs::DIR_AUTO_LEFT;
else if (ARGS[2] == "auto-up")
newrule.autoDir = eAutoDirs::DIR_AUTO_UP;
else if (ARGS[2] == "auto-down")
newrule.autoDir = eAutoDirs::DIR_AUTO_DOWN;

} else {
if (!ARGS[2].contains("x")) {
error += "invalid offset ";
Expand Down
9 changes: 9 additions & 0 deletions src/helpers/Monitor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,16 @@
#include "Region.hpp"
#include <optional>

// Enum for the different types of auto directions, e.g. auto-left, auto-up.
enum class eAutoDirs {
DIR_AUTO_UP,
DIR_AUTO_DOWN,
DIR_AUTO_LEFT,
DIR_AUTO_RIGHT
};

struct SMonitorRule {
eAutoDirs autoDir;
std::string name = "";
Vector2D resolution = Vector2D(1280, 720);
Vector2D offset = Vector2D(0, 0);
Expand Down