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

@ -179,6 +179,7 @@ fn addServerDeps(exe: *zbs.LibExeObjStep, scanner: *ScanProtocolsStep) void {
exe.linkLibC();
exe.linkSystemLibrary("libevdev");
exe.linkSystemLibrary("libinput");
exe.addPackage(wayland);
exe.linkSystemLibrary("wayland-server");

2
deps/zig-wlroots vendored

@ -1 +1 @@
Subproject commit d6444e7a8d43750e2d4ce0cb76483ff968848cb1
Subproject commit c85fe796d932731ead64e916c90fdfc3d1e1269a

309
river/InputConfig.zig Normal file
View file

@ -0,0 +1,309 @@
// This file is part of river, a dynamic tiling wayland compositor.
//
// Copyright 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
// 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/>.
const Self = @This();
const build_options = @import("build_options");
const std = @import("std");
const wlr = @import("wlroots");
const log = std.log.scoped(.input_config);
const c = @import("c.zig");
const server = &@import("main.zig").server;
const util = @import("util.zig");
const InputDevice = @import("InputManager.zig").InputDevice;
// TODO - keyboards
// - mapping to output / region
// - calibration matrices
// - scroll factor
pub const EventState = enum {
enabled,
disabled,
@"disabled-on-external-mouse",
pub fn apply(event_state: EventState, device: *c.libinput_device) void {
const want = switch (event_state) {
.enabled => c.LIBINPUT_CONFIG_SEND_EVENTS_ENABLED,
.disabled => c.LIBINPUT_CONFIG_SEND_EVENTS_DISABLED,
.@"disabled-on-external-mouse" => c.LIBINPUT_CONFIG_SEND_EVENTS_DISABLED_ON_EXTERNAL_MOUSE,
};
const current = c.libinput_device_config_send_events_get_mode(device);
if (want != current) {
_ = c.libinput_device_config_send_events_set_mode(device, @intCast(u32, want));
}
}
};
pub const AccelProfile = enum {
none,
flat,
adaptive,
pub fn apply(accel_profile: AccelProfile, device: *c.libinput_device) void {
const want = @intToEnum(c.libinput_config_accel_profile, switch (accel_profile) {
.none => c.LIBINPUT_CONFIG_ACCEL_PROFILE_NONE,
.flat => c.LIBINPUT_CONFIG_ACCEL_PROFILE_FLAT,
.adaptive => c.LIBINPUT_CONFIG_ACCEL_PROFILE_ADAPTIVE,
});
if (c.libinput_device_config_accel_is_available(device) == 0) return;
const current = c.libinput_device_config_accel_get_profile(device);
if (want != current) {
_ = c.libinput_device_config_accel_set_profile(device, want);
}
}
};
pub const ClickMethod = enum {
none,
@"button-areas",
clickfinger,
pub fn apply(click_method: ClickMethod, device: *c.libinput_device) void {
const want = @intToEnum(c.libinput_config_click_method, switch (click_method) {
.none => c.LIBINPUT_CONFIG_CLICK_METHOD_NONE,
.@"button-areas" => c.LIBINPUT_CONFIG_CLICK_METHOD_BUTTON_AREAS,
.clickfinger => c.LIBINPUT_CONFIG_CLICK_METHOD_CLICKFINGER,
});
const supports = c.libinput_device_config_click_get_methods(device);
if (supports & @intCast(u32, @enumToInt(want)) == 0) return;
_ = c.libinput_device_config_click_set_method(device, want);
}
};
pub const DragState = enum {
disabled,
enabled,
pub fn apply(drag_state: DragState, device: *c.libinput_device) void {
const want = @intToEnum(c.libinput_config_drag_state, switch (drag_state) {
.disabled => c.LIBINPUT_CONFIG_DRAG_DISABLED,
.enabled => c.LIBINPUT_CONFIG_DRAG_ENABLED,
});
if (c.libinput_device_config_tap_get_finger_count(device) <= 0) return;
const current = c.libinput_device_config_tap_get_drag_enabled(device);
if (want != current) {
_ = c.libinput_device_config_tap_set_drag_enabled(device, want);
}
}
};
pub const DragLock = enum {
disabled,
enabled,
pub fn apply(drag_lock: DragLock, device: *c.libinput_device) void {
const want = @intToEnum(c.libinput_config_drag_lock_state, switch (drag_lock) {
.disabled => c.LIBINPUT_CONFIG_DRAG_LOCK_DISABLED,
.enabled => c.LIBINPUT_CONFIG_DRAG_LOCK_ENABLED,
});
if (c.libinput_device_config_tap_get_finger_count(device) <= 0) return;
const current = c.libinput_device_config_tap_get_drag_lock_enabled(device);
if (want != current) {
_ = c.libinput_device_config_tap_set_drag_lock_enabled(device, want);
}
}
};
pub const DwtState = enum {
disabled,
enabled,
pub fn apply(dwt_state: DwtState, device: *c.libinput_device) void {
const want = @intToEnum(c.libinput_config_dwt_state, switch (dwt_state) {
.disabled => c.LIBINPUT_CONFIG_DWT_DISABLED,
.enabled => c.LIBINPUT_CONFIG_DWT_ENABLED,
});
if (c.libinput_device_config_dwt_is_available(device) == 0) return;
const current = c.libinput_device_config_dwt_get_enabled(device);
if (want != current) {
_ = c.libinput_device_config_dwt_set_enabled(device, want);
}
}
};
pub const MiddleEmulation = enum {
disabled,
enabled,
pub fn apply(middle_emulation: MiddleEmulation, device: *c.libinput_device) void {
const want = @intToEnum(c.libinput_config_middle_emulation_state, switch (middle_emulation) {
.disabled => c.LIBINPUT_CONFIG_MIDDLE_EMULATION_DISABLED,
.enabled => c.LIBINPUT_CONFIG_MIDDLE_EMULATION_ENABLED,
});
if (c.libinput_device_config_middle_emulation_is_available(device) == 0) return;
const current = c.libinput_device_config_middle_emulation_get_enabled(device);
if (want != current) {
_ = c.libinput_device_config_middle_emulation_set_enabled(device, want);
}
}
};
pub const NaturalScroll = enum {
disabled,
enabled,
pub fn apply(natural_scroll: NaturalScroll, device: *c.libinput_device) void {
const want: c_int = switch (natural_scroll) {
.disabled => 0,
.enabled => 1,
};
if (c.libinput_device_config_scroll_has_natural_scroll(device) == 0) return;
const current = c.libinput_device_config_scroll_get_natural_scroll_enabled(device);
if (want != current) {
_ = c.libinput_device_config_scroll_set_natural_scroll_enabled(device, want);
}
}
};
pub const LeftHanded = enum {
disabled,
enabled,
pub fn apply(left_handed: LeftHanded, device: *c.libinput_device) void {
const want: c_int = switch (left_handed) {
.disabled => 0,
.enabled => 1,
};
if (c.libinput_device_config_left_handed_is_available(device) == 0) return;
const current = c.libinput_device_config_left_handed_get(device);
if (want != current) {
_ = c.libinput_device_config_left_handed_set(device, want);
}
}
};
pub const TapState = enum {
disabled,
enabled,
pub fn apply(tap_state: TapState, device: *c.libinput_device) void {
const want = @intToEnum(c.libinput_config_tap_state, switch (tap_state) {
.disabled => c.LIBINPUT_CONFIG_TAP_DISABLED,
.enabled => c.LIBINPUT_CONFIG_TAP_ENABLED,
});
if (c.libinput_device_config_tap_get_finger_count(device) <= 0) return;
const current = c.libinput_device_config_tap_get_enabled(device);
if (want != current) {
_ = c.libinput_device_config_tap_set_enabled(device, want);
}
}
};
pub const TapButtonMap = enum {
@"left-middle-right",
@"left-right-middle",
pub fn apply(tap_button_map: TapButtonMap, device: *c.libinput_device) void {
const want = @intToEnum(c.libinput_config_tap_button_map, switch (tap_button_map) {
.@"left-right-middle" => c.LIBINPUT_CONFIG_TAP_MAP_LRM,
.@"left-middle-right" => c.LIBINPUT_CONFIG_TAP_MAP_LMR,
});
if (c.libinput_device_config_tap_get_finger_count(device) <= 0) return;
const current = c.libinput_device_config_tap_get_button_map(device);
if (want != current) {
_ = c.libinput_device_config_tap_set_button_map(device, want);
}
}
};
pub const PointerAccel = struct {
value: f32,
pub fn apply(pointer_accel: PointerAccel, device: *c.libinput_device) void {
if (c.libinput_device_config_accel_is_available(device) == 0) return;
if (c.libinput_device_config_accel_get_speed(device) != pointer_accel.value) {
_ = c.libinput_device_config_accel_set_speed(device, pointer_accel.value);
}
}
};
pub const ScrollMethod = enum {
none,
@"two-finger",
edge,
button,
pub fn apply(scroll_method: ScrollMethod, device: *c.libinput_device) void {
const want = @intToEnum(c.libinput_config_scroll_method, switch (scroll_method) {
.none => c.LIBINPUT_CONFIG_SCROLL_NO_SCROLL,
.@"two-finger" => c.LIBINPUT_CONFIG_SCROLL_2FG,
.edge => c.LIBINPUT_CONFIG_SCROLL_EDGE,
.button => c.LIBINPUT_CONFIG_SCROLL_ON_BUTTON_DOWN,
});
const supports = c.libinput_device_config_scroll_get_methods(device);
if (supports & @intCast(u32, @enumToInt(want)) == 0) return;
_ = c.libinput_device_config_scroll_set_method(device, want);
}
};
pub const ScrollButton = struct {
button: u32,
pub fn apply(scroll_button: ScrollButton, device: *c.libinput_device) void {
const supports = c.libinput_device_config_scroll_get_methods(device);
if (supports & ~@intCast(u32, c.LIBINPUT_CONFIG_SCROLL_NO_SCROLL) == 0) return;
_ = c.libinput_device_config_scroll_set_button(device, scroll_button.button);
}
};
identifier: []const u8,
event_state: ?EventState = null,
accel_profile: ?AccelProfile = null,
click_method: ?ClickMethod = null,
drag_state: ?DragState = null,
drag_lock: ?DragLock = null,
dwt_state: ?DwtState = null,
middle_emulation: ?MiddleEmulation = null,
natural_scroll: ?NaturalScroll = null,
left_handed: ?LeftHanded = null,
tap_state: ?TapState = null,
tap_button_map: ?TapButtonMap = null,
pointer_accel: ?PointerAccel = null,
scroll_method: ?ScrollMethod = null,
scroll_button: ?ScrollButton = null,
pub fn deinit(self: *Self) void {
util.gpa.free(self.identifier);
}
pub fn apply(self: *Self, device: *InputDevice) void {
const libinput_device = @ptrCast(
*c.libinput_device,
device.device.getLibinputDevice() orelse return,
);
log.debug("applying input configuration to device: {s}", .{device.identifier});
if (self.event_state) |setting| setting.apply(libinput_device);
if (self.accel_profile) |setting| setting.apply(libinput_device);
if (self.click_method) |setting| setting.apply(libinput_device);
if (self.drag_state) |setting| setting.apply(libinput_device);
if (self.drag_lock) |setting| setting.apply(libinput_device);
if (self.dwt_state) |setting| setting.apply(libinput_device);
if (self.middle_emulation) |setting| setting.apply(libinput_device);
if (self.natural_scroll) |setting| setting.apply(libinput_device);
if (self.left_handed) |setting| setting.apply(libinput_device);
if (self.pointer_accel) |setting| setting.apply(libinput_device);
if (self.scroll_button) |setting| setting.apply(libinput_device);
if (self.scroll_method) |setting| setting.apply(libinput_device);
if (self.tap_state) |setting| setting.apply(libinput_device);
if (self.tap_button_map) |setting| setting.apply(libinput_device);
}

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 {

View file

@ -23,4 +23,6 @@ pub usingnamespace @cImport({
@cInclude("linux/input-event-codes.h");
@cInclude("libevdev/libevdev.h");
@cInclude("libinput.h");
});

View file

@ -57,6 +57,7 @@ const str_to_impl_fn = [_]struct {
.{ .name = "focus-follows-cursor", .impl = @import("command/focus_follows_cursor.zig").focusFollowsCursor },
.{ .name = "focus-output", .impl = @import("command/focus_output.zig").focusOutput },
.{ .name = "focus-view", .impl = @import("command/focus_view.zig").focusView },
.{ .name = "input", .impl = @import("command/input.zig").input },
.{ .name = "map", .impl = @import("command/map.zig").map },
.{ .name = "map-pointer", .impl = @import("command/map.zig").mapPointer },
.{ .name = "mod-layout-value", .impl = @import("command/layout.zig").modLayoutValue },
@ -90,6 +91,7 @@ pub const Error = error{
NotEnoughArguments,
TooManyArguments,
Overflow,
InvalidButton,
InvalidCharacter,
InvalidDirection,
InvalidPhysicalDirection,
@ -134,6 +136,7 @@ pub fn errToMsg(err: Error) [:0]const u8 {
Error.NotEnoughArguments => "not enough arguments",
Error.TooManyArguments => "too many arguments",
Error.Overflow => "value out of bounds",
Error.InvalidButton => "invalid button",
Error.InvalidCharacter => "invalid character in argument",
Error.InvalidDirection => "invalid direction. Must be 'next' or 'previous'",
Error.InvalidPhysicalDirection => "invalid direction. Must be 'up', 'down', 'left' or 'right'",

124
river/command/input.zig Normal file
View file

@ -0,0 +1,124 @@
// This file is part of river, a dynamic tiling wayland compositor.
//
// Copyright 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
// 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/>.
const std = @import("std");
const mem = std.mem;
const server = &@import("../main.zig").server;
const util = @import("../util.zig");
const c = @import("../c.zig");
const Error = @import("../command.zig").Error;
const Seat = @import("../Seat.zig");
const InputConfig = @import("../InputConfig.zig");
const InputManager = @import("../InputManager.zig");
pub fn input(
allocator: *mem.Allocator,
seat: *Seat,
args: []const []const u8,
out: *?[]const u8,
) Error!void {
if (args.len < 4) return Error.NotEnoughArguments;
if (args.len > 4) return Error.TooManyArguments;
// Try to find an existing InputConfig with matching identifier, or create
// a new one if none was found.
var new = false;
const input_config = for (server.input_manager.input_configs.items) |*input_config| {
if (mem.eql(u8, input_config.identifier, args[1])) break input_config;
} else blk: {
// Use util.gpa instead of allocator to assure the identifier is
// allocated by the same allocator as the ArrayList.
try server.input_manager.input_configs.ensureUnusedCapacity(1);
server.input_manager.input_configs.appendAssumeCapacity(.{
.identifier = try util.gpa.dupe(u8, args[1]),
});
new = true;
break :blk &server.input_manager.input_configs.items[server.input_manager.input_configs.items.len - 1];
};
errdefer {
if (new) {
var cfg = server.input_manager.input_configs.pop();
cfg.deinit();
}
}
if (mem.eql(u8, "events", args[2])) {
input_config.event_state = std.meta.stringToEnum(InputConfig.EventState, args[3]) orelse
return Error.UnknownOption;
} else if (mem.eql(u8, "accel-profile", args[2])) {
input_config.accel_profile = std.meta.stringToEnum(InputConfig.AccelProfile, args[3]) orelse
return Error.UnknownOption;
} else if (mem.eql(u8, "click-method", args[2])) {
input_config.click_method = std.meta.stringToEnum(InputConfig.ClickMethod, args[3]) orelse
return Error.UnknownOption;
} else if (mem.eql(u8, "drag", args[2])) {
input_config.drag_state = std.meta.stringToEnum(InputConfig.DragState, args[3]) orelse
return Error.UnknownOption;
} else if (mem.eql(u8, "drag-lock", args[2])) {
input_config.drag_lock = std.meta.stringToEnum(InputConfig.DragLock, args[3]) orelse
return Error.UnknownOption;
} else if (mem.eql(u8, "disable-while-typing", args[2])) {
input_config.dwt_state = std.meta.stringToEnum(InputConfig.DwtState, args[3]) orelse
return Error.UnknownOption;
} else if (mem.eql(u8, "middle-emulation", args[2])) {
input_config.middle_emulation = std.meta.stringToEnum(InputConfig.MiddleEmulation, args[3]) orelse
return Error.UnknownOption;
} else if (mem.eql(u8, "natural-scroll", args[2])) {
input_config.natural_scroll = std.meta.stringToEnum(InputConfig.NaturalScroll, args[3]) orelse
return Error.UnknownOption;
} else if (mem.eql(u8, "left-handed", args[2])) {
input_config.left_handed = std.meta.stringToEnum(InputConfig.LeftHanded, args[3]) orelse
return Error.UnknownOption;
} else if (mem.eql(u8, "tap", args[2])) {
input_config.tap_state = std.meta.stringToEnum(InputConfig.TapState, args[3]) orelse
return Error.UnknownOption;
} else if (mem.eql(u8, "tap-button-map", args[2])) {
input_config.tap_button_map = std.meta.stringToEnum(InputConfig.TapButtonMap, args[3]) orelse
return Error.UnknownOption;
} else if (mem.eql(u8, "pointer-accel", args[2])) {
input_config.pointer_accel = InputConfig.PointerAccel{
.value = std.math.clamp(
try std.fmt.parseFloat(f32, args[3]),
@as(f32, -1.0),
@as(f32, 1.0),
),
};
} else if (mem.eql(u8, "scroll-method", args[2])) {
input_config.scroll_method = std.meta.stringToEnum(InputConfig.ScrollMethod, args[3]) orelse
return Error.UnknownOption;
} else if (mem.eql(u8, "scroll-button", args[2])) {
const event_code_name = try std.cstr.addNullByte(allocator, args[3]);
defer allocator.free(event_code_name);
const ret = c.libevdev_event_code_from_name(c.EV_KEY, event_code_name);
if (ret < 1) return Error.InvalidButton;
input_config.scroll_button = InputConfig.ScrollButton{ .button = @intCast(u32, ret) };
} else {
return Error.UnknownCommand;
}
// Update matching existing input devices.
var it = server.input_manager.input_devices.first;
while (it) |device_node| : (it = device_node.next) {
if (mem.eql(u8, device_node.data.identifier, args[1])) {
input_config.apply(&device_node.data);
// We don't break here because it is common to have multiple input
// devices with the same identifier.
}
}
}