From df77fcd9e01d8cd2a31f785acde214f2146c04e2 Mon Sep 17 00:00:00 2001 From: Aleix Pol Date: Fri, 18 Nov 2022 22:49:37 +0100 Subject: [PATCH] x11window: Calculate the native floor from the begining of the output If this function is useful to make sure we are properly aligned with the output's pixels, we should start with the output's 0, otherwise we'll be carrying over the rounding errors increasingly as our workspace grows. BUG: 459373 --- src/core/output.cpp | 10 ++++++++++ src/core/output.h | 10 ++++++++++ src/utils/xcbutils.cpp | 13 +++++++++---- src/utils/xcbutils.h | 1 - 4 files changed, 29 insertions(+), 5 deletions(-) diff --git a/src/core/output.cpp b/src/core/output.cpp index abd25c91af..63029a3fe5 100644 --- a/src/core/output.cpp +++ b/src/core/output.cpp @@ -154,6 +154,16 @@ QRect Output::mapFromGlobal(const QRect &rect) const return rect.translated(-geometry().topLeft()); } +QRectF Output::mapFromGlobal(const QRectF &rect) const +{ + return rect.translated(-geometry().topLeft()); +} + +QRectF Output::mapToGlobal(const QRectF &rect) const +{ + return rect.translated(geometry().topLeft()); +} + Output::Capabilities Output::capabilities() const { return m_information.capabilities; diff --git a/src/core/output.h b/src/core/output.h index 3d2945625a..cc45723eb2 100644 --- a/src/core/output.h +++ b/src/core/output.h @@ -109,6 +109,16 @@ public: */ QRect mapFromGlobal(const QRect &rect) const; + /** + * Maps the specified @a rect from the global coordinate system to the output-local coords. + */ + QRectF mapFromGlobal(const QRectF &rect) const; + + /** + * Maps a @a rect in this output coordinates to the global coordinate system. + */ + QRectF mapToGlobal(const QRectF &rect) const; + /** * Returns a short identifiable name of this output. */ diff --git a/src/utils/xcbutils.cpp b/src/utils/xcbutils.cpp index 99089b304e..7f0ff780b4 100644 --- a/src/utils/xcbutils.cpp +++ b/src/utils/xcbutils.cpp @@ -9,6 +9,9 @@ */ #include "utils/xcbutils.h" #include "utils/common.h" +#include +#include + // Qt #include // xcb @@ -641,15 +644,17 @@ QSizeF fromXNative(const QSize &s) return QSizeF(fromXNative(s.width()), fromXNative(s.height())); } -qreal nativeFloor(qreal value) +static qreal nativeFloor(qreal value) { return std::floor(value / kwinApp()->xwaylandScale()) * kwinApp()->xwaylandScale(); } -QRectF nativeFloor(const QRectF &value) +QRectF nativeFloor(const QRectF &rect) { - return QRectF(nativeFloor(value.left()), nativeFloor(value.top()), - nativeFloor(value.width()), nativeFloor(value.height())); + const auto output = workspace()->outputAt(rect.center()); + const QRectF outputRect = output->mapFromGlobal(rect); + return output->mapToGlobal(QRectF(nativeFloor(outputRect.left()), nativeFloor(outputRect.top()), + nativeFloor(outputRect.width()), nativeFloor(outputRect.height()))); } } // namespace Xcb diff --git a/src/utils/xcbutils.h b/src/utils/xcbutils.h index 0667797ec7..76b1331d63 100644 --- a/src/utils/xcbutils.h +++ b/src/utils/xcbutils.h @@ -43,7 +43,6 @@ QSizeF KWIN_EXPORT fromXNative(const QSize &value); * Use when flooring to ints from Xwayland * i.e floor(a/scale) * scale */ -qreal KWIN_EXPORT nativeFloor(qreal value); QRectF KWIN_EXPORT nativeFloor(const QRectF &value); // forward declaration of methods