forked from remyoudompheng/go-alpm
-
Notifications
You must be signed in to change notification settings - Fork 14
/
callbacks.go
73 lines (55 loc) · 1.66 KB
/
callbacks.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// callbacks.go - Handles libalpm callbacks.
//
// Copyright (c) 2013 The go-alpm Authors
//
// MIT Licensed. See LICENSE for details.
package alpm
/*
#include "callbacks.h"
*/
import "C"
import (
"fmt"
"os"
"unsafe"
)
var DefaultLogLevel = LogWarning
type (
logCallbackSig func(interface{}, LogLevel, string)
questionCallbackSig func(interface{}, QuestionAny)
callbackContextPool map[C.go_ctx_t]interface{}
)
var (
logCallbackContextPool = callbackContextPool{}
questionCallbackContextPool = callbackContextPool{}
)
func DefaultLogCallback(ctx interface{}, lvl LogLevel, s string) {
if lvl <= DefaultLogLevel {
fmt.Fprintln(os.Stdout, "go-alpm: ", s)
}
}
//export go_alpm_go_log_callback
func go_alpm_go_log_callback(goCb unsafe.Pointer, goCtx C.go_ctx_t, lvl C.alpm_loglevel_t, s *C.char) {
cb := *(*logCallbackSig)(goCb)
ctx := logCallbackContextPool[goCtx]
cb(ctx, LogLevel(lvl), C.GoString(s))
}
//export go_alpm_go_question_callback
func go_alpm_go_question_callback(goCb unsafe.Pointer, goCtx C.go_ctx_t, question *C.alpm_question_t) {
q := (*C.alpm_question_any_t)(unsafe.Pointer(question))
cb := *(*questionCallbackSig)(goCb)
ctx := questionCallbackContextPool[goCtx]
cb(ctx, QuestionAny{q})
}
func (h *Handle) SetLogCallback(cb logCallbackSig, ctx interface{}) {
goCb := unsafe.Pointer(&cb)
goCtx := C.go_ctx_t(h.ptr)
logCallbackContextPool[goCtx] = ctx
C.go_alpm_set_log_callback(h.ptr, goCb, goCtx)
}
func (h *Handle) SetQuestionCallback(cb questionCallbackSig, ctx interface{}) {
goCb := unsafe.Pointer(&cb)
goCtx := C.go_ctx_t(h.ptr)
questionCallbackContextPool[goCtx] = ctx
C.go_alpm_set_question_callback(h.ptr, goCb, goCtx)
}