| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 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 // +build android | |
| 6 | |
| 7 // Go runtime entry point for apps running on android. | |
| 8 // Sets up everything the runtime needs and exposes | |
| 9 // the entry point to JNI. | |
| 10 | |
| 11 package app | |
| 12 | |
| 13 /* | |
| 14 #cgo LDFLAGS: -llog -landroid | |
| 15 | |
| 16 #include <android/log.h> | |
| 17 #include <android/asset_manager.h> | |
| 18 #include <android/configuration.h> | |
| 19 #include <android/native_activity.h> | |
| 20 | |
| 21 #include <jni.h> | |
| 22 #include <pthread.h> | |
| 23 #include <stdlib.h> | |
| 24 | |
| 25 pthread_cond_t go_started_cond; | |
| 26 pthread_mutex_t go_started_mu; | |
| 27 int go_started; | |
| 28 | |
| 29 // current_vm is stored to initialize other cgo packages. | |
| 30 // | |
| 31 // As all the Go packages in a program form a single shared library, | |
| 32 // there can only be one JNI_OnLoad function for iniitialization. In | |
| 33 // OpenJDK there is JNI_GetCreatedJavaVMs, but this is not available | |
| 34 // on android. | |
| 35 JavaVM* current_vm; | |
| 36 | |
| 37 // current_ctx is Android's android.context.Context. May be NULL. | |
| 38 jobject current_ctx; | |
| 39 | |
| 40 // current_native_activity is the Android ANativeActivity. May be NULL. | |
| 41 ANativeActivity* current_native_activity; | |
| 42 | |
| 43 // asset_manager is the asset manager of the app. | |
| 44 // For all-Go app, this is initialized in onCreate. | |
| 45 // For go library app, this is set from the context passed to Go.run. | |
| 46 AAssetManager* asset_manager; | |
| 47 | |
| 48 // build_auxv builds an ELF auxiliary vector for initializing the Go | |
| 49 // runtime. While there does not appear to be any spec for this | |
| 50 // format, there are some notes in | |
| 51 // | |
| 52 // Phrack, V. 0x0b, Issue 0x3a, P. 0x05. | |
| 53 // http://phrack.org/issues/58/5.html | |
| 54 // | |
| 55 // Much of the time on linux the real auxv can be read from the file | |
| 56 // /proc/self/auxv, however there are several conditions under which | |
| 57 // Android apps cannot read this file (see a note to this effect in | |
| 58 // sources/android/cpufeatures/cpu-features.c). So we construct a | |
| 59 // fake one, working backwards from what the Go runtime wants to see | |
| 60 // as defined by the code in src/runtime/os_linux_GOARCH.c. | |
| 61 void build_auxv(uint32_t *auxv, size_t len); | |
| 62 */ | |
| 63 import "C" | |
| 64 import ( | |
| 65 "fmt" | |
| 66 "io" | |
| 67 "log" | |
| 68 "os" | |
| 69 "runtime" | |
| 70 "unsafe" | |
| 71 | |
| 72 "golang.org/x/mobile/geom" | |
| 73 ) | |
| 74 | |
| 75 //export onCreate | |
| 76 func onCreate(activity *C.ANativeActivity) { | |
| 77 C.asset_manager = activity.assetManager | |
| 78 | |
| 79 config := C.AConfiguration_new() | |
| 80 C.AConfiguration_fromAssetManager(config, activity.assetManager) | |
| 81 density := C.AConfiguration_getDensity(config) | |
| 82 C.AConfiguration_delete(config) | |
| 83 | |
| 84 var dpi int | |
| 85 switch density { | |
| 86 case C.ACONFIGURATION_DENSITY_DEFAULT: | |
| 87 dpi = 160 | |
| 88 case C.ACONFIGURATION_DENSITY_LOW, | |
| 89 C.ACONFIGURATION_DENSITY_MEDIUM, | |
| 90 213, // C.ACONFIGURATION_DENSITY_TV | |
| 91 C.ACONFIGURATION_DENSITY_HIGH, | |
| 92 320, // ACONFIGURATION_DENSITY_XHIGH | |
| 93 480, // ACONFIGURATION_DENSITY_XXHIGH | |
| 94 640: // ACONFIGURATION_DENSITY_XXXHIGH | |
| 95 dpi = int(density) | |
| 96 case C.ACONFIGURATION_DENSITY_NONE: | |
| 97 log.Print("android device reports no screen density") | |
| 98 dpi = 72 | |
| 99 default: | |
| 100 log.Print("android device reports unknown density: %d", density) | |
| 101 dpi = int(density) // This is a guess. | |
| 102 } | |
| 103 | |
| 104 geom.PixelsPerPt = float32(dpi) / 72 | |
| 105 } | |
| 106 | |
| 107 //export onStart | |
| 108 func onStart(activity *C.ANativeActivity) { | |
| 109 } | |
| 110 | |
| 111 //export onResume | |
| 112 func onResume(activity *C.ANativeActivity) { | |
| 113 } | |
| 114 | |
| 115 //export onSaveInstanceState | |
| 116 func onSaveInstanceState(activity *C.ANativeActivity, outSize *C.size_t) unsafe.
Pointer { | |
| 117 return nil | |
| 118 } | |
| 119 | |
| 120 //export onPause | |
| 121 func onPause(activity *C.ANativeActivity) { | |
| 122 } | |
| 123 | |
| 124 //export onStop | |
| 125 func onStop(activity *C.ANativeActivity) { | |
| 126 } | |
| 127 | |
| 128 //export onDestroy | |
| 129 func onDestroy(activity *C.ANativeActivity) { | |
| 130 } | |
| 131 | |
| 132 //export onWindowFocusChanged | |
| 133 func onWindowFocusChanged(activity *C.ANativeActivity, hasFocus int) { | |
| 134 } | |
| 135 | |
| 136 //export onNativeWindowCreated | |
| 137 func onNativeWindowCreated(activity *C.ANativeActivity, w *C.ANativeWindow) { | |
| 138 windowCreated <- w | |
| 139 } | |
| 140 | |
| 141 //export onNativeWindowResized | |
| 142 func onNativeWindowResized(activity *C.ANativeActivity, window *C.ANativeWindow)
{ | |
| 143 } | |
| 144 | |
| 145 //export onNativeWindowRedrawNeeded | |
| 146 func onNativeWindowRedrawNeeded(activity *C.ANativeActivity, window *C.ANativeWi
ndow) { | |
| 147 } | |
| 148 | |
| 149 //export onNativeWindowDestroyed | |
| 150 func onNativeWindowDestroyed(activity *C.ANativeActivity, window *C.ANativeWindo
w) { | |
| 151 windowDestroyed <- true | |
| 152 } | |
| 153 | |
| 154 var queue *C.AInputQueue | |
| 155 | |
| 156 //export onInputQueueCreated | |
| 157 func onInputQueueCreated(activity *C.ANativeActivity, q *C.AInputQueue) { | |
| 158 queue = q | |
| 159 } | |
| 160 | |
| 161 //export onInputQueueDestroyed | |
| 162 func onInputQueueDestroyed(activity *C.ANativeActivity, queue *C.AInputQueue) { | |
| 163 queue = nil | |
| 164 } | |
| 165 | |
| 166 //export onContentRectChanged | |
| 167 func onContentRectChanged(activity *C.ANativeActivity, rect *C.ARect) { | |
| 168 } | |
| 169 | |
| 170 //export onConfigurationChanged | |
| 171 func onConfigurationChanged(activity *C.ANativeActivity) { | |
| 172 } | |
| 173 | |
| 174 //export onLowMemory | |
| 175 func onLowMemory(activity *C.ANativeActivity) { | |
| 176 } | |
| 177 | |
| 178 type androidState struct { | |
| 179 } | |
| 180 | |
| 181 func (androidState) JavaVM() unsafe.Pointer { | |
| 182 return unsafe.Pointer(C.current_vm) | |
| 183 } | |
| 184 | |
| 185 func (androidState) AndroidContext() unsafe.Pointer { | |
| 186 return unsafe.Pointer(C.current_ctx) | |
| 187 } | |
| 188 | |
| 189 var ( | |
| 190 windowDestroyed = make(chan bool) | |
| 191 windowCreated = make(chan *C.ANativeWindow) | |
| 192 ) | |
| 193 | |
| 194 func openAsset(name string) (ReadSeekCloser, error) { | |
| 195 cname := C.CString(name) | |
| 196 defer C.free(unsafe.Pointer(cname)) | |
| 197 a := &asset{ | |
| 198 ptr: C.AAssetManager_open(C.asset_manager, cname, C.AASSET_MODE
_UNKNOWN), | |
| 199 name: name, | |
| 200 } | |
| 201 if a.ptr == nil { | |
| 202 return nil, a.errorf("open", "bad asset") | |
| 203 } | |
| 204 return a, nil | |
| 205 } | |
| 206 | |
| 207 type asset struct { | |
| 208 ptr *C.AAsset | |
| 209 name string | |
| 210 } | |
| 211 | |
| 212 func (a *asset) errorf(op string, format string, v ...interface{}) error { | |
| 213 return &os.PathError{ | |
| 214 Op: op, | |
| 215 Path: a.name, | |
| 216 Err: fmt.Errorf(format, v...), | |
| 217 } | |
| 218 } | |
| 219 | |
| 220 func (a *asset) Read(p []byte) (n int, err error) { | |
| 221 n = int(C.AAsset_read(a.ptr, unsafe.Pointer(&p[0]), C.size_t(len(p)))) | |
| 222 if n == 0 && len(p) > 0 { | |
| 223 return 0, io.EOF | |
| 224 } | |
| 225 if n < 0 { | |
| 226 return 0, a.errorf("read", "negative bytes: %d", n) | |
| 227 } | |
| 228 return n, nil | |
| 229 } | |
| 230 | |
| 231 func (a *asset) Seek(offset int64, whence int) (int64, error) { | |
| 232 // TODO(crawshaw): use AAsset_seek64 if it is available. | |
| 233 off := C.AAsset_seek(a.ptr, C.off_t(offset), C.int(whence)) | |
| 234 if off == -1 { | |
| 235 return 0, a.errorf("seek", "bad result for offset=%d, whence=%d"
, offset, whence) | |
| 236 } | |
| 237 return int64(off), nil | |
| 238 } | |
| 239 | |
| 240 func (a *asset) Close() error { | |
| 241 C.AAsset_close(a.ptr) | |
| 242 return nil | |
| 243 } | |
| 244 | |
| 245 func runStart(cb Callbacks) { | |
| 246 State = androidState{} | |
| 247 | |
| 248 if cb.Start != nil { | |
| 249 cb.Start() | |
| 250 } | |
| 251 } | |
| 252 | |
| 253 // notifyInitDone informs Java that the program is initialized. | |
| 254 // A NativeActivity will not create a window until this is called. | |
| 255 func notifyInitDone() { | |
| 256 C.pthread_mutex_lock(&C.go_started_mu) | |
| 257 C.go_started = 1 | |
| 258 C.pthread_cond_signal(&C.go_started_cond) | |
| 259 C.pthread_mutex_unlock(&C.go_started_mu) | |
| 260 } | |
| 261 | |
| 262 func run(cb Callbacks) { | |
| 263 // We want to keep the event loop on a consistent OS thread. | |
| 264 runtime.LockOSThread() | |
| 265 | |
| 266 ctag := C.CString("Go") | |
| 267 cstr := C.CString("app.Run") | |
| 268 C.__android_log_write(C.ANDROID_LOG_INFO, ctag, cstr) | |
| 269 C.free(unsafe.Pointer(ctag)) | |
| 270 C.free(unsafe.Pointer(cstr)) | |
| 271 | |
| 272 if C.current_native_activity == nil { | |
| 273 runStart(cb) | |
| 274 notifyInitDone() | |
| 275 select {} | |
| 276 } else { | |
| 277 notifyInitDone() | |
| 278 windowDrawLoop(cb, <-windowCreated, queue) | |
| 279 } | |
| 280 } | |
| OLD | NEW |