From f9f92f2e0f590ece488e06718ccbbeb4bd6fc601 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sun, 15 Mar 2026 15:48:09 -0700 Subject: [PATCH] terminal: consolidate mouse types into mouse.zig Move MouseEvent and MouseFormat out of Terminal.zig and MouseShape out of mouse_shape.zig into a new mouse.zig file. The types are named without the Mouse prefix inside the module (Event, Format, Shape) and re-exported with the prefix from terminal/main.zig for external use. Update all call sites (mouse_encode.zig, surface_mouse.zig, stream.zig) to import through terminal/main.zig or directly from mouse.zig. Remove the now-unused mouse_shape.zig. --- src/input/mouse_encode.zig | 7 ++-- src/surface_mouse.zig | 4 +-- src/terminal/Terminal.zig | 33 +++--------------- src/terminal/main.zig | 5 ++- src/terminal/{mouse_shape.zig => mouse.zig} | 37 +++++++++++++++++---- src/terminal/stream.zig | 2 +- 6 files changed, 46 insertions(+), 42 deletions(-) rename src/terminal/{mouse_shape.zig => mouse.zig} (77%) diff --git a/src/input/mouse_encode.zig b/src/input/mouse_encode.zig index 2dfe084fb..9dceb0ab3 100644 --- a/src/input/mouse_encode.zig +++ b/src/input/mouse_encode.zig @@ -1,6 +1,7 @@ const std = @import("std"); const testing = std.testing; -const Terminal = @import("../terminal/Terminal.zig"); +const terminal = @import("../terminal/main.zig"); +const Terminal = terminal.Terminal; const renderer_size = @import("../renderer/size.zig"); const point = @import("../terminal/point.zig"); const key = @import("key.zig"); @@ -11,10 +12,10 @@ const log = std.log.scoped(.mouse_encode); /// Options that affect mouse encoding behavior and provide runtime context. pub const Options = struct { /// Terminal mouse reporting mode (X10, normal, button, any). - event: Terminal.MouseEvent = .none, + event: terminal.MouseEvent = .none, /// Terminal mouse reporting format. - format: Terminal.MouseFormat = .x10, + format: terminal.MouseFormat = .x10, /// Full renderer size used to convert surface-space pixel positions /// into grid cell coordinates (for most formats) and terminal-space diff --git a/src/surface_mouse.zig b/src/surface_mouse.zig index 6d3c11394..8fa53d240 100644 --- a/src/surface_mouse.zig +++ b/src/surface_mouse.zig @@ -9,14 +9,14 @@ const std = @import("std"); const builtin = @import("builtin"); const input = @import("input.zig"); const terminal = @import("terminal/main.zig"); -const MouseShape = @import("terminal/mouse_shape.zig").MouseShape; +const MouseShape = terminal.MouseShape; /// For processing key events; the key that was physically pressed on the /// keyboard. physical_key: input.Key, /// The mouse event tracking mode, if any. -mouse_event: terminal.Terminal.MouseEvent, +mouse_event: terminal.MouseEvent, /// The current terminal's mouse shape. mouse_shape: MouseShape, diff --git a/src/terminal/Terminal.zig b/src/terminal/Terminal.zig index 9e21ba97a..636a8f2ee 100644 --- a/src/terminal/Terminal.zig +++ b/src/terminal/Terminal.zig @@ -23,7 +23,7 @@ const point = @import("point.zig"); const sgr = @import("sgr.zig"); const Tabstops = @import("Tabstops.zig"); const color = @import("color.zig"); -const mouse_shape_pkg = @import("mouse_shape.zig"); +const mouse = @import("mouse.zig"); const ReadonlyHandler = @import("stream_readonly.zig").Handler; const ReadonlyStream = @import("stream_readonly.zig").Stream; @@ -79,7 +79,7 @@ previous_char: ?u21 = null, modes: modespkg.ModeState = .{}, /// The most recently set mouse shape for the terminal. -mouse_shape: mouse_shape_pkg.MouseShape = .text, +mouse_shape: mouse.Shape = .text, /// These are just a packed set of flags we may set on the terminal. flags: packed struct { @@ -96,8 +96,8 @@ flags: packed struct { /// set mode in modes. You can't get the right event/format to use /// based on modes alone because modes don't show you what order /// this was called so we have to track it separately. - mouse_event: MouseEvent = .none, - mouse_format: MouseFormat = .x10, + mouse_event: mouse.Event = .none, + mouse_format: mouse.Format = .x10, /// Set via the XTSHIFTESCAPE sequence. If true (XTSHIFTESCAPE = 1) /// then we want to capture the shift key for the mouse protocol @@ -167,31 +167,6 @@ pub const Dirty = packed struct { preedit: bool = false, }; -/// The event types that can be reported for mouse-related activities. -/// These are all mutually exclusive (hence in a single enum). -pub const MouseEvent = enum(u3) { - none = 0, - x10 = 1, // 9 - normal = 2, // 1000 - button = 3, // 1002 - any = 4, // 1003 - - /// Returns true if this event sends motion events. - pub fn motion(self: MouseEvent) bool { - return self == .button or self == .any; - } -}; - -/// The format of mouse events when enabled. -/// These are all mutually exclusive (hence in a single enum). -pub const MouseFormat = enum(u3) { - x10 = 0, - utf8 = 1, // 1005 - sgr = 2, // 1006 - urxvt = 3, // 1015 - sgr_pixels = 4, // 1016 -}; - /// Scrolling region is the area of the screen designated where scrolling /// occurs. When scrolling the screen, only this viewport is scrolled. pub const ScrollingRegion = struct { diff --git a/src/terminal/main.zig b/src/terminal/main.zig index 06c930014..a87d5ec87 100644 --- a/src/terminal/main.zig +++ b/src/terminal/main.zig @@ -31,7 +31,10 @@ pub const Cell = page.Cell; pub const Coordinate = point.Coordinate; pub const CSI = Parser.Action.CSI; pub const DCS = Parser.Action.DCS; -pub const MouseShape = @import("mouse_shape.zig").MouseShape; +pub const mouse = @import("mouse.zig"); +pub const MouseEvent = mouse.Event; +pub const MouseFormat = mouse.Format; +pub const MouseShape = mouse.Shape; pub const Page = page.Page; pub const PageList = @import("PageList.zig"); pub const Parser = @import("Parser.zig"); diff --git a/src/terminal/mouse_shape.zig b/src/terminal/mouse.zig similarity index 77% rename from src/terminal/mouse_shape.zig rename to src/terminal/mouse.zig index b5c6ac4d1..758369ae9 100644 --- a/src/terminal/mouse_shape.zig +++ b/src/terminal/mouse.zig @@ -2,12 +2,37 @@ const std = @import("std"); const build_options = @import("terminal_options"); const lib = @import("../lib/main.zig"); +/// The event types that can be reported for mouse-related activities. +/// These are all mutually exclusive (hence in a single enum). +pub const Event = enum(u3) { + none = 0, + x10 = 1, // 9 + normal = 2, // 1000 + button = 3, // 1002 + any = 4, // 1003 + + /// Returns true if this event sends motion events. + pub fn motion(self: Event) bool { + return self == .button or self == .any; + } +}; + +/// The format of mouse events when enabled. +/// These are all mutually exclusive (hence in a single enum). +pub const Format = enum(u3) { + x10 = 0, + utf8 = 1, // 1005 + sgr = 2, // 1006 + urxvt = 3, // 1015 + sgr_pixels = 4, // 1016 +}; + /// The possible cursor shapes. Not all app runtimes support these shapes. /// The shapes are always based on the W3C supported cursor styles so we /// can have a cross platform list. // // Must be kept in sync with ghostty_cursor_shape_e -pub const MouseShape = enum(c_int) { +pub const Shape = enum(c_int) { default, context_menu, help, @@ -44,7 +69,7 @@ pub const MouseShape = enum(c_int) { zoom_out, /// Build cursor shape from string or null if its unknown. - pub fn fromString(v: []const u8) ?MouseShape { + pub fn fromString(v: []const u8) ?Shape { return string_map.get(v); } @@ -57,7 +82,7 @@ pub const MouseShape = enum(c_int) { break :gtk switch (@import("../build_config.zig").app_runtime) { .gtk => @import("gobject").ext.defineEnum( - MouseShape, + Shape, .{ .name = "GhosttyMouseShape" }, ), @@ -66,11 +91,11 @@ pub const MouseShape = enum(c_int) { }; test "ghostty.h MouseShape" { - try lib.checkGhosttyHEnum(MouseShape, "GHOSTTY_MOUSE_SHAPE_"); + try lib.checkGhosttyHEnum(Shape, "GHOSTTY_MOUSE_SHAPE_"); } }; -const string_map = std.StaticStringMap(MouseShape).initComptime(.{ +const string_map = std.StaticStringMap(Shape).initComptime(.{ // W3C .{ "default", .default }, .{ "context-menu", .context_menu }, @@ -134,7 +159,7 @@ const string_map = std.StaticStringMap(MouseShape).initComptime(.{ test "cursor shape from string" { const testing = std.testing; - try testing.expectEqual(MouseShape.default, MouseShape.fromString("default").?); + try testing.expectEqual(Shape.default, Shape.fromString("default").?); } test { diff --git a/src/terminal/stream.zig b/src/terminal/stream.zig index 5f83128a9..f78bcc6f5 100644 --- a/src/terminal/stream.zig +++ b/src/terminal/stream.zig @@ -16,7 +16,7 @@ const modes = @import("modes.zig"); const osc = @import("osc.zig"); const sgr = @import("sgr.zig"); const UTF8Decoder = @import("UTF8Decoder.zig"); -const MouseShape = @import("mouse_shape.zig").MouseShape; +const MouseShape = @import("mouse.zig").Shape; const log = std.log.scoped(.stream);