summaryrefslogtreecommitdiffstats
path: root/src/compositor/wayland_wrapper/qwlinputdevice.cpp
blob: 12c38bf83cef02b6e8b2c774509e5c6e7c34d067 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/****************************************************************************
**
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing/
**
** This file is part of the QtWaylandCompositor module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL3$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see http://www.qt.io/terms-conditions. For further
** information use the contact form at http://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPLv3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or later as published by the Free
** Software Foundation and appearing in the file LICENSE.GPL included in
** the packaging of this file. Please review the following information to
** ensure the GNU General Public License version 2.0 requirements will be
** met: http://www.gnu.org/licenses/gpl-2.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "qwlinputdevice_p.h"

#include <QtCompositor/QWaylandCompositor>
#include <QtCompositor/private/qwaylandcompositor_p.h>
#include "qwldatadevice_p.h"
#include "qwlinputmethod_p.h"
#include "qwlqttouch_p.h"
#include "qwlqtkey_p.h"
#include "qwaylanddrag.h"
#include "qwlpointer_p.h"
#include "qwlkeyboard_p.h"
#include "qwltouch_p.h"
#include "qwaylandview.h"
#include <QtCompositor/QWaylandClient>

#include <QtGui/QTouchEvent>

QT_BEGIN_NAMESPACE

QWaylandInputDevicePrivate::QWaylandInputDevicePrivate(QWaylandInputDevice *inputdevice, QWaylandCompositor *compositor)
    : QObjectPrivate()
    , QtWaylandServer::wl_seat(compositor->display(), 3)
    , m_dragHandle(new QWaylandDrag(inputdevice))
    , m_compositor(compositor)
    , m_outputSpace(compositor->primaryOutputSpace())
    , m_mouseFocus(Q_NULLPTR)
    , m_capabilities()
    , m_inputMethod(m_compositor->extensionFlags() & QWaylandCompositor::TextInputExtension ? new QtWayland::InputMethod(m_compositor, inputdevice) : 0)
    , m_data_device()
{
}

QWaylandInputDevicePrivate::~QWaylandInputDevicePrivate()
{
}

QWaylandPointer *QWaylandInputDevicePrivate::pointerDevice() const
{
    return m_pointer.data();
}

QWaylandKeyboard *QWaylandInputDevicePrivate::keyboardDevice() const
{
    return m_keyboard.data();
}

QWaylandTouch *QWaylandInputDevicePrivate::touchDevice() const
{
    return m_touch.data();
}

QtWayland::InputMethod *QWaylandInputDevicePrivate::inputMethod()
{
    return m_inputMethod.data();
}


void QWaylandInputDevicePrivate::seat_destroy_resource(wl_seat::Resource *)
{
//    cleanupDataDeviceForClient(resource->client(), true);
}

void QWaylandInputDevicePrivate::seat_bind_resource(wl_seat::Resource *resource)
{
    // The order of m_capabilities matches the order defined in the wayland protocol
    wl_seat::send_capabilities(resource->handle, (uint32_t)m_capabilities);
}

void QWaylandInputDevicePrivate::setCapabilities(QWaylandInputDevice::CapabilityFlags caps)
{
    Q_Q(QWaylandInputDevice);
    if (m_capabilities != caps) {
        QWaylandInputDevice::CapabilityFlags changed = caps ^ m_capabilities;

        if (changed & QWaylandInputDevice::Pointer) {
            m_pointer.reset(m_pointer.isNull() ? QWaylandCompositorPrivate::get(compositor())->callCreatePointerDevice(q) : 0);
        }

        if (changed & QWaylandInputDevice::Keyboard) {
            m_keyboard.reset(m_keyboard.isNull() ? QWaylandCompositorPrivate::get(compositor())->callCreateKeyboardDevice(q) : 0);
        }

        if (changed & QWaylandInputDevice::Touch) {
            m_touch.reset(m_touch.isNull() ? QWaylandCompositorPrivate::get(compositor())->callCreateTouchDevice(q) : 0);
        }

        m_capabilities = caps;
        QList<Resource *> resources = resourceMap().values();
        for (int i = 0; i < resources.size(); i++) {
            wl_seat::send_capabilities(resources.at(i)->handle, (uint32_t)m_capabilities);
        }
    }
}

void QWaylandInputDevicePrivate::seat_get_pointer(wl_seat::Resource *resource, uint32_t id)
{
    if (!m_pointer.isNull()) {
        m_pointer->addClient(QWaylandClient::fromWlClient(m_compositor, resource->client()), id);
    }
}

void QWaylandInputDevicePrivate::seat_get_keyboard(wl_seat::Resource *resource, uint32_t id)
{
    if (!m_keyboard.isNull()) {
        m_keyboard->addClient(QWaylandClient::fromWlClient(m_compositor, resource->client()), id);
    }
}

void QWaylandInputDevicePrivate::seat_get_touch(wl_seat::Resource *resource, uint32_t id)
{
    if (!m_touch.isNull()) {
        m_touch->addClient(QWaylandClient::fromWlClient(m_compositor, resource->client()), id);
    }
}

void QWaylandInputDevicePrivate::sendMousePressEvent(Qt::MouseButton button)
{
    pointerDevice()->sendMousePressEvent(button);
}

void QWaylandInputDevicePrivate::sendMouseReleaseEvent(Qt::MouseButton button)
{
    pointerDevice()->sendMouseReleaseEvent(button);
}

void QWaylandInputDevicePrivate::sendMouseMoveEvent(QWaylandView *view, const QPointF &localPos, const QPointF &globalPos)
{
    pointerDevice()->sendMouseMoveEvent(view, localPos,globalPos);
}

void QWaylandInputDevicePrivate::sendMouseWheelEvent(Qt::Orientation orientation, int delta)
{
    pointerDevice()->sendMouseWheelEvent(orientation, delta);
}

void QWaylandInputDevicePrivate::sendTouchPointEvent(int id, const QPointF &point, Qt::TouchPointState state)
{
    if (m_touch.isNull()) {
        return;
    }
    m_touch->sendTouchPointEvent(id, point,state);
}

void QWaylandInputDevicePrivate::sendTouchFrameEvent()
{
    if (!m_touch.isNull()) {
        m_touch->sendFrameEvent();
    }
}

void QWaylandInputDevicePrivate::sendTouchCancelEvent()
{
    if (!m_touch.isNull()) {
        m_touch->sendCancelEvent();
    }
}

void QWaylandInputDevicePrivate::sendFullKeyEvent(QKeyEvent *event)
{
    if (!keyboardFocus()) {
        qWarning("Cannot send key event, no keyboard focus, fix the compositor");
        return;
    }

    QtWayland::QtKeyExtensionGlobal *ext = QtWayland::QtKeyExtensionGlobal::findIn(m_compositor);
    if (ext && ext->postQtKeyEvent(event, keyboardFocus()))
        return;

    if (!m_keyboard.isNull() && !event->isAutoRepeat()) {
        if (event->type() == QEvent::KeyPress)
            m_keyboard->sendKeyPressEvent(event->nativeScanCode());
        else if (event->type() == QEvent::KeyRelease)
            m_keyboard->sendKeyReleaseEvent(event->nativeScanCode());
    }
}

void QWaylandInputDevicePrivate::sendFullKeyEvent(QWaylandSurface *surface, QKeyEvent *event)
{
    QtWayland::QtKeyExtensionGlobal *ext = QtWayland::QtKeyExtensionGlobal::findIn(m_compositor);
    if (ext)
        ext->postQtKeyEvent(event, surface);
}

void QWaylandInputDevicePrivate::sendFullTouchEvent(QTouchEvent *event)
{
    if (!mouseFocus()) {
        qWarning("Cannot send touch event, no pointer focus, fix the compositor");
        return;
    }

    if (!m_touch)
        return;

    m_touch->sendFullTouchEvent(event);
}

QWaylandSurface *QWaylandInputDevicePrivate::keyboardFocus() const
{
    if (m_keyboard.isNull() || !m_keyboard->focus())
        return Q_NULLPTR;

    return m_keyboard->focus();
}

/*!
 * \return True if the keyboard focus is changed successfully. False for inactive transient surfaces.
 */
