android_native_app_glue.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. /*
  2. * Copyright (C) 2010 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17. #ifdef ANDROID_NATIVE_ACTIVITY
  18. #include <jni.h>
  19. #include <errno.h>
  20. #include <string.h>
  21. #include <unistd.h>
  22. #include <sys/resource.h>
  23. #include "android_native_app_glue.h"
  24. #include <android/log.h>
  25. #define LOGI(...) ((void)__android_log_print(ANDROID_LOG_INFO, "threaded_app", __VA_ARGS__))
  26. static void free_saved_state(struct android_app* android_app) {
  27. pthread_mutex_lock(&android_app->mutex);
  28. if (android_app->savedState != NULL) {
  29. free(android_app->savedState);
  30. android_app->savedState = NULL;
  31. android_app->savedStateSize = 0;
  32. }
  33. pthread_mutex_unlock(&android_app->mutex);
  34. }
  35. int8_t android_app_read_cmd(struct android_app* android_app) {
  36. int8_t cmd;
  37. if (read(android_app->msgread, &cmd, sizeof(cmd)) == sizeof(cmd)) {
  38. switch (cmd) {
  39. case APP_CMD_SAVE_STATE:
  40. free_saved_state(android_app);
  41. break;
  42. }
  43. return cmd;
  44. } else {
  45. LOGI("No data on command pipe!");
  46. }
  47. return -1;
  48. }
  49. static void print_cur_config(struct android_app* android_app) {
  50. char lang[2], country[2];
  51. AConfiguration_getLanguage(android_app->config, lang);
  52. AConfiguration_getCountry(android_app->config, country);
  53. LOGI("Config: mcc=%d mnc=%d lang=%c%c cnt=%c%c orien=%d touch=%d dens=%d "
  54. "keys=%d nav=%d keysHid=%d navHid=%d sdk=%d size=%d long=%d "
  55. "modetype=%d modenight=%d",
  56. AConfiguration_getMcc(android_app->config),
  57. AConfiguration_getMnc(android_app->config),
  58. lang[0], lang[1], country[0], country[1],
  59. AConfiguration_getOrientation(android_app->config),
  60. AConfiguration_getTouchscreen(android_app->config),
  61. AConfiguration_getDensity(android_app->config),
  62. AConfiguration_getKeyboard(android_app->config),
  63. AConfiguration_getNavigation(android_app->config),
  64. AConfiguration_getKeysHidden(android_app->config),
  65. AConfiguration_getNavHidden(android_app->config),
  66. AConfiguration_getSdkVersion(android_app->config),
  67. AConfiguration_getScreenSize(android_app->config),
  68. AConfiguration_getScreenLong(android_app->config),
  69. AConfiguration_getUiModeType(android_app->config),
  70. AConfiguration_getUiModeNight(android_app->config));
  71. }
  72. void android_app_pre_exec_cmd(struct android_app* android_app, int8_t cmd) {
  73. switch (cmd) {
  74. case APP_CMD_INPUT_CHANGED:
  75. LOGI("APP_CMD_INPUT_CHANGED\n");
  76. pthread_mutex_lock(&android_app->mutex);
  77. if (android_app->inputQueue != NULL) {
  78. AInputQueue_detachLooper(android_app->inputQueue);
  79. }
  80. android_app->inputQueue = android_app->pendingInputQueue;
  81. if (android_app->inputQueue != NULL) {
  82. LOGI("Attaching input queue to looper");
  83. AInputQueue_attachLooper(android_app->inputQueue,
  84. android_app->looper, LOOPER_ID_INPUT, NULL,
  85. &android_app->inputPollSource);
  86. }
  87. pthread_cond_broadcast(&android_app->cond);
  88. pthread_mutex_unlock(&android_app->mutex);
  89. break;
  90. case APP_CMD_INIT_WINDOW:
  91. LOGI("APP_CMD_INIT_WINDOW\n");
  92. pthread_mutex_lock(&android_app->mutex);
  93. android_app->window = android_app->pendingWindow;
  94. pthread_cond_broadcast(&android_app->cond);
  95. pthread_mutex_unlock(&android_app->mutex);
  96. break;
  97. case APP_CMD_TERM_WINDOW:
  98. LOGI("APP_CMD_TERM_WINDOW\n");
  99. pthread_cond_broadcast(&android_app->cond);
  100. break;
  101. case APP_CMD_RESUME:
  102. case APP_CMD_START:
  103. case APP_CMD_PAUSE:
  104. case APP_CMD_STOP:
  105. LOGI("activityState=%d\n", cmd);
  106. pthread_mutex_lock(&android_app->mutex);
  107. android_app->activityState = cmd;
  108. pthread_cond_broadcast(&android_app->cond);
  109. pthread_mutex_unlock(&android_app->mutex);
  110. break;
  111. case APP_CMD_CONFIG_CHANGED:
  112. LOGI("APP_CMD_CONFIG_CHANGED\n");
  113. AConfiguration_fromAssetManager(android_app->config,
  114. android_app->activity->assetManager);
  115. print_cur_config(android_app);
  116. break;
  117. case APP_CMD_DESTROY:
  118. LOGI("APP_CMD_DESTROY\n");
  119. android_app->destroyRequested = 1;
  120. break;
  121. }
  122. }
  123. void android_app_post_exec_cmd(struct android_app* android_app, int8_t cmd) {
  124. switch (cmd) {
  125. case APP_CMD_TERM_WINDOW:
  126. LOGI("APP_CMD_TERM_WINDOW\n");
  127. pthread_mutex_lock(&android_app->mutex);
  128. android_app->window = NULL;
  129. pthread_cond_broadcast(&android_app->cond);
  130. pthread_mutex_unlock(&android_app->mutex);
  131. break;
  132. case APP_CMD_SAVE_STATE:
  133. LOGI("APP_CMD_SAVE_STATE\n");
  134. pthread_mutex_lock(&android_app->mutex);
  135. android_app->stateSaved = 1;
  136. pthread_cond_broadcast(&android_app->cond);
  137. pthread_mutex_unlock(&android_app->mutex);
  138. break;
  139. case APP_CMD_RESUME:
  140. free_saved_state(android_app);
  141. break;
  142. }
  143. }
  144. void app_dummy() {
  145. }
  146. static void android_app_destroy(struct android_app* android_app) {
  147. LOGI("android_app_destroy!");
  148. free_saved_state(android_app);
  149. pthread_mutex_lock(&android_app->mutex);
  150. if (android_app->inputQueue != NULL) {
  151. AInputQueue_detachLooper(android_app->inputQueue);
  152. }
  153. AConfiguration_delete(android_app->config);
  154. android_app->destroyed = 1;
  155. pthread_cond_broadcast(&android_app->cond);
  156. pthread_mutex_unlock(&android_app->mutex);
  157. // Can't touch android_app object after this.
  158. }
  159. static void process_input(struct android_app* app, struct android_poll_source* source) {
  160. AInputEvent* event = NULL;
  161. if (AInputQueue_getEvent(app->inputQueue, &event) >= 0) {
  162. LOGI("New input event: type=%d\n", AInputEvent_getType(event));
  163. if (AInputQueue_preDispatchEvent(app->inputQueue, event)) {
  164. return;
  165. }
  166. int32_t handled = 0;
  167. if (app->onInputEvent != NULL) handled = app->onInputEvent(app, event);
  168. AInputQueue_finishEvent(app->inputQueue, event, handled);
  169. } else {
  170. LOGI("Failure reading next input event: %s\n", strerror(errno));
  171. }
  172. }
  173. static void process_cmd(struct android_app* app, struct android_poll_source* source) {
  174. int8_t cmd = android_app_read_cmd(app);
  175. android_app_pre_exec_cmd(app, cmd);
  176. if (app->onAppCmd != NULL) app->onAppCmd(app, cmd);
  177. android_app_post_exec_cmd(app, cmd);
  178. }
  179. static void* android_app_entry(void* param) {
  180. struct android_app* android_app = (struct android_app*)param;
  181. android_app->config = AConfiguration_new();
  182. AConfiguration_fromAssetManager(android_app->config, android_app->activity->assetManager);
  183. print_cur_config(android_app);
  184. android_app->cmdPollSource.id = LOOPER_ID_MAIN;
  185. android_app->cmdPollSource.app = android_app;
  186. android_app->cmdPollSource.process = process_cmd;
  187. android_app->inputPollSource.id = LOOPER_ID_INPUT;
  188. android_app->inputPollSource.app = android_app;
  189. android_app->inputPollSource.process = process_input;
  190. ALooper* looper = ALooper_prepare(ALOOPER_PREPARE_ALLOW_NON_CALLBACKS);
  191. ALooper_addFd(looper, android_app->msgread, LOOPER_ID_MAIN, ALOOPER_EVENT_INPUT, NULL,
  192. &android_app->cmdPollSource);
  193. android_app->looper = looper;
  194. pthread_mutex_lock(&android_app->mutex);
  195. android_app->running = 1;
  196. pthread_cond_broadcast(&android_app->cond);
  197. pthread_mutex_unlock(&android_app->mutex);
  198. android_main(android_app);
  199. android_app_destroy(android_app);
  200. return NULL;
  201. }
  202. // --------------------------------------------------------------------
  203. // Native activity interaction (called from main thread)
  204. // --------------------------------------------------------------------
  205. static struct android_app* android_app_create(ANativeActivity* activity,
  206. void* savedState, size_t savedStateSize) {
  207. struct android_app* android_app = (struct android_app*)malloc(sizeof(struct android_app));
  208. memset(android_app, 0, sizeof(struct android_app));
  209. android_app->activity = activity;
  210. pthread_mutex_init(&android_app->mutex, NULL);
  211. pthread_cond_init(&android_app->cond, NULL);
  212. if (savedState != NULL) {
  213. android_app->savedState = malloc(savedStateSize);
  214. android_app->savedStateSize = savedStateSize;
  215. memcpy(android_app->savedState, savedState, savedStateSize);
  216. }
  217. int msgpipe[2];
  218. if (pipe(msgpipe)) {
  219. LOGI("could not create pipe: %s", strerror(errno));
  220. }
  221. android_app->msgread = msgpipe[0];
  222. android_app->msgwrite = msgpipe[1];
  223. pthread_attr_t attr;
  224. pthread_attr_init(&attr);
  225. pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
  226. pthread_create(&android_app->thread, &attr, android_app_entry, android_app);
  227. // Wait for thread to start.
  228. pthread_mutex_lock(&android_app->mutex);
  229. while (!android_app->running) {
  230. pthread_cond_wait(&android_app->cond, &android_app->mutex);
  231. }
  232. pthread_mutex_unlock(&android_app->mutex);
  233. return android_app;
  234. }
  235. static void android_app_write_cmd(struct android_app* android_app, int8_t cmd) {
  236. if (write(android_app->msgwrite, &cmd, sizeof(cmd)) != sizeof(cmd)) {
  237. LOGI("Failure writing android_app cmd: %s\n", strerror(errno));
  238. }
  239. }
  240. static void android_app_set_input(struct android_app* android_app, AInputQueue* inputQueue) {
  241. pthread_mutex_lock(&android_app->mutex);
  242. android_app->pendingInputQueue = inputQueue;
  243. android_app_write_cmd(android_app, APP_CMD_INPUT_CHANGED);
  244. while (android_app->inputQueue != android_app->pendingInputQueue) {
  245. pthread_cond_wait(&android_app->cond, &android_app->mutex);
  246. }
  247. pthread_mutex_unlock(&android_app->mutex);
  248. }
  249. static void android_app_set_window(struct android_app* android_app, ANativeWindow* window) {
  250. pthread_mutex_lock(&android_app->mutex);
  251. if (android_app->pendingWindow != NULL) {
  252. android_app_write_cmd(android_app, APP_CMD_TERM_WINDOW);
  253. }
  254. android_app->pendingWindow = window;
  255. if (window != NULL) {
  256. android_app_write_cmd(android_app, APP_CMD_INIT_WINDOW);
  257. }
  258. while (android_app->window != android_app->pendingWindow) {
  259. pthread_cond_wait(&android_app->cond, &android_app->mutex);
  260. }
  261. pthread_mutex_unlock(&android_app->mutex);
  262. }
  263. static void android_app_set_activity_state(struct android_app* android_app, int8_t cmd) {
  264. pthread_mutex_lock(&android_app->mutex);
  265. android_app_write_cmd(android_app, cmd);
  266. while (android_app->activityState != cmd) {
  267. pthread_cond_wait(&android_app->cond, &android_app->mutex);
  268. }
  269. pthread_mutex_unlock(&android_app->mutex);
  270. }
  271. static void android_app_free(struct android_app* android_app) {
  272. pthread_mutex_lock(&android_app->mutex);
  273. android_app_write_cmd(android_app, APP_CMD_DESTROY);
  274. while (!android_app->destroyed) {
  275. pthread_cond_wait(&android_app->cond, &android_app->mutex);
  276. }
  277. pthread_mutex_unlock(&android_app->mutex);
  278. close(android_app->msgread);
  279. close(android_app->msgwrite);
  280. pthread_cond_destroy(&android_app->cond);
  281. pthread_mutex_destroy(&android_app->mutex);
  282. free(android_app);
  283. }
  284. static void onDestroy(ANativeActivity* activity) {
  285. LOGI("Destroy: %p\n", activity);
  286. android_app_free((struct android_app*)activity->instance);
  287. }
  288. static void onStart(ANativeActivity* activity) {
  289. LOGI("Start: %p\n", activity);
  290. android_app_set_activity_state((struct android_app*)activity->instance, APP_CMD_START);
  291. }
  292. static void onResume(ANativeActivity* activity) {
  293. LOGI("Resume: %p\n", activity);
  294. android_app_set_activity_state((struct android_app*)activity->instance, APP_CMD_RESUME);
  295. }
  296. static void* onSaveInstanceState(ANativeActivity* activity, size_t* outLen) {
  297. struct android_app* android_app = (struct android_app*)activity->instance;
  298. void* savedState = NULL;
  299. LOGI("SaveInstanceState: %p\n", activity);
  300. pthread_mutex_lock(&android_app->mutex);
  301. android_app->stateSaved = 0;
  302. android_app_write_cmd(android_app, APP_CMD_SAVE_STATE);
  303. while (!android_app->stateSaved) {
  304. pthread_cond_wait(&android_app->cond, &android_app->mutex);
  305. }
  306. if (android_app->savedState != NULL) {
  307. savedState = android_app->savedState;
  308. *outLen = android_app->savedStateSize;
  309. android_app->savedState = NULL;
  310. android_app->savedStateSize = 0;
  311. }
  312. pthread_mutex_unlock(&android_app->mutex);
  313. return savedState;
  314. }
  315. static void onPause(ANativeActivity* activity) {
  316. LOGI("Pause: %p\n", activity);
  317. android_app_set_activity_state((struct android_app*)activity->instance, APP_CMD_PAUSE);
  318. }
  319. static void onStop(ANativeActivity* activity) {
  320. LOGI("Stop: %p\n", activity);
  321. android_app_set_activity_state((struct android_app*)activity->instance, APP_CMD_STOP);
  322. }
  323. static void onConfigurationChanged(ANativeActivity* activity) {
  324. struct android_app* android_app = (struct android_app*)activity->instance;
  325. LOGI("ConfigurationChanged: %p\n", activity);
  326. android_app_write_cmd(android_app, APP_CMD_CONFIG_CHANGED);
  327. }
  328. static void onLowMemory(ANativeActivity* activity) {
  329. struct android_app* android_app = (struct android_app*)activity->instance;
  330. LOGI("LowMemory: %p\n", activity);
  331. android_app_write_cmd(android_app, APP_CMD_LOW_MEMORY);
  332. }
  333. static void onWindowFocusChanged(ANativeActivity* activity, int focused) {
  334. LOGI("WindowFocusChanged: %p -- %d\n", activity, focused);
  335. android_app_write_cmd((struct android_app*)activity->instance,
  336. focused ? APP_CMD_GAINED_FOCUS : APP_CMD_LOST_FOCUS);
  337. }
  338. static void onNativeWindowCreated(ANativeActivity* activity, ANativeWindow* window) {
  339. LOGI("NativeWindowCreated: %p -- %p\n", activity, window);
  340. android_app_set_window((struct android_app*)activity->instance, window);
  341. }
  342. static void onNativeWindowDestroyed(ANativeActivity* activity, ANativeWindow* window) {
  343. LOGI("NativeWindowDestroyed: %p -- %p\n", activity, window);
  344. android_app_set_window((struct android_app*)activity->instance, NULL);
  345. }
  346. static void onInputQueueCreated(ANativeActivity* activity, AInputQueue* queue) {
  347. LOGI("InputQueueCreated: %p -- %p\n", activity, queue);
  348. android_app_set_input((struct android_app*)activity->instance, queue);
  349. }
  350. static void onInputQueueDestroyed(ANativeActivity* activity, AInputQueue* queue) {
  351. LOGI("InputQueueDestroyed: %p -- %p\n", activity, queue);
  352. android_app_set_input((struct android_app*)activity->instance, NULL);
  353. }
  354. void ANativeActivity_onCreate(ANativeActivity* activity,
  355. void* savedState, size_t savedStateSize) {
  356. LOGI("Creating: %p\n", activity);
  357. activity->callbacks->onDestroy = onDestroy;
  358. activity->callbacks->onStart = onStart;
  359. activity->callbacks->onResume = onResume;
  360. activity->callbacks->onSaveInstanceState = onSaveInstanceState;
  361. activity->callbacks->onPause = onPause;
  362. activity->callbacks->onStop = onStop;
  363. activity->callbacks->onConfigurationChanged = onConfigurationChanged;
  364. activity->callbacks->onLowMemory = onLowMemory;
  365. activity->callbacks->onWindowFocusChanged = onWindowFocusChanged;
  366. activity->callbacks->onNativeWindowCreated = onNativeWindowCreated;
  367. activity->callbacks->onNativeWindowDestroyed = onNativeWindowDestroyed;
  368. activity->callbacks->onInputQueueCreated = onInputQueueCreated;
  369. activity->callbacks->onInputQueueDestroyed = onInputQueueDestroyed;
  370. activity->instance = android_app_create(activity, savedState, savedStateSize);
  371. }
  372. #endif