Introduce new input engine and framework to replace Qt VKBD

NOTE: CJK support is split into !113 (merged)

This merge request implements a simple and straightforward input engine to replace the current Qt + QtVKBD input engine. It also introduces a library to create keyboard layouts in a similar fashion to Qt VKBD.

Features

  • An input engine "library" in /virtualkeyboard, as well as a keyboard layout framework in /virtualkeyboard/components
  • Multiple keyboard layouts per keyboard package (not tied to locales anymore)
    • Distinction between desktop keyboard layouts and mobile ones
    • QWERTY, Dvorak, etc. (fixes #61 (closed))
  • Desktop key emulation for relevant layouts (similar to GNOME and Windows) (implements #14 (closed), #47 (closed))
  • Improved KCM layout selector
  • Improved Chinese support
    • Desktop layout that can swap between latin and pinyin composition
    • Zhuyin prediction support
    • Text composer provided via system-provided libpinyin and libchewing (rather than Qt VKBD's internal forks)
  • Improved Japanese support
    • All of the layouts are no longer crammed into a single keyboard layout
    • Separate 12-key (flick), romaji and desktop layouts
    • Improved 12-key flick keyboard: with latin flick layout too (fixes #50 (closed), and fixes kana placement #75 (closed))
      • Flick keys now support multiple taps to cycle characters
    • Text composer provided via system-provided anthy
  • Korean support
    • Desktop layout that can swap between latin and hangul composition
    • Text composer provided via system-provided libhangul
  • Chinese, Japanese and Korean text composers (via libhangul, libpinyin, libchewing, anthy rather than Qt VKBD's internal forks), with the option to make more (extend AbstractTextComposer and import it in your layout)
  • Other fixes:

Opportunities (not implemented here, but is easier to do)

  • We can make a presage-based keyboard by adding a new text composer
  • We can implement a lot of features without hacks:
    • Swipe typing (since we have access to the key positions now): #5
    • Split keyboard layouts (we can introduce native components for layouts to "split"): !71
    • Dragging on spacebar to move: !69
    • Swiping on backspace to delete: #62
    • Resizing: #56
  • We can modify the text prediction bar without hacks, and introduce copy/paste options, voice input, etc.
  • We can implement a native emoji keyboard without hacks (as it requires integration with the existing layout to provide "text search"): (#25)
  • We can implement physical keyboard text prediction (via our text composers) for CJK

Missing features from Qt VKBD

  • No styling engine just yet, can investigate integrating Union as a followup
  • Most of the keyboard layouts have not yet been ported, the only languages so far are English, Chinese, Japanese, and Korean
    • I focused on the hardest ones first (CJK requires custom text composing), the rest should be straightforward layout ports
    • I left the old layouts in the tree to avoid the diff becoming too big

Background

plasma-keyboard's virtual keyboard effectively is a wrapper (hack?) around Qt VirtualKeyboard, proxied through Qt's internal text input engine (Qt VKBD < Qt's internal text engine > plasma-keyboard <- input-method-v1 -> KWin). It currently acts as a text input consumer in Qt to proxy it to KWin.

There are multiple challenges that I've faced in trying to develop it:

  1. The extra conversion layer makes debugging issues significantly harder (Qt VKBD < Qt's internal text engine > plasma-keyboard <- input-method-v1 -> KWin), see https://bugs.kde.org/show_bug.cgi?id=516511, https://bugs.kde.org/show_bug.cgi?id=517087. Qt's text input framework doesn't map 1-to-1 to input-method-v1, which makes it tricky
  2. Layouts are tied 1-1 to locales internally in Qt VKBD, making it not possible in making several layouts per locale (and resulting in a crammed japanese keyboard for layout switching)
  3. Our theming is effectively a hack, because we can't hold state in the theme, and so there is a separate QML plugin to proxy the state over to plasma-keyboard: https://invent.kde.org/plasma/plasma-keyboard/-/tree/master/src/qmlplugin?ref_type=heads
  4. Implementing support for desktop keys is effectively a hack; we have to introduce custom Qt VKBD keys
  5. I've had to completely disable text prediction because there is unpredictable behavior (not sure if it's from Qt VKBD or Qt itself) with sending external cursor changes
  6. It feels like implementing any sort of feature is just adding more hacks on top of hacks, making it hard for outsiders to contribute since they need to understand Qt VKBD, Qt's input engine and plasma-keyboard (see !71, !69)
  7. We rely on how the distro packages Qt VirtualKeyboard (which is shared amongst ALL Qt applications). They may choose to omit certain features (ex. hunspell on alpine, chinese on debian) perhaps as a workaround for certain apps or due to complexity (ex. Qt VKBD forks mozc, tcime in their repo), but that affects us in plasma-keyboard as well.

While I feel like the original intention was solid (in building on top of an existing input engine), in practice it's quite hard to build on top of to really meet the demands of our users with this current architecture. Upstreaming everything we do poses a larger challenge, because we need to take into account the needs of Qt VirtualKeyboard's primary audience (targeted, embedded usecases), while our usecases are quite different in trying to provide a flexible keyboard for Plasma. All of the handwriting and advanced text prediction engines are also proprietary, and so we don't really have a huge benefit in terms of input engines (there are already widely packaged open source input engines for CJK we can use).


Architecture

The virtual keyboard library and engine are in /virtualkeyboard:

  • /virtualkeyboard/components - QML components for building keyboard layouts and the keyboard itself
    • The component names are loosely based on what is used in Qt VirtualKeyboard, making porting layouts easier
    • Example usage for qwerty keyboard: link
  • /virtualkeyboard/inputbackend.h - generic input "backend" for sending events from the vkbd, currently just for input-method-v1 but could be for xdg-remote-desktop?
  • /virtualkeyboard/inputengine.h - internal input method "state"
  • /virtualkeyboard/keyboardcontroller.h - internal virtual keyboard panel "state"
  • /virtualkeyboard/keyboardpackageresolver.h - utilities to find and parse keyboard "layout packages"
  • /virtualkeyboard/textcomposers/abstracttextcomposer.h - abstract interface that implements event handling from the keyboard layout and talks to InputEngine for the final "text" (needed for CJK, text prediction)
  • /virtualkeyboard/textcomposers/directtextcomposer.h - directly enters text (enough for most layouts)

/src retains the actual input method client:

  • /src/vkbdwaylandinputbackend.h - extends InputBackend, bridges the virtualkeyboard to our compositor communication (InputMethodConnection)
  • /src/inputmethodconnection.h - replaces InputListenerItem, talks input-method-v1 and fake-input to KWin, and bridges it to diacritics/vkbd
  • /layouts now has the keyboard layout packages

Untitled_Diagram.drawio_1_

For an example keyboard package, please see https://invent.kde.org/plasma/plasma-keyboard/-/tree/27b9d1e089b7e9182a2e2869e645d05f7ac98581/layouts/en (shared layouts in https://invent.kde.org/plasma/plasma-keyboard/-/tree/27b9d1e089b7e9182a2e2869e645d05f7ac98581/layouts/defaultplugin)

  • Metadata is in metadata.json, which lists the locales the package is meant for, and what layouts it provides
  • Each keyboard layout has predefined "required" layouts: main, symbols, digits, numbers and dialpad
  • Each keyboard layout can define which form factors to show up for (ex. desktop ones won't show up in the mobile kcm keyboard selector by default)
  • Keyboard layout packages can provide multiple layouts. For example, in the English layout we have Mobile QWERTY, Mobile Dvorak, Desktop and extended desktop.
  • Keyboard layout packages also define locales the package is meant for
Example of the mobile qwerty layout:
KeyboardLayout {
    KeyboardRow {
        Key { key: Qt.Key_Q; text: "q"; alternativeKeys: "q1"; smallText: "1"}
        Key { key: Qt.Key_W; text: "w"; alternativeKeys: "w2"; smallText: "2"}
        Key { key: Qt.Key_E; text: "e"; alternativeKeys: "êe3ëèé"; smallText: "3"}
        Key { key: Qt.Key_R; text: "r"; alternativeKeys: "ŕr4ř"; smallText: "4"}
        Key { key: Qt.Key_T; text: "t"; alternativeKeys: "ţt5ŧť"; smallText: "5"}
        Key { key: Qt.Key_Y; text: "y"; alternativeKeys: "ÿy6ýŷ"; smallText: "6"}
        Key { key: Qt.Key_U; text: "u"; alternativeKeys: "űūũûüu7ùú"; smallText: "7"}
        Key { key: Qt.Key_I; text: "i"; alternativeKeys: "îïīĩi8ìí"; smallText: "8"}
        Key { key: Qt.Key_O; text: "o"; alternativeKeys: "œøõôöòóo9"; smallText: "9"}
        Key { key: Qt.Key_P; text: "p"; alternativeKeys: "p0"; smallText: "0"}
    }
    KeyboardRow {
        FillerKey { weight: defaultKeyWeight / 2 }
        Key { key: Qt.Key_A; text: "a"; alternativeKeys: "aäåãâàá" }
        Key { key: Qt.Key_S; text: "s"; alternativeKeys: "šsşś" }
        Key { key: Qt.Key_D; text: "d"; alternativeKeys: "dđď" }
        Key { key: Qt.Key_F; text: "f" }
        Key { key: Qt.Key_G; text: "g"; alternativeKeys: "ġgģĝğ" }
        Key { key: Qt.Key_H; text: "h" }
        Key { key: Qt.Key_J; text: "j" }
        Key { key: Qt.Key_K; text: "k" }
        Key { key: Qt.Key_L; text: "l"; alternativeKeys: "ĺŀłļľl" }
        FillerKey { weight: defaultKeyWeight / 2 }
    }
    KeyboardRow {
        ShiftKey { weight: 150 }
        Key { key: Qt.Key_Z; text: "z"; alternativeKeys: "zžż" }
        Key { key: Qt.Key_X; text: "x" }
        Key { key: Qt.Key_C; text: "c"; alternativeKeys: "çcċčć" }
        Key { key: Qt.Key_V; text: "v" }
        Key { key: Qt.Key_B; text: "b" }
        Key { key: Qt.Key_N; text: "n"; alternativeKeys: "ņńnň" }
        Key { key: Qt.Key_M; text: "m" }
        BackspaceKey { weight: 150 }
    }
    KeyboardRow {
        SymbolModeKey { weight: 150 }
        ChangeLanguageKey {}
        Key { key: Qt.Key_Comma; text: ","; secondaryStyle: true }
        SpaceKey { weight: 300 }
        Key { key: Qt.Key_Period; text: "."; alternativeKeys: "!.?"; smallText: "!?"; secondaryStyle: true }
        HideKeyboardKey {}
        EnterKey { weight: 150 }
    }
}
More complicated example of a desktop layout, with keysyms, and a number row that can swap as function keys:
KeyboardLayout {
    id: root

    readonly property int rowCount: 5
    readonly property int fullRowWeight: 1550
    readonly property bool fnActive: inputEngine && inputEngine.pressedKeys.indexOf(KeyCodes.KeyFn) !== -1

    // Keep normal key slots square: 15.5 key-width units across / 5 key-height units tall
    maxWidthToHeightRatio: (fullRowWeight / defaultKeyWeight) / rowCount

    KeyboardRow {
        heightWeight: defaultRowHeightWeight * 0.75

        KeysymKey { key: Qt.Key_Escape; displayText: "Esc" }
        Key { key: Qt.Key_QuoteLeft; text: "`"; alternativeKeys: "`~"; smallText: "~" }
        Key {
            key: root.fnActive ? Qt.Key_F1 : Qt.Key_1
            text: root.fnActive ? "" : "1"
            displayText: root.fnActive ? "F1" : "1"
            alternativeKeys: root.fnActive ? [] : "1!"
            smallText: root.fnActive ? "" : "!"
        }
        Key {
            key: root.fnActive ? Qt.Key_F2 : Qt.Key_2
            text: root.fnActive ? "" : "2"
            displayText: root.fnActive ? "F2" : "2"
            alternativeKeys: root.fnActive ? [] : "2@"
            smallText: root.fnActive ? "" : "@"
        }
        Key {
            key: root.fnActive ? Qt.Key_F3 : Qt.Key_3
            text: root.fnActive ? "" : "3"
            displayText: root.fnActive ? "F3" : "3"
            alternativeKeys: root.fnActive ? [] : "3#"
            smallText: root.fnActive ? "" : "#"
        }
        Key {
            key: root.fnActive ? Qt.Key_F4 : Qt.Key_4
            text: root.fnActive ? "" : "4"
            displayText: root.fnActive ? "F4" : "4"
            alternativeKeys: root.fnActive ? [] : "4$"
            smallText: root.fnActive ? "" : "$"
        }
        Key {
            key: root.fnActive ? Qt.Key_F5 : Qt.Key_5
            text: root.fnActive ? "" : "5"
            displayText: root.fnActive ? "F5" : "5"
            alternativeKeys: root.fnActive ? [] : "5%"
            smallText: root.fnActive ? "" : "%"
        }
        Key {
            key: root.fnActive ? Qt.Key_F6 : Qt.Key_6
            text: root.fnActive ? "" : "6"
            displayText: root.fnActive ? "F6" : "6"
            alternativeKeys: root.fnActive ? [] : "6^"
            smallText: root.fnActive ? "" : "^"
        }
        Key {
            key: root.fnActive ? Qt.Key_F7 : Qt.Key_7
            text: root.fnActive ? "" : "7"
            displayText: root.fnActive ? "F7" : "7"
            alternativeKeys: root.fnActive ? [] : "7&"
            smallText: root.fnActive ? "" : "&"
        }
        Key {
            key: root.fnActive ? Qt.Key_F8 : Qt.Key_8
            text: root.fnActive ? "" : "8"
            displayText: root.fnActive ? "F8" : "8"
            alternativeKeys: root.fnActive ? [] : "8*"
            smallText: root.fnActive ? "" : "*"
        }
        Key {
            key: root.fnActive ? Qt.Key_F9 : Qt.Key_9
            text: root.fnActive ? "" : "9"
            displayText: root.fnActive ? "F9" : "9"
            alternativeKeys: root.fnActive ? [] : "9("
            smallText: root.fnActive ? "" : "("
        }
        Key {
            key: root.fnActive ? Qt.Key_F10 : Qt.Key_0
            text: root.fnActive ? "" : "0"
            displayText: root.fnActive ? "F10" : "0"
            alternativeKeys: root.fnActive ? [] : "0)"
            smallText: root.fnActive ? "" : ")"
        }
        Key {
            key: root.fnActive ? Qt.Key_F11 : Qt.Key_Minus
            text: root.fnActive ? "" : "-"
            displayText: root.fnActive ? "F11" : "-"
            alternativeKeys: root.fnActive ? [] : "-_"
            smallText: root.fnActive ? "" : "_"
        }
        Key {
            key: root.fnActive ? Qt.Key_F12 : Qt.Key_Equal
            text: root.fnActive ? "" : "="
            displayText: root.fnActive ? "F12" : "="
            alternativeKeys: root.fnActive ? [] : "=+"
            smallText: root.fnActive ? "" : "+"
        }
        BackspaceKey { weight: root.defaultKeyWeight * 1.5 }
    }

    KeyboardRow {
        KeysymKey { key: Qt.Key_Tab; displayText: "Tab"; weight: root.defaultKeyWeight * 1.5 }
        Key { key: Qt.Key_Q; text: "q" }
        Key { key: Qt.Key_W; text: "w" }
        Key { key: Qt.Key_E; text: "e" }
        Key { key: Qt.Key_R; text: "r" }
        Key { key: Qt.Key_T; text: "t" }
        Key { key: Qt.Key_Y; text: "y" }
        Key { key: Qt.Key_U; text: "u" }
        Key { key: Qt.Key_I; text: "i" }
        Key { key: Qt.Key_O; text: "o" }
        Key { key: Qt.Key_P; text: "p" }
        Key { key: Qt.Key_BracketLeft; text: "["; alternativeKeys: "[{"; smallText: "{" }
        Key { key: Qt.Key_BracketRight; text: "]"; alternativeKeys: "]}"; smallText: "}" }
        Key { key: Qt.Key_Backslash; text: "\\"; alternativeKeys: "\\|"; smallText: "|" }
        KeysymKey { key: Qt.Key_Delete; displayText: "Del" }
    }

    KeyboardRow {
        KeysymKey {
            key: Qt.Key_CapsLock
            displayText: "Caps"
            weight: root.defaultKeyWeight * 1.75
            highlighted: inputEngine && inputEngine.capsLockActive
            onClicked: {
                if (!inputEngine) {
                    return;
                }
                inputEngine.capsLockActive = !inputEngine.capsLockActive;
                inputEngine.shiftActive = false;
            }
        }
        Key { key: Qt.Key_A; text: "a" }
        Key { key: Qt.Key_S; text: "s" }
        Key { key: Qt.Key_D; text: "d" }
        Key { key: Qt.Key_F; text: "f" }
        Key { key: Qt.Key_G; text: "g" }
        Key { key: Qt.Key_H; text: "h" }
        Key { key: Qt.Key_J; text: "j" }
        Key { key: Qt.Key_K; text: "k" }
        Key { key: Qt.Key_L; text: "l" }
        Key { key: Qt.Key_Semicolon; text: ";"; alternativeKeys: ";:"; smallText: ":" }
        Key { key: Qt.Key_Apostrophe; text: "'"; alternativeKeys: "'\""; smallText: "\"" }
        EnterKey { weight: root.defaultKeyWeight * 2.75 }
    }

    KeyboardRow {
        ShiftKey { weight: root.defaultKeyWeight * 2.5; sendDirectKeyEvents: root.sendShiftDirectKeyEvents }
        Key { key: Qt.Key_Z; text: "z" }
        Key { key: Qt.Key_X; text: "x" }
        Key { key: Qt.Key_C; text: "c" }
        Key { key: Qt.Key_V; text: "v" }
        Key { key: Qt.Key_B; text: "b" }
        Key { key: Qt.Key_N; text: "n" }
        Key { key: Qt.Key_M; text: "m" }
        Key { key: Qt.Key_Comma; text: ","; alternativeKeys: ",<"; smallText: "<" }
        Key { key: Qt.Key_Period; text: "."; alternativeKeys: ".>"; smallText: ">" }
        Key { key: Qt.Key_Slash; text: "/"; alternativeKeys: "/?"; smallText: "?" }
        KeysymKey { key: Qt.Key_Up; iconName: "go-up-symbolic"; displayText: ""; repeat: true }
        ShiftKey { weight: root.defaultKeyWeight * 2; sendDirectKeyEvents: root.sendShiftDirectKeyEvents }
    }

    KeyboardRow {
        KeysymKey { key: Qt.Key_Control; displayText: "Ctrl"; sticky: true }
        KeysymKey { key: KeyCodes.KeyFn; displayText: "Fn"; sticky: true }
        KeysymKey { key: Qt.Key_Meta; iconName: "start-here-symbolic"; displayText: ""; sticky: true }
        KeysymKey { key: Qt.Key_Alt; displayText: "Alt"; sticky: true }
        InputModeKey {
            visible: root.inputModeKeyVisible
            height: visible ? implicitHeight : 0
            weight: visible ? root.defaultKeyWeight : 0
            textComposer: root.textComposer
            modes: root.inputModeKeyModes
        }
        SpaceKey { weight: root.defaultKeyWeight * (root.inputModeKeyVisible ? 6.5 : 7.5) }
        KeysymKey { key: Qt.Key_Left; iconName: "go-previous-symbolic"; displayText: ""; repeat: true }
        KeysymKey { key: Qt.Key_Down; iconName: "go-down-symbolic"; displayText: ""; repeat: true }
        KeysymKey { key: Qt.Key_Right; iconName: "go-next-symbolic"; displayText: ""; repeat: true }
        ChangeLanguageKey { weight: root.defaultKeyWeight / 2 }
        HideKeyboardKey { weight: root.defaultKeyWeight / 2 }
    }
}

The usage is quite similar to Qt VirtualKeyboard, however we use key "weights" to do relative sizing for each row (as well as column).


Screenshots

image image image image image image

image image image image image image image

For each locale, there is a "mobile", "full" (basic modifiers) and "extended full" (modifiers + fn keys) layout (not required by any means for new layouts though).

image

Testing keyboard navigation

In order to test keyboard navigation, I had to pass an extra flag to KWin so that it would forward keyboard events:

KWIN_DEVICE_SKIPS_INPUT_METHOD="_none_" dbus-run-session kwin_wayland "plasmashell"
Edited by Devin Lin

Merge request reports

Loading
Loading