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 <mgba/core/core.h>
9#include <mgba/core/input.h>
10#include <mgba/core/serialize.h>
11#include <mgba/core/thread.h>
12#include <mgba/debugger/debugger.h>
13#include <mgba/internal/gba/input.h>
14#include <mgba-util/configuration.h>
15#include <mgba-util/formatting.h>
16#include <mgba-util/vfs.h>
17
18#if SDL_VERSION_ATLEAST(2, 0, 0) && defined(__APPLE__)
19#define GUI_MOD KMOD_GUI
20#else
21#define GUI_MOD KMOD_CTRL
22#endif
23
24#define GYRO_STEPS 100
25#define RUMBLE_PWM 16
26#define RUMBLE_STEPS 2
27
28mLOG_DEFINE_CATEGORY(SDL_EVENTS, "SDL Events", "platform.sdl.events");
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 mLOG(SDL_EVENTS, 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 mLOG(SDL_EVENTS, ERROR, "SDL haptic initialization failed: %s", SDL_GetError());
52 }
53 if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0) {
54 mLOG(SDL_EVENTS, 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, NULL);
63 // Some OSes don't do hotplug detection
64 if (!SDL_JoystickListSize(&context->joysticks)) {
65 int i;
66 for (i = 0; i < nJoysticks; ++i) {
67 SDL_Joystick* sdlJoystick = SDL_JoystickOpen(i);
68 if (!sdlJoystick) {
69 continue;
70 }
71 struct SDL_JoystickCombo* joystick = SDL_JoystickListAppend(&context->joysticks);
72 joystick->joystick = sdlJoystick;
73 joystick->index = SDL_JoystickListSize(&context->joysticks) - 1;
74#if SDL_VERSION_ATLEAST(2, 0, 0)
75 joystick->id = SDL_JoystickInstanceID(joystick->joystick);
76 joystick->haptic = SDL_HapticOpenFromJoystick(joystick->joystick);
77#else
78 joystick->id = SDL_JoystickIndex(joystick->joystick);
79#endif
80 }
81 }
82 }
83
84 context->playersAttached = 0;
85
86 size_t i;
87 for (i = 0; i < MAX_PLAYERS; ++i) {
88 context->preferredJoysticks[i] = 0;
89 }
90
91#if !SDL_VERSION_ATLEAST(2, 0, 0)
92 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
93#else
94 context->screensaverSuspendDepth = 0;
95#endif
96 return true;
97}
98
99void mSDLDeinitEvents(struct mSDLEvents* context) {
100 size_t i;
101 for (i = 0; i < SDL_JoystickListSize(&context->joysticks); ++i) {
102 struct SDL_JoystickCombo* joystick = SDL_JoystickListGetPointer(&context->joysticks, i);
103#if SDL_VERSION_ATLEAST(2, 0, 0)
104 SDL_HapticClose(joystick->haptic);
105#endif
106 SDL_JoystickClose(joystick->joystick);
107 }
108 SDL_JoystickListDeinit(&context->joysticks);
109 SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
110}
111
112void mSDLEventsLoadConfig(struct mSDLEvents* context, const struct Configuration* config) {
113 context->preferredJoysticks[0] = mInputGetPreferredDevice(config, "gba", SDL_BINDING_BUTTON, 0);
114 context->preferredJoysticks[1] = mInputGetPreferredDevice(config, "gba", SDL_BINDING_BUTTON, 1);
115 context->preferredJoysticks[2] = mInputGetPreferredDevice(config, "gba", SDL_BINDING_BUTTON, 2);
116 context->preferredJoysticks[3] = mInputGetPreferredDevice(config, "gba", SDL_BINDING_BUTTON, 3);
117}
118
119void mSDLInitBindingsGBA(struct mInputMap* inputMap) {
120#ifdef BUILD_PANDORA
121 mInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_PAGEDOWN, GBA_KEY_A);
122 mInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_END, GBA_KEY_B);
123 mInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_RSHIFT, GBA_KEY_L);
124 mInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_RCTRL, GBA_KEY_R);
125 mInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_LALT, GBA_KEY_START);
126 mInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_LCTRL, GBA_KEY_SELECT);
127 mInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_UP, GBA_KEY_UP);
128 mInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_DOWN, GBA_KEY_DOWN);
129 mInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_LEFT, GBA_KEY_LEFT);
130 mInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_RIGHT, GBA_KEY_RIGHT);
131#else
132 mInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_x, GBA_KEY_A);
133 mInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_z, GBA_KEY_B);
134 mInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_a, GBA_KEY_L);
135 mInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_s, GBA_KEY_R);
136 mInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_RETURN, GBA_KEY_START);
137 mInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_BACKSPACE, GBA_KEY_SELECT);
138 mInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_UP, GBA_KEY_UP);
139 mInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_DOWN, GBA_KEY_DOWN);
140 mInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_LEFT, GBA_KEY_LEFT);
141 mInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_RIGHT, GBA_KEY_RIGHT);
142#endif
143
144 struct mInputAxis description = { GBA_KEY_RIGHT, GBA_KEY_LEFT, 0x4000, -0x4000 };
145 mInputBindAxis(inputMap, SDL_BINDING_BUTTON, 0, &description);
146 description = (struct mInputAxis) { GBA_KEY_DOWN, GBA_KEY_UP, 0x4000, -0x4000 };
147 mInputBindAxis(inputMap, SDL_BINDING_BUTTON, 1, &description);
148
149 mInputBindHat(inputMap, SDL_BINDING_BUTTON, 0, &GBAInputInfo.hat);
150}
151
152bool mSDLAttachPlayer(struct mSDLEvents* events, struct mSDLPlayer* player) {
153 player->joystick = 0;
154
155 if (events->playersAttached >= MAX_PLAYERS) {
156 return false;
157 }
158
159#if SDL_VERSION_ATLEAST(2, 0, 0)
160 player->rumble.d.setRumble = _mSDLSetRumble;
161 CircleBufferInit(&player->rumble.history, RUMBLE_PWM);
162 player->rumble.level = 0;
163 player->rumble.activeLevel = 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 (!joystickName) {
211 continue;
212 }
213 if (events->preferredJoysticks[player->playerId] && strcmp(events->preferredJoysticks[player->playerId], joystickName) == 0) {
214 index = i;
215 break;
216 }
217 }
218
219 if (index == SIZE_MAX && firstUnclaimed != SIZE_MAX) {
220 index = firstUnclaimed;
221 }
222
223 if (index != SIZE_MAX) {
224 player->joystick = SDL_JoystickListGetPointer(&events->joysticks, index);
225
226#if SDL_VERSION_ATLEAST(2, 0, 0)
227 if (player->joystick->haptic) {
228 SDL_HapticRumbleInit(player->joystick->haptic);
229 }
230#endif
231 }
232
233 ++events->playersAttached;
234 return true;
235}
236
237void mSDLDetachPlayer(struct mSDLEvents* events, struct mSDLPlayer* player) {
238 if (player != events->players[player->playerId]) {
239 return;
240 }
241 int i;
242 for (i = player->playerId; i < events->playersAttached; ++i) {
243 if (i + 1 < MAX_PLAYERS) {
244 events->players[i] = events->players[i + 1];
245 }
246 if (i < events->playersAttached - 1) {
247 events->players[i]->playerId = i;
248 }
249 }
250 --events->playersAttached;
251 CircleBufferDeinit(&player->rotation.zHistory);
252}
253
254void mSDLPlayerLoadConfig(struct mSDLPlayer* context, const struct Configuration* config) {
255 mInputMapLoad(context->bindings, SDL_BINDING_KEY, config);
256 if (context->joystick) {
257 mInputMapLoad(context->bindings, SDL_BINDING_BUTTON, config);
258#if SDL_VERSION_ATLEAST(2, 0, 0)
259 const char* name = SDL_JoystickName(context->joystick->joystick);
260#else
261 const char* name = SDL_JoystickName(SDL_JoystickIndex(context->joystick->joystick));
262#endif
263 if (!name) {
264 return;
265 }
266 mInputProfileLoad(context->bindings, SDL_BINDING_BUTTON, config, name);
267
268 const char* value;
269 char* end;
270 int numAxes = SDL_JoystickNumAxes(context->joystick->joystick);
271 int axis;
272 value = mInputGetCustomValue(config, "gba", SDL_BINDING_BUTTON, "tiltAxisX", name);
273 if (value) {
274 axis = strtol(value, &end, 0);
275 if (axis >= 0 && axis < numAxes && end && !*end) {
276 context->rotation.axisX = axis;
277 }
278 }
279 value = mInputGetCustomValue(config, "gba", SDL_BINDING_BUTTON, "tiltAxisY", name);
280 if (value) {
281 axis = strtol(value, &end, 0);
282 if (axis >= 0 && axis < numAxes && end && !*end) {
283 context->rotation.axisY = axis;
284 }
285 }
286 value = mInputGetCustomValue(config, "gba", SDL_BINDING_BUTTON, "gyroAxisX", name);
287 if (value) {
288 axis = strtol(value, &end, 0);
289 if (axis >= 0 && axis < numAxes && end && !*end) {
290 context->rotation.gyroX = axis;
291 }
292 }
293 value = mInputGetCustomValue(config, "gba", SDL_BINDING_BUTTON, "gyroAxisY", name);
294 if (value) {
295 axis = strtol(value, &end, 0);
296 if (axis >= 0 && axis < numAxes && end && !*end) {
297 context->rotation.gyroY = axis;
298 }
299 }
300 value = mInputGetCustomValue(config, "gba", SDL_BINDING_BUTTON, "gyroSensitivity", name);
301 if (value) {
302 float sensitivity = strtof_u(value, &end);
303 if (end && !*end) {
304 context->rotation.gyroSensitivity = sensitivity;
305 }
306 }
307 }
308}
309
310void mSDLPlayerSaveConfig(const struct mSDLPlayer* context, struct Configuration* config) {
311 if (context->joystick) {
312#if SDL_VERSION_ATLEAST(2, 0, 0)
313 const char* name = SDL_JoystickName(context->joystick->joystick);
314#else
315 const char* name = SDL_JoystickName(SDL_JoystickIndex(context->joystick->joystick));
316#endif
317 if (!name) {
318 return;
319 }
320 char value[16];
321 snprintf(value, sizeof(value), "%i", context->rotation.axisX);
322 mInputSetCustomValue(config, "gba", SDL_BINDING_BUTTON, "tiltAxisX", value, name);
323 snprintf(value, sizeof(value), "%i", context->rotation.axisY);
324 mInputSetCustomValue(config, "gba", SDL_BINDING_BUTTON, "tiltAxisY", value, name);
325 snprintf(value, sizeof(value), "%i", context->rotation.gyroX);
326 mInputSetCustomValue(config, "gba", SDL_BINDING_BUTTON, "gyroAxisX", value, name);
327 snprintf(value, sizeof(value), "%i", context->rotation.gyroY);
328 mInputSetCustomValue(config, "gba", SDL_BINDING_BUTTON, "gyroAxisY", value, name);
329 snprintf(value, sizeof(value), "%g", context->rotation.gyroSensitivity);
330 mInputSetCustomValue(config, "gba", SDL_BINDING_BUTTON, "gyroSensitivity", value, name);
331 }
332}
333
334void mSDLPlayerChangeJoystick(struct mSDLEvents* events, struct mSDLPlayer* player, size_t index) {
335 if (player->playerId >= MAX_PLAYERS || index >= SDL_JoystickListSize(&events->joysticks)) {
336 return;
337 }
338 player->joystick = SDL_JoystickListGetPointer(&events->joysticks, index);
339}
340
341void mSDLUpdateJoysticks(struct mSDLEvents* events, const struct Configuration* config) {
342 // Pump SDL joystick events without eating the rest of the events
343 SDL_JoystickUpdate();
344#if SDL_VERSION_ATLEAST(2, 0, 0)
345 SDL_Event event;
346 while (SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_JOYDEVICEADDED, SDL_JOYDEVICEREMOVED) > 0) {
347 if (event.type == SDL_JOYDEVICEADDED) {
348 SDL_Joystick* sdlJoystick = SDL_JoystickOpen(event.jdevice.which);
349 if (!sdlJoystick) {
350 continue;
351 }
352 struct SDL_JoystickCombo* joystick = SDL_JoystickListAppend(&events->joysticks);
353 joystick->joystick = sdlJoystick;
354 joystick->id = SDL_JoystickInstanceID(joystick->joystick);
355 joystick->index = SDL_JoystickListSize(&events->joysticks) - 1;
356#if SDL_VERSION_ATLEAST(2, 0, 0)
357 joystick->haptic = SDL_HapticOpenFromJoystick(joystick->joystick);
358#endif
359
360 const char* joystickName;
361#if SDL_VERSION_ATLEAST(2, 0, 0)
362 joystickName = SDL_JoystickName(joystick->joystick);
363#else
364 joystickName = SDL_JoystickName(SDL_JoystickIndex(joystick->joystick));
365#endif
366 size_t i;
367 if (joystickName) {
368 for (i = 0; (int) i < events->playersAttached; ++i) {
369 if (events->players[i]->joystick) {
370 continue;
371 }
372 if (events->preferredJoysticks[i] && strcmp(events->preferredJoysticks[i], joystickName) == 0) {
373 events->players[i]->joystick = joystick;
374 if (config) {
375 mInputProfileLoad(events->players[i]->bindings, SDL_BINDING_BUTTON, config, joystickName);
376 }
377 return;
378 }
379 }
380 }
381 for (i = 0; (int) i < events->playersAttached; ++i) {
382 if (events->players[i]->joystick) {
383 continue;
384 }
385 events->players[i]->joystick = joystick;
386 if (config && joystickName) {
387 mInputProfileLoad(events->players[i]->bindings, SDL_BINDING_BUTTON, config, joystickName);
388 }
389 break;
390 }
391 } else if (event.type == SDL_JOYDEVICEREMOVED) {
392 SDL_JoystickID ids[MAX_PLAYERS] = { 0 };
393 size_t i;
394 for (i = 0; (int) i < events->playersAttached; ++i) {
395 if (events->players[i]->joystick) {
396 ids[i] = events->players[i]->joystick->id;
397 events->players[i]->joystick = 0;
398 } else {
399 ids[i] = -1;
400 }
401 }
402 for (i = 0; i < SDL_JoystickListSize(&events->joysticks);) {
403 struct SDL_JoystickCombo* joystick = SDL_JoystickListGetPointer(&events->joysticks, i);
404 if (joystick->id == event.jdevice.which) {
405 SDL_JoystickListShift(&events->joysticks, i, 1);
406 continue;
407 }
408 SDL_JoystickListGetPointer(&events->joysticks, i)->index = i;
409 int p;
410 for (p = 0; p < events->playersAttached; ++p) {
411 if (joystick->id == ids[p]) {
412 events->players[p]->joystick = SDL_JoystickListGetPointer(&events->joysticks, i);
413 }
414 }
415 ++i;
416 }
417 }
418 }
419#endif
420}
421
422static void _pauseAfterFrame(struct mCoreThread* context) {
423 context->frameCallback = 0;
424 mCoreThreadPauseFromThread(context);
425}
426
427static void _mSDLHandleKeypress(struct mCoreThread* context, struct mSDLPlayer* sdlContext, const struct SDL_KeyboardEvent* event) {
428 int key = -1;
429 if (!(event->keysym.mod & ~(KMOD_NUM | KMOD_CAPS))) {
430 key = mInputMapKey(sdlContext->bindings, SDL_BINDING_KEY, event->keysym.sym);
431 }
432 if (key != -1) {
433 mCoreThreadInterrupt(context);
434 if (event->type == SDL_KEYDOWN) {
435 context->core->addKeys(context->core, 1 << key);
436 } else {
437 context->core->clearKeys(context->core, 1 << key);
438 }
439 mCoreThreadContinue(context);
440 return;
441 }
442 if (event->keysym.sym == SDLK_TAB) {
443 context->impl->sync.audioWait = event->type != SDL_KEYDOWN;
444 return;
445 }
446 if (event->keysym.sym == SDLK_BACKQUOTE) {
447 mCoreThreadSetRewinding(context, event->type == SDL_KEYDOWN);
448 }
449 if (event->type == SDL_KEYDOWN) {
450 switch (event->keysym.sym) {
451#ifdef USE_DEBUGGERS
452 case SDLK_F11:
453 if (context->core->debugger) {
454 mDebuggerEnter(context->core->debugger, DEBUGGER_ENTER_MANUAL, NULL);
455 }
456 return;
457#endif
458#ifdef USE_PNG
459 case SDLK_F12:
460 mCoreTakeScreenshot(context->core);
461 return;
462#endif
463 case SDLK_BACKSLASH:
464 mCoreThreadPause(context);
465 context->frameCallback = _pauseAfterFrame;
466 mCoreThreadUnpause(context);
467 return;
468#ifdef BUILD_PANDORA
469 case SDLK_ESCAPE:
470 mCoreThreadEnd(context);
471 return;
472#endif
473 default:
474 if ((event->keysym.mod & GUI_MOD) && (event->keysym.mod & GUI_MOD) == event->keysym.mod) {
475 switch (event->keysym.sym) {
476#if SDL_VERSION_ATLEAST(2, 0, 0)
477 case SDLK_f:
478 SDL_SetWindowFullscreen(sdlContext->window, sdlContext->fullscreen ? 0 : SDL_WINDOW_FULLSCREEN_DESKTOP);
479 sdlContext->fullscreen = !sdlContext->fullscreen;
480 sdlContext->windowUpdated = 1;
481 break;
482#endif
483 case SDLK_p:
484 mCoreThreadTogglePause(context);
485 break;
486 case SDLK_n:
487 mCoreThreadPause(context);
488 context->frameCallback = _pauseAfterFrame;
489 mCoreThreadUnpause(context);
490 break;
491 case SDLK_r:
492 mCoreThreadReset(context);
493 break;
494 default:
495 break;
496 }
497 }
498 if (event->keysym.mod & KMOD_SHIFT) {
499 switch (event->keysym.sym) {
500 case SDLK_F1:
501 case SDLK_F2:
502 case SDLK_F3:
503 case SDLK_F4:
504 case SDLK_F5:
505 case SDLK_F6:
506 case SDLK_F7:
507 case SDLK_F8:
508 case SDLK_F9:
509 mCoreThreadInterrupt(context);
510 mCoreSaveState(context->core, event->keysym.sym - SDLK_F1 + 1, SAVESTATE_SAVEDATA | SAVESTATE_SCREENSHOT | SAVESTATE_RTC);
511 mCoreThreadContinue(context);
512 break;
513 default:
514 break;
515 }
516 } else {
517 switch (event->keysym.sym) {
518 case SDLK_F1:
519 case SDLK_F2:
520 case SDLK_F3:
521 case SDLK_F4:
522 case SDLK_F5:
523 case SDLK_F6:
524 case SDLK_F7:
525 case SDLK_F8:
526 case SDLK_F9:
527 mCoreThreadInterrupt(context);
528 mCoreLoadState(context->core, event->keysym.sym - SDLK_F1 + 1, SAVESTATE_SCREENSHOT | SAVESTATE_RTC);
529 mCoreThreadContinue(context);
530 break;
531 default:
532 break;
533 }
534 }
535 return;
536 }
537 }
538}
539
540static void _mSDLHandleJoyButton(struct mCoreThread* context, struct mSDLPlayer* sdlContext, const struct SDL_JoyButtonEvent* event) {
541 int key = 0;
542 key = mInputMapKey(sdlContext->bindings, SDL_BINDING_BUTTON, event->button);
543 if (key == -1) {
544 return;
545 }
546
547 mCoreThreadInterrupt(context);
548 if (event->type == SDL_JOYBUTTONDOWN) {
549 context->core->addKeys(context->core, 1 << key);
550 } else {
551 context->core->clearKeys(context->core, 1 << key);
552 }
553 mCoreThreadContinue(context);
554}
555
556static void _mSDLHandleJoyHat(struct mCoreThread* context, struct mSDLPlayer* sdlContext, const struct SDL_JoyHatEvent* event) {
557 int allKeys = mInputMapHat(sdlContext->bindings, SDL_BINDING_BUTTON, event->hat, -1);
558 if (allKeys == 0) {
559 return;
560 }
561
562 int keys = mInputMapHat(sdlContext->bindings, SDL_BINDING_BUTTON, event->hat, event->value);
563
564 mCoreThreadInterrupt(context);
565 context->core->clearKeys(context->core, allKeys ^ keys);
566 context->core->addKeys(context->core, keys);
567 mCoreThreadContinue(context);
568}
569
570static void _mSDLHandleJoyAxis(struct mCoreThread* context, struct mSDLPlayer* sdlContext, const struct SDL_JoyAxisEvent* event) {
571 int clearKeys = ~mInputClearAxis(sdlContext->bindings, SDL_BINDING_BUTTON, event->axis, -1);
572 int newKeys = 0;
573 int key = mInputMapAxis(sdlContext->bindings, SDL_BINDING_BUTTON, event->axis, event->value);
574 if (key != -1) {
575 newKeys |= 1 << key;
576 }
577 clearKeys &= ~newKeys;
578 mCoreThreadInterrupt(context);
579 context->core->clearKeys(context->core, clearKeys);
580 context->core->addKeys(context->core, newKeys);
581 mCoreThreadContinue(context);
582
583}
584
585#if SDL_VERSION_ATLEAST(2, 0, 0)
586static void _mSDLHandleWindowEvent(struct mSDLPlayer* sdlContext, const struct SDL_WindowEvent* event) {
587 switch (event->event) {
588 case SDL_WINDOWEVENT_SIZE_CHANGED:
589 sdlContext->windowUpdated = 1;
590 break;
591 }
592}
593#endif
594
595void mSDLHandleEvent(struct mCoreThread* context, struct mSDLPlayer* sdlContext, const union SDL_Event* event) {
596 switch (event->type) {
597 case SDL_QUIT:
598 mCoreThreadEnd(context);
599 break;
600#if SDL_VERSION_ATLEAST(2, 0, 0)
601 case SDL_WINDOWEVENT:
602 _mSDLHandleWindowEvent(sdlContext, &event->window);
603 break;
604#endif
605 case SDL_KEYDOWN:
606 case SDL_KEYUP:
607 _mSDLHandleKeypress(context, sdlContext, &event->key);
608 break;
609 case SDL_JOYBUTTONDOWN:
610 case SDL_JOYBUTTONUP:
611 _mSDLHandleJoyButton(context, sdlContext, &event->jbutton);
612 break;
613 case SDL_JOYHATMOTION:
614 _mSDLHandleJoyHat(context, sdlContext, &event->jhat);
615 break;
616 case SDL_JOYAXISMOTION:
617 _mSDLHandleJoyAxis(context, sdlContext, &event->jaxis);
618 break;
619 }
620}
621
622#if SDL_VERSION_ATLEAST(2, 0, 0)
623static void _mSDLSetRumble(struct mRumble* rumble, int enable) {
624 struct mSDLRumble* sdlRumble = (struct mSDLRumble*) rumble;
625 if (!sdlRumble->p->joystick || !sdlRumble->p->joystick->haptic || !SDL_HapticRumbleSupported(sdlRumble->p->joystick->haptic)) {
626 return;
627 }
628 int8_t originalLevel = sdlRumble->level;
629 sdlRumble->level += enable;
630 if (CircleBufferSize(&sdlRumble->history) == RUMBLE_PWM) {
631 int8_t oldLevel;
632 CircleBufferRead8(&sdlRumble->history, &oldLevel);
633 sdlRumble->level -= oldLevel;
634 }
635 CircleBufferWrite8(&sdlRumble->history, enable);
636 if (sdlRumble->level == originalLevel) {
637 return;
638 }
639 float activeLevel = ceil(RUMBLE_STEPS * sdlRumble->level / (float) RUMBLE_PWM) / RUMBLE_STEPS;
640 if (fabsf(sdlRumble->activeLevel - activeLevel) < 0.75 / RUMBLE_STEPS) {
641 return;
642 }
643 sdlRumble->activeLevel = activeLevel;
644 if (sdlRumble->activeLevel > 0.5 / RUMBLE_STEPS) {
645 SDL_HapticRumbleStop(sdlRumble->p->joystick->haptic);
646 SDL_HapticRumblePlay(sdlRumble->p->joystick->haptic, activeLevel, 500);
647 } else {
648 SDL_HapticRumbleStop(sdlRumble->p->joystick->haptic);
649 }
650}
651#endif
652
653static int32_t _readTilt(struct mSDLPlayer* player, int axis) {
654 if (!player->joystick) {
655 return 0;
656 }
657 return SDL_JoystickGetAxis(player->joystick->joystick, axis) * 0x3800;
658}
659
660static int32_t _mSDLReadTiltX(struct mRotationSource* source) {
661 struct mSDLRotation* rotation = (struct mSDLRotation*) source;
662 return _readTilt(rotation->p, rotation->axisX);
663}
664
665static int32_t _mSDLReadTiltY(struct mRotationSource* source) {
666 struct mSDLRotation* rotation = (struct mSDLRotation*) source;
667 return _readTilt(rotation->p, rotation->axisY);
668}
669
670static int32_t _mSDLReadGyroZ(struct mRotationSource* source) {
671 struct mSDLRotation* rotation = (struct mSDLRotation*) source;
672 float z = rotation->zDelta;
673 return z * rotation->gyroSensitivity;
674}
675
676static void _mSDLRotationSample(struct mRotationSource* source) {
677 struct mSDLRotation* rotation = (struct mSDLRotation*) source;
678 SDL_JoystickUpdate();
679 if (!rotation->p->joystick) {
680 return;
681 }
682
683 int x = SDL_JoystickGetAxis(rotation->p->joystick->joystick, rotation->gyroX);
684 int y = SDL_JoystickGetAxis(rotation->p->joystick->joystick, rotation->gyroY);
685 union {
686 float f;
687 int32_t i;
688 } theta = { .f = atan2f(y, x) - atan2f(rotation->oldY, rotation->oldX) };
689 if (isnan(theta.f)) {
690 theta.f = 0.0f;
691 } else if (theta.f > M_PI) {
692 theta.f -= 2.0f * M_PI;
693 } else if (theta.f < -M_PI) {
694 theta.f += 2.0f * M_PI;
695 }
696 rotation->oldX = x;
697 rotation->oldY = y;
698
699 float oldZ = 0;
700 if (CircleBufferSize(&rotation->zHistory) == GYRO_STEPS * sizeof(float)) {
701 CircleBufferRead32(&rotation->zHistory, (int32_t*) &oldZ);
702 }
703 CircleBufferWrite32(&rotation->zHistory, theta.i);
704 rotation->zDelta += theta.f - oldZ;
705}
706
707#if SDL_VERSION_ATLEAST(2, 0, 0)
708void mSDLSuspendScreensaver(struct mSDLEvents* events) {
709 if (events->screensaverSuspendDepth == 0 && events->screensaverSuspendable) {
710 SDL_DisableScreenSaver();
711 }
712 ++events->screensaverSuspendDepth;
713}
714
715void mSDLResumeScreensaver(struct mSDLEvents* events) {
716 --events->screensaverSuspendDepth;
717 if (events->screensaverSuspendDepth == 0 && events->screensaverSuspendable) {
718 SDL_EnableScreenSaver();
719 }
720}
721
722void mSDLSetScreensaverSuspendable(struct mSDLEvents* events, bool suspendable) {
723 bool wasSuspendable = events->screensaverSuspendable;
724 events->screensaverSuspendable = suspendable;
725 if (events->screensaverSuspendDepth > 0) {
726 if (suspendable && !wasSuspendable) {
727 SDL_DisableScreenSaver();
728 } else if (!suspendable && wasSuspendable) {
729 SDL_EnableScreenSaver();
730 }
731 } else {
732 SDL_EnableScreenSaver();
733 }
734}
735#endif