summaryrefslogtreecommitdiffstats
path: root/src/compositor/wayland_wrapper/qwlpointer.cpp
blob: fe67bd1f0b3175011bff4b872fc9535aad7a2ef7 (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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/****************************************************************************
**
** Copyright (C) 2015 The Qt Company Ltd.
** Copyright (C) 2013 Klarälvdalens Datakonsult AB (KDAB).
** 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 "qwlpointer_p.h"

#include "qwlcompositor_p.h"
#include "qwlinputdevice_p.h"
#include "qwlkeyboard_p.h"
#include "qwlsurface_p.h"
#include "qwaylandcompositor.h"
#include "qwaylandsurfaceview.h"

QT_BEGIN_NAMESPACE

namespace QtWayland {

using QtWaylandServer::wl_keyboard;

static uint32_t toWaylandButton(Qt::MouseButton button)
{
#ifndef BTN_LEFT
    uint32_t BTN_LEFT = 0x110;
#endif
    // the range of valid buttons (evdev module) is from 0x110
    // through 0x11f. 0x120 is the first 'Joystick' button.
    switch (button) {
    case Qt::LeftButton: return BTN_LEFT;
    case Qt::RightButton: return uint32_t(0x111);
    case Qt::MiddleButton: return uint32_t(0x112);
    case Qt::ExtraButton1: return uint32_t(0x113);  // AKA Qt::BackButton, Qt::XButton1
    case Qt::ExtraButton2: return uint32_t(0x114);  // AKA Qt::ForwardButton, Qt::XButton2
    case Qt::ExtraButton3: return uint32_t(0x115);
    case Qt::ExtraButton4: return uint32_t(0x116);
    case Qt::ExtraButton5: return uint32_t(0x117);
    case Qt::ExtraButton6: return uint32_t(0x118);
    case Qt::ExtraButton7: return uint32_t(0x119);
    case Qt::ExtraButton8: return uint32_t(0x11a);
    case Qt::ExtraButton9: return uint32_t(0x11b);
    case Qt::ExtraButton10: return uint32_t(0x11c);
    case Qt::ExtraButton11: return uint32_t(0x11d);
    case Qt::ExtraButton12: return uint32_t(0x11e);
    case Qt::ExtraButton13: return uint32_t(0x11f);
        // default should not occur; but if it does, then return Wayland's highest possible button number.
    default: return uint32_t(0x11f);
    }
}

Pointer::Pointer(Compositor *compositor, InputDevice *seat)
    : wl_pointer()
    , PointerGrabber()
    , m_compositor(compositor)
    , m_seat(seat)
    , m_grab(this)
    , m_grabButton()
    , m_grabTime()
    , m_grabSerial()
    , m_position(100, 100)
    , m_focus()
    , m_focusResource()
    , m_buttonCount()
{
    connect(&m_focusDestroyListener, &WlListener::fired, this, &Pointer::focusDestroyed);
}

void Pointer::setFocus(QWaylandSurfaceView *surface, const QPointF &position)
{
    if (m_focusResource && m_focus != surface) {
        uint32_t serial = wl_display_next_serial(m_compositor->wl_display());
        send_leave(m_focusResource->handle, serial, m_focus->surface()->handle()->resource()->handle);
        m_focusDestroyListener.reset();
    }

    Resource *resource = surface ? resourceMap().value(surface->surface()->handle()->resource()->client()) : 0;

    if (resource && (m_focus != surface || resource != m_focusResource)) {
        uint32_t serial = wl_display_next_serial(m_compositor->wl_display());
        Keyboard *keyboard = m_seat->keyboardDevice();
        if (keyboard) {
            wl_keyboard::Resource *kr = keyboard->resourceMap().value(surface->surface()->handle()->resource()->client());
            if (kr)
                keyboard->sendKeyModifiers(kr, serial);
        }
        send_enter(resource->handle, serial, surface->surface()->handle()->resource()->handle,
                   wl_fixed_from_double(position.x()), wl_fixed_from_double(position.y()));

        m_focusDestroyListener.listenForDestruction(surface->surface()->handle()->resource()->handle);
    }

    m_focusResource = resource;
    m_focus = surface;
}

void Pointer::focusDestroyed(void *data)
{
    Q_UNUSED(data)
    m_focusDestroyListener.reset();

    m_focus = 0;
    m_focusResource = 0;
    m_buttonCount = 0;
    setMouseFocus(0, QPointF(), QPointF());
    endGrab();
}

void Pointer::startGrab(PointerGrabber *grab)
{
    m_grab = grab;
    grab->m_pointer = this;

    if (m_currentPosition.view())
        grab->focus();
}

void Pointer::endGrab()
{
    m_grab = this;
    m_grab->focus();
}

void Pointer::setCurrent(QWaylandSurfaceView *surface, const QPointF &point)
{
    m_currentPosition.setCurrent(surface, point);
}

bool Pointer::buttonPressed() const
{
    return m_buttonCount > 0;
}

PointerGrabber *Pointer::currentGrab() const
{
    return m_grab;
}

Qt::MouseButton Pointer::grabButton() const
{
    return m_grabButton;
}

uint32_t Pointer::grabTime() const
{
    return m_grabTime;
}

uint32_t Pointer::grabSerial() const
{
    return m_grabSerial;
}

QWaylandSurfaceView *Pointer::focusSurface() const
{
    return m_focus;
}

QWaylandSurfaceView *Pointer::current() const
{
    return m_currentPosition.view();
}

QPointF Pointer::position() const
{
    return m_position;
}

QPointF Pointer::currentPosition() const
{
    return m_currentPosition.position();
}

QtWaylandServer::wl_pointer::Resource *Pointer::focusResource() const
{
    return m_focusResource;
}

void Pointer::pointer_destroy_resource(wl_pointer::Resource *resource)
{
    if (m_focusResource == resource)
        m_focusResource = 0;
}

void Pointer::pointer_release(wl_pointer::Resource *resource)
{
    wl_resource_destroy(resource->handle);
}

void Pointer::setMouseFocus(QWaylandSurfaceView *surface, const QPointF &localPos, const QPointF &globalPos)
{
    m_position = globalPos;

    m_currentPosition.setCurrent(surface, localPos);

    m_grab->focus();
}

void Pointer::sendButton(uint32_t time, Qt::MouseButton button, uint32_t state)
{
    uint32_t serial = wl_display_next_serial(m_compositor->wl_display());
    send_button(m_focusResource->handle, serial, time, toWaylandButton(button), state);
}

void Pointer::sendMousePressEvent(Qt::MouseButton button, const QPointF &localPos, const QPointF &globalPos)
{
    sendMouseMoveEvent(localPos, globalPos);
    uint32_t time = m_compositor->currentTimeMsecs();
    if (m_buttonCount == 0) {
        m_grabButton = button;
        m_grabTime = time;
    }
    m_buttonCount++;
    m_grab->button(time, button, WL_POINTER_BUTTON_STATE_PRESSED);

    if (m_buttonCount == 1)
        m_grabSerial = wl_display_get_serial(m_compositor->wl_display());
}

void Pointer::sendMouseReleaseEvent(Qt::MouseButton button, const QPointF &localPos, const QPointF &globalPos)
{
    sendMouseMoveEvent(localPos, globalPos);
    uint32_t time = m_compositor->currentTimeMsecs();
    m_buttonCount--;
    m_grab->button(time, button, WL_POINTER_BUTTON_STATE_RELEASED);

    if (m_buttonCount == 1)
        m_grabSerial = wl_display_get_serial(m_compositor->wl_display());
}

void Pointer::sendMouseMoveEvent(const QPointF &localPos, const QPointF &globalPos)
{
    uint32_t time = m_compositor->currentTimeMsecs();

    m_position = globalPos;

    m_currentPosition.updatePosition(localPos);

    m_grab->motion(time);
}

void Pointer::sendMouseWheelEvent(Qt::Orientation orientation, int delta)
{
    if (!m_focusResource)
        return;

    uint32_t time = m_compositor->currentTimeMsecs();
    uint32_t axis = orientation == Qt::Horizontal ? WL_POINTER_AXIS_HORIZONTAL_SCROLL
                                                  : WL_POINTER_AXIS_VERTICAL_SCROLL;
    send_axis(m_focusResource->handle, time, axis, wl_fixed_from_int(-delta / 12));
}

void Pointer::sendMouseEnterEvent(QWaylandSurfaceView *view, const QPointF &localPos)
{
    if (view == m_currentPosition.view()) {
        qWarning("Possible input state error. Want to send enter event for the current input surface");
    }

    setMouseFocus(view, localPos, QPointF());
}

void Pointer::sendMouseLeaveEvent(QWaylandSurfaceView *view)
{
    if (view != m_currentPosition.view()) {
        return;
    }

    setMouseFocus(Q_NULLPTR, QPointF(), QPointF());
}

void Pointer::focus()
{
    if (buttonPressed())
        return;

    setFocus(m_currentPosition.view(), m_currentPosition.position());
}

void Pointer::motion(uint32_t time)
{
    if (m_focusResource)
        send_motion(m_focusResource->handle, time,
                    wl_fixed_from_double(m_currentPosition.x()),
                    wl_fixed_from_double(m_currentPosition.y()));

}

void Pointer::button(uint32_t time, Qt::MouseButton button, uint32_t state)
{
    if (m_focusResource) {
        sendButton(time, button, state);
    }

    if (!buttonPressed() && state == WL_POINTER_BUTTON_STATE_RELEASED)
        setFocus(m_currentPosition.view(), m_currentPosition.position());
}

static void requestCursorSurface(QWaylandCompositor *compositor, QWaylandSurface *surface, int32_t hotspot_x, int hotspot_y)
{
#if QT_DEPRECATED_SINCE(5, 5)
    compositor->setCursorSurface(surface, hotspot_x, hotspot_y);
#endif
    compositor->currentCurserSurfaceRequest(surface, hotspot_x, hotspot_y);
}

void Pointer::pointer_set_cursor(wl_pointer::Resource *resource, uint32_t serial, wl_resource *surface, int32_t hotspot_x, int32_t hotspot_y)
{
    Q_UNUSED(resource);
    Q_UNUSED(serial);

    if (!surface) {
        requestCursorSurface(m_compositor->waylandCompositor(), Q_NULLPTR, 0, 0);
        return;
    }

    Surface *s = Surface::fromResource(surface);
    s->setCursorSurface(true);
    requestCursorSurface(m_compositor->waylandCompositor(), s->waylandSurface(), hotspot_x, hotspot_y);
}

PointerGrabber::~PointerGrabber()
{
}

} // namespace QtWayland

QT_END_NAMESPACE