1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the plugins of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include "qwaylandtouch.h"
43 #include "qwaylandinputdevice.h"
45 #include "wayland-touch-extension-client-protocol.h"
47 QWaylandTouchExtension::QWaylandTouchExtension(QWaylandDisplay *display, uint32_t id)
55 mTouch = static_cast<struct wl_touch_extension *>(wl_display_bind(display->wl_display(), id, &wl_touch_extension_interface));
56 wl_touch_extension_add_listener(mTouch, &touch_listener, this);
59 void QWaylandTouchExtension::registerDevice(int caps)
61 mTouchDevice = new QTouchDevice;
62 mTouchDevice->setType(QTouchDevice::TouchScreen);
63 mTouchDevice->setCapabilities(QTouchDevice::Capabilities(caps));
64 QWindowSystemInterface::registerTouchDevice(mTouchDevice);
67 static inline qreal fromFixed(int f)
69 return f / qreal(10000);
72 void QWaylandTouchExtension::handle_touch(void *data, wl_touch_extension *ext, uint32_t time,
73 uint32_t id, uint32_t state, int32_t x, int32_t y,
74 int32_t normalized_x, int32_t normalized_y,
75 int32_t width, int32_t height, uint32_t pressure,
76 int32_t velocity_x, int32_t velocity_y,
77 uint32_t flags, wl_array *rawdata)
80 QWaylandTouchExtension *self = static_cast<QWaylandTouchExtension *>(data);
82 if (!self->mInputDevice) {
83 QList<QWaylandInputDevice *> inputDevices = self->mDisplay->inputDevices();
84 if (inputDevices.isEmpty()) {
85 qWarning("wl_touch_extension: handle_touch: No input devices");
88 self->mInputDevice = inputDevices.first();
90 QWaylandWindow *win = self->mInputDevice->mTouchFocus;
92 win = self->mInputDevice->mPointerFocus;
94 win = self->mInputDevice->mKeyboardFocus;
95 if (!win || !win->window()) {
96 qWarning("wl_touch_extension: handle_touch: No pointer focus");
99 self->mTargetWindow = win->window();
101 QWindowSystemInterface::TouchPoint tp;
103 tp.state = Qt::TouchPointState(int(state & 0xFFFF));
104 int sentPointCount = state >> 16;
105 if (!self->mPointsLeft) {
106 Q_ASSERT(sentPointCount > 0);
107 self->mPointsLeft = sentPointCount;
109 tp.flags = QTouchEvent::TouchPoint::InfoFlags(int(flags & 0xFFFF));
111 if (!self->mTouchDevice)
112 self->registerDevice(flags >> 16);
114 tp.area = QRectF(0, 0, fromFixed(width), fromFixed(height));
115 // Got surface-relative coords but need a (virtual) screen position.
116 QPointF relPos = QPointF(fromFixed(x), fromFixed(y));
117 QPointF delta = relPos - relPos.toPoint();
118 tp.area.moveCenter(self->mTargetWindow->mapToGlobal(relPos.toPoint()) + delta);
120 tp.normalPosition.setX(fromFixed(normalized_x));
121 tp.normalPosition.setY(fromFixed(normalized_y));
122 tp.pressure = pressure / 255.0;
123 tp.velocity.setX(fromFixed(velocity_x));
124 tp.velocity.setY(fromFixed(velocity_y));
127 const int rawPosCount = rawdata->size / sizeof(float) / 2;
128 float *p = static_cast<float *>(rawdata->data);
129 for (int i = 0; i < rawPosCount; ++i) {
132 tp.rawPositions.append(QPointF(x, y));
136 self->mTouchPoints.append(tp);
137 self->mTimestamp = time;
139 if (!--self->mPointsLeft)
140 self->sendTouchEvent();
143 void QWaylandTouchExtension::sendTouchEvent()
145 // Copy all points, that are in the previous but not in the current list, as stationary.
146 for (int i = 0; i < mPrevTouchPoints.count(); ++i) {
147 const QWindowSystemInterface::TouchPoint &prevPoint(mPrevTouchPoints.at(i));
148 if (prevPoint.state == Qt::TouchPointReleased)
151 for (int j = 0; j < mTouchPoints.count(); ++j)
152 if (mTouchPoints.at(j).id == prevPoint.id) {
157 QWindowSystemInterface::TouchPoint p = prevPoint;
158 p.state = Qt::TouchPointStationary;
159 mTouchPoints.append(p);
163 if (mTouchPoints.isEmpty()) {
164 mPrevTouchPoints.clear();
168 QWindowSystemInterface::handleTouchEvent(mTargetWindow, mTimestamp, mTouchDevice, mTouchPoints);
170 Qt::TouchPointStates states = 0;
171 for (int i = 0; i < mTouchPoints.count(); ++i)
172 states |= mTouchPoints.at(i).state;
174 if (mFlags & WL_TOUCH_EXTENSION_FLAGS_MOUSE_FROM_TOUCH) {
175 if (states == Qt::TouchPointPressed)
176 mMouseSourceId = mTouchPoints.first().id;
177 for (int i = 0; i < mTouchPoints.count(); ++i) {
178 const QWindowSystemInterface::TouchPoint &tp(mTouchPoints.at(i));
179 if (tp.id == mMouseSourceId) {
180 Qt::MouseButtons buttons = tp.state == Qt::TouchPointReleased ? Qt::NoButton : Qt::LeftButton;
181 mLastMouseGlobal = tp.area.center();
182 QPoint globalPoint = mLastMouseGlobal.toPoint();
183 QPointF delta = mLastMouseGlobal - globalPoint;
184 mLastMouseLocal = mTargetWindow->mapFromGlobal(globalPoint) + delta;
185 QWindowSystemInterface::handleMouseEvent(mTargetWindow, mTimestamp, mLastMouseLocal, mLastMouseGlobal, buttons);
186 if (buttons == Qt::NoButton)
193 mPrevTouchPoints = mTouchPoints;
194 mTouchPoints.clear();
196 if (states == Qt::TouchPointReleased)
197 mPrevTouchPoints.clear();
200 void QWaylandTouchExtension::touchCanceled()
202 mTouchPoints.clear();
203 mPrevTouchPoints.clear();
204 if (mMouseSourceId != -1)
205 QWindowSystemInterface::handleMouseEvent(mTargetWindow, mTimestamp, mLastMouseLocal, mLastMouseGlobal, Qt::NoButton);
208 void QWaylandTouchExtension::handle_configure(void *data, wl_touch_extension *ext, uint32_t flags)
211 QWaylandTouchExtension *self = static_cast<QWaylandTouchExtension *>(data);
212 self->mFlags = flags;
215 const struct wl_touch_extension_listener QWaylandTouchExtension::touch_listener = {
216 QWaylandTouchExtension::handle_touch,
217 QWaylandTouchExtension::handle_configure