Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
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// Qt-Security score:significant reason:default
5
7
11#include "qwayland-pointer-warp-v1.h"
12
13#include <QtGui/private/qguiapplication_p.h>
14#include <qpa/qplatformtheme.h>
15
16#include <QtGui/QImageReader>
17#include <QDebug>
18
19#include <wayland-cursor.h>
20
21#include <algorithm>
22
23QT_BEGIN_NAMESPACE
24
25namespace QtWaylandClient {
26
28{
31
32 if (!theme) {
33 qCWarning(lcQpaWayland) << "Could not load cursor theme" << themeName << "size" << size;
34 return nullptr;
35 }
36
38}
39
41{
43}
44
46{
47 if (struct wl_cursor *cursor = m_cursors[shape])
48 return cursor;
49
50 static Q_CONSTEXPR struct ShapeAndName {
52 const char name[33];
53 } cursorNamesMap[] = {
54 {ArrowCursor, "left_ptr"},
55 {ArrowCursor, "default"},
56 {ArrowCursor, "top_left_arrow"},
57 {ArrowCursor, "left_arrow"},
58
59 {UpArrowCursor, "up_arrow"},
60
61 {CrossCursor, "cross"},
62
63 {WaitCursor, "wait"},
64 {WaitCursor, "watch"},
65 {WaitCursor, "0426c94ea35c87780ff01dc239897213"},
66
67 {IBeamCursor, "ibeam"},
68 {IBeamCursor, "text"},
69 {IBeamCursor, "xterm"},
70
71 {SizeVerCursor, "size_ver"},
72 {SizeVerCursor, "ns-resize"},
73 {SizeVerCursor, "v_double_arrow"},
74 {SizeVerCursor, "00008160000006810000408080010102"},
75
76 {SizeHorCursor, "size_hor"},
77 {SizeHorCursor, "ew-resize"},
78 {SizeHorCursor, "h_double_arrow"},
79 {SizeHorCursor, "028006030e0e7ebffc7f7070c0600140"},
80
81 {SizeBDiagCursor, "size_bdiag"},
82 {SizeBDiagCursor, "nesw-resize"},
83 {SizeBDiagCursor, "50585d75b494802d0151028115016902"},
84 {SizeBDiagCursor, "fcf1c3c7cd4491d801f1e1c78f100000"},
85
86 {SizeFDiagCursor, "size_fdiag"},
87 {SizeFDiagCursor, "nwse-resize"},
88 {SizeFDiagCursor, "38c5dff7c7b8962045400281044508d2"},
89 {SizeFDiagCursor, "c7088f0f3e6c8088236ef8e1e3e70000"},
90
91 {SizeAllCursor, "size_all"},
92
93 {BlankCursor, "blank"},
94
95 {SplitVCursor, "split_v"},
96 {SplitVCursor, "row-resize"},
97 {SplitVCursor, "sb_v_double_arrow"},
98 {SplitVCursor, "2870a09082c103050810ffdffffe0204"},
99 {SplitVCursor, "c07385c7190e701020ff7ffffd08103c"},
100
101 {SplitHCursor, "split_h"},
102 {SplitHCursor, "col-resize"},
103 {SplitHCursor, "sb_h_double_arrow"},
104 {SplitHCursor, "043a9f68147c53184671403ffa811cc5"},
105 {SplitHCursor, "14fef782d02440884392942c11205230"},
106
107 {PointingHandCursor, "pointing_hand"},
108 {PointingHandCursor, "pointer"},
109 {PointingHandCursor, "hand1"},
110 {PointingHandCursor, "e29285e634086352946a0e7090d73106"},
111
112 {ForbiddenCursor, "forbidden"},
113 {ForbiddenCursor, "not-allowed"},
114 {ForbiddenCursor, "crossed_circle"},
115 {ForbiddenCursor, "circle"},
116 {ForbiddenCursor, "03b6e0fcb3499374a867c041f52298f0"},
117
118 {WhatsThisCursor, "whats_this"},
119 {WhatsThisCursor, "help"},
120 {WhatsThisCursor, "question_arrow"},
121 {WhatsThisCursor, "5c6cd98b3f3ebcb1f9c7f1c204630408"},
122 {WhatsThisCursor, "d9ce0ab605698f320427677b458ad60b"},
123
124 {BusyCursor, "left_ptr_watch"},
125 {BusyCursor, "half-busy"},
126 {BusyCursor, "progress"},
127 {BusyCursor, "00000000000000020006000e7e9ffc3f"},
128 {BusyCursor, "08e8e1c95fe2fc01f976f1e063a24ccd"},
129
130 {OpenHandCursor, "openhand"},
131 {OpenHandCursor, "fleur"},
132 {OpenHandCursor, "5aca4d189052212118709018842178c0"},
133 {OpenHandCursor, "9d800788f1b08800ae810202380a0822"},
134
135 {ClosedHandCursor, "closedhand"},
136 {ClosedHandCursor, "grabbing"},
137 {ClosedHandCursor, "208530c400c041818281048008011002"},
138
139 {DragCopyCursor, "dnd-copy"},
140 {DragCopyCursor, "copy"},
141
142 {DragMoveCursor, "dnd-move"},
143 {DragMoveCursor, "move"},
144
145 {DragLinkCursor, "dnd-link"},
146 {DragLinkCursor, "link"},
147
148 {ResizeNorthCursor, "n-resize"},
149 {ResizeNorthCursor, "top_side"},
150
151 {ResizeSouthCursor, "s-resize"},
152 {ResizeSouthCursor, "bottom_side"},
153
154 {ResizeEastCursor, "e-resize"},
155 {ResizeEastCursor, "right_side"},
156
157 {ResizeWestCursor, "w-resize"},
158 {ResizeWestCursor, "left_side"},
159
160 {ResizeNorthWestCursor, "nw-resize"},
161 {ResizeNorthWestCursor, "top_left_corner"},
162
163 {ResizeSouthEastCursor, "se-resize"},
164 {ResizeSouthEastCursor, "bottom_right_corner"},
165
166 {ResizeNorthEastCursor, "ne-resize"},
167 {ResizeNorthEastCursor, "top_right_corner"},
168
169 {ResizeSouthWestCursor, "sw-resize"},
170 {ResizeSouthWestCursor, "bottom_left_corner"},
171 };
172
173 const auto byShape = [](ShapeAndName lhs, ShapeAndName rhs) {
174 return lhs.shape < rhs.shape;
175 };
179 for (auto it = p.first; it != p.second; ++it) {
182 return cursor;
183 }
184 }
185
186 // Fallback to arrow cursor
187 if (shape != ArrowCursor)
189
190 // Give up
191 return nullptr;
192}
193
195{
196 struct wl_cursor *waylandCursor = nullptr;
197
198 if (shape < Qt::BitmapCursor) {
200 } else if (shape == Qt::BitmapCursor) {
201 qCWarning(lcQpaWayland) << "cannot create a wl_cursor_image for a CursorShape";
202 return nullptr;
203 } else {
204 //TODO: Custom cursor logic (for resize arrows)
205 }
206
207 if (!waylandCursor) {
208 qCWarning(lcQpaWayland) << "Could not find cursor for shape" << shape;
209 return nullptr;
210 }
211
212 return waylandCursor;
213}
214
217{}
218
220{
221 destroy();
222}
223
225{
226 using QtWayland::wp_cursor_shape_device_v1;
227
228 switch (cursorShape) {
229 case Qt::BlankCursor:
230 case Qt::CustomCursor:
231 case Qt::BitmapCursor:
232 // these should have been handled separately before using the shape protocol
233 Q_ASSERT(false);
234 break;
235 case Qt::ArrowCursor:
236 return wp_cursor_shape_device_v1::shape_default;
237 case Qt::SizeVerCursor:
238 return wp_cursor_shape_device_v1::shape_ns_resize;
239 case Qt::UpArrowCursor:
240 return wp_cursor_shape_device_v1::shape_n_resize;
241 case Qt::SizeHorCursor:
242 return wp_cursor_shape_device_v1::shape_ew_resize;
243 case Qt::CrossCursor:
244 return wp_cursor_shape_device_v1::shape_crosshair;
245 case Qt::SizeBDiagCursor:
246 return wp_cursor_shape_device_v1::shape_nesw_resize;
247 case Qt::IBeamCursor:
248 return wp_cursor_shape_device_v1::shape_text;
249 case Qt::SizeFDiagCursor:
250 return wp_cursor_shape_device_v1::shape_nwse_resize;
251 case Qt::WaitCursor:
252 return wp_cursor_shape_device_v1::shape_wait;
253 case Qt::SizeAllCursor:
254 return wp_cursor_shape_device_v1::shape_all_scroll;
255 case Qt::BusyCursor:
256 return wp_cursor_shape_device_v1::shape_progress;
257 case Qt::SplitVCursor:
258 return wp_cursor_shape_device_v1::shape_row_resize;
259 case Qt::ForbiddenCursor:
260 return wp_cursor_shape_device_v1::shape_not_allowed;
261 case Qt::SplitHCursor:
262 return wp_cursor_shape_device_v1::shape_col_resize;
263 case Qt::PointingHandCursor:
264 return wp_cursor_shape_device_v1::shape_pointer;
265 case Qt::OpenHandCursor:
266 return wp_cursor_shape_device_v1::shape_grab;
267 case Qt::WhatsThisCursor:
268 return wp_cursor_shape_device_v1::shape_help;
269 case Qt::ClosedHandCursor:
270 return wp_cursor_shape_device_v1::shape_grabbing;
271 case Qt::DragMoveCursor:
272 return wp_cursor_shape_device_v1::shape_move;
273 case Qt::DragCopyCursor:
274 return wp_cursor_shape_device_v1::shape_copy;
275 case Qt::DragLinkCursor:
276 return wp_cursor_shape_device_v1::shape_alias;
277 }
278 return wp_cursor_shape_device_v1::shape_default;
279}
280
282{
284}
285
288{
289}
290
292{
295
296 // convert to supported format if necessary
297 if (!display->shm()->formatSupported(img.format())) {
298 if (cursor->mask().isNull()) {
300 } else {
301 // preserve mask
305 img = pixmap.toImage();
306 }
307 }
308
311 return buffer;
312}
313
315{
316 if (!window)
317 return;
318 // Create the buffer here so we don't have to create one per input device
320 if (cursor && cursor->shape() == Qt::BitmapCursor)
322
324 if (!waylandWindow)
325 return;
326
327 if (cursor)
329 else
331
335 }
336
338}
339
341{
343}
344
346{
347 return mLastPos;
348}
349
350void QWaylandCursor::setPos(const QPoint &pos)
351{
352 if (mDisplay->pointerWarp()) {
353 const auto seats = mDisplay->inputDevices();
354 for (auto *seat : seats) {
355 if (!seat->pointer() || !seat->pointer()->focusWindow()) {
356 continue;
357 }
358 const auto focus = seat->pointer()->focusWindow();
360 continue;
361 }
363 return;
364 }
365 } else {
366 qCWarning(lcQpaWayland) << "Setting cursor position requires pointer warp v1 protocol support";
367 }
368}
369
371{
372 mLastPos = pos;
373}
374
376{
379 return QSize(24, 24);
380}
381
382} // namespace QtWaylandClient
383
384QT_END_NAMESPACE
static QtWayland::wp_cursor_shape_device_v1::shape qtCursorShapeToWaylandShape(Qt::CursorShape cursorShape)