This repository has been archived by the owner on Aug 14, 2023. It is now read-only.
forked from tildearrow/kwin-lowlatency
-
Notifications
You must be signed in to change notification settings - Fork 0
/
keyboard_layout.cpp
338 lines (296 loc) · 10.8 KB
/
keyboard_layout.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/********************************************************************
KWin - the KDE window manager
This file is part of the KDE project.
Copyright (C) 2016, 2017 Martin Gräßlin <[email protected]>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*********************************************************************/
#include "keyboard_layout.h"
#include "keyboard_layout_switching.h"
#include "keyboard_input.h"
#include "input_event.h"
#include "main.h"
#include "platform.h"
#include "utils.h"
#include <KConfigGroup>
#include <KGlobalAccel>
#include <KLocalizedString>
#include <KNotifications/KStatusNotifierItem>
#include <QAction>
#include <QDBusConnection>
#include <QDBusMessage>
#include <QDBusPendingCall>
#include <QMenu>
namespace KWin
{
KeyboardLayout::KeyboardLayout(Xkb *xkb)
: QObject()
, m_xkb(xkb)
, m_notifierItem(nullptr)
{
}
KeyboardLayout::~KeyboardLayout() = default;
static QString translatedLayout(const QString &layout)
{
return i18nd("xkeyboard-config", layout.toUtf8().constData());
}
void KeyboardLayout::init()
{
QAction *switchKeyboardAction = new QAction(this);
switchKeyboardAction->setObjectName(QStringLiteral("Switch to Next Keyboard Layout"));
switchKeyboardAction->setProperty("componentName", QStringLiteral("KDE Keyboard Layout Switcher"));
const QKeySequence sequence = QKeySequence(Qt::ALT+Qt::CTRL+Qt::Key_K);
KGlobalAccel::self()->setDefaultShortcut(switchKeyboardAction, QList<QKeySequence>({sequence}));
KGlobalAccel::self()->setShortcut(switchKeyboardAction, QList<QKeySequence>({sequence}));
kwinApp()->platform()->setupActionForGlobalAccel(switchKeyboardAction);
connect(switchKeyboardAction, &QAction::triggered, this, &KeyboardLayout::switchToNextLayout);
QDBusConnection::sessionBus().connect(QString(),
QStringLiteral("/Layouts"),
QStringLiteral("org.kde.keyboard"),
QStringLiteral("reloadConfig"),
this,
SLOT(reconfigure()));
reconfigure();
}
void KeyboardLayout::initDBusInterface()
{
if (m_xkb->numberOfLayouts() <= 1) {
delete m_dbusInterface;
m_dbusInterface = nullptr;
return;
}
if (m_dbusInterface) {
return;
}
m_dbusInterface = new KeyboardLayoutDBusInterface(m_xkb, this);
connect(this, &KeyboardLayout::layoutChanged, m_dbusInterface,
[this] {
emit m_dbusInterface->currentLayoutChanged(m_xkb->layoutName());
}
);
// TODO: the signal might be emitted even if the list didn't change
connect(this, &KeyboardLayout::layoutsReconfigured, m_dbusInterface, &KeyboardLayoutDBusInterface::layoutListChanged);
}
void KeyboardLayout::initNotifierItem()
{
bool showNotifier = true;
bool showSingle = false;
if (m_config) {
const auto config = m_config->group(QStringLiteral("Layout"));
showNotifier = config.readEntry("ShowLayoutIndicator", true);
showSingle = config.readEntry("ShowSingle", false);
}
const bool shouldShow = showNotifier && (showSingle || m_xkb->numberOfLayouts() > 1);
if (shouldShow) {
if (m_notifierItem) {
return;
}
} else {
delete m_notifierItem;
m_notifierItem = nullptr;
return;
}
m_notifierItem = new KStatusNotifierItem(this);
m_notifierItem->setCategory(KStatusNotifierItem::Hardware);
m_notifierItem->setStatus(KStatusNotifierItem::Passive);
m_notifierItem->setToolTipTitle(i18nc("tooltip title", "Keyboard Layout"));
m_notifierItem->setTitle(i18nc("tooltip title", "Keyboard Layout"));
m_notifierItem->setToolTipIconByName(QStringLiteral("preferences-desktop-keyboard"));
m_notifierItem->setStandardActionsEnabled(false);
// TODO: proper icon
m_notifierItem->setIconByName(QStringLiteral("preferences-desktop-keyboard"));
connect(m_notifierItem, &KStatusNotifierItem::activateRequested, this, &KeyboardLayout::switchToNextLayout);
connect(m_notifierItem, &KStatusNotifierItem::scrollRequested, this,
[this] (int delta, Qt::Orientation orientation) {
if (orientation == Qt::Horizontal) {
return;
}
if (delta > 0) {
switchToNextLayout();
} else {
switchToPreviousLayout();
}
}
);
}
void KeyboardLayout::switchToNextLayout()
{
m_xkb->switchToNextLayout();
checkLayoutChange();
}
void KeyboardLayout::switchToPreviousLayout()
{
m_xkb->switchToPreviousLayout();
checkLayoutChange();
}
void KeyboardLayout::switchToLayout(xkb_layout_index_t index)
{
m_xkb->switchToLayout(index);
checkLayoutChange();
}
void KeyboardLayout::reconfigure()
{
if (m_config) {
m_config->reparseConfiguration();
const QString policyKey = m_config->group(QStringLiteral("Layout")).readEntry("SwitchMode", QStringLiteral("Global"));
if (!m_policy || m_policy->name() != policyKey) {
delete m_policy;
m_policy = KeyboardLayoutSwitching::Policy::create(m_xkb, this, policyKey);
}
}
m_xkb->reconfigure();
resetLayout();
}
void KeyboardLayout::resetLayout()
{
m_layout = m_xkb->currentLayout();
initNotifierItem();
updateNotifier();
reinitNotifierMenu();
loadShortcuts();
emit layoutsReconfigured();
initDBusInterface();
}
void KeyboardLayout::loadShortcuts()
{
qDeleteAll(m_layoutShortcuts);
m_layoutShortcuts.clear();
const auto layouts = m_xkb->layoutNames();
const QString componentName = QStringLiteral("KDE Keyboard Layout Switcher");
for (auto it = layouts.begin(); it != layouts.end(); it++) {
// layout name is translated in the action name in keyboard kcm!
const QString action = QStringLiteral("Switch keyboard layout to %1").arg(translatedLayout(it.value()));
const auto shortcuts = KGlobalAccel::self()->globalShortcut(componentName, action);
if (shortcuts.isEmpty()) {
continue;
}
QAction *a = new QAction(this);
a->setObjectName(action);
a->setProperty("componentName", componentName);
connect(a, &QAction::triggered, this,
std::bind(&KeyboardLayout::switchToLayout, this, it.key()));
KGlobalAccel::self()->setShortcut(a, shortcuts, KGlobalAccel::Autoloading);
m_layoutShortcuts << a;
}
}
void KeyboardLayout::keyEvent(KeyEvent *event)
{
if (!event->isAutoRepeat()) {
checkLayoutChange();
}
}
void KeyboardLayout::checkLayoutChange()
{
const auto layout = m_xkb->currentLayout();
if (m_layout == layout) {
return;
}
m_layout = layout;
notifyLayoutChange();
updateNotifier();
emit layoutChanged();
}
void KeyboardLayout::notifyLayoutChange()
{
// notify OSD service about the new layout
QDBusMessage msg = QDBusMessage::createMethodCall(
QStringLiteral("org.kde.plasmashell"),
QStringLiteral("/org/kde/osdService"),
QStringLiteral("org.kde.osdService"),
QStringLiteral("kbdLayoutChanged"));
msg << translatedLayout(m_xkb->layoutName());
QDBusConnection::sessionBus().asyncCall(msg);
}
void KeyboardLayout::updateNotifier()
{
if (!m_notifierItem) {
return;
}
m_notifierItem->setToolTipSubTitle(translatedLayout(m_xkb->layoutName()));
// TODO: update icon
}
void KeyboardLayout::reinitNotifierMenu()
{
if (!m_notifierItem) {
return;
}
const auto layouts = m_xkb->layoutNames();
QMenu *menu = new QMenu;
for (auto it = layouts.begin(); it != layouts.end(); it++) {
menu->addAction(translatedLayout(it.value()), std::bind(&KeyboardLayout::switchToLayout, this, it.key()));
}
menu->addSeparator();
menu->addAction(QIcon::fromTheme(QStringLiteral("configure")), i18n("Configure Layouts..."), this,
[this] {
// TODO: introduce helper function to start kcmshell5
QProcess *p = new Process(this);
p->setArguments(QStringList{QStringLiteral("--args=--tab=layouts"), QStringLiteral("kcm_keyboard")});
p->setProcessEnvironment(kwinApp()->processStartupEnvironment());
p->setProgram(QStringLiteral("kcmshell5"));
connect(p, static_cast<void (QProcess::*)(int, QProcess::ExitStatus)>(&QProcess::finished), p, &QProcess::deleteLater);
connect(p, &QProcess::errorOccurred, this, [](QProcess::ProcessError e) {
if (e == QProcess::FailedToStart) {
qCDebug(KWIN_CORE) << "Failed to start kcmshell5";
}
});
p->start();
}
);
m_notifierItem->setContextMenu(menu);
}
static const QString s_keyboardService = QStringLiteral("org.kde.keyboard");
static const QString s_keyboardObject = QStringLiteral("/Layouts");
KeyboardLayoutDBusInterface::KeyboardLayoutDBusInterface(Xkb *xkb, KeyboardLayout *parent)
: QObject(parent)
, m_xkb(xkb)
, m_keyboardLayout(parent)
{
QDBusConnection::sessionBus().registerService(s_keyboardService);
QDBusConnection::sessionBus().registerObject(s_keyboardObject, this, QDBusConnection::ExportAllSlots | QDBusConnection::ExportAllSignals);
}
KeyboardLayoutDBusInterface::~KeyboardLayoutDBusInterface()
{
QDBusConnection::sessionBus().unregisterService(s_keyboardService);
}
bool KeyboardLayoutDBusInterface::setLayout(const QString &layout)
{
const auto layouts = m_xkb->layoutNames();
auto it = layouts.begin();
for (; it !=layouts.end(); it++) {
if (it.value() == layout) {
break;
}
}
if (it == layouts.end()) {
return false;
}
m_xkb->switchToLayout(it.key());
m_keyboardLayout->checkLayoutChange();
return true;
}
QString KeyboardLayoutDBusInterface::getCurrentLayout()
{
return m_xkb->layoutName();
}
QStringList KeyboardLayoutDBusInterface::getLayoutsList()
{
const auto layouts = m_xkb->layoutNames();
QStringList ret;
for (auto it = layouts.begin(); it != layouts.end(); it++) {
ret << it.value();
}
return ret;
}
QString KeyboardLayoutDBusInterface::getLayoutDisplayName(const QString &layout)
{
return translatedLayout(layout);
}
}