Reputation: 747
So I am working on writing a gui for an existing c application.
the gui is just a simple interface which would only display and not really feed back any information into the application. But I need to check a linked list if some new information is available.
Now I don`t want to use another thread to manually run the function to do this.
is there a way to 'hack' timed functions or callbacks into the GTK main loop? so that My function is called once every second or something like that. Of course my function is non-blocking.
I am writing in c.
Upvotes: 4
Views: 3535
Reputation: 4411
GTK includes glib. glib is the core algorithm and data structure library used by gtk and many other gobject libraries. glib provides functions that will be called from the mainloop. glib is included when you include gtk.
You need to have a GSourceFunc as a callback the signature for a GSourceFunc isgboolean
(*GSourceFunc) (gpointer user_data);
gboolean YourCallBack(void* data)
{
GSList list = (GSList*) data;
// Check your list
return TRUE; // return FALSE to remove the timeout
}
Somewhere else you have to register the function:
GSList* List; //This is a singly linked list provided by glib as well.
g_timeout_add_seconds(1, YourCallBack, List);
The first argument to g_timeout_add is the number of seconds between each call of the callback function, the second is the pointer to the GSourceFunc(the callback function) and the third argument is a pointer to data to be passed to the callback function.
edit A GSlist is typically a pointer is to one of the nodes(the first) in the list since the list can change, nodes can be prepended or removed from the list this is dangerous. Although this example shows how to use g_timeout_add_seconds you need a safer way to access your list then I present here. This does however show the mechanics of how to use g_timeout_add_x family of functions.
edit: You can use g_timeout_add to specify a callback for even smaller intervals than seconds, but if you don't need such precision, g_timeout_add_seconds cost fewer resources.
see: https://developer.gnome.org/glib/2.42/glib-The-Main-Event-Loop.html#g-timeout-add for more information.
Upvotes: 8
Reputation: 1
Read about GTK main loop which is above Glib main loop. You'll need g_timeout_add and perhaps g_idle_add etc...
You might also need to learn Gio. We cannot guess how is the new information coming into your application.
If you have a multi-threaded application, be aware that only the main thread can do GTK calls.
Upvotes: 5