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 @@ -62,4 +62,22 @@ default void enableBracketedPaste(boolean enable) {
default void showCursor(boolean show) {
}

/**
* A program that asks for mouse events wants them instead of the terminal's own
* handling of the mouse, scrolling included.
*
* @param mode 1000 (buttons), 1002 (buttons and drags), 1003 (every move), or 0 for none
*/
default void enableMouseReporting(int mode) {
}

/**
* SGR encoding (CSI ?1006) reports coordinates as decimal numbers instead of
* single bytes, which is what lets them go past column 223.
*
* @param enable whether to use it
*/
default void enableSgrMouseEncoding(boolean enable) {
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,11 @@ public void setBufferLineLimit(int bufferLineLimit) {
text.setBufferLineLimit(bufferLineLimit);
}

/** @return the lines of the screen, which are the last lines of the text */
public int getLines() {
return text.getLines();
}

/**
* @see IVT100EmulatorBackend#getBufferLineLimit()
*/
Expand Down Expand Up @@ -533,6 +538,8 @@ private void resetTerminal() {
private void resetProgramModes() {
terminal.showCursor(true);
terminal.enableBracketedPaste(false);
terminal.enableMouseReporting(0);
terminal.enableSgrMouseEncoding(false);
}

/**
Expand Down Expand Up @@ -1360,6 +1367,14 @@ private void setDecPrivateMode(int param) {
// Bracketed paste: pasted text is wrapped so a program can tell it from typing.
terminal.enableBracketedPaste(true);
break;
case 1000: // report button press and release
case 1002: // also report drag
case 1003: // also report every move
terminal.enableMouseReporting(param);
break;
case 1006:
terminal.enableSgrMouseEncoding(true);
break;
Comment thread
insjang marked this conversation as resolved.
Resolved
default:
Logger.log("Unsupported command parameter: CSI ?" + param + 'h'); //$NON-NLS-1$
break;
Expand Down Expand Up @@ -1400,6 +1415,14 @@ private void resetDecPrivateMode(int param) {
case 2004:
terminal.enableBracketedPaste(false);
break;
case 1000:
case 1002:
case 1003:
terminal.enableMouseReporting(0);
break;
case 1006:
terminal.enableSgrMouseEncoding(false);
break;
default:
Logger.log("Unsupported command parameter: CSI ?" + param + 'l'); //$NON-NLS-1$
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import java.io.OutputStream;
import java.net.SocketException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.EnumMap;
import java.util.Map;

Expand Down Expand Up @@ -86,6 +87,7 @@
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
Expand Down Expand Up @@ -114,6 +116,7 @@
import org.eclipse.terminal.internal.textcanvas.TextCanvas;
import org.eclipse.terminal.internal.textcanvas.TextLineRenderer;
import org.eclipse.terminal.model.ITerminalTextData;
import org.eclipse.terminal.model.ITerminalTextDataReadOnly;
import org.eclipse.terminal.model.ITerminalTextDataSnapshot;
import org.eclipse.terminal.model.TerminalColor;
import org.eclipse.terminal.model.TerminalTextDataFactory;
Expand Down Expand Up @@ -165,7 +168,10 @@ public class VT100TerminalControl implements ITerminalControlForText, ITerminalC

private final EditActionAccelerators editActionAccelerators = new EditActionAccelerators();

private boolean fApplicationCursorKeys;
// set by the reader job, read by the display thread on a key press
private volatile boolean fApplicationCursorKeys;
// what a program that asked for the mouse is told of it
private final MouseReporter fMouse = new MouseReporter();

/**
* Listens to changes in the preferences
Expand Down Expand Up @@ -559,14 +565,16 @@ private void showErrorMessage(String message) {

protected void sendString(String string) {
Logger.log(string);
try {
// Send the string after converting it to an array of bytes using the
// platform's default character encoding.
//
// TODO: Find a way to force this to use the ISO Latin-1 encoding.
// TODO: handle Encoding Errors in a better way
// Send the string after converting it to an array of bytes using the
// platform's default character encoding.
//
// TODO: handle Encoding Errors in a better way
sendBytes(string.getBytes(fCharset));
}

getOutputStream().write(string.getBytes(fCharset));
private void sendBytes(byte[] bytes) {
try {
getOutputStream().write(bytes);
getOutputStream().flush();
} catch (SocketException socketException) {
displayTextInTerminal(socketException.getMessage());
Expand Down Expand Up @@ -875,6 +883,7 @@ protected void setupControls(Composite parent) {
new TextLineRenderer(() -> fCtlText, fPollingTextCanvasModel));

fCtlText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
fCtlText.setMouseHandler(fMouse);
fCtlText.addResizeHandler((lines, columns) -> fTerminalText.setDimensions(lines, columns));
fCtlText.addMouseListener(new MouseAdapter() {
@Override
Expand Down Expand Up @@ -1524,6 +1533,193 @@ public void enableApplicationCursorKeys(boolean enable) {
fApplicationCursorKeys = enable;
}

@Override
public void enableMouseReporting(int mode) {
fMouse.setMode(mode);
}

@Override
public void enableSgrMouseEncoding(boolean enable) {
fMouse.fSgr = enable;
}

/**
* One mode a program asked for, and what it has been told in it. A new mode is a
* new session: nothing told to the program that asked before carries over.
*/
private static final class MouseSession {
/** 1000 (buttons), 1002 (and drags), 1003 (and every move), 0 for none */
final int mode;
/** the cell the pointer was last reported in */
int line = -1, column = -1;
/** lines the wheel moved that make no whole notch yet, up positive */
int wheelLines;

MouseSession(int mode) {
this.mode = mode;
}
}

/**
* Presses, releases, moves and the wheel, told to a program that asked for them
* the way a terminal has always told it, so that a program drawing its own screen
* can be clicked on and scrolled. Whether a gesture is the program's at all is the
* canvas's to decide; this only tells it.
*/
private class MouseReporter implements TextCanvas.IMouseHandler {
// set by the reader job, read by the display thread, which alone changes the session
private volatile MouseSession fSession = new MouseSession(0);
volatile boolean fSgr;

void setMode(int mode) {
fSession = new MouseSession(mode);
// the last click told belongs to the program that asked then
if (fCtlText != null) {
fDisplay.asyncExec(() -> {
if (!fCtlText.isDisposed()) {
fCtlText.forgetProgramClick();
}
});
}
}

@Override
public boolean button(int button, int modifiers, Point cell, boolean pressed) {
MouseSession session = fSession;
if (session.mode == 0 || button < 1 || button > 3 || !isConnected()) {
return false;
}
Point at = onScreen(cell);
if (at == null) {
if (pressed) {
return false; // over the history, which the program knows nothing of
}
// a release is reported wherever it happens: its press was
at = new Point(Math.max(1, Math.min(cell.x + 1, fPollingTextCanvasModel.getTerminalText().getWidth())), 1);
}
send(button - 1 + modifiers, at, pressed);
// the program now knows the pointer is here: a drag after this press, or
// after one that ended elsewhere, is reported from here on
session.line = at.y;
session.column = at.x;
if (pressed) {
// the press takes the terminal's selection away (the canvas clears it once
// this returns): the view's copy must know, after that
fDisplay.asyncExec(fTerminalListener::setTerminalSelectionChanged);
}
return true;
}

@Override
public boolean moved(int button, int modifiers, Point cell) {
MouseSession session = fSession;
// 1002 is only interested while a button is held, 1003 in every move.
if (session.mode < 1002 || (session.mode == 1002 && button == 0) || !isConnected()) {
return false;
}
Point at = onScreen(cell);
if (at == null) {
return false;
}
if (at.y == session.line && at.x == session.column) {
return true; // still the same cell, and a cell is all the program is told
}
session.line = at.y;
session.column = at.x;
send((button == 0 ? 3 : button - 1) + 32 + modifiers, at, true);
return true;
}

/**
* A wheel notch is reported as a press of button 64 or 65, never released.
* SWT counts lines; a notch is three of them, however the events split them,
* and what is left over counts toward the next one in the same direction.
*/
@Override
public boolean wheel(int lines, int modifiers, Point cell) {
MouseSession session = fSession;
if (session.mode == 0 || lines == 0 || !isConnected()) {
return false;
}
Point at = onScreen(cell);
if (at == null) {
return false; // over the history: the view scrolls
}
if (Integer.signum(lines) != Integer.signum(session.wheelLines)) {
session.wheelLines = 0;
}
session.wheelLines += lines;
int notches = Math.abs(session.wheelLines) / 3;
session.wheelLines %= 3;
for (int i = 0; i < notches; i++) {
send((lines > 0 ? 64 : 65) + modifiers, at, true);
}
return true; // the program's, told once the lines make a notch
}

private void send(int button, Point at, boolean pressed) {
sendBytes(mouseReportBytes(mouseReport(fSgr, button, at.y, at.x, pressed)));
}
}

/**
* @return where a cell of the text is on the screen a program sees, as column and
* line counted from one; null for a line of the history above it, which the
* user scrolled back to and the program knows nothing of. Below the last line
* (the strip under it) is the last line.
*/
private Point onScreen(Point cell) {
// the height of what the canvas shows, which the cell is from, not of the text the
// reader is writing ahead of it
ITerminalTextDataReadOnly shown = fPollingTextCanvasModel.getTerminalText();
return onScreen(cell, shown.getHeight(), fTerminalText.getLines(), shown.getWidth());
}

/**
* Where a cell of the text is on the screen a program sees, as in {@link #onScreen(Point)}.
*
* @param height the lines of the text, history included
* @param screenLines the lines of the screen, the last of the text
* @param width the columns of the screen
*/
public static Point onScreen(Point cell, int height, int screenLines, int width) {
int line = cell.y - (height - screenLines) + 1;
if (line < 1) {
return null;
}
return new Point(Math.max(1, Math.min(cell.x + 1, width)), Math.min(line, screenLines));
}

public static String mouseReport(boolean sgr, int button, int line, int column, boolean pressed) {
if (sgr) {
return "\u001b[<" + button + ';' + column + ';' + line + (pressed ? 'M' : 'm'); //$NON-NLS-1$
}
// The older spelling has no way to say which button came up, so a release is
// reported as button 3, meaning "some button", with the keys held as in a press.
return "\u001b[M" + (char) (32 + (pressed ? button : button | 3)) + legacyPosition(column) //$NON-NLS-1$
+ legacyPosition(line);
}

/**
* A column or line in one byte, which has no room past 223: as xterm, anything
* further is sent as a zero byte, so that a drag past it still ends in a release.
*/
private static char legacyPosition(int value) {
return value > 223 ? 0 : (char) (32 + value);
}

/**
* A report is bytes, not text: the older spelling puts a column or line past 95
* in one byte of 128 or more, which the terminal's encoding (UTF-8, say) would
* turn into two. The SGR spelling is ASCII, the same either way.
*
* @param report a report as {@link #mouseReport(boolean, int, int, int, boolean)} spells it
* @return the bytes the program is sent
*/
public static byte[] mouseReportBytes(String report) {
return report.getBytes(StandardCharsets.ISO_8859_1);
}

@Override
public void addMouseListener(ITerminalMouseListener listener) {
getCtlText().addTerminalMouseListener(listener);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ abstract public class GridCanvas extends VirtualCanvas {
public GridCanvas(Composite parent, int style) {
super(parent, style);
addListener(SWT.MouseWheel, event -> {
if (getVerticalBar().isVisible()) {
// Windows set to scroll a page at a time counts notches, not lines: a notch is three
// lines here, as GTK and the Windows default count them. Another number of lines per
// notch in the Windows settings is not known to SWT's API, and still counts as lines.
int lines = event.detail == SWT.SCROLL_PAGE ? 3 * event.count : event.count;
if (!handleMouseWheel(event.x, event.y, lines, event.stateMask) && getVerticalBar().isVisible()) {
int delta = -fCellHeight * event.count;
scrollYDelta(delta);
}
Expand All @@ -40,6 +44,17 @@ public GridCanvas(Composite parent, int style) {

}

/**
* Offered the wheel before the canvas scrolls itself.
*
* @param count lines the wheel asked for, positive when scrolling up
* @param stateMask the keys held at the time
* @return whether it was taken, in which case the canvas leaves it alone
*/
protected boolean handleMouseWheel(int x, int y, int count, int stateMask) {
return false;
}

/** template method paint.
* iterates over all cells in the clipping rectangle and paints them.
*/
Expand Down
Loading
Loading