Integrate Gtk4 mainloop in an external loop

Hi all:

I need to integrate the mainloop of a Gtk4 application into an external mainloop (unfortunately it can’t be the other way for several reasons). Is there a way of getting an fd from the mainloop to poll/select over it and know when to run it?

Thanks.

Hm, this might be tricky. It’s much more common to do the opposite and change your application’s main loop abstraction to use GMainContext. It’s not going to be as easy as checking an fd: you cannot operate on fds because not every GSource corresponds to an fd, they can trigger for different reasons based on prepare()/check() functions and/or child sources, etc.

Maybe g_main_context_pending() would help? You’d have to check that on a timeout, though, which could mess up code that expects to execute sooner than whatever timeout you pick. You might need to make API changes to GMainContext if you want to do better than this.

1 Like

Yes, I considered that, but I wanted to avoid polling…

Consider running the GMainContext in a separate thread and message passing between it and your other threads.

Or, look at g_main_context_query() for integrating into another main loop. You’re on your own with that, though. It’s not something we particularly support. See File: eventloop.c | Debian Sources for a bit of a code example of how to use it.

3 Likes

I understand… Thanks anyway.