android_native_app_glue.c 17 KB

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