Add basic input configuration

This commit is contained in:
Leon Henrik Plickat
2021-06-04 05:51:15 +02:00
committed by Isaac Freund
parent cab947b3a3
commit 833248e805
7 changed files with 522 additions and 3 deletions

View File

@ -1,6 +1,6 @@
// This file is part of river, a dynamic tiling wayland compositor.
//
// Copyright 2020 The River Developers
// Copyright 2020 - 2021 The River Developers
//
// 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
@ -19,12 +19,15 @@ const Self = @This();
const build_options = @import("build_options");
const std = @import("std");
const mem = std.mem;
const ascii = std.ascii;
const wlr = @import("wlroots");
const wl = @import("wayland").server.wl;
const server = &@import("main.zig").server;
const util = @import("util.zig");
const InputConfig = @import("InputConfig.zig");
const Seat = @import("Seat.zig");
const Server = @import("Server.zig");
const View = @import("View.zig");
@ -34,6 +37,55 @@ const default_seat_name = "default";
const log = std.log.scoped(.input_manager);
pub const InputDevice = struct {
device: *wlr.InputDevice,
destroy: wl.Listener(*wlr.InputDevice) = wl.Listener(*wlr.InputDevice).init(handleDestroy),
/// Careful: The identifier is not unique! A physical input device may have
/// multiple logical input devices with the exact same vendor id, product id
/// and name. However identifiers of InputConfigs are unique.
identifier: []const u8,
pub fn init(self: *InputDevice, device: *wlr.InputDevice) !void {
// The identifier is formatted exactly as in Sway
const identifier = try std.fmt.allocPrint(
util.gpa,
"{}:{}:{s}",
.{ device.vendor, device.product, mem.trim(
u8,
mem.sliceTo(device.name, 0),
&ascii.spaces,
) },
);
for (identifier) |*char| {
if (char.* == ' ' or !std.ascii.isPrint(char.*)) {
char.* = '_';
}
}
self.* = .{
.device = device,
.identifier = identifier,
};
log.debug("new input device: {s}", .{self.identifier});
device.events.destroy.add(&self.destroy);
}
pub fn deinit(self: *InputDevice) void {
util.gpa.free(self.identifier);
self.destroy.link.remove();
}
fn handleDestroy(listener: *wl.Listener(*wlr.InputDevice), device: *wlr.InputDevice) void {
const self = @fieldParentPtr(InputDevice, "destroy", listener);
log.debug("removed input device: {s}", .{self.identifier});
self.deinit();
const node = @fieldParentPtr(std.TailQueue(InputDevice).Node, "data", self);
server.input_manager.input_devices.remove(node);
util.gpa.destroy(node);
}
};
new_input: wl.Listener(*wlr.InputDevice) = wl.Listener(*wlr.InputDevice).init(handleNewInput),
idle: *wlr.Idle,
@ -43,6 +95,8 @@ relative_pointer_manager: *wlr.RelativePointerManagerV1,
virtual_pointer_manager: *wlr.VirtualPointerManagerV1,
virtual_keyboard_manager: *wlr.VirtualKeyboardManagerV1,
input_configs: std.ArrayList(InputConfig),
input_devices: std.TailQueue(InputDevice) = .{},
seats: std.TailQueue(Seat) = .{},
exclusive_client: ?*wl.Client = null,
@ -70,6 +124,7 @@ pub fn init(self: *Self) !void {
.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),
.input_configs = std.ArrayList(InputConfig).init(util.gpa),
};
self.seats.prepend(seat_node);
@ -90,6 +145,16 @@ pub fn deinit(self: *Self) void {
seat_node.data.deinit();
util.gpa.destroy(seat_node);
}
while (self.input_devices.pop()) |input_device_node| {
input_device_node.data.deinit();
util.gpa.destroy(input_device_node);
}
for (self.input_configs.items) |*input_config| {
input_config.deinit();
}
self.input_configs.deinit();
}
pub fn defaultSeat(self: Self) *Seat {
@ -172,8 +237,23 @@ fn handleInhibitDeactivate(
/// This event is raised by the backend when a new input device becomes available.
fn handleNewInput(listener: *wl.Listener(*wlr.InputDevice), device: *wlr.InputDevice) void {
const self = @fieldParentPtr(Self, "new_input", listener);
// TODO: suport multiple seats
// TODO: support multiple seats
const input_device_node = util.gpa.create(std.TailQueue(InputDevice).Node) catch return;
input_device_node.data.init(device) catch {
util.gpa.destroy(input_device_node);
return;
};
self.input_devices.append(input_device_node);
self.defaultSeat().addDevice(device);
// Apply matching input device configuration, if exists.
for (self.input_configs.items) |*input_config| {
if (mem.eql(u8, input_config.identifier, mem.sliceTo(input_device_node.data.identifier, 0))) {
input_config.apply(&input_device_node.data);
break; // There will only ever be one InputConfig for any unique identifier;
}
}
}
fn handleNewPointerConstraint(listener: *wl.Listener(*wlr.PointerConstraintV1), constraint: *wlr.PointerConstraintV1) void {