File indexing completed on 2024-02-25 17:23:43

0001 /*
0002     KWin - the KDE window manager
0003     This file is part of the KDE project.
0004 
0005     SPDX-FileCopyrightText: 2017 Martin Gräßlin <mgraesslin@kde.org>
0006 
0007     SPDX-License-Identifier: GPL-2.0-or-later
0008 */
0009 #pragma once
0010 
0011 #include <KConfigGroup>
0012 #include <QHash>
0013 #include <QObject>
0014 #include <memory>
0015 
0016 namespace KWin
0017 {
0018 
0019 class Window;
0020 class KeyboardLayout;
0021 class Xkb;
0022 class VirtualDesktop;
0023 
0024 namespace KeyboardLayoutSwitching
0025 {
0026 
0027 class Policy : public QObject
0028 {
0029     Q_OBJECT
0030 public:
0031     ~Policy() override;
0032 
0033     virtual QString name() const = 0;
0034 
0035     static std::unique_ptr<Policy> create(Xkb *xkb, KeyboardLayout *layout, const KConfigGroup &config, const QString &policy);
0036 
0037 protected:
0038     explicit Policy(Xkb *xkb, KeyboardLayout *layout, const KConfigGroup &config = KConfigGroup());
0039     virtual void clearCache() = 0;
0040     virtual void layoutChanged(uint index) = 0;
0041 
0042     void setLayout(uint index);
0043 
0044     KConfigGroup m_config;
0045     virtual const QString defaultLayoutEntryKey() const;
0046     void clearLayouts();
0047 
0048     static const char defaultLayoutEntryKeyPrefix[];
0049     Xkb *m_xkb;
0050 
0051 private:
0052     KeyboardLayout *m_layout;
0053 };
0054 
0055 class GlobalPolicy : public Policy
0056 {
0057     Q_OBJECT
0058 public:
0059     explicit GlobalPolicy(Xkb *xkb, KeyboardLayout *layout, const KConfigGroup &config);
0060     ~GlobalPolicy() override;
0061 
0062     QString name() const override
0063     {
0064         return QStringLiteral("Global");
0065     }
0066 
0067 protected:
0068     void clearCache() override
0069     {
0070     }
0071     void layoutChanged(uint index) override
0072     {
0073     }
0074 
0075 private:
0076     const QString defaultLayoutEntryKey() const override;
0077 };
0078 
0079 class VirtualDesktopPolicy : public Policy
0080 {
0081     Q_OBJECT
0082 public:
0083     explicit VirtualDesktopPolicy(Xkb *xkb, KeyboardLayout *layout, const KConfigGroup &config);
0084     ~VirtualDesktopPolicy() override;
0085 
0086     QString name() const override
0087     {
0088         return QStringLiteral("Desktop");
0089     }
0090 
0091 protected:
0092     void clearCache() override;
0093     void layoutChanged(uint index) override;
0094 
0095 private:
0096     void desktopChanged();
0097     QHash<VirtualDesktop *, quint32> m_layouts;
0098 };
0099 
0100 class WindowPolicy : public Policy
0101 {
0102     Q_OBJECT
0103 public:
0104     explicit WindowPolicy(Xkb *xkb, KeyboardLayout *layout);
0105     ~WindowPolicy() override;
0106 
0107     QString name() const override
0108     {
0109         return QStringLiteral("Window");
0110     }
0111 
0112 protected:
0113     void clearCache() override;
0114     void layoutChanged(uint index) override;
0115 
0116 private:
0117     QHash<Window *, quint32> m_layouts;
0118 };
0119 
0120 class ApplicationPolicy : public Policy
0121 {
0122     Q_OBJECT
0123 public:
0124     explicit ApplicationPolicy(Xkb *xkb, KeyboardLayout *layout, const KConfigGroup &config);
0125     ~ApplicationPolicy() override;
0126 
0127     QString name() const override
0128     {
0129         return QStringLiteral("WinClass");
0130     }
0131 
0132 protected:
0133     void clearCache() override;
0134     void layoutChanged(uint index) override;
0135 
0136 private:
0137     void windowActivated(Window *window);
0138     QHash<Window *, quint32> m_layouts;
0139     QHash<QString, quint32> m_layoutsRestored;
0140 };
0141 
0142 }
0143 }