/
callbacks.go
117 lines (100 loc) · 2.82 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package pulse
// #cgo CFLAGS: -Wno-implicit-function-declaration
// #include "conn.h"
// #cgo pkg-config: libpulse
import "C"
import (
"errors"
"fmt"
"github.com/ghetzel/go-stockutil/stringutil"
"github.com/ghetzel/go-stockutil/typeutil"
)
//export go_connStartupDone
func go_connStartupDone(connID *C.char, message *C.char) {
if conn, ok := cgoget(C.GoString(connID)).(*Conn); ok {
conn.SignalAll(false)
}
}
//export go_operationSetProperty
func go_operationSetProperty(operationId *C.char, k *C.char, v *C.char, convertTo *C.char) {
if operation, ok := cgoget(C.GoString(operationId)).(*Operation); ok {
var payload *Payload
if operation.Index < 0 {
payload = operation.AddPayload()
operation.Index = 0
} else {
payload = operation.Payloads[operation.Index]
}
if key := C.GoString(k); key != `` {
if value := C.GoString(v); value != `` {
if convertTo != nil {
var ctype stringutil.ConvertType
switch C.GoString(convertTo) {
case `bool`:
ctype = stringutil.Boolean
case `float`:
ctype = stringutil.Float
case `int`:
ctype = stringutil.Integer
case `volume`:
ctype = stringutil.Integer
case `time`:
ctype = stringutil.Time
default:
ctype = stringutil.String
}
if cV, err := stringutil.ConvertTo(ctype, value); err == nil {
switch C.GoString(convertTo) {
case `volume`:
payload.Properties[key] = typeutil.Float(cV) / DefaultVolumeStep
default:
payload.Properties[key] = cV
}
} else {
payload.Properties[key] = value
}
} else {
payload.Properties[key] = typeutil.Auto(value)
}
}
}
}
}
//export go_operationCreatePayload
func go_operationCreatePayload(operationId *C.char) {
if operation, ok := cgoget(C.GoString(operationId)).(*Operation); ok {
operation.AddPayload()
operation.Index = (len(operation.Payloads) - 1)
}
}
//export go_operationComplete
func go_operationComplete(operationId *C.char) {
if operation, ok := cgoget(C.GoString(operationId)).(*Operation); ok {
// truncate empty payloads
for i, payload := range operation.Payloads {
if len(payload.Properties) == 0 && len(payload.Data) == 0 {
operation.Payloads = append(operation.Payloads[:i], operation.Payloads[i+1:]...)
}
}
// unref pa_operation
if operation.paOper != nil {
C.pa_operation_unref(operation.paOper)
}
operation.Done()
}
}
//export go_operationFailed
func go_operationFailed(operationId *C.char, message *C.char) {
if operation, ok := cgoget(C.GoString(operationId)).(*Operation); ok {
// unref pa_operation
if operation.paOper != nil {
C.pa_operation_unref(operation.paOper)
}
if msg := C.GoString(message); msg == `` {
operation.SetError(fmt.Errorf("Unknown error"))
} else {
operation.SetError(errors.New(msg))
}
operation.Done()
}
}