river/river/InputManager.zig

165 lines
5.8 KiB
Zig
Raw Normal View History

2020-05-02 10:21:10 -07:00
// This file is part of river, a dynamic tiling wayland compositor.
//
2021-06-03 20:51:15 -07:00
// Copyright 2020 - 2021 The River Developers
2020-05-02 10:21:10 -07:00
//
// 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, version 3.
2020-05-02 10:21:10 -07:00
//
// 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:28:17 -07:00
const Self = @This();
const build_options = @import("build_options");
const std = @import("std");
const assert = std.debug.assert;
2021-06-03 20:51:15 -07:00
const mem = std.mem;
const wlr = @import("wlroots");
const wl = @import("wayland").server.wl;
2020-05-02 07:28:17 -07:00
const server = &@import("main.zig").server;
2020-06-16 11:54:05 -07:00
const util = @import("util.zig");
2021-06-03 20:51:15 -07:00
const InputConfig = @import("InputConfig.zig");
2022-06-20 07:29:35 -07:00
const InputDevice = @import("InputDevice.zig");
const Keyboard = @import("Keyboard.zig");
const PointerConstraint = @import("PointerConstraint.zig");
const Seat = @import("Seat.zig");
const Switch = @import("Switch.zig");
2020-05-02 07:28:17 -07:00
const default_seat_name = "default";
2021-02-05 00:46:18 -08:00
const log = std.log.scoped(.input_manager);
2020-12-31 06:35:35 -08:00
new_input: wl.Listener(*wlr.InputDevice) = wl.Listener(*wlr.InputDevice).init(handleNewInput),
idle_notifier: *wlr.IdleNotifierV1,
pointer_constraints: *wlr.PointerConstraintsV1,
relative_pointer_manager: *wlr.RelativePointerManagerV1,
virtual_pointer_manager: *wlr.VirtualPointerManagerV1,
virtual_keyboard_manager: *wlr.VirtualKeyboardManagerV1,
configs: std.ArrayList(InputConfig),
devices: wl.list.Head(InputDevice, "link"),
seats: std.TailQueue(Seat) = .{},
2020-04-18 15:59:07 -07:00
exclusive_client: ?*wl.Client = null,
new_pointer_constraint: wl.Listener(*wlr.PointerConstraintV1) =
wl.Listener(*wlr.PointerConstraintV1).init(handleNewPointerConstraint),
2020-12-31 06:35:35 -08:00
new_virtual_pointer: wl.Listener(*wlr.VirtualPointerManagerV1.event.NewPointer) =
wl.Listener(*wlr.VirtualPointerManagerV1.event.NewPointer).init(handleNewVirtualPointer),
new_virtual_keyboard: wl.Listener(*wlr.VirtualKeyboardV1) =
wl.Listener(*wlr.VirtualKeyboardV1).init(handleNewVirtualKeyboard),
2020-04-18 15:59:07 -07:00
pub fn init(self: *Self) !void {
2020-08-21 10:08:52 -07:00
const seat_node = try util.gpa.create(std.TailQueue(Seat).Node);
errdefer util.gpa.destroy(seat_node);
2020-08-21 10:08:52 -07:00
self.* = .{
// These are automatically freed when the display is destroyed
.idle_notifier = try wlr.IdleNotifierV1.create(server.wl_server),
.pointer_constraints = try wlr.PointerConstraintsV1.create(server.wl_server),
.relative_pointer_manager = try wlr.RelativePointerManagerV1.create(server.wl_server),
.virtual_pointer_manager = try wlr.VirtualPointerManagerV1.create(server.wl_server),
.virtual_keyboard_manager = try wlr.VirtualKeyboardManagerV1.create(server.wl_server),
.configs = std.ArrayList(InputConfig).init(util.gpa),
.devices = undefined,
2020-08-21 10:08:52 -07:00
};
self.devices.init();
2020-04-18 15:59:07 -07:00
2020-05-02 07:28:17 -07:00
self.seats.prepend(seat_node);
try seat_node.data.init(default_seat_name);
if (build_options.xwayland) server.xwayland.setSeat(self.defaultSeat().wlr_seat);
2020-12-31 06:35:35 -08:00
server.backend.events.new_input.add(&self.new_input);
self.pointer_constraints.events.new_constraint.add(&self.new_pointer_constraint);
self.virtual_pointer_manager.events.new_virtual_pointer.add(&self.new_virtual_pointer);
self.virtual_keyboard_manager.events.new_virtual_keyboard.add(&self.new_virtual_keyboard);
2020-05-02 07:28:17 -07:00
}
2020-04-18 15:59:07 -07:00
2020-05-02 07:28:17 -07:00
pub fn deinit(self: *Self) void {
// This function must be called after the backend has been destroyed
assert(self.devices.empty());
2020-05-02 07:28:17 -07:00
while (self.seats.pop()) |seat_node| {
seat_node.data.deinit();
util.gpa.destroy(seat_node);
}
2021-06-03 20:51:15 -07:00
for (self.configs.items) |*config| {
config.deinit();
2021-06-03 20:51:15 -07:00
}
self.configs.deinit();
2020-05-02 07:28:17 -07:00
}
pub fn defaultSeat(self: Self) *Seat {
return &self.seats.first.?.data;
}
2020-05-02 07:28:17 -07:00
/// Returns true if input is currently allowed on the passed surface.
pub fn inputAllowed(self: Self, wlr_surface: *wlr.Surface) bool {
2020-05-02 07:28:17 -07:00
return if (self.exclusive_client) |exclusive_client|
exclusive_client == wlr_surface.resource.getClient()
2020-05-02 07:28:17 -07:00
else
true;
}
2020-04-13 12:00:18 -07:00
pub fn updateCursorState(self: Self) void {
var it = self.seats.first;
while (it) |node| : (it = node.next) node.data.cursor.updateState();
}
fn handleNewInput(listener: *wl.Listener(*wlr.InputDevice), wlr_device: *wlr.InputDevice) void {
const self = @fieldParentPtr(Self, "new_input", listener);
2021-06-03 20:51:15 -07:00
self.defaultSeat().addDevice(wlr_device);
2020-05-02 07:28:17 -07:00
}
2021-10-11 03:44:46 -07:00
fn handleNewPointerConstraint(
_: *wl.Listener(*wlr.PointerConstraintV1),
constraint: *wlr.PointerConstraintV1,
) void {
const pointer_constraint = util.gpa.create(PointerConstraint) catch {
2021-10-11 03:44:46 -07:00
constraint.resource.getClient().postNoMemory();
log.err("out of memory", .{});
return;
};
pointer_constraint.init(constraint);
}
fn handleNewVirtualPointer(
listener: *wl.Listener(*wlr.VirtualPointerManagerV1.event.NewPointer),
event: *wlr.VirtualPointerManagerV1.event.NewPointer,
) void {
const self = @fieldParentPtr(Self, "new_virtual_pointer", listener);
// TODO Support multiple seats and don't ignore
if (event.suggested_seat != null) {
2021-02-05 00:46:18 -08:00
log.debug("Ignoring seat suggestion from virtual pointer", .{});
}
// TODO dont ignore output suggestion
if (event.suggested_output != null) {
2021-02-05 00:46:18 -08:00
log.debug("Ignoring output suggestion from virtual pointer", .{});
}
2022-11-11 11:25:21 -08:00
self.defaultSeat().addDevice(&event.new_pointer.pointer.base);
}
fn handleNewVirtualKeyboard(
2021-10-11 03:44:46 -07:00
_: *wl.Listener(*wlr.VirtualKeyboardV1),
virtual_keyboard: *wlr.VirtualKeyboardV1,
) void {
const seat = @intToPtr(*Seat, virtual_keyboard.seat.data);
2022-11-11 11:25:21 -08:00
seat.addDevice(&virtual_keyboard.keyboard.base);
}