*/}}

android_native_app_glue.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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. #include "android_native_app_glue.h"
  17. #include <jni.h>
  18. #include <errno.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <unistd.h>
  22. #include <android/log.h>
  23. #define LOGI(...) ((void)__android_log_print(ANDROID_LOG_INFO, "threaded_app", __VA_ARGS__))
  24. #define LOGE(...) ((void)__android_log_print(ANDROID_LOG_ERROR, "threaded_app", __VA_ARGS__))
  25. /* For debug builds, always enable the debug traces in this library */
  26. #ifndef NDEBUG
  27. # define LOGV(...) ((void)__android_log_print(ANDROID_LOG_VERBOSE, "threaded_app", __VA_ARGS__))
  28. #else
  29. # define LOGV(...) ((void)0)
  30. #endif
  31. static void free_saved_state(struct android_app* android_app) {
  32. pthread_mutex_lock(&android_app->mutex);
  33. if (android_app->savedState != NULL) {
  34. free(android_app->savedState);
  35. android_app->savedState = NULL;
  36. android_app->savedStateSize = 0;
  37. }
  38. pthread_mutex_unlock(&android_app->mutex);
  39. }
  40. int8_t android_app_read_cmd(struct android_app* android_app) {
  41. int8_t cmd;
  42. if (read(android_app->msgread, &cmd, sizeof(cmd)) != sizeof(cmd)) {
  43. LOGE("No data on command pipe!");
  44. return -1;
  45. }
  46. if (cmd == APP_CMD_SAVE_STATE) free_saved_state(android_app);
  47. return cmd;
  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. LOGV("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. LOGV("APP_CMD_INPUT_CHANGED");
  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. LOGV("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. LOGV("APP_CMD_INIT_WINDOW");
  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. LOGV("APP_CMD_TERM_WINDOW");
  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. LOGV("activityState=%d", 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. LOGV("APP_CMD_CONFIG_CHANGED");
  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. LOGV("APP_CMD_DESTROY");
  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. LOGV("APP_CMD_TERM_WINDOW");
  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. LOGV("APP_CMD_SAVE_STATE");
  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. LOGV("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. while (AInputQueue_getEvent(app->inputQueue, &event) >= 0) {
  162. LOGV("New input event: type=%d", AInputEvent_getType(event));
  163. if (AInputQueue_preDispatchEvent(app->inputQueue, event)) {
  164. continue;
  165. }
  166. int32_t handled = 0;
  167. if (app->onInputEvent != NULL) handled = app->onInputEvent(app, event);
  168. AInputQueue_finishEvent(app->inputQueue, event, handled);
  169. }
  170. }
  171. static void process_cmd(struct android_app* app, struct android_poll_source* source) {
  172. int8_t cmd = android_app_read_cmd(app);
  173. android_app_pre_exec_cmd(app, cmd);
  174. if (app->onAppCmd != NULL) app->onAppCmd(app, cmd);
  175. android_app_post_exec_cmd(app, cmd);
  176. }
  177. static void* android_app_entry(void* param) {
  178. struct android_app* android_app = (struct android_app*)param;
  179. android_app->config = AConfiguration_new();
  180. AConfiguration_fromAssetManager(android_app->config, android_app->activity->assetManager);
  181. print_cur_config(android_app);
  182. android_app->cmdPollSource.id = LOOPER_ID_MAIN;
  183. android_app->cmdPollSource.app = android_app;
  184. android_app->cmdPollSource.process = process_cmd;
  185. android_app->inputPollSource.id = LOOPER_ID_INPUT;
  186. android_app->inputPollSource.app = android_app;
  187. android_app->inputPollSource.process = process_input;
  188. ALooper* looper = ALooper_prepare(ALOOPER_PREPARE_ALLOW_NON_CALLBACKS);
  189. ALooper_addFd(looper, android_app->msgread, LOOPER_ID_MAIN, ALOOPER_EVENT_INPUT, NULL,
  190. &android_app->cmdPollSource);
  191. android_app->looper = looper;
  192. pthread_mutex_lock(&android_app->mutex);
  193. android_app->running = 1;
  194. pthread_cond_broadcast(&android_app->cond);
  195. pthread_mutex_unlock(&android_app->mutex);
  196. android_main(android_app);
  197. android_app_destroy(android_app);
  198. return NULL;
  199. }
  200. // --------------------------------------------------------------------
  201. // Native activity interaction (called from main thread)
  202. // --------------------------------------------------------------------
  203. static struct android_app* android_app_create(ANativeActivity* activity,
  204. void* savedState, size_t savedStateSize) {
  205. struct android_app* android_app = calloc(1, sizeof(struct android_app));
  206. android_app->activity = activity;
  207. pthread_mutex_init(&android_app->mutex, NULL);
  208. pthread_cond_init(&android_app->cond, NULL);
  209. if (savedState != NULL) {
  210. android_app->savedState = malloc(savedStateSize);
  211. android_app->savedStateSize = savedStateSize;
  212. memcpy(android_app->savedState, savedState, savedStateSize);
  213. }
  214. int msgpipe[2];
  215. if (pipe(msgpipe)) {
  216. LOGE("could not create pipe: %s", strerror(errno));
  217. return NULL;
  218. }
  219. android_app->msgread = msgpipe[0];
  220. android_app->msgwrite = msgpipe[1];
  221. pthread_attr_t attr;
  222. pthread_attr_init(&attr);
  223. pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
  224. pthread_create(&android_app->thread, &attr, android_app_entry, android_app);
  225. // Wait for thread to start.
  226. pthread_mutex_lock(&android_app->mutex);
  227. while (!android_app->running) {
  228. pthread_cond_wait(&android_app->cond, &android_app->mutex);
  229. }
  230. pthread_mutex_unlock(&android_app->mutex);
  231. return android_app;
  232. }
  233. static void android_app_write_cmd(struct android_app* android_app, int8_t cmd) {
  234. if (write(android_app->msgwrite, &cmd, sizeof(cmd)) != sizeof(cmd)) {
  235. LOGE("Failure writing android_app cmd: %s", strerror(errno));
  236. }
  237. }
  238. static void android_app_set_input(struct android_app* android_app, AInputQueue* inputQueue) {
  239. pthread_mutex_lock(&android_app->mutex);
  240. android_app->pendingInputQueue = inputQueue;
  241. android_app_write_cmd(android_app, APP_CMD_INPUT_CHANGED);
  242. while (android_app->inputQueue != android_app->pendingInputQueue) {
  243. pthread_cond_wait(&android_app->cond, &android_app->mutex);
  244. }
  245. pthread_mutex_unlock(&android_app->mutex);
  246. }
  247. static void android_app_set_window(struct android_app* android_app, ANativeWindow* window) {
  248. pthread_mutex_lock(&android_app->mutex);
  249. if (android_app->pendingWindow != NULL) {
  250. android_app_write_cmd(android_app, APP_CMD_TERM_WINDOW);
  251. }
  252. android_app->pendingWindow = window;
  253. if (window != NULL) {
  254. android_app_write_cmd(android_app, APP_CMD_INIT_WINDOW);
  255. }
  256. while (android_app->window != android_app->pendingWindow) {
  257. pthread_cond_wait(&android_app->cond, &android_app->mutex);
  258. }
  259. pthread_mutex_unlock(&android_app->mutex);
  260. }
  261. static void android_app_set_activity_state(struct android_app* android_app, int8_t cmd) {
  262. pthread_mutex_lock(&android_app->mutex);
  263. android_app_write_cmd(android_app, cmd);
  264. while (android_app->activityState != cmd) {
  265. pthread_cond_wait(&android_app->cond, &android_app->mutex);
  266. }
  267. pthread_mutex_unlock(&android_app->mutex);
  268. }
  269. static void android_app_free(struct android_app* android_app) {
  270. pthread_mutex_lock(&android_app->mutex);
  271. android_app_write_cmd(android_app, APP_CMD_DESTROY);
  272. while (!android_app->destroyed) {
  273. pthread_cond_wait(&android_app->cond, &android_app->mutex);
  274. }
  275. pthread_mutex_unlock(&android_app->mutex);
  276. close(android_app->msgread);
  277. close(android_app->msgwrite);
  278. pthread_cond_destroy(&android_app->cond);
  279. pthread_mutex_destroy(&android_app->mutex);
  280. free(android_app);
  281. }
  282. static struct android_app* ToApp(ANativeActivity* activity) {
  283. return (struct android_app*) activity->instance;
  284. }
  285. static void onDestroy(ANativeActivity* activity) {
  286. LOGV("Destroy: %p", activity);
  287. android_app_free(ToApp(activity));
  288. }
  289. static void onStart(ANativeActivity* activity) {
  290. LOGV("Start: %p", activity);
  291. android_app_set_activity_state(ToApp(activity), APP_CMD_START);
  292. }
  293. static void onResume(ANativeActivity* activity) {
  294. LOGV("Resume: %p", activity);
  295. android_app_set_activity_state(ToApp(activity), APP_CMD_RESUME);
  296. }
  297. static void* onSaveInstanceState(ANativeActivity* activity, size_t* outLen) {
  298. LOGV("SaveInstanceState: %p", activity);
  299. struct android_app* android_app = ToApp(activity);
  300. void* savedState = NULL;
  301. pthread_mutex_lock(&android_app->mutex);
  302. android_app->stateSaved = 0;
  303. android_app_write_cmd(android_app, APP_CMD_SAVE_STATE);
  304. while (!android_app->stateSaved) {
  305. pthread_cond_wait(&android_app->cond, &android_app->mutex);
  306. }
  307. if (android_app->savedState != NULL) {
  308. savedState = android_app->savedState;
  309. *outLen = android_app->savedStateSize;
  310. android_app->savedState = NULL;
  311. android_app->savedStateSize = 0;
  312. }
  313. pthread_mutex_unlock(&android_app->mutex);
  314. return savedState;
  315. }
  316. static void onPause(ANativeActivity* activity) {
  317. LOGV("Pause: %p", activity);
  318. android_app_set_activity_state(ToApp(activity), APP_CMD_PAUSE);
  319. }
  320. static void onStop(ANativeActivity* activity) {
  321. LOGV("Stop: %p", activity);
  322. android_app_set_activity_state(ToApp(activity), APP_CMD_STOP);
  323. }
  324. static void onConfigurationChanged(ANativeActivity* activity) {
  325. LOGV("ConfigurationChanged: %p", activity);
  326. android_app_write_cmd(ToApp(activity), APP_CMD_CONFIG_CHANGED);
  327. }
  328. static void onContentRectChanged(ANativeActivity* activity, const ARect* r) {
  329. LOGV("ContentRectChanged: l=%d,t=%d,r=%d,b=%d", r->left, r->top, r->right, r->bottom);
  330. struct android_app* android_app = ToApp(activity);
  331. pthread_mutex_lock(&android_app->mutex);
  332. android_app->contentRect = *r;
  333. pthread_mutex_unlock(&android_app->mutex);
  334. android_app_write_cmd(ToApp(activity), APP_CMD_CONTENT_RECT_CHANGED);
  335. }
  336. static void onLowMemory(ANativeActivity* activity) {
  337. LOGV("LowMemory: %p", activity);
  338. android_app_write_cmd(ToApp(activity), APP_CMD_LOW_MEMORY);
  339. }
  340. static void onWindowFocusChanged(ANativeActivity* activity, int focused) {
  341. LOGV("WindowFocusChanged: %p -- %d", activity, focused);
  342. android_app_write_cmd(ToApp(activity), focused ? APP_CMD_GAINED_FOCUS : APP_CMD_LOST_FOCUS);
  343. }
  344. static void onNativeWindowCreated(ANativeActivity* activity, ANativeWindow* window) {
  345. LOGV("NativeWindowCreated: %p -- %p", activity, window);
  346. android_app_set_window(ToApp(activity), window);
  347. }
  348. static void onNativeWindowDestroyed(ANativeActivity* activity, ANativeWindow* window) {
  349. LOGV("NativeWindowDestroyed: %p -- %p", activity, window);
  350. android_app_set_window(ToApp(activity), NULL);
  351. }
  352. static void onNativeWindowRedrawNeeded(ANativeActivity* activity, ANativeWindow* window) {
  353. LOGV("NativeWindowRedrawNeeded: %p -- %p", activity, window);
  354. android_app_write_cmd(ToApp(activity), APP_CMD_WINDOW_REDRAW_NEEDED);
  355. }
  356. static void onNativeWindowResized(ANativeActivity* activity, ANativeWindow* window) {
  357. LOGV("NativeWindowResized: %p -- %p", activity, window);
  358. android_app_write_cmd(ToApp(activity), APP_CMD_WINDOW_RESIZED);
  359. }
  360. static void onInputQueueCreated(ANativeActivity* activity, AInputQueue* queue) {
  361. LOGV("InputQueueCreated: %p -- %p", activity, queue);
  362. android_app_set_input(ToApp(activity), queue);
  363. }
  364. static void onInputQueueDestroyed(ANativeActivity* activity, AInputQueue* queue) {
  365. LOGV("InputQueueDestroyed: %p -- %p", activity, queue);
  366. android_app_set_input(ToApp(activity), NULL);
  367. }
  368. JNIEXPORT
  369. void ANativeActivity_onCreate(ANativeActivity* activity, void* savedState, size_t savedStateSize) {
  370. LOGV("Creating: %p", activity);
  371. activity->callbacks->onConfigurationChanged = onConfigurationChanged;
  372. activity->callbacks->onContentRectChanged = onContentRectChanged;
  373. activity->callbacks->onDestroy = onDestroy;
  374. activity->callbacks->onInputQueueCreated = onInputQueueCreated;
  375. activity->callbacks->onInputQueueDestroyed = onInputQueueDestroyed;
  376. activity->callbacks->onLowMemory = onLowMemory;
  377. activity->callbacks->onNativeWindowCreated = onNativeWindowCreated;
  378. activity->callbacks->onNativeWindowDestroyed = onNativeWindowDestroyed;
  379. activity->callbacks->onNativeWindowRedrawNeeded = onNativeWindowRedrawNeeded;
  380. activity->callbacks->onNativeWindowResized = onNativeWindowResized;
  381. activity->callbacks->onPause = onPause;
  382. activity->callbacks->onResume = onResume;
  383. activity->callbacks->onSaveInstanceState = onSaveInstanceState;
  384. activity->callbacks->onStart = onStart;
  385. activity->callbacks->onStop = onStop;
  386. activity->callbacks->onWindowFocusChanged = onWindowFocusChanged;
  387. activity->instance = android_app_create(activity, savedState, savedStateSize);
  388. }