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
This commit is contained in:
Martin Gräßlin 2016-12-24 11:00:08 +01:00
parent bba18a1ba8
commit e3d79cdda6
5 changed files with 30 additions and 93 deletions

View file

@ -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)

13
input.h
View file

@ -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;
}

View file

@ -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)

View file

@ -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()

View file

@ -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;
}