1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the qmake spec of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#include "qeglfshooks_p.h"
41#include <QLoggingCategory>
42
43QT_BEGIN_NAMESPACE
44
45Q_DECLARE_LOGGING_CATEGORY(qLcEglDevDebug)
46
47#ifdef EGLFS_PLATFORM_HOOKS
48
49QEglFSDeviceIntegration *qt_egl_device_integration()
50{
51 extern QEglFSHooks *platformHooks;
52 return platformHooks;
53}
54
55#else
56
57namespace {
58class DeviceIntegration
59{
60public:
61 DeviceIntegration();
62 ~DeviceIntegration() { delete m_integration; }
63 QEglFSDeviceIntegration *integration() { return m_integration; }
64private:
65 QEglFSDeviceIntegration *m_integration;
66};
67}
68
69Q_GLOBAL_STATIC(DeviceIntegration, deviceIntegration)
70
71DeviceIntegration::DeviceIntegration()
72 : m_integration(nullptr)
73{
74 QStringList pluginKeys = QEglFSDeviceIntegrationFactory::keys();
75 if (!pluginKeys.isEmpty()) {
76 // Some built-in logic: Prioritize either X11 or KMS/DRM.
77 if (qEnvironmentVariableIsSet("DISPLAY")) {
78 const QString x11key = QStringLiteral("eglfs_x11");
79 if (pluginKeys.contains(x11key)) {
80 pluginKeys.removeOne(x11key);
81 pluginKeys.prepend(x11key);
82 }
83 } else {
84 const QString kmskey = QStringLiteral("eglfs_kms");
85 if (pluginKeys.contains(kmskey)) {
86 pluginKeys.removeOne(kmskey);
87 pluginKeys.prepend(kmskey);
88 }
89 }
90
91 QByteArray requested;
92
93 // The environment variable can override everything.
94 if (qEnvironmentVariableIsSet("QT_QPA_EGLFS_INTEGRATION")) {
95 requested = qgetenv("QT_QPA_EGLFS_INTEGRATION");
96 } else {
97 // Device-specific makespecs may define a preferred plugin.
98#ifdef EGLFS_PREFERRED_PLUGIN
99#define DEFAULT_PLUGIN EGLFS_PREFERRED_PLUGIN
100#define STR(s) #s
101#define STRQ(s) STR(s)
102 requested = STRQ(DEFAULT_PLUGIN);
103#endif
104 }
105
106 // Treat "none" as special. There has to be a way to indicate
107 // that plugins must be ignored when the device is known to be
108 // functional with the default, non-specialized integration.
109 if (requested != QByteArrayLiteral("none")) {
110 if (!requested.isEmpty()) {
111 QString reqStr = QString::fromLocal8Bit(requested);
112 pluginKeys.removeOne(reqStr);
113 pluginKeys.prepend(reqStr);
114 }
115 qCDebug(qLcEglDevDebug) << "EGL device integration plugin keys (sorted):" << pluginKeys;
116 while (!m_integration && !pluginKeys.isEmpty()) {
117 QString key = pluginKeys.takeFirst();
118 qCDebug(qLcEglDevDebug) << "Trying to load device EGL integration" << key;
119 m_integration = QEglFSDeviceIntegrationFactory::create(key);
120 }
121 }
122 }
123
124 if (!m_integration) {
125 // Use a default, non-specialized device integration when no plugin is available.
126 // For some systems this is sufficient.
127 qCDebug(qLcEglDevDebug) << "Using base device integration";
128 m_integration = new QEglFSDeviceIntegration;
129 }
130}
131
132QEglFSDeviceIntegration *qt_egl_device_integration()
133{
134 return deviceIntegration()->integration();
135}
136
137#endif // EGLFS_PLATFORM_HOOKS
138
139QT_END_NAMESPACE
140