From 55f7a9638ebcd2dfb78394cf396f2edac926baaf Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Wed, 12 Mar 2014 20:00:29 -0400 Subject: Changed the way uitask is dispatched on GTK+ to make event handling not a CPU hog. --- callbacks_unix.go | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) (limited to 'callbacks_unix.go') diff --git a/callbacks_unix.go b/callbacks_unix.go index 9a0ed99..e67e7a3 100644 --- a/callbacks_unix.go +++ b/callbacks_unix.go @@ -10,7 +10,7 @@ import ( /* cgo doesn't support calling Go functions by default; we have to mark them for export. Not a problem, except arguments to GTK+ callbacks depend on the callback itself. Since we're generating callback functions as simple closures of one type, this file will wrap the generated callbacks in the appropriate callback type. We pass the actual generated pointer to the extra data parameter of the callback. -while we're at it the callback for our idle function will be handled here too for cleanliness purposes +while we're at it the callback for our idle function will be handled here too */ // #cgo pkg-config: gtk+-3.0 @@ -18,6 +18,7 @@ while we're at it the callback for our idle function will be handled here too fo // extern gboolean our_callback(gpointer); // extern gboolean our_window_callback(GtkWidget *, GdkEvent *, gpointer); // extern void our_clicked_callback(GtkButton *, gpointer); +// extern gboolean our_idle_callback(gpointer); import "C" //export our_callback @@ -36,8 +37,20 @@ func our_clicked_callback(button *C.GtkButton, what C.gpointer) { our_callback(what) } +//export our_idle_callback +func our_idle_callback(what C.gpointer) C.gboolean { + // there are two issues we solve here: + // 1) we need to make sure the uitask request gets garbage collected when we're done so as to not waste memory, but only when we're done so as to not have craziness happen + // 2) we need to make sure one idle function runs and finishes running before we start the next; otherwise we could wind up with weird things like the ret channel being closed early + // so this function calls the uitask function and sends a message back to the dispatcher that it finished running; the dispatcher is still holding onto the uitask function so it won't be collected + idleop := (*gtkIdleOp)(unsafe.Pointer(what)) + idleop.what() + idleop.done <- struct{}{} + return C.FALSE // remove this idle function; we're finished +} + var callbacks = map[string]C.GCallback{ - "idle": C.GCallback(C.our_callback), + "idle": C.GCallback(C.our_idle_callback), "delete-event": C.GCallback(C.our_window_callback), "configure-event": C.GCallback(C.our_window_callback), "clicked": C.GCallback(C.our_clicked_callback), -- cgit v1.2.3