Skip to content
This repository has been archived by the owner on Apr 29, 2023. It is now read-only.

Commit

Permalink
Pass SwitchEvent from LibInput through KWin and add to DebugConsole
Browse files Browse the repository at this point in the history
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
  • Loading branch information
mgraesslin committed Jan 9, 2018
1 parent 8a3128c commit d2a9232
Show file tree
Hide file tree
Showing 9 changed files with 132 additions and 0 deletions.
31 changes: 31 additions & 0 deletions autotests/libinput/input_event_test.cpp
Expand Up @@ -23,6 +23,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.

#include <QtTest/QtTest>

Q_DECLARE_METATYPE(KWin::SwitchEvent::State);

using namespace KWin;
using namespace KWin::LibInput;

Expand All @@ -36,6 +38,8 @@ private Q_SLOTS:
void testInitKeyEvent();
void testInitWheelEvent_data();
void testInitWheelEvent();
void testInitSwitchEvent_data();
void testInitSwitchEvent();
};

void InputEventsTest::testInitMouseEvent_data()
Expand Down Expand Up @@ -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)
#include "input_event_test.moc"
37 changes: 37 additions & 0 deletions debug_console.cpp
Expand Up @@ -459,6 +459,43 @@ void DebugConsoleFilter::swipeGestureCancelled(quint32 time)
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()
: QWidget()
, m_ui(new Ui::DebugConsole)
Expand Down
2 changes: 2 additions & 0 deletions debug_console.h
Expand Up @@ -150,6 +150,8 @@ class DebugConsoleFilter : public InputEventSpy
void swipeGestureEnd(quint32 time) override;
void swipeGestureCancelled(quint32 time) override;

void switchEvent(SwitchEvent *event) override;

private:
QTextEdit *m_textEdit;
};
Expand Down
15 changes: 15 additions & 0 deletions input.cpp
Expand Up @@ -167,6 +167,12 @@ bool InputEventFilter::swipeGestureCancelled(quint32 time)
return false;
}

bool InputEventFilter::switchEvent(SwitchEvent *event)
{
Q_UNUSED(event)
return false;
}

void InputEventFilter::passToWaylandServer(QKeyEvent *event)
{
Q_ASSERT(waylandServer());
Expand Down Expand Up @@ -1766,6 +1772,15 @@ void InputRedirection::setupLibInput()
connect(conn, &LibInput::Connection::touchMotion, m_touch, &TouchInputRedirection::processMotion);
connect(conn, &LibInput::Connection::touchCanceled, m_touch, &TouchInputRedirection::cancel);
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()) {
setupLibInputWithScreens();
} else {
Expand Down
3 changes: 3 additions & 0 deletions input.h
Expand Up @@ -47,6 +47,7 @@ class PointerConstraintsFilter;
class PointerInputRedirection;
class TouchInputRedirection;
class WindowSelectorFilter;
class SwitchEvent;

namespace Decoration
{
Expand Down Expand Up @@ -360,6 +361,8 @@ class KWIN_EXPORT InputEventFilter
virtual bool swipeGestureEnd(quint32 time);
virtual bool swipeGestureCancelled(quint32 time);

virtual bool switchEvent(SwitchEvent *event);

protected:
void passToWaylandServer(QKeyEvent *event);
};
Expand Down
9 changes: 9 additions & 0 deletions input_event.cpp
Expand Up @@ -51,4 +51,13 @@ KeyEvent::KeyEvent(QEvent::Type type, Qt::Key key, Qt::KeyboardModifiers modifie
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);
}

}
27 changes: 27 additions & 0 deletions input_event.h
Expand Up @@ -124,6 +124,33 @@ class KeyEvent : public QKeyEvent
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
5 changes: 5 additions & 0 deletions input_event_spy.cpp
Expand Up @@ -116,4 +116,9 @@ void InputEventSpy::swipeGestureCancelled(quint32 time)
Q_UNUSED(time)
}

void InputEventSpy::switchEvent(SwitchEvent *event)
{
Q_UNUSED(event)
}

}
3 changes: 3 additions & 0 deletions input_event_spy.h
Expand Up @@ -31,6 +31,7 @@ namespace KWin
class KeyEvent;
class MouseEvent;
class WheelEvent;
class SwitchEvent;


/**
Expand Down Expand Up @@ -81,6 +82,8 @@ class KWIN_EXPORT InputEventSpy
virtual void swipeGestureEnd(quint32 time);
virtual void swipeGestureCancelled(quint32 time);

virtual void switchEvent(SwitchEvent *event);

};


Expand Down

0 comments on commit d2a9232

Please sign in to comment.