all repos — mgba @ 54ef61f715d59e50429a3b3982c7f32e56b7b8eb

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