func IdleAdd(f interface{}, datas ...interface{}) { var data interface{} if len(datas) > 0 { data = datas[0] } ctx := &SourcefuncContext{reflect.ValueOf(f), reflect.ValueOf(data)} id := sourcefunc_contexts.Add(ctx) C._g_idle_add(C.int(id)) }
func IdleAdd(f interface{}, datas ...interface{}) { var data interface{} if len(datas) > 0 { data = datas[0] } ctx := &SourcefuncContext{f, nil, reflect.ValueOf(data)} ctx.sfi = unsafe.Pointer(C._g_idle_add(C.int(len(sourcefunc_contexts)))) sourcefunc_contexts = append(sourcefunc_contexts, ctx) }
// IdleAdd() is a wrapper around g_idle_add() and adds the function f, // called with the arguments in datas, to run in the context of the GLib // event loop. IdleAdd() returns a uint representing the identifier for // this source function, and an error if f is not a function, len(datas) // does not match the number of inputs to f, or there is a type mismatch // between arguments. func IdleAdd(f interface{}, datas ...interface{}) (uint, error) { rf := reflect.ValueOf(f) if rf.Kind() != reflect.Func { return 0, errors.New("f is not a function") } t := rf.Type() if t.NumIn() != len(datas) { return 0, errors.New("Number of arguments do not match") } var vals []reflect.Value for i := range datas { ntharg := t.In(i) val := reflect.ValueOf(datas[i]) if ntharg.Kind() != val.Kind() { s := fmt.Sprint("Types of arg", i, "do not match") return 0, errors.New(s) } vals = append(vals, val) } ctx := &idleFnContext{} ctx.f = f ctx.args = vals idleFnContexts.Lock() idleFnContexts.s = append(idleFnContexts.s, ctx) idleFnContexts.Unlock() idleFnContexts.RLock() nIdleFns := len(idleFnContexts.s) idleFnContexts.RUnlock() idl := C._g_idle_add(C.int(nIdleFns) - 1) ctx.idl = idl return uint(idl.id), nil }