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 QtGui module 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 "qevdevkeyboardmanager_p.h"
41
42#include <QtInputSupport/private/qevdevutil_p.h>
43
44#include <QStringList>
45#include <QCoreApplication>
46#include <QLoggingCategory>
47
48#include <private/qguiapplication_p.h>
49#include <private/qinputdevicemanager_p_p.h>
50
51QT_BEGIN_NAMESPACE
52
53Q_DECLARE_LOGGING_CATEGORY(qLcEvdevKey)
54
55QEvdevKeyboardManager::QEvdevKeyboardManager(const QString &key, const QString &specification, QObject *parent)
56 : QObject(parent)
57{
58 Q_UNUSED(key);
59
60
61 QString spec = QString::fromLocal8Bit(qgetenv("QT_QPA_EVDEV_KEYBOARD_PARAMETERS"));
62
63 if (spec.isEmpty())
64 spec = specification;
65
66 auto parsed = QEvdevUtil::parseSpecification(spec);
67 m_spec = std::move(parsed.spec);
68
69 // add all keyboards for devices specified in the argument list
70 for (const QString &device : qAsConst(parsed.devices))
71 addKeyboard(device);
72
73 if (parsed.devices.isEmpty()) {
74 qCDebug(qLcEvdevKey, "evdevkeyboard: Using device discovery");
75 if (auto deviceDiscovery = QDeviceDiscovery::create(QDeviceDiscovery::Device_Keyboard, this)) {
76 // scan and add already connected keyboards
77 const QStringList devices = deviceDiscovery->scanConnectedDevices();
78 for (const QString &device : devices)
79 addKeyboard(device);
80
81 connect(deviceDiscovery, &QDeviceDiscovery::deviceDetected,
82 this, &QEvdevKeyboardManager::addKeyboard);
83 connect(deviceDiscovery, &QDeviceDiscovery::deviceRemoved,
84 this, &QEvdevKeyboardManager::removeKeyboard);
85 }
86 }
87}
88
89QEvdevKeyboardManager::~QEvdevKeyboardManager()
90{
91}
92
93void QEvdevKeyboardManager::addKeyboard(const QString &deviceNode)
94{
95 qCDebug(qLcEvdevKey, "Adding keyboard at %ls", qUtf16Printable(deviceNode));
96 auto keyboard = QEvdevKeyboardHandler::create(deviceNode, m_spec, m_defaultKeymapFile);
97 if (keyboard) {
98 m_keyboards.add(deviceNode, std::move(keyboard));
99 updateDeviceCount();
100 } else {
101 qWarning("Failed to open keyboard device %ls", qUtf16Printable(deviceNode));
102 }
103}
104
105void QEvdevKeyboardManager::removeKeyboard(const QString &deviceNode)
106{
107 if (m_keyboards.remove(deviceNode)) {
108 qCDebug(qLcEvdevKey, "Removing keyboard at %ls", qUtf16Printable(deviceNode));
109 updateDeviceCount();
110 }
111}
112
113void QEvdevKeyboardManager::updateDeviceCount()
114{
115 QInputDeviceManagerPrivate::get(QGuiApplicationPrivate::inputDeviceManager())->setDeviceCount(
116 QInputDeviceManager::DeviceTypeKeyboard, m_keyboards.count());
117}
118
119void QEvdevKeyboardManager::loadKeymap(const QString &file)
120{
121 m_defaultKeymapFile = file;
122
123 if (file.isEmpty()) {
124 // Restore the default, which is either the built-in keymap or
125 // the one given in the plugin spec.
126 QString keymapFromSpec;
127 const auto specs = QStringView{m_spec}.split(QLatin1Char(':'));
128 for (const auto &arg : specs) {
129 if (arg.startsWith(QLatin1String("keymap=")))
130 keymapFromSpec = arg.mid(7).toString();
131 }
132 for (const auto &keyboard : m_keyboards) {
133 if (keymapFromSpec.isEmpty())
134 keyboard.handler->unloadKeymap();
135 else
136 keyboard.handler->loadKeymap(keymapFromSpec);
137 }
138 } else {
139 for (const auto &keyboard : m_keyboards)
140 keyboard.handler->loadKeymap(file);
141 }
142}
143
144void QEvdevKeyboardManager::switchLang()
145{
146 for (const auto &keyboard : m_keyboards)
147 keyboard.handler->switchLang();
148}
149
150QT_END_NAMESPACE
151