platform_unix.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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. struct {
  164. jclass clazz;
  165. jmethodID pollInputDevices;
  166. } joypad_manager;
  167. } beans;
  168. };
  169. enum
  170. {
  171. LOOPER_ID_MAIN = 1,
  172. LOOPER_ID_INPUT,
  173. LOOPER_ID_USER,
  174. LOOPER_ID_INPUT_MSG
  175. };
  176. enum
  177. {
  178. APP_CMD_INPUT_CHANGED,
  179. /**
  180. * Command from main thread: a new ANativeWindow is ready for use. Upon
  181. * receiving this command, android_app->window will contain the new window
  182. * surface.
  183. */
  184. APP_CMD_INIT_WINDOW,
  185. /**
  186. * Command from main thread: the existing ANativeWindow needs to be
  187. * terminated. Upon receiving this command, android_app->window still
  188. * contains the existing window; after calling android_app_exec_cmd
  189. * it will be set to NULL.
  190. */
  191. APP_CMD_TERM_WINDOW,
  192. /**
  193. * Command from main thread: the current ANativeWindow has been resized.
  194. * Please redraw with its new size.
  195. */
  196. APP_CMD_WINDOW_RESIZED,
  197. /**
  198. * Command from main thread: the system needs that the current ANativeWindow
  199. * be redrawn. You should redraw the window before handing this to
  200. * android_app_exec_cmd() in order to avoid transient drawing glitches.
  201. */
  202. APP_CMD_WINDOW_REDRAW_NEEDED,
  203. /**
  204. * Command from main thread: the content area of the window has changed,
  205. * such as from the soft input window being shown or hidden. You can
  206. * find the new content rect in android_app::contentRect.
  207. */
  208. APP_CMD_CONTENT_RECT_CHANGED,
  209. /**
  210. * Command from main thread: the app's activity window has gained
  211. * input focus.
  212. */
  213. APP_CMD_GAINED_FOCUS,
  214. /**
  215. * Command from main thread: the app's activity window has lost
  216. * input focus.
  217. */
  218. APP_CMD_LOST_FOCUS,
  219. /**
  220. * Command from main thread: the current device configuration has changed.
  221. */
  222. APP_CMD_CONFIG_CHANGED,
  223. /**
  224. * Command from main thread: the system is running low on memory.
  225. * Try to reduce your memory use.
  226. */
  227. APP_CMD_LOW_MEMORY,
  228. /**
  229. * Command from main thread: the app's activity has been started.
  230. */
  231. APP_CMD_START,
  232. /**
  233. * Command from main thread: the app's activity has been resumed.
  234. */
  235. APP_CMD_RESUME,
  236. /**
  237. * Command from main thread: the app should generate a new saved state
  238. * for itself, to restore from later if needed.
  239. */
  240. APP_CMD_SAVE_STATE,
  241. /**
  242. * Command from main thread: the app's activity has been paused.
  243. */
  244. APP_CMD_PAUSE,
  245. /**
  246. * Command from main thread: the app's activity has been stopped.
  247. */
  248. APP_CMD_STOP,
  249. /**
  250. * Command from main thread: the app's activity is being destroyed,
  251. * and waiting for the app thread to clean up and exit before proceeding.
  252. */
  253. APP_CMD_DESTROY,
  254. APP_CMD_REINIT_DONE,
  255. APP_CMD_GAME_DIALOG_OPENED,
  256. APP_CMD_GAME_DIALOG_CLOSED,
  257. };
  258. #define JNI_EXCEPTION(env) \
  259. if ((*env)->ExceptionOccurred(env)) \
  260. { \
  261. (*env)->ExceptionDescribe(env); \
  262. (*env)->ExceptionClear(env); \
  263. }
  264. #define FIND_CLASS(env, var, classname) \
  265. var = (*env)->FindClass(env, classname); \
  266. JNI_EXCEPTION(env)
  267. #define GET_OBJECT_CLASS(env, var, clazz_obj) \
  268. var = (*env)->GetObjectClass(env, clazz_obj); \
  269. JNI_EXCEPTION(env)
  270. #define GET_FIELD_ID(env, var, clazz, fieldName, fieldDescriptor) \
  271. var = (*env)->GetFieldID(env, clazz, fieldName, fieldDescriptor); \
  272. JNI_EXCEPTION(env)
  273. #define GET_METHOD_ID(env, var, clazz, methodName, fieldDescriptor) \
  274. var = (*env)->GetMethodID(env, clazz, methodName, fieldDescriptor); \
  275. JNI_EXCEPTION(env)
  276. #define GET_STATIC_METHOD_ID(env, var, clazz, methodName, fieldDescriptor) \
  277. var = (*env)->GetStaticMethodID(env, clazz, methodName, fieldDescriptor); \
  278. JNI_EXCEPTION(env)
  279. #define CALL_OBJ_METHOD(env, var, clazz_obj, methodId) \
  280. var = (*env)->CallObjectMethod(env, clazz_obj, methodId); \
  281. JNI_EXCEPTION(env)
  282. #define CALL_OBJ_STATIC_METHOD(env, var, clazz, methodId) \
  283. var = (*env)->CallStaticObjectMethod(env, clazz, methodId); \
  284. JNI_EXCEPTION(env)
  285. #define CALL_OBJ_STATIC_METHOD_PARAM(env, var, clazz, methodId, ...) \
  286. var = (*env)->CallStaticObjectMethod(env, clazz, methodId, __VA_ARGS__); \
  287. JNI_EXCEPTION(env)
  288. #define CALL_OBJ_METHOD_PARAM(env, var, clazz_obj, methodId, ...) \
  289. var = (*env)->CallObjectMethod(env, clazz_obj, methodId, __VA_ARGS__); \
  290. JNI_EXCEPTION(env)
  291. #define CALL_VOID_METHOD(env, clazz_obj, methodId) \
  292. (*env)->CallVoidMethod(env, clazz_obj, methodId); \
  293. JNI_EXCEPTION(env)
  294. #define CALL_VOID_STATIC_METHOD(env, clazz_obj, methodId) \
  295. (*env)->CallStaticVoidMethod(env, clazz_obj, methodId); \
  296. JNI_EXCEPTION(env)
  297. #define CALL_VOID_METHOD_PARAM(env, clazz_obj, methodId, ...) \
  298. (*env)->CallVoidMethod(env, clazz_obj, methodId, __VA_ARGS__); \
  299. JNI_EXCEPTION(env)
  300. #define CALL_BOOLEAN_METHOD(env, var, clazz_obj, methodId) \
  301. var = (*env)->CallBooleanMethod(env, clazz_obj, methodId); \
  302. JNI_EXCEPTION(env)
  303. #define CALL_DOUBLE_METHOD(env, var, clazz_obj, methodId) \
  304. var = (*env)->CallDoubleMethod(env, clazz_obj, methodId); \
  305. JNI_EXCEPTION(env)
  306. #define CALL_INT_METHOD(env, var, clazz_obj, methodId) \
  307. var = (*env)->CallIntMethod(env, clazz_obj, methodId); \
  308. JNI_EXCEPTION(env)
  309. extern JNIEnv *jni_thread_getenv(void);
  310. void android_app_write_cmd(struct android_app *android_app, int8_t cmd);
  311. void android_dpi_get_density(char *s, size_t len);
  312. void android_environment_cb_native(unsigned cmd, void *desc);
  313. extern struct android_app *g_android;
  314. #endif
  315. #endif