Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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).
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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$
Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -869,6 +872,7 @@ protected void setupControls(Composite parent) {
synchronized (fCursorLock) {
PollingTextCanvasModel model = new PollingTextCanvasModel(snapshot);
model.setCursorHidden(fCursorHidden);
model.setSynchronizedOutput(fSynchronizedOutput);
Comment thread
insjang marked this conversation as resolved.
Resolved
fPollingTextCanvasModel = model;
}
fCtlText = new TextCanvas(fWndParent, fPollingTextCanvasModel, SWT.NONE,
Expand Down Expand Up @@ -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);
Comment thread
insjang marked this conversation as resolved.
Resolved
}
}
}

@Override
public void updateTerminalDimensions() {
getTerminalText().adjustTerminalDimensions();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand All @@ -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
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

/**
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
}
}
}
Loading