Fumofumotris/rewrite/src/platform.zig
2024-05-26 07:11:53 -05:00

57 lines
1.3 KiB
Zig

const std = @import("std");
const input = @import("input.zig");
const windows = @cImport({
@cInclude("windows.h");
});
const stdin = windows.GetStdHandle(windows.STD_INPUT_HANDLE);
const PlatformError = error{ReadInputCall};
pub fn ReadInput(
out_buf: *[input.IO_BUF_SIZE]input.Record,
n_out: *usize,
str_buf: *[input.STR_BUF_SIZE]u8,
n_str: *usize,
) !void {
var buf: [input.IO_BUF_SIZE]windows.INPUT_RECORD = undefined;
var n_buf: usize = 0;
const success: bool = windows.ReadConsoleInputW(
stdin,
&buf,
input.IO_BUF_SIZE,
&n_buf,
);
if (!success)
return PlatformError.ReadInputCall;
n_out.* = 0;
for (buf, out_buf) |rec, *out_rec| {}
}
fn dispatch(win_rec: windows.INPUT_RECORD) bool {
switch (win_rec.EventType) {
windows.KEY_EVENT => blk: {
out_rec.* = input.Record{
.scancode = rec.Event.KeyEvent.wVirtualScanCode,
.data = rec.Event.KeyEvent.bKeyDown,
.time = now,
.is_down = rec.Event.KeyEvent.bKeyDown,
};
if (rec.Event.KeyEvent.bKeyDown)
n_str.* += ucs2ToUtf8(str_buf + n_out, rec.Event.KeyEvent.uChar.UnicodeChar);
break :blk true;
},
windows.WINDOW_BUFFER_SIZE_EVENT => false,
else => false,
}
}