2
* Copyright (C) 2013 Canonical, Ltd.
4
* This program is free software; you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License as published by
6
* the Free Software Foundation; version 3.
8
* This program is distributed in the hope that it will be useful,
9
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
* GNU General Public License for more details.
13
* You should have received a copy of the GNU General Public License
14
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16
* Authored by: Daniel d'Andrada <daniel.dandrada@canonical.com>
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;
35
// Button values 4-7 are Wheel events
36
default: return Qt::NoButton;
39
} // end of anonymous namespace
41
MouseTouchAdaptor::MouseTouchAdaptor()
43
, m_leftButtonIsPressed(false)
45
m_touchDevice = new QTouchDevice;
46
m_touchDevice->setType(QTouchDevice::TouchScreen);
47
QWindowSystemInterface::registerTouchDevice(m_touchDevice);
50
void MouseTouchAdaptor::setTargetWindow(QWindow *window)
52
m_targetWindow = window;
55
bool MouseTouchAdaptor::nativeEventFilter(const QByteArray & eventType,
56
void * message, long * /*result*/)
58
Q_ASSERT(m_targetWindow);
60
if (eventType != "xcb_generic_event_t") {
62
qWarning("MouseTouchAdaptor: XCB backend not in use. Adaptor inoperative!");
66
xcb_generic_event_t *xcbEvent = static_cast<xcb_generic_event_t *>(message);
68
switch (xcbEvent->response_type & ~0x80) {
69
case XCB_BUTTON_PRESS:
70
return handleButtonPress(reinterpret_cast<xcb_button_press_event_t *>(xcbEvent));
72
case XCB_BUTTON_RELEASE:
73
return handleButtonRelease(reinterpret_cast<xcb_button_release_event_t *>(xcbEvent));
75
case XCB_MOTION_NOTIFY:
76
return handleMotionNotify(reinterpret_cast<xcb_motion_notify_event_t *>(xcbEvent));
84
bool MouseTouchAdaptor::handleButtonPress(xcb_button_press_event_t *pressEvent)
86
Qt::MouseButton button = translateMouseButton(pressEvent->detail);
88
// Just eat the event if it wasn't a left mouse press
89
if (button != Qt::LeftButton)
92
QPoint windowPos(pressEvent->event_x, pressEvent->event_y);
94
QTouchEventSequence touchEvent = QTest::touchEvent(m_targetWindow, m_touchDevice,
95
false /* autoCommit */);
96
touchEvent.press(0 /* touchId */, windowPos);
97
touchEvent.commit(false /* processEvents */);
99
m_leftButtonIsPressed = true;
103
bool MouseTouchAdaptor::handleButtonRelease(xcb_button_release_event_t *releaseEvent)
105
Qt::MouseButton button = translateMouseButton(releaseEvent->detail);
107
// Just eat the event if it wasn't a left mouse release
108
if (button != Qt::LeftButton)
111
QPoint windowPos(releaseEvent->event_x, releaseEvent->event_y);
113
QTouchEventSequence touchEvent = QTest::touchEvent(m_targetWindow, m_touchDevice,
114
false /* autoCommit */);
115
touchEvent.release(0 /* touchId */, windowPos);
116
touchEvent.commit(false /* processEvents */);
118
m_leftButtonIsPressed = false;
122
bool MouseTouchAdaptor::handleMotionNotify(xcb_motion_notify_event_t *event)
124
if (!m_leftButtonIsPressed) {
128
QPoint windowPos(event->event_x, event->event_y);
130
QTouchEventSequence touchEvent = QTest::touchEvent(m_targetWindow, m_touchDevice,
131
false /* autoCommit */);
132
touchEvent.move(0 /* touchId */, windowPos);
133
touchEvent.commit(false /* processEvents */);