From c81dc40ba38389295c7bc0af2a71b2d9a402cebd Mon Sep 17 00:00:00 2001 From: Shinyzenith Date: Mon, 30 Oct 2023 00:40:51 +0530 Subject: [PATCH] feat(pointer): cmd: add sloppy-focus Signed-off-by: Shinyzenith --- next/Config.zig | 2 ++ next/control/command.zig | 1 + next/control/cursor.zig | 18 ++++++++++++++++++ next/input/Cursor.zig | 9 ++++++++- 4 files changed, 29 insertions(+), 1 deletion(-) diff --git a/next/Config.zig b/next/Config.zig index 2f71940..6878964 100644 --- a/next/Config.zig +++ b/next/Config.zig @@ -38,6 +38,8 @@ toplevel_corner_radius: c_int = 20, toplevel_opacity: f32 = 1, // Ranges from 0 to 1 toplevel_box_shadow_color: [4]f32 = .{ 0.0, 0.0, 0.0, 1.0 }, +focus_is_sloppy: bool = true, + pub fn init() Self { log.debug("Initialized compositor config", .{}); const self = .{}; diff --git a/next/control/command.zig b/next/control/command.zig index 92a8380..ac75607 100644 --- a/next/control/command.zig +++ b/next/control/command.zig @@ -44,6 +44,7 @@ const commands = std.ComptimeStringMap( .{ "set-repeat-rate", inputs.setRepeat }, .{ "warp-cursor", cursor.warpCursor }, .{ "hide-cursor", cursor.hideCursor }, + .{ "sloppy-focus", cursor.setSloppyFocus }, }, ); // zig fmt: on diff --git a/next/control/cursor.zig b/next/control/cursor.zig index 055a987..f2e602a 100644 --- a/next/control/cursor.zig +++ b/next/control/cursor.zig @@ -48,3 +48,21 @@ pub fn warpCursor( out.* = try std.fmt.allocPrint(allocator, "Failed to parse provided state.\n", .{}); } } + +pub fn setSloppyFocus( + args: []const [:0]const u8, + out: *?[]const u8, +) !void { + if (args.len < 2) return Error.NotEnoughArguments; + if (args.len > 2) return Error.TooManyArguments; + + if (std.mem.eql(u8, "true", args[1])) { + server.config.focus_is_sloppy = true; + } else if (std.mem.eql(u8, "false", args[1])) { + server.config.focus_is_sloppy = false; + } else if (std.mem.eql(u8, "toggle", args[1])) { + server.config.focus_is_sloppy = !server.config.focus_is_sloppy; + } else { + out.* = try std.fmt.allocPrint(allocator, "Failed to parse focus state: {s}\n", .{args[1]}); + } +} diff --git a/next/input/Cursor.zig b/next/input/Cursor.zig index bdff520..302b8ee 100644 --- a/next/input/Cursor.zig +++ b/next/input/Cursor.zig @@ -115,6 +115,10 @@ pub fn handleButton(listener: *wl.Listener(*wlr.Pointer.event.Button), event: *w log.debug("Signal: wlr_pointer_button", .{}); _ = self.server.seat.wlr_seat.pointerNotifyButton(event.time_msec, event.button, event.state); + + if (Window.windowAt(self.server.wlr_cursor.x, self.server.wlr_cursor.y)) |window_data| { + self.server.seat.setFocus(window_data.window, window_data.surface); + } } pub fn handleFrame(listener: *wl.Listener(*wlr.Cursor), _: *wlr.Cursor) void { @@ -146,11 +150,14 @@ pub fn processCursorMotion(self: *Self, time_msec: u32) void { //TODO: Handle cursor modes like move, resize, etc // for now we do very basic handling of a passthrough mode. + // Passthrough mode. if (Window.windowAt(self.server.wlr_cursor.x, self.server.wlr_cursor.y)) |window_data| { self.server.seat.wlr_seat.pointerNotifyEnter(window_data.surface, window_data.sx, window_data.sy); self.server.seat.wlr_seat.pointerNotifyMotion(time_msec, window_data.sx, window_data.sy); - self.server.seat.setFocus(window_data.window, window_data.surface); + if (self.server.config.focus_is_sloppy) { + self.server.seat.setFocus(window_data.window, window_data.surface); + } } else { self.server.wlr_xcursor_manager.setCursorImage("left_ptr", self.server.wlr_cursor); self.server.seat.wlr_seat.pointerClearFocus();