I am fairly new to GTK development, not very much aware if GTK supports custom / user-defined events. I am building an application where GUI layout (i.e. the size and position of GtkWidgets) needs to be read from memory and using that data the UI can be created and shown for user interaction.
The GTK Main event loop runs on the Main thread, whereas another (non-main) thread processes the GUI layout and updates the memory. The goal is to send a trigger from the non-main thread to the main thread when the memory is ready for reading. One way this may be achieved is by making the main thread wait in an infinite while loop until the memory is ready with the help of some shared flag. But due to some application architecture specific constraints, we want the main thread to enter the GTK event loop as early as possible.
Therefore, I tried to define and use a Custom GdkEvent (CREATE_WINDOW). In the main thread I have created a GTK_EVENT_BOX (since we don't want any visible window to be created until memory is read) and attached a callback function to it, before it enters the GTK event loop (gtk_main()). The intention is to post a custom event from the non-main thread that would eventually result into the callback function being invoked. The callback function in-turn shall read the memory and create the actual GUI.
The main-thread implementation is as below -
eventbox = gtk_event_box_new ();
gtk_event_box_set_above_child (GTK_EVENT_BOX (eventbox), true);
g_signal_connect (G_OBJECT (eventbox), "event", G_CALLBACK (InternalWindowLifecycleHandler), nullptr);
gtk_widget_set_realized (eventbox, true);
gtk_widget_show_all (eventbox);
gtk_main ();
The non-main thread code is pretty simple (skipped the memory preparation code) -
GdkEvent * createwinevent;
createwinevent = gdk_event_new ((GdkEventType) custEvent::CREATE_WINDOW);
gdk_event_put (createwinevent);
Note - custEvent is an Enum defined by me.
On contrary to our expectations, posting this custom event does not trigger the callback function InternalWindowLifecycleHandler
.
I wonder, is it possible to achieve my goal in GTK, with or without custom / user-defined events?