Cleaned up the idle callback code.

This commit is contained in:
Pietro Gagliardi 2014-02-16 17:09:58 -05:00
parent f5652737eb
commit 1bcbce4142
4 changed files with 26 additions and 27 deletions

View File

@ -9,19 +9,28 @@ 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
*/
// #cgo pkg-config: gtk+-3.0
// #include <gtk/gtk.h>
// extern gboolean our_callback(gpointer);
// extern gboolean our_delete_event_callback(GtkWidget *, GdkEvent *, gpointer);
import "C"
//export our_delete_event_callback
func our_delete_event_callback(widget *C.GtkWidget, event *C.GdkEvent, what C.gpointer) C.gboolean {
//export our_callback
func our_callback(what C.gpointer) C.gboolean {
f := *(*func() bool)(unsafe.Pointer(what))
return togbool(f())
}
//export our_delete_event_callback
func our_delete_event_callback(widget *C.GtkWidget, event *C.GdkEvent, what C.gpointer) C.gboolean {
return our_callback(what)
}
var callbacks = map[string]C.GCallback{
"idle": C.GCallback(C.our_callback),
"delete-event": C.GCallback(C.our_delete_event_callback),
}

View File

@ -12,9 +12,7 @@ import (
// #include <stdlib.h>
// #include <gtk/gtk.h>
// /* because cgo is flaky with macros */
// static inline void gSignalConnect(GtkWidget *widget, char *signal, GCallback callback, void *data) { g_signal_connect(widget, signal, callback, data); }
// /* so we can call uistep */
// extern gboolean our_thread_callback(gpointer);
// void gSignalConnect(GtkWidget *widget, char *signal, GCallback callback, void *data) { g_signal_connect(widget, signal, callback, data); }
import "C"
type (
@ -32,21 +30,13 @@ func togbool(b bool) C.gboolean {
return C.FALSE
}
//export our_thread_callback
func our_thread_callback(C.gpointer) C.gboolean {
uistep()
return C.TRUE
}
func gtk_init() bool {
// TODO allow GTK+ standard command-line argument processing
b := fromgbool(C.gtk_init_check((*C.int)(nil), (***C.char)(nil)))
if !b {
return false
}
// thanks to tristan in irc.gimp.net/#gtk
C.gdk_threads_add_idle(C.GSourceFunc(C.our_thread_callback), C.gpointer(unsafe.Pointer(nil)))
return true
return fromgbool(C.gtk_init_check((*C.int)(nil), (***C.char)(nil)))
}
func gdk_threads_add_idle(what func() bool) {
C.gdk_threads_add_idle(callbacks["idle"], C.gpointer(unsafe.Pointer(&what)))
}
func gtk_main() {

View File

@ -20,7 +20,6 @@ so I don't forget:
- have Combobox.InsertBefore, Listbox.InsertBefore, Combobox.Delete, and Listbox.Delete return an error on invalid index before creation
important things:
- the C.gdk_threads_add_idle logic is messy and will break when I implement actual connections...
- there's no GTK+ error handling whatsoever; we need to figure out how it works
- make sure GTK+ documentation point differences don't matter

View File

@ -21,17 +21,18 @@ func ui(initDone chan error) {
}
initDone <- nil
// thanks to tristan in irc.gimp.net/#gtk
gdk_threads_add_idle(func() bool {
select {
case f := <-uitask:
f()
default: // do not block
}
return true // don't destroy the callback
})
gtk_main()
}
func uistep() {
select {
case f := <-uitask:
f()
default: // do not block
}
}
// temporary
func MsgBox(string, string, ...interface{}) {}
func MsgBoxError(title string, text string, args ...interface{}) {panic(title+"\n"+fmt.Sprintf(text,args...))}