-
Notifications
You must be signed in to change notification settings - Fork 1
/
entrypoint_android.c
425 lines (342 loc) · 11.5 KB
/
entrypoint_android.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
#ifdef __ANDROID__
// partially based on bgfx entry_android.cpp
#define ENTRYPOINT_CTX
#include "entrypoint.h"
#include "entrypoint_android.h"
#include <android/log.h>
#include <android/input.h>
#include <android/window.h>
#include <android/native_window.h>
#include <jni.h>
#include <android_native_app_glue.c> // just so we don't need to build it
static entrypoint_ctx_t ctx = {0};
entrypoint_ctx_t * ep_ctx() {return &ctx;}
// -----------------------------------------------------------------------------
ep_size_t ep_size()
{
ep_size_t r = {ctx.view_w, ctx.view_h};
return r;
}
bool ep_retina()
{
return false; // TODO
}
ep_ui_margins_t ep_ui_margins()
{
// TODO some phones actually have curved screen, how do we detect that?
ep_ui_margins_t r = {0, 0, 0, 0};
return r;
}
// -----------------------------------------------------------------------------
static void * entrypoint_thread(void * param);
static void on_app_cmd(struct android_app * app, int32_t cmd)
{
switch(cmd)
{
case APP_CMD_INPUT_CHANGED:
// Command from main thread: the AInputQueue has changed. Upon processing
// this command, android_app->inputQueue will be updated to the new queue
// (or NULL).
break;
case APP_CMD_INIT_WINDOW:
// Command from main thread: a new ANativeWindow is ready for use. Upon
// receiving this command, android_app->window will contain the new window
// surface.
if(ctx.window != app->window)
{
ctx.window = app->window;
pthread_mutex_lock(&ctx.mutex);
ctx.view_w = ANativeWindow_getWidth(ctx.window);
ctx.view_h = ANativeWindow_getHeight(ctx.window);
pthread_mutex_unlock(&ctx.mutex);
if(!ctx.thread_running)
{
ctx.thread_running = true;
pthread_create(&ctx.thread, NULL, entrypoint_thread, NULL);
}
}
break;
case APP_CMD_TERM_WINDOW:
// Command from main thread: the existing ANativeWindow needs to be
// terminated. Upon receiving this command, android_app->window still
// contains the existing window; after calling android_app_exec_cmd
// it will be set to NULL.
pthread_mutex_lock(&ctx.mutex);
ctx.window = NULL;
pthread_mutex_unlock(&ctx.mutex);
break;
case APP_CMD_WINDOW_RESIZED:
// Command from main thread: the current ANativeWindow has been resized.
// Please redraw with its new size.
break;
case APP_CMD_WINDOW_REDRAW_NEEDED:
// Command from main thread: the system needs that the current ANativeWindow
// be redrawn. You should redraw the window before handing this to
// android_app_exec_cmd() in order to avoid transient drawing glitches.
break;
case APP_CMD_CONTENT_RECT_CHANGED:
// Command from main thread: the content area of the window has changed,
// such as from the soft input window being shown or hidden. You can
// find the new content rect in android_app::contentRect.
break;
case APP_CMD_GAINED_FOCUS:
// Command from main thread: the app's activity window has gained
// input focus.
break;
case APP_CMD_LOST_FOCUS:
// Command from main thread: the app's activity window has lost
// input focus.
pthread_mutex_lock(&ctx.mutex);
entrypoint_might_unload();
pthread_mutex_unlock(&ctx.mutex);
break;
case APP_CMD_CONFIG_CHANGED:
// Command from main thread: the current device configuration has changed.
break;
case APP_CMD_LOW_MEMORY:
// Command from main thread: the system is running low on memory.
// Try to reduce your memory use.
pthread_mutex_lock(&ctx.mutex);
entrypoint_might_unload();
pthread_mutex_unlock(&ctx.mutex);
break;
case APP_CMD_START:
// Command from main thread: the app's activity has been started.
break;
case APP_CMD_RESUME:
// Command from main thread: the app's activity has been resumed.
break;
case APP_CMD_SAVE_STATE:
// Command from main thread: the app should generate a new saved state
// for itself, to restore from later if needed. If you have saved state,
// allocate it with malloc and place it in android_app.savedState with
// the size in android_app.savedStateSize. The will be freed for you
// later.
pthread_mutex_lock(&ctx.mutex);
entrypoint_might_unload();
pthread_mutex_unlock(&ctx.mutex);
break;
case APP_CMD_PAUSE:
// Command from main thread: the app's activity has been paused.
pthread_mutex_lock(&ctx.mutex);
entrypoint_might_unload();
pthread_mutex_unlock(&ctx.mutex);
break;
case APP_CMD_STOP:
// Command from main thread: the app's activity has been stopped.
break;
case APP_CMD_DESTROY:
// Command from main thread: the app's activity is being destroyed,
// and waiting for the app thread to clean up and exit before proceeding.
break;
}
}
#ifdef ENTRYPOINT_PROVIDE_INPUT
int32_t on_input_event(struct android_app *_app, AInputEvent *_event)
{
const int32_t type = AInputEvent_getType(_event);
const int32_t source = AInputEvent_getSource(_event);
const int32_t actionBits = AMotionEvent_getAction(_event);
switch (type)
{
case AINPUT_EVENT_TYPE_MOTION:
{
//pthread_mutex_lock(&ctx.mutex);
int32_t touch_count = AMotionEvent_getPointerCount(_event);
if(touch_count > ENTRYPOINT_MAX_MULTITOUCH)
touch_count = ENTRYPOINT_MAX_MULTITOUCH;
for(int32_t touch = 0; touch < touch_count; ++touch)
{
ctx.touch.multitouch[touch].x = AMotionEvent_getX(_event, touch);
ctx.touch.multitouch[touch].y = AMotionEvent_getY(_event, touch);
}
int32_t action = (actionBits & AMOTION_EVENT_ACTION_MASK);
int32_t index = (actionBits & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) >> AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
if(index < ENTRYPOINT_MAX_MULTITOUCH)
{
if(action == AMOTION_EVENT_ACTION_DOWN || action == AMOTION_EVENT_ACTION_POINTER_DOWN)
ctx.touch.multitouch[index].touched = 1;
else if(action == AMOTION_EVENT_ACTION_UP || action == AMOTION_EVENT_ACTION_POINTER_UP)
ctx.touch.multitouch[index].touched = 0;
}
ctx.touch.x = ctx.touch.multitouch[0].x;
ctx.touch.y = ctx.touch.multitouch[0].y;
ctx.touch.left = ctx.touch.multitouch[0].touched;
//pthread_mutex_unlock(&ctx.mutex);
}
break;
default:
break;
}
return 0;
}
#endif
void * entrypoint_thread(void * param)
{
JNIEnv * jni_env_entrypoint_thread = NULL;
(*ctx.app->activity->vm)->AttachCurrentThread(ctx.app->activity->vm, &jni_env_entrypoint_thread, NULL);
ctx.jni_env_entrypoint_thread = jni_env_entrypoint_thread;
if(entrypoint_init(ctx.argc, ctx.argv))
{
(*ctx.app->activity->vm)->DetachCurrentThread(ctx.app->activity->vm);
return NULL;
}
while(!ctx.flag_want_to_close)
{
pthread_mutex_lock(&ctx.mutex);
if(entrypoint_loop())
{
pthread_mutex_unlock(&ctx.mutex);
break;
}
pthread_mutex_unlock(&ctx.mutex);
}
if(entrypoint_might_unload()) // TODO is this needed ?
{
(*ctx.app->activity->vm)->DetachCurrentThread(ctx.app->activity->vm);
return NULL;
}
if(entrypoint_deinit())
{
(*ctx.app->activity->vm)->DetachCurrentThread(ctx.app->activity->vm);
return NULL;
}
(*ctx.app->activity->vm)->DetachCurrentThread(ctx.app->activity->vm);
return NULL;
}
void android_main(struct android_app * app)
{
const char * argv[1] = { "android.so" };
ctx.argc = 1;
ctx.argv = (char**)argv;
ctx.app = app;
app->userData = &ctx;
ctx.app->onAppCmd = on_app_cmd;
#ifdef ENTRYPOINT_PROVIDE_INPUT
ctx.app->onInputEvent = on_input_event;
#endif
ANativeActivity_setWindowFlags(ctx.app->activity, AWINDOW_FLAG_FULLSCREEN | AWINDOW_FLAG_KEEP_SCREEN_ON, 0);
JNIEnv * jni_env_main_thread = NULL;
(*ctx.app->activity->vm)->AttachCurrentThread(ctx.app->activity->vm, &jni_env_main_thread, NULL);
ctx.jni_env_main_thread = jni_env_main_thread;
{
JNIEnv * env = ctx.jni_env_main_thread;
jobject na_class = ctx.app->activity->clazz;
jclass acl = (*env)->GetObjectClass(env, na_class);
jmethodID get_class_loader = (*env)->GetMethodID(env, acl, "getClassLoader", "()Ljava/lang/ClassLoader;");
jobject cl_obj = (*env)->CallObjectMethod(env, na_class, get_class_loader);
ctx.j_class_loader = (void*)((*env)->NewWeakGlobalRef(env, cl_obj));
jclass class_loader = (*env)->FindClass(env, "java/lang/ClassLoader");
ctx.j_find_class_method_id = (void*)((*env)->GetMethodID(env, class_loader, "loadClass", "(Ljava/lang/String;)Ljava/lang/Class;"));
}
ENTRYPOINT_ANDROID_PREPARE_PARAMS;
pthread_mutex_init(&ctx.mutex, NULL);
int poll_events = 0;
struct android_poll_source * poll_source = NULL;
while(!ctx.app->destroyRequested)
{
// TODO do we want one or all events?
ALooper_pollAll(-1, NULL, &poll_events, (void**)&poll_source);
if(poll_source)
poll_source->process(ctx.app, poll_source);
}
ctx.flag_want_to_close = 1;
pthread_join(ctx.thread, NULL);
pthread_mutex_destroy(&ctx.mutex);
// TODO do we need this?
(*ctx.app->activity->vm)->DetachCurrentThread(ctx.app->activity->vm);
return;
}
// -----------------------------------------------------------------------------
#ifdef ENTRYPOINT_PROVIDE_TIME
double ep_delta_time()
{
if(!ctx.flag_time_set)
{
gettimeofday(&ctx.prev_time, NULL);
ctx.flag_time_set = true;
return 0.0;
}
struct timeval now;
gettimeofday(&now, NULL);
double elapsed = (now.tv_sec - ctx.prev_time.tv_sec) + ((now.tv_usec - ctx.prev_time.tv_usec) / 1000000.0);
ctx.prev_time = now;
return elapsed;
}
void ep_sleep(double seconds)
{
usleep((useconds_t)(seconds * 1000000.0));
}
#endif
// -----------------------------------------------------------------------------
#ifdef ENTRYPOINT_PROVIDE_LOG
void ep_log(const char * message, ...)
{
va_list args;
va_start(args, message);
__android_log_vprint(ANDROID_LOG_INFO, ENTRYPOINT_ANDROID_LOG_TAG, message, args);
va_end(args);
}
#endif
// -----------------------------------------------------------------------------
#ifdef ENTRYPOINT_PROVIDE_INPUT
void ep_touch(ep_touch_t * touch)
{
if(touch)
*touch = ctx.touch;
}
bool ep_khit(int32_t key) {return false;}
bool ep_kdown(int32_t key) {return false;}
uint32_t ep_kchar() {return 0;}
#endif
// -----------------------------------------------------------------------------
#ifdef ENTRYPOINT_PROVIDE_OPENURL
void ep_openurl(const char * url)
{
ep_jni_method_t r = ep_get_static_java_method("com/beardsvibe/EntrypointNativeActivity", "openURL", "(Ljava/lang/String;)V");
if(r.found)
{
JNIEnv * e = (JNIEnv*)r.env;
jstring j_str = (*e)->NewStringUTF(e, url);
(*e)->CallStaticVoidMethod(r.env, r.class_id, r.method_id, j_str);
(*e)->DeleteLocalRef(e, j_str);
}
ep_get_static_java_method_clear(r);
}
#endif
// -----------------------------------------------------------------------------
// jni helper is loosely based on cocos2d-x
ep_jni_method_t ep_get_static_java_method(const char * class_name, const char * method_name, const char * param_code)
{
ep_jni_method_t r = {0};
JNIEnv * env = ctx.jni_env_entrypoint_thread;
if(!class_name || !method_name || !param_code || !env)
return r;
jobject j_class_loader = (jobject)ctx.j_class_loader;
jmethodID j_find_class_method_id = (jmethodID)ctx.j_find_class_method_id;
jstring class_jstr = (*env)->NewStringUTF(env, class_name);
jclass class_id = (jclass)((*env)->CallObjectMethod(env, j_class_loader, j_find_class_method_id, class_jstr));
if(!class_id) {
(*env)->ExceptionClear(env);
(*env)->DeleteLocalRef(env, class_jstr);
return r;
}
(*env)->DeleteLocalRef(env, class_jstr);
jmethodID method_id = (*env)->GetStaticMethodID(env, class_id, method_name, param_code);
if(!method_id) {
(*env)->ExceptionClear(env);
return r;
}
r.found = true;
r.env = (void*)env;
r.class_id = (void*)class_id;
r.method_id = (void*)method_id;
return r;
}
void ep_get_static_java_method_clear(ep_jni_method_t method)
{
if(!method.found)
return;
(*((JNIEnv*)method.env))->DeleteLocalRef(method.env, method.class_id);
}
#endif