aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMartin Gräßlin <[email protected]>2016-12-24 11:00:08 +0100
committerMartin Gräßlin <[email protected]>2016-12-27 17:38:32 +0100
commite3d79cdda6eeb71e5d0a83cfc1b3c95b125232dd (patch)
tree4a6036cb67212748e4b0817115aea1251c5a446a
parentbba18a1ba812f3a147173edc19516f0e00e7c855 (diff)
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
-rw-r--r--input.cpp5
-rw-r--r--input.h13
-rw-r--r--keyboard_input.cpp7
-rw-r--r--pointer_input.cpp77
-rw-r--r--touch_input.cpp21
5 files changed, 30 insertions, 93 deletions
diff --git a/input.cpp b/input.cpp
index 28ca85d..bbbd9c9 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<bool(InputEventFilter*)> 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 314f6e1..ba6c0fa 100644
--- a/input.h
+++ b/input.h
@@ -155,9 +155,16 @@ public:
return m_shortcuts;
}
- QVector<InputEventFilter*> 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<bool(InputEventFilter*)> function);
+
KeyboardInputRedirection *keyboard() const {
return m_keyboard;
}
diff --git a/keyboard_input.cpp b/keyboard_input.cpp
index 0ac9f6a..4fc856c 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 83b2405..6f9f053 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 635c47a..a453eb6 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;
}