Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit eb0c5075 authored by Prabir Pradhan's avatar Prabir Pradhan
Browse files

Use integer coordinates as the cursor starting point on a display

Ensure that the mouse cursor starts off with integer coordinates. Also,
remove the unused getBounds method.

Bug: 332973324
Test: atest PointerIconTest
Test: Presubmit
Flag: EXEMPT refactor
Change-Id: Ieb26e443d047b016980001ec51002d3d40d00e91
parent d67b1f83
Loading
Loading
Loading
Loading
+18 −26
Original line number Diff line number Diff line
@@ -64,25 +64,6 @@ MouseCursorController::~MouseCursorController() {
    mLocked.pointerSprite.clear();
}

std::optional<FloatRect> MouseCursorController::getBounds() const {
    std::scoped_lock lock(mLock);

    return getBoundsLocked();
}

std::optional<FloatRect> MouseCursorController::getBoundsLocked() const REQUIRES(mLock) {
    if (!mLocked.viewport.isValid()) {
        return {};
    }

    return FloatRect{
            static_cast<float>(mLocked.viewport.logicalLeft),
            static_cast<float>(mLocked.viewport.logicalTop),
            static_cast<float>(mLocked.viewport.logicalRight - 1),
            static_cast<float>(mLocked.viewport.logicalBottom - 1),
    };
}

void MouseCursorController::move(float deltaX, float deltaY) {
#if DEBUG_MOUSE_CURSOR_UPDATES
    ALOGD("Move pointer by deltaX=%0.3f, deltaY=%0.3f", deltaX, deltaY);
@@ -105,11 +86,20 @@ void MouseCursorController::setPosition(float x, float y) {
}

void MouseCursorController::setPositionLocked(float x, float y) REQUIRES(mLock) {
    const auto bounds = getBoundsLocked();
    if (!bounds) return;
    const auto& v = mLocked.viewport;
    if (!v.isValid()) return;

    mLocked.pointerX = std::max(bounds->left, std::min(bounds->right, x));
    mLocked.pointerY = std::max(bounds->top, std::min(bounds->bottom, y));
    // The valid bounds for a mouse cursor. Since the right and bottom edges are considered outside
    // the display, clip the bounds by one pixel instead of letting the cursor get arbitrarily
    // close to the outside edge.
    const FloatRect bounds{
            static_cast<float>(mLocked.viewport.logicalLeft),
            static_cast<float>(mLocked.viewport.logicalTop),
            static_cast<float>(mLocked.viewport.logicalRight - 1),
            static_cast<float>(mLocked.viewport.logicalBottom - 1),
    };
    mLocked.pointerX = std::max(bounds.left, std::min(bounds.right, x));
    mLocked.pointerY = std::max(bounds.top, std::min(bounds.bottom, y));

    updatePointerLocked();
}
@@ -216,9 +206,11 @@ void MouseCursorController::setDisplayViewport(const DisplayViewport& viewport,
    // Reset cursor position to center if size or display changed.
    if (oldViewport.displayId != viewport.displayId || oldDisplayWidth != newDisplayWidth ||
        oldDisplayHeight != newDisplayHeight) {
        if (const auto bounds = getBoundsLocked(); bounds) {
            mLocked.pointerX = (bounds->left + bounds->right) * 0.5f;
            mLocked.pointerY = (bounds->top + bounds->bottom) * 0.5f;
        if (viewport.isValid()) {
            // Use integer coordinates as the starting point for the cursor location.
            // We usually expect display sizes to be even numbers, so the flooring is precautionary.
            mLocked.pointerX = std::floor((viewport.logicalLeft + viewport.logicalRight) / 2);
            mLocked.pointerY = std::floor((viewport.logicalTop + viewport.logicalBottom) / 2);
            // Reload icon resources for density may be changed.
            loadResourcesLocked(getAdditionalMouseResources);
        } else {
+0 −2
Original line number Diff line number Diff line
@@ -43,7 +43,6 @@ public:
    MouseCursorController(PointerControllerContext& context);
    ~MouseCursorController();

    std::optional<FloatRect> getBounds() const;
    void move(float deltaX, float deltaY);
    void setPosition(float x, float y);
    FloatPoint getPosition() const;
@@ -104,7 +103,6 @@ private:

    } mLocked GUARDED_BY(mLock);

    std::optional<FloatRect> getBoundsLocked() const;
    void setPositionLocked(float x, float y);

    void updatePointerLocked();
+0 −4
Original line number Diff line number Diff line
@@ -138,10 +138,6 @@ std::mutex& PointerController::getLock() const {
    return mDisplayInfoListener->mLock;
}

std::optional<FloatRect> PointerController::getBounds() const {
    return mCursorController.getBounds();
}

void PointerController::move(float deltaX, float deltaY) {
    const ui::LogicalDisplayId displayId = mCursorController.getDisplayId();
    vec2 transformed;
+0 −4
Original line number Diff line number Diff line
@@ -51,7 +51,6 @@ public:

    ~PointerController() override;

    std::optional<FloatRect> getBounds() const override;
    void move(float deltaX, float deltaY) override;
    void setPosition(float x, float y) override;
    FloatPoint getPosition() const override;
@@ -166,9 +165,6 @@ public:

    ~TouchPointerController() override;

    std::optional<FloatRect> getBounds() const override {
        LOG_ALWAYS_FATAL("Should not be called");
    }
    void move(float, float) override {
        LOG_ALWAYS_FATAL("Should not be called");
    }