Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qevdevkeyboardmanager.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
5
6#include <QtInputSupport/private/qevdevutil_p.h>
7
8#include <QStringList>
9#include <QCoreApplication>
10#include <QLoggingCategory>
11
12#include <private/qguiapplication_p.h>
13#include <private/qinputdevicemanager_p_p.h>
14
16
17using namespace Qt::StringLiterals;
18
19QEvdevKeyboardManager::QEvdevKeyboardManager(const QString &key, const QString &specification, QObject *parent)
21{
22 Q_UNUSED(key);
23
24
25 QString spec = qEnvironmentVariable("QT_QPA_EVDEV_KEYBOARD_PARAMETERS");
26
27 if (spec.isEmpty())
28 spec = specification;
29
30 auto parsed = QEvdevUtil::parseSpecification(spec);
31 m_spec = std::move(parsed.spec);
32
33 // add all keyboards for devices specified in the argument list
34 for (const QString &device : std::as_const(parsed.devices))
35 addKeyboard(device);
36
37 if (parsed.devices.isEmpty()) {
38 qCDebug(qLcEvdevKey, "evdevkeyboard: Using device discovery");
39 if (auto deviceDiscovery = QDeviceDiscovery::create(QDeviceDiscovery::Device_Keyboard, this)) {
40 // scan and add already connected keyboards
41 const QStringList devices = deviceDiscovery->scanConnectedDevices();
42 for (const QString &device : devices)
43 addKeyboard(device);
44
45 connect(deviceDiscovery, &QDeviceDiscovery::deviceDetected,
46 this, &QEvdevKeyboardManager::addKeyboard);
47 connect(deviceDiscovery, &QDeviceDiscovery::deviceRemoved,
48 this, &QEvdevKeyboardManager::removeKeyboard);
49 }
50 }
51}
52
56
57void QEvdevKeyboardManager::addKeyboard(const QString &deviceNode)
58{
59 qCDebug(qLcEvdevKey, "Adding keyboard at %ls", qUtf16Printable(deviceNode));
60 auto keyboard = QEvdevKeyboardHandler::create(deviceNode, m_spec, m_defaultKeymapFile);
61 if (keyboard) {
62 m_keyboards.add(deviceNode, std::move(keyboard));
63 updateDeviceCount();
64 } else {
65 qWarning("Failed to open keyboard device %ls", qUtf16Printable(deviceNode));
66 }
67}
68
69void QEvdevKeyboardManager::removeKeyboard(const QString &deviceNode)
70{
71 if (m_keyboards.remove(deviceNode)) {
72 qCDebug(qLcEvdevKey, "Removing keyboard at %ls", qUtf16Printable(deviceNode));
73 updateDeviceCount();
74 }
75}
76
77void QEvdevKeyboardManager::updateDeviceCount()
78{
79 QInputDeviceManagerPrivate::get(QGuiApplicationPrivate::inputDeviceManager())->setDeviceCount(
80 QInputDeviceManager::DeviceTypeKeyboard, m_keyboards.count());
81}
82
83void QEvdevKeyboardManager::loadKeymap(const QString &file)
84{
85 m_defaultKeymapFile = file;
86
87 if (file.isEmpty()) {
88 // Restore the default, which is either the built-in keymap or
89 // the one given in the plugin spec.
90 QString keymapFromSpec;
91 const auto specs = QStringView{m_spec}.split(u':');
92 for (const auto &arg : specs) {
93 if (arg.startsWith("keymap="_L1))
94 keymapFromSpec = arg.mid(7).toString();
95 }
96 for (const auto &keyboard : m_keyboards) {
97 if (keymapFromSpec.isEmpty())
98 keyboard.handler->unloadKeymap();
99 else
100 keyboard.handler->loadKeymap(keymapFromSpec);
101 }
102 } else {
103 for (const auto &keyboard : m_keyboards)
104 keyboard.handler->loadKeymap(file);
105 }
106}
107
109{
110 for (const auto &keyboard : m_keyboards)
111 keyboard.handler->switchLang();
112}
113
114QT_END_NAMESPACE
void removeKeyboard(const QString &deviceNode)
QEvdevKeyboardManager(const QString &key, const QString &specification, QObject *parent=nullptr)
void addKeyboard(const QString &deviceNode=QString())
void loadKeymap(const QString &file)
#define qCDebug(category,...)