refactor: start refactoring
This commit is contained in:
parent
f0ee83ebbb
commit
1d7313ad68
61
src/main.zig
61
src/main.zig
@ -8,33 +8,16 @@ const c = @cImport({
|
||||
|
||||
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 };
|
||||
var uidev: *align(8) void = undefined;
|
||||
var uinput: *align(8) c.libevdev_uinput = 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);
|
||||
}
|
||||
|
||||
fn event_write(event: *const c.input_event) !void {
|
||||
if (c.libevdev_uinput_write_event(@ptrCast(uidev), event.type, event.code, event.value) < 0) {
|
||||
return error.writeFailed;
|
||||
}
|
||||
}
|
||||
|
||||
fn write_esc() !void {
|
||||
std.time.sleep(20000);
|
||||
try event_write(&EscDown);
|
||||
try event_write(&Syn);
|
||||
std.time.sleep(20000);
|
||||
try event_write(&EscUp);
|
||||
}
|
||||
|
||||
fn inputDeviceOpenAndGrab(dev_name: []const u8) !?*c.libevdev {
|
||||
const dev_fd = try std.fs.openFileAbsolute(dev_name, .{});
|
||||
const dev = c.libevdev_new();
|
||||
@ -44,9 +27,36 @@ fn inputDeviceOpenAndGrab(dev_name: []const u8) !?*c.libevdev {
|
||||
if (c.libevdev_grab(dev, c.LIBEVDEV_GRAB) < 0) {
|
||||
return error.libevdevGrabFail;
|
||||
}
|
||||
if (c.libevdev_uinput_create_from_device(@ptrCast(dev), c.LIBEVDEV_UINPUT_OPEN_MANAGED, @ptrCast(uinput)) < 0) {
|
||||
return error.libevdevUinputCreateFail;
|
||||
}
|
||||
return dev;
|
||||
}
|
||||
|
||||
fn uinputCreate(dev: *c.libevdev) !?*c.libevdev_uinput {
|
||||
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 };
|
||||
std.time.sleep(20000);
|
||||
try uinputWrite(uinput_dev, &pressKey);
|
||||
try uinputWrite(uinput_dev, &sync);
|
||||
std.time.sleep(20000);
|
||||
try uinputWrite(uinput_dev, &releaseKey);
|
||||
}
|
||||
|
||||
pub fn main() !void {
|
||||
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
||||
const allocator = gpa.allocator();
|
||||
@ -60,11 +70,8 @@ pub fn main() !void {
|
||||
}
|
||||
const dev_name = args[1];
|
||||
const dev = try inputDeviceOpenAndGrab(dev_name);
|
||||
|
||||
if (c.libevdev_uinput_create_from_device(@ptrCast(dev), c.LIBEVDEV_UINPUT_OPEN_MANAGED, @ptrCast(&uidev)) < 0) {
|
||||
std.debug.print("evdev: failed creating uinput device", .{});
|
||||
std.os.exit(1);
|
||||
}
|
||||
//const uinput_dev = try uinputCreate(dev.?);
|
||||
const uinput_dev = uinput;
|
||||
|
||||
var write_esc_lshift: bool = undefined;
|
||||
var write_esc_rshift: bool = undefined;
|
||||
@ -92,7 +99,7 @@ pub fn main() !void {
|
||||
}
|
||||
|
||||
if (event.type != c.EV_KEY) {
|
||||
try event_write(&event);
|
||||
try uinputWrite(uinput_dev, &event);
|
||||
continue :outer;
|
||||
}
|
||||
|
||||
@ -105,16 +112,16 @@ pub fn main() !void {
|
||||
event.code = c.KEY_LEFTMETA;
|
||||
}
|
||||
|
||||
try event_write(&event);
|
||||
try uinputWrite(uinput_dev, &event);
|
||||
|
||||
if (write_esc_lshift) {
|
||||
write_esc_lshift = false;
|
||||
try write_esc();
|
||||
try uinputWriteKey(uinput_dev, c.KEY_ESC);
|
||||
}
|
||||
|
||||
if (write_esc_rshift) {
|
||||
write_esc_rshift = false;
|
||||
try write_esc();
|
||||
try uinputWriteKey(uinput_dev, c.KEY_ESC);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user