2020-05-02 10:21:10 -07:00
|
|
|
// This file is part of river, a dynamic tiling wayland compositor.
|
|
|
|
//
|
|
|
|
// Copyright 2020 Isaac Freund
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
2020-05-02 07:35:15 -07:00
|
|
|
const Self = @This();
|
|
|
|
|
2020-04-18 11:47:51 -07:00
|
|
|
const std = @import("std");
|
2020-03-22 14:42:55 -07:00
|
|
|
|
2020-05-02 10:21:10 -07:00
|
|
|
const c = @import("c.zig");
|
2020-05-26 13:55:07 -07:00
|
|
|
const command = @import("command.zig");
|
2020-06-26 09:43:20 -07:00
|
|
|
const log = @import("log.zig");
|
2020-06-16 11:54:05 -07:00
|
|
|
const util = @import("util.zig");
|
2020-05-02 10:21:10 -07:00
|
|
|
|
2020-05-02 14:11:56 -07:00
|
|
|
const Cursor = @import("Cursor.zig");
|
|
|
|
const InputManager = @import("InputManager.zig");
|
|
|
|
const Keyboard = @import("Keyboard.zig");
|
|
|
|
const LayerSurface = @import("LayerSurface.zig");
|
|
|
|
const Output = @import("Output.zig");
|
2020-06-04 07:56:58 -07:00
|
|
|
const SeatStatus = @import("SeatStatus.zig");
|
2020-05-02 14:11:56 -07:00
|
|
|
const View = @import("View.zig");
|
2020-04-13 12:00:18 -07:00
|
|
|
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-05-02 07:35:15 -07:00
|
|
|
input_manager: *InputManager,
|
|
|
|
wlr_seat: *c.wlr_seat,
|
2020-04-12 13:19:48 -07:00
|
|
|
|
2020-05-02 07:35:15 -07:00
|
|
|
/// Multiple mice are handled by the same Cursor
|
|
|
|
cursor: Cursor,
|
2020-03-22 14:42:55 -07:00
|
|
|
|
2020-05-02 07:35:15 -07:00
|
|
|
/// Mulitple keyboards are handled separately
|
|
|
|
keyboards: std.TailQueue(Keyboard),
|
2020-04-15 08:59:46 -07:00
|
|
|
|
2020-06-01 06:16:18 -07:00
|
|
|
/// ID of the current keymap mode
|
2020-05-31 15:20:49 -07:00
|
|
|
mode_id: usize,
|
2020-05-16 15:03:26 -07:00
|
|
|
|
2020-05-02 07:35:15 -07:00
|
|
|
/// Currently focused output, may be the noop output if no
|
|
|
|
focused_output: *Output,
|
2020-04-13 12:00:18 -07:00
|
|
|
|
2020-07-15 05:15:17 -07:00
|
|
|
/// Currently focused view/layer surface if any
|
|
|
|
focused: FocusTarget,
|
2020-04-13 12:00:18 -07:00
|
|
|
|
2020-05-02 07:35:15 -07:00
|
|
|
/// 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
|
|
|
|
2020-06-04 07:56:58 -07:00
|
|
|
/// List of status tracking objects relaying changes to this seat to clients.
|
|
|
|
status_trackers: std.SinglyLinkedList(SeatStatus),
|
|
|
|
|
2020-07-07 07:39:08 -07:00
|
|
|
/// State of pointer modifier; Used for pointer operations such as move ans resize.
|
|
|
|
pointer_modifier: bool,
|
|
|
|
|
2020-05-02 07:35:15 -07:00
|
|
|
listen_request_set_selection: c.wl_listener,
|
2020-03-22 14:42:55 -07:00
|
|
|
|
2020-06-25 16:13:00 -07:00
|
|
|
pub fn init(self: *Self, input_manager: *InputManager, name: [*:0]const u8) !void {
|
2020-05-02 07:35:15 -07:00
|
|
|
self.input_manager = input_manager;
|
2020-03-23 13:51:46 -07:00
|
|
|
|
2020-05-02 07:35:15 -07:00
|
|
|
// This will be automatically destroyed when the display is destroyed
|
2020-06-25 16:13:00 -07:00
|
|
|
self.wlr_seat = c.wlr_seat_create(input_manager.server.wl_display, name) orelse return error.OutOfMemory;
|
2020-06-04 07:56:58 -07:00
|
|
|
self.wlr_seat.data = self;
|
2020-03-23 13:51:46 -07:00
|
|
|
|
2020-05-02 07:35:15 -07:00
|
|
|
try self.cursor.init(self);
|
2020-06-25 15:59:31 -07:00
|
|
|
errdefer self.cursor.deinit();
|
2020-04-13 12:00:18 -07:00
|
|
|
|
2020-05-02 07:35:15 -07:00
|
|
|
self.keyboards = std.TailQueue(Keyboard).init();
|
2020-04-15 08:59:46 -07:00
|
|
|
|
2020-05-31 14:56:25 -07:00
|
|
|
self.mode_id = 0;
|
2020-05-16 15:03:26 -07:00
|
|
|
|
2020-05-02 07:35:15 -07:00
|
|
|
self.focused_output = &self.input_manager.server.root.noop_output;
|
2020-04-13 12:00:18 -07:00
|
|
|
|
2020-07-15 05:15:17 -07:00
|
|
|
self.focused = .none;
|
2020-04-18 11:47:51 -07:00
|
|
|
|
2020-05-02 07:35:15 -07:00
|
|
|
self.focus_stack.init();
|
2020-04-24 16:04:12 -07:00
|
|
|
|
2020-06-04 07:56:58 -07:00
|
|
|
self.status_trackers = std.SinglyLinkedList(SeatStatus).init();
|
|
|
|
|
2020-07-07 07:39:08 -07:00
|
|
|
self.pointer_modifier = false;
|
|
|
|
|
2020-05-02 07:35:15 -07:00
|
|
|
self.listen_request_set_selection.notify = handleRequestSetSelection;
|
|
|
|
c.wl_signal_add(&self.wlr_seat.events.request_set_selection, &self.listen_request_set_selection);
|
|
|
|
}
|
2020-04-18 03:21:43 -07:00
|
|
|
|
2020-05-02 07:35:15 -07:00
|
|
|
pub fn deinit(self: *Self) void {
|
|
|
|
self.cursor.deinit();
|
2020-04-18 03:21:43 -07:00
|
|
|
|
2020-06-19 05:31:53 -07:00
|
|
|
while (self.keyboards.pop()) |node| util.gpa.destroy(node);
|
2020-03-25 08:24:21 -07:00
|
|
|
|
2020-05-02 07:35:15 -07:00
|
|
|
while (self.focus_stack.first) |node| {
|
|
|
|
self.focus_stack.remove(node);
|
2020-06-19 05:31:53 -07:00
|
|
|
util.gpa.destroy(node);
|
2020-04-13 12:00:18 -07:00
|
|
|
}
|
2020-05-02 07:35:15 -07:00
|
|
|
}
|
2020-04-13 12:00:18 -07:00
|
|
|
|
2020-05-02 07:35:15 -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-25 12:40:26 -07:00
|
|
|
|
2020-05-02 07:35:15 -07:00
|
|
|
// While a layer surface is focused, views may not recieve focus
|
2020-07-15 05:15:17 -07:00
|
|
|
if (self.focused == .layer) return;
|
2020-04-18 11:47:51 -07:00
|
|
|
|
2020-06-28 16:00:05 -07:00
|
|
|
// If the view is not currently visible, behave as if null was passed
|
|
|
|
if (view) |v| {
|
|
|
|
if (v.output != self.focused_output or
|
2020-07-02 12:55:21 -07:00
|
|
|
v.current.tags & self.focused_output.current.tags == 0) view = null;
|
2020-06-28 16:00:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// If the target view is not fullscreen or null, then a fullscreen view
|
|
|
|
// will grab focus if visible.
|
|
|
|
if (if (view) |v| !v.current.fullscreen else true) {
|
2020-07-02 12:55:21 -07:00
|
|
|
var it = ViewStack(*View).iterator(self.focus_stack.first, self.focused_output.current.tags);
|
2020-05-02 07:35:15 -07:00
|
|
|
view = while (it.next()) |node| {
|
2020-06-28 16:00:05 -07:00
|
|
|
if (node.view.output == self.focused_output and node.view.current.fullscreen) break node.view;
|
|
|
|
} else view;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (view == null) {
|
|
|
|
// Set view to the first currently visible view in the focus stack if any
|
2020-07-02 12:55:21 -07:00
|
|
|
var it = ViewStack(*View).iterator(self.focus_stack.first, self.focused_output.current.tags);
|
2020-06-28 16:00:05 -07:00
|
|
|
view = while (it.next()) |node| {
|
|
|
|
if (node.view.output == self.focused_output) break node.view;
|
2020-05-02 07:35:15 -07:00
|
|
|
} else null;
|
2020-04-18 11:47:51 -07:00
|
|
|
}
|
|
|
|
|
2020-05-02 07:35:15 -07:00
|
|
|
if (view) |view_to_focus| {
|
|
|
|
// Find or allocate a new node in the focus stack for the target view
|
2020-04-13 12:00:18 -07:00
|
|
|
var it = self.focus_stack.first;
|
|
|
|
while (it) |node| : (it = node.next) {
|
2020-05-02 07:35:15 -07:00
|
|
|
// If the view is found, move it to the top of the stack
|
|
|
|
if (node.view == view_to_focus) {
|
|
|
|
const new_focus_node = self.focus_stack.remove(node);
|
|
|
|
self.focus_stack.push(node);
|
2020-04-13 12:00:18 -07:00
|
|
|
break;
|
|
|
|
}
|
2020-05-02 07:35:15 -07:00
|
|
|
} else {
|
|
|
|
// The view is not in the stack, so allocate a new node and prepend it
|
2020-07-05 13:49:17 -07:00
|
|
|
const new_focus_node = util.gpa.create(ViewStack(*View).Node) catch return;
|
2020-05-02 07:35:15 -07:00
|
|
|
new_focus_node.view = view_to_focus;
|
|
|
|
self.focus_stack.push(new_focus_node);
|
2020-04-13 12:00:18 -07:00
|
|
|
}
|
|
|
|
|
2020-05-02 07:35:15 -07:00
|
|
|
// Focus the target view
|
|
|
|
self.setFocusRaw(.{ .view = view_to_focus });
|
|
|
|
} else {
|
|
|
|
// Otherwise clear the focus
|
|
|
|
self.setFocusRaw(.{ .none = {} });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Switch focus to the target, handling unfocus and input inhibition
|
|
|
|
/// properly. This should only be called directly if dealing with layers.
|
2020-07-15 05:15:17 -07:00
|
|
|
pub fn setFocusRaw(self: *Self, new_focus: FocusTarget) void {
|
2020-05-02 07:35:15 -07:00
|
|
|
// If the target is already focused, do nothing
|
2020-07-15 05:15:17 -07:00
|
|
|
if (std.meta.eql(new_focus, self.focused)) return;
|
2020-05-02 07:35:15 -07:00
|
|
|
|
|
|
|
// Obtain the target wlr_surface
|
2020-07-15 05:15:17 -07:00
|
|
|
const target_wlr_surface = switch (new_focus) {
|
2020-05-02 07:35:15 -07:00
|
|
|
.view => |target_view| target_view.wlr_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.
|
2020-07-15 05:15:17 -07:00
|
|
|
if (if (target_wlr_surface) |wlr_surface| self.input_manager.inputAllowed(wlr_surface) else true) {
|
2020-05-02 07:35:15 -07:00
|
|
|
// First clear the current focus
|
2020-07-15 05:15:17 -07:00
|
|
|
if (self.focused == .view) self.focused.view.setFocused(false);
|
2020-05-02 07:35:15 -07:00
|
|
|
c.wlr_seat_keyboard_clear_focus(self.wlr_seat);
|
|
|
|
|
|
|
|
// Set the new focus
|
2020-07-15 05:15:17 -07:00
|
|
|
switch (new_focus) {
|
2020-05-02 07:35:15 -07:00
|
|
|
.view => |target_view| {
|
|
|
|
std.debug.assert(self.focused_output == target_view.output);
|
|
|
|
target_view.setFocused(true);
|
|
|
|
},
|
2020-07-15 05:15:17 -07:00
|
|
|
.layer => |target_layer| std.debug.assert(self.focused_output == target_layer.output),
|
2020-05-02 07:35:15 -07:00
|
|
|
.none => {},
|
2020-04-13 12:00:18 -07:00
|
|
|
}
|
2020-07-15 05:15:17 -07:00
|
|
|
self.focused = new_focus;
|
2020-04-13 12:00:18 -07:00
|
|
|
|
2020-05-02 07:35:15 -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-07 12:48:56 -07:00
|
|
|
}
|
|
|
|
}
|
2020-06-04 07:56:58 -07:00
|
|
|
|
|
|
|
// Inform any clients tracking status of the change
|
|
|
|
var it = self.status_trackers.first;
|
|
|
|
while (it) |node| : (it = node.next) node.data.sendFocusedView();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Focus the given output, notifying any listening clients of the change.
|
|
|
|
pub fn focusOutput(self: *Self, output: *Output) void {
|
|
|
|
const root = &self.input_manager.server.root;
|
|
|
|
|
|
|
|
var it = self.status_trackers.first;
|
|
|
|
while (it) |node| : (it = node.next) node.data.sendOutput(.unfocused);
|
|
|
|
|
|
|
|
self.focused_output = output;
|
|
|
|
|
|
|
|
it = self.status_trackers.first;
|
|
|
|
while (it) |node| : (it = node.next) node.data.sendOutput(.focused);
|
2020-05-02 07:35:15 -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);
|
2020-06-19 05:31:53 -07:00
|
|
|
util.gpa.destroy(node);
|
2020-05-02 07:35:15 -07:00
|
|
|
break;
|
2020-04-12 13:19:48 -07:00
|
|
|
}
|
2020-05-02 07:35:15 -07:00
|
|
|
}
|
2020-04-12 13:19:48 -07:00
|
|
|
|
2020-05-02 07:35:15 -07:00
|
|
|
// If the unmapped view is focused, choose a new focus
|
2020-07-15 05:15:17 -07:00
|
|
|
if (self.focused == .view and self.focused.view == view) self.focus(null);
|
2020-05-02 07:35:15 -07:00
|
|
|
}
|
|
|
|
|
2020-06-01 06:16:18 -07:00
|
|
|
/// Handle any user-defined mapping for the passed keysym and modifiers
|
2020-05-02 07:35:15 -07:00
|
|
|
/// Returns true if the key was handled
|
2020-06-01 06:16:18 -07:00
|
|
|
pub fn handleMapping(self: *Self, keysym: c.xkb_keysym_t, modifiers: u32) bool {
|
2020-05-31 14:56:25 -07:00
|
|
|
const modes = &self.input_manager.server.config.modes;
|
2020-06-01 06:16:18 -07:00
|
|
|
for (modes.items[self.mode_id].items) |mapping| {
|
|
|
|
if (modifiers == mapping.modifiers and keysym == mapping.keysym) {
|
2020-05-02 07:35:15 -07:00
|
|
|
// Execute the bound command
|
2020-06-26 09:43:20 -07:00
|
|
|
const args = mapping.command_args;
|
|
|
|
var out: ?[]const u8 = null;
|
|
|
|
defer if (out) |s| util.gpa.free(s);
|
|
|
|
command.run(util.gpa, self, args, &out) catch |err| {
|
|
|
|
const failure_message = switch (err) {
|
|
|
|
command.Error.Other => out.?,
|
|
|
|
else => command.errToMsg(err),
|
|
|
|
};
|
|
|
|
log.err(.command, "{}: {}", .{ args[0], failure_message });
|
|
|
|
return true;
|
2020-05-26 13:55:07 -07:00
|
|
|
};
|
2020-06-26 09:43:20 -07:00
|
|
|
if (out) |s| {
|
|
|
|
const stdout = std.io.getStdOut().outStream();
|
|
|
|
stdout.print("{}", .{s}) catch
|
|
|
|
|err| log.err(.command, "{}: write to stdout failed {}", .{ args[0], err });
|
|
|
|
}
|
2020-05-02 07:35:15 -07:00
|
|
|
return true;
|
|
|
|
}
|
2020-03-22 14:42:55 -07:00
|
|
|
}
|
2020-05-02 07:35:15 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Add a newly created input device to the seat and update the reported
|
|
|
|
/// capabilities.
|
2020-07-05 13:49:17 -07:00
|
|
|
pub fn addDevice(self: *Self, device: *c.wlr_input_device) void {
|
2020-05-02 07:35:15 -07:00
|
|
|
switch (device.type) {
|
2020-07-05 13:49:17 -07:00
|
|
|
.WLR_INPUT_DEVICE_KEYBOARD => self.addKeyboard(device) catch return,
|
2020-05-02 07:35:15 -07:00
|
|
|
.WLR_INPUT_DEVICE_POINTER => self.addPointer(device),
|
2020-07-05 13:49:17 -07:00
|
|
|
else => return,
|
2020-03-22 14:42:55 -07:00
|
|
|
}
|
2020-04-24 16:04:12 -07:00
|
|
|
|
2020-05-02 07:35:15 -07:00
|
|
|
// 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 = @intCast(u32, c.WL_SEAT_CAPABILITY_POINTER);
|
2020-07-05 13:49:17 -07:00
|
|
|
if (self.keyboards.len > 0) caps |= @intCast(u32, c.WL_SEAT_CAPABILITY_KEYBOARD);
|
2020-05-02 07:35:15 -07:00
|
|
|
c.wlr_seat_set_capabilities(self.wlr_seat, caps);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn addKeyboard(self: *Self, device: *c.wlr_input_device) !void {
|
2020-07-05 13:49:17 -07:00
|
|
|
const node = try self.keyboards.allocateNode(util.gpa);
|
|
|
|
node.data.init(self, device) catch |err| {
|
|
|
|
switch (err) {
|
2020-07-16 12:20:43 -07:00
|
|
|
error.XkbContextFailed => log.err(.keyboard, "Failed to create XKB context", .{}),
|
|
|
|
error.XkbKeymapFailed => log.err(.keyboard, "Failed to create XKB keymap", .{}),
|
|
|
|
error.SetKeymapFailed => log.err(.keyboard, "Failed to set wlr keyboard keymap", .{}),
|
2020-07-05 13:49:17 -07:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
};
|
2020-05-02 07:35:15 -07:00
|
|
|
self.keyboards.append(node);
|
2020-07-05 13:49:17 -07:00
|
|
|
c.wlr_seat_set_keyboard(self.wlr_seat, device);
|
2020-05-02 07:35:15 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
fn addPointer(self: Self, device: *c.struct_wlr_input_device) void {
|
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn handleRequestSetSelection(listener: ?*c.wl_listener, data: ?*c_void) callconv(.C) void {
|
|
|
|
const self = @fieldParentPtr(Self, "listen_request_set_selection", listener.?);
|
2020-06-16 11:54:05 -07:00
|
|
|
const event = util.voidCast(c.wlr_seat_request_set_selection_event, data.?);
|
2020-05-02 07:35:15 -07:00
|
|
|
c.wlr_seat_set_selection(self.wlr_seat, event.source, event.serial);
|
|
|
|
}
|