Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sugest to reuse architecture direct golang instead of add java code ... #2

Closed
wants to merge 23 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
finish and reload ctx
  • Loading branch information
MatejMagat305 authored Jul 21, 2023
commit ce6241cc66d7e89b2819788510fb998437583a67
45 changes: 23 additions & 22 deletions internal/driver/mobile/app/android.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,21 @@ jint JNI_OnLoad(JavaVM* vm, void* reserved) {

static int main_running = 0;

void set_global(ANativeActivity *activity){
JNIEnv* env = activity->env;
// Note that activity->clazz is mis-named.
current_class = (*env)->GetObjectClass(env, activity->clazz);
current_class = (*env)->NewGlobalRef(env, current_class);
key_rune_method = find_static_method(env, current_class, "getRune", "(III)I");
show_keyboard_method = find_static_method(env, current_class, "showKeyboard", "(I)V");
hide_keyboard_method = find_static_method(env, current_class, "hideKeyboard", "()V");
show_file_open_method = find_static_method(env, current_class, "showFileOpen", "(Ljava/lang/String;)V");
show_file_save_method = find_static_method(env, current_class, "showFileSave", "(Ljava/lang/String;Ljava/lang/String;)V");
finish_method = find_method(env, current_class, "finish", "()V");

setCurrentContext(activity->vm, (*env)->NewGlobalRef(env, activity->clazz));
}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

re-set env at call function ...


// Entry point from our subclassed NativeActivity.
//
// By here, the Go runtime has been initialized (as we are running in
Expand All @@ -78,17 +93,7 @@ void ANativeActivity_onCreate(ANativeActivity *activity, void* savedState, size_
if (!main_running) {
JNIEnv* env = activity->env;

// Note that activity->clazz is mis-named.
current_class = (*env)->GetObjectClass(env, activity->clazz);
current_class = (*env)->NewGlobalRef(env, current_class);
key_rune_method = find_static_method(env, current_class, "getRune", "(III)I");
show_keyboard_method = find_static_method(env, current_class, "showKeyboard", "(I)V");
hide_keyboard_method = find_static_method(env, current_class, "hideKeyboard", "()V");
show_file_open_method = find_static_method(env, current_class, "showFileOpen", "(Ljava/lang/String;)V");
show_file_save_method = find_static_method(env, current_class, "showFileSave", "(Ljava/lang/String;Ljava/lang/String;)V");
finish_method = find_method(env, current_class, "finishActivity", "()V");

setCurrentContext(activity->vm, (*env)->NewGlobalRef(env, activity->clazz));
set_global(activity);

// Set FILESDIR
if (setenv("FILESDIR", activity->internalDataPath, 1) != 0) {
Expand Down Expand Up @@ -160,6 +165,13 @@ static char* initEGLDisplay() {
return NULL;
}

void finish(JNIEnv* env, jobject ctx) {
(*env)->CallVoidMethod(
env,
ctx,
finish_method);
}

char* createEGLSurface(ANativeWindow* window) {
char* err;
EGLint numConfigs, format;
Expand Down Expand Up @@ -206,13 +218,6 @@ char* destroyEGLSurface() {
return NULL;
}

void finish(JNIEnv* env, jobject ctx) {
(*env)->CallVoidMethod(
env,
ctx,
finish_method);
}

int32_t getKeyRune(JNIEnv* env, AInputEvent* e) {
return (int32_t)(*env)->CallStaticIntMethod(
env,
Expand Down Expand Up @@ -281,10 +286,6 @@ void Java_org_golang_app_GoNativeActivity_keyboardDelete(JNIEnv *env, jclass cla
keyboardDelete();
}

void Java_org_golang_app_GoNativeActivity_backPressed(JNIEnv *env, jclass clazz) {
onBackPressed();
}

void Java_org_golang_app_GoNativeActivity_setDarkMode(JNIEnv *env, jclass clazz, jboolean dark) {
setDarkMode((bool)dark);
}
50 changes: 19 additions & 31 deletions internal/driver/mobile/app/android.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ void hideKeyboard(JNIEnv* env);
void showFileOpen(JNIEnv* env, char* mimes);
void showFileSave(JNIEnv* env, char* mimes, char* filename);
void finish(JNIEnv* env, jobject ctx);
void set_global(ANativeActivity *activity);

void Java_org_golang_app_GoNativeActivity_filePickerReturned(JNIEnv *env, jclass clazz, jstring str);
*/
Expand Down Expand Up @@ -78,18 +79,6 @@ var mimeMap = map[string]string{
".txt": "text/plain",
}

// GoBack asks the OS to go to the previous app / activity
func GoBack() {
err := RunOnJVM(func(_, jniEnv, ctx uintptr) error {
env := (*C.JNIEnv)(unsafe.Pointer(jniEnv))
C.finish(env, C.jobject(ctx))
return nil
})
if err != nil {
log.Fatalf("app: %v", err)
}
}

// RunOnJVM runs fn on a new goroutine locked to an OS thread with a JNIEnv.
//
// RunOnJVM blocks until the call to fn is complete. Any Java
Expand Down Expand Up @@ -133,10 +122,12 @@ func callMain(mainPC uintptr) {

//export onStart
func onStart(activity *C.ANativeActivity) {
C.set_global(activity)
}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

set current referencies on start and resume ...


//export onResume
func onResume(activity *C.ANativeActivity) {
C.set_global(activity)
}

//export onSaveInstanceState
Expand All @@ -152,19 +143,6 @@ func onPause(activity *C.ANativeActivity) {
func onStop(activity *C.ANativeActivity) {
}

//export onBackPressed
func onBackPressed() {
k := key.Event{
Code: key.CodeBackButton,
Direction: key.DirPress,
}
log.Println("Logging key event back")
theApp.events.In() <- k

k.Direction = key.DirRelease
theApp.events.In() <- k
}

//export onCreate
func onCreate(activity *C.ANativeActivity) {
// Set the initial configuration.
Expand Down Expand Up @@ -230,6 +208,14 @@ type windowConfig struct {
pixelsPerPt float32
}

func Finish() {
RunOnJVM(func(vm, jniEnv, ctx uintptr) error {
println("finish")
env := (*C.JNIEnv)(unsafe.Pointer(jniEnv)) // not a Go heap pointer
C.finish(env, C.jobject(ctx))
return nil
})
}
func windowConfigRead(activity *C.ANativeActivity) windowConfig {
aconfig := C.AConfiguration_new()
C.AConfiguration_fromAssetManager(aconfig, activity.assetManager)
Expand Down Expand Up @@ -555,6 +541,7 @@ func runInputQueue(vm, jniEnv, ctx uintptr) error {
}
}


func processEvents(env *C.JNIEnv, q *C.AInputQueue) {
var e *C.AInputEvent
for C.AInputQueue_getEvent(q, &e) >= 0 {
Expand Down Expand Up @@ -605,12 +592,13 @@ func processKey(env *C.JNIEnv, e *C.AInputEvent) int {
// Software keyboard input, leaving for scribe/IME.
return 0
}
keyCode := C.AKeyEvent_getKeyCode(e)
if (keyCode == C.AKEYCODE_BACK) {
return 1 // Handle back button press and return handle
} else if (keyCode == C.AKEYCODE_MENU) {
return 1 // Handle menu button press
}
keyCode := C.AKeyEvent_getKeyCode(e)
if (keyCode == C.AKEYCODE_BACK) {
println("back ok")
return 0 // Handle back button press and return handle
} else if (keyCode == C.AKEYCODE_MENU) {
return 1 // Handle menu button press
}
k := key.Event{
Rune: rune(C.getKeyRune(env, e)),
Code: convAndroidKeyCode(int32(keyCode)),
Expand Down
6 changes: 2 additions & 4 deletions internal/driver/mobile/app/darwin_desktop.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,6 @@ func main(f func(App)) {
C.runApp()
}

func GoBack() {
// When simulating mobile there are no other activities open (and we can't just force background)
}

// loop is the primary drawing loop.
//
// After Cocoa has captured the initial OS thread for processing Cocoa
Expand Down Expand Up @@ -107,7 +103,9 @@ func (a *app) loop(ctx C.GLintptr) {
}
}
}
func Finish() {

}
var drawDone = make(chan struct{})

// drawgl is used by Cocoa to occasionally request screen updates.
Expand Down
6 changes: 2 additions & 4 deletions internal/driver/mobile/app/darwin_ios.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,6 @@ var DisplayMetrics struct {
HeightPx int
}

func GoBack() {
// Apple do not permit apps to exit in any way other than user pressing home button / gesture
}

//export setDisplayMetrics
func setDisplayMetrics(width, height int, scale int) {
DisplayMetrics.WidthPx = width
Expand Down Expand Up @@ -150,7 +146,9 @@ func updateConfig(width, height, orientation int32) {
}
theApp.events.In() <- paint.Event{External: true}
}
func Finish() {

}
// touchIDs is the current active touches. The position in the array
// is the ID, the value is the UITouch* pointer value.
//
Expand Down
9 changes: 3 additions & 6 deletions internal/driver/mobile/app/x11.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//go:build (linux && !android) || freebsd
// +build linux,!android freebsd
//go:build linux && !android
// +build linux,!android

package app

Expand All @@ -15,7 +15,6 @@ than screens with touch panels.

/*
#cgo LDFLAGS: -lEGL -lGLESv2 -lX11
#cgo freebsd CFLAGS: -I/usr/local/include/

void createWindow(void);
void processEvents(void);
Expand Down Expand Up @@ -79,11 +78,9 @@ func main(f func(App)) {
}
}
}
func Finish() {

func GoBack() {
// When simulating mobile there are no other activities open (and we can't just force background)
}

//export onResize
func onResize(w, h int) {
// TODO(nigeltao): don't assume 72 DPI. DisplayWidth and DisplayWidthMM
Expand Down