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

Expose winit::platform::emscripten::set_main_loop_callback #814

Closed
tangmi opened this issue Mar 17, 2019 · 7 comments
Closed

Expose winit::platform::emscripten::set_main_loop_callback #814

tangmi opened this issue Mar 17, 2019 · 7 comments
Labels
C - in progress Implementation is proceeding smoothly

Comments

@tangmi
Copy link
Contributor

tangmi commented Mar 17, 2019

Hi, it seems the only way to use winit on WASM is to use EventLoop::run_forever, which doesn't seem to give the user an opportunity to do work (e.g. render) if no events have come through. Doing the normal loop { events_loop.poll_events(/* ... */) } seems to cause the tab to hang, since we're not getting that sweet, sweet requestAnimationFrame (I'm assuming).

I'm wondering if it'd be possible to export winit::platform::emscripten::set_main_loop_callback publicly, so one can write a main loop for emscripten. (Maybe even better would be to add a method something like pub fn loop<F>(&mut self, mut per_frame: F) where F: FnMut() -> ControlFlow; that could be implemented with set_main_loop_callback on emscripten and something like while per_frame() != ControlFlow::Break {} everywhere else.)

Cheers!

@Osspial
Copy link
Contributor

Osspial commented Mar 17, 2019

Thanks for the issue!

The run_forever and poll_events methods are actually implemented with emscripten_set_main_loop. That's actually why loop { events_loop.poll_events(/* ... */) } doesn't work - emscripten_set_main_loop needs your application's main function to return before the browser can run the callback, and Emscripten accomplishes this by throwing a JS exception to unwind the stack. That causes poll_events to never return, what with your stack no longer existing.

However, work is ongoing to address your API concerns! @ryanisaacg has been doing great working getting a WASM backend going with the new event loop API through stdweb (see #797). Unfortunately that isn't quite done yet, but it's rapidly getting there and I expect to see it merged sooner rather than later.

You can check out the API docs for the new API here, and I believe that addresses your issue; you can get periodically woken up for rendering with the new EventLoop::run method and the redesigned ControlFlow enum. Right now we're waiting on the X11/macOS backends to be completed before those changes get on crates.io, but I unfortunately don't have any solid timeline for when those will get done - work on the macOS backend is apparently largely done but frustratingly has stalled for reasons I am not aware of, and I don't know if anybody is currently working on the X11 backend.

@Osspial Osspial added platform: Emscripten C - in progress Implementation is proceeding smoothly labels Mar 17, 2019
@tangmi
Copy link
Contributor Author

tangmi commented Mar 17, 2019

Sounds good, perhaps we should close this issue in favor of eventloop 2.0?

I’m happy to take a stab at X11 (I don’t have a Linux box, but that’s what VMs are for, I suppose), especially if your Windows PR can be used as a reference.

@Osspial
Copy link
Contributor

Osspial commented Mar 18, 2019

@vberger you mentioned that you might be doing the X11 backend in this comment - has that gone anywhere yet? If that work hasn't started, @tangmi you should be good to go. Thanks for the offer!

@Osspial Osspial closed this as completed Mar 18, 2019
@elinorbgr
Copy link
Contributor

I haven't started yet, I was thinking aybe getting at it in the coming days, though if you want to try & tackle it @tangmi , please do.

Just be aware that the x11 code is very different from the Windows code, and it also currently contains a lot of work-arounds to compensate for X11 gotchas...

@tangmi
Copy link
Contributor Author

tangmi commented Mar 18, 2019

Thanks for the heads up. I'll take a look at it, although no promises yet (ran into issues trying to install a linux system...).

@goddessfreya
Copy link
Contributor

Hey @tangmi, are you still working on X11 support? How far did you get?

@tangmi
Copy link
Contributor Author

tangmi commented Apr 14, 2019

Unfortunately no real progress. I don't have a linux machine or any machine powerful enough to reasonably run a VM :(.

I ended up finding emscripten-specific workarounds to make my project work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C - in progress Implementation is proceeding smoothly
Development

No branches or pull requests

4 participants