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