]> git.eshelyaron.com Git - emacs.git/commitdiff
Guarantee files are auto-saved when Emacs is terminated by Android
authorPo Lu <luangruo@yahoo.com>
Thu, 9 Nov 2023 02:12:24 +0000 (10:12 +0800)
committerPo Lu <luangruo@yahoo.com>
Thu, 9 Nov 2023 02:12:24 +0000 (10:12 +0800)
* java/org/gnu/emacs/EmacsNative.java (shutDownEmacs):

* java/org/gnu/emacs/EmacsService.java (onDestroy): New
function.  When invoked, call shut_down_emacs and await its
completion.

* src/android.c (android_shut_down_emacs, shutDownEmacs): New
functions.

java/org/gnu/emacs/EmacsNative.java
java/org/gnu/emacs/EmacsService.java
src/android.c

index f15927bb3a7c20a5cd654b77aaa210389c13dd82..946a38f7f847f602ddb1276c548ff62098ef21be 100644 (file)
@@ -92,6 +92,10 @@ public final class EmacsNative
      loadup.el itself.  */
   public static native void initEmacs (String argv[], String dumpFile);
 
+  /* Call shut_down_emacs to auto-save and unlock files in the main
+     thread, then return.  */
+  public static native void shutDownEmacs ();
+
   /* Abort and generate a native core dump.  */
   public static native void emacsAbort ();
 
index 1325cd85e9bcb86aee3900cdc317fd31b064b512..ab6d57b9c4f35f07cc7b0452f4877e343c0091df 100644 (file)
@@ -321,6 +321,18 @@ public final class EmacsService extends Service
       }
   }
 
+  @Override
+  public void
+  onDestroy ()
+  {
+    /* This function is called immediately before the system kills
+       Emacs.  In this respect, it is rather akin to a SIGDANGER
+       signal, so force an auto-save accordingly.  */
+
+    EmacsNative.shutDownEmacs ();
+    super.onDestroy ();
+  }
+
 \f
 
   /* Functions from here on must only be called from the Emacs
index 3397ec0e740375cb1a4bad7662e5b321f47c5a6f..f5af742b4224235b32e943ca991e6edf4b1a93b8 100644 (file)
@@ -1938,6 +1938,25 @@ NATIVE_NAME (quit) (JNIEnv *env, jobject object)
   kill (getpid (), SIGIO);
 }
 
+/* Call shut_down_emacs subsequent to a call to the service's
+   onDestroy callback.  CLOSURE is ignored.  */
+
+static void
+android_shut_down_emacs (void *closure)
+{
+  __android_log_print (ANDROID_LOG_INFO, __func__,
+                      "The Emacs service is being shut down");
+  shut_down_emacs (0, Qnil);
+}
+
+JNIEXPORT void JNICALL
+NATIVE_NAME (shutDownEmacs) (JNIEnv *env, jobject object)
+{
+  JNI_STACK_ALIGNMENT_PROLOGUE;
+
+  android_run_in_emacs_thread (android_shut_down_emacs, NULL);
+}
+
 JNIEXPORT jlong JNICALL
 NATIVE_NAME (sendConfigureNotify) (JNIEnv *env, jobject object,
                                   jshort window, jlong time,