forked from floooh/sokol
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sokol_gamepad.h
673 lines (581 loc) · 23.3 KB
/
sokol_gamepad.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
#ifndef SOKOL_GAMEPAD_INCLUDED
/*
sokol_gamepad.h -- cross-platform gamepad API
Project URL: https://github.com/floooh/sokol
Do this:
#define SOKOL_GAMEPAD_IMPL
before you include this file in *one* C or C++ file to create the
implementation.
This is a wrapper around each platform's native gamepad APIs:
- xinput on Windows (7 and up, Win32 and UWP)
- game controller framework on macOS/iOS/tvOS
- input API on Android
and abstracts gamepads to the layout of an idealized Xbox 360 gamepad:
- one d-pad
- two analog sticks acting as buttons when pressed
- guide and back buttons
- two analog shoulder buttons
- two analog triggers
This is the most popular and comprehensive configuration across game systems
(PS1/PS2/PS3/PS4/Xbox/Xbox360/Xbox One/Switch map directly, just for start)
To use:
- In your app initialization code call sgamepad_init()
- At the exact time you want to record input state call sgamepad_record_state()
- Get the state for a particular gamepad like this:
sgamepad_gamepad_state state;
int read = sgamepad_get_gamepad_state(0, &state);
If read equals the first argument, the gamepad was successfully read.
sgamepad_gamepad_state's members are set to the contoller's state as recorded previously.
Analog stick states are pre-processed to take dead zones into account: in
most cases you should rely on direction_x/direction_y/magnitude for input processing.
TODO: port to MacOS
https ://github.com/wetadigital/USDPluginExamples/issues/5
https://github.com/wetadigital/USDPluginExamples/issues/6
*/
#define SOKOL_GAMEPAD_INCLUDED (1)
#include <stdint.h>
#include <stdbool.h>
#ifndef SOKOL_API_DECL
#if defined(_WIN32) && defined(SOKOL_DLL) && defined(SOKOL_GAMEPAD_IMPL)
#define SOKOL_API_DECL __declspec(dllexport)
#elif defined(_WIN32) && defined(SOKOL_DLL)
#define SOKOL_API_DECL __declspec(dllimport)
#else
#define SOKOL_API_DECL extern
#endif
#endif
#ifdef __cplusplus
extern "C" {
#endif
/* Bit flags for digital_inputs bitfield */
typedef enum sgamepad_digital_inputs {
SGAMEPAD_GAMEPAD_DPAD_UP = 0x0001,
SGAMEPAD_GAMEPAD_DPAD_DOWN = 0x0002,
SGAMEPAD_GAMEPAD_DPAD_LEFT = 0x0004,
SGAMEPAD_GAMEPAD_DPAD_RIGHT = 0x0008,
SGAMEPAD_GAMEPAD_START = 0x0010,
SGAMEPAD_GAMEPAD_BACK = 0x0020, // Select on DualShock
SGAMEPAD_GAMEPAD_A = 0x0040, // X on DualShock
SGAMEPAD_GAMEPAD_B = 0x0080, // Circle on DualShock
SGAMEPAD_GAMEPAD_X = 0x0100, // Square on DualShock
SGAMEPAD_GAMEPAD_Y = 0x0200, // Triangle on DualShock
SGAMEPAD_GAMEPAD_LEFT_THUMB = 0x0400, // L3 on DualShock
SGAMEPAD_GAMEPAD_RIGHT_THUMB = 0x0800, // R3 on DualShock
} sgamepad_digital_inputs;
typedef struct sgamepad_analog_stick_state {
float normalized_x; //X component as reported by underlying API, scaled 0 to 1
float normalized_y; //Y component as reported by underlying API, scaled 0 to 1
float direction_x; //X component of normalized direction vector
float direction_y; //Y component of normalized direction vector
float magnitude; //Normalized magnitude
} sgamepad_analog_stick_state;
typedef struct sgamepad_gamepad_state {
uint16_t digital_inputs;
sgamepad_analog_stick_state left_stick;
sgamepad_analog_stick_state right_stick;
float left_shoulder;
float right_shoulder;
float left_trigger;
float right_trigger;
bool connected;
} sgamepad_gamepad_state;
SOKOL_API_DECL unsigned int sgamepad_get_max_supported_gamepads();
SOKOL_API_DECL bool sgamepad_is_connected(unsigned int index);
SOKOL_API_DECL void sgamepad_init();
SOKOL_API_DECL void sgamepad_record_state();
SOKOL_API_DECL unsigned int sgamepad_get_gamepad_state(unsigned int index, sgamepad_gamepad_state* pstate);
#ifdef __cplusplus
} /* extern "C" */
#endif
#endif // SOKOL_GAMEPAD_INCLUDED
//------------------------------------------------------------------------------
// Implementation follows
#ifdef SOKOL_GAMEPAD_IMPL
#define SOKOL_GAMEPAD_IMPL_INCLUDED (1)
#include <string.h> /* memset */
#include <math.h>
#ifndef SOKOL_API_IMPL
#define SOKOL_API_IMPL
#endif
#ifndef SOKOL_DEBUG
#ifndef NDEBUG
#define SOKOL_DEBUG (1)
#endif
#endif
#ifndef SOKOL_ASSERT
#include <assert.h>
#define SOKOL_ASSERT(c) assert(c)
#endif
#ifndef SOKOL_UNREACHABLE
#define SOKOL_UNREACHABLE SOKOL_ASSERT(false)
#endif
#if !defined(SOKOL_CALLOC) || !defined(SOKOL_FREE)
#include <stdlib.h>
#endif
#if !defined(SOKOL_CALLOC)
#define SOKOL_CALLOC(n,s) calloc(n,s)
#endif
#if !defined(SOKOL_FREE)
#define SOKOL_FREE(p) free(p)
#endif
#ifndef SOKOL_LOG
#ifdef SOKOL_DEBUG
#if defined(__ANDROID__)
#include <android/log.h>
#define SOKOL_LOG(s) { SOKOL_ASSERT(s); __android_log_write(ANDROID_LOG_INFO, "SOKOL_APP", s); }
#else
#include <stdio.h>
#define SOKOL_LOG(s) { SOKOL_ASSERT(s); puts(s); }
#endif
#else
#define SOKOL_LOG(s)
#endif
#endif
#ifndef SOKOL_ABORT
#include <stdlib.h>
#define SOKOL_ABORT() abort()
#endif
#ifndef _SOKOL_PRIVATE
#if defined(__GNUC__) || defined(__clang__)
#define _SOKOL_PRIVATE __attribute__((unused)) static
#else
#define _SOKOL_PRIVATE static
#endif
#endif
#ifndef _SOKOL_UNUSED
#define _SOKOL_UNUSED(x) (void)(x)
#endif
/*== COMMON INCLUDES AND DEFINES ==================================*/
_SOKOL_PRIVATE float _sgamepad_normalize_analog_trigger(float value, float max_value, float activation_value) {
if (value < activation_value) {
return 0.0f;
}
float output = (value - activation_value) / (max_value - activation_value);
return output;
}
static float float_min(float a, float b)
{
return a < b ? a : b;
}
static float float_max(float a, float b)
{
return a > b ? a : b;
}
_SOKOL_PRIVATE void _sgamepad_generate_analog_stick_state(float x_value, float y_value, float max_magnitude, float dead_zone_magnitude, sgamepad_analog_stick_state* pstate) {
float magnitude = 0.0f;
if (max_magnitude != 1.0f) {
pstate->normalized_x = float_max(float_min(x_value/max_magnitude, 1.0f), -1.0f);
pstate->normalized_y = float_max(float_min(y_value/max_magnitude, 1.0f), -1.0f);
magnitude = sqrtf(x_value * x_value + y_value * y_value);
}
else {
pstate->normalized_x = x_value;
pstate->normalized_y = y_value;
magnitude = float_min(sqrtf(x_value * x_value + y_value * y_value), 1.0f);
}
if (magnitude <= dead_zone_magnitude) {
pstate->direction_x = 0.0f;
pstate->direction_y = 0.0f;
pstate->magnitude = 0.0f;
return;
}
pstate->direction_x = x_value / magnitude;
pstate->direction_y = y_value / magnitude;
magnitude = float_min(magnitude, max_magnitude);
pstate->magnitude = (magnitude - dead_zone_magnitude) / (max_magnitude - dead_zone_magnitude);
}
/*== PLATFORM SPECIFIC INCLUDES AND DEFINES ==================================*/
#if defined (_WIN32)
#include <Windows.h>
#include <Xinput.h>
#define SGAMEPAD_MAX_SUPPORTED_GAMEPADS XUSER_MAX_COUNT
#elif defined(__APPLE__)
#define SGAMEPAD_MAX_SUPPORTED_GAMEPADS 4
#if __has_feature(objc_arc) && !__has_feature(objc_arc_fields)
#error "sokol_app.h requires __has_feature(objc_arc_field) if ARC is enabled (use a more recent compiler version)"
#endif
#include <GameController/GameController.h>
#elif defined(__ANDROID__)
#define SGAMEPAD_MAX_SUPPORTED_GAMEPADS 4
#include <android/input.h>
#include <android/keycodes.h>
/* No access to InputManager without going through the JNI
means no way to distinguish between devices generating events */
#else
#define SGAMEPAD_MAX_SUPPORTED_GAMEPADS 0
#endif
typedef struct sgamepad {
sgamepad_gamepad_state gamepad_states[SGAMEPAD_MAX_SUPPORTED_GAMEPADS];
#if defined(__ANDROID__)
sgamepad_gamepad_state transient_gamepad_states[SGAMEPAD_MAX_SUPPORTED_GAMEPADS];
int device_player_mappings[SGAMEPAD_MAX_SUPPORTED_GAMEPADS];
#endif
} sgamepad;
_SOKOL_PRIVATE sgamepad _sgamepad = {0};
/*== Windows Implementation ============================================*/
#if defined (_WIN32)
_SOKOL_PRIVATE HMODULE _hXInputDLL = NULL;
_SOKOL_PRIVATE DWORD (__stdcall* _XInputGetCapabilities)(DWORD, DWORD, XINPUT_CAPABILITIES*) = NULL;
_SOKOL_PRIVATE DWORD (__stdcall* _XInputGetState)(DWORD, XINPUT_STATE*) = NULL;
/// @TODO: _sgamepad_windows_init should be called whenever WM_DEVICECHANGE has
/// been received. Calling, and updating XInputGetState with device indices for
/// unconnected entries has measurable overhead.
_SOKOL_PRIVATE void _sgamepad_windows_init()
{
if (!_hXInputDLL)
{
const wchar_t* dll_names[] = {
L"xinput1_4.dll", // windows 8+
L"xinput1_3.dll", // DXSDK, WinXP
L"xinput9_1_0.dll" // windows 7, vista
};
for (int i = 0; i < 3; ++i)
{
HMODULE dll = LoadLibraryW(dll_names[i]);
if (dll != NULL)
{
_hXInputDLL = dll;
typedef DWORD(__stdcall* fn1)(DWORD, DWORD, XINPUT_CAPABILITIES*);
_XInputGetCapabilities = (fn1)GetProcAddress(dll, "XInputGetCapabilities");
typedef DWORD(__stdcall* fn2)(DWORD, XINPUT_STATE*);
_XInputGetState = (fn2)GetProcAddress(dll, "XInputGetState");
break;
}
}
}
if (_hXInputDLL)
{
for (int i = 0; i < SGAMEPAD_MAX_SUPPORTED_GAMEPADS; i++)
{
XINPUT_CAPABILITIES caps;
_sgamepad.gamepad_states[i].connected = _XInputGetCapabilities(i, XINPUT_FLAG_GAMEPAD, &caps) == ERROR_SUCCESS;
}
}
}
_SOKOL_PRIVATE void _sgamepad_record_state() {
if (!_hXInputDLL)
return;
for (int i = 0; i < SGAMEPAD_MAX_SUPPORTED_GAMEPADS; i++)
{
sgamepad_gamepad_state* target = _sgamepad.gamepad_states + i;
XINPUT_STATE xinput_state;
ZeroMemory(&xinput_state, sizeof(XINPUT_STATE));
if (_XInputGetState(i, &xinput_state) != ERROR_SUCCESS)
{
target->connected = false;
continue;
}
else
{
target->connected = true;
}
WORD xinput_digital_buttons = xinput_state.Gamepad.wButtons;
uint16_t new_flags = 0;
if (xinput_digital_buttons & XINPUT_GAMEPAD_DPAD_UP)
new_flags |= SGAMEPAD_GAMEPAD_DPAD_UP;
if (xinput_digital_buttons & XINPUT_GAMEPAD_DPAD_DOWN)
new_flags |= SGAMEPAD_GAMEPAD_DPAD_DOWN;
if (xinput_digital_buttons & XINPUT_GAMEPAD_DPAD_LEFT)
new_flags |= SGAMEPAD_GAMEPAD_DPAD_LEFT;
if (xinput_digital_buttons & XINPUT_GAMEPAD_DPAD_RIGHT)
new_flags |= SGAMEPAD_GAMEPAD_DPAD_RIGHT;
if (xinput_digital_buttons & XINPUT_GAMEPAD_START)
new_flags |= SGAMEPAD_GAMEPAD_START;
if (xinput_digital_buttons & XINPUT_GAMEPAD_BACK)
new_flags |= SGAMEPAD_GAMEPAD_BACK;
if (xinput_digital_buttons & XINPUT_GAMEPAD_A)
new_flags |= SGAMEPAD_GAMEPAD_A;
if (xinput_digital_buttons & XINPUT_GAMEPAD_B)
new_flags |= SGAMEPAD_GAMEPAD_B;
if (xinput_digital_buttons & XINPUT_GAMEPAD_X)
new_flags |= SGAMEPAD_GAMEPAD_X;
if (xinput_digital_buttons & XINPUT_GAMEPAD_Y)
new_flags |= SGAMEPAD_GAMEPAD_Y;
if (xinput_digital_buttons & XINPUT_GAMEPAD_LEFT_THUMB)
new_flags |= SGAMEPAD_GAMEPAD_LEFT_THUMB;
if (xinput_digital_buttons & XINPUT_GAMEPAD_RIGHT_THUMB)
new_flags |= SGAMEPAD_GAMEPAD_RIGHT_THUMB;
target->digital_inputs = new_flags;
_sgamepad_generate_analog_stick_state(xinput_state.Gamepad.sThumbLX, xinput_state.Gamepad.sThumbLY, SHRT_MAX, XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE, &(target->left_stick));
_sgamepad_generate_analog_stick_state(xinput_state.Gamepad.sThumbRX, xinput_state.Gamepad.sThumbRY, SHRT_MAX, XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE, &(target->right_stick));
target->left_shoulder = (xinput_digital_buttons & XINPUT_GAMEPAD_LEFT_SHOULDER) ? 1.0f : 0.0f;
target->right_shoulder = (xinput_digital_buttons & XINPUT_GAMEPAD_RIGHT_SHOULDER) ? 1.0f : 0.0f;
target->left_trigger = _sgamepad_normalize_analog_trigger(xinput_state.Gamepad.bLeftTrigger, 255, XINPUT_GAMEPAD_TRIGGER_THRESHOLD);
target->right_trigger = _sgamepad_normalize_analog_trigger(xinput_state.Gamepad.bRightTrigger, 255, XINPUT_GAMEPAD_TRIGGER_THRESHOLD);
}
}
/*== Apple Implementation ============================================*/
#elif defined(__APPLE__)
_SOKOL_PRIVATE void _sgamepad_record_state() {
int target_index = 0;
for (GCController* controller in GCController.controllers) {
sgamepad_gamepad_state* target = _sgamepad.gamepad_states + target_index;
GCExtendedGamepad* extended_gamepad = controller.extendedGamepad;
if (extended_gamepad == nil) {
target->connected = false;
continue;
}
target->connected = true;
target_index++;
uint16_t new_flags = 0;
if (extended_gamepad.dpad.up.pressed) {
new_flags |= SGAMEPAD_GAMEPAD_DPAD_UP;
}
if (extended_gamepad.dpad.down.pressed) {
new_flags |= SGAMEPAD_GAMEPAD_DPAD_DOWN;
}
if (extended_gamepad.dpad.left.pressed) {
new_flags |= SGAMEPAD_GAMEPAD_DPAD_LEFT;
}
if (extended_gamepad.dpad.right.pressed) {
new_flags |= SGAMEPAD_GAMEPAD_DPAD_RIGHT;
}
if (extended_gamepad.buttonMenu.pressed) {
new_flags |= SGAMEPAD_GAMEPAD_START;
}
if (extended_gamepad.buttonOptions.pressed) {
new_flags |= SGAMEPAD_GAMEPAD_BACK;
}
if (extended_gamepad.buttonA.pressed) {
new_flags |= SGAMEPAD_GAMEPAD_A;
}
if (extended_gamepad.buttonB.pressed) {
new_flags |= SGAMEPAD_GAMEPAD_B;
}
if (extended_gamepad.buttonX.pressed) {
new_flags |= SGAMEPAD_GAMEPAD_X;
}
if (extended_gamepad.buttonY.pressed) {
new_flags |= SGAMEPAD_GAMEPAD_Y;
}
target->digital_inputs = new_flags;
_sgamepad_generate_analog_stick_state(extended_gamepad.leftThumbstick.xAxis.value, extended_gamepad.leftThumbstick.yAxis.value, 1.0f, 0.01f, &(target->left_stick));
_sgamepad_generate_analog_stick_state(extended_gamepad.rightThumbstick.xAxis.value, extended_gamepad.rightThumbstick.yAxis.value, 1.0f, 0.01f, &(target->right_stick));
target->left_shoulder = extended_gamepad.leftShoulder.value;
target->right_shoulder = extended_gamepad.rightShoulder.value;
target->left_trigger = extended_gamepad.leftTrigger.value;
target->right_trigger = extended_gamepad.rightTrigger.value;
}
++target_index;
for ( ; target_index < SGAMEPAD_MAX_SUPPORTED_GAMEPADS; ++target_index) {
sgamepad_gamepad_state* target = _sgamepad.gamepad_states + target_index;
target->connected = false;
}
}
/*== Android Implementation ============================================*/
#elif defined(__ANDROID__)
_SOKOL_PRIVATE int _sgamepad_android_get_player_id(const AInputEvent* event) {
int32_t source_type = AInputEvent_getSource(event);
if (!(source_type & (AINPUT_SOURCE_DPAD | AINPUT_SOURCE_GAMEPAD | AINPUT_SOURCE_JOYSTICK))) {
return -1;
}
int device_id = AInputEvent_getDeviceId(event);
for (int i=0; i<SGAMEPAD_MAX_SUPPORTED_GAMEPADS; i++) {
if (_sgamepad.device_player_mappings[i] == device_id) {
return i;
}
}
for (int i=0; i<SGAMEPAD_MAX_SUPPORTED_GAMEPADS; i++) {
if (_sgamepad.device_player_mappings[i] == 0) {
_sgamepad.device_player_mappings[i] = device_id;
return i;
}
}
return -1;
}
_SOKOL_PRIVATE bool _sgamepad_android_map_keycode_to_button(int32_t key_code, sgamepad_digital_inputs* out_mapped_input) {
bool mapped = true;
switch (key_code) {
case AKEYCODE_DPAD_UP:
*out_mapped_input = SGAMEPAD_GAMEPAD_DPAD_UP;
break;
case AKEYCODE_DPAD_DOWN:
*out_mapped_input = SGAMEPAD_GAMEPAD_DPAD_DOWN;
break;
case AKEYCODE_DPAD_LEFT:
*out_mapped_input = SGAMEPAD_GAMEPAD_DPAD_LEFT;
break;
case AKEYCODE_DPAD_RIGHT:
*out_mapped_input = SGAMEPAD_GAMEPAD_DPAD_RIGHT;
break;
case AKEYCODE_BUTTON_START:
*out_mapped_input = SGAMEPAD_GAMEPAD_START;
break;
case AKEYCODE_BUTTON_SELECT:
*out_mapped_input = SGAMEPAD_GAMEPAD_BACK;
break;
case AKEYCODE_BUTTON_A:
*out_mapped_input = SGAMEPAD_GAMEPAD_A;
break;
case AKEYCODE_BUTTON_B:
*out_mapped_input = SGAMEPAD_GAMEPAD_B;
break;
case AKEYCODE_BUTTON_X:
*out_mapped_input = SGAMEPAD_GAMEPAD_X;
break;
case AKEYCODE_BUTTON_Y:
*out_mapped_input = SGAMEPAD_GAMEPAD_Y;
break;
case AKEYCODE_BUTTON_THUMBL:
*out_mapped_input = SGAMEPAD_GAMEPAD_LEFT_THUMB;
break;
case AKEYCODE_BUTTON_THUMBR:
*out_mapped_input = SGAMEPAD_GAMEPAD_RIGHT_THUMB;
break;
default:
mapped = false;
break;
}
return mapped;
}
_SOKOL_PRIVATE bool _sgamepad_android_map_keycode_to_shoulder(int32_t key_code, sgamepad_gamepad_state* target, float** out_mapped_input) {
bool mapped = true;
switch (key_code) {
case AKEYCODE_BUTTON_L1:
*out_mapped_input = &(target->left_shoulder);
break;
case AKEYCODE_BUTTON_R1:
*out_mapped_input = &(target->right_shoulder);
break;
default:
mapped = false;
break;
}
return mapped;
}
_SOKOL_PRIVATE bool _sgamepad_android_key_handler(const AInputEvent* event) {
int player_id = _sgamepad_android_get_player_id(event);
if (player_id < 0 || player_id >= SGAMEPAD_MAX_SUPPORTED_GAMEPADS) {
return false;
}
sgamepad_gamepad_state* target = _sgamepad.transient_gamepad_states + player_id;
target->connected = true;
int32_t key_code = AKeyEvent_getKeyCode(event);
int32_t key_action = AKeyEvent_getAction(event);
static sgamepad_digital_inputs mapped_button_input;
if (_sgamepad_android_map_keycode_to_button(key_code, &mapped_button_input)) {
switch (key_action) {
case AKEY_EVENT_ACTION_DOWN:
if(AKeyEvent_getRepeatCount(event) == 0) {
target->digital_inputs |= mapped_button_input;
}
return true;
case AKEY_EVENT_ACTION_UP:
target->digital_inputs &= (~mapped_button_input);
return true;
}
}
static float* mapped_shoulder_input;
if (_sgamepad_android_map_keycode_to_shoulder(key_code, target, &mapped_shoulder_input)) {
switch (key_action) {
case AKEY_EVENT_ACTION_DOWN:
if(AKeyEvent_getRepeatCount(event) == 0) {
*mapped_shoulder_input = 1.0f;
}
return true;
case AKEY_EVENT_ACTION_UP:
*mapped_shoulder_input = 0.0f;
return true;
}
}
return false;
}
_SOKOL_PRIVATE bool _sgamepad_android_motion_handler(const AInputEvent* event) {
int player_id = _sgamepad_android_get_player_id(event);
if (player_id < 0 || player_id >= SGAMEPAD_MAX_SUPPORTED_GAMEPADS) {
return false;
}
if (AMotionEvent_getAction(event) != AMOTION_EVENT_ACTION_MOVE) {
return false;
}
sgamepad_gamepad_state* target = _sgamepad.transient_gamepad_states + player_id;
/* D-Pad mapped to analog */
float d_pad_sample = AMotionEvent_getAxisValue(event, AMOTION_EVENT_AXIS_HAT_X, 0);
if (d_pad_sample < 0) {
target->digital_inputs |= SGAMEPAD_GAMEPAD_DPAD_LEFT;
target->digital_inputs &= (~SGAMEPAD_GAMEPAD_DPAD_RIGHT);
}
else if(d_pad_sample > 0) {
target->digital_inputs |= SGAMEPAD_GAMEPAD_DPAD_RIGHT;
target->digital_inputs &= (~SGAMEPAD_GAMEPAD_DPAD_LEFT);
}
else {
target->digital_inputs &= (~SGAMEPAD_GAMEPAD_DPAD_LEFT);
target->digital_inputs &= (~SGAMEPAD_GAMEPAD_DPAD_RIGHT);
}
d_pad_sample = AMotionEvent_getAxisValue(event, AMOTION_EVENT_AXIS_HAT_Y, 0);
if (d_pad_sample < 0) {
target->digital_inputs |= SGAMEPAD_GAMEPAD_DPAD_UP;
target->digital_inputs &= (~SGAMEPAD_GAMEPAD_DPAD_DOWN);
}
else if(d_pad_sample > 0) {
target->digital_inputs |= SGAMEPAD_GAMEPAD_DPAD_DOWN;
target->digital_inputs &= (~SGAMEPAD_GAMEPAD_DPAD_UP);
}
else {
target->digital_inputs &= (~SGAMEPAD_GAMEPAD_DPAD_UP);
target->digital_inputs &= (~SGAMEPAD_GAMEPAD_DPAD_DOWN);
}
/* Analog sticks */
float stick_x, stick_y;
stick_x = AMotionEvent_getAxisValue(event, AMOTION_EVENT_AXIS_X, 0);
stick_y = AMotionEvent_getAxisValue(event, AMOTION_EVENT_AXIS_Y, 0);
_sgamepad_generate_analog_stick_state(stick_x, -stick_y, 1.0f, 0.01f, &(target->left_stick));
stick_x = AMotionEvent_getAxisValue(event, AMOTION_EVENT_AXIS_Z, 0);
stick_y = AMotionEvent_getAxisValue(event, AMOTION_EVENT_AXIS_RZ, 0);
_sgamepad_generate_analog_stick_state(stick_x, -stick_y, 1.0f, 0.01f, &(target->right_stick));
target->left_trigger = AMotionEvent_getAxisValue(event, AMOTION_EVENT_AXIS_LTRIGGER, 0);
target->right_trigger = AMotionEvent_getAxisValue(event, AMOTION_EVENT_AXIS_RTRIGGER, 0);
return false;
}
_SOKOL_PRIVATE bool _sgamepad_android_input_handler(const AInputEvent* event) {
switch(AInputEvent_getType(event)) {
case AINPUT_EVENT_TYPE_KEY:
return _sgamepad_android_key_handler(event);
case AINPUT_EVENT_TYPE_MOTION:
return _sgamepad_android_motion_handler(event);
default:
return false;
}
}
_SOKOL_PRIVATE void _sgamepad_android_init() {
_sapp.android.gamepad_event_handler = _sgamepad_android_input_handler;
}
_SOKOL_PRIVATE void _sgamepad_record_state() {
for(int i=0; i<SGAMEPAD_MAX_SUPPORTED_GAMEPADS; i++) {
_sgamepad.gamepad_states[i] = _sgamepad.transient_gamepad_states[i];
}
}
/*== Fallback dummy Implementation ============================================*/
#else
_SOKOL_PRIVATE void _sgamepad_record_state() {
}
#endif
/*=== PUBLIC API FUNCTIONS ===================================================*/
SOKOL_API_IMPL unsigned int sgamepad_get_max_supported_gamepads() {
return SGAMEPAD_MAX_SUPPORTED_GAMEPADS;
}
SOKOL_API_IMPL void sgamepad_init() {
#if defined(__ANDROID__)
_sgamepad_android_init();
#elif defined(_WIN32)
_sgamepad_windows_init();
#endif
}
SOKOL_API_IMPL void sgamepad_record_state() {
memset(_sgamepad.gamepad_states, 0, sizeof(_sgamepad.gamepad_states));
_sgamepad_record_state();
}
SOKOL_API_IMPL unsigned int sgamepad_get_gamepad_state(unsigned int index, sgamepad_gamepad_state* pstate) {
if (index < SGAMEPAD_MAX_SUPPORTED_GAMEPADS) {
*pstate = _sgamepad.gamepad_states[index];
return index;
}
return SGAMEPAD_MAX_SUPPORTED_GAMEPADS;
}
SOKOL_API_DECL bool sgamepad_is_connected(unsigned int index) {
if (index >= SGAMEPAD_MAX_SUPPORTED_GAMEPADS)
return false;
return _sgamepad.gamepad_states[index].connected;
}
#endif /* SOKOL_GAMEPAD_IMPL */