diff --git a/keyboard_input.cpp b/keyboard_input.cpp index 36987a3d1..f3d657403 100644 --- a/keyboard_input.cpp +++ b/keyboard_input.cpp @@ -1,722 +1,766 @@ /******************************************************************** KWin - the KDE window manager This file is part of the KDE project. Copyright (C) 2013, 2016 Martin Gräßlin 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 . *********************************************************************/ #include "keyboard_input.h" #include "input_event.h" #include "input_event_spy.h" #include "keyboard_layout.h" #include "abstract_client.h" #include "options.h" #include "utils.h" #include "screenlockerwatcher.h" #include "toplevel.h" #include "wayland_server.h" #include "workspace.h" // KWayland #include #include //screenlocker #include // Frameworks #include #include // Qt #include #include #include #include #include // xkbcommon #include #include #include // system #include #include Q_LOGGING_CATEGORY(KWIN_XKB, "kwin_xkbcommon", QtCriticalMsg) namespace KWin { static void xkbLogHandler(xkb_context *context, xkb_log_level priority, const char *format, va_list args) { Q_UNUSED(context) char buf[1024]; if (std::vsnprintf(buf, 1023, format, args) <= 0) { return; } switch (priority) { case XKB_LOG_LEVEL_DEBUG: qCDebug(KWIN_XKB) << "XKB:" << buf; break; case XKB_LOG_LEVEL_INFO: #if (QT_VERSION >= QT_VERSION_CHECK(5, 5, 0)) qCInfo(KWIN_XKB) << "XKB:" << buf; #endif break; case XKB_LOG_LEVEL_WARNING: qCWarning(KWIN_XKB) << "XKB:" << buf; break; case XKB_LOG_LEVEL_ERROR: case XKB_LOG_LEVEL_CRITICAL: default: qCCritical(KWIN_XKB) << "XKB:" << buf; break; } } Xkb::Xkb(InputRedirection *input) : m_input(input) , m_context(xkb_context_new(static_cast(0))) , m_keymap(NULL) , m_state(NULL) , m_shiftModifier(0) , m_capsModifier(0) , m_controlModifier(0) , m_altModifier(0) , m_metaModifier(0) , m_numLock(0) , m_capsLock(0) , m_scrollLock(0) , m_modifiers(Qt::NoModifier) , m_consumedModifiers(Qt::NoModifier) , m_keysym(XKB_KEY_NoSymbol) , m_leds() { qRegisterMetaType(); if (!m_context) { qCDebug(KWIN_XKB) << "Could not create xkb context"; } else { xkb_context_set_log_level(m_context, XKB_LOG_LEVEL_DEBUG); xkb_context_set_log_fn(m_context, &xkbLogHandler); // get locale as described in xkbcommon doc // cannot use QLocale as it drops the modifier part QByteArray locale = qgetenv("LC_ALL"); if (locale.isEmpty()) { locale = qgetenv("LC_CTYPE"); } if (locale.isEmpty()) { locale = qgetenv("LANG"); } if (locale.isEmpty()) { locale = QByteArrayLiteral("C"); } m_compose.table = xkb_compose_table_new_from_locale(m_context, locale.constData(), XKB_COMPOSE_COMPILE_NO_FLAGS); if (m_compose.table) { m_compose.state = xkb_compose_state_new(m_compose.table, XKB_COMPOSE_STATE_NO_FLAGS); } } auto resetModOnlyShortcut = [this] { m_modOnlyShortcut.modifier = Qt::NoModifier; }; QObject::connect(m_input, &InputRedirection::pointerButtonStateChanged, resetModOnlyShortcut); QObject::connect(m_input, &InputRedirection::pointerAxisChanged, resetModOnlyShortcut); QObject::connect(ScreenLockerWatcher::self(), &ScreenLockerWatcher::locked, m_input, resetModOnlyShortcut); } Xkb::~Xkb() { xkb_compose_state_unref(m_compose.state); xkb_compose_table_unref(m_compose.table); xkb_state_unref(m_state); xkb_keymap_unref(m_keymap); xkb_context_unref(m_context); } void Xkb::reconfigure() { if (!m_context) { return; } xkb_keymap *keymap = nullptr; if (!qEnvironmentVariableIsSet("KWIN_XKB_DEFAULT_KEYMAP")) { keymap = loadKeymapFromConfig(); } if (!keymap) { qCDebug(KWIN_XKB) << "Could not create xkb keymap from configuration"; keymap = loadDefaultKeymap(); } if (keymap) { updateKeymap(keymap); } else { qCDebug(KWIN_XKB) << "Could not create default xkb keymap"; } } xkb_keymap *Xkb::loadKeymapFromConfig() { // load config const KConfigGroup config = KSharedConfig::openConfig(QStringLiteral("kxkbrc"), KConfig::NoGlobals)->group("Layout"); const QByteArray model = config.readEntry("Model", "pc104").toLocal8Bit(); const QByteArray layout = config.readEntry("LayoutList", "").toLocal8Bit(); const QByteArray options = config.readEntry("Options", "").toLocal8Bit(); xkb_rule_names ruleNames = { .rules = nullptr, .model = model.constData(), .layout = layout.constData(), .variant = nullptr, .options = options.constData() }; return xkb_keymap_new_from_names(m_context, &ruleNames, XKB_KEYMAP_COMPILE_NO_FLAGS); } xkb_keymap *Xkb::loadDefaultKeymap() { return xkb_keymap_new_from_names(m_context, nullptr, XKB_KEYMAP_COMPILE_NO_FLAGS); } void Xkb::installKeymap(int fd, uint32_t size) { if (!m_context) { return; } char *map = reinterpret_cast(mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0)); if (map == MAP_FAILED) { return; } xkb_keymap *keymap = xkb_keymap_new_from_string(m_context, map, XKB_KEYMAP_FORMAT_TEXT_V1, XKB_MAP_COMPILE_PLACEHOLDER); munmap(map, size); if (!keymap) { qCDebug(KWIN_XKB) << "Could not map keymap from file"; return; } updateKeymap(keymap); } void Xkb::updateKeymap(xkb_keymap *keymap) { Q_ASSERT(keymap); xkb_state *state = xkb_state_new(keymap); if (!state) { qCDebug(KWIN_XKB) << "Could not create XKB state"; xkb_keymap_unref(keymap); return; } // now release the old ones xkb_state_unref(m_state); xkb_keymap_unref(m_keymap); m_keymap = keymap; m_state = state; m_shiftModifier = xkb_keymap_mod_get_index(m_keymap, XKB_MOD_NAME_SHIFT); m_capsModifier = xkb_keymap_mod_get_index(m_keymap, XKB_MOD_NAME_CAPS); m_controlModifier = xkb_keymap_mod_get_index(m_keymap, XKB_MOD_NAME_CTRL); m_altModifier = xkb_keymap_mod_get_index(m_keymap, XKB_MOD_NAME_ALT); m_metaModifier = xkb_keymap_mod_get_index(m_keymap, XKB_MOD_NAME_LOGO); m_numLock = xkb_keymap_led_get_index(m_keymap, XKB_LED_NAME_NUM); m_capsLock = xkb_keymap_led_get_index(m_keymap, XKB_LED_NAME_CAPS); m_scrollLock = xkb_keymap_led_get_index(m_keymap, XKB_LED_NAME_SCROLL); m_currentLayout = xkb_state_serialize_layout(m_state, XKB_STATE_LAYOUT_EFFECTIVE); createKeymapFile(); } void Xkb::createKeymapFile() { if (!waylandServer()) { return; } // TODO: uninstall keymap on server? if (!m_keymap) { return; } ScopedCPointer keymapString(xkb_keymap_get_as_string(m_keymap, XKB_KEYMAP_FORMAT_TEXT_V1)); if (keymapString.isNull()) { return; } const uint size = qstrlen(keymapString.data()) + 1; QTemporaryFile *tmp = new QTemporaryFile(m_input); if (!tmp->open()) { delete tmp; return; } unlink(tmp->fileName().toUtf8().constData()); if (!tmp->resize(size)) { delete tmp; return; } uchar *address = tmp->map(0, size); if (!address) { return; } if (qstrncpy(reinterpret_cast(address), keymapString.data(), size) == nullptr) { delete tmp; return; } waylandServer()->seat()->setKeymap(tmp->handle(), size); } void Xkb::updateModifiers(uint32_t modsDepressed, uint32_t modsLatched, uint32_t modsLocked, uint32_t group) { if (!m_keymap || !m_state) { return; } xkb_state_update_mask(m_state, modsDepressed, modsLatched, modsLocked, 0, 0, group); updateModifiers(); } void Xkb::updateKey(uint32_t key, InputRedirection::KeyboardKeyState state) { if (!m_keymap || !m_state) { return; } const auto oldMods = modifiersRelevantForGlobalShortcuts(); xkb_state_update_key(m_state, key + 8, static_cast(state)); if (state == InputRedirection::KeyboardKeyPressed) { const auto sym = toKeysym(key); if (m_compose.state && xkb_compose_state_feed(m_compose.state, sym) == XKB_COMPOSE_FEED_ACCEPTED) { switch (xkb_compose_state_get_status(m_compose.state)) { case XKB_COMPOSE_NOTHING: m_keysym = sym; break; case XKB_COMPOSE_COMPOSED: m_keysym = xkb_compose_state_get_one_sym(m_compose.state); break; default: m_keysym = XKB_KEY_NoSymbol; break; } } else { m_keysym = sym; } } updateModifiers(); updateConsumedModifiers(key); if (state == InputRedirection::KeyboardKeyPressed) { m_modOnlyShortcut.pressCount++; if (m_modOnlyShortcut.pressCount == 1 && !ScreenLockerWatcher::self()->isLocked() && oldMods == Qt::NoModifier && m_input->qtButtonStates() == Qt::NoButton) { m_modOnlyShortcut.modifier = Qt::KeyboardModifier(int(modifiersRelevantForGlobalShortcuts())); } else { m_modOnlyShortcut.modifier = Qt::NoModifier; } } else { m_modOnlyShortcut.pressCount--; if (m_modOnlyShortcut.pressCount == 0 && modifiersRelevantForGlobalShortcuts() == Qt::NoModifier && !workspace()->globalShortcutsDisabled()) { if (m_modOnlyShortcut.modifier != Qt::NoModifier) { const auto list = options->modifierOnlyDBusShortcut(m_modOnlyShortcut.modifier); if (list.size() >= 4) { auto call = QDBusMessage::createMethodCall(list.at(0), list.at(1), list.at(2), list.at(3)); QVariantList args; for (int i = 4; i < list.size(); ++i) { args << list.at(i); } call.setArguments(args); QDBusConnection::sessionBus().asyncCall(call); } } } m_modOnlyShortcut.modifier = Qt::NoModifier; } } void Xkb::updateModifiers() { Qt::KeyboardModifiers mods = Qt::NoModifier; if (xkb_state_mod_index_is_active(m_state, m_shiftModifier, XKB_STATE_MODS_EFFECTIVE) == 1 || xkb_state_mod_index_is_active(m_state, m_capsModifier, XKB_STATE_MODS_EFFECTIVE) == 1) { mods |= Qt::ShiftModifier; } if (xkb_state_mod_index_is_active(m_state, m_altModifier, XKB_STATE_MODS_EFFECTIVE) == 1) { mods |= Qt::AltModifier; } if (xkb_state_mod_index_is_active(m_state, m_controlModifier, XKB_STATE_MODS_EFFECTIVE) == 1) { mods |= Qt::ControlModifier; } if (xkb_state_mod_index_is_active(m_state, m_metaModifier, XKB_STATE_MODS_EFFECTIVE) == 1) { mods |= Qt::MetaModifier; } m_modifiers = mods; // update LEDs LEDs leds; if (xkb_state_led_index_is_active(m_state, m_numLock) == 1) { leds = leds | LED::NumLock; } if (xkb_state_led_index_is_active(m_state, m_capsLock) == 1) { leds = leds | LED::CapsLock; } if (xkb_state_led_index_is_active(m_state, m_scrollLock) == 1) { leds = leds | LED::ScrollLock; } if (m_leds != leds) { m_leds = leds; emit m_input->keyboard()->ledsChanged(m_leds); } const xkb_layout_index_t layout = xkb_state_serialize_layout(m_state, XKB_STATE_LAYOUT_EFFECTIVE); if (layout != m_currentLayout) { m_currentLayout = layout; } if (waylandServer()) { waylandServer()->seat()->updateKeyboardModifiers(xkb_state_serialize_mods(m_state, xkb_state_component(XKB_STATE_MODS_DEPRESSED)), xkb_state_serialize_mods(m_state, xkb_state_component(XKB_STATE_MODS_LATCHED)), xkb_state_serialize_mods(m_state, xkb_state_component(XKB_STATE_MODS_LOCKED)), layout); } } QString Xkb::layoutName() const { - return QString::fromLocal8Bit(xkb_keymap_layout_get_name(m_keymap, m_currentLayout)); + return layoutName(m_currentLayout); +} + +QString Xkb::layoutName(xkb_layout_index_t layout) const +{ + return QString::fromLocal8Bit(xkb_keymap_layout_get_name(m_keymap, layout)); +} + +QMap Xkb::layoutNames() const +{ + QMap layouts; + const auto size = xkb_keymap_num_layouts(m_keymap); + for (xkb_layout_index_t i = 0; i < size; i++) { + layouts.insert(i, layoutName(i)); + } + return layouts; } void Xkb::updateConsumedModifiers(uint32_t key) { Qt::KeyboardModifiers mods = Qt::NoModifier; if (xkb_state_mod_index_is_consumed2(m_state, key + 8, m_shiftModifier, XKB_CONSUMED_MODE_GTK) == 1) { mods |= Qt::ShiftModifier; } if (xkb_state_mod_index_is_consumed2(m_state, key + 8, m_altModifier, XKB_CONSUMED_MODE_GTK) == 1) { mods |= Qt::AltModifier; } if (xkb_state_mod_index_is_consumed2(m_state, key + 8, m_controlModifier, XKB_CONSUMED_MODE_GTK) == 1) { mods |= Qt::ControlModifier; } if (xkb_state_mod_index_is_consumed2(m_state, key + 8, m_metaModifier, XKB_CONSUMED_MODE_GTK) == 1) { mods |= Qt::MetaModifier; } m_consumedModifiers = mods; } Qt::KeyboardModifiers Xkb::modifiersRelevantForGlobalShortcuts() const { Qt::KeyboardModifiers mods = Qt::NoModifier; if (xkb_state_mod_index_is_active(m_state, m_shiftModifier, XKB_STATE_MODS_EFFECTIVE) == 1) { mods |= Qt::ShiftModifier; } if (xkb_state_mod_index_is_active(m_state, m_altModifier, XKB_STATE_MODS_EFFECTIVE) == 1) { mods |= Qt::AltModifier; } if (xkb_state_mod_index_is_active(m_state, m_controlModifier, XKB_STATE_MODS_EFFECTIVE) == 1) { mods |= Qt::ControlModifier; } if (xkb_state_mod_index_is_active(m_state, m_metaModifier, XKB_STATE_MODS_EFFECTIVE) == 1) { mods |= Qt::MetaModifier; } Qt::KeyboardModifiers consumedMods = m_consumedModifiers; if ((mods & Qt::ShiftModifier) && (consumedMods == Qt::ShiftModifier)) { // test whether current keysym is a letter // in that case the shift should be removed from the consumed modifiers again // otherwise it would not be possible to trigger e.g. Shift+W as a shortcut // see BUG: 370341 if (QChar(toQtKey(m_keysym)).isLetter()) { consumedMods = Qt::KeyboardModifiers(); } } return mods & ~consumedMods; } xkb_keysym_t Xkb::toKeysym(uint32_t key) { if (!m_state) { return XKB_KEY_NoSymbol; } return xkb_state_key_get_one_sym(m_state, key + 8); } QString Xkb::toString(xkb_keysym_t keysym) { if (!m_state || keysym == XKB_KEY_NoSymbol) { return QString(); } QByteArray byteArray(7, 0); int ok = xkb_keysym_to_utf8(keysym, byteArray.data(), byteArray.size()); if (ok == -1 || ok == 0) { return QString(); } return QString::fromUtf8(byteArray.constData()); } Qt::Key Xkb::toQtKey(xkb_keysym_t keysym) const { int key = Qt::Key_unknown; KKeyServer::symXToKeyQt(keysym, &key); return static_cast(key); } bool Xkb::shouldKeyRepeat(quint32 key) const { if (!m_keymap) { return false; } return xkb_keymap_key_repeats(m_keymap, key + 8) != 0; } void Xkb::switchToNextLayout() { if (!m_keymap || !m_state) { return; } const xkb_layout_index_t numLayouts = xkb_keymap_num_layouts(m_keymap); const xkb_layout_index_t nextLayout = (xkb_state_serialize_layout(m_state, XKB_STATE_LAYOUT_EFFECTIVE) + 1) % numLayouts; + switchToLayout(nextLayout); +} + +void Xkb::switchToPreviousLayout() +{ + if (!m_keymap || !m_state) { + return; + } + const xkb_layout_index_t previousLayout = m_currentLayout == 0 ? numberOfLayouts() - 1 : m_currentLayout -1; + switchToLayout(previousLayout); +} + +void Xkb::switchToLayout(xkb_layout_index_t layout) +{ + if (!m_keymap || !m_state) { + return; + } + if (layout >= numberOfLayouts()) { + return; + } const xkb_mod_mask_t depressed = xkb_state_serialize_mods(m_state, xkb_state_component(XKB_STATE_MODS_DEPRESSED)); const xkb_mod_mask_t latched = xkb_state_serialize_mods(m_state, xkb_state_component(XKB_STATE_MODS_LATCHED)); const xkb_mod_mask_t locked = xkb_state_serialize_mods(m_state, xkb_state_component(XKB_STATE_MODS_LOCKED)); - xkb_state_update_mask(m_state, depressed, latched, locked, 0, 0, nextLayout); + xkb_state_update_mask(m_state, depressed, latched, locked, 0, 0, layout); updateModifiers(); } +quint32 Xkb::numberOfLayouts() const +{ + if (!m_keymap) { + return 0; + } + return xkb_keymap_num_layouts(m_keymap); +} + KeyboardInputRedirection::KeyboardInputRedirection(InputRedirection *parent) : QObject(parent) , m_input(parent) , m_xkb(new Xkb(parent)) { } KeyboardInputRedirection::~KeyboardInputRedirection() = default; class KeyStateChangedSpy : public InputEventSpy { public: KeyStateChangedSpy(InputRedirection *input) : m_input(input) { } void keyEvent(KeyEvent *event) override { if (event->isAutoRepeat()) { return; } emit m_input->keyStateChanged(event->nativeScanCode(), event->type() == QEvent::KeyPress ? InputRedirection::KeyboardKeyPressed : InputRedirection::KeyboardKeyReleased); } private: InputRedirection *m_input; }; class ModifiersChangedSpy : public InputEventSpy { public: ModifiersChangedSpy(InputRedirection *input) : m_input(input) , m_modifiers() { } void keyEvent(KeyEvent *event) override { if (event->isAutoRepeat()) { return; } updateModifiers(event->modifiers()); } void updateModifiers(Qt::KeyboardModifiers mods) { if (mods == m_modifiers) { return; } emit m_input->keyboardModifiersChanged(mods, m_modifiers); m_modifiers = mods; } private: InputRedirection *m_input; Qt::KeyboardModifiers m_modifiers; }; void KeyboardInputRedirection::init() { Q_ASSERT(!m_inited); m_inited = true; m_input->installInputEventSpy(new KeyStateChangedSpy(m_input)); m_modifiersChangedSpy = new ModifiersChangedSpy(m_input); m_input->installInputEventSpy(m_modifiersChangedSpy); m_keyboardLayout = new KeyboardLayout(m_xkb.data()); + m_keyboardLayout->setConfig(KSharedConfig::openConfig(QStringLiteral("kxkbrc"), KConfig::NoGlobals)); m_keyboardLayout->init(); m_input->installInputEventSpy(m_keyboardLayout); // setup key repeat m_keyRepeat.timer = new QTimer(this); connect(m_keyRepeat.timer, &QTimer::timeout, this, [this] { if (waylandServer()->seat()->keyRepeatRate() != 0) { m_keyRepeat.timer->setInterval(1000 / waylandServer()->seat()->keyRepeatRate()); } // TODO: better time processKey(m_keyRepeat.key, InputRedirection::KeyboardKeyAutoRepeat, m_keyRepeat.time); } ); connect(workspace(), &QObject::destroyed, this, [this] { m_inited = false; }); connect(waylandServer(), &QObject::destroyed, this, [this] { m_inited = false; }); connect(workspace(), &Workspace::clientActivated, this, [this] { disconnect(m_activeClientSurfaceChangedConnection); if (auto c = workspace()->activeClient()) { m_activeClientSurfaceChangedConnection = connect(c, &Toplevel::surfaceChanged, this, &KeyboardInputRedirection::update); } else { m_activeClientSurfaceChangedConnection = QMetaObject::Connection(); } update(); } ); if (waylandServer()->hasScreenLockerIntegration()) { connect(ScreenLocker::KSldApp::self(), &ScreenLocker::KSldApp::lockStateChanged, this, &KeyboardInputRedirection::update); } } void KeyboardInputRedirection::update() { if (!m_inited) { return; } auto seat = waylandServer()->seat(); // TODO: this needs better integration Toplevel *found = nullptr; if (waylandServer()->isScreenLocked()) { const ToplevelList &stacking = Workspace::self()->stackingOrder(); if (!stacking.isEmpty()) { auto it = stacking.end(); do { --it; Toplevel *t = (*it); if (t->isDeleted()) { // a deleted window doesn't get mouse events continue; } if (!t->isLockScreen()) { continue; } if (!t->readyForPainting()) { continue; } found = t; break; } while (it != stacking.begin()); } } else if (!input()->isSelectingWindow() && !input()->isBreakingPointerConstraints()) { found = workspace()->activeClient(); } if (found && found->surface()) { if (found->surface() != seat->focusedKeyboardSurface()) { seat->setFocusedKeyboardSurface(found->surface()); auto newKeyboard = seat->focusedKeyboard(); if (newKeyboard && newKeyboard->client() == waylandServer()->xWaylandConnection()) { // focus passed to an XWayland surface const auto selection = seat->selection(); auto xclipboard = waylandServer()->xclipboardSyncDataDevice(); if (xclipboard && selection != xclipboard.data()) { if (selection) { xclipboard->sendSelection(selection); } else { xclipboard->sendClearSelection(); } } } } } else { seat->setFocusedKeyboardSurface(nullptr); } } void KeyboardInputRedirection::processKey(uint32_t key, InputRedirection::KeyboardKeyState state, uint32_t time, LibInput::Device *device) { if (!m_inited) { return; } QEvent::Type type; bool autoRepeat = false; switch (state) { case InputRedirection::KeyboardKeyAutoRepeat: autoRepeat = true; // fall through case InputRedirection::KeyboardKeyPressed: type = QEvent::KeyPress; break; case InputRedirection::KeyboardKeyReleased: type = QEvent::KeyRelease; break; default: Q_UNREACHABLE(); } if (!autoRepeat) { m_xkb->updateKey(key, state); } const xkb_keysym_t keySym = m_xkb->currentKeysym(); KeyEvent event(type, m_xkb->toQtKey(keySym), m_xkb->modifiers(), key, keySym, m_xkb->toString(keySym), autoRepeat, time, device); event.setModifiersRelevantForGlobalShortcuts(m_xkb->modifiersRelevantForGlobalShortcuts()); if (state == InputRedirection::KeyboardKeyPressed) { if (m_xkb->shouldKeyRepeat(key) && waylandServer()->seat()->keyRepeatDelay() != 0) { m_keyRepeat.timer->setInterval(waylandServer()->seat()->keyRepeatDelay()); m_keyRepeat.key = key; m_keyRepeat.time = time; m_keyRepeat.timer->start(); } } else if (state == InputRedirection::KeyboardKeyReleased) { if (key == m_keyRepeat.key) { m_keyRepeat.timer->stop(); } } m_input->processSpies(std::bind(&InputEventSpy::keyEvent, std::placeholders::_1, &event)); m_input->processFilters(std::bind(&InputEventFilter::keyEvent, std::placeholders::_1, &event)); } void KeyboardInputRedirection::processModifiers(uint32_t modsDepressed, uint32_t modsLatched, uint32_t modsLocked, uint32_t group) { if (!m_inited) { return; } // TODO: send to proper Client and also send when active Client changes m_xkb->updateModifiers(modsDepressed, modsLatched, modsLocked, group); m_modifiersChangedSpy->updateModifiers(modifiers()); m_keyboardLayout->checkLayoutChange(); } void KeyboardInputRedirection::processKeymapChange(int fd, uint32_t size) { if (!m_inited) { return; } // TODO: should we pass the keymap to our Clients? Or only to the currently active one and update m_xkb->installKeymap(fd, size); m_keyboardLayout->resetLayout(); } } diff --git a/keyboard_input.h b/keyboard_input.h index e9a31bc79..25ed56075 100644 --- a/keyboard_input.h +++ b/keyboard_input.h @@ -1,197 +1,203 @@ /******************************************************************** KWin - the KDE window manager This file is part of the KDE project. Copyright (C) 2013, 2016 Martin Gräßlin 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 . *********************************************************************/ #ifndef KWIN_KEYBOARD_INPUT_H #define KWIN_KEYBOARD_INPUT_H #include "input.h" #include #include #include #include Q_DECLARE_LOGGING_CATEGORY(KWIN_XKB) class QWindow; struct xkb_context; struct xkb_keymap; struct xkb_state; struct xkb_compose_table; struct xkb_compose_state; typedef uint32_t xkb_mod_index_t; typedef uint32_t xkb_led_index_t; typedef uint32_t xkb_keysym_t; +typedef uint32_t xkb_layout_index_t; namespace KWin { class InputRedirection; class KeyboardLayout; class ModifiersChangedSpy; class Toplevel; namespace LibInput { class Device; } class KWIN_EXPORT Xkb { public: Xkb(InputRedirection *input); ~Xkb(); void reconfigure(); void installKeymap(int fd, uint32_t size); void updateModifiers(uint32_t modsDepressed, uint32_t modsLatched, uint32_t modsLocked, uint32_t group); void updateKey(uint32_t key, InputRedirection::KeyboardKeyState state); xkb_keysym_t toKeysym(uint32_t key); xkb_keysym_t currentKeysym() const { return m_keysym; } QString toString(xkb_keysym_t keysym); Qt::Key toQtKey(xkb_keysym_t keysym) const; Qt::KeyboardModifiers modifiers() const; Qt::KeyboardModifiers modifiersRelevantForGlobalShortcuts() const; bool shouldKeyRepeat(quint32 key) const; void switchToNextLayout(); + void switchToPreviousLayout(); + void switchToLayout(xkb_layout_index_t layout); enum class LED { NumLock = 1 << 0, CapsLock = 1 << 1, ScrollLock = 1 << 2 }; Q_DECLARE_FLAGS(LEDs, LED) LEDs leds() const { return m_leds; } xkb_keymap *keymap() const { return m_keymap; } xkb_state *state() const { return m_state; } quint32 currentLayout() const { return m_currentLayout; } QString layoutName() const; + QMap layoutNames() const; + quint32 numberOfLayouts() const; private: xkb_keymap *loadKeymapFromConfig(); xkb_keymap *loadDefaultKeymap(); void updateKeymap(xkb_keymap *keymap); void createKeymapFile(); void updateModifiers(); void updateConsumedModifiers(uint32_t key); + QString layoutName(xkb_layout_index_t layout) const; InputRedirection *m_input; xkb_context *m_context; xkb_keymap *m_keymap; xkb_state *m_state; xkb_mod_index_t m_shiftModifier; xkb_mod_index_t m_capsModifier; xkb_mod_index_t m_controlModifier; xkb_mod_index_t m_altModifier; xkb_mod_index_t m_metaModifier; xkb_led_index_t m_numLock; xkb_led_index_t m_capsLock; xkb_led_index_t m_scrollLock; Qt::KeyboardModifiers m_modifiers; Qt::KeyboardModifiers m_consumedModifiers; xkb_keysym_t m_keysym; struct { uint pressCount = 0; Qt::KeyboardModifier modifier = Qt::NoModifier; } m_modOnlyShortcut; quint32 m_currentLayout = 0; struct { xkb_compose_table *table = nullptr; xkb_compose_state *state = nullptr; } m_compose; LEDs m_leds; }; class KWIN_EXPORT KeyboardInputRedirection : public QObject { Q_OBJECT public: explicit KeyboardInputRedirection(InputRedirection *parent); virtual ~KeyboardInputRedirection(); void init(); void update(); /** * @internal */ void processKey(uint32_t key, InputRedirection::KeyboardKeyState state, uint32_t time, LibInput::Device *device = nullptr); /** * @internal */ void processModifiers(uint32_t modsDepressed, uint32_t modsLatched, uint32_t modsLocked, uint32_t group); /** * @internal **/ void processKeymapChange(int fd, uint32_t size); Xkb *xkb() const { return m_xkb.data(); } Qt::KeyboardModifiers modifiers() const { return m_xkb->modifiers(); } Qt::KeyboardModifiers modifiersRelevantForGlobalShortcuts() const { return m_xkb->modifiersRelevantForGlobalShortcuts(); } Q_SIGNALS: void ledsChanged(KWin::Xkb::LEDs); private: InputRedirection *m_input; bool m_inited = false; QScopedPointer m_xkb; QMetaObject::Connection m_activeClientSurfaceChangedConnection; struct { quint32 key = 0; quint32 time = 0; QTimer *timer = nullptr; } m_keyRepeat; ModifiersChangedSpy *m_modifiersChangedSpy = nullptr; KeyboardLayout *m_keyboardLayout = nullptr; }; inline Qt::KeyboardModifiers Xkb::modifiers() const { return m_modifiers; } } Q_DECLARE_METATYPE(KWin::Xkb::LED) Q_DECLARE_METATYPE(KWin::Xkb::LEDs) #endif diff --git a/keyboard_layout.cpp b/keyboard_layout.cpp index 566efad6e..51d21bf90 100644 --- a/keyboard_layout.cpp +++ b/keyboard_layout.cpp @@ -1,112 +1,227 @@ /******************************************************************** KWin - the KDE window manager This file is part of the KDE project. Copyright (C) 2016, 2017 Martin Gräßlin 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 . *********************************************************************/ #include "keyboard_layout.h" #include "keyboard_input.h" #include "input_event.h" #include "main.h" #include "platform.h" +#include "utils.h" +#include #include #include +#include #include #include #include #include +#include namespace KWin { KeyboardLayout::KeyboardLayout(Xkb *xkb) : QObject() , m_xkb(xkb) + , m_notifierItem(nullptr) { } 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({sequence})); KGlobalAccel::self()->setShortcut(switchKeyboardAction, QList({sequence})); kwinApp()->platform()->setupActionForGlobalAccel(switchKeyboardAction); - connect(switchKeyboardAction, &QAction::triggered, this, - [this] { - m_xkb->switchToNextLayout(); - checkLayoutChange(); - } - ); + connect(switchKeyboardAction, &QAction::triggered, this, &KeyboardLayout::switchToNextLayout); QDBusConnection::sessionBus().connect(QString(), QStringLiteral("/Layouts"), QStringLiteral("org.kde.keyboard"), QStringLiteral("reloadConfig"), this, SLOT(reconfigure())); reconfigure(); } +void KeyboardLayout::initNotifierItem() +{ + bool showNotifier = true; + bool showSingle = false; + if (m_config) { + const auto config = m_config->group(QStringLiteral("Layout")); + showNotifier = config.readEntry("ShowLayoutIndicator", true); + showSingle = config.readEntry("ShowSingle", false); + } + const bool shouldShow = showNotifier && (showSingle || m_xkb->numberOfLayouts() > 1); + if (shouldShow) { + if (m_notifierItem) { + return; + } + } else { + delete m_notifierItem; + m_notifierItem = nullptr; + return; + } + + m_notifierItem = new KStatusNotifierItem(this); + m_notifierItem->setCategory(KStatusNotifierItem::Hardware); + m_notifierItem->setStatus(KStatusNotifierItem::Active); + m_notifierItem->setToolTipTitle(i18nc("tooltip title", "Keyboard Layout")); + m_notifierItem->setTitle(i18nc("tooltip title", "Keyboard Layout")); + m_notifierItem->setToolTipIconByName(QStringLiteral("preferences-desktop-keyboard")); + m_notifierItem->setStandardActionsEnabled(false); + + // TODO: proper icon + m_notifierItem->setIconByName(QStringLiteral("preferences-desktop-keyboard")); + + connect(m_notifierItem, &KStatusNotifierItem::activateRequested, this, &KeyboardLayout::switchToNextLayout); + connect(m_notifierItem, &KStatusNotifierItem::scrollRequested, this, + [this] (int delta, Qt::Orientation orientation) { + if (orientation == Qt::Horizontal) { + return; + } + if (delta > 0) { + switchToNextLayout(); + } else { + switchToPreviousLayout(); + } + } + ); + + m_notifierItem->setStatus(KStatusNotifierItem::Active); +} + +void KeyboardLayout::switchToNextLayout() +{ + m_xkb->switchToNextLayout(); + checkLayoutChange(); +} + +void KeyboardLayout::switchToPreviousLayout() +{ + m_xkb->switchToPreviousLayout(); + checkLayoutChange(); +} + void KeyboardLayout::reconfigure() { m_xkb->reconfigure(); + if (m_config) { + m_config->reparseConfiguration(); + } resetLayout(); } void KeyboardLayout::resetLayout() { m_layout = m_xkb->currentLayout(); + initNotifierItem(); + updateNotifier(); + reinitNotifierMenu(); } 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(); + updateNotifier(); } void KeyboardLayout::notifyLayoutChange() { // notify OSD service about the new layout QDBusMessage msg = QDBusMessage::createMethodCall( QStringLiteral("org.kde.plasmashell"), QStringLiteral("/org/kde/osdService"), QStringLiteral("org.kde.osdService"), QStringLiteral("kbdLayoutChanged")); msg << i18nd("xkeyboard-config", m_xkb->layoutName().toUtf8().constData()); QDBusConnection::sessionBus().asyncCall(msg); } +void KeyboardLayout::updateNotifier() +{ + if (!m_notifierItem) { + return; + } + m_notifierItem->setToolTipSubTitle(i18nd("xkeyboard-config", m_xkb->layoutName().toUtf8().constData())); + // TODO: update icon +} + +void KeyboardLayout::reinitNotifierMenu() +{ + if (!m_notifierItem) { + return; + } + const auto layouts = m_xkb->layoutNames(); + + QMenu *menu = new QMenu; + auto switchLayout = [this] (xkb_layout_index_t index) { + m_xkb->switchToLayout(index); + checkLayoutChange(); + }; + for (auto it = layouts.begin(); it != layouts.end(); it++) { + menu->addAction(i18nd("xkeyboard-config", it.value().toUtf8().constData()), std::bind(switchLayout, it.key())); + } + + menu->addSeparator(); + menu->addAction(QIcon::fromTheme(QStringLiteral("configure")), i18n("Configure Layouts..."), this, + [this] { + // TODO: introduce helper function to start kcmshell5 + QProcess *p = new Process(this); + p->setArguments(QStringList{QStringLiteral("--args=--tab=layouts"), QStringLiteral("kcm_keyboard")}); + p->setProcessEnvironment(kwinApp()->processStartupEnvironment()); + p->setProgram(QStringLiteral("kcmshell5")); + connect(p, static_cast(&QProcess::finished), p, &QProcess::deleteLater); + connect(p, static_cast(&QProcess::error), this, + [p] (QProcess::ProcessError e) { + if (e == QProcess::FailedToStart) { + qCDebug(KWIN_CORE) << "Failed to start kcmshell5"; + } + } + ); + p->start(); + } + ); + + m_notifierItem->setContextMenu(menu); +} + } diff --git a/keyboard_layout.h b/keyboard_layout.h index 7a71e6e5d..aa39fd736 100644 --- a/keyboard_layout.h +++ b/keyboard_layout.h @@ -1,56 +1,71 @@ /******************************************************************** KWin - the KDE window manager This file is part of the KDE project. Copyright (C) 2016, 2017 Martin Gräßlin 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 . *********************************************************************/ #ifndef KWIN_KEYBOARD_LAYOUT_H #define KWIN_KEYBOARD_LAYOUT_H #include "input_event_spy.h" #include + +#include typedef uint32_t xkb_layout_index_t; +class KStatusNotifierItem; + namespace KWin { class Xkb; class KeyboardLayout : public QObject, public InputEventSpy { Q_OBJECT public: explicit KeyboardLayout(Xkb *xkb); ~KeyboardLayout() override; + void setConfig(KSharedConfigPtr config) { + m_config = config; + } + void init(); void checkLayoutChange(); void resetLayout(); void keyEvent(KeyEvent *event) override; private Q_SLOTS: void reconfigure(); private: void notifyLayoutChange(); + void initNotifierItem(); + void switchToNextLayout(); + void switchToPreviousLayout(); + void updateNotifier(); + void reinitNotifierMenu(); Xkb *m_xkb; xkb_layout_index_t m_layout = 0; + KStatusNotifierItem *m_notifierItem; + KSharedConfigPtr m_config; }; } #endif