Commit ce7a6eda authored by Martin Flöser's avatar Martin Flöser
Browse files

Overloads taking Qt::MouseButton added to Server::SeatInterface

Qt::MouseButton is mapped to the linux buttons. The mapping does
not match the mapping used in QtWayland module [1] as that seems
to be incorrect to me. E.g. Qt::BackButton is not mapped to BTN_BACK.

[1] qtwayland/src/client/qwaylandinputdevice.cpp
parent 3fa065a2
......@@ -52,6 +52,8 @@ private Q_SLOTS:
void testCapabilities_data();
void testCapabilities();
void testPointer();
void testPointerButton_data();
void testPointerButton();
void testKeyboard();
void testCast();
void testDestroy();
......@@ -391,6 +393,97 @@ void TestWaylandSeat::testPointer()
QVERIFY(!serverPointer->focusedSurface());
}
Q_DECLARE_METATYPE(Qt::MouseButton)
void TestWaylandSeat::testPointerButton_data()
{
QTest::addColumn<Qt::MouseButton>("qtButton");
QTest::addColumn<quint32>("waylandButton");
QTest::newRow("left") << Qt::LeftButton << quint32(BTN_LEFT);
QTest::newRow("right") << Qt::RightButton << quint32(BTN_RIGHT);
QTest::newRow("mid") << Qt::MidButton << quint32(BTN_MIDDLE);
QTest::newRow("middle") << Qt::MiddleButton << quint32(BTN_MIDDLE);
QTest::newRow("back") << Qt::BackButton << quint32(BTN_BACK);
QTest::newRow("x1") << Qt::XButton1 << quint32(BTN_BACK);
QTest::newRow("extra1") << Qt::ExtraButton1 << quint32(BTN_BACK);
QTest::newRow("forward") << Qt::ForwardButton << quint32(BTN_FORWARD);
QTest::newRow("x2") << Qt::XButton2 << quint32(BTN_FORWARD);
QTest::newRow("extra2") << Qt::ExtraButton2 << quint32(BTN_FORWARD);
QTest::newRow("task") << Qt::TaskButton << quint32(BTN_TASK);
QTest::newRow("extra3") << Qt::ExtraButton3 << quint32(BTN_TASK);
QTest::newRow("extra4") << Qt::ExtraButton4 << quint32(BTN_EXTRA);
QTest::newRow("extra5") << Qt::ExtraButton5 << quint32(BTN_SIDE);
QTest::newRow("extra6") << Qt::ExtraButton6 << quint32(0x118);
QTest::newRow("extra7") << Qt::ExtraButton7 << quint32(0x119);
QTest::newRow("extra8") << Qt::ExtraButton8 << quint32(0x11a);
QTest::newRow("extra9") << Qt::ExtraButton9 << quint32(0x11b);
QTest::newRow("extra10") << Qt::ExtraButton10 << quint32(0x11c);
QTest::newRow("extra11") << Qt::ExtraButton11 << quint32(0x11d);
QTest::newRow("extra12") << Qt::ExtraButton12 << quint32(0x11e);
QTest::newRow("extra13") << Qt::ExtraButton13 << quint32(0x11f);
}
void TestWaylandSeat::testPointerButton()
{
using namespace KWayland::Client;
using namespace KWayland::Server;
QSignalSpy pointerSpy(m_seat, SIGNAL(hasPointerChanged(bool)));
QVERIFY(pointerSpy.isValid());
m_seatInterface->setHasPointer(true);
QVERIFY(pointerSpy.wait());
QSignalSpy surfaceCreatedSpy(m_compositorInterface, SIGNAL(surfaceCreated(KWayland::Server::SurfaceInterface*)));
QVERIFY(surfaceCreatedSpy.isValid());
m_compositor->createSurface(m_compositor);
QVERIFY(surfaceCreatedSpy.wait());
SurfaceInterface *serverSurface = surfaceCreatedSpy.first().first().value<KWayland::Server::SurfaceInterface*>();
QVERIFY(serverSurface);
QScopedPointer<Pointer> p(m_seat->createPointer());
QVERIFY(p->isValid());
QSignalSpy buttonChangedSpy(p.data(), SIGNAL(buttonStateChanged(quint32, quint32, quint32, KWayland::Client::Pointer::ButtonState)));
QVERIFY(buttonChangedSpy.isValid());
wl_display_flush(m_connection->display());
QCoreApplication::processEvents();
PointerInterface *serverPointer = m_seatInterface->pointer();
serverPointer->setGlobalPos(QPoint(20, 18));
serverPointer->setFocusedSurface(serverSurface, QPoint(10, 15));
// no pointer yet - won't be set
QVERIFY(serverPointer->focusedSurface());
QFETCH(Qt::MouseButton, qtButton);
QFETCH(quint32, waylandButton);
quint32 msec = QDateTime::currentMSecsSinceEpoch();
QCOMPARE(serverPointer->isButtonPressed(waylandButton), false);
QCOMPARE(serverPointer->isButtonPressed(qtButton), false);
serverPointer->updateTimestamp(msec);
serverPointer->buttonPressed(qtButton);
QCOMPARE(serverPointer->isButtonPressed(waylandButton), true);
QCOMPARE(serverPointer->isButtonPressed(qtButton), true);
QVERIFY(buttonChangedSpy.wait());
QCOMPARE(buttonChangedSpy.count(), 1);
QCOMPARE(buttonChangedSpy.last().at(0).value<quint32>(), serverPointer->buttonSerial(waylandButton));
QCOMPARE(buttonChangedSpy.last().at(0).value<quint32>(), serverPointer->buttonSerial(qtButton));
QCOMPARE(buttonChangedSpy.last().at(1).value<quint32>(), msec);
QCOMPARE(buttonChangedSpy.last().at(2).value<quint32>(), waylandButton);
QCOMPARE(buttonChangedSpy.last().at(3).value<KWayland::Client::Pointer::ButtonState>(), Pointer::ButtonState::Pressed);
msec = QDateTime::currentMSecsSinceEpoch();
serverPointer->updateTimestamp(QDateTime::currentMSecsSinceEpoch());
serverPointer->buttonReleased(qtButton);
QCOMPARE(serverPointer->isButtonPressed(waylandButton), false);
QCOMPARE(serverPointer->isButtonPressed(qtButton), false);
QVERIFY(buttonChangedSpy.wait());
QCOMPARE(buttonChangedSpy.count(), 2);
QCOMPARE(buttonChangedSpy.last().at(0).value<quint32>(), serverPointer->buttonSerial(waylandButton));
QCOMPARE(buttonChangedSpy.last().at(0).value<quint32>(), serverPointer->buttonSerial(qtButton));
QCOMPARE(buttonChangedSpy.last().at(1).value<quint32>(), msec);
QCOMPARE(buttonChangedSpy.last().at(2).value<quint32>(), waylandButton);
QCOMPARE(buttonChangedSpy.last().at(3).value<KWayland::Client::Pointer::ButtonState>(), Pointer::ButtonState::Released);
}
void TestWaylandSeat::testKeyboard()
{
using namespace KWayland::Client;
......
......@@ -30,6 +30,8 @@ License along with this library. If not, see <http://www.gnu.org/licenses/>.
#ifndef WL_SEAT_NAME_SINCE_VERSION
#define WL_SEAT_NAME_SINCE_VERSION 2
#endif
// linux
#include <linux/input.h>
namespace KWayland
{
......@@ -451,6 +453,30 @@ void PointerInterface::updateTimestamp(quint32 time)
d->eventTime = time;
}
static quint32 qtToWaylandButton(Qt::MouseButton button)
{
static const QHash<Qt::MouseButton, quint32> s_buttons({
{Qt::LeftButton, BTN_LEFT},
{Qt::RightButton, BTN_RIGHT},
{Qt::MiddleButton, BTN_MIDDLE},
{Qt::ExtraButton1, BTN_BACK}, // note: QtWayland maps BTN_SIDE
{Qt::ExtraButton2, BTN_FORWARD}, // note: QtWayland maps BTN_EXTRA
{Qt::ExtraButton3, BTN_TASK}, // note: QtWayland maps BTN_FORWARD
{Qt::ExtraButton4, BTN_EXTRA}, // note: QtWayland maps BTN_BACK
{Qt::ExtraButton5, BTN_SIDE}, // note: QtWayland maps BTN_TASK
{Qt::ExtraButton6, BTN_TASK + 1},
{Qt::ExtraButton7, BTN_TASK + 2},
{Qt::ExtraButton8, BTN_TASK + 3},
{Qt::ExtraButton9, BTN_TASK + 4},
{Qt::ExtraButton10, BTN_TASK + 5},
{Qt::ExtraButton11, BTN_TASK + 6},
{Qt::ExtraButton12, BTN_TASK + 7},
{Qt::ExtraButton13, BTN_TASK + 8}
// further mapping not possible, 0x120 is BTN_JOYSTICK
});
return s_buttons.value(button, 0);
};
void PointerInterface::buttonPressed(quint32 button)
{
const quint32 serial = d->display->nextSerial();
......@@ -462,6 +488,15 @@ void PointerInterface::buttonPressed(quint32 button)
wl_pointer_send_button(d->focusedSurface.pointer, serial, d->eventTime, button, WL_POINTER_BUTTON_STATE_PRESSED);
}
void PointerInterface::buttonPressed(Qt::MouseButton button)
{
const quint32 nativeButton = qtToWaylandButton(button);
if (nativeButton == 0) {
return;
}
buttonPressed(nativeButton);
}
void PointerInterface::buttonReleased(quint32 button)
{
const quint32 serial = d->display->nextSerial();
......@@ -473,6 +508,15 @@ void PointerInterface::buttonReleased(quint32 button)
wl_pointer_send_button(d->focusedSurface.pointer, serial, d->eventTime, button, WL_POINTER_BUTTON_STATE_RELEASED);
}
void PointerInterface::buttonReleased(Qt::MouseButton button)
{
const quint32 nativeButton = qtToWaylandButton(button);
if (nativeButton == 0) {
return;
}
buttonReleased(nativeButton);
}
void PointerInterface::Private::updateButtonSerial(quint32 button, quint32 serial)
{
auto it = buttonSerials.find(button);
......@@ -492,6 +536,11 @@ quint32 PointerInterface::buttonSerial(quint32 button) const
return it.value();
}
quint32 PointerInterface::buttonSerial(Qt::MouseButton button) const
{
return buttonSerial(qtToWaylandButton(button));
}
void PointerInterface::Private::updateButtonState(quint32 button, ButtonState state)
{
auto it = buttonStates.find(button);
......@@ -511,6 +560,15 @@ bool PointerInterface::isButtonPressed(quint32 button) const
return it.value() == Private::ButtonState::Pressed ? true : false;
}
bool PointerInterface::isButtonPressed(Qt::MouseButton button) const
{
const quint32 nativeButton = qtToWaylandButton(button);
if (nativeButton == 0) {
return false;
}
return isButtonPressed(nativeButton);
}
void PointerInterface::axis(Qt::Orientation orientation, quint32 delta)
{
if (!d->focusedSurface.surface || !d->focusedSurface.pointer) {
......
......@@ -93,9 +93,13 @@ public:
void setGlobalPos(const QPoint &pos);
QPoint globalPos() const;
void buttonPressed(quint32 button);
void buttonPressed(Qt::MouseButton button);
void buttonReleased(quint32 button);
void buttonReleased(Qt::MouseButton button);
bool isButtonPressed(quint32 button) const;
bool isButtonPressed(Qt::MouseButton button) const;
quint32 buttonSerial(quint32 button) const;
quint32 buttonSerial(Qt::MouseButton button) const;
void axis(Qt::Orientation orientation, quint32 delta);
void setFocusedSurface(SurfaceInterface *surface, const QPoint &surfacePosition = QPoint());
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment