main_android.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. * Copyright (c) 2012-2014 Daniele Bartolini and individual contributors.
  3. * License: https://github.com/taylor001/crown/blob/master/LICENSE
  4. */
  5. #include "config.h"
  6. #if CROWN_PLATFORM_ANDROID
  7. #include "os_event_queue.h"
  8. #include "os_window_android.h"
  9. #include "thread.h"
  10. #include "main.h"
  11. #include "apk_filesystem.h"
  12. #include "console_server.h"
  13. #include "crown.h"
  14. #include <jni.h>
  15. #include <android/sensor.h>
  16. #include <android_native_app_glue.h>
  17. #include <bgfxplatform.h>
  18. extern "C"
  19. {
  20. #include <android_native_app_glue.c>
  21. }
  22. namespace crown
  23. {
  24. // void display_modes(Array<DisplayMode>& /*modes*/)
  25. // {
  26. // // Do nothing
  27. // }
  28. // void set_display_mode(uint32_t /*id*/)
  29. // {
  30. // // Do nothing
  31. // }
  32. // void set_fullscreen(bool /*full*/)
  33. // {
  34. // // Do nothing
  35. // }
  36. static bool s_exit = false;
  37. struct MainThreadArgs
  38. {
  39. Filesystem* fs;
  40. ConfigSettings* cs;
  41. };
  42. int32_t func(void* data)
  43. {
  44. MainThreadArgs* args = (MainThreadArgs*) data;
  45. crown::init(*args->fs, *args->cs);
  46. crown::update();
  47. crown::shutdown();
  48. s_exit = true;
  49. return EXIT_SUCCESS;
  50. }
  51. struct AndroidDevice
  52. {
  53. void run(struct android_app* app, Filesystem& fs, ConfigSettings& cs)
  54. {
  55. _margs.fs = &fs;
  56. _margs.cs = &cs;
  57. app->userData = this;
  58. app->onAppCmd = crown::AndroidDevice::on_app_cmd;
  59. app->onInputEvent = crown::AndroidDevice::on_input_event;
  60. while (app->destroyRequested == 0)
  61. {
  62. int32_t num;
  63. android_poll_source* source;
  64. /*int32_t id =*/ ALooper_pollAll(-1, NULL, &num, (void**)&source);
  65. if (NULL != source)
  66. {
  67. source->process(app, source);
  68. }
  69. }
  70. _main_thread.stop();
  71. }
  72. void process_command(struct android_app* app, int32_t cmd)
  73. {
  74. switch (cmd)
  75. {
  76. case APP_CMD_SAVE_STATE:
  77. {
  78. break;
  79. }
  80. case APP_CMD_INIT_WINDOW:
  81. {
  82. CE_ASSERT(app->window != NULL, "Android window is NULL");
  83. bgfx::androidSetWindow(app->window);
  84. // Push metrics here since Android does not trigger APP_CMD_WINDOW_RESIZED
  85. const int32_t width = ANativeWindow_getWidth(app->window);
  86. const int32_t height = ANativeWindow_getHeight(app->window);
  87. _queue.push_metrics_event(0, 0, width, height);
  88. _main_thread.start(func, &_margs);
  89. break;
  90. }
  91. case APP_CMD_TERM_WINDOW:
  92. {
  93. // The window is being hidden or closed, clean it up.
  94. break;
  95. }
  96. case APP_CMD_WINDOW_RESIZED:
  97. {
  98. // Not triggered by Android
  99. break;
  100. }
  101. case APP_CMD_GAINED_FOCUS:
  102. {
  103. break;
  104. }
  105. case APP_CMD_LOST_FOCUS:
  106. {
  107. break;
  108. }
  109. case APP_CMD_DESTROY:
  110. {
  111. _queue.push_exit_event(0);
  112. break;
  113. }
  114. }
  115. }
  116. int32_t process_input(struct android_app* app, AInputEvent* event)
  117. {
  118. if (AInputEvent_getType(event) == AINPUT_EVENT_TYPE_MOTION)
  119. {
  120. const int32_t action = AMotionEvent_getAction(event);
  121. const int32_t pointerIndex = (action & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) >> AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
  122. const int32_t pointerCount = AMotionEvent_getPointerCount(event);
  123. const int32_t pointerId = AMotionEvent_getPointerId(event, pointerIndex);
  124. const float x = AMotionEvent_getX(event, pointerIndex);
  125. const float y = AMotionEvent_getY(event, pointerIndex);
  126. const int32_t actionMasked = (action & AMOTION_EVENT_ACTION_MASK);
  127. switch (actionMasked)
  128. {
  129. case AMOTION_EVENT_ACTION_DOWN:
  130. case AMOTION_EVENT_ACTION_POINTER_DOWN:
  131. {
  132. _queue.push_touch_event((uint16_t) x, (uint16_t) y, (uint8_t) pointerId, true);
  133. break;
  134. }
  135. case AMOTION_EVENT_ACTION_UP:
  136. case AMOTION_EVENT_ACTION_POINTER_UP:
  137. {
  138. _queue.push_touch_event((uint16_t) x, (uint16_t) y, (uint8_t) pointerId, false);
  139. break;
  140. }
  141. case AMOTION_EVENT_ACTION_OUTSIDE:
  142. case AMOTION_EVENT_ACTION_CANCEL:
  143. {
  144. _queue.push_touch_event((uint16_t) x, (uint16_t) y, (uint8_t) pointerId, false);
  145. break;
  146. }
  147. case AMOTION_EVENT_ACTION_MOVE:
  148. {
  149. for (int index = 0; index < pointerCount; index++)
  150. {
  151. const float xx = AMotionEvent_getX(event, index);
  152. const float yy = AMotionEvent_getY(event, index);
  153. const int32_t id = AMotionEvent_getPointerId(event, index);
  154. _queue.push_touch_event((uint16_t) xx, (uint16_t) yy, (uint8_t) id);
  155. }
  156. break;
  157. }
  158. }
  159. return 1;
  160. }
  161. else if (AInputEvent_getType(event) == AINPUT_EVENT_TYPE_KEY)
  162. {
  163. const int32_t keycode = AKeyEvent_getKeyCode(event);
  164. const int32_t keyaction = AKeyEvent_getAction(event);
  165. if (keycode == AKEYCODE_BACK)
  166. {
  167. _queue.push_keyboard_event(0, KeyboardButton::ESCAPE, keyaction == AKEY_EVENT_ACTION_DOWN ? true : false);
  168. }
  169. return 1;
  170. }
  171. return 0;
  172. }
  173. static int32_t on_input_event(struct android_app* app, AInputEvent* event)
  174. {
  175. return ((AndroidDevice*) app->userData)->process_input(app, event);
  176. }
  177. static void on_app_cmd(struct android_app* app, int32_t cmd)
  178. {
  179. ((AndroidDevice*) app->userData)->process_command(app, cmd);
  180. }
  181. public:
  182. OsEventQueue _queue;
  183. Thread _main_thread;
  184. MainThreadArgs _margs;
  185. };
  186. static AndroidDevice s_advc;
  187. bool next_event(OsEvent& ev)
  188. {
  189. return s_advc._queue.pop_event(ev);
  190. }
  191. } // namespace crown
  192. void android_main(struct android_app* app)
  193. {
  194. using namespace crown;
  195. // Make sure glue isn't stripped.
  196. app_dummy();
  197. ConfigSettings cs;
  198. memory_globals::init();
  199. // DiskFilesystem src_fs(cls.source_dir);
  200. ApkFilesystem src_fs(app->activity->assetManager);
  201. parse_config_file(src_fs, cs);
  202. console_server_globals::init(cs.console_port, false);
  203. crown::s_advc.run(app, src_fs, cs);
  204. console_server_globals::shutdown();
  205. memory_globals::shutdown();
  206. }
  207. #endif // CROWN_PLATFORM_ANDROID