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#if SDL_VERSION_ATLEAST(2, 0, 0)
205 char joystickName[34] = {0};
206 SDL_JoystickGetGUIDString(SDL_JoystickGetGUID(SDL_JoystickListGetPointer(&events->joysticks, i)->joystick), joystickName, sizeof(joystickName));
207#else
208 const char* joystickName = SDL_JoystickName(SDL_JoystickIndex(SDL_JoystickListGetPointer(&events->joysticks, i)->joystick));
209 if (!joystickName) {
210 continue;
211 }
212#endif
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 char name[34] = {0};
260 SDL_JoystickGetGUIDString(SDL_JoystickGetGUID(context->joystick->joystick), name, sizeof(name));
261#else
262 const char* name = SDL_JoystickName(SDL_JoystickIndex(context->joystick->joystick));
263 if (!name) {
264 return;
265 }
266#endif
267 mInputProfileLoad(context->bindings, SDL_BINDING_BUTTON, config, name);
268
269 const char* value;
270 char* end;
271 int numAxes = SDL_JoystickNumAxes(context->joystick->joystick);
272 int axis;
273 value = mInputGetCustomValue(config, "gba", SDL_BINDING_BUTTON, "tiltAxisX", name);
274 if (value) {
275 axis = strtol(value, &end, 0);
276 if (axis >= 0 && axis < numAxes && end && !*end) {
277 context->rotation.axisX = axis;
278 }
279 }
280 value = mInputGetCustomValue(config, "gba", SDL_BINDING_BUTTON, "tiltAxisY", name);
281 if (value) {
282 axis = strtol(value, &end, 0);
283 if (axis >= 0 && axis < numAxes && end && !*end) {
284 context->rotation.axisY = axis;
285 }
286 }
287 value = mInputGetCustomValue(config, "gba", SDL_BINDING_BUTTON, "gyroAxisX", name);
288 if (value) {
289 axis = strtol(value, &end, 0);
290 if (axis >= 0 && axis < numAxes && end && !*end) {
291 context->rotation.gyroX = axis;
292 }
293 }
294 value = mInputGetCustomValue(config, "gba", SDL_BINDING_BUTTON, "gyroAxisY", name);
295 if (value) {
296 axis = strtol(value, &end, 0);
297 if (axis >= 0 && axis < numAxes && end && !*end) {
298 context->rotation.gyroY = axis;
299 }
300 }
301 value = mInputGetCustomValue(config, "gba", SDL_BINDING_BUTTON, "gyroSensitivity", name);
302 if (value) {
303 float sensitivity = strtof_u(value, &end);
304 if (end && !*end) {
305 context->rotation.gyroSensitivity = sensitivity;
306 }
307 }
308 }
309}
310
311void mSDLPlayerSaveConfig(const struct mSDLPlayer* context, struct Configuration* config) {
312 if (context->joystick) {
313#if SDL_VERSION_ATLEAST(2, 0, 0)
314 char name[34] = {0};
315 SDL_JoystickGetGUIDString(SDL_JoystickGetGUID(context->joystick->joystick), name, sizeof(name));
316#else
317 const char* name = SDL_JoystickName(SDL_JoystickIndex(context->joystick->joystick));
318 if (!name) {
319 return;
320 }
321#endif
322 char value[16];
323 snprintf(value, sizeof(value), "%i", context->rotation.axisX);
324 mInputSetCustomValue(config, "gba", SDL_BINDING_BUTTON, "tiltAxisX", value, name);
325 snprintf(value, sizeof(value), "%i", context->rotation.axisY);
326 mInputSetCustomValue(config, "gba", SDL_BINDING_BUTTON, "tiltAxisY", value, name);
327 snprintf(value, sizeof(value), "%i", context->rotation.gyroX);
328 mInputSetCustomValue(config, "gba", SDL_BINDING_BUTTON, "gyroAxisX", value, name);
329 snprintf(value, sizeof(value), "%i", context->rotation.gyroY);
330 mInputSetCustomValue(config, "gba", SDL_BINDING_BUTTON, "gyroAxisY", value, name);
331 snprintf(value, sizeof(value), "%g", context->rotation.gyroSensitivity);
332 mInputSetCustomValue(config, "gba", SDL_BINDING_BUTTON, "gyroSensitivity", value, name);
333 }
334}
335
336void mSDLPlayerChangeJoystick(struct mSDLEvents* events, struct mSDLPlayer* player, size_t index) {
337 if (player->playerId >= MAX_PLAYERS || index >= SDL_JoystickListSize(&events->joysticks)) {
338 return;
339 }
340 player->joystick = SDL_JoystickListGetPointer(&events->joysticks, index);
341}
342
343void mSDLUpdateJoysticks(struct mSDLEvents* events, const struct Configuration* config) {
344 // Pump SDL joystick events without eating the rest of the events
345 SDL_JoystickUpdate();
346#if SDL_VERSION_ATLEAST(2, 0, 0)
347 SDL_Event event;
348 while (SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_JOYDEVICEADDED, SDL_JOYDEVICEREMOVED) > 0) {
349 if (event.type == SDL_JOYDEVICEADDED) {
350 SDL_Joystick* sdlJoystick = SDL_JoystickOpen(event.jdevice.which);
351 if (!sdlJoystick) {
352 continue;
353 }
354 struct SDL_JoystickCombo* joystick = SDL_JoystickListAppend(&events->joysticks);
355 joystick->joystick = sdlJoystick;
356 joystick->id = SDL_JoystickInstanceID(joystick->joystick);
357 joystick->index = SDL_JoystickListSize(&events->joysticks) - 1;
358#if SDL_VERSION_ATLEAST(2, 0, 0)
359 joystick->haptic = SDL_HapticOpenFromJoystick(joystick->joystick);
360#endif
361
362 size_t i;
363#if SDL_VERSION_ATLEAST(2, 0, 0)
364 char joystickName[34] = {0};
365 SDL_JoystickGetGUIDString(SDL_JoystickGetGUID(joystick->joystick), joystickName, sizeof(joystickName));
366#else
367 const char* joystickName = SDL_JoystickName(SDL_JoystickIndex(joystick->joystick));
368 if (joystickName)
369#endif
370 {
371 for (i = 0; (int) i < events->playersAttached; ++i) {
372 if (events->players[i]->joystick) {
373 continue;
374 }
375 if (events->preferredJoysticks[i] && strcmp(events->preferredJoysticks[i], joystickName) == 0) {
376 events->players[i]->joystick = joystick;
377 if (config) {
378 mInputProfileLoad(events->players[i]->bindings, SDL_BINDING_BUTTON, config, joystickName);
379 }
380 return;
381 }
382 }
383 }
384 for (i = 0; (int) i < events->playersAttached; ++i) {
385 if (events->players[i]->joystick) {
386 continue;
387 }
388 events->players[i]->joystick = joystick;
389 if (config && joystickName) {
390 mInputProfileLoad(events->players[i]->bindings, SDL_BINDING_BUTTON, config, joystickName);
391 }
392 break;
393 }
394 } else if (event.type == SDL_JOYDEVICEREMOVED) {
395 SDL_JoystickID ids[MAX_PLAYERS] = { 0 };
396 size_t i;
397 for (i = 0; (int) i < events->playersAttached; ++i) {
398 if (events->players[i]->joystick) {
399 ids[i] = events->players[i]->joystick->id;
400 events->players[i]->joystick = 0;
401 } else {
402 ids[i] = -1;
403 }
404 }
405 for (i = 0; i < SDL_JoystickListSize(&events->joysticks);) {
406 struct SDL_JoystickCombo* joystick = SDL_JoystickListGetPointer(&events->joysticks, i);
407 if (joystick->id == event.jdevice.which) {
408 SDL_JoystickListShift(&events->joysticks, i, 1);
409 continue;
410 }
411 SDL_JoystickListGetPointer(&events->joysticks, i)->index = i;
412 int p;
413 for (p = 0; p < events->playersAttached; ++p) {
414 if (joystick->id == ids[p]) {
415 events->players[p]->joystick = SDL_JoystickListGetPointer(&events->joysticks, i);
416 }
417 }
418 ++i;
419 }
420 }
421 }
422#endif
423}
424
425static void _pauseAfterFrame(struct mCoreThread* context) {
426 context->frameCallback = 0;
427 mCoreThreadPauseFromThread(context);
428}
429
430static void _mSDLHandleKeypress(struct mCoreThread* context, struct mSDLPlayer* sdlContext, const struct SDL_KeyboardEvent* event) {
431 int key = -1;
432 if (!(event->keysym.mod & ~(KMOD_NUM | KMOD_CAPS))) {
433 key = mInputMapKey(sdlContext->bindings, SDL_BINDING_KEY, event->keysym.sym);
434 }
435 if (key != -1) {
436 mCoreThreadInterrupt(context);
437 if (event->type == SDL_KEYDOWN) {
438 context->core->addKeys(context->core, 1 << key);
439 } else {
440 context->core->clearKeys(context->core, 1 << key);
441 }
442 mCoreThreadContinue(context);
443 return;
444 }
445 if (event->keysym.sym == SDLK_TAB) {
446 context->impl->sync.audioWait = event->type != SDL_KEYDOWN;
447 return;
448 }
449 if (event->keysym.sym == SDLK_BACKQUOTE) {
450 mCoreThreadSetRewinding(context, event->type == SDL_KEYDOWN);
451 }
452 if (event->type == SDL_KEYDOWN) {
453 switch (event->keysym.sym) {
454#ifdef USE_DEBUGGERS
455 case SDLK_F11:
456 if (context->core->debugger) {
457 mDebuggerEnter(context->core->debugger, DEBUGGER_ENTER_MANUAL, NULL);
458 }
459 return;
460#endif
461#ifdef USE_PNG
462 case SDLK_F12:
463 mCoreTakeScreenshot(context->core);
464 return;
465#endif
466 case SDLK_BACKSLASH:
467 mCoreThreadPause(context);
468 context->frameCallback = _pauseAfterFrame;
469 mCoreThreadUnpause(context);
470 return;
471#ifdef BUILD_PANDORA
472 case SDLK_ESCAPE:
473 mCoreThreadEnd(context);
474 return;
475#endif
476 default:
477 if ((event->keysym.mod & GUI_MOD) && (event->keysym.mod & GUI_MOD) == event->keysym.mod) {
478 switch (event->keysym.sym) {
479#if SDL_VERSION_ATLEAST(2, 0, 0)
480 case SDLK_f:
481 SDL_SetWindowFullscreen(sdlContext->window, sdlContext->fullscreen ? 0 : SDL_WINDOW_FULLSCREEN_DESKTOP);
482 sdlContext->fullscreen = !sdlContext->fullscreen;
483 sdlContext->windowUpdated = 1;
484 break;
485#endif
486 case SDLK_p:
487 mCoreThreadTogglePause(context);
488 break;
489 case SDLK_n:
490 mCoreThreadPause(context);
491 context->frameCallback = _pauseAfterFrame;
492 mCoreThreadUnpause(context);
493 break;
494 case SDLK_r:
495 mCoreThreadReset(context);
496 break;
497 default:
498 break;
499 }
500 }
501 if (event->keysym.mod & KMOD_SHIFT) {
502 switch (event->keysym.sym) {
503 case SDLK_F1:
504 case SDLK_F2:
505 case SDLK_F3:
506 case SDLK_F4:
507 case SDLK_F5:
508 case SDLK_F6:
509 case SDLK_F7:
510 case SDLK_F8:
511 case SDLK_F9:
512 mCoreThreadInterrupt(context);
513 mCoreSaveState(context->core, event->keysym.sym - SDLK_F1 + 1, SAVESTATE_SAVEDATA | SAVESTATE_SCREENSHOT | SAVESTATE_RTC);
514 mCoreThreadContinue(context);
515 break;
516 default:
517 break;
518 }
519 } else {
520 switch (event->keysym.sym) {
521 case SDLK_F1:
522 case SDLK_F2:
523 case SDLK_F3:
524 case SDLK_F4:
525 case SDLK_F5:
526 case SDLK_F6:
527 case SDLK_F7:
528 case SDLK_F8:
529 case SDLK_F9:
530 mCoreThreadInterrupt(context);
531 mCoreLoadState(context->core, event->keysym.sym - SDLK_F1 + 1, SAVESTATE_SCREENSHOT | SAVESTATE_RTC);
532 mCoreThreadContinue(context);
533 break;
534 default:
535 break;
536 }
537 }
538 return;
539 }
540 }
541}
542
543static void _mSDLHandleJoyButton(struct mCoreThread* context, struct mSDLPlayer* sdlContext, const struct SDL_JoyButtonEvent* event) {
544 int key = 0;
545 key = mInputMapKey(sdlContext->bindings, SDL_BINDING_BUTTON, event->button);
546 if (key == -1) {
547 return;
548 }
549
550 mCoreThreadInterrupt(context);
551 if (event->type == SDL_JOYBUTTONDOWN) {
552 context->core->addKeys(context->core, 1 << key);
553 } else {
554 context->core->clearKeys(context->core, 1 << key);
555 }
556 mCoreThreadContinue(context);
557}
558
559static void _mSDLHandleJoyHat(struct mCoreThread* context, struct mSDLPlayer* sdlContext, const struct SDL_JoyHatEvent* event) {
560 int allKeys = mInputMapHat(sdlContext->bindings, SDL_BINDING_BUTTON, event->hat, -1);
561 if (allKeys == 0) {
562 return;
563 }
564
565 int keys = mInputMapHat(sdlContext->bindings, SDL_BINDING_BUTTON, event->hat, event->value);
566
567 mCoreThreadInterrupt(context);
568 context->core->clearKeys(context->core, allKeys ^ keys);
569 context->core->addKeys(context->core, keys);
570 mCoreThreadContinue(context);
571}
572
573static void _mSDLHandleJoyAxis(struct mCoreThread* context, struct mSDLPlayer* sdlContext, const struct SDL_JoyAxisEvent* event) {
574 int clearKeys = ~mInputClearAxis(sdlContext->bindings, SDL_BINDING_BUTTON, event->axis, -1);
575 int newKeys = 0;
576 int key = mInputMapAxis(sdlContext->bindings, SDL_BINDING_BUTTON, event->axis, event->value);
577 if (key != -1) {
578 newKeys |= 1 << key;
579 }
580 clearKeys &= ~newKeys;
581 mCoreThreadInterrupt(context);
582 context->core->clearKeys(context->core, clearKeys);
583 context->core->addKeys(context->core, newKeys);
584 mCoreThreadContinue(context);
585
586}
587
588#if SDL_VERSION_ATLEAST(2, 0, 0)
589static void _mSDLHandleWindowEvent(struct mSDLPlayer* sdlContext, const struct SDL_WindowEvent* event) {
590 switch (event->event) {
591 case SDL_WINDOWEVENT_SIZE_CHANGED:
592 sdlContext->windowUpdated = 1;
593 break;
594 }
595}
596#endif
597
598void mSDLHandleEvent(struct mCoreThread* context, struct mSDLPlayer* sdlContext, const union SDL_Event* event) {
599 switch (event->type) {
600 case SDL_QUIT:
601 mCoreThreadEnd(context);
602 break;
603#if SDL_VERSION_ATLEAST(2, 0, 0)
604 case SDL_WINDOWEVENT:
605 _mSDLHandleWindowEvent(sdlContext, &event->window);
606 break;
607#else
608 case SDL_VIDEORESIZE:
609 sdlContext->newWidth = event->resize.w;
610 sdlContext->newHeight = event->resize.h;
611 sdlContext->windowUpdated = 1;
612 break;
613#endif
614 case SDL_KEYDOWN:
615 case SDL_KEYUP:
616 _mSDLHandleKeypress(context, sdlContext, &event->key);
617 break;
618 case SDL_JOYBUTTONDOWN:
619 case SDL_JOYBUTTONUP:
620 _mSDLHandleJoyButton(context, sdlContext, &event->jbutton);
621 break;
622 case SDL_JOYHATMOTION:
623 _mSDLHandleJoyHat(context, sdlContext, &event->jhat);
624 break;
625 case SDL_JOYAXISMOTION:
626 _mSDLHandleJoyAxis(context, sdlContext, &event->jaxis);
627 break;
628 }
629}
630
631#if SDL_VERSION_ATLEAST(2, 0, 0)
632static void _mSDLSetRumble(struct mRumble* rumble, int enable) {
633 struct mSDLRumble* sdlRumble = (struct mSDLRumble*) rumble;
634 if (!sdlRumble->p->joystick || !sdlRumble->p->joystick->haptic || !SDL_HapticRumbleSupported(sdlRumble->p->joystick->haptic)) {
635 return;
636 }
637 int8_t originalLevel = sdlRumble->level;
638 sdlRumble->level += enable;
639 if (CircleBufferSize(&sdlRumble->history) == RUMBLE_PWM) {
640 int8_t oldLevel;
641 CircleBufferRead8(&sdlRumble->history, &oldLevel);
642 sdlRumble->level -= oldLevel;
643 }
644 CircleBufferWrite8(&sdlRumble->history, enable);
645 if (sdlRumble->level == originalLevel) {
646 return;
647 }
648 float activeLevel = ceil(RUMBLE_STEPS * sdlRumble->level / (float) RUMBLE_PWM) / RUMBLE_STEPS;
649 if (fabsf(sdlRumble->activeLevel - activeLevel) < 0.75 / RUMBLE_STEPS) {
650 return;
651 }
652 sdlRumble->activeLevel = activeLevel;
653 if (sdlRumble->activeLevel > 0.5 / RUMBLE_STEPS) {
654 SDL_HapticRumbleStop(sdlRumble->p->joystick->haptic);
655 SDL_HapticRumblePlay(sdlRumble->p->joystick->haptic, activeLevel, 500);
656 } else {
657 SDL_HapticRumbleStop(sdlRumble->p->joystick->haptic);
658 }
659}
660#endif
661
662static int32_t _readTilt(struct mSDLPlayer* player, int axis) {
663 if (!player->joystick) {
664 return 0;
665 }
666 return SDL_JoystickGetAxis(player->joystick->joystick, axis) * 0x3800;
667}
668
669static int32_t _mSDLReadTiltX(struct mRotationSource* source) {
670 struct mSDLRotation* rotation = (struct mSDLRotation*) source;
671 return _readTilt(rotation->p, rotation->axisX);
672}
673
674static int32_t _mSDLReadTiltY(struct mRotationSource* source) {
675 struct mSDLRotation* rotation = (struct mSDLRotation*) source;
676 return _readTilt(rotation->p, rotation->axisY);
677}
678
679static int32_t _mSDLReadGyroZ(struct mRotationSource* source) {
680 struct mSDLRotation* rotation = (struct mSDLRotation*) source;
681 float z = rotation->zDelta;
682 return z * rotation->gyroSensitivity;
683}
684
685static void _mSDLRotationSample(struct mRotationSource* source) {
686 struct mSDLRotation* rotation = (struct mSDLRotation*) source;
687 SDL_JoystickUpdate();
688 if (!rotation->p->joystick) {
689 return;
690 }
691
692 int x = SDL_JoystickGetAxis(rotation->p->joystick->joystick, rotation->gyroX);
693 int y = SDL_JoystickGetAxis(rotation->p->joystick->joystick, rotation->gyroY);
694 union {
695 float f;
696 int32_t i;
697 } theta = { .f = atan2f(y, x) - atan2f(rotation->oldY, rotation->oldX) };
698 if (isnan(theta.f)) {
699 theta.f = 0.0f;
700 } else if (theta.f > M_PI) {
701 theta.f -= 2.0f * M_PI;
702 } else if (theta.f < -M_PI) {
703 theta.f += 2.0f * M_PI;
704 }
705 rotation->oldX = x;
706 rotation->oldY = y;
707
708 float oldZ = 0;
709 if (CircleBufferSize(&rotation->zHistory) == GYRO_STEPS * sizeof(float)) {
710 CircleBufferRead32(&rotation->zHistory, (int32_t*) &oldZ);
711 }
712 CircleBufferWrite32(&rotation->zHistory, theta.i);
713 rotation->zDelta += theta.f - oldZ;
714}
715
716#if SDL_VERSION_ATLEAST(2, 0, 0)
717void mSDLSuspendScreensaver(struct mSDLEvents* events) {
718 if (events->screensaverSuspendDepth == 0 && events->screensaverSuspendable) {
719 SDL_DisableScreenSaver();
720 }
721 ++events->screensaverSuspendDepth;
722}
723
724void mSDLResumeScreensaver(struct mSDLEvents* events) {
725 --events->screensaverSuspendDepth;
726 if (events->screensaverSuspendDepth == 0 && events->screensaverSuspendable) {
727 SDL_EnableScreenSaver();
728 }
729}
730
731void mSDLSetScreensaverSuspendable(struct mSDLEvents* events, bool suspendable) {
732 bool wasSuspendable = events->screensaverSuspendable;
733 events->screensaverSuspendable = suspendable;
734 if (events->screensaverSuspendDepth > 0) {
735 if (suspendable && !wasSuspendable) {
736 SDL_DisableScreenSaver();
737 } else if (!suspendable && wasSuspendable) {
738 SDL_EnableScreenSaver();
739 }
740 } else {
741 SDL_EnableScreenSaver();
742 }
743}
744#endif