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 
64 QT_BEGIN_NAMESPACE
65 
Q_DECLARE_LOGGING_CATEGORY(qLcEglfsKmsDebug)66 Q_DECLARE_LOGGING_CATEGORY(qLcEglfsKmsDebug)
67 
68 QEglFSKmsGbmCursor::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("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, nullptr);
106 #endif
107     setPos(QPoint(0, 0));
108 }
109 
~QEglFSKmsGbmCursor()110 QEglFSKmsGbmCursor::~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 
updateMouseStatus()126 void 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 
hasMouse() const140 bool QEglFSKmsGbmCursorDeviceListener::hasMouse() const
141 {
142     return QGuiApplicationPrivate::inputDeviceManager()->deviceCount(QInputDeviceManager::DeviceTypePointer) > 0;
143 }
144 
onDeviceListChanged(QInputDeviceManager::DeviceType type)145 void QEglFSKmsGbmCursorDeviceListener::onDeviceListChanged(QInputDeviceManager::DeviceType type)
146 {
147     if (type == QInputDeviceManager::DeviceTypePointer)
148         m_cursor->updateMouseStatus();
149 }
150 
pointerEvent(const QMouseEvent & event)151 void QEglFSKmsGbmCursor::pointerEvent(const QMouseEvent &event)
152 {
153     setPos(event.screenPos().toPoint());
154 }
155 
156 #ifndef QT_NO_CURSOR
changeCursor(QCursor * windowCursor,QWindow * window)157 void 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         if (kmsScreen->isCursorOutOfRange())
218             continue;
219         int status = drmModeSetCursor(kmsScreen->device()->fd(), kmsScreen->output().crtc_id, handle,
220                                       m_cursorSize.width(), m_cursorSize.height());
221         if (status != 0)
222             qWarning("Could not set cursor on screen %s: %d", kmsScreen->name().toLatin1().constData(), status);
223     }
224 }
225 #endif // QT_NO_CURSOR
226 
pos() const227 QPoint QEglFSKmsGbmCursor::pos() const
228 {
229     return m_pos;
230 }
231 
setPos(const QPoint & pos)232 void 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(kmsScreen->device()->fd(), kmsScreen->output().crtc_id, 0, 0, 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(m_bo).u32;
253                 ret = drmModeSetCursor(kmsScreen->device()->fd(), kmsScreen->output().crtc_id,
254                                        handle, m_cursorSize.width(), m_cursorSize.height());
255             } else {
256                 ret = drmModeMoveCursor(kmsScreen->device()->fd(), kmsScreen->output().crtc_id,
257                                         adjustedLocalPos.x(), adjustedLocalPos.y());
258             }
259             if (ret == 0)
260                 m_pos = pos;
261             else
262                 qWarning("Failed to move cursor on screen %s: %d", kmsScreen->name().toLatin1().constData(), ret);
263 
264             kmsScreen->handleCursorMove(pos);
265         }
266     }
267 }
268 
initCursorAtlas()269 void QEglFSKmsGbmCursor::initCursorAtlas()
270 {
271     static QByteArray json = qgetenv("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(json));
278     if (!file.open(QFile::ReadOnly)) {
279         Q_FOREACH (QPlatformScreen *screen, m_screen->virtualSiblings()) {
280             QEglFSKmsScreen *kmsScreen = static_cast<QEglFSKmsScreen *>(screen);
281             drmModeSetCursor(kmsScreen->device()->fd(), kmsScreen->output().crtc_id, 0, 0, 0);
282             drmModeMoveCursor(kmsScreen->device()->fd(), kmsScreen->output().crtc_id, 0, 0);
283         }
284         m_state = CursorDisabled;
285         return;
286     }
287 
288     QJsonDocument doc = QJsonDocument::fromJson(file.readAll());
289     QJsonObject object = doc.object();
290 
291     QString atlas = object.value(QLatin1String("image")).toString();
292     Q_ASSERT(!atlas.isEmpty());
293 
294     const int cursorsPerRow = object.value(QLatin1String("cursorsPerRow")).toDouble();
295     Q_ASSERT(cursorsPerRow);
296     m_cursorAtlas.cursorsPerRow = cursorsPerRow;
297 
298     const QJsonArray hotSpots = object.value(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(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 
313 QT_END_NAMESPACE
314