main: add inputDeviceOpenAndGrab fun

This commit is contained in:
Asmir A 2023-09-04 19:34:00 +02:00
parent 9e68abf2a0
commit f0ee83ebbb

View File

@ -35,7 +35,17 @@ fn write_esc() !void {
try event_write(&EscUp); 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 { pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){}; var gpa = std.heap.GeneralPurposeAllocator(.{}){};
@ -49,17 +59,7 @@ pub fn main() !void {
std.os.exit(1); std.os.exit(1);
} }
const dev_name = args[1]; const dev_name = args[1];
var dev_fd = try std.fs.openFileAbsolute(dev_name, .{}); const dev = try inputDeviceOpenAndGrab(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);
}
if (c.libevdev_uinput_create_from_device(@ptrCast(dev), c.LIBEVDEV_UINPUT_OPEN_MANAGED, @ptrCast(&uidev)) < 0) { 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.debug.print("evdev: failed creating uinput device", .{});