2014-07-10 11:29:36 +00:00
|
|
|
// Copyright 2014 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 android
|
|
|
|
|
2014-07-10 17:13:00 +00:00
|
|
|
// Go runtime entry point for apps running on android.
|
|
|
|
// Sets up everything the runtime needs and exposes
|
|
|
|
// the entry point to JNI.
|
|
|
|
|
2014-07-10 11:29:36 +00:00
|
|
|
package app
|
|
|
|
|
|
|
|
/*
|
2014-09-03 13:03:00 +00:00
|
|
|
#cgo LDFLAGS: -llog -landroid
|
2014-12-05 18:52:06 +00:00
|
|
|
|
2014-07-10 11:29:36 +00:00
|
|
|
#include <android/log.h>
|
2014-12-05 18:52:06 +00:00
|
|
|
#include <android/asset_manager.h>
|
2014-09-11 23:39:16 +00:00
|
|
|
#include <android/configuration.h>
|
2014-09-03 13:03:00 +00:00
|
|
|
#include <android/native_activity.h>
|
2014-12-05 18:52:06 +00:00
|
|
|
|
2014-09-03 13:03:00 +00:00
|
|
|
#include <jni.h>
|
2014-12-05 18:52:06 +00:00
|
|
|
#include <pthread.h>
|
|
|
|
#include <stdlib.h>
|
2014-07-10 11:29:36 +00:00
|
|
|
|
|
|
|
pthread_cond_t go_started_cond;
|
|
|
|
pthread_mutex_t go_started_mu;
|
|
|
|
int go_started;
|
|
|
|
|
2014-07-31 12:27:33 +00:00
|
|
|
// current_vm is stored to initialize other cgo packages.
|
|
|
|
//
|
|
|
|
// As all the Go packages in a program form a single shared library,
|
|
|
|
// there can only be one JNI_OnLoad function for iniitialization. In
|
|
|
|
// OpenJDK there is JNI_GetCreatedJavaVMs, but this is not available
|
|
|
|
// on android.
|
|
|
|
JavaVM* current_vm;
|
2014-12-10 14:22:32 +00:00
|
|
|
|
|
|
|
// build_auxv builds an ELF auxiliary vector for initializing the Go
|
|
|
|
// runtime. While there does not appear to be any spec for this
|
|
|
|
// format, there are some notes in
|
|
|
|
//
|
|
|
|
// Phrack, V. 0x0b, Issue 0x3a, P. 0x05.
|
|
|
|
// http://phrack.org/issues/58/5.html
|
|
|
|
//
|
|
|
|
// Much of the time on linux the real auxv can be read from the file
|
|
|
|
// /proc/self/auxv, however there are several conditions under which
|
|
|
|
// Android apps cannot read this file (see a note to this effect in
|
|
|
|
// sources/android/cpufeatures/cpu-features.c). So we construct a
|
|
|
|
// fake one, working backwards from what the Go runtime wants to see
|
|
|
|
// as defined by the code in src/runtime/os_linux_GOARCH.c.
|
|
|
|
void build_auxv(uint32_t *auxv, size_t len);
|
2014-09-03 13:03:00 +00:00
|
|
|
*/
|
|
|
|
import "C"
|
2014-09-09 23:51:04 +00:00
|
|
|
import (
|
2014-12-05 18:52:06 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2014-09-11 23:39:16 +00:00
|
|
|
"log"
|
2014-12-05 18:52:06 +00:00
|
|
|
"os"
|
2014-09-09 23:51:04 +00:00
|
|
|
"runtime"
|
|
|
|
"unsafe"
|
2014-09-11 23:39:16 +00:00
|
|
|
|
2014-11-09 21:55:57 +00:00
|
|
|
"golang.org/x/mobile/geom"
|
2014-09-09 23:51:04 +00:00
|
|
|
)
|
2014-07-31 12:27:33 +00:00
|
|
|
|
2014-12-05 18:52:06 +00:00
|
|
|
var assetManager *C.AAssetManager
|
|
|
|
|
2014-09-11 23:39:16 +00:00
|
|
|
//export onCreate
|
|
|
|
func onCreate(activity *C.ANativeActivity) {
|
2014-12-05 18:52:06 +00:00
|
|
|
assetManager = activity.assetManager
|
|
|
|
|
2014-09-11 23:39:16 +00:00
|
|
|
config := C.AConfiguration_new()
|
|
|
|
C.AConfiguration_fromAssetManager(config, activity.assetManager)
|
|
|
|
density := C.AConfiguration_getDensity(config)
|
|
|
|
C.AConfiguration_delete(config)
|
|
|
|
|
|
|
|
var dpi int
|
|
|
|
switch density {
|
|
|
|
case C.ACONFIGURATION_DENSITY_DEFAULT:
|
|
|
|
dpi = 160
|
|
|
|
case C.ACONFIGURATION_DENSITY_LOW,
|
|
|
|
C.ACONFIGURATION_DENSITY_MEDIUM,
|
2014-09-27 22:22:16 +00:00
|
|
|
213, // C.ACONFIGURATION_DENSITY_TV
|
2014-09-11 23:39:16 +00:00
|
|
|
C.ACONFIGURATION_DENSITY_HIGH,
|
|
|
|
320, // ACONFIGURATION_DENSITY_XHIGH
|
|
|
|
480, // ACONFIGURATION_DENSITY_XXHIGH
|
|
|
|
640: // ACONFIGURATION_DENSITY_XXXHIGH
|
|
|
|
dpi = int(density)
|
|
|
|
case C.ACONFIGURATION_DENSITY_NONE:
|
|
|
|
log.Print("android device reports no screen density")
|
|
|
|
dpi = 72
|
|
|
|
default:
|
|
|
|
log.Print("android device reports unknown density: %d", density)
|
|
|
|
dpi = int(density) // This is a guess.
|
|
|
|
}
|
|
|
|
|
2014-10-22 02:50:04 +00:00
|
|
|
geom.PixelsPerPt = float32(dpi) / 72
|
2014-09-11 23:39:16 +00:00
|
|
|
}
|
|
|
|
|
2014-09-03 13:03:00 +00:00
|
|
|
//export onStart
|
|
|
|
func onStart(activity *C.ANativeActivity) {
|
|
|
|
}
|
2014-07-31 12:27:33 +00:00
|
|
|
|
2014-09-03 13:03:00 +00:00
|
|
|
//export onResume
|
|
|
|
func onResume(activity *C.ANativeActivity) {
|
|
|
|
}
|
2014-07-10 11:29:36 +00:00
|
|
|
|
2014-09-03 13:03:00 +00:00
|
|
|
//export onSaveInstanceState
|
|
|
|
func onSaveInstanceState(activity *C.ANativeActivity, outSize *C.size_t) unsafe.Pointer {
|
|
|
|
return nil
|
|
|
|
}
|
2014-07-10 11:29:36 +00:00
|
|
|
|
2014-09-03 13:03:00 +00:00
|
|
|
//export onPause
|
|
|
|
func onPause(activity *C.ANativeActivity) {
|
|
|
|
}
|
2014-07-10 11:29:36 +00:00
|
|
|
|
2014-09-03 13:03:00 +00:00
|
|
|
//export onStop
|
|
|
|
func onStop(activity *C.ANativeActivity) {
|
2014-07-10 11:29:36 +00:00
|
|
|
}
|
|
|
|
|
2014-09-03 13:03:00 +00:00
|
|
|
//export onDestroy
|
|
|
|
func onDestroy(activity *C.ANativeActivity) {
|
2014-07-10 11:29:36 +00:00
|
|
|
}
|
2014-07-31 12:27:33 +00:00
|
|
|
|
2014-09-03 13:03:00 +00:00
|
|
|
//export onWindowFocusChanged
|
|
|
|
func onWindowFocusChanged(activity *C.ANativeActivity, hasFocus int) {
|
|
|
|
}
|
2014-07-10 11:29:36 +00:00
|
|
|
|
2014-09-03 13:03:00 +00:00
|
|
|
//export onNativeWindowCreated
|
|
|
|
func onNativeWindowCreated(activity *C.ANativeActivity, w *C.ANativeWindow) {
|
2014-09-09 23:51:04 +00:00
|
|
|
windowCreated <- w
|
2014-09-03 13:03:00 +00:00
|
|
|
}
|
2014-07-10 11:29:36 +00:00
|
|
|
|
2014-09-03 13:03:00 +00:00
|
|
|
//export onNativeWindowResized
|
|
|
|
func onNativeWindowResized(activity *C.ANativeActivity, window *C.ANativeWindow) {
|
|
|
|
}
|
|
|
|
|
|
|
|
//export onNativeWindowRedrawNeeded
|
|
|
|
func onNativeWindowRedrawNeeded(activity *C.ANativeActivity, window *C.ANativeWindow) {
|
|
|
|
}
|
|
|
|
|
|
|
|
//export onNativeWindowDestroyed
|
|
|
|
func onNativeWindowDestroyed(activity *C.ANativeActivity, window *C.ANativeWindow) {
|
2014-09-09 23:51:04 +00:00
|
|
|
windowDestroyed <- true
|
2014-09-03 13:03:00 +00:00
|
|
|
}
|
|
|
|
|
2014-09-11 23:39:16 +00:00
|
|
|
var queue *C.AInputQueue
|
|
|
|
|
2014-09-03 13:03:00 +00:00
|
|
|
//export onInputQueueCreated
|
2014-09-11 23:39:16 +00:00
|
|
|
func onInputQueueCreated(activity *C.ANativeActivity, q *C.AInputQueue) {
|
|
|
|
queue = q
|
2014-09-03 13:03:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//export onInputQueueDestroyed
|
|
|
|
func onInputQueueDestroyed(activity *C.ANativeActivity, queue *C.AInputQueue) {
|
2014-09-11 23:39:16 +00:00
|
|
|
queue = nil
|
2014-09-03 13:03:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//export onContentRectChanged
|
|
|
|
func onContentRectChanged(activity *C.ANativeActivity, rect *C.ARect) {
|
|
|
|
}
|
|
|
|
|
|
|
|
//export onConfigurationChanged
|
|
|
|
func onConfigurationChanged(activity *C.ANativeActivity) {
|
|
|
|
}
|
|
|
|
|
|
|
|
//export onLowMemory
|
|
|
|
func onLowMemory(activity *C.ANativeActivity) {
|
|
|
|
}
|
|
|
|
|
|
|
|
// JavaInit is an initialization function registered by the package
|
2014-12-11 12:24:19 +00:00
|
|
|
// golang.org/x/mobile/bind/java. It gives the Java language
|
2014-09-03 13:03:00 +00:00
|
|
|
// bindings access to the JNI *JavaVM object.
|
|
|
|
var JavaInit func(javaVM uintptr)
|
|
|
|
|
2014-09-09 23:51:04 +00:00
|
|
|
var (
|
|
|
|
windowDestroyed = make(chan bool)
|
|
|
|
windowCreated = make(chan *C.ANativeWindow)
|
|
|
|
)
|
|
|
|
|
2014-12-05 18:52:06 +00:00
|
|
|
func openAsset(name string) (ReadSeekCloser, error) {
|
|
|
|
cname := C.CString(name)
|
|
|
|
defer C.free(unsafe.Pointer(cname))
|
|
|
|
a := &asset{
|
|
|
|
ptr: C.AAssetManager_open(assetManager, cname, C.AASSET_MODE_UNKNOWN),
|
|
|
|
name: name,
|
|
|
|
}
|
|
|
|
if a.ptr == nil {
|
|
|
|
return nil, a.errorf("open", "bad asset")
|
|
|
|
}
|
|
|
|
return a, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type asset struct {
|
|
|
|
ptr *C.AAsset
|
|
|
|
name string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *asset) errorf(op string, format string, v ...interface{}) error {
|
|
|
|
return &os.PathError{
|
|
|
|
Op: op,
|
|
|
|
Path: a.name,
|
|
|
|
Err: fmt.Errorf(format, v...),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *asset) Read(p []byte) (n int, err error) {
|
|
|
|
n = int(C.AAsset_read(a.ptr, unsafe.Pointer(&p[0]), C.size_t(len(p))))
|
|
|
|
if n == 0 && len(p) > 0 {
|
|
|
|
return 0, io.EOF
|
|
|
|
}
|
|
|
|
if n < 0 {
|
|
|
|
return 0, a.errorf("read", "negative bytes: %d", n)
|
|
|
|
}
|
|
|
|
return n, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *asset) Seek(offset int64, whence int) (int64, error) {
|
|
|
|
// TODO(crawshaw): use AAsset_seek64 if it is available.
|
|
|
|
off := C.AAsset_seek(a.ptr, C.off_t(offset), C.int(whence))
|
|
|
|
if off == -1 {
|
|
|
|
return 0, a.errorf("seek", "bad result for offset=%d, whence=%d", offset, whence)
|
|
|
|
}
|
|
|
|
return int64(off), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *asset) Close() error {
|
|
|
|
C.AAsset_close(a.ptr)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-09-09 23:51:04 +00:00
|
|
|
func run(cb Callbacks) {
|
|
|
|
// We want to keep the event loop on a consistent OS thread.
|
|
|
|
runtime.LockOSThread()
|
|
|
|
|
2014-07-10 11:29:36 +00:00
|
|
|
ctag := C.CString("Go")
|
2014-09-03 13:03:00 +00:00
|
|
|
cstr := C.CString("app.Run")
|
2014-07-10 11:29:36 +00:00
|
|
|
C.__android_log_write(C.ANDROID_LOG_INFO, ctag, cstr)
|
|
|
|
C.free(unsafe.Pointer(ctag))
|
|
|
|
C.free(unsafe.Pointer(cstr))
|
|
|
|
|
2014-09-03 13:03:00 +00:00
|
|
|
if JavaInit != nil {
|
|
|
|
JavaInit(uintptr(unsafe.Pointer(C.current_vm)))
|
|
|
|
}
|
2014-07-31 12:27:33 +00:00
|
|
|
|
2014-07-10 11:29:36 +00:00
|
|
|
// Inform Java that the program is initialized.
|
|
|
|
C.pthread_mutex_lock(&C.go_started_mu)
|
|
|
|
C.go_started = 1
|
|
|
|
C.pthread_cond_signal(&C.go_started_cond)
|
|
|
|
C.pthread_mutex_unlock(&C.go_started_mu)
|
|
|
|
|
2014-09-09 23:51:04 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case w := <-windowCreated:
|
2014-09-11 23:39:16 +00:00
|
|
|
windowDrawLoop(cb, w, queue)
|
2014-09-09 23:51:04 +00:00
|
|
|
}
|
|
|
|
}
|
2014-07-10 11:29:36 +00:00
|
|
|
}
|