From e3d79cdda6eeb71e5d0a83cfc1b3c95b125232dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Gr=C3=A4=C3=9Flin?= Date: Sat, 24 Dec 2016 11:00:08 +0100 Subject: [PATCH] Reduce code duplication for processing input events Summary: For every input event we have similar code. We go through all InputFilters, invoke a method with some arguments and check whether the filter returns true. Instead of duplicating that logic everywhere, there is now one method in InputRedirection which takes a std::function to call on the input filters. The std::function is supposed to be generated with a std::bind on the InputFilter::method with all the required arguments. Reviewers: #kwin, #plasma Subscribers: plasma-devel, kwin Tags: #kwin Differential Revision: https://phabricator.kde.org/D3806 --- input.cpp | 5 +++ input.h | 13 ++++++-- keyboard_input.cpp | 7 +---- pointer_input.cpp | 77 +++++++--------------------------------------- touch_input.cpp | 21 ++----------- 5 files changed, 30 insertions(+), 93 deletions(-) diff --git a/input.cpp b/input.cpp index 28ca85d2ff..bbbd9c9078 100644 --- a/input.cpp +++ b/input.cpp @@ -1863,6 +1863,11 @@ bool InputRedirection::isBreakingPointerConstraints() const return m_pointerConstraintsFilter ? m_pointerConstraintsFilter->isActive() : false; } +void InputRedirection::processFilters(std::function function) +{ + std::any_of(m_filters.constBegin(), m_filters.constEnd(), function); +} + InputDeviceHandler::InputDeviceHandler(InputRedirection *input) : QObject(input) , m_input(input) diff --git a/input.h b/input.h index 314f6e1381..ba6c0fa5d6 100644 --- a/input.h +++ b/input.h @@ -155,9 +155,16 @@ public: return m_shortcuts; } - QVector filters() const { - return m_filters; - } + /** + * Sends an event through all InputFilters. + * The method @p function is invoked on each input filter. Processing is stopped if + * a filter returns @c true for @p function. + * + * The intended usage is to std::bind the method to invoke on the filter with all arguments + * bind. + **/ + void processFilters(std::function function); + KeyboardInputRedirection *keyboard() const { return m_keyboard; } diff --git a/keyboard_input.cpp b/keyboard_input.cpp index 0ac9f6aca8..4fc856cd6e 100644 --- a/keyboard_input.cpp +++ b/keyboard_input.cpp @@ -691,12 +691,7 @@ void KeyboardInputRedirection::processKey(uint32_t key, InputRedirection::Keyboa } } - const auto &filters = m_input->filters(); - for (auto it = filters.begin(), end = filters.end(); it != end; it++) { - if ((*it)->keyEvent(&event)) { - return; - } - } + m_input->processFilters(std::bind(&InputEventFilter::keyEvent, std::placeholders::_1, &event)); } void KeyboardInputRedirection::processModifiers(uint32_t modsDepressed, uint32_t modsLatched, uint32_t modsLocked, uint32_t group) diff --git a/pointer_input.cpp b/pointer_input.cpp index 83b2405e65..6f9f053b28 100644 --- a/pointer_input.cpp +++ b/pointer_input.cpp @@ -229,12 +229,7 @@ void PointerInputRedirection::processMotion(const QPointF &pos, const QSizeF &de m_input->keyboardModifiers(), time, delta, deltaNonAccelerated, timeUsec, device); - const auto &filters = m_input->filters(); - for (auto it = filters.begin(), end = filters.end(); it != end; it++) { - if ((*it)->pointerEvent(&event, 0)) { - return; - } - } + m_input->processFilters(std::bind(&InputEventFilter::pointerEvent, std::placeholders::_1, &event, 0)); } void PointerInputRedirection::processButton(uint32_t button, InputRedirection::PointerButtonState state, uint32_t time, LibInput::Device *device) @@ -261,12 +256,7 @@ void PointerInputRedirection::processButton(uint32_t button, InputRedirection::P MouseEvent event(type, m_pos, buttonToQtMouseButton(button), m_qtButtons, m_input->keyboardModifiers(), time, QSizeF(), QSizeF(), 0, device); - const auto &filters = m_input->filters(); - for (auto it = filters.begin(), end = filters.end(); it != end; it++) { - if ((*it)->pointerEvent(&event, button)) { - return; - } - } + m_input->processFilters(std::bind(&InputEventFilter::pointerEvent, std::placeholders::_1, &event, button)); } void PointerInputRedirection::processAxis(InputRedirection::PointerAxis axis, qreal delta, uint32_t time, LibInput::Device *device) @@ -285,12 +275,7 @@ void PointerInputRedirection::processAxis(InputRedirection::PointerAxis axis, qr (axis == InputRedirection::PointerAxisHorizontal) ? Qt::Horizontal : Qt::Vertical, m_qtButtons, m_input->keyboardModifiers(), time, device); - const auto &filters = m_input->filters(); - for (auto it = filters.begin(), end = filters.end(); it != end; it++) { - if ((*it)->wheelEvent(&wheelEvent)) { - return; - } - } + m_input->processFilters(std::bind(&InputEventFilter::wheelEvent, std::placeholders::_1, &wheelEvent)); } void PointerInputRedirection::processSwipeGestureBegin(int fingerCount, quint32 time, KWin::LibInput::Device *device) @@ -300,12 +285,7 @@ void PointerInputRedirection::processSwipeGestureBegin(int fingerCount, quint32 return; } - const auto &filters = m_input->filters(); - for (auto it = filters.begin(), end = filters.end(); it != end; it++) { - if ((*it)->swipeGestureBegin(fingerCount, time)) { - return; - } - } + m_input->processFilters(std::bind(&InputEventFilter::swipeGestureBegin, std::placeholders::_1, fingerCount, time)); } void PointerInputRedirection::processSwipeGestureUpdate(const QSizeF &delta, quint32 time, KWin::LibInput::Device *device) @@ -315,12 +295,7 @@ void PointerInputRedirection::processSwipeGestureUpdate(const QSizeF &delta, qui return; } - const auto &filters = m_input->filters(); - for (auto it = filters.begin(), end = filters.end(); it != end; it++) { - if ((*it)->swipeGestureUpdate(delta, time)) { - return; - } - } + m_input->processFilters(std::bind(&InputEventFilter::swipeGestureUpdate, std::placeholders::_1, delta, time)); } void PointerInputRedirection::processSwipeGestureEnd(quint32 time, KWin::LibInput::Device *device) @@ -330,12 +305,7 @@ void PointerInputRedirection::processSwipeGestureEnd(quint32 time, KWin::LibInpu return; } - const auto &filters = m_input->filters(); - for (auto it = filters.begin(), end = filters.end(); it != end; it++) { - if ((*it)->swipeGestureEnd(time)) { - return; - } - } + m_input->processFilters(std::bind(&InputEventFilter::swipeGestureEnd, std::placeholders::_1, time)); } void PointerInputRedirection::processSwipeGestureCancelled(quint32 time, KWin::LibInput::Device *device) @@ -345,12 +315,7 @@ void PointerInputRedirection::processSwipeGestureCancelled(quint32 time, KWin::L return; } - const auto &filters = m_input->filters(); - for (auto it = filters.begin(), end = filters.end(); it != end; it++) { - if ((*it)->swipeGestureCancelled(time)) { - return; - } - } + m_input->processFilters(std::bind(&InputEventFilter::swipeGestureCancelled, std::placeholders::_1, time)); } void PointerInputRedirection::processPinchGestureBegin(int fingerCount, quint32 time, KWin::LibInput::Device *device) @@ -360,12 +325,7 @@ void PointerInputRedirection::processPinchGestureBegin(int fingerCount, quint32 return; } - const auto &filters = m_input->filters(); - for (auto it = filters.begin(), end = filters.end(); it != end; it++) { - if ((*it)->pinchGestureBegin(fingerCount, time)) { - return; - } - } + m_input->processFilters(std::bind(&InputEventFilter::pinchGestureBegin, std::placeholders::_1, fingerCount, time)); } void PointerInputRedirection::processPinchGestureUpdate(qreal scale, qreal angleDelta, const QSizeF &delta, quint32 time, KWin::LibInput::Device *device) @@ -375,12 +335,7 @@ void PointerInputRedirection::processPinchGestureUpdate(qreal scale, qreal angle return; } - const auto &filters = m_input->filters(); - for (auto it = filters.begin(), end = filters.end(); it != end; it++) { - if ((*it)->pinchGestureUpdate(scale, angleDelta, delta, time)) { - return; - } - } + m_input->processFilters(std::bind(&InputEventFilter::pinchGestureUpdate, std::placeholders::_1, scale, angleDelta, delta, time)); } void PointerInputRedirection::processPinchGestureEnd(quint32 time, KWin::LibInput::Device *device) @@ -390,12 +345,7 @@ void PointerInputRedirection::processPinchGestureEnd(quint32 time, KWin::LibInpu return; } - const auto &filters = m_input->filters(); - for (auto it = filters.begin(), end = filters.end(); it != end; it++) { - if ((*it)->pinchGestureEnd(time)) { - return; - } - } + m_input->processFilters(std::bind(&InputEventFilter::pinchGestureEnd, std::placeholders::_1, time)); } void PointerInputRedirection::processPinchGestureCancelled(quint32 time, KWin::LibInput::Device *device) @@ -405,12 +355,7 @@ void PointerInputRedirection::processPinchGestureCancelled(quint32 time, KWin::L return; } - const auto &filters = m_input->filters(); - for (auto it = filters.begin(), end = filters.end(); it != end; it++) { - if ((*it)->pinchGestureCancelled(time)) { - return; - } - } + m_input->processFilters(std::bind(&InputEventFilter::pinchGestureCancelled, std::placeholders::_1, time)); } void PointerInputRedirection::update() diff --git a/touch_input.cpp b/touch_input.cpp index 635c47a0cc..a453eb6673 100644 --- a/touch_input.cpp +++ b/touch_input.cpp @@ -151,12 +151,7 @@ void TouchInputRedirection::processDown(qint32 id, const QPointF &pos, quint32 t return; } m_windowUpdatedInCycle = false; - const auto &filters = m_input->filters(); - for (auto it = filters.begin(), end = filters.end(); it != end; it++) { - if ((*it)->touchDown(id, pos, time)) { - return; - } - } + m_input->processFilters(std::bind(&InputEventFilter::touchDown, std::placeholders::_1, id, pos, time)); m_windowUpdatedInCycle = false; } @@ -167,12 +162,7 @@ void TouchInputRedirection::processUp(qint32 id, quint32 time, LibInput::Device return; } m_windowUpdatedInCycle = false; - const auto &filters = m_input->filters(); - for (auto it = filters.begin(), end = filters.end(); it != end; it++) { - if ((*it)->touchUp(id, time)) { - return; - } - } + m_input->processFilters(std::bind(&InputEventFilter::touchUp, std::placeholders::_1, id, time)); m_windowUpdatedInCycle = false; } @@ -183,12 +173,7 @@ void TouchInputRedirection::processMotion(qint32 id, const QPointF &pos, quint32 return; } m_windowUpdatedInCycle = false; - const auto &filters = m_input->filters(); - for (auto it = filters.begin(), end = filters.end(); it != end; it++) { - if ((*it)->touchMotion(id, pos, time)) { - return; - } - } + m_input->processFilters(std::bind(&InputEventFilter::touchMotion, std::placeholders::_1, id, pos, time)); m_windowUpdatedInCycle = false; }