fix(vaxis): enable pixel mouse mode only if detected

And use translated cell + offset coordinates for compatibility with
terminals that do not support pixel mouse mode.
This commit is contained in:
CJ van den Berg 2024-05-22 21:26:02 +02:00
parent f075ab7272
commit 1d698afe55

View file

@ -92,7 +92,6 @@ pub fn run(self: *Self) !void {
const ws = try vaxis.Tty.getWinsize(self.input_fd_blocking()); const ws = try vaxis.Tty.getWinsize(self.input_fd_blocking());
try self.vx.resize(self.a, ws); try self.vx.resize(self.a, ws);
self.vx.queueRefresh(); self.vx.queueRefresh();
try self.vx.setMouseMode(.pixels);
try self.vx.setBracketedPaste(true); try self.vx.setBracketedPaste(true);
} }
@ -178,53 +177,47 @@ pub fn process_input(self: *Self, input_: []const u8) !void {
}); });
if (self.bracketed_paste) {} else if (self.dispatch_input) |f| f(self.handler_ctx, cbor_msg); if (self.bracketed_paste) {} else if (self.dispatch_input) |f| f(self.handler_ctx, cbor_msg);
}, },
.mouse => |mouse| { .mouse => |mouse_| {
const ypos = mouse.row - 1; const mouse = self.vx.translateMouse(mouse_);
const xpos = mouse.col - 1; // const mouse = mouse_;
const ycell = self.vx.screen.height_pix / self.vx.screen.height;
const xcell = self.vx.screen.width_pix / self.vx.screen.width;
const y = ypos / ycell;
const x = xpos / xcell;
const ypx = ypos % ycell;
const xpx = xpos % xcell;
if (self.dispatch_mouse) |f| switch (mouse.type) { if (self.dispatch_mouse) |f| switch (mouse.type) {
.motion => f(self.handler_ctx, @intCast(y), @intCast(x), try self.fmtmsg(.{ .motion => f(self.handler_ctx, @intCast(mouse.row), @intCast(mouse.col), try self.fmtmsg(.{
"M", "M",
x, mouse.col,
y, mouse.row,
xpx, mouse.xoffset,
ypx, mouse.yoffset,
})), })),
.press => f(self.handler_ctx, @intCast(y), @intCast(x), try self.fmtmsg(.{ .press => f(self.handler_ctx, @intCast(mouse.row), @intCast(mouse.col), try self.fmtmsg(.{
"B", "B",
event_type.PRESS, event_type.PRESS,
@intFromEnum(mouse.button), @intFromEnum(mouse.button),
input.utils.button_id_string(@intFromEnum(mouse.button)), input.utils.button_id_string(@intFromEnum(mouse.button)),
x, mouse.col,
y, mouse.row,
xpx, mouse.xoffset,
ypx, mouse.yoffset,
})), })),
.release => f(self.handler_ctx, @intCast(y), @intCast(x), try self.fmtmsg(.{ .release => f(self.handler_ctx, @intCast(mouse.row), @intCast(mouse.col), try self.fmtmsg(.{
"B", "B",
event_type.RELEASE, event_type.RELEASE,
@intFromEnum(mouse.button), @intFromEnum(mouse.button),
input.utils.button_id_string(@intFromEnum(mouse.button)), input.utils.button_id_string(@intFromEnum(mouse.button)),
x, mouse.col,
y, mouse.row,
xpx, mouse.xoffset,
ypx, mouse.yoffset,
})), })),
.drag => if (self.dispatch_mouse_drag) |f_| .drag => if (self.dispatch_mouse_drag) |f_|
f_(self.handler_ctx, @intCast(y), @intCast(x), true, try self.fmtmsg(.{ f_(self.handler_ctx, @intCast(mouse.row), @intCast(mouse.col), true, try self.fmtmsg(.{
"D", "D",
event_type.PRESS, event_type.PRESS,
@intFromEnum(mouse.button), @intFromEnum(mouse.button),
input.utils.button_id_string(@intFromEnum(mouse.button)), input.utils.button_id_string(@intFromEnum(mouse.button)),
x, mouse.col,
y, mouse.row,
xpx, mouse.xoffset,
ypx, mouse.yoffset,
})), })),
}; };
}, },
@ -248,8 +241,16 @@ pub fn process_input(self: *Self, input_: []const u8) !void {
self.vx.caps.unicode = .unicode; self.vx.caps.unicode = .unicode;
self.vx.screen.width_method = .unicode; self.vx.screen.width_method = .unicode;
}, },
.cap_sgr_pixels => {
self.logger.print("pixel mouse capability detected", .{});
self.vx.caps.sgr_pixels = true;
},
.cap_da1 => { .cap_da1 => {
self.vx.enableDetectedFeatures() catch |e| self.logger.err("enable features", e); self.vx.enableDetectedFeatures() catch |e| self.logger.err("enable features", e);
self.vx.setMouseMode(.pixels) catch |e| switch (e) {
error.NoSgrPixelsCapability => try self.vx.setMouseMode(.cells),
else => return e,
};
}, },
.cap_kitty_keyboard => { .cap_kitty_keyboard => {
self.logger.print("kitty keyboard capability detected", .{}); self.logger.print("kitty keyboard capability detected", .{});