entry_android.cpp 5.5 KB

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