summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/Window.zig12
-rw-r--r--src/gfz.zig1
-rw-r--r--src/input.zig45
3 files changed, 52 insertions, 6 deletions
diff --git a/src/Window.zig b/src/Window.zig
index 74433e8..82ae8c3 100644
--- a/src/Window.zig
+++ b/src/Window.zig
@@ -18,18 +18,20 @@
 
 usingnamespace @import("cimport.zig");
 const Error = gfz.Error;
-const Key = @import("input.zig").Key;
+const Key = input.Key;
+const Mods = input.Mods;
 const Monitor = @import("Monitor.zig");
 const checkError = gfz.checkError;
 const getError = gfz.getError;
 const gfz = @import("gfz.zig");
+const input = @import("input.zig");
 
 const Self = @This();
 const CursorPosFun = fn (window: Self, xpos: f64, ypos: f64) void;
 const KeyFun = fn (window: Self, key: Key, scancode: c_int,
-                   action: c_int, mods: c_int) void;
+                   action: c_int, mods: Mods) void;
 const MouseButtonFun = fn (window: Self, button: c_int,
-                           action: c_int, mods: c_int) void;
+                           action: c_int, mods: Mods) void;
 const SizeFun = fn (window: Self, width: c_int, height: c_int) void;
 const Impl = GLFWwindow;
 pimpl: *Impl,
@@ -47,14 +49,14 @@ fn fnCast(comptime DestType: type, comptime fun: anytype) DestType {
             pub fn callback(window: ?*Impl, key: c_int, scancode: c_int,
                             action: c_int, mods: c_int) callconv(.C) void {
                 fun(@fieldParentPtr(Self, "pimpl", &window.?).*,
-                    @intToEnum(Key, key), scancode, action, mods);
+                    @intToEnum(Key, key), scancode, action, Mods.fromInt(mods));
             }
         },
         GLFWmousebuttonfun => struct {
             pub fn callback(window: ?*Impl, button: c_int,
                             action: c_int, mods: c_int) callconv(.C) void {
                 fun(@fieldParentPtr(Self, "pimpl", &window.?).*,
-                    button, action, mods);
+                    button, action, Mods.fromInt(mods));
             }
         },
         GLFWwindowsizefun => struct {
diff --git a/src/gfz.zig b/src/gfz.zig
index e68e158..31051dd 100644
--- a/src/gfz.zig
+++ b/src/gfz.zig
@@ -132,6 +132,7 @@ pub fn rawMouseMotionSupported() Error!bool {
 
 pub const Window = @import("Window.zig");
 pub const Key = input.Key;
+pub const Mods = input.Mods;
 
 test {
     try init();
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,
+        });
+    }
+};