Debian Codegraph @

src/android/android_joystick.c

raw
1 #include "allegro5/allegro.h"
2 #include "allegro5/internal/aintern.h"
3 #include "allegro5/internal/aintern_events.h"
4 #include "allegro5/internal/aintern_joystick.h"
5 #include "allegro5/internal/aintern_android.h"
6
7 ALLEGRO_DEBUG_CHANNEL("android")
8
9 /* the number of gamepad & dpad buttons defined in the Android documentation
10 https://developer.android.com/reference/android/view/KeyEvent */
11 #define MAX_BUTTONS 36
12 ALLEGRO_STATIC_ASSERT(android_joystick, _AL_MAX_JOYSTICK_BUTTONS >= MAX_BUTTONS);
13
14 /* sticks: left & right thumbsticks; left & right triggers */
15 #define MAX_STICKS 4
16 ALLEGRO_STATIC_ASSERT(android_joystick, _AL_MAX_JOYSTICK_STICKS >= MAX_STICKS);
17
18 typedef struct ALLEGRO_JOYSTICK_ANDROID {
19 ALLEGRO_JOYSTICK parent;
20 ALLEGRO_JOYSTICK_STATE joystate;
21 char name[64];
22 } ALLEGRO_JOYSTICK_ANDROID;
23
24 static const char *button_name[MAX_BUTTONS] = {
25 /* buttons are named according to the Android documentation
26 https://developer.android.com/reference/android/view/KeyEvent */
27
28 /* standard gamepad & d-pad buttons */
29 "A",
30 "B",
31 "X",
32 "Y",
33 "L1",
34 "R1",
35 "DPAD LEFT",
36 "DPAD RIGHT",
37 "DPAD UP",
38 "DPAD DOWN",
39 "START", /* start / menu */
40 "SELECT", /* select / back */
41 "MODE", /* mode / guide */
42 "THUMBL",
43 "THUMBR",
44 "L2",
45 "R2",
46 "C",
47 "Z",
48 "DPAD CENTER",
49
50 /* generic gamepad buttons */
51 "1",
52 "2",
53 "3",
54 "4",
55 "5",
56 "6",
57 "7",
58 "8",
59 "9",
60 "10",
61 "11",
62 "12",
63 "13",
64 "14",
65 "15",
66 "16"
67 };
68
69 static _AL_VECTOR joysticks = _AL_VECTOR_INITIALIZER(ALLEGRO_JOYSTICK_ANDROID *);
70 static bool initialized;
71
72 static char *android_get_joystick_name(JNIEnv *env, jobject activity, int index, char *buffer, size_t buffer_size)
73 {
74 jstring str_obj = (jstring)_jni_callObjectMethodV(env, activity, "getJoystickName", "(I)Ljava/lang/String;", (jint)index);
75 ALLEGRO_USTR *s = _jni_getString(env, str_obj);
76 _al_sane_strncpy(buffer, al_cstr(s), buffer_size);
77 al_ustr_free(s);
78 _jni_callv(env, DeleteLocalRef, str_obj);
79
80 return buffer;
81 }
82
83 static void android_init_joysticks(int num)
84 {
85 int i, j;
86 JNIEnv *env = _al_android_get_jnienv();
87 jobject activity = _al_android_activity_object();
88
89 for (i = 0; i < num; i++) {
90 ALLEGRO_JOYSTICK_ANDROID *stick = al_calloc(1, sizeof(ALLEGRO_JOYSTICK_ANDROID));
91 ALLEGRO_JOYSTICK_ANDROID **ptr;
92 ALLEGRO_JOYSTICK *joy;
93
94 joy = (void *)stick;
95
96 /* Fill in the joystick information fields. */
97 android_get_joystick_name(env, activity, i, stick->name, sizeof(stick->name));
98 joy->info.num_sticks = MAX_STICKS;
99 joy->info.num_buttons = MAX_BUTTONS;
100 joy->info.stick[0].name = "Left Thumbstick";
101 joy->info.stick[0].num_axes = 2;
102 joy->info.stick[0].axis[0].name = "X";
103 joy->info.stick[0].axis[1].name = "Y";
104 joy->info.stick[0].flags = ALLEGRO_JOYFLAG_ANALOGUE;
105 joy->info.stick[1].name = "Right Thumbstick";
106 joy->info.stick[1].num_axes = 2;
107 joy->info.stick[1].axis[0].name = "X";
108 joy->info.stick[1].axis[1].name = "Y";
109 joy->info.stick[1].flags = ALLEGRO_JOYFLAG_ANALOGUE;
110 joy->info.stick[2].name = "Left Trigger";
111 joy->info.stick[2].num_axes = 1;
112 joy->info.stick[2].axis[0].name = "Ramp";
113 joy->info.stick[2].flags = ALLEGRO_JOYFLAG_ANALOGUE;
114 joy->info.stick[3].name = "Right Trigger";
115 joy->info.stick[3].num_axes = 1;
116 joy->info.stick[3].axis[0].name = "Ramp";
117 joy->info.stick[3].flags = ALLEGRO_JOYFLAG_ANALOGUE;
118
119 for (j = 0; j < joy->info.num_buttons; j++) {
120 joy->info.button[j].name = button_name[j];
121 }
122
123 ptr = _al_vector_alloc_back(&joysticks);
124 *ptr = stick;
125 }
126 }
127
128 static bool andjoy_init_joystick(void)
129 {
130 ALLEGRO_JOYSTICK_ANDROID *accel = al_calloc(1, sizeof(ALLEGRO_JOYSTICK_ANDROID));
131 ALLEGRO_JOYSTICK_ANDROID **ptr;
132 ALLEGRO_JOYSTICK *joy;
133 int num;
134
135 joy = (void *)accel;
136
137 /* Fill in the joystick information fields. */
138 _al_sane_strncpy(accel->name, "Accelerometer", sizeof(accel->name));
139 joy->info.num_sticks = 1;
140 joy->info.num_buttons = 0;
141 joy->info.stick[0].name = "Accelerometer";
142 joy->info.stick[0].num_axes = 3;
143 joy->info.stick[0].axis[0].name = "X";
144 joy->info.stick[0].axis[1].name = "Y";
145 joy->info.stick[0].axis[2].name = "Z";
146 joy->info.stick[0].flags = ALLEGRO_JOYFLAG_ANALOGUE;
147
148 ptr = _al_vector_alloc_back(&joysticks);
149 *ptr = accel;
150
151 num = _jni_callIntMethodV(_al_android_get_jnienv(), _al_android_activity_object(), "getNumJoysticks", "()I");
152
153 android_init_joysticks(num);
154
155 initialized = true;
156
157 _jni_callVoidMethod(_al_android_get_jnienv(), _al_android_activity_object(), "setJoystickActive");
158
159 return true;
160 }
161
162 static void andjoy_exit_joystick(void)
163 {
164 initialized = false;
165 }
166
167 static bool andjoy_reconfigure_joysticks(void)
168 {
169 int i;
170 int sz;
171 int num;
172
173 sz = _al_vector_size(&joysticks);
174
175 for (i = 1; i < sz; i++) {
176 al_free(*((ALLEGRO_JOYSTICK_ANDROID **)_al_vector_ref(&joysticks, 1)));
177 _al_vector_delete_at(&joysticks, 1);
178 }
179
180 _jni_callVoidMethod(_al_android_get_jnienv(), _al_android_activity_object(), "reconfigureJoysticks");
181
182 num = _jni_callIntMethodV(_al_android_get_jnienv(), _al_android_activity_object(), "getNumJoysticks", "()I");
183
184 android_init_joysticks(num);
185
186 return true;
187 }
188
189 static int andjoy_num_joysticks(void)
190 {
191 return _al_vector_size(&joysticks);
192 }
193
194 static ALLEGRO_JOYSTICK *andjoy_get_joystick(int num)
195 {
196 ALLEGRO_JOYSTICK_ANDROID *andjoy;
197 ALLEGRO_JOYSTICK *joy;
198
199 if (num >= andjoy_num_joysticks())
200 return NULL;
201
202 andjoy = *((ALLEGRO_JOYSTICK_ANDROID **)_al_vector_ref(&joysticks, num));
203 joy = &andjoy->parent;
204
205 return joy;
206 }
207
208 static void andjoy_release_joystick(ALLEGRO_JOYSTICK *joy)
209 {
210 int i;
211 int sz;
212 (void)joy;
213
214 sz = _al_vector_size(&joysticks);
215
216 for (i = 0; i < sz; i++) {
217 al_free(*((ALLEGRO_JOYSTICK_ANDROID **)_al_vector_ref(&joysticks, 0)));
218 _al_vector_delete_at(&joysticks, 0);
219 }
220
221 _jni_callVoidMethod(_al_android_get_jnienv(), _al_android_activity_object(), "setJoystickInactive");
222
223 ALLEGRO_DEBUG("Joystick released.\n");
224 initialized = false;
225 }
226
227 static void andjoy_get_joystick_state(ALLEGRO_JOYSTICK *joy, ALLEGRO_JOYSTICK_STATE *ret_state)
228 {
229 ALLEGRO_JOYSTICK_ANDROID *andjoy = (void *)joy;
230 ALLEGRO_EVENT_SOURCE *es = al_get_joystick_event_source();
231 int i;
232 bool found = false;
233
234 for (i = 0; i < (int)_al_vector_size(&joysticks); i++) {
235 ALLEGRO_JOYSTICK_ANDROID **ptr = _al_vector_ref(&joysticks, i);
236 ALLEGRO_JOYSTICK_ANDROID *thisjoy = *ptr;
237 if (andjoy == thisjoy) {
238 found = true;
239 break;
240 }
241 }
242
243 if (!found) {
244 memset(ret_state, 0, sizeof(*ret_state));
245 return;
246 }
247
248 _al_event_source_lock(es);
249 *ret_state = andjoy->joystate;
250 _al_event_source_unlock(es);
251 }
252
253 void _al_android_generate_accelerometer_event(float x, float y, float z)
254 {
255 if (!initialized)
256 return;
257
258 /* Android reports accelerometer data in the approximate range
259 * -9.81 -> 9.81, but can be higher or lower. Allegro joysticks
260 * use -1 -> 1. Also, the axis' are all reversed on Android,
261 * hence the negative division.
262 */
263 x /= -9.81f;
264 if (x < -1) x = -1;
265 if (x > 1) x = 1;
266 y /= -9.81f;
267 if (y < -1) y = -1;
268 if (y > 1) y = 1;
269 z /= -9.81f;
270 if (z < -1) z = -1;
271 if (z > 1) z = 1;
272
273 ALLEGRO_JOYSTICK_ANDROID *accel = *((ALLEGRO_JOYSTICK_ANDROID **)_al_vector_ref(&joysticks, 0));
274 ALLEGRO_JOYSTICK *joy = &accel->parent;
275
276 ALLEGRO_EVENT_SOURCE *es = al_get_joystick_event_source();
277
278 ALLEGRO_EVENT event;
279
280 _al_event_source_lock(es);
281
282 if (_al_event_source_needs_to_generate_event(es)) {
283 float pos[] = {x, y, z};
284 int i;
285 for (i = 0; i < 3; i++) {
286 event.joystick.id = joy;
287 event.joystick.type = ALLEGRO_EVENT_JOYSTICK_AXIS;
288 event.joystick.timestamp = al_get_time();
289 event.joystick.stick = 0;
290 event.joystick.axis = i;
291 event.joystick.pos = pos[i];
292 event.joystick.button = 0;
293
294 accel->joystate.stick[0].axis[i] = pos[i];
295
296 _al_event_source_emit_event(es, &event);
297 }
298 }
299
300 _al_event_source_unlock(es);
301 }
302
303 void _al_android_generate_joystick_axis_event(int index, int stick, int axis, float value)
304 {
305 if (!initialized || index >= andjoy_num_joysticks())
306 return;
307
308 ALLEGRO_JOYSTICK_ANDROID *joystick = *((ALLEGRO_JOYSTICK_ANDROID **)_al_vector_ref(&joysticks, index));
309 ALLEGRO_JOYSTICK *joy = &joystick->parent;
310
311 ALLEGRO_EVENT_SOURCE *es = al_get_joystick_event_source();
312
313 ALLEGRO_EVENT event;
314
315 _al_event_source_lock(es);
316
317 if (_al_event_source_needs_to_generate_event(es)) {
318 event.joystick.id = joy;
319 event.joystick.type = ALLEGRO_EVENT_JOYSTICK_AXIS;
320 event.joystick.timestamp = al_get_time();
321 event.joystick.stick = stick;
322 event.joystick.axis = axis;
323 event.joystick.pos = value;
324 event.joystick.button = 0;
325
326 joystick->joystate.stick[stick].axis[axis] = value;
327
328 _al_event_source_emit_event(es, &event);
329 }
330 _al_event_source_unlock(es);
331 }
332
333 void _al_android_generate_joystick_button_event(int index, int button, bool down)
334 {
335 int type;
336
337 if (!initialized || index >= andjoy_num_joysticks())
338 return;
339
340 ALLEGRO_JOYSTICK_ANDROID *joystick = *((ALLEGRO_JOYSTICK_ANDROID **)_al_vector_ref(&joysticks, index));
341 ALLEGRO_JOYSTICK *joy = &joystick->parent;
342
343 ALLEGRO_EVENT_SOURCE *es = al_get_joystick_event_source();
344
345 ALLEGRO_EVENT event;
346
347 _al_event_source_lock(es);
348
349 if (down)
350 type = ALLEGRO_EVENT_JOYSTICK_BUTTON_DOWN;
351 else
352 type = ALLEGRO_EVENT_JOYSTICK_BUTTON_UP;
353
354 if (_al_event_source_needs_to_generate_event(es)) {
355 event.joystick.id = joy;
356 event.joystick.type = type;
357 event.joystick.timestamp = al_get_time();
358 event.joystick.stick = 0;
359 event.joystick.axis = 0;
360 event.joystick.pos = 0;
361 event.joystick.button = button;
362
363 joystick->joystate.button[button] = type == ALLEGRO_EVENT_JOYSTICK_BUTTON_DOWN ? 1 : 0;
364
365 _al_event_source_emit_event(es, &event);
366 }
367 _al_event_source_unlock(es);
368 }
369
370 static char const *andjoy_get_name(ALLEGRO_JOYSTICK *joy)
371 {
372 int i;
373 (void)joy;
374
375 for (i = 0; i < (int)_al_vector_size(&joysticks); i++) {
376 ALLEGRO_JOYSTICK_ANDROID *andjoy = *((ALLEGRO_JOYSTICK_ANDROID **)_al_vector_ref(&joysticks, i));
377 if ((ALLEGRO_JOYSTICK *)andjoy == joy)
378 return andjoy->name;
379 }
380
381 return "";
382 }
383
384 static bool andjoy_get_active(ALLEGRO_JOYSTICK *joy)
385 {
386 (void)joy;
387 return true;
388 }
389
390 ALLEGRO_JOYSTICK_DRIVER _al_android_joystick_driver = {
391 AL_ID('A', 'N', 'D', 'R'),
392 "",
393 "",
394 "android joystick",
395 andjoy_init_joystick,
396 andjoy_exit_joystick,
397 andjoy_reconfigure_joysticks,
398 andjoy_num_joysticks,
399 andjoy_get_joystick,
400 andjoy_release_joystick,
401 andjoy_get_joystick_state,
402 andjoy_get_name,
403 andjoy_get_active
404 };