zremap/src/main.zig
2023-08-27 20:32:47 +02:00

75 lines
2.9 KiB
Zig

const std = @import("std");
const print = std.debug.print;
const input = @cImport(@cInclude("linux/input.h"));
const CapsLockDown = input.input_event{ .type = input.EV_KEY, .code = input.KEY_CAPSLOCK, .value = 1, .time = undefined };
const CapsLockUp = input.input_event{ .type = input.EV_KEY, .code = input.KEY_CAPSLOCK, .value = 0, .time = undefined };
const EscDown = input.input_event{ .type = input.EV_KEY, .code = input.KEY_ESC, .value = 1, .time = undefined };
const EscUp = input.input_event{ .type = input.EV_KEY, .code = input.KEY_ESC, .value = 0, .time = undefined };
const LShiftDown = input.input_event{ .type = input.EV_KEY, .code = input.KEY_LEFTSHIFT, .value = 1, .time = undefined };
const LShiftUp = input.input_event{ .type = input.EV_KEY, .code = input.KEY_LEFTSHIFT, .value = 0, .time = undefined };
const RShiftDown = input.input_event{ .type = input.EV_KEY, .code = input.KEY_RIGHTSHIFT, .value = 1, .time = undefined };
const RShiftUp = input.input_event{ .type = input.EV_KEY, .code = input.KEY_RIGHTSHIFT, .value = 0, .time = undefined };
const Syn = input.input_event{ .type = input.EV_SYN, .code = input.SYN_REPORT, .value = 0, .time = undefined };
fn event_equal(e1: *const input.input_event, e2: *const input.input_event) bool {
return (e1.*.type == e2.*.type) and (e1.*.code == e2.*.code) and (e1.*.value == e2.*.value);
}
fn event_read(event: *input.input_event) !void {
event.* = try std.io.getStdIn().reader().readStruct(@TypeOf(event.*));
}
fn event_write(event: *const input.input_event) !void {
try std.io.getStdOut().writer().writeStruct(event.*);
}
fn write_esc() !void {
std.time.sleep(20000);
try event_write(&EscDown);
try event_write(&Syn);
std.time.sleep(20000);
try event_write(&EscUp);
}
pub fn main() !void {
var event: input.input_event = undefined;
var write_esc_lshift: bool = undefined;
var write_esc_rshift: bool = undefined;
var is_lshift_down: bool = false;
var is_rshift_down: bool = false;
while (true) {
try event_read(&event);
if (event.type == input.EV_MSC and event.type == input.MSC_SCAN) {
continue;
}
if (event.type != input.EV_KEY) {
try event_write(&event);
continue;
}
write_esc_lshift = event_equal(&event, &LShiftUp) and is_lshift_down;
write_esc_rshift = event_equal(&event, &RShiftUp) and is_rshift_down;
is_lshift_down = event_equal(&event, &LShiftDown);
is_rshift_down = event_equal(&event, &RShiftDown);
if (event_equal(&event, &CapsLockUp) or event_equal(&event, &CapsLockDown)) {
event.code = input.KEY_LEFTMETA;
}
try event_write(&event);
if (write_esc_lshift) {
write_esc_lshift = false;
try write_esc();
}
if (write_esc_rshift) {
write_esc_rshift = false;
try write_esc();
}
}
}