diff --git a/app/src/main/java/io/github/lordofpolls/shellwave/terminal/TerminalInput.kt b/app/src/main/java/io/github/lordofpolls/shellwave/terminal/TerminalInput.kt index 888e5b6..71b128e 100644 --- a/app/src/main/java/io/github/lordofpolls/shellwave/terminal/TerminalInput.kt +++ b/app/src/main/java/io/github/lordofpolls/shellwave/terminal/TerminalInput.kt @@ -185,16 +185,33 @@ fun hardwareKeyCode(event: ComposeKeyEvent, cursorApp: Boolean, keypadApp: Boole ctrlChordCode(native.getUnicodeChar(0))?.let { return it.toString() } } - // xterm/bash convention: ESC then the character, e.g. Alt+. to recall the last argument. if (alt && !ctrl) { - val codePoint = - native.getUnicodeChar(native.metaState and android.view.KeyEvent.META_SHIFT_ON) - if (codePoint > 0) return "\u001B" + String(Character.toChars(codePoint)) + val shift = native.metaState and android.view.KeyEvent.META_SHIFT_ON + altChordText( + rightAlt = native.metaState and android.view.KeyEvent.META_ALT_RIGHT_ON != 0, + composed = native.getUnicodeChar(native.metaState), + base = native.getUnicodeChar(shift), + )?.let { return it } } return null } +/** + * Text for a key pressed with Alt held. Left Alt follows the xterm/bash convention: ESC then the + * base character, e.g. Alt+. to recall the last argument. + * + * Right Alt is AltGr on layouts that have one (German AltGr+Q = @). Android reports it as Alt too, + * so it is told apart by the character it composes: when Right Alt changes the character, that + * composed character is sent as is, without the ESC prefix. [composed] is the code point with the + * full meta state applied, [base] the one with only Shift applied. Null when neither is printable. + */ +internal fun altChordText(rightAlt: Boolean, composed: Int, base: Int): String? { + if (rightAlt && composed > 0 && composed != base) return String(Character.toChars(composed)) + if (base > 0) return "\u001B" + String(Character.toChars(base)) + return null +} + /** Ported from upstream `TerminalView.inputCodePoint`. */ private fun ctrlChordCode(codePoint: Int): Char? { if (codePoint in 0..0xFFFF) { diff --git a/app/src/test/java/io/github/lordofpolls/shellwave/terminal/AltChordTextTest.kt b/app/src/test/java/io/github/lordofpolls/shellwave/terminal/AltChordTextTest.kt new file mode 100644 index 0000000..86c2213 --- /dev/null +++ b/app/src/test/java/io/github/lordofpolls/shellwave/terminal/AltChordTextTest.kt @@ -0,0 +1,44 @@ +package io.github.lordofpolls.shellwave.terminal + +import org.junit.Assert.assertEquals +import org.junit.Assert.assertNull +import org.junit.Test + +class AltChordTextTest { + @Test + fun leftAlt_sendsEscapePrefixedBaseCharacter() { + assertEquals("\u001B.", altChordText(rightAlt = false, composed = '.'.code, base = '.'.code)) + } + + @Test + fun leftAlt_ignoresComposedCharacter() { + assertEquals("\u001Bq", altChordText(rightAlt = false, composed = '@'.code, base = 'q'.code)) + } + + @Test + fun altGr_sendsComposedCharacterWithoutEscape() { + assertEquals("@", altChordText(rightAlt = true, composed = '@'.code, base = 'q'.code)) + assertEquals("[", altChordText(rightAlt = true, composed = '['.code, base = '8'.code)) + } + + @Test + fun rightAlt_withoutComposedCharacter_actsAsMeta() { + assertEquals("\u001Bx", altChordText(rightAlt = true, composed = 'x'.code, base = 'x'.code)) + } + + @Test + fun rightAlt_noMapping_zeroComposedCharacter_actsAsMeta() { + assertEquals("\u001Bx", altChordText(rightAlt = true, composed = 0, base = 'x'.code)) + } + + @Test + fun altGr_supplementaryCodePoint() { + assertEquals("😀", altChordText(rightAlt = true, composed = 0x1F600, base = 'e'.code)) + } + + @Test + fun unprintableKey_isNull() { + assertNull(altChordText(rightAlt = false, composed = 0, base = 0)) + assertNull(altChordText(rightAlt = true, composed = 0, base = 0)) + } +}