kwin/keyboard_layout.cpp

116 lines
3.4 KiB
C++
Raw Normal View History

/********************************************************************
KWin - the KDE window manager
This file is part of the KDE project.
Copyright (C) 2016, 2017 Martin Gräßlin <mgraesslin@kde.org>
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_input.h"
#include "input_event.h"
#include "main.h"
#include "platform.h"
#include <KGlobalAccel>
#include <QAction>
#include <QDBusConnection>
#include <QDBusMessage>
#include <QDBusPendingCall>
namespace KWin
{
KeyboardLayout::KeyboardLayout(Xkb *xkb)
: QObject()
, m_xkb(xkb)
{
}
KeyboardLayout::~KeyboardLayout() = default;
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,
[this] {
m_xkb->switchToNextLayout();
checkLayoutChange();
}
);
QDBusConnection::sessionBus().connect(QString(),
QStringLiteral("/Layouts"),
QStringLiteral("org.kde.keyboard"),
QStringLiteral("reloadConfig"),
this,
SLOT(reconfigure()));
reconfigure();
}
void KeyboardLayout::reconfigure()
{
m_xkb->reconfigure();
resetLayout();
}
void KeyboardLayout::resetLayout()
{
m_layout = m_xkb->currentLayout();
}
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();
}
void KeyboardLayout::notifyLayoutChange()
{
// notify OSD service about the new layout
if (!kwinApp()->usesLibinput()) {
return;
}
// only if kwin is in charge of keyboard input
QDBusMessage msg = QDBusMessage::createMethodCall(
QStringLiteral("org.kde.plasmashell"),
QStringLiteral("/org/kde/osdService"),
QStringLiteral("org.kde.osdService"),
QStringLiteral("kbdLayoutChanged"));
msg << m_xkb->layoutName();
QDBusConnection::sessionBus().asyncCall(msg);
}
}