src/platform/sdl/sdl-events.c (view raw)
1/* Copyright (c) 2013-2014 Jeffrey Pfau
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6#include "sdl-events.h"
7
8#include "core/input.h"
9#include "core/thread.h"
10#include "debugger/debugger.h"
11#include "gba/input.h"
12#include "gba/io.h"
13#include "gba/rr/rr.h"
14#include "gba/serialize.h"
15#include "gba/video.h"
16#include "gba/renderers/video-software.h"
17#include "util/configuration.h"
18#include "util/formatting.h"
19#include "util/vfs.h"
20
21#if SDL_VERSION_ATLEAST(2, 0, 0) && defined(__APPLE__)
22#define GUI_MOD KMOD_GUI
23#else
24#define GUI_MOD KMOD_CTRL
25#endif
26
27#define GYRO_STEPS 100
28#define RUMBLE_PWM 20
29
30DEFINE_VECTOR(SDL_JoystickList, struct SDL_JoystickCombo);
31
32#if SDL_VERSION_ATLEAST(2, 0, 0)
33static void _mSDLSetRumble(struct mRumble* rumble, int enable);
34#endif
35static int32_t _mSDLReadTiltX(struct mRotationSource* rumble);
36static int32_t _mSDLReadTiltY(struct mRotationSource* rumble);
37static int32_t _mSDLReadGyroZ(struct mRotationSource* rumble);
38static void _mSDLRotationSample(struct mRotationSource* source);
39
40bool mSDLInitEvents(struct mSDLEvents* context) {
41#if SDL_VERSION_ATLEAST(2, 0, 4)
42 SDL_SetHint(SDL_HINT_NO_SIGNAL_HANDLERS, "1");
43#endif
44 if (SDL_InitSubSystem(SDL_INIT_JOYSTICK) < 0) {
45 GBALog(0, GBA_LOG_ERROR, "SDL joystick initialization failed: %s", SDL_GetError());
46 }
47
48#if SDL_VERSION_ATLEAST(2, 0, 0)
49 SDL_SetHint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS, "1");
50 if (SDL_InitSubSystem(SDL_INIT_HAPTIC) < 0) {
51 GBALog(0, GBA_LOG_ERROR, "SDL haptic initialization failed: %s", SDL_GetError());
52 }
53 if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0) {
54 GBALog(0, GBA_LOG_ERROR, "SDL video initialization failed: %s", SDL_GetError());
55 }
56#endif
57
58 SDL_JoystickEventState(SDL_ENABLE);
59 int nJoysticks = SDL_NumJoysticks();
60 SDL_JoystickListInit(&context->joysticks, nJoysticks);
61 if (nJoysticks > 0) {
62 mSDLUpdateJoysticks(context);
63 }
64
65 context->playersAttached = 0;
66
67 size_t i;
68 for (i = 0; i < MAX_PLAYERS; ++i) {
69 context->preferredJoysticks[i] = 0;
70 }
71
72#if !SDL_VERSION_ATLEAST(2, 0, 0)
73 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
74#else
75 context->screensaverSuspendDepth = 0;
76#endif
77 return true;
78}
79
80void mSDLDeinitEvents(struct mSDLEvents* context) {
81 size_t i;
82 for (i = 0; i < SDL_JoystickListSize(&context->joysticks); ++i) {
83 struct SDL_JoystickCombo* joystick = SDL_JoystickListGetPointer(&context->joysticks, i);
84#if SDL_VERSION_ATLEAST(2, 0, 0)
85 SDL_HapticClose(joystick->haptic);
86#endif
87 SDL_JoystickClose(joystick->joystick);
88 }
89 SDL_JoystickListDeinit(&context->joysticks);
90 SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
91}
92
93void mSDLEventsLoadConfig(struct mSDLEvents* context, const struct Configuration* config) {
94 context->preferredJoysticks[0] = mInputGetPreferredDevice(config, "gba", SDL_BINDING_BUTTON, 0);
95 context->preferredJoysticks[1] = mInputGetPreferredDevice(config, "gba", SDL_BINDING_BUTTON, 1);
96 context->preferredJoysticks[2] = mInputGetPreferredDevice(config, "gba", SDL_BINDING_BUTTON, 2);
97 context->preferredJoysticks[3] = mInputGetPreferredDevice(config, "gba", SDL_BINDING_BUTTON, 3);
98}
99
100void mSDLInitBindingsGBA(struct mInputMap* inputMap) {
101#ifdef BUILD_PANDORA
102 mInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_PAGEDOWN, GBA_KEY_A);
103 mInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_END, GBA_KEY_B);
104 mInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_RSHIFT, GBA_KEY_L);
105 mInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_RCTRL, GBA_KEY_R);
106 mInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_LALT, GBA_KEY_START);
107 mInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_LCTRL, GBA_KEY_SELECT);
108 mInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_UP, GBA_KEY_UP);
109 mInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_DOWN, GBA_KEY_DOWN);
110 mInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_LEFT, GBA_KEY_LEFT);
111 mInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_RIGHT, GBA_KEY_RIGHT);
112#elif SDL_VERSION_ATLEAST(2, 0, 0)
113 mInputBindKey(inputMap, SDL_BINDING_KEY, SDL_SCANCODE_X, GBA_KEY_A);
114 mInputBindKey(inputMap, SDL_BINDING_KEY, SDL_SCANCODE_Z, GBA_KEY_B);
115 mInputBindKey(inputMap, SDL_BINDING_KEY, SDL_SCANCODE_A, GBA_KEY_L);
116 mInputBindKey(inputMap, SDL_BINDING_KEY, SDL_SCANCODE_S, GBA_KEY_R);
117 mInputBindKey(inputMap, SDL_BINDING_KEY, SDL_SCANCODE_RETURN, GBA_KEY_START);
118 mInputBindKey(inputMap, SDL_BINDING_KEY, SDL_SCANCODE_BACKSPACE, GBA_KEY_SELECT);
119 mInputBindKey(inputMap, SDL_BINDING_KEY, SDL_SCANCODE_UP, GBA_KEY_UP);
120 mInputBindKey(inputMap, SDL_BINDING_KEY, SDL_SCANCODE_DOWN, GBA_KEY_DOWN);
121 mInputBindKey(inputMap, SDL_BINDING_KEY, SDL_SCANCODE_LEFT, GBA_KEY_LEFT);
122 mInputBindKey(inputMap, SDL_BINDING_KEY, SDL_SCANCODE_RIGHT, GBA_KEY_RIGHT);
123#else
124 mInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_x, GBA_KEY_A);
125 mInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_z, GBA_KEY_B);
126 mInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_a, GBA_KEY_L);
127 mInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_s, GBA_KEY_R);
128 mInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_RETURN, GBA_KEY_START);
129 mInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_BACKSPACE, GBA_KEY_SELECT);
130 mInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_UP, GBA_KEY_UP);
131 mInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_DOWN, GBA_KEY_DOWN);
132 mInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_LEFT, GBA_KEY_LEFT);
133 mInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_RIGHT, GBA_KEY_RIGHT);
134#endif
135
136 mInputBindKey(inputMap, SDL_BINDING_BUTTON, 13, GBA_KEY_A);
137 mInputBindKey(inputMap, SDL_BINDING_BUTTON, 14, GBA_KEY_B);
138 mInputBindKey(inputMap, SDL_BINDING_BUTTON, 10, GBA_KEY_L);
139 mInputBindKey(inputMap, SDL_BINDING_BUTTON, 11, GBA_KEY_R);
140 mInputBindKey(inputMap, SDL_BINDING_BUTTON, 3, GBA_KEY_START);
141 mInputBindKey(inputMap, SDL_BINDING_BUTTON, 0, GBA_KEY_SELECT);
142 mInputBindKey(inputMap, SDL_BINDING_BUTTON, 4, GBA_KEY_UP);
143 mInputBindKey(inputMap, SDL_BINDING_BUTTON, 6, GBA_KEY_DOWN);
144 mInputBindKey(inputMap, SDL_BINDING_BUTTON, 7, GBA_KEY_LEFT);
145 mInputBindKey(inputMap, SDL_BINDING_BUTTON, 5, GBA_KEY_RIGHT);
146
147 struct mInputAxis description = { GBA_KEY_RIGHT, GBA_KEY_LEFT, 0x4000, -0x4000 };
148 mInputBindAxis(inputMap, SDL_BINDING_BUTTON, 0, &description);
149 description = (struct mInputAxis) { GBA_KEY_DOWN, GBA_KEY_UP, 0x4000, -0x4000 };
150 mInputBindAxis(inputMap, SDL_BINDING_BUTTON, 1, &description);
151}
152
153bool mSDLAttachPlayer(struct mSDLEvents* events, struct mSDLPlayer* player) {
154 player->joystick = 0;
155
156 if (events->playersAttached >= MAX_PLAYERS) {
157 return false;
158 }
159
160#if SDL_VERSION_ATLEAST(2, 0, 0)
161 player->rumble.d.setRumble = _mSDLSetRumble;
162 CircleBufferInit(&player->rumble.history, RUMBLE_PWM);
163 player->rumble.level = 0;
164 player->rumble.p = player;
165#endif
166
167 player->rotation.d.readTiltX = _mSDLReadTiltX;
168 player->rotation.d.readTiltY = _mSDLReadTiltY;
169 player->rotation.d.readGyroZ = _mSDLReadGyroZ;
170 player->rotation.d.sample = _mSDLRotationSample;
171 player->rotation.axisX = 2;
172 player->rotation.axisY = 3;
173 player->rotation.gyroSensitivity = 2.2e9f;
174 player->rotation.gyroX = 0;
175 player->rotation.gyroY = 1;
176 player->rotation.zDelta = 0;
177 CircleBufferInit(&player->rotation.zHistory, sizeof(float) * GYRO_STEPS);
178 player->rotation.p = player;
179
180 player->playerId = events->playersAttached;
181 events->players[player->playerId] = player;
182 size_t firstUnclaimed = SIZE_MAX;
183 size_t index = SIZE_MAX;
184
185 size_t i;
186 for (i = 0; i < SDL_JoystickListSize(&events->joysticks); ++i) {
187 bool claimed = false;
188
189 int p;
190 for (p = 0; p < events->playersAttached; ++p) {
191 if (events->players[p]->joystick == SDL_JoystickListGetPointer(&events->joysticks, i)) {
192 claimed = true;
193 break;
194 }
195 }
196 if (claimed) {
197 continue;
198 }
199
200 if (firstUnclaimed == SIZE_MAX) {
201 firstUnclaimed = i;
202 }
203
204 const char* joystickName;
205#if SDL_VERSION_ATLEAST(2, 0, 0)
206 joystickName = SDL_JoystickName(SDL_JoystickListGetPointer(&events->joysticks, i)->joystick);
207#else
208 joystickName = SDL_JoystickName(SDL_JoystickIndex(SDL_JoystickListGetPointer(&events->joysticks, i)->joystick));
209#endif
210 if (events->preferredJoysticks[player->playerId] && strcmp(events->preferredJoysticks[player->playerId], joystickName) == 0) {
211 index = i;
212 break;
213 }
214 }
215
216 if (index == SIZE_MAX && firstUnclaimed != SIZE_MAX) {
217 index = firstUnclaimed;
218 }
219
220 if (index != SIZE_MAX) {
221 player->joystick = SDL_JoystickListGetPointer(&events->joysticks, index);
222
223#if SDL_VERSION_ATLEAST(2, 0, 0)
224 if (player->joystick->haptic) {
225 SDL_HapticRumbleInit(player->joystick->haptic);
226 }
227#endif
228 }
229
230 ++events->playersAttached;
231 return true;
232}
233
234void mSDLDetachPlayer(struct mSDLEvents* events, struct mSDLPlayer* player) {
235 if (player != events->players[player->playerId]) {
236 return;
237 }
238 int i;
239 for (i = player->playerId; i < events->playersAttached; ++i) {
240 if (i + 1 < MAX_PLAYERS) {
241 events->players[i] = events->players[i + 1];
242 }
243 if (i < events->playersAttached - 1) {
244 events->players[i]->playerId = i;
245 }
246 }
247 --events->playersAttached;
248 CircleBufferDeinit(&player->rotation.zHistory);
249}
250
251void mSDLPlayerLoadConfig(struct mSDLPlayer* context, const struct Configuration* config) {
252 mInputMapLoad(context->bindings, SDL_BINDING_KEY, config);
253 if (context->joystick) {
254 mInputMapLoad(context->bindings, SDL_BINDING_BUTTON, config);
255#if SDL_VERSION_ATLEAST(2, 0, 0)
256 const char* name = SDL_JoystickName(context->joystick->joystick);
257#else
258 const char* name = SDL_JoystickName(SDL_JoystickIndex(context->joystick->joystick));
259#endif
260 mInputProfileLoad(context->bindings, SDL_BINDING_BUTTON, config, name);
261
262 const char* value;
263 char* end;
264 int numAxes = SDL_JoystickNumAxes(context->joystick->joystick);
265 int axis;
266 value = mInputGetCustomValue(config, "gba", SDL_BINDING_BUTTON, "tiltAxisX", name);
267 if (value) {
268 axis = strtol(value, &end, 0);
269 if (axis >= 0 && axis < numAxes && end && !*end) {
270 context->rotation.axisX = axis;
271 }
272 }
273 value = mInputGetCustomValue(config, "gba", SDL_BINDING_BUTTON, "tiltAxisY", name);
274 if (value) {
275 axis = strtol(value, &end, 0);
276 if (axis >= 0 && axis < numAxes && end && !*end) {
277 context->rotation.axisY = axis;
278 }
279 }
280 value = mInputGetCustomValue(config, "gba", SDL_BINDING_BUTTON, "gyroAxisX", name);
281 if (value) {
282 axis = strtol(value, &end, 0);
283 if (axis >= 0 && axis < numAxes && end && !*end) {
284 context->rotation.gyroX = axis;
285 }
286 }
287 value = mInputGetCustomValue(config, "gba", SDL_BINDING_BUTTON, "gyroAxisY", name);
288 if (value) {
289 axis = strtol(value, &end, 0);
290 if (axis >= 0 && axis < numAxes && end && !*end) {
291 context->rotation.gyroY = axis;
292 }
293 }
294 value = mInputGetCustomValue(config, "gba", SDL_BINDING_BUTTON, "gyroSensitivity", name);
295 if (value) {
296 float sensitivity = strtof_u(value, &end);
297 if (end && !*end) {
298 context->rotation.gyroSensitivity = sensitivity;
299 }
300 }
301 }
302}
303
304void mSDLPlayerSaveConfig(const struct mSDLPlayer* context, struct Configuration* config) {
305 if (context->joystick) {
306#if SDL_VERSION_ATLEAST(2, 0, 0)
307 const char* name = SDL_JoystickName(context->joystick->joystick);
308#else
309 const char* name = SDL_JoystickName(SDL_JoystickIndex(context->joystick->joystick));
310#endif
311 char value[12];
312 snprintf(value, sizeof(value), "%i", context->rotation.axisX);
313 mInputSetCustomValue(config, "gba", SDL_BINDING_BUTTON, "tiltAxisX", value, name);
314 snprintf(value, sizeof(value), "%i", context->rotation.axisY);
315 mInputSetCustomValue(config, "gba", SDL_BINDING_BUTTON, "tiltAxisY", value, name);
316 snprintf(value, sizeof(value), "%i", context->rotation.gyroX);
317 mInputSetCustomValue(config, "gba", SDL_BINDING_BUTTON, "gyroAxisX", value, name);
318 snprintf(value, sizeof(value), "%i", context->rotation.gyroY);
319 mInputSetCustomValue(config, "gba", SDL_BINDING_BUTTON, "gyroAxisY", value, name);
320 snprintf(value, sizeof(value), "%g", context->rotation.gyroSensitivity);
321 mInputSetCustomValue(config, "gba", SDL_BINDING_BUTTON, "gyroSensitivity", value, name);
322 }
323}
324
325void mSDLPlayerChangeJoystick(struct mSDLEvents* events, struct mSDLPlayer* player, size_t index) {
326 if (player->playerId >= MAX_PLAYERS || index >= SDL_JoystickListSize(&events->joysticks)) {
327 return;
328 }
329 player->joystick = SDL_JoystickListGetPointer(&events->joysticks, index);
330}
331
332void mSDLUpdateJoysticks(struct mSDLEvents* events) {
333 // Pump SDL joystick events without eating the rest of the events
334 SDL_JoystickUpdate();
335#if SDL_VERSION_ATLEAST(2, 0, 0)
336 SDL_Event event;
337 while (SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_JOYDEVICEADDED, SDL_JOYDEVICEREMOVED) > 0) {
338 if (event.type == SDL_JOYDEVICEADDED) {
339 struct SDL_JoystickCombo* joystick = SDL_JoystickListAppend(&events->joysticks);
340 joystick->joystick = SDL_JoystickOpen(event.jdevice.which);
341 joystick->id = SDL_JoystickInstanceID(joystick->joystick);
342 joystick->index = SDL_JoystickListSize(&events->joysticks) - 1;
343#if SDL_VERSION_ATLEAST(2, 0, 0)
344 joystick->haptic = SDL_HapticOpenFromJoystick(joystick->joystick);
345#endif
346 } else if (event.type == SDL_JOYDEVICEREMOVED) {
347 SDL_JoystickID ids[MAX_PLAYERS];
348 size_t i;
349 for (i = 0; (int) i < events->playersAttached; ++i) {
350 if (events->players[i]->joystick) {
351 ids[i] = events->players[i]->joystick->id;
352 events->players[i]->joystick = 0;
353 } else {
354 ids[i] = -1;
355 }
356 }
357 for (i = 0; i < SDL_JoystickListSize(&events->joysticks);) {
358 struct SDL_JoystickCombo* joystick = SDL_JoystickListGetPointer(&events->joysticks, i);
359 if (joystick->id == event.jdevice.which) {
360 SDL_JoystickListShift(&events->joysticks, i, 1);
361 continue;
362 }
363 SDL_JoystickListGetPointer(&events->joysticks, i)->index = i;
364 int p;
365 for (p = 0; p < events->playersAttached; ++p) {
366 if (joystick->id == ids[p]) {
367 events->players[p]->joystick = SDL_JoystickListGetPointer(&events->joysticks, i);
368 }
369 }
370 ++i;
371 }
372 }
373 }
374#endif
375}
376
377static void _pauseAfterFrame(struct mCoreThread* context) {
378 context->frameCallback = 0;
379 mCoreThreadPauseFromThread(context);
380}
381
382static void _mSDLHandleKeypress(struct mCoreThread* context, struct mSDLPlayer* sdlContext, const struct SDL_KeyboardEvent* event) {
383 int key = -1;
384 if (!event->keysym.mod) {
385#if !defined(BUILD_PANDORA) && SDL_VERSION_ATLEAST(2, 0, 0)
386 key = mInputMapKey(sdlContext->bindings, SDL_BINDING_KEY, event->keysym.scancode);
387#else
388 key = mInputMapKey(sdlContext->bindings, SDL_BINDING_KEY, event->keysym.sym);
389#endif
390 }
391 if (key != -1) {
392 if (event->type == SDL_KEYDOWN) {
393 context->core->addKeys(context->core, 1 << key);
394 } else {
395 context->core->clearKeys(context->core, 1 << key);
396 }
397 return;
398 }
399 if (event->keysym.sym == SDLK_TAB) {
400 context->sync.audioWait = event->type != SDL_KEYDOWN;
401 return;
402 }
403 if (event->type == SDL_KEYDOWN) {
404 switch (event->keysym.sym) {
405 case SDLK_F11:
406 // TODO: Put back debugger
407 return;
408#ifdef USE_PNG
409 case SDLK_F12:
410 mCoreTakeScreenshot(context->core);
411 return;
412#endif
413 case SDLK_BACKSLASH:
414 mCoreThreadPause(context);
415 context->frameCallback = _pauseAfterFrame;
416 mCoreThreadUnpause(context);
417 return;
418 case SDLK_BACKQUOTE:
419 // TODO: Put back rewind
420 return;
421#ifdef BUILD_PANDORA
422 case SDLK_ESCAPE:
423 mCoreThreadEnd(context);
424 return;
425#endif
426 default:
427 if ((event->keysym.mod & GUI_MOD) && (event->keysym.mod & GUI_MOD) == event->keysym.mod) {
428 switch (event->keysym.sym) {
429#if SDL_VERSION_ATLEAST(2, 0, 0)
430 case SDLK_f:
431 SDL_SetWindowFullscreen(sdlContext->window, sdlContext->fullscreen ? 0 : SDL_WINDOW_FULLSCREEN_DESKTOP);
432 sdlContext->fullscreen = !sdlContext->fullscreen;
433 sdlContext->windowUpdated = 1;
434 break;
435#endif
436 case SDLK_p:
437 mCoreThreadTogglePause(context);
438 break;
439 case SDLK_n:
440 mCoreThreadPause(context);
441 context->frameCallback = _pauseAfterFrame;
442 mCoreThreadUnpause(context);
443 break;
444 case SDLK_r:
445 mCoreThreadReset(context);
446 break;
447 default:
448 break;
449 }
450 }
451 if (event->keysym.mod & KMOD_SHIFT) {
452 switch (event->keysym.sym) {
453 case SDLK_F1:
454 case SDLK_F2:
455 case SDLK_F3:
456 case SDLK_F4:
457 case SDLK_F5:
458 case SDLK_F6:
459 case SDLK_F7:
460 case SDLK_F8:
461 case SDLK_F9:
462 mCoreThreadInterrupt(context);
463 mCoreSaveState(context->core, event->keysym.sym - SDLK_F1 + 1, SAVESTATE_SCREENSHOT);
464 mCoreThreadContinue(context);
465 break;
466 default:
467 break;
468 }
469 } else {
470 switch (event->keysym.sym) {
471 case SDLK_F1:
472 case SDLK_F2:
473 case SDLK_F3:
474 case SDLK_F4:
475 case SDLK_F5:
476 case SDLK_F6:
477 case SDLK_F7:
478 case SDLK_F8:
479 case SDLK_F9:
480 mCoreThreadInterrupt(context);
481 mCoreLoadState(context->core, event->keysym.sym - SDLK_F1 + 1, SAVESTATE_SCREENSHOT);
482 mCoreThreadContinue(context);
483 break;
484 default:
485 break;
486 }
487 }
488 return;
489 }
490 }
491}
492
493static void _mSDLHandleJoyButton(struct mCore* core, struct mSDLPlayer* sdlContext, const struct SDL_JoyButtonEvent* event) {
494 int key = 0;
495 key = mInputMapKey(sdlContext->bindings, SDL_BINDING_BUTTON, event->button);
496 if (key == -1) {
497 return;
498 }
499
500 if (event->type == SDL_JOYBUTTONDOWN) {
501 core->addKeys(core, 1 << key);
502 } else {
503 core->clearKeys(core, 1 << key);
504 }
505}
506
507static void _mSDLHandleJoyAxis(struct mCore* core, struct mSDLPlayer* sdlContext, const struct SDL_JoyAxisEvent* event) {
508 int clearKeys = ~mInputClearAxis(sdlContext->bindings, SDL_BINDING_BUTTON, event->axis, -1);
509 int newKeys = 0;
510 int key = mInputMapAxis(sdlContext->bindings, SDL_BINDING_BUTTON, event->axis, event->value);
511 if (key != -1) {
512 newKeys |= 1 << key;
513 }
514 clearKeys &= ~newKeys;
515 core->clearKeys(core, clearKeys);
516 core->addKeys(core, newKeys);
517
518}
519
520#if SDL_VERSION_ATLEAST(2, 0, 0)
521static void _mSDLHandleWindowEvent(struct mSDLPlayer* sdlContext, const struct SDL_WindowEvent* event) {
522 switch (event->event) {
523 case SDL_WINDOWEVENT_SIZE_CHANGED:
524 sdlContext->windowUpdated = 1;
525 break;
526 }
527}
528#endif
529
530void mSDLHandleEvent(struct mCoreThread* context, struct mSDLPlayer* sdlContext, const union SDL_Event* event) {
531 switch (event->type) {
532 case SDL_QUIT:
533 mCoreThreadEnd(context);
534 break;
535#if SDL_VERSION_ATLEAST(2, 0, 0)
536 case SDL_WINDOWEVENT:
537 _mSDLHandleWindowEvent(sdlContext, &event->window);
538 break;
539#endif
540 case SDL_KEYDOWN:
541 case SDL_KEYUP:
542 _mSDLHandleKeypress(context, sdlContext, &event->key);
543 break;
544 case SDL_JOYBUTTONDOWN:
545 case SDL_JOYBUTTONUP:
546 _mSDLHandleJoyButton(context->core, sdlContext, &event->jbutton);
547 break;
548 case SDL_JOYHATMOTION:
549 // TODO
550 break;
551 case SDL_JOYAXISMOTION:
552 _mSDLHandleJoyAxis(context->core, sdlContext, &event->jaxis);
553 break;
554 }
555}
556
557#if SDL_VERSION_ATLEAST(2, 0, 0)
558static void _mSDLSetRumble(struct mRumble* rumble, int enable) {
559 struct mSDLRumble* sdlRumble = (struct mSDLRumble*) rumble;
560 if (!sdlRumble->p->joystick->haptic || !SDL_HapticRumbleSupported(sdlRumble->p->joystick->haptic)) {
561 return;
562 }
563 sdlRumble->level += enable;
564 if (CircleBufferSize(&sdlRumble->history) == RUMBLE_PWM) {
565 int8_t oldLevel;
566 CircleBufferRead8(&sdlRumble->history, &oldLevel);
567 sdlRumble->level -= oldLevel;
568 }
569 CircleBufferWrite8(&sdlRumble->history, enable);
570 if (sdlRumble->level) {
571 SDL_HapticRumblePlay(sdlRumble->p->joystick->haptic, sdlRumble->level / (float) RUMBLE_PWM, 20);
572 } else {
573 SDL_HapticRumbleStop(sdlRumble->p->joystick->haptic);
574 }
575}
576#endif
577
578static int32_t _readTilt(struct mSDLPlayer* player, int axis) {
579 if (!player->joystick) {
580 return 0;
581 }
582 return SDL_JoystickGetAxis(player->joystick->joystick, axis) * 0x3800;
583}
584
585static int32_t _mSDLReadTiltX(struct mRotationSource* source) {
586 struct mSDLRotation* rotation = (struct mSDLRotation*) source;
587 return _readTilt(rotation->p, rotation->axisX);
588}
589
590static int32_t _mSDLReadTiltY(struct mRotationSource* source) {
591 struct mSDLRotation* rotation = (struct mSDLRotation*) source;
592 return _readTilt(rotation->p, rotation->axisY);
593}
594
595static int32_t _mSDLReadGyroZ(struct mRotationSource* source) {
596 struct mSDLRotation* rotation = (struct mSDLRotation*) source;
597 float z = rotation->zDelta;
598 return z * rotation->gyroSensitivity;
599}
600
601static void _mSDLRotationSample(struct mRotationSource* source) {
602 struct mSDLRotation* rotation = (struct mSDLRotation*) source;
603 SDL_JoystickUpdate();
604 if (!rotation->p->joystick) {
605 return;
606 }
607
608 int x = SDL_JoystickGetAxis(rotation->p->joystick->joystick, rotation->gyroX);
609 int y = SDL_JoystickGetAxis(rotation->p->joystick->joystick, rotation->gyroY);
610 union {
611 float f;
612 int32_t i;
613 } theta = { .f = atan2f(y, x) - atan2f(rotation->oldY, rotation->oldX) };
614 if (isnan(theta.f)) {
615 theta.f = 0.0f;
616 } else if (theta.f > M_PI) {
617 theta.f -= 2.0f * M_PI;
618 } else if (theta.f < -M_PI) {
619 theta.f += 2.0f * M_PI;
620 }
621 rotation->oldX = x;
622 rotation->oldY = y;
623
624 float oldZ = 0;
625 if (CircleBufferSize(&rotation->zHistory) == GYRO_STEPS * sizeof(float)) {
626 CircleBufferRead32(&rotation->zHistory, (int32_t*) &oldZ);
627 }
628 CircleBufferWrite32(&rotation->zHistory, theta.i);
629 rotation->zDelta += theta.f - oldZ;
630}
631
632#if SDL_VERSION_ATLEAST(2, 0, 0)
633void mSDLSuspendScreensaver(struct mSDLEvents* events) {
634 if (events->screensaverSuspendDepth == 0 && events->screensaverSuspendable) {
635 SDL_DisableScreenSaver();
636 }
637 ++events->screensaverSuspendDepth;
638}
639
640void mSDLResumeScreensaver(struct mSDLEvents* events) {
641 --events->screensaverSuspendDepth;
642 if (events->screensaverSuspendDepth == 0 && events->screensaverSuspendable) {
643 SDL_EnableScreenSaver();
644 }
645}
646
647void mSDLSetScreensaverSuspendable(struct mSDLEvents* events, bool suspendable) {
648 bool wasSuspendable = events->screensaverSuspendable;
649 events->screensaverSuspendable = suspendable;
650 if (events->screensaverSuspendDepth > 0) {
651 if (suspendable && !wasSuspendable) {
652 SDL_DisableScreenSaver();
653 } else if (!suspendable && wasSuspendable) {
654 SDL_EnableScreenSaver();
655 }
656 } else {
657 SDL_EnableScreenSaver();
658 }
659}
660#endif