commit 8a8d49243e16fe444420000cc4ca1065b08bef4b Author: Asmir A Date: Sun Aug 27 20:32:47 2023 +0200 start project diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..cbc15d1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,38 @@ +# Created by https://www.toptal.com/developers/gitignore/api/zig,vim +# Edit at https://www.toptal.com/developers/gitignore?templates=zig,vim + +### Vim ### +# Swap +[._]*.s[a-v][a-z] +!*.svg # comment out if you don't need vector files +[._]*.sw[a-p] +[._]s[a-rt-v][a-z] +[._]ss[a-gi-z] +[._]sw[a-p] + +# Session +Session.vim +Sessionx.vim + +# Temporary +.netrwhist +*~ +# Auto-generated tag files +tags +# Persistent undo +[._]*.un~ + +### zig ### +# Zig programming language + +zig-cache/ +zig-out/ +build/ +build-*/ +docgen_tmp/ + +### meta ### +run.sh +result + +# End of https://www.toptal.com/developers/gitignore/api/zig,vim diff --git a/build.zig b/build.zig new file mode 100644 index 0000000..0823e63 --- /dev/null +++ b/build.zig @@ -0,0 +1,71 @@ +const std = @import("std"); + +// Although this function looks imperative, note that its job is to +// declaratively construct a build graph that will be executed by an external +// runner. +pub fn build(b: *std.Build) void { + // Standard target options allows the person running `zig build` to choose + // what target to build for. Here we do not override the defaults, which + // means any target is allowed, and the default is native. Other options + // for restricting supported target set are available. + const target = b.standardTargetOptions(.{}); + + // Standard optimization options allow the person running `zig build` to select + // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. Here we do not + // set a preferred release mode, allowing the user to decide how to optimize. + const optimize = b.standardOptimizeOption(.{}); + + var exe = b.addExecutable(.{ + .name = "zremap", + // In this case the main source file is merely a path, however, in more + // complicated build scripts, this could be a generated file. + .root_source_file = .{ .path = "src/main.zig" }, + .target = target, + .optimize = optimize, + }); + exe.linkLibC(); + + // This declares intent for the executable to be installed into the + // standard location when the user invokes the "install" step (the default + // step when running `zig build`). + b.installArtifact(exe); + + // This *creates* a Run step in the build graph, to be executed when another + // step is evaluated that depends on it. The next line below will establish + // such a dependency. + const run_cmd = b.addRunArtifact(exe); + + // By making the run step depend on the install step, it will be run from the + // installation directory rather than directly from within the cache directory. + // This is not necessary, however, if the application depends on other installed + // files, this ensures they will be present and in the expected location. + run_cmd.step.dependOn(b.getInstallStep()); + + // This allows the user to pass arguments to the application in the build + // command itself, like this: `zig build run -- arg1 arg2 etc` + if (b.args) |args| { + run_cmd.addArgs(args); + } + + // This creates a build step. It will be visible in the `zig build --help` menu, + // and can be selected like this: `zig build run` + // This will evaluate the `run` step rather than the default, which is "install". + const run_step = b.step("run", "Run the app"); + run_step.dependOn(&run_cmd.step); + + // Creates a step for unit testing. This only builds the test executable + // but does not run it. + const unit_tests = b.addTest(.{ + .root_source_file = .{ .path = "src/main.zig" }, + .target = target, + .optimize = optimize, + }); + + const run_unit_tests = b.addRunArtifact(unit_tests); + + // Similar to creating the run step earlier, this exposes a `test` step to + // the `zig build --help` menu, providing a way for the user to request + // running the unit tests. + const test_step = b.step("test", "Run unit tests"); + test_step.dependOn(&run_unit_tests.step); +} diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..73c6078 --- /dev/null +++ b/flake.lock @@ -0,0 +1,25 @@ +{ + "nodes": { + "nixpkgs": { + "locked": { + "lastModified": 1693060755, + "narHash": "sha256-KNsbfqewEziFJEpPR0qvVz4rx0x6QXxw1CcunRhlFdk=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "c66ccfa00c643751da2fd9290e096ceaa30493fc", + "type": "github" + }, + "original": { + "id": "nixpkgs", + "type": "indirect" + } + }, + "root": { + "inputs": { + "nixpkgs": "nixpkgs" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..698ed37 --- /dev/null +++ b/flake.nix @@ -0,0 +1,15 @@ +{ + description = "zremap nix flake"; + + outputs = { self, nixpkgs }: { + + defaultPackage.x86_64-linux = + with import nixpkgs { system = "x86_64-linux"; }; + stdenv.mkDerivation { + pname = "zremap"; + version = "0.1"; + src = self; + nativeBuildInputs = [ zig.hook ]; + }; + }; +} diff --git a/src/main.zig b/src/main.zig new file mode 100644 index 0000000..87c3a27 --- /dev/null +++ b/src/main.zig @@ -0,0 +1,74 @@ +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(); + } + } +}