Skip to content
This repository has been archived by the owner on Jan 16, 2024. It is now read-only.

Commit

Permalink
app/internal/window: [Android] remove JNI_OnLoad JNI callback
Browse files Browse the repository at this point in the history
There can only be one JNI_OnLoad callback per JNI library, and the
Gio program may need it for its own purposes.

Gio only used JNI_OnLoad for explicitly registering native methods. Switch
to implicit name based registration and get rid of JNI_OnLoad.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
  • Loading branch information
eliasnaur committed May 5, 2020
1 parent 0c0a22b commit cde651d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 125 deletions.
95 changes: 0 additions & 95 deletions app/internal/window/os_android.c
Expand Up @@ -4,101 +4,6 @@
#include "os_android.h"
#include "_cgo_export.h"

JNIEXPORT jint JNI_OnLoad(JavaVM *vm, void *reserved) {
JNIEnv *env;
if ((*vm)->GetEnv(vm, (void**)&env, JNI_VERSION_1_6) != JNI_OK) {
return -1;
}

jclass viewClass = (*env)->FindClass(env, "org/gioui/GioView");
if (viewClass == NULL) {
return -1;
}

static const JNINativeMethod methods[] = {
{
.name = "runGoMain",
.signature = "([BLandroid/content/Context;)V",
.fnPtr = runGoMain
},
{
.name = "onCreateView",
.signature = "(Lorg/gioui/GioView;)J",
.fnPtr = onCreateView
},
{
.name = "onDestroyView",
.signature = "(J)V",
.fnPtr = onDestroyView
},
{
.name = "onStartView",
.signature = "(J)V",
.fnPtr = onStartView
},
{
.name = "onStopView",
.signature = "(J)V",
.fnPtr = onStopView
},
{
.name = "onSurfaceDestroyed",
.signature = "(J)V",
.fnPtr = onSurfaceDestroyed
},
{
.name = "onSurfaceChanged",
.signature = "(JLandroid/view/Surface;)V",
.fnPtr = onSurfaceChanged
},
{
.name = "onConfigurationChanged",
.signature = "(J)V",
.fnPtr = onConfigurationChanged
},
{
.name = "onWindowInsets",
.signature = "(JIIII)V",
.fnPtr = onWindowInsets
},
{
.name = "onLowMemory",
.signature = "()V",
.fnPtr = onLowMemory
},
{
.name = "onTouchEvent",
.signature = "(JIIIFFIJ)V",
.fnPtr = onTouchEvent
},
{
.name = "onKeyEvent",
.signature = "(JIIJ)V",
.fnPtr = onKeyEvent
},
{
.name = "onFrameCallback",
.signature = "(JJ)V",
.fnPtr = onFrameCallback
},
{
.name = "onBack",
.signature = "(J)Z",
.fnPtr = onBack
},
{
.name = "onFocusChange",
.signature = "(JZ)V",
.fnPtr = onFocusChange
}
};
if ((*env)->RegisterNatives(env, viewClass, methods, sizeof(methods)/sizeof(methods[0])) != 0) {
return -1;
}

return JNI_VERSION_1_6;
}

jint gio_jni_GetEnv(JavaVM *vm, JNIEnv **env, jint version) {
return (*vm)->GetEnv(vm, (void **)env, version);
}
Expand Down
60 changes: 30 additions & 30 deletions app/internal/window/os_android.go
Expand Up @@ -86,8 +86,8 @@ func jniGetStaticMethodID(env *C.JNIEnv, class C.jclass, method, sig string) C.j
return C.gio_jni_GetStaticMethodID(env, class, m, s)
}

