2020-03-29 10:36:15 -07:00
|
|
|
const c = @import("c.zig");
|
2020-04-18 11:47:51 -07:00
|
|
|
const std = @import("std");
|
2020-03-22 14:42:55 -07:00
|
|
|
|
2020-03-23 08:50:20 -07:00
|
|
|
const Cursor = @import("cursor.zig").Cursor;
|
2020-04-12 13:19:48 -07:00
|
|
|
const InputManager = @import("input_manager.zig").InputManager;
|
2020-03-23 08:50:20 -07:00
|
|
|
const Keyboard = @import("keyboard.zig").Keyboard;
|
2020-04-18 11:47:51 -07:00
|
|
|
const LayerSurface = @import("layer_surface.zig").LayerSurface;
|
2020-04-15 08:59:46 -07:00
|
|
|
const Output = @import("output.zig").Output;
|
2020-04-13 12:00:18 -07:00
|
|
|
const View = @import("view.zig").View;
|
|
|
|
const ViewStack = @import("view_stack.zig").ViewStack;
|
2020-03-23 08:50:20 -07:00
|
|
|
|
2020-04-18 15:59:07 -07:00
|
|
|
const FocusTarget = union(enum) {
|
|
|
|
view: *View,
|
|
|
|
layer: *LayerSurface,
|
|
|
|
none: void,
|
|
|
|
};
|
|
|
|
|
2020-03-22 14:42:55 -07:00
|
|
|
pub const Seat = struct {
|
2020-03-24 12:48:38 -07:00
|
|
|
const Self = @This();
|
|
|
|
|
2020-04-12 13:19:48 -07:00
|
|
|
input_manager: *InputManager,
|
2020-03-22 14:42:55 -07:00
|
|
|
wlr_seat: *c.wlr_seat,
|
|
|
|
|
2020-04-12 13:19:48 -07:00
|
|
|
/// Multiple mice are handled by the same Cursor
|
2020-03-22 14:42:55 -07:00
|
|
|
cursor: Cursor,
|
2020-04-12 13:19:48 -07:00
|
|
|
|
|
|
|
/// Mulitple keyboards are handled separately
|
2020-03-23 13:51:46 -07:00
|
|
|
keyboards: std.TailQueue(Keyboard),
|
2020-03-22 14:42:55 -07:00
|
|
|
|
2020-04-15 08:59:46 -07:00
|
|
|
/// Currently focused output, may be the noop output if no
|
|
|
|
focused_output: *Output,
|
|
|
|
|
2020-04-13 12:00:18 -07:00
|
|
|
/// Currently focused view if any
|
|
|
|
focused_view: ?*View,
|
|
|
|
|
|
|
|
/// Stack of views in most recently focused order
|
|
|
|
/// If there is a currently focused view, it is on top.
|
|
|
|
focus_stack: ViewStack(*View),
|
|
|
|
|
2020-04-18 11:47:51 -07:00
|
|
|
/// Currently focused layer, if any. While this is non-null, no views may
|
|
|
|
/// recieve focus.
|
|
|
|
focused_layer: ?*LayerSurface,
|
|
|
|
|
2020-04-12 13:19:48 -07:00
|
|
|
pub fn init(self: *Self, input_manager: *InputManager, name: []const u8) !void {
|
|
|
|
self.input_manager = input_manager;
|
2020-03-22 14:42:55 -07:00
|
|
|
|
2020-03-25 08:24:21 -07:00
|
|
|
// This will be automatically destroyed when the display is destroyed
|
2020-04-12 13:19:48 -07:00
|
|
|
self.wlr_seat = c.wlr_seat_create(input_manager.server.wl_display, name.ptr) orelse
|
2020-03-23 13:51:46 -07:00
|
|
|
return error.CantCreateWlrSeat;
|
|
|
|
|
2020-03-25 08:24:21 -07:00
|
|
|
try self.cursor.init(self);
|
|
|
|
errdefer self.cursor.destroy();
|
2020-03-23 13:51:46 -07:00
|
|
|
|
2020-03-25 08:24:21 -07:00
|
|
|
self.keyboards = std.TailQueue(Keyboard).init();
|
2020-04-13 12:00:18 -07:00
|
|
|
|
2020-04-15 08:59:46 -07:00
|
|
|
self.focused_output = &self.input_manager.server.root.noop_output;
|
|
|
|
|
2020-04-13 12:00:18 -07:00
|
|
|
self.focused_view = null;
|
|
|
|
|
|
|
|
self.focus_stack.init();
|
2020-04-18 11:47:51 -07:00
|
|
|
|
|
|
|
self.focused_layer = null;
|
2020-03-22 14:42:55 -07:00
|
|
|
}
|
|
|
|
|
2020-04-18 03:21:43 -07:00
|
|
|
pub fn deinit(self: *Self) void {
|
|
|
|
self.cursor.deinit();
|
|
|
|
|
|
|
|
while (self.keyboards.pop()) |node| {
|
|
|
|
self.input_manager.server.allocator.destroy(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
while (self.focus_stack.first) |node| {
|
|
|
|
self.focus_stack.remove(node);
|
|
|
|
self.input_manager.server.allocator.destroy(node);
|
|
|
|
}
|
2020-03-25 08:24:21 -07:00
|
|
|
}
|
|
|
|
|
2020-04-13 12:00:18 -07:00
|
|
|
/// Set the current focus. If a visible view is passed it will be focused.
|
|
|
|
/// If null is passed, the first visible view in the focus stack will be focused.
|
|
|
|
pub fn focus(self: *Self, _view: ?*View) void {
|
|
|
|
var view = _view;
|
|
|
|
|
2020-04-18 11:47:51 -07:00
|
|
|
// While a layer surface is focused, views may not recieve focus
|
|
|
|
if (self.focused_layer != null) {
|
|
|
|
std.debug.assert(self.focused_view == null);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-04-13 12:00:18 -07:00
|
|
|
// If view is null or not currently visible
|
2020-04-15 08:59:46 -07:00
|
|
|
if (if (view) |v|
|
|
|
|
v.output != self.focused_output or
|
|
|
|
v.current_tags & self.focused_output.current_focused_tags == 0
|
|
|
|
else
|
|
|
|
true) {
|
2020-04-15 11:02:55 -07:00
|
|
|
// Set view to the first currently visible view on in the focus stack if any
|
|
|
|
var it = ViewStack(*View).iterator(
|
2020-04-13 12:00:18 -07:00
|
|
|
self.focus_stack.first,
|
2020-04-15 08:59:46 -07:00
|
|
|
self.focused_output.current_focused_tags,
|
2020-04-15 11:02:55 -07:00
|
|
|
);
|
|
|
|
view = while (it.next()) |node| {
|
|
|
|
if (node.view.output == self.focused_output) {
|
|
|
|
break node.view;
|
|
|
|
}
|
|
|
|
} else null;
|
2020-04-13 12:00:18 -07:00
|
|
|
}
|
|
|
|
|
2020-04-18 11:47:51 -07:00
|
|
|
if (view) |view_to_focus| {
|
2020-04-13 12:00:18 -07:00
|
|
|
// 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
|
2020-04-18 11:47:51 -07:00
|
|
|
if (node.view == view_to_focus) {
|
2020-04-13 12:00:18 -07:00
|
|
|
const new_focus_node = self.focus_stack.remove(node);
|
|
|
|
self.focus_stack.push(node);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// The view is not in the stack, so allocate a new node and prepend it
|
|
|
|
const new_focus_node = self.input_manager.server.allocator.create(
|
|
|
|
ViewStack(*View).Node,
|
|
|
|
) catch unreachable;
|
2020-04-18 11:47:51 -07:00
|
|
|
new_focus_node.view = view_to_focus;
|
2020-04-13 12:00:18 -07:00
|
|
|
self.focus_stack.push(new_focus_node);
|
|
|
|
}
|
|
|
|
|
2020-04-18 15:59:07 -07:00
|
|
|
// Focus the target view
|
|
|
|
self.setFocusRaw(.{ .view = view_to_focus });
|
|
|
|
} else {
|
|
|
|
// Otherwise clear the focus
|
|
|
|
self.setFocusRaw(.{ .none = {} });
|
2020-04-13 12:00:18 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-18 15:59:07 -07:00
|
|
|
/// 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 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
|
2020-04-18 11:47:51 -07:00
|
|
|
if (self.focused_view) |current_focus| {
|
2020-04-18 15:59:07 -07:00
|
|
|
std.debug.assert(self.focused_layer == null);
|
2020-04-18 11:47:51 -07:00
|
|
|
current_focus.setActivated(false);
|
|
|
|
self.focused_view = null;
|
2020-04-18 15:59:07 -07:00
|
|
|
} else if (self.focused_layer) |current_focus| {
|
2020-04-18 11:47:51 -07:00
|
|
|
std.debug.assert(self.focused_view == null);
|
|
|
|
self.focused_layer = null;
|
|
|
|
}
|
2020-04-18 15:59:07 -07:00
|
|
|
c.wlr_seat_keyboard_clear_focus(self.wlr_seat);
|
|
|
|
|
|
|
|
// 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 => {},
|
|
|
|
}
|
2020-04-18 11:47:51 -07:00
|
|
|
|
2020-04-18 15:59:07 -07:00
|
|
|
// 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,
|
|
|
|
);
|
|
|
|
}
|
2020-04-18 11:47:51 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-13 12:00:18 -07:00
|
|
|
/// 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 {
|
|
|
|
// Remove the node from the focus stack and destroy it.
|
|
|
|
var it = self.focus_stack.first;
|
|
|
|
while (it) |node| : (it = node.next) {
|
|
|
|
if (node.view == view) {
|
|
|
|
self.focus_stack.remove(node);
|
|
|
|
self.input_manager.server.allocator.destroy(node);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the unmapped view is focused, choose a new focus
|
|
|
|
if (self.focused_view) |current_focus| {
|
|
|
|
if (current_focus == view) {
|
|
|
|
self.focus(null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-07 12:48:56 -07:00
|
|
|
/// Handle any user-defined keybinding for the passed keysym and modifiers
|
|
|
|
/// Returns true if the key was handled
|
2020-04-13 10:25:37 -07:00
|
|
|
pub fn handleKeybinding(self: *Self, keysym: c.xkb_keysym_t, modifiers: u32) bool {
|
2020-04-12 13:19:48 -07:00
|
|
|
for (self.input_manager.server.config.keybinds.items) |keybind| {
|
2020-04-07 12:48:56 -07:00
|
|
|
if (modifiers == keybind.modifiers and keysym == keybind.keysym) {
|
|
|
|
// Execute the bound command
|
2020-04-13 10:25:37 -07:00
|
|
|
keybind.command(self, keybind.arg);
|
2020-04-07 12:48:56 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-04-12 13:19:48 -07:00
|
|
|
/// Add a newly created input device to the seat and update the reported
|
|
|
|
/// capabilities.
|
|
|
|
pub fn addDevice(self: *Self, device: *c.wlr_input_device) !void {
|
|
|
|
switch (device.type) {
|
|
|
|
.WLR_INPUT_DEVICE_KEYBOARD => self.addKeyboard(device) catch unreachable,
|
|
|
|
.WLR_INPUT_DEVICE_POINTER => self.addPointer(device),
|
|
|
|
else => {},
|
|
|
|
}
|
|
|
|
|
|
|
|
// We need to let the wlr_seat know what our capabilities are, which is
|
|
|
|
// communiciated to the client. We always have a cursor, even if
|
|
|
|
// there are no pointer devices, so we always include that capability.
|
|
|
|
var caps: u32 = @intCast(u32, c.WL_SEAT_CAPABILITY_POINTER);
|
|
|
|
// if list not empty
|
|
|
|
if (self.keyboards.len > 0) {
|
|
|
|
caps |= @intCast(u32, c.WL_SEAT_CAPABILITY_KEYBOARD);
|
|
|
|
}
|
|
|
|
c.wlr_seat_set_capabilities(self.wlr_seat, caps);
|
|
|
|
}
|
|
|
|
|
2020-03-24 13:13:56 -07:00
|
|
|
fn addKeyboard(self: *Self, device: *c.wlr_input_device) !void {
|
2020-03-23 08:50:20 -07:00
|
|
|
c.wlr_seat_set_keyboard(self.wlr_seat, device);
|
2020-03-23 13:51:46 -07:00
|
|
|
|
2020-04-12 13:19:48 -07:00
|
|
|
const node = try self.keyboards.allocateNode(self.input_manager.server.allocator);
|
2020-03-23 13:51:46 -07:00
|
|
|
try node.data.init(self, device);
|
|
|
|
self.keyboards.append(node);
|
2020-03-22 14:42:55 -07:00
|
|
|
}
|
|
|
|
|
2020-03-29 06:58:04 -07:00
|
|
|
fn addPointer(self: Self, device: *c.struct_wlr_input_device) void {
|
2020-03-22 14:42:55 -07:00
|
|
|
// We don't do anything special with pointers. All of our pointer handling
|
|
|
|
// is proxied through wlr_cursor. On another compositor, you might take this
|
|
|
|
// opportunity to do libinput configuration on the device to set
|
|
|
|
// acceleration, etc.
|
|
|
|
c.wlr_cursor_attach_input_device(self.cursor.wlr_cursor, device);
|
|
|
|
}
|
|
|
|
};
|