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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
|
/*
* Copyright 2011-2017 Branimir Karadzic. All rights reserved.
* License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
*/
#include "entry_p.h"
#if ENTRY_CONFIG_USE_NATIVE && BX_PLATFORM_ANDROID
#include <bgfx/platform.h>
#include <bx/thread.h>
#include <bx/file.h>
#include <android/input.h>
#include <android/log.h>
#include <android/looper.h>
#include <android/window.h>
#include <android_native_app_glue.h>
#include <android/native_window.h>
extern "C"
{
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
#include <android_native_app_glue.c>
#pragma GCC diagnostic pop
} // extern "C"
namespace entry
{
///
inline void androidSetWindow(::ANativeWindow* _window)
{
bgfx::PlatformData pd;
pd.ndt = NULL;
pd.nwh = _window;
pd.context = NULL;
pd.backBuffer = NULL;
pd.backBufferDS = NULL;
bgfx::setPlatformData(pd);
}
struct GamepadRemap
{
uint16_t m_keyCode;
Key::Enum m_key;
};
static GamepadRemap s_gamepadRemap[] =
{
{ AKEYCODE_DPAD_UP, Key::GamepadUp },
{ AKEYCODE_DPAD_DOWN, Key::GamepadDown },
{ AKEYCODE_DPAD_LEFT, Key::GamepadLeft },
{ AKEYCODE_DPAD_RIGHT, Key::GamepadRight },
{ AKEYCODE_BUTTON_START, Key::GamepadStart },
{ AKEYCODE_BACK, Key::GamepadBack },
{ AKEYCODE_BUTTON_THUMBL, Key::GamepadThumbL },
{ AKEYCODE_BUTTON_THUMBR, Key::GamepadThumbR },
{ AKEYCODE_BUTTON_L1, Key::GamepadShoulderL },
{ AKEYCODE_BUTTON_R1, Key::GamepadShoulderR },
{ AKEYCODE_GUIDE, Key::GamepadGuide },
{ AKEYCODE_BUTTON_A, Key::GamepadA },
{ AKEYCODE_BUTTON_B, Key::GamepadB },
{ AKEYCODE_BUTTON_X, Key::GamepadX },
{ AKEYCODE_BUTTON_Y, Key::GamepadY },
};
struct GamepadAxisRemap
{
int32_t m_event;
GamepadAxis::Enum m_axis;
bool m_convert;
};
static GamepadAxisRemap s_translateAxis[] =
{
{ AMOTION_EVENT_AXIS_X, GamepadAxis::LeftX, false },
{ AMOTION_EVENT_AXIS_Y, GamepadAxis::LeftY, false },
{ AMOTION_EVENT_AXIS_LTRIGGER, GamepadAxis::LeftZ, false },
{ AMOTION_EVENT_AXIS_Z, GamepadAxis::RightX, true },
{ AMOTION_EVENT_AXIS_RZ, GamepadAxis::RightY, false },
{ AMOTION_EVENT_AXIS_RTRIGGER, GamepadAxis::RightZ, false },
};
struct MainThreadEntry
{
int m_argc;
const char* const* m_argv;
static int32_t threadFunc(bx::Thread* _thread, void* _userData);
};
class FileReaderAndroid : public bx::FileReaderI
{
public:
FileReaderAndroid(AAssetManager* _assetManager, AAsset* _file)
: m_assetManager(_assetManager)
, m_file(_file)
, m_open(false)
{
}
virtual ~FileReaderAndroid()
{
close();
}
virtual bool open(const bx::FilePath& _filePath, bx::Error* _err) override
{
BX_CHECK(NULL != _err, "Reader/Writer interface calling functions must handle errors.");
if (NULL != m_file)
{
BX_ERROR_SET(_err, BX_ERROR_READERWRITER_ALREADY_OPEN, "FileReader: File is already open.");
return false;
}
m_file = AAssetManager_open(m_assetManager, _filePath.get(), AASSET_MODE_RANDOM);
if (NULL == m_file)
{
BX_ERROR_SET(_err, BX_ERROR_READERWRITER_OPEN, "FileReader: Failed to open file.");
return false;
}
m_open = true;
return true;
}
virtual void close() override
{
if (m_open
&& NULL != m_file)
{
AAsset_close(m_file);
m_file = NULL;
}
}
virtual int64_t seek(int64_t _offset, bx::Whence::Enum _whence) override
{
BX_CHECK(NULL != m_file, "Reader/Writer file is not open.");
return AAsset_seek64(m_file, _offset, _whence);
}
virtual int32_t read(void* _data, int32_t _size, bx::Error* _err) override
{
BX_CHECK(NULL != m_file, "Reader/Writer file is not open.");
BX_CHECK(NULL != _err, "Reader/Writer interface calling functions must handle errors.");
int32_t size = (int32_t)AAsset_read(m_file, _data, _size);
if (size != _size)
{
if (0 == AAsset_getRemainingLength(m_file) )
{
BX_ERROR_SET(_err, BX_ERROR_READERWRITER_EOF, "FileReader: EOF.");
}
return size >= 0 ? size : 0;
}
return size;
}
private:
AAssetManager* m_assetManager;
AAsset* m_file;
bool m_open;
};
struct Context
{
Context()
: m_window(NULL)
{
bx::memSet(m_value, 0, sizeof(m_value) );
// Deadzone values from xinput.h
m_deadzone[GamepadAxis::LeftX ] =
m_deadzone[GamepadAxis::LeftY ] = 7849;
m_deadzone[GamepadAxis::RightX] =
m_deadzone[GamepadAxis::RightY] = 8689;
m_deadzone[GamepadAxis::LeftZ ] =
m_deadzone[GamepadAxis::RightZ] = 30;
}
void run(android_app* _app)
{
m_app = _app;
m_app->userData = (void*)this;
m_app->onAppCmd = onAppCmdCB;
m_app->onInputEvent = onInputEventCB;
ANativeActivity_setWindowFlags(m_app->activity, 0
| AWINDOW_FLAG_FULLSCREEN
| AWINDOW_FLAG_KEEP_SCREEN_ON
, 0
);
const char* const argv[1] = { "android.so" };
m_mte.m_argc = 1;
m_mte.m_argv = argv;
while (0 == m_app->destroyRequested)
{
int32_t num;
android_poll_source* source;
/*int32_t id =*/ ALooper_pollAll(-1, NULL, &num, (void**)&source);
if (NULL != source)
{
source->process(m_app, source);
}
}
m_thread.shutdown();
}
void onAppCmd(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 (m_window != m_app->window)
{
m_window = m_app->window;
androidSetWindow(m_window);
int32_t width = ANativeWindow_getWidth(m_window);
int32_t height = ANativeWindow_getHeight(m_window);
DBG("ANativeWindow width %d, height %d", width, height);
WindowHandle defaultWindow = { 0 };
m_eventQueue.postSizeEvent(defaultWindow, width, height);
if (!m_thread.isRunning() )
{
m_thread.init(MainThreadEntry::threadFunc, &m_mte);
}
}
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.
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.
WindowHandle defaultWindow = { 0 };
m_eventQueue.postSuspendEvent(defaultWindow, Suspend::WillResume);
break;
}
case APP_CMD_LOST_FOCUS:
{
// Command from main thread: the app's activity window has lost
// input focus.
WindowHandle defaultWindow = { 0 };
m_eventQueue.postSuspendEvent(defaultWindow, Suspend::WillSuspend);
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.
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.
WindowHandle defaultWindow = { 0 };
m_eventQueue.postSuspendEvent(defaultWindow, Suspend::DidResume);
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.
break;
case APP_CMD_PAUSE:
{
// Command from main thread: the app's activity has been paused.
WindowHandle defaultWindow = { 0 };
m_eventQueue.postSuspendEvent(defaultWindow, Suspend::DidSuspend);
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.
m_eventQueue.postExitEvent();
break;
}
}
bool filter(GamepadAxis::Enum _axis, int32_t* _value)
{
const int32_t old = m_value[_axis];
const int32_t deadzone = m_deadzone[_axis];
int32_t value = *_value;
value = value > deadzone || value < -deadzone ? value : 0;
m_value[_axis] = value;
*_value = value;
return old != value;
}
int32_t onInputEvent(AInputEvent* _event)
{
WindowHandle defaultWindow = { 0 };
GamepadHandle handle = { 0 };
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:
{
if (0 != (source & (AINPUT_SOURCE_GAMEPAD|AINPUT_SOURCE_JOYSTICK) ) )
{
for (uint32_t ii = 0; ii < BX_COUNTOF(s_translateAxis); ++ii)
{
const float fval = AMotionEvent_getAxisValue(_event, s_translateAxis[ii].m_event, 0);
int32_t value = int32_t( (s_translateAxis[ii].m_convert ? fval * 2.0f + 1.0f : fval) * INT16_MAX);
GamepadAxis::Enum axis = s_translateAxis[ii].m_axis;
if (filter(axis, &value) )
{
m_eventQueue.postAxisEvent(defaultWindow, handle, axis, value);
}
}
return 1;
}
else
{
float mx = AMotionEvent_getX(_event, 0);
float my = AMotionEvent_getY(_event, 0);
int32_t count = AMotionEvent_getPointerCount(_event);
int32_t action = (actionBits & AMOTION_EVENT_ACTION_MASK);
int32_t index = (actionBits & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) >> AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
// Simulate left mouse click with 1st touch and right mouse click with 2nd touch. ignore other touchs
if (count < 2)
{
switch (action)
{
case AMOTION_EVENT_ACTION_DOWN:
case AMOTION_EVENT_ACTION_POINTER_DOWN:
m_eventQueue.postMouseEvent(defaultWindow
, (int32_t)mx
, (int32_t)my
, 0
, action == AMOTION_EVENT_ACTION_DOWN ? MouseButton::Left : MouseButton::Right
, true
);
break;
case AMOTION_EVENT_ACTION_UP:
case AMOTION_EVENT_ACTION_POINTER_UP:
m_eventQueue.postMouseEvent(defaultWindow
, (int32_t)mx
, (int32_t)my
, 0
, action == AMOTION_EVENT_ACTION_UP ? MouseButton::Left : MouseButton::Right
, false
);
break;
default:
break;
}
}
switch (action)
{
case AMOTION_EVENT_ACTION_MOVE:
if (0 == index)
{
m_eventQueue.postMouseEvent(defaultWindow
, (int32_t)mx
, (int32_t)my
, 0
);
}
break;
default:
break;
}
}
}
break;
case AINPUT_EVENT_TYPE_KEY:
{
int32_t keyCode = AKeyEvent_getKeyCode(_event);
if (0 != (source & (AINPUT_SOURCE_GAMEPAD|AINPUT_SOURCE_JOYSTICK) ) )
{
for (uint32_t jj = 0; jj < BX_COUNTOF(s_gamepadRemap); ++jj)
{
if (keyCode == s_gamepadRemap[jj].m_keyCode)
{
m_eventQueue.postKeyEvent(defaultWindow, s_gamepadRemap[jj].m_key, 0, actionBits == AKEY_EVENT_ACTION_DOWN);
break;
}
}
}
return 1;
}
break;
default:
DBG("type %d", type);
break;
}
return 0;
}
static void onAppCmdCB(struct android_app* _app, int32_t _cmd)
{
Context* self = (Context*)_app->userData;
self->onAppCmd(_cmd);
}
static int32_t onInputEventCB(struct android_app* _app, AInputEvent* _event)
{
Context* self = (Context*)_app->userData;
return self->onInputEvent(_event);
}
MainThreadEntry m_mte;
bx::Thread m_thread;
EventQueue m_eventQueue;
ANativeWindow* m_window;
android_app* m_app;
int32_t m_value[GamepadAxis::Count];
int32_t m_deadzone[GamepadAxis::Count];
};
static Context s_ctx;
const Event* poll()
{
return s_ctx.m_eventQueue.poll();
}
const Event* poll(WindowHandle _handle)
{
return s_ctx.m_eventQueue.poll(_handle);
}
void release(const Event* _event)
{
s_ctx.m_eventQueue.release(_event);
}
WindowHandle createWindow(int32_t _x, int32_t _y, uint32_t _width, uint32_t _height, uint32_t _flags, const char* _title)
{
BX_UNUSED(_x, _y, _width, _height, _flags, _title);
WindowHandle handle = { UINT16_MAX };
return handle;
}
void destroyWindow(WindowHandle _handle)
{
BX_UNUSED(_handle);
}
void setWindowPos(WindowHandle _handle, int32_t _x, int32_t _y)
{
BX_UNUSED(_handle, _x, _y);
}
void setWindowSize(WindowHandle _handle, uint32_t _width, uint32_t _height)
{
BX_UNUSED(_handle, _width, _height);
}
void setWindowTitle(WindowHandle _handle, const char* _title)
{
BX_UNUSED(_handle, _title);
}
void toggleWindowFrame(WindowHandle _handle)
{
BX_UNUSED(_handle);
}
void toggleFullscreen(WindowHandle _handle)
{
BX_UNUSED(_handle);
}
void setMouseLock(WindowHandle _handle, bool _lock)
{
BX_UNUSED(_handle, _lock);
}
int32_t MainThreadEntry::threadFunc(bx::Thread* _thread, void* _userData)
{
BX_UNUSED(_thread);
int32_t result = chdir("/sdcard/bgfx/examples/runtime");
BX_CHECK(0 == result, "Failed to chdir to dir. android.permission.WRITE_EXTERNAL_STORAGE?", errno);
MainThreadEntry* self = (MainThreadEntry*)_userData;
result = main(self->m_argc, self->m_argv);
// PostMessage(s_ctx.m_hwnd, WM_QUIT, 0, 0);
return result;
}
} // namespace entry
extern "C" void android_main(android_app* _app)
{
using namespace entry;
s_ctx.run(_app);
}
#endif // ENTRY_CONFIG_USE_NATIVE && BX_PLATFORM_ANDROID
|