Pass SwitchEvent from LibInput through KWin and add to DebugConsole

Summary:
This change introduces a new SwitchEvent and passes it through the
InputEventSpy and InputEventFilter. The DebugConsoleFilter implements it
so that the events can be monitored in the debug console.

Test Plan: Untested as my only device with such switches has too old libinput

Reviewers: #kwin, #plasma

Subscribers: plasma-devel, kwin

Tags: #plasma

Differential Revision: https://phabricator.kde.org/D9521
This commit is contained in:
Martin Flöser 2017-12-27 20:25:36 +01:00
parent 8a3128c8cf
commit d2a9232ad3
9 changed files with 132 additions and 0 deletions

View file

@ -23,6 +23,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include <QtTest/QtTest> #include <QtTest/QtTest>
Q_DECLARE_METATYPE(KWin::SwitchEvent::State);
using namespace KWin; using namespace KWin;
using namespace KWin::LibInput; using namespace KWin::LibInput;
@ -36,6 +38,8 @@ private Q_SLOTS:
void testInitKeyEvent(); void testInitKeyEvent();
void testInitWheelEvent_data(); void testInitWheelEvent_data();
void testInitWheelEvent(); void testInitWheelEvent();
void testInitSwitchEvent_data();
void testInitSwitchEvent();
}; };
void InputEventsTest::testInitMouseEvent_data() void InputEventsTest::testInitMouseEvent_data()
@ -149,5 +153,32 @@ void InputEventsTest::testInitWheelEvent()
} }
void InputEventsTest::testInitSwitchEvent_data()
{
QTest::addColumn<KWin::SwitchEvent::State>("state");
QTest::addColumn<quint32>("timestamp");
QTest::addColumn<quint64>("micro");
QTest::newRow("on") << SwitchEvent::State::On << 23u << quint64{23456790};
QTest::newRow("off") << SwitchEvent::State::Off << 456892u << quint64{45689235987};
}
void InputEventsTest::testInitSwitchEvent()
{
// this test verifies that a SwitchEvent is constructed correctly
libinput_device device;
Device d(&device);
QFETCH(SwitchEvent::State, state);
QFETCH(quint32, timestamp);
QFETCH(quint64, micro);
SwitchEvent event(state, timestamp, micro, &d);
QCOMPARE(event.state(), state);
QCOMPARE(event.timestamp(), ulong(timestamp));
QCOMPARE(event.timestampMicroseconds(), micro);
QCOMPARE(event.device(), &d);
}
QTEST_GUILESS_MAIN(InputEventsTest) QTEST_GUILESS_MAIN(InputEventsTest)
#include "input_event_test.moc" #include "input_event_test.moc"

View file

@ -459,6 +459,43 @@ void DebugConsoleFilter::swipeGestureCancelled(quint32 time)
m_textEdit->ensureCursorVisible(); m_textEdit->ensureCursorVisible();
} }
void DebugConsoleFilter::switchEvent(SwitchEvent *event)
{
QString text = s_hr;
text.append(s_tableStart);
text.append(tableHeaderRow(i18nc("A hardware switch (e.g. notebook lid) got toggled", "Switch toggled")));
text.append(timestampRow(event->timestamp()));
if (event->timestampMicroseconds() != 0) {
text.append(timestampRowUsec(event->timestampMicroseconds()));
}
#if HAVE_INPUT
text.append(deviceRow(event->device()));
QString switchName;
if (event->device()->isLidSwitch()) {
switchName = i18nc("Name of a hardware switch", "Notebook lid");
} else if (event->device()->isTabletModeSwitch()) {
switchName = i18nc("Name of a hardware switch", "Tablet mode");
}
text.append(tableRow(i18nc("A hardware switch", "Switch"), switchName));
#endif
QString switchState;
switch (event->state()) {
case SwitchEvent::State::Off:
switchState = i18nc("The hardware switch got turned off", "Off");
break;
case SwitchEvent::State::On:
switchState = i18nc("The hardware switch got turned on", "On");
break;
default:
Q_UNREACHABLE();
}
text.append(tableRow(i18nc("State of a hardware switch (on/off)", "State"), switchState));
text.append(s_tableEnd);
m_textEdit->insertHtml(text);
m_textEdit->ensureCursorVisible();
}
DebugConsole::DebugConsole() DebugConsole::DebugConsole()
: QWidget() : QWidget()
, m_ui(new Ui::DebugConsole) , m_ui(new Ui::DebugConsole)

View file

@ -150,6 +150,8 @@ public:
void swipeGestureEnd(quint32 time) override; void swipeGestureEnd(quint32 time) override;
void swipeGestureCancelled(quint32 time) override; void swipeGestureCancelled(quint32 time) override;
void switchEvent(SwitchEvent *event) override;
private: private:
QTextEdit *m_textEdit; QTextEdit *m_textEdit;
}; };

View file

