Compare commits
No commits in common. "9043844893cfc333ed04d04c1d3b67d9904c0afc" and "df0c3657b46ef1f40ca9aecad09ebde27cd7987d" have entirely different histories.
9043844893
...
df0c3657b4
2
debug.sh
2
debug.sh
@ -1,2 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
lldb -- ./zig-out/bin/zremap /dev/input/by-path/platform-i8042-serio-0-event-kbd
|
6
flake.lock
generated
6
flake.lock
generated
@ -2,11 +2,11 @@
|
||||
"nodes": {
|
||||
"nixpkgs": {
|
||||
"locked": {
|
||||
"lastModified": 1696419054,
|
||||
"narHash": "sha256-EdR+dIKCfqL3voZUDYwcvgRDOektQB9KbhBVcE0/3Mo=",
|
||||
"lastModified": 1693060755,
|
||||
"narHash": "sha256-KNsbfqewEziFJEpPR0qvVz4rx0x6QXxw1CcunRhlFdk=",
|
||||
"owner": "NixOS",
|
||||
"repo": "nixpkgs",
|
||||
"rev": "7131f3c223a2d799568e4b278380cd9dac2b8579",
|
||||
"rev": "c66ccfa00c643751da2fd9290e096ceaa30493fc",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
|
@ -11,22 +11,17 @@
|
||||
version = "0.1";
|
||||
src = self;
|
||||
nativeBuildInputs = [zig.hook];
|
||||
buildInputs = [libevdev pkg-config];
|
||||
buildPhase = ''
|
||||
NIX_CFLAGS_COMPILE="-isystem $(pkg-config --variable=includedir libevdev)/libevdev-1.0 $NIX_CFLAGS_COMPILE"
|
||||
'';
|
||||
buildInputs = [libevdev];
|
||||
};
|
||||
|
||||
devShells.x86_64-linux.default = with import nixpkgs {system = "x86_64-linux";};
|
||||
mkShell {
|
||||
nativeBuildInputs = [zig lldb];
|
||||
buildInputs = [
|
||||
libevdev
|
||||
pkg-config
|
||||
zig
|
||||
];
|
||||
|
||||
shellHook = ''
|
||||
NIX_CFLAGS_COMPILE="-isystem $(pkg-config --variable=includedir libevdev)/libevdev-1.0 $NIX_CFLAGS_COMPILE"
|
||||
echo "happy hacking!"
|
||||
'';
|
||||
};
|
||||
|
74
src/main.zig
74
src/main.zig
@ -1,59 +1,42 @@
|
||||
const std = @import("std");
|
||||
const c = @cImport({
|
||||
@cInclude("linux/input.h");
|
||||
@cInclude("libevdev/libevdev.h");
|
||||
@cInclude("libevdev/libevdev-uinput.h");
|
||||
@cInclude("libevdev-1.0/libevdev/libevdev.h");
|
||||
@cInclude("errno.h");
|
||||
});
|
||||
|
||||
const CapsLockDown = c.input_event{ .type = c.EV_KEY, .code = c.KEY_CAPSLOCK, .value = 1, .time = undefined };
|
||||
const CapsLockUp = c.input_event{ .type = c.EV_KEY, .code = c.KEY_CAPSLOCK, .value = 0, .time = undefined };
|
||||
const EscDown = c.input_event{ .type = c.EV_KEY, .code = c.KEY_ESC, .value = 1, .time = undefined };
|
||||
const EscUp = c.input_event{ .type = c.EV_KEY, .code = c.KEY_ESC, .value = 0, .time = undefined };
|
||||
const LShiftDown = c.input_event{ .type = c.EV_KEY, .code = c.KEY_LEFTSHIFT, .value = 1, .time = undefined };
|
||||
const LShiftUp = c.input_event{ .type = c.EV_KEY, .code = c.KEY_LEFTSHIFT, .value = 0, .time = undefined };
|
||||
const RShiftDown = c.input_event{ .type = c.EV_KEY, .code = c.KEY_RIGHTSHIFT, .value = 1, .time = undefined };
|
||||
const RShiftUp = c.input_event{ .type = c.EV_KEY, .code = c.KEY_RIGHTSHIFT, .value = 0, .time = undefined };
|
||||
const Syn = c.input_event{ .type = c.EV_SYN, .code = c.SYN_REPORT, .value = 0, .time = undefined };
|
||||
|
||||
fn event_equal(e1: *const c.input_event, e2: *const c.input_event) bool {
|
||||
return (e1.type == e2.type) and (e1.code == e2.code) and (e1.value == e2.value);
|
||||
return (e1.*.type == e2.*.type) and (e1.*.code == e2.*.code) and (e1.*.value == e2.*.value);
|
||||
}
|
||||
|
||||
fn inputDeviceOpenAndGrab(dev_name: []const u8) !?*c.libevdev {
|
||||
const dev_fd = try std.fs.openFileAbsolute(dev_name, .{});
|
||||
const dev = c.libevdev_new();
|
||||
if (c.libevdev_set_fd(dev, dev_fd.handle) < 0) {
|
||||
return error.libevdevCreateFail;
|
||||
}
|
||||
if (c.libevdev_grab(dev, c.LIBEVDEV_GRAB) < 0) {
|
||||
return error.libevdevGrabFail;
|
||||
}
|
||||
return dev;
|
||||
//fn event_read(event: *c.input_event) !void {
|
||||
// event.* = try std.io.getStdIn().reader().readStruct(@TypeOf(event.*));
|
||||
//}
|
||||
|
||||
fn event_write(event: *const c.input_event) !void {
|
||||
try std.io.getStdOut().writer().writeStruct(event.*);
|
||||
}
|
||||
|
||||
fn uinputCreate(dev: *c.libevdev) !?*c.libevdev_uinput {
|
||||
var uinput: *align(8) c.libevdev_uinput = undefined;
|
||||
if (c.libevdev_uinput_create_from_device(@ptrCast(dev), c.LIBEVDEV_UINPUT_OPEN_MANAGED, @ptrCast(&uinput)) < 0) {
|
||||
return error.libevdevUinputCreateFail;
|
||||
}
|
||||
return uinput;
|
||||
}
|
||||
|
||||
fn uinputWrite(uinput_dev: *c.libevdev_uinput, event: *const c.input_event) !void {
|
||||
if (c.libevdev_uinput_write_event(@ptrCast(uinput_dev), event.type, event.code, event.value) < 0) {
|
||||
return error.writeFailed;
|
||||
}
|
||||
}
|
||||
|
||||
fn uinputWriteKey(uinput_dev: *c.libevdev_uinput, key: u16) !void {
|
||||
const pressKey = c.input_event{ .type = c.EV_KEY, .code = key, .value = 1, .time = undefined };
|
||||
const releaseKey = c.input_event{ .type = c.EV_KEY, .code = key, .value = 0, .time = undefined };
|
||||
const sync = c.input_event{ .type = c.EV_SYN, .code = c.SYN_REPORT, .value = 0, .time = undefined };
|
||||
fn write_esc() !void {
|
||||
std.time.sleep(20000);
|
||||
try uinputWrite(uinput_dev, &pressKey);
|
||||
try uinputWrite(uinput_dev, &sync);
|
||||
try event_write(&EscDown);
|
||||
try event_write(&Syn);
|
||||
std.time.sleep(20000);
|
||||
try uinputWrite(uinput_dev, &releaseKey);
|
||||
try event_write(&EscUp);
|
||||
}
|
||||
|
||||
fn device_open() !void {}
|
||||
|
||||
pub fn main() !void {
|
||||
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
||||
const allocator = gpa.allocator();
|
||||
@ -66,8 +49,17 @@ pub fn main() !void {
|
||||
std.os.exit(1);
|
||||
}
|
||||
const dev_name = args[1];
|
||||
const dev = try inputDeviceOpenAndGrab(dev_name);
|
||||
const uinput_dev = try uinputCreate(dev.?);
|
||||
var dev_fd = try std.fs.openFileAbsolute(dev_name, .{});
|
||||
|
||||
const dev = c.libevdev_new();
|
||||
if (c.libevdev_set_fd(dev, dev_fd.handle) < 0) {
|
||||
std.debug.print("evdev: failed creating device from descriptor", .{});
|
||||
std.os.exit(1);
|
||||
}
|
||||
if (c.libevdev_grab(dev, c.LIBEVDEV_GRAB) < 0) {
|
||||
std.debug.print("evdev: failed grabbing device", .{});
|
||||
std.os.exit(1);
|
||||
}
|
||||
|
||||
var write_esc_lshift: bool = undefined;
|
||||
var write_esc_rshift: bool = undefined;
|
||||
@ -90,12 +82,12 @@ pub fn main() !void {
|
||||
break :outer;
|
||||
}
|
||||
|
||||
if (event.type == c.EV_MSC and event.code == c.MSC_SCAN) {
|
||||
if (event.type == c.EV_MSC and event.type == c.MSC_SCAN) {
|
||||
continue :outer;
|
||||
}
|
||||
|
||||
if (event.type != c.EV_KEY) {
|
||||
try uinputWrite(uinput_dev.?, &event);
|
||||
try event_write(&event);
|
||||
continue :outer;
|
||||
}
|
||||
|
||||
@ -108,16 +100,16 @@ pub fn main() !void {
|
||||
event.code = c.KEY_LEFTMETA;
|
||||
}
|
||||
|
||||
try uinputWrite(uinput_dev.?, &event);
|
||||
try event_write(&event);
|
||||
|
||||
if (write_esc_lshift) {
|
||||
write_esc_lshift = false;
|
||||
try uinputWriteKey(uinput_dev.?, c.KEY_ESC);
|
||||
try write_esc();
|
||||
}
|
||||
|
||||
if (write_esc_rshift) {
|
||||
write_esc_rshift = false;
|
||||
try uinputWriteKey(uinput_dev.?, c.KEY_ESC);
|
||||
try write_esc();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user