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#if SDL_VERSION_ATLEAST(2, 0, 0)
202 char joystickName[34] = {0};
203 SDL_JoystickGetGUIDString(SDL_JoystickGetGUID(SDL_JoystickListGetPointer(&events->joysticks, i)->joystick), joystickName, sizeof(joystickName));
204#else
205 const char* joystickName = SDL_JoystickName(SDL_JoystickIndex(SDL_JoystickListGetPointer(&events->joysticks, i)->joystick));
206 if (!joystickName) {
207 continue;
208 }
209#endif
210 if (events->preferredJoysticks[player->playerId] && strcmp(events->preferredJoysticks[player->playerId], joystickName) == 0) {
211 index = i;
212 break;
213 }
214 }
215
216 if (index == SIZE_MAX && firstUnclaimed != SIZE_MAX) {
217 index = firstUnclaimed;
218 }
219
220 if (index != SIZE_MAX) {
221 player->joystick = SDL_JoystickListGetPointer(&events->joysticks, index);
222
223#if SDL_VERSION_ATLEAST(2, 0, 0)
224 if (player->joystick->haptic) {
225 SDL_HapticRumbleInit(player->joystick->haptic);
226 }
227#endif
228 }
229
230 ++events->playersAttached;
231 return true;
232}
233
234void mSDLDetachPlayer(struct mSDLEvents* events, struct mSDLPlayer* player) {
235 if (player != events->players[player->playerId]) {
236 return;
237 }
238 int i;
239 for (i = player->playerId; i < events->playersAttached; ++i) {
240 if (i + 1 < MAX_PLAYERS) {
241 events->players[i] = events->players[i + 1];
242 }
243 if (i < events->playersAttached - 1) {
244 events->players[i]->playerId = i;
245 }
246 }
247 --events->playersAttached;
248 CircleBufferDeinit(&player->rotation.zHistory);
249}
250
251void mSDLPlayerLoadConfig(struct mSDLPlayer* context, const struct Configuration* config) {
252 mInputMapLoad(context->bindings, SDL_BINDING_KEY, config);
253 if (context->joystick) {
254 mInputMapLoad(context->bindings, SDL_BINDING_BUTTON, config);
255#if SDL_VERSION_ATLEAST(2, 0, 0)
256 char name[34] = {0};
257 SDL_JoystickGetGUIDString(SDL_JoystickGetGUID(context->joystick->joystick), name, sizeof(name));
258#else
259 const char* name = SDL_JoystickName(SDL_JoystickIndex(context->joystick->joystick));
260 if (!name) {
261 return;
262 }
263#endif
264 mInputProfileLoad(context->bindings, SDL_BINDING_BUTTON, config, name);
265
266 const char* value;
267 char* end;
268 int numAxes = SDL_JoystickNumAxes(context->joystick->joystick);
269 int axis;
270 value = mInputGetCustomValue(config, "gba", SDL_BINDING_BUTTON, "tiltAxisX", name);
271 if (value) {
272 axis = strtol(value, &end, 0);
273 if (axis >= 0 && axis < numAxes && end && !*end) {
274 context->rotation.axisX = axis;
275 }
276 }
277 value = mInputGetCustomValue(config, "gba", SDL_BINDING_BUTTON, "tiltAxisY", name);
278 if (value) {
279 axis = strtol(value, &end, 0);
280 if (axis >= 0 && axis < numAxes && end && !*end) {
281 context->rotation.axisY = axis;
282 }
283 }
284 value = mInputGetCustomValue(config, "gba", SDL_BINDING_BUTTON, "gyroAxisX", name);
285 if (value) {
286 axis = strtol(value, &end, 0);
287 if (axis >= 0 && axis < numAxes && end && !*end) {
288 context->rotation.gyroX = axis;
289 }
290 }
291 value = mInputGetCustomValue(config, "gba", SDL_BINDING_BUTTON, "gyroAxisY", name);
292 if (value) {
293 axis = strtol(value, &end, 0);
294 if (axis >= 0 && axis < numAxes && end && !*end) {
295 context->rotation.gyroY = axis;
296 }
297 }
298 value = mInputGetCustomValue(config, "gba", SDL_BINDING_BUTTON, "gyroSensitivity", name);
299 if (value) {
300 float sensitivity = strtof_u(value, &end);
301 if (end && !*end) {
302 context->rotation.gyroSensitivity = sensitivity;
303 }
304 }
305 }
306}
307
308void mSDLPlayerSaveConfig(const struct mSDLPlayer* context, struct Configuration* config) {
309 if (context->joystick) {
310#if SDL_VERSION_ATLEAST(2, 0, 0)
311 char name[34] = {0};
312 SDL_JoystickGetGUIDString(SDL_JoystickGetGUID(context->joystick->joystick), name, sizeof(name));
313#else
314 const char* name = SDL_JoystickName(SDL_JoystickIndex(context->joystick->joystick));
315 if (!name) {
316 return;
317 }
318#endif
319 char value[16];
320 snprintf(value, sizeof(value), "%i", context->rotation.axisX);
321 mInputSetCustomValue(config, "gba", SDL_BINDING_BUTTON, "tiltAxisX", value, name);
322 snprintf(value, sizeof(value), "%i", context->rotation.axisY);
323 mInputSetCustomValue(config, "gba", SDL_BINDING_BUTTON, "tiltAxisY", value, name);
324 snprintf(value, sizeof(value), "%i", context->rotation.gyroX);
325 mInputSetCustomValue(config, "gba", SDL_BINDING_BUTTON, "gyroAxisX", value, name);
326 snprintf(value, sizeof(value), "%i", context->rotation.gyroY);
327 mInputSetCustomValue(config, "gba", SDL_BINDING_BUTTON, "gyroAxisY", value, name);
328 snprintf(value, sizeof(value), "%g", context->rotation.gyroSensitivity);
329 mInputSetCustomValue(config, "gba", SDL_BINDING_BUTTON, "gyroSensitivity", value, name);
330 }
331}
332
333void mSDLPlayerChangeJoystick(struct mSDLEvents* events, struct mSDLPlayer* player, size_t index) {
334 if (player->playerId >= MAX_PLAYERS || index >= SDL_JoystickListSize(&events->joysticks)) {
335 return;
336 }
337 player->joystick = SDL_JoystickListGetPointer(&events->joysticks, index);
338}
339
340void mSDLUpdateJoysticks(struct mSDLEvents* events, const struct Configuration* config) {
341 // Pump SDL joystick events without eating the rest of the events
342 SDL_JoystickUpdate();
343#if SDL_VERSION_ATLEAST(2, 0, 0)
344 SDL_Event event;
345 while (SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_JOYDEVICEADDED, SDL_JOYDEVICEREMOVED) > 0) {
346 if (event.type == SDL_JOYDEVICEADDED) {
347 SDL_Joystick* sdlJoystick = SDL_JoystickOpen(event.jdevice.which);
348 if (!sdlJoystick) {
349 continue;
350 }
351 struct SDL_JoystickCombo* joystick = SDL_JoystickListAppend(&events->joysticks);
352 joystick->joystick = sdlJoystick;
353 joystick->id = SDL_JoystickInstanceID(joystick->joystick);
354 joystick->index = SDL_JoystickListSize(&events->joysticks) - 1;
355#if SDL_VERSION_ATLEAST(2, 0, 0)
356 joystick->haptic = SDL_HapticOpenFromJoystick(joystick->joystick);
357#endif
358
359 size_t i;
360#if SDL_VERSION_ATLEAST(2, 0, 0)
361 char joystickName[34] = {0};
362 SDL_JoystickGetGUIDString(SDL_JoystickGetGUID(joystick->joystick), joystickName, sizeof(joystickName));
363#else
364 const char* joystickName = SDL_JoystickName(SDL_JoystickIndex(joystick->joystick));
365 if (joystickName)
366#endif
367 {
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 && events->players[i]->bindings) {
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 && events->players[i]->bindings && 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 _mSDLHandleMouseButton(struct mCore* core, struct mSDLPlayer* sdlContext, const struct SDL_MouseButtonEvent* event) {
541 if (event->button != SDL_BUTTON_LEFT) {
542 return;
543 }
544 int x = event->x;
545 int y = event->y;
546#if SDL_VERSION_ATLEAST(2, 0, 0)
547 int windowW;
548 int windowH;
549 SDL_GetWindowSize(sdlContext->window, &windowW, &windowH);
550 unsigned coreW;
551 unsigned coreH;
552 core->desiredVideoDimensions(core, &coreW, &coreH);
553 x = x * coreW / windowW;
554 y = y * coreH / windowH;
555#endif
556 core->setCursorLocation(core, x, y);
557 core->setCursorDown(core, event->state == SDL_PRESSED);
558}
559
560static void _mSDLHandleMouseMotion(struct mCore* core, struct mSDLPlayer* sdlContext, const struct SDL_MouseMotionEvent* event) {
561 int x = event->x;
562 int y = event->y;
563#if SDL_VERSION_ATLEAST(2, 0, 0)
564 int windowW;
565 int windowH;
566 SDL_GetWindowSize(sdlContext->window, &windowW, &windowH);
567 unsigned coreW;
568 unsigned coreH;
569 core->desiredVideoDimensions(core, &coreW, &coreH);
570 x = x * coreW / windowW;
571 y = y * coreH / windowH;
572#endif
573 core->setCursorLocation(core, x, y);
574}
575
576static void _mSDLHandleJoyButton(struct mCoreThread* context, struct mSDLPlayer* sdlContext, const struct SDL_JoyButtonEvent* event) {
577 int key = 0;
578 key = mInputMapKey(sdlContext->bindings, SDL_BINDING_BUTTON, event->button);
579 if (key == -1) {
580 return;
581 }
582
583 mCoreThreadInterrupt(context);
584 if (event->type == SDL_JOYBUTTONDOWN) {
585 context->core->addKeys(context->core, 1 << key);
586 } else {
587 context->core->clearKeys(context->core, 1 << key);
588 }
589 mCoreThreadContinue(context);
590}
591
592static void _mSDLHandleJoyHat(struct mCoreThread* context, struct mSDLPlayer* sdlContext, const struct SDL_JoyHatEvent* event) {
593 int allKeys = mInputMapHat(sdlContext->bindings, SDL_BINDING_BUTTON, event->hat, -1);
594 if (allKeys == 0) {
595 return;
596 }
597
598 int keys = mInputMapHat(sdlContext->bindings, SDL_BINDING_BUTTON, event->hat, event->value);
599
600 mCoreThreadInterrupt(context);
601 context->core->clearKeys(context->core, allKeys ^ keys);
602 context->core->addKeys(context->core, keys);
603 mCoreThreadContinue(context);
604}
605
606static void _mSDLHandleJoyAxis(struct mCoreThread* context, struct mSDLPlayer* sdlContext, const struct SDL_JoyAxisEvent* event) {
607 int clearKeys = ~mInputClearAxis(sdlContext->bindings, SDL_BINDING_BUTTON, event->axis, -1);
608 int newKeys = 0;
609 int key = mInputMapAxis(sdlContext->bindings, SDL_BINDING_BUTTON, event->axis, event->value);
610 if (key != -1) {
611 newKeys |= 1 << key;
612 }
613 clearKeys &= ~newKeys;
614 mCoreThreadInterrupt(context);
615 context->core->clearKeys(context->core, clearKeys);
616 context->core->addKeys(context->core, newKeys);
617 mCoreThreadContinue(context);
618
619}
620
621#if SDL_VERSION_ATLEAST(2, 0, 0)
622static void _mSDLHandleWindowEvent(struct mSDLPlayer* sdlContext, const struct SDL_WindowEvent* event) {
623 switch (event->event) {
624 case SDL_WINDOWEVENT_SIZE_CHANGED:
625 sdlContext->windowUpdated = 1;
626 break;
627 }
628}
629#endif
630
631void mSDLHandleEvent(struct mCoreThread* context, struct mSDLPlayer* sdlContext, const union SDL_Event* event) {
632 switch (event->type) {
633 case SDL_QUIT:
634 mCoreThreadEnd(context);
635 break;
636#if SDL_VERSION_ATLEAST(2, 0, 0)
637 case SDL_WINDOWEVENT:
638 _mSDLHandleWindowEvent(sdlContext, &event->window);
639 break;
640#else
641 case SDL_VIDEORESIZE:
642 sdlContext->newWidth = event->resize.w;
643 sdlContext->newHeight = event->resize.h;
644 sdlContext->windowUpdated = 1;
645 break;
646#endif
647 case SDL_KEYDOWN:
648 case SDL_KEYUP:
649 _mSDLHandleKeypress(context, sdlContext, &event->key);
650 break;
651 case SDL_MOUSEBUTTONDOWN:
652 case SDL_MOUSEBUTTONUP:
653 _mSDLHandleMouseButton(context->core, sdlContext, &event->button);
654 break;
655 case SDL_MOUSEMOTION:
656 _mSDLHandleMouseMotion(context->core, sdlContext, &event->motion);
657 break;
658 case SDL_JOYBUTTONDOWN:
659 case SDL_JOYBUTTONUP:
660 _mSDLHandleJoyButton(context, sdlContext, &event->jbutton);
661 break;
662 case SDL_JOYHATMOTION:
663 _mSDLHandleJoyHat(context, sdlContext, &event->jhat);
664 break;
665 case SDL_JOYAXISMOTION:
666 _mSDLHandleJoyAxis(context, sdlContext, &event->jaxis);
667 break;
668 }
669}
670
671#if SDL_VERSION_ATLEAST(2, 0, 0)
672static void _mSDLSetRumble(struct mRumble* rumble, int enable) {
673 struct mSDLRumble* sdlRumble = (struct mSDLRumble*) rumble;
674 if (!sdlRumble->p->joystick || !sdlRumble->p->joystick->haptic || !SDL_HapticRumbleSupported(sdlRumble->p->joystick->haptic)) {
675 return;
676 }
677 int8_t originalLevel = sdlRumble->level;
678 sdlRumble->level += enable;
679 if (CircleBufferSize(&sdlRumble->history) == RUMBLE_PWM) {
680 int8_t oldLevel;
681 CircleBufferRead8(&sdlRumble->history, &oldLevel);
682 sdlRumble->level -= oldLevel;
683 }
684 CircleBufferWrite8(&sdlRumble->history, enable);
685 if (sdlRumble->level == originalLevel) {
686 return;
687 }
688 float activeLevel = ceil(RUMBLE_STEPS * sdlRumble->level / (float) RUMBLE_PWM) / RUMBLE_STEPS;
689 if (fabsf(sdlRumble->activeLevel - activeLevel) < 0.75 / RUMBLE_STEPS) {
690 return;
691 }
692 sdlRumble->activeLevel = activeLevel;
693 if (sdlRumble->activeLevel > 0.5 / RUMBLE_STEPS) {
694 SDL_HapticRumbleStop(sdlRumble->p->joystick->haptic);
695 SDL_HapticRumblePlay(sdlRumble->p->joystick->haptic, activeLevel, 500);
696 } else {
697 SDL_HapticRumbleStop(sdlRumble->p->joystick->haptic);
698 }
699}
700#endif
701
702static int32_t _readTilt(struct mSDLPlayer* player, int axis) {
703 if (!player->joystick) {
704 return 0;
705 }
706 return SDL_JoystickGetAxis(player->joystick->joystick, axis) * 0x3800;
707}
708
709static int32_t _mSDLReadTiltX(struct mRotationSource* source) {
710 struct mSDLRotation* rotation = (struct mSDLRotation*) source;
711 return _readTilt(rotation->p, rotation->axisX);
712}
713
714static int32_t _mSDLReadTiltY(struct mRotationSource* source) {
715 struct mSDLRotation* rotation = (struct mSDLRotation*) source;
716 return _readTilt(rotation->p, rotation->axisY);
717}
718
719static int32_t _mSDLReadGyroZ(struct mRotationSource* source) {
720 struct mSDLRotation* rotation = (struct mSDLRotation*) source;
721 float z = rotation->zDelta;
722 return z * rotation->gyroSensitivity;
723}
724
725static void _mSDLRotationSample(struct mRotationSource* source) {
726 struct mSDLRotation* rotation = (struct mSDLRotation*) source;
727 SDL_JoystickUpdate();
728 if (!rotation->p->joystick) {
729 return;
730 }
731
732 int x = SDL_JoystickGetAxis(rotation->p->joystick->joystick, rotation->gyroX);
733 int y = SDL_JoystickGetAxis(rotation->p->joystick->joystick, rotation->gyroY);
734 union {
735 float f;
736 int32_t i;
737 } theta = { .f = atan2f(y, x) - atan2f(rotation->oldY, rotation->oldX) };
738 if (isnan(theta.f)) {
739 theta.f = 0.0f;
740 } else if (theta.f > M_PI) {
741 theta.f -= 2.0f * M_PI;
742 } else if (theta.f < -M_PI) {
743 theta.f += 2.0f * M_PI;
744 }
745 rotation->oldX = x;
746 rotation->oldY = y;
747
748 float oldZ = 0;
749 if (CircleBufferSize(&rotation->zHistory) == GYRO_STEPS * sizeof(float)) {
750 CircleBufferRead32(&rotation->zHistory, (int32_t*) &oldZ);
751 }
752 CircleBufferWrite32(&rotation->zHistory, theta.i);
753 rotation->zDelta += theta.f - oldZ;
754}
755
756#if SDL_VERSION_ATLEAST(2, 0, 0)
757void mSDLSuspendScreensaver(struct mSDLEvents* events) {
758 if (events->screensaverSuspendDepth == 0 && events->screensaverSuspendable) {
759 SDL_DisableScreenSaver();
760 }
761 ++events->screensaverSuspendDepth;
762}
763
764void mSDLResumeScreensaver(struct mSDLEvents* events) {
765 --events->screensaverSuspendDepth;
766 if (events->screensaverSuspendDepth == 0 && events->screensaverSuspendable) {
767 SDL_EnableScreenSaver();
768 }
769}
770
771void mSDLSetScreensaverSuspendable(struct mSDLEvents* events, bool suspendable) {
772 bool wasSuspendable = events->screensaverSuspendable;
773 events->screensaverSuspendable = suspendable;
774 if (events->screensaverSuspendDepth > 0) {
775 if (suspendable && !wasSuspendable) {
776 SDL_DisableScreenSaver();
777 } else if (!suspendable && wasSuspendable) {
778 SDL_EnableScreenSaver();
779 }
780 } else {
781 SDL_EnableScreenSaver();
782 }
783}
784#endif