2020-08-02 22:22:19 +00:00
|
|
|
/*
|
|
|
|
KWin - the KDE window manager
|
|
|
|
This file is part of the KDE project.
|
2017-01-14 17:06:41 +00:00
|
|
|
|
2020-08-02 22:22:19 +00:00
|
|
|
SPDX-FileCopyrightText: 2016, 2017 Martin Gräßlin <mgraesslin@kde.org>
|
2017-01-14 17:06:41 +00:00
|
|
|
|
2020-08-02 22:22:19 +00:00
|
|
|
SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
*/
|
2017-01-14 17:06:41 +00:00
|
|
|
#include "keyboard_layout.h"
|
|
|
|
#include "input_event.h"
|
2022-03-23 10:13:38 +00:00
|
|
|
#include "keyboard_input.h"
|
|
|
|
#include "keyboard_layout_switching.h"
|
2017-01-14 17:06:41 +00:00
|
|
|
#include "main.h"
|
|
|
|
#include "platform.h"
|
|
|
|
|
|
|
|
#include <KGlobalAccel>
|
2017-01-22 15:53:57 +00:00
|
|
|
#include <KLocalizedString>
|
2017-01-14 17:06:41 +00:00
|
|
|
#include <QAction>
|
|
|
|
#include <QDBusConnection>
|
|
|
|
#include <QDBusMessage>
|
2021-01-06 16:25:03 +00:00
|
|
|
#include <QDBusMetaType>
|
2022-03-23 10:13:38 +00:00
|
|
|
#include <QDBusPendingCall>
|
2017-01-14 17:06:41 +00:00
|
|
|
|
|
|
|
namespace KWin
|
|
|
|
{
|
|
|
|
|
2021-01-06 16:25:03 +00:00
|
|
|
KeyboardLayout::KeyboardLayout(Xkb *xkb, const KSharedConfigPtr &config)
|
2017-01-14 17:06:41 +00:00
|
|
|
: QObject()
|
|
|
|
, m_xkb(xkb)
|
2021-01-06 16:25:03 +00:00
|
|
|
, m_configGroup(config->group("Layout"))
|
2017-01-14 17:06:41 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
KeyboardLayout::~KeyboardLayout() = default;
|
|
|
|
|
2017-01-23 19:06:53 +00:00
|
|
|
static QString translatedLayout(const QString &layout)
|
|
|
|
{
|
|
|
|
return i18nd("xkeyboard-config", layout.toUtf8().constData());
|
|
|
|
}
|
|
|
|
|
2017-01-14 17:06:41 +00:00
|
|
|
void KeyboardLayout::init()
|
|
|
|
{
|
|
|
|
QAction *switchKeyboardAction = new QAction(this);
|
|
|
|
switchKeyboardAction->setObjectName(QStringLiteral("Switch to Next Keyboard Layout"));
|
|
|
|
switchKeyboardAction->setProperty("componentName", QStringLiteral("KDE Keyboard Layout Switcher"));
|
2021-07-26 08:37:48 +00:00
|
|
|
switchKeyboardAction->setProperty("componentDisplayName", i18n("Keyboard Layout Switcher"));
|
2022-03-09 16:35:12 +00:00
|
|
|
const QKeySequence sequence = QKeySequence(Qt::ALT | Qt::CTRL | Qt::Key_K);
|
2017-01-14 17:06:41 +00:00
|
|
|
KGlobalAccel::self()->setDefaultShortcut(switchKeyboardAction, QList<QKeySequence>({sequence}));
|
|
|
|
KGlobalAccel::self()->setShortcut(switchKeyboardAction, QList<QKeySequence>({sequence}));
|
|
|
|
kwinApp()->platform()->setupActionForGlobalAccel(switchKeyboardAction);
|
2017-01-20 06:15:27 +00:00
|
|
|
connect(switchKeyboardAction, &QAction::triggered, this, &KeyboardLayout::switchToNextLayout);
|
2017-01-14 17:06:41 +00:00
|
|
|
|
|
|
|
QDBusConnection::sessionBus().connect(QString(),
|
|
|
|
QStringLiteral("/Layouts"),
|
|
|
|
QStringLiteral("org.kde.keyboard"),
|
|
|
|
QStringLiteral("reloadConfig"),
|
|
|
|
this,
|
|
|
|
SLOT(reconfigure()));
|
|
|
|
|
|
|
|
reconfigure();
|
2017-01-28 14:25:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void KeyboardLayout::initDBusInterface()
|
|
|
|
{
|
2017-02-11 10:34:49 +00:00
|
|
|
if (m_xkb->numberOfLayouts() <= 1) {
|
2020-06-25 15:21:08 +00:00
|
|
|
if (m_dbusInterface) {
|
|
|
|
m_dbusInterface->deleteLater();
|
|
|
|
m_dbusInterface = nullptr;
|
|
|
|
}
|
2017-02-11 10:34:49 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (m_dbusInterface) {
|
|
|
|
return;
|
|
|
|
}
|
2021-01-06 16:25:03 +00:00
|
|
|
m_dbusInterface = new KeyboardLayoutDBusInterface(m_xkb, m_configGroup, this);
|
2021-01-09 13:22:29 +00:00
|
|
|
connect(this, &KeyboardLayout::layoutChanged,
|
|
|
|
m_dbusInterface, &KeyboardLayoutDBusInterface::layoutChanged);
|
2017-01-28 14:25:40 +00:00
|
|
|
// TODO: the signal might be emitted even if the list didn't change
|
2017-02-11 10:34:49 +00:00
|
|
|
connect(this, &KeyboardLayout::layoutsReconfigured, m_dbusInterface, &KeyboardLayoutDBusInterface::layoutListChanged);
|
2017-01-14 17:06:41 +00:00
|
|
|
}
|
|
|
|
|
2017-01-20 06:15:27 +00:00
|
|
|
void KeyboardLayout::switchToNextLayout()
|
|
|
|
{
|
2020-08-24 13:51:43 +00:00
|
|
|
const quint32 previousLayout = m_xkb->currentLayout();
|
2017-01-20 06:15:27 +00:00
|
|
|
m_xkb->switchToNextLayout();
|
2020-08-24 13:51:43 +00:00
|
|
|
checkLayoutChange(previousLayout);
|
2017-01-20 06:15:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void KeyboardLayout::switchToPreviousLayout()
|
|
|
|
{
|
2020-08-24 13:51:43 +00:00
|
|
|
const quint32 previousLayout = m_xkb->currentLayout();
|
2017-01-20 06:15:27 +00:00
|
|
|
m_xkb->switchToPreviousLayout();
|
2020-08-24 13:51:43 +00:00
|
|
|
checkLayoutChange(previousLayout);
|
2017-01-20 06:15:27 +00:00
|
|
|
}
|
|
|
|
|
2017-01-23 19:06:53 +00:00
|
|
|
void KeyboardLayout::switchToLayout(xkb_layout_index_t index)
|
|
|
|
{
|
2020-08-24 13:51:43 +00:00
|
|
|
const quint32 previousLayout = m_xkb->currentLayout();
|
2017-01-23 19:06:53 +00:00
|
|
|
m_xkb->switchToLayout(index);
|
2020-08-24 13:51:43 +00:00
|
|
|
checkLayoutChange(previousLayout);
|
2017-01-23 19:06:53 +00:00
|
|
|
}
|
|
|
|
|
2017-01-14 17:06:41 +00:00
|
|
|
void KeyboardLayout::reconfigure()
|
|
|
|
{
|
2021-01-06 16:25:03 +00:00
|
|
|
if (m_configGroup.isValid()) {
|
|
|
|
m_configGroup.config()->reparseConfiguration();
|
|
|
|
const QString policyKey = m_configGroup.readEntry("SwitchMode", QStringLiteral("Global"));
|
2020-05-29 17:08:24 +00:00
|
|
|
m_xkb->reconfigure();
|
2017-04-04 16:56:10 +00:00
|
|
|
if (!m_policy || m_policy->name() != policyKey) {
|
|
|
|
delete m_policy;
|
2021-01-06 16:25:03 +00:00
|
|
|
m_policy = KeyboardLayoutSwitching::Policy::create(m_xkb, this, m_configGroup, policyKey);
|
2017-04-04 16:56:10 +00:00
|
|
|
}
|
2020-05-29 17:08:24 +00:00
|
|
|
} else {
|
|
|
|
m_xkb->reconfigure();
|
2017-01-20 06:15:27 +00:00
|
|
|
}
|
2017-01-14 17:06:41 +00:00
|
|
|
resetLayout();
|
|
|
|
}
|
|
|
|
|
|
|
|
void KeyboardLayout::resetLayout()
|
|
|
|
{
|
|
|
|
m_layout = m_xkb->currentLayout();
|
2017-01-23 19:06:53 +00:00
|
|
|
loadShortcuts();
|
2017-02-11 10:34:49 +00:00
|
|
|
|
|
|
|
initDBusInterface();
|
2021-06-08 07:02:14 +00:00
|
|
|
Q_EMIT layoutsReconfigured();
|
2017-01-23 19:06:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void KeyboardLayout::loadShortcuts()
|
|
|
|
{
|
|
|
|
qDeleteAll(m_layoutShortcuts);
|
|
|
|
m_layoutShortcuts.clear();
|
|
|
|
const QString componentName = QStringLiteral("KDE Keyboard Layout Switcher");
|
2021-01-08 20:06:59 +00:00
|
|
|
const quint32 count = m_xkb->numberOfLayouts();
|
2022-03-23 10:13:38 +00:00
|
|
|
for (uint i = 0; i < count; ++i) {
|
2017-01-23 19:06:53 +00:00
|
|
|
// layout name is translated in the action name in keyboard kcm!
|
2022-03-23 10:13:38 +00:00
|
|
|
const QString action = QStringLiteral("Switch keyboard layout to %1").arg(translatedLayout(m_xkb->layoutName(i)));
|
2017-01-23 19:06:53 +00:00
|
|
|
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,
|
2021-01-08 20:06:59 +00:00
|
|
|
std::bind(&KeyboardLayout::switchToLayout, this, i));
|
2017-01-23 19:06:53 +00:00
|
|
|
KGlobalAccel::self()->setShortcut(a, shortcuts, KGlobalAccel::Autoloading);
|
|
|
|
m_layoutShortcuts << a;
|
|
|
|
}
|
2017-01-14 17:06:41 +00:00
|
|
|
}
|
|
|
|
|
2021-01-09 13:22:29 +00:00
|
|
|
void KeyboardLayout::checkLayoutChange(uint previousLayout)
|
2017-01-14 17:06:41 +00:00
|
|
|
{
|
2020-08-24 13:51:43 +00:00
|
|
|
// Get here on key event or DBus call.
|
|
|
|
// m_layout - layout saved last time OSD occurred
|
|
|
|
// previousLayout - actual layout just before potential layout change
|
|
|
|
// We need OSD if current layout deviates from any of these
|
2021-01-09 13:22:29 +00:00
|
|
|
const uint currentLayout = m_xkb->currentLayout();
|
|
|
|
if (m_layout != currentLayout || previousLayout != currentLayout) {
|
|
|
|
m_layout = currentLayout;
|
2020-08-24 13:51:43 +00:00
|
|
|
notifyLayoutChange();
|
2021-06-08 07:02:14 +00:00
|
|
|
Q_EMIT layoutChanged(currentLayout);
|
2017-01-14 17:06:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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"));
|
|
|
|
|
2017-01-23 19:06:53 +00:00
|
|
|
msg << translatedLayout(m_xkb->layoutName());
|
2017-01-14 17:06:41 +00:00
|
|
|
|
|
|
|
QDBusConnection::sessionBus().asyncCall(msg);
|
|
|
|
}
|
|
|
|
|
2017-01-28 14:25:40 +00:00
|
|
|
static const QString s_keyboardService = QStringLiteral("org.kde.keyboard");
|
|
|
|
static const QString s_keyboardObject = QStringLiteral("/Layouts");
|
|
|
|
|
2021-01-06 16:25:03 +00:00
|
|
|
KeyboardLayoutDBusInterface::KeyboardLayoutDBusInterface(Xkb *xkb, const KConfigGroup &configGroup, KeyboardLayout *parent)
|
2017-01-28 14:25:40 +00:00
|
|
|
: QObject(parent)
|
|
|
|
, m_xkb(xkb)
|
2021-01-06 16:25:03 +00:00
|
|
|
, m_configGroup(configGroup)
|
2017-02-01 05:49:32 +00:00
|
|
|
, m_keyboardLayout(parent)
|
2017-01-28 14:25:40 +00:00
|
|
|
{
|
2021-01-06 16:25:03 +00:00
|
|
|
qRegisterMetaType<QVector<LayoutNames>>("QVector<LayoutNames>");
|
|
|
|
qDBusRegisterMetaType<LayoutNames>();
|
|
|
|
qDBusRegisterMetaType<QVector<LayoutNames>>();
|
|
|
|
|
2017-01-28 14:25:40 +00:00
|
|
|
QDBusConnection::sessionBus().registerObject(s_keyboardObject, this, QDBusConnection::ExportAllSlots | QDBusConnection::ExportAllSignals);
|
2021-01-20 23:27:02 +00:00
|
|
|
QDBusConnection::sessionBus().registerService(s_keyboardService);
|
2017-01-28 14:25:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
KeyboardLayoutDBusInterface::~KeyboardLayoutDBusInterface()
|
|
|
|
{
|
|
|
|
QDBusConnection::sessionBus().unregisterService(s_keyboardService);
|
|
|
|
}
|
|
|
|
|
2020-11-23 13:56:18 +00:00
|
|
|
void KeyboardLayoutDBusInterface::switchToNextLayout()
|
|
|
|
{
|
|
|
|
m_keyboardLayout->switchToNextLayout();
|
|
|
|
}
|
|
|
|
|
|
|
|
void KeyboardLayoutDBusInterface::switchToPreviousLayout()
|
|
|
|
{
|
|
|
|
m_keyboardLayout->switchToPreviousLayout();
|
|
|
|
}
|
|
|
|
|
2021-01-08 20:06:59 +00:00
|
|
|
bool KeyboardLayoutDBusInterface::setLayout(uint index)
|
2017-01-28 14:25:40 +00:00
|
|
|
{
|
2020-08-24 13:51:43 +00:00
|
|
|
const quint32 previousLayout = m_xkb->currentLayout();
|
2022-03-23 10:13:38 +00:00
|
|
|
if (!m_xkb->switchToLayout(index)) {
|
2021-01-09 17:41:32 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-08-24 13:51:43 +00:00
|
|
|
m_keyboardLayout->checkLayoutChange(previousLayout);
|
2017-01-28 14:25:40 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-01-08 20:06:59 +00:00
|
|
|
uint KeyboardLayoutDBusInterface::getLayout() const
|
2017-01-28 14:25:40 +00:00
|
|
|
{
|
2021-01-08 20:06:59 +00:00
|
|
|
return m_xkb->currentLayout();
|
2017-01-28 14:25:40 +00:00
|
|
|
}
|
|
|
|
|
2021-01-06 16:25:03 +00:00
|
|
|
QVector<KeyboardLayoutDBusInterface::LayoutNames> KeyboardLayoutDBusInterface::getLayoutsList() const
|
2017-01-28 14:25:40 +00:00
|
|
|
{
|
2021-01-06 16:25:03 +00:00
|
|
|
// TODO: - should be handled by layout applet itself, it has nothing to do with KWin
|
|
|
|
const QStringList displayNames = m_configGroup.readEntry("DisplayNames", QStringList());
|
|
|
|
|
|
|
|
QVector<LayoutNames> ret;
|
2021-01-08 20:06:59 +00:00
|
|
|
const int layoutsSize = m_xkb->numberOfLayouts();
|
2021-01-06 16:25:03 +00:00
|
|
|
const int displayNamesSize = displayNames.size();
|
|
|
|
for (int i = 0; i < layoutsSize; ++i) {
|
2022-03-23 10:13:38 +00:00
|
|
|
ret.append({m_xkb->layoutShortName(i), i < displayNamesSize ? displayNames.at(i) : QString(), translatedLayout(m_xkb->layoutName(i))});
|
2017-01-28 14:25:40 +00:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-01-06 16:25:03 +00:00
|
|
|
QDBusArgument &operator<<(QDBusArgument &argument, const KeyboardLayoutDBusInterface::LayoutNames &layoutNames)
|
|
|
|
{
|
|
|
|
argument.beginStructure();
|
2021-01-08 20:06:59 +00:00
|
|
|
argument << layoutNames.shortName << layoutNames.displayName << layoutNames.longName;
|
2021-01-06 16:25:03 +00:00
|
|
|
argument.endStructure();
|
|
|
|
return argument;
|
|
|
|
}
|
|
|
|
|
|
|
|
const QDBusArgument &operator>>(const QDBusArgument &argument, KeyboardLayoutDBusInterface::LayoutNames &layoutNames)
|
|
|
|
{
|
|
|
|
argument.beginStructure();
|
2021-01-08 20:06:59 +00:00
|
|
|
argument >> layoutNames.shortName >> layoutNames.displayName >> layoutNames.longName;
|
2021-01-06 16:25:03 +00:00
|
|
|
argument.endStructure();
|
|
|
|
return argument;
|
|
|
|
}
|
|
|
|
|
2017-01-14 17:06:41 +00:00
|
|
|
}
|