diff options
Diffstat (limited to 'src/input.zig')
-rw-r--r-- | src/input.zig | 45 |
1 files changed, 44 insertions, 1 deletions
diff --git a/src/input.zig b/src/input.zig index 172f965..ef99038 100644 --- a/src/input.zig +++ b/src/input.zig @@ -16,8 +16,10 @@ // You should have received a copy of the GNU Lesser General Public License // along with gfz. If not, see <https://www.gnu.org/licenses/>. +const Int = std.meta.Int; const EnumField = TypeInfo.EnumField; const TypeInfo = std.builtin.TypeInfo; +const endian = @import("builtin").target.cpu.arch.endian(); const glfw = @import("cimport.zig"); const std = @import("std"); @@ -43,7 +45,7 @@ const keys = [_][]const u8{ "RIGHT_SHIFT", "RIGHT_CONTROL", "RIGHT_ALT", "RIGHT_SUPER", "MENU", }; -/// Keyboard key enumeration +/// Keyboard key enumeration: https://www.glfw.org/docs/latest/group__keys.html pub const Key = @Type(TypeInfo{ .Enum = .{ .layout = .Auto, @@ -61,3 +63,44 @@ pub const Key = @Type(TypeInfo{ .is_exhaustive = true, } }); + +/// Modifier key flags: https://www.glfw.org/docs/latest/group__mods.html +pub const Mods = packed struct { + /// One or more Shift keys are held down. + shift: bool, + /// One or more Control keys are held down. + control: bool, + /// One or more Alt keys are held down. + alt: bool, + /// One or more Super keys are held down. + super: bool, + /// The Caps Lock key is enabled and InputMode.lock_key_mods is set. + caps_lock: bool, + /// The Num Lock key is enabled and InputMode.lock_key_mods is set. + num_lock: bool, + + // @bitCast will not work directly with an integer + // due to alignment: https://github.com/ziglang/zig/issues/8102 + const MaskInt = Int(.unsigned, @bitSizeOf(Mods)); + const Mask = packed struct { integer: MaskInt }; + + /// Convert c_int to Mods. + pub fn fromInt(mask: c_int) Mods { + const integer = @intCast(MaskInt, mask); + return @bitCast(Mods, Mask{ + .integer = switch (endian) { + .Big => @bitReverse(MaskInt, integer), + .Little => integer, + }, + }); + } + + /// Convert Mods to c_int. + pub fn toInt(self: Mods) c_int { + const integer = @bitCast(Mask, self).integer; + return @intCast(c_int, switch (endian) { + .Big => @bitReverse(MaskInt, integer), + .Little => integer, + }); + } +}; |