entry_android.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * Copyright 2011-2014 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. #include <android_native_app_glue.c>
  18. } // extern "C"
  19. namespace entry
  20. {
  21. struct MainThreadEntry
  22. {
  23. int m_argc;
  24. char** m_argv;
  25. static int32_t threadFunc(void* _userData);
  26. };
  27. struct Context
  28. {
  29. Context()
  30. : m_window(NULL)
  31. {
  32. }
  33. void run(android_app* _app)
  34. {
  35. m_app = _app;
  36. m_app->userData = (void*)this;
  37. m_app->onAppCmd = onAppCmdCB;
  38. m_app->onInputEvent = onInputEventCB;
  39. const char* argv[1] = { "android.so" };
  40. m_mte.m_argc = 1;
  41. m_mte.m_argv = const_cast<char**>(argv);
  42. while (0 == m_app->destroyRequested)
  43. {
  44. int32_t num;
  45. android_poll_source* source;
  46. /*int32_t id =*/ ALooper_pollAll(-1, NULL, &num, (void**)&source);
  47. if (NULL != source)
  48. {
  49. source->process(m_app, source);
  50. }
  51. }
  52. m_thread.shutdown();
  53. }
  54. void onAppCmd(int32_t _cmd)
  55. {
  56. switch (_cmd)
  57. {
  58. case APP_CMD_INPUT_CHANGED:
  59. // Command from main thread: the AInputQueue has changed. Upon processing
  60. // this command, android_app->inputQueue will be updated to the new queue
  61. // (or NULL).
  62. break;
  63. case APP_CMD_INIT_WINDOW:
  64. // Command from main thread: a new ANativeWindow is ready for use. Upon
  65. // receiving this command, android_app->window will contain the new window
  66. // surface.
  67. if (m_window == NULL)
  68. {
  69. m_window = m_app->window;
  70. bgfx::androidSetWindow(m_app->window);
  71. m_thread.init(MainThreadEntry::threadFunc, &m_mte);
  72. }
  73. break;
  74. case APP_CMD_TERM_WINDOW:
  75. // Command from main thread: the existing ANativeWindow needs to be
  76. // terminated. Upon receiving this command, android_app->window still
  77. // contains the existing window; after calling android_app_exec_cmd
  78. // it will be set to NULL.
  79. break;
  80. case APP_CMD_WINDOW_RESIZED:
  81. // Command from main thread: the current ANativeWindow has been resized.
  82. // Please redraw with its new size.
  83. break;
  84. case APP_CMD_WINDOW_REDRAW_NEEDED:
  85. // Command from main thread: the system needs that the current ANativeWindow
  86. // be redrawn. You should redraw the window before handing this to
  87. // android_app_exec_cmd() in order to avoid transient drawing glitches.
  88. break;
  89. case APP_CMD_CONTENT_RECT_CHANGED:
  90. // Command from main thread: the content area of the window has changed,
  91. // such as from the soft input window being shown or hidden. You can
  92. // find the new content rect in android_app::contentRect.
  93. break;
  94. case APP_CMD_GAINED_FOCUS:
  95. // Command from main thread: the app's activity window has gained
  96. // input focus.
  97. break;
  98. case APP_CMD_LOST_FOCUS:
  99. // Command from main thread: the app's activity window has lost
  100. // input focus.
  101. break;
  102. case APP_CMD_CONFIG_CHANGED:
  103. // Command from main thread: the current device configuration has changed.
  104. break;
  105. case APP_CMD_LOW_MEMORY:
  106. // Command from main thread: the system is running low on memory.
  107. // Try to reduce your memory use.
  108. break;
  109. case APP_CMD_START:
  110. // Command from main thread: the app's activity has been started.
  111. break;
  112. case APP_CMD_RESUME:
  113. // Command from main thread: the app's activity has been resumed.
  114. break;
  115. case APP_CMD_SAVE_STATE:
  116. // Command from main thread: the app should generate a new saved state
  117. // for itself, to restore from later if needed. If you have saved state,
  118. // allocate it with malloc and place it in android_app.savedState with
  119. // the size in android_app.savedStateSize. The will be freed for you
  120. // later.
  121. break;
  122. case APP_CMD_PAUSE:
  123. // Command from main thread: the app's activity has been paused.
  124. break;
  125. case APP_CMD_STOP:
  126. // Command from main thread: the app's activity has been stopped.
  127. break;
  128. case APP_CMD_DESTROY:
  129. // Command from main thread: the app's activity is being destroyed,
  130. // and waiting for the app thread to clean up and exit before proceeding.
  131. m_eventQueue.postExitEvent();
  132. break;
  133. }
  134. }
  135. int32_t onInputEvent(AInputEvent* _event)
  136. {
  137. BX_UNUSED(_event);
  138. return 0;
  139. }
  140. static void onAppCmdCB(struct android_app* _app, int32_t _cmd)
  141. {
  142. Context* self = (Context*)_app->userData;
  143. self->onAppCmd(_cmd);
  144. }
  145. static int32_t onInputEventCB(struct android_app* _app, AInputEvent* _event)
  146. {
  147. Context* self = (Context*)_app->userData;
  148. return self->onInputEvent(_event);
  149. }
  150. MainThreadEntry m_mte;
  151. bx::Thread m_thread;
  152. EventQueue m_eventQueue;
  153. ANativeWindow* m_window;
  154. android_app* m_app;
  155. };
  156. static Context s_ctx;
  157. const Event* poll()
  158. {
  159. return s_ctx.m_eventQueue.poll();
  160. }
  161. void release(const Event* _event)
  162. {
  163. s_ctx.m_eventQueue.release(_event);
  164. }
  165. void setWindowSize(uint32_t _width, uint32_t _height)
  166. {
  167. BX_UNUSED(_width, _height);
  168. }
  169. void setWindowTitle(const char* _title)
  170. {
  171. BX_UNUSED(_title);
  172. }
  173. void toggleWindowFrame()
  174. {
  175. }
  176. void setMouseLock(bool _lock)
  177. {
  178. BX_UNUSED(_lock);
  179. }
  180. int32_t MainThreadEntry::threadFunc(void* _userData)
  181. {
  182. int32_t result = chdir("/sdcard/bgfx/examples/runtime");
  183. BX_CHECK(0 == result, "Failed to chdir to dir. android.permission.WRITE_EXTERNAL_STORAGE?", errno);
  184. MainThreadEntry* self = (MainThreadEntry*)_userData;
  185. result = main(self->m_argc, self->m_argv);
  186. // PostMessage(s_ctx.m_hwnd, WM_QUIT, 0, 0);
  187. return result;
  188. }
  189. } // namespace entry
  190. extern "C" void android_main(android_app* _app)
  191. {
  192. using namespace entry;
  193. s_ctx.run(_app);
  194. }
  195. #endif // ENTRY_CONFIG_USE_NATIVE && BX_PLATFORM_ANDROID