git.lucas.co / cce-compositor
Wayland compositor (wlroots)
git clone https://git.lucas.co/cce-compositor.git

commit39ef8676d136f8e4d3013134af3fd3da6405dda3
parent995e1047db
authorIsaac Freund <[email protected]>
date2023-12-31 13:32
TextInput: remove pending_focused_surface

This state doesn't need to exist, just get the focus from the Seat.

 river/InputManager.zig |  6 ++----
 river/InputRelay.zig   | 24 +++---------------------
 river/Seat.zig         | 19 +++++++++++--------
 river/TextInput.zig    | 24 ------------------------
 4 files changed, 16 insertions(+), 57 deletions(-)

diff --git a/river/InputManager.zig b/river/InputManager.zig
index f069257..c202c28 100644
--- a/river/InputManager.zig
+++ b/river/InputManager.zig
@@ -196,10 +196,8 @@ fn handleNewInputMethod(
 
     log.debug("new input method on seat {s}", .{relay.seat.wlr_seat.name});
 
-    const text_input = relay.getFocusableTextInput() orelse return;
-    if (text_input.pending_focused_surface) |surface| {
-        text_input.wlr_text_input.sendEnter(surface);
-        text_input.setPendingFocusedSurface(null);
+    if (seat.focused.surface()) |surface| {
+        relay.setSurfaceFocus(surface);
     }
 }
 
diff --git a/river/InputRelay.zig b/river/InputRelay.zig
index d3ffb6a..f5fad43 100644
--- a/river/InputRelay.zig
+++ b/river/InputRelay.zig
@@ -102,11 +102,9 @@ fn handleInputMethodDestroy(
 
     self.input_method = null;
 
-    const text_input = self.getFocusedTextInput() orelse return;
-    if (text_input.wlr_text_input.focused_surface) |surface| {
-        text_input.setPendingFocusedSurface(surface);
+    if (self.getFocusedTextInput()) |text_input| {
+        text_input.wlr_text_input.sendLeave();
     }
-    text_input.wlr_text_input.sendLeave();
 }
 
 fn handleInputMethodGrabKeyboard(
@@ -133,14 +131,6 @@ fn handleInputMethodGrabKeyboardDestroy(
     }
 }
 
-pub fn getFocusableTextInput(self: *Self) ?*TextInput {
-    var it = self.text_inputs.first;
-    return while (it) |node| : (it = node.next) {
-        const text_input = &node.data;
-        if (text_input.pending_focused_surface != null) break text_input;
-    } else null;
-}
-
 pub fn getFocusedTextInput(self: *Self) ?*TextInput {
     var it = self.text_inputs.first;
     return while (it) |node| : (it = node.next) {
@@ -194,13 +184,7 @@ pub fn setSurfaceFocus(self: *Self, wlr_surface: ?*wlr.Surface) void {
     var it = self.text_inputs.first;
     while (it) |node| : (it = node.next) {
         const text_input = &node.data;
-        if (text_input.pending_focused_surface) |surface| {
-            assert(text_input.wlr_text_input.focused_surface == null);
-            if (wlr_surface != surface) {
-                text_input.setPendingFocusedSurface(null);
-            }
-        } else if (text_input.wlr_text_input.focused_surface) |surface| {
-            assert(text_input.pending_focused_surface == null);
+        if (text_input.wlr_text_input.focused_surface) |surface| {
             if (wlr_surface != surface) {
                 text_input.relay.disableTextInput(text_input);
                 text_input.wlr_text_input.sendLeave();
@@ -221,8 +205,6 @@ pub fn setSurfaceFocus(self: *Self, wlr_surface: ?*wlr.Surface) void {
     if (new_text_input) |text_input| {
         if (self.input_method != null) {
             text_input.wlr_text_input.sendEnter(wlr_surface.?);
-        } else {
-            text_input.setPendingFocusedSurface(wlr_surface.?);
         }
     }
 }
diff --git a/river/Seat.zig b/river/Seat.zig
index fac49ae..880dc3c 100644
--- a/river/Seat.zig
+++ b/river/Seat.zig
@@ -53,6 +53,16 @@ pub const FocusTarget = union(enum) {
     layer: *LayerSurface,
     lock_surface: *LockSurface,
     none: void,
+
+    pub fn surface(target: FocusTarget) ?*wlr.Surface {
+        return switch (target) {
+            .view => |view| view.rootSurface(),
+            .xwayland_override_redirect => |xwayland_or| xwayland_or.xwayland_surface.surface,
+            .layer => |layer| layer.wlr_layer_surface.surface,
+            .lock_surface => |lock_surface| lock_surface.wlr_lock_surface.surface,
+            .none => null,
+        };
+    }
 };
 
 wlr_seat: *wlr.Seat,
@@ -216,14 +226,7 @@ pub fn setFocusRaw(self: *Self, new_focus: FocusTarget) void {
     // If the target is already focused, do nothing
     if (std.meta.eql(new_focus, self.focused)) return;
 
-    // Obtain the target surface
-    const target_surface = switch (new_focus) {
-        .view => |target_view| target_view.rootSurface(),
-        .xwayland_override_redirect => |target_or| target_or.xwayland_surface.surface,
-        .layer => |target_layer| target_layer.wlr_layer_surface.surface,
-        .lock_surface => |lock_surface| lock_surface.wlr_lock_surface.surface,
-        .none => null,
-    };
+    const target_surface = new_focus.surface();
 
     // First clear the current focus
     switch (self.focused) {
diff --git a/river/TextInput.zig b/river/TextInput.zig
index 2208184..3f7e794 100644
--- a/river/TextInput.zig
+++ b/river/TextInput.zig
@@ -32,10 +32,6 @@ const log = std.log.scoped(.text_input);
 relay: *InputRelay,
 wlr_text_input: *wlr.TextInputV3,
 
-/// Surface stored for when text-input can't receive an enter event immediately
-/// after getting focus. Cleared once text-input receive the enter event.
-pending_focused_surface: ?*wlr.Surface = null,
-
 enable: wl.Listener(*wlr.TextInputV3) =
     wl.Listener(*wlr.TextInputV3).init(handleEnable),
 commit: wl.Listener(*wlr.TextInputV3) =
@@ -45,9 +41,6 @@ disable: wl.Listener(*wlr.TextInputV3) =
 destroy: wl.Listener(*wlr.TextInputV3) =
     wl.Listener(*wlr.TextInputV3).init(handleDestroy),
 
-pending_focused_surface_destroy: wl.Listener(*wlr.Surface) =
-    wl.Listener(*wlr.Surface).init(handlePendingFocusedSurfaceDestroy),
-
 pub fn init(self: *Self, relay: *InputRelay, wlr_text_input: *wlr.TextInputV3) void {
     self.* = .{
         .relay = relay,
@@ -104,8 +97,6 @@ fn handleDestroy(listener: *wl.Listener(*wlr.TextInputV3), _: *wlr.TextInputV3)
 
     if (self.wlr_text_input.current_enabled) self.relay.disableTextInput(self);
 
-    node.data.setPendingFocusedSurface(null);
-
     self.enable.link.remove();
     self.commit.link.remove();
     self.disable.link.remove();
@@ -114,18 +105,3 @@ fn handleDestroy(listener: *wl.Listener(*wlr.TextInputV3), _: *wlr.TextInputV3)
     self.relay.text_inputs.remove(node);
     util.gpa.destroy(node);
 }
-
-fn handlePendingFocusedSurfaceDestroy(listener: *wl.Listener(*wlr.Surface), surface: *wlr.Surface) void {
-    const self = @fieldParentPtr(Self, "pending_focused_surface_destroy", listener);
-    assert(self.pending_focused_surface == surface);
-    self.pending_focused_surface = null;
-    self.pending_focused_surface_destroy.link.remove();
-}
-
-pub fn setPendingFocusedSurface(self: *Self, wlr_surface: ?*wlr.Surface) void {
-    if (self.pending_focused_surface != null) self.pending_focused_surface_destroy.link.remove();
-    self.pending_focused_surface = wlr_surface;
-    if (self.pending_focused_surface) |surface| {
-        surface.events.destroy.add(&self.pending_focused_surface_destroy);
-    }
-}