forked from therecipe/qt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qt.go
332 lines (287 loc) · 7.58 KB
/
qt.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
package qt
import (
"log"
"os"
"runtime"
"strings"
"sync"
"unsafe"
)
var (
Logger = log.New(os.Stderr, "", log.Ltime)
signals = make(map[unsafe.Pointer]map[string]unsafe.Pointer)
signalsJNI = make(map[string]map[string]unsafe.Pointer)
signalsMutex sync.Mutex
objects = make(map[unsafe.Pointer]interface{})
objectsMutex sync.Mutex
objectsTemp = make(map[unsafe.Pointer]unsafe.Pointer)
objectsTempMutex sync.Mutex
connectionTypes = make(map[unsafe.Pointer]map[string]int64)
connectionTypesMutex sync.Mutex
finalizerMap = make(map[unsafe.Pointer]struct{})
finalizerMapMutex sync.Mutex
//
FuncMap = make(map[string]interface{})
FuncMapMutex sync.Mutex
ItfMap = make(map[string]interface{})
itfMapMutex sync.Mutex
EnumMap = make(map[string]int64)
EnumMapMutex sync.Mutex
)
func init() { runtime.LockOSThread() }
func ExistsSignal(cPtr unsafe.Pointer, signal string) (exists bool) {
signalsMutex.Lock()
_, exists = signals[cPtr][signal]
signalsMutex.Unlock()
return
}
func LendSignal(cPtr unsafe.Pointer, signal string) (s unsafe.Pointer) {
signalsMutex.Lock()
s = signals[cPtr][signal]
signalsMutex.Unlock()
return
}
func lendSignalJNI(cPtr, signal string) (s unsafe.Pointer) {
signalsMutex.Lock()
s = signalsJNI[cPtr][signal]
signalsMutex.Unlock()
return
}
func GetSignal(cPtr interface{}, signal string) unsafe.Pointer {
if dcPtr, ok := cPtr.(unsafe.Pointer); ok {
if signal == "destroyed" || strings.HasPrefix(signal, "~") {
defer DisconnectAllSignals(dcPtr, signal)
}
return LendSignal(dcPtr, signal)
}
return lendSignalJNI(cPtr.(string), signal)
}
func ConnectSignal(cPtr interface{}, signal string, function unsafe.Pointer) {
if dcPtr, ok := cPtr.(unsafe.Pointer); ok {
signalsMutex.Lock()
if s, exists := signals[dcPtr]; !exists {
signals[dcPtr] = map[string]unsafe.Pointer{signal: function}
} else {
s[signal] = function
}
signalsMutex.Unlock()
} else {
connectSignalJNI(cPtr.(string), signal, function)
}
}
func connectSignalJNI(cPtr, signal string, function unsafe.Pointer) {
signalsMutex.Lock()
if s, exists := signalsJNI[cPtr]; !exists {
signalsJNI[cPtr] = map[string]unsafe.Pointer{signal: function}
} else {
s[signal] = function
}
signalsMutex.Unlock()
}
func DisconnectSignal(cPtr interface{}, signal string) {
if dcPtr, ok := cPtr.(unsafe.Pointer); ok {
signalsMutex.Lock()
delete(signals[dcPtr], signal)
if len(signals[dcPtr]) == 0 {
delete(signals, dcPtr)
}
signalsMutex.Unlock()
} else {
disconnectSignalJNI(cPtr.(string), signal)
}
}
func disconnectSignalJNI(cPtr, signal string) {
signalsMutex.Lock()
delete(signalsJNI[cPtr], signal)
if len(signalsJNI[cPtr]) == 0 {
delete(signalsJNI, cPtr)
}
signalsMutex.Unlock()
}
func DisconnectAllSignals(cPtr unsafe.Pointer, signal string) {
signalsMutex.Lock()
if s, exists := signals[cPtr]["destroyed"]; signal != "destroyed" && exists {
signals[cPtr] = map[string]unsafe.Pointer{"destroyed": s}
} else {
delete(signals, cPtr)
}
signalsMutex.Unlock()
}
func DumpSignals() {
Debug("##############################\tSIGNALS_TABLE_START\t##############################")
signalsMutex.Lock()
for cPtr, entry := range signals {
Debug(cPtr, entry)
}
signalsMutex.Unlock()
Debug("##############################\tSIGNALS_TABLE_END\t##############################")
}
func DumpObjects() {
Debug("##############################\tOBJECTS_TABLE_START\t##############################")
objectsMutex.Lock()
for cPtr, entry := range objects {
Debug(cPtr, entry)
}
objectsMutex.Unlock()
Debug("##############################\tOBJECTS_TABLE_END\t##############################")
}
func DumpTempObjects() {
Debug("##############################\tTMP_OBJECTS_TABLE_START\t##############################")
objectsTempMutex.Lock()
for cPtr, entry := range objectsTemp {
Debug(cPtr, entry)
}
objectsTempMutex.Unlock()
Debug("##############################\tTMP_OBJECTS_TABLE_END\t##############################")
}
func DumpConnectionTypes() {
Debug("##############################\tCON_MODES_TABLE_START\t##############################")
connectionTypesMutex.Lock()
for cPtr, entry := range connectionTypes {
Debug(cPtr, entry)
}
connectionTypesMutex.Unlock()
Debug("##############################\tCON_MODES_TABLE_END\t##############################")
}
func CountSignals() (c int) {
signalsMutex.Lock()
c = len(signals)
signalsMutex.Unlock()
return
}
func GoBoolToInt(b bool) int8 {
if b {
return 1
}
return 0
}
func Recover(fn string) {
if recover() != nil {
Debug("RECOVERED:", fn)
}
}
func Debug(fn ...interface{}) {
if strings.ToLower(os.Getenv("QT_DEBUG")) == "true" || runtime.GOARCH == "js" || runtime.GOARCH == "wasm" {
Logger.Println(fn...)
}
}
func ClearSignals() {
signalsMutex.Lock()
signals = make(map[unsafe.Pointer]map[string]unsafe.Pointer)
signalsMutex.Unlock()
}
func Register(cPtr unsafe.Pointer, gPtr interface{}) {
objectsMutex.Lock()
objects[cPtr] = gPtr
objectsMutex.Unlock()
}
func Receive(cPtr unsafe.Pointer) (o interface{}, ok bool) {
objectsMutex.Lock()
o, ok = objects[cPtr]
objectsMutex.Unlock()
return
}
func Unregister(cPtr unsafe.Pointer) {
objectsMutex.Lock()
delete(objects, cPtr)
objectsMutex.Unlock()
}
func RegisterTemp(cPtr unsafe.Pointer, gPtr unsafe.Pointer) {
objectsTempMutex.Lock()
objectsTemp[cPtr] = gPtr
objectsTempMutex.Unlock()
}
func ReceiveTemp(cPtr unsafe.Pointer) (o unsafe.Pointer, ok bool) {
objectsTempMutex.Lock()
o, ok = objectsTemp[cPtr]
objectsTempMutex.Unlock()
return
}
func UnregisterTemp(cPtr unsafe.Pointer) {
objectsTempMutex.Lock()
delete(objectsTemp, cPtr)
objectsTempMutex.Unlock()
}
func RegisterConnectionType(cPtr unsafe.Pointer, signal string, mode int64) {
connectionTypesMutex.Lock()
if s, exists := connectionTypes[cPtr]; !exists {
connectionTypes[cPtr] = map[string]int64{signal: mode}
} else {
s[signal] = mode
}
connectionTypesMutex.Unlock()
}
func ConnectionType(cPtr unsafe.Pointer, signal string) (m int64) {
connectionTypesMutex.Lock()
if s, exists := connectionTypes[cPtr]; exists {
if lm, ok := s[signal]; ok {
delete(s, signal)
if len(s) == 0 {
delete(connectionTypes, cPtr)
}
m = lm
}
}
connectionTypesMutex.Unlock()
return
}
func GetFuncMap(n string) (o interface{}, ok bool) {
FuncMapMutex.Lock()
o, ok = FuncMap[n]
FuncMapMutex.Unlock()
return
}
func SetFuncMap(n string, v interface{}) {
FuncMapMutex.Lock()
FuncMap[n] = v
FuncMapMutex.Unlock()
}
func GetItfMap(n string) (o interface{}, ok bool) {
itfMapMutex.Lock()
o, ok = ItfMap[n]
itfMapMutex.Unlock()
return
}
func SetItfMap(n string, v interface{}) {
itfMapMutex.Lock()
ItfMap[n] = v
itfMapMutex.Unlock()
}
func GetEnumMap(n string) (o int64, ok bool) {
EnumMapMutex.Lock()
o, ok = EnumMap[n]
EnumMapMutex.Unlock()
return
}
func SetEnumMap(n string, v int64) {
EnumMapMutex.Lock()
EnumMap[n] = v
EnumMapMutex.Unlock()
}
type ptr_itf interface {
Pointer() unsafe.Pointer
SetPointer(p unsafe.Pointer)
}
func SetFinalizer(ptr interface{}, f interface{}) {
finalizerMapMutex.Lock()
cPtr := ptr.(ptr_itf).Pointer()
if _, ok := finalizerMap[cPtr]; !ok || f == nil || cPtr == nil {
if cPtr != nil || (cPtr == nil && f == nil) {
runtime.SetFinalizer(ptr, f)
}
if f == nil {
delete(finalizerMap, cPtr)
} else if cPtr != nil {
finalizerMap[cPtr] = struct{}{}
}
} else {
runtime.SetFinalizer(ptr, func(p ptr_itf) { p.SetPointer(nil) })
}
finalizerMapMutex.Unlock()
}
func HasFinalizer(ptr interface{}) (ok bool) {
finalizerMapMutex.Lock()
_, ok = finalizerMap[ptr.(ptr_itf).Pointer()]
finalizerMapMutex.Unlock()
return
}