]> git.eshelyaron.com Git - emacs.git/commitdiff
Facilitate typing `C-SPC' on Android
authorPo Lu <luangruo@yahoo.com>
Wed, 30 Aug 2023 02:07:49 +0000 (10:07 +0800)
committerPo Lu <luangruo@yahoo.com>
Wed, 30 Aug 2023 02:07:49 +0000 (10:07 +0800)
* 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)
<android_intercept_control_space>: New defvar.

doc/emacs/android.texi
java/org/gnu/emacs/EmacsNative.java
java/org/gnu/emacs/EmacsView.java
java/org/gnu/emacs/EmacsWindow.java
src/android.c
src/androidfns.c

index 5e7ff0e4bb317caa92eb50b046b3bc4de18e31c6..c6f169e30061e8f684aac70054edde52408e5185 100644 (file)
@@ -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
index fae0ba98f86bf0022384939aa4501f3771e4942b..a4b45aafbc14e2324945a8b5eab445a82a41aed9 100644 (file)
@@ -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 ();
index 5a4bcbaa005d4435dfd51b7d25946839f36bd1a3..04c3d824027eea38cb13f1ee8070f32f8de7f2a9 100644 (file)
@@ -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)
index aff5046b22e7b31160e09231c367278f5f73c6be..3738376a6f45a293523e02452d1e1fcae83d73fa 100644 (file)
@@ -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;
index ed304baf0e6ae8b3bf18da98a99ff76d61e6443d..1ccb724247d7ec8d14c164afe9d6f6a1e3345057 100644 (file)
@@ -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,
index 9e8372f524b055ff0114ce2368d23930887a42b5..51421f0a68ae39be0016a5c67a18826703a49004 100644 (file)
@@ -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.