Implement wlr-input-inhibitor

This commit is contained in:
Isaac Freund 2020-04-19 00:59:07 +02:00
parent cd9d4ee395
commit c959a426b7
No known key found for this signature in database
GPG key ID: 86DED400DDFD7A11
5 changed files with 135 additions and 56 deletions

View file

@ -10,6 +10,7 @@ pub usingnamespace @cImport({
@cInclude("wlr/types/wlr_cursor.h");
@cInclude("wlr/types/wlr_data_device.h");
@cInclude("wlr/types/wlr_input_device.h");
@cInclude("wlr/types/wlr_input_inhibitor.h");
@cInclude("wlr/types/wlr_keyboard.h");
@cInclude("wlr/types/wlr_layer_shell_v1.h");
@cInclude("wlr/types/wlr_matrix.h");

View file

@ -11,14 +11,25 @@ pub const InputManager = struct {
server: *Server,
wlr_input_inhibit_manager: *c.wlr_input_inhibit_manager,
seats: std.TailQueue(Seat),
default_seat: *Seat,
exclusive_client: ?*c.wl_client,
listen_inhibit_activate: c.wl_listener,
listen_inhibit_deactivate: c.wl_listener,
listen_new_input: c.wl_listener,
pub fn init(self: *Self, server: *Server) !void {
self.server = server;
// This is automatically freed when the display is destroyed
self.wlr_input_inhibit_manager =
c.wlr_input_inhibit_manager_create(server.wl_display) orelse
return error.CantCreateInputInhibitManager;
self.seats = std.TailQueue(Seat).init();
const seat_node = try server.allocator.create(std.TailQueue(Seat).Node);
@ -26,8 +37,21 @@ pub const InputManager = struct {
self.default_seat = &seat_node.data;
self.seats.prepend(seat_node);
// Set up handler for all new input devices made available. This
// includes keyboards, pointers, touch, etc.
self.exclusive_client = null;
// Set up all listeners
self.listen_inhibit_activate.notify = handleInhibitActivate;
c.wl_signal_add(
&self.wlr_input_inhibit_manager.events.activate,
&self.listen_inhibit_activate,
);
self.listen_inhibit_deactivate.notify = handleInhibitDeactivate;
c.wl_signal_add(
&self.wlr_input_inhibit_manager.events.deactivate,
&self.listen_inhibit_deactivate,
);
self.listen_new_input.notify = handleNewInput;
c.wl_signal_add(&self.server.wlr_backend.events.new_input, &self.listen_new_input);
}
@ -48,6 +72,46 @@ pub const InputManager = struct {
}
}
/// Returns true if input is currently allowed on the passed surface.
pub fn inputAllowed(self: Self, wlr_surface: *c.wlr_surface) bool {
return if (self.exclusive_client) |exclusive_client|
exclusive_client == c.wl_resource_get_client(wlr_surface.resource)
else
true;
}
fn handleInhibitActivate(listener: ?*c.wl_listener, data: ?*c_void) callconv(.C) void {
const self = @fieldParentPtr(Self, "listen_inhibit_activate", listener.?);
// Clear focus of all seats
var seat_it = self.seats.first;
while (seat_it) |seat_node| : (seat_it = seat_node.next) {
seat_node.data.setFocusRaw(.{ .none = {} });
}
self.exclusive_client = self.wlr_input_inhibit_manager.active_client;
}
fn handleInhibitDeactivate(listener: ?*c.wl_listener, data: ?*c_void) callconv(.C) void {
const self = @fieldParentPtr(Self, "listen_inhibit_deactivate", listener.?);
self.exclusive_client = null;
// Calling arrangeLayers() like this ensures that any top or overlay,
// keyboard-interactive surfaces will re-grab focus.
var output_it = self.server.root.outputs.first;
while (output_it) |output_node| : (output_it = output_node.next) {
output_node.data.arrangeLayers();
}
// After ensuring that any possible layer surface focus grab has occured,
// have each Seat handle focus.
var seat_it = self.seats.first;
while (seat_it) |seat_node| : (seat_it = seat_node.next) {
seat_node.data.focus(null);
}
}
/// This event is raised by the backend when a new input device becomes available.
fn handleNewInput(listener: ?*c.wl_listener, data: ?*c_void) callconv(.C) void {
const input_manager = @fieldParentPtr(InputManager, "listen_new_input", listener.?);

View file

@ -277,12 +277,13 @@ pub const Output = struct {
if (topmost_surface) |to_focus| {
// If we found a surface that requires focus, grab the focus of all
// seats.
seat.focusLayer(to_focus);
seat.setFocusRaw(.{ .layer = 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);
seat.setFocusRaw(.{ .none = {} });
seat.focus(null);
}
}
}

View file

@ -242,7 +242,7 @@ fn renderSurface(_surface: ?*c.wlr_surface, sx: c_int, sy: c_int, data: ?*c_void
fn renderBorders(output: Output, view: *View, now: *c.struct_timespec) void {
var border: c.wlr_box = undefined;
const color = if (view.wlr_xdg_surface.unnamed_163.toplevel.*.current.activated)
const color = if (view.wlr_xdg_surface.unnamed_164.toplevel.*.current.activated)
[_]f32{ 0.57647059, 0.63137255, 0.63137255, 1.0 } // Solarized base1
else
[_]f32{ 0.34509804, 0.43137255, 0.45882353, 1.0 }; // Solarized base01

View file

@ -9,6 +9,12 @@ const Output = @import("output.zig").Output;
const View = @import("view.zig").View;
const ViewStack = @import("view_stack.zig").ViewStack;
const FocusTarget = union(enum) {
view: *View,
layer: *LayerSurface,
none: void,
};
pub const Seat = struct {
const Self = @This();
@ -98,15 +104,6 @@ pub const Seat = struct {
} else null;
}
if (self.focused_view) |current_focus| {
// Don't refocus the currently focused view
if (if (view) |v| current_focus == v else false) {
return;
}
// Deactivate the currently focused view
current_focus.setActivated(false);
}
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;
@ -126,52 +123,66 @@ pub const Seat = struct {
self.focus_stack.push(new_focus_node);
}
// The target view is now at the top of the focus stack, so activate it
view_to_focus.setActivated(true);
self.sendKeyboardEnter(view_to_focus.wlr_xdg_surface.surface);
// Focus the target view
self.setFocusRaw(.{ .view = view_to_focus });
} else {
// Otherwise clear the focus
self.setFocusRaw(.{ .none = {} });
}
}
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);
/// Switch focus to the target, handling unfocus and input inhibition
/// properly. This should only be called directly if dealing with layers.
pub fn setFocusRaw(self: *Self, focus_target: FocusTarget) void {
// If the target is already focused, do nothing
if (switch (focus_target) {
.view => |target_view| target_view == self.focused_view,
.layer => |target_layer| target_layer == self.focused_layer,
.none => false,
}) {
return;
}
}
// Obtain the target wlr_surface
const target_wlr_surface = switch (focus_target) {
.view => |target_view| target_view.wlr_xdg_surface.surface,
.layer => |target_layer| target_layer.wlr_layer_surface.surface,
.none => null,
};
// If a view is currently focused, unfocus it
// If input is not allowed on the target surface (e.g. due to an active
// input inhibitor) do not set focus. If there is no target surface we
// still clear the focus.
if (if (target_wlr_surface) |wlr_surface|
self.input_manager.inputAllowed(wlr_surface)
else
true) {
// First clear the current focus
if (self.focused_view) |current_focus| {
std.debug.assert(self.focused_layer == null);
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;
std.debug.assert(self.focused_output == layer_to_focus.output);
} else {
// If there is a layer currently focused, unfocus it
if (self.focused_layer != null) {
} else if (self.focused_layer) |current_focus| {
std.debug.assert(self.focused_view == null);
self.focused_layer = null;
}
c.wlr_seat_keyboard_clear_focus(self.wlr_seat);
// Then focus the view on the top of the focus stack if any
self.focus(null);
}
// Set the new focus
switch (focus_target) {
.view => |target_view| {
std.debug.assert(self.focused_output == target_view.output);
target_view.setActivated(true);
self.focused_view = target_view;
},
.layer => |target_layer| blk: {
std.debug.assert(self.focused_output == target_layer.output);
self.focused_layer = target_layer;
},
.none => {},
}
/// 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 {
// Tell wlroots to send the new keyboard focus if we have a target
if (target_wlr_surface) |wlr_surface| {
const keyboard: *c.wlr_keyboard = c.wlr_seat_get_keyboard(self.wlr_seat);
c.wlr_seat_keyboard_notify_enter(
self.wlr_seat,
@ -181,6 +192,8 @@ pub const Seat = struct {
&keyboard.modifiers,
);
}
}
}
/// Handle the unmapping of a view, removing it from the focus stack and
/// setting the focus if needed.