entry_android.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. /*
  2. * Copyright 2011-2015 Branimir Karadzic. All rights reserved.
  3. * License: http://www.opensource.org/licenses/BSD-2-Clause
  4. */
  5. #include "entry_p.h"
  6. #if ENTRY_CONFIG_USE_NATIVE && BX_PLATFORM_ANDROID
  7. #include <bgfxplatform.h>
  8. #include <stdio.h>
  9. #include <bx/thread.h>
  10. #include <android/input.h>
  11. #include <android/log.h>
  12. #include <android/looper.h>
  13. #include <android/window.h>
  14. #include <android_native_app_glue.h>
  15. extern "C"
  16. {
  17. #pragma GCC diagnostic push
  18. #pragma GCC diagnostic ignored "-Wunused-parameter"
  19. #include <android_native_app_glue.c>
  20. #pragma GCC diagnostic pop
  21. } // extern "C"
  22. namespace entry
  23. {
  24. struct GamepadRemap
  25. {
  26. uint16_t m_keyCode;
  27. Key::Enum m_key;
  28. };
  29. static GamepadRemap s_gamepadRemap[] =
  30. {
  31. { AKEYCODE_DPAD_UP, Key::GamepadUp },
  32. { AKEYCODE_DPAD_DOWN, Key::GamepadDown },
  33. { AKEYCODE_DPAD_LEFT, Key::GamepadLeft },
  34. { AKEYCODE_DPAD_RIGHT, Key::GamepadRight },
  35. { AKEYCODE_BUTTON_START, Key::GamepadStart },
  36. { AKEYCODE_BACK, Key::GamepadBack },
  37. { AKEYCODE_BUTTON_THUMBL, Key::GamepadThumbL },
  38. { AKEYCODE_BUTTON_THUMBR, Key::GamepadThumbR },
  39. { AKEYCODE_BUTTON_L1, Key::GamepadShoulderL },
  40. { AKEYCODE_BUTTON_R1, Key::GamepadShoulderR },
  41. { AKEYCODE_GUIDE, Key::GamepadGuide },
  42. { AKEYCODE_BUTTON_A, Key::GamepadA },
  43. { AKEYCODE_BUTTON_B, Key::GamepadB },
  44. { AKEYCODE_BUTTON_X, Key::GamepadX },
  45. { AKEYCODE_BUTTON_Y, Key::GamepadY },
  46. };
  47. struct GamepadAxisRemap
  48. {
  49. int32_t m_event;
  50. GamepadAxis::Enum m_axis;
  51. bool m_convert;
  52. };
  53. static GamepadAxisRemap s_translateAxis[] =
  54. {
  55. { AMOTION_EVENT_AXIS_X, GamepadAxis::LeftX, false },
  56. { AMOTION_EVENT_AXIS_Y, GamepadAxis::LeftY, false },
  57. { AMOTION_EVENT_AXIS_LTRIGGER, GamepadAxis::LeftZ, false },
  58. { AMOTION_EVENT_AXIS_Z, GamepadAxis::RightX, true },
  59. { AMOTION_EVENT_AXIS_RZ, GamepadAxis::RightY, false },
  60. { AMOTION_EVENT_AXIS_RTRIGGER, GamepadAxis::RightZ, false },
  61. };
  62. struct MainThreadEntry
  63. {
  64. int m_argc;
  65. char** m_argv;
  66. static int32_t threadFunc(void* _userData);
  67. };
  68. struct Context
  69. {
  70. Context()
  71. : m_window(NULL)
  72. , m_count(0)
  73. {
  74. memset(m_value, 0, sizeof(m_value) );
  75. // Deadzone values from xinput.h
  76. m_deadzone[GamepadAxis::LeftX ] =
  77. m_deadzone[GamepadAxis::LeftY ] = 7849;
  78. m_deadzone[GamepadAxis::RightX] =
  79. m_deadzone[GamepadAxis::RightY] = 8689;
  80. m_deadzone[GamepadAxis::LeftZ ] =
  81. m_deadzone[GamepadAxis::RightZ] = 30;
  82. }
  83. void run(android_app* _app)
  84. {
  85. m_app = _app;
  86. m_app->userData = (void*)this;
  87. m_app->onAppCmd = onAppCmdCB;
  88. m_app->onInputEvent = onInputEventCB;
  89. ANativeActivity_setWindowFlags(m_app->activity, 0
  90. | AWINDOW_FLAG_FULLSCREEN
  91. | AWINDOW_FLAG_KEEP_SCREEN_ON
  92. , 0
  93. );
  94. const char* argv[1] = { "android.so" };
  95. m_mte.m_argc = 1;
  96. m_mte.m_argv = const_cast<char**>(argv);
  97. while (0 == m_app->destroyRequested)
  98. {
  99. int32_t num;
  100. android_poll_source* source;
  101. /*int32_t id =*/ ALooper_pollAll(-1, NULL, &num, (void**)&source);
  102. if (NULL != source)
  103. {
  104. source->process(m_app, source);
  105. }
  106. }
  107. m_thread.shutdown();
  108. }
  109. void onAppCmd(int32_t _cmd)
  110. {
  111. switch (_cmd)
  112. {
  113. case APP_CMD_INPUT_CHANGED:
  114. // Command from main thread: the AInputQueue has changed. Upon processing
  115. // this command, android_app->inputQueue will be updated to the new queue
  116. // (or NULL).
  117. break;
  118. case APP_CMD_INIT_WINDOW:
  119. // Command from main thread: a new ANativeWindow is ready for use. Upon
  120. // receiving this command, android_app->window will contain the new window
  121. // surface.
  122. if (m_window == NULL)
  123. {
  124. m_window = m_app->window;
  125. bgfx::androidSetWindow(m_window);
  126. int32_t width = ANativeWindow_getWidth(m_window);
  127. int32_t height = ANativeWindow_getHeight(m_window);
  128. DBG("ANativeWindow width %d, height %d", width, height);
  129. WindowHandle defaultWindow = { 0 };
  130. m_eventQueue.postSizeEvent(defaultWindow, width, height);
  131. m_thread.init(MainThreadEntry::threadFunc, &m_mte);
  132. }
  133. break;
  134. case APP_CMD_TERM_WINDOW:
  135. // Command from main thread: the existing ANativeWindow needs to be
  136. // terminated. Upon receiving this command, android_app->window still
  137. // contains the existing window; after calling android_app_exec_cmd
  138. // it will be set to NULL.
  139. break;
  140. case APP_CMD_WINDOW_RESIZED:
  141. // Command from main thread: the current ANativeWindow has been resized.
  142. // Please redraw with its new size.
  143. break;
  144. case APP_CMD_WINDOW_REDRAW_NEEDED:
  145. // Command from main thread: the system needs that the current ANativeWindow
  146. // be redrawn. You should redraw the window before handing this to
  147. // android_app_exec_cmd() in order to avoid transient drawing glitches.
  148. break;
  149. case APP_CMD_CONTENT_RECT_CHANGED:
  150. // Command from main thread: the content area of the window has changed,
  151. // such as from the soft input window being shown or hidden. You can
  152. // find the new content rect in android_app::contentRect.
  153. break;
  154. case APP_CMD_GAINED_FOCUS:
  155. // Command from main thread: the app's activity window has gained
  156. // input focus.
  157. break;
  158. case APP_CMD_LOST_FOCUS:
  159. // Command from main thread: the app's activity window has lost
  160. // input focus.
  161. break;
  162. case APP_CMD_CONFIG_CHANGED:
  163. // Command from main thread: the current device configuration has changed.
  164. break;
  165. case APP_CMD_LOW_MEMORY:
  166. // Command from main thread: the system is running low on memory.
  167. // Try to reduce your memory use.
  168. break;
  169. case APP_CMD_START:
  170. // Command from main thread: the app's activity has been started.
  171. break;
  172. case APP_CMD_RESUME:
  173. // Command from main thread: the app's activity has been resumed.
  174. break;
  175. case APP_CMD_SAVE_STATE:
  176. // Command from main thread: the app should generate a new saved state
  177. // for itself, to restore from later if needed. If you have saved state,
  178. // allocate it with malloc and place it in android_app.savedState with
  179. // the size in android_app.savedStateSize. The will be freed for you
  180. // later.
  181. break;
  182. case APP_CMD_PAUSE:
  183. // Command from main thread: the app's activity has been paused.
  184. break;
  185. case APP_CMD_STOP:
  186. // Command from main thread: the app's activity has been stopped.
  187. break;
  188. case APP_CMD_DESTROY:
  189. // Command from main thread: the app's activity is being destroyed,
  190. // and waiting for the app thread to clean up and exit before proceeding.
  191. m_eventQueue.postExitEvent();
  192. break;
  193. }
  194. }
  195. bool filter(GamepadAxis::Enum _axis, int32_t* _value)
  196. {
  197. const int32_t old = m_value[_axis];
  198. const int32_t deadzone = m_deadzone[_axis];
  199. int32_t value = *_value;
  200. value = value > deadzone || value < -deadzone ? value : 0;
  201. m_value[_axis] = value;
  202. *_value = value;
  203. return old != value;
  204. }
  205. int32_t onInputEvent(AInputEvent* _event)
  206. {
  207. WindowHandle defaultWindow = { 0 };
  208. GamepadHandle handle = { 0 };
  209. const int32_t type = AInputEvent_getType(_event);
  210. const int32_t source = AInputEvent_getSource(_event);
  211. const int32_t actionBits = AMotionEvent_getAction(_event);
  212. switch (type)
  213. {
  214. case AINPUT_EVENT_TYPE_MOTION:
  215. {
  216. if (0 != (source & (AINPUT_SOURCE_GAMEPAD|AINPUT_SOURCE_JOYSTICK) ) )
  217. {
  218. for (uint32_t ii = 0; ii < BX_COUNTOF(s_translateAxis); ++ii)
  219. {
  220. const float fval = AMotionEvent_getAxisValue(_event, s_translateAxis[ii].m_event, 0);
  221. int32_t value = int32_t( (s_translateAxis[ii].m_convert ? fval * 2.0f + 1.0f : fval) * INT16_MAX);
  222. GamepadAxis::Enum axis = s_translateAxis[ii].m_axis;
  223. if (filter(axis, &value) )
  224. {
  225. m_eventQueue.postAxisEvent(defaultWindow, handle, axis, value);
  226. }
  227. }
  228. return 1;
  229. }
  230. else
  231. {
  232. float mx = AMotionEvent_getX(_event, 0);
  233. float my = AMotionEvent_getY(_event, 0);
  234. int32_t count = AMotionEvent_getPointerCount(_event);
  235. int32_t action = (actionBits & AMOTION_EVENT_ACTION_MASK);
  236. int32_t index = (actionBits & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) >> AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
  237. count = m_count;
  238. switch (action)
  239. {
  240. case AMOTION_EVENT_ACTION_DOWN:
  241. case AMOTION_EVENT_ACTION_POINTER_DOWN:
  242. m_count++;
  243. break;
  244. case AMOTION_EVENT_ACTION_UP:
  245. case AMOTION_EVENT_ACTION_POINTER_UP:
  246. m_count--;
  247. break;
  248. default:
  249. break;
  250. }
  251. if (count != m_count)
  252. {
  253. m_eventQueue.postMouseEvent(defaultWindow
  254. , (int32_t)mx
  255. , (int32_t)my
  256. , 0
  257. , 1 == count ? MouseButton::Left : MouseButton::Right
  258. , false
  259. );
  260. if (0 != m_count)
  261. {
  262. m_eventQueue.postMouseEvent(defaultWindow
  263. , (int32_t)mx
  264. , (int32_t)my
  265. , 0
  266. , 1 == m_count ? MouseButton::Left : MouseButton::Right
  267. , true
  268. );
  269. }
  270. }
  271. switch (action)
  272. {
  273. case AMOTION_EVENT_ACTION_MOVE:
  274. if (0 == index)
  275. {
  276. m_eventQueue.postMouseEvent(defaultWindow
  277. , (int32_t)mx
  278. , (int32_t)my
  279. , 0
  280. );
  281. }
  282. break;
  283. default:
  284. break;
  285. }
  286. }
  287. }
  288. break;
  289. case AINPUT_EVENT_TYPE_KEY:
  290. {
  291. int32_t keyCode = AKeyEvent_getKeyCode(_event);
  292. if (0 != (source & (AINPUT_SOURCE_GAMEPAD|AINPUT_SOURCE_JOYSTICK) ) )
  293. {
  294. for (uint32_t jj = 0; jj < BX_COUNTOF(s_gamepadRemap); ++jj)
  295. {
  296. if (keyCode == s_gamepadRemap[jj].m_keyCode)
  297. {
  298. m_eventQueue.postKeyEvent(defaultWindow, s_gamepadRemap[jj].m_key, 0, actionBits == AKEY_EVENT_ACTION_DOWN);
  299. break;
  300. }
  301. }
  302. }
  303. return 1;
  304. }
  305. break;
  306. default:
  307. DBG("type %d", type);
  308. break;
  309. }
  310. return 0;
  311. }
  312. static void onAppCmdCB(struct android_app* _app, int32_t _cmd)
  313. {
  314. Context* self = (Context*)_app->userData;
  315. self->onAppCmd(_cmd);
  316. }
  317. static int32_t onInputEventCB(struct android_app* _app, AInputEvent* _event)
  318. {
  319. Context* self = (Context*)_app->userData;
  320. return self->onInputEvent(_event);
  321. }
  322. MainThreadEntry m_mte;
  323. bx::Thread m_thread;
  324. EventQueue m_eventQueue;
  325. ANativeWindow* m_window;
  326. android_app* m_app;
  327. int32_t m_count;
  328. int32_t m_value[GamepadAxis::Count];
  329. int32_t m_deadzone[GamepadAxis::Count];
  330. };
  331. static Context s_ctx;
  332. const Event* poll()
  333. {
  334. return s_ctx.m_eventQueue.poll();
  335. }
  336. const Event* poll(WindowHandle _handle)
  337. {
  338. return s_ctx.m_eventQueue.poll(_handle);
  339. }
  340. void release(const Event* _event)
  341. {
  342. s_ctx.m_eventQueue.release(_event);
  343. }
  344. WindowHandle createWindow(int32_t _x, int32_t _y, uint32_t _width, uint32_t _height, uint32_t _flags, const char* _title)
  345. {
  346. BX_UNUSED(_x, _y, _width, _height, _flags, _title);
  347. WindowHandle handle = { UINT16_MAX };
  348. return handle;
  349. }
  350. void destroyWindow(WindowHandle _handle)
  351. {
  352. BX_UNUSED(_handle);
  353. }
  354. void setWindowPos(WindowHandle _handle, int32_t _x, int32_t _y)
  355. {
  356. BX_UNUSED(_handle, _x, _y);
  357. }
  358. void setWindowSize(WindowHandle _handle, uint32_t _width, uint32_t _height)
  359. {
  360. BX_UNUSED(_handle, _width, _height);
  361. }
  362. void setWindowTitle(WindowHandle _handle, const char* _title)
  363. {
  364. BX_UNUSED(_handle, _title);
  365. }
  366. void toggleWindowFrame(WindowHandle _handle)
  367. {
  368. BX_UNUSED(_handle);
  369. }
  370. void setMouseLock(WindowHandle _handle, bool _lock)
  371. {
  372. BX_UNUSED(_handle, _lock);
  373. }
  374. int32_t MainThreadEntry::threadFunc(void* _userData)
  375. {
  376. int32_t result = chdir("/sdcard/bgfx/examples/runtime");
  377. BX_CHECK(0 == result, "Failed to chdir to dir. android.permission.WRITE_EXTERNAL_STORAGE?", errno);
  378. MainThreadEntry* self = (MainThreadEntry*)_userData;
  379. result = main(self->m_argc, self->m_argv);
  380. // PostMessage(s_ctx.m_hwnd, WM_QUIT, 0, 0);
  381. return result;
  382. }
  383. } // namespace entry
  384. extern "C" void android_main(android_app* _app)
  385. {
  386. using namespace entry;
  387. s_ctx.run(_app);
  388. }
  389. #endif // ENTRY_CONFIG_USE_NATIVE && BX_PLATFORM_ANDROID