In my code, GTK (3, in C) operations are in a single thread.
Every 200ms, my GTK code checks a volatile variable if there is any change (set by another thread), and if yes, queues a redraw
#define REFRESH_MILISECS 200
static gboolean timeout_callback (gpointer data)
{
static int last_changements = 0;
if ( last_changements != changements ) {
last_changements = changements;
g_idle_add((GSourceFunc)gtk_widget_queue_draw, (void*)draw_area);
while (gtk_events_pending()) {
gtk_main_iteration();
}
}
return 1; // keep going
}
The non-GTK thread simply sets a struct to define the rectangle to be redrawn (accessible via a mutex), and increments the changements
volatile int variable.
The drawing function accesses the struct via the mutex and knows what part are to be (re)drawn.
static gboolean draw_status(GtkWidget *widget, cairo_t *cr, void *user_data)
However, and this is the question:
-
How can my callback drawing function know if the drawing event was generated by me (code above), or by Gnome/GTK following a
expose-event
? -
since I’m here, another question: since the GTK code is all on its own thread, do I need the
g_idle_add
wrapping function, and thewhile (gtk_events_pending())
loop?
Thanks