platform_unix.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /* RetroArch - A frontend for libretro.
  2. * Copyright (C) 2010-2014 - Hans-Kristian Arntzen
  3. * Copyright (C) 2011-2017 - Daniel De Matteis
  4. * Copyright (C) 2012-2015 - Michael Lelli
  5. *
  6. * RetroArch is free software: you can redistribute it and/or modify it under the terms
  7. * of the GNU General Public License as published by the Free Software Found-
  8. * ation, either version 3 of the License, or (at your option) any later version.
  9. *
  10. * RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  11. * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  12. * PURPOSE. See the GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with RetroArch.
  15. * If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #ifndef _PLATFORM_UNIX_H
  18. #define _PLATFORM_UNIX_H
  19. #include <stdint.h>
  20. #include <boolean.h>
  21. #include <retro_miscellaneous.h>
  22. #include "../../config.def.h"
  23. #ifndef MAX_AXIS
  24. #define MAX_AXIS 10
  25. #endif
  26. #ifdef ANDROID
  27. #include <jni.h>
  28. #include <poll.h>
  29. #include <sched.h>
  30. #include <android/looper.h>
  31. #include <android/configuration.h>
  32. #include <android/native_activity.h>
  33. #include <android/window.h>
  34. #include <android/sensor.h>
  35. #include <rthreads/rthreads.h>
  36. #include "../../config.def.h"
  37. bool test_permissions(const char *path);
  38. char internal_storage_path[PATH_MAX_LENGTH];
  39. char internal_storage_app_path[PATH_MAX_LENGTH];
  40. struct android_app;
  41. struct android_poll_source
  42. {
  43. /* The identifier of this source. May be LOOPER_ID_MAIN or
  44. * LOOPER_ID_INPUT. */
  45. int32_t id;
  46. /* The android_app this ident is associated with. */
  47. struct android_app* app;
  48. /* Function to call to perform the standard processing of data from
  49. * this source. */
  50. void (*process)(struct android_app* app, struct android_poll_source* source);
  51. };
  52. struct android_app
  53. {
  54. /* The application can place a pointer to its own state object
  55. * here if it likes. */
  56. void* userData;
  57. /* Fill this in with the function to process main app commands (APP_CMD_*) */
  58. void (*onAppCmd)(struct android_app* app, int32_t cmd);
  59. /* Fill this in with the function to process input events. At this point
  60. * the event has already been pre-dispatched, and it will be finished upon
  61. * return. Return 1 if you have handled the event, 0 for any default
  62. * dispatching. */
  63. int32_t (*onInputEvent)(struct android_app* app, AInputEvent* event);
  64. /* The ANativeActivity object instance that this app is running in. */
  65. ANativeActivity* activity;
  66. /* The current configuration the app is running in. */
  67. AConfiguration *config;
  68. /* This is the last instance's saved state, as provided at creation time.
  69. * It is NULL if there was no state. You can use this as you need; the
  70. * memory will remain around until you call android_app_exec_cmd() for
  71. * APP_CMD_RESUME, at which point it will be freed and savedState set to NULL.
  72. * These variables should only be changed when processing a APP_CMD_SAVE_STATE,
  73. * at which point they will be initialized to NULL and you can malloc your
  74. * state and place the information here. In that case the memory will be
  75. * freed for you later.
  76. */
  77. void* savedState;
  78. size_t savedStateSize;
  79. /* The ALooper associated with the app's thread. */
  80. ALooper* looper;
  81. /* When non-NULL, this is the input queue from which the app will
  82. * receive user input events. */
  83. AInputQueue* inputQueue;
  84. /* When non-NULL, this is the window surface that the app can draw in. */
  85. ANativeWindow* window;
  86. /* Current state of the app's activity. May be either APP_CMD_START,
  87. * APP_CMD_RESUME, APP_CMD_PAUSE, or APP_CMD_STOP; see below. */
  88. int activityState;
  89. int reinitRequested;
  90. /* This is non-zero when the application's NativeActivity is being
  91. * destroyed and waiting for the app thread to complete. */
  92. int destroyRequested;
  93. int openGameDialogRequested;
  94. /* Below are "private" implementation of the glue code. */
  95. slock_t *mutex;
  96. scond_t *cond;
  97. int msgread;
  98. int msgwrite;
  99. sthread_t *thread;
  100. struct android_poll_source cmdPollSource;
  101. struct android_poll_source inputPollSource;
  102. int running;
  103. int stateSaved;
  104. int destroyed;
  105. AInputQueue* pendingInputQueue;
  106. ANativeWindow* pendingWindow;
  107. /* Below are "private" implementation of RA code. */
  108. bool unfocused;
  109. unsigned accelerometer_event_rate;
  110. unsigned gyroscope_event_rate;
  111. ASensorManager *sensorManager;
  112. ASensorEventQueue *sensorEventQueue;
  113. const ASensor* accelerometerSensor;
  114. const ASensor* gyroscopeSensor;
  115. uint64_t sensor_state_mask;
  116. char current_ime[NAME_MAX_LENGTH];
  117. bool input_alive;
  118. int16_t analog_state[DEFAULT_MAX_PADS][MAX_AXIS];
  119. int8_t hat_state[DEFAULT_MAX_PADS][2];
  120. jmethodID getIntent;
  121. jmethodID onRetroArchExit;
  122. jmethodID getStringExtra;
  123. jmethodID clearPendingIntent;
  124. jmethodID hasPendingIntent;
  125. jmethodID getPendingIntentConfigPath;
  126. jmethodID getPendingIntentLibretroPath;
  127. jmethodID getPendingIntentFullPath;
  128. jmethodID getPendingIntentIME;
  129. jmethodID getPendingIntentStorageLocation;
  130. jmethodID getPendingIntentDownloadsLocation;
  131. jmethodID getPendingIntentScreenshotsLocation;
  132. jmethodID isAndroidTV;
  133. jmethodID getPowerstate;
  134. jmethodID getBatteryLevel;
  135. jmethodID setSustainedPerformanceMode;
  136. jmethodID setScreenOrientation;
  137. jmethodID getUserLanguageString;
  138. jmethodID doVibrate;
  139. jmethodID doHapticFeedback;
  140. jmethodID isPlayStoreBuild;
  141. jmethodID getAvailableCores;
  142. jmethodID getInstalledCores;
  143. jmethodID downloadCore;
  144. jmethodID deleteCore;
  145. jmethodID getVolumeCount;
  146. jmethodID getVolumePath;
  147. jmethodID openGameDialog;
  148. jmethodID environmentCallback;
  149. struct
  150. {
  151. unsigned width, height;
  152. bool changed;
  153. } content_rect;
  154. uint16_t rumble_last_strength_strong[MAX_USERS];
  155. uint16_t rumble_last_strength_weak[MAX_USERS];
  156. uint16_t rumble_last_strength[MAX_USERS];
  157. int id[MAX_USERS];
  158. struct {
  159. struct {
  160. jclass clazz;
  161. jmethodID constructor;
  162. } input_descriptor_bean;
  163. } beans;
  164. };
  165. enum
  166. {
  167. LOOPER_ID_MAIN = 1,
  168. LOOPER_ID_INPUT,
  169. LOOPER_ID_USER,
  170. LOOPER_ID_INPUT_MSG
  171. };
  172. enum
  173. {
  174. APP_CMD_INPUT_CHANGED,
  175. /**
  176. * Command from main thread: a new ANativeWindow is ready for use. Upon
  177. * receiving this command, android_app->window will contain the new window
  178. * surface.
  179. */
  180. APP_CMD_INIT_WINDOW,
  181. /**
  182. * Command from main thread: the existing ANativeWindow needs to be
  183. * terminated. Upon receiving this command, android_app->window still
  184. * contains the existing window; after calling android_app_exec_cmd
  185. * it will be set to NULL.
  186. */
  187. APP_CMD_TERM_WINDOW,
  188. /**
  189. * Command from main thread: the current ANativeWindow has been resized.
  190. * Please redraw with its new size.
  191. */
  192. APP_CMD_WINDOW_RESIZED,
  193. /**
  194. * Command from main thread: the system needs that the current ANativeWindow
  195. * be redrawn. You should redraw the window before handing this to
  196. * android_app_exec_cmd() in order to avoid transient drawing glitches.
  197. */
  198. APP_CMD_WINDOW_REDRAW_NEEDED,
  199. /**
  200. * Command from main thread: the content area of the window has changed,
  201. * such as from the soft input window being shown or hidden. You can
  202. * find the new content rect in android_app::contentRect.
  203. */
  204. APP_CMD_CONTENT_RECT_CHANGED,
  205. /**
  206. * Command from main thread: the app's activity window has gained
  207. * input focus.
  208. */
  209. APP_CMD_GAINED_FOCUS,
  210. /**
  211. * Command from main thread: the app's activity window has lost
  212. * input focus.
  213. */
  214. APP_CMD_LOST_FOCUS,
  215. /**
  216. * Command from main thread: the current device configuration has changed.
  217. */
  218. APP_CMD_CONFIG_CHANGED,
  219. /**
  220. * Command from main thread: the system is running low on memory.
  221. * Try to reduce your memory use.
  222. */
  223. APP_CMD_LOW_MEMORY,
  224. /**
  225. * Command from main thread: the app's activity has been started.
  226. */
  227. APP_CMD_START,
  228. /**
  229. * Command from main thread: the app's activity has been resumed.
  230. */
  231. APP_CMD_RESUME,
  232. /**
  233. * Command from main thread: the app should generate a new saved state
  234. * for itself, to restore from later if needed.
  235. */
  236. APP_CMD_SAVE_STATE,
  237. /**
  238. * Command from main thread: the app's activity has been paused.
  239. */
  240. APP_CMD_PAUSE,
  241. /**
  242. * Command from main thread: the app's activity has been stopped.
  243. */
  244. APP_CMD_STOP,
  245. /**
  246. * Command from main thread: the app's activity is being destroyed,
  247. * and waiting for the app thread to clean up and exit before proceeding.
  248. */
  249. APP_CMD_DESTROY,
  250. APP_CMD_REINIT_DONE,
  251. APP_CMD_GAME_DIALOG_OPENED,
  252. APP_CMD_GAME_DIALOG_CLOSED,
  253. };
  254. #define JNI_EXCEPTION(env) \
  255. if ((*env)->ExceptionOccurred(env)) \
  256. { \
  257. (*env)->ExceptionDescribe(env); \
  258. (*env)->ExceptionClear(env); \
  259. }
  260. #define FIND_CLASS(env, var, classname) \
  261. var = (*env)->FindClass(env, classname); \
  262. JNI_EXCEPTION(env)
  263. #define GET_OBJECT_CLASS(env, var, clazz_obj) \
  264. var = (*env)->GetObjectClass(env, clazz_obj); \
  265. JNI_EXCEPTION(env)
  266. #define GET_FIELD_ID(env, var, clazz, fieldName, fieldDescriptor) \
  267. var = (*env)->GetFieldID(env, clazz, fieldName, fieldDescriptor); \
  268. JNI_EXCEPTION(env)
  269. #define GET_METHOD_ID(env, var, clazz, methodName, fieldDescriptor) \
  270. var = (*env)->GetMethodID(env, clazz, methodName, fieldDescriptor); \
  271. JNI_EXCEPTION(env)
  272. #define GET_STATIC_METHOD_ID(env, var, clazz, methodName, fieldDescriptor) \
  273. var = (*env)->GetStaticMethodID(env, clazz, methodName, fieldDescriptor); \
  274. JNI_EXCEPTION(env)
  275. #define CALL_OBJ_METHOD(env, var, clazz_obj, methodId) \
  276. var = (*env)->CallObjectMethod(env, clazz_obj, methodId); \
  277. JNI_EXCEPTION(env)
  278. #define CALL_OBJ_STATIC_METHOD(env, var, clazz, methodId) \
  279. var = (*env)->CallStaticObjectMethod(env, clazz, methodId); \
  280. JNI_EXCEPTION(env)
  281. #define CALL_OBJ_STATIC_METHOD_PARAM(env, var, clazz, methodId, ...) \
  282. var = (*env)->CallStaticObjectMethod(env, clazz, methodId, __VA_ARGS__); \
  283. JNI_EXCEPTION(env)
  284. #define CALL_OBJ_METHOD_PARAM(env, var, clazz_obj, methodId, ...) \
  285. var = (*env)->CallObjectMethod(env, clazz_obj, methodId, __VA_ARGS__); \
  286. JNI_EXCEPTION(env)
  287. #define CALL_VOID_METHOD(env, clazz_obj, methodId) \
  288. (*env)->CallVoidMethod(env, clazz_obj, methodId); \
  289. JNI_EXCEPTION(env)
  290. #define CALL_VOID_METHOD_PARAM(env, clazz_obj, methodId, ...) \
  291. (*env)->CallVoidMethod(env, clazz_obj, methodId, __VA_ARGS__); \
  292. JNI_EXCEPTION(env)
  293. #define CALL_BOOLEAN_METHOD(env, var, clazz_obj, methodId) \
  294. var = (*env)->CallBooleanMethod(env, clazz_obj, methodId); \
  295. JNI_EXCEPTION(env)
  296. #define CALL_DOUBLE_METHOD(env, var, clazz_obj, methodId) \
  297. var = (*env)->CallDoubleMethod(env, clazz_obj, methodId); \
  298. JNI_EXCEPTION(env)
  299. #define CALL_INT_METHOD(env, var, clazz_obj, methodId) \
  300. var = (*env)->CallIntMethod(env, clazz_obj, methodId); \
  301. JNI_EXCEPTION(env)
  302. extern JNIEnv *jni_thread_getenv(void);
  303. void android_app_write_cmd(struct android_app *android_app, int8_t cmd);
  304. void android_dpi_get_density(char *s, size_t len);
  305. void android_environment_cb_native(unsigned cmd, void *desc);
  306. extern struct android_app *g_android;
  307. #endif
  308. #endif