[Previous] [Contents] [Next]

17. Timeouts, IO and Idle Functions

17.1 Timeouts

You may be wondering how you make GTK do useful work when in gtk_main(). Well, you have several options. Using the following function you can create a timeout function that will be called every interval milliseconds.

function g_timeout_add (interval : guint32;
                        function : GtkFunction;
                        data : gpointer) : gint;

The first argument is the number of milliseconds between calls to your function. The second argument is the function you wish to have called, and the third, the data passed to this callback function. The return value is an integer "tag" which may be used to stop the timeout by calling:

procedure g_source_remove (tag : gint);

You may also stop the timeout function by returning zero or false from your callback function. Obviously this means if you want your function to continue to be called, it should return a non-zero value, i.e., true.

The declaration of your callback should look something like this:

function timeout_callback (data : gpointer) : gint;

17.2 Monitoring IO

A nifty feature of GDK (the library that underlies GTK), is the ability to have it check for data on a file descriptor for you (as returned by open(2) or socket(2)). This is especially useful for networking applications. The function:

function gdk_input_add (source : gint;
                        input_condition : GdkInputCondition;
                        input_function : GdkInputFunction;
                        data : gpointer) : gint;

where the first argument is the file descriptor you wish to have watched, and the second specifies what you want GDK to look for. This may be one of:

As I'm sure you've figured out already, the third argument is the function you wish to have called when the above conditions are satisfied, and the fourth is the data to pass to this function.

The return value is a tag that may be used to stop GDK from monitoring this file descriptor using the following procedure.

procedure gdk_input_remove (tag : gint);

The callback procedure should be declared as:

procedure input_callback (data : gpointer;
                          source : gint;
                          input_condition : GdkInputCondition);

Where source and input_condition are as specified above.


17.3 Idle Functions

What if you have a function which you want to be called when nothing else is happening?

function g_idle_add (function : TGSourceFunc;
                     data : gpointer) : gint;

This causes GTK to call the specified function whenever nothing else is happening.

function g_source_remove (tag : gint) : boolean;

I won't explain the meaning of the arguments as they follow very much like the ones above. The function pointed to by the first argument to g_idle_add() will be called whenever the opportunity arises. As with the others, returning false will stop the idle function from being called.


[Previous] [Contents] [Next]