Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Post History

71%
+3 −0
Q&A Using DBUS and GTK in one perl program

I haven't developed with Perl or DBUS myself (but I have used GTK), so I can only give an answer in general terms rather than specific details. In most, if not all, GUI frameworks, the "run" or "m...

posted 1y ago by deleted user

Answer
#1: Initial revision by (deleted user) · 2023-06-21T09:44:26Z (over 1 year ago)
I haven't developed with Perl or DBUS myself (but I have used GTK), so I can only give an answer in general terms rather than specific details.

In most, if not all, GUI frameworks, the "run" or "main" method is simply a convenience that is used by most apps to hand over control of the main event-handling loop to the framework. If you need more control, you can implement the loop yourself.

As the GLib docs [describe here](https://docs.gtk.org/glib/main-loop.html):


 > Single iterations of a `GMainContext` can be run with `g_main_context_iteration()`. In some cases, more detailed control of exactly how the details of the main loop work is desired, for instance, when integrating the `GMainLoop` with an external main loop. In such cases, you can call the component functions of `g_main_context_iteration()` directly. These functions are `g_main_context_prepare()`, `g_main_context_query()`, `g_main_context_check()` and `g_main_context_dispatch()`.

This is for the GLib C API, but I would assume that any decent-quality language binding would expose these same functions. The GTK functionality is built on top of GLib, and offers [similar functions](https://docs.gtk.org/gtk3/func.main_iteration.html) like `gtk_main_iteration()`.

What this means in practice is that if you need two main loops, such as the GTK + DBUS example you describe, at least one of those loops is going to need to be implemented manually in your code, rather than handed over to a "main" or "run" function.

This could be achieved by:

 1. Writing the entire loop manually, interleaving methods like `gtk_main_iteration()` and whatever DBUS offers as an equivalent, ensuring you handle return values, check for pending events or whatever else is required by the API when handling a main loop yourself.
 2. Using `gtk_main()` to hand over control to GTK, then using either [timeouts](https://docs.gtk.org/glib/func.timeout_add.html) or [idle callbacks](https://docs.gtk.org/glib/func.idle_add.html) to periodically invoke your code to receive DBUS events and iterate the DBUS main loop. This may be slightly easier because you only have to implement _one_ of the main loops manually, although you'll have less control over precisely when your event-handling code gets called.