InputConfig: eliminate "self" naming convention

This commit is contained in:
Isaac Freund 2024-03-14 12:53:09 +01:00
parent b65e4f6b17
commit 4a259d322b
No known key found for this signature in database
GPG Key ID: 86DED400DDFD7A11

View File

@ -14,7 +14,7 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>. // along with this program. If not, see <https://www.gnu.org/licenses/>.
const Self = @This(); const InputConfig = @This();
const build_options = @import("build_options"); const build_options = @import("build_options");
const std = @import("std"); const std = @import("std");
@ -272,43 +272,43 @@ tap: ?TapState = null,
@"scroll-button": ?ScrollButton = null, @"scroll-button": ?ScrollButton = null,
@"map-to-output": MapToOutput = .{ .output_name = null }, @"map-to-output": MapToOutput = .{ .output_name = null },
pub fn deinit(self: *Self) void { pub fn deinit(config: *InputConfig) void {
util.gpa.free(self.glob); util.gpa.free(config.glob);
if (self.@"map-to-output".output_name) |output_name| { if (config.@"map-to-output".output_name) |output_name| {
util.gpa.free(output_name); util.gpa.free(output_name);
} }
} }
pub fn apply(self: *const Self, device: *InputDevice) void { pub fn apply(config: *const InputConfig, 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 '{s}' to device '{s}'.", .{ self.glob, device.identifier }); log.debug("applying input configuration '{s}' to device '{s}'.", .{ config.glob, device.identifier });
inline for (@typeInfo(Self).Struct.fields) |field| { inline for (@typeInfo(InputConfig).Struct.fields) |field| {
if (comptime mem.eql(u8, field.name, "glob")) continue; if (comptime mem.eql(u8, field.name, "glob")) continue;
if (comptime mem.eql(u8, field.name, "map-to-output")) { if (comptime mem.eql(u8, field.name, "map-to-output")) {
@field(self, field.name).apply(device); @field(config, field.name).apply(device);
} else if (@field(self, field.name)) |setting| { } else if (@field(config, field.name)) |setting| {
log.debug("applying setting: {s}", .{field.name}); log.debug("applying setting: {s}", .{field.name});
setting.apply(libinput_device); setting.apply(libinput_device);
} }
} }
} }
pub fn parse(self: *Self, setting: []const u8, value: []const u8) !void { pub fn parse(config: *InputConfig, setting: []const u8, value: []const u8) !void {
inline for (@typeInfo(Self).Struct.fields) |field| { inline for (@typeInfo(InputConfig).Struct.fields) |field| {
if (comptime mem.eql(u8, field.name, "glob")) continue; if (comptime mem.eql(u8, field.name, "glob")) continue;
if (mem.eql(u8, setting, field.name)) { if (mem.eql(u8, setting, field.name)) {
// Special-case the settings which are not enums. // Special-case the settings which are not enums.
if (comptime mem.eql(u8, field.name, "pointer-accel")) { if (comptime mem.eql(u8, field.name, "pointer-accel")) {
self.@"pointer-accel" = PointerAccel{ config.@"pointer-accel" = PointerAccel{
.value = math.clamp(try std.fmt.parseFloat(f32, value), -1.0, 1.0), .value = math.clamp(try std.fmt.parseFloat(f32, value), -1.0, 1.0),
}; };
} else if (comptime mem.eql(u8, field.name, "scroll-button")) { } else if (comptime mem.eql(u8, field.name, "scroll-button")) {
const ret = c.libevdev_event_code_from_name(c.EV_KEY, value.ptr); const ret = c.libevdev_event_code_from_name(c.EV_KEY, value.ptr);
if (ret < 1) return error.InvalidButton; if (ret < 1) return error.InvalidButton;
self.@"scroll-button" = ScrollButton{ .button = @intCast(ret) }; config.@"scroll-button" = ScrollButton{ .button = @intCast(ret) };
} else if (comptime mem.eql(u8, field.name, "map-to-output")) { } else if (comptime mem.eql(u8, field.name, "map-to-output")) {
const output_name_owned = blk: { const output_name_owned = blk: {
if (mem.eql(u8, value, "disabled")) { if (mem.eql(u8, value, "disabled")) {
@ -318,14 +318,14 @@ pub fn parse(self: *Self, setting: []const u8, value: []const u8) !void {
} }
}; };
if (self.@"map-to-output".output_name) |old| util.gpa.free(old); if (config.@"map-to-output".output_name) |old| util.gpa.free(old);
self.@"map-to-output" = .{ .output_name = output_name_owned }; config.@"map-to-output" = .{ .output_name = output_name_owned };
} else { } else {
const T = @typeInfo(field.type).Optional.child; const T = @typeInfo(field.type).Optional.child;
if (@typeInfo(T) != .Enum) { if (@typeInfo(T) != .Enum) {
@compileError("You forgot to implement parsing for an input configuration setting."); @compileError("You forgot to implement parsing for an input configuration setting.");
} }
@field(self, field.name) = meta.stringToEnum(T, value) orelse @field(config, field.name) = meta.stringToEnum(T, value) orelse
return error.UnknownOption; return error.UnknownOption;
} }
@ -336,17 +336,17 @@ pub fn parse(self: *Self, setting: []const u8, value: []const u8) !void {
return error.UnknownCommand; return error.UnknownCommand;
} }
pub fn write(self: *Self, writer: anytype) !void { pub fn write(config: *InputConfig, writer: anytype) !void {
try writer.print("{s}\n", .{self.glob}); try writer.print("{s}\n", .{config.glob});
inline for (@typeInfo(Self).Struct.fields) |field| { inline for (@typeInfo(InputConfig).Struct.fields) |field| {
if (comptime mem.eql(u8, field.name, "glob")) continue; if (comptime mem.eql(u8, field.name, "glob")) continue;
if (comptime mem.eql(u8, field.name, "map-to-output")) { if (comptime mem.eql(u8, field.name, "map-to-output")) {
if (@field(self, field.name).output_name) |output_name| { if (@field(config, field.name).output_name) |output_name| {
try writer.print("\tmap-to-output: {s}\n", .{output_name}); try writer.print("\tmap-to-output: {s}\n", .{output_name});
} }
} else if (@field(self, field.name)) |setting| { } else if (@field(config, field.name)) |setting| {
// Special-case the settings which are not enums. // Special-case the settings which are not enums.
if (comptime mem.eql(u8, field.name, "pointer-accel")) { if (comptime mem.eql(u8, field.name, "pointer-accel")) {
try writer.print("\tpointer-accel: {d}\n", .{setting.value}); try writer.print("\tpointer-accel: {d}\n", .{setting.value});