on c7eed8a help sign in
unity8/MouseTouchAdaptor.cpp
136 lines 4.4 kB history raw
1/*
2 * Copyright (C) 2013 Canonical, Ltd.
3 *
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.
7 *
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.
12 *
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/>.
15 *
16 * Authored by: Daniel d'Andrada <daniel.dandrada@canonical.com>
17 */
19#include "MouseTouchAdaptor.h"
21#include <qpa/qwindowsysteminterface.h>
23#include <QtGui/QMouseEvent>
24#include <QtTest/QTest>
26using QTest::QTouchEventSequence;
28namespace {
29Qt::MouseButton translateMouseButton(xcb_button_t detail)
31 switch (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;
37 }
39} // end of anonymous namespace
41MouseTouchAdaptor::MouseTouchAdaptor()
42 : m_targetWindow(0)
43 , m_leftButtonIsPressed(false)
45 m_touchDevice = new QTouchDevice;
46 m_touchDevice->setType(QTouchDevice::TouchScreen);
47 QWindowSystemInterface::registerTouchDevice(m_touchDevice);
50void MouseTouchAdaptor::setTargetWindow(QWindow *window)
52 m_targetWindow = window;
55bool MouseTouchAdaptor::nativeEventFilter(const QByteArray & eventType,
56 void * message, long * /*result*/)
58 Q_ASSERT(m_targetWindow);
60 if (eventType != "xcb_generic_event_t") {
61 // wrong backend.
62 qWarning("MouseTouchAdaptor: XCB backend not in use. Adaptor inoperative!");
63 return false;
64 }
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));
71 break;
72 case XCB_BUTTON_RELEASE:
73 return handleButtonRelease(reinterpret_cast<xcb_button_release_event_t *>(xcbEvent));
74 break;
75 case XCB_MOTION_NOTIFY:
76 return handleMotionNotify(reinterpret_cast<xcb_motion_notify_event_t *>(xcbEvent));
77 break;
78 default:
79 return false;
80 break;
81 };
84bool 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)
90 return true;
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;
100 return true;
103bool 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)
109 return true;
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;
119 return true;
122bool MouseTouchAdaptor::handleMotionNotify(xcb_motion_notify_event_t *event)
124 if (!m_leftButtonIsPressed) {
125 return true;
126 }
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 */);
135 return true;