| 1 | // Copyright (C) 2022 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 | #pragma once |
| 5 | |
| 6 | #include <qpa/qplatformcursor.h> |
| 7 | #include "qxcbscreen.h" |
| 8 | #include <xcb/xcb_cursor.h> |
| 9 | |
| 10 | #include <QtCore/QCache> |
| 11 | |
| 12 | QT_BEGIN_NAMESPACE |
| 13 | |
| 14 | #ifndef QT_NO_CURSOR |
| 15 | |
| 16 | struct QXcbCursorCacheKey |
| 17 | { |
| 18 | explicit QXcbCursorCacheKey(const QCursor &c); |
| 19 | explicit QXcbCursorCacheKey(Qt::CursorShape s) : shape(s), bitmapCacheKey(0), maskCacheKey(0) {} |
| 20 | QXcbCursorCacheKey() : shape(Qt::CustomCursor), bitmapCacheKey(0), maskCacheKey(0) {} |
| 21 | |
| 22 | Qt::CursorShape shape; |
| 23 | qint64 bitmapCacheKey; |
| 24 | qint64 maskCacheKey; |
| 25 | }; |
| 26 | |
| 27 | inline bool operator==(const QXcbCursorCacheKey &k1, const QXcbCursorCacheKey &k2) |
| 28 | { |
| 29 | return k1.shape == k2.shape && k1.bitmapCacheKey == k2.bitmapCacheKey && k1.maskCacheKey == k2.maskCacheKey; |
| 30 | } |
| 31 | |
| 32 | inline size_t qHash(const QXcbCursorCacheKey &k, size_t seed) noexcept |
| 33 | { |
| 34 | return (size_t(k.shape) + size_t(k.bitmapCacheKey) + size_t(k.maskCacheKey)) ^ seed; |
| 35 | } |
| 36 | |
| 37 | #endif // !QT_NO_CURSOR |
| 38 | |
| 39 | class QXcbCursor : public QXcbObject, public QPlatformCursor |
| 40 | { |
| 41 | public: |
| 42 | QXcbCursor(QXcbConnection *conn, QXcbScreen *screen); |
| 43 | ~QXcbCursor(); |
| 44 | #ifndef QT_NO_CURSOR |
| 45 | void changeCursor(QCursor *cursor, QWindow *window) override; |
| 46 | #endif |
| 47 | QPoint pos() const override; |
| 48 | void setPos(const QPoint &pos) override; |
| 49 | |
| 50 | QSize size() const override; |
| 51 | |
| 52 | void updateContext(); |
| 53 | |
| 54 | static void queryPointer(QXcbConnection *c, QXcbVirtualDesktop **virtualDesktop, QPoint *pos, int *keybMask = nullptr); |
| 55 | |
| 56 | #ifndef QT_NO_CURSOR |
| 57 | xcb_cursor_t xcbCursor(const QCursor &c) const |
| 58 | { return m_cursorHash.value(key: QXcbCursorCacheKey(c), defaultValue: xcb_cursor_t(0)); } |
| 59 | #endif |
| 60 | |
| 61 | private: |
| 62 | |
| 63 | #ifndef QT_NO_CURSOR |
| 64 | typedef QHash<QXcbCursorCacheKey, xcb_cursor_t> CursorHash; |
| 65 | |
| 66 | struct CachedCursor |
| 67 | { |
| 68 | explicit CachedCursor(xcb_connection_t *conn, xcb_cursor_t c) |
| 69 | : cursor(c), connection(conn) {} |
| 70 | ~CachedCursor() { xcb_free_cursor(c: connection, cursor); } |
| 71 | xcb_cursor_t cursor; |
| 72 | xcb_connection_t *connection; |
| 73 | }; |
| 74 | typedef QCache<QXcbCursorCacheKey, CachedCursor> BitmapCursorCache; |
| 75 | |
| 76 | xcb_cursor_t createFontCursor(int cshape); |
| 77 | xcb_cursor_t createBitmapCursor(QCursor *cursor); |
| 78 | xcb_cursor_t createNonStandardCursor(int cshape); |
| 79 | #endif |
| 80 | |
| 81 | QXcbScreen *m_screen; |
| 82 | xcb_cursor_context_t *m_cursorContext; |
| 83 | #ifndef QT_NO_CURSOR |
| 84 | CursorHash m_cursorHash; |
| 85 | BitmapCursorCache m_bitmapCache; |
| 86 | #endif |
| 87 | static void cursorThemePropertyChanged(QXcbVirtualDesktop *screen, |
| 88 | const QByteArray &name, |
| 89 | const QVariant &property, |
| 90 | void *handle); |
| 91 | bool m_callbackForPropertyRegistered; |
| 92 | }; |
| 93 | |
| 94 | QT_END_NAMESPACE |
| 95 | |