frang75 / nappgui_src

SDK for building cross-platform desktop apps in ANSI-C
https://www.nappgui.com
MIT License
442 stars 43 forks source link

Implement a Manual Event Pump API to Enable Non-Blocking Applications #100

Open turbo opened 3 months ago

turbo commented 3 months ago

This is a feature request to extend the OSApp API to allow external message pumping.

The goal is to allow NApp to be integrated into programs that already have their own event loop (async runtimes, game engines, etc.). This proposal would make osapp_run non-blocking and expose an API function callable by the program's main thread to manually process events.

The default would remain exactly as it works now, manual pumping would be an option. This is already partially in place for Windows, but not for other platforms.

Current Implementation

This is my understanding of the current implementation, which might be wrong (please correct me).

Proposed changes

First, implement osgui_message_loop on all platforms:

Then, add an app->blocking flag that is TRUE by default. This flag must be respected in the osgui_message_loop_imp implementations. If it is TRUE, then the message loops need to be blocking, as is the case for Windows today (src). However, when it is FALSE, the message loop must return after processing events in the queue.

Finally, the osgui_message_loop function should be available via the public API so that in non-blocking mode, the parent program can call it.

Result

Now applications can launch NApp in two different ways:

This is how libraries like GLFW work today, and indeed I'm currently integrating NApp into an engine that has a competing event loop in the main thread. Here are details on the changes required:

All in all, these changes should not require much additional code, OSApp is almost there and the concept of a message loop function is already in place.

frang75 commented 3 months ago

Hi @turbo!

I am very surprised by this issue. Both for the proposal itself, and for the level of detail when proposing the solution. Thank you for the time you have dedicated and your involvement in the project.

It is a necessary feature, which will open the use of NAppGUI to existing applications, with their own runLoop. A few years ago, in an early version of NAppGUI I implemented this support (Windows only) to create an experimental interface for a Rhinoceros 3D plugin. The const bool_t with_run_loop parameter of osapp_init_imp is a legacy from those times.

I'll get into this as soon as possible.

Please email me with your Discord username. We have created a server with a small community of active users, where we have several open development lines.

Thanks again, and welcome to NAppGUI

turbo commented 3 months ago

My Discord user is summarity (or send an invite to mail@summarity.com) 🙂.