-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput.zig
More file actions
298 lines (255 loc) · 9.43 KB
/
input.zig
File metadata and controls
298 lines (255 loc) · 9.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
//! Input encoding for terminal - keys, text, and mouse events.
//! Key encoding delegates to ghostty-vt for correct handling of all
//! terminal modes (kitty keyboard, modifyOtherKeys, application cursor, etc.).
const std = @import("std");
const ghostty = @import("ghostty-vt");
const Key = ghostty.input.Key;
const KeyEvent = ghostty.input.KeyEvent;
const KeyMods = ghostty.input.KeyMods;
pub const KeyEncodeOptions = ghostty.input.KeyEncodeOptions;
const log = std.log.scoped(.input);
/// Result of text encoding - tracks whether allocation occurred
pub const EncodeResult = struct {
text: []const u8,
owned: bool,
/// Free the text if it was allocated
pub fn deinit(self: EncodeResult, allocator: std.mem.Allocator) void {
if (self.owned) {
allocator.free(self.text);
}
}
};
/// Encode text for terminal input.
/// Converts \n to \r for proper terminal input (Enter sends \r).
/// Returns EncodeResult with owned=true if allocation occurred.
pub fn encodeText(allocator: std.mem.Allocator, text: []const u8) !EncodeResult {
if (std.mem.indexOfScalar(u8, text, '\n') == null) {
return .{ .text = text, .owned = false };
}
const result = try allocator.alloc(u8, text.len);
for (text, 0..) |c, i| {
result[i] = if (c == '\n') '\r' else c;
}
return .{ .text = result, .owned = true };
}
pub const KeyEventInput = struct {
key: []const u8,
code: ?[]const u8 = null,
mods: KeyMods = .{},
};
/// Encode a key event using ghostty-vt's encoder, which respects all active
/// terminal modes. Caller should obtain opts via KeyEncodeOptions.fromTerminal()
/// while holding the terminal lock, then pass them here.
pub fn encodeKeyEvent(
event: KeyEventInput,
opts: KeyEncodeOptions,
buf: []u8,
) ?[]const u8 {
const key_event = buildKeyEvent(event) orelse return null;
var writer: std.Io.Writer = .fixed(buf);
ghostty.input.encodeKey(&writer, key_event, opts) catch |err| {
log.warn("key encode failed: {}", .{err});
return null;
};
const encoded = writer.buffered();
if (encoded.len == 0) return null;
return encoded;
}
fn buildKeyEvent(input: KeyEventInput) ?KeyEvent {
if (input.key.len == 0 and input.code == null) return null;
// Keep our mapping aligned with Prise's W3C key parsing logic
// (prise/src/key_parse.zig) so ghostty-vt sees consistent key events.
var key_enum: Key = .unidentified;
if (input.code) |code| {
key_enum = Key.fromW3C(code) orelse return null;
}
const utf8 = input.key;
const utf8_codepoint = firstCodepoint(utf8);
var unshifted_codepoint: u21 = 0;
if (key_enum.codepoint()) |cp| {
unshifted_codepoint = cp;
} else if (utf8_codepoint != 0) {
unshifted_codepoint = utf8_codepoint;
}
if (input.mods.shift and utf8_codepoint >= 'A' and utf8_codepoint <= 'Z') {
unshifted_codepoint = std.ascii.toLower(@intCast(utf8_codepoint));
}
const shift_consumed = utf8.len > 0 and input.mods.shift and utf8_codepoint != 0 and
(input.code == null or unshifted_codepoint != utf8_codepoint);
return .{
.action = .press,
.key = key_enum,
.mods = input.mods,
.consumed_mods = .{ .shift = shift_consumed },
.utf8 = utf8,
.unshifted_codepoint = unshifted_codepoint,
};
}
fn firstCodepoint(utf8: []const u8) u21 {
if (utf8.len == 0) return 0;
const view = std.unicode.Utf8View.init(utf8) catch return 0;
var it = view.iterator();
return it.nextCodepoint() orelse 0;
}
/// Encode mouse event to escape sequence
pub fn encodeMouse(
button: MouseButton,
row: u16,
col: u16,
press: bool,
modifiers: Modifiers,
format: MouseFormat,
buf: []u8,
) ?[]const u8 {
const base_cb: u8 = switch (button) {
.left => 0,
.middle => 1,
.right => 2,
.scroll_up => 64,
.scroll_down => 65,
.none => 3,
};
// Modifier bits per xterm spec: Shift=+4, Alt=+8, Ctrl=+16
var cb = base_cb;
if (modifiers.shift) cb += 4;
if (modifiers.alt) cb += 8;
if (modifiers.ctrl) cb += 16;
switch (format) {
.sgr => {
const suffix: u8 = if (press) 'M' else 'm';
const len = std.fmt.bufPrint(buf, "\x1b[<{d};{d};{d}{c}", .{
cb,
col + 1,
row + 1,
suffix,
}) catch return null;
return len;
},
.x10 => {
if (buf.len < 6) return null;
buf[0] = 0x1b;
buf[1] = '[';
buf[2] = 'M';
buf[3] = 32 + cb;
buf[4] = @intCast(@min(col + 33, 255));
buf[5] = @intCast(@min(row + 33, 255));
return buf[0..6];
},
}
}
pub const MouseButton = enum {
left,
middle,
right,
scroll_up,
scroll_down,
none,
};
pub const MouseFormat = enum {
x10,
sgr,
};
pub const Modifiers = struct {
shift: bool = false,
ctrl: bool = false,
alt: bool = false,
};
test "encode ctrl+c via ghostty" {
var buf: [32]u8 = undefined;
const result = encodeKeyEvent(.{ .key = "c", .mods = .{ .ctrl = true } }, KeyEncodeOptions.default, &buf);
try std.testing.expect(result != null);
try std.testing.expectEqualSlices(u8, "\x03", result.?);
}
test "encode enter via ghostty" {
var buf: [32]u8 = undefined;
const result = encodeKeyEvent(.{ .key = "", .code = "Enter" }, KeyEncodeOptions.default, &buf);
try std.testing.expect(result != null);
try std.testing.expectEqualSlices(u8, "\r", result.?);
}
test "encode punctuation via utf8" {
var buf: [32]u8 = undefined;
const result = encodeKeyEvent(.{ .key = ":" }, KeyEncodeOptions.default, &buf);
try std.testing.expect(result != null);
try std.testing.expectEqualSlices(u8, ":", result.?);
}
test "encode shifted letter via utf8" {
var buf: [32]u8 = undefined;
const result = encodeKeyEvent(.{ .key = "A", .mods = .{ .shift = true } }, KeyEncodeOptions.default, &buf);
try std.testing.expect(result != null);
try std.testing.expectEqualSlices(u8, "A", result.?);
}
test "encode alt letter via ghostty" {
var buf: [32]u8 = undefined;
var opts = KeyEncodeOptions.default;
opts.alt_esc_prefix = true;
opts.macos_option_as_alt = .true;
const result = encodeKeyEvent(.{ .key = "x", .mods = .{ .alt = true } }, opts, &buf);
try std.testing.expect(result != null);
try std.testing.expectEqualSlices(u8, "\x1bx", result.?);
}
test "encode arrow keys normal vs application mode" {
var buf: [32]u8 = undefined;
// Normal mode (default)
const normal = encodeKeyEvent(.{ .key = "", .code = "ArrowUp" }, KeyEncodeOptions.default, &buf);
try std.testing.expect(normal != null);
try std.testing.expectEqualSlices(u8, "\x1b[A", normal.?);
// Application cursor mode
var app_opts = KeyEncodeOptions.default;
app_opts.cursor_key_application = true;
const app = encodeKeyEvent(.{ .key = "", .code = "ArrowUp" }, app_opts, &buf);
try std.testing.expect(app != null);
try std.testing.expectEqualSlices(u8, "\x1bOA", app.?);
}
test "encode text newline conversion" {
const allocator = std.testing.allocator;
// No newlines - should not allocate
const no_nl = try encodeText(allocator, "hello");
defer no_nl.deinit(allocator);
try std.testing.expect(!no_nl.owned);
try std.testing.expectEqualSlices(u8, "hello", no_nl.text);
// With newlines - should allocate and convert
const with_nl = try encodeText(allocator, "line1\nline2\n");
defer with_nl.deinit(allocator);
try std.testing.expect(with_nl.owned);
try std.testing.expectEqualSlices(u8, "line1\rline2\r", with_nl.text);
}
test "encode mouse SGR left click" {
var buf: [64]u8 = undefined;
const result = encodeMouse(.left, 5, 10, true, .{}, .sgr, &buf);
try std.testing.expect(result != null);
// SGR format: \x1b[<Cb;Col;RowM (1-indexed)
try std.testing.expectEqualSlices(u8, "\x1b[<0;11;6M", result.?);
}
test "encode mouse SGR release" {
var buf: [64]u8 = undefined;
const result = encodeMouse(.left, 0, 0, false, .{}, .sgr, &buf);
try std.testing.expect(result != null);
try std.testing.expectEqualSlices(u8, "\x1b[<0;1;1m", result.?);
}
test "encode mouse SGR right click with modifiers" {
var buf: [64]u8 = undefined;
const result = encodeMouse(.right, 3, 7, true, .{ .shift = true, .ctrl = true }, .sgr, &buf);
try std.testing.expect(result != null);
// right=2, +shift(4)+ctrl(16) = 22
try std.testing.expectEqualSlices(u8, "\x1b[<22;8;4M", result.?);
}
test "encode mouse SGR scroll up" {
var buf: [64]u8 = undefined;
const result = encodeMouse(.scroll_up, 10, 20, true, .{}, .sgr, &buf);
try std.testing.expect(result != null);
try std.testing.expectEqualSlices(u8, "\x1b[<64;21;11M", result.?);
}
test "encode mouse X10 left click" {
var buf: [64]u8 = undefined;
const result = encodeMouse(.left, 0, 0, true, .{}, .x10, &buf);
try std.testing.expect(result != null);
// X10: \x1b[M then (32+cb) (col+33) (row+33)
try std.testing.expectEqualSlices(u8, "\x1b[M\x20\x21\x21", result.?);
}
test "encode mouse X10 with alt modifier" {
var buf: [64]u8 = undefined;
const result = encodeMouse(.left, 5, 10, true, .{ .alt = true }, .x10, &buf);
try std.testing.expect(result != null);
// left=0, +alt(8) = 8; col=10+33=43, row=5+33=38
try std.testing.expectEqualSlices(u8, "\x1b[M\x28\x2b\x26", result.?);
}