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 "debugger/debugger.h"
9#include "gba/io.h"
10#include "gba/rr/rr.h"
11#include "gba/serialize.h"
12#include "gba/video.h"
13#include "gba/renderers/video-software.h"
14#include "util/configuration.h"
15#include "util/formatting.h"
16#include "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 20
26
27DEFINE_VECTOR(SDL_JoystickList, struct SDL_JoystickCombo);
28
29#if SDL_VERSION_ATLEAST(2, 0, 0)
30static void _GBASDLSetRumble(struct GBARumble* rumble, int enable);
31#endif
32static int32_t _GBASDLReadTiltX(struct GBARotationSource* rumble);
33static int32_t _GBASDLReadTiltY(struct GBARotationSource* rumble);
34static int32_t _GBASDLReadGyroZ(struct GBARotationSource* rumble);
35static void _GBASDLRotationSample(struct GBARotationSource* source);
36
37bool GBASDLInitEvents(struct GBASDLEvents* context) {
38#if SDL_VERSION_ATLEAST(2, 0, 4)
39 SDL_SetHint(SDL_HINT_NO_SIGNAL_HANDLERS, "1");
40#endif
41 if (SDL_InitSubSystem(SDL_INIT_JOYSTICK) < 0) {
42 GBALog(0, GBA_LOG_ERROR, "SDL joystick initialization failed: %s", SDL_GetError());
43 }
44
45#if SDL_VERSION_ATLEAST(2, 0, 0)
46 SDL_SetHint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS, "1");
47 if (SDL_InitSubSystem(SDL_INIT_HAPTIC) < 0) {
48 GBALog(0, GBA_LOG_ERROR, "SDL haptic initialization failed: %s", SDL_GetError());
49 }
50 if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0) {
51 GBALog(0, GBA_LOG_ERROR, "SDL video initialization failed: %s", SDL_GetError());
52 }
53#endif
54
55 SDL_JoystickEventState(SDL_ENABLE);
56 int nJoysticks = SDL_NumJoysticks();
57 SDL_JoystickListInit(&context->joysticks, nJoysticks);
58 if (nJoysticks > 0) {
59 GBASDLUpdateJoysticks(context);
60 }
61
62 context->playersAttached = 0;
63
64 size_t i;
65 for (i = 0; i < MAX_PLAYERS; ++i) {
66 context->preferredJoysticks[i] = 0;
67 }
68
69#if !SDL_VERSION_ATLEAST(2, 0, 0)
70 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
71#else
72 context->screensaverSuspendDepth = 0;
73#endif
74 return true;
75}
76
77void GBASDLDeinitEvents(struct GBASDLEvents* context) {
78 size_t i;
79 for (i = 0; i < SDL_JoystickListSize(&context->joysticks); ++i) {
80 struct SDL_JoystickCombo* joystick = SDL_JoystickListGetPointer(&context->joysticks, i);
81#if SDL_VERSION_ATLEAST(2, 0, 0)
82 SDL_HapticClose(joystick->haptic);
83#endif
84 SDL_JoystickClose(joystick->joystick);
85 }
86 SDL_JoystickListDeinit(&context->joysticks);
87 SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
88}
89
90void GBASDLEventsLoadConfig(struct GBASDLEvents* context, const struct Configuration* config) {
91 context->preferredJoysticks[0] = GBAInputGetPreferredDevice(config, SDL_BINDING_BUTTON, 0);
92 context->preferredJoysticks[1] = GBAInputGetPreferredDevice(config, SDL_BINDING_BUTTON, 1);
93 context->preferredJoysticks[2] = GBAInputGetPreferredDevice(config, SDL_BINDING_BUTTON, 2);
94 context->preferredJoysticks[3] = GBAInputGetPreferredDevice(config, SDL_BINDING_BUTTON, 3);
95}
96
97void GBASDLInitBindings(struct GBAInputMap* inputMap) {
98#ifdef BUILD_PANDORA
99 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_PAGEDOWN, GBA_KEY_A);
100 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_END, GBA_KEY_B);
101 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_RSHIFT, GBA_KEY_L);
102 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_RCTRL, GBA_KEY_R);
103 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_LALT, GBA_KEY_START);
104 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_LCTRL, GBA_KEY_SELECT);
105 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_UP, GBA_KEY_UP);
106 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_DOWN, GBA_KEY_DOWN);
107 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_LEFT, GBA_KEY_LEFT);
108 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_RIGHT, GBA_KEY_RIGHT);
109#elif SDL_VERSION_ATLEAST(2, 0, 0)
110 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDL_SCANCODE_X, GBA_KEY_A);
111 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDL_SCANCODE_Z, GBA_KEY_B);
112 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDL_SCANCODE_A, GBA_KEY_L);
113 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDL_SCANCODE_S, GBA_KEY_R);
114 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDL_SCANCODE_RETURN, GBA_KEY_START);
115 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDL_SCANCODE_BACKSPACE, GBA_KEY_SELECT);
116 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDL_SCANCODE_UP, GBA_KEY_UP);
117 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDL_SCANCODE_DOWN, GBA_KEY_DOWN);
118 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDL_SCANCODE_LEFT, GBA_KEY_LEFT);
119 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDL_SCANCODE_RIGHT, GBA_KEY_RIGHT);
120#else
121 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_x, GBA_KEY_A);
122 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_z, GBA_KEY_B);
123 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_a, GBA_KEY_L);
124 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_s, GBA_KEY_R);
125 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_RETURN, GBA_KEY_START);
126 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_BACKSPACE, GBA_KEY_SELECT);
127 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_UP, GBA_KEY_UP);
128 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_DOWN, GBA_KEY_DOWN);
129 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_LEFT, GBA_KEY_LEFT);
130 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_RIGHT, GBA_KEY_RIGHT);
131#endif
132
133 GBAInputBindKey(inputMap, SDL_BINDING_BUTTON, 13, GBA_KEY_A);
134 GBAInputBindKey(inputMap, SDL_BINDING_BUTTON, 14, GBA_KEY_B);
135 GBAInputBindKey(inputMap, SDL_BINDING_BUTTON, 10, GBA_KEY_L);
136 GBAInputBindKey(inputMap, SDL_BINDING_BUTTON, 11, GBA_KEY_R);
137 GBAInputBindKey(inputMap, SDL_BINDING_BUTTON, 3, GBA_KEY_START);
138 GBAInputBindKey(inputMap, SDL_BINDING_BUTTON, 0, GBA_KEY_SELECT);
139 GBAInputBindKey(inputMap, SDL_BINDING_BUTTON, 4, GBA_KEY_UP);
140 GBAInputBindKey(inputMap, SDL_BINDING_BUTTON, 6, GBA_KEY_DOWN);
141 GBAInputBindKey(inputMap, SDL_BINDING_BUTTON, 7, GBA_KEY_LEFT);
142 GBAInputBindKey(inputMap, SDL_BINDING_BUTTON, 5, GBA_KEY_RIGHT);
143
144 struct GBAAxis description = { GBA_KEY_RIGHT, GBA_KEY_LEFT, 0x4000, -0x4000 };
145 GBAInputBindAxis(inputMap, SDL_BINDING_BUTTON, 0, &description);
146 description = (struct GBAAxis) { GBA_KEY_DOWN, GBA_KEY_UP, 0x4000, -0x4000 };
147 GBAInputBindAxis(inputMap, SDL_BINDING_BUTTON, 1, &description);
148}
149
150bool GBASDLAttachPlayer(struct GBASDLEvents* events, struct GBASDLPlayer* player) {
151 player->joystick = 0;
152
153 if (events->playersAttached >= MAX_PLAYERS) {
154 return false;
155 }
156
157#if SDL_VERSION_ATLEAST(2, 0, 0)
158 player->rumble.d.setRumble = _GBASDLSetRumble;
159 CircleBufferInit(&player->rumble.history, RUMBLE_PWM);
160 player->rumble.level = 0;
161 player->rumble.p = player;
162#endif
163
164 player->rotation.d.readTiltX = _GBASDLReadTiltX;
165 player->rotation.d.readTiltY = _GBASDLReadTiltY;
166 player->rotation.d.readGyroZ = _GBASDLReadGyroZ;
167 player->rotation.d.sample = _GBASDLRotationSample;
168 player->rotation.axisX = 2;
169 player->rotation.axisY = 3;
170 player->rotation.gyroSensitivity = 2.2e9f;
171 player->rotation.gyroX = 0;
172 player->rotation.gyroY = 1;
173 player->rotation.zDelta = 0;
174 CircleBufferInit(&player->rotation.zHistory, sizeof(float) * GYRO_STEPS);
175 player->rotation.p = player;
176
177 player->playerId = events->playersAttached;
178 events->players[player->playerId] = player;
179 size_t firstUnclaimed = SIZE_MAX;
180 size_t index = SIZE_MAX;
181
182 size_t i;
183 for (i = 0; i < SDL_JoystickListSize(&events->joysticks); ++i) {
184 bool claimed = false;
185
186 int p;
187 for (p = 0; p < events->playersAttached; ++p) {
188 if (events->players[p]->joystick == SDL_JoystickListGetPointer(&events->joysticks, i)) {
189 claimed = true;
190 break;
191 }
192 }
193 if (claimed) {
194 continue;
195 }
196
197 if (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 (events->preferredJoysticks[player->playerId] && strcmp(events->preferredJoysticks[player->playerId], joystickName) == 0) {
208 index = i;
209 break;
210 }
211 }
212
213 if (index == SIZE_MAX && firstUnclaimed != SIZE_MAX) {
214 index = firstUnclaimed;
215 }
216
217 if (index != SIZE_MAX) {
218 player->joystick = SDL_JoystickListGetPointer(&events->joysticks, index);
219
220#if SDL_VERSION_ATLEAST(2, 0, 0)
221 if (player->joystick->haptic) {
222 SDL_HapticRumbleInit(player->joystick->haptic);
223 }
224#endif
225 }
226
227 ++events->playersAttached;
228 return true;
229}
230
231void GBASDLDetachPlayer(struct GBASDLEvents* events, struct GBASDLPlayer* player) {
232 if (player != events->players[player->playerId]) {
233 return;
234 }
235 int i;
236 for (i = player->playerId; i < events->playersAttached; ++i) {
237 if (i + 1 < MAX_PLAYERS) {
238 events->players[i] = events->players[i + 1];
239 }
240 if (i < events->playersAttached - 1) {
241 events->players[i]->playerId = i;
242 }
243 }
244 --events->playersAttached;
245 CircleBufferDeinit(&player->rotation.zHistory);
246}
247
248void GBASDLPlayerLoadConfig(struct GBASDLPlayer* context, const struct Configuration* config) {
249 GBAInputMapLoad(context->bindings, SDL_BINDING_KEY, config);
250 if (context->joystick) {
251 GBAInputMapLoad(context->bindings, SDL_BINDING_BUTTON, config);
252#if SDL_VERSION_ATLEAST(2, 0, 0)
253 const char* name = SDL_JoystickName(context->joystick->joystick);
254#else
255 const char* name = SDL_JoystickName(SDL_JoystickIndex(context->joystick->joystick));
256#endif
257 GBAInputProfileLoad(context->bindings, SDL_BINDING_BUTTON, config, name);
258
259 const char* value;
260 char* end;
261 int numAxes = SDL_JoystickNumAxes(context->joystick->joystick);
262 int axis;
263 value = GBAInputGetCustomValue(config, SDL_BINDING_BUTTON, "tiltAxisX", name);
264 if (value) {
265 axis = strtol(value, &end, 0);
266 if (axis >= 0 && axis < numAxes && end && !*end) {
267 context->rotation.axisX = axis;
268 }
269 }
270 value = GBAInputGetCustomValue(config, SDL_BINDING_BUTTON, "tiltAxisY", name);
271 if (value) {
272 axis = strtol(value, &end, 0);
273 if (axis >= 0 && axis < numAxes && end && !*end) {
274 context->rotation.axisY = axis;
275 }
276 }
277 value = GBAInputGetCustomValue(config, SDL_BINDING_BUTTON, "gyroAxisX", name);
278 if (value) {
279 axis = strtol(value, &end, 0);
280 if (axis >= 0 && axis < numAxes && end && !*end) {
281 context->rotation.gyroX = axis;
282 }
283 }
284 value = GBAInputGetCustomValue(config, SDL_BINDING_BUTTON, "gyroAxisY", name);
285 if (value) {
286 axis = strtol(value, &end, 0);
287 if (axis >= 0 && axis < numAxes && end && !*end) {
288 context->rotation.gyroY = axis;
289 }
290 }
291 value = GBAInputGetCustomValue(config, SDL_BINDING_BUTTON, "gyroSensitivity", name);
292 if (value) {
293 float sensitivity = strtof_u(value, &end);
294 if (end && !*end) {
295 context->rotation.gyroSensitivity = sensitivity;
296 }
297 }
298 }
299}
300
301void GBASDLPlayerSaveConfig(const struct GBASDLPlayer* context, struct Configuration* config) {
302 if (context->joystick) {
303#if SDL_VERSION_ATLEAST(2, 0, 0)
304 const char* name = SDL_JoystickName(context->joystick->joystick);
305#else
306 const char* name = SDL_JoystickName(SDL_JoystickIndex(context->joystick->joystick));
307#endif
308 char value[12];
309 snprintf(value, sizeof(value), "%i", context->rotation.axisX);
310 GBAInputSetCustomValue(config, SDL_BINDING_BUTTON, "tiltAxisX", value, name);
311 snprintf(value, sizeof(value), "%i", context->rotation.axisY);
312 GBAInputSetCustomValue(config, SDL_BINDING_BUTTON, "tiltAxisY", value, name);
313 snprintf(value, sizeof(value), "%i", context->rotation.gyroX);
314 GBAInputSetCustomValue(config, SDL_BINDING_BUTTON, "gyroAxisX", value, name);
315 snprintf(value, sizeof(value), "%i", context->rotation.gyroY);
316 GBAInputSetCustomValue(config, SDL_BINDING_BUTTON, "gyroAxisY", value, name);
317 snprintf(value, sizeof(value), "%g", context->rotation.gyroSensitivity);
318 GBAInputSetCustomValue(config, SDL_BINDING_BUTTON, "gyroSensitivity", value, name);
319 }
320}
321
322void GBASDLPlayerChangeJoystick(struct GBASDLEvents* events, struct GBASDLPlayer* player, size_t index) {
323 if (player->playerId >= MAX_PLAYERS || index >= SDL_JoystickListSize(&events->joysticks)) {
324 return;
325 }
326 player->joystick = SDL_JoystickListGetPointer(&events->joysticks, index);
327}
328
329void GBASDLUpdateJoysticks(struct GBASDLEvents* events) {
330 // Pump SDL joystick events without eating the rest of the events
331 SDL_JoystickUpdate();
332#if SDL_VERSION_ATLEAST(2, 0, 0)
333 SDL_Event event;
334 while (SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_JOYDEVICEADDED, SDL_JOYDEVICEREMOVED) > 0) {
335 if (event.type == SDL_JOYDEVICEADDED) {
336 struct SDL_JoystickCombo* joystick = SDL_JoystickListAppend(&events->joysticks);
337 joystick->joystick = SDL_JoystickOpen(event.jdevice.which);
338 joystick->id = SDL_JoystickInstanceID(joystick->joystick);
339 joystick->index = SDL_JoystickListSize(&events->joysticks) - 1;
340#if SDL_VERSION_ATLEAST(2, 0, 0)
341 joystick->haptic = SDL_HapticOpenFromJoystick(joystick->joystick);
342#endif
343 } else if (event.type == SDL_JOYDEVICEREMOVED) {
344 SDL_JoystickID ids[MAX_PLAYERS];
345 size_t i;
346 for (i = 0; (int) i < events->playersAttached; ++i) {
347 if (events->players[i]->joystick) {
348 ids[i] = events->players[i]->joystick->id;
349 events->players[i]->joystick = 0;
350 } else {
351 ids[i] = -1;
352 }
353 }
354 for (i = 0; i < SDL_JoystickListSize(&events->joysticks);) {
355 struct SDL_JoystickCombo* joystick = SDL_JoystickListGetPointer(&events->joysticks, i);
356 if (joystick->id == event.jdevice.which) {
357 SDL_JoystickListShift(&events->joysticks, i, 1);
358 continue;
359 }
360 SDL_JoystickListGetPointer(&events->joysticks, i)->index = i;
361 int p;
362 for (p = 0; p < events->playersAttached; ++p) {
363 if (joystick->id == ids[p]) {
364 events->players[p]->joystick = SDL_JoystickListGetPointer(&events->joysticks, i);
365 }
366 }
367 ++i;
368 }
369 }
370 }
371#endif
372}
373
374static void _pauseAfterFrame(struct GBAThread* context) {
375 context->frameCallback = 0;
376 GBAThreadPauseFromThread(context);
377}
378
379static void _GBASDLHandleKeypress(struct GBAThread* context, struct GBASDLPlayer* sdlContext, const struct SDL_KeyboardEvent* event) {
380 enum GBAKey key = GBA_KEY_NONE;
381 if (!event->keysym.mod) {
382#if !defined(BUILD_PANDORA) && SDL_VERSION_ATLEAST(2, 0, 0)
383 key = GBAInputMapKey(sdlContext->bindings, SDL_BINDING_KEY, event->keysym.scancode);
384#else
385 key = GBAInputMapKey(sdlContext->bindings, SDL_BINDING_KEY, event->keysym.sym);
386#endif
387 }
388 if (key != GBA_KEY_NONE) {
389 if (event->type == SDL_KEYDOWN) {
390 context->activeKeys |= 1 << key;
391 } else {
392 context->activeKeys &= ~(1 << key);
393 }
394 return;
395 }
396 if (event->keysym.sym == SDLK_TAB) {
397 context->sync.audioWait = event->type != SDL_KEYDOWN;
398 return;
399 }
400 if (event->type == SDL_KEYDOWN) {
401 switch (event->keysym.sym) {
402 case SDLK_F11:
403 if (context->debugger) {
404 ARMDebuggerEnter(context->debugger, DEBUGGER_ENTER_MANUAL, 0);
405 }
406 return;
407#ifdef USE_PNG
408 case SDLK_F12:
409 GBAThreadInterrupt(context);
410 GBAThreadTakeScreenshot(context);
411 GBAThreadContinue(context);
412 return;
413#endif
414 case SDLK_BACKSLASH:
415 GBAThreadPause(context);
416 context->frameCallback = _pauseAfterFrame;
417 GBAThreadUnpause(context);
418 return;
419 case SDLK_BACKQUOTE:
420 GBAThreadInterrupt(context);
421 GBARewind(context, 10);
422 GBAThreadContinue(context);
423 return;
424#ifdef BUILD_PANDORA
425 case SDLK_ESCAPE:
426 GBAThreadEnd(context);
427 return;
428#endif
429 default:
430 if ((event->keysym.mod & GUI_MOD) && (event->keysym.mod & GUI_MOD) == event->keysym.mod) {
431 switch (event->keysym.sym) {
432#if SDL_VERSION_ATLEAST(2, 0, 0)
433 case SDLK_f:
434 SDL_SetWindowFullscreen(sdlContext->window, sdlContext->fullscreen ? 0 : SDL_WINDOW_FULLSCREEN_DESKTOP);
435 sdlContext->fullscreen = !sdlContext->fullscreen;
436 sdlContext->windowUpdated = 1;
437 break;
438#endif
439 case SDLK_p:
440 GBAThreadTogglePause(context);
441 break;
442 case SDLK_n:
443 GBAThreadPause(context);
444 context->frameCallback = _pauseAfterFrame;
445 GBAThreadUnpause(context);
446 break;
447 case SDLK_r:
448 GBAThreadReset(context);
449 break;
450 default:
451 break;
452 }
453 }
454 if (event->keysym.mod & KMOD_SHIFT) {
455 switch (event->keysym.sym) {
456 case SDLK_F1:
457 case SDLK_F2:
458 case SDLK_F3:
459 case SDLK_F4:
460 case SDLK_F5:
461 case SDLK_F6:
462 case SDLK_F7:
463 case SDLK_F8:
464 case SDLK_F9:
465 GBAThreadInterrupt(context);
466 GBASaveState(context, context->dirs.state, event->keysym.sym - SDLK_F1 + 1, SAVESTATE_SCREENSHOT);
467 GBAThreadContinue(context);
468 break;
469 default:
470 break;
471 }
472 } else {
473 switch (event->keysym.sym) {
474 case SDLK_F1:
475 case SDLK_F2:
476 case SDLK_F3:
477 case SDLK_F4:
478 case SDLK_F5:
479 case SDLK_F6:
480 case SDLK_F7:
481 case SDLK_F8:
482 case SDLK_F9:
483 GBAThreadInterrupt(context);
484 GBALoadState(context, context->dirs.state, event->keysym.sym - SDLK_F1 + 1, SAVESTATE_SCREENSHOT);
485 GBAThreadContinue(context);
486 break;
487 default:
488 break;
489 }
490 }
491 return;
492 }
493 }
494}
495
496static void _GBASDLHandleJoyButton(struct GBAThread* context, struct GBASDLPlayer* sdlContext, const struct SDL_JoyButtonEvent* event) {
497 enum GBAKey key = 0;
498 key = GBAInputMapKey(sdlContext->bindings, SDL_BINDING_BUTTON, event->button);
499 if (key == GBA_KEY_NONE) {
500 return;
501 }
502
503 if (event->type == SDL_JOYBUTTONDOWN) {
504 context->activeKeys |= 1 << key;
505 } else {
506 context->activeKeys &= ~(1 << key);
507 }
508}
509
510static void _GBASDLHandleJoyHat(struct GBAThread* context, const struct SDL_JoyHatEvent* event) {
511 enum GBAKey key = 0;
512
513 if (event->value & SDL_HAT_UP) {
514 key |= 1 << GBA_KEY_UP;
515 }
516 if (event->value & SDL_HAT_LEFT) {
517 key |= 1 << GBA_KEY_LEFT;
518 }
519 if (event->value & SDL_HAT_DOWN) {
520 key |= 1 << GBA_KEY_DOWN;
521 }
522 if (event->value & SDL_HAT_RIGHT) {
523 key |= 1 << GBA_KEY_RIGHT;
524 }
525
526 context->activeKeys &= ~((1 << GBA_KEY_UP) | (1 << GBA_KEY_LEFT) | (1 << GBA_KEY_DOWN) | (1 << GBA_KEY_RIGHT));
527 context->activeKeys |= key;
528}
529
530static void _GBASDLHandleJoyAxis(struct GBAThread* context, struct GBASDLPlayer* sdlContext, const struct SDL_JoyAxisEvent* event) {
531 int keys = context->activeKeys;
532
533 keys = GBAInputClearAxis(sdlContext->bindings, SDL_BINDING_BUTTON, event->axis, keys);
534 enum GBAKey key = GBAInputMapAxis(sdlContext->bindings, SDL_BINDING_BUTTON, event->axis, event->value);
535 if (key != GBA_KEY_NONE) {
536 keys |= 1 << key;
537 }
538
539 context->activeKeys = keys;
540}
541
542#if SDL_VERSION_ATLEAST(2, 0, 0)
543static void _GBASDLHandleWindowEvent(struct GBAThread* context, struct GBASDLPlayer* sdlContext, const struct SDL_WindowEvent* event) {
544 UNUSED(context);
545 switch (event->event) {
546 case SDL_WINDOWEVENT_SIZE_CHANGED:
547 sdlContext->windowUpdated = 1;
548 break;
549 }
550}
551#endif
552
553void GBASDLHandleEvent(struct GBAThread* context, struct GBASDLPlayer* sdlContext, const union SDL_Event* event) {
554 switch (event->type) {
555 case SDL_QUIT:
556 GBAThreadEnd(context);
557 break;
558#if SDL_VERSION_ATLEAST(2, 0, 0)
559 case SDL_WINDOWEVENT:
560 _GBASDLHandleWindowEvent(context, sdlContext, &event->window);
561 break;
562#endif
563 case SDL_KEYDOWN:
564 case SDL_KEYUP:
565 _GBASDLHandleKeypress(context, sdlContext, &event->key);
566 break;
567 case SDL_JOYBUTTONDOWN:
568 case SDL_JOYBUTTONUP:
569 _GBASDLHandleJoyButton(context, sdlContext, &event->jbutton);
570 break;
571 case SDL_JOYHATMOTION:
572 _GBASDLHandleJoyHat(context, &event->jhat);
573 break;
574 case SDL_JOYAXISMOTION:
575 _GBASDLHandleJoyAxis(context, sdlContext, &event->jaxis);
576 break;
577 }
578}
579
580#if SDL_VERSION_ATLEAST(2, 0, 0)
581static void _GBASDLSetRumble(struct GBARumble* rumble, int enable) {
582 struct GBASDLRumble* sdlRumble = (struct GBASDLRumble*) rumble;
583 if (!sdlRumble->p->joystick->haptic || !SDL_HapticRumbleSupported(sdlRumble->p->joystick->haptic)) {
584 return;
585 }
586 sdlRumble->level += enable;
587 if (CircleBufferSize(&sdlRumble->history) == RUMBLE_PWM) {
588 int8_t oldLevel;
589 CircleBufferRead8(&sdlRumble->history, &oldLevel);
590 sdlRumble->level -= oldLevel;
591 }
592 CircleBufferWrite8(&sdlRumble->history, enable);
593 if (sdlRumble->level) {
594 SDL_HapticRumblePlay(sdlRumble->p->joystick->haptic, sdlRumble->level / (float) RUMBLE_PWM, 20);
595 } else {
596 SDL_HapticRumbleStop(sdlRumble->p->joystick->haptic);
597 }
598}
599#endif
600
601static int32_t _readTilt(struct GBASDLPlayer* player, int axis) {
602 return SDL_JoystickGetAxis(player->joystick->joystick, axis) * 0x3800;
603}
604
605static int32_t _GBASDLReadTiltX(struct GBARotationSource* source) {
606 struct GBASDLRotation* rotation = (struct GBASDLRotation*) source;
607 return _readTilt(rotation->p, rotation->axisX);
608}
609
610static int32_t _GBASDLReadTiltY(struct GBARotationSource* source) {
611 struct GBASDLRotation* rotation = (struct GBASDLRotation*) source;
612 return _readTilt(rotation->p, rotation->axisY);
613}
614
615static int32_t _GBASDLReadGyroZ(struct GBARotationSource* source) {
616 struct GBASDLRotation* rotation = (struct GBASDLRotation*) source;
617 float z = rotation->zDelta;
618 return z * rotation->gyroSensitivity;
619}
620
621static void _GBASDLRotationSample(struct GBARotationSource* source) {
622 struct GBASDLRotation* rotation = (struct GBASDLRotation*) source;
623 SDL_JoystickUpdate();
624
625 int x = SDL_JoystickGetAxis(rotation->p->joystick->joystick, rotation->gyroX);
626 int y = SDL_JoystickGetAxis(rotation->p->joystick->joystick, rotation->gyroY);
627 union {
628 float f;
629 int32_t i;
630 } theta = { .f = atan2f(y, x) - atan2f(rotation->oldY, rotation->oldX) };
631 if (isnan(theta.f)) {
632 theta.f = 0.0f;
633 } else if (theta.f > M_PI) {
634 theta.f -= 2.0f * M_PI;
635 } else if (theta.f < -M_PI) {
636 theta.f += 2.0f * M_PI;
637 }
638 rotation->oldX = x;
639 rotation->oldY = y;
640
641 float oldZ = 0;
642 if (CircleBufferSize(&rotation->zHistory) == GYRO_STEPS * sizeof(float)) {
643 CircleBufferRead32(&rotation->zHistory, (int32_t*) &oldZ);
644 }
645 CircleBufferWrite32(&rotation->zHistory, theta.i);
646 rotation->zDelta += theta.f - oldZ;
647}
648
649#if SDL_VERSION_ATLEAST(2, 0, 0)
650void GBASDLSuspendScreensaver(struct GBASDLEvents* events) {
651 if (events->screensaverSuspendDepth == 0 && events->screensaverSuspendable) {
652 SDL_DisableScreenSaver();
653 }
654 ++events->screensaverSuspendDepth;
655}
656
657void GBASDLResumeScreensaver(struct GBASDLEvents* events) {
658 --events->screensaverSuspendDepth;
659 if (events->screensaverSuspendDepth == 0 && events->screensaverSuspendable) {
660 SDL_EnableScreenSaver();
661 }
662}
663
664void GBASDLSetScreensaverSuspendable(struct GBASDLEvents* events, bool suspendable) {
665 bool wasSuspendable = events->screensaverSuspendable;
666 events->screensaverSuspendable = suspendable;
667 if (events->screensaverSuspendDepth > 0) {
668 if (suspendable && !wasSuspendable) {
669 SDL_DisableScreenSaver();
670 } else if (!suspendable && wasSuspendable) {
671 SDL_EnableScreenSaver();
672 }
673 } else {
674 SDL_EnableScreenSaver();
675 }
676}
677#endif