From 6c9b6567af368514ff5e2ac2e02e6a133eba0d24 Mon Sep 17 00:00:00 2001 From: tester Date: Sat, 28 Nov 2026 19:42:09 -0800 Subject: [PATCH 1/2] Answer OSC 10/11 color queries so codex shades its logo to the background Codex asks the terminal for its foreground/background colors and shades each cell of its braille logo to the background when it gets an answer. Microhm answered nothing, so codex fell back to SGR 2 dim text, which renders much brighter than the subtle watermark macOS Terminal shows. The emulator now answers OSC 10/11 queries in xterm's rgb:RRRR/GGGG/BBBB form once setOscColors has been given the theme's colors, mirroring the CSI ? 996 n color-scheme pattern: only a client knows its theme, so the daemon never sets them and the queries reach the client. TerminalView pushes its default fg/bg on construction, theme change, and palette change. --- .../microhm/app/terminal/TerminalView.java | 8 ++++ .../app/terminal/SessionTerminalTest.java | 10 ++++ .../app/terminal/TerminalViewTest.java | 15 ++++++ .../ghosthack/microhm/vt/QueryFilter.java | 8 ++-- .../microhm/vt/TerminalEmulator.java | 46 +++++++++++++++++-- .../microhm/vt/TerminalEmulatorTest.java | 34 ++++++++++++-- 6 files changed, 111 insertions(+), 10 deletions(-) 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/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. *
  • {@code CSI ? 996 n} — colour-scheme preference: passed through; only a * client knows its theme, and the server never answers it.
  • - *
  • 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.
  • + *
  • OSC 10/11 {@code ?} — passed through; a client whose theme is set answers them + * (see {@link TerminalEmulator#setOscColors}), the server itself never does.
  • + *
  • DECRQM, XTVERSION, kitty keyboard, XTGETTCAP (which the server also answers + * "invalid", as before), everything else: passed through, so behaviour matches a + * direct attach.
  • * * * Keep this and the emulator's basic reply scope in lock-step (TerminalEmulatorTest checks diff --git a/vt/src/main/java/io/github/ghosthack/microhm/vt/TerminalEmulator.java b/vt/src/main/java/io/github/ghosthack/microhm/vt/TerminalEmulator.java index f718ebf..9c057f6 100644 --- a/vt/src/main/java/io/github/ghosthack/microhm/vt/TerminalEmulator.java +++ b/vt/src/main/java/io/github/ghosthack/microhm/vt/TerminalEmulator.java @@ -51,8 +51,11 @@ *

    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 { @@ -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 kittyFlags = new ArrayDeque<>(); @@ -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; @@ -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) { diff --git a/vt/src/test/java/io/github/ghosthack/microhm/vt/TerminalEmulatorTest.java b/vt/src/test/java/io/github/ghosthack/microhm/vt/TerminalEmulatorTest.java index 9153ad0..259b3c2 100644 --- a/vt/src/test/java/io/github/ghosthack/microhm/vt/TerminalEmulatorTest.java +++ b/vt/src/test/java/io/github/ghosthack/microhm/vt/TerminalEmulatorTest.java @@ -796,15 +796,43 @@ void colourSchemeQueryIsAnsweredOnceASchemeIsSet() { } @Test - void clientScopeAnswersOnlyTheColourScheme() { + void clientScopeAnswersOnlyClientKnownQueries() { List 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 From 753bbd758920eb4df636425d85e4e87b8354c0e8 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Sun, 29 Nov 2026 04:31:35 +0000 Subject: [PATCH 2/2] docs(vt): mention OSC 10/11 in CLIENT scope and HeadlessTerminal Co-authored-by: ghosthack --- .../io/github/ghosthack/microhm/vt/HeadlessTerminal.java | 5 +++-- .../io/github/ghosthack/microhm/vt/TerminalEmulator.java | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) 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/TerminalEmulator.java b/vt/src/main/java/io/github/ghosthack/microhm/vt/TerminalEmulator.java index 9c057f6..9c767cd 100644 --- a/vt/src/main/java/io/github/ghosthack/microhm/vt/TerminalEmulator.java +++ b/vt/src/main/java/io/github/ghosthack/microhm/vt/TerminalEmulator.java @@ -67,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 }