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