entry_android.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. * Copyright 2011-2013 Branimir Karadzic. All rights reserved.
  3. * License: http://www.opensource.org/licenses/BSD-2-Clause
  4. */
  5. #include "entry.h"
  6. #if BX_PLATFORM_ANDROID
  7. #include <bgfxplatform.h>
  8. #include "entry_p.h"
  9. #include <stdio.h>
  10. #include <bx/thread.h>
  11. #include <android/input.h>
  12. #include <android/log.h>
  13. #include <android/looper.h>
  14. #include <android/window.h>
  15. #include <android_native_app_glue.h>
  16. extern "C"
  17. {
  18. #include <android_native_app_glue.c>
  19. } // extern "C"
  20. namespace entry
  21. {
  22. struct MainThreadEntry
  23. {
  24. int m_argc;
  25. char** m_argv;
  26. static int32_t threadFunc(void* _userData);
  27. };
  28. struct Context
  29. {
  30. Context()
  31. : m_window(NULL)
  32. {
  33. }
  34. void run(android_app* _app)
  35. {
  36. m_app = _app;
  37. m_app->userData = (void*)this;
  38. m_app->onAppCmd = onAppCmdCB;
  39. m_app->onInputEvent = onInputEventCB;
  40. const char* argv[1] = { "android.so" };
  41. m_mte.m_argc = 1;
  42. m_mte.m_argv = const_cast<char**>(argv);
  43. while (0 == m_app->destroyRequested)
  44. {
  45. int32_t num;
  46. android_poll_source* source;
  47. /*int32_t id =*/ ALooper_pollAll(-1, NULL, &num, (void**)&source);
  48. if (NULL != source)
  49. {
  50. source->process(m_app, source);
  51. }
  52. }
  53. m_thread.shutdown();
  54. }
  55. void onAppCmd(int32_t _cmd)
  56. {
  57. switch (_cmd)
  58. {
  59. case APP_CMD_INPUT_CHANGED:
  60. // Command from main thread: the AInputQueue has changed. Upon processing
  61. // this command, android_app->inputQueue will be updated to the new queue
  62. // (or NULL).
  63. break;
  64. case APP_CMD_INIT_WINDOW:
  65. // Command from main thread: a new ANativeWindow is ready for use. Upon
  66. // receiving this command, android_app->window will contain the new window
  67. // surface.
  68. if (m_window == NULL)
  69. {
  70. m_window = m_app->window;
  71. bgfx::androidSetWindow(m_app->window);
  72. m_thread.init(MainThreadEntry::threadFunc, &m_mte);
  73. }
  74. break;
  75. case APP_CMD_TERM_WINDOW:
  76. // Command from main thread: the existing ANativeWindow needs to be
  77. // terminated. Upon receiving this command, android_app->window still
  78. // contains the existing window; after calling android_app_exec_cmd
  79. // it will be set to NULL.
  80. break;
  81. case APP_CMD_WINDOW_RESIZED:
  82. // Command from main thread: the current ANativeWindow has been resized.
  83. // Please redraw with its new size.
  84. break;
  85. case APP_CMD_WINDOW_REDRAW_NEEDED:
  86. // Command from main thread: the system needs that the current ANativeWindow
  87. // be redrawn. You should redraw the window before handing this to
  88. // android_app_exec_cmd() in order to avoid transient drawing glitches.
  89. break;
  90. case APP_CMD_CONTENT_RECT_CHANGED:
  91. // Command from main thread: the content area of the window has changed,
  92. // such as from the soft input window being shown or hidden. You can
  93. // find the new content rect in android_app::contentRect.
  94. break;
  95. case APP_CMD_GAINED_FOCUS:
  96. // Command from main thread: the app's activity window has gained
  97. // input focus.
  98. break;
  99. case APP_CMD_LOST_FOCUS:
  100. // Command from main thread: the app's activity window has lost
  101. // input focus.
  102. break;
  103. case APP_CMD_CONFIG_CHANGED:
  104. // Command from main thread: the current device configuration has changed.
  105. break;
  106. case APP_CMD_LOW_MEMORY:
  107. // Command from main thread: the system is running low on memory.
  108. // Try to reduce your memory use.
  109. break;
  110. case APP_CMD_START:
  111. // Command from main thread: the app's activity has been started.
  112. break;
  113. case APP_CMD_RESUME:
  114. // Command from main thread: the app's activity has been resumed.
  115. break;
  116. case APP_CMD_SAVE_STATE:
  117. // Command from main thread: the app should generate a new saved state
  118. // for itself, to restore from later if needed. If you have saved state,
  119. // allocate it with malloc and place it in android_app.savedState with
  120. // the size in android_app.savedStateSize. The will be freed for you
  121. // later.
  122. break;
  123. case APP_CMD_PAUSE:
  124. // Command from main thread: the app's activity has been paused.
  125. break;
  126. case APP_CMD_STOP:
  127. // Command from main thread: the app's activity has been stopped.
  128. break;
  129. case APP_CMD_DESTROY:
  130. // Command from main thread: the app's activity is being destroyed,
  131. // and waiting for the app thread to clean up and exit before proceeding.
  132. m_eventQueue.postExitEvent();
  133. break;
  134. }
  135. }
  136. int32_t onInputEvent(AInputEvent* _event)
  137. {
  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. }
  168. void toggleWindowFrame()
  169. {
  170. }
  171. void setMouseLock(bool _lock)
  172. {
  173. }
  174. int32_t MainThreadEntry::threadFunc(void* _userData)
  175. {
  176. MainThreadEntry* self = (MainThreadEntry*)_userData;
  177. int32_t result = main(self->m_argc, self->m_argv);
  178. // PostMessage(s_ctx.m_hwnd, WM_QUIT, 0, 0);
  179. return result;
  180. }
  181. } // namespace entry
  182. extern "C" void android_main(android_app* _app)
  183. {
  184. using namespace entry;
  185. s_ctx.run(_app);
  186. }
  187. #endif // BX_PLATFORM_ANDROID