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 8 commits
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
49 changes: 27 additions & 22 deletions internal/driver/mobile/app/android.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,25 @@ 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 ...


void delete_ref(ANativeActivity *activity){
(*env)->DeleteGlobalRef(activity->env, current_class);
}

// Entry point from our subclassed NativeActivity.
//
// By here, the Go runtime has been initialized (as we are running in
Expand All @@ -78,17 +97,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 +169,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 +222,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 +290,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);
}
69 changes: 34 additions & 35 deletions internal/driver/mobile/app/android.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ 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 delete_ref(ANativeActivity *activity);

void Java_org_golang_app_GoNativeActivity_filePickerReturned(JNIEnv *env, jclass clazz, jstring str);
*/
Expand Down Expand Up @@ -78,18 +80,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 +123,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 @@ -146,23 +138,12 @@ func onSaveInstanceState(activity *C.ANativeActivity, outSize *C.size_t) unsafe.

//export onPause
func onPause(activity *C.ANativeActivity) {
C.delete_ref(activity)
}

//export onStop
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
C.delete_ref(activity)
}

//export onCreate
Expand All @@ -177,6 +158,7 @@ func onCreate(activity *C.ANativeActivity) {

//export onDestroy
func onDestroy(activity *C.ANativeActivity) {
C.delete_ref(activity)
activityDestroyed <- struct{}{}
}

Expand Down Expand Up @@ -230,6 +212,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,21 +545,22 @@ 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 {
if C.AInputQueue_preDispatchEvent(q, e) != 0 {
continue
}
processEvent(env, e)
C.AInputQueue_finishEvent(q, e, 0)
handle := processEvent(env, e)
C.AInputQueue_finishEvent(q, e, C.int(handle))
}
}

func processEvent(env *C.JNIEnv, e *C.AInputEvent) {
func processEvent(env *C.JNIEnv, e *C.AInputEvent) int {
switch C.AInputEvent_getType(e) {
case C.AINPUT_EVENT_TYPE_KEY:
processKey(env, e)
return processKey(env, e)
case C.AINPUT_EVENT_TYPE_MOTION:
// At most one of the events in this batch is an up or down event; get its index and change.
upDownIndex := C.size_t(C.AMotionEvent_getAction(e)&C.AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) >> C.AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT
Expand All @@ -596,21 +587,28 @@ func processEvent(env *C.JNIEnv, e *C.AInputEvent) {
default:
log.Printf("unknown input event, type=%d", C.AInputEvent_getType(e))
}
return 0
}

func processKey(env *C.JNIEnv, e *C.AInputEvent) {
func processKey(env *C.JNIEnv, e *C.AInputEvent) int {
deviceID := C.AInputEvent_getDeviceId(e)
if deviceID == 0 {
// Software keyboard input, leaving for scribe/IME.
return
return 0
}
keyCode := C.AKeyEvent_getKeyCode(e)
if (keyCode == C.AKEYCODE_BACK) {
println("back ok")
return 1 // Handle back button press and return handle
Copy link
Author

Choose a reason for hiding this comment

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

well here you can put send event to your architekture ...

} else if (keyCode == C.AKEYCODE_MENU) {
return 1 // Handle menu button press
}

k := key.Event{
Rune: rune(C.getKeyRune(env, e)),
Code: convAndroidKeyCode(int32(C.AKeyEvent_getKeyCode(e))),
Code: convAndroidKeyCode(int32(keyCode)),
}
if k.Rune >= '0' && k.Rune <= '9' { // GBoard generates key events for numbers, but we see them in textChanged
return
return 0
}
switch C.AKeyEvent_getAction(e) {
case C.AKEY_STATE_DOWN:
Expand All @@ -621,7 +619,8 @@ func processKey(env *C.JNIEnv, e *C.AInputEvent) {
k.Direction = key.DirNone
}
// TODO(crawshaw): set Modifiers.
theApp.events.In() <- k
go func() {theApp.events.In() <- k}()
return 0
}

func eglGetError() string {
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
Loading