-
-
Notifications
You must be signed in to change notification settings - Fork 178
/
NativeGlue.c
514 lines (408 loc) · 14.4 KB
/
NativeGlue.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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
/*
* Copyright (c) scott.cgi All Rights Reserved.
*
* This source code belongs to project Mojoc, which is a pure C Game Engine hosted on GitHub.
* The Mojoc Game Engine is licensed under the MIT License, and will continue to be iterated with coding passion.
*
* License : https://github.com/scottcgi/Mojoc/blob/master/LICENSE
* GitHub : https://github.com/scottcgi/Mojoc
* CodeStyle: https://github.com/scottcgi/Mojoc/blob/master/Docs/CodeStyle.md
*
* Since : 2016-1-28
* Update : 2019-1-7
* Author : scott.cgi
*/
#include "Engine/Toolkit/Platform/Platform.h"
//------------------------
#ifdef IS_PLATFORM_ANDROID
//------------------------
#include <android/asset_manager.h>
#include <android/configuration.h>
#include <android/looper.h>
#include <android/native_activity.h>
#include <stdbool.h>
#include <stdio.h>
#include "Engine/Application/Application.h"
#include "Engine/Toolkit/Utils/FileTool.h"
#include "Engine/Graphics/OpenGL/Platform/EGLTool.h"
#include "Engine/Toolkit/Platform/Log.h"
#include "Engine/Application/Input.h"
#include "Engine/Toolkit/Utils/Thread.h"
#include "JniTool.h"
/**
* IDentify input event.
*/
enum
{
LooperID_Input,
LooperID_Sensor,
};
/**
* IDentify main thread callbacks from java client.
*/
typedef volatile enum
{
MainThread_OnLoop,
MainThread_OnWait,
MainThread_OnResized,
MainThread_OnPause,
MainThread_OnResume,
MainThread_OnNeedCreateEGL,
MainThread_OnDestroy,
}
MainThreadCallback;
/* public to external methods calls. */
ANativeActivity* nativeActivity = NULL;
AConfiguration* nativeActivityConfig = NULL;
/* EGL setting objects. */
static EGLDisplay eglDisplay;
static EGLSurface eglSurface;
static EGLContext eglContext;
static EGLConfig eglConfig;
static EGLint eglFormat;
/* NDK support objects. */
static ANativeWindow* nativeWindow;
static ALooper* looper;
static AInputQueue* inputQueue;
/**
* The volatile make sure not optimized by the compiler,
* because two threads one set and one get,
* but look at one thread, it seems never modified.
*/
static volatile MainThreadCallback mainThreadCallback = MainThread_OnWait;
static bool isNeedCreateEGL = true;
static bool isPaused = true;
/**
* One touch event handle.
*/
static void inline Touch(AInputEvent* event, int pointerIndex, InputTouchType inputTouchType)
{
AApplication->Touch
(
AMotionEvent_getPointerId(event, (size_t) pointerIndex),
AMotionEvent_getX (event, (size_t) pointerIndex),
AMotionEvent_getY (event, (size_t) pointerIndex),
inputTouchType
);
}
/**
* Multiple touches event handle.
*/
static void inline Touches(AInputEvent* event, InputTouchType inputTouchType)
{
int count = (int) AMotionEvent_getPointerCount(event);
int fingerIDs[count];
float pixelXs [count];
float pixelYs [count];
for (int i = 0; i < count; ++i)
{
fingerIDs[i] = AMotionEvent_getPointerId(event, (size_t) i);
pixelXs [i] = AMotionEvent_getX (event, (size_t) i);
pixelYs [i] = AMotionEvent_getY (event, (size_t) i);
}
AApplication->Touches(fingerIDs, pixelXs, pixelYs, count, inputTouchType);
}
/**
* Process the input event.
* return 0 event not handled.
* return 1 event handled.
*/
static inline int32_t OnInputEvent(AInputEvent* event)
{
switch (AInputEvent_getType(event))
{
case AINPUT_EVENT_TYPE_MOTION:
{
int32_t action = AMotionEvent_getAction(event);
switch (action & AMOTION_EVENT_ACTION_MASK)
{
// first pointer down
case AMOTION_EVENT_ACTION_DOWN:
{
Touch(event, 0, InputTouchType_Down);
break;
}
// not first pointer down
case AMOTION_EVENT_ACTION_POINTER_DOWN:
Touch
(
event,
(action & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) >>
AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT,
InputTouchType_Down
);
break;
// first pinter up
case AMOTION_EVENT_ACTION_UP:
Touch(event, 0, InputTouchType_Up);
break;
// not first pointer up
case AMOTION_EVENT_ACTION_POINTER_UP:
Touch
(
event,
(action & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) >>
AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT,
InputTouchType_Up
);
break;
case AMOTION_EVENT_ACTION_MOVE:
Touches(event, InputTouchType_Move);
break;
case AMOTION_EVENT_ACTION_CANCEL:
Touches(event, InputTouchType_Cancel);
break;
default:
return 0;
}
return 1;
}
case AINPUT_EVENT_TYPE_KEY:
break;
default:
break;
}
// default continue dispatching event
return 0;
}
/**
* Implementations should return 1 to continue receiving callbacks,
* or 0 to have this file descriptor (fd) and callback unregistered from the looper.
*
* events: typically ALOOPER_EVENT_INPUT
*/
static int LooperOnInputEvent(int fd, int events, void* data)
{
AInputEvent* event;
while (AInputQueue_getEvent(inputQueue, &event) >= 0)
{
// returns 0 if it was not pre-dispatched,
// meaning you can process it right now.
// not 0 maybe IME consumed this event.
if (AInputQueue_preDispatchEvent(inputQueue, event) == 0)
{
AInputQueue_finishEvent(inputQueue, event, OnInputEvent(event));
}
}
// continue receiving callbacks
return 1;
}
static void* LoopThreadRun(void* param)
{
// attach thread env
AJniTool->Init();
looper = ALooper_prepare(0);
while (true)
{
switch (mainThreadCallback)
{
case MainThread_OnLoop:
// handle event
ALooper_pollAll(0, NULL, NULL, NULL);
// application main loop
AApplication->Loop();
// render buffer
eglSwapBuffers(eglDisplay, eglSurface);
continue;
case MainThread_OnDestroy:
AApplication->Destroy();
// wait for OnStart when app destroy in background
mainThreadCallback = MainThread_OnWait;
return NULL;
case MainThread_OnPause: // sometimes before resized
AApplication->Pause();
mainThreadCallback = MainThread_OnWait;
continue;
case MainThread_OnResume:
AApplication->Resume();
mainThreadCallback = MainThread_OnWait;
break;
case MainThread_OnNeedCreateEGL:
// we need create EGL and use openGL in one thread
AEGLTool->CreateEGL(nativeWindow, &eglDisplay, &eglContext, &eglSurface, &eglConfig);
// EGL_NATIVE_VISUAL_ID is an attribute of the EGLConfig that is
// guaranteed to be accepted by ANativeWindow_SetBuffersGeometry()
// As soon as we picked a EGLConfig, we can safely reconfigure the
// ANativeWindow buffers to match, using EGL_NATIVE_VISUAL_ID
eglGetConfigAttrib(eglDisplay, eglConfig, EGL_NATIVE_VISUAL_ID, &eglFormat);
ANativeWindow_setBuffersGeometry(nativeWindow, 0, 0, eglFormat);
// app ready to rendering
AApplication->Ready
(
ANativeWindow_getWidth (nativeWindow),
ANativeWindow_getHeight(nativeWindow)
);
mainThreadCallback = MainThread_OnLoop;
break;
case MainThread_OnResized:
// not need to destroy EGL but only reset window
AEGLTool->ResetSurface(nativeWindow, eglDisplay, eglContext, eglConfig, &eglSurface);
ANativeWindow_setBuffersGeometry(nativeWindow, 0, 0, eglFormat);
AApplication->Resized
(
ANativeWindow_getWidth(nativeWindow),
ANativeWindow_getHeight(nativeWindow)
);
mainThreadCallback = MainThread_OnLoop;
break;
case MainThread_OnWait:
break;
default:
break;
}
}
}
/* The following methods callback from ANativeActivity. */
static void OnStart(ANativeActivity* activity)
{
ALog_D("ANativeActivity OnStart");
}
static void OnResume(ANativeActivity* activity)
{
ALog_D("ANativeActivity OnResume");
mainThreadCallback = MainThread_OnResume;
isPaused = false;
}
static void* OnSaveInstanceState(ANativeActivity* activity, size_t* outSaveSize)
{
ALog_D("ANativeActivity OnSaveInstanceState");
AApplication->SaveData(NULL);
*outSaveSize = 0;
return NULL;
}
static void OnPause(ANativeActivity* activity)
{
ALog_D("ANativeActivity OnPause");
mainThreadCallback = MainThread_OnPause;
isPaused = true;
}
static void OnStop(ANativeActivity* activity)
{
ALog_D("ANativeActivity OnStop");
}
static void OnDestroy(ANativeActivity* activity)
{
ALog_D("ANativeActivity OnDestroy");
mainThreadCallback = MainThread_OnDestroy;
AApplication->SaveData(NULL);
}
static void OnWindowFocusChanged(ANativeActivity* activity, int hasFocus)
{
ALog_D("ANativeActivity OnWindowFocusChanged");
}
static void OnNativeWindowCreated(ANativeActivity* activity, ANativeWindow* window)
{
ALog_D("ANativeActivity OnNativeWindowCreated");
nativeWindow = window;
}
static void OnNativeWindowResized(ANativeActivity* activity, ANativeWindow* window)
{
ALog_D("ANativeActivity OnNativeWindowResized");
// when start app and from back to front screen
if (isNeedCreateEGL == false)
{
mainThreadCallback = MainThread_OnResized;
ALog_D("When OnNativeWindowResized do MainThread_OnResized");
}
else
{
isNeedCreateEGL = false;
mainThreadCallback = MainThread_OnNeedCreateEGL;
ALog_D("When OnNativeWindowResized do MainThread_OnNeedCreateEGL");
}
}
static void OnNativeWindowRedrawNeeded(ANativeActivity* activity, ANativeWindow* window)
{
ALog_D("ANativeActivity OnNativeWindowRedrawNeeded");
if (isPaused == false)
{
if (mainThreadCallback == MainThread_OnWait)
{
mainThreadCallback = MainThread_OnResized;
ALog_D("When OnNativeWindowRedrawNeeded do MainThread_OnResized");
}
}
}
static void OnNativeWindowDestroyed(ANativeActivity* activity, ANativeWindow* window)
{
ALog_D("ANativeActivity OnNativeWindowDestroyed");
mainThreadCallback = MainThread_OnWait;
}
static void OnInputQueueCreated(ANativeActivity* activity, AInputQueue* queue)
{
ALog_D("ANativeActivity OnInputQueueCreated");
inputQueue = queue;
AInputQueue_attachLooper(inputQueue, looper, LooperID_Input, LooperOnInputEvent, NULL);
}
static void OnInputQueueDestroyed(ANativeActivity* activity, AInputQueue* queue)
{
ALog_D("ANativeActivity OnInputQueueDestroyed");
AInputQueue_detachLooper(queue);
}
static void OnContentRectChanged(ANativeActivity* activity, const ARect* rect)
{
ALog_D("ANativeActivity OnContentRectChanged");
// when start app and lock/unlock screen
if (isPaused == false)
{
if (isNeedCreateEGL == false)
{
if (mainThreadCallback != MainThread_OnNeedCreateEGL)
{
mainThreadCallback = MainThread_OnResized;
ALog_D("When OnContentRectChanged do MainThread_OnResized");
}
}
}
}
static void OnConfigurationChanged(ANativeActivity* activity)
{
ALog_D("ANativeActivity OnConfigurationChanged");
AConfiguration_fromAssetManager(nativeActivityConfig, activity->assetManager);
}
static void OnLowMemory(ANativeActivity* activity)
{
ALog_D("ANativeActivity OnLowMemory");
}
/**
* Called from java native activity when the java activity creating.
* the func name set by AndroidManifest.xml with meta-data android:name = "android.app.func_name".
*/
void ANativeActivity_OnCreate(ANativeActivity* activity, void* savedState, size_t savedStateSize)
{
ALog_D("ANativeActivity_OnCreate Start");
activity->callbacks->onStart = OnStart;
activity->callbacks->onResume = OnResume;
activity->callbacks->onSaveInstanceState = OnSaveInstanceState;
activity->callbacks->onPause = OnPause;
activity->callbacks->onStop = OnStop;
activity->callbacks->onDestroy = OnDestroy;
activity->callbacks->onWindowFocusChanged = OnWindowFocusChanged;
activity->callbacks->onNativeWindowCreated = OnNativeWindowCreated;
activity->callbacks->onNativeWindowResized = OnNativeWindowResized;
activity->callbacks->onNativeWindowRedrawNeeded = OnNativeWindowRedrawNeeded;
activity->callbacks->onNativeWindowDestroyed = OnNativeWindowDestroyed;
activity->callbacks->onInputQueueCreated = OnInputQueueCreated;
activity->callbacks->onInputQueueDestroyed = OnInputQueueDestroyed;
activity->callbacks->onContentRectChanged = OnContentRectChanged;
activity->callbacks->onConfigurationChanged = OnConfigurationChanged;
activity->callbacks->onLowMemory = OnLowMemory;
if (nativeActivity == NULL)
{
AApplication->Init();
}
else
{
// app destroy in background
AConfiguration_delete(nativeActivityConfig);
}
nativeActivity = activity;
// create and set AConfiguration by assetManager
nativeActivityConfig = AConfiguration_new();
AConfiguration_fromAssetManager(nativeActivityConfig, activity->assetManager);
// start loop thread for app's render and logic
AThread->StartThread(LoopThreadRun, NULL);
}
//---------------------------
#endif // IS_PLATFORM_ANDROID
//---------------------------