diff --git a/app/src/main/cpp/lorie/activity.c b/app/src/main/cpp/lorie/activity.c index 6e50b23d8..c6abc1fed 100644 --- a/app/src/main/cpp/lorie/activity.c +++ b/app/src/main/cpp/lorie/activity.c @@ -358,7 +358,7 @@ static void sendTextEvent(JNIEnv *env, __unused jobject thiz, jbyteArray text) { p += len; if (p - (char*) str >= length) break; - usleep(30000); + usleep(10000); } (*env)->ReleaseByteArrayElements(env, text, str, JNI_ABORT); diff --git a/app/src/main/java/com/termux/x11/LorieView.java b/app/src/main/java/com/termux/x11/LorieView.java index bc28f8c81..896e2559d 100644 --- a/app/src/main/java/com/termux/x11/LorieView.java +++ b/app/src/main/java/com/termux/x11/LorieView.java @@ -11,7 +11,6 @@ import android.graphics.Point; import android.graphics.Rect; import android.graphics.drawable.ColorDrawable; -import android.os.Build; import android.text.Editable; import android.text.InputType; import android.util.AttributeSet; @@ -23,8 +22,7 @@ import android.view.inputmethod.BaseInputConnection; import android.view.inputmethod.EditorInfo; import android.view.inputmethod.InputConnection; -import android.view.inputmethod.InputMethodManager; -import android.view.inputmethod.InputMethodSubtype; +import android.view.inputmethod.TextSnapshot; import androidx.annotation.Keep; import androidx.annotation.NonNull; @@ -32,9 +30,10 @@ import com.termux.x11.input.InputStub; import com.termux.x11.input.TouchInputHandler; -import java.nio.charset.StandardCharsets; import java.util.regex.PatternSyntaxException; +import static java.nio.charset.StandardCharsets.UTF_8; + import dalvik.annotation.optimization.CriticalNative; import dalvik.annotation.optimization.FastNative; @@ -53,12 +52,86 @@ interface PixelFormat { private long lastClipboardTimestamp = System.currentTimeMillis(); private static boolean clipboardSyncEnabled = false; private static boolean hardwareKbdScancodesWorkaround = false; - private final InputMethodManager mIMM = (InputMethodManager)getContext().getSystemService(Context.INPUT_METHOD_SERVICE); - private String mImeLang; - private boolean mImeCJK; - public boolean enableGboardCJK; private Callback mCallback; private final Point p = new Point(); + private final InputConnection mConnection = new BaseInputConnection(this, true) { + private final MainActivity a = MainActivity.getInstance(); + private final CharSequence seq = " "; + CharSequence currentText = null; + + @Override public Editable getEditable() { + return null; + } + + // Needed to send arrow keys with IME's cursor control feature + @Override public CharSequence getTextBeforeCursor(int length, int flags) { + return seq; + } + + // Needed to send arrow keys with IME's cursor control feature + @Override public CharSequence getTextAfterCursor(int length, int flags) { + return seq; + } + + void sendKey(int k) { + LorieView.this.sendKeyEvent(0, k, true); + LorieView.this.sendKeyEvent(0, k, false); + } + + @Override public boolean deleteSurroundingText(int beforeLength, int afterLength) { + for (int i=0; i= 2 && !languageTag.substring(0, 2).equals(mImeLang)) - mIMM.restartInput(this); - else if (recheck) { // recheck needed because sometimes requestCursorUpdates() is called too fast, before InputMethodManager detect change in IM subtype - MainActivity.handler.postDelayed(() -> checkRestartInput(false), 40); - } - } - @Override public InputConnection onCreateInputConnection(EditorInfo outAttrs) { - outAttrs.inputType = InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD; - + outAttrs.inputType = InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS | InputType.TYPE_TEXT_VARIATION_NORMAL; + outAttrs.actionLabel = "↵"; // Note that IME_ACTION_NONE cannot be used as that makes it impossible to input newlines using the on-screen // keyboard on Android TV (see https://github.com/termux/termux-app/issues/221). outAttrs.imeOptions = EditorInfo.IME_FLAG_NO_FULLSCREEN; - - if (enableGboardCJK) { - InputMethodSubtype methodSubtype = mIMM.getCurrentInputMethodSubtype(); - mImeLang = methodSubtype == null ? null : methodSubtype.getLanguageTag(); - if (mImeLang != null && mImeLang.length() > 2) - mImeLang = mImeLang.substring(0, 2); - mImeCJK = mImeLang != null && (mImeLang.equals("zh") || mImeLang.equals("ko") || mImeLang.equals("ja")); - outAttrs.inputType = InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS | - (mImeCJK ? InputType.TYPE_TEXT_VARIATION_NORMAL : InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD); - return new BaseInputConnection(this, false) { - // workaround for Gboard - // Gboard calls requestCursorUpdates() whenever switching language - // check and then restart keyboard in different inputType when needed - @Override - public Editable getEditable() { - checkRestartInput(true); - return super.getEditable(); - } - @Override - public boolean requestCursorUpdates(int cursorUpdateMode) { - checkRestartInput(true); - return super.requestCursorUpdates(cursorUpdateMode); - } - - @Override - public boolean commitText(CharSequence text, int newCursorPosition) { - boolean result = super.commitText(text, newCursorPosition); - if (mImeCJK) - // suppress Gboard CJK keyboard suggestion - // this workaround does not work well for non-CJK keyboards - // , when typing fast and two keypresses (commitText) are close in time - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) - mIMM.invalidateInput(LorieView.this); - else - mIMM.restartInput(LorieView.this); - return result; - } - }; - } else { - return super.onCreateInputConnection(outAttrs); - } + return mConnection; } static native boolean renderingInActivity(); diff --git a/app/src/main/java/com/termux/x11/MainActivity.java b/app/src/main/java/com/termux/x11/MainActivity.java index 6a61c4a44..21ccbc286 100644 --- a/app/src/main/java/com/termux/x11/MainActivity.java +++ b/app/src/main/java/com/termux/x11/MainActivity.java @@ -24,6 +24,7 @@ import android.content.res.Configuration; import android.graphics.Canvas; import android.graphics.Color; +import android.graphics.Rect; import android.net.Uri; import android.os.Build; import android.os.Build.VERSION_CODES; @@ -90,7 +91,7 @@ public class MainActivity extends AppCompatActivity implements View.OnApplyWindo private static boolean externalKeyboardConnected = false; private View.OnKeyListener mLorieKeyListener; private boolean filterOutWinKey = false; - private boolean useTermuxEKBarBehaviour = false; + boolean useTermuxEKBarBehaviour = false; private boolean isInPictureInPictureMode = false; public static Prefs prefs = null; @@ -232,7 +233,6 @@ else if (SamsungDexUtils.checkDeXEnabled(this)) onPreferencesChanged(""); toggleExtraKeys(false, false); - checkRestartInput(); initStylusAuxButtons(); initMouseAuxButtons(); @@ -879,15 +879,6 @@ public static boolean isConnected() { return LorieView.connected(); } - private void checkRestartInput() { - // an imperfect workaround for Gboard CJK keyboard in DeX soft keyboard mode - // in that particular mode during language switching, InputConnection#requestCursorUpdates() is not called and no signal can be picked up. - // therefore, check to activate CJK keyboard is done upon a keypress. - if (getLorieView().enableGboardCJK && SamsungDexUtils.checkDeXEnabled(this)) - getLorieView().checkRestartInput(false); - handler.postDelayed(this::checkRestartInput, 300); - } - public static void getRealMetrics(DisplayMetrics m) { if (getInstance() != null && getInstance().getLorieView() != null && diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 051ad26f1..a94d120f2 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -84,8 +84,6 @@ E.g. META for META key, \n Pause key intercepting with Esc key Filter out intercepted Win (Meta/Mod4) key. Allows you to use Dex shortcuts while intercepting. Requires Accessibility service to work. - Workaround to enable CJK Gboard - May require Android 14 and Gboard 14 Clipboard sharing Request notification permission diff --git a/app/src/main/res/xml/preferences.xml b/app/src/main/res/xml/preferences.xml index 0015779ba..6520cffd1 100644 --- a/app/src/main/res/xml/preferences.xml +++ b/app/src/main/res/xml/preferences.xml @@ -49,7 +49,6 @@ -