forked from go-qml/qml
-
Notifications
You must be signed in to change notification settings - Fork 7
/
qmlengine.go
293 lines (260 loc) · 8.33 KB
/
qmlengine.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
package qml
// #include <stdlib.h>
//
// #include "capi.h"
//
import "C"
import (
"errors"
"fmt"
"image"
"io"
"io/ioutil"
"os"
"path/filepath"
"reflect"
"strings"
"unsafe"
"github.com/limetext/qml-go/internal/util"
)
// Engine provides an environment for instantiating QML components.
type Engine struct {
Common
values map[interface{}]*valueFold
destroyed bool
savedAddr unsafe.Pointer // addr might be cleared when destroyed, use this after destroyed
imageProviders map[string]*func(imageId string, width, height int) image.Image
}
var engines = make(map[unsafe.Pointer]*Engine)
// NewEngine returns a new QML engine.
//
// The Destory method must be called to finalize the engine and
// release any resources used.
func NewEngine() *Engine {
engine := &Engine{values: make(map[interface{}]*valueFold)}
RunMain(func() {
engine.engine = engine
engine.setAddr(C.newEngine(nil))
engine.savedAddr = engine.addr
engine.imageProviders = make(map[string]*func(imageId string, width, height int) image.Image)
engines[engine.addr] = engine
stats.enginesAlive(+1)
})
return engine
}
func (e *Engine) assertValid() {
if e.destroyed {
panic("engine already destroyed")
}
}
// Destroy finalizes the engine and releases any resources used.
// The engine must not be used after calling this method.
//
// It is safe to call Destroy more than once.
func (e *Engine) Destroy() {
if !e.destroyed {
RunMain(func() {
if !e.destroyed {
e.destroyed = true
C.delObjectLater(e.addr)
if len(e.values) == 0 {
delete(engines, e.addr)
} else {
// The engine reference keeps those values alive.
// The last value destroyed will clear it.
}
stats.enginesAlive(-1)
}
})
}
}
// Load loads a new component with the provided location and with the
// content read from r. The location informs the resource name for
// logged messages, and its path is used to locate any other resources
// referenced by the QML content.
//
// Once a component is loaded, component instances may be created from
// the resulting object via its Create and CreateWindow methods.
func (e *Engine) Load(location string, r io.Reader) (Object, error) {
var cdata unsafe.Pointer
var cdatalen int
qrc := strings.HasPrefix(location, "qrc:")
if qrc {
if r != nil {
return nil, fmt.Errorf("cannot load qrc resource while providing data: %s", location)
}
} else {
data, err := ioutil.ReadAll(r)
if err != nil {
return nil, err
}
if colon, slash := strings.Index(location, ":"), strings.Index(location, "/"); colon == -1 || slash <= colon {
if filepath.IsAbs(location) {
location = "file:///" + filepath.ToSlash(location)
} else {
dir, err := os.Getwd()
if err != nil {
return nil, fmt.Errorf("cannot obtain absolute path: %v", err)
}
location = "file:///" + filepath.ToSlash(filepath.Join(dir, location))
}
}
// Workaround issue #84 (QTBUG-41193) by not refering to an existent file.
if s := strings.TrimPrefix(location, "file:///"); s != location {
if _, err := os.Stat(filepath.FromSlash(s)); err == nil {
location = location + "."
}
}
cdata, cdatalen = util.UnsafeBytesData(data)
}
var err error
cloc, cloclen := util.UnsafeStringData(location)
comp := &Common{engine: e}
RunMain(func() {
// TODO The component's parent should probably be the engine.
comp.setAddr(C.newComponent(e.addr, nilPtr))
if qrc {
C.componentLoadURL(comp.addr, (*C.char)(cloc), C.int(cloclen))
} else {
C.componentSetData(comp.addr, (*C.char)(cdata), C.int(cdatalen), (*C.char)(cloc), C.int(cloclen))
}
message := C.componentErrorString(comp.addr)
if message != nilCharPtr {
err = errors.New(strings.TrimRight(C.GoString(message), "\n"))
C.free(unsafe.Pointer(message))
}
})
if err != nil {
return nil, err
}
return comp, nil
}
// LoadFile loads a component from the provided QML file.
// Resources referenced by the QML content will be resolved relative to its path.
//
// Once a component is loaded, component instances may be created from
// the resulting object via its Create and CreateWindow methods.
func (e *Engine) LoadFile(path string) (Object, error) {
if strings.HasPrefix(path, "qrc:") {
return e.Load(path, nil)
}
// TODO Test this.
f, err := os.Open(path)
if err != nil {
return nil, err
}
defer f.Close()
return e.Load(path, f)
}
// LoadString loads a component from the provided QML string.
// The location informs the resource name for logged messages, and its
// path is used to locate any other resources referenced by the QML content.
//
// Once a component is loaded, component instances may be created from
// the resulting object via its Create and CreateWindow methods.
func (e *Engine) LoadString(location, qml string) (Object, error) {
return e.Load(location, strings.NewReader(qml))
}
// Context returns the engine's root context.
func (e *Engine) Context() *Context {
e.assertValid()
var ctx Context
ctx.engine = e
RunMain(func() {
ctx.setAddr(C.engineRootContext(e.addr))
})
return &ctx
}
func (e *Engine) ClearImportPaths() {
RunMain(func() {
C.engineClearImportPaths(e.addr)
})
}
func (e *Engine) AddImportPath(path string) {
cpath, cpathLen := util.UnsafeStringData(path)
RunMain(func() {
C.engineAddImportPath(e.addr, (*C.char)(cpath), C.int(cpathLen))
})
}
func (e *Engine) ClearPluginPaths() {
RunMain(func() {
C.engineClearPluginPaths(e.addr)
})
}
func (e *Engine) AddPluginPath(path string) {
cpath, cpathLen := util.UnsafeStringData(path)
RunMain(func() {
C.engineAddPluginPath(e.addr, (*C.char)(cpath), C.int(cpathLen))
})
}
func (e *Engine) ClearComponentCache() {
RunMain(func() {
C.engineClearComponentCache(e.addr)
})
}
// TODO ObjectOf is probably still worth it, but turned out unnecessary
// for GL functionality. Test it properly before introducing it.
// ObjectOf returns the QML Object representation of the provided Go value
// within the e engine.
//func (e *Engine) ObjectOf(value interface{}) Object {
// // TODO Would be good to preserve identity on the Go side. See unpackDataValue as well.
// return &Common{
// engine: e,
// addr: wrapGoValue(e, value, cppOwner),
// }
//}
// AddImageProvider registers f to be called when an image is requested by QML code
// with the specified provider identifier. It is a runtime error to register the same
// provider identifier multiple times.
//
// The imgId provided to f is the requested image source, with the "image:" scheme
// and provider identifier removed. For example, with an image image source of
// "image://myprovider/icons/home.ext", the respective imgId would be "icons/home.ext".
//
// If either the width or the height parameters provided to f are zero, no specific
// size for the image was requested. If non-zero, the returned image should have the
// the provided size, and will be resized if the returned image has a different size.
//
// See the documentation for more details on image providers:
//
// http://qt-project.org/doc/qt-5.0/qtquick/qquickimageprovider.html
//
func (e *Engine) AddImageProvider(prvId string, f func(imgId string, width, height int) image.Image) {
if _, ok := e.imageProviders[prvId]; ok {
panic(fmt.Sprintf("engine already has an image provider with id %q", prvId))
}
e.imageProviders[prvId] = &f
cprvId, cprvIdLen := util.UnsafeStringData(prvId)
RunMain(func() {
qprvId := C.newString((*C.char)(cprvId), C.int(cprvIdLen))
defer C.delString(qprvId)
C.engineAddImageProvider(e.addr, qprvId, unsafe.Pointer(&f))
})
}
//export hookRequestImage
func hookRequestImage(imageFunc unsafe.Pointer, cid *C.char, cidLen, cwidth, cheight C.int) unsafe.Pointer {
f := *(*func(imgId string, width, height int) image.Image)(imageFunc)
id := util.UnsafeString(unsafe.Pointer(cid), int(cidLen))
width := int(cwidth)
height := int(cheight)
img := f(id, width, height)
var cimage unsafe.Pointer
rect := img.Bounds()
width = rect.Max.X - rect.Min.X
height = rect.Max.Y - rect.Min.Y
cimage = C.newImage(C.int(width), C.int(height))
var cbits []byte
cbitsh := (*reflect.SliceHeader)((unsafe.Pointer)(&cbits))
cbitsh.Data = (uintptr)((unsafe.Pointer)(C.imageBits(cimage)))
cbitsh.Len = width * height * 4 // RGBA
cbitsh.Cap = cbitsh.Len
i := 0
for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
r, g, b, a := img.At(x, y).RGBA()
*(*uint32)(unsafe.Pointer(&cbits[i])) = (a>>8)<<24 | (r>>8)<<16 | (g>>8)<<8 | (b >> 8)
i += 4
}
}
return cimage
}