From a461e13d1ad769a1c717f5b9a9d7950028a63f25 Mon Sep 17 00:00:00 2001 From: "Hagb (Junyu Guo)" Date: Sat, 11 Jan 2025 02:24:34 +0800 Subject: [PATCH] Unbuffer `MotionEvent` objects for touch events to avoid ignoring points and reduce latency It fixes the problem that, when the touchscreen/stylus sampling rate is higher than the screen refresh rate, multiple points from the touchscreen/stylus are batched into a single MotionEvent object with extra latency, and all points of this object except the most current one are ignored. For reference: - https://developer.android.com/develop/ui/views/touch-and-input/stylus-input/advanced-stylus-features#rendering - https://developer.android.com/reference/android/view/MotionEvent#batching --- app/src/main/java/com/termux/x11/MainActivity.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/com/termux/x11/MainActivity.java b/app/src/main/java/com/termux/x11/MainActivity.java index 9e9674dfe..557f97b69 100644 --- a/app/src/main/java/com/termux/x11/MainActivity.java +++ b/app/src/main/java/com/termux/x11/MainActivity.java @@ -177,7 +177,14 @@ protected void onCreate(Bundle savedInstanceState) { return result; }; - lorieParent.setOnTouchListener((v, e) -> mInputHandler.handleTouchEvent(lorieParent, lorieView, e)); + lorieParent.setOnTouchListener((v, e) -> { + // Avoid batched MotionEvent objects and reduce potential latency. + // For reference: https://developer.android.com/develop/ui/views/touch-and-input/stylus-input/advanced-stylus-features#rendering. + if (e.getAction() == MotionEvent.ACTION_DOWN) + lorieParent.requestUnbufferedDispatch(e); + + return mInputHandler.handleTouchEvent(lorieParent, lorieView, e); + }); lorieParent.setOnHoverListener((v, e) -> mInputHandler.handleTouchEvent(lorieParent, lorieView, e)); lorieParent.setOnGenericMotionListener((v, e) -> mInputHandler.handleTouchEvent(lorieParent, lorieView, e)); lorieView.setOnCapturedPointerListener((v, e) -> mInputHandler.handleTouchEvent(lorieView, lorieView, e));