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