github.com/gop9/olt@v0.0.0-20200202132135-d956aad50b08/gio/app/internal/window/os_android.c (about)

     1  // SPDX-License-Identifier: Unlicense OR MIT
     2  
     3  #include <jni.h>
     4  #include <dlfcn.h>
     5  #include <android/log.h>
     6  #include "os_android.h"
     7  #include "_cgo_export.h"
     8  
     9  JNIEXPORT jint JNI_OnLoad(JavaVM *vm, void *reserved) {
    10  	JNIEnv *env;
    11  	if ((*vm)->GetEnv(vm, (void**)&env, JNI_VERSION_1_6) != JNI_OK) {
    12  		return -1;
    13  	}
    14  
    15  	setJVM(vm);
    16  
    17  	jclass viewClass = (*env)->FindClass(env, "org/gioui/GioView");
    18  	if (viewClass == NULL) {
    19  		return -1;
    20  	}
    21  
    22  	static const JNINativeMethod methods[] = {
    23  		{
    24  			.name = "runGoMain",
    25  			.signature = "([BLandroid/content/Context;)V",
    26  			.fnPtr = runGoMain
    27  		},
    28  		{
    29  			.name = "onCreateView",
    30  			.signature = "(Lorg/gioui/GioView;)J",
    31  			.fnPtr = onCreateView
    32  		},
    33  		{
    34  			.name = "onDestroyView",
    35  			.signature = "(J)V",
    36  			.fnPtr = onDestroyView
    37  		},
    38  		{
    39  			.name = "onStartView",
    40  			.signature = "(J)V",
    41  			.fnPtr = onStartView
    42  		},
    43  		{
    44  			.name = "onStopView",
    45  			.signature = "(J)V",
    46  			.fnPtr = onStopView
    47  		},
    48  		{
    49  			.name = "onSurfaceDestroyed",
    50  			.signature = "(J)V",
    51  			.fnPtr = onSurfaceDestroyed
    52  		},
    53  		{
    54  			.name = "onSurfaceChanged",
    55  			.signature = "(JLandroid/view/Surface;)V",
    56  			.fnPtr = onSurfaceChanged
    57  		},
    58  		{
    59  			.name = "onConfigurationChanged",
    60  			.signature = "(J)V",
    61  			.fnPtr = onConfigurationChanged
    62  		},
    63  		{
    64  			.name = "onWindowInsets",
    65  			.signature = "(JIIII)V",
    66  			.fnPtr = onWindowInsets
    67  		},
    68  		{
    69  			.name = "onLowMemory",
    70  			.signature = "()V",
    71  			.fnPtr = onLowMemory
    72  		},
    73  		{
    74  			.name = "onTouchEvent",
    75  			.signature = "(JIIIFFIJ)V",
    76  			.fnPtr = onTouchEvent
    77  		},
    78  		{
    79  			.name = "onKeyEvent",
    80  			.signature = "(JIIJ)V",
    81  			.fnPtr = onKeyEvent
    82  		},
    83  		{
    84  			.name = "onFrameCallback",
    85  			.signature = "(JJ)V",
    86  			.fnPtr = onFrameCallback
    87  		},
    88  		{
    89  			.name = "onBack",
    90  			.signature = "(J)Z",
    91  			.fnPtr = onBack
    92  		},
    93  		{
    94  			.name = "onFocusChange",
    95  			.signature = "(JZ)V",
    96  			.fnPtr = onFocusChange
    97  		}
    98  	};
    99  	if ((*env)->RegisterNatives(env, viewClass, methods, sizeof(methods)/sizeof(methods[0])) != 0) {
   100  		return -1;
   101  	}
   102  
   103  	return JNI_VERSION_1_6;
   104  }
   105  
   106  jint gio_jni_GetEnv(JavaVM *vm, JNIEnv **env, jint version) {
   107  	return (*vm)->GetEnv(vm, (void **)env, version);
   108  }
   109  
   110  jint gio_jni_AttachCurrentThread(JavaVM *vm, JNIEnv **p_env, void *thr_args) {
   111  	return (*vm)->AttachCurrentThread(vm, p_env, thr_args);
   112  }
   113  
   114  jint gio_jni_DetachCurrentThread(JavaVM *vm) {
   115  	return (*vm)->DetachCurrentThread(vm);
   116  }
   117  
   118  jobject gio_jni_NewGlobalRef(JNIEnv *env, jobject obj) {
   119  	return (*env)->NewGlobalRef(env, obj);
   120  }
   121  
   122  void gio_jni_DeleteGlobalRef(JNIEnv *env, jobject obj) {
   123  	(*env)->DeleteGlobalRef(env, obj);
   124  }
   125  
   126  jclass gio_jni_GetObjectClass(JNIEnv *env, jobject obj) {
   127  	return (*env)->GetObjectClass(env, obj);
   128  }
   129  
   130  jmethodID gio_jni_GetMethodID(JNIEnv *env, jclass clazz, const char *name, const char *sig) {
   131  	return (*env)->GetMethodID(env, clazz, name, sig);
   132  }
   133  
   134  jmethodID gio_jni_GetStaticMethodID(JNIEnv *env, jclass clazz, const char *name, const char *sig) {
   135  	return (*env)->GetStaticMethodID(env, clazz, name, sig);
   136  }
   137  
   138  jint gio_jni_CallStaticIntMethodII(JNIEnv *env, jclass clazz, jmethodID methodID, jint a1, jint a2) {
   139  	return (*env)->CallStaticIntMethod(env, clazz, methodID, a1, a2);
   140  }
   141  
   142  jfloat gio_jni_CallFloatMethod(JNIEnv *env, jobject obj, jmethodID methodID) {
   143  	return (*env)->CallFloatMethod(env, obj, methodID);
   144  }
   145  
   146  jint gio_jni_CallIntMethod(JNIEnv *env, jobject obj, jmethodID methodID) {
   147  	return (*env)->CallIntMethod(env, obj, methodID);
   148  }
   149  
   150  void gio_jni_CallVoidMethod(JNIEnv *env, jobject obj, jmethodID methodID) {
   151  	(*env)->CallVoidMethod(env, obj, methodID);
   152  }
   153  
   154  void gio_jni_CallVoidMethod_J(JNIEnv *env, jobject obj, jmethodID methodID, jlong a1) {
   155  	(*env)->CallVoidMethod(env, obj, methodID, a1);
   156  }
   157  
   158  jbyte *gio_jni_GetByteArrayElements(JNIEnv *env, jbyteArray arr) {
   159  	return (*env)->GetByteArrayElements(env, arr, NULL);
   160  }
   161  
   162  void gio_jni_ReleaseByteArrayElements(JNIEnv *env, jbyteArray arr, jbyte *bytes) {
   163  	(*env)->ReleaseByteArrayElements(env, arr, bytes, JNI_ABORT);
   164  }
   165  
   166  jsize gio_jni_GetArrayLength(JNIEnv *env, jbyteArray arr) {
   167  	return (*env)->GetArrayLength(env, arr);
   168  }
   169  
   170  void gio_jni_RegisterFragment(JNIEnv *env, jobject view, jmethodID mid, char* del) {
   171  	jstring jdel = (*env)->NewStringUTF(env, del);
   172  	(*env)->CallObjectMethod(env, view, mid, jdel);
   173  }