pike
pike copied to clipboard
How to listen to two open file descriptors?
I'm struggling to create what seems like should be a very basic use of pike: listen to two file descriptors, and if anything is read, print it out (without blocking).
My use case is I'd like to listen to several linux evdev devices (mouse, touchpad, keyboard for example).
I tried using example_event.zig
as a starting off point, but I'm failing to understand how to connect file descriptors to a Notifier (I think?)
I'm also unsure why, when notifier.poll(10_000)
is called in the example, it doesn't wait 10 seconds like I expect it should... unless it's actually receiving events, but then, what events is the example receiving?
Anyway, I'm new to Zig but like what I see here and just thought I'd write here in case this use case might help make Pike easier to pick up for others.
Thanks.
I think I got it!
//[...snip...]
pub fn main() !void {
try pike.init();
defer pike.deinit();
const notifier = try pike.Notifier.init();
defer notifier.deinit();
const fd: os.fd_t = try udev.open_touchpad();
defer os.close(fd); //udev.close_touchpad(fd);
const handle: pike.Handle =
.{ .inner = fd, .wake_fn = wake };
try notifier.register(
&handle,
.{ .read = true, .write = false },
);
var iter: u64 = 0;
while(true) : (iter += 1) {
try notifier.poll(1000);
std.debug.print("loop {}\n", .{iter});
}
}
fn wake(handle: *pike.Handle, batch: *pike.Batch, opts: pike.WakeOptions) void {
// if (opts.read_ready)
std.debug.print("fd: {d}, opts: {}\n", .{ handle.inner, opts });
_ = batch;
}
Now I just need to actually read from the handle fd...