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(0, 0, 0, 0, 0, 0)
73 , m_state(CursorPendingVisible)
74 , m_deviceListener(nullptr)
75{
76 QByteArray hideCursorVal = qgetenv("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(m_screen->device()->fd(), DRM_CAP_CURSOR_WIDTH, &width) == 0)
84 && (drmGetCap(m_screen->device()->fd(), DRM_CAP_CURSOR_HEIGHT, &height) == 0)) {
85 m_cursorSize.setWidth(width);
86 m_cursorSize.setHeight(height);
87 }
88
89 m_bo = gbm_bo_create(static_cast<QEglFSKmsGbmDevice *>(m_screen->device())->gbmDevice(), m_cursorSize.width(), m_cursorSize.height(),
90 GBM_FORMAT_ARGB8888, GBM_BO_USE_CURSOR_64X64 | GBM_BO_USE_WRITE);
91 if (!m_bo) {
92 qWarning("Could not create buffer for cursor!");
93 } else {
94 initCursorAtlas();
95 }
96
97 m_deviceListener = new QEglFSKmsGbmCursorDeviceListener(this);
98 connect(QGuiApplicationPrivate::inputDeviceManager(), &QInputDeviceManager::deviceListChanged,
99 m_deviceListener, &QEglFSKmsGbmCursorDeviceListener::onDeviceListChanged);
100 if (!m_deviceListener->hasMouse())
101 m_state = CursorPendingHidden;
102
103#ifndef QT_NO_CURSOR
104 QCursor cursor(Qt::ArrowCursor);
105 changeCursor(&cursor, 0);
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(kmsScreen->device()->fd(), kmsScreen->output().crtc_id, 0, 0, 0);
117 drmModeMoveCursor(kmsScreen->device()->fd(), kmsScreen->output().crtc_id, 0, 0);
118 }
119
120 if (m_bo) {
121 gbm_bo_destroy(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(nullptr, m_screen->topLevelAt(pos()));
137#endif
138}
139
140bool QEglFSKmsGbmCursorDeviceListener::hasMouse() const
141{
142 return QGuiApplicationPrivate::inputDeviceManager()->deviceCount(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(kmsScreen->device()->fd(), kmsScreen->output().crtc_id, 0, 0, 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(windowCursor->pixmap().toImage(),
178 windowCursor->hotSpot().x(),
179 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(m_cursorAtlas.image.copy(textureRect),
193 hotSpot.x(),
194 hotSpot.y());
195 }
196
197 if (m_cursorImage.image()->width() > m_cursorSize.width() || m_cursorImage.image()->height() > m_cursorSize.height())
198 qWarning("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(Qt::transparent);
202
203 QPainter painter;
204 painter.begin(&cursorImage);
205 painter.drawImage(0, 0, *m_cursorImage.image());
206 painter.end();
207
208 gbm_bo_write(m_bo, cursorImage.constBits(), cursorImage.sizeInBytes());
209
210 uint32_t handle = gbm_bo_get_handle(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
218 int status = drmModeSetCursor(kmsScreen->device()->fd(), kmsScreen->output().crtc_id, handle,
219 m_cursorSize.width(), m_cursorSize.height());
220 if (status != 0)
221 qWarning("Could not set cursor on screen %s: %d", kmsScreen->name().toLatin1().constData(), status);
222 }
223}
224#endif // QT_NO_CURSOR
225
226QPoint QEglFSKmsGbmCursor::pos() const
227{
228 return m_pos;
229}
230
231void QEglFSKmsGbmCursor::setPos(const QPoint &pos)
232{
233 Q_FOREACH (QPlatformScreen *screen, m_screen->virtualSiblings()) {
234 QEglFSKmsScreen *kmsScreen = static_cast<QEglFSKmsScreen *>(screen);
235 QPoint origin = kmsScreen->geometry().topLeft();
236 QPoint localPos = pos - origin;
237 QPoint adjustedPos = localPos - m_cursorImage.hotspot();
238
239 int ret = drmModeMoveCursor(kmsScreen->device()->fd(), kmsScreen->output().crtc_id, adjustedPos.x(), adjustedPos.y());
240 if (ret == 0)
241 m_pos = pos;
242 else
243 qWarning("Failed to move cursor on screen %s: %d", kmsScreen->name().toLatin1().constData(), ret);
244
245 kmsScreen->handleCursorMove(pos);
246 }
247}
248
249void QEglFSKmsGbmCursor::initCursorAtlas()
250{
251 static QByteArray json = qgetenv("QT_QPA_EGLFS_CURSOR");
252 if (json.isEmpty())
253 json = ":/cursor.json";
254
255 qCDebug(qLcEglfsKmsDebug) << "Initializing cursor atlas from" << json;
256
257 QFile file(QString::fromUtf8(json));
258 if (!file.open(QFile::ReadOnly)) {
259 Q_FOREACH (QPlatformScreen *screen, m_screen->virtualSiblings()) {
260 QEglFSKmsScreen *kmsScreen = static_cast<QEglFSKmsScreen *>(screen);
261 drmModeSetCursor(kmsScreen->device()->fd(), kmsScreen->output().crtc_id, 0, 0, 0);
262 drmModeMoveCursor(kmsScreen->device()->fd(), kmsScreen->output().crtc_id, 0, 0);
263 }
264 m_state = CursorDisabled;
265 return;
266 }
267
268 QJsonDocument doc = QJsonDocument::fromJson(file.readAll());
269 QJsonObject object = doc.object();
270
271 QString atlas = object.value(QLatin1String("image")).toString();
272 Q_ASSERT(!atlas.isEmpty());
273
274 const int cursorsPerRow = object.value(QLatin1String("cursorsPerRow")).toDouble();
275 Q_ASSERT(cursorsPerRow);
276 m_cursorAtlas.cursorsPerRow = cursorsPerRow;
277
278 const QJsonArray hotSpots = object.value(QLatin1String("hotSpots")).toArray();
279 Q_ASSERT(hotSpots.count() == Qt::LastCursor + 1);
280 for (int i = 0; i < hotSpots.count(); i++) {
281 QPoint hotSpot(hotSpots[i].toArray()[0].toDouble(), hotSpots[i].toArray()[1].toDouble());
282 m_cursorAtlas.hotSpots << hotSpot;
283 }
284
285 QImage image = QImage(atlas).convertToFormat(QImage::Format_ARGB32);
286 m_cursorAtlas.cursorWidth = image.width() / m_cursorAtlas.cursorsPerRow;
287 m_cursorAtlas.cursorHeight = image.height() / ((Qt::LastCursor + cursorsPerRow) / cursorsPerRow);
288 m_cursorAtlas.width = image.width();
289 m_cursorAtlas.height = image.height();
290 m_cursorAtlas.image = image;
291}
292
293QT_END_NAMESPACE
294