all repos — mgba @ 9873da0eb7d1b167de686a37d9e5d7902ef8f98f

mGBA Game Boy Advance Emulator

src/platform/sdl/sdl-events.c (view raw)

  1#include "sdl-events.h"
  2
  3#include "debugger/debugger.h"
  4#include "gba-io.h"
  5#include "gba-rr.h"
  6#include "gba-serialize.h"
  7#include "gba-video.h"
  8#include "renderers/video-software.h"
  9#include "util/vfs.h"
 10
 11#if SDL_VERSION_ATLEAST(2, 0, 0) && defined(__APPLE__)
 12#define GUI_MOD KMOD_GUI
 13#else
 14#define GUI_MOD KMOD_CTRL
 15#endif
 16
 17#define SDL_BINDING_KEY 0x53444C4B
 18#define SDL_BINDING_BUTTON 0x53444C42
 19
 20bool GBASDLInitEvents(struct GBASDLEvents* context) {
 21	if (SDL_InitSubSystem(SDL_INIT_JOYSTICK) < 0) {
 22		return false;
 23	}
 24	SDL_JoystickEventState(SDL_ENABLE);
 25	context->joystick = SDL_JoystickOpen(0);
 26#if !SDL_VERSION_ATLEAST(2, 0, 0)
 27	SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
 28#endif
 29
 30	GBAInputBindKey(context->bindings, SDL_BINDING_KEY, SDLK_z, GBA_KEY_A);
 31	GBAInputBindKey(context->bindings, SDL_BINDING_KEY, SDLK_x, GBA_KEY_B);
 32	GBAInputBindKey(context->bindings, SDL_BINDING_KEY, SDLK_a, GBA_KEY_L);
 33	GBAInputBindKey(context->bindings, SDL_BINDING_KEY, SDLK_s, GBA_KEY_R);
 34	GBAInputBindKey(context->bindings, SDL_BINDING_KEY, SDLK_RETURN, GBA_KEY_START);
 35	GBAInputBindKey(context->bindings, SDL_BINDING_KEY, SDLK_BACKSPACE, GBA_KEY_SELECT);
 36	GBAInputBindKey(context->bindings, SDL_BINDING_KEY, SDLK_UP, GBA_KEY_UP);
 37	GBAInputBindKey(context->bindings, SDL_BINDING_KEY, SDLK_DOWN, GBA_KEY_DOWN);
 38	GBAInputBindKey(context->bindings, SDL_BINDING_KEY, SDLK_LEFT, GBA_KEY_LEFT);
 39	GBAInputBindKey(context->bindings, SDL_BINDING_KEY, SDLK_RIGHT, GBA_KEY_RIGHT);
 40
 41	GBAInputBindKey(context->bindings, SDL_BINDING_BUTTON, 2, GBA_KEY_A);
 42	GBAInputBindKey(context->bindings, SDL_BINDING_BUTTON, 1, GBA_KEY_B);
 43	GBAInputBindKey(context->bindings, SDL_BINDING_BUTTON, 6, GBA_KEY_L);
 44	GBAInputBindKey(context->bindings, SDL_BINDING_BUTTON, 7, GBA_KEY_R);
 45	GBAInputBindKey(context->bindings, SDL_BINDING_BUTTON, 8, GBA_KEY_START);
 46	GBAInputBindKey(context->bindings, SDL_BINDING_BUTTON, 9, GBA_KEY_SELECT);
 47	return true;
 48}
 49
 50void GBASDLDeinitEvents(struct GBASDLEvents* context) {
 51	SDL_JoystickClose(context->joystick);
 52	SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
 53}
 54
 55static void _pauseAfterFrame(struct GBAThread* context) {
 56	context->frameCallback = 0;
 57	GBAThreadPauseFromThread(context);
 58}
 59
 60static void _GBASDLHandleKeypress(struct GBAThread* context, struct GBASDLEvents* sdlContext, const struct SDL_KeyboardEvent* event) {
 61	enum GBAKey key = GBA_KEY_NONE;
 62	if (!event->keysym.mod) {
 63		key = GBAInputMapKey(&context->inputMap, SDL_BINDING_KEY, event->keysym.sym);
 64	}
 65	if (key != GBA_KEY_NONE) {
 66		if (event->type == SDL_KEYDOWN) {
 67			context->activeKeys |= 1 << key;
 68		} else {
 69			context->activeKeys &= ~(1 << key);
 70		}
 71		return;
 72	}
 73	switch (event->keysym.sym) {
 74	case SDLK_F11:
 75		if (event->type == SDL_KEYDOWN && context->debugger) {
 76			ARMDebuggerEnter(context->debugger, DEBUGGER_ENTER_MANUAL);
 77		}
 78		return;
 79	case SDLK_F12:
 80		if (event->type == SDL_KEYDOWN) {
 81			GBAThreadInterrupt(context);
 82			GBAThreadTakeScreenshot(context);
 83			GBAThreadContinue(context);
 84		}
 85		return;
 86	case SDLK_TAB:
 87		context->sync.audioWait = event->type != SDL_KEYDOWN;
 88		return;
 89	case SDLK_LEFTBRACKET:
 90		GBAThreadInterrupt(context);
 91		GBARewind(context, 10);
 92		GBAThreadContinue(context);
 93		return;
 94	case SDLK_ESCAPE:
 95		GBAThreadInterrupt(context);
 96		if (context->gba->rr) {
 97			GBARRStopPlaying(context->gba->rr);
 98			GBARRStopRecording(context->gba->rr);
 99		}
100		GBAThreadContinue(context);
101		return;
102	default:
103		if (event->type == SDL_KEYDOWN) {
104			if (event->keysym.mod & GUI_MOD) {
105				switch (event->keysym.sym) {
106#if SDL_VERSION_ATLEAST(2, 0, 0)
107				case SDLK_f:
108					SDL_SetWindowFullscreen(sdlContext->window, sdlContext->fullscreen ? 0 : SDL_WINDOW_FULLSCREEN_DESKTOP);
109					sdlContext->fullscreen = !sdlContext->fullscreen;
110					sdlContext->windowUpdated = 1;
111					break;
112#endif
113				case SDLK_p:
114					GBAThreadTogglePause(context);
115					break;
116				case SDLK_n:
117					GBAThreadPause(context);
118					context->frameCallback = _pauseAfterFrame;
119					GBAThreadUnpause(context);
120					break;
121				case SDLK_r:
122					GBAThreadReset(context);
123					break;
124				case SDLK_t:
125					if (context->stateDir) {
126						GBAThreadReset(context);
127						GBAThreadInterrupt(context);
128						GBARRContextCreate(context->gba);
129						GBARRSetStream(context->gba->rr, context->stateDir);
130						GBARRStopPlaying(context->gba->rr);
131						GBARRStartRecording(context->gba->rr);
132						GBAThreadContinue(context);
133					}
134					break;
135				case SDLK_y:
136					if (context->stateDir) {
137						GBAThreadReset(context);
138						GBAThreadInterrupt(context);
139						GBARRContextCreate(context->gba);
140						GBARRSetStream(context->gba->rr, context->stateDir);
141						GBARRStopRecording(context->gba->rr);
142						GBARRStartPlaying(context->gba->rr, event->keysym.mod & KMOD_SHIFT);
143						GBAThreadContinue(context);
144					}
145					break;
146				default:
147					break;
148				}
149			}
150			if (event->keysym.mod & KMOD_SHIFT) {
151				switch (event->keysym.sym) {
152				case SDLK_F1:
153				case SDLK_F2:
154				case SDLK_F3:
155				case SDLK_F4:
156				case SDLK_F5:
157				case SDLK_F6:
158				case SDLK_F7:
159				case SDLK_F8:
160				case SDLK_F9:
161				case SDLK_F10:
162					GBAThreadInterrupt(context);
163					GBASaveState(context->gba, context->stateDir, event->keysym.sym - SDLK_F1, true);
164					GBAThreadContinue(context);
165					break;
166				default:
167					break;
168				}
169			} else {
170				switch (event->keysym.sym) {
171				case SDLK_F1:
172				case SDLK_F2:
173				case SDLK_F3:
174				case SDLK_F4:
175				case SDLK_F5:
176				case SDLK_F6:
177				case SDLK_F7:
178				case SDLK_F8:
179				case SDLK_F9:
180				case SDLK_F10:
181					GBAThreadInterrupt(context);
182					GBALoadState(context->gba, context->stateDir, event->keysym.sym - SDLK_F1);
183					GBAThreadContinue(context);
184					break;
185				default:
186					break;
187				}
188			}
189		}
190		return;
191	}
192}
193
194static void _GBASDLHandleJoyButton(struct GBAThread* context, const struct SDL_JoyButtonEvent* event) {
195	enum GBAKey key = 0;
196	key = GBAInputMapKey(&context->inputMap, SDL_BINDING_BUTTON, event->button);
197	if (key == GBA_KEY_NONE) {
198		return;
199	}
200
201	if (event->type == SDL_JOYBUTTONDOWN) {
202		context->activeKeys |= 1 << key;
203	} else {
204		context->activeKeys &= ~(1 << key);
205	}
206}
207
208static void _GBASDLHandleJoyHat(struct GBAThread* context, const struct SDL_JoyHatEvent* event) {
209	enum GBAKey key = 0;
210
211	if (event->value & SDL_HAT_UP) {
212		key |= 1 << GBA_KEY_UP;
213	}
214	if (event->value & SDL_HAT_LEFT) {
215		key |= 1 << GBA_KEY_LEFT;
216	}
217	if (event->value & SDL_HAT_DOWN) {
218		key |= 1 << GBA_KEY_DOWN;
219	}
220	if (event->value & SDL_HAT_RIGHT) {
221		key |= 1 << GBA_KEY_RIGHT;
222	}
223
224	context->activeKeys &= ~((1 << GBA_KEY_UP) | (1 << GBA_KEY_LEFT) | (1 << GBA_KEY_DOWN) | (1 << GBA_KEY_RIGHT));
225	context->activeKeys |= key;
226}
227
228#if SDL_VERSION_ATLEAST(2, 0, 0)
229static void _GBASDLHandleWindowEvent(struct GBAThread* context, struct GBASDLEvents* sdlContext, const struct SDL_WindowEvent* event) {
230	UNUSED(context);
231	switch (event->event) {
232	case SDL_WINDOWEVENT_SIZE_CHANGED:
233		sdlContext->windowUpdated = 1;
234		break;
235	}
236}
237#endif
238
239void GBASDLHandleEvent(struct GBAThread* context, struct GBASDLEvents* sdlContext, const union SDL_Event* event) {
240	switch (event->type) {
241	case SDL_QUIT:
242		GBAThreadEnd(context);
243		break;
244#if SDL_VERSION_ATLEAST(2, 0, 0)
245	case SDL_WINDOWEVENT:
246		_GBASDLHandleWindowEvent(context, sdlContext, &event->window);
247		break;
248#endif
249	case SDL_KEYDOWN:
250	case SDL_KEYUP:
251		_GBASDLHandleKeypress(context, sdlContext, &event->key);
252		break;
253	case SDL_JOYBUTTONDOWN:
254	case SDL_JOYBUTTONUP:
255		_GBASDLHandleJoyButton(context, &event->jbutton);
256		break;
257	case SDL_JOYHATMOTION:
258		_GBASDLHandleJoyHat(context, &event->jhat);
259	}
260}