From: Po Lu Date: Wed, 30 Aug 2023 02:07:49 +0000 (+0800) Subject: Facilitate typing `C-SPC' on Android X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=297ccd967f24e37bc51c057da43c862291a55ddd;p=emacs.git Facilitate typing `C-SPC' on Android * doc/emacs/android.texi (Android Windowing): Mention C-SPC interception and how it may be disabled. * java/org/gnu/emacs/EmacsNative.java (shouldForwardCtrlSpace): New function. * java/org/gnu/emacs/EmacsView.java (onKeyPreIme): New function. If the provided key code is SPC and the event's modifier key mask contains ControlMask, relay it directly to onKeyDown. * java/org/gnu/emacs/EmacsWindow.java (eventModifiers): Export and make static. * src/android.c (shouldForwardCtrlSpace): New function. * src/androidfns.c (syms_of_androidfns) : New defvar. --- diff --git a/doc/emacs/android.texi b/doc/emacs/android.texi index 5e7ff0e4bb3..c6f169e3006 100644 --- a/doc/emacs/android.texi +++ b/doc/emacs/android.texi @@ -663,6 +663,19 @@ in Emacs. modifier: it is referred to as @key{SYM} on Android keyboards and within the Settings keymap menu. +@vindex android-intercept-control-space +@cindex @kbd{C-SPC} interception, android + Android input methods have a penchant for irritating users by +silently discarding key sequences containing @kbd{C-SPC} during the +event filtering process, that they normally have no real application +for such key sequences notwithstanding. By default, Emacs intercepts +these key sequences before they can be filtered by the input method. + + If this proves unwanted (for instance, if the input method treats +@kbd{C-SPC} as a shortcut key for switching languages), it can be +disabled by setting the variable +@code{android-intercept-control-space} to @code{nil}. + @node Android Fonts @section Font Backends and Selection under Android @cindex fonts, android diff --git a/java/org/gnu/emacs/EmacsNative.java b/java/org/gnu/emacs/EmacsNative.java index fae0ba98f86..a4b45aafbc1 100644 --- a/java/org/gnu/emacs/EmacsNative.java +++ b/java/org/gnu/emacs/EmacsNative.java @@ -196,6 +196,10 @@ public final class EmacsNative KEYCODE_VOLUME_MUTE should be forwarded to Emacs. */ public static native boolean shouldForwardMultimediaButtons (); + /* Return whether KEYCODE_SPACE combined with META_CTRL_MASK should + be prevented from reaching the system input method. */ + public static native boolean shouldForwardCtrlSpace (); + /* Initialize the current thread, by blocking signals that do not interest it. */ public static native void setupSystemThread (); diff --git a/java/org/gnu/emacs/EmacsView.java b/java/org/gnu/emacs/EmacsView.java index 5a4bcbaa005..04c3d824027 100644 --- a/java/org/gnu/emacs/EmacsView.java +++ b/java/org/gnu/emacs/EmacsView.java @@ -470,6 +470,26 @@ public final class EmacsView extends ViewGroup surfaceView.setBitmap (bitmap, damageRect); } + @Override + public boolean + onKeyPreIme (int keyCode, KeyEvent event) + { + /* Several Android systems intercept key events representing + C-SPC. Avert this by detecting C-SPC events here and relaying + them directly to onKeyDown. + + Make this optional though, since some input methods also + leverage C-SPC as a shortcut for switching languages. */ + + if ((keyCode == KeyEvent.KEYCODE_SPACE + && (window.eventModifiers (event) + & KeyEvent.META_CTRL_MASK) != 0) + && !EmacsNative.shouldForwardCtrlSpace ()) + return onKeyDown (keyCode, event); + + return super.onKeyPreIme (keyCode, event); + } + @Override public boolean onKeyDown (int keyCode, KeyEvent event) diff --git a/java/org/gnu/emacs/EmacsWindow.java b/java/org/gnu/emacs/EmacsWindow.java index aff5046b22e..3738376a6f4 100644 --- a/java/org/gnu/emacs/EmacsWindow.java +++ b/java/org/gnu/emacs/EmacsWindow.java @@ -576,7 +576,7 @@ public final class EmacsWindow extends EmacsHandleObject input EVENT. Replace bits corresponding to Left or Right keys with their corresponding general modifier bits. */ - private int + public static int eventModifiers (KeyEvent event) { int state; diff --git a/src/android.c b/src/android.c index ed304baf0e6..1ccb724247d 100644 --- a/src/android.c +++ b/src/android.c @@ -2267,6 +2267,12 @@ NATIVE_NAME (shouldForwardMultimediaButtons) (JNIEnv *env, return !android_pass_multimedia_buttons_to_system; } +JNIEXPORT jboolean JNICALL +NATIVE_NAME (shouldForwardCtrlSpace) (JNIEnv *env, jobject object) +{ + return !android_intercept_control_space; +} + JNIEXPORT void JNICALL NATIVE_NAME (blitRect) (JNIEnv *env, jobject object, jobject src, jobject dest, diff --git a/src/androidfns.c b/src/androidfns.c index 9e8372f524b..51421f0a68a 100644 --- a/src/androidfns.c +++ b/src/androidfns.c @@ -3205,6 +3205,19 @@ Note that if you set this, you will no longer be able to quit Emacs using the volume down button. */); android_pass_multimedia_buttons_to_system = false; + DEFVAR_BOOL ("android-intercept-control-space", + android_intercept_control_space, + doc: /* Whether Emacs should intercept C-SPC. +When this variable is set, Emacs intercepts C-SPC events as they are +delivered to a frame before they are registered and filtered by the +input method. + +For no apparent purpose, Android input methods customarily discard SPC +events with the Ctrl modifier set without delivering them to Emacs +afterwards, which is an impediment to typing key sequences +incorporating such keys. */); + android_intercept_control_space = true; + DEFVAR_BOOL ("android-use-exec-loader", android_use_exec_loader, doc: /* Whether or not to bypass system restrictions on program execution.