//export runGoMain
func runGoMain(env *C.JNIEnv, class C.jclass, jdataDir C.jbyteArray, context C.jobject) {
//export Java_org_gioui_GioView_runGoMain
func Java_org_gioui_GioView_runGoMain(env *C.JNIEnv, class C.jclass, jdataDir C.jbyteArray, context C.jobject) {
if res := C.gio_jni_GetJavaVM(env, &theJVM); res != 0 {
panic("gio: GetJavaVM failed")
}
Expand Down Expand Up @@ -116,8 +116,8 @@ func GetDataDir() string {
return <-dataDirChan
}

//export onCreateView
func onCreateView(env *C.JNIEnv, class C.jclass, view C.jobject) C.jlong {
//export Java_org_gioui_GioView_onCreateView
func Java_org_gioui_GioView_onCreateView(env *C.JNIEnv, class C.jclass, view C.jobject) C.jlong {
view = C.gio_jni_NewGlobalRef(env, view)
w := &window{
view: view,
Expand All @@ -139,42 +139,42 @@ func onCreateView(env *C.JNIEnv, class C.jclass, view C.jobject) C.jlong {
return handle
}

//export onDestroyView
func onDestroyView(env *C.JNIEnv, class C.jclass, handle C.jlong) {
//export Java_org_gioui_GioView_onDestroyView
func Java_org_gioui_GioView_onDestroyView(env *C.JNIEnv, class C.jclass, handle C.jlong) {
w := views[handle]
w.callbacks.SetDriver(nil)
delete(views, handle)
C.gio_jni_DeleteGlobalRef(env, w.view)
w.view = 0
}

//export onStopView
func onStopView(env *C.JNIEnv, class C.jclass, handle C.jlong) {
//export Java_org_gioui_GioView_onStopView
func Java_org_gioui_GioView_onStopView(env *C.JNIEnv, class C.jclass, handle C.jlong) {
w := views[handle]
w.started = false
w.setStage(system.StagePaused)
}

//export onStartView
func onStartView(env *C.JNIEnv, class C.jclass, handle C.jlong) {
//export Java_org_gioui_GioView_onStartView
func Java_org_gioui_GioView_onStartView(env *C.JNIEnv, class C.jclass, handle C.jlong) {
w := views[handle]
w.started = true
if w.aNativeWindow() != nil {
w.setVisible()
}
}

//export onSurfaceDestroyed
func onSurfaceDestroyed(env *C.JNIEnv, class C.jclass, handle C.jlong) {
//export Java_org_gioui_GioView_onSurfaceDestroyed
func Java_org_gioui_GioView_onSurfaceDestroyed(env *C.JNIEnv, class C.jclass, handle C.jlong) {
w := views[handle]
w.mu.Lock()
w.win = nil
w.mu.Unlock()
w.setStage(system.StagePaused)
}

//export onSurfaceChanged
func onSurfaceChanged(env *C.JNIEnv, class C.jclass, handle C.jlong, surf C.jobject) {
//export Java_org_gioui_GioView_onSurfaceChanged
func Java_org_gioui_GioView_onSurfaceChanged(env *C.JNIEnv, class C.jclass, handle C.jlong, surf C.jobject) {
w := views[handle]
w.mu.Lock()
w.win = C.ANativeWindow_fromSurface(env, surf)
Expand All @@ -184,23 +184,23 @@ func onSurfaceChanged(env *C.JNIEnv, class C.jclass, handle C.jlong, surf C.jobj
}
}

//export onLowMemory
func onLowMemory() {
//export Java_org_gioui_GioView_onLowMemory
func Java_org_gioui_GioView_onLowMemory() {
runtime.GC()
debug.FreeOSMemory()
}

//export onConfigurationChanged
func onConfigurationChanged(env *C.JNIEnv, class C.jclass, view C.jlong) {
//export Java_org_gioui_GioView_onConfigurationChanged
func Java_org_gioui_GioView_onConfigurationChanged(env *C.JNIEnv, class C.jclass, view C.jlong) {
w := views[view]
w.loadConfig(env, class)
if w.stage >= system.StageRunning {
w.draw(true)
}
}

//export onFrameCallback
func onFrameCallback(env *C.JNIEnv, class C.jclass, view C.jlong, nanos C.jlong) {
//export Java_org_gioui_GioView_onFrameCallback
func Java_org_gioui_GioView_onFrameCallback(env *C.JNIEnv, class C.jclass, view C.jlong, nanos C.jlong) {
w, exist := views[view]
if !exist {
return
Expand All @@ -219,8 +219,8 @@ func onFrameCallback(env *C.JNIEnv, class C.jclass, view C.jlong, nanos C.jlong)
}
}

//export onBack
func onBack(env *C.JNIEnv, class C.jclass, view C.jlong) C.jboolean {
//export Java_org_gioui_GioView_onBack
func Java_org_gioui_GioView_onBack(env *C.JNIEnv, class C.jclass, view C.jlong) C.jboolean {
w := views[view]
ev := &system.CommandEvent{Type: system.CommandBack}
w.callbacks.Event(ev)
Expand All @@ -230,14 +230,14 @@ func onBack(env *C.JNIEnv, class C.jclass, view C.jlong) C.jboolean {
return C.JNI_FALSE
}

//export onFocusChange
func onFocusChange(env *C.JNIEnv, class C.jclass, view C.jlong, focus C.jboolean) {
//export Java_org_gioui_GioView_onFocusChange
func Java_org_gioui_GioView_onFocusChange(env *C.JNIEnv, class C.jclass, view C.jlong, focus C.jboolean) {
w := views[view]
w.callbacks.Event(key.FocusEvent{Focus: focus == C.JNI_TRUE})
}

//export onWindowInsets
func onWindowInsets(env *C.JNIEnv, class C.jclass, view C.jlong, top, right, bottom, left C.jint) {
//export Java_org_gioui_GioView_onWindowInsets
func Java_org_gioui_GioView_onWindowInsets(env *C.JNIEnv, class C.jclass, view C.jlong, top, right, bottom, left C.jint) {
w := views[view]
w.insets = system.Insets{
Top: unit.Px(float32(top)),
Expand Down Expand Up @@ -383,8 +383,8 @@ func convertKeyCode(code C.jint) (string, bool) {
return n, true
}

//export onKeyEvent
func onKeyEvent(env *C.JNIEnv, class C.jclass, handle C.jlong, keyCode, r C.jint, t C.jlong) {
//export Java_org_gioui_GioView_onKeyEvent
func Java_org_gioui_GioView_onKeyEvent(env *C.JNIEnv, class C.jclass, handle C.jlong, keyCode, r C.jint, t C.jlong) {
w := views[handle]
if n, ok := convertKeyCode(keyCode); ok {
w.callbacks.Event(key.Event{Name: n})
Expand All @@ -394,8 +394,8 @@ func onKeyEvent(env *C.JNIEnv, class C.jclass, handle C.jlong, keyCode, r C.jint
}
}

//export onTouchEvent
func onTouchEvent(env *C.JNIEnv, class C.jclass, handle C.jlong, action, pointerID, tool C.jint, x, y C.jfloat, jbtns C.jint, t C.jlong) {
//export Java_org_gioui_GioView_onTouchEvent
func Java_org_gioui_GioView_onTouchEvent(env *C.JNIEnv, class C.jclass, handle C.jlong, action, pointerID, tool C.jint, x, y C.jfloat, jbtns C.jint, t C.jlong) {
w := views[handle]
var typ pointer.Type
switch action {
Expand Down

0 comments on commit cde651d

Please sign in to comment.