glfw: enums (#41)

* move key values in an enum

* bitmask for key modifiers

* export Key type and move key fns

instead of exporting the key file, export Key enum.
functions related to the Key type are moved into the enum as well so that they get exported with the type
This commit is contained in:
Aksel Hjerpbakk 2021-10-24 15:23:20 +02:00 committed by GitHub
parent 388f3aa659
commit 1a3f391891
Failed to generate hash of commit
4 changed files with 380 additions and 225 deletions

View file

@ -10,6 +10,7 @@ const getError = @import("errors.zig").getError;
const Image = @import("Image.zig"); const Image = @import("Image.zig");
const Monitor = @import("Monitor.zig"); const Monitor = @import("Monitor.zig");
const Cursor = @import("Cursor.zig"); const Cursor = @import("Cursor.zig");
const Key = @import("key.zig").Key;
const Window = @This(); const Window = @This();
@ -1405,8 +1406,8 @@ pub inline fn setInputMode(self: Window, mode: isize, value: anytype) Error!void
/// @thread_safety This function must only be called from the main thread. /// @thread_safety This function must only be called from the main thread.
/// ///
/// see also: input_key /// see also: input_key
pub inline fn getKey(self: Window, key: isize) Error!bool { pub inline fn getKey(self: Window, key: Key) Error!bool {
const state = c.glfwGetKey(self.handle, @intCast(c_int, key)); const state = c.glfwGetKey(self.handle, @enumToInt(key));
try getError(); try getError();
return state == c.GLFW_PRESS; return state == c.GLFW_PRESS;
} }
@ -2608,7 +2609,7 @@ test "getKey" {
}; };
defer window.destroy(); defer window.destroy();
_ = try window.getKey(glfw.key.escape); _ = try window.getKey(glfw.Key.escape);
} }
test "getMouseButton" { test "getMouseButton" {

View file

@ -20,134 +20,139 @@ const cc = @import("c.zig").c;
const Error = @import("errors.zig").Error; const Error = @import("errors.zig").Error;
const getError = @import("errors.zig").getError; const getError = @import("errors.zig").getError;
/// enum containing all glfw keys
pub const Key = enum(c_int) {
/// The unknown key /// The unknown key
pub const unknown = cc.GLFW_KEY_UNKNOWN; unknown = cc.GLFW_KEY_UNKNOWN,
/// Printable keys /// Printable keys
pub const space = cc.GLFW_KEY_SPACE; space = cc.GLFW_KEY_SPACE,
pub const apostrophe = cc.GLFW_KEY_APOSTROPHE; apostrophe = cc.GLFW_KEY_APOSTROPHE,
pub const comma = cc.GLFW_KEY_COMMA; comma = cc.GLFW_KEY_COMMA,
pub const minus = cc.GLFW_KEY_MINUS; minus = cc.GLFW_KEY_MINUS,
pub const period = cc.GLFW_KEY_PERIOD; period = cc.GLFW_KEY_PERIOD,
pub const slash = cc.GLFW_KEY_SLASH; slash = cc.GLFW_KEY_SLASH,
pub const zero = cc.GLFW_KEY_0; zero = cc.GLFW_KEY_0,
pub const one = cc.GLFW_KEY_1; one = cc.GLFW_KEY_1,
pub const two = cc.GLFW_KEY_2; two = cc.GLFW_KEY_2,
pub const three = cc.GLFW_KEY_3; three = cc.GLFW_KEY_3,
pub const four = cc.GLFW_KEY_4; four = cc.GLFW_KEY_4,
pub const five = cc.GLFW_KEY_5; five = cc.GLFW_KEY_5,
pub const six = cc.GLFW_KEY_6; six = cc.GLFW_KEY_6,
pub const seven = cc.GLFW_KEY_7; seven = cc.GLFW_KEY_7,
pub const eight = cc.GLFW_KEY_8; eight = cc.GLFW_KEY_8,
pub const nine = cc.GLFW_KEY_9; nine = cc.GLFW_KEY_9,
pub const semicolon = cc.GLFW_KEY_SEMICOLON; semicolon = cc.GLFW_KEY_SEMICOLON,
pub const equal = cc.GLFW_KEY_EQUAL; equal = cc.GLFW_KEY_EQUAL,
pub const a = cc.GLFW_KEY_A; a = cc.GLFW_KEY_A,
pub const b = cc.GLFW_KEY_B; b = cc.GLFW_KEY_B,
pub const c = cc.GLFW_KEY_C; c = cc.GLFW_KEY_C,
pub const d = cc.GLFW_KEY_D; d = cc.GLFW_KEY_D,
pub const e = cc.GLFW_KEY_E; e = cc.GLFW_KEY_E,
pub const f = cc.GLFW_KEY_F; f = cc.GLFW_KEY_F,
pub const g = cc.GLFW_KEY_G; g = cc.GLFW_KEY_G,
pub const h = cc.GLFW_KEY_H; h = cc.GLFW_KEY_H,
pub const i = cc.GLFW_KEY_I; i = cc.GLFW_KEY_I,
pub const j = cc.GLFW_KEY_J; j = cc.GLFW_KEY_J,
pub const k = cc.GLFW_KEY_K; k = cc.GLFW_KEY_K,
pub const l = cc.GLFW_KEY_L; l = cc.GLFW_KEY_L,
pub const m = cc.GLFW_KEY_M; m = cc.GLFW_KEY_M,
pub const n = cc.GLFW_KEY_N; n = cc.GLFW_KEY_N,
pub const o = cc.GLFW_KEY_O; o = cc.GLFW_KEY_O,
pub const p = cc.GLFW_KEY_P; p = cc.GLFW_KEY_P,
pub const q = cc.GLFW_KEY_Q; q = cc.GLFW_KEY_Q,
pub const r = cc.GLFW_KEY_R; r = cc.GLFW_KEY_R,
pub const s = cc.GLFW_KEY_S; s = cc.GLFW_KEY_S,
pub const t = cc.GLFW_KEY_T; t = cc.GLFW_KEY_T,
pub const u = cc.GLFW_KEY_U; u = cc.GLFW_KEY_U,
pub const v = cc.GLFW_KEY_V; v = cc.GLFW_KEY_V,
pub const w = cc.GLFW_KEY_W; w = cc.GLFW_KEY_W,
pub const x = cc.GLFW_KEY_X; x = cc.GLFW_KEY_X,
pub const y = cc.GLFW_KEY_Y; y = cc.GLFW_KEY_Y,
pub const z = cc.GLFW_KEY_Z; z = cc.GLFW_KEY_Z,
pub const left_bracket = cc.GLFW_KEY_LEFT_BRACKET; left_bracket = cc.GLFW_KEY_LEFT_BRACKET,
pub const backslash = cc.GLFW_KEY_BACKSLASH; backslash = cc.GLFW_KEY_BACKSLASH,
pub const right_bracket = cc.GLFW_KEY_RIGHT_BRACKET; right_bracket = cc.GLFW_KEY_RIGHT_BRACKET,
pub const grave_accent = cc.GLFW_KEY_GRAVE_ACCENT; grave_accent = cc.GLFW_KEY_GRAVE_ACCENT,
pub const world_1 = cc.GLFW_KEY_WORLD_1; // non-US #1 world_1 = cc.GLFW_KEY_WORLD_1, // non-US #1
pub const world_2 = cc.GLFW_KEY_WORLD_2; // non-US #2 world_2 = cc.GLFW_KEY_WORLD_2, // non-US #2
/// Function keys // Function keys
pub const escape = cc.GLFW_KEY_ESCAPE; escape = cc.GLFW_KEY_ESCAPE,
pub const enter = cc.GLFW_KEY_ENTER; enter = cc.GLFW_KEY_ENTER,
pub const tab = cc.GLFW_KEY_TAB; tab = cc.GLFW_KEY_TAB,
pub const backspace = cc.GLFW_KEY_BACKSPACE; backspace = cc.GLFW_KEY_BACKSPACE,
pub const insert = cc.GLFW_KEY_INSERT; insert = cc.GLFW_KEY_INSERT,
pub const delete = cc.GLFW_KEY_DELETE; delete = cc.GLFW_KEY_DELETE,
pub const right = cc.GLFW_KEY_RIGHT; right = cc.GLFW_KEY_RIGHT,
pub const left = cc.GLFW_KEY_LEFT; left = cc.GLFW_KEY_LEFT,
pub const down = cc.GLFW_KEY_DOWN; down = cc.GLFW_KEY_DOWN,
pub const up = cc.GLFW_KEY_UP; up = cc.GLFW_KEY_UP,
pub const page_up = cc.GLFW_KEY_PAGE_UP; page_up = cc.GLFW_KEY_PAGE_UP,
pub const page_down = cc.GLFW_KEY_PAGE_DOWN; page_down = cc.GLFW_KEY_PAGE_DOWN,
pub const home = cc.GLFW_KEY_HOME; home = cc.GLFW_KEY_HOME,
pub const end = cc.GLFW_KEY_END; end = cc.GLFW_KEY_END,
pub const caps_lock = cc.GLFW_KEY_CAPS_LOCK; caps_lock = cc.GLFW_KEY_CAPS_LOCK,
pub const scroll_lock = cc.GLFW_KEY_SCROLL_LOCK; scroll_lock = cc.GLFW_KEY_SCROLL_LOCK,
pub const num_lock = cc.GLFW_KEY_NUM_LOCK; num_lock = cc.GLFW_KEY_NUM_LOCK,
pub const print_screen = cc.GLFW_KEY_PRINT_SCREEN; print_screen = cc.GLFW_KEY_PRINT_SCREEN,
pub const pause = cc.GLFW_KEY_PAUSE; pause = cc.GLFW_KEY_PAUSE,
pub const F1 = cc.GLFW_KEY_F1; F1 = cc.GLFW_KEY_F1,
pub const F2 = cc.GLFW_KEY_F2; F2 = cc.GLFW_KEY_F2,
pub const F3 = cc.GLFW_KEY_F3; F3 = cc.GLFW_KEY_F3,
pub const F4 = cc.GLFW_KEY_F4; F4 = cc.GLFW_KEY_F4,
pub const F5 = cc.GLFW_KEY_F5; F5 = cc.GLFW_KEY_F5,
pub const F6 = cc.GLFW_KEY_F6; F6 = cc.GLFW_KEY_F6,
pub const F7 = cc.GLFW_KEY_F7; F7 = cc.GLFW_KEY_F7,
pub const F8 = cc.GLFW_KEY_F8; F8 = cc.GLFW_KEY_F8,
pub const F9 = cc.GLFW_KEY_F9; F9 = cc.GLFW_KEY_F9,
pub const F10 = cc.GLFW_KEY_F10; F10 = cc.GLFW_KEY_F10,
pub const F11 = cc.GLFW_KEY_F11; F11 = cc.GLFW_KEY_F11,
pub const F12 = cc.GLFW_KEY_F12; F12 = cc.GLFW_KEY_F12,
pub const F13 = cc.GLFW_KEY_F13; F13 = cc.GLFW_KEY_F13,
pub const F14 = cc.GLFW_KEY_F14; F14 = cc.GLFW_KEY_F14,
pub const F15 = cc.GLFW_KEY_F15; F15 = cc.GLFW_KEY_F15,
pub const F16 = cc.GLFW_KEY_F16; F16 = cc.GLFW_KEY_F16,
pub const F17 = cc.GLFW_KEY_F17; F17 = cc.GLFW_KEY_F17,
pub const F18 = cc.GLFW_KEY_F18; F18 = cc.GLFW_KEY_F18,
pub const F19 = cc.GLFW_KEY_F19; F19 = cc.GLFW_KEY_F19,
pub const F20 = cc.GLFW_KEY_F20; F20 = cc.GLFW_KEY_F20,
pub const F21 = cc.GLFW_KEY_F21; F21 = cc.GLFW_KEY_F21,
pub const F22 = cc.GLFW_KEY_F22; F22 = cc.GLFW_KEY_F22,
pub const F23 = cc.GLFW_KEY_F23; F23 = cc.GLFW_KEY_F23,
pub const F24 = cc.GLFW_KEY_F24; F24 = cc.GLFW_KEY_F24,
pub const F25 = cc.GLFW_KEY_F25; F25 = cc.GLFW_KEY_F25,
pub const kp_0 = cc.GLFW_KEY_KP_0; kp_0 = cc.GLFW_KEY_KP_0,
pub const kp_1 = cc.GLFW_KEY_KP_1; kp_1 = cc.GLFW_KEY_KP_1,
pub const kp_2 = cc.GLFW_KEY_KP_2; kp_2 = cc.GLFW_KEY_KP_2,
pub const kp_3 = cc.GLFW_KEY_KP_3; kp_3 = cc.GLFW_KEY_KP_3,
pub const kp_4 = cc.GLFW_KEY_KP_4; kp_4 = cc.GLFW_KEY_KP_4,
pub const kp_5 = cc.GLFW_KEY_KP_5; kp_5 = cc.GLFW_KEY_KP_5,
pub const kp_6 = cc.GLFW_KEY_KP_6; kp_6 = cc.GLFW_KEY_KP_6,
pub const kp_7 = cc.GLFW_KEY_KP_7; kp_7 = cc.GLFW_KEY_KP_7,
pub const kp_8 = cc.GLFW_KEY_KP_8; kp_8 = cc.GLFW_KEY_KP_8,
pub const kp_9 = cc.GLFW_KEY_KP_9; kp_9 = cc.GLFW_KEY_KP_9,
pub const kp_decimal = cc.GLFW_KEY_KP_DECIMAL; kp_decimal = cc.GLFW_KEY_KP_DECIMAL,
pub const kp_divide = cc.GLFW_KEY_KP_DIVIDE; kp_divide = cc.GLFW_KEY_KP_DIVIDE,
pub const kp_multiply = cc.GLFW_KEY_KP_MULTIPLY; kp_multiply = cc.GLFW_KEY_KP_MULTIPLY,
pub const kp_subtract = cc.GLFW_KEY_KP_SUBTRACT; kp_subtract = cc.GLFW_KEY_KP_SUBTRACT,
pub const kp_add = cc.GLFW_KEY_KP_ADD; kp_add = cc.GLFW_KEY_KP_ADD,
pub const kp_enter = cc.GLFW_KEY_KP_ENTER; kp_enter = cc.GLFW_KEY_KP_ENTER,
pub const kp_equal = cc.GLFW_KEY_KP_EQUAL; kp_equal = cc.GLFW_KEY_KP_EQUAL,
pub const left_shift = cc.GLFW_KEY_LEFT_SHIFT; left_shift = cc.GLFW_KEY_LEFT_SHIFT,
pub const left_control = cc.GLFW_KEY_LEFT_CONTROL; left_control = cc.GLFW_KEY_LEFT_CONTROL,
pub const left_alt = cc.GLFW_KEY_LEFT_ALT; left_alt = cc.GLFW_KEY_LEFT_ALT,
pub const left_super = cc.GLFW_KEY_LEFT_SUPER; left_super = cc.GLFW_KEY_LEFT_SUPER,
pub const right_shift = cc.GLFW_KEY_RIGHT_SHIFT; right_shift = cc.GLFW_KEY_RIGHT_SHIFT,
pub const right_control = cc.GLFW_KEY_RIGHT_CONTROL; right_control = cc.GLFW_KEY_RIGHT_CONTROL,
pub const right_alt = cc.GLFW_KEY_RIGHT_ALT; right_alt = cc.GLFW_KEY_RIGHT_ALT,
pub const right_super = cc.GLFW_KEY_RIGHT_SUPER; right_super = cc.GLFW_KEY_RIGHT_SUPER,
pub const menu = cc.GLFW_KEY_MENU; menu = cc.GLFW_KEY_MENU,
pub inline fn last() Key {
return @intToEnum(Key, cc.GLFW_KEY_LAST);
}
pub const last = cc.GLFW_KEY_LAST;
/// Returns the layout-specific name of the specified printable key. /// Returns the layout-specific name of the specified printable key.
/// ///
@ -168,27 +173,27 @@ pub const last = cc.GLFW_KEY_LAST;
/// ///
/// The printable keys are: /// The printable keys are:
/// ///
/// - `glfw.key.apostrophe` /// - `glfw.Key.apostrophe`
/// - `glfw.key.comma` /// - `glfw.Key.comma`
/// - `glfw.key.minus` /// - `glfw.Key.minus`
/// - `glfw.key.period` /// - `glfw.Key.period`
/// - `glfw.key.slash` /// - `glfw.Key.slash`
/// - `glfw.key.semicolon` /// - `glfw.Key.semicolon`
/// - `glfw.key.equal` /// - `glfw.Key.equal`
/// - `glfw.key.left_bracket` /// - `glfw.Key.left_bracket`
/// - `glfw.key.right_bracket` /// - `glfw.Key.right_bracket`
/// - `glfw.key.backslash` /// - `glfw.Key.backslash`
/// - `glfw.key.world_1` /// - `glfw.Key.world_1`
/// - `glfw.key.world_2` /// - `glfw.Key.world_2`
/// - `glfw.key.0` to `glfw.key.9` /// - `glfw.Key.0` to `glfw.key.9`
/// - `glfw.key.a` to `glfw.key.z` /// - `glfw.Key.a` to `glfw.key.z`
/// - `glfw.key.kp_0` to `glfw.key.kp_9` /// - `glfw.Key.kp_0` to `glfw.key.kp_9`
/// - `glfw.key.kp_decimal` /// - `glfw.Key.kp_decimal`
/// - `glfw.key.kp_divide` /// - `glfw.Key.kp_divide`
/// - `glfw.key.kp_multiply` /// - `glfw.Key.kp_multiply`
/// - `glfw.key.kp_subtract` /// - `glfw.Key.kp_subtract`
/// - `glfw.key.kp_add` /// - `glfw.Key.kp_add`
/// - `glfw.key.kp_equal` /// - `glfw.Key.kp_equal`
/// ///
/// Names for printable keys depend on keyboard layout, while names for non-printable keys are the /// Names for printable keys depend on keyboard layout, while names for non-printable keys are the
/// same across layouts but depend on the application language and should be localized along with /// same across layouts but depend on the application language and should be localized along with
@ -208,8 +213,8 @@ pub const last = cc.GLFW_KEY_LAST;
/// @thread_safety This function must only be called from the main thread. /// @thread_safety This function must only be called from the main thread.
/// ///
/// see also: input_key_name /// see also: input_key_name
pub inline fn getName(key: isize, scancode: isize) Error![*c]const u8 { pub inline fn getName(self: Key, scancode: isize) Error![*c]const u8 {
const name = cc.glfwGetKeyName(@intCast(c_int, key), @intCast(c_int, scancode)); const name = cc.glfwGetKeyName(@enumToInt(self), @intCast(c_int, scancode));
try getError(); try getError();
return name; return name;
} }
@ -226,18 +231,20 @@ pub inline fn getName(key: isize, scancode: isize) Error![*c]const u8 {
/// Possible errors include glfw.Error.NotInitialized, glfw.Error.InvalidEnum and glfw.Error.PlatformError. /// Possible errors include glfw.Error.NotInitialized, glfw.Error.InvalidEnum and glfw.Error.PlatformError.
/// ///
/// @thread_safety This function may be called from any thread. /// @thread_safety This function may be called from any thread.
pub inline fn getScancode(key: isize) Error!isize { pub inline fn getScancode(self: Key) Error!isize {
const scancode = cc.glfwGetKeyScancode(@intCast(c_int, key)); const scancode = cc.glfwGetKeyScancode(@enumToInt(self));
try getError(); try getError();
return scancode; return scancode;
} }
};
test "getName" { test "getName" {
const glfw = @import("main.zig"); const glfw = @import("main.zig");
try glfw.init(); try glfw.init();
defer glfw.terminate(); defer glfw.terminate();
_ = glfw.key.getName(glfw.key.a, 0) catch |err| std.debug.print("failed to get key name, not supported? error={}\n", .{err}); _ = glfw.Key.a.getName(0) catch |err| std.debug.print("failed to get key name, not supported? error={}\n", .{err});
} }
test "getScancode" { test "getScancode" {
@ -245,5 +252,5 @@ test "getScancode" {
try glfw.init(); try glfw.init();
defer glfw.terminate(); defer glfw.terminate();
_ = glfw.key.getScancode(glfw.key.a) catch |err| std.debug.print("failed to get key scancode, not supported? error={}\n", .{err}); _ = glfw.Key.a.getScancode() catch |err| std.debug.print("failed to get key scancode, not supported? error={}\n", .{err});
} }

View file

@ -3,6 +3,8 @@ const testing = std.testing;
const c = @import("c.zig").c; const c = @import("c.zig").c;
const key = @import("key.zig");
pub usingnamespace @import("consts.zig"); pub usingnamespace @import("consts.zig");
pub const Error = @import("errors.zig").Error; pub const Error = @import("errors.zig").Error;
const getError = @import("errors.zig").getError; const getError = @import("errors.zig").getError;
@ -14,7 +16,6 @@ pub const GammaRamp = @import("GammaRamp.zig");
pub const hat = @import("hat.zig"); pub const hat = @import("hat.zig");
pub const Image = @import("Image.zig"); pub const Image = @import("Image.zig");
pub const Joystick = @import("Joystick.zig"); pub const Joystick = @import("Joystick.zig");
pub const key = @import("key.zig");
pub const mod = @import("mod.zig"); pub const mod = @import("mod.zig");
pub const Monitor = @import("Monitor.zig"); pub const Monitor = @import("Monitor.zig");
pub const mouse_button = @import("mouse_button.zig"); pub const mouse_button = @import("mouse_button.zig");
@ -22,6 +23,7 @@ pub const version = @import("version.zig");
pub const VideoMode = @import("VideoMode.zig"); pub const VideoMode = @import("VideoMode.zig");
pub const Window = @import("Window.zig"); pub const Window = @import("Window.zig");
pub const Cursor = @import("Cursor.zig"); pub const Cursor = @import("Cursor.zig");
pub const Key = key.Key;
pub usingnamespace @import("clipboard.zig"); pub usingnamespace @import("clipboard.zig");
pub usingnamespace @import("opengl.zig"); pub usingnamespace @import("opengl.zig");

View file

@ -4,6 +4,38 @@
const c = @import("c.zig").c; const c = @import("c.zig").c;
// must be in sync with https://www.glfw.org/docs/3.3/group__mods.html
/// A bitmask of all key modifiers
pub const Mods = packed struct {
shift: bool align(@alignOf(c_int)) = false,
control: bool = false,
alt: bool = false,
super: bool = false,
caps_lock: bool = false,
num_lock: bool = false,
inline fn verifyIntType(comptime IntType: type) void {
comptime {
switch (@typeInfo(IntType)) {
.Int => {},
else => @compileError("Int was not of int type"),
}
}
}
pub inline fn toInt(comptime IntType: type, self: Mods) IntType {
verifyIntType(IntType);
return @bitCast(IntType, self);
}
pub inline fn fromInt(flags: anytype) Mods {
verifyIntType(@TypeOf(flags));
return @bitCast(Mods, flags);
}
};
/// Hold all glfw values as is
pub const RawMods = struct {
/// If this bit is set one or more Shift keys were held down. /// If this bit is set one or more Shift keys were held down.
pub const shift = c.GLFW_MOD_SHIFT; pub const shift = c.GLFW_MOD_SHIFT;
@ -21,3 +53,116 @@ pub const caps_lock = c.GLFW_MOD_CAPS_LOCK;
/// If this bit is set the Num Lock key is enabled and the glfw.lock_key_mods input mode is set. /// If this bit is set the Num Lock key is enabled and the glfw.lock_key_mods input mode is set.
pub const num_lock = c.GLFW_MOD_NUM_LOCK; pub const num_lock = c.GLFW_MOD_NUM_LOCK;
};
test "shift int to bitmask" {
const std = @import("std");
const int_mod = RawMods.shift;
const mod = Mods.fromInt(int_mod);
std.testing.expect(mod.shift == true);
std.testing.expect(mod.control == false);
std.testing.expect(mod.alt == false);
std.testing.expect(mod.super == false);
std.testing.expect(mod.caps_lock == false);
std.testing.expect(mod.num_lock == false);
}
test "shift int and alt to bitmask" {
const std = @import("std");
const int_mod = RawMods.shift | RawMods.alt;
const mod = Mods.fromInt(int_mod);
std.testing.expect(mod.shift == true);
std.testing.expect(mod.control == false);
std.testing.expect(mod.alt == true);
std.testing.expect(mod.super == false);
std.testing.expect(mod.caps_lock == false);
std.testing.expect(mod.num_lock == false);
}
test "super int to bitmask" {
const std = @import("std");
const int_mod = RawMods.super;
const mod = Mods.fromInt(int_mod);
std.testing.expect(mod.shift == false);
std.testing.expect(mod.control == false);
std.testing.expect(mod.alt == false);
std.testing.expect(mod.super == true);
std.testing.expect(mod.caps_lock == false);
std.testing.expect(mod.num_lock == false);
}
test "num lock int to bitmask" {
const std = @import("std");
const int_mod = RawMods.num_lock;
const mod = Mods.fromInt(int_mod);
std.testing.expect(mod.shift == false);
std.testing.expect(mod.control == false);
std.testing.expect(mod.alt == false);
std.testing.expect(mod.super == false);
std.testing.expect(mod.caps_lock == false);
std.testing.expect(mod.num_lock == true);
}
test "all int to bitmask" {
const std = @import("std");
const int_mod = RawMods.shift | RawMods.control |
RawMods.alt | RawMods.super |
RawMods.caps_lock | RawMods.num_lock;
const mod = Mods.fromInt(int_mod);
std.testing.expect(mod.shift == true);
std.testing.expect(mod.control == true);
std.testing.expect(mod.alt == true);
std.testing.expect(mod.super == true);
std.testing.expect(mod.caps_lock == true);
std.testing.expect(mod.num_lock == true);
}
test "shift bitmask to int" {
const std = @import("std");
const mod = Mods{ .shift = true };
const int_mod = mod.toInt(c_int);
std.testing.expectEqual(int_mod, RawMods.shift);
}
test "shift and alt bitmask to int" {
const std = @import("std");
const mod = Mods{ .shift = true, .alt = true };
const int_mod = mod.toInt(c_int);
std.testing.expectEqual(int_mod, RawMods.shift | RawMods.alt);
}
test "all bitmask to int" {
const std = @import("std");
const mod = Mods{
.shift = true,
.control = true,
.alt = true,
.super = true,
.caps_lock = true,
.num_lock = true,
};
const int_mod = mod.toInt(c_int);
const expected = RawMods.shift | RawMods.control |
RawMods.alt | RawMods.super |
RawMods.caps_lock | RawMods.num_lock;
std.testing.expectEqual(int_mod, expected);
}