From 6fe83760734bf4b18fd2f27d72c8e40f993c3b3d Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 2 Feb 2024 13:08:10 -0800 Subject: [PATCH] terminal: support mode 2031 --- src/Surface.zig | 33 ++++++++++++++++++++++++--------- src/terminal/modes.zig | 1 + 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/src/Surface.zig b/src/Surface.zig index 23dd0064a..59b8b803d 100644 --- a/src/Surface.zig +++ b/src/Surface.zig @@ -838,18 +838,21 @@ pub fn handleMessage(self: *Surface, msg: Message) !void { .renderer_health => |health| self.updateRendererHealth(health), - .report_color_scheme => { - const output = switch (self.color_scheme) { - .light => "\x1B[?997;2n", - .dark => "\x1B[?997;1n", - }; - - _ = self.io_thread.mailbox.push(.{ .write_stable = output }, .{ .forever = {} }); - try self.io_thread.wakeup.notify(); - }, + .report_color_scheme => try self.reportColorScheme(), } } +/// Sends a DSR response for the current color scheme to the pty. +fn reportColorScheme(self: *const Surface) !void { + const output = switch (self.color_scheme) { + .light => "\x1B[?997;2n", + .dark => "\x1B[?997;1n", + }; + + _ = self.io_thread.mailbox.push(.{ .write_stable = output }, .{ .forever = {} }); + try self.io_thread.wakeup.notify(); +} + /// Call this when modifiers change. This is safe to call even if modifiers /// match the previous state. /// @@ -2804,7 +2807,19 @@ fn dragLeftClickBefore( /// Call to notify Ghostty that the color scheme for the terminal has /// changed. pub fn colorSchemeCallback(self: *Surface, scheme: apprt.ColorScheme) !void { + // If our scheme didn't change, then we don't do anything. + if (self.color_scheme == scheme) return; + + // Set our new scheme self.color_scheme = scheme; + + // If mode 2031 is on, then we report the change live. + const report = report: { + self.renderer_state.mutex.lock(); + defer self.renderer_state.mutex.unlock(); + break :report self.renderer_state.terminal.modes.get(.report_color_scheme); + }; + if (report) try self.reportColorScheme(); } fn posToViewport(self: Surface, xpos: f64, ypos: f64) terminal.point.Viewport { diff --git a/src/terminal/modes.zig b/src/terminal/modes.zig index dab87c718..e42efa16e 100644 --- a/src/terminal/modes.zig +++ b/src/terminal/modes.zig @@ -215,6 +215,7 @@ const entries: []const ModeEntry = &.{ .{ .name = "bracketed_paste", .value = 2004 }, .{ .name = "synchronized_output", .value = 2026 }, .{ .name = "grapheme_cluster", .value = 2027 }, + .{ .name = "report_color_scheme", .value = 2031 }, }; test {