From d359f3d8f5010c97fb869ba127b835f6812459f7 Mon Sep 17 00:00:00 2001 From: insjang Date: Fri, 13 Nov 2026 17:20:39 +0900 Subject: [PATCH] Terminal: support synchronized output (DEC mode 2026) A program that redraws its screen in many writes can bracket each screen with CSI ? 2026 h and CSI ? 2026 l. The view polls the text every 50 ms and used to paint whatever it found, often a screen half drawn, which shows as flicker and as a cursor jumping over the screen. While a program is between the two, the canvas model does not look at the text, and the next poll after the end paints the finished screen. The check and the copy of the text are one locked step, so a screen cannot begin between them, and a scroll or a resize waits like a poll: the view moves with the next copy, not over the lines held back. A program that never ends a screen, or begins the next one right after, does not freeze the view: it is held back at most 200 ms since it last looked, on a clock that does not move with the time of day. A full reset or a disconnect ends the mode, and the last poll then shows what the program had drawn. As with DECTCEM, the flag is kept on the control and copied to the new canvas model under the same lock when the tab is moved to another view. Split out of #2902 as asked in its review. --- .../control/impl/ITerminalControlForText.java | 9 +++ .../internal/emulator/VT100Emulator.java | 8 +++ .../emulator/VT100TerminalControl.java | 20 +++++- .../textcanvas/AbstractTextCanvasModel.java | 21 ++++++- .../textcanvas/PollingTextCanvasModel.java | 63 +++++++++++++++++++ .../emulator/MockTerminalControlForText.java | 11 ++++ .../internal/emulator/VT100EmulatorTest.java | 15 +++++ .../textcanvas/TextCanvasModelTest.java | 42 +++++++++++++ 8 files changed, 184 insertions(+), 5 deletions(-) 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..2906b083cc4 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 @@ -174,10 +174,11 @@ public class VT100TerminalControl implements ITerminalControlForText, ITerminalC private final IPropertyChangeListener fFontListener = this::updateFont; // set by the reader job, read by the display thread when pasting private volatile boolean fBracketedPaste; - // asked for by the program (DECTCEM); kept on the control, as the canvas is new after a move. - // 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 + // asked for by the program (DECTCEM, mode 2026); kept on the control, as the canvas is new + // after a move. Written with the model they are copied to under fCursorLock, so a model made + // during a move cannot miss a change that the reader job makes meanwhile private boolean fCursorHidden; + private boolean fSynchronizedOutput; 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 +469,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 +872,7 @@ protected void setupControls(Composite parent) { synchronized (fCursorLock) { PollingTextCanvasModel model = new PollingTextCanvasModel(snapshot); model.setCursorHidden(fCursorHidden); + model.setSynchronizedOutput(fSynchronizedOutput); fPollingTextCanvasModel = model; } fCtlText = new TextCanvas(fWndParent, fPollingTextCanvasModel, SWT.NONE, @@ -1554,6 +1558,16 @@ public void showCursor(boolean show) { } } + @Override + public void enableSynchronizedOutput(boolean redrawing) { + synchronized (fCursorLock) { + fSynchronizedOutput = redrawing; + if (fPollingTextCanvasModel != null) { + fPollingTextCanvasModel.setSynchronizedOutput(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..b856eb2f828 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 @@ -20,7 +20,70 @@ */ public class PollingTextCanvasModel extends AbstractTextCanvasModel { private static final int DEFAULT_POLL_INTERVAL = 50; + /** + * 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 int REDRAW_GRACE = 200; int fPollInterval = -1; + private boolean fRedrawing; + // whether the view held back since it last looked, and since when (System.nanoTime) + private boolean fHeldBack; + private long fHeldBackSince; + // 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 synchronized void setSynchronizedOutput(boolean redrawing) { + fRedrawing = redrawing; + } + + /** + * 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. The view is held + * back for at most {@link #REDRAW_GRACE} ms since it last looked, however many + * screens begin meanwhile, on a clock that does not move with the time of day. + */ + @Override + protected synchronized boolean takeSnapshot() { + long now = System.nanoTime(); + if (fRedrawing && !fHeldBack) { + fHeldBack = true; + fHeldBackSince = now; + } + if (fRedrawing && now - fHeldBackSince < REDRAW_GRACE * 1_000_000L) { + return false; + } + fHeldBack = 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/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..f93a59c8558 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,45 @@ 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)); + } finally { + if (display != null) { + display.dispose(); + } + } + } }