From 260fa665a2e0197c3988e8f3be9ffabdc2d952e5 Mon Sep 17 00:00:00 2001 From: Andrey Butirsky Date: Fri, 8 Jan 2021 23:06:59 +0300 Subject: [PATCH] impr: keyboard layout DBus API: rework to index based The base handle for layouts in libxkbcommon is an index. Let's follow this notion in our API to set/get layout, instead of using it's name as an ID. On the way, do cleanup. Following methods are removed as not needed any more: - Xkb::layoutShortNames() - Xkb::layoutNames() --- keyboard_layout.cpp | 40 +++++++++++++--------------------------- keyboard_layout.h | 7 +++---- xkb.cpp | 30 ++++++++++-------------------- xkb.h | 5 ++--- 4 files changed, 28 insertions(+), 54 deletions(-) diff --git a/keyboard_layout.cpp b/keyboard_layout.cpp index 16b13330cd..2f1fbd64f1 100644 --- a/keyboard_layout.cpp +++ b/keyboard_layout.cpp @@ -74,7 +74,7 @@ void KeyboardLayout::initDBusInterface() m_dbusInterface = new KeyboardLayoutDBusInterface(m_xkb, m_configGroup, this); connect(this, &KeyboardLayout::layoutChanged, m_dbusInterface, [this] { - emit m_dbusInterface->layoutChanged(m_xkb->layoutName()); + emit m_dbusInterface->layoutChanged(m_xkb->currentLayout()); } ); // TODO: the signal might be emitted even if the list didn't change @@ -131,11 +131,11 @@ void KeyboardLayout::loadShortcuts() { qDeleteAll(m_layoutShortcuts); m_layoutShortcuts.clear(); - const auto layouts = m_xkb->layoutNames(); const QString componentName = QStringLiteral("KDE Keyboard Layout Switcher"); - for (auto it = layouts.begin(); it != layouts.end(); it++) { + const quint32 count = m_xkb->numberOfLayouts(); + for (uint i = 0; i < count ; ++i) { // layout name is translated in the action name in keyboard kcm! - const QString action = QStringLiteral("Switch keyboard layout to %1").arg(translatedLayout(it.value())); + const QString action = QStringLiteral("Switch keyboard layout to %1").arg( translatedLayout(m_xkb->layoutName(i)) ); const auto shortcuts = KGlobalAccel::self()->globalShortcut(componentName, action); if (shortcuts.isEmpty()) { continue; @@ -144,7 +144,7 @@ void KeyboardLayout::loadShortcuts() a->setObjectName(action); a->setProperty("componentName", componentName); connect(a, &QAction::triggered, this, - std::bind(&KeyboardLayout::switchToLayout, this, it.key())); + std::bind(&KeyboardLayout::switchToLayout, this, i)); KGlobalAccel::self()->setShortcut(a, shortcuts, KGlobalAccel::Autoloading); m_layoutShortcuts << a; } @@ -210,27 +210,17 @@ void KeyboardLayoutDBusInterface::switchToPreviousLayout() m_keyboardLayout->switchToPreviousLayout(); } -bool KeyboardLayoutDBusInterface::setLayout(const QString &layout) +bool KeyboardLayoutDBusInterface::setLayout(uint index) { - const auto layouts = m_xkb->layoutNames(); - auto it = layouts.begin(); - for (; it !=layouts.end(); it++) { - if (it.value() == layout) { - break; - } - } - if (it == layouts.end()) { - return false; - } const quint32 previousLayout = m_xkb->currentLayout(); - m_xkb->switchToLayout(it.key()); + m_xkb->switchToLayout(index); m_keyboardLayout->checkLayoutChange(previousLayout); return true; } -QString KeyboardLayoutDBusInterface::getLayout() const +uint KeyboardLayoutDBusInterface::getLayout() const { - return m_xkb->layoutName(); + return m_xkb->currentLayout(); } QString KeyboardLayoutDBusInterface::getLayoutLongName() const @@ -240,18 +230,14 @@ QString KeyboardLayoutDBusInterface::getLayoutLongName() const QVector KeyboardLayoutDBusInterface::getLayoutsList() const { - const auto layouts = m_xkb->layoutNames(); - const QStringList &shortNames = m_xkb->layoutShortNames(); - // TODO: - should be handled by layout applet itself, it has nothing to do with KWin const QStringList displayNames = m_configGroup.readEntry("DisplayNames", QStringList()); QVector ret; - const int layoutsSize = layouts.size(); + const int layoutsSize = m_xkb->numberOfLayouts(); const int displayNamesSize = displayNames.size(); for (int i = 0; i < layoutsSize; ++i) { - const QString &id = layouts[i]; - ret.append( {id, shortNames.at(i), i < displayNamesSize ? displayNames.at(i) : QString(), translatedLayout(id)} ); + ret.append( {m_xkb->layoutShortName(i), i < displayNamesSize ? displayNames.at(i) : QString(), translatedLayout(m_xkb->layoutName(i))} ); } return ret; } @@ -259,7 +245,7 @@ QVector KeyboardLayoutDBusInterface::g QDBusArgument &operator<<(QDBusArgument &argument, const KeyboardLayoutDBusInterface::LayoutNames &layoutNames) { argument.beginStructure(); - argument << layoutNames.id << layoutNames.shortName << layoutNames.displayName << layoutNames.longName; + argument << layoutNames.shortName << layoutNames.displayName << layoutNames.longName; argument.endStructure(); return argument; } @@ -267,7 +253,7 @@ QDBusArgument &operator<<(QDBusArgument &argument, const KeyboardLayoutDBusInter const QDBusArgument &operator>>(const QDBusArgument &argument, KeyboardLayoutDBusInterface::LayoutNames &layoutNames) { argument.beginStructure(); - argument >> layoutNames.id >> layoutNames.shortName >> layoutNames.displayName >> layoutNames.longName; + argument >> layoutNames.shortName >> layoutNames.displayName >> layoutNames.longName; argument.endStructure(); return argument; } diff --git a/keyboard_layout.h b/keyboard_layout.h index e35ccd651a..d8a1611a06 100644 --- a/keyboard_layout.h +++ b/keyboard_layout.h @@ -76,7 +76,6 @@ public: struct LayoutNames { - QString id; QString shortName; QString displayName; QString longName; @@ -85,13 +84,13 @@ public: public Q_SLOTS: void switchToNextLayout(); void switchToPreviousLayout(); - bool setLayout(const QString &layout); - QString getLayout() const; + bool setLayout(uint index); + uint getLayout() const; QString getLayoutLongName() const; QVector getLayoutsList() const; Q_SIGNALS: - void layoutChanged(QString layout); + void layoutChanged(uint index); void layoutListChanged(); private: diff --git a/xkb.cpp b/xkb.cpp index 8eba7f1025..c1e4dc66b5 100644 --- a/xkb.cpp +++ b/xkb.cpp @@ -390,32 +390,22 @@ void Xkb::forwardModifiers() m_currentLayout); } +QString Xkb::layoutName(xkb_layout_index_t index) const +{ + if (!m_keymap) { + return QString{}; + } + return QString::fromLocal8Bit(xkb_keymap_layout_get_name(m_keymap, index)); +} + QString Xkb::layoutName() const { return layoutName(m_currentLayout); } -const QStringList &Xkb::layoutShortNames() const +const QString &Xkb::layoutShortName(int index) const { - return m_layoutList; -} - -QString Xkb::layoutName(xkb_layout_index_t layout) const -{ - if (!m_keymap) { - return QString{}; - } - return QString::fromLocal8Bit(xkb_keymap_layout_get_name(m_keymap, layout)); -} - -QMap Xkb::layoutNames() const -{ - QMap layouts; - const auto size = m_keymap ? xkb_keymap_num_layouts(m_keymap) : 0u; - for (xkb_layout_index_t i = 0; i < size; i++) { - layouts.insert(i, layoutName(i)); - } - return layouts; + return m_layoutList.at(index); } void Xkb::updateConsumedModifiers(uint32_t key) diff --git a/xkb.h b/xkb.h index b118e2d259..69b4d96d88 100644 --- a/xkb.h +++ b/xkb.h @@ -86,9 +86,9 @@ public: quint32 currentLayout() const { return m_currentLayout; } + QString layoutName(xkb_layout_index_t index) const; QString layoutName() const; - const QStringList &layoutShortNames() const; - QMap layoutNames() const; + const QString &layoutShortName(int index) const; quint32 numberOfLayouts() const; /** @@ -109,7 +109,6 @@ private: void createKeymapFile(); void updateModifiers(); void updateConsumedModifiers(uint32_t key); - QString layoutName(xkb_layout_index_t layout) const; xkb_context *m_context; xkb_keymap *m_keymap; QStringList m_layoutList;