GApplication and hacking the main loop

, ,

I needed to “hack” the GMainLoop of my application. That means that my application at the moment does not start with g_application_run(), but starts with something like start_hacked_mainloop().

However, since GApplication/GtkApplication have nice signals and a nice interface, I would like to create my own class derived from GtkApplication, which would run my “hacked” GMainLoop.

How can I do that? How can I access to the GMainLoop and GMainContext of a class derived from GtkApplication?

You don’t. GtkApplication by necessity spins the default GMainContext.

And how about GApplication (instead of GtkApplication)?

Is there no way at all for my program to have a GApplication?

GtkApplication doesn’t do anything special, except GTK-specific things like session management and binding the application instance to the application window(s). The main loop handling is all in the GApplication base class, and it’s the application that spins the current default GMainContext.

You could push a different GMainContext as the default, but that is going to be absolutely horrific with GTK because GTK can only ever use a single thread and the default main context.

Okay, I will go more into detail. I am working with GNUnet, which already has its own main loop (the event loop API is called GNUNET_SCHEDULER). This means that the solutions are two when working with GTK: launching GNUnet’s event loop in another thread (I have already created a library that does exactly that) or merging the events of the two event loops, and this is what GNUnet GTK does. However GNUnet GTK’s code is too tied to its specific case, so I wanted to create a general-purpose class derived from GApplication usable in any context, which does more or less what GNUnet GTK already does in its specific case (maybe trying to improve things here and there, when possible).

GNUnet GTK’s code obtains the GMainContext normally via g_main_loop_get_context(). The question is: how to push that into a GApplication-derived class?


Just out of curiosity, how would I “push” a GMainContext obtained (via g_main_loop_get_context()) from a GMainLoop manually launched with g_main_loop_new()?

So, basically I have this situation:

GMainLoop * my_loop = g_main_loop_new(NULL, TRUE);
GMainContext * my_context = g_main_loop_get_context(my_loop);

Is it possible to push these two as the GMainLoop and GMainContext on which a custom class derived from GApplication runs?

Your GMainLoop is using the default main context (first parameter is NULL).

I think GApplication always iterates the default main context. So you don’t need to do anything. There’s also no need to ever call g_main_loop_get_context() if you know your GMainLoop iterates the default main context.

1 Like


Thank you. If you see the code I linked earlier from GNUnet GTK, it needs both the GMainLoop and GMainContext for launching the following functions:

  • g_main_loop_quit()
  • g_main_loop_is_running()
  • g_main_loop_get_context()
  • g_main_context_prepare()
  • g_main_context_dispatch()
  • g_main_context_query()
  • g_main_context_check()

But when I create a GApplication (or a class derived from it), both the GMainLoop and the GMainContext on which this spins seem unreachable to me, and therefore I cannot launch any on the functions above (what would I pass as argument?). What I would need is either a pointer to the GApplication's event loop or a way to re-assign one.

As I said, GApplication probably uses the default main context (I don’t think anything else would make sense). You can pass NULL to any functions that require a GMainContext for it to use the default main context. Please see the documentation of GMainContext if any of this is unclear.

1 Like

GMainLoop is a convenience wrapper around GMainContext that iterates the context for you. It’s not important and it’s entirely possible that GApplication doesn’t even create one. Doesn’t matter.

Only the GMainContext is important.

1 Like

Thank you a lot, Michael. I will see what I will be able to do in this way.

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