From fbc393756e53e827844b236d7ab457c3ffb44ac3 Mon Sep 17 00:00:00 2001 From: Tristan Simas Date: Mon, 2 Nov 2026 02:57:36 -0500 Subject: [PATCH 1/9] Send right mouse click for finger long press during mouse tracking --- .../termux/view/TerminalViewMouseTest.java | 133 ++++++++++++++++++ .../com/termux/terminal/TerminalEmulator.java | 1 + .../java/com/termux/view/TerminalView.java | 6 + 3 files changed, 140 insertions(+) create mode 100644 app/src/test/java/com/termux/view/TerminalViewMouseTest.java diff --git a/app/src/test/java/com/termux/view/TerminalViewMouseTest.java b/app/src/test/java/com/termux/view/TerminalViewMouseTest.java new file mode 100644 index 00000000000..7f847ec16cc --- /dev/null +++ b/app/src/test/java/com/termux/view/TerminalViewMouseTest.java @@ -0,0 +1,133 @@ +package com.termux.view; + +import android.app.Application; +import android.os.Looper; +import android.os.SystemClock; +import android.view.InputDevice; +import android.view.MotionEvent; +import android.view.ViewConfiguration; + +import com.termux.shared.termux.terminal.TermuxTerminalSessionClientBase; +import com.termux.shared.termux.terminal.TermuxTerminalViewClientBase; +import com.termux.terminal.TerminalEmulator; +import com.termux.terminal.TerminalOutput; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RobolectricTestRunner; +import org.robolectric.RuntimeEnvironment; +import org.robolectric.annotation.Config; +import org.robolectric.annotation.LooperMode; +import org.robolectric.Shadows; + +import java.io.ByteArrayOutputStream; +import java.nio.charset.StandardCharsets; +import java.util.concurrent.TimeUnit; + +import static org.junit.Assert.*; + +@RunWith(RobolectricTestRunner.class) +@Config(sdk = 28, manifest = Config.NONE, application = Application.class) +@LooperMode(LooperMode.Mode.PAUSED) +public class TerminalViewMouseTest { + private final RecordingOutput output = new RecordingOutput(); + private final RecordingClient client = new RecordingClient(); + private TerminalView view; + + @Before + public void setUp() { + view = new TerminalView(RuntimeEnvironment.getApplication(), null); + view.setTerminalViewClient(client); + view.setTextSize(16); + view.mEmulator = new TerminalEmulator(output, 80, 24, 8, 16, null, + new TermuxTerminalSessionClientBase()); + } + + private void input(String text) { + byte[] bytes = text.getBytes(StandardCharsets.UTF_8); + view.mEmulator.append(bytes, bytes.length); + } + + private void longPress(int source) { + long down = SystemClock.uptimeMillis(); + float x = 2.5f * view.mRenderer.mFontWidth; + float y = view.mRenderer.mFontLineSpacingAndAscent + 3.5f * view.mRenderer.mFontLineSpacing; + MotionEvent event = MotionEvent.obtain(down, down, MotionEvent.ACTION_DOWN, x, y, 0); + event.setSource(source); + view.onTouchEvent(event); + event.recycle(); + Shadows.shadowOf(Looper.getMainLooper()).idleFor( + ViewConfiguration.getTapTimeout() + ViewConfiguration.getLongPressTimeout() + 1, + TimeUnit.MILLISECONDS); + event = MotionEvent.obtain(down, SystemClock.uptimeMillis(), MotionEvent.ACTION_UP, x, y, 0); + event.setSource(source); + view.onTouchEvent(event); + event.recycle(); + } + + @Test + public void sgrLongPressReportsRightPressAndReleaseWithoutLeftTap() { + input("\033[?1000h\033[?1006h"); + longPress(InputDevice.SOURCE_TOUCHSCREEN); + assertEquals("\033[<2;3;4M\033[<2;3;4m", output.text()); + assertEquals(0, client.longPresses); + assertEquals(0, client.taps); + assertFalse(view.isSelectingText()); + } + + @Test + public void legacyLongPressUsesExistingReleaseEncoding() { + input("\033[?1002h"); + longPress(InputDevice.SOURCE_TOUCHSCREEN); + assertEquals("\033[M\"#$\033[M##$", output.text()); + assertEquals(0, client.longPresses); + } + + @Test + public void trackingOffKeepsClientLongPressAndDoesNotEmitMouseInput() { + longPress(InputDevice.SOURCE_TOUCHSCREEN); + assertEquals("", output.text()); + assertEquals(1, client.longPresses); + assertEquals(0, client.taps); + } + + @Test + public void mouseDeviceLongPressKeepsClientCallbackAndLeftButtonPath() { + input("\033[?1000h\033[?1006h"); + longPress(InputDevice.SOURCE_MOUSE); + assertEquals("\033[<0;3;4M\033[<0;3;4m", output.text()); + assertEquals(1, client.longPresses); + } + + private static final class RecordingClient extends TermuxTerminalViewClientBase { + int longPresses; + int taps; + + @Override + public boolean onLongPress(MotionEvent event) { + longPresses++; + return true; + } + + @Override + public void onSingleTapUp(MotionEvent event) { + taps++; + } + } + + private static final class RecordingOutput extends TerminalOutput { + private final ByteArrayOutputStream bytes = new ByteArrayOutputStream(); + + String text() { + return new String(bytes.toByteArray(), StandardCharsets.UTF_8); + } + + @Override public void write(byte[] data, int offset, int count) { bytes.write(data, offset, count); } + @Override public void titleChanged(String oldTitle, String newTitle) { } + @Override public void onCopyTextToClipboard(String text) { } + @Override public void onPasteTextFromClipboard() { } + @Override public void onBell() { } + @Override public void onColorsChanged() { } + } +} diff --git a/terminal-emulator/src/main/java/com/termux/terminal/TerminalEmulator.java b/terminal-emulator/src/main/java/com/termux/terminal/TerminalEmulator.java index 5e943ab5fc1..6db0a7d0a1e 100644 --- a/terminal-emulator/src/main/java/com/termux/terminal/TerminalEmulator.java +++ b/terminal-emulator/src/main/java/com/termux/terminal/TerminalEmulator.java @@ -34,6 +34,7 @@ public final class TerminalEmulator { private static final boolean LOG_ESCAPE_SEQUENCES = false; public static final int MOUSE_LEFT_BUTTON = 0; + public static final int MOUSE_RIGHT_BUTTON = 2; /** Mouse moving while having left mouse button pressed. */ public static final int MOUSE_LEFT_BUTTON_MOVED = 32; diff --git a/terminal-view/src/main/java/com/termux/view/TerminalView.java b/terminal-view/src/main/java/com/termux/view/TerminalView.java index f4f82ad7c51..40c98b350ac 100644 --- a/terminal-view/src/main/java/com/termux/view/TerminalView.java +++ b/terminal-view/src/main/java/com/termux/view/TerminalView.java @@ -249,6 +249,12 @@ public boolean onDoubleTap(MotionEvent event) { @Override public void onLongPress(MotionEvent event) { if (mGestureRecognizer.isInProgress()) return; + if (mEmulator != null && mEmulator.isMouseTrackingActive() + && !event.isFromSource(InputDevice.SOURCE_MOUSE) && !isSelectingText()) { + sendMouseEventCode(event, TerminalEmulator.MOUSE_RIGHT_BUTTON, true); + sendMouseEventCode(event, TerminalEmulator.MOUSE_RIGHT_BUTTON, false); + return; + } if (mClient.onLongPress(event)) return; if (!isSelectingText()) { performHapticFeedback(HapticFeedbackConstants.LONG_PRESS); From a677b83c7ebfaa9537442d7d66c69190e45e95ba Mon Sep 17 00:00:00 2001 From: Tristan Simas Date: Mon, 2 Nov 2026 03:06:33 -0500 Subject: [PATCH 2/9] Send horizontal mouse wheel events for dominant-axis finger swipes --- .../termux/view/TerminalViewMouseTest.java | 160 ++++++++++++++++++ .../com/termux/terminal/TerminalEmulator.java | 2 + .../java/com/termux/view/TerminalView.java | 48 +++++- 3 files changed, 201 insertions(+), 9 deletions(-) create mode 100644 app/src/test/java/com/termux/view/TerminalViewMouseTest.java diff --git a/app/src/test/java/com/termux/view/TerminalViewMouseTest.java b/app/src/test/java/com/termux/view/TerminalViewMouseTest.java new file mode 100644 index 00000000000..1f0d452e6ff --- /dev/null +++ b/app/src/test/java/com/termux/view/TerminalViewMouseTest.java @@ -0,0 +1,160 @@ +package com.termux.view; + +import android.app.Application; +import android.os.SystemClock; +import android.view.InputDevice; +import android.view.MotionEvent; + +import com.termux.shared.termux.terminal.TermuxTerminalSessionClientBase; +import com.termux.shared.termux.terminal.TermuxTerminalViewClientBase; +import com.termux.terminal.TerminalEmulator; +import com.termux.terminal.TerminalOutput; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RobolectricTestRunner; +import org.robolectric.RuntimeEnvironment; +import org.robolectric.annotation.Config; + +import java.io.ByteArrayOutputStream; +import java.nio.charset.StandardCharsets; + +import static org.junit.Assert.*; + +@RunWith(RobolectricTestRunner.class) +@Config(sdk = 28, manifest = Config.NONE, application = Application.class) +public class TerminalViewMouseTest { + private final RecordingOutput output = new RecordingOutput(); + private TerminalView view; + + @Before + public void setUp() { + view = new TerminalView(RuntimeEnvironment.getApplication(), null); + view.setTerminalViewClient(new TermuxTerminalViewClientBase()); + view.setTextSize(16); + view.mEmulator = new TerminalEmulator(output, 80, 24, 8, 16, null, + new TermuxTerminalSessionClientBase()); + view.mGestureRecognizer.mListener.onDown(0, 0); + } + + private void input(String text) { + byte[] bytes = text.getBytes(StandardCharsets.UTF_8); + view.mEmulator.append(bytes, bytes.length); + } + + private MotionEvent event(long down, int action, float column, float row, int source) { + MotionEvent event = MotionEvent.obtain(down, SystemClock.uptimeMillis(), action, + column * view.mRenderer.mFontWidth, + view.mRenderer.mFontLineSpacingAndAscent + row * view.mRenderer.mFontLineSpacing, 0); + event.setSource(source); + return event; + } + + private void scroll(float columns, float rows) { + MotionEvent event = event(1, MotionEvent.ACTION_MOVE, 2.5f, 3.5f, InputDevice.SOURCE_TOUCHSCREEN); + view.mGestureRecognizer.mListener.onScroll(event, + columns * view.mRenderer.mFontWidth, rows * view.mRenderer.mFontLineSpacing); + event.recycle(); + } + + @Test + public void horizontalFractionalMotionUsesRightWheelAndLocksAxis() { + input("\033[?1000h\033[?1006h"); + scroll(0.4f, 0); + assertEquals("", output.text()); + scroll(0.7f, 0); + assertEquals("\033[<67;3;4M", output.text()); + scroll(1, 4); + assertEquals("\033[<67;3;4M\033[<67;3;4M", output.text()); + } + + @Test + public void negativeMotionUsesLeftWheelThroughLegacyEncoder() { + input("\033[?1002h"); + scroll(-2.25f, 0); + assertEquals("\033[Mb#$\033[Mb#$", output.text()); + } + + @Test + public void verticalGestureKeepsUpWheelOriginAndAxis() { + input("\033[?1000h\033[?1006h"); + scroll(0, -1); + MotionEvent event = event(1, MotionEvent.ACTION_MOVE, 9.5f, 8.5f, InputDevice.SOURCE_TOUCHSCREEN); + view.mGestureRecognizer.mListener.onScroll(event, 100, -view.mRenderer.mFontLineSpacing); + event.recycle(); + assertEquals("\033[<64;3;4M\033[<64;3;4M", output.text()); + } + + @Test + public void trackingOffDoesNotSendHorizontalWheel() { + scroll(4, 0); + assertEquals("", output.text()); + assertEquals(0, view.getTopRow()); + } + + @Test + public void mouseDragStillSendsMotionRatherThanWheel() { + input("\033[?1002h\033[?1006h"); + MotionEvent event = event(1, MotionEvent.ACTION_MOVE, 2.5f, 3.5f, InputDevice.SOURCE_MOUSE); + view.mGestureRecognizer.mListener.onScroll(event, 100, 0); + event.recycle(); + assertEquals("\033[<32;3;4M", output.text()); + } + + @Test + public void horizontalGestureDoesNotStartVerticalFling() { + input("\033[?1000h\033[?1006h"); + scroll(2, 0); + MotionEvent event = event(1, MotionEvent.ACTION_UP, 2.5f, 3.5f, InputDevice.SOURCE_TOUCHSCREEN); + view.mGestureRecognizer.mListener.onFling(event, -1000, 2000); + event.recycle(); + assertTrue(view.mScroller.isFinished()); + assertEquals("\033[<67;3;4M\033[<67;3;4M", output.text()); + } + + @Test + public void nextDownClearsCancelledGestureFractionAndAxis() { + input("\033[?1000h\033[?1006h"); + scroll(0.8f, 0); + MotionEvent cancel = event(1, MotionEvent.ACTION_CANCEL, 2.5f, 3.5f, InputDevice.SOURCE_TOUCHSCREEN); + view.onTouchEvent(cancel); + cancel.recycle(); + MotionEvent down = event(2, MotionEvent.ACTION_DOWN, 2.5f, 3.5f, InputDevice.SOURCE_TOUCHSCREEN); + view.onTouchEvent(down); + down.recycle(); + scroll(0.3f, 0); + assertEquals("", output.text()); + } + + @Test + public void actualTouchSwipeDoesNotAlsoProduceVerticalWheelOrLeftTap() { + input("\033[?1000h\033[?1006h"); + long time = SystemClock.uptimeMillis(); + MotionEvent down = event(time, MotionEvent.ACTION_DOWN, 30.5f, 3.5f, InputDevice.SOURCE_TOUCHSCREEN); + view.onTouchEvent(down); + down.recycle(); + MotionEvent move = event(time, MotionEvent.ACTION_MOVE, 10.5f, 3.5f, InputDevice.SOURCE_TOUCHSCREEN); + view.onTouchEvent(move); + move.recycle(); + MotionEvent up = event(time, MotionEvent.ACTION_UP, 10.5f, 3.5f, InputDevice.SOURCE_TOUCHSCREEN); + view.onTouchEvent(up); + up.recycle(); + assertFalse(output.text().isEmpty()); + assertFalse(output.text().contains("\033[<0;")); + assertFalse(output.text().contains("\033[<64;")); + assertFalse(output.text().contains("\033[<65;")); + assertTrue(output.text().contains("\033[<67;")); + } + + private static final class RecordingOutput extends TerminalOutput { + private final ByteArrayOutputStream bytes = new ByteArrayOutputStream(); + String text() { return new String(bytes.toByteArray(), StandardCharsets.UTF_8); } + @Override public void write(byte[] data, int offset, int count) { bytes.write(data, offset, count); } + @Override public void titleChanged(String oldTitle, String newTitle) { } + @Override public void onCopyTextToClipboard(String text) { } + @Override public void onPasteTextFromClipboard() { } + @Override public void onBell() { } + @Override public void onColorsChanged() { } + } +} diff --git a/terminal-emulator/src/main/java/com/termux/terminal/TerminalEmulator.java b/terminal-emulator/src/main/java/com/termux/terminal/TerminalEmulator.java index 5e943ab5fc1..42657375f7a 100644 --- a/terminal-emulator/src/main/java/com/termux/terminal/TerminalEmulator.java +++ b/terminal-emulator/src/main/java/com/termux/terminal/TerminalEmulator.java @@ -39,6 +39,8 @@ public final class TerminalEmulator { public static final int MOUSE_LEFT_BUTTON_MOVED = 32; public static final int MOUSE_WHEELUP_BUTTON = 64; public static final int MOUSE_WHEELDOWN_BUTTON = 65; + public static final int MOUSE_WHEELLEFT_BUTTON = 66; + public static final int MOUSE_WHEELRIGHT_BUTTON = 67; /** Used for invalid data - http://en.wikipedia.org/wiki/Replacement_character#Replacement_character */ public static final int UNICODE_REPLACEMENT_CHAR = 0xFFFD; diff --git a/terminal-view/src/main/java/com/termux/view/TerminalView.java b/terminal-view/src/main/java/com/termux/view/TerminalView.java index f4f82ad7c51..60ff96c8bdc 100644 --- a/terminal-view/src/main/java/com/termux/view/TerminalView.java +++ b/terminal-view/src/main/java/com/termux/view/TerminalView.java @@ -138,6 +138,7 @@ public TerminalView(Context context, AttributeSet attributes) { // NO_UCD (unuse mGestureRecognizer = new GestureAndScaleRecognizer(context, new GestureAndScaleRecognizer.Listener() { boolean scrolledWithFinger; + boolean scrolledHorizontally; @Override public boolean onUp(MotionEvent event) { @@ -150,6 +151,7 @@ public boolean onUp(MotionEvent event) { return true; } scrolledWithFinger = false; + scrolledHorizontally = false; return false; } @@ -176,11 +178,25 @@ public boolean onScroll(MotionEvent e, float distanceX, float distanceY) { // which we do not do for touch input, only mouse in onTouchEvent(). sendMouseEventCode(e, TerminalEmulator.MOUSE_LEFT_BUTTON_MOVED, true); } else { + if (!scrolledWithFinger) { + // Lock the axis at the first scroll, after GestureDetector's touch slop. + scrolledHorizontally = mEmulator.isMouseTrackingActive() && !isSelectingText() + && Math.abs(distanceX) > Math.abs(distanceY); + } scrolledWithFinger = true; - distanceY += mScrollRemainder; - int deltaRows = (int) (distanceY / mRenderer.mFontLineSpacing); - mScrollRemainder = distanceY - deltaRows * mRenderer.mFontLineSpacing; - doScroll(e, deltaRows); + if (scrolledHorizontally && !mEmulator.isMouseTrackingActive()) { + scrolledHorizontally = false; + mScrollRemainder = 0.0f; + } + float cellSize = scrolledHorizontally ? mRenderer.mFontWidth : mRenderer.mFontLineSpacing; + float distance = (scrolledHorizontally ? distanceX : distanceY) + mScrollRemainder; + int delta = (int) (distance / cellSize); + mScrollRemainder = distance - delta * cellSize; + if (scrolledHorizontally) { + doMouseScroll(e, delta, TerminalEmulator.MOUSE_WHEELLEFT_BUTTON, TerminalEmulator.MOUSE_WHEELRIGHT_BUTTON); + } else { + doScroll(e, delta); + } } return true; } @@ -195,7 +211,7 @@ public boolean onScale(float focusX, float focusY, float scale) { @Override public boolean onFling(final MotionEvent e2, float velocityX, float velocityY) { - if (mEmulator == null) return true; + if (mEmulator == null || scrolledHorizontally) return true; // Do not start scrolling until last fling has been taken care of: if (!mScroller.isFinished()) return true; @@ -231,6 +247,10 @@ public void run() { @Override public boolean onDown(float x, float y) { + // Also reset after a cancelled gesture or a long press, which skips onUp(). + mScrollRemainder = 0.0f; + scrolledWithFinger = false; + scrolledHorizontally = false; // Why is true not returned here? // https://developer.android.com/training/gestures/detector.html#detect-a-subset-of-supported-gestures // Although setting this to true still does not solve the following errors when long pressing in terminal view text area @@ -558,7 +578,7 @@ void sendMouseEventCode(MotionEvent e, int button, boolean pressed) { int[] columnAndRow = getColumnAndRow(e, false); int x = columnAndRow[0] + 1; int y = columnAndRow[1] + 1; - if (pressed && (button == TerminalEmulator.MOUSE_WHEELDOWN_BUTTON || button == TerminalEmulator.MOUSE_WHEELUP_BUTTON)) { + if (pressed && button >= TerminalEmulator.MOUSE_WHEELUP_BUTTON && button <= TerminalEmulator.MOUSE_WHEELRIGHT_BUTTON) { if (mMouseStartDownTime == e.getDownTime()) { x = mMouseScrollStartX; y = mMouseScrollStartY; @@ -573,12 +593,14 @@ void sendMouseEventCode(MotionEvent e, int button, boolean pressed) { /** Perform a scroll, either from dragging the screen or by scrolling a mouse wheel. */ void doScroll(MotionEvent event, int rowsDown) { + if (mEmulator.isMouseTrackingActive()) { + doMouseScroll(event, rowsDown, TerminalEmulator.MOUSE_WHEELUP_BUTTON, TerminalEmulator.MOUSE_WHEELDOWN_BUTTON); + return; + } boolean up = rowsDown < 0; int amount = Math.abs(rowsDown); for (int i = 0; i < amount; i++) { - if (mEmulator.isMouseTrackingActive()) { - sendMouseEventCode(event, up ? TerminalEmulator.MOUSE_WHEELUP_BUTTON : TerminalEmulator.MOUSE_WHEELDOWN_BUTTON, true); - } else if (mEmulator.isAlternateBufferActive()) { + if (mEmulator.isAlternateBufferActive()) { // Send up and down key events for scrolling, which is what some terminals do to make scroll work in // e.g. less, which shifts to the alt screen without mouse handling. handleKeyCode(up ? KeyEvent.KEYCODE_DPAD_UP : KeyEvent.KEYCODE_DPAD_DOWN, 0); @@ -589,6 +611,14 @@ void doScroll(MotionEvent event, int rowsDown) { } } + /** Both wheel axes use the same encoder and gesture-origin cell. */ + void doMouseScroll(MotionEvent event, int steps, int negativeButton, int positiveButton) { + int button = steps < 0 ? negativeButton : positiveButton; + for (int i = 0; i < Math.abs(steps); i++) { + sendMouseEventCode(event, button, true); + } + } + /** Overriding {@link View#onGenericMotionEvent(MotionEvent)}. */ @Override public boolean onGenericMotionEvent(MotionEvent event) { From cca5d449be5569bd0d86e05b10992e701b0a7a0a Mon Sep 17 00:00:00 2001 From: Tristan Simas Date: Mon, 2 Nov 2026 03:20:45 -0500 Subject: [PATCH 3/9] Use native font metrics in gesture tests and decode signer digests independent of labels --- .github/workflows/debug_build.yml | 9 +++++++-- .../test/java/com/termux/view/TerminalViewMouseTest.java | 4 ++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/.github/workflows/debug_build.yml b/.github/workflows/debug_build.yml index 953bbd4b7bb..bb714605675 100644 --- a/.github/workflows/debug_build.yml +++ b/.github/workflows/debug_build.yml @@ -91,16 +91,21 @@ jobs: set -euo pipefail APKSIGNER="$(find "$ANDROID_HOME/build-tools" -type f -name apksigner -print -quit)" test -n "$APKSIGNER" + certificate_digests() { + awk '/certificate SHA-256 digest:/ { print tolower($NF) }' "$1" | sort -u + } OFFICIAL_APK="termux-app_v0.118.3+github-debug_arm64-v8a.apk" curl --fail --location --retry 2 --output "$RUNNER_TEMP/$OFFICIAL_APK" \ "https://github.com/termux/termux-app/releases/download/v0.118.3/$OFFICIAL_APK" printf '%s %s\n' '72fdb596045116bf5ba1b5bdf5b26fddb9acc0bd074ad9f2da9eb0ae85e83a4e' "$RUNNER_TEMP/$OFFICIAL_APK" | sha256sum --check "$APKSIGNER" verify --verbose --print-certs "$RUNNER_TEMP/$OFFICIAL_APK" | tee "$APK_DIR_PATH/signature-official.txt" - grep '^Signer #1 certificate SHA-256 digest:' "$APK_DIR_PATH/signature-official.txt" > "$RUNNER_TEMP/official-signer.txt" + certificate_digests "$APK_DIR_PATH/signature-official.txt" > "$RUNNER_TEMP/official-signer.txt" + test "$(wc -l < "$RUNNER_TEMP/official-signer.txt")" -eq 1 for apk in "$APK_DIR_PATH"/*.apk; do report="$APK_DIR_PATH/signature-$(basename "$apk").txt" "$APKSIGNER" verify --verbose --print-certs "$apk" | tee "$report" - grep '^Signer #1 certificate SHA-256 digest:' "$report" > "$RUNNER_TEMP/fork-signer.txt" + certificate_digests "$report" > "$RUNNER_TEMP/fork-signer.txt" + test "$(wc -l < "$RUNNER_TEMP/fork-signer.txt")" -eq 1 cmp "$RUNNER_TEMP/official-signer.txt" "$RUNNER_TEMP/fork-signer.txt" done diff --git a/app/src/test/java/com/termux/view/TerminalViewMouseTest.java b/app/src/test/java/com/termux/view/TerminalViewMouseTest.java index e4854b1849e..0e90a09b726 100644 --- a/app/src/test/java/com/termux/view/TerminalViewMouseTest.java +++ b/app/src/test/java/com/termux/view/TerminalViewMouseTest.java @@ -18,6 +18,7 @@ import org.robolectric.RobolectricTestRunner; import org.robolectric.RuntimeEnvironment; import org.robolectric.annotation.Config; +import org.robolectric.annotation.GraphicsMode; import org.robolectric.annotation.LooperMode; import org.robolectric.Shadows; @@ -29,6 +30,7 @@ @RunWith(RobolectricTestRunner.class) @Config(sdk = 28, manifest = Config.NONE, application = Application.class) +@GraphicsMode(GraphicsMode.Mode.NATIVE) @LooperMode(LooperMode.Mode.PAUSED) public class TerminalViewMouseTest { private final RecordingOutput output = new RecordingOutput(); @@ -40,6 +42,8 @@ public void setUp() { view = new TerminalView(RuntimeEnvironment.getApplication(), null); view.setTerminalViewClient(client); view.setTextSize(16); + assertTrue("Gesture coordinates require real font width", view.mRenderer.mFontWidth > 0); + assertTrue("Gesture coordinates require real line spacing", view.mRenderer.mFontLineSpacing > 0); view.mEmulator = new TerminalEmulator(output, 80, 24, 8, 16, null, new TermuxTerminalSessionClientBase()); view.mGestureRecognizer.mListener.onDown(0, 0); From 1b628cf10b5a49f0c956877bc0f879cd01ba4ac9 Mon Sep 17 00:00:00 2001 From: Tristan Simas Date: Mon, 2 Nov 2026 03:21:46 -0500 Subject: [PATCH 4/9] Exercise long-press cells with native graphics metrics --- app/src/test/java/com/termux/view/TerminalViewMouseTest.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/app/src/test/java/com/termux/view/TerminalViewMouseTest.java b/app/src/test/java/com/termux/view/TerminalViewMouseTest.java index 7f847ec16cc..99d85c4db10 100644 --- a/app/src/test/java/com/termux/view/TerminalViewMouseTest.java +++ b/app/src/test/java/com/termux/view/TerminalViewMouseTest.java @@ -18,6 +18,7 @@ import org.robolectric.RobolectricTestRunner; import org.robolectric.RuntimeEnvironment; import org.robolectric.annotation.Config; +import org.robolectric.annotation.GraphicsMode; import org.robolectric.annotation.LooperMode; import org.robolectric.Shadows; @@ -29,6 +30,7 @@ @RunWith(RobolectricTestRunner.class) @Config(sdk = 28, manifest = Config.NONE, application = Application.class) +@GraphicsMode(GraphicsMode.Mode.NATIVE) @LooperMode(LooperMode.Mode.PAUSED) public class TerminalViewMouseTest { private final RecordingOutput output = new RecordingOutput(); @@ -40,6 +42,8 @@ public void setUp() { view = new TerminalView(RuntimeEnvironment.getApplication(), null); view.setTerminalViewClient(client); view.setTextSize(16); + assertTrue("Gesture coordinates require real font width", view.mRenderer.mFontWidth > 0); + assertTrue("Gesture coordinates require real line spacing", view.mRenderer.mFontLineSpacing > 0); view.mEmulator = new TerminalEmulator(output, 80, 24, 8, 16, null, new TermuxTerminalSessionClientBase()); } From 0c13945f889096dc7eefeca9f94b1fee16e05feb Mon Sep 17 00:00:00 2001 From: Tristan Simas Date: Mon, 2 Nov 2026 03:22:33 -0500 Subject: [PATCH 5/9] Exercise horizontal gesture cells with native graphics metrics --- app/src/test/java/com/termux/view/TerminalViewMouseTest.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/app/src/test/java/com/termux/view/TerminalViewMouseTest.java b/app/src/test/java/com/termux/view/TerminalViewMouseTest.java index 1f0d452e6ff..77a7cc5717a 100644 --- a/app/src/test/java/com/termux/view/TerminalViewMouseTest.java +++ b/app/src/test/java/com/termux/view/TerminalViewMouseTest.java @@ -16,6 +16,7 @@ import org.robolectric.RobolectricTestRunner; import org.robolectric.RuntimeEnvironment; import org.robolectric.annotation.Config; +import org.robolectric.annotation.GraphicsMode; import java.io.ByteArrayOutputStream; import java.nio.charset.StandardCharsets; @@ -24,6 +25,7 @@ @RunWith(RobolectricTestRunner.class) @Config(sdk = 28, manifest = Config.NONE, application = Application.class) +@GraphicsMode(GraphicsMode.Mode.NATIVE) public class TerminalViewMouseTest { private final RecordingOutput output = new RecordingOutput(); private TerminalView view; @@ -33,6 +35,8 @@ public void setUp() { view = new TerminalView(RuntimeEnvironment.getApplication(), null); view.setTerminalViewClient(new TermuxTerminalViewClientBase()); view.setTextSize(16); + assertTrue("Gesture coordinates require real font width", view.mRenderer.mFontWidth > 0); + assertTrue("Gesture coordinates require real line spacing", view.mRenderer.mFontLineSpacing > 0); view.mEmulator = new TerminalEmulator(output, 80, 24, 8, 16, null, new TermuxTerminalSessionClientBase()); view.mGestureRecognizer.mListener.onDown(0, 0); From c2501e3ee5b94bbc55043f03d854827e2cdb7a5a Mon Sep 17 00:00:00 2001 From: Tristan Simas Date: Mon, 2 Nov 2026 03:47:37 -0500 Subject: [PATCH 6/9] Request soft keyboard once for a cursor-enabled response after a finger tap --- .../terminal/TermuxTerminalViewClient.java | 14 +- .../termux/view/TerminalViewKeyboardTest.java | 150 ++++++++++++++++++ .../com/termux/view/TerminalViewTestCase.java | 106 +++++++++++++ .../java/com/termux/view/TerminalView.java | 27 ++++ .../com/termux/view/TerminalViewClient.java | 3 + 5 files changed, 296 insertions(+), 4 deletions(-) create mode 100644 app/src/test/java/com/termux/view/TerminalViewKeyboardTest.java create mode 100644 app/src/test/java/com/termux/view/TerminalViewTestCase.java diff --git a/app/src/main/java/com/termux/app/terminal/TermuxTerminalViewClient.java b/app/src/main/java/com/termux/app/terminal/TermuxTerminalViewClient.java index 700c5e50980..49b068d5ff3 100644 --- a/app/src/main/java/com/termux/app/terminal/TermuxTerminalViewClient.java +++ b/app/src/main/java/com/termux/app/terminal/TermuxTerminalViewClient.java @@ -199,13 +199,19 @@ public void onSingleTapUp(MotionEvent e) { } if (!term.isMouseTrackingActive() && !e.isFromSource(InputDevice.SOURCE_MOUSE)) { - if (!KeyboardUtils.areDisableSoftKeyboardFlagsSet(mActivity)) - KeyboardUtils.showSoftKeyboard(mActivity, mActivity.getTerminalView()); - else - Logger.logVerbose(LOG_TAG, "Not showing soft keyboard onSingleTapUp since its disabled"); + onRequestSoftKeyboard(); } } + @Override + public void onRequestSoftKeyboard() { + if (!mActivity.isVisible()) return; + if (!KeyboardUtils.areDisableSoftKeyboardFlagsSet(mActivity)) + KeyboardUtils.showSoftKeyboard(mActivity, mActivity.getTerminalView()); + else + Logger.logVerbose(LOG_TAG, "Not showing soft keyboard since its disabled"); + } + @Override public boolean shouldBackButtonBeMappedToEscape() { return mActivity.getProperties().isBackKeyTheEscapeKey(); diff --git a/app/src/test/java/com/termux/view/TerminalViewKeyboardTest.java b/app/src/test/java/com/termux/view/TerminalViewKeyboardTest.java new file mode 100644 index 00000000000..5131bcf16a8 --- /dev/null +++ b/app/src/test/java/com/termux/view/TerminalViewKeyboardTest.java @@ -0,0 +1,150 @@ +package com.termux.view; + +import android.os.SystemClock; +import android.view.InputDevice; +import android.view.MotionEvent; +import android.view.ViewConfiguration; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +/** Tests keyboard-request intent, not Android IME display or a remote phone. */ +public class TerminalViewKeyboardTest extends TerminalViewTestCase { + private void trackingTap() { + input("\033[?1000h\033[?25l"); + tap(InputDevice.SOURCE_TOUCHSCREEN); + } + + private void cursorResponse() { + input("\033[?25h"); + view.onScreenUpdated(); + } + + @Test + public void cursorResponseAfterTrackingTapRequestsKeyboardOnce() { + trackingTap(); + assertEquals(0, client.keyboardRequests); + cursorResponse(); + assertEquals(1, client.keyboardRequests); + cursorResponse(); + view.onScreenUpdated(); + assertEquals(1, client.keyboardRequests); + } + + @Test + public void unsolicitedCursorVisibilityDoesNotRequestKeyboard() { + input("\033[?1000h"); + cursorResponse(); + assertEquals(0, client.keyboardRequests); + } + + @Test + public void animationPhaseDoesNotSubstituteForEnabledCursor() { + trackingTap(); + view.mEmulator.setCursorBlinkState(true); + view.onScreenUpdated(); + assertEquals(0, client.keyboardRequests); + cursorResponse(); + assertEquals(1, client.keyboardRequests); + view.mEmulator.setCursorBlinkState(false); + view.mEmulator.setCursorBlinkState(true); + view.onScreenUpdated(); + assertEquals(1, client.keyboardRequests); + } + + @Test + public void responseAfterRecentTapWindowHasExpiredIsIgnored() { + trackingTap(); + advance(TerminalView.MOUSE_TAP_KEYBOARD_TIMEOUT_MILLIS + 1); + cursorResponse(); + assertEquals(0, client.keyboardRequests); + } + + @Test + public void newDownInvalidatesEarlierTap() { + trackingTap(); + touch(SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN, 2.5f, 3.5f, InputDevice.SOURCE_TOUCHSCREEN); + cursorResponse(); + assertEquals(0, client.keyboardRequests); + } + + @Test + public void cancelInvalidatesEarlierTap() { + trackingTap(); + touch(SystemClock.uptimeMillis(), MotionEvent.ACTION_CANCEL, 2.5f, 3.5f, InputDevice.SOURCE_TOUCHSCREEN); + cursorResponse(); + assertEquals(0, client.keyboardRequests); + } + + @Test + public void lostWindowFocusInvalidatesEarlierTap() { + trackingTap(); + view.onWindowFocusChanged(false); + cursorResponse(); + assertEquals(0, client.keyboardRequests); + } + + @Test + public void detachInvalidatesEarlierTap() { + trackingTap(); + view.onDetachedFromWindow(); + cursorResponse(); + assertEquals(0, client.keyboardRequests); + } + + @Test + public void trackingOffClearsAuthorizationBeforeTrackingReturns() { + trackingTap(); + input("\033[?1000l"); + view.onScreenUpdated(); + input("\033[?1000h"); + cursorResponse(); + assertEquals(0, client.keyboardRequests); + } + + @Test + public void trackingOffTapKeepsExistingClientCallback() { + tap(InputDevice.SOURCE_TOUCHSCREEN); + advance(ViewConfiguration.getDoubleTapTimeout() + 1); + assertEquals(1, client.taps); + cursorResponse(); + assertEquals(0, client.keyboardRequests); + } + + @Test + public void mouseDeviceTapDoesNotAuthorizeKeyboard() { + input("\033[?1000h\033[?25l"); + tap(InputDevice.SOURCE_MOUSE); + cursorResponse(); + assertEquals(0, client.keyboardRequests); + } + + @Test + public void longPressDoesNotAuthorizeKeyboardOnRelease() { + input("\033[?1000h\033[?25l"); + longPress(InputDevice.SOURCE_TOUCHSCREEN); + cursorResponse(); + assertEquals(0, client.keyboardRequests); + } + + @Test + public void fingerScrollDoesNotAuthorizeKeyboardOnRelease() { + input("\033[?1000h\033[?25l"); + long down = SystemClock.uptimeMillis(); + touch(down, MotionEvent.ACTION_DOWN, 2.5f, 15.5f, InputDevice.SOURCE_TOUCHSCREEN); + touch(down, MotionEvent.ACTION_MOVE, 2.5f, 3.5f, InputDevice.SOURCE_TOUCHSCREEN); + touch(down, MotionEvent.ACTION_UP, 2.5f, 3.5f, InputDevice.SOURCE_TOUCHSCREEN); + cursorResponse(); + assertEquals(0, client.keyboardRequests); + } + + @Test + public void anotherTapCanRequestKeyboardForAnAlreadyEnabledCursor() { + trackingTap(); + cursorResponse(); + tap(InputDevice.SOURCE_TOUCHSCREEN); + view.onScreenUpdated(); + assertEquals(2, client.keyboardRequests); + } +} diff --git a/app/src/test/java/com/termux/view/TerminalViewTestCase.java b/app/src/test/java/com/termux/view/TerminalViewTestCase.java new file mode 100644 index 00000000000..523d1d617ef --- /dev/null +++ b/app/src/test/java/com/termux/view/TerminalViewTestCase.java @@ -0,0 +1,106 @@ +package com.termux.view; + +import android.app.Application; +import android.os.Looper; +import android.os.SystemClock; +import android.view.MotionEvent; +import android.view.ViewConfiguration; + +import com.termux.shared.termux.terminal.TermuxTerminalSessionClientBase; +import com.termux.shared.termux.terminal.TermuxTerminalViewClientBase; +import com.termux.terminal.TerminalEmulator; +import com.termux.terminal.TerminalOutput; + +import org.junit.Before; +import org.junit.runner.RunWith; +import org.robolectric.RobolectricTestRunner; +import org.robolectric.RuntimeEnvironment; +import org.robolectric.Shadows; +import org.robolectric.annotation.Config; +import org.robolectric.annotation.GraphicsMode; +import org.robolectric.annotation.LooperMode; + +import java.io.ByteArrayOutputStream; +import java.nio.charset.StandardCharsets; +import java.util.concurrent.TimeUnit; + +import static org.junit.Assert.assertTrue; + +/** Shared real-view/recognizer/emulator fixture; no renderer or gesture mocks. */ +@RunWith(RobolectricTestRunner.class) +@Config(sdk = 28, manifest = Config.NONE, application = Application.class) +@GraphicsMode(GraphicsMode.Mode.NATIVE) +@LooperMode(LooperMode.Mode.PAUSED) +public abstract class TerminalViewTestCase { + protected final RecordingOutput output = new RecordingOutput(); + protected final RecordingClient client = new RecordingClient(); + protected TerminalView view; + + @Before + public void setUp() { + view = new TerminalView(RuntimeEnvironment.getApplication(), null); + view.setTerminalViewClient(client); + view.setTextSize(16); + assertTrue("Gesture coordinates require real font width", view.mRenderer.mFontWidth > 0); + assertTrue("Gesture coordinates require real line spacing", view.mRenderer.mFontLineSpacing > 0); + view.mEmulator = new TerminalEmulator(output, 80, 24, 8, 16, null, + new TermuxTerminalSessionClientBase()); + view.mGestureRecognizer.mListener.onDown(0, 0); + } + + protected void input(String text) { + byte[] bytes = text.getBytes(StandardCharsets.UTF_8); + view.mEmulator.append(bytes, bytes.length); + } + + protected MotionEvent event(long down, int action, float column, float row, int source) { + MotionEvent event = MotionEvent.obtain(down, SystemClock.uptimeMillis(), action, + column * view.mRenderer.mFontWidth, + view.mRenderer.mFontLineSpacingAndAscent + row * view.mRenderer.mFontLineSpacing, 0); + event.setSource(source); + return event; + } + + protected void touch(long down, int action, float column, float row, int source) { + MotionEvent event = event(down, action, column, row, source); + view.onTouchEvent(event); + event.recycle(); + } + + protected void tap(int source) { + long down = SystemClock.uptimeMillis(); + touch(down, MotionEvent.ACTION_DOWN, 2.5f, 3.5f, source); + touch(down, MotionEvent.ACTION_UP, 2.5f, 3.5f, source); + } + + protected void longPress(int source) { + long down = SystemClock.uptimeMillis(); + touch(down, MotionEvent.ACTION_DOWN, 2.5f, 3.5f, source); + advance(ViewConfiguration.getTapTimeout() + ViewConfiguration.getLongPressTimeout() + 1); + touch(down, MotionEvent.ACTION_UP, 2.5f, 3.5f, source); + } + + protected void advance(long milliseconds) { + Shadows.shadowOf(Looper.getMainLooper()).idleFor(milliseconds, TimeUnit.MILLISECONDS); + } + + protected static final class RecordingClient extends TermuxTerminalViewClientBase { + int longPresses; + int taps; + int keyboardRequests; + @Override public boolean onLongPress(MotionEvent event) { longPresses++; return true; } + @Override public void onSingleTapUp(MotionEvent event) { taps++; } + @Override public void onRequestSoftKeyboard() { keyboardRequests++; } + } + + protected static final class RecordingOutput extends TerminalOutput { + private final ByteArrayOutputStream bytes = new ByteArrayOutputStream(); + String text() { return new String(bytes.toByteArray(), StandardCharsets.UTF_8); } + @Override public void write(byte[] data, int offset, int count) { bytes.write(data, offset, count); } + @Override public void titleChanged(String oldTitle, String newTitle) { } + @Override public void onCopyTextToClipboard(String text) { } + @Override public void onPasteTextFromClipboard() { } + @Override public void onBell() { } + @Override public void onColorsChanged() { } + } +} diff --git a/terminal-view/src/main/java/com/termux/view/TerminalView.java b/terminal-view/src/main/java/com/termux/view/TerminalView.java index f4f82ad7c51..ac1b7280409 100644 --- a/terminal-view/src/main/java/com/termux/view/TerminalView.java +++ b/terminal-view/src/main/java/com/termux/view/TerminalView.java @@ -78,6 +78,10 @@ public final class TerminalView extends View { /** Keep track of the time when a touch event leading to sending mouse scroll events started. */ private long mMouseStartDownTime = -1; + /** A remote cursor response may request the keyboard once after a finger tap. */ + static final long MOUSE_TAP_KEYBOARD_TIMEOUT_MILLIS = 3000; + private long mMouseTapKeyboardDeadline; + final Scroller mScroller; /** What was left in from scrolling movement. */ @@ -145,6 +149,8 @@ public boolean onUp(MotionEvent event) { if (mEmulator != null && mEmulator.isMouseTrackingActive() && !event.isFromSource(InputDevice.SOURCE_MOUSE) && !isSelectingText() && !scrolledWithFinger) { // Quick event processing when mouse tracking is active - do not wait for check of double tapping // for zooming. + requestFocus(); + mMouseTapKeyboardDeadline = SystemClock.uptimeMillis() + MOUSE_TAP_KEYBOARD_TIMEOUT_MILLIS; sendMouseEventCode(event, TerminalEmulator.MOUSE_LEFT_BUTTON, true); sendMouseEventCode(event, TerminalEmulator.MOUSE_LEFT_BUTTON, false); return true; @@ -290,6 +296,7 @@ public void setIsTerminalViewKeyLoggingEnabled(boolean value) { public boolean attachSession(TerminalSession session) { if (session == mTermSession) return false; + mMouseTapKeyboardDeadline = 0; mTermSession = session; mEmulator = null; mCombiningAccent = 0; @@ -459,6 +466,15 @@ public void onScreenUpdated() { public void onScreenUpdated(boolean skipScrolling) { if (mEmulator == null) return; + // Read only this view's emulator: the shared cursor callback has no session identity. + // Use DECTCEM, not the cursor's local animation phase, and consume before showing IME. + if (!mEmulator.isMouseTrackingActive() || isSelectingText()) { + mMouseTapKeyboardDeadline = 0; + } else if (mMouseTapKeyboardDeadline > SystemClock.uptimeMillis() && mEmulator.isCursorEnabled()) { + mMouseTapKeyboardDeadline = 0; + mClient.onRequestSoftKeyboard(); + } + int rowsInHistory = mEmulator.getScreen().getActiveTranscriptRows(); if (mTopRow < -rowsInHistory) setTopRow(-rowsInHistory); @@ -607,6 +623,10 @@ public boolean onGenericMotionEvent(MotionEvent event) { public boolean onTouchEvent(MotionEvent event) { if (mEmulator == null) return true; final int action = event.getAction(); + if (action == MotionEvent.ACTION_DOWN || action == MotionEvent.ACTION_CANCEL + || event.getActionMasked() == MotionEvent.ACTION_POINTER_DOWN) { + mMouseTapKeyboardDeadline = 0; + } if (isSelectingText()) { updateFloatingToolbarVisibility(event); @@ -643,6 +663,12 @@ public boolean onTouchEvent(MotionEvent event) { return true; } + @Override + public void onWindowFocusChanged(boolean hasWindowFocus) { + if (!hasWindowFocus) mMouseTapKeyboardDeadline = 0; + super.onWindowFocusChanged(hasWindowFocus); + } + @Override public boolean onKeyPreIme(int keyCode, KeyEvent event) { if (TERMINAL_VIEW_KEY_LOGGING_ENABLED) @@ -1461,6 +1487,7 @@ protected void onAttachedToWindow() { @Override protected void onDetachedFromWindow() { + mMouseTapKeyboardDeadline = 0; super.onDetachedFromWindow(); if (mTextSelectionCursorController != null) { diff --git a/terminal-view/src/main/java/com/termux/view/TerminalViewClient.java b/terminal-view/src/main/java/com/termux/view/TerminalViewClient.java index d6b49b892ca..95d284fefc9 100644 --- a/terminal-view/src/main/java/com/termux/view/TerminalViewClient.java +++ b/terminal-view/src/main/java/com/termux/view/TerminalViewClient.java @@ -28,6 +28,9 @@ public interface TerminalViewClient { */ void onSingleTapUp(MotionEvent e); + /** Request the client's soft keyboard, subject to its visibility and keyboard preferences. */ + default void onRequestSoftKeyboard() { } + boolean shouldBackButtonBeMappedToEscape(); boolean shouldEnforceCharBasedInput(); From 66fa71632e43193a86c88539964fec3eb63050db Mon Sep 17 00:00:00 2001 From: Tristan Simas Date: Mon, 2 Nov 2026 04:10:37 -0500 Subject: [PATCH 7/9] Keep native long-press selection by default and make tracked right-click opt-in --- .../terminal/TermuxTerminalViewClient.java | 5 ++ .../termux/view/TerminalViewMouseTest.java | 63 ++++++++++++++++++- docs/mouse-long-press.md | 21 +++++++ .../java/com/termux/view/TerminalView.java | 1 + .../com/termux/view/TerminalViewClient.java | 3 + .../properties/TermuxPropertyConstants.java | 30 +++++---- .../properties/TermuxSharedProperties.java | 4 ++ 7 files changed, 109 insertions(+), 18 deletions(-) create mode 100644 docs/mouse-long-press.md diff --git a/app/src/main/java/com/termux/app/terminal/TermuxTerminalViewClient.java b/app/src/main/java/com/termux/app/terminal/TermuxTerminalViewClient.java index 700c5e50980..c5a0af3c515 100644 --- a/app/src/main/java/com/termux/app/terminal/TermuxTerminalViewClient.java +++ b/app/src/main/java/com/termux/app/terminal/TermuxTerminalViewClient.java @@ -206,6 +206,11 @@ public void onSingleTapUp(MotionEvent e) { } } + @Override + public boolean shouldSendRightClickOnLongPress() { + return mActivity.getProperties().shouldSendRightClickOnLongPress(); + } + @Override public boolean shouldBackButtonBeMappedToEscape() { return mActivity.getProperties().isBackKeyTheEscapeKey(); diff --git a/app/src/test/java/com/termux/view/TerminalViewMouseTest.java b/app/src/test/java/com/termux/view/TerminalViewMouseTest.java index 99d85c4db10..54adb8400b8 100644 --- a/app/src/test/java/com/termux/view/TerminalViewMouseTest.java +++ b/app/src/test/java/com/termux/view/TerminalViewMouseTest.java @@ -1,5 +1,6 @@ package com.termux.view; +import android.app.Activity; import android.app.Application; import android.os.Looper; import android.os.SystemClock; @@ -7,15 +8,20 @@ import android.view.MotionEvent; import android.view.ViewConfiguration; +import com.termux.shared.termux.settings.properties.TermuxPropertyConstants; +import com.termux.shared.termux.settings.properties.TermuxSharedProperties; import com.termux.shared.termux.terminal.TermuxTerminalSessionClientBase; import com.termux.shared.termux.terminal.TermuxTerminalViewClientBase; import com.termux.terminal.TerminalEmulator; import com.termux.terminal.TerminalOutput; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; +import org.robolectric.Robolectric; import org.robolectric.RobolectricTestRunner; +import org.robolectric.android.controller.ActivityController; import org.robolectric.RuntimeEnvironment; import org.robolectric.annotation.Config; import org.robolectric.annotation.GraphicsMode; @@ -36,10 +42,13 @@ public class TerminalViewMouseTest { private final RecordingOutput output = new RecordingOutput(); private final RecordingClient client = new RecordingClient(); private TerminalView view; + private ActivityController activity; @Before public void setUp() { - view = new TerminalView(RuntimeEnvironment.getApplication(), null); + activity = Robolectric.buildActivity(Activity.class).setup().visible(); + view = new TerminalView(activity.get(), null); + activity.get().setContentView(view); view.setTerminalViewClient(client); view.setTextSize(16); assertTrue("Gesture coordinates require real font width", view.mRenderer.mFontWidth > 0); @@ -48,6 +57,11 @@ public void setUp() { new TermuxTerminalSessionClientBase()); } + @After + public void tearDown() { + activity.pause().stop().destroy(); + } + private void input(String text) { byte[] bytes = text.getBytes(StandardCharsets.UTF_8); view.mEmulator.append(bytes, bytes.length); @@ -72,6 +86,7 @@ private void longPress(int source) { @Test public void sgrLongPressReportsRightPressAndReleaseWithoutLeftTap() { + client.rightClickOnLongPress = true; input("\033[?1000h\033[?1006h"); longPress(InputDevice.SOURCE_TOUCHSCREEN); assertEquals("\033[<2;3;4M\033[<2;3;4m", output.text()); @@ -82,6 +97,7 @@ public void sgrLongPressReportsRightPressAndReleaseWithoutLeftTap() { @Test public void legacyLongPressUsesExistingReleaseEncoding() { + client.rightClickOnLongPress = true; input("\033[?1002h"); longPress(InputDevice.SOURCE_TOUCHSCREEN); assertEquals("\033[M\"#$\033[M##$", output.text()); @@ -90,6 +106,7 @@ public void legacyLongPressUsesExistingReleaseEncoding() { @Test public void trackingOffKeepsClientLongPressAndDoesNotEmitMouseInput() { + client.rightClickOnLongPress = true; longPress(InputDevice.SOURCE_TOUCHSCREEN); assertEquals("", output.text()); assertEquals(1, client.longPresses); @@ -98,20 +115,62 @@ public void trackingOffKeepsClientLongPressAndDoesNotEmitMouseInput() { @Test public void mouseDeviceLongPressKeepsClientCallbackAndLeftButtonPath() { + client.rightClickOnLongPress = true; input("\033[?1000h\033[?1006h"); longPress(InputDevice.SOURCE_MOUSE); assertEquals("\033[<0;3;4M\033[<0;3;4m", output.text()); assertEquals(1, client.longPresses); } + @Test + public void optedIn1002SgrLongPressAlsoUsesRightButton() { + client.rightClickOnLongPress = true; + input("\033[?1002h\033[?1006h"); + longPress(InputDevice.SOURCE_TOUCHSCREEN); + assertEquals("\033[<2;3;4M\033[<2;3;4m", output.text()); + assertFalse(view.isSelectingText()); + } + + private void assertDefaultSelection(int mode) { + client.handleLongPress = false; + input("\033[?" + mode + "h\033[?1006h"); + longPress(InputDevice.SOURCE_TOUCHSCREEN); + assertEquals("", output.text()); + assertEquals(1, client.longPresses); + assertTrue(view.isSelectingText()); + } + + @Test + public void default1000SgrLongPressSelectsText() { assertDefaultSelection(1000); } + + @Test + public void default1002SgrLongPressSelectsText() { assertDefaultSelection(1002); } + + @Test + public void rightClickPropertyIsExplicitlyOptIn() { + String key = TermuxPropertyConstants.KEY_MOUSE_RIGHT_CLICK_ON_LONG_PRESS; + assertTrue(TermuxPropertyConstants.TERMUX_APP_PROPERTIES_LIST.contains(key)); + assertEquals(Boolean.FALSE, TermuxSharedProperties.getInternalTermuxPropertyValueFromValue( + RuntimeEnvironment.getApplication(), key, null)); + assertEquals(Boolean.FALSE, TermuxSharedProperties.getInternalTermuxPropertyValueFromValue( + RuntimeEnvironment.getApplication(), key, "false")); + assertEquals(Boolean.TRUE, TermuxSharedProperties.getInternalTermuxPropertyValueFromValue( + RuntimeEnvironment.getApplication(), key, "true")); + } + private static final class RecordingClient extends TermuxTerminalViewClientBase { int longPresses; int taps; + boolean rightClickOnLongPress; + boolean handleLongPress = true; + + @Override + public boolean shouldSendRightClickOnLongPress() { return rightClickOnLongPress; } @Override public boolean onLongPress(MotionEvent event) { longPresses++; - return true; + return handleLongPress; } @Override diff --git a/docs/mouse-long-press.md b/docs/mouse-long-press.md new file mode 100644 index 00000000000..c3c8024940d --- /dev/null +++ b/docs/mouse-long-press.md @@ -0,0 +1,21 @@ +# Long press while an application tracks the mouse + +Finger long press keeps Termux's native text selection by default, even when a +terminal application has enabled mouse tracking. + +To use right-click instead, add this to `~/.termux/termux.properties`: + +```properties +mouse-right-click-on-long-press = true +``` + +Run `termux-reload-settings` to reload the existing properties cache. Remove the +line or set it to `false` to restore default selection. + +When enabled, a finger long press in a tracked terminal sends a RIGHT press and +release at that terminal cell through the existing mouse encoder. SGR and legacy +mouse encodings remain supported. Releasing the long press does not add a LEFT +click: the existing gesture recognizer suppresses that release tap. + +The option does not change tracking-off long press or physical mouse handling. +It does not implement held-finger dragging of a remote scrollbar or splitter. diff --git a/terminal-view/src/main/java/com/termux/view/TerminalView.java b/terminal-view/src/main/java/com/termux/view/TerminalView.java index 40c98b350ac..c88fafab5a6 100644 --- a/terminal-view/src/main/java/com/termux/view/TerminalView.java +++ b/terminal-view/src/main/java/com/termux/view/TerminalView.java @@ -250,6 +250,7 @@ public boolean onDoubleTap(MotionEvent event) { public void onLongPress(MotionEvent event) { if (mGestureRecognizer.isInProgress()) return; if (mEmulator != null && mEmulator.isMouseTrackingActive() + && mClient.shouldSendRightClickOnLongPress() && !event.isFromSource(InputDevice.SOURCE_MOUSE) && !isSelectingText()) { sendMouseEventCode(event, TerminalEmulator.MOUSE_RIGHT_BUTTON, true); sendMouseEventCode(event, TerminalEmulator.MOUSE_RIGHT_BUTTON, false); diff --git a/terminal-view/src/main/java/com/termux/view/TerminalViewClient.java b/terminal-view/src/main/java/com/termux/view/TerminalViewClient.java index d6b49b892ca..6cb13fdfada 100644 --- a/terminal-view/src/main/java/com/termux/view/TerminalViewClient.java +++ b/terminal-view/src/main/java/com/termux/view/TerminalViewClient.java @@ -46,6 +46,9 @@ public interface TerminalViewClient { boolean onKeyUp(int keyCode, KeyEvent e); + /** Keep text selection by default; clients may explicitly opt into tracked finger right-click. */ + default boolean shouldSendRightClickOnLongPress() { return false; } + boolean onLongPress(MotionEvent event); diff --git a/termux-shared/src/main/java/com/termux/shared/termux/settings/properties/TermuxPropertyConstants.java b/termux-shared/src/main/java/com/termux/shared/termux/settings/properties/TermuxPropertyConstants.java index dd935f3ab58..0ea648222cb 100644 --- a/termux-shared/src/main/java/com/termux/shared/termux/settings/properties/TermuxPropertyConstants.java +++ b/termux-shared/src/main/java/com/termux/shared/termux/settings/properties/TermuxPropertyConstants.java @@ -18,7 +18,7 @@ import java.util.Set; /* - * Version: v0.18.0 + * Version: v0.19.0 * SPDX-License-Identifier: MIT * * Changelog @@ -80,6 +80,10 @@ * * - 0.18.0 (2022-06-13) * - Add `KEY_DISABLE_FILE_SHARE_RECEIVER` and `KEY_DISABLE_FILE_VIEW_RECEIVER`. + * + * - 0.19.0 (2026-11-02) + * - Add `KEY_MOUSE_RIGHT_CLICK_ON_LONG_PRESS`, default false. + * - Derive loaded boolean keys from their declared default-behaviour sets. */ /** @@ -130,6 +134,9 @@ public final class TermuxPropertyConstants { /** Defines the key for whether to hide soft keyboard when termux app is started */ public static final String KEY_HIDE_SOFT_KEYBOARD_ON_STARTUP = "hide-soft-keyboard-on-startup"; // Default: "hide-soft-keyboard-on-startup" + /** Opt into RIGHT for tracked finger long press; otherwise retain native text selection. */ + public static final String KEY_MOUSE_RIGHT_CLICK_ON_LONG_PRESS = "mouse-right-click-on-long-press"; + /** Defines the key for whether the {@link TermuxAmSocketServer} should be run at app startup */ @@ -390,21 +397,6 @@ public final class TermuxPropertyConstants { * Setting this to {@code null} will make {@link SharedProperties} throw an exception. * */ public static final Set TERMUX_APP_PROPERTIES_LIST = new HashSet<>(Arrays.asList( - /* boolean */ - KEY_DISABLE_FILE_SHARE_RECEIVER, - KEY_DISABLE_FILE_VIEW_RECEIVER, - KEY_DISABLE_HARDWARE_KEYBOARD_SHORTCUTS, - KEY_DISABLE_TERMINAL_SESSION_CHANGE_TOAST, - KEY_ENFORCE_CHAR_BASED_INPUT, - KEY_EXTRA_KEYS_TEXT_ALL_CAPS, - KEY_HIDE_SOFT_KEYBOARD_ON_STARTUP, - KEY_RUN_TERMUX_AM_SOCKET_SERVER, - KEY_TERMINAL_ONCLICK_URL_OPEN, - KEY_USE_CTRL_SPACE_WORKAROUND, - KEY_USE_FULLSCREEN, - KEY_USE_FULLSCREEN_WORKAROUND, - TermuxConstants.PROP_ALLOW_EXTERNAL_APPS, - /* int */ KEY_BELL_BEHAVIOUR, KEY_DELETE_TMPDIR_FILES_OLDER_THAN_X_DAYS_ON_EXIT, @@ -445,6 +437,7 @@ public final class TermuxPropertyConstants { KEY_DISABLE_TERMINAL_SESSION_CHANGE_TOAST, KEY_ENFORCE_CHAR_BASED_INPUT, KEY_HIDE_SOFT_KEYBOARD_ON_STARTUP, + KEY_MOUSE_RIGHT_CLICK_ON_LONG_PRESS, KEY_TERMINAL_ONCLICK_URL_OPEN, KEY_USE_CTRL_SPACE_WORKAROUND, KEY_USE_FULLSCREEN, @@ -478,4 +471,9 @@ public final class TermuxPropertyConstants { public static final Set TERMUX_DEFAULT_INVERETED_TRUE_BOOLEAN_BEHAVIOUR_PROPERTIES_LIST = new HashSet<>(Arrays.asList( )); + static { + TERMUX_APP_PROPERTIES_LIST.addAll(TERMUX_DEFAULT_FALSE_BOOLEAN_BEHAVIOUR_PROPERTIES_LIST); + TERMUX_APP_PROPERTIES_LIST.addAll(TERMUX_DEFAULT_TRUE_BOOLEAN_BEHAVIOUR_PROPERTIES_LIST); + } + } diff --git a/termux-shared/src/main/java/com/termux/shared/termux/settings/properties/TermuxSharedProperties.java b/termux-shared/src/main/java/com/termux/shared/termux/settings/properties/TermuxSharedProperties.java index e277f4506ad..8b3bdceaefb 100644 --- a/termux-shared/src/main/java/com/termux/shared/termux/settings/properties/TermuxSharedProperties.java +++ b/termux-shared/src/main/java/com/termux/shared/termux/settings/properties/TermuxSharedProperties.java @@ -606,6 +606,10 @@ public boolean shouldSoftKeyboardBeHiddenOnStartup() { return (boolean) getInternalPropertyValue(TermuxPropertyConstants.KEY_HIDE_SOFT_KEYBOARD_ON_STARTUP, true); } + public boolean shouldSendRightClickOnLongPress() { + return (boolean) getInternalPropertyValue(TermuxPropertyConstants.KEY_MOUSE_RIGHT_CLICK_ON_LONG_PRESS, true); + } + public boolean shouldRunTermuxAmSocketServer() { return (boolean) getInternalPropertyValue(TermuxPropertyConstants.KEY_RUN_TERMUX_AM_SOCKET_SERVER, true); } From a2758c7f7445a6956e05fe2989902e119bcf791a Mon Sep 17 00:00:00 2001 From: Tristan Simas Date: Mon, 2 Nov 2026 04:30:19 -0500 Subject: [PATCH 8/9] Load real terminal layout attributes in the shared gesture fixture --- .../com/termux/view/TerminalViewTestCase.java | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/app/src/test/java/com/termux/view/TerminalViewTestCase.java b/app/src/test/java/com/termux/view/TerminalViewTestCase.java index 1c337ea833e..0ac12b954ec 100644 --- a/app/src/test/java/com/termux/view/TerminalViewTestCase.java +++ b/app/src/test/java/com/termux/view/TerminalViewTestCase.java @@ -2,11 +2,13 @@ import android.app.Activity; import android.app.Application; +import android.content.res.XmlResourceParser; import android.os.Looper; import android.os.SystemClock; import android.view.MotionEvent; import android.view.ViewConfiguration; +import com.termux.R; import com.termux.shared.termux.terminal.TermuxTerminalSessionClientBase; import com.termux.shared.termux.terminal.TermuxTerminalViewClientBase; import com.termux.terminal.TerminalEmulator; @@ -15,6 +17,7 @@ import org.junit.After; import org.junit.Before; import org.junit.runner.RunWith; +import org.xmlpull.v1.XmlPullParser; import org.robolectric.Robolectric; import org.robolectric.RobolectricTestRunner; import org.robolectric.android.controller.ActivityController; @@ -27,6 +30,7 @@ import java.nio.charset.StandardCharsets; import java.util.concurrent.TimeUnit; +import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; /** Shared real-view/recognizer/emulator fixture; no renderer or gesture mocks. */ @@ -41,9 +45,20 @@ public abstract class TerminalViewTestCase { private ActivityController activity; @Before - public void setUp() { + public void setUp() throws Exception { activity = Robolectric.buildActivity(Activity.class).setup().visible(); - view = new TerminalView(activity.get(), null); + // Use the actual app attributes: a null AttributeSet loses focusability and selection. + try (XmlResourceParser layout = activity.get().getResources().getLayout(R.layout.activity_termux)) { + while (layout.next() != XmlPullParser.END_DOCUMENT) { + if (layout.getEventType() == XmlPullParser.START_TAG + && layout.getIdAttributeResourceValue(0) == R.id.terminal_view) { + view = new TerminalView(activity.get(), layout); + break; + } + } + } + assertNotNull("The app layout must declare the terminal view", view); + assertTrue("Native selection requires the app's touch focusability", view.isFocusableInTouchMode()); view.setTerminalViewClient(client); view.setTextSize(16); assertTrue("Gesture coordinates require real font width", view.mRenderer.mFontWidth > 0); From 37d82cb955a198c15535e4ba06cf1fcbaea9c12a Mon Sep 17 00:00:00 2001 From: Tristan Simas Date: Mon, 2 Nov 2026 04:46:32 -0500 Subject: [PATCH 9/9] Stop tracked wheel input at finger release while preserving untracked fling --- .../termux/view/TerminalViewMouseTest.java | 71 ++++++++++++++++++- .../java/com/termux/view/TerminalView.java | 19 ++--- 2 files changed, 75 insertions(+), 15 deletions(-) diff --git a/app/src/test/java/com/termux/view/TerminalViewMouseTest.java b/app/src/test/java/com/termux/view/TerminalViewMouseTest.java index 77a7cc5717a..c67e3fe5fca 100644 --- a/app/src/test/java/com/termux/view/TerminalViewMouseTest.java +++ b/app/src/test/java/com/termux/view/TerminalViewMouseTest.java @@ -1,6 +1,8 @@ package com.termux.view; +import android.app.Activity; import android.app.Application; +import android.os.Looper; import android.os.SystemClock; import android.view.InputDevice; import android.view.MotionEvent; @@ -10,29 +12,37 @@ import com.termux.terminal.TerminalEmulator; import com.termux.terminal.TerminalOutput; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; +import org.robolectric.Robolectric; import org.robolectric.RobolectricTestRunner; -import org.robolectric.RuntimeEnvironment; +import org.robolectric.Shadows; +import org.robolectric.android.controller.ActivityController; import org.robolectric.annotation.Config; import org.robolectric.annotation.GraphicsMode; +import org.robolectric.annotation.LooperMode; import java.io.ByteArrayOutputStream; import java.nio.charset.StandardCharsets; +import java.util.concurrent.TimeUnit; import static org.junit.Assert.*; @RunWith(RobolectricTestRunner.class) @Config(sdk = 28, manifest = Config.NONE, application = Application.class) @GraphicsMode(GraphicsMode.Mode.NATIVE) +@LooperMode(LooperMode.Mode.PAUSED) public class TerminalViewMouseTest { private final RecordingOutput output = new RecordingOutput(); private TerminalView view; + private ActivityController activity; @Before public void setUp() { - view = new TerminalView(RuntimeEnvironment.getApplication(), null); + activity = Robolectric.buildActivity(Activity.class).setup().visible(); + view = new TerminalView(activity.get(), null); view.setTerminalViewClient(new TermuxTerminalViewClientBase()); view.setTextSize(16); assertTrue("Gesture coordinates require real font width", view.mRenderer.mFontWidth > 0); @@ -40,6 +50,13 @@ public void setUp() { view.mEmulator = new TerminalEmulator(output, 80, 24, 8, 16, null, new TermuxTerminalSessionClientBase()); view.mGestureRecognizer.mListener.onDown(0, 0); + activity.get().setContentView(view); + } + + @After + public void tearDown() { + view.mScroller.abortAnimation(); + activity.pause().stop().destroy(); } private void input(String text) { @@ -117,6 +134,56 @@ public void horizontalGestureDoesNotStartVerticalFling() { assertEquals("\033[<67;3;4M\033[<67;3;4M", output.text()); } + private void assertTrackedVerticalReleaseStops(int mode) { + input("\033[?" + mode + "h\033[?1006h"); + scroll(0, 2); + String actualRows = "\033[<65;3;4M\033[<65;3;4M"; + assertEquals(actualRows, output.text()); + MotionEvent up = event(1, MotionEvent.ACTION_UP, 2.5f, 3.5f, InputDevice.SOURCE_TOUCHSCREEN); + view.mGestureRecognizer.mListener.onFling(up, 0, -2000); + up.recycle(); + assertTrue(view.mScroller.isFinished()); + Shadows.shadowOf(Looper.getMainLooper()).idleFor(1, TimeUnit.SECONDS); + assertEquals(actualRows, output.text()); + } + + @Test + public void tracked1000VerticalReleaseDoesNotAddWheelRows() { assertTrackedVerticalReleaseStops(1000); } + + @Test + public void tracked1002VerticalReleaseDoesNotAddWheelRows() { assertTrackedVerticalReleaseStops(1002); } + + private void prepareScrollback() { + for (int i = 0; i < 60; i++) input("row\r\n"); + view.onScreenUpdated(); + assertTrue(view.mEmulator.getScreen().getActiveTranscriptRows() > 0); + } + + @Test + public void trackingOffScrollbackStillStartsFling() { + prepareScrollback(); + MotionEvent up = event(1, MotionEvent.ACTION_UP, 2.5f, 3.5f, InputDevice.SOURCE_TOUCHSCREEN); + view.mGestureRecognizer.mListener.onFling(up, 0, 2000); + up.recycle(); + try { + assertFalse(view.mScroller.isFinished()); + } finally { + view.mScroller.abortAnimation(); + } + } + + @Test + public void enablingTrackingAbortsAnUntrackedFlingWithoutWheelReports() { + prepareScrollback(); + MotionEvent up = event(1, MotionEvent.ACTION_UP, 2.5f, 3.5f, InputDevice.SOURCE_TOUCHSCREEN); + view.mGestureRecognizer.mListener.onFling(up, 0, 2000); + up.recycle(); + input("\033[?1000h\033[?1006h"); + Shadows.shadowOf(Looper.getMainLooper()).idleFor(1, TimeUnit.MILLISECONDS); + assertTrue(view.mScroller.isFinished()); + assertEquals("", output.text()); + } + @Test public void nextDownClearsCancelledGestureFractionAndAxis() { input("\033[?1000h\033[?1006h"); diff --git a/terminal-view/src/main/java/com/termux/view/TerminalView.java b/terminal-view/src/main/java/com/termux/view/TerminalView.java index 60ff96c8bdc..9c1d0e5d0cd 100644 --- a/terminal-view/src/main/java/com/termux/view/TerminalView.java +++ b/terminal-view/src/main/java/com/termux/view/TerminalView.java @@ -211,33 +211,26 @@ public boolean onScale(float focusX, float focusY, float scale) { @Override public boolean onFling(final MotionEvent e2, float velocityX, float velocityY) { - if (mEmulator == null || scrolledHorizontally) return true; + // Tracked applications already received every gesture row; do not invent wheel + // reports after release. Preserve kinetic scrolling when tracking is off. + if (mEmulator == null || scrolledHorizontally || mEmulator.isMouseTrackingActive()) return true; // Do not start scrolling until last fling has been taken care of: if (!mScroller.isFinished()) return true; - final boolean mouseTrackingAtStartOfFling = mEmulator.isMouseTrackingActive(); float SCALE = 0.25f; - if (mouseTrackingAtStartOfFling) { - mScroller.fling(0, 0, 0, -(int) (velocityY * SCALE), 0, 0, -mEmulator.mRows / 2, mEmulator.mRows / 2); - } else { - mScroller.fling(0, mTopRow, 0, -(int) (velocityY * SCALE), 0, 0, -mEmulator.getScreen().getActiveTranscriptRows(), 0); - } + mScroller.fling(0, mTopRow, 0, -(int) (velocityY * SCALE), 0, 0, -mEmulator.getScreen().getActiveTranscriptRows(), 0); post(new Runnable() { - private int mLastY = 0; - @Override public void run() { - if (mouseTrackingAtStartOfFling != mEmulator.isMouseTrackingActive()) { + if (mEmulator == null || mEmulator.isMouseTrackingActive()) { mScroller.abortAnimation(); return; } if (mScroller.isFinished()) return; boolean more = mScroller.computeScrollOffset(); int newY = mScroller.getCurrY(); - int diff = mouseTrackingAtStartOfFling ? (newY - mLastY) : (newY - mTopRow); - doScroll(e2, diff); - mLastY = newY; + doScroll(e2, newY - mTopRow); if (more) post(this); } });