main_android.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #include "config.h"
  24. #if CROWN_PLATFORM_ANDROID
  25. #include "os_event_queue.h"
  26. #include "os_window_android.h"
  27. #include "thread.h"
  28. #include "main.h"
  29. #include "apk_filesystem.h"
  30. #include "console_server.h"
  31. #include "crown.h"
  32. #include <jni.h>
  33. #include <android/sensor.h>
  34. #include <android_native_app_glue.h>
  35. #include <bgfxplatform.h>
  36. extern "C"
  37. {
  38. #include <android_native_app_glue.c>
  39. }
  40. namespace crown
  41. {
  42. // void display_modes(Array<DisplayMode>& /*modes*/)
  43. // {
  44. // // Do nothing
  45. // }
  46. // void set_display_mode(uint32_t /*id*/)
  47. // {
  48. // // Do nothing
  49. // }
  50. // void set_fullscreen(bool /*full*/)
  51. // {
  52. // // Do nothing
  53. // }
  54. static bool s_exit = false;
  55. struct MainThreadArgs
  56. {
  57. Filesystem* fs;
  58. ConfigSettings* cs;
  59. };
  60. int32_t func(void* data)
  61. {
  62. MainThreadArgs* args = (MainThreadArgs*) data;
  63. crown::init(*args->fs, *args->cs);
  64. crown::update();
  65. crown::shutdown();
  66. s_exit = true;
  67. return EXIT_SUCCESS;
  68. }
  69. struct AndroidDevice
  70. {
  71. void run(struct android_app* app, Filesystem& fs, ConfigSettings& cs)
  72. {
  73. _margs.fs = &fs;
  74. _margs.cs = &cs;
  75. app->userData = this;
  76. app->onAppCmd = crown::AndroidDevice::on_app_cmd;
  77. app->onInputEvent = crown::AndroidDevice::on_input_event;
  78. while (app->destroyRequested == 0)
  79. {
  80. int32_t num;
  81. android_poll_source* source;
  82. /*int32_t id =*/ ALooper_pollAll(-1, NULL, &num, (void**)&source);
  83. if (NULL != source)
  84. {
  85. source->process(app, source);
  86. }
  87. }
  88. _main_thread.stop();
  89. }
  90. void process_command(struct android_app* app, int32_t cmd)
  91. {
  92. switch (cmd)
  93. {
  94. case APP_CMD_SAVE_STATE:
  95. {
  96. break;
  97. }
  98. case APP_CMD_INIT_WINDOW:
  99. {
  100. CE_ASSERT(app->window != NULL, "Android window is NULL");
  101. bgfx::androidSetWindow(app->window);
  102. // Push metrics here since Android does not trigger APP_CMD_WINDOW_RESIZED
  103. const int32_t width = ANativeWindow_getWidth(app->window);
  104. const int32_t height = ANativeWindow_getHeight(app->window);
  105. _queue.push_metrics_event(0, 0, width, height);
  106. _main_thread.start(func, &_margs);
  107. break;
  108. }
  109. case APP_CMD_TERM_WINDOW:
  110. {
  111. // The window is being hidden or closed, clean it up.
  112. break;
  113. }
  114. case APP_CMD_WINDOW_RESIZED:
  115. {
  116. // Not triggered by Android
  117. break;
  118. }
  119. case APP_CMD_GAINED_FOCUS:
  120. {
  121. break;
  122. }
  123. case APP_CMD_LOST_FOCUS:
  124. {
  125. break;
  126. }
  127. case APP_CMD_DESTROY:
  128. {
  129. _queue.push_exit_event(0);
  130. break;
  131. }
  132. }
  133. }
  134. int32_t process_input(struct android_app* app, AInputEvent* event)
  135. {
  136. if (AInputEvent_getType(event) == AINPUT_EVENT_TYPE_MOTION)
  137. {
  138. const int32_t action = AMotionEvent_getAction(event);
  139. const int32_t pointerIndex = (action & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) >> AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
  140. const int32_t pointerCount = AMotionEvent_getPointerCount(event);
  141. const int32_t pointerId = AMotionEvent_getPointerId(event, pointerIndex);
  142. const float x = AMotionEvent_getX(event, pointerIndex);
  143. const float y = AMotionEvent_getY(event, pointerIndex);
  144. const int32_t actionMasked = (action & AMOTION_EVENT_ACTION_MASK);
  145. switch (actionMasked)
  146. {
  147. case AMOTION_EVENT_ACTION_DOWN:
  148. case AMOTION_EVENT_ACTION_POINTER_DOWN:
  149. {
  150. _queue.push_touch_event((uint16_t) x, (uint16_t) y, (uint8_t) pointerId, true);
  151. break;
  152. }
  153. case AMOTION_EVENT_ACTION_UP:
  154. case AMOTION_EVENT_ACTION_POINTER_UP:
  155. {
  156. _queue.push_touch_event((uint16_t) x, (uint16_t) y, (uint8_t) pointerId, false);
  157. break;
  158. }
  159. case AMOTION_EVENT_ACTION_OUTSIDE:
  160. case AMOTION_EVENT_ACTION_CANCEL:
  161. {
  162. _queue.push_touch_event((uint16_t) x, (uint16_t) y, (uint8_t) pointerId, false);
  163. break;
  164. }
  165. case AMOTION_EVENT_ACTION_MOVE:
  166. {
  167. for (int index = 0; index < pointerCount; index++)
  168. {
  169. const float xx = AMotionEvent_getX(event, index);
  170. const float yy = AMotionEvent_getY(event, index);
  171. const int32_t id = AMotionEvent_getPointerId(event, index);
  172. _queue.push_touch_event((uint16_t) xx, (uint16_t) yy, (uint8_t) id);
  173. }
  174. break;
  175. }
  176. }
  177. return 1;
  178. }
  179. else if (AInputEvent_getType(event) == AINPUT_EVENT_TYPE_KEY)
  180. {
  181. const int32_t keycode = AKeyEvent_getKeyCode(event);
  182. const int32_t keyaction = AKeyEvent_getAction(event);
  183. if (keycode == AKEYCODE_BACK)
  184. {
  185. _queue.push_keyboard_event(0, KeyboardButton::ESCAPE, keyaction == AKEY_EVENT_ACTION_DOWN ? true : false);
  186. }
  187. return 1;
  188. }
  189. return 0;
  190. }
  191. static int32_t on_input_event(struct android_app* app, AInputEvent* event)
  192. {
  193. return ((AndroidDevice*) app->userData)->process_input(app, event);
  194. }
  195. static void on_app_cmd(struct android_app* app, int32_t cmd)
  196. {
  197. ((AndroidDevice*) app->userData)->process_command(app, cmd);
  198. }
  199. public:
  200. OsEventQueue _queue;
  201. Thread _main_thread;
  202. MainThreadArgs _margs;
  203. };
  204. static AndroidDevice s_advc;
  205. bool next_event(OsEvent& ev)
  206. {
  207. return s_advc._queue.pop_event(ev);
  208. }
  209. } // namespace crown
  210. void android_main(struct android_app* app)
  211. {
  212. using namespace crown;
  213. // Make sure glue isn't stripped.
  214. app_dummy();
  215. memory_globals::init();
  216. // DiskFilesystem src_fs(cls.source_dir);
  217. ApkFilesystem src_fs(app->activity->assetManager);
  218. ConfigSettings cs = parse_config_file(src_fs);
  219. console_server_globals::init();
  220. console_server_globals::console().init(cs.console_port, false);
  221. crown::s_advc.run(app, src_fs, cs);
  222. console_server_globals::shutdown();
  223. memory_globals::shutdown();
  224. }
  225. #endif // CROWN_PLATFORM_ANDROID