Introduce support for keyboard layout switching policies
Summary: This change introduces the initial support for keyboard layout switching policies like in the X11 session. This first change only adds support for Global and Virtual Desktop policy. This means the current layout is stored in context to the current virtual desktop. Whenever one changes the virtual desktop the previous layout is restored. If the user has not yet navigated to this virtual desktop a switch to default layout is performed. This is the first code interacting with the new Virtual Desktop API which is not based on integer ids. To fully support this the API is slightly extended. Test Plan: Added test case Reviewers: #kwin, #plasma Subscribers: plasma-devel, kwin Tags: #kwin Differential Revision: https://phabricator.kde.org/D5301master
parent
a5735e19b9
commit
bf99d9ffdd
@ -0,0 +1,119 @@
|
||||
/********************************************************************
|
||||
KWin - the KDE window manager
|
||||
This file is part of the KDE project.
|
||||
|
||||
Copyright (C) 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_switching.h"
|
||||
#include "keyboard_layout.h"
|
||||
#include "virtualdesktops.h"
|
||||
#include "xkb.h"
|
||||
|
||||
namespace KWin
|
||||
{
|
||||
|
||||
namespace KeyboardLayoutSwitching
|
||||
{
|
||||
|
||||
Policy::Policy(Xkb *xkb, KeyboardLayout *layout)
|
||||
: QObject(layout)
|
||||
, m_xkb(xkb)
|
||||
, m_layout(layout)
|
||||
{
|
||||
connect(m_layout, &KeyboardLayout::layoutsReconfigured, this, &Policy::clearCache);
|
||||
connect(m_layout, &KeyboardLayout::layoutChanged, this, &Policy::layoutChanged);
|
||||
}
|
||||
|
||||
Policy::~Policy() = default;
|
||||
|
||||
void Policy::setLayout(quint32 layout)
|
||||
{
|
||||
m_xkb->switchToLayout(layout);
|
||||
}
|
||||
|
||||
quint32 Policy::layout() const
|
||||
{
|
||||
return m_xkb->currentLayout();
|
||||
}
|
||||
|
||||
Policy *Policy::create(Xkb *xkb, KeyboardLayout *layout, const QString &policy)
|
||||
{
|
||||
if (policy.toLower() == QStringLiteral("desktop")) {
|
||||
return new VirtualDesktopPolicy(xkb, layout);
|
||||
}
|
||||
return new GlobalPolicy(xkb, layout);
|
||||
}
|
||||
|
||||
GlobalPolicy::GlobalPolicy(Xkb *xkb, KeyboardLayout *layout)
|
||||
: Policy(xkb, layout)
|
||||
{
|
||||
}
|
||||
|
||||
GlobalPolicy::~GlobalPolicy() = default;
|
||||
|
||||
VirtualDesktopPolicy::VirtualDesktopPolicy(Xkb *xkb, KeyboardLayout *layout)
|
||||
: Policy(xkb, layout)
|
||||
{
|
||||
connect(VirtualDesktopManager::self(), &VirtualDesktopManager::currentChanged, this, &VirtualDesktopPolicy::desktopChanged);
|
||||
}
|
||||
|
||||
VirtualDesktopPolicy::~VirtualDesktopPolicy() = default;
|
||||
|
||||
void VirtualDesktopPolicy::clearCache()
|
||||
{
|
||||
m_layouts.clear();
|
||||
}
|
||||
|
||||
void VirtualDesktopPolicy::desktopChanged()
|
||||
{
|
||||
auto d = VirtualDesktopManager::self()->currentDesktop();
|
||||
if (!d) {
|
||||
return;
|
||||
}
|
||||
auto it = m_layouts.constFind(d);
|
||||
if (it == m_layouts.constEnd()) {
|
||||
// new desktop - go to default;
|
||||
setLayout(0);
|
||||
} else {
|
||||
setLayout(it.value());
|
||||
}
|
||||
}
|
||||
|
||||
void VirtualDesktopPolicy::layoutChanged()
|
||||
{
|
||||
auto d = VirtualDesktopManager::self()->currentDesktop();
|
||||
if (!d) {
|
||||
return;
|
||||
}
|
||||
auto it = m_layouts.find(d);
|
||||
const auto l = layout();
|
||||
if (it == m_layouts.constEnd()) {
|
||||
m_layouts.insert(d, l);
|
||||
connect(d, &VirtualDesktop::aboutToBeDestroyed, this,
|
||||
[this, d] {
|
||||
m_layouts.remove(d);
|
||||
}
|
||||
);
|
||||
} else {
|
||||
if (it.value() == l) {
|
||||
return;
|
||||
}
|
||||
it.value() = l;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,98 @@
|
||||
/********************************************************************
|
||||
KWin - the KDE window manager
|
||||
This file is part of the KDE project.
|
||||
|
||||
Copyright (C) 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/>.
|
||||
*********************************************************************/
|
||||
#ifndef KWIN_KEYBOARD_LAYOUT_SWITCHING_H
|
||||
#define KWIN_KEYBOARD_LAYOUT_SWITCHING_H
|
||||
|
||||
#include <QObject>
|
||||
#include <QHash>
|
||||
|
||||
namespace KWin
|
||||
{
|
||||
|
||||
class KeyboardLayout;
|
||||
class Xkb;
|
||||
class VirtualDesktop;
|
||||
|
||||
namespace KeyboardLayoutSwitching
|
||||
{
|
||||
|
||||
class Policy : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
virtual ~Policy();
|
||||
|
||||
virtual QString name() const = 0;
|
||||
|
||||
static Policy *create(Xkb *xkb, KeyboardLayout *layout, const QString &policy);
|
||||
|
||||
protected:
|
||||
explicit Policy(Xkb *xkb, KeyboardLayout *layout);
|
||||
virtual void clearCache() = 0;
|
||||
virtual void layoutChanged() = 0;
|
||||
|
||||
void setLayout(quint32 layout);
|
||||
quint32 layout() const;
|
||||
|
||||
private:
|
||||
Xkb *m_xkb;
|
||||
KeyboardLayout *m_layout;
|
||||
};
|
||||
|
||||
class GlobalPolicy : public Policy
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit GlobalPolicy(Xkb *xkb, KeyboardLayout *layout);
|
||||
~GlobalPolicy() override;
|
||||
|
||||
QString name() const override {
|
||||
return QStringLiteral("Global");
|
||||
}
|
||||
|
||||
protected:
|
||||
void clearCache() override {}
|
||||
void layoutChanged() override {}
|
||||
};
|
||||
|
||||
class VirtualDesktopPolicy : public Policy
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit VirtualDesktopPolicy(Xkb *xkb, KeyboardLayout *layout);
|
||||
~VirtualDesktopPolicy() override;
|
||||
|
||||
QString name() const override {
|
||||
return QStringLiteral("Desktop");
|
||||
}
|
||||
|
||||
protected:
|
||||
void clearCache() override;
|
||||
void layoutChanged() override;
|
||||
|
||||
private:
|
||||
void desktopChanged();
|
||||
QHash<VirtualDesktop *, quint32> m_layouts;
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue