forked from fyne-io/fyne
-
Notifications
You must be signed in to change notification settings - Fork 1
/
darwin_ios.go
303 lines (256 loc) · 7.26 KB
/
darwin_ios.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
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build darwin
// +build ios
package app
/*
#cgo CFLAGS: -x objective-c -DGL_SILENCE_DEPRECATION
#cgo LDFLAGS: -framework Foundation -framework UIKit -framework MobileCoreServices -framework GLKit -framework OpenGLES -framework QuartzCore -framework UserNotifications
#include <sys/utsname.h>
#include <stdint.h>
#include <pthread.h>
#include <UIKit/UIDevice.h>
#import <GLKit/GLKit.h>
extern struct utsname sysInfo;
void runApp(void);
void makeCurrentContext(GLintptr ctx);
void swapBuffers(GLintptr ctx);
uint64_t threadID();
UIEdgeInsets getDevicePadding();
void showKeyboard(int keyboardType);
void hideKeyboard();
void showFileOpenPicker(char* mimes, char *exts);
void closeFileResource(void* urlPtr);
*/
import "C"
import (
"log"
"runtime"
"strings"
"sync"
"time"
"unsafe"
"github.com/fyne-io/mobile/event/lifecycle"
"github.com/fyne-io/mobile/event/paint"
"github.com/fyne-io/mobile/event/size"
"github.com/fyne-io/mobile/event/touch"
"github.com/fyne-io/mobile/geom"
)
var initThreadID uint64
func init() {
// Lock the goroutine responsible for initialization to an OS thread.
// This means the goroutine running main (and calling the run function
// below) is locked to the OS thread that started the program. This is
// necessary for the correct delivery of UIKit events to the process.
//
// A discussion on this topic:
// https://groups.google.com/forum/#!msg/golang-nuts/IiWZ2hUuLDA/SNKYYZBelsYJ
runtime.LockOSThread()
initThreadID = uint64(C.threadID())
}
func main(f func(App)) {
//if tid := uint64(C.threadID()); tid != initThreadID {
// log.Fatalf("app.Run called on thread %d, but app.init ran on %d", tid, initThreadID)
//}
go func() {
f(theApp)
// TODO(crawshaw): trigger runApp to return
}()
C.runApp()
panic("unexpected return from app.runApp")
}
var pixelsPerPt float32
var screenScale int // [UIScreen mainScreen].scale, either 1, 2, or 3.
var DisplayMetrics struct {
WidthPx int
HeightPx int
}
//export setDisplayMetrics
func setDisplayMetrics(width, height int, scale int) {
DisplayMetrics.WidthPx = width
DisplayMetrics.HeightPx = height
}
//export setScreen
func setScreen(scale int) {
C.uname(&C.sysInfo)
name := C.GoString(&C.sysInfo.machine[0])
var v float32
switch {
case strings.HasPrefix(name, "iPhone"):
v = 163
case strings.HasPrefix(name, "iPad"):
// TODO: is there a better way to distinguish the iPad Mini?
switch name {
case "iPad2,5", "iPad2,6", "iPad2,7", "iPad4,4", "iPad4,5", "iPad4,6", "iPad4,7":
v = 163 // iPad Mini
default:
v = 132
}
default:
v = 163 // names like i386 and x86_64 are the simulator
}
if v == 0 {
log.Printf("unknown machine: %s", name)
v = 163 // emergency fallback
}
pixelsPerPt = v * float32(scale) / 72
screenScale = scale
}
//export updateConfig
func updateConfig(width, height, orientation int32) {
o := size.OrientationUnknown
switch orientation {
case C.UIDeviceOrientationPortrait, C.UIDeviceOrientationPortraitUpsideDown:
o = size.OrientationPortrait
case C.UIDeviceOrientationLandscapeLeft, C.UIDeviceOrientationLandscapeRight:
o = size.OrientationLandscape
width, height = height, width
}
insets := C.getDevicePadding()
theApp.eventsIn <- size.Event{
WidthPx: int(width),
HeightPx: int(height),
WidthPt: geom.Pt(float32(width) / pixelsPerPt),
HeightPt: geom.Pt(float32(height) / pixelsPerPt),
InsetTopPx: int(float32(insets.top) * float32(screenScale)),
InsetBottomPx: int(float32(insets.bottom) * float32(screenScale)),
InsetLeftPx: int(float32(insets.left) * float32(screenScale)),
InsetRightPx: int(float32(insets.right) * float32(screenScale)),
PixelsPerPt: pixelsPerPt,
Orientation: o,
}
theApp.eventsIn <- paint.Event{External: true}
}
// touchIDs is the current active touches. The position in the array
// is the ID, the value is the UITouch* pointer value.
//
// It is widely reported that the iPhone can handle up to 5 simultaneous
// touch events, while the iPad can handle 11.
var touchIDs [11]uintptr
var touchEvents struct {
sync.Mutex
pending []touch.Event
}
//export sendTouch
func sendTouch(cTouch, cTouchType uintptr, x, y float32) {
id := -1
for i, val := range touchIDs {
if val == cTouch {
id = i
break
}
}
if id == -1 {
for i, val := range touchIDs {
if val == 0 {
touchIDs[i] = cTouch
id = i
break
}
}
if id == -1 {
panic("out of touchIDs")
}
}
t := touch.Type(cTouchType)
if t == touch.TypeEnd {
touchIDs[id] = 0
}
theApp.eventsIn <- touch.Event{
X: x,
Y: y,
Sequence: touch.Sequence(id),
Type: t,
}
}
//export lifecycleDead
func lifecycleDead() { theApp.sendLifecycle(lifecycle.StageDead) }
//export lifecycleAlive
func lifecycleAlive() { theApp.sendLifecycle(lifecycle.StageAlive) }
//export lifecycleVisible
func lifecycleVisible() { theApp.sendLifecycle(lifecycle.StageVisible) }
//export lifecycleFocused
func lifecycleFocused() { theApp.sendLifecycle(lifecycle.StageFocused) }
//export drawloop
func drawloop() {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
for workAvailable := theApp.worker.WorkAvailable(); ; {
select {
case <-workAvailable:
theApp.worker.DoWork()
case <-theApp.publish:
theApp.publishResult <- PublishResult{}
return
case <-time.After(100 * time.Millisecond): // incase the method blocked!!
return
}
}
}
//export startloop
func startloop(ctx C.GLintptr) {
go theApp.loop(ctx)
}
// loop is the primary drawing loop.
//
// After UIKit has captured the initial OS thread for processing UIKit
// events in runApp, it starts loop on another goroutine. It is locked
// to an OS thread for its OpenGL context.
func (a *app) loop(ctx C.GLintptr) {
runtime.LockOSThread()
C.makeCurrentContext(ctx)
workAvailable := a.worker.WorkAvailable()
for {
select {
case <-workAvailable:
a.worker.DoWork()
case <-theApp.publish:
loop1:
for {
select {
case <-workAvailable:
a.worker.DoWork()
default:
break loop1
}
}
C.swapBuffers(ctx)
theApp.publishResult <- PublishResult{}
}
}
}
// driverShowVirtualKeyboard requests the driver to show a virtual keyboard for text input
func driverShowVirtualKeyboard(keyboard KeyboardType) {
C.showKeyboard(C.int(int32(keyboard)))
}
// driverHideVirtualKeyboard requests the driver to hide any visible virtual keyboard
func driverHideVirtualKeyboard() {
C.hideKeyboard()
}
var fileCallback func(string, func())
//export filePickerReturned
func filePickerReturned(str *C.char, urlPtr unsafe.Pointer) {
if fileCallback == nil {
return
}
fileCallback(C.GoString(str), func() {
C.closeFileResource(urlPtr)
})
fileCallback = nil
}
func driverShowFileOpenPicker(callback func(string, func()), filter *FileFilter) {
fileCallback = callback
mimes := strings.Join(filter.MimeTypes, "|")
// extensions must have the '.' removed for UTI lookups on iOS
extList := []string{}
for _, ext := range filter.Extensions {
extList = append(extList, ext[1:])
}
exts := strings.Join(extList, "|")
mimeStr := C.CString(mimes)
defer C.free(unsafe.Pointer(mimeStr))
extStr := C.CString(exts)
defer C.free(unsafe.Pointer(extStr))
C.showFileOpenPicker(mimeStr, extStr)
}