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

source code of qtbase/src/plugins/platforms/eglfs/deviceintegration/eglfs_kms/qeglfskmsgbmcursor.cpp