entry_android.cpp 6.6 KB

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