entry_android.cpp 7.0 KB

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