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-07-10 11:29:36 +00:00
|
|
|
#include <android/log.h>
|
2014-09-03 13:03:00 +00:00
|
|
|
#include <android/native_activity.h>
|
2014-07-10 11:29:36 +00:00
|
|
|
#include <pthread.h>
|
2014-09-03 13:03:00 +00:00
|
|
|
#include <jni.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-09-03 13:03:00 +00:00
|
|
|
*/
|
|
|
|
import "C"
|
2014-09-09 23:51:04 +00:00
|
|
|
import (
|
|
|
|
"runtime"
|
|
|
|
"unsafe"
|
|
|
|
)
|
2014-07-31 12:27:33 +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
|
|
|
}
|
|
|
|
|
|
|
|
//export onInputQueueCreated
|
|
|
|
func onInputQueueCreated(activity *C.ANativeActivity, queue *C.AInputQueue) {
|
|
|
|
}
|
|
|
|
|
|
|
|
//export onInputQueueDestroyed
|
|
|
|
func onInputQueueDestroyed(activity *C.ANativeActivity, queue *C.AInputQueue) {
|
|
|
|
}
|
|
|
|
|
|
|
|
//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
|
|
|
|
// code.google.com/p/go.mobile/bind/java. It gives the Java language
|
|
|
|
// 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)
|
|
|
|
)
|
|
|
|
|
|
|
|
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:
|
|
|
|
windowDrawLoop(cb, w)
|
|
|
|
}
|
|
|
|
}
|
2014-07-10 11:29:36 +00:00
|
|
|
}
|