Skip to content
Merged
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 @@ -202,6 +202,7 @@ public TerminalView(int columns, int rows, int maxHistory, Output output, int[]
this.fonts = new TerminalFonts(fontFamily, fontSize, scale);
emulator.setReplies(this::sendReply, TerminalEmulator.ReplyScope.CLIENT);
emulator.setColorScheme(light ? TerminalEmulator.ColorScheme.LIGHT : TerminalEmulator.ColorScheme.DARK);
pushOscColors();
emulator.setListener(this);

frameTimer = new Timer(0, e -> frameDue());
Expand Down Expand Up @@ -301,18 +302,25 @@ public void setOptionAsMeta(boolean on) {
public void setTheme(Integer background, boolean light) {
colors.setTheme(background, light);
emulator.setColorScheme(light ? TerminalEmulator.ColorScheme.LIGHT : TerminalEmulator.ColorScheme.DARK);
pushOscColors();
updateBackground();
viewStamp++;
repaint();
}

public void setPalette(int[] palette) {
colors.setPalette(palette);
pushOscColors();
updateBackground();
viewStamp++;
repaint();
}

/** Reports the theme's default colours for OSC 10/11 queries (codex shades its logo by them). */
private void pushOscColors() {
emulator.setOscColors(colors.defaultForeground(), colors.defaultBackground());
}

public Color defaultBackground() {
return colors.color(colors.defaultBackground());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,16 @@ void colourSchemeQueryIsAnsweredThroughTheConnection() throws Exception {
assertTrue(connection.awaitWritten("997;2n").endsWith("\u001b[?997;2n"));
}

@Test
void oscColourQueriesAreAnsweredThroughTheConnection() throws Exception {
open();
connection.push("\u001b]10;?\u0007");
assertTrue(connection.awaitWritten("rgb:").endsWith("\u001b]10;rgb:e9e9/ebeb/ebeb\u001b\\"));
edtRun(() -> terminal.applyBackground(Color.WHITE, true));
connection.push("\u001b]11;?\u001b\\");
assertTrue(connection.awaitWritten("11;rgb").endsWith("\u001b]11;rgb:ffff/ffff/ffff\u001b\\"));
}

/** A 10 MB burst must not starve the EDT: frames are coalesced and painting only waits
* for one chunk's worth of emulation. */
@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,21 @@ void answersOnlyTheColourSchemeQuery() throws Exception {
assertEquals(C + "?997;2n", takeWritten());
}

@Test
void answersOscColourQueriesWithTheThemeDefaults() throws Exception {
feed(E + "]10;?" + E + "\\" + E + "]11;?\u0007");
assertEquals(E + "]10;rgb:e9e9/ebeb/ebeb" + E + "\\" + E + "]11;rgb:1010/1010/1010" + E + "\\",
takeWritten());
edtRun(() -> view.setTheme(null, true));
feed(E + "]10;?" + E + "\\" + E + "]11;?" + E + "\\");
assertEquals(E + "]10;rgb:0000/0000/0000" + E + "\\" + E + "]11;rgb:e9e9/ebeb/ebeb" + E + "\\",
takeWritten());
edtRun(() -> view.setPalette(AnsiPalettes.forSetting(AppSettings.TerminalPalette.XTERM)));
feed(E + "]10;?" + E + "\\" + E + "]11;?" + E + "\\");
assertEquals(E + "]10;rgb:0000/0000/0000" + E + "\\" + E + "]11;rgb:ffff/ffff/ffff" + E + "\\",
takeWritten());
}

// --- keyboard -----------------------------------------------------------------------

private void key(int keyCode, char keyChar, int modifiers) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
* <p>Replies (DA1, DA2, DSR 5/6) go to {@link Replies#send(byte[])} — the session's PTY
* writer queue — in the emulator's {@link TerminalEmulator.ReplyScope#BASIC basic} scope,
* which answers exactly the queries {@link QueryFilter} strips from the client stream. The
* colour-scheme report {@code CSI ?997;…n} is never produced (only a client knows its theme),
* and XTWINOPS resize requests from the program ({@code CSI 8;h;w t}) are ignored. A
* colour-scheme report {@code CSI ?997;…n} and OSC 10/11 colour queries are never produced
* (only a client knows its theme); those queries pass through {@link QueryFilter} to clients.
* XTWINOPS resize requests from the program ({@code CSI 8;h;w t}) are ignored. A
* {@link ModeTracker} sees every token too, for the OSC 7 / OSC 9;9 working directory.
*/
public final class HeadlessTerminal implements AutoCloseable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@
* injected after this filter.</li>
* <li>{@code CSI ? 996 n} — colour-scheme preference: <b>passed through</b>; only a
* client knows its theme, and the server never answers it.</li>
* <li>OSC 10/11 {@code ?}, DECRQM, XTVERSION, kitty keyboard, XTGETTCAP (which the server
* also answers "invalid", as before), everything else: passed through, so behaviour
* matches a direct attach.</li>
* <li>OSC 10/11 {@code ?} — passed through; a client whose theme is set answers them
* (see {@link TerminalEmulator#setOscColors}), the server itself never does.</li>
* <li>DECRQM, XTVERSION, kitty keyboard, XTGETTCAP (which the server also answers
* "invalid", as before), everything else: passed through, so behaviour matches a
* direct attach.</li>
* </ul>
*
* Keep this and the emulator's basic reply scope in lock-step (TerminalEmulatorTest checks
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,11 @@
* <p>The colour-scheme query {@code CSI ? 996 n} is answered ({@code CSI ? 997 ; 1 n} dark,
* {@code ; 2 n} light) in every scope, but only once {@link #setColorScheme} has been given a
* scheme — only a client knows its theme, so the daemon never sets one. While the program has
* DECSET 2031 on, a scheme change is also reported unsolicited. Nothing answers OSC colour
* queries.
* DECSET 2031 on, a scheme change is also reported unsolicited. The foreground/background
* queries {@code OSC 10 ; ?} and {@code OSC 11 ; ?} work the same way once {@link #setOscColors}
* has been given the theme's colours (answered in xterm's {@code rgb:RRRR/GGGG/BBBB} form);
* without them a program that shades itself to the background (codex's logo) falls back to
* dim text. Nothing answers any other OSC query.
*/
public final class TerminalEmulator {

Expand All @@ -64,7 +67,7 @@ public interface ReplySink {
/** Which queries are answered; see the class documentation. */
public enum ReplyScope {
BASIC, FULL,
/** Nothing but the colour-scheme report (see {@link #setColorScheme}). */
/** Colour-scheme ({@link #setColorScheme}) and OSC 10/11 ({@link #setOscColors}) only. */
CLIENT
}

Expand Down Expand Up @@ -177,6 +180,9 @@ public record SavedCursor(int column, int row, boolean pendingWrap, long style,
private boolean synchronizedOutput;
private boolean colorSchemeUpdates;
private ColorScheme colorScheme;
/** {@code 0xRRGGBB} OSC 10/11 answers; null until {@link #setOscColors} (unanswered). */
private Integer oscForeground;
private Integer oscBackground;
private MouseTracking mouseTracking = MouseTracking.NONE;
private MouseEncoding mouseEncoding = MouseEncoding.DEFAULT;
private final ArrayDeque<Integer> kittyFlags = new ArrayDeque<>();
Expand Down Expand Up @@ -259,6 +265,32 @@ private static byte[] colorSchemeReport(ColorScheme scheme) {
return ("\u001b[?997;" + scheme.report + "n").getBytes(StandardCharsets.US_ASCII);
}

/**
* The theme's default foreground/background {@code 0xRRGGBB} that {@code OSC 10 ; ?} and
* {@code OSC 11 ; ?} are answered with, in every scope. Unset (the default) leaves them
* unanswered — only a client knows its theme, so the daemon never sets these, and the
* queries reach the client (see {@link QueryFilter}).
*/
public void setOscColors(int foreground, int background) {
lock.lock();
try {
oscForeground = foreground & 0xFFFFFF;
oscBackground = background & 0xFFFFFF;
} finally {
lock.unlock();
}
}

private static byte[] oscColorReport(int ps, int rgb) {
return ("\u001b]" + ps + ";rgb:" + hex16((rgb >> 16) & 0xFF) + "/" + hex16((rgb >> 8) & 0xFF)
+ "/" + hex16(rgb & 0xFF) + "\u001b\\").getBytes(StandardCharsets.US_ASCII);
}

/** An 8-bit channel in xterm's 4-digit form ({@code 0xAB} → {@code "abab"}). */
private static String hex16(int value) {
return String.format("%04x", (value << 8) | value);
}

/** Sets the (single) listener; null removes it. */
public void setListener(TerminalListener listener) {
this.listener = listener;
Expand Down Expand Up @@ -1880,8 +1912,14 @@ private void osc(byte[] b, int len) {
String text = new String(b, i + 1, end - i - 1, StandardCharsets.UTF_8);
setTitle(text.length() > MAX_TITLE_LENGTH ? text.substring(0, MAX_TITLE_LENGTH) : text);
}
// 4, 7, 8, 10–12, 52, 104, 110–112, 133, …: parsed and ignored here (the session
// tracks OSC 7 through ModeTracker; nothing is answered).
if ((ps == 10 || ps == 11) && replySink != null && i < end) {
Integer color = ps == 10 ? oscForeground : oscBackground;
if (color != null && end - i == 2 && b[i + 1] == '?') {
pendingReplies.add(oscColorReport(ps, color));
}
}
// 4, 7, 8, 12, 52, 104, 110–112, 133, … and OSC 10/11 sets: parsed and ignored here
// (the session tracks OSC 7 through ModeTracker; only OSC 10/11 queries are answered).
}

private void setTitle(String newTitle) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -796,15 +796,43 @@ void colourSchemeQueryIsAnsweredOnceASchemeIsSet() {
}

@Test
void clientScopeAnswersOnlyTheColourScheme() {
void clientScopeAnswersOnlyClientKnownQueries() {
List<String> replies = new ArrayList<>();
TerminalEmulator e = new TerminalEmulator(10, 3, 0);
e.setReplies(b -> replies.add(new String(b, StandardCharsets.UTF_8)), TerminalEmulator.ReplyScope.CLIENT);
e.setColorScheme(TerminalEmulator.ColorScheme.LIGHT);
e.setOscColors(0xE9EBEB, 0x000000);
String all = C + "c" + C + ">c" + C + "5n" + C + "6n" + C + "?6n" + C + "?1$p" + C + ">q" + C + "?u"
+ E + "P+q544e" + E + "\\" + C + "?996n";
+ E + "P+q544e" + E + "\\" + C + "?996n" + E + "]10;?" + E + "\\" + E + "]11;?\u0007";
e.feed(all.getBytes(StandardCharsets.UTF_8));
assertEquals(List.of(C + "?997;2n"), replies);
assertEquals(List.of(C + "?997;2n", E + "]10;rgb:e9e9/ebeb/ebeb" + E + "\\",
E + "]11;rgb:0000/0000/0000" + E + "\\"), replies);
}

@Test
void oscColourQueriesAreNotAnsweredUntilColoursAreSet() {
Term t = term(10, 3);
t.feed(E + "]10;?\u0007" + E + "]11;?" + E + "\\");
assertTrue(t.replies.isEmpty());
}

@Test
void oscColourQueriesAreAnsweredOnceColoursAreSet() {
Term t = term(10, 3);
t.e.setOscColors(0xE9EBEB, 0x101010);
t.feed(E + "]10;?\u0007");
t.feed(E + "]11;?" + E + "\\");
assertEquals(List.of(E + "]10;rgb:e9e9/ebeb/ebeb" + E + "\\",
E + "]11;rgb:1010/1010/1010" + E + "\\"), t.replies);
}

@Test
void oscColourSetsAndOtherQueriesAreIgnored() {
Term t = term(10, 3);
t.e.setOscColors(0xFFFFFF, 0x000000);
t.feed(E + "]10;red" + E + "\\" + E + "]11;#123456\u0007" + E + "]4;1;?" + E + "\\"
+ E + "]10" + E + "\\" + E + "]12;?" + E + "\\" + E + "]10;??" + E + "\\");
assertTrue(t.replies.isEmpty());
}

@Test
Expand Down
Loading