Change copyrights from Nokia to Digia
[profile/ivi/qtwayland.git] / src / compositor / wayland_wrapper / wltouch.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 Qt Compositor.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** You may use this file under the terms of the BSD license as follows:
10 **
11 ** "Redistribution and use in source and binary forms, with or without
12 ** modification, are permitted provided that the following conditions are
13 ** met:
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
19 **     distribution.
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.
23 **
24 **
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."
36 **
37 ** $QT_END_LICENSE$
38 **
39 ****************************************************************************/
40
41 #include "wltouch.h"
42 #include "wlsurface.h"
43 #include <QTouchEvent>
44 #include <QWindow>
45
46 namespace Wayland {
47
48 static void dummy(wl_client *, wl_resource *)
49 {
50 }
51
52 const struct wl_touch_extension_interface TouchExtensionGlobal::touch_interface = {
53     dummy
54 };
55
56 static const int maxRawPos = 24;
57
58 TouchExtensionGlobal::TouchExtensionGlobal(Compositor *compositor)
59     : m_compositor(compositor),
60       m_flags(0)
61 {
62     wl_array_init(&m_rawdata_array);
63     m_rawdata_ptr = static_cast<float *>(wl_array_add(&m_rawdata_array, maxRawPos * sizeof(float) * 2));
64
65     wl_display_add_global(compositor->wl_display(),
66                           &wl_touch_extension_interface,
67                           this,
68                           TouchExtensionGlobal::bind_func);
69 }
70
71 TouchExtensionGlobal::~TouchExtensionGlobal()
72 {
73     wl_array_release(&m_rawdata_array);
74 }
75
76 void TouchExtensionGlobal::destroy_resource(wl_resource *resource)
77 {
78     TouchExtensionGlobal *self = static_cast<TouchExtensionGlobal *>(resource->data);
79     self->m_resources.removeOne(resource);
80     free(resource);
81 }
82
83 void TouchExtensionGlobal::bind_func(wl_client *client, void *data, uint32_t version, uint32_t id)
84 {
85     Q_UNUSED(version);
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);
91 }
92
93 static inline int toFixed(qreal f)
94 {
95     return int(f * 10000);
96 }
97
98 bool TouchExtensionGlobal::postTouchEvent(QTouchEvent *event, Surface *surface)
99 {
100     const QList<QTouchEvent::TouchPoint> points = event->touchPoints();
101     const int pointCount = points.count();
102     if (!pointCount)
103         return false;
104
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();
109
110     for (int res = 0; res < rescount; ++res) {
111         wl_resource *target = m_resources.at(res);
112         if (target->client != surfaceClient)
113             continue;
114
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)
121                 ++sentPointCount;
122         }
123
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)
128                 continue;
129
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);
133
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);
144
145             wl_array *rawData = 0;
146             QVector<QPointF> rawPosList = tp.rawScreenPositions();
147             int rawPosCount = rawPosList.count();
148             if (rawPosCount) {
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());
160                 }
161             }
162
163             wl_touch_extension_send_touch(target,
164                                    time, id, state,
165                                    x, y, nx, ny, w, h,
166                                    pressure, vx, vy,
167                                    flags, rawData);
168         }
169
170         return true;
171     }
172
173     return false;
174 }
175
176 }