all repos — mgba @ 6d898542c765f4efc4a094c5ebd3f3465c36f417

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#ifdef USE_DEBUGGERS
402		case SDLK_F11:
403			if (context->core->debugger) {
404				mDebuggerEnter(context->core->debugger, DEBUGGER_ENTER_MANUAL, NULL);
405			}
406			return;
407#endif
408#ifdef USE_PNG
409		case SDLK_F12:
410			mCoreTakeScreenshot(context->core);
411			return;
412#endif
413		case SDLK_BACKSLASH:
414			mCoreThreadPause(context);
415			context->frameCallback = _pauseAfterFrame;
416			mCoreThreadUnpause(context);
417			return;
418#ifdef BUILD_PANDORA
419		case SDLK_ESCAPE:
420			mCoreThreadEnd(context);
421			return;
422#endif
423		default:
424			if ((event->keysym.mod & GUI_MOD) && (event->keysym.mod & GUI_MOD) == event->keysym.mod) {
425				switch (event->keysym.sym) {
426#if SDL_VERSION_ATLEAST(2, 0, 0)
427				case SDLK_f:
428					SDL_SetWindowFullscreen(sdlContext->window, sdlContext->fullscreen ? 0 : SDL_WINDOW_FULLSCREEN_DESKTOP);
429					sdlContext->fullscreen = !sdlContext->fullscreen;
430					sdlContext->windowUpdated = 1;
431					break;
432#endif
433				case SDLK_p:
434					mCoreThreadTogglePause(context);
435					break;
436				case SDLK_n:
437					mCoreThreadPause(context);
438					context->frameCallback = _pauseAfterFrame;
439					mCoreThreadUnpause(context);
440					break;
441				case SDLK_r:
442					mCoreThreadReset(context);
443					break;
444				default:
445					break;
446				}
447			}
448			if (event->keysym.mod & KMOD_SHIFT) {
449				switch (event->keysym.sym) {
450				case SDLK_F1:
451				case SDLK_F2:
452				case SDLK_F3:
453				case SDLK_F4:
454				case SDLK_F5:
455				case SDLK_F6:
456				case SDLK_F7:
457				case SDLK_F8:
458				case SDLK_F9:
459					mCoreThreadInterrupt(context);
460					mCoreSaveState(context->core, event->keysym.sym - SDLK_F1 + 1, SAVESTATE_SAVEDATA | SAVESTATE_SCREENSHOT);
461					mCoreThreadContinue(context);
462					break;
463				default:
464					break;
465				}
466			} else {
467				switch (event->keysym.sym) {
468				case SDLK_F1:
469				case SDLK_F2:
470				case SDLK_F3:
471				case SDLK_F4:
472				case SDLK_F5:
473				case SDLK_F6:
474				case SDLK_F7:
475				case SDLK_F8:
476				case SDLK_F9:
477					mCoreThreadInterrupt(context);
478					mCoreLoadState(context->core, event->keysym.sym - SDLK_F1 + 1, SAVESTATE_SCREENSHOT);
479					mCoreThreadContinue(context);
480					break;
481				default:
482					break;
483				}
484			}
485			return;
486		}
487	}
488}
489
490static void _mSDLHandleJoyButton(struct mCore* core, struct mSDLPlayer* sdlContext, const struct SDL_JoyButtonEvent* event) {
491	int key = 0;
492	key = mInputMapKey(sdlContext->bindings, SDL_BINDING_BUTTON, event->button);
493	if (key == -1) {
494		return;
495	}
496
497	if (event->type == SDL_JOYBUTTONDOWN) {
498		core->addKeys(core, 1 << key);
499	} else {
500		core->clearKeys(core, 1 << key);
501	}
502}
503
504static void _mSDLHandleJoyAxis(struct mCore* core, struct mSDLPlayer* sdlContext, const struct SDL_JoyAxisEvent* event) {
505	int clearKeys = ~mInputClearAxis(sdlContext->bindings, SDL_BINDING_BUTTON, event->axis, -1);
506	int newKeys = 0;
507	int key = mInputMapAxis(sdlContext->bindings, SDL_BINDING_BUTTON, event->axis, event->value);
508	if (key != -1) {
509		newKeys |= 1 << key;
510	}
511	clearKeys &= ~newKeys;
512	core->clearKeys(core, clearKeys);
513	core->addKeys(core, newKeys);
514
515}
516
517#if SDL_VERSION_ATLEAST(2, 0, 0)
518static void _mSDLHandleWindowEvent(struct mSDLPlayer* sdlContext, const struct SDL_WindowEvent* event) {
519	switch (event->event) {
520	case SDL_WINDOWEVENT_SIZE_CHANGED:
521		sdlContext->windowUpdated = 1;
522		break;
523	}
524}
525#endif
526
527void mSDLHandleEvent(struct mCoreThread* context, struct mSDLPlayer* sdlContext, const union SDL_Event* event) {
528	switch (event->type) {
529	case SDL_QUIT:
530		mCoreThreadEnd(context);
531		break;
532#if SDL_VERSION_ATLEAST(2, 0, 0)
533	case SDL_WINDOWEVENT:
534		_mSDLHandleWindowEvent(sdlContext, &event->window);
535		break;
536#endif
537	case SDL_KEYDOWN:
538	case SDL_KEYUP:
539		_mSDLHandleKeypress(context, sdlContext, &event->key);
540		break;
541	case SDL_JOYBUTTONDOWN:
542	case SDL_JOYBUTTONUP:
543		_mSDLHandleJoyButton(context->core, sdlContext, &event->jbutton);
544		break;
545	case SDL_JOYHATMOTION:
546		// TODO
547		break;
548	case SDL_JOYAXISMOTION:
549		_mSDLHandleJoyAxis(context->core, sdlContext, &event->jaxis);
550		break;
551	}
552}
553
554#if SDL_VERSION_ATLEAST(2, 0, 0)
555static void _mSDLSetRumble(struct mRumble* rumble, int enable) {
556	struct mSDLRumble* sdlRumble = (struct mSDLRumble*) rumble;
557	if (!sdlRumble->p->joystick || !sdlRumble->p->joystick->haptic || !SDL_HapticRumbleSupported(sdlRumble->p->joystick->haptic)) {
558		return;
559	}
560	int8_t originalLevel = sdlRumble->level;
561	sdlRumble->level += enable;
562	if (CircleBufferSize(&sdlRumble->history) == RUMBLE_PWM) {
563		int8_t oldLevel;
564		CircleBufferRead8(&sdlRumble->history, &oldLevel);
565		sdlRumble->level -= oldLevel;
566	}
567	CircleBufferWrite8(&sdlRumble->history, enable);
568	if (sdlRumble->level == originalLevel) {
569		return;
570	}
571	float activeLevel = ceil(RUMBLE_STEPS * sdlRumble->level / (float) RUMBLE_PWM) / RUMBLE_STEPS;
572	if (fabsf(sdlRumble->activeLevel - activeLevel) < 0.75 / RUMBLE_STEPS) {
573		return;
574	}
575	sdlRumble->activeLevel = activeLevel;
576	if (sdlRumble->activeLevel > 0.5 / RUMBLE_STEPS) {
577		SDL_HapticRumbleStop(sdlRumble->p->joystick->haptic);
578		SDL_HapticRumblePlay(sdlRumble->p->joystick->haptic, activeLevel, 500);
579	} else {
580		SDL_HapticRumbleStop(sdlRumble->p->joystick->haptic);
581	}
582}
583#endif
584
585static int32_t _readTilt(struct mSDLPlayer* player, int axis) {
586	if (!player->joystick) {
587		return 0;
588	}
589	return SDL_JoystickGetAxis(player->joystick->joystick, axis) * 0x3800;
590}
591
592static int32_t _mSDLReadTiltX(struct mRotationSource* source) {
593	struct mSDLRotation* rotation = (struct mSDLRotation*) source;
594	return _readTilt(rotation->p, rotation->axisX);
595}
596
597static int32_t _mSDLReadTiltY(struct mRotationSource* source) {
598	struct mSDLRotation* rotation = (struct mSDLRotation*) source;
599	return _readTilt(rotation->p, rotation->axisY);
600}
601
602static int32_t _mSDLReadGyroZ(struct mRotationSource* source) {
603	struct mSDLRotation* rotation = (struct mSDLRotation*) source;
604	float z = rotation->zDelta;
605	return z * rotation->gyroSensitivity;
606}
607
608static void _mSDLRotationSample(struct mRotationSource* source) {
609	struct mSDLRotation* rotation = (struct mSDLRotation*) source;
610	SDL_JoystickUpdate();
611	if (!rotation->p->joystick) {
612		return;
613	}
614
615	int x = SDL_JoystickGetAxis(rotation->p->joystick->joystick, rotation->gyroX);
616	int y = SDL_JoystickGetAxis(rotation->p->joystick->joystick, rotation->gyroY);
617	union {
618		float f;
619		int32_t i;
620	} theta = { .f = atan2f(y, x) - atan2f(rotation->oldY, rotation->oldX) };
621	if (isnan(theta.f)) {
622		theta.f = 0.0f;
623	} else if (theta.f > M_PI) {
624		theta.f -= 2.0f * M_PI;
625	} else if (theta.f < -M_PI) {
626		theta.f += 2.0f * M_PI;
627	}
628	rotation->oldX = x;
629	rotation->oldY = y;
630
631	float oldZ = 0;
632	if (CircleBufferSize(&rotation->zHistory) == GYRO_STEPS * sizeof(float)) {
633		CircleBufferRead32(&rotation->zHistory, (int32_t*) &oldZ);
634	}
635	CircleBufferWrite32(&rotation->zHistory, theta.i);
636	rotation->zDelta += theta.f - oldZ;
637}
638
639#if SDL_VERSION_ATLEAST(2, 0, 0)
640void mSDLSuspendScreensaver(struct mSDLEvents* events) {
641	if (events->screensaverSuspendDepth == 0 && events->screensaverSuspendable) {
642		SDL_DisableScreenSaver();
643	}
644	++events->screensaverSuspendDepth;
645}
646
647void mSDLResumeScreensaver(struct mSDLEvents* events) {
648	--events->screensaverSuspendDepth;
649	if (events->screensaverSuspendDepth == 0 && events->screensaverSuspendable) {
650		SDL_EnableScreenSaver();
651	}
652}
653
654void mSDLSetScreensaverSuspendable(struct mSDLEvents* events, bool suspendable) {
655	bool wasSuspendable = events->screensaverSuspendable;
656	events->screensaverSuspendable = suspendable;
657	if (events->screensaverSuspendDepth > 0) {
658		if (suspendable && !wasSuspendable) {
659			SDL_DisableScreenSaver();
660		} else if (!suspendable && wasSuspendable) {
661			SDL_EnableScreenSaver();
662		}
663	} else {
664		SDL_EnableScreenSaver();
665	}
666}
667#endif