all repos — mgba @ 104a80422c57ef225429ea0f93e737f07c4aa01c

mGBA Game Boy Advance Emulator

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