Debian Codegraph @

app/android.c

raw symbols SCIP
1 // Copyright 2014 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 //go:build android
6 // +build android
7
8 #include <android/log.h>
9 #include <dlfcn.h>
10 #include <errno.h>
11 #include <fcntl.h>
12 #include <stdint.h>
13 #include <string.h>
14 #include "_cgo_export.h"
15
16 #define LOG_INFO(...) __android_log_print(ANDROID_LOG_INFO, "Go", __VA_ARGS__)
17 #define LOG_FATAL(...) __android_log_print(ANDROID_LOG_FATAL, "Go", __VA_ARGS__)
18
19 static jclass current_class;
20
21 static jclass find_class(JNIEnv *env, const char *class_name) {
22 jclass clazz = (*env)->FindClass(env, class_name);
23 if (clazz == NULL) {
24 (*env)->ExceptionClear(env);
25 LOG_FATAL("cannot find %s", class_name);
26 return NULL;
27 }
28 return clazz;
29 }
30
31 static jmethodID find_method(JNIEnv *env, jclass clazz, const char *name, const char *sig) {
32 jmethodID m = (*env)->GetMethodID(env, clazz, name, sig);
33 if (m == 0) {
34 (*env)->ExceptionClear(env);
35 LOG_FATAL("cannot find method %s %s", name, sig);
36 return 0;
37 }
38 return m;
39 }
40
41 static jmethodID find_static_method(JNIEnv *env, jclass clazz, const char *name, const char *sig) {
42 jmethodID m = (*env)->GetStaticMethodID(env, clazz, name, sig);
43 if (m == 0) {
44 (*env)->ExceptionClear(env);
45 LOG_FATAL("cannot find method %s %s", name, sig);
46 return 0;
47 }
48 return m;
49 }
50
51 static jmethodID key_rune_method;
52
53 jint JNI_OnLoad(JavaVM* vm, void* reserved) {
54 JNIEnv* env;
55 if ((*vm)->GetEnv(vm, (void**)&env, JNI_VERSION_1_6) != JNI_OK) {
56 return -1;
57 }
58
59 return JNI_VERSION_1_6;
60 }
61
62 static int main_running = 0;
63
64 // Entry point from our subclassed NativeActivity.
65 //
66 // By here, the Go runtime has been initialized (as we are running in
67 // -buildmode=c-shared) but the first time it is called, Go's main.main
68 // hasn't been called yet.
69 //
70 // The Activity may be created and destroyed multiple times throughout
71 // the life of a single process. Each time, onCreate is called.
72 void ANativeActivity_onCreate(ANativeActivity *activity, void* savedState, size_t savedStateSize) {
73 if (!main_running) {
74 JNIEnv* env = activity->env;
75
76 // Note that activity->clazz is mis-named.
77 current_class = (*env)->GetObjectClass(env, activity->clazz);
78 current_class = (*env)->NewGlobalRef(env, current_class);
79 key_rune_method = find_static_method(env, current_class, "getRune", "(III)I");
80
81 setCurrentContext(activity->vm, (*env)->NewGlobalRef(env, activity->clazz));
82
83 // Set TMPDIR.
84 jmethodID gettmpdir = find_method(env, current_class, "getTmpdir", "()Ljava/lang/String;");
85 jstring jpath = (jstring)(*env)->CallObjectMethod(env, activity->clazz, gettmpdir, NULL);
86 const char* tmpdir = (*env)->GetStringUTFChars(env, jpath, NULL);
87 if (setenv("TMPDIR", tmpdir, 1) != 0) {
88 LOG_INFO("setenv(\"TMPDIR\", \"%s\", 1) failed: %d", tmpdir, errno);
89 }
90 (*env)->ReleaseStringUTFChars(env, jpath, tmpdir);
91
92 // Call the Go main.main.
93 uintptr_t mainPC = (uintptr_t)dlsym(RTLD_DEFAULT, "main.main");
94 if (!mainPC) {
95 LOG_FATAL("missing main.main");
96 }
97 callMain(mainPC);
98 main_running = 1;
99 }
100
101 // These functions match the methods on Activity, described at
102 // http://developer.android.com/reference/android/app/Activity.html
103 //
104 // Note that onNativeWindowResized is not called on resize. Avoid it.
105 // https://code.google.com/p/android/issues/detail?id=180645
106 activity->callbacks->onStart = onStart;
107 activity->callbacks->onResume = onResume;
108 activity->callbacks->onSaveInstanceState = onSaveInstanceState;
109 activity->callbacks->onPause = onPause;
110 activity->callbacks->onStop = onStop;
111 activity->callbacks->onDestroy = onDestroy;
112 activity->callbacks->onWindowFocusChanged = onWindowFocusChanged;
113 activity->callbacks->onNativeWindowCreated = onNativeWindowCreated;
114 activity->callbacks->onNativeWindowRedrawNeeded = onNativeWindowRedrawNeeded;
115 activity->callbacks->onNativeWindowDestroyed = onNativeWindowDestroyed;
116 activity->callbacks->onInputQueueCreated = onInputQueueCreated;
117 activity->callbacks->onInputQueueDestroyed = onInputQueueDestroyed;
118 activity->callbacks->onConfigurationChanged = onConfigurationChanged;
119 activity->callbacks->onLowMemory = onLowMemory;
120
121 onCreate(activity);
122 }
123
124 // TODO(crawshaw): Test configuration on more devices.
125 static const EGLint RGB_888[] = {
126 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
127 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
128 EGL_BLUE_SIZE, 8,
129 EGL_GREEN_SIZE, 8,
130 EGL_RED_SIZE, 8,
131 EGL_DEPTH_SIZE, 16,
132 EGL_CONFIG_CAVEAT, EGL_NONE,
133 EGL_NONE
134 };
135
136 EGLDisplay display = NULL;
137 EGLSurface surface = NULL;
138
139 static char* initEGLDisplay() {
140 display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
141 if (!eglInitialize(display, 0, 0)) {
142 return "EGL initialize failed";
143 }
144 return NULL;
145 }
146
147 char* createEGLSurface(ANativeWindow* window) {
148 char* err;
149 EGLint numConfigs, format;
150 EGLConfig config;
151 EGLContext context;
152
153 if (display == 0) {
154 if ((err = initEGLDisplay()) != NULL) {
155 return err;
156 }
157 }
158
159 if (!eglChooseConfig(display, RGB_888, &config, 1, &numConfigs)) {
160 return "EGL choose RGB_888 config failed";
161 }
162 if (numConfigs <= 0) {
163 return "EGL no config found";
164 }
165
166 eglGetConfigAttrib(display, config, EGL_NATIVE_VISUAL_ID, &format);
167 if (ANativeWindow_setBuffersGeometry(window, 0, 0, format) != 0) {
168 return "EGL set buffers geometry failed";
169 }
170
171 surface = eglCreateWindowSurface(display, config, window, NULL);
172 if (surface == EGL_NO_SURFACE) {
173 return "EGL create surface failed";
174 }
175
176 const EGLint contextAttribs[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE };
177 context = eglCreateContext(display, config, EGL_NO_CONTEXT, contextAttribs);
178
179 if (eglMakeCurrent(display, surface, surface, context) == EGL_FALSE) {
180 return "eglMakeCurrent failed";
181 }
182 return NULL;
183 }
184
185 char* destroyEGLSurface() {
186 if (!eglDestroySurface(display, surface)) {
187 return "EGL destroy surface failed";
188 }
189 return NULL;
190 }
191
192 int32_t getKeyRune(JNIEnv* env, AInputEvent* e) {
193 return (int32_t)(*env)->CallStaticIntMethod(
194 env,
195 current_class,
196 key_rune_method,
197 AInputEvent_getDeviceId(e),
198 AKeyEvent_getKeyCode(e),
199 AKeyEvent_getMetaState(e)
200 );
201 }