2014-07-10 07:29:36 -04: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
|
|
|
|
|
2015-05-02 13:58:37 -04:00
|
|
|
/*
|
|
|
|
Android Apps are built with -buildmode=c-shared. They are loaded by a
|
|
|
|
running Java process.
|
|
|
|
|
|
|
|
Before any entry point is reached, a global constructor initializes the
|
|
|
|
Go runtime, calling all Go init functions. All cgo calls will block
|
|
|
|
until this is complete. Next JNI_OnLoad is called. When that is
|
|
|
|
complete, one of two entry points is called.
|
|
|
|
|
|
|
|
All-Go apps built using NativeActivity enter at ANativeActivity_onCreate.
|
|
|
|
|
2015-06-27 18:35:21 -04:00
|
|
|
Go libraries (for example, those built with gomobile bind) do not use
|
|
|
|
the app package initialization.
|
2015-05-02 13:58:37 -04:00
|
|
|
*/
|
2014-07-10 13:13:00 -04:00
|
|
|
|
2014-07-10 07:29:36 -04:00
|
|
|
package app
|
|
|
|
|
|
|
|
/*
|
2015-06-29 13:49:38 +10:00
|
|
|
#cgo LDFLAGS: -landroid
|
2014-12-05 13:52:06 -05:00
|
|
|
|
2014-09-11 19:39:16 -04:00
|
|
|
#include <android/configuration.h>
|
2014-09-03 09:03:00 -04:00
|
|
|
#include <android/native_activity.h>
|
2015-05-21 14:24:22 -04:00
|
|
|
#include <time.h>
|
2014-12-05 13:52:06 -05:00
|
|
|
|
2014-09-03 09:03:00 -04:00
|
|
|
#include <jni.h>
|
2014-12-05 13:52:06 -05:00
|
|
|
#include <pthread.h>
|
|
|
|
#include <stdlib.h>
|
2014-07-10 07:29:36 -04:00
|
|
|
|
2015-02-10 19:14:04 +00:00
|
|
|
// current_vm is stored to initialize other cgo packages.
|
2014-07-31 08:27:33 -04:00
|
|
|
//
|
|
|
|
// As all the Go packages in a program form a single shared library,
|
2015-02-10 19:14:04 +00:00
|
|
|
// 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 09:22:32 -05:00
|
|
|
|
2015-02-22 15:27:20 -05:00
|
|
|
// current_ctx is Android's android.context.Context. May be NULL.
|
|
|
|
jobject current_ctx;
|
|
|
|
|
2015-05-05 15:59:45 -04:00
|
|
|
jclass app_find_class(JNIEnv* env, const char* name);
|
2014-09-03 09:03:00 -04:00
|
|
|
*/
|
|
|
|
import "C"
|
2014-09-09 19:51:04 -04:00
|
|
|
import (
|
2014-09-11 19:39:16 -04:00
|
|
|
"log"
|
2014-12-05 13:52:06 -05:00
|
|
|
"os"
|
2015-06-27 18:32:29 -04:00
|
|
|
"runtime"
|
2015-05-21 14:24:22 -04:00
|
|
|
"time"
|
2014-09-09 19:51:04 -04:00
|
|
|
"unsafe"
|
2014-09-11 19:39:16 -04:00
|
|
|
|
2015-04-17 08:05:20 -04:00
|
|
|
"golang.org/x/mobile/app/internal/callfn"
|
2014-09-09 19:51:04 -04:00
|
|
|
)
|
2014-07-31 08:27:33 -04:00
|
|
|
|
2015-04-17 08:05:20 -04:00
|
|
|
//export callMain
|
|
|
|
func callMain(mainPC uintptr) {
|
|
|
|
for _, name := range []string{"TMPDIR", "PATH", "LD_LIBRARY_PATH"} {
|
|
|
|
n := C.CString(name)
|
|
|
|
os.Setenv(name, C.GoString(C.getenv(n)))
|
|
|
|
C.free(unsafe.Pointer(n))
|
|
|
|
}
|
2015-05-21 14:24:22 -04:00
|
|
|
|
|
|
|
// Set timezone.
|
|
|
|
//
|
|
|
|
// Note that Android zoneinfo is stored in /system/usr/share/zoneinfo,
|
|
|
|
// but it is in some kind of packed TZiff file that we do not support
|
|
|
|
// yet. As a stopgap, we build a fixed zone using the tm_zone name.
|
|
|
|
var curtime C.time_t
|
|
|
|
var curtm C.struct_tm
|
|
|
|
C.time(&curtime)
|
|
|
|
C.localtime_r(&curtime, &curtm)
|
|
|
|
tzOffset := int(curtm.tm_gmtoff)
|
|
|
|
tz := C.GoString(curtm.tm_zone)
|
|
|
|
time.Local = time.FixedZone(tz, tzOffset)
|
|
|
|
|
2015-05-02 13:58:37 -04:00
|
|
|
go callfn.CallFn(mainPC)
|
2015-04-17 08:05:20 -04:00
|
|
|
}
|
|
|
|
|
2014-09-11 19:39:16 -04:00
|
|
|
//export onCreate
|
|
|
|
func onCreate(activity *C.ANativeActivity) {
|
|
|
|
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-28 08:22:16 +10:00
|
|
|
213, // C.ACONFIGURATION_DENSITY_TV
|
2014-09-11 19:39:16 -04: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.
|
|
|
|
}
|
|
|
|
|
app: use one thread for both GL and other UI C code.
This change will break Darwin. I have only built and tested this on
desktop linux and Android linux. A follow-up CL will fix Darwin.
Currently, OpenGL gets its own thread, and UI C code (e.g. the Android
event loop, or the X11 event loop) gets its own thread. This relies on
multiple system-provided UI-related C libraries working nicely together,
even when running on different threads. Keeping all the C code on the
one thread seems more sound.
As side-effects:
- In package app/debug, DrawFPS now takes an explicit Config.
- In package app, some callbacks now take an explicit Config.
- In package exp/sprite, Render now takes an explicit Config.
- In package event, there are new events (Config, Draw, Lifecycle),
and an event filter mechanism to replace multiple app Callbacks.
- In package geom, the deprecated Width, Height and PixelsPerPt global
variables were removed in favor of an event.Config that is
explicitly passed around (and does not require mutex-locking).
Converting a geom.Pt to pixels now requires passing a pixelsPerPt.
- In package gl, the Do, Start and Stop functions are removed, as well
as the need to call Start in its own goroutine. There is no longer a
separate GL thread. Instead, package app explicitly performs any GL
work (gl.DoWork) when some is available (gl.WorkAvailable).
- In package gl/glutil, Image.Draw now takes an explicit Config.
Callbacks are no longer executed on 'the UI thread'.
Changing the app programming model from callbacks to events (since a
channel of events works with select) will be a follow-up change.
Change-Id: Id9865cd9ee1c45a98c613e9021a63c17226a64b1
Reviewed-on: https://go-review.googlesource.com/11351
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2015-06-23 16:41:48 +10:00
|
|
|
pixelsPerPt = float32(dpi) / 72
|
2014-09-11 19:39:16 -04:00
|
|
|
}
|
|
|
|
|
2014-09-03 09:03:00 -04:00
|
|
|
//export onStart
|
|
|
|
func onStart(activity *C.ANativeActivity) {
|
|
|
|
}
|
2014-07-31 08:27:33 -04:00
|
|
|
|
2014-09-03 09:03:00 -04:00
|
|
|
//export onResume
|
|
|
|
func onResume(activity *C.ANativeActivity) {
|
|
|
|
}
|
2014-07-10 07:29:36 -04:00
|
|
|
|
2014-09-03 09:03:00 -04:00
|
|
|
//export onSaveInstanceState
|
|
|
|
func onSaveInstanceState(activity *C.ANativeActivity, outSize *C.size_t) unsafe.Pointer {
|
|
|
|
return nil
|
|
|
|
}
|
2014-07-10 07:29:36 -04:00
|
|
|
|
2014-09-03 09:03:00 -04:00
|
|
|
//export onPause
|
|
|
|
func onPause(activity *C.ANativeActivity) {
|
|
|
|
}
|
2014-07-10 07:29:36 -04:00
|
|
|
|
2014-09-03 09:03:00 -04:00
|
|
|
//export onStop
|
|
|
|
func onStop(activity *C.ANativeActivity) {
|
2014-07-10 07:29:36 -04:00
|
|
|
}
|
|
|
|
|
2014-09-03 09:03:00 -04:00
|
|
|
//export onDestroy
|
|
|
|
func onDestroy(activity *C.ANativeActivity) {
|
2014-07-10 07:29:36 -04:00
|
|
|
}
|
2014-07-31 08:27:33 -04:00
|
|
|
|
2014-09-03 09:03:00 -04:00
|
|
|
//export onWindowFocusChanged
|
|
|
|
func onWindowFocusChanged(activity *C.ANativeActivity, hasFocus int) {
|
|
|
|
}
|
2014-07-10 07:29:36 -04:00
|
|
|
|
2014-09-03 09:03:00 -04:00
|
|
|
//export onNativeWindowCreated
|
|
|
|
func onNativeWindowCreated(activity *C.ANativeActivity, w *C.ANativeWindow) {
|
2014-09-09 19:51:04 -04:00
|
|
|
windowCreated <- w
|
2014-09-03 09:03:00 -04:00
|
|
|
}
|
2014-07-10 07:29:36 -04:00
|
|
|
|
2014-09-03 09:03:00 -04:00
|
|
|
//export onNativeWindowResized
|
|
|
|
func onNativeWindowResized(activity *C.ANativeActivity, window *C.ANativeWindow) {
|
|
|
|
}
|
|
|
|
|
|
|
|
//export onNativeWindowRedrawNeeded
|
|
|
|
func onNativeWindowRedrawNeeded(activity *C.ANativeActivity, window *C.ANativeWindow) {
|
2015-04-09 15:00:55 -04:00
|
|
|
windowRedrawNeeded <- window
|
2014-09-03 09:03:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
//export onNativeWindowDestroyed
|
|
|
|
func onNativeWindowDestroyed(activity *C.ANativeActivity, window *C.ANativeWindow) {
|
2014-09-09 19:51:04 -04:00
|
|
|
windowDestroyed <- true
|
2014-09-03 09:03:00 -04:00
|
|
|
}
|
|
|
|
|
2014-09-11 19:39:16 -04:00
|
|
|
var queue *C.AInputQueue
|
|
|
|
|
2014-09-03 09:03:00 -04:00
|
|
|
//export onInputQueueCreated
|
2014-09-11 19:39:16 -04:00
|
|
|
func onInputQueueCreated(activity *C.ANativeActivity, q *C.AInputQueue) {
|
|
|
|
queue = q
|
2014-09-03 09:03:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
//export onInputQueueDestroyed
|
2015-06-01 13:35:56 +10:00
|
|
|
func onInputQueueDestroyed(activity *C.ANativeActivity, q *C.AInputQueue) {
|
2014-09-11 19:39:16 -04:00
|
|
|
queue = nil
|
2014-09-03 09:03:00 -04: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) {
|
|
|
|
}
|
|
|
|
|
app: use one thread for both GL and other UI C code.
This change will break Darwin. I have only built and tested this on
desktop linux and Android linux. A follow-up CL will fix Darwin.
Currently, OpenGL gets its own thread, and UI C code (e.g. the Android
event loop, or the X11 event loop) gets its own thread. This relies on
multiple system-provided UI-related C libraries working nicely together,
even when running on different threads. Keeping all the C code on the
one thread seems more sound.
As side-effects:
- In package app/debug, DrawFPS now takes an explicit Config.
- In package app, some callbacks now take an explicit Config.
- In package exp/sprite, Render now takes an explicit Config.
- In package event, there are new events (Config, Draw, Lifecycle),
and an event filter mechanism to replace multiple app Callbacks.
- In package geom, the deprecated Width, Height and PixelsPerPt global
variables were removed in favor of an event.Config that is
explicitly passed around (and does not require mutex-locking).
Converting a geom.Pt to pixels now requires passing a pixelsPerPt.
- In package gl, the Do, Start and Stop functions are removed, as well
as the need to call Start in its own goroutine. There is no longer a
separate GL thread. Instead, package app explicitly performs any GL
work (gl.DoWork) when some is available (gl.WorkAvailable).
- In package gl/glutil, Image.Draw now takes an explicit Config.
Callbacks are no longer executed on 'the UI thread'.
Changing the app programming model from callbacks to events (since a
channel of events works with select) will be a follow-up change.
Change-Id: Id9865cd9ee1c45a98c613e9021a63c17226a64b1
Reviewed-on: https://go-review.googlesource.com/11351
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2015-06-23 16:41:48 +10:00
|
|
|
// Context holds global OS-specific context.
|
|
|
|
//
|
|
|
|
// Its extra methods are deliberately difficult to access because they must be
|
|
|
|
// used with care. Their use implies the use of cgo, which probably requires
|
|
|
|
// you understand the initialization process in the app package. Also care must
|
|
|
|
// be taken to write both Android, iOS, and desktop-testing versions to
|
|
|
|
// maintain portability.
|
|
|
|
type Context struct{}
|
|
|
|
|
|
|
|
// AndroidContext returns a jobject for the app android.context.Context.
|
|
|
|
func (Context) AndroidContext() unsafe.Pointer {
|
|
|
|
return unsafe.Pointer(C.current_ctx)
|
2015-02-22 15:27:20 -05:00
|
|
|
}
|
|
|
|
|
app: use one thread for both GL and other UI C code.
This change will break Darwin. I have only built and tested this on
desktop linux and Android linux. A follow-up CL will fix Darwin.
Currently, OpenGL gets its own thread, and UI C code (e.g. the Android
event loop, or the X11 event loop) gets its own thread. This relies on
multiple system-provided UI-related C libraries working nicely together,
even when running on different threads. Keeping all the C code on the
one thread seems more sound.
As side-effects:
- In package app/debug, DrawFPS now takes an explicit Config.
- In package app, some callbacks now take an explicit Config.
- In package exp/sprite, Render now takes an explicit Config.
- In package event, there are new events (Config, Draw, Lifecycle),
and an event filter mechanism to replace multiple app Callbacks.
- In package geom, the deprecated Width, Height and PixelsPerPt global
variables were removed in favor of an event.Config that is
explicitly passed around (and does not require mutex-locking).
Converting a geom.Pt to pixels now requires passing a pixelsPerPt.
- In package gl, the Do, Start and Stop functions are removed, as well
as the need to call Start in its own goroutine. There is no longer a
separate GL thread. Instead, package app explicitly performs any GL
work (gl.DoWork) when some is available (gl.WorkAvailable).
- In package gl/glutil, Image.Draw now takes an explicit Config.
Callbacks are no longer executed on 'the UI thread'.
Changing the app programming model from callbacks to events (since a
channel of events works with select) will be a follow-up change.
Change-Id: Id9865cd9ee1c45a98c613e9021a63c17226a64b1
Reviewed-on: https://go-review.googlesource.com/11351
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2015-06-23 16:41:48 +10:00
|
|
|
// JavaVM returns a JNI *JavaVM.
|
|
|
|
func (Context) JavaVM() unsafe.Pointer {
|
|
|
|
return unsafe.Pointer(C.current_vm)
|
2015-02-22 15:27:20 -05:00
|
|
|
}
|
2014-09-03 09:03:00 -04:00
|
|
|
|
2014-09-09 19:51:04 -04:00
|
|
|
var (
|
2015-04-09 15:00:55 -04:00
|
|
|
windowDestroyed = make(chan bool)
|
|
|
|
windowCreated = make(chan *C.ANativeWindow)
|
|
|
|
windowRedrawNeeded = make(chan *C.ANativeWindow)
|
2014-09-09 19:51:04 -04:00
|
|
|
)
|
|
|
|
|
2015-06-27 18:32:29 -04:00
|
|
|
func main(f func(App)) {
|
|
|
|
// Preserve this OS thread for the GL context created in windowDraw.
|
|
|
|
runtime.LockOSThread()
|
|
|
|
|
|
|
|
donec := make(chan struct{})
|
app: use one thread for both GL and other UI C code.
This change will break Darwin. I have only built and tested this on
desktop linux and Android linux. A follow-up CL will fix Darwin.
Currently, OpenGL gets its own thread, and UI C code (e.g. the Android
event loop, or the X11 event loop) gets its own thread. This relies on
multiple system-provided UI-related C libraries working nicely together,
even when running on different threads. Keeping all the C code on the
one thread seems more sound.
As side-effects:
- In package app/debug, DrawFPS now takes an explicit Config.
- In package app, some callbacks now take an explicit Config.
- In package exp/sprite, Render now takes an explicit Config.
- In package event, there are new events (Config, Draw, Lifecycle),
and an event filter mechanism to replace multiple app Callbacks.
- In package geom, the deprecated Width, Height and PixelsPerPt global
variables were removed in favor of an event.Config that is
explicitly passed around (and does not require mutex-locking).
Converting a geom.Pt to pixels now requires passing a pixelsPerPt.
- In package gl, the Do, Start and Stop functions are removed, as well
as the need to call Start in its own goroutine. There is no longer a
separate GL thread. Instead, package app explicitly performs any GL
work (gl.DoWork) when some is available (gl.WorkAvailable).
- In package gl/glutil, Image.Draw now takes an explicit Config.
Callbacks are no longer executed on 'the UI thread'.
Changing the app programming model from callbacks to events (since a
channel of events works with select) will be a follow-up change.
Change-Id: Id9865cd9ee1c45a98c613e9021a63c17226a64b1
Reviewed-on: https://go-review.googlesource.com/11351
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2015-06-23 16:41:48 +10:00
|
|
|
go func() {
|
2015-06-27 18:32:29 -04:00
|
|
|
f(app{})
|
|
|
|
close(donec)
|
app: use one thread for both GL and other UI C code.
This change will break Darwin. I have only built and tested this on
desktop linux and Android linux. A follow-up CL will fix Darwin.
Currently, OpenGL gets its own thread, and UI C code (e.g. the Android
event loop, or the X11 event loop) gets its own thread. This relies on
multiple system-provided UI-related C libraries working nicely together,
even when running on different threads. Keeping all the C code on the
one thread seems more sound.
As side-effects:
- In package app/debug, DrawFPS now takes an explicit Config.
- In package app, some callbacks now take an explicit Config.
- In package exp/sprite, Render now takes an explicit Config.
- In package event, there are new events (Config, Draw, Lifecycle),
and an event filter mechanism to replace multiple app Callbacks.
- In package geom, the deprecated Width, Height and PixelsPerPt global
variables were removed in favor of an event.Config that is
explicitly passed around (and does not require mutex-locking).
Converting a geom.Pt to pixels now requires passing a pixelsPerPt.
- In package gl, the Do, Start and Stop functions are removed, as well
as the need to call Start in its own goroutine. There is no longer a
separate GL thread. Instead, package app explicitly performs any GL
work (gl.DoWork) when some is available (gl.WorkAvailable).
- In package gl/glutil, Image.Draw now takes an explicit Config.
Callbacks are no longer executed on 'the UI thread'.
Changing the app programming model from callbacks to events (since a
channel of events works with select) will be a follow-up change.
Change-Id: Id9865cd9ee1c45a98c613e9021a63c17226a64b1
Reviewed-on: https://go-review.googlesource.com/11351
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2015-06-23 16:41:48 +10:00
|
|
|
}()
|
|
|
|
|
2015-06-27 18:32:29 -04:00
|
|
|
for w := range windowCreated {
|
|
|
|
if windowDraw(w, queue, donec) {
|
|
|
|
return
|
2015-05-05 06:52:19 -07:00
|
|
|
}
|
2014-09-09 19:51:04 -04:00
|
|
|
}
|
app: use one thread for both GL and other UI C code.
This change will break Darwin. I have only built and tested this on
desktop linux and Android linux. A follow-up CL will fix Darwin.
Currently, OpenGL gets its own thread, and UI C code (e.g. the Android
event loop, or the X11 event loop) gets its own thread. This relies on
multiple system-provided UI-related C libraries working nicely together,
even when running on different threads. Keeping all the C code on the
one thread seems more sound.
As side-effects:
- In package app/debug, DrawFPS now takes an explicit Config.
- In package app, some callbacks now take an explicit Config.
- In package exp/sprite, Render now takes an explicit Config.
- In package event, there are new events (Config, Draw, Lifecycle),
and an event filter mechanism to replace multiple app Callbacks.
- In package geom, the deprecated Width, Height and PixelsPerPt global
variables were removed in favor of an event.Config that is
explicitly passed around (and does not require mutex-locking).
Converting a geom.Pt to pixels now requires passing a pixelsPerPt.
- In package gl, the Do, Start and Stop functions are removed, as well
as the need to call Start in its own goroutine. There is no longer a
separate GL thread. Instead, package app explicitly performs any GL
work (gl.DoWork) when some is available (gl.WorkAvailable).
- In package gl/glutil, Image.Draw now takes an explicit Config.
Callbacks are no longer executed on 'the UI thread'.
Changing the app programming model from callbacks to events (since a
channel of events works with select) will be a follow-up change.
Change-Id: Id9865cd9ee1c45a98c613e9021a63c17226a64b1
Reviewed-on: https://go-review.googlesource.com/11351
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2015-06-23 16:41:48 +10:00
|
|
|
panic("unreachable")
|
2014-07-10 07:29:36 -04:00
|
|
|
}
|