all repos — mgba @ 4fa1ad7ce0cb410a677bccdb5ea63c3edc353bf8

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/vfs.h"
 15
 16#if SDL_VERSION_ATLEAST(2, 0, 0) && defined(__APPLE__)
 17#define GUI_MOD KMOD_GUI
 18#else
 19#define GUI_MOD KMOD_CTRL
 20#endif
 21
 22static int _openContexts = 0;
 23
 24bool GBASDLInitEvents(struct GBASDLEvents* context) {
 25	if (!_openContexts && SDL_InitSubSystem(SDL_INIT_JOYSTICK) < 0) {
 26		return false;
 27	}
 28	++_openContexts;
 29	SDL_JoystickEventState(SDL_ENABLE);
 30	context->joystick = SDL_JoystickOpen(0);
 31#if !SDL_VERSION_ATLEAST(2, 0, 0)
 32	SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
 33#endif
 34	return true;
 35}
 36
 37void GBASDLInitBindings(struct GBAInputMap* inputMap) {
 38#ifdef BUILD_PANDORA
 39	GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_PAGEDOWN, GBA_KEY_A);
 40	GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_END, GBA_KEY_B);
 41	GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_RSHIFT, GBA_KEY_L);
 42	GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_RCTRL, GBA_KEY_R);
 43	GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_LALT, GBA_KEY_START);
 44	GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_LCTRL, GBA_KEY_SELECT);
 45	GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_UP, GBA_KEY_UP);
 46	GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_DOWN, GBA_KEY_DOWN);
 47	GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_LEFT, GBA_KEY_LEFT);
 48	GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_RIGHT, GBA_KEY_RIGHT);
 49#elif SDL_VERSION_ATLEAST(2, 0, 0)
 50	GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDL_SCANCODE_X, GBA_KEY_A);
 51	GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDL_SCANCODE_Z, GBA_KEY_B);
 52	GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDL_SCANCODE_A, GBA_KEY_L);
 53	GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDL_SCANCODE_S, GBA_KEY_R);
 54	GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDL_SCANCODE_RETURN, GBA_KEY_START);
 55	GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDL_SCANCODE_BACKSPACE, GBA_KEY_SELECT);
 56	GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDL_SCANCODE_UP, GBA_KEY_UP);
 57	GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDL_SCANCODE_DOWN, GBA_KEY_DOWN);
 58	GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDL_SCANCODE_LEFT, GBA_KEY_LEFT);
 59	GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDL_SCANCODE_RIGHT, GBA_KEY_RIGHT);
 60#else
 61	GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_x, GBA_KEY_A);
 62	GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_z, GBA_KEY_B);
 63	GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_a, GBA_KEY_L);
 64	GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_s, GBA_KEY_R);
 65	GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_RETURN, GBA_KEY_START);
 66	GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_BACKSPACE, GBA_KEY_SELECT);
 67	GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_UP, GBA_KEY_UP);
 68	GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_DOWN, GBA_KEY_DOWN);
 69	GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_LEFT, GBA_KEY_LEFT);
 70	GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_RIGHT, GBA_KEY_RIGHT);
 71#endif
 72
 73	GBAInputBindKey(inputMap, SDL_BINDING_BUTTON, 13, GBA_KEY_A);
 74	GBAInputBindKey(inputMap, SDL_BINDING_BUTTON, 14, GBA_KEY_B);
 75	GBAInputBindKey(inputMap, SDL_BINDING_BUTTON, 10, GBA_KEY_L);
 76	GBAInputBindKey(inputMap, SDL_BINDING_BUTTON, 11, GBA_KEY_R);
 77	GBAInputBindKey(inputMap, SDL_BINDING_BUTTON, 3, GBA_KEY_START);
 78	GBAInputBindKey(inputMap, SDL_BINDING_BUTTON, 0, GBA_KEY_SELECT);
 79	GBAInputBindKey(inputMap, SDL_BINDING_BUTTON, 4, GBA_KEY_UP);
 80	GBAInputBindKey(inputMap, SDL_BINDING_BUTTON, 6, GBA_KEY_DOWN);
 81	GBAInputBindKey(inputMap, SDL_BINDING_BUTTON, 7, GBA_KEY_LEFT);
 82	GBAInputBindKey(inputMap, SDL_BINDING_BUTTON, 5, GBA_KEY_RIGHT);
 83
 84	struct GBAAxis description = { GBA_KEY_RIGHT, GBA_KEY_LEFT, 0x4000, -0x4000 };
 85	GBAInputBindAxis(inputMap, SDL_BINDING_BUTTON, 0, &description);
 86	description = (struct GBAAxis) { GBA_KEY_DOWN, GBA_KEY_UP, 0x4000, -0x4000 };
 87	GBAInputBindAxis(inputMap, SDL_BINDING_BUTTON, 1, &description);
 88}
 89
 90void GBASDLEventsLoadConfig(struct GBASDLEvents* context, const struct Configuration* config) {
 91	GBAInputMapLoad(context->bindings, SDL_BINDING_KEY, config);
 92	if (context->joystick) {
 93		GBAInputMapLoad(context->bindings, SDL_BINDING_BUTTON, config);
 94#if SDL_VERSION_ATLEAST(2, 0, 0)
 95		GBAInputProfileLoad(context->bindings, SDL_BINDING_BUTTON, config, SDL_JoystickName(context->joystick));
 96#else
 97		GBAInputProfileLoad(context->bindings, SDL_BINDING_BUTTON, config, SDL_JoystickName(SDL_JoystickIndex(context->joystick)));
 98#endif
 99	}
100}
101
102void GBASDLDeinitEvents(struct GBASDLEvents* context) {
103	SDL_JoystickClose(context->joystick);
104
105	--_openContexts;
106	if (!_openContexts) {
107		SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
108	}
109}
110
111static void _pauseAfterFrame(struct GBAThread* context) {
112	context->frameCallback = 0;
113	GBAThreadPauseFromThread(context);
114}
115
116static void _GBASDLHandleKeypress(struct GBAThread* context, struct GBASDLEvents* sdlContext, const struct SDL_KeyboardEvent* event) {
117	enum GBAKey key = GBA_KEY_NONE;
118	if (!event->keysym.mod) {
119#if !defined(BUILD_PANDORA) && SDL_VERSION_ATLEAST(2, 0, 0)
120		key = GBAInputMapKey(sdlContext->bindings, SDL_BINDING_KEY, event->keysym.scancode);
121#else
122		key = GBAInputMapKey(sdlContext->bindings, SDL_BINDING_KEY, event->keysym.sym);
123#endif
124	}
125	if (key != GBA_KEY_NONE) {
126		if (event->type == SDL_KEYDOWN) {
127			context->activeKeys |= 1 << key;
128		} else {
129			context->activeKeys &= ~(1 << key);
130		}
131		return;
132	}
133	if (event->keysym.sym == SDLK_TAB) {
134		context->sync.audioWait = event->type != SDL_KEYDOWN;
135		return;
136	}
137	if (event->type == SDL_KEYDOWN) {
138		switch (event->keysym.sym) {
139		case SDLK_F11:
140			if (context->debugger) {
141				ARMDebuggerEnter(context->debugger, DEBUGGER_ENTER_MANUAL, 0);
142			}
143			return;
144#ifdef USE_PNG
145		case SDLK_F12:
146			GBAThreadInterrupt(context);
147			GBAThreadTakeScreenshot(context);
148			GBAThreadContinue(context);
149			return;
150#endif
151		case SDLK_BACKSLASH:
152			GBAThreadPause(context);
153			context->frameCallback = _pauseAfterFrame;
154			GBAThreadUnpause(context);
155			return;
156		case SDLK_BACKQUOTE:
157			GBAThreadInterrupt(context);
158			GBARewind(context, 10);
159			GBAThreadContinue(context);
160			return;
161		default:
162			if ((event->keysym.mod & GUI_MOD) && (event->keysym.mod & GUI_MOD) == event->keysym.mod) {
163				switch (event->keysym.sym) {
164#if SDL_VERSION_ATLEAST(2, 0, 0)
165				case SDLK_f:
166					SDL_SetWindowFullscreen(sdlContext->window, sdlContext->fullscreen ? 0 : SDL_WINDOW_FULLSCREEN_DESKTOP);
167					sdlContext->fullscreen = !sdlContext->fullscreen;
168					sdlContext->windowUpdated = 1;
169					break;
170#endif
171				case SDLK_p:
172					GBAThreadTogglePause(context);
173					break;
174				case SDLK_n:
175					GBAThreadPause(context);
176					context->frameCallback = _pauseAfterFrame;
177					GBAThreadUnpause(context);
178					break;
179				case SDLK_r:
180					GBAThreadReset(context);
181					break;
182				default:
183					break;
184				}
185			}
186			if (event->keysym.mod & KMOD_SHIFT) {
187				switch (event->keysym.sym) {
188				case SDLK_F1:
189				case SDLK_F2:
190				case SDLK_F3:
191				case SDLK_F4:
192				case SDLK_F5:
193				case SDLK_F6:
194				case SDLK_F7:
195				case SDLK_F8:
196				case SDLK_F9:
197					GBAThreadInterrupt(context);
198					GBASaveState(context, context->stateDir, event->keysym.sym - SDLK_F1 + 1, true);
199					GBAThreadContinue(context);
200					break;
201				default:
202					break;
203				}
204			} else {
205				switch (event->keysym.sym) {
206				case SDLK_F1:
207				case SDLK_F2:
208				case SDLK_F3:
209				case SDLK_F4:
210				case SDLK_F5:
211				case SDLK_F6:
212				case SDLK_F7:
213				case SDLK_F8:
214				case SDLK_F9:
215					GBAThreadInterrupt(context);
216					GBALoadState(context, context->stateDir, event->keysym.sym - SDLK_F1 + 1);
217					GBAThreadContinue(context);
218					break;
219				default:
220					break;
221				}
222			}
223			return;
224		}
225	}
226}
227
228static void _GBASDLHandleJoyButton(struct GBAThread* context, struct GBASDLEvents* sdlContext, const struct SDL_JoyButtonEvent* event) {
229	enum GBAKey key = 0;
230	key = GBAInputMapKey(sdlContext->bindings, SDL_BINDING_BUTTON, event->button);
231	if (key == GBA_KEY_NONE) {
232		return;
233	}
234
235	if (event->type == SDL_JOYBUTTONDOWN) {
236		context->activeKeys |= 1 << key;
237	} else {
238		context->activeKeys &= ~(1 << key);
239	}
240}
241
242static void _GBASDLHandleJoyHat(struct GBAThread* context, const struct SDL_JoyHatEvent* event) {
243	enum GBAKey key = 0;
244
245	if (event->value & SDL_HAT_UP) {
246		key |= 1 << GBA_KEY_UP;
247	}
248	if (event->value & SDL_HAT_LEFT) {
249		key |= 1 << GBA_KEY_LEFT;
250	}
251	if (event->value & SDL_HAT_DOWN) {
252		key |= 1 << GBA_KEY_DOWN;
253	}
254	if (event->value & SDL_HAT_RIGHT) {
255		key |= 1 << GBA_KEY_RIGHT;
256	}
257
258	context->activeKeys &= ~((1 << GBA_KEY_UP) | (1 << GBA_KEY_LEFT) | (1 << GBA_KEY_DOWN) | (1 << GBA_KEY_RIGHT));
259	context->activeKeys |= key;
260}
261
262static void _GBASDLHandleJoyAxis(struct GBAThread* context, struct GBASDLEvents* sdlContext, const struct SDL_JoyAxisEvent* event) {
263	int keys = context->activeKeys;
264
265	keys = GBAInputClearAxis(sdlContext->bindings, SDL_BINDING_BUTTON, event->axis, keys);
266	enum GBAKey key = GBAInputMapAxis(sdlContext->bindings, SDL_BINDING_BUTTON, event->axis, event->value);
267	if (key != GBA_KEY_NONE) {
268		keys |= 1 << key;
269	}
270
271	context->activeKeys = keys;
272}
273
274#if SDL_VERSION_ATLEAST(2, 0, 0)
275static void _GBASDLHandleWindowEvent(struct GBAThread* context, struct GBASDLEvents* sdlContext, const struct SDL_WindowEvent* event) {
276	UNUSED(context);
277	switch (event->event) {
278	case SDL_WINDOWEVENT_SIZE_CHANGED:
279		sdlContext->windowUpdated = 1;
280		break;
281	}
282}
283#endif
284
285void GBASDLHandleEvent(struct GBAThread* context, struct GBASDLEvents* sdlContext, const union SDL_Event* event) {
286	switch (event->type) {
287	case SDL_QUIT:
288		GBAThreadEnd(context);
289		break;
290#if SDL_VERSION_ATLEAST(2, 0, 0)
291	case SDL_WINDOWEVENT:
292		_GBASDLHandleWindowEvent(context, sdlContext, &event->window);
293		break;
294#endif
295	case SDL_KEYDOWN:
296	case SDL_KEYUP:
297		_GBASDLHandleKeypress(context, sdlContext, &event->key);
298		break;
299	case SDL_JOYBUTTONDOWN:
300	case SDL_JOYBUTTONUP:
301		_GBASDLHandleJoyButton(context, sdlContext, &event->jbutton);
302		break;
303	case SDL_JOYHATMOTION:
304		_GBASDLHandleJoyHat(context, &event->jhat);
305		break;
306	case SDL_JOYAXISMOTION:
307		_GBASDLHandleJoyAxis(context, sdlContext, &event->jaxis);
308		break;
309	}
310}