Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qwaylandcursor.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// Copyright (C) 2023 David Edmundson <davidedmundson@kde.org>
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
4
5#include "qwaylandcursor_p.h"
6
7#include "qwaylanddisplay_p.h"
10
11#include <QtGui/private/qguiapplication_p.h>
12#include <qpa/qplatformtheme.h>
13
14#include <QtGui/QImageReader>
15#include <QDebug>
16
17#include <wayland-cursor.h>
18
19#include <algorithm>
20
22
23namespace QtWaylandClient {
24
25std::unique_ptr<QWaylandCursorTheme> QWaylandCursorTheme::create(QWaylandShm *shm, int size, const QString &themeName)
26{
27 QByteArray nameBytes = themeName.toLocal8Bit();
28 struct ::wl_cursor_theme *theme = wl_cursor_theme_load(nameBytes.constData(), size, shm->object());
29
30 if (!theme) {
31 qCWarning(lcQpaWayland) << "Could not load cursor theme" << themeName << "size" << size;
32 return nullptr;
33 }
34
35 return std::unique_ptr<QWaylandCursorTheme>{new QWaylandCursorTheme(theme)};
36}
37
38QWaylandCursorTheme::~QWaylandCursorTheme()
39{
40 wl_cursor_theme_destroy(m_theme);
41}
42
43wl_cursor *QWaylandCursorTheme::requestCursor(WaylandCursor shape)
44{
45 if (struct wl_cursor *cursor = m_cursors[shape])
46 return cursor;
47
48 static Q_CONSTEXPR struct ShapeAndName {
49 WaylandCursor shape;
50 const char name[33];
51 } cursorNamesMap[] = {
52 {ArrowCursor, "left_ptr"},
53 {ArrowCursor, "default"},
54 {ArrowCursor, "top_left_arrow"},
55 {ArrowCursor, "left_arrow"},
56
57 {UpArrowCursor, "up_arrow"},
58
59 {CrossCursor, "cross"},
60
61 {WaitCursor, "wait"},
62 {WaitCursor, "watch"},
63 {WaitCursor, "0426c94ea35c87780ff01dc239897213"},
64
65 {IBeamCursor, "ibeam"},
66 {IBeamCursor, "text"},
67 {IBeamCursor, "xterm"},
68
69 {SizeVerCursor, "size_ver"},
70 {SizeVerCursor, "ns-resize"},
71 {SizeVerCursor, "v_double_arrow"},
72 {SizeVerCursor, "00008160000006810000408080010102"},
73
74 {SizeHorCursor, "size_hor"},
75 {SizeHorCursor, "ew-resize"},
76 {SizeHorCursor, "h_double_arrow"},
77 {SizeHorCursor, "028006030e0e7ebffc7f7070c0600140"},
78
79 {SizeBDiagCursor, "size_bdiag"},
80 {SizeBDiagCursor, "nesw-resize"},
81 {SizeBDiagCursor, "50585d75b494802d0151028115016902"},
82 {SizeBDiagCursor, "fcf1c3c7cd4491d801f1e1c78f100000"},
83
84 {SizeFDiagCursor, "size_fdiag"},
85 {SizeFDiagCursor, "nwse-resize"},
86 {SizeFDiagCursor, "38c5dff7c7b8962045400281044508d2"},
87 {SizeFDiagCursor, "c7088f0f3e6c8088236ef8e1e3e70000"},
88
89 {SizeAllCursor, "size_all"},
90
91 {BlankCursor, "blank"},
92
93 {SplitVCursor, "split_v"},
94 {SplitVCursor, "row-resize"},
95 {SplitVCursor, "sb_v_double_arrow"},
96 {SplitVCursor, "2870a09082c103050810ffdffffe0204"},
97 {SplitVCursor, "c07385c7190e701020ff7ffffd08103c"},
98
99 {SplitHCursor, "split_h"},
100 {SplitHCursor, "col-resize"},
101 {SplitHCursor, "sb_h_double_arrow"},
102 {SplitHCursor, "043a9f68147c53184671403ffa811cc5"},
103 {SplitHCursor, "14fef782d02440884392942c11205230"},
104
105 {PointingHandCursor, "pointing_hand"},
106 {PointingHandCursor, "pointer"},
107 {PointingHandCursor, "hand1"},
108 {PointingHandCursor, "e29285e634086352946a0e7090d73106"},
109
110 {ForbiddenCursor, "forbidden"},
111 {ForbiddenCursor, "not-allowed"},
112 {ForbiddenCursor, "crossed_circle"},
113 {ForbiddenCursor, "circle"},
114 {ForbiddenCursor, "03b6e0fcb3499374a867c041f52298f0"},
115
116 {WhatsThisCursor, "whats_this"},
117 {WhatsThisCursor, "help"},
118 {WhatsThisCursor, "question_arrow"},
119 {WhatsThisCursor, "5c6cd98b3f3ebcb1f9c7f1c204630408"},
120 {WhatsThisCursor, "d9ce0ab605698f320427677b458ad60b"},
121
122 {BusyCursor, "left_ptr_watch"},
123 {BusyCursor, "half-busy"},
124 {BusyCursor, "progress"},
125 {BusyCursor, "00000000000000020006000e7e9ffc3f"},
126 {BusyCursor, "08e8e1c95fe2fc01f976f1e063a24ccd"},
127
128 {OpenHandCursor, "openhand"},
129 {OpenHandCursor, "fleur"},
130 {OpenHandCursor, "5aca4d189052212118709018842178c0"},
131 {OpenHandCursor, "9d800788f1b08800ae810202380a0822"},
132
133 {ClosedHandCursor, "closedhand"},
134 {ClosedHandCursor, "grabbing"},
135 {ClosedHandCursor, "208530c400c041818281048008011002"},
136
137 {DragCopyCursor, "dnd-copy"},
138 {DragCopyCursor, "copy"},
139
140 {DragMoveCursor, "dnd-move"},
141 {DragMoveCursor, "move"},
142
143 {DragLinkCursor, "dnd-link"},
144 {DragLinkCursor, "link"},
145
146 {ResizeNorthCursor, "n-resize"},
147 {ResizeNorthCursor, "top_side"},
148
149 {ResizeSouthCursor, "s-resize"},
150 {ResizeSouthCursor, "bottom_side"},
151
152 {ResizeEastCursor, "e-resize"},
153 {ResizeEastCursor, "right_side"},
154
155 {ResizeWestCursor, "w-resize"},
156 {ResizeWestCursor, "left_side"},
157
158 {ResizeNorthWestCursor, "nw-resize"},
159 {ResizeNorthWestCursor, "top_left_corner"},
160
161 {ResizeSouthEastCursor, "se-resize"},
162 {ResizeSouthEastCursor, "bottom_right_corner"},
163
164 {ResizeNorthEastCursor, "ne-resize"},
165 {ResizeNorthEastCursor, "top_right_corner"},
166
167 {ResizeSouthWestCursor, "sw-resize"},
168 {ResizeSouthWestCursor, "bottom_left_corner"},
169 };
170
171 const auto byShape = [](ShapeAndName lhs, ShapeAndName rhs) {
172 return lhs.shape < rhs.shape;
173 };
174 Q_ASSERT(std::is_sorted(std::begin(cursorNamesMap), std::end(cursorNamesMap), byShape));
175 const auto p = std::equal_range(std::begin(cursorNamesMap), std::end(cursorNamesMap),
176 ShapeAndName{shape, ""}, byShape);
177 for (auto it = p.first; it != p.second; ++it) {
178 if (wl_cursor *cursor = wl_cursor_theme_get_cursor(m_theme, it->name)) {
179 m_cursors[shape] = cursor;
180 return cursor;
181 }
182 }
183
184 // Fallback to arrow cursor
185 if (shape != ArrowCursor)
186 return requestCursor(ArrowCursor);
187
188 // Give up
189 return nullptr;
190}
191
192::wl_cursor *QWaylandCursorTheme::cursor(Qt::CursorShape shape)
193{
194 struct wl_cursor *waylandCursor = nullptr;
195
196 if (shape < Qt::BitmapCursor) {
197 waylandCursor = requestCursor(WaylandCursor(shape));
198 } else if (shape == Qt::BitmapCursor) {
199 qCWarning(lcQpaWayland) << "cannot create a wl_cursor_image for a CursorShape";
200 return nullptr;
201 } else {
202 //TODO: Custom cursor logic (for resize arrows)
203 }
204
205 if (!waylandCursor) {
206 qCWarning(lcQpaWayland) << "Could not find cursor for shape" << shape;
207 return nullptr;
208 }
209
210 return waylandCursor;
211}
212
213QWaylandCursorShape::QWaylandCursorShape(::wp_cursor_shape_device_v1 *object)
214 : QtWayland::wp_cursor_shape_device_v1(object)
215{}
216
217QWaylandCursorShape::~QWaylandCursorShape()
218{
219 destroy();
220}
221
222static QtWayland::wp_cursor_shape_device_v1::shape qtCursorShapeToWaylandShape(Qt::CursorShape cursorShape)
223{
224 using QtWayland::wp_cursor_shape_device_v1;
225
226 switch (cursorShape) {
227 case Qt::BlankCursor:
228 case Qt::CustomCursor:
229 case Qt::BitmapCursor:
230 // these should have been handled separately before using the shape protocol
231 Q_ASSERT(false);
232 break;
233 case Qt::ArrowCursor:
234 return wp_cursor_shape_device_v1::shape_default;
236 return wp_cursor_shape_device_v1::shape_ns_resize;
238 return wp_cursor_shape_device_v1::shape_n_resize;
240 return wp_cursor_shape_device_v1::shape_ew_resize;
241 case Qt::CrossCursor:
242 return wp_cursor_shape_device_v1::shape_crosshair;
244 return wp_cursor_shape_device_v1::shape_nesw_resize;
245 case Qt::IBeamCursor:
246 return wp_cursor_shape_device_v1::shape_text;
248 return wp_cursor_shape_device_v1::shape_nwse_resize;
249 case Qt::WaitCursor:
250 return wp_cursor_shape_device_v1::shape_wait;
252 return wp_cursor_shape_device_v1::shape_all_scroll;
253 case Qt::BusyCursor:
254 return wp_cursor_shape_device_v1::shape_progress;
255 case Qt::SplitVCursor:
256 return wp_cursor_shape_device_v1::shape_row_resize;
258 return wp_cursor_shape_device_v1::shape_not_allowed;
259 case Qt::SplitHCursor:
260 return wp_cursor_shape_device_v1::shape_col_resize;
262 return wp_cursor_shape_device_v1::shape_pointer;
264 return wp_cursor_shape_device_v1::shape_grab;
266 return wp_cursor_shape_device_v1::shape_help;
268 return wp_cursor_shape_device_v1::shape_grabbing;
270 return wp_cursor_shape_device_v1::shape_move;
272 return wp_cursor_shape_device_v1::shape_copy;
274 return wp_cursor_shape_device_v1::shape_alias;
275 }
276 return wp_cursor_shape_device_v1::shape_default;
277}
278
279void QWaylandCursorShape::setShape(uint32_t serial, Qt::CursorShape shape)
280{
281 set_shape(serial, qtCursorShapeToWaylandShape(shape));
282}
283
284QWaylandCursor::QWaylandCursor(QWaylandDisplay *display)
285 : mDisplay(display)
286{
287}
288
289QSharedPointer<QWaylandBuffer> QWaylandCursor::cursorBitmapBuffer(QWaylandDisplay *display, const QCursor *cursor)
290{
293
294 // convert to supported format if necessary
295 if (!display->shm()->formatSupported(img.format())) {
296 if (cursor->mask().isNull()) {
298 } else {
299 // preserve mask
300 img.convertTo(QImage::Format_ARGB32);
302 pixmap.setMask(cursor->mask());
303 img = pixmap.toImage();
304 }
305 }
306
307 QSharedPointer<QWaylandShmBuffer> buffer(new QWaylandShmBuffer(display, img.size(), img.format()));
308 memcpy(buffer->image()->bits(), img.bits(), size_t(img.sizeInBytes()));
309 return buffer;
310}
311
312void QWaylandCursor::changeCursor(QCursor *cursor, QWindow *window)
313{
315 // Create the buffer here so we don't have to create one per input device
316 QSharedPointer<QWaylandBuffer> bitmapBuffer;
317 if (cursor && cursor->shape() == Qt::BitmapCursor)
318 bitmapBuffer = cursorBitmapBuffer(mDisplay, cursor);
319
320 int fallbackOutputScale = int(window->devicePixelRatio());
321 const auto seats = mDisplay->inputDevices();
322 for (auto *seat : seats)
323 seat->setCursor(cursor, bitmapBuffer, fallbackOutputScale);
324}
325
326void QWaylandCursor::pointerEvent(const QMouseEvent &event)
327{
328 mLastPos = event.globalPosition().toPoint();
329}
330
331QPoint QWaylandCursor::pos() const
332{
333 return mLastPos;
334}
335
336void QWaylandCursor::setPos(const QPoint &pos)
337{
338 Q_UNUSED(pos);
339 qCWarning(lcQpaWayland) << "Setting cursor position is not possible on wayland";
340}
341
342QSize QWaylandCursor::size() const
343{
345 return theme->themeHint(QPlatformTheme::MouseCursorSize).toSize();
346 return QSize(24, 24);
347}
348
349} // namespace QtWaylandClient
350
\inmodule QtCore
Definition qbytearray.h:57
The QCursor class provides a mouse cursor with an arbitrary shape.
Definition qcursor.h:45
QBitmap bitmap() const
Returns the cursor bitmap, or a null bitmap if it is one of the standard cursors.
Definition qcursor.cpp:545
QPixmap pixmap() const
Returns the cursor pixmap.
Definition qcursor.cpp:584
Qt::CursorShape shape() const
Returns the cursor shape identifier.
Definition qcursor.cpp:498
QBitmap mask() const
Returns the cursor bitmap mask, or a null bitmap if it is one of the standard cursors.
Definition qcursor.cpp:571
static QPlatformTheme * platformTheme()
\inmodule QtGui
Definition qimage.h:37
void convertTo(Format f, Qt::ImageConversionFlags flags=Qt::AutoColor)
Definition qimage.cpp:2399
@ Format_RGB32
Definition qimage.h:46
@ Format_ARGB32
Definition qimage.h:47
\inmodule QtGui
Definition qevent.h:196
Returns a copy of the pixmap that is transformed using the given transformation transform and transfo...
Definition qpixmap.h:27
QImage toImage() const
Converts the pixmap to a QImage.
Definition qpixmap.cpp:408
bool isNull() const
Returns true if this is a null pixmap; otherwise returns false.
Definition qpixmap.cpp:456
static QPixmap fromImage(const QImage &image, Qt::ImageConversionFlags flags=Qt::AutoColor)
Converts the given image to a pixmap using the specified flags to control the conversion.
Definition qpixmap.cpp:1437
The QPlatformTheme class allows customizing the UI based on themes.
\inmodule QtCore\reentrant
Definition qpoint.h:25
\inmodule QtCore
Definition qsize.h:25
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
\inmodule QtGui
Definition qwindow.h:63
QCursor cursor
QSet< QString >::iterator it
struct wl_display * display
Definition linuxdmabuf.h:41
Combined button and popup list for selecting options.
static QtWayland::wp_cursor_shape_device_v1::shape qtCursorShapeToWaylandShape(Qt::CursorShape cursorShape)
CursorShape
@ BlankCursor
@ CrossCursor
@ DragCopyCursor
@ BitmapCursor
@ PointingHandCursor
@ SizeHorCursor
@ SizeAllCursor
@ CustomCursor
@ WaitCursor
@ SizeVerCursor
@ DragLinkCursor
@ OpenHandCursor
@ SizeFDiagCursor
@ WhatsThisCursor
@ ArrowCursor
@ SplitVCursor
@ UpArrowCursor
@ ClosedHandCursor
@ DragMoveCursor
@ IBeamCursor
@ SizeBDiagCursor
@ ForbiddenCursor
@ BusyCursor
@ SplitHCursor
#define Q_CONSTEXPR
#define qCWarning(category,...)
static QString themeName()
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLuint object
[3]
GLenum GLuint buffer
GLuint name
struct _cl_event * event
GLint void * img
Definition qopenglext.h:233
GLfloat GLfloat p
[1]
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
#define Q_UNUSED(x)
item setCursor(Qt::IBeamCursor)
[1]
widget render & pixmap
aWidget window() -> setWindowTitle("New Window Title")
[2]