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 "qeglfskmsgbmdevice.h"
43#include "qeglfskmsgbmscreen.h"
44
45#include "qeglfsintegration_p.h"
46
47#include <QtCore/QLoggingCategory>
48#include <QtCore/private/qcore_unix_p.h>
49
50#define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
51
52QT_BEGIN_NAMESPACE
53
54Q_DECLARE_LOGGING_CATEGORY(qLcEglfsKmsDebug)
55
56QEglFSKmsGbmDevice::QEglFSKmsGbmDevice(QKmsScreenConfig *screenConfig, const QString &path)
57 : QEglFSKmsDevice(screenConfig, path)
58 , m_gbm_device(nullptr)
59 , m_globalCursor(nullptr)
60{
61}
62
63bool QEglFSKmsGbmDevice::open()
64{
65 Q_ASSERT(fd() == -1);
66 Q_ASSERT(m_gbm_device == nullptr);
67
68 int fd = qt_safe_open(pathname: devicePath().toLocal8Bit().constData(), O_RDWR | O_CLOEXEC);
69 if (fd == -1) {
70 qErrnoWarning(msg: "Could not open DRM device %s", qPrintable(devicePath()));
71 return false;
72 }
73
74 qCDebug(qLcEglfsKmsDebug) << "Creating GBM device for file descriptor" << fd
75 << "obtained from" << devicePath();
76 m_gbm_device = gbm_create_device(fd);
77 if (!m_gbm_device) {
78 qErrnoWarning(msg: "Could not create GBM device");
79 qt_safe_close(fd);
80 fd = -1;
81 return false;
82 }
83
84 setFd(fd);
85
86 m_eventReader.create(device: this);
87
88 return true;
89}
90
91void QEglFSKmsGbmDevice::close()
92{
93 // Note: screens are gone at this stage.
94
95 m_eventReader.destroy();
96
97 if (m_gbm_device) {
98 gbm_device_destroy(gbm: m_gbm_device);
99 m_gbm_device = nullptr;
100 }
101
102 if (fd() != -1) {
103 qt_safe_close(fd: fd());
104 setFd(-1);
105 }
106}
107
108void *QEglFSKmsGbmDevice::nativeDisplay() const
109{
110 return m_gbm_device;
111}
112
113gbm_device * QEglFSKmsGbmDevice::gbmDevice() const
114{
115 return m_gbm_device;
116}
117
118QPlatformCursor *QEglFSKmsGbmDevice::globalCursor() const
119{
120 return m_globalCursor;
121}
122
123// Cannot do this from close(), it may be too late.
124// Call this from the last screen dtor instead.
125void QEglFSKmsGbmDevice::destroyGlobalCursor()
126{
127 if (m_globalCursor) {
128 qCDebug(qLcEglfsKmsDebug, "Destroying global GBM mouse cursor");
129 delete m_globalCursor;
130 m_globalCursor = nullptr;
131 }
132}
133
134QPlatformScreen *QEglFSKmsGbmDevice::createScreen(const QKmsOutput &output)
135{
136 QEglFSKmsGbmScreen *screen = new QEglFSKmsGbmScreen(this, output, false);
137
138 if (!m_globalCursor && screenConfig()->hwCursor()) {
139 qCDebug(qLcEglfsKmsDebug, "Creating new global GBM mouse cursor");
140 m_globalCursor = new QEglFSKmsGbmCursor(screen);
141 }
142
143 return screen;
144}
145
146QPlatformScreen *QEglFSKmsGbmDevice::createHeadlessScreen()
147{
148 return new QEglFSKmsGbmScreen(this, QKmsOutput(), true);
149}
150
151void QEglFSKmsGbmDevice::registerScreenCloning(QPlatformScreen *screen,
152 QPlatformScreen *screenThisScreenClones,
153 const QVector<QPlatformScreen *> &screensCloningThisScreen)
154{
155 if (!screenThisScreenClones && screensCloningThisScreen.isEmpty())
156 return;
157
158 QEglFSKmsGbmScreen *gbmScreen = static_cast<QEglFSKmsGbmScreen *>(screen);
159 gbmScreen->initCloning(screenThisScreenClones, screensCloningThisScreen);
160}
161
162void QEglFSKmsGbmDevice::registerScreen(QPlatformScreen *screen,
163 bool isPrimary,
164 const QPoint &virtualPos,
165 const QList<QPlatformScreen *> &virtualSiblings)
166{
167 QEglFSKmsDevice::registerScreen(screen, isPrimary, virtualPos, virtualSiblings);
168 if (screenConfig()->hwCursor() && m_globalCursor)
169 m_globalCursor->reevaluateVisibilityForScreens();
170}
171
172QT_END_NAMESPACE
173

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