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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
|
const std = @import("std");
const Allocator = std.mem.Allocator;
const testing = std.testing;
const lib = @import("../lib.zig");
const CAllocator = lib.alloc.Allocator;
const input_mouse_encode = @import("../../input/mouse_encode.zig");
const renderer_size = @import("../../renderer/size.zig");
const point = @import("../point.zig");
const terminal_mouse = @import("../mouse.zig");
const mouse_event = @import("mouse_event.zig");
const Result = @import("result.zig").Result;
const Event = mouse_event.Event;
const Terminal = @import("terminal.zig").Terminal;
const ZigTerminal = @import("../Terminal.zig");
const log = std.log.scoped(.mouse_encode);
/// Wrapper around mouse encoding options that tracks the allocator for C API usage.
const MouseEncoderWrapper = struct {
opts: input_mouse_encode.Options,
track_last_cell: bool = false,
last_cell: ?point.Coordinate = null,
alloc: Allocator,
};
/// C: GhosttyMouseEncoder
pub const Encoder = ?*MouseEncoderWrapper;
/// C: GhosttyMouseTrackingMode
pub const TrackingMode = terminal_mouse.Event;
/// C: GhosttyMouseFormat
pub const Format = terminal_mouse.Format;
/// C: GhosttyMouseEncoderSize
pub const Size = extern struct {
size: usize = @sizeOf(Size),
screen_width: u32,
screen_height: u32,
cell_width: u32,
cell_height: u32,
padding_top: u32,
padding_bottom: u32,
padding_right: u32,
padding_left: u32,
fn toRenderer(self: Size) ?renderer_size.Size {
if (self.cell_width == 0 or self.cell_height == 0) return null;
return .{
.screen = .{
.width = self.screen_width,
.height = self.screen_height,
},
.cell = .{
.width = self.cell_width,
.height = self.cell_height,
},
.padding = .{
.top = self.padding_top,
.bottom = self.padding_bottom,
.right = self.padding_right,
.left = self.padding_left,
},
};
}
};
/// C: GhosttyMouseEncoderOption
pub const Option = enum(c_int) {
event = 0,
format = 1,
size = 2,
any_button_pressed = 3,
track_last_cell = 4,
/// Input type expected for setting the option.
pub fn InType(comptime self: Option) type {
return switch (self) {
.event => TrackingMode,
.format => Format,
.size => Size,
.any_button_pressed,
.track_last_cell,
=> bool,
};
}
};
pub fn new(
alloc_: ?*const CAllocator,
result: *Encoder,
) callconv(lib.calling_conv) Result {
const alloc = lib.alloc.default(alloc_);
const ptr = alloc.create(MouseEncoderWrapper) catch
return .out_of_memory;
ptr.* = .{
.opts = .{ .size = defaultSize() },
.alloc = alloc,
};
result.* = ptr;
return .success;
}
pub fn free(encoder_: Encoder) callconv(lib.calling_conv) void {
const wrapper = encoder_ orelse return;
const alloc = wrapper.alloc;
alloc.destroy(wrapper);
}
pub fn setopt(
encoder_: Encoder,
option: Option,
value: ?*const anyopaque,
) callconv(lib.calling_conv) void {
if (comptime std.debug.runtime_safety) {
_ = std.enums.fromInt(Option, @intFromEnum(option)) orelse {
log.warn("setopt invalid option value={d}", .{@intFromEnum(option)});
return;
};
}
return switch (option) {
inline else => |comptime_option| setoptTyped(
encoder_,
comptime_option,
@ptrCast(@alignCast(value orelse return)),
),
};
}
fn setoptTyped(
encoder_: Encoder,
comptime option: Option,
value: *const option.InType(),
) void {
const wrapper = encoder_.?;
switch (option) {
.event => {
if (comptime std.debug.runtime_safety) {
_ = std.enums.fromInt(TrackingMode, @intFromEnum(value.*)) orelse {
log.warn("setopt invalid TrackingMode value={d}", .{@intFromEnum(value.*)});
return;
};
}
if (wrapper.opts.event != value.*) wrapper.last_cell = null;
wrapper.opts.event = value.*;
},
.format => {
if (comptime std.debug.runtime_safety) {
_ = std.enums.fromInt(Format, @intFromEnum(value.*)) orelse {
log.warn("setopt invalid Format value={d}", .{@intFromEnum(value.*)});
return;
};
}
if (wrapper.opts.format != value.*) wrapper.last_cell = null;
wrapper.opts.format = value.*;
},
.size => {
if (value.size < @sizeOf(Size)) {
log.warn("setopt size struct too small size={d} expected>={d}", .{
value.size,
@sizeOf(Size),
});
return;
}
wrapper.opts.size = value.toRenderer() orelse {
log.warn("setopt invalid size values (cell width and height must be non-zero)", .{});
return;
};
wrapper.last_cell = null;
},
.any_button_pressed => wrapper.opts.any_button_pressed = value.*,
.track_last_cell => {
wrapper.track_last_cell = value.*;
if (!value.*) wrapper.last_cell = null;
},
}
}
pub fn setopt_from_terminal(
encoder_: Encoder,
terminal_: Terminal,
) callconv(lib.calling_conv) void {
const wrapper = encoder_ orelse return;
const t: *ZigTerminal = (terminal_ orelse return).terminal;
wrapper.opts.event = t.flags.mouse_event;
wrapper.opts.format = t.flags.mouse_format;
wrapper.last_cell = null;
}
pub fn reset(encoder_: Encoder) callconv(lib.calling_conv) void {
const wrapper = encoder_ orelse return;
wrapper.last_cell = null;
}
pub fn encode(
encoder_: Encoder,
event_: Event,
out_: ?[*]u8,
out_len: usize,
out_written: *usize,
) callconv(lib.calling_conv) Result {
const wrapper = encoder_ orelse return .invalid_value;
const event = event_ orelse return .invalid_value;
const prev_last_cell = wrapper.last_cell;
var opts = wrapper.opts;
opts.last_cell = if (wrapper.track_last_cell) &wrapper.last_cell else null;
var writer: std.Io.Writer = .fixed(if (out_) |out| out[0..out_len] else &.{});
input_mouse_encode.encode(
&writer,
event.event,
opts,
) catch |err| switch (err) {
error.WriteFailed => {
// Failed writes should not mutate motion dedupe state because no
// complete sequence was produced.
wrapper.last_cell = prev_last_cell;
// Use a discarding writer to count how much space we would have needed.
var count_last_cell = prev_last_cell;
var count_opts = wrapper.opts;
count_opts.last_cell = if (wrapper.track_last_cell) &count_last_cell else null;
var discarding: std.Io.Writer.Discarding = .init(&.{});
input_mouse_encode.encode(
&discarding.writer,
event.event,
count_opts,
) catch unreachable;
// Discarding always uses a u64. If we're on 32-bit systems
// we cast down. We should make this safer in the future.
out_written.* = @intCast(discarding.count);
return .out_of_space;
},
};
out_written.* = writer.end;
return .success;
}
fn defaultSize() renderer_size.Size {
return .{
.screen = .{ .width = 1, .height = 1 },
.cell = .{ .width = 1, .height = 1 },
.padding = .{},
};
}
fn testSize() Size {
return .{
.size = @sizeOf(Size),
.screen_width = 1_000,
.screen_height = 1_000,
.cell_width = 1,
.cell_height = 1,
.padding_top = 0,
.padding_bottom = 0,
.padding_right = 0,
.padding_left = 0,
};
}
test "alloc" {
var e: Encoder = undefined;
try testing.expectEqual(Result.success, new(
&lib.alloc.test_allocator,
&e,
));
free(e);
}
test "setopt" {
var e: Encoder = undefined;
try testing.expectEqual(Result.success, new(
&lib.alloc.test_allocator,
&e,
));
defer free(e);
const event_mode: TrackingMode = .any;
setopt(e, .event, &event_mode);
try testing.expectEqual(TrackingMode.any, e.?.opts.event);
const format_mode: Format = .sgr;
setopt(e, .format, &format_mode);
try testing.expectEqual(Format.sgr, e.?.opts.format);
const size = testSize();
setopt(e, .size, &size);
try testing.expectEqual(size.screen_width, e.?.opts.size.screen.width);
try testing.expectEqual(size.screen_height, e.?.opts.size.screen.height);
const any_button_pressed = true;
setopt(e, .any_button_pressed, &any_button_pressed);
try testing.expect(e.?.opts.any_button_pressed);
const track_last_cell = true;
setopt(e, .track_last_cell, &track_last_cell);
try testing.expect(e.?.track_last_cell);
}
test "setopt_from_terminal" {
const terminal_c = @import("terminal.zig");
var e: Encoder = undefined;
try testing.expectEqual(Result.success, new(
&lib.alloc.test_allocator,
&e,
));
defer free(e);
var t: Terminal = undefined;
try testing.expectEqual(Result.success, terminal_c.new(
&lib.alloc.test_allocator,
&t,
80,
24,
));
defer terminal_c.free(t);
const event_mode: TrackingMode = .any;
setopt(e, .event, &event_mode);
const format_mode: Format = .sgr;
setopt(e, .format, &format_mode);
setopt_from_terminal(e, t);
try testing.expectEqual(TrackingMode.none, e.?.opts.event);
try testing.expectEqual(Format.x10, e.?.opts.format);
}
test "setopt_from_terminal null" {
setopt_from_terminal(null, null);
const terminal_c = @import("terminal.zig");
var t: Terminal = undefined;
try testing.expectEqual(Result.success, terminal_c.new(
&lib.alloc.test_allocator,
&t,
80,
24,
));
defer terminal_c.free(t);
setopt_from_terminal(null, t);
var e: Encoder = undefined;
try testing.expectEqual(Result.success, new(
&lib.alloc.test_allocator,
&e,
));
defer free(e);
setopt_from_terminal(e, null);
}
test "encode: sgr press left" {
var encoder: Encoder = undefined;
try testing.expectEqual(Result.success, new(
&lib.alloc.test_allocator,
&encoder,
));
defer free(encoder);
const event_mode: TrackingMode = .any;
setopt(encoder, .event, &event_mode);
const format_mode: Format = .sgr;
setopt(encoder, .format, &format_mode);
const size = testSize();
setopt(encoder, .size, &size);
var event: Event = undefined;
try testing.expectEqual(Result.success, mouse_event.new(
&lib.alloc.test_allocator,
&event,
));
defer mouse_event.free(event);
mouse_event.set_action(event, .press);
mouse_event.set_button(event, .left);
mouse_event.set_position(event, .{ .x = 0, .y = 0 });
var required: usize = 0;
try testing.expectEqual(Result.out_of_space, encode(
encoder,
event,
null,
0,
&required,
));
try testing.expectEqual(@as(usize, 9), required);
var buf: [32]u8 = undefined;
var written: usize = 0;
try testing.expectEqual(Result.success, encode(
encoder,
event,
&buf,
buf.len,
&written,
));
try testing.expectEqual(required, written);
try testing.expectEqualStrings("\x1b[<0;1;1M", buf[0..written]);
}
test "encode: motion dedupe and reset" {
var encoder: Encoder = undefined;
try testing.expectEqual(Result.success, new(
&lib.alloc.test_allocator,
&encoder,
));
defer free(encoder);
const event_mode: TrackingMode = .any;
setopt(encoder, .event, &event_mode);
const format_mode: Format = .sgr;
setopt(encoder, .format, &format_mode);
const size = testSize();
setopt(encoder, .size, &size);
const track_last_cell = true;
setopt(encoder, .track_last_cell, &track_last_cell);
var event: Event = undefined;
try testing.expectEqual(Result.success, mouse_event.new(
&lib.alloc.test_allocator,
&event,
));
defer mouse_event.free(event);
mouse_event.set_action(event, .motion);
mouse_event.set_button(event, .left);
mouse_event.set_position(event, .{ .x = 5, .y = 6 });
{
var buf: [32]u8 = undefined;
var written: usize = 0;
try testing.expectEqual(Result.success, encode(
encoder,
event,
&buf,
buf.len,
&written,
));
try testing.expect(written > 0);
}
{
var buf: [32]u8 = undefined;
var written: usize = 0;
try testing.expectEqual(Result.success, encode(
encoder,
event,
&buf,
buf.len,
&written,
));
try testing.expectEqual(@as(usize, 0), written);
}
reset(encoder);
{
var buf: [32]u8 = undefined;
var written: usize = 0;
try testing.expectEqual(Result.success, encode(
encoder,
event,
&buf,
buf.len,
&written,
));
try testing.expect(written > 0);
}
}
test "encode: querying required size doesn't update dedupe state" {
var encoder: Encoder = undefined;
try testing.expectEqual(Result.success, new(
&lib.alloc.test_allocator,
&encoder,
));
defer free(encoder);
const event_mode: TrackingMode = .any;
setopt(encoder, .event, &event_mode);
const format_mode: Format = .sgr;
setopt(encoder, .format, &format_mode);
const size = testSize();
setopt(encoder, .size, &size);
const track_last_cell = true;
setopt(encoder, .track_last_cell, &track_last_cell);
var event: Event = undefined;
try testing.expectEqual(Result.success, mouse_event.new(
&lib.alloc.test_allocator,
&event,
));
defer mouse_event.free(event);
mouse_event.set_action(event, .motion);
mouse_event.set_button(event, .left);
mouse_event.set_position(event, .{ .x = 5, .y = 6 });
var required: usize = 0;
try testing.expectEqual(Result.out_of_space, encode(
encoder,
event,
null,
0,
&required,
));
try testing.expect(required > 0);
var buf: [32]u8 = undefined;
var written: usize = 0;
try testing.expectEqual(Result.success, encode(
encoder,
event,
&buf,
buf.len,
&written,
));
try testing.expect(written > 0);
}
|