gtk - Drawing area using cairo -
i developing simple interface simulates led using gtk3 , c. when receive command "led" turn on or turn off according command. using cairo in drawing area draw circle representing led , using gtk_widget_queue_draw_area update screen in timeout function. after while cpu usage increase 100% in application.
when receive command call function below
void update_status_led(int led, int status_led) { g_signal_connect(g_object(darea[led]), "draw", g_callback(on_draw_event_leds), gint_to_pointer(status_led)); }
so callback function "on_draw_event_leds" called
gboolean on_draw_event_leds(gtkwidget *widget, cairo_t *cr, gpointer user_data) { set_status_led(cr, gpointer_to_int(user_data)); return false; }
so calls function "set_status_led"
void set_status_led(cairo_t *cr, int status) { printf("update status led: %d\n", countref++); cairo_reference(cr); cairo_set_line_width(cr, 2); cairo_set_source_rgb(cr, 0, 0, 0); cairo_arc(cr, 9, 9, 7, 0, 2 * m_pi); if (status>0) { cairo_set_source_rgb(cr, 1, 0, 0); //red } else { cairo_set_source_rgb(cr, 0, 0, 0); //black } cairo_fill(cr); cairo_stroke(cr); cairo_destroy(cr); }
i using "printf("update status led: %d\n", countref++);" know how many times function called , log printed below
new connection 127.0.0.1:34589 1 command received update status led: 6 update status led: 7 1 command received update status led: 8 update status led: 9 update status led: 10 1 command received update status led: 11 update status led: 12 update status led: 13 update status led: 14 1 command received update status led: 15 update status led: 16 update status led: 17 update status led: 18 update status led: 19 1 command received update status led: 20 update status led: 21 update status led: 22 update status led: 23 update status led: 24 update status led: 25 1 command received ...
as can seen, receive 1 command , callback function on_draw_event_leds called many times , increase 1 more call each received command previous cairo not deleted , gtk_widget_queue_draw_area render them again.
can me?
thanks,
the g_signal_connect()
function says "when event happens, function". confusing gtk_widget_queue_draw()
, function causes widget redrawn.
somewhat counter-intuitively, signal connections many-many. means number of functions can connect same signal, , all of them called when signal emitted. because call g_signal_connect()
repeatedly, multiple connections drawing function, , drawing function called multiple times.
so want move g_signal_connect()
initialization code instead, that's signals connected anyway, , have gtk_widget_queue_draw()
(or similar function) in update_status_led()
.
Comments
Post a Comment