From f0ee83ebbbea2007b53b5ccf648d721b2cac2eb7 Mon Sep 17 00:00:00 2001 From: Asmir A Date: Mon, 4 Sep 2023 19:34:00 +0200 Subject: [PATCH] main: add inputDeviceOpenAndGrab fun --- src/main.zig | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/main.zig b/src/main.zig index e9b484f..51cd71b 100644 --- a/src/main.zig +++ b/src/main.zig @@ -35,7 +35,17 @@ fn write_esc() !void { try event_write(&EscUp); } -fn device_open() !void {} +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; +} pub fn main() !void { var gpa = std.heap.GeneralPurposeAllocator(.{}){}; @@ -49,17 +59,7 @@ pub fn main() !void { std.os.exit(1); } const dev_name = args[1]; - 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); - } + 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", .{});