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