| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
package app |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 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 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 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 |
|
| 61 |
}() |
| 62 |
C.runApp() |
| 63 |
panic("unexpected return from app.runApp") |
| 64 |
} |
| 65 |
|
| 66 |
var pixelsPerPt float32 |
| 67 |
var screenScale int |
| 68 |
|
| 69 |
|
| 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 |
|
| 81 |
switch name { |
| 82 |
case "iPad2,5", "iPad2,6", "iPad2,7", "iPad4,4", "iPad4,5", "iPad4,6", "iPad4,7": |
| 83 |
v = 163 |
| 84 |
default: |
| 85 |
v = 132 |
| 86 |
} |
| 87 |
default: |
| 88 |
v = 163 |
| 89 |
} |
| 90 |
|
| 91 |
if v == 0 { |
| 92 |
log.Printf("unknown machine: %s", name) |
| 93 |
v = 163 |
| 94 |
} |
| 95 |
|
| 96 |
pixelsPerPt = v * float32(scale) / 72 |
| 97 |
screenScale = scale |
| 98 |
} |
| 99 |
|
| 100 |
|
| 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 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
var touchIDs [11]uintptr |
| 128 |
|
| 129 |
var touchEvents struct { |
| 130 |
sync.Mutex |
| 131 |
pending []touch.Event |
| 132 |
} |
| 133 |
|
| 134 |
|
| 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 |
|
| 170 |
func lifecycleDead() { theApp.sendLifecycle(lifecycle.StageDead) } |
| 171 |
|
| 172 |
|
| 173 |
func lifecycleAlive() { theApp.sendLifecycle(lifecycle.StageAlive) } |
| 174 |
|
| 175 |
|
| 176 |
func lifecycleVisible() { theApp.sendLifecycle(lifecycle.StageVisible) } |
| 177 |
|
| 178 |
|
| 179 |
func lifecycleFocused() { theApp.sendLifecycle(lifecycle.StageFocused) } |
| 180 |
|
| 181 |
|
| 182 |
func startloop(ctx C.GLintptr) { |
| 183 |
go theApp.loop(ctx) |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 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 |
} |