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_cursor.h");
@cInclude("wlr/types/wlr_data_device.h"); @cInclude("wlr/types/wlr_data_device.h");
@cInclude("wlr/types/wlr_input_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_keyboard.h");
@cInclude("wlr/types/wlr_layer_shell_v1.h"); @cInclude("wlr/types/wlr_layer_shell_v1.h");
@cInclude("wlr/types/wlr_matrix.h"); @cInclude("wlr/types/wlr_matrix.h");

View file

@ -11,14 +11,25 @@ pub const InputManager = struct {
server: *Server, server: *Server,
wlr_input_inhibit_manager: *c.wlr_input_inhibit_manager,
seats: std.TailQueue(Seat), seats: std.TailQueue(Seat),
default_seat: *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, listen_new_input: c.wl_listener,
pub fn init(self: *Self, server: *Server) !void { pub fn init(self: *Self, server: *Server) !void {
self.server = server; 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(); self.seats = std.TailQueue(Seat).init();
const seat_node = try server.allocator.create(std.TailQueue(Seat).Node); 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.default_seat = &seat_node.data;
self.seats.prepend(seat_node); self.seats.prepend(seat_node);
// Set up handler for all new input devices made available. This self.exclusive_client = null;
// includes keyboards, pointers, touch, etc.
// 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; self.listen_new_input.notify = handleNewInput;
c.wl_signal_add(&self.server.wlr_backend.events.new_input, &self.listen_new_input); 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. /// 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 { fn handleNewInput(listener: ?*c.wl_listener, data: ?*c_void) callconv(.C) void {
const input_manager = @fieldParentPtr(InputManager, "listen_new_input", listener.?); 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 (topmost_surface) |to_focus| {
// If we found a surface that requires focus, grab the focus of all // If we found a surface that requires focus, grab the focus of all
// seats. // seats.
seat.focusLayer(to_focus); seat.setFocusRaw(.{ .layer = to_focus });
} else if (seat.focused_layer) |current_focus| { } else if (seat.focused_layer) |current_focus| {
// If the seat is currently focusing a layer without keyboard // If the seat is currently focusing a layer without keyboard
// interactivity, clear the focused layer. // interactivity, clear the focused layer.
if (!current_focus.wlr_layer_surface.current.keyboard_interactive) { 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 { fn renderBorders(output: Output, view: *View, now: *c.struct_timespec) void {
var border: c.wlr_box = undefined; 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 [_]f32{ 0.57647059, 0.63137255, 0.63137255, 1.0 } // Solarized base1
else else
[_]f32{ 0.34509804, 0.43137255, 0.45882353, 1.0 }; // Solarized base01 [_]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 View = @import("view.zig").View;
const ViewStack = @import("view_stack.zig").ViewStack; const ViewStack = @import("view_stack.zig").ViewStack;
const FocusTarget = union(enum) {
view: *View,
layer: *LayerSurface,
none: void,
};
pub const Seat = struct { pub const Seat = struct {
const Self = @This(); const Self = @This();
@ -98,15 +104,6 @@ pub const Seat = struct {
} else null; } 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| { if (view) |view_to_focus| {
// Find or allocate a new node in the focus stack for the target view // Find or allocate a new node in the focus stack for the target view
var it = self.focus_stack.first; var it = self.focus_stack.first;
@ -126,62 +123,78 @@ pub const Seat = struct {
self.focus_stack.push(new_focus_node); self.focus_stack.push(new_focus_node);
} }
// The target view is now at the top of the focus stack, so activate it // Focus the target view
view_to_focus.setActivated(true); self.setFocusRaw(.{ .view = view_to_focus });
self.sendKeyboardEnter(view_to_focus.wlr_xdg_surface.surface); } 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 /// Switch focus to the target, handling unfocus and input inhibition
/// if the argument is null. /// properly. This should only be called directly if dealing with layers.
pub fn focusLayer(self: *Self, layer_surface: ?*LayerSurface) void { pub fn setFocusRaw(self: *Self, focus_target: FocusTarget) void {
if (layer_surface) |layer_to_focus| { // If the target is already focused, do nothing
// If the layer is already focused, don't re-focus it. if (switch (focus_target) {
if (self.focused_layer) |current_focus| { .view => |target_view| target_view == self.focused_view,
if (current_focus == layer_to_focus) { .layer => |target_layer| target_layer == self.focused_layer,
std.debug.assert(self.focused_view == null); .none => false,
return; }) {
} 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| { if (self.focused_view) |current_focus| {
std.debug.assert(self.focused_layer == null);
current_focus.setActivated(false); current_focus.setActivated(false);
self.focused_view = null; self.focused_view = null;
} } else if (self.focused_layer) |current_focus| {
// 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) {
std.debug.assert(self.focused_view == null); std.debug.assert(self.focused_view == null);
self.focused_layer = 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 // Set the new focus
self.focus(null); 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 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,
wlr_surface,
&keyboard.keycodes,
keyboard.num_keycodes,
&keyboard.modifiers,
);
}
} }
} }
/// 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 /// Handle the unmapping of a view, removing it from the focus stack and
/// setting the focus if needed. /// setting the focus if needed.
pub fn handleViewUnmap(self: *Self, view: *View) void { pub fn handleViewUnmap(self: *Self, view: *View) void {