From aec342918b240536958cf2ee26852d03ffe1cffe Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sun, 13 Aug 2023 15:38:30 -0700 Subject: [PATCH] input: various more helpers --- src/input/function_keys.zig | 38 +++++++++++++++++++------------------ src/input/key.zig | 18 ++++++++++++++++++ 2 files changed, 38 insertions(+), 18 deletions(-) diff --git a/src/input/function_keys.zig b/src/input/function_keys.zig index 0f077d092..52dfe75bf 100644 --- a/src/input/function_keys.zig +++ b/src/input/function_keys.zig @@ -45,6 +45,26 @@ pub const Entry = struct { sequence: []const u8, }; +/// The list of modifier combinations for modify other key sequences. +/// The mode value is index + 2. +pub const modifiers: []const key.Mods = &.{ + .{ .shift = true }, + .{ .alt = true }, + .{ .shift = true, .alt = true }, + .{ .ctrl = true }, + .{ .shift = true, .ctrl = true }, + .{ .alt = true, .ctrl = true }, + .{ .shift = true, .alt = true, .ctrl = true }, + .{ .super = true }, + .{ .shift = true, .super = true }, + .{ .alt = true, .super = true }, + .{ .shift = true, .alt = true, .super = true }, + .{ .ctrl = true, .super = true }, + .{ .shift = true, .ctrl = true, .super = true }, + .{ .alt = true, .ctrl = true, .super = true }, + .{ .shift = true, .alt = true, .ctrl = true, .super = true }, +}; + /// This is the array of entries for the PC style function keys as mapped to /// our set of possible key codes. Not every key has any entries. pub const KeyEntryArray = std.EnumArray(key.Key, []const Entry); @@ -239,24 +259,6 @@ fn pcStyle(comptime fmt: []const u8) []const Entry { // The comptime {} wrapper is superflous but it prevents us from // accidentally running this function at runtime. comptime { - const modifiers: []const key.Mods = &.{ - .{ .shift = true }, - .{ .alt = true }, - .{ .shift = true, .alt = true }, - .{ .ctrl = true }, - .{ .shift = true, .ctrl = true }, - .{ .alt = true, .ctrl = true }, - .{ .shift = true, .alt = true, .ctrl = true }, - .{ .super = true }, - .{ .shift = true, .super = true }, - .{ .alt = true, .super = true }, - .{ .shift = true, .alt = true, .super = true }, - .{ .ctrl = true, .super = true }, - .{ .shift = true, .ctrl = true, .super = true }, - .{ .alt = true, .ctrl = true, .super = true }, - .{ .shift = true, .alt = true, .ctrl = true, .super = true }, - }; - var entries: [modifiers.len]Entry = undefined; for (modifiers, 2.., 0..) |mods, code, i| { entries[i] = .{ diff --git a/src/input/key.zig b/src/input/key.zig index 6ef6ae38d..2d951b996 100644 --- a/src/input/key.zig +++ b/src/input/key.zig @@ -14,6 +14,24 @@ pub const Mods = packed struct(u8) { num_lock: bool = false, _padding: u2 = 0, + /// Returns true if no modifiers are set. + pub fn empty(self: Mods) bool { + return @as(u8, @bitCast(self)) == 0; + } + + /// Returns true if two mods are equal. + pub fn equal(self: Mods, other: Mods) bool { + return @as(u8, @bitCast(self)) == @as(u8, @bitCast(other)); + } + + /// Returns the mods without locks set. + pub fn withoutLocks(self: Mods) Mods { + var copy = self; + copy.caps_lock = false; + copy.num_lock = false; + return copy; + } + // For our own understanding test { const testing = std.testing;