bool QWaylandInputDevicePrivate::setKeyboardFocus(QWaylandSurface *surface)
{
    if (surface && surface->isDestroyed())
        return false;

    if (!m_keyboard.isNull() && m_keyboard->setFocus(surface)) {
        if (m_data_device)
            m_data_device->setFocus(m_keyboard->focusClient());
        return true;
    }
    return false;
}

QWaylandView *QWaylandInputDevicePrivate::mouseFocus() const
{
    return m_mouseFocus;
}

void QWaylandInputDevicePrivate::setMouseFocus(QWaylandView *focus)
{
    Q_Q(QWaylandInputDevice);
    if (focus == m_mouseFocus)
        return;

    QWaylandView *oldFocus = m_mouseFocus;
    m_mouseFocus = focus;
    q->mouseFocusChanged(m_mouseFocus, oldFocus);
}

void QWaylandInputDevicePrivate::clientRequestedDataDevice(QtWayland::DataDeviceManager *, struct wl_client *client, uint32_t id)
{
    Q_Q(QWaylandInputDevice);
    if (!m_data_device)
        m_data_device.reset(new QtWayland::DataDevice(q));
    m_data_device->add(client, id, 1);
}

QWaylandCompositor *QWaylandInputDevicePrivate::compositor() const
{
    return m_compositor;
}

QWaylandDrag *QWaylandInputDevicePrivate::dragHandle() const
{
    return m_dragHandle.data();
}

const QtWayland::DataDevice *QWaylandInputDevicePrivate::dataDevice() const
{
    return m_data_device.data();
}

QWaylandOutputSpace *QWaylandInputDevicePrivate::outputSpace() const
{
    return m_outputSpace;
}

void QWaylandInputDevicePrivate::setOutputSpace(QWaylandOutputSpace *outputSpace)
{
    m_outputSpace = outputSpace;
}

QWaylandInputDevicePrivate *QWaylandInputDevicePrivate::get(QWaylandInputDevice *device)
{
    return device->d_func();
}

QT_END_NAMESPACE