Integrating GMainLoop with libdispatch

Hi, I’m trying to integrate a Swift app I’ve built for both macOS and Linux with a GMainLoop. On Linux my app uses a native Wayland window rendering via EGL for UI, and I have a frame timer (driven by the display timer externally) which updates the window. The application’s event loop is driven by libdispatch, calling dispatch_main() to start the event loop.

I’d now like to implement the UI in HTML/JS using WebKit WPE, which relies on GLib and processes its events.signals and runs its internal WebProcess on Linux via a GMainLoop. I’m having some difficulty integrating this with libdispatch.

Some users have attempted to manually poll for Glib events using an X socket ( however this won’t work as Wayland doesn’t have this concept.

Alternatively there is an option to use internal dispatch API to drive libdispatch from a GMainLoop ( however this doesn’t seem quite robust to me.

My third option is to iterate either the GMainLoop or libdispatch run loop from my display callback and driving the application from the other, however as the rest of the application doesn’t depend on GLib and also needs to run on macOS (where WebKit is also driven by libdispatch/CFRunLoop) I’d prefer not to do this. I’d also rather have the ability to have the app run headless.

Would love any comments or advice about the best approach. Also, is there any way to listen on a single fd for GLib events? I could just create a libdispatch dispatch source watching the fd in that case.

I have just found Clutter’s approach which seems to be able to add a GObject event source to CFRunLoop (which will process libdispatch events when CFRunLoopRun() is called) which seems to do what I want. Does anyone have any thoughts on this?

[EDIT] Reading the source more closely it seems this code is taken straight from GDK so I’ll assume it’s the best solution, it definitely integrates into CFRunLoop very nicely.

If the GDK code works for you, then use it. I don’t know enough about macOS to comment definitively, but in general what you want when integrating two event loops is a way of exposing one of them as an event source to the other. The standard way of doing this with poll()-based event loops (for example) is to expose an FD for one event loop and add it as an event source to the other.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.