2
0

AndroidDevice.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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 <jni.h>
  24. #include <android/sensor.h>
  25. #include <android_native_app_glue.h>
  26. #include "Allocator.h"
  27. #include "Device.h"
  28. #include "Log.h"
  29. #include "OsEventQueue.h"
  30. #include "Renderer.h"
  31. #include "Touch.h"
  32. #include "OsWindow.h"
  33. namespace crown
  34. {
  35. ANativeWindow* g_android_window;
  36. AAssetManager* g_android_asset_manager;
  37. class AndroidDevice : public Device
  38. {
  39. public:
  40. //-----------------------------------------------------------------------------
  41. AndroidDevice()
  42. : m_game_thread("game_thread")
  43. {
  44. #if defined(CROWN_DEBUG) || defined(CROWN_DEVELOPMENT)
  45. m_fileserver = 1;
  46. #endif
  47. }
  48. //-----------------------------------------------------------------------------
  49. void display_modes(Array<DisplayMode>& /*modes*/)
  50. {
  51. // Do nothing
  52. }
  53. //-----------------------------------------------------------------------------
  54. void set_display_mode(uint32_t /*id*/)
  55. {
  56. // Do nothing
  57. }
  58. //-----------------------------------------------------------------------------
  59. void set_fullscreen(bool /*full*/)
  60. {
  61. // Do nothing
  62. }
  63. void run(struct android_app* app)
  64. {
  65. app->userData = this;
  66. app->onAppCmd = crown::AndroidDevice::on_app_cmd;
  67. app->onInputEvent = crown::AndroidDevice::on_input_event;
  68. g_android_asset_manager = app->activity->assetManager;
  69. while (app->destroyRequested == 0)
  70. {
  71. int32_t num;
  72. android_poll_source* source;
  73. /*int32_t id =*/ ALooper_pollAll(-1, NULL, &num, (void**)&source);
  74. if (NULL != source)
  75. {
  76. source->process(app, source);
  77. }
  78. }
  79. m_game_thread.stop();
  80. }
  81. //-----------------------------------------------------------------------------
  82. int32_t run(int, char**)
  83. {
  84. return 0;
  85. }
  86. //-----------------------------------------------------------------------------
  87. void loop()
  88. {
  89. #if defined(CROWN_DEBUG) || defined(CROWN_DEVELOPMENT)
  90. m_console = CE_NEW(default_allocator(), ConsoleServer)();
  91. m_console->init(m_console_port, false);
  92. #endif
  93. Device::init();
  94. // Push metrics here since Android does not trigger APP_CMD_WINDOW_RESIZED
  95. const int32_t width = ANativeWindow_getWidth(g_android_window);
  96. const int32_t height = ANativeWindow_getHeight(g_android_window);
  97. m_queue.push_metrics_event(0, 0, width, height);
  98. while (is_running() && !process_events())
  99. {
  100. #if defined(CROWN_DEBUG) || defined(CROWN_DEVELOPMENT)
  101. m_console->update();
  102. #endif
  103. Device::frame();
  104. m_touch->update();
  105. m_keyboard->update();
  106. }
  107. Device::shutdown();
  108. #if defined(CROWN_DEBUG) || defined(CROWN_DEVELOPMENT)
  109. m_console->shutdown();
  110. CE_DELETE(default_allocator(), m_console);
  111. #endif
  112. }
  113. //-----------------------------------------------------------------------------
  114. static int32_t main_loop(void* thiz)
  115. {
  116. return ((AndroidDevice*) thiz)->loop();
  117. }
  118. //-----------------------------------------------------------------------------
  119. bool process_events()
  120. {
  121. OsEvent event;
  122. while (m_queue.pop_event(event))
  123. {
  124. if (event.type == OsEvent::NONE) continue;
  125. switch (event.type)
  126. {
  127. case OsEvent::TOUCH:
  128. {
  129. const OsTouchEvent& ev = event.touch;
  130. switch (ev.type)
  131. {
  132. case OsTouchEvent::POINTER: m_touch->set_pointer_state(ev.x, ev.y, ev.pointer_id, ev.pressed); break;
  133. case OsTouchEvent::MOVE: m_touch->set_position(ev.pointer_id, ev.x, ev.y); break;
  134. default: CE_FATAL("Oops, unknown touch event type"); break;
  135. }
  136. break;
  137. }
  138. case OsEvent::KEYBOARD:
  139. {
  140. const OsKeyboardEvent& ev = event.keyboard;
  141. m_keyboard->set_button_state(ev.button, ev.pressed);
  142. break;
  143. }
  144. case OsEvent::METRICS:
  145. {
  146. const OsMetricsEvent& ev = event.metrics;
  147. m_window->m_x = ev.x;
  148. m_window->m_y = ev.y;
  149. m_window->m_width = ev.width;
  150. m_window->m_height = ev.height;
  151. break;
  152. }
  153. case OsEvent::EXIT:
  154. {
  155. return true;
  156. }
  157. case OsEvent::PAUSE:
  158. {
  159. pause();
  160. break;
  161. }
  162. case OsEvent::RESUME:
  163. {
  164. unpause();
  165. break;
  166. }
  167. default:
  168. {
  169. CE_FATAL("Unknown Os Event");
  170. break;
  171. }
  172. }
  173. }
  174. return false;
  175. }
  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. //-----------------------------------------------------------------------------
  182. void process_command(struct android_app* app, int32_t cmd)
  183. {
  184. switch (cmd)
  185. {
  186. case APP_CMD_SAVE_STATE:
  187. {
  188. // // The system has asked us to save our current state. Do so.
  189. // engine->app->savedState = malloc(sizeof(struct saved_state));
  190. // *((struct saved_state*)engine->app->savedState) = engine->state;
  191. // engine->app->savedStateSize = sizeof(struct saved_state);
  192. break;
  193. }
  194. case APP_CMD_INIT_WINDOW:
  195. {
  196. CE_ASSERT(app->window != NULL, "Android window is NULL");
  197. g_android_window = app->window;
  198. m_game_thread.start(main_loop, (void*)this);
  199. break;
  200. }
  201. case APP_CMD_TERM_WINDOW:
  202. {
  203. // The window is being hidden or closed, clean it up.
  204. break;
  205. }
  206. case APP_CMD_WINDOW_RESIZED:
  207. {
  208. // Not triggered by Android
  209. break;
  210. }
  211. case APP_CMD_GAINED_FOCUS:
  212. {
  213. break;
  214. }
  215. case APP_CMD_LOST_FOCUS:
  216. {
  217. break;
  218. }
  219. case APP_CMD_DESTROY:
  220. {
  221. m_queue.push_exit_event(0);
  222. break;
  223. }
  224. }
  225. }
  226. //-----------------------------------------------------------------------------
  227. static int32_t on_input_event(struct android_app* app, AInputEvent* event)
  228. {
  229. return ((AndroidDevice*) app->userData)->process_input(app, event);
  230. }
  231. //-----------------------------------------------------------------------------
  232. int32_t process_input(struct android_app* app, AInputEvent* event)
  233. {
  234. if (AInputEvent_getType(event) == AINPUT_EVENT_TYPE_MOTION)
  235. {
  236. const int32_t action = AMotionEvent_getAction(event);
  237. const int32_t pointerIndex = (action & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) >> AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
  238. const int32_t pointerCount = AMotionEvent_getPointerCount(event);
  239. const int32_t pointerId = AMotionEvent_getPointerId(event, pointerIndex);
  240. const float x = AMotionEvent_getX(event, pointerIndex);
  241. const float y = AMotionEvent_getY(event, pointerIndex);
  242. const int32_t actionMasked = (action & AMOTION_EVENT_ACTION_MASK);
  243. switch (actionMasked)
  244. {
  245. case AMOTION_EVENT_ACTION_DOWN:
  246. case AMOTION_EVENT_ACTION_POINTER_DOWN:
  247. {
  248. m_queue.push_touch_event((uint16_t) x, (uint16_t) y, (uint8_t) pointerId, true);
  249. break;
  250. }
  251. case AMOTION_EVENT_ACTION_UP:
  252. case AMOTION_EVENT_ACTION_POINTER_UP:
  253. {
  254. m_queue.push_touch_event((uint16_t) x, (uint16_t) y, (uint8_t) pointerId, false);
  255. break;
  256. }
  257. case AMOTION_EVENT_ACTION_OUTSIDE:
  258. case AMOTION_EVENT_ACTION_CANCEL:
  259. {
  260. m_queue.push_touch_event((uint16_t) x, (uint16_t) y, (uint8_t) pointerId, false);
  261. break;
  262. }
  263. case AMOTION_EVENT_ACTION_MOVE:
  264. {
  265. for (int index = 0; index < pointerCount; index++)
  266. {
  267. const float xx = AMotionEvent_getX(event, index);
  268. const float yy = AMotionEvent_getY(event, index);
  269. const int32_t id = AMotionEvent_getPointerId(event, index);
  270. m_queue.push_touch_event((uint16_t) xx, (uint16_t) yy, (uint8_t) id);
  271. }
  272. break;
  273. }
  274. }
  275. return 1;
  276. }
  277. else if (AInputEvent_getType(event) == AINPUT_EVENT_TYPE_KEY)
  278. {
  279. const int32_t keycode = AKeyEvent_getKeyCode(event);
  280. const int32_t keyaction = AKeyEvent_getAction(event);
  281. if (keycode == AKEYCODE_BACK)
  282. {
  283. m_queue.push_keyboard_event(0, KeyboardButton::ESCAPE, keyaction == AKEY_EVENT_ACTION_DOWN ? true : false);
  284. }
  285. return 1;
  286. }
  287. return 0;
  288. }
  289. private:
  290. OsEventQueue m_queue;
  291. OsThread m_game_thread;
  292. };
  293. } // namespace crown
  294. void android_main(struct android_app* app)
  295. {
  296. // Make sure glue isn't stripped.
  297. app_dummy();
  298. crown::memory::init();
  299. crown::os::init_os();
  300. crown::AndroidDevice* engine = CE_NEW(crown::default_allocator(), crown::AndroidDevice)();
  301. crown::set_device(engine);
  302. engine->run(app);
  303. CE_DELETE(crown::default_allocator(), engine);
  304. crown::memory::shutdown();
  305. }