@ -167,6 +167,12 @@ bool InputEventFilter::swipeGestureCancelled(quint32 time)
return false; return false;
} }
bool InputEventFilter::switchEvent(SwitchEvent *event)
{
Q_UNUSED(event)
return false;
}
void InputEventFilter::passToWaylandServer(QKeyEvent *event) void InputEventFilter::passToWaylandServer(QKeyEvent *event)
{ {
Q_ASSERT(waylandServer()); Q_ASSERT(waylandServer());
@ -1766,6 +1772,15 @@ void InputRedirection::setupLibInput()
connect(conn, &LibInput::Connection::touchMotion, m_touch, &TouchInputRedirection::processMotion); connect(conn, &LibInput::Connection::touchMotion, m_touch, &TouchInputRedirection::processMotion);
connect(conn, &LibInput::Connection::touchCanceled, m_touch, &TouchInputRedirection::cancel); connect(conn, &LibInput::Connection::touchCanceled, m_touch, &TouchInputRedirection::cancel);
connect(conn, &LibInput::Connection::touchFrame, m_touch, &TouchInputRedirection::frame); connect(conn, &LibInput::Connection::touchFrame, m_touch, &TouchInputRedirection::frame);
auto handleSwitchEvent = [this] (SwitchEvent::State state, quint32 time, quint64 timeMicroseconds, LibInput::Device *device) {
SwitchEvent event(state, time, timeMicroseconds, device);
processSpies(std::bind(&InputEventSpy::switchEvent, std::placeholders::_1, &event));
processFilters(std::bind(&InputEventFilter::switchEvent, std::placeholders::_1, &event));
};
connect(conn, &LibInput::Connection::switchToggledOn, this,
std::bind(handleSwitchEvent, SwitchEvent::State::On, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
connect(conn, &LibInput::Connection::switchToggledOff, this,
std::bind(handleSwitchEvent, SwitchEvent::State::Off, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
if (screens()) { if (screens()) {
setupLibInputWithScreens(); setupLibInputWithScreens();
} else { } else {

View file

@ -47,6 +47,7 @@ class PointerConstraintsFilter;
class PointerInputRedirection; class PointerInputRedirection;
class TouchInputRedirection; class TouchInputRedirection;
class WindowSelectorFilter; class WindowSelectorFilter;
class SwitchEvent;
namespace Decoration namespace Decoration
{ {
@ -360,6 +361,8 @@ public:
virtual bool swipeGestureEnd(quint32 time); virtual bool swipeGestureEnd(quint32 time);
virtual bool swipeGestureCancelled(quint32 time); virtual bool swipeGestureCancelled(quint32 time);
virtual bool switchEvent(SwitchEvent *event);
protected: protected:
void passToWaylandServer(QKeyEvent *event); void passToWaylandServer(QKeyEvent *event);
}; };

View file

@ -51,4 +51,13 @@ KeyEvent::KeyEvent(QEvent::Type type, Qt::Key key, Qt::KeyboardModifiers modifie
setTimestamp(timestamp); setTimestamp(timestamp);
} }
SwitchEvent::SwitchEvent(State state, quint32 timestamp, quint64 timestampMicroseconds, LibInput::Device* device)
: QInputEvent(QEvent::User)
, m_state(state)
, m_timestampMicroseconds(timestampMicroseconds)
, m_device(device)
{
setTimestamp(timestamp);
}
} }

View file

@ -124,6 +124,33 @@ private:
Qt::KeyboardModifiers m_modifiersRelevantForShortcuts = Qt::KeyboardModifiers(); Qt::KeyboardModifiers m_modifiersRelevantForShortcuts = Qt::KeyboardModifiers();
}; };
class SwitchEvent : public QInputEvent
{
public:
enum class State {
Off,
On
};
explicit SwitchEvent(State state, quint32 timestamp, quint64 timestampMicroseconds, LibInput::Device *device);
State state() const {
return m_state;
}
quint64 timestampMicroseconds() const {
return m_timestampMicroseconds;
}
LibInput::Device *device() const {
return m_device;
}
private:
State m_state;
quint64 m_timestampMicroseconds;
LibInput::Device *m_device;
};
} }
#endif #endif

View file

@ -116,4 +116,9 @@ void InputEventSpy::swipeGestureCancelled(quint32 time)
Q_UNUSED(time) Q_UNUSED(time)
} }
void InputEventSpy::switchEvent(SwitchEvent *event)
{
Q_UNUSED(event)
}
} }

View file

@ -31,6 +31,7 @@ namespace KWin
class KeyEvent; class KeyEvent;
class MouseEvent; class MouseEvent;
class WheelEvent; class WheelEvent;
class SwitchEvent;
/** /**
@ -81,6 +82,8 @@ public:
virtual void swipeGestureEnd(quint32 time); virtual void swipeGestureEnd(quint32 time);
virtual void swipeGestureCancelled(quint32 time); virtual void swipeGestureCancelled(quint32 time);
virtual void switchEvent(SwitchEvent *event);
}; };