uinput: implement uinput functionality

This commit is contained in:
Asmir A 2023-09-03 21:06:16 +02:00
parent 468c4bd474
commit 33db0ea301

View File

@ -1,7 +1,8 @@
const std = @import("std"); const std = @import("std");
const c = @cImport({ const c = @cImport({
@cInclude("linux/input.h"); @cInclude("linux/input.h");
@cInclude("libevdev-1.0/libevdev/libevdev.h"); @cInclude("libevdev/libevdev.h");
@cInclude("libevdev/libevdev-uinput.h");
@cInclude("errno.h"); @cInclude("errno.h");
}); });
@ -14,17 +15,16 @@ const LShiftUp = c.input_event{ .type = c.EV_KEY, .code = c.KEY_LEFTSHIFT, .valu
const RShiftDown = c.input_event{ .type = c.EV_KEY, .code = c.KEY_RIGHTSHIFT, .value = 1, .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 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 }; const Syn = c.input_event{ .type = c.EV_SYN, .code = c.SYN_REPORT, .value = 0, .time = undefined };
var uidev: *align(8) void = undefined;
fn event_equal(e1: *const c.input_event, e2: *const c.input_event) bool { 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 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 { fn event_write(event: *const c.input_event) !void {
try std.io.getStdOut().writer().writeStruct(event.*); if (c.libevdev_uinput_write_event(@ptrCast(uidev), event.*.type, event.*.code, event.*.value) < 0) {
return error.writeFailed;
}
} }
fn write_esc() !void { fn write_esc() !void {
@ -61,6 +61,11 @@ pub fn main() !void {
std.os.exit(1); std.os.exit(1);
} }
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);
}
var write_esc_lshift: bool = undefined; var write_esc_lshift: bool = undefined;
var write_esc_rshift: bool = undefined; var write_esc_rshift: bool = undefined;
var is_lshift_down: bool = false; var is_lshift_down: bool = false;
@ -82,7 +87,7 @@ pub fn main() !void {
break :outer; break :outer;
} }
if (event.type == c.EV_MSC and event.type == c.MSC_SCAN) { if (event.type == c.EV_MSC and event.code == c.MSC_SCAN) {
continue :outer; continue :outer;
} }