android_native_app_glue.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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. #include <jni.h>
  18. #include <stdlib.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. #define LOGE(...) ((void)__android_log_print(ANDROID_LOG_ERROR, "threaded_app", __VA_ARGS__))
  27. /* For debug builds, always enable the debug traces in this library */
  28. #ifndef NDEBUG
  29. # define LOGV(...) ((void)__android_log_print(ANDROID_LOG_VERBOSE, "threaded_app", __VA_ARGS__))
  30. #else
  31. # define LOGV(...) ((void)0)
  32. #endif
  33. static void free_saved_state(struct android_app* android_app) {
  34. pthread_mutex_lock(&android_app->mutex);
  35. if (android_app->savedState != NULL) {
  36. free(android_app->savedState);
  37. android_app->savedState = NULL;
  38. android_app->savedStateSize = 0;
  39. }
  40. pthread_mutex_unlock(&android_app->mutex);
  41. }
  42. int8_t android_app_read_cmd(struct android_app* android_app) {
  43. int8_t cmd;
  44. if (read(android_app->msgread, &cmd, sizeof(cmd)) == sizeof(cmd)) {
  45. switch (cmd) {
  46. case APP_CMD_SAVE_STATE:
  47. free_saved_state(android_app);
  48. break;
  49. }
  50. return cmd;
  51. } else {
  52. LOGE("No data on command pipe!");
  53. }
  54. return -1;
  55. }
  56. static void print_cur_config(struct android_app* android_app) {
  57. char lang[2], country[2];
  58. AConfiguration_getLanguage(android_app->config, lang);
  59. AConfiguration_getCountry(android_app->config, country);
  60. LOGV("Config: mcc=%d mnc=%d lang=%c%c cnt=%c%c orien=%d touch=%d dens=%d "
  61. "keys=%d nav=%d keysHid=%d navHid=%d sdk=%d size=%d long=%d "
  62. "modetype=%d modenight=%d",
  63. AConfiguration_getMcc(android_app->config),
  64. AConfiguration_getMnc(android_app->config),
  65. lang[0], lang[1], country[0], country[1],
  66. AConfiguration_getOrientation(android_app->config),
  67. AConfiguration_getTouchscreen(android_app->config),
  68. AConfiguration_getDensity(android_app->config),
  69. AConfiguration_getKeyboard(android_app->config),
  70. AConfiguration_getNavigation(android_app->config),
  71. AConfiguration_getKeysHidden(android_app->config),
  72. AConfiguration_getNavHidden(android_app->config),
  73. AConfiguration_getSdkVersion(android_app->config),
  74. AConfiguration_getScreenSize(android_app->config),
  75. AConfiguration_getScreenLong(android_app->config),
  76. AConfiguration_getUiModeType(android_app->config),
  77. AConfiguration_getUiModeNight(android_app->config));
  78. }
  79. void android_app_pre_exec_cmd(struct android_app* android_app, int8_t cmd) {
  80. switch (cmd) {
  81. case APP_CMD_INPUT_CHANGED:
  82. LOGV("APP_CMD_INPUT_CHANGED\n");
  83. pthread_mutex_lock(&android_app->mutex);
  84. if (android_app->inputQueue != NULL) {
  85. AInputQueue_detachLooper(android_app->inputQueue);
  86. }
  87. android_app->inputQueue = android_app->pendingInputQueue;
  88. if (android_app->inputQueue != NULL) {
  89. LOGV("Attaching input queue to looper");
  90. AInputQueue_attachLooper(android_app->inputQueue,
  91. android_app->looper, LOOPER_ID_INPUT, NULL,
  92. &android_app->inputPollSource);
  93. }
  94. pthread_cond_broadcast(&android_app->cond);
  95. pthread_mutex_unlock(&android_app->mutex);
  96. break;
  97. case APP_CMD_INIT_WINDOW:
  98. LOGV("APP_CMD_INIT_WINDOW\n");
  99. pthread_mutex_lock(&android_app->mutex);
  100. android_app->window = android_app->pendingWindow;
  101. pthread_cond_broadcast(&android_app->cond);
  102. pthread_mutex_unlock(&android_app->mutex);
  103. break;
  104. case APP_CMD_TERM_WINDOW:
  105. LOGV("APP_CMD_TERM_WINDOW\n");
  106. pthread_cond_broadcast(&android_app->cond);
  107. break;
  108. case APP_CMD_RESUME:
  109. case APP_CMD_START:
  110. case APP_CMD_PAUSE:
  111. case APP_CMD_STOP:
  112. LOGV("activityState=%d\n", cmd);
  113. pthread_mutex_lock(&android_app->mutex);
  114. android_app->activityState = cmd;
  115. pthread_cond_broadcast(&android_app->cond);
  116. pthread_mutex_unlock(&android_app->mutex);
  117. break;
  118. case APP_CMD_CONFIG_CHANGED:
  119. LOGV("APP_CMD_CONFIG_CHANGED\n");
  120. AConfiguration_fromAssetManager(android_app->config,
  121. android_app->activity->assetManager);
  122. print_cur_config(android_app);
  123. break;
  124. case APP_CMD_DESTROY:
  125. LOGV("APP_CMD_DESTROY\n");
  126. android_app->destroyRequested = 1;
  127. break;
  128. }
  129. }
  130. void android_app_post_exec_cmd(struct android_app* android_app, int8_t cmd) {
  131. switch (cmd) {
  132. case APP_CMD_TERM_WINDOW:
  133. LOGV("APP_CMD_TERM_WINDOW\n");
  134. pthread_mutex_lock(&android_app->mutex);
  135. android_app->window = NULL;
  136. pthread_cond_broadcast(&android_app->cond);
  137. pthread_mutex_unlock(&android_app->mutex);
  138. break;
  139. case APP_CMD_SAVE_STATE:
  140. LOGV("APP_CMD_SAVE_STATE\n");
  141. pthread_mutex_lock(&android_app->mutex);
  142. android_app->stateSaved = 1;
  143. pthread_cond_broadcast(&android_app->cond);
  144. pthread_mutex_unlock(&android_app->mutex);
  145. break;
  146. case APP_CMD_RESUME:
  147. free_saved_state(android_app);
  148. break;
  149. }
  150. }
  151. void app_dummy() {
  152. }
  153. static void android_app_destroy(struct android_app* android_app) {
  154. LOGV("android_app_destroy!");
  155. free_saved_state(android_app);
  156. pthread_mutex_lock(&android_app->mutex);
  157. if (android_app->inputQueue != NULL) {
  158. AInputQueue_detachLooper(android_app->inputQueue);
  159. }
  160. AConfiguration_delete(android_app->config);
  161. android_app->destroyed = 1;
  162. pthread_cond_broadcast(&android_app->cond);
  163. pthread_mutex_unlock(&android_app->mutex);
  164. // Can't touch android_app object after this.
  165. }
  166. static void process_input(struct android_app* app, struct android_poll_source* source) {
  167. AInputEvent* event = NULL;
  168. int processed = 0;
  169. while (AInputQueue_getEvent(app->inputQueue, &event) >= 0) {
  170. LOGV("New input event: type=%d\n", AInputEvent_getType(event));
  171. if (AInputEvent_getType(event) == AINPUT_EVENT_TYPE_KEY) {
  172. int action = AKeyEvent_getAction(event);
  173. int key_val = AKeyEvent_getKeyCode(event);
  174. int metastate = AKeyEvent_getMetaState(event);
  175. if (key_val == AKEYCODE_BACK) {
  176. int32_t handled = 0;
  177. if (app->onInputEvent != NULL) handled = app->onInputEvent(app, event);
  178. AInputQueue_finishEvent(app->inputQueue, event, handled);
  179. processed = 1;
  180. continue;
  181. }
  182. }
  183. if (AInputQueue_preDispatchEvent(app->inputQueue, event)) {
  184. continue;
  185. }
  186. int32_t handled = 0;
  187. if (app->onInputEvent != NULL) handled = app->onInputEvent(app, event);
  188. AInputQueue_finishEvent(app->inputQueue, event, handled);
  189. processed = 1;
  190. }
  191. if (processed == 0) {
  192. LOGE("Failure reading next input event: %s\n", strerror(errno));
  193. }
  194. }
  195. static void process_cmd(struct android_app* app, struct android_poll_source* source) {
  196. int8_t cmd = android_app_read_cmd(app);
  197. android_app_pre_exec_cmd(app, cmd);
  198. if (app->onAppCmd != NULL) app->onAppCmd(app, cmd);
  199. android_app_post_exec_cmd(app, cmd);
  200. }
  201. static void* android_app_entry(void* param) {
  202. struct android_app* android_app = (struct android_app*)param;
  203. android_app->config = AConfiguration_new();
  204. AConfiguration_fromAssetManager(android_app->config, android_app->activity->assetManager);
  205. print_cur_config(android_app);
  206. android_app->cmdPollSource.id = LOOPER_ID_MAIN;
  207. android_app->cmdPollSource.app = android_app;
  208. android_app->cmdPollSource.process = process_cmd;
  209. android_app->inputPollSource.id = LOOPER_ID_INPUT;
  210. android_app->inputPollSource.app = android_app;
  211. android_app->inputPollSource.process = process_input;
  212. ALooper* looper = ALooper_prepare(ALOOPER_PREPARE_ALLOW_NON_CALLBACKS);
  213. ALooper_addFd(looper, android_app->msgread, LOOPER_ID_MAIN, ALOOPER_EVENT_INPUT, NULL,
  214. &android_app->cmdPollSource);
  215. android_app->looper = looper;
  216. pthread_mutex_lock(&android_app->mutex);
  217. android_app->running = 1;
  218. pthread_cond_broadcast(&android_app->cond);
  219. pthread_mutex_unlock(&android_app->mutex);
  220. android_main(android_app);
  221. pthread_mutex_lock(&android_app->mutex);
  222. android_app->running = 0;
  223. pthread_cond_broadcast(&android_app->cond);
  224. pthread_mutex_unlock(&android_app->mutex);
  225. android_app_destroy(android_app);
  226. //Hack to get the app to stil exit after keyboard work around
  227. exit(0);
  228. return NULL;
  229. }
  230. // --------------------------------------------------------------------
  231. // Native activity interaction (called from main thread)
  232. // --------------------------------------------------------------------
  233. static struct android_app* android_app_create(ANativeActivity* activity,
  234. void* savedState, size_t savedStateSize) {
  235. struct android_app* android_app = (struct android_app*)malloc(sizeof(struct android_app));
  236. memset(android_app, 0, sizeof(struct android_app));
  237. android_app->activity = activity;
  238. pthread_mutex_init(&android_app->mutex, NULL);
  239. pthread_cond_init(&android_app->cond, NULL);
  240. if (savedState != NULL) {
  241. android_app->savedState = malloc(savedStateSize);
  242. android_app->savedStateSize = savedStateSize;
  243. memcpy(android_app->savedState, savedState, savedStateSize);
  244. }
  245. int msgpipe[2];
  246. if (pipe(msgpipe)) {
  247. LOGE("could not create pipe: %s", strerror(errno));
  248. return NULL;
  249. }
  250. android_app->msgread = msgpipe[0];
  251. android_app->msgwrite = msgpipe[1];
  252. pthread_attr_t attr;
  253. pthread_attr_init(&attr);
  254. pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
  255. pthread_create(&android_app->thread, &attr, android_app_entry, android_app);
  256. // Wait for thread to start.
  257. pthread_mutex_lock(&android_app->mutex);
  258. while (!android_app->running) {
  259. pthread_cond_wait(&android_app->cond, &android_app->mutex);
  260. }
  261. pthread_mutex_unlock(&android_app->mutex);
  262. return android_app;
  263. }
  264. static void android_app_write_cmd(struct android_app* android_app, int8_t cmd) {
  265. if (write(android_app->msgwrite, &cmd, sizeof(cmd)) != sizeof(cmd)) {
  266. LOGE("Failure writing android_app cmd: %s\n", strerror(errno));
  267. }
  268. }
  269. static void android_app_set_input(struct android_app* android_app, AInputQueue* inputQueue) {
  270. pthread_mutex_lock(&android_app->mutex);
  271. android_app->pendingInputQueue = inputQueue;
  272. android_app_write_cmd(android_app, APP_CMD_INPUT_CHANGED);
  273. while (android_app->inputQueue != android_app->pendingInputQueue) {
  274. pthread_cond_wait(&android_app->cond, &android_app->mutex);
  275. }
  276. pthread_mutex_unlock(&android_app->mutex);
  277. }
  278. static void android_app_set_window(struct android_app* android_app, ANativeWindow* window) {
  279. pthread_mutex_lock(&android_app->mutex);
  280. if (android_app->pendingWindow != NULL) {
  281. android_app_write_cmd(android_app, APP_CMD_TERM_WINDOW);
  282. }
  283. android_app->pendingWindow = window;
  284. if (window != NULL) {
  285. android_app_write_cmd(android_app, APP_CMD_INIT_WINDOW);
  286. }
  287. while (android_app->window != android_app->pendingWindow) {
  288. pthread_cond_wait(&android_app->cond, &android_app->mutex);
  289. }
  290. pthread_mutex_unlock(&android_app->mutex);
  291. }
  292. static void android_app_set_activity_state(struct android_app* android_app, int8_t cmd) {
  293. pthread_mutex_lock(&android_app->mutex);
  294. android_app_write_cmd(android_app, cmd);
  295. while (android_app->activityState != cmd) {
  296. pthread_cond_wait(&android_app->cond, &android_app->mutex);
  297. }
  298. pthread_mutex_unlock(&android_app->mutex);
  299. }
  300. static void android_app_free(struct android_app* android_app) {
  301. pthread_mutex_lock(&android_app->mutex);
  302. android_app_write_cmd(android_app, APP_CMD_DESTROY);
  303. while (!android_app->destroyed) {
  304. pthread_cond_wait(&android_app->cond, &android_app->mutex);
  305. }
  306. pthread_mutex_unlock(&android_app->mutex);
  307. close(android_app->msgread);
  308. close(android_app->msgwrite);
  309. pthread_cond_destroy(&android_app->cond);
  310. pthread_mutex_destroy(&android_app->mutex);
  311. free(android_app);
  312. }
  313. static void onDestroy(ANativeActivity* activity) {
  314. LOGV("Destroy: %p\n", activity);
  315. android_app_free((struct android_app*)activity->instance);
  316. }
  317. static void onStart(ANativeActivity* activity) {
  318. LOGV("Start: %p\n", activity);
  319. android_app_set_activity_state((struct android_app*)activity->instance, APP_CMD_START);
  320. }
  321. static void onResume(ANativeActivity* activity) {
  322. LOGV("Resume: %p\n", activity);
  323. android_app_set_activity_state((struct android_app*)activity->instance, APP_CMD_RESUME);
  324. }
  325. static void* onSaveInstanceState(ANativeActivity* activity, size_t* outLen) {
  326. struct android_app* android_app = (struct android_app*)activity->instance;
  327. void* savedState = NULL;
  328. LOGV("SaveInstanceState: %p\n", activity);
  329. pthread_mutex_lock(&android_app->mutex);
  330. android_app->stateSaved = 0;
  331. android_app_write_cmd(android_app, APP_CMD_SAVE_STATE);
  332. while (!android_app->stateSaved) {
  333. pthread_cond_wait(&android_app->cond, &android_app->mutex);
  334. }
  335. if (android_app->savedState != NULL) {
  336. savedState = android_app->savedState;
  337. *outLen = android_app->savedStateSize;
  338. android_app->savedState = NULL;
  339. android_app->savedStateSize = 0;
  340. }
  341. pthread_mutex_unlock(&android_app->mutex);
  342. return savedState;
  343. }
  344. static void onPause(ANativeActivity* activity) {
  345. LOGV("Pause: %p\n", activity);
  346. android_app_set_activity_state((struct android_app*)activity->instance, APP_CMD_PAUSE);
  347. }
  348. static void onStop(ANativeActivity* activity) {
  349. LOGV("Stop: %p\n", activity);
  350. android_app_set_activity_state((struct android_app*)activity->instance, APP_CMD_STOP);
  351. }
  352. static void onConfigurationChanged(ANativeActivity* activity) {
  353. struct android_app* android_app = (struct android_app*)activity->instance;
  354. LOGV("ConfigurationChanged: %p\n", activity);
  355. android_app_write_cmd(android_app, APP_CMD_CONFIG_CHANGED);
  356. }
  357. static void onLowMemory(ANativeActivity* activity) {
  358. struct android_app* android_app = (struct android_app*)activity->instance;
  359. LOGV("LowMemory: %p\n", activity);
  360. android_app_write_cmd(android_app, APP_CMD_LOW_MEMORY);
  361. }
  362. static void onWindowFocusChanged(ANativeActivity* activity, int focused) {
  363. LOGV("WindowFocusChanged: %p -- %d\n", activity, focused);
  364. android_app_write_cmd((struct android_app*)activity->instance,
  365. focused ? APP_CMD_GAINED_FOCUS : APP_CMD_LOST_FOCUS);
  366. }
  367. static void onNativeWindowCreated(ANativeActivity* activity, ANativeWindow* window) {
  368. LOGV("NativeWindowCreated: %p -- %p\n", activity, window);
  369. android_app_set_window((struct android_app*)activity->instance, window);
  370. }
  371. static void onNativeWindowDestroyed(ANativeActivity* activity, ANativeWindow* window) {
  372. LOGV("NativeWindowDestroyed: %p -- %p\n", activity, window);
  373. android_app_set_window((struct android_app*)activity->instance, NULL);
  374. }
  375. static void onInputQueueCreated(ANativeActivity* activity, AInputQueue* queue) {
  376. LOGV("InputQueueCreated: %p -- %p\n", activity, queue);
  377. android_app_set_input((struct android_app*)activity->instance, queue);
  378. }
  379. static void onInputQueueDestroyed(ANativeActivity* activity, AInputQueue* queue) {
  380. LOGV("InputQueueDestroyed: %p -- %p\n", activity, queue);
  381. android_app_set_input((struct android_app*)activity->instance, NULL);
  382. }
  383. void ANativeActivity_onCreate(ANativeActivity* activity,
  384. void* savedState, size_t savedStateSize) {
  385. LOGV("Creating: %p\n", activity);
  386. activity->callbacks->onDestroy = onDestroy;
  387. activity->callbacks->onStart = onStart;
  388. activity->callbacks->onResume = onResume;
  389. activity->callbacks->onSaveInstanceState = onSaveInstanceState;
  390. activity->callbacks->onPause = onPause;
  391. activity->callbacks->onStop = onStop;
  392. activity->callbacks->onConfigurationChanged = onConfigurationChanged;
  393. activity->callbacks->onLowMemory = onLowMemory;
  394. activity->callbacks->onWindowFocusChanged = onWindowFocusChanged;
  395. activity->callbacks->onNativeWindowCreated = onNativeWindowCreated;
  396. activity->callbacks->onNativeWindowDestroyed = onNativeWindowDestroyed;
  397. activity->callbacks->onInputQueueCreated = onInputQueueCreated;
  398. activity->callbacks->onInputQueueDestroyed = onInputQueueDestroyed;
  399. activity->instance = android_app_create(activity, savedState, savedStateSize);
  400. }