| 1 | // Copyright (C) 2019 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #ifndef QWAYLANDCURSORSURFACE_H |
| 5 | #define QWAYLANDCURSORSURFACE_H |
| 6 | |
| 7 | #include "qwaylandsurface_p.h" |
| 8 | #include "qwaylandcallback_p.h" |
| 9 | |
| 10 | QT_BEGIN_NAMESPACE |
| 11 | |
| 12 | namespace QtWaylandClient { |
| 13 | |
| 14 | #if QT_CONFIG(cursor) |
| 15 | template <typename InputDevice> |
| 16 | class CursorSurface : public QWaylandSurface |
| 17 | { |
| 18 | public: |
| 19 | explicit CursorSurface(InputDevice *pointer, QWaylandDisplay *display) |
| 20 | : QWaylandSurface(display), m_pointer(pointer) |
| 21 | { |
| 22 | connect(this, &QWaylandSurface::screensChanged, m_pointer, &InputDevice::updateCursor); |
| 23 | } |
| 24 | |
| 25 | void reset() |
| 26 | { |
| 27 | m_setSerial = 0; |
| 28 | m_hotspot = QPoint(); |
| 29 | } |
| 30 | |
| 31 | // Size and hotspot are in surface coordinates |
| 32 | void update(wl_buffer *buffer, const QPoint &hotspot, const QSize &size, int bufferScale, |
| 33 | bool animated = false) |
| 34 | { |
| 35 | // Calling code needs to ensure buffer scale is supported if != 1 |
| 36 | Q_ASSERT(bufferScale == 1 || version() >= 3); |
| 37 | |
| 38 | auto enterSerial = m_pointer->mEnterSerial; |
| 39 | if (m_setSerial < enterSerial || m_hotspot != hotspot) { |
| 40 | m_pointer->set_cursor(m_pointer->mEnterSerial, object(), hotspot.x(), hotspot.y()); |
| 41 | m_setSerial = enterSerial; |
| 42 | m_hotspot = hotspot; |
| 43 | } |
| 44 | |
| 45 | if (version() >= 3) |
| 46 | set_buffer_scale(bufferScale); |
| 47 | |
| 48 | attach(buffer, 0, 0); |
| 49 | damage(0, 0, size.width(), size.height()); |
| 50 | m_frameCallback.reset(); |
| 51 | if (animated) { |
| 52 | m_frameCallback.reset(other: new WlCallback(frame(), [this](uint32_t time) { |
| 53 | Q_UNUSED(time); |
| 54 | m_pointer->cursorFrameCallback(); |
| 55 | })); |
| 56 | } |
| 57 | commit(); |
| 58 | } |
| 59 | |
| 60 | int outputScale() const |
| 61 | { |
| 62 | int scale = 0; |
| 63 | for (auto *screen : m_screens) |
| 64 | scale = qMax(scale, screen->scale()); |
| 65 | return scale; |
| 66 | } |
| 67 | |
| 68 | private: |
| 69 | QScopedPointer<WlCallback> m_frameCallback; |
| 70 | InputDevice *m_pointer = nullptr; |
| 71 | uint m_setSerial = 0; |
| 72 | QPoint m_hotspot; |
| 73 | }; |
| 74 | |
| 75 | #endif // QT_CONFIG(cursor) |
| 76 | |
| 77 | } // namespace QtWaylandClient |
| 78 | |
| 79 | QT_END_NAMESPACE |
| 80 | |
| 81 | #endif // QWAYLANDCURSORSURFACE_H |
| 82 | |