android_native_app_glue.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /*************************************************************************/
  2. /* android_native_app_glue.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2015 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. /* Copyright (C) 2010 The Android Open Source Project
  30. *
  31. * Licensed under the Apache License, Version 2.0 (the "License");
  32. * you may not use this file except in compliance with the License.
  33. * You may obtain a copy of the License at
  34. *
  35. * http://www.apache.org/licenses/LICENSE-2.0
  36. *
  37. * Unless required by applicable law or agreed to in writing, software
  38. * distributed under the License is distributed on an "AS IS" BASIS,
  39. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  40. * See the License for the specific language governing permissions and
  41. * limitations under the License.
  42. *
  43. */
  44. #ifndef _ANDROID_NATIVE_APP_GLUE_H
  45. #define _ANDROID_NATIVE_APP_GLUE_H
  46. #ifdef ANDROID_NATIVE_ACTIVITY
  47. #include <poll.h>
  48. #include <pthread.h>
  49. #include <sched.h>
  50. #include <android/configuration.h>
  51. #include <android/looper.h>
  52. #include <android/native_activity.h>
  53. #ifdef __cplusplus
  54. extern "C" {
  55. #endif
  56. /**
  57. * The native activity interface provided by <android/native_activity.h>
  58. * is based on a set of application-provided callbacks that will be called
  59. * by the Activity's main thread when certain events occur.
  60. *
  61. * This means that each one of this callbacks _should_ _not_ block, or they
  62. * risk having the system force-close the application. This programming
  63. * model is direct, lightweight, but constraining.
  64. *
  65. * The 'threaded_native_app' static library is used to provide a different
  66. * execution model where the application can implement its own main event
  67. * loop in a different thread instead. Here's how it works:
  68. *
  69. * 1/ The application must provide a function named "android_main()" that
  70. * will be called when the activity is created, in a new thread that is
  71. * distinct from the activity's main thread.
  72. *
  73. * 2/ android_main() receives a pointer to a valid "android_app" structure
  74. * that contains references to other important objects, e.g. the
  75. * ANativeActivity obejct instance the application is running in.
  76. *
  77. * 3/ the "android_app" object holds an ALooper instance that already
  78. * listens to two important things:
  79. *
  80. * - activity lifecycle events (e.g. "pause", "resume"). See APP_CMD_XXX
  81. * declarations below.
  82. *
  83. * - input events coming from the AInputQueue attached to the activity.
  84. *
  85. * Each of these correspond to an ALooper identifier returned by
  86. * ALooper_pollOnce with values of LOOPER_ID_MAIN and LOOPER_ID_INPUT,
  87. * respectively.
  88. *
  89. * Your application can use the same ALooper to listen to additional
  90. * file-descriptors. They can either be callback based, or with return
  91. * identifiers starting with LOOPER_ID_USER.
  92. *
  93. * 4/ Whenever you receive a LOOPER_ID_MAIN or LOOPER_ID_INPUT event,
  94. * the returned data will point to an android_poll_source structure. You
  95. * can call the process() function on it, and fill in android_app->onAppCmd
  96. * and android_app->onInputEvent to be called for your own processing
  97. * of the event.
  98. *
  99. * Alternatively, you can call the low-level functions to read and process
  100. * the data directly... look at the process_cmd() and process_input()
  101. * implementations in the glue to see how to do this.
  102. *
  103. * See the sample named "native-activity" that comes with the NDK with a
  104. * full usage example. Also look at the JavaDoc of NativeActivity.
  105. */
  106. struct android_app;
  107. /**
  108. * Data associated with an ALooper fd that will be returned as the "outData"
  109. * when that source has data ready.
  110. */
  111. struct android_poll_source {
  112. // The identifier of this source. May be LOOPER_ID_MAIN or
  113. // LOOPER_ID_INPUT.
  114. int32_t id;
  115. // The android_app this ident is associated with.
  116. struct android_app* app;
  117. // Function to call to perform the standard processing of data from
  118. // this source.
  119. void (*process)(struct android_app* app, struct android_poll_source* source);
  120. };
  121. /**
  122. * This is the interface for the standard glue code of a threaded
  123. * application. In this model, the application's code is running
  124. * in its own thread separate from the main thread of the process.
  125. * It is not required that this thread be associated with the Java
  126. * VM, although it will need to be in order to make JNI calls any
  127. * Java objects.
  128. */
  129. struct android_app {
  130. // The application can place a pointer to its own state object
  131. // here if it likes.
  132. void* userData;
  133. // Fill this in with the function to process main app commands (APP_CMD_*)
  134. void (*onAppCmd)(struct android_app* app, int32_t cmd);
  135. // Fill this in with the function to process input events. At this point
  136. // the event has already been pre-dispatched, and it will be finished upon
  137. // return. Return 1 if you have handled the event, 0 for any default
  138. // dispatching.
  139. int32_t (*onInputEvent)(struct android_app* app, AInputEvent* event);
  140. // The ANativeActivity object instance that this app is running in.
  141. ANativeActivity* activity;
  142. // The current configuration the app is running in.
  143. AConfiguration* config;
  144. // This is the last instance's saved state, as provided at creation time.
  145. // It is NULL if there was no state. You can use this as you need; the
  146. // memory will remain around until you call android_app_exec_cmd() for
  147. // APP_CMD_RESUME, at which point it will be freed and savedState set to NULL.
  148. // These variables should only be changed when processing a APP_CMD_SAVE_STATE,
  149. // at which point they will be initialized to NULL and you can malloc your
  150. // state and place the information here. In that case the memory will be
  151. // freed for you later.
  152. void* savedState;
  153. size_t savedStateSize;
  154. // The ALooper associated with the app's thread.
  155. ALooper* looper;
  156. // When non-NULL, this is the input queue from which the app will
  157. // receive user input events.
  158. AInputQueue* inputQueue;
  159. // When non-NULL, this is the window surface that the app can draw in.
  160. ANativeWindow* window;
  161. // Current content rectangle of the window; this is the area where the
  162. // window's content should be placed to be seen by the user.
  163. ARect contentRect;
  164. // Current state of the app's activity. May be either APP_CMD_START,
  165. // APP_CMD_RESUME, APP_CMD_PAUSE, or APP_CMD_STOP; see below.
  166. int activityState;
  167. // This is non-zero when the application's NativeActivity is being
  168. // destroyed and waiting for the app thread to complete.
  169. int destroyRequested;
  170. // -------------------------------------------------
  171. // Below are "private" implementation of the glue code.
  172. pthread_mutex_t mutex;
  173. pthread_cond_t cond;
  174. int msgread;
  175. int msgwrite;
  176. pthread_t thread;
  177. struct android_poll_source cmdPollSource;
  178. struct android_poll_source inputPollSource;
  179. int running;
  180. int stateSaved;
  181. int destroyed;
  182. int redrawNeeded;
  183. AInputQueue* pendingInputQueue;
  184. ANativeWindow* pendingWindow;
  185. ARect pendingContentRect;
  186. };
  187. enum {
  188. /**
  189. * Looper data ID of commands coming from the app's main thread, which
  190. * is returned as an identifier from ALooper_pollOnce(). The data for this
  191. * identifier is a pointer to an android_poll_source structure.
  192. * These can be retrieved and processed with android_app_read_cmd()
  193. * and android_app_exec_cmd().
  194. */
  195. LOOPER_ID_MAIN = 1,
  196. /**
  197. * Looper data ID of events coming from the AInputQueue of the
  198. * application's window, which is returned as an identifier from
  199. * ALooper_pollOnce(). The data for this identifier is a pointer to an
  200. * android_poll_source structure. These can be read via the inputQueue
  201. * object of android_app.
  202. */
  203. LOOPER_ID_INPUT = 2,
  204. /**
  205. * Start of user-defined ALooper identifiers.
  206. */
  207. LOOPER_ID_USER = 3,
  208. };
  209. enum {
  210. /**
  211. * Command from main thread: the AInputQueue has changed. Upon processing
  212. * this command, android_app->inputQueue will be updated to the new queue
  213. * (or NULL).
  214. */
  215. APP_CMD_INPUT_CHANGED,
  216. /**
  217. * Command from main thread: a new ANativeWindow is ready for use. Upon
  218. * receiving this command, android_app->window will contain the new window
  219. * surface.
  220. */
  221. APP_CMD_INIT_WINDOW,
  222. /**
  223. * Command from main thread: the existing ANativeWindow needs to be
  224. * terminated. Upon receiving this command, android_app->window still
  225. * contains the existing window; after calling android_app_exec_cmd
  226. * it will be set to NULL.
  227. */
  228. APP_CMD_TERM_WINDOW,
  229. /**
  230. * Command from main thread: the current ANativeWindow has been resized.
  231. * Please redraw with its new size.
  232. */
  233. APP_CMD_WINDOW_RESIZED,
  234. /**
  235. * Command from main thread: the system needs that the current ANativeWindow
  236. * be redrawn. You should redraw the window before handing this to
  237. * android_app_exec_cmd() in order to avoid transient drawing glitches.
  238. */
  239. APP_CMD_WINDOW_REDRAW_NEEDED,
  240. /**
  241. * Command from main thread: the content area of the window has changed,
  242. * such as from the soft input window being shown or hidden. You can
  243. * find the new content rect in android_app::contentRect.
  244. */
  245. APP_CMD_CONTENT_RECT_CHANGED,
  246. /**
  247. * Command from main thread: the app's activity window has gained
  248. * input focus.
  249. */
  250. APP_CMD_GAINED_FOCUS,
  251. /**
  252. * Command from main thread: the app's activity window has lost
  253. * input focus.
  254. */
  255. APP_CMD_LOST_FOCUS,
  256. /**
  257. * Command from main thread: the current device configuration has changed.
  258. */
  259. APP_CMD_CONFIG_CHANGED,
  260. /**
  261. * Command from main thread: the system is running low on memory.
  262. * Try to reduce your memory use.
  263. */
  264. APP_CMD_LOW_MEMORY,
  265. /**
  266. * Command from main thread: the app's activity has been started.
  267. */
  268. APP_CMD_START,
  269. /**
  270. * Command from main thread: the app's activity has been resumed.
  271. */
  272. APP_CMD_RESUME,
  273. /**
  274. * Command from main thread: the app should generate a new saved state
  275. * for itself, to restore from later if needed. If you have saved state,
  276. * allocate it with malloc and place it in android_app.savedState with
  277. * the size in android_app.savedStateSize. The will be freed for you
  278. * later.
  279. */
  280. APP_CMD_SAVE_STATE,
  281. /**
  282. * Command from main thread: the app's activity has been paused.
  283. */
  284. APP_CMD_PAUSE,
  285. /**
  286. * Command from main thread: the app's activity has been stopped.
  287. */
  288. APP_CMD_STOP,
  289. /**
  290. * Command from main thread: the app's activity is being destroyed,
  291. * and waiting for the app thread to clean up and exit before proceeding.
  292. */
  293. APP_CMD_DESTROY,
  294. };
  295. /**
  296. * Call when ALooper_pollAll() returns LOOPER_ID_MAIN, reading the next
  297. * app command message.
  298. */
  299. int8_t android_app_read_cmd(struct android_app* android_app);
  300. /**
  301. * Call with the command returned by android_app_read_cmd() to do the
  302. * initial pre-processing of the given command. You can perform your own
  303. * actions for the command after calling this function.
  304. */
  305. void android_app_pre_exec_cmd(struct android_app* android_app, int8_t cmd);
  306. /**
  307. * Call with the command returned by android_app_read_cmd() to do the
  308. * final post-processing of the given command. You must have done your own
  309. * actions for the command before calling this function.
  310. */
  311. void android_app_post_exec_cmd(struct android_app* android_app, int8_t cmd);
  312. /**
  313. * Dummy function you can call to ensure glue code isn't stripped.
  314. */
  315. void app_dummy();
  316. /**
  317. * This is the function that application code must implement, representing
  318. * the main entry to the app.
  319. */
  320. extern void android_main(struct android_app* app);
  321. #ifdef __cplusplus
  322. }
  323. #endif
  324. #endif /* _ANDROID_NATIVE_APP_GLUE_H */
  325. #endif