Change copyrights from Nokia to Digia
[profile/ivi/qtwayland.git] / src / plugins / platforms / wayland / qwaylandtouch.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the plugins of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.  For licensing terms and
14 ** conditions see http://qt.digia.com/licensing.  For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file.  Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights.  These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file.  Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "qwaylandtouch.h"
43 #include "qwaylandinputdevice.h"
44
45 #include "wayland-touch-extension-client-protocol.h"
46
47 QWaylandTouchExtension::QWaylandTouchExtension(QWaylandDisplay *display, uint32_t id)
48     : mDisplay(display),
49       mTouchDevice(0),
50       mPointsLeft(0),
51       mFlags(0),
52       mMouseSourceId(-1),
53       mInputDevice(0)
54 {
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);
57 }
58
59 void QWaylandTouchExtension::registerDevice(int caps)
60 {
61     mTouchDevice = new QTouchDevice;
62     mTouchDevice->setType(QTouchDevice::TouchScreen);
63     mTouchDevice->setCapabilities(QTouchDevice::Capabilities(caps));
64     QWindowSystemInterface::registerTouchDevice(mTouchDevice);
65 }
66
67 static inline qreal fromFixed(int f)
68 {
69     return f / qreal(10000);
70 }
71
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)
78 {
79     Q_UNUSED(ext);
80     QWaylandTouchExtension *self = static_cast<QWaylandTouchExtension *>(data);
81
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");
86             return;
87         }
88         self->mInputDevice = inputDevices.first();
89     }
90     QWaylandWindow *win = self->mInputDevice->mTouchFocus;
91     if (!win)
92         win = self->mInputDevice->mPointerFocus;
93     if (!win)
94         win = self->mInputDevice->mKeyboardFocus;
95     if (!win || !win->window()) {
96         qWarning("wl_touch_extension: handle_touch: No pointer focus");
97         return;
98     }
99     self->mTargetWindow = win->window();
100
101     QWindowSystemInterface::TouchPoint tp;
102     tp.id = id;
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;
108     }
109     tp.flags = QTouchEvent::TouchPoint::InfoFlags(int(flags & 0xFFFF));
110
111     if (!self->mTouchDevice)
112         self->registerDevice(flags >> 16);
113
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);
119
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));
125
126     if (rawdata) {
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) {
130             float x = *p++;
131             float y = *p++;
132             tp.rawPositions.append(QPointF(x, y));
133         }
134     }
135
136     self->mTouchPoints.append(tp);
137     self->mTimestamp = time;
138
139     if (!--self->mPointsLeft)
140         self->sendTouchEvent();
141 }
142
143 void QWaylandTouchExtension::sendTouchEvent()
144 {
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)
149             continue;
150         bool found = false;
151         for (int j = 0; j < mTouchPoints.count(); ++j)
152             if (mTouchPoints.at(j).id == prevPoint.id) {
153                 found = true;
154                 break;
155             }
156         if (!found) {
157             QWindowSystemInterface::TouchPoint p = prevPoint;
158             p.state = Qt::TouchPointStationary;
159             mTouchPoints.append(p);
160         }
161     }
162
163     if (mTouchPoints.isEmpty()) {
164         mPrevTouchPoints.clear();
165         return;
166     }
167
168     QWindowSystemInterface::handleTouchEvent(mTargetWindow, mTimestamp, mTouchDevice, mTouchPoints);
169
170     Qt::TouchPointStates states = 0;
171     for (int i = 0; i < mTouchPoints.count(); ++i)
172         states |= mTouchPoints.at(i).state;
173
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)
187                     mMouseSourceId = -1;
188                 break;
189             }
190         }
191     }
192
193     mPrevTouchPoints = mTouchPoints;
194     mTouchPoints.clear();
195
196     if (states == Qt::TouchPointReleased)
197         mPrevTouchPoints.clear();
198 }
199
200 void QWaylandTouchExtension::touchCanceled()
201 {
202     mTouchPoints.clear();
203     mPrevTouchPoints.clear();
204     if (mMouseSourceId != -1)
205         QWindowSystemInterface::handleMouseEvent(mTargetWindow, mTimestamp, mLastMouseLocal, mLastMouseGlobal, Qt::NoButton);
206 }
207
208 void QWaylandTouchExtension::handle_configure(void *data, wl_touch_extension *ext, uint32_t flags)
209 {
210     Q_UNUSED(ext);
211     QWaylandTouchExtension *self = static_cast<QWaylandTouchExtension *>(data);
212     self->mFlags = flags;
213 }
214
215 const struct wl_touch_extension_listener QWaylandTouchExtension::touch_listener = {
216     QWaylandTouchExtension::handle_touch,
217     QWaylandTouchExtension::handle_configure
218 };