refactor input configuration
This commit is contained in:
parent
a531311ac6
commit
0cae415a93
@ -1,6 +1,6 @@
|
|||||||
// This file is part of river, a dynamic tiling wayland compositor.
|
// This file is part of river, a dynamic tiling wayland compositor.
|
||||||
//
|
//
|
||||||
// Copyright 2021 The River Developers
|
// Copyright 2021 - 2024 The River Developers
|
||||||
//
|
//
|
||||||
// This program is free software: you can redistribute it and/or modify
|
// 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
|
// it under the terms of the GNU General Public License as published by
|
||||||
@ -18,6 +18,9 @@ const Self = @This();
|
|||||||
|
|
||||||
const build_options = @import("build_options");
|
const build_options = @import("build_options");
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
|
const mem = std.mem;
|
||||||
|
const math = std.math;
|
||||||
|
const meta = std.meta;
|
||||||
const wlr = @import("wlroots");
|
const wlr = @import("wlroots");
|
||||||
|
|
||||||
const log = std.log.scoped(.input_config);
|
const log = std.log.scoped(.input_config);
|
||||||
@ -261,20 +264,20 @@ pub const ScrollButton = struct {
|
|||||||
|
|
||||||
identifier: []const u8,
|
identifier: []const u8,
|
||||||
|
|
||||||
event_state: ?EventState = null,
|
@"event-state": ?EventState = null,
|
||||||
accel_profile: ?AccelProfile = null,
|
@"accel-profile": ?AccelProfile = null,
|
||||||
click_method: ?ClickMethod = null,
|
@"click-method": ?ClickMethod = null,
|
||||||
drag_state: ?DragState = null,
|
@"drag-state": ?DragState = null,
|
||||||
drag_lock: ?DragLock = null,
|
@"drag-lock": ?DragLock = null,
|
||||||
dwt_state: ?DwtState = null,
|
@"dwt-state": ?DwtState = null,
|
||||||
middle_emulation: ?MiddleEmulation = null,
|
@"middle-emulation": ?MiddleEmulation = null,
|
||||||
natural_scroll: ?NaturalScroll = null,
|
@"natural-scroll": ?NaturalScroll = null,
|
||||||
left_handed: ?LeftHanded = null,
|
@"left-handed": ?LeftHanded = null,
|
||||||
tap_state: ?TapState = null,
|
@"tap-state": ?TapState = null,
|
||||||
tap_button_map: ?TapButtonMap = null,
|
@"tap-button-map": ?TapButtonMap = null,
|
||||||
pointer_accel: ?PointerAccel = null,
|
@"pointer-accel": ?PointerAccel = null,
|
||||||
scroll_method: ?ScrollMethod = null,
|
@"scroll-method": ?ScrollMethod = null,
|
||||||
scroll_button: ?ScrollButton = null,
|
@"scroll-button": ?ScrollButton = null,
|
||||||
|
|
||||||
pub fn deinit(self: *Self) void {
|
pub fn deinit(self: *Self) void {
|
||||||
util.gpa.free(self.identifier);
|
util.gpa.free(self.identifier);
|
||||||
@ -283,18 +286,67 @@ pub fn deinit(self: *Self) void {
|
|||||||
pub fn apply(self: *Self, device: *InputDevice) void {
|
pub fn apply(self: *Self, device: *InputDevice) void {
|
||||||
const libinput_device: *c.libinput_device = @ptrCast(device.wlr_device.getLibinputDevice() orelse return);
|
const libinput_device: *c.libinput_device = @ptrCast(device.wlr_device.getLibinputDevice() orelse return);
|
||||||
log.debug("applying input configuration to device: {s}", .{device.identifier});
|
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);
|
inline for (@typeInfo(Self).Struct.fields) |field| {
|
||||||
if (self.click_method) |setting| setting.apply(libinput_device);
|
if (comptime mem.eql(u8, field.name, "identifier")) continue;
|
||||||
if (self.drag_state) |setting| setting.apply(libinput_device);
|
|
||||||
if (self.drag_lock) |setting| setting.apply(libinput_device);
|
if (@field(self, field.name)) |setting| {
|
||||||
if (self.dwt_state) |setting| setting.apply(libinput_device);
|
log.debug("applying setting: {s}", .{field.name});
|
||||||
if (self.middle_emulation) |setting| setting.apply(libinput_device);
|
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);
|
pub fn parse(self: *Self, setting: []const u8, value: []const u8) !void {
|
||||||
if (self.tap_state) |setting| setting.apply(libinput_device);
|
inline for (@typeInfo(Self).Struct.fields) |field| {
|
||||||
if (self.tap_button_map) |setting| setting.apply(libinput_device);
|
if (comptime mem.eql(u8, field.name, "identifier")) continue;
|
||||||
|
|
||||||
|
if (mem.eql(u8, setting, field.name)) {
|
||||||
|
// Special-case the settings which are not enums.
|
||||||
|
if (comptime mem.eql(u8, field.name, "pointer-accel")) {
|
||||||
|
self.@"pointer-accel" = PointerAccel{
|
||||||
|
.value = math.clamp(try std.fmt.parseFloat(f32, value), -1.0, 1.0),
|
||||||
|
};
|
||||||
|
} else if (comptime mem.eql(u8, field.name, "scroll-button")) {
|
||||||
|
const ret = c.libevdev_event_code_from_name(c.EV_KEY, value.ptr);
|
||||||
|
if (ret < 1) return error.InvalidButton;
|
||||||
|
self.@"scroll-button" = ScrollButton{ .button = @intCast(ret) };
|
||||||
|
} else {
|
||||||
|
const T = @typeInfo(field.type).Optional.child;
|
||||||
|
if (@typeInfo(T) != .Enum) {
|
||||||
|
@compileError("You forgot to implement parsing for an input configuration setting.");
|
||||||
|
}
|
||||||
|
@field(self, field.name) = meta.stringToEnum(T, value) orelse
|
||||||
|
return error.UnknownOption;
|
||||||
|
}
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return error.UnknownCommand;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn write(self: *Self, writer: anytype) !void {
|
||||||
|
try writer.print("{s}\n", .{self.identifier});
|
||||||
|
|
||||||
|
inline for (@typeInfo(Self).Struct.fields) |field| {
|
||||||
|
if (comptime mem.eql(u8, field.name, "identifier")) continue;
|
||||||
|
if (@field(self, field.name)) |setting| {
|
||||||
|
// Special-case the settings which are not enums.
|
||||||
|
if (comptime mem.eql(u8, field.name, "pointer-accel")) {
|
||||||
|
try writer.print("\tpointer-accel: {d}\n", .{setting.value});
|
||||||
|
} else if (comptime mem.eql(u8, field.name, "scroll-button")) {
|
||||||
|
try writer.print("\tscroll-button: {s}\n", .{
|
||||||
|
mem.sliceTo(c.libevdev_event_code_get_name(c.EV_KEY, setting.button), 0),
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
const T = @typeInfo(field.type).Optional.child;
|
||||||
|
if (@typeInfo(T) != .Enum) {
|
||||||
|
@compileError("You forgot to implement listing for an input configuration setting.");
|
||||||
|
}
|
||||||
|
try writer.print("\t{s}: {s}\n", .{ field.name, @tagName(setting) });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -16,11 +16,9 @@
|
|||||||
|
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const mem = std.mem;
|
const mem = std.mem;
|
||||||
const meta = std.meta;
|
|
||||||
|
|
||||||
const server = &@import("../main.zig").server;
|
const server = &@import("../main.zig").server;
|
||||||
const util = @import("../util.zig");
|
const util = @import("../util.zig");
|
||||||
const c = @import("../c.zig");
|
|
||||||
|
|
||||||
const Error = @import("../command.zig").Error;
|
const Error = @import("../command.zig").Error;
|
||||||
const Seat = @import("../Seat.zig");
|
const Seat = @import("../Seat.zig");
|
||||||
@ -69,54 +67,8 @@ pub fn listInputConfigs(
|
|||||||
const writer = input_list.writer();
|
const writer = input_list.writer();
|
||||||
|
|
||||||
for (server.input_manager.configs.items, 0..) |*input_config, i| {
|
for (server.input_manager.configs.items, 0..) |*input_config, i| {
|
||||||
if (i > 0) try input_list.appendSlice("\n");
|
if (i > 0) try writer.writeByte('\n');
|
||||||
|
try input_config.write(writer);
|
||||||
try writer.print("{s}\n", .{input_config.identifier});
|
|
||||||
|
|
||||||
if (input_config.event_state) |event_state| {
|
|
||||||
try writer.print("\tevents: {s}\n", .{@tagName(event_state)});
|
|
||||||
}
|
|
||||||
if (input_config.accel_profile) |accel_profile| {
|
|
||||||
try writer.print("\taccel-profile: {s}\n", .{@tagName(accel_profile)});
|
|
||||||
}
|
|
||||||
if (input_config.click_method) |click_method| {
|
|
||||||
try writer.print("\tclick-method: {s}\n", .{@tagName(click_method)});
|
|
||||||
}
|
|
||||||
if (input_config.drag_state) |drag_state| {
|
|
||||||
try writer.print("\tdrag: {s}\n", .{@tagName(drag_state)});
|
|
||||||
}
|
|
||||||
if (input_config.drag_lock) |drag_lock| {
|
|
||||||
try writer.print("\tdrag-lock: {s}\n", .{@tagName(drag_lock)});
|
|
||||||
}
|
|
||||||
if (input_config.dwt_state) |dwt_state| {
|
|
||||||
try writer.print("\tdisable-while-typing: {s}\n", .{@tagName(dwt_state)});
|
|
||||||
}
|
|
||||||
if (input_config.middle_emulation) |middle_emulation| {
|
|
||||||
try writer.print("\tmiddle-emulation: {s}\n", .{@tagName(middle_emulation)});
|
|
||||||
}
|
|
||||||
if (input_config.natural_scroll) |natural_scroll| {
|
|
||||||
try writer.print("\tnatural-scroll: {s}\n", .{@tagName(natural_scroll)});
|
|
||||||
}
|
|
||||||
if (input_config.left_handed) |left_handed| {
|
|
||||||
try writer.print("\tleft-handed: {s}\n", .{@tagName(left_handed)});
|
|
||||||
}
|
|
||||||
if (input_config.tap_state) |tap_state| {
|
|
||||||
try writer.print("\ttap: {s}\n", .{@tagName(tap_state)});
|
|
||||||
}
|
|
||||||
if (input_config.tap_button_map) |tap_button_map| {
|
|
||||||
try writer.print("\ttap-button-map: {s}\n", .{@tagName(tap_button_map)});
|
|
||||||
}
|
|
||||||
if (input_config.pointer_accel) |pointer_accel| {
|
|
||||||
try writer.print("\tpointer-accel: {d}\n", .{pointer_accel.value});
|
|
||||||
}
|
|
||||||
if (input_config.scroll_method) |scroll_method| {
|
|
||||||
try writer.print("\tscroll-method: {s}\n", .{@tagName(scroll_method)});
|
|
||||||
}
|
|
||||||
if (input_config.scroll_button) |scroll_button| {
|
|
||||||
try writer.print("\tscroll-button: {s}\n", .{
|
|
||||||
mem.sliceTo(c.libevdev_event_code_get_name(c.EV_KEY, scroll_button.button), 0),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
out.* = try input_list.toOwnedSlice();
|
out.* = try input_list.toOwnedSlice();
|
||||||
@ -132,75 +84,26 @@ pub fn input(
|
|||||||
|
|
||||||
// Try to find an existing InputConfig with matching identifier, or create
|
// Try to find an existing InputConfig with matching identifier, or create
|
||||||
// a new one if none was found.
|
// a new one if none was found.
|
||||||
var new = false;
|
|
||||||
const input_config = for (server.input_manager.configs.items) |*input_config| {
|
const input_config = for (server.input_manager.configs.items) |*input_config| {
|
||||||
if (mem.eql(u8, input_config.identifier, args[1])) break input_config;
|
if (mem.eql(u8, input_config.identifier, args[1])) {
|
||||||
|
try input_config.parse(args[2], args[3]);
|
||||||
|
break input_config;
|
||||||
|
}
|
||||||
} else blk: {
|
} else blk: {
|
||||||
try server.input_manager.configs.ensureUnusedCapacity(1);
|
const identifier_owned = try util.gpa.dupe(u8, args[1]);
|
||||||
server.input_manager.configs.appendAssumeCapacity(.{
|
errdefer util.gpa.free(identifier_owned);
|
||||||
.identifier = try util.gpa.dupe(u8, args[1]),
|
|
||||||
});
|
|
||||||
new = true;
|
|
||||||
break :blk &server.input_manager.configs.items[server.input_manager.configs.items.len - 1];
|
|
||||||
};
|
|
||||||
errdefer {
|
|
||||||
if (new) {
|
|
||||||
var cfg = server.input_manager.configs.pop();
|
|
||||||
cfg.deinit();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (mem.eql(u8, "events", args[2])) {
|
try server.input_manager.configs.ensureUnusedCapacity(1);
|
||||||
input_config.event_state = meta.stringToEnum(InputConfig.EventState, args[3]) orelse
|
const input_config = server.input_manager.configs.addOneAssumeCapacity();
|
||||||
return Error.UnknownOption;
|
errdefer _ = server.input_manager.configs.pop();
|
||||||
} else if (mem.eql(u8, "accel-profile", args[2])) {
|
|
||||||
input_config.accel_profile = meta.stringToEnum(InputConfig.AccelProfile, args[3]) orelse
|
input_config.* = .{
|
||||||
return Error.UnknownOption;
|
.identifier = identifier_owned,
|
||||||
} else if (mem.eql(u8, "click-method", args[2])) {
|
};
|
||||||
input_config.click_method = meta.stringToEnum(InputConfig.ClickMethod, args[3]) orelse
|
try input_config.parse(args[2], args[3]);
|
||||||
return Error.UnknownOption;
|
|
||||||
} else if (mem.eql(u8, "drag", args[2])) {
|
break :blk input_config;
|
||||||
input_config.drag_state = meta.stringToEnum(InputConfig.DragState, args[3]) orelse
|
|
||||||
return Error.UnknownOption;
|
|
||||||
} else if (mem.eql(u8, "drag-lock", args[2])) {
|
|
||||||
input_config.drag_lock = meta.stringToEnum(InputConfig.DragLock, args[3]) orelse
|
|
||||||
return Error.UnknownOption;
|
|
||||||
} else if (mem.eql(u8, "disable-while-typing", args[2])) {
|
|
||||||
input_config.dwt_state = meta.stringToEnum(InputConfig.DwtState, args[3]) orelse
|
|
||||||
return Error.UnknownOption;
|
|
||||||
} else if (mem.eql(u8, "middle-emulation", args[2])) {
|
|
||||||
input_config.middle_emulation = meta.stringToEnum(InputConfig.MiddleEmulation, args[3]) orelse
|
|
||||||
return Error.UnknownOption;
|
|
||||||
} else if (mem.eql(u8, "natural-scroll", args[2])) {
|
|
||||||
input_config.natural_scroll = meta.stringToEnum(InputConfig.NaturalScroll, args[3]) orelse
|
|
||||||
return Error.UnknownOption;
|
|
||||||
} else if (mem.eql(u8, "left-handed", args[2])) {
|
|
||||||
input_config.left_handed = meta.stringToEnum(InputConfig.LeftHanded, args[3]) orelse
|
|
||||||
return Error.UnknownOption;
|
|
||||||
} else if (mem.eql(u8, "tap", args[2])) {
|
|
||||||
input_config.tap_state = 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 = 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 = meta.stringToEnum(InputConfig.ScrollMethod, args[3]) orelse
|
|
||||||
return Error.UnknownOption;
|
|
||||||
} else if (mem.eql(u8, "scroll-button", args[2])) {
|
|
||||||
const ret = c.libevdev_event_code_from_name(c.EV_KEY, args[3].ptr);
|
|
||||||
if (ret < 1) return Error.InvalidButton;
|
|
||||||
input_config.scroll_button = InputConfig.ScrollButton{ .button = @intCast(ret) };
|
|
||||||
} else {
|
|
||||||
return Error.UnknownCommand;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Update matching existing input devices.
|
// Update matching existing input devices.
|
||||||
var it = server.input_manager.devices.iterator(.forward);
|
var it = server.input_manager.devices.iterator(.forward);
|
||||||
|
Loading…
Reference in New Issue
Block a user