Debian Codegraph @

app/darwin_ios.go

raw symbols SCIP
1 // Copyright 2015 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 //go:build darwin && ios
6
7 package app
8
9 /*
10 #cgo CFLAGS: -x objective-c -DGL_SILENCE_DEPRECATION -DGLES_SILENCE_DEPRECATION
11 #cgo LDFLAGS: -framework Foundation -framework UIKit -framework GLKit -framework OpenGLES -framework QuartzCore
12 #include <sys/utsname.h>
13 #include <stdint.h>
14 #include <pthread.h>
15 #include <UIKit/UIDevice.h>
16 #import <GLKit/GLKit.h>
17
18 extern struct utsname sysInfo;
19
20 void runApp(void);
21 void makeCurrentContext(GLintptr ctx);
22 void swapBuffers(GLintptr ctx);
23 uint64_t threadID();
24 */
25 import "C"
26 import (
27 "log"
28 "runtime"
29 "strings"
30 "sync"
31
32 "golang.org/x/mobile/event/lifecycle"
33 "golang.org/x/mobile/event/paint"
34 "golang.org/x/mobile/event/size"
35 "golang.org/x/mobile/event/touch"
36 "golang.org/x/mobile/geom"
37 )
38
39 var initThreadID uint64
40
41 func init() {
42 // Lock the goroutine responsible for initialization to an OS thread.
43 // This means the goroutine running main (and calling the run function
44 // below) is locked to the OS thread that started the program. This is
45 // necessary for the correct delivery of UIKit events to the process.
46 //
47 // A discussion on this topic:
48 // https://groups.google.com/forum/#!msg/golang-nuts/IiWZ2hUuLDA/SNKYYZBelsYJ
49 runtime.LockOSThread()
50 initThreadID = uint64(C.threadID())
51 }
52
53 func main(f func(App)) {
54 if tid := uint64(C.threadID()); tid != initThreadID {
55 log.Fatalf("app.Run called on thread %d, but app.init ran on %d", tid, initThreadID)
56 }
57
58 go func() {
59 f(theApp)
60 // TODO(crawshaw): trigger runApp to return
61 }()
62 C.runApp()
63 panic("unexpected return from app.runApp")
64 }
65
66 var pixelsPerPt float32
67 var screenScale int // [UIScreen mainScreen].scale, either 1, 2, or 3.
68
69 //export setScreen
70 func setScreen(scale int) {
71 C.uname(&C.sysInfo)
72 name := C.GoString(&C.sysInfo.machine[0])
73
74 var v float32
75
76 switch {
77 case strings.HasPrefix(name, "iPhone"):
78 v = 163
79 case strings.HasPrefix(name, "iPad"):
80 // TODO: is there a better way to distinguish the iPad Mini?
81 switch name {
82 case "iPad2,5", "iPad2,6", "iPad2,7", "iPad4,4", "iPad4,5", "iPad4,6", "iPad4,7":
83 v = 163 // iPad Mini
84 default:
85 v = 132
86 }
87 default:
88 v = 163 // names like i386 and x86_64 are the simulator
89 }
90
91 if v == 0 {
92 log.Printf("unknown machine: %s", name)
93 v = 163 // emergency fallback
94 }
95
96 pixelsPerPt = v * float32(scale) / 72
97 screenScale = scale
98 }
99
100 //export updateConfig
101 func updateConfig(width, height, orientation int32) {
102 o := size.OrientationUnknown
103 switch orientation {
104 case C.UIDeviceOrientationPortrait, C.UIDeviceOrientationPortraitUpsideDown:
105 o = size.OrientationPortrait
106 case C.UIDeviceOrientationLandscapeLeft, C.UIDeviceOrientationLandscapeRight:
107 o = size.OrientationLandscape
108 }
109 widthPx := screenScale * int(width)
110 heightPx := screenScale * int(height)
111 theApp.eventsIn <- size.Event{
112 WidthPx: widthPx,
113 HeightPx: heightPx,
114 WidthPt: geom.Pt(float32(widthPx) / pixelsPerPt),
115 HeightPt: geom.Pt(float32(heightPx) / pixelsPerPt),
116 PixelsPerPt: pixelsPerPt,
117 Orientation: o,
118 }
119 theApp.eventsIn <- paint.Event{External: true}
120 }
121
122 // touchIDs is the current active touches. The position in the array
123 // is the ID, the value is the UITouch* pointer value.
124 //
125 // It is widely reported that the iPhone can handle up to 5 simultaneous
126 // touch events, while the iPad can handle 11.
127 var touchIDs [11]uintptr
128
129 var touchEvents struct {
130 sync.Mutex
131 pending []touch.Event
132 }
133
134 //export sendTouch
135 func sendTouch(cTouch, cTouchType uintptr, x, y float32) {
136 id := -1
137 for i, val := range touchIDs {
138 if val == cTouch {
139 id = i
140 break
141 }
142 }
143 if id == -1 {
144 for i, val := range touchIDs {
145 if val == 0 {
146 touchIDs[i] = cTouch
147 id = i
148 break
149 }
150 }
151 if id == -1 {
152 panic("out of touchIDs")
153 }
154 }
155
156 t := touch.Type(cTouchType)
157 if t == touch.TypeEnd {
158 touchIDs[id] = 0
159 }
160
161 theApp.eventsIn <- touch.Event{
162 X: x,
163 Y: y,
164 Sequence: touch.Sequence(id),
165 Type: t,
166 }
167 }
168
169 //export lifecycleDead
170 func lifecycleDead() { theApp.sendLifecycle(lifecycle.StageDead) }
171
172 //export lifecycleAlive
173 func lifecycleAlive() { theApp.sendLifecycle(lifecycle.StageAlive) }
174
175 //export lifecycleVisible
176 func lifecycleVisible() { theApp.sendLifecycle(lifecycle.StageVisible) }
177
178 //export lifecycleFocused
179 func lifecycleFocused() { theApp.sendLifecycle(lifecycle.StageFocused) }
180
181 //export startloop
182 func startloop(ctx C.GLintptr) {
183 go theApp.loop(ctx)
184 }
185
186 // loop is the primary drawing loop.
187 //
188 // After UIKit has captured the initial OS thread for processing UIKit
189 // events in runApp, it starts loop on another goroutine. It is locked
190 // to an OS thread for its OpenGL context.
191 func (a *app) loop(ctx C.GLintptr) {
192 runtime.LockOSThread()
193 C.makeCurrentContext(ctx)
194
195 workAvailable := a.worker.WorkAvailable()
196
197 for {
198 select {
199 case <-workAvailable:
200 a.worker.DoWork()
201 case <-theApp.publish:
202 loop1:
203 for {
204 select {
205 case <-workAvailable:
206 a.worker.DoWork()
207 default:
208 break loop1
209 }
210 }
211 C.swapBuffers(ctx)
212 theApp.publishResult <- PublishResult{}
213 }
214 }
215 }