RetroArch
platform_unix.h
Go to the documentation of this file.
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 
18 #ifndef _PLATFORM_UNIX_H
19 #define _PLATFORM_UNIX_H
20 
21 #include <stdint.h>
22 
23 #include <boolean.h>
24 
25 #ifndef MAX_PADS
26 #define MAX_PADS 8
27 #endif
28 
29 #ifndef MAX_AXIS
30 #define MAX_AXIS 10
31 #endif
32 
33 #ifdef ANDROID
34 #include <jni.h>
35 #include <poll.h>
36 #include <sched.h>
37 
38 #include <android/looper.h>
39 #include <android/configuration.h>
40 #include <android/native_activity.h>
41 #include <android/window.h>
42 #include <android/sensor.h>
43 
44 #include <rthreads/rthreads.h>
45 
46 bool test_permissions(const char *path);
47 
48 char internal_storage_path[PATH_MAX_LENGTH];
49 char internal_storage_app_path[PATH_MAX_LENGTH];
50 
51 struct android_app;
52 
53 struct android_poll_source
54 {
55  /* The identifier of this source. May be LOOPER_ID_MAIN or
56  * LOOPER_ID_INPUT. */
57  int32_t id;
58 
59  /* The android_app this ident is associated with. */
60  struct android_app* app;
61 
62  /* Function to call to perform the standard processing of data from
63  * this source. */
64  void (*process)(struct android_app* app, struct android_poll_source* source);
65 };
66 
67 struct android_app
68 {
69  /* The application can place a pointer to its own state object
70  * here if it likes. */
71  void* userData;
72 
73  /* Fill this in with the function to process main app commands (APP_CMD_*) */
74  void (*onAppCmd)(struct android_app* app, int32_t cmd);
75 
76  /* Fill this in with the function to process input events. At this point
77  * the event has already been pre-dispatched, and it will be finished upon
78  * return. Return 1 if you have handled the event, 0 for any default
79  * dispatching. */
80  int32_t (*onInputEvent)(struct android_app* app, AInputEvent* event);
81 
82  /* The ANativeActivity object instance that this app is running in. */
83  ANativeActivity* activity;
84 
85  /* The current configuration the app is running in. */
86  AConfiguration *config;
87 
88  /* This is the last instance's saved state, as provided at creation time.
89  * It is NULL if there was no state. You can use this as you need; the
90  * memory will remain around until you call android_app_exec_cmd() for
91  * APP_CMD_RESUME, at which point it will be freed and savedState set to NULL.
92  * These variables should only be changed when processing a APP_CMD_SAVE_STATE,
93  * at which point they will be initialized to NULL and you can malloc your
94  * state and place the information here. In that case the memory will be
95  * freed for you later.
96  */
97  void* savedState;
98  size_t savedStateSize;
99 
100  /* The ALooper associated with the app's thread. */
101  ALooper* looper;
102 
103  /* When non-NULL, this is the input queue from which the app will
104  * receive user input events. */
105  AInputQueue* inputQueue;
106 
107 
108  /* When non-NULL, this is the window surface that the app can draw in. */
109  ANativeWindow* window;
110 
111  /* Current state of the app's activity. May be either APP_CMD_START,
112  * APP_CMD_RESUME, APP_CMD_PAUSE, or APP_CMD_STOP; see below. */
113  int activityState;
114 
115  int reinitRequested;
116 
117  /* This is non-zero when the application's NativeActivity is being
118  * destroyed and waiting for the app thread to complete. */
119  int destroyRequested;
120 
121  /* Below are "private" implementation of the glue code. */
122  slock_t *mutex;
123  scond_t *cond;
124 
125  int msgread;
126  int msgwrite;
127 
128  sthread_t *thread;
129 
130  struct android_poll_source cmdPollSource;
131  struct android_poll_source inputPollSource;
132 
133  int running;
134  int stateSaved;
135  int destroyed;
136  AInputQueue* pendingInputQueue;
137  ANativeWindow* pendingWindow;
138 
139  /* Below are "private" implementation of RA code. */
140  bool unfocused;
141  unsigned accelerometer_event_rate;
142  ASensorManager *sensorManager;
143  ASensorEventQueue *sensorEventQueue;
144  const ASensor* accelerometerSensor;
145  uint64_t sensor_state_mask;
146  char current_ime[PATH_MAX_LENGTH];
147  bool input_alive;
149  int8_t hat_state[MAX_PADS][2];
150  jmethodID getIntent;
151  jmethodID onRetroArchExit;
152  jmethodID getStringExtra;
153  jmethodID clearPendingIntent;
154  jmethodID hasPendingIntent;
155  jmethodID getPendingIntentConfigPath;
156  jmethodID getPendingIntentLibretroPath;
157  jmethodID getPendingIntentFullPath;
158  jmethodID getPendingIntentIME;
159  jmethodID getPendingIntentStorageLocation;
160  jmethodID getPendingIntentDownloadsLocation;
161  jmethodID getPendingIntentScreenshotsLocation;
162  jmethodID isAndroidTV;
163  jmethodID getPowerstate;
164  jmethodID getBatteryLevel;
165  jmethodID setSustainedPerformanceMode;
166 };
167 
168 
169 enum
170 {
171  LOOPER_ID_MAIN = 1,
172  LOOPER_ID_INPUT,
173  LOOPER_ID_USER,
174  LOOPER_ID_INPUT_MSG
175 };
176 
177 enum
178 {
179  APP_CMD_INPUT_CHANGED,
185  APP_CMD_INIT_WINDOW,
186 
193  APP_CMD_TERM_WINDOW,
194 
199  APP_CMD_WINDOW_RESIZED,
200 
206  APP_CMD_WINDOW_REDRAW_NEEDED,
207 
213  APP_CMD_CONTENT_RECT_CHANGED,
214 
219  APP_CMD_GAINED_FOCUS,
220 
225  APP_CMD_LOST_FOCUS,
226 
230  APP_CMD_CONFIG_CHANGED,
231 
236  APP_CMD_LOW_MEMORY,
237 
241  APP_CMD_START,
242 
246  APP_CMD_RESUME,
247 
252  APP_CMD_SAVE_STATE,
253 
257  APP_CMD_PAUSE,
258 
262  APP_CMD_STOP,
263 
268  APP_CMD_DESTROY,
269 
270  APP_CMD_REINIT_DONE
271 };
272 
273 #define JNI_EXCEPTION(env) \
274  if ((*env)->ExceptionOccurred(env)) \
275  { \
276  (*env)->ExceptionDescribe(env); \
277  (*env)->ExceptionClear(env); \
278  }
279 
280 #define FIND_CLASS(env, var, classname) \
281  var = (*env)->FindClass(env, classname); \
282  JNI_EXCEPTION(env)
283 
284 #define GET_OBJECT_CLASS(env, var, clazz_obj) \
285  var = (*env)->GetObjectClass(env, clazz_obj); \
286  JNI_EXCEPTION(env)
287 
288 #define GET_FIELD_ID(env, var, clazz, fieldName, fieldDescriptor) \
289  var = (*env)->GetFieldID(env, clazz, fieldName, fieldDescriptor); \
290  JNI_EXCEPTION(env)
291 
292 #define GET_METHOD_ID(env, var, clazz, methodName, fieldDescriptor) \
293  var = (*env)->GetMethodID(env, clazz, methodName, fieldDescriptor); \
294  JNI_EXCEPTION(env)
295 
296 #define GET_STATIC_METHOD_ID(env, var, clazz, methodName, fieldDescriptor) \
297  var = (*env)->GetStaticMethodID(env, clazz, methodName, fieldDescriptor); \
298  JNI_EXCEPTION(env)
299 
300 #define CALL_OBJ_METHOD(env, var, clazz_obj, methodId) \
301  var = (*env)->CallObjectMethod(env, clazz_obj, methodId); \
302  JNI_EXCEPTION(env)
303 
304 #define CALL_OBJ_STATIC_METHOD(env, var, clazz, methodId) \
305  var = (*env)->CallStaticObjectMethod(env, clazz, methodId); \
306  JNI_EXCEPTION(env)
307 
308 #define CALL_OBJ_STATIC_METHOD_PARAM(env, var, clazz, methodId, ...) \
309  var = (*env)->CallStaticObjectMethod(env, clazz, methodId, __VA_ARGS__); \
310  JNI_EXCEPTION(env)
311 
312 #define CALL_OBJ_METHOD_PARAM(env, var, clazz_obj, methodId, ...) \
313  var = (*env)->CallObjectMethod(env, clazz_obj, methodId, __VA_ARGS__); \
314  JNI_EXCEPTION(env)
315 
316 #define CALL_VOID_METHOD(env, clazz_obj, methodId) \
317  (*env)->CallVoidMethod(env, clazz_obj, methodId); \
318  JNI_EXCEPTION(env)
319 
320 #define CALL_VOID_METHOD_PARAM(env, clazz_obj, methodId, ...) \
321  (*env)->CallVoidMethod(env, clazz_obj, methodId, __VA_ARGS__); \
322  JNI_EXCEPTION(env)
323 
324 #define CALL_BOOLEAN_METHOD(env, var, clazz_obj, methodId) \
325  var = (*env)->CallBooleanMethod(env, clazz_obj, methodId); \
326  JNI_EXCEPTION(env)
327 
328 #define CALL_DOUBLE_METHOD(env, var, clazz_obj, methodId) \
329  var = (*env)->CallDoubleMethod(env, clazz_obj, methodId); \
330  JNI_EXCEPTION(env)
331 
332 #define CALL_INT_METHOD(env, var, clazz_obj, methodId) \
333  var = (*env)->CallIntMethod(env, clazz_obj, methodId); \
334  JNI_EXCEPTION(env)
335 
336 extern JNIEnv *jni_thread_getenv(void);
337 
338 void android_app_write_cmd(struct android_app *android_app, int8_t cmd);
339 
340 extern struct android_app *g_android;
341 #endif
342 
343 #endif
#define MAX_AXIS
Definition: platform_unix.h:30
static int16_t analog_state[1][2][2]
Definition: ctr_joypad.c:35
Definition: rthreads.c:88
GLsizei const GLchar ** path
Definition: glext.h:7901
#define MAX_PADS
Definition: platform_unix.h:26
GLenum GLuint id
Definition: glext.h:6233
static int cond(LexState *ls)
Definition: lparser.c:1177
typedef void(__stdcall *PFN_DESTRUCTION_CALLBACK)(void *pData)
static sys_sem mutex
Definition: memp.c:120
#define PATH_MAX_LENGTH
Definition: retro_miscellaneous.h:83
typedefRETRO_BEGIN_DECLS struct sthread sthread_t
Definition: rthreads.h:35
ubyte cmd
Definition: wiiuse_internal.h:319
signed short int16_t
Definition: stdint.h:122
Definition: rthreads.c:106
struct config_s config
signed int int32_t
Definition: stdint.h:123
Definition: nk_menu.h:45
GLsizei GLsizei GLchar * source
Definition: glext.h:6688
CRetroArch app
Definition: xui.cpp:140
struct _cl_event * event
Definition: glext.h:8406
void * userData
Definition: jsonsax_full.h:259
unsigned __int64 uint64_t
Definition: stdint.h:136
signed char int8_t
Definition: stdint.h:121