diff --git a/app/src/main/java/io/github/ghosthack/microhm/app/terminal/TerminalView.java b/app/src/main/java/io/github/ghosthack/microhm/app/terminal/TerminalView.java index 6066ec6..968a3f8 100644 --- a/app/src/main/java/io/github/ghosthack/microhm/app/terminal/TerminalView.java +++ b/app/src/main/java/io/github/ghosthack/microhm/app/terminal/TerminalView.java @@ -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()); @@ -301,6 +302,7 @@ 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(); @@ -308,11 +310,17 @@ public void setTheme(Integer background, boolean light) { 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()); } diff --git a/app/src/test/java/io/github/ghosthack/microhm/app/terminal/SessionTerminalTest.java b/app/src/test/java/io/github/ghosthack/microhm/app/terminal/SessionTerminalTest.java index c9c519a..edcba49 100644 --- a/app/src/test/java/io/github/ghosthack/microhm/app/terminal/SessionTerminalTest.java +++ b/app/src/test/java/io/github/ghosthack/microhm/app/terminal/SessionTerminalTest.java @@ -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 diff --git a/app/src/test/java/io/github/ghosthack/microhm/app/terminal/TerminalViewTest.java b/app/src/test/java/io/github/ghosthack/microhm/app/terminal/TerminalViewTest.java index fdf8dd9..1a44c14 100644 --- a/app/src/test/java/io/github/ghosthack/microhm/app/terminal/TerminalViewTest.java +++ b/app/src/test/java/io/github/ghosthack/microhm/app/terminal/TerminalViewTest.java @@ -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) { diff --git a/vt/src/main/java/io/github/ghosthack/microhm/vt/HeadlessTerminal.java b/vt/src/main/java/io/github/ghosthack/microhm/vt/HeadlessTerminal.java index 9ae670c..114ef4c 100644 --- a/vt/src/main/java/io/github/ghosthack/microhm/vt/HeadlessTerminal.java +++ b/vt/src/main/java/io/github/ghosthack/microhm/vt/HeadlessTerminal.java @@ -18,8 +18,9 @@ *
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 { diff --git a/vt/src/main/java/io/github/ghosthack/microhm/vt/QueryFilter.java b/vt/src/main/java/io/github/ghosthack/microhm/vt/QueryFilter.java index 627cf8a..0020031 100644 --- a/vt/src/main/java/io/github/ghosthack/microhm/vt/QueryFilter.java +++ b/vt/src/main/java/io/github/ghosthack/microhm/vt/QueryFilter.java @@ -25,9 +25,11 @@ * injected after this filter. *
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 {
@@ -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
}
@@ -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