all repos — mgba @ 7f443f2fae0b217b611ff609bd3da367bc1948b3

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 "core/input.h"
  9#include "core/serialize.h"
 10#include "core/thread.h"
 11#include "debugger/debugger.h"
 12#include "gba/input.h"
 13#include "gba/io.h"
 14#include "gba/rr/rr.h"
 15#include "gba/video.h"
 16#include "gba/renderers/video-software.h"
 17#include "util/configuration.h"
 18#include "util/formatting.h"
 19#include "util/vfs.h"
 20
 21#if SDL_VERSION_ATLEAST(2, 0, 0) && defined(__APPLE__)
 22#define GUI_MOD KMOD_GUI
 23#else
 24#define GUI_MOD KMOD_CTRL
 25#endif
 26
 27#define GYRO_STEPS 100
 28#define RUMBLE_PWM 16
 29#define RUMBLE_STEPS 2
 30
 31mLOG_DEFINE_CATEGORY(SDL_EVENTS, "SDL Events");
 32
 33DEFINE_VECTOR(SDL_JoystickList, struct SDL_JoystickCombo);
 34
 35#if SDL_VERSION_ATLEAST(2, 0, 0)
 36static void _mSDLSetRumble(struct mRumble* rumble, int enable);
 37#endif
 38static int32_t _mSDLReadTiltX(struct mRotationSource* rumble);
 39static int32_t _mSDLReadTiltY(struct mRotationSource* rumble);
 40static int32_t _mSDLReadGyroZ(struct mRotationSource* rumble);
 41static void _mSDLRotationSample(struct mRotationSource* source);
 42
 43bool mSDLInitEvents(struct mSDLEvents* context) {
 44#if SDL_VERSION_ATLEAST(2, 0, 4)
 45	SDL_SetHint(SDL_HINT_NO_SIGNAL_HANDLERS, "1");
 46#endif
 47	if (SDL_InitSubSystem(SDL_INIT_JOYSTICK) < 0) {
 48		mLOG(SDL_EVENTS, ERROR, "SDL joystick initialization failed: %s", SDL_GetError());
 49	}
 50
 51#if SDL_VERSION_ATLEAST(2, 0, 0)
 52	SDL_SetHint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS, "1");
 53	if (SDL_InitSubSystem(SDL_INIT_HAPTIC) < 0) {
 54		mLOG(SDL_EVENTS, ERROR, "SDL haptic initialization failed: %s", SDL_GetError());
 55	}
 56	if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0) {
 57		mLOG(SDL_EVENTS, ERROR, "SDL video initialization failed: %s", SDL_GetError());
 58	}
 59#endif
 60
 61	SDL_JoystickEventState(SDL_ENABLE);
 62	int nJoysticks = SDL_NumJoysticks();
 63	SDL_JoystickListInit(&context->joysticks, nJoysticks);
 64	if (nJoysticks > 0) {
 65		mSDLUpdateJoysticks(context);
 66		// Some OSes don't do hotplug detection
 67		if (!SDL_JoystickListSize(&context->joysticks)) {
 68			int i;
 69			for (i = 0; i < nJoysticks; ++i) {
 70				struct SDL_JoystickCombo* joystick = SDL_JoystickListAppend(&context->joysticks);
 71				joystick->joystick = SDL_JoystickOpen(i);
 72				joystick->index = SDL_JoystickListSize(&context->joysticks) - 1;
 73#if SDL_VERSION_ATLEAST(2, 0, 0)
 74				joystick->id = SDL_JoystickInstanceID(joystick->joystick);
 75				joystick->haptic = SDL_HapticOpenFromJoystick(joystick->joystick);
 76#else
 77				joystick->id = SDL_JoystickIndex(joystick->joystick);
 78#endif
 79			}
 80		}
 81	}
 82
 83	context->playersAttached = 0;
 84
 85	size_t i;
 86	for (i = 0; i < MAX_PLAYERS; ++i) {
 87		context->preferredJoysticks[i] = 0;
 88	}
 89
 90#if !SDL_VERSION_ATLEAST(2, 0, 0)
 91	SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
 92#else
 93	context->screensaverSuspendDepth = 0;
 94#endif
 95	return true;
 96}
 97
 98void mSDLDeinitEvents(struct mSDLEvents* context) {
 99	size_t i;
100	for (i = 0; i < SDL_JoystickListSize(&context->joysticks); ++i) {
101		struct SDL_JoystickCombo* joystick = SDL_JoystickListGetPointer(&context->joysticks, i);
102#if SDL_VERSION_ATLEAST(2, 0, 0)
103		SDL_HapticClose(joystick->haptic);
104#endif
105		SDL_JoystickClose(joystick->joystick);
106	}
107	SDL_JoystickListDeinit(&context->joysticks);
108	SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
109}
110
111void mSDLEventsLoadConfig(struct mSDLEvents* context, const struct Configuration* config) {
112	context->preferredJoysticks[0] = mInputGetPreferredDevice(config, "gba", SDL_BINDING_BUTTON, 0);
113	context->preferredJoysticks[1] = mInputGetPreferredDevice(config, "gba", SDL_BINDING_BUTTON, 1);
114	context->preferredJoysticks[2] = mInputGetPreferredDevice(config, "gba", SDL_BINDING_BUTTON, 2);
115	context->preferredJoysticks[3] = mInputGetPreferredDevice(config, "gba", SDL_BINDING_BUTTON, 3);
116}
117
118void mSDLInitBindingsGBA(struct mInputMap* inputMap) {
119#ifdef BUILD_PANDORA
120	mInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_PAGEDOWN, GBA_KEY_A);
121	mInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_END, GBA_KEY_B);
122	mInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_RSHIFT, GBA_KEY_L);
123	mInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_RCTRL, GBA_KEY_R);
124	mInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_LALT, GBA_KEY_START);
125	mInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_LCTRL, GBA_KEY_SELECT);
126	mInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_UP, GBA_KEY_UP);
127	mInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_DOWN, GBA_KEY_DOWN);
128	mInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_LEFT, GBA_KEY_LEFT);
129	mInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_RIGHT, GBA_KEY_RIGHT);
130#else
131	mInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_x, GBA_KEY_A);
132	mInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_z, GBA_KEY_B);
133	mInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_a, GBA_KEY_L);
134	mInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_s, GBA_KEY_R);
135	mInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_RETURN, GBA_KEY_START);
136	mInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_BACKSPACE, GBA_KEY_SELECT);
137	mInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_UP, GBA_KEY_UP);
138	mInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_DOWN, GBA_KEY_DOWN);
139	mInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_LEFT, GBA_KEY_LEFT);
140	mInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_RIGHT, GBA_KEY_RIGHT);
141#endif
142
143	struct mInputAxis description = { GBA_KEY_RIGHT, GBA_KEY_LEFT, 0x4000, -0x4000 };
144	mInputBindAxis(inputMap, SDL_BINDING_BUTTON, 0, &description);
145	description = (struct mInputAxis) { GBA_KEY_DOWN, GBA_KEY_UP, 0x4000, -0x4000 };
146	mInputBindAxis(inputMap, SDL_BINDING_BUTTON, 1, &description);
147}
148
149bool mSDLAttachPlayer(struct mSDLEvents* events, struct mSDLPlayer* player) {
150	player->joystick = 0;
151
152	if (events->playersAttached >= MAX_PLAYERS) {
153		return false;
154	}
155
156#if SDL_VERSION_ATLEAST(2, 0, 0)
157	player->rumble.d.setRumble = _mSDLSetRumble;
158	CircleBufferInit(&player->rumble.history, RUMBLE_PWM);
159	player->rumble.level = 0;
160	player->rumble.activeLevel = 0;
161	player->rumble.p = player;
162#endif
163
164	player->rotation.d.readTiltX = _mSDLReadTiltX;
165	player->rotation.d.readTiltY = _mSDLReadTiltY;
166	player->rotation.d.readGyroZ = _mSDLReadGyroZ;
167	player->rotation.d.sample = _mSDLRotationSample;
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 mSDLDetachPlayer(struct mSDLEvents* events, struct mSDLPlayer* 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 mSDLPlayerLoadConfig(struct mSDLPlayer* context, const struct Configuration* config) {
249	mInputMapLoad(context->bindings, SDL_BINDING_KEY, config);
250	if (context->joystick) {
251		mInputMapLoad(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		mInputProfileLoad(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 = mInputGetCustomValue(config, "gba", 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 = mInputGetCustomValue(config, "gba", 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 = mInputGetCustomValue(config, "gba", 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 = mInputGetCustomValue(config, "gba", 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 = mInputGetCustomValue(config, "gba", 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 mSDLPlayerSaveConfig(const struct mSDLPlayer* 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		mInputSetCustomValue(config, "gba", SDL_BINDING_BUTTON, "tiltAxisX", value, name);
311		snprintf(value, sizeof(value), "%i", context->rotation.axisY);
312		mInputSetCustomValue(config, "gba", SDL_BINDING_BUTTON, "tiltAxisY", value, name);
313		snprintf(value, sizeof(value), "%i", context->rotation.gyroX);
314		mInputSetCustomValue(config, "gba", SDL_BINDING_BUTTON, "gyroAxisX", value, name);
315		snprintf(value, sizeof(value), "%i", context->rotation.gyroY);
316		mInputSetCustomValue(config, "gba", SDL_BINDING_BUTTON, "gyroAxisY", value, name);
317		snprintf(value, sizeof(value), "%g", context->rotation.gyroSensitivity);
318		mInputSetCustomValue(config, "gba", SDL_BINDING_BUTTON, "gyroSensitivity", value, name);
319	}
320}
321
322void mSDLPlayerChangeJoystick(struct mSDLEvents* events, struct mSDLPlayer* 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 mSDLUpdateJoysticks(struct mSDLEvents* 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 mCoreThread* context) {
375	context->frameCallback = 0;
376	mCoreThreadPauseFromThread(context);
377}
378
379static void _mSDLHandleKeypress(struct mCoreThread* context, struct mSDLPlayer* sdlContext, const struct SDL_KeyboardEvent* event) {
380	int key = -1;
381	if (!event->keysym.mod) {
382		key = mInputMapKey(sdlContext->bindings, SDL_BINDING_KEY, event->keysym.sym);
383	}
384	if (key != -1) {
385		if (event->type == SDL_KEYDOWN) {
386			context->core->addKeys(context->core, 1 << key);
387		} else {
388			context->core->clearKeys(context->core, 1 << key);
389		}
390		return;
391	}
392	if (event->keysym.sym == SDLK_TAB) {
393		context->sync.audioWait = event->type != SDL_KEYDOWN;
394		return;
395	}
396	if (event->keysym.sym == SDLK_BACKQUOTE) {
397		mCoreThreadSetRewinding(context, event->type == SDL_KEYDOWN);
398	}
399	if (event->type == SDL_KEYDOWN) {
400		switch (event->keysym.sym) {
401		case SDLK_F11:
402			if (context->core->debugger) {
403				mDebuggerEnter(context->core->debugger, DEBUGGER_ENTER_MANUAL, NULL);
404			}
405			return;
406#ifdef USE_PNG
407		case SDLK_F12:
408			mCoreTakeScreenshot(context->core);
409			return;
410#endif
411		case SDLK_BACKSLASH:
412			mCoreThreadPause(context);
413			context->frameCallback = _pauseAfterFrame;
414			mCoreThreadUnpause(context);
415			return;
416#ifdef BUILD_PANDORA
417		case SDLK_ESCAPE:
418			mCoreThreadEnd(context);
419			return;
420#endif
421		default:
422			if ((event->keysym.mod & GUI_MOD) && (event->keysym.mod & GUI_MOD) == event->keysym.mod) {
423				switch (event->keysym.sym) {
424#if SDL_VERSION_ATLEAST(2, 0, 0)
425				case SDLK_f:
426					SDL_SetWindowFullscreen(sdlContext->window, sdlContext->fullscreen ? 0 : SDL_WINDOW_FULLSCREEN_DESKTOP);
427					sdlContext->fullscreen = !sdlContext->fullscreen;
428					sdlContext->windowUpdated = 1;
429					break;
430#endif
431				case SDLK_p:
432					mCoreThreadTogglePause(context);
433					break;
434				case SDLK_n:
435					mCoreThreadPause(context);
436					context->frameCallback = _pauseAfterFrame;
437					mCoreThreadUnpause(context);
438					break;
439				case SDLK_r:
440					mCoreThreadReset(context);
441					break;
442				default:
443					break;
444				}
445			}
446			if (event->keysym.mod & KMOD_SHIFT) {
447				switch (event->keysym.sym) {
448				case SDLK_F1:
449				case SDLK_F2:
450				case SDLK_F3:
451				case SDLK_F4:
452				case SDLK_F5:
453				case SDLK_F6:
454				case SDLK_F7:
455				case SDLK_F8:
456				case SDLK_F9:
457					mCoreThreadInterrupt(context);
458					mCoreSaveState(context->core, event->keysym.sym - SDLK_F1 + 1, SAVESTATE_SAVEDATA | SAVESTATE_SCREENSHOT);
459					mCoreThreadContinue(context);
460					break;
461				default:
462					break;
463				}
464			} else {
465				switch (event->keysym.sym) {
466				case SDLK_F1:
467				case SDLK_F2:
468				case SDLK_F3:
469				case SDLK_F4:
470				case SDLK_F5:
471				case SDLK_F6:
472				case SDLK_F7:
473				case SDLK_F8:
474				case SDLK_F9:
475					mCoreThreadInterrupt(context);
476					mCoreLoadState(context->core, event->keysym.sym - SDLK_F1 + 1, SAVESTATE_SCREENSHOT);
477					mCoreThreadContinue(context);
478					break;
479				default:
480					break;
481				}
482			}
483			return;
484		}
485	}
486}
487
488static void _mSDLHandleJoyButton(struct mCore* core, struct mSDLPlayer* sdlContext, const struct SDL_JoyButtonEvent* event) {
489	int key = 0;
490	key = mInputMapKey(sdlContext->bindings, SDL_BINDING_BUTTON, event->button);
491	if (key == -1) {
492		return;
493	}
494
495	if (event->type == SDL_JOYBUTTONDOWN) {
496		core->addKeys(core, 1 << key);
497	} else {
498		core->clearKeys(core, 1 << key);
499	}
500}
501
502static void _mSDLHandleJoyAxis(struct mCore* core, struct mSDLPlayer* sdlContext, const struct SDL_JoyAxisEvent* event) {
503	int clearKeys = ~mInputClearAxis(sdlContext->bindings, SDL_BINDING_BUTTON, event->axis, -1);
504	int newKeys = 0;
505	int key = mInputMapAxis(sdlContext->bindings, SDL_BINDING_BUTTON, event->axis, event->value);
506	if (key != -1) {
507		newKeys |= 1 << key;
508	}
509	clearKeys &= ~newKeys;
510	core->clearKeys(core, clearKeys);
511	core->addKeys(core, newKeys);
512
513}
514
515#if SDL_VERSION_ATLEAST(2, 0, 0)
516static void _mSDLHandleWindowEvent(struct mSDLPlayer* sdlContext, const struct SDL_WindowEvent* event) {
517	switch (event->event) {
518	case SDL_WINDOWEVENT_SIZE_CHANGED:
519		sdlContext->windowUpdated = 1;
520		break;
521	}
522}
523#endif
524
525void mSDLHandleEvent(struct mCoreThread* context, struct mSDLPlayer* sdlContext, const union SDL_Event* event) {
526	switch (event->type) {
527	case SDL_QUIT:
528		mCoreThreadEnd(context);
529		break;
530#if SDL_VERSION_ATLEAST(2, 0, 0)
531	case SDL_WINDOWEVENT:
532		_mSDLHandleWindowEvent(sdlContext, &event->window);
533		break;
534#endif
535	case SDL_KEYDOWN:
536	case SDL_KEYUP:
537		_mSDLHandleKeypress(context, sdlContext, &event->key);
538		break;
539	case SDL_JOYBUTTONDOWN:
540	case SDL_JOYBUTTONUP:
541		_mSDLHandleJoyButton(context->core, sdlContext, &event->jbutton);
542		break;
543	case SDL_JOYHATMOTION:
544		// TODO
545		break;
546	case SDL_JOYAXISMOTION:
547		_mSDLHandleJoyAxis(context->core, sdlContext, &event->jaxis);
548		break;
549	}
550}
551
552#if SDL_VERSION_ATLEAST(2, 0, 0)
553static void _mSDLSetRumble(struct mRumble* rumble, int enable) {
554	struct mSDLRumble* sdlRumble = (struct mSDLRumble*) rumble;
555	if (!sdlRumble->p->joystick || !sdlRumble->p->joystick->haptic || !SDL_HapticRumbleSupported(sdlRumble->p->joystick->haptic)) {
556		return;
557	}
558	int8_t originalLevel = sdlRumble->level;
559	sdlRumble->level += enable;
560	if (CircleBufferSize(&sdlRumble->history) == RUMBLE_PWM) {
561		int8_t oldLevel;
562		CircleBufferRead8(&sdlRumble->history, &oldLevel);
563		sdlRumble->level -= oldLevel;
564	}
565	CircleBufferWrite8(&sdlRumble->history, enable);
566	if (sdlRumble->level == originalLevel) {
567		return;
568	}
569	float activeLevel = ceil(RUMBLE_STEPS * sdlRumble->level / (float) RUMBLE_PWM) / RUMBLE_STEPS;
570	if (fabsf(sdlRumble->activeLevel - activeLevel) < 0.75 / RUMBLE_STEPS) {
571		return;
572	}
573	sdlRumble->activeLevel = activeLevel;
574	if (sdlRumble->activeLevel > 0.5 / RUMBLE_STEPS) {
575		SDL_HapticRumbleStop(sdlRumble->p->joystick->haptic);
576		SDL_HapticRumblePlay(sdlRumble->p->joystick->haptic, activeLevel, 500);
577	} else {
578		SDL_HapticRumbleStop(sdlRumble->p->joystick->haptic);
579	}
580}
581#endif
582
583static int32_t _readTilt(struct mSDLPlayer* player, int axis) {
584	if (!player->joystick) {
585		return 0;
586	}
587	return SDL_JoystickGetAxis(player->joystick->joystick, axis) * 0x3800;
588}
589
590static int32_t _mSDLReadTiltX(struct mRotationSource* source) {
591	struct mSDLRotation* rotation = (struct mSDLRotation*) source;
592	return _readTilt(rotation->p, rotation->axisX);
593}
594
595static int32_t _mSDLReadTiltY(struct mRotationSource* source) {
596	struct mSDLRotation* rotation = (struct mSDLRotation*) source;
597	return _readTilt(rotation->p, rotation->axisY);
598}
599
600static int32_t _mSDLReadGyroZ(struct mRotationSource* source) {
601	struct mSDLRotation* rotation = (struct mSDLRotation*) source;
602	float z = rotation->zDelta;
603	return z * rotation->gyroSensitivity;
604}
605
606static void _mSDLRotationSample(struct mRotationSource* source) {
607	struct mSDLRotation* rotation = (struct mSDLRotation*) source;
608	SDL_JoystickUpdate();
609	if (!rotation->p->joystick) {
610		return;
611	}
612
613	int x = SDL_JoystickGetAxis(rotation->p->joystick->joystick, rotation->gyroX);
614	int y = SDL_JoystickGetAxis(rotation->p->joystick->joystick, rotation->gyroY);
615	union {
616		float f;
617		int32_t i;
618	} theta = { .f = atan2f(y, x) - atan2f(rotation->oldY, rotation->oldX) };
619	if (isnan(theta.f)) {
620		theta.f = 0.0f;
621	} else if (theta.f > M_PI) {
622		theta.f -= 2.0f * M_PI;
623	} else if (theta.f < -M_PI) {
624		theta.f += 2.0f * M_PI;
625	}
626	rotation->oldX = x;
627	rotation->oldY = y;
628
629	float oldZ = 0;
630	if (CircleBufferSize(&rotation->zHistory) == GYRO_STEPS * sizeof(float)) {
631		CircleBufferRead32(&rotation->zHistory, (int32_t*) &oldZ);
632	}
633	CircleBufferWrite32(&rotation->zHistory, theta.i);
634	rotation->zDelta += theta.f - oldZ;
635}
636
637#if SDL_VERSION_ATLEAST(2, 0, 0)
638void mSDLSuspendScreensaver(struct mSDLEvents* events) {
639	if (events->screensaverSuspendDepth == 0 && events->screensaverSuspendable) {
640		SDL_DisableScreenSaver();
641	}
642	++events->screensaverSuspendDepth;
643}
644
645void mSDLResumeScreensaver(struct mSDLEvents* events) {
646	--events->screensaverSuspendDepth;
647	if (events->screensaverSuspendDepth == 0 && events->screensaverSuspendable) {
648		SDL_EnableScreenSaver();
649	}
650}
651
652void mSDLSetScreensaverSuspendable(struct mSDLEvents* events, bool suspendable) {
653	bool wasSuspendable = events->screensaverSuspendable;
654	events->screensaverSuspendable = suspendable;
655	if (events->screensaverSuspendDepth > 0) {
656		if (suspendable && !wasSuspendable) {
657			SDL_DisableScreenSaver();
658		} else if (!suspendable && wasSuspendable) {
659			SDL_EnableScreenSaver();
660		}
661	} else {
662		SDL_EnableScreenSaver();
663	}
664}
665#endif