1 | // Copyright (C) 2015 Pier Luigi Fiorini <pierluigi.fiorini@gmail.com> |
2 | // Copyright (C) 2016 The Qt Company Ltd. |
3 | // Copyright (C) 2016 Pelagicore AG |
4 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
5 | |
6 | #include "qeglfskmsgbmcursor_p.h" |
7 | #include "qeglfskmsgbmscreen_p.h" |
8 | #include "qeglfskmsgbmdevice_p.h" |
9 | |
10 | #include <QtCore/QFile> |
11 | #include <QtCore/QJsonDocument> |
12 | #include <QtCore/QJsonObject> |
13 | #include <QtCore/QJsonArray> |
14 | #include <QtCore/QLoggingCategory> |
15 | #include <QtGui/QPainter> |
16 | #include <QtGui/private/qguiapplication_p.h> |
17 | |
18 | #include <xf86drm.h> |
19 | #include <xf86drmMode.h> |
20 | |
21 | #ifndef DRM_CAP_CURSOR_WIDTH |
22 | #define DRM_CAP_CURSOR_WIDTH 0x8 |
23 | #endif |
24 | |
25 | #ifndef DRM_CAP_CURSOR_HEIGHT |
26 | #define DRM_CAP_CURSOR_HEIGHT 0x9 |
27 | #endif |
28 | |
29 | QT_BEGIN_NAMESPACE |
30 | |
31 | using namespace Qt::StringLiterals; |
32 | |
33 | Q_DECLARE_LOGGING_CATEGORY(qLcEglfsKmsDebug) |
34 | |
35 | QEglFSKmsGbmCursor::QEglFSKmsGbmCursor(QEglFSKmsGbmScreen *screen) |
36 | : m_screen(screen) |
37 | , m_cursorSize(64, 64) // 64x64 is the old standard size, we now try to query the real size below |
38 | , m_bo(nullptr) |
39 | , m_cursorImage(nullptr, nullptr, 0, 0, 0, 0) |
40 | , m_state(CursorPendingVisible) |
41 | , m_deviceListener(nullptr) |
42 | { |
43 | QByteArray hideCursorVal = qgetenv(varName: "QT_QPA_EGLFS_HIDECURSOR" ); |
44 | if (!hideCursorVal.isEmpty() && hideCursorVal.toInt()) { |
45 | m_state = CursorDisabled; |
46 | return; |
47 | } |
48 | |
49 | uint64_t width, height; |
50 | if ((drmGetCap(fd: m_screen->device()->fd(), DRM_CAP_CURSOR_WIDTH, value: &width) == 0) |
51 | && (drmGetCap(fd: m_screen->device()->fd(), DRM_CAP_CURSOR_HEIGHT, value: &height) == 0)) { |
52 | m_cursorSize.setWidth(width); |
53 | m_cursorSize.setHeight(height); |
54 | } |
55 | |
56 | m_bo = gbm_bo_create(gbm: static_cast<QEglFSKmsGbmDevice *>(m_screen->device())->gbmDevice(), width: m_cursorSize.width(), height: m_cursorSize.height(), |
57 | GBM_FORMAT_ARGB8888, flags: GBM_BO_USE_CURSOR_64X64 | GBM_BO_USE_WRITE); |
58 | if (!m_bo) { |
59 | qWarning(msg: "Could not create buffer for cursor!" ); |
60 | } else { |
61 | initCursorAtlas(); |
62 | } |
63 | |
64 | m_deviceListener = new QEglFSKmsGbmCursorDeviceListener(this); |
65 | connect(sender: QGuiApplicationPrivate::inputDeviceManager(), signal: &QInputDeviceManager::deviceListChanged, |
66 | context: m_deviceListener, slot: &QEglFSKmsGbmCursorDeviceListener::onDeviceListChanged); |
67 | if (!m_deviceListener->hasMouse()) |
68 | m_state = CursorPendingHidden; |
69 | |
70 | #ifndef QT_NO_CURSOR |
71 | QCursor cursor(Qt::ArrowCursor); |
72 | changeCursor(windowCursor: &cursor, window: nullptr); |
73 | #endif |
74 | setPos(QPoint(0, 0)); |
75 | } |
76 | |
77 | QEglFSKmsGbmCursor::~QEglFSKmsGbmCursor() |
78 | { |
79 | delete m_deviceListener; |
80 | |
81 | for (QPlatformScreen *screen : m_screen->virtualSiblings()) { |
82 | QEglFSKmsScreen *kmsScreen = static_cast<QEglFSKmsScreen *>(screen); |
83 | drmModeSetCursor(fd: kmsScreen->device()->fd(), crtcId: kmsScreen->output().crtc_id, bo_handle: 0, width: 0, height: 0); |
84 | drmModeMoveCursor(fd: kmsScreen->device()->fd(), crtcId: kmsScreen->output().crtc_id, x: 0, y: 0); |
85 | } |
86 | |
87 | if (m_bo) { |
88 | gbm_bo_destroy(bo: m_bo); |
89 | m_bo = nullptr; |
90 | } |
91 | } |
92 | |
93 | void QEglFSKmsGbmCursor::updateMouseStatus() |
94 | { |
95 | const bool wasVisible = m_state == CursorVisible; |
96 | const bool visible = m_deviceListener->hasMouse(); |
97 | if (visible == wasVisible) |
98 | return; |
99 | |
100 | m_state = visible ? CursorPendingVisible : CursorPendingHidden; |
101 | |
102 | #ifndef QT_NO_CURSOR |
103 | changeCursor(windowCursor: nullptr, window: m_screen->topLevelAt(point: pos())); |
104 | #endif |
105 | } |
106 | |
107 | bool QEglFSKmsGbmCursorDeviceListener::hasMouse() const |
108 | { |
109 | return QGuiApplicationPrivate::inputDeviceManager()->deviceCount(type: QInputDeviceManager::DeviceTypePointer) > 0; |
110 | } |
111 | |
112 | void QEglFSKmsGbmCursorDeviceListener::onDeviceListChanged(QInputDeviceManager::DeviceType type) |
113 | { |
114 | if (type == QInputDeviceManager::DeviceTypePointer) |
115 | m_cursor->updateMouseStatus(); |
116 | } |
117 | |
118 | void QEglFSKmsGbmCursor::pointerEvent(const QMouseEvent &event) |
119 | { |
120 | setPos(event.globalPosition().toPoint()); |
121 | } |
122 | |
123 | #ifndef QT_NO_CURSOR |
124 | void QEglFSKmsGbmCursor::changeCursor(QCursor *windowCursor, QWindow *window) |
125 | { |
126 | Q_UNUSED(window); |
127 | |
128 | if (!m_bo) |
129 | return; |
130 | |
131 | if (m_state == CursorPendingHidden) { |
132 | m_state = CursorHidden; |
133 | for (QPlatformScreen *screen : m_screen->virtualSiblings()) { |
134 | QEglFSKmsScreen *kmsScreen = static_cast<QEglFSKmsScreen *>(screen); |
135 | drmModeSetCursor(fd: kmsScreen->device()->fd(), crtcId: kmsScreen->output().crtc_id, bo_handle: 0, width: 0, height: 0); |
136 | } |
137 | } |
138 | |
139 | if (m_state == CursorHidden || m_state == CursorDisabled) |
140 | return; |
141 | |
142 | const Qt::CursorShape newShape = windowCursor ? windowCursor->shape() : Qt::ArrowCursor; |
143 | if (newShape == Qt::BitmapCursor) { |
144 | m_cursorImage.set(image: windowCursor->pixmap().toImage(), |
145 | hx: windowCursor->hotSpot().x(), |
146 | hy: windowCursor->hotSpot().y()); |
147 | } else { |
148 | // Standard cursor, look up in atlas |
149 | const int width = m_cursorAtlas.cursorWidth; |
150 | const int height = m_cursorAtlas.cursorHeight; |
151 | const qreal ws = (qreal)m_cursorAtlas.cursorWidth / m_cursorAtlas.width; |
152 | const qreal hs = (qreal)m_cursorAtlas.cursorHeight / m_cursorAtlas.height; |
153 | |
154 | QRect textureRect(ws * (newShape % m_cursorAtlas.cursorsPerRow) * m_cursorAtlas.width, |
155 | hs * (newShape / m_cursorAtlas.cursorsPerRow) * m_cursorAtlas.height, |
156 | width, |
157 | height); |
158 | QPoint hotSpot = m_cursorAtlas.hotSpots[newShape]; |
159 | m_cursorImage.set(image: m_cursorAtlas.image.copy(rect: textureRect), |
160 | hx: hotSpot.x(), |
161 | hy: hotSpot.y()); |
162 | } |
163 | |
164 | if (m_cursorImage.image()->width() > m_cursorSize.width() || m_cursorImage.image()->height() > m_cursorSize.height()) |
165 | qWarning(msg: "Cursor larger than %dx%d, cursor will be clipped." , m_cursorSize.width(), m_cursorSize.height()); |
166 | |
167 | QImage cursorImage(m_cursorSize, QImage::Format_ARGB32); |
168 | cursorImage.fill(color: Qt::transparent); |
169 | |
170 | QPainter painter; |
171 | painter.begin(&cursorImage); |
172 | painter.drawImage(x: 0, y: 0, image: *m_cursorImage.image()); |
173 | painter.end(); |
174 | |
175 | gbm_bo_write(bo: m_bo, buf: cursorImage.constBits(), count: cursorImage.sizeInBytes()); |
176 | |
177 | uint32_t handle = gbm_bo_get_handle(bo: m_bo).u32; |
178 | |
179 | if (m_state == CursorPendingVisible) |
180 | m_state = CursorVisible; |
181 | |
182 | for (QPlatformScreen *screen : m_screen->virtualSiblings()) { |
183 | QEglFSKmsScreen *kmsScreen = static_cast<QEglFSKmsScreen *>(screen); |
184 | if (kmsScreen->isCursorOutOfRange()) |
185 | continue; |
186 | int status = drmModeSetCursor(fd: kmsScreen->device()->fd(), crtcId: kmsScreen->output().crtc_id, bo_handle: handle, |
187 | width: m_cursorSize.width(), height: m_cursorSize.height()); |
188 | if (status != 0) |
189 | qWarning(msg: "Could not set cursor on screen %s: %d" , kmsScreen->name().toLatin1().constData(), status); |
190 | } |
191 | } |
192 | #endif // QT_NO_CURSOR |
193 | |
194 | QPoint QEglFSKmsGbmCursor::pos() const |
195 | { |
196 | return m_pos; |
197 | } |
198 | |
199 | void QEglFSKmsGbmCursor::setPos(const QPoint &pos) |
200 | { |
201 | for (QPlatformScreen *screen : m_screen->virtualSiblings()) { |
202 | QEglFSKmsScreen *kmsScreen = static_cast<QEglFSKmsScreen *>(screen); |
203 | const QRect screenGeom = kmsScreen->geometry(); |
204 | const QPoint origin = screenGeom.topLeft(); |
205 | const QPoint localPos = pos - origin; |
206 | const QPoint adjustedLocalPos = localPos - m_cursorImage.hotspot(); |
207 | |
208 | if (localPos.x() < 0 || localPos.y() < 0 |
209 | || localPos.x() >= screenGeom.width() || localPos.y() >= screenGeom.height()) |
210 | { |
211 | if (!kmsScreen->isCursorOutOfRange()) { |
212 | kmsScreen->setCursorOutOfRange(true); |
213 | drmModeSetCursor(fd: kmsScreen->device()->fd(), crtcId: kmsScreen->output().crtc_id, bo_handle: 0, width: 0, height: 0); |
214 | } |
215 | } else { |
216 | int ret; |
217 | if (kmsScreen->isCursorOutOfRange() && m_bo) { |
218 | kmsScreen->setCursorOutOfRange(false); |
219 | uint32_t handle = gbm_bo_get_handle(bo: m_bo).u32; |
220 | ret = drmModeSetCursor(fd: kmsScreen->device()->fd(), crtcId: kmsScreen->output().crtc_id, |
221 | bo_handle: handle, width: m_cursorSize.width(), height: m_cursorSize.height()); |
222 | } else { |
223 | ret = drmModeMoveCursor(fd: kmsScreen->device()->fd(), crtcId: kmsScreen->output().crtc_id, |
224 | x: adjustedLocalPos.x(), y: adjustedLocalPos.y()); |
225 | } |
226 | if (ret == 0) |
227 | m_pos = pos; |
228 | else |
229 | qWarning(msg: "Failed to move cursor on screen %s: %d" , kmsScreen->name().toLatin1().constData(), ret); |
230 | |
231 | kmsScreen->handleCursorMove(pos); |
232 | } |
233 | } |
234 | } |
235 | |
236 | void QEglFSKmsGbmCursor::initCursorAtlas() |
237 | { |
238 | static QByteArray json = qgetenv(varName: "QT_QPA_EGLFS_CURSOR" ); |
239 | if (json.isEmpty()) |
240 | json = ":/cursor.json" ; |
241 | |
242 | qCDebug(qLcEglfsKmsDebug) << "Initializing cursor atlas from" << json; |
243 | |
244 | QFile file(QString::fromUtf8(ba: json)); |
245 | if (!file.open(flags: QFile::ReadOnly)) { |
246 | for (QPlatformScreen *screen : m_screen->virtualSiblings()) { |
247 | QEglFSKmsScreen *kmsScreen = static_cast<QEglFSKmsScreen *>(screen); |
248 | drmModeSetCursor(fd: kmsScreen->device()->fd(), crtcId: kmsScreen->output().crtc_id, bo_handle: 0, width: 0, height: 0); |
249 | drmModeMoveCursor(fd: kmsScreen->device()->fd(), crtcId: kmsScreen->output().crtc_id, x: 0, y: 0); |
250 | } |
251 | m_state = CursorDisabled; |
252 | return; |
253 | } |
254 | |
255 | QJsonDocument doc = QJsonDocument::fromJson(json: file.readAll()); |
256 | QJsonObject object = doc.object(); |
257 | |
258 | QString atlas = object.value(key: "image"_L1 ).toString(); |
259 | Q_ASSERT(!atlas.isEmpty()); |
260 | |
261 | const int cursorsPerRow = object.value(key: "cursorsPerRow"_L1 ).toDouble(); |
262 | Q_ASSERT(cursorsPerRow); |
263 | m_cursorAtlas.cursorsPerRow = cursorsPerRow; |
264 | |
265 | const QJsonArray hotSpots = object.value(key: "hotSpots"_L1 ).toArray(); |
266 | Q_ASSERT(hotSpots.count() == Qt::LastCursor + 1); |
267 | for (int i = 0; i < hotSpots.count(); i++) { |
268 | QPoint hotSpot(hotSpots[i].toArray()[0].toDouble(), hotSpots[i].toArray()[1].toDouble()); |
269 | m_cursorAtlas.hotSpots << hotSpot; |
270 | } |
271 | |
272 | QImage image = QImage(atlas).convertToFormat(f: QImage::Format_ARGB32); |
273 | m_cursorAtlas.cursorWidth = image.width() / m_cursorAtlas.cursorsPerRow; |
274 | m_cursorAtlas.cursorHeight = image.height() / ((Qt::LastCursor + cursorsPerRow) / cursorsPerRow); |
275 | m_cursorAtlas.width = image.width(); |
276 | m_cursorAtlas.height = image.height(); |
277 | m_cursorAtlas.image = image; |
278 | } |
279 | |
280 | QT_END_NAMESPACE |
281 | |