From 5244618b015e40a5de228b85d28b4fce25bed152 Mon Sep 17 00:00:00 2001 From: Isaac Freund Date: Sat, 18 Apr 2020 20:47:51 +0200 Subject: [PATCH] Implement keyboard interactivity for layer shell --- src/output.zig | 41 +++++++++++++++++++++--- src/seat.zig | 84 ++++++++++++++++++++++++++++++++++++++++---------- 2 files changed, 103 insertions(+), 22 deletions(-) diff --git a/src/output.zig b/src/output.zig index 4baf8a8..c8c4e44 100644 --- a/src/output.zig +++ b/src/output.zig @@ -228,27 +228,58 @@ pub const Output = struct { // This box is modified as exclusive zones are applied var usable_box = full_box; - const layers = [_]usize{ + const layer_idxs = [_]usize{ c.ZWLR_LAYER_SHELL_V1_LAYER_OVERLAY, c.ZWLR_LAYER_SHELL_V1_LAYER_TOP, c.ZWLR_LAYER_SHELL_V1_LAYER_BOTTOM, c.ZWLR_LAYER_SHELL_V1_LAYER_BACKGROUND, }; - for (layers) |layer| { + // Arrange all layer surfaces with exclusive zones, applying them to the + // usable box along the way. + for (layer_idxs) |layer| { self.arrangeLayer(self.layers[layer], full_box, &usable_box, true); } - if (self.usable_box.width != usable_box.width or self.usable_box.height != usable_box.height) { + // If the the usable_box has changed, we need to rearrange the output + if (!std.meta.eql(self.usable_box, usable_box)) { self.usable_box = usable_box; self.root.arrange(); } - for (layers) |layer| { + // Arrange the layers without exclusive zones + for (layer_idxs) |layer| { self.arrangeLayer(self.layers[layer], full_box, &usable_box, false); } - // TODO: handle seat focus + // If there is any layer surface in the top or overlay layers which requests + // keyboard interactivity, give it focus. + const topmost_surface = outer: for (layer_idxs[0..2]) |layer| { + // Iterate in reverse order since the last layer is rendered on top + var it = self.layers[layer].last; + while (it) |node| : (it = node.prev) { + const layer_surface = &node.data; + if (layer_surface.wlr_layer_surface.current.keyboard_interactive) { + break :outer layer_surface; + } + } + } else null; + + var it = self.root.server.input_manager.seats.first; + while (it) |node| : (it = node.next) { + const seat = &node.data; + if (topmost_surface) |to_focus| { + // If we found a surface that requires focus, grab the focus of all + // seats. + seat.focusLayer(to_focus); + } else if (seat.focused_layer) |current_focus| { + // If the seat is currently focusing a layer without keyboard + // interactivity, clear the focused layer. + if (!current_focus.wlr_layer_surface.current.keyboard_interactive) { + seat.focusLayer(null); + } + } + } } /// Arrange the layer surfaces of a given layer diff --git a/src/seat.zig b/src/seat.zig index 179b93f..7407474 100644 --- a/src/seat.zig +++ b/src/seat.zig @@ -1,9 +1,10 @@ -const std = @import("std"); const c = @import("c.zig"); +const std = @import("std"); const Cursor = @import("cursor.zig").Cursor; const InputManager = @import("input_manager.zig").InputManager; const Keyboard = @import("keyboard.zig").Keyboard; +const LayerSurface = @import("layer_surface.zig").LayerSurface; const Output = @import("output.zig").Output; const View = @import("view.zig").View; const ViewStack = @import("view_stack.zig").ViewStack; @@ -30,6 +31,10 @@ pub const Seat = struct { /// If there is a currently focused view, it is on top. focus_stack: ViewStack(*View), + /// Currently focused layer, if any. While this is non-null, no views may + /// recieve focus. + focused_layer: ?*LayerSurface, + pub fn init(self: *Self, input_manager: *InputManager, name: []const u8) !void { self.input_manager = input_manager; @@ -47,6 +52,8 @@ pub const Seat = struct { self.focused_view = null; self.focus_stack.init(); + + self.focused_layer = null; } pub fn deinit(self: *Self) void { @@ -67,6 +74,12 @@ pub const Seat = struct { pub fn focus(self: *Self, _view: ?*View) void { var view = _view; + // While a layer surface is focused, views may not recieve focus + if (self.focused_layer != null) { + std.debug.assert(self.focused_view == null); + return; + } + // If view is null or not currently visible if (if (view) |v| v.output != self.focused_output or @@ -94,12 +107,12 @@ pub const Seat = struct { current_focus.setActivated(false); } - if (view) |to_focus| { + if (view) |view_to_focus| { // Find or allocate a new node in the focus stack for the target view var it = self.focus_stack.first; while (it) |node| : (it = node.next) { // If the view is found, move it to the top of the stack - if (node.view == to_focus) { + if (node.view == view_to_focus) { const new_focus_node = self.focus_stack.remove(node); self.focus_stack.push(node); break; @@ -109,29 +122,66 @@ pub const Seat = struct { const new_focus_node = self.input_manager.server.allocator.create( ViewStack(*View).Node, ) catch unreachable; - new_focus_node.view = to_focus; + new_focus_node.view = view_to_focus; self.focus_stack.push(new_focus_node); } // The target view is now at the top of the focus stack, so activate it - to_focus.setActivated(true); - - // Tell the seat to have the keyboard enter this surface. wlroots will keep - // track of this and automatically send key events to the appropriate - // clients without additional work on your part. - const keyboard: *c.wlr_keyboard = c.wlr_seat_get_keyboard(self.wlr_seat); - c.wlr_seat_keyboard_notify_enter( - self.wlr_seat, - to_focus.wlr_xdg_surface.surface, - &keyboard.keycodes, - keyboard.num_keycodes, - &keyboard.modifiers, - ); + view_to_focus.setActivated(true); + self.sendKeyboardEnter(view_to_focus.wlr_xdg_surface.surface); } self.focused_view = view; } + /// Set the focus to the passed layer surface, or clear the focused layer + /// if the argument is null. + pub fn focusLayer(self: *Self, layer_surface: ?*LayerSurface) void { + if (layer_surface) |layer_to_focus| { + // If the layer is already focused, don't re-focus it. + if (self.focused_layer) |current_focus| { + if (current_focus == layer_to_focus) { + std.debug.assert(self.focused_view == null); + return; + } + } + + // If a view is currently focused, unfocus it + if (self.focused_view) |current_focus| { + current_focus.setActivated(false); + self.focused_view = null; + } + + // Focus the layer surface + self.sendKeyboardEnter(layer_to_focus.wlr_layer_surface.surface); + self.focused_layer = layer_to_focus; + self.focused_output = layer_to_focus.output; + } else { + // If there is a layer currently focused, unfocus it + if (self.focused_layer != null) { + std.debug.assert(self.focused_view == null); + self.focused_layer = null; + } + + // Then focus the view on the top of the focus stack if any + self.focus(null); + } + } + + /// Tell the seat to have the keyboard enter this surface. wlroots will keep + /// track of this and automatically send key events to the appropriate + /// clients. + fn sendKeyboardEnter(self: Self, wlr_surface: *c.wlr_surface) void { + const keyboard: *c.wlr_keyboard = c.wlr_seat_get_keyboard(self.wlr_seat); + c.wlr_seat_keyboard_notify_enter( + self.wlr_seat, + wlr_surface, + &keyboard.keycodes, + keyboard.num_keycodes, + &keyboard.modifiers, + ); + } + /// Handle the unmapping of a view, removing it from the focus stack and /// setting the focus if needed. pub fn handleViewUnmap(self: *Self, view: *View) void {