diff --git a/terminal/bundles/org.eclipse.terminal.control/META-INF/MANIFEST.MF b/terminal/bundles/org.eclipse.terminal.control/META-INF/MANIFEST.MF index 9ace9ed4bee..70e66a2a2df 100644 --- a/terminal/bundles/org.eclipse.terminal.control/META-INF/MANIFEST.MF +++ b/terminal/bundles/org.eclipse.terminal.control/META-INF/MANIFEST.MF @@ -2,7 +2,7 @@ Manifest-Version: 1.0 Bundle-ManifestVersion: 2 Bundle-Name: %pluginName Bundle-SymbolicName: org.eclipse.terminal.control; singleton:=true -Bundle-Version: 1.2.0.qualifier +Bundle-Version: 1.2.100.qualifier Bundle-Activator: org.eclipse.terminal.internal.control.impl.TerminalPlugin Bundle-Vendor: %providerName Bundle-Localization: plugin diff --git a/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/control/impl/ITerminalControlForText.java b/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/control/impl/ITerminalControlForText.java index 66e4a82b25b..6141a15386a 100644 --- a/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/control/impl/ITerminalControlForText.java +++ b/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/control/impl/ITerminalControlForText.java @@ -54,6 +54,15 @@ public interface ITerminalControlForText { default void enableBracketedPaste(boolean enable) { } + /** + * Says a program is drawing a screen and has not finished (DEC mode 2026), so + * that the view does not show it half drawn. + * + * @param redrawing whether a screen is being drawn + */ + default void enableSynchronizedOutput(boolean redrawing) { + } + /** * Shows or hides the cursor at the program's request (DEC mode 25). * diff --git a/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/emulator/VT100Emulator.java b/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/emulator/VT100Emulator.java index 832d6e628dd..1a66a90897b 100644 --- a/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/emulator/VT100Emulator.java +++ b/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/emulator/VT100Emulator.java @@ -531,6 +531,7 @@ private void resetTerminal() { * without one. */ private void resetProgramModes() { + terminal.enableSynchronizedOutput(false); // or the screen would stay frozen terminal.showCursor(true); terminal.enableBracketedPaste(false); } @@ -1356,6 +1357,10 @@ private void setDecPrivateMode(int param) { // Save cursor, as ESC 7 does. saveCursor(); break; + case 2026: + // Synchronized output: the program begins a screen; show it once it is drawn. + terminal.enableSynchronizedOutput(true); + break; case 2004: // Bracketed paste: pasted text is wrapped so a program can tell it from typing. terminal.enableBracketedPaste(true); @@ -1397,6 +1402,9 @@ private void resetDecPrivateMode(int param) { // Restore cursor, as ESC 8 does. restoreCursor(); break; + case 2026: + terminal.enableSynchronizedOutput(false); + break; case 2004: terminal.enableBracketedPaste(false); break; diff --git a/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/emulator/VT100TerminalControl.java b/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/emulator/VT100TerminalControl.java index 699f6a18630..cf35a6dbd80 100644 --- a/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/emulator/VT100TerminalControl.java +++ b/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/emulator/VT100TerminalControl.java @@ -111,6 +111,7 @@ import org.eclipse.terminal.internal.preferences.TerminalColorPresets; import org.eclipse.terminal.internal.textcanvas.PipedInputStream; import org.eclipse.terminal.internal.textcanvas.PollingTextCanvasModel; +import org.eclipse.terminal.internal.textcanvas.RedrawHold; import org.eclipse.terminal.internal.textcanvas.TextCanvas; import org.eclipse.terminal.internal.textcanvas.TextLineRenderer; import org.eclipse.terminal.model.ITerminalTextData; @@ -178,6 +179,8 @@ public class VT100TerminalControl implements ITerminalControlForText, ITerminalC // Written with the model it is copied to under fCursorLock, so a model made during a move // cannot miss a hide or show that the reader job makes meanwhile private boolean fCursorHidden; + // synchronized output (mode 2026), shared by every model made for the canvas + private final RedrawHold fRedrawHold = new RedrawHold(); private final Object fCursorLock = new Object(); private static final String PASTE_START = "\u001b[200~"; //$NON-NLS-1$ private static final String PASTE_END = "\u001b[201~"; //$NON-NLS-1$ @@ -468,6 +471,8 @@ public void disconnectTerminal() { } catch (InterruptedException e) { } } + // the screen a program was drawing when cut off is shown by the last poll + enableSynchronizedOutput(false); fPollingTextCanvasModel.stopPolling(); } @@ -869,6 +874,7 @@ protected void setupControls(Composite parent) { synchronized (fCursorLock) { PollingTextCanvasModel model = new PollingTextCanvasModel(snapshot); model.setCursorHidden(fCursorHidden); + model.setRedrawHold(fRedrawHold); fPollingTextCanvasModel = model; } fCtlText = new TextCanvas(fWndParent, fPollingTextCanvasModel, SWT.NONE, @@ -1554,6 +1560,11 @@ public void showCursor(boolean show) { } } + @Override + public void enableSynchronizedOutput(boolean redrawing) { + fRedrawHold.setRedrawing(redrawing); + } + @Override public void updateTerminalDimensions() { getTerminalText().adjustTerminalDimensions(); diff --git a/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/textcanvas/AbstractTextCanvasModel.java b/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/textcanvas/AbstractTextCanvasModel.java index 4792864b406..88cb04d76aa 100644 --- a/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/textcanvas/AbstractTextCanvasModel.java +++ b/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/textcanvas/AbstractTextCanvasModel.java @@ -102,10 +102,12 @@ protected ITerminalTextDataSnapshot getSnapshot() { } protected void updateSnapshot() { - if (!fInUpdate && fSnapshot.isOutOfDate()) { + if (!fInUpdate && isOutOfDate()) { fInUpdate = true; try { - fSnapshot.updateSnapshot(false); + if (!takeSnapshot()) { + return; + } if (fSnapshot.hasTerminalChanged()) { fireTerminalDataChanged(); } @@ -130,6 +132,21 @@ protected void updateSnapshot() { } } + /** @return whether the snapshot the view paints is behind the terminal text */ + protected boolean isOutOfDate() { + return fSnapshot.isOutOfDate(); + } + + /** + * Copies the terminal text into the snapshot the view paints. + * + * @return whether it was copied + */ + protected boolean takeSnapshot() { + fSnapshot.updateSnapshot(false); + return true; + } + /** * must be called from the UI thread */ diff --git a/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/textcanvas/PollingTextCanvasModel.java b/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/textcanvas/PollingTextCanvasModel.java index 5bc81c3271b..e5fb2a40119 100644 --- a/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/textcanvas/PollingTextCanvasModel.java +++ b/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/textcanvas/PollingTextCanvasModel.java @@ -21,6 +21,65 @@ public class PollingTextCanvasModel extends AbstractTextCanvasModel { private static final int DEFAULT_POLL_INTERVAL = 50; int fPollInterval = -1; + // synchronized output: whether the view leaves the text alone for now + private RedrawHold fHold = new RedrawHold(); + // where a scroll or resize moved the view, or -1: applied with the next copy, as moving + // the window over a snapshot held back would show its lines in the wrong places + private int fWindowStart = -1, fWindowSize; + + /** + * A program that draws a screen in pieces can say where a screen begins and + * ends, and while it is between the two the view leaves it alone rather than + * catching it half drawn. + * + * @param redrawing whether a screen is being drawn + */ + public void setSynchronizedOutput(boolean redrawing) { + fHold.setRedrawing(redrawing); + } + + /** + * @param hold the terminal's, which every model it makes shares, so that one made + * after a move holds as the one before did + */ + public synchronized void setRedrawHold(RedrawHold hold) { + fHold = hold; + } + + /** + * Copies the text unless a screen is being drawn. Every look at the text + * comes here, the polls and a scroll or resize alike, and the check and the + * copy are one step, so a screen cannot begin between them. How long the view + * is held back is the {@link RedrawHold}'s to decide. + */ + @Override + protected synchronized boolean takeSnapshot() { + synchronized (fHold) { + if (fHold.holds(System.nanoTime())) { + return false; + } + if (fWindowStart >= 0) { + getSnapshot().setInterestWindow(fWindowStart, fWindowSize); + fWindowStart = -1; + } + return super.takeSnapshot(); + } + } + + @Override + protected synchronized boolean isOutOfDate() { + return fWindowStart >= 0 || super.isOutOfDate(); + } + + /** A scroll or resize moves the window with the next copy of the text, not before. */ + @Override + public void setVisibleRectangle(int startLine, int startCol, int height, int width) { + synchronized (this) { + fWindowStart = Math.max(0, startLine); + fWindowSize = Math.max(1, height); + } + update(); + } /** * diff --git a/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/textcanvas/RedrawHold.java b/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/textcanvas/RedrawHold.java new file mode 100644 index 00000000000..c76c0d73b67 --- /dev/null +++ b/terminal/bundles/org.eclipse.terminal.control/src/org/eclipse/terminal/internal/textcanvas/RedrawHold.java @@ -0,0 +1,56 @@ +/******************************************************************************* + * Copyright (c) 2026 Contributors to the Eclipse Foundation. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License 2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + *******************************************************************************/ +package org.eclipse.terminal.internal.textcanvas; + +/** + * Synchronized output (DEC mode 2026): whether the view leaves the text alone + * because a program is drawing a screen. One for a terminal, handed to every + * model it makes, so a model made after a move holds as the one before did, and + * decided here only. + */ +public final class RedrawHold { + + /** + * How long a program is given to finish a screen it said it was drawing. A + * program that says so and then stops must not leave the view frozen. + */ + private static final long GRACE_NANOS = 200 * 1_000_000L; + + private boolean fRedrawing; + /** since when (System.nanoTime) the view has been held back, -1 while it is not */ + private long fSince = -1; + + /** @param redrawing whether a program says it is drawing a screen */ + public synchronized void setRedrawing(boolean redrawing) { + fRedrawing = redrawing; + } + + /** + * Asked each time the view would look at the text. The view is held back for at + * most {@link #GRACE_NANOS} since it last looked, however many screens begin + * meanwhile, on a clock that does not move with the time of day. Past that the + * mode ends, as in kitty: a program that said it was drawing and stopped does + * not hold back every later screen. + * + * @param now System.nanoTime() + * @return whether the view keeps what it shows + */ + synchronized boolean holds(long now) { + if (fRedrawing && fSince < 0) { + fSince = now; + } + if (fRedrawing && now - fSince < GRACE_NANOS) { + return true; + } + fRedrawing = false; + fSince = -1; + return false; + } +} diff --git a/terminal/features/org.eclipse.terminal.feature/feature.xml b/terminal/features/org.eclipse.terminal.feature/feature.xml index 276e237b4b3..73775fa6ee8 100644 --- a/terminal/features/org.eclipse.terminal.feature/feature.xml +++ b/terminal/features/org.eclipse.terminal.feature/feature.xml @@ -2,7 +2,7 @@ diff --git a/terminal/tests/org.eclipse.terminal.test/src/org/eclipse/terminal/internal/emulator/MockTerminalControlForText.java b/terminal/tests/org.eclipse.terminal.test/src/org/eclipse/terminal/internal/emulator/MockTerminalControlForText.java index 6601d6e8de5..006381b989c 100644 --- a/terminal/tests/org.eclipse.terminal.test/src/org/eclipse/terminal/internal/emulator/MockTerminalControlForText.java +++ b/terminal/tests/org.eclipse.terminal.test/src/org/eclipse/terminal/internal/emulator/MockTerminalControlForText.java @@ -66,6 +66,17 @@ public boolean isCursorShown() { return cursorShown; } + private boolean synchronizedOutput; + + @Override + public void enableSynchronizedOutput(boolean redrawing) { + synchronizedOutput = redrawing; + } + + public boolean isSynchronizedOutput() { + return synchronizedOutput; + } + @Override public void enableBracketedPaste(boolean enable) { bracketedPaste = enable; diff --git a/terminal/tests/org.eclipse.terminal.test/src/org/eclipse/terminal/internal/emulator/VT100EmulatorTest.java b/terminal/tests/org.eclipse.terminal.test/src/org/eclipse/terminal/internal/emulator/VT100EmulatorTest.java index 32366a9f172..193f0c57d17 100644 --- a/terminal/tests/org.eclipse.terminal.test/src/org/eclipse/terminal/internal/emulator/VT100EmulatorTest.java +++ b/terminal/tests/org.eclipse.terminal.test/src/org/eclipse/terminal/internal/emulator/VT100EmulatorTest.java @@ -666,6 +666,21 @@ public void testFullResetEndsDecPrivateModes() { assertAll(() -> assertTrue(control.isCursorShown()), () -> assertFalse(control.isBracketedPaste())); } + @Test + public void testSynchronizedOutput() { + // a program begins and ends a screen with mode 2026 + run("\u001b[?2026h"); + assertTrue(control.isSynchronizedOutput()); + run("\u001b[?2026l"); + assertFalse(control.isSynchronizedOutput()); + // a program that began one and was cut off or reset does not leave the view frozen + run("\u001b[?2026h"); + emulator.resetState(); + assertFalse(control.isSynchronizedOutput()); + run("\u001b[?2026h\u001bc"); + assertFalse(control.isSynchronizedOutput()); + } + @Test public void testPasteText() { // each line break goes as a CR, as the Enter key sends it: a Windows CR LF is one break diff --git a/terminal/tests/org.eclipse.terminal.test/src/org/eclipse/terminal/internal/textcanvas/TextCanvasModelTest.java b/terminal/tests/org.eclipse.terminal.test/src/org/eclipse/terminal/internal/textcanvas/TextCanvasModelTest.java index ecc792c2fa1..b388ec5981d 100644 --- a/terminal/tests/org.eclipse.terminal.test/src/org/eclipse/terminal/internal/textcanvas/TextCanvasModelTest.java +++ b/terminal/tests/org.eclipse.terminal.test/src/org/eclipse/terminal/internal/textcanvas/TextCanvasModelTest.java @@ -12,6 +12,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import org.eclipse.swt.graphics.Point; +import org.eclipse.swt.widgets.Display; import org.eclipse.terminal.internal.model.TerminalTextData; import org.eclipse.terminal.model.ITerminalTextDataSnapshot; import org.junit.jupiter.api.Test; @@ -86,4 +87,57 @@ public void testCopyKeepsSpacesBeforeAFold() { model.setSelection(0, 1, 0, 3); assertEquals("foo bar", model.getSelectedText()); } + + @Test + public void testSynchronizedOutputHoldsTheViewBackForAWhile() throws InterruptedException { + Display display = Display.getCurrent() != null ? null : new Display(); + try { + TerminalTextData data = new TerminalTextData(); + data.setDimensions(3, 4); + data.setChars(0, 0, "a".toCharArray(), null); + data.setChars(1, 0, "b".toCharArray(), null); + data.setChars(2, 0, "c".toCharArray(), null); + ITerminalTextDataSnapshot snapshot = data.makeSnapshot(); + snapshot.updateSnapshot(true); + PollingTextCanvasModel model = new PollingTextCanvasModel(snapshot); + model.stopPolling(); + model.setVisibleRectangle(0, 0, 3, 4); + model.setSynchronizedOutput(true); + data.setChars(1, 0, "x".toCharArray(), null); + model.update(); + assertEquals('b', model.getTerminalText().getChar(1, 0)); + // a scroll waits like a poll, and the lines already shown stay where they are + model.setVisibleRectangle(1, 0, 2, 4); + assertEquals(0, snapshot.getInterestWindowStartLine()); + assertEquals('b', model.getTerminalText().getChar(1, 0)); + assertEquals('c', model.getTerminalText().getChar(2, 0)); + // a new screen begun meanwhile does not hold the view back longer + Thread.sleep(150); + model.setSynchronizedOutput(false); + model.setSynchronizedOutput(true); + Thread.sleep(100); + model.update(); + assertEquals(1, snapshot.getInterestWindowStartLine()); + data.setChars(2, 0, "y".toCharArray(), null); + model.setSynchronizedOutput(false); + model.update(); + assertEquals('y', model.getTerminalText().getChar(2, 0)); + // a program that begins a screen and never ends it holds the view back once: the + // mode ends with the grace, and the screens after it show at once + model.setSynchronizedOutput(true); + data.setChars(2, 0, "p".toCharArray(), null); + model.update(); + assertEquals('y', model.getTerminalText().getChar(2, 0)); + Thread.sleep(250); + model.update(); + assertEquals('p', model.getTerminalText().getChar(2, 0)); + data.setChars(2, 0, "q".toCharArray(), null); + model.update(); + assertEquals('q', model.getTerminalText().getChar(2, 0)); + } finally { + if (display != null) { + display.dispose(); + } + } + } }