Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to listen to two open file descriptors? #15

Open
canadaduane opened this issue Nov 26, 2021 · 1 comment
Open

How to listen to two open file descriptors? #15

canadaduane opened this issue Nov 26, 2021 · 1 comment

Comments

@canadaduane
Copy link

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.

@canadaduane
Copy link
Author

canadaduane commented Nov 26, 2021

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...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant