19 #include "MouseTouchAdaptor.h"
21 #include <qpa/qwindowsysteminterface.h>
23 #include <QtGui/QMouseEvent>
24 #include <QtTest/QTest>
26 using QTest::QTouchEventSequence;
29 Qt::MouseButton translateMouseButton(xcb_button_t detail)
32 case 1:
return Qt::LeftButton;
33 case 2:
return Qt::MidButton;
34 case 3:
return Qt::RightButton;
36 default:
return Qt::NoButton;
41 MouseTouchAdaptor::MouseTouchAdaptor()
42 : m_leftButtonIsPressed(false)
44 m_touchDevice =
new QTouchDevice;
45 m_touchDevice->setType(QTouchDevice::TouchScreen);
46 QWindowSystemInterface::registerTouchDevice(m_touchDevice);
49 bool MouseTouchAdaptor::nativeEventFilter(
const QByteArray & eventType,
50 void * message,
long * )
52 if (eventType !=
"xcb_generic_event_t") {
54 qWarning(
"MouseTouchAdaptor: XCB backend not in use. Adaptor inoperative!");
58 xcb_generic_event_t *xcbEvent =
static_cast<xcb_generic_event_t *
>(message);
60 switch (xcbEvent->response_type & ~0x80) {
61 case XCB_BUTTON_PRESS:
62 return handleButtonPress(reinterpret_cast<xcb_button_press_event_t *>(xcbEvent));
64 case XCB_BUTTON_RELEASE:
65 return handleButtonRelease(reinterpret_cast<xcb_button_release_event_t *>(xcbEvent));
67 case XCB_MOTION_NOTIFY:
68 return handleMotionNotify(reinterpret_cast<xcb_motion_notify_event_t *>(xcbEvent));
76 bool MouseTouchAdaptor::handleButtonPress(xcb_button_press_event_t *pressEvent)
78 Qt::MouseButton button = translateMouseButton(pressEvent->detail);
81 if (button != Qt::LeftButton)
84 QPoint windowPos(pressEvent->event_x, pressEvent->event_y);
86 QWindow *targetWindow = findQWindowWithXWindowID(static_cast<WId>(pressEvent->event));
88 QTouchEventSequence touchEvent = QTest::touchEvent(targetWindow, m_touchDevice,
90 touchEvent.press(0 , windowPos);
91 touchEvent.commit(
false );
93 m_leftButtonIsPressed =
true;
97 bool MouseTouchAdaptor::handleButtonRelease(xcb_button_release_event_t *releaseEvent)
99 Qt::MouseButton button = translateMouseButton(releaseEvent->detail);
102 if (button != Qt::LeftButton)
105 QPoint windowPos(releaseEvent->event_x, releaseEvent->event_y);
107 QWindow *targetWindow = findQWindowWithXWindowID(static_cast<WId>(releaseEvent->event));
109 QTouchEventSequence touchEvent = QTest::touchEvent(targetWindow, m_touchDevice,
111 touchEvent.release(0 , windowPos);
112 touchEvent.commit(
false );
114 m_leftButtonIsPressed =
false;
118 bool MouseTouchAdaptor::handleMotionNotify(xcb_motion_notify_event_t *event)
120 if (!m_leftButtonIsPressed) {
124 QPoint windowPos(event->event_x, event->event_y);
126 QWindow *targetWindow = findQWindowWithXWindowID(static_cast<WId>(event->event));
128 QTouchEventSequence touchEvent = QTest::touchEvent(targetWindow, m_touchDevice,
130 touchEvent.move(0 , windowPos);
131 touchEvent.commit(
false );
136 QWindow *MouseTouchAdaptor::findQWindowWithXWindowID(WId windowId)
138 QWindowList windowList = QGuiApplication::topLevelWindows();
139 QWindow *foundWindow =
nullptr;
142 while (!foundWindow && i < windowList.count()) {
143 QWindow *window = windowList[i];
144 if (window->winId() == windowId) {
145 foundWindow = window;
151 Q_ASSERT(foundWindow);