all repos — mgba @ a95e2c06b73e9023a53d928e047e68bfd050cd2e

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