1 /****************************************************************************
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
6 ** This file is part of the Qt Compositor.
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** You may use this file under the terms of the BSD license as follows:
11 ** "Redistribution and use in source and binary forms, with or without
12 ** modification, are permitted provided that the following conditions are
14 ** * Redistributions of source code must retain the above copyright
15 ** notice, this list of conditions and the following disclaimer.
16 ** * Redistributions in binary form must reproduce the above copyright
17 ** notice, this list of conditions and the following disclaimer in
18 ** the documentation and/or other materials provided with the
20 ** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
21 ** of its contributors may be used to endorse or promote products derived
22 ** from this software without specific prior written permission.
25 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
39 ****************************************************************************/
42 #include "wlsurface.h"
43 #include <QTouchEvent>
48 static void dummy(wl_client *, wl_resource *)
52 const struct wl_touch_extension_interface TouchExtensionGlobal::touch_interface = {
56 static const int maxRawPos = 24;
58 TouchExtensionGlobal::TouchExtensionGlobal(Compositor *compositor)
59 : m_compositor(compositor),
62 wl_array_init(&m_rawdata_array);
63 m_rawdata_ptr = static_cast<float *>(wl_array_add(&m_rawdata_array, maxRawPos * sizeof(float) * 2));
65 wl_display_add_global(compositor->wl_display(),
66 &wl_touch_extension_interface,
68 TouchExtensionGlobal::bind_func);
71 TouchExtensionGlobal::~TouchExtensionGlobal()
73 wl_array_release(&m_rawdata_array);
76 void TouchExtensionGlobal::destroy_resource(wl_resource *resource)
78 TouchExtensionGlobal *self = static_cast<TouchExtensionGlobal *>(resource->data);
79 self->m_resources.removeOne(resource);
83 void TouchExtensionGlobal::bind_func(wl_client *client, void *data, uint32_t version, uint32_t id)
86 wl_resource *resource = wl_client_add_object(client, &wl_touch_extension_interface, &touch_interface, id, data);
87 resource->destroy = destroy_resource;
88 TouchExtensionGlobal *self = static_cast<TouchExtensionGlobal *>(resource->data);
89 self->m_resources.append(resource);
90 wl_touch_extension_send_configure(resource, self->m_flags);
93 static inline int toFixed(qreal f)
95 return int(f * 10000);
98 bool TouchExtensionGlobal::postTouchEvent(QTouchEvent *event, Surface *surface)
100 const QList<QTouchEvent::TouchPoint> points = event->touchPoints();
101 const int pointCount = points.count();
105 QPointF surfacePos = surface->pos();
106 wl_client *surfaceClient = surface->base()->resource.client;
107 uint32_t time = m_compositor->currentTimeMsecs();
108 const int rescount = m_resources.count();
110 for (int res = 0; res < rescount; ++res) {
111 wl_resource *target = m_resources.at(res);
112 if (target->client != surfaceClient)
115 // We will use no touch_frame type of event, to reduce the number of
116 // events flowing through the wire. Instead, the number of points sent is
117 // included in the touch point events.
118 int sentPointCount = 0;
119 for (int i = 0; i < pointCount; ++i) {
120 if (points.at(i).state() != Qt::TouchPointStationary)
124 for (int i = 0; i < pointCount; ++i) {
125 const QTouchEvent::TouchPoint &tp(points.at(i));
126 // Stationary points are never sent. They are cached on client side.
127 if (tp.state() == Qt::TouchPointStationary)
130 uint32_t id = tp.id();
131 uint32_t state = (tp.state() & 0xFFFF) | (sentPointCount << 16);
132 uint32_t flags = (tp.flags() & 0xFFFF) | (int(event->device()->capabilities()) << 16);
134 QPointF p = tp.pos() - surfacePos; // surface-relative
135 int x = toFixed(p.x());
136 int y = toFixed(p.y());
137 int nx = toFixed(tp.normalizedPos().x());
138 int ny = toFixed(tp.normalizedPos().y());
139 int w = toFixed(tp.rect().width());
140 int h = toFixed(tp.rect().height());
141 int vx = toFixed(tp.velocity().x());
142 int vy = toFixed(tp.velocity().y());
143 uint32_t pressure = uint32_t(tp.pressure() * 255);
145 wl_array *rawData = 0;
146 QVector<QPointF> rawPosList = tp.rawScreenPositions();
147 int rawPosCount = rawPosList.count();
149 rawPosCount = qMin(maxRawPos, rawPosCount);
150 rawData = &m_rawdata_array;
151 rawData->size = rawPosCount * sizeof(float) * 2;
152 float *p = m_rawdata_ptr;
153 for (int rpi = 0; rpi < rawPosCount; ++rpi) {
154 const QPointF &rawPos(rawPosList.at(rpi));
155 // This will stay in screen coordinates for performance
156 // reasons, clients using this data will presumably know
157 // what they are doing.
158 *p++ = float(rawPos.x());
159 *p++ = float(rawPos.y());
163 wl_touch_extension_send_touch(target,