diff --git a/input.cpp b/input.cpp index f9c8693d47..b3b892b75c 100644 --- a/input.cpp +++ b/input.cpp @@ -226,10 +226,13 @@ void InputRedirection::setupLibInput() processPointerMotion(screen, time); } ); + connect(screens(), &Screens::changed, this, &InputRedirection::updatePointerAfterScreenChange); // set pos to center of all screens if (screens()) { m_globalPointer = screens()->geometry().center(); emit globalPointerChanged(m_globalPointer); + // sanitize + updatePointerAfterScreenChange(); } } #endif @@ -259,8 +262,7 @@ void InputRedirection::processPointerMotion(const QPointF &pos, uint32_t time) Q_UNUSED(time) // first update to new mouse position // const QPointF oldPos = m_globalPointer; - m_globalPointer = pos; - emit globalPointerChanged(m_globalPointer); + updatePointerPosition(pos); // TODO: check which part of KWin would like to intercept the event QMouseEvent event(QEvent::MouseMove, m_globalPointer.toPoint(), m_globalPointer.toPoint(), @@ -558,4 +560,35 @@ void InputRedirection::registerAxisShortcut(Qt::KeyboardModifiers modifiers, Poi m_shortcuts->registerAxisShortcut(action, modifiers, axis); } +static bool screenContainsPos(const QPointF &pos) +{ + for (int i = 0; i < screens()->count(); ++i) { + if (screens()->geometry(i).contains(pos.toPoint())) { + return true; + } + } + return false; +} + +void InputRedirection::updatePointerPosition(const QPointF &pos) +{ + // verify that at least one screen contains the pointer position + if (!screenContainsPos(pos)) { + return; + } + m_globalPointer = pos; + emit globalPointerChanged(m_globalPointer); +} + +void InputRedirection::updatePointerAfterScreenChange() +{ + if (screenContainsPos(m_globalPointer)) { + // pointer still on a screen + return; + } + // pointer no longer on a screen, reposition to closes screen + m_globalPointer = screens()->geometry(screens()->number(m_globalPointer.toPoint())).center(); + emit globalPointerChanged(m_globalPointer); +} + } // namespace diff --git a/input.h b/input.h index 865f3ccc39..5e7a3cbf94 100644 --- a/input.h +++ b/input.h @@ -156,6 +156,8 @@ private: static Qt::MouseButton buttonToQtMouseButton(uint32_t button); Toplevel *findToplevel(const QPoint &pos); void setupLibInput(); + void updatePointerPosition(const QPointF &pos); + void updatePointerAfterScreenChange(); QPointF m_globalPointer; QHash m_pointerButtons; #if HAVE_XKB