File indexing completed on 2024-04-28 05:30:21

0001 /*
0002     KWin - the KDE window manager
0003     This file is part of the KDE project.
0004 
0005     SPDX-FileCopyrightText: 2016, 2017 Martin Gräßlin <mgraesslin@kde.org>
0006 
0007     SPDX-License-Identifier: GPL-2.0-or-later
0008 */
0009 #pragma once
0010 
0011 #include "input_event_spy.h"
0012 #include <QList>
0013 #include <QObject>
0014 #include <memory>
0015 #include <optional>
0016 
0017 #include <KConfigGroup>
0018 #include <KSharedConfig>
0019 typedef uint32_t xkb_layout_index_t;
0020 
0021 class QAction;
0022 class QDBusArgument;
0023 
0024 namespace KWin
0025 {
0026 class Xkb;
0027 class KeyboardLayoutDBusInterface;
0028 
0029 namespace KeyboardLayoutSwitching
0030 {
0031 class Policy;
0032 }
0033 
0034 class KeyboardLayout : public QObject, public InputEventSpy
0035 {
0036     Q_OBJECT
0037 public:
0038     explicit KeyboardLayout(Xkb *xkb, const KSharedConfigPtr &config);
0039 
0040     ~KeyboardLayout() override;
0041 
0042     void init();
0043 
0044     void checkLayoutChange(uint previousLayout);
0045     void switchToNextLayout();
0046     void switchToPreviousLayout();
0047     void switchToLastUsedLayout();
0048     void resetLayout();
0049 
0050 Q_SIGNALS:
0051     void layoutChanged(uint index);
0052     void layoutsReconfigured();
0053 
0054 private Q_SLOTS:
0055     void reconfigure();
0056 
0057 private:
0058     void initDBusInterface();
0059     void notifyLayoutChange();
0060     void switchToLayout(xkb_layout_index_t index);
0061     void loadShortcuts();
0062     Xkb *m_xkb;
0063     xkb_layout_index_t m_layout = 0;
0064     KConfigGroup m_configGroup;
0065     QList<QAction *> m_layoutShortcuts;
0066     KeyboardLayoutDBusInterface *m_dbusInterface = nullptr;
0067     std::unique_ptr<KeyboardLayoutSwitching::Policy> m_policy;
0068     std::optional<uint> m_lastUsedLayout;
0069 };
0070 
0071 class KeyboardLayoutDBusInterface : public QObject
0072 {
0073     Q_OBJECT
0074     Q_CLASSINFO("D-Bus Interface", "org.kde.KeyboardLayouts")
0075 
0076 public:
0077     explicit KeyboardLayoutDBusInterface(Xkb *xkb, const KConfigGroup &configGroup, KeyboardLayout *parent);
0078     ~KeyboardLayoutDBusInterface() override;
0079 
0080     struct LayoutNames
0081     {
0082         QString shortName;
0083         QString displayName;
0084         QString longName;
0085     };
0086 
0087 public Q_SLOTS:
0088     void switchToNextLayout();
0089     void switchToPreviousLayout();
0090     bool setLayout(uint index);
0091     uint getLayout() const;
0092     QList<LayoutNames> getLayoutsList() const;
0093 
0094 Q_SIGNALS:
0095     void layoutChanged(uint index);
0096     void layoutListChanged();
0097 
0098 private:
0099     Xkb *m_xkb;
0100     const KConfigGroup &m_configGroup;
0101     KeyboardLayout *m_keyboardLayout;
0102 };
0103 
0104 QDBusArgument &operator<<(QDBusArgument &argument, const KeyboardLayoutDBusInterface::LayoutNames &layoutNames);
0105 const QDBusArgument &operator>>(const QDBusArgument &argument, KeyboardLayoutDBusInterface::LayoutNames &layoutNames);
0106 
0107 }
0108 Q_DECLARE_METATYPE(KWin::KeyboardLayoutDBusInterface::LayoutNames)