all repos — mgba @ e797d009d0da181ba0587927a9da96ef8cf4eef9

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