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