entry_android.cpp 6.7 KB

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