Skip to content

Commit

Permalink
Unbuffer MotionEvent objects for touch events to avoid ignoring points
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Hagb authored and twaik committed Jan 12, 2025
1 parent efb2d97 commit a461e13
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion app/src/main/java/com/termux/x11/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down

0 comments on commit a461e13

Please sign in to